1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2015 Rafał Lużyński <digitalfreak@lingonborough.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "config.h"
#include <glib/gi18n.h>
#include "gs-hiding-box.h"
enum {
PROP_0,
PROP_SPACING
};
struct _GsHidingBox
{
GtkContainer parent_instance;
GList *children;
gint spacing;
};
static void
gs_hiding_box_buildable_add_child (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *type)
{
if (!type)
gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
else
GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GS_HIDING_BOX (buildable), type);
}
static void
gs_hiding_box_buildable_init (GtkBuildableIface *iface)
{
iface->add_child = gs_hiding_box_buildable_add_child;
}
G_DEFINE_TYPE_WITH_CODE (GsHidingBox, gs_hiding_box, GTK_TYPE_CONTAINER,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gs_hiding_box_buildable_init))
static void
gs_hiding_box_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GsHidingBox *box = GS_HIDING_BOX (object);
switch (prop_id) {
case PROP_SPACING:
gs_hiding_box_set_spacing (box, g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gs_hiding_box_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GsHidingBox *box = GS_HIDING_BOX (object);
switch (prop_id) {
case PROP_SPACING:
g_value_set_int (value, box->spacing);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gs_hiding_box_add (GtkContainer *container, GtkWidget *widget)
{
GsHidingBox *box = GS_HIDING_BOX (container);
box->children = g_list_append (box->children, widget);
gtk_widget_set_parent (widget, GTK_WIDGET (box));
}
static void
gs_hiding_box_remove (GtkContainer *container, GtkWidget *widget)
{
GList *child;
GsHidingBox *box = GS_HIDING_BOX (container);
for (child = box->children; child != NULL; child = child->next) {
if (child->data == widget) {
gboolean was_visible = gtk_widget_get_visible (widget) &&
gtk_widget_get_child_visible (widget);
gtk_widget_unparent (widget);
box->children = g_list_delete_link (box->children, child);
if (was_visible)
gtk_widget_queue_resize (GTK_WIDGET (container));
break;
}
}
}
static void
gs_hiding_box_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
GsHidingBox *box = GS_HIDING_BOX (container);
GtkWidget *child;
GList *children;
children = box->children;
while (children) {
child = children->data;
children = children->next;
(* callback) (child, callback_data);
}
}
static void
gs_hiding_box_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
GsHidingBox *box = GS_HIDING_BOX (widget);
gint nvis_children = 0;
GtkTextDirection direction;
GtkAllocation child_allocation;
GtkRequestedSize *sizes;
gint size;
gint extra = 0;
gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
gint x = 0, i;
GList *child;
GtkWidget *child_widget;
gint spacing = box->spacing;
gint children_size;
GtkAllocation clip, child_clip;
gtk_widget_set_allocation (widget, allocation);
for (child = box->children; child != NULL; child = child->next) {
if (gtk_widget_get_visible (child->data))
++nvis_children;
}
/* If there is no visible child, simply return. */
if (nvis_children == 0)
return;
direction = gtk_widget_get_direction (widget);
sizes = g_newa (GtkRequestedSize, nvis_children);
size = allocation->width;
children_size = -spacing;
/* Retrieve desired size for visible children. */
for (i = 0, child = box->children; child != NULL; child = child->next) {
child_widget = GTK_WIDGET (child->data);
if (!gtk_widget_get_visible (child_widget))
continue;
gtk_widget_get_preferred_width_for_height (child_widget,
allocation->height,
&sizes[i].minimum_size,
&sizes[i].natural_size);
/* Assert the api is working properly */
if (sizes[i].minimum_size < 0)
g_error ("GsHidingBox child %s minimum width: %d < 0 for height %d",
gtk_widget_get_name (child_widget),
sizes[i].minimum_size, allocation->height);
if (sizes[i].natural_size < sizes[i].minimum_size)
g_error ("GsHidingBox child %s natural width: %d < minimum %d for height %d",
gtk_widget_get_name (child_widget),
sizes[i].natural_size, sizes[i].minimum_size,
allocation->height);
children_size += sizes[i].minimum_size + spacing;
if (i > 0 && children_size > allocation->width)
break;
size -= sizes[i].minimum_size;
sizes[i].data = child_widget;
i++;
}
nvis_children = i;
/* Bring children up to size first */
size = gtk_distribute_natural_allocation (MAX (0, size), (guint) nvis_children, sizes);
/* Only now we can subtract the spacings */
size -= (nvis_children - 1) * spacing;
if (nvis_children > 1) {
extra = size / nvis_children;
n_extra_widgets = size % nvis_children;
}
x = allocation->x;
for (i = 0, child = box->children; child != NULL; child = child->next) {
child_widget = GTK_WIDGET (child->data);
if (!gtk_widget_get_visible (child_widget))
continue;
/* Hide the overflowing children even if they have visible=TRUE */
if (i >= nvis_children) {
while (child) {
gtk_widget_set_child_visible (child->data, FALSE);
child = child->next;
}
break;
}
child_allocation.x = x;
child_allocation.y = allocation->y;
child_allocation.width = sizes[i].minimum_size + extra;
child_allocation.height = allocation->height;
if (n_extra_widgets) {
++child_allocation.width;
--n_extra_widgets;
}
if (direction == GTK_TEXT_DIR_RTL) {
child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
}
/* Let this child be visible */
gtk_widget_set_child_visible (child_widget, TRUE);
gtk_widget_size_allocate (child_widget, &child_allocation);
x += child_allocation.width + spacing;
++i;
}
/*
* The code below is inspired by _gtk_widget_set_simple_clip.
* Note: Here we ignore the "box-shadow" CSS property of the
* hiding box because we don't use it.
*/
clip = *allocation;
if (gtk_widget_get_has_window (widget)) {
clip.x = clip.y = 0;
}
for (child = box->children; child != NULL; child = child->next) {
child_widget = GTK_WIDGET (child->data);
if (gtk_widget_get_visible (child_widget) &&
gtk_widget_get_child_visible (child_widget)) {
gtk_widget_get_clip (child_widget, &child_clip);
gdk_rectangle_union (&child_clip, &clip, &clip);
}
}
if (gtk_widget_get_has_window (widget)) {
clip.x += allocation->x;
clip.y += allocation->y;
}
gtk_widget_set_clip (widget, &clip);
}
static void
gs_hiding_box_get_preferred_width (GtkWidget *widget, gint *min, gint *nat)
{
GsHidingBox *box = GS_HIDING_BOX (widget);
gint cm, cn;
gint m, n;
GList *child;
gint nvis_children;
gboolean have_min = FALSE;
m = n = nvis_children = 0;
for (child = box->children; child != NULL; child = child->next) {
if (!gtk_widget_is_visible (child->data))
continue;
++nvis_children;
gtk_widget_get_preferred_width (child->data, &cm, &cn);
/* Minimum is a minimum of the first visible child */
if (!have_min) {
m = cm;
have_min = TRUE;
}
/* Natural is a sum of all visible children */
n += cn;
}
/* Natural must also include the spacing */
if (box->spacing && nvis_children > 1)
n += box->spacing * (nvis_children - 1);
if (min)
*min = m;
if (nat)
*nat = n;
}
static void
gs_hiding_box_get_preferred_height (GtkWidget *widget, gint *min, gint *nat)
{
gint m, n;
gint cm, cn;
GList *child;
GsHidingBox *box = GS_HIDING_BOX (widget);
m = n = 0;
for (child = box->children; child != NULL; child = child->next) {
if (!gtk_widget_is_visible (child->data))
continue;
gtk_widget_get_preferred_height (child->data, &cm, &cn);
m = MAX (m, cm);
n = MAX (n, cn);
}
if (min)
*min = m;
if (nat)
*nat = n;
}
static void
gs_hiding_box_init (GsHidingBox *box)
{
gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
box->spacing = 0;
}
static void
gs_hiding_box_class_init (GsHidingBoxClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
object_class->set_property = gs_hiding_box_set_property;
object_class->get_property = gs_hiding_box_get_property;
widget_class->size_allocate = gs_hiding_box_size_allocate;
widget_class->get_preferred_width = gs_hiding_box_get_preferred_width;
widget_class->get_preferred_height = gs_hiding_box_get_preferred_height;
container_class->add = gs_hiding_box_add;
container_class->remove = gs_hiding_box_remove;
container_class->forall = gs_hiding_box_forall;
g_object_class_install_property (object_class,
PROP_SPACING,
g_param_spec_int ("spacing",
"Spacing",
"The amount of space between children",
0, G_MAXINT, 0,
G_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
}
/**
* gs_hiding_box_new:
*
* Creates a new #GsHidingBox.
*
* Returns: a new #GsHidingBox.
**/
GtkWidget *
gs_hiding_box_new (void)
{
return g_object_new (GS_TYPE_HIDING_BOX, NULL);
}
/**
* gs_hiding_box_set_spacing:
* @box: a #GsHidingBox
* @spacing: the number of pixels to put between children
*
* Sets the #GsHidingBox:spacing property of @box, which is the
* number of pixels to place between children of @box.
*/
void
gs_hiding_box_set_spacing (GsHidingBox *box, gint spacing)
{
g_return_if_fail (GS_IS_HIDING_BOX (box));
if (box->spacing != spacing) {
box->spacing = spacing;
g_object_notify (G_OBJECT (box), "spacing");
gtk_widget_queue_resize (GTK_WIDGET (box));
}
}
/**
* gs_hiding_box_get_spacing:
* @box: a #GsHidingBox
*
* Gets the value set by gs_hiding_box_set_spacing().
*
* Returns: spacing between children
**/
gint
gs_hiding_box_get_spacing (GsHidingBox *box)
{
g_return_val_if_fail (GS_IS_HIDING_BOX (box), 0);
return box->spacing;
}
|