1
/* GTK - The GIMP Toolkit
2
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4
* GtkHWrapBox: Horizontal wrapping box widget
5
* Copyright (C) 1999 Tim Janik
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
25
#include "gtkhwrapbox.h"
27
#include "libgimpmath/gimpmath.h"
30
/* --- prototypes --- */
31
static void gtk_hwrap_box_class_init (GtkHWrapBoxClass *klass);
32
static void gtk_hwrap_box_init (GtkHWrapBox *hwbox);
33
static void gtk_hwrap_box_size_request (GtkWidget *widget,
34
GtkRequisition *requisition);
35
static void gtk_hwrap_box_size_allocate (GtkWidget *widget,
36
GtkAllocation *allocation);
37
static GSList* reverse_list_row_children (GtkWrapBox *wbox,
38
GtkWrapBoxChild **child_p,
41
gboolean *can_vexpand);
44
/* --- variables --- */
45
static gpointer parent_class = NULL;
48
/* --- functions --- */
50
gtk_hwrap_box_get_type (void)
52
static GType hwrap_box_type = 0;
56
static const GTypeInfo hwrap_box_info =
58
sizeof (GtkHWrapBoxClass),
60
NULL, /* base_finalize */
61
(GClassInitFunc) gtk_hwrap_box_class_init,
62
NULL, /* class_finalize */
63
NULL, /* class_data */
66
(GInstanceInitFunc) gtk_hwrap_box_init,
69
hwrap_box_type = g_type_register_static (GTK_TYPE_WRAP_BOX, "GtkHWrapBox",
73
return hwrap_box_type;
77
gtk_hwrap_box_class_init (GtkHWrapBoxClass *class)
79
GObjectClass *object_class;
80
GtkWidgetClass *widget_class;
81
GtkContainerClass *container_class;
82
GtkWrapBoxClass *wrap_box_class;
84
object_class = G_OBJECT_CLASS (class);
85
widget_class = GTK_WIDGET_CLASS (class);
86
container_class = GTK_CONTAINER_CLASS (class);
87
wrap_box_class = GTK_WRAP_BOX_CLASS (class);
89
parent_class = g_type_class_peek_parent (class);
91
widget_class->size_request = gtk_hwrap_box_size_request;
92
widget_class->size_allocate = gtk_hwrap_box_size_allocate;
94
wrap_box_class->rlist_line_children = reverse_list_row_children;
98
gtk_hwrap_box_init (GtkHWrapBox *hwbox)
100
hwbox->max_child_width = 0;
101
hwbox->max_child_height = 0;
105
gtk_hwrap_box_new (gboolean homogeneous)
107
return g_object_new (GTK_TYPE_HWRAP_BOX, "homogeneous", homogeneous, NULL);
111
get_child_requisition (GtkWrapBox *wbox,
113
GtkRequisition *child_requisition)
115
if (wbox->homogeneous)
117
GtkHWrapBox *hwbox = GTK_HWRAP_BOX (wbox);
119
child_requisition->width = hwbox->max_child_width;
120
child_requisition->height = hwbox->max_child_height;
123
gtk_widget_get_child_requisition (child, child_requisition);
127
get_layout_size (GtkHWrapBox *this,
131
GtkWrapBox *wbox = GTK_WRAP_BOX (this);
132
GtkWrapBoxChild *child;
133
guint n_rows, left_over = 0, total_height = 0;
134
gboolean last_row_filled = TRUE;
136
*width_inc = this->max_child_width + 1;
139
for (child = wbox->children; child; child = child->next)
141
GtkWrapBoxChild *row_child;
142
GtkRequisition child_requisition;
143
guint row_width, row_height, n = 1;
145
if (!GTK_WIDGET_VISIBLE (child->widget))
148
get_child_requisition (wbox, child->widget, &child_requisition);
149
if (!last_row_filled)
150
*width_inc = MIN (*width_inc, child_requisition.width - left_over);
151
row_width = child_requisition.width;
152
row_height = child_requisition.height;
153
for (row_child = child->next; row_child && n < wbox->child_limit; row_child = row_child->next)
155
if (GTK_WIDGET_VISIBLE (row_child->widget))
157
get_child_requisition (wbox, row_child->widget, &child_requisition);
158
if (row_width + wbox->hspacing + child_requisition.width > max_width)
160
row_width += wbox->hspacing + child_requisition.width;
161
row_height = MAX (row_height, child_requisition.height);
166
last_row_filled = n >= wbox->child_limit;
167
left_over = last_row_filled ? 0 : max_width - (row_width + wbox->hspacing);
168
total_height += (n_rows ? wbox->vspacing : 0) + row_height;
172
if (*width_inc > this->max_child_width)
175
return MAX (total_height, 1);
179
gtk_hwrap_box_size_request (GtkWidget *widget,
180
GtkRequisition *requisition)
182
GtkHWrapBox *this = GTK_HWRAP_BOX (widget);
183
GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
184
GtkWrapBoxChild *child;
185
gfloat ratio_dist, layout_width = 0;
188
g_return_if_fail (requisition != NULL);
190
requisition->width = 0;
191
requisition->height = 0;
192
this->max_child_width = 0;
193
this->max_child_height = 0;
195
/* size_request all children */
196
for (child = wbox->children; child; child = child->next)
197
if (GTK_WIDGET_VISIBLE (child->widget))
199
GtkRequisition child_requisition;
201
gtk_widget_size_request (child->widget, &child_requisition);
203
this->max_child_width = MAX (this->max_child_width, child_requisition.width);
204
this->max_child_height = MAX (this->max_child_height, child_requisition.height);
207
/* figure all possible layouts */
209
layout_width = this->max_child_width;
212
gfloat layout_height;
215
layout_width += row_inc;
216
layout_height = get_layout_size (this, layout_width, &row_inc);
217
ratio = layout_width / layout_height; /*<h2v-skip>*/
218
dist = MAX (ratio, wbox->aspect_ratio) - MIN (ratio, wbox->aspect_ratio);
219
if (dist < ratio_dist)
222
requisition->width = layout_width;
223
requisition->height = layout_height;
226
/* g_print ("ratio for width %d height %d = %f\n",
228
(gint) layout_height,
234
requisition->width += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
235
requisition->height += GTK_CONTAINER (wbox)->border_width * 2; /*<h2v-skip>*/
236
/* g_print ("choosen: width %d, height %d\n",
238
requisition->height);
243
reverse_list_row_children (GtkWrapBox *wbox,
244
GtkWrapBoxChild **child_p,
246
guint *max_child_size,
247
gboolean *expand_line)
249
GSList *slist = NULL;
250
guint width = 0, row_width = area->width;
251
GtkWrapBoxChild *child = *child_p;
254
*expand_line = FALSE;
256
while (child && !GTK_WIDGET_VISIBLE (child->widget))
258
*child_p = child->next;
264
GtkRequisition child_requisition;
267
get_child_requisition (wbox, child->widget, &child_requisition);
268
width += child_requisition.width;
269
*max_child_size = MAX (*max_child_size, child_requisition.height);
270
*expand_line |= child->vexpand;
271
slist = g_slist_prepend (slist, child);
272
*child_p = child->next;
275
while (child && n < wbox->child_limit)
277
if (GTK_WIDGET_VISIBLE (child->widget))
279
get_child_requisition (wbox, child->widget, &child_requisition);
280
if (width + wbox->hspacing + child_requisition.width > row_width ||
283
width += wbox->hspacing + child_requisition.width;
284
*max_child_size = MAX (*max_child_size, child_requisition.height);
285
*expand_line |= child->vexpand;
286
slist = g_slist_prepend (slist, child);
289
*child_p = child->next;
298
layout_row (GtkWrapBox *wbox,
301
guint children_per_line,
305
guint n_children = 0, n_expand_children = 0, have_expand_children = 0;
306
gint total_width = 0;
307
gfloat x, width, extra;
308
GtkAllocation child_allocation;
310
for (slist = children; slist; slist = slist->next)
312
GtkWrapBoxChild *child = slist->data;
313
GtkRequisition child_requisition;
319
get_child_requisition (wbox, child->widget, &child_requisition);
320
total_width += child_requisition.width;
323
width = MAX (1, area->width - (n_children - 1) * wbox->hspacing);
324
if (width > total_width)
325
extra = width - total_width;
328
have_expand_children = n_expand_children && extra;
331
if (wbox->homogeneous)
333
width = MAX (1, area->width - (children_per_line - 1) * wbox->hspacing);
334
width /= ((gdouble) children_per_line);
337
else if (have_expand_children && wbox->justify != GTK_JUSTIFY_FILL)
340
extra /= ((gdouble) n_expand_children);
344
if (wbox->justify == GTK_JUSTIFY_FILL)
347
have_expand_children = TRUE;
348
n_expand_children = n_children;
349
extra /= ((gdouble) n_expand_children);
351
else if (wbox->justify == GTK_JUSTIFY_CENTER)
357
else if (wbox->justify == GTK_JUSTIFY_LEFT)
362
else if (wbox->justify == GTK_JUSTIFY_RIGHT)
371
for (slist = children; slist; slist = slist->next)
373
GtkWrapBoxChild *child = slist->data;
375
child_allocation.x = x;
376
child_allocation.y = area->y;
377
if (wbox->homogeneous)
379
child_allocation.height = area->height;
380
child_allocation.width = width;
381
x += child_allocation.width + wbox->hspacing;
385
GtkRequisition child_requisition;
387
get_child_requisition (wbox, child->widget, &child_requisition);
389
if (child_requisition.height >= area->height)
390
child_allocation.height = area->height;
393
child_allocation.height = child_requisition.height;
394
if (wbox->line_justify == GTK_JUSTIFY_FILL || child->vfill)
395
child_allocation.height = area->height;
396
else if (child->vexpand || wbox->line_justify == GTK_JUSTIFY_CENTER)
397
child_allocation.y += (area->height - child_requisition.height) / 2;
398
else if (wbox->line_justify == GTK_JUSTIFY_BOTTOM)
399
child_allocation.y += area->height - child_requisition.height;
402
if (have_expand_children)
404
child_allocation.width = child_requisition.width;
405
if (child->hexpand || wbox->justify == GTK_JUSTIFY_FILL)
410
space = extra * n_expand_children;
411
space = width - space;
414
child_allocation.width += space;
417
child_allocation.x += space / 2;
424
/* g_print ("child_allocation.x %d += %d * %f ",
425
child_allocation.x, n_children, extra); */
426
child_allocation.x += n_children * extra;
427
/* g_print ("= %d\n",
428
child_allocation.x); */
429
child_allocation.width = MIN (child_requisition.width,
430
area->width - child_allocation.x + area->x);
434
x += child_allocation.width + wbox->hspacing;
435
gtk_widget_size_allocate (child->widget, &child_allocation);
440
typedef struct _Line Line;
450
layout_rows (GtkWrapBox *wbox,
453
GtkWrapBoxChild *next_child;
457
Line *line_list = NULL;
458
guint total_height = 0, n_expand_lines = 0, n_lines = 0;
459
gfloat shrink_height;
460
guint children_per_line;
462
next_child = wbox->children;
463
slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
468
slist = g_slist_reverse (slist);
470
children_per_line = g_slist_length (slist);
473
Line *line = g_new (Line, 1);
475
line->children = slist;
476
line->min_size = min_height;
477
total_height += min_height;
478
line->expand = vexpand;
481
line->next = line_list;
485
slist = GTK_WRAP_BOX_GET_CLASS (wbox)->rlist_line_children (wbox,
490
slist = g_slist_reverse (slist);
493
if (total_height > area->height)
494
shrink_height = total_height - area->height;
498
if (1) /* reverse lines and shrink */
500
Line *prev = NULL, *last = NULL;
501
gfloat n_shrink_lines = n_lines;
505
Line *tmp = line_list->next;
509
Line *line = line_list;
510
guint shrink_fract = shrink_height / n_shrink_lines + 0.5;
512
if (line->min_size > shrink_fract)
514
shrink_height -= shrink_fract;
515
line->min_size -= shrink_fract;
519
shrink_height -= line->min_size - 1;
526
line_list->next = prev;
536
gfloat y, height, extra = 0;
538
height = area->height;
539
height = MAX (n_lines, height - (n_lines - 1) * wbox->vspacing);
541
if (wbox->homogeneous)
542
height /= ((gdouble) n_lines);
543
else if (n_expand_lines)
545
height = MAX (0, height - total_height);
546
extra = height / ((gdouble) n_expand_lines);
555
GtkAllocation row_allocation;
556
Line *next_line = line->next;
558
row_allocation.x = area->x;
559
row_allocation.width = area->width;
560
if (wbox->homogeneous)
561
row_allocation.height = height;
564
row_allocation.height = line->min_size;
567
row_allocation.height += extra;
570
row_allocation.y = y;
572
y += row_allocation.height + wbox->vspacing;
579
g_slist_free (line->children);
587
gtk_hwrap_box_size_allocate (GtkWidget *widget,
588
GtkAllocation *allocation)
590
GtkWrapBox *wbox = GTK_WRAP_BOX (widget);
592
gint border = GTK_CONTAINER (wbox)->border_width; /*<h2v-skip>*/
594
widget->allocation = *allocation;
595
area.x = allocation->x + border;
596
area.y = allocation->y + border;
597
area.width = MAX (1, (gint) allocation->width - border * 2);
598
area.height = MAX (1, (gint) allocation->height - border * 2);
601
/* g_print ("got: width %d, height %d\n",
607
layout_rows (wbox, &area);