1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: Base class for box containers
31 * @See_also:i #GtkHBox, #GtkVBox, #GtkFrame, #GtkTable, #GtkLayout
33 * GtkBox is an abstract widget which encapsulates functionality for a
34 * particular kind of container, one that organizes a variable number of
35 * widgets into a rectangular area. GtkBox has a number of derived
36 * classes, e.g. #GtkHBox and #GtkVBox.
38 * The rectangular area of a GtkBox is organized into either a single row
39 * or a single column of child widgets depending upon whether the box is
40 * of type #GtkHBox or #GtkVBox, respectively. Thus, all children of a
41 * GtkBox are allocated one dimension in common, which is the height of a
42 * row, or the width of a column.
44 * GtkBox uses a notion of <emphasis>packing</emphasis>. Packing refers to
45 * adding widgets with reference to a particular position in a
46 * #GtkContainer. For a GtkBox, there are two reference positions: the
47 * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
48 * For a #GtkVBox, the start is defined as the top of the box and the end is
49 * defined as the bottom. For a #GtkHBox the start is defined as the
50 * left side and the end is defined as the right side.
52 * Use repeated calls to gtk_box_pack_start() to pack widgets into a
53 * GtkBox from start to end. Use gtk_box_pack_end() to add widgets from
54 * end to start. You may intersperse these calls and add widgets from
55 * both ends of the same GtkBox.
57 * Use gtk_box_pack_start_defaults() or gtk_box_pack_end_defaults()
58 * to pack widgets into a GtkBox if you do not need to specify the
59 * #GtkBox:expand, #GtkBox:fill, or #GtkBox:padding child properties
60 * for the child to be added.
62 * Because GtkBox is a #GtkContainer, you may also use
63 * gtk_container_add() to insert widgets into the box, and they will be
64 * packed as if with gtk_box_pack_start_defaults(). Use
65 * gtk_container_remove() to remove widgets from the GtkBox.
67 * Use gtk_box_set_homogeneous() to specify whether or not all children
68 * of the GtkBox are forced to get the same amount of space.
70 * Use gtk_box_set_spacing() to determine how much space will be
71 * minimally placed between all children in the GtkBox.
73 * Use gtk_box_reorder_child() to move a GtkBox child to a different
76 * Use gtk_box_set_child_packing() to reset the #GtkBox:expand,
77 * #GtkBox:fill and #GtkBox:padding child properties.
78 * Use gtk_box_query_child_packing() to query these fields.
84 #include "gtkorientable.h"
85 #include "gtkextendedlayout.h"
86 #include "gtkprivate.h"
102 CHILD_PROP_PACK_TYPE,
107 typedef struct _GtkBoxPrivate GtkBoxPrivate;
109 struct _GtkBoxPrivate
111 GtkOrientation orientation;
112 guint default_expand : 1;
113 guint spacing_set : 1;
116 #define GTK_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_BOX, GtkBoxPrivate))
118 typedef struct _GtkBoxDesiredSizes GtkBoxDesiredSizes;
119 typedef struct _GtkBoxSpreading GtkBoxSpreading;
121 struct _GtkBoxDesiredSizes
127 struct _GtkBoxSpreading
133 static void gtk_box_size_allocate (GtkWidget *widget,
134 GtkAllocation *allocation);
136 static void gtk_box_set_property (GObject *object,
140 static void gtk_box_get_property (GObject *object,
145 static void gtk_box_add (GtkContainer *container,
147 static void gtk_box_remove (GtkContainer *container,
149 static void gtk_box_forall (GtkContainer *container,
150 gboolean include_internals,
151 GtkCallback callback,
152 gpointer callback_data);
153 static void gtk_box_set_child_property (GtkContainer *container,
158 static void gtk_box_get_child_property (GtkContainer *container,
163 static GType gtk_box_child_type (GtkContainer *container);
166 static void gtk_box_extended_layout_init (GtkExtendedLayoutIface *iface);
167 static gboolean gtk_box_is_height_for_width (GtkExtendedLayout *layout);
168 static void gtk_box_get_desired_width (GtkExtendedLayout *layout,
171 static void gtk_box_get_desired_height (GtkExtendedLayout *layout,
174 static void gtk_box_get_width_for_height (GtkExtendedLayout *layout,
177 gint *natural_width);
178 static void gtk_box_get_height_for_width (GtkExtendedLayout *layout,
180 gint *minimum_height,
181 gint *natural_height);
183 static GtkExtendedLayoutIface *parent_extended_layout_iface;
185 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
186 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
188 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
189 gtk_box_extended_layout_init));
192 gtk_box_class_init (GtkBoxClass *class)
194 GObjectClass *object_class = G_OBJECT_CLASS (class);
195 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
196 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
198 object_class->set_property = gtk_box_set_property;
199 object_class->get_property = gtk_box_get_property;
201 widget_class->size_allocate = gtk_box_size_allocate;
203 container_class->add = gtk_box_add;
204 container_class->remove = gtk_box_remove;
205 container_class->forall = gtk_box_forall;
206 container_class->child_type = gtk_box_child_type;
207 container_class->set_child_property = gtk_box_set_child_property;
208 container_class->get_child_property = gtk_box_get_child_property;
210 g_object_class_override_property (object_class,
214 g_object_class_install_property (object_class,
216 g_param_spec_int ("spacing",
218 P_("The amount of space between children"),
222 GTK_PARAM_READWRITE));
224 g_object_class_install_property (object_class,
226 g_param_spec_boolean ("homogeneous",
228 P_("Whether the children should all be the same size"),
230 GTK_PARAM_READWRITE));
232 gtk_container_class_install_child_property (container_class,
234 g_param_spec_boolean ("expand",
236 P_("Whether the child should receive extra space when the parent grows"),
238 GTK_PARAM_READWRITE));
239 gtk_container_class_install_child_property (container_class,
241 g_param_spec_boolean ("fill",
243 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
245 GTK_PARAM_READWRITE));
246 gtk_container_class_install_child_property (container_class,
248 g_param_spec_uint ("padding",
250 P_("Extra space to put between the child and its neighbors, in pixels"),
252 GTK_PARAM_READWRITE));
253 gtk_container_class_install_child_property (container_class,
254 CHILD_PROP_PACK_TYPE,
255 g_param_spec_enum ("pack-type",
257 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
258 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
259 GTK_PARAM_READWRITE));
260 gtk_container_class_install_child_property (container_class,
262 g_param_spec_int ("position",
264 P_("The index of the child in the parent"),
266 GTK_PARAM_READWRITE));
268 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
272 gtk_box_init (GtkBox *box)
274 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
276 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
277 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
279 box->children = NULL;
281 box->homogeneous = FALSE;
283 private->orientation = GTK_ORIENTATION_HORIZONTAL;
284 private->default_expand = FALSE;
285 private->spacing_set = FALSE;
289 gtk_box_set_property (GObject *object,
294 GtkBox *box = GTK_BOX (object);
295 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
299 case PROP_ORIENTATION:
300 private->orientation = g_value_get_enum (value);
301 gtk_widget_queue_resize (GTK_WIDGET (box));
304 gtk_box_set_spacing (box, g_value_get_int (value));
306 case PROP_HOMOGENEOUS:
307 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gtk_box_get_property (GObject *object,
321 GtkBox *box = GTK_BOX (object);
322 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
326 case PROP_ORIENTATION:
327 g_value_set_enum (value, private->orientation);
330 g_value_set_int (value, box->spacing);
332 case PROP_HOMOGENEOUS:
333 g_value_set_boolean (value, box->homogeneous);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 count_expand_children (GtkBox *box, gint *visible_children, gint *expand_children)
348 *visible_children = *expand_children = 0;
350 for (children = box->children; children; children = children->next)
352 child = children->data;
354 if (gtk_widget_get_visible (child->widget))
356 *visible_children += 1;
358 *expand_children += 1;
364 gtk_box_compare_gap (gconstpointer p1,
368 GtkBoxDesiredSizes *sizes = data;
369 const GtkBoxSpreading *c1 = p1;
370 const GtkBoxSpreading *c2 = p2;
372 const gint d1 = MAX (sizes[c1->index].natural_size -
373 sizes[c1->index].minimum_size,
375 const gint d2 = MAX (sizes[c2->index].natural_size -
376 sizes[c2->index].minimum_size,
379 gint delta = (d2 - d1);
382 delta = (c2->index - c1->index);
388 gtk_box_size_allocate (GtkWidget *widget,
389 GtkAllocation *allocation)
391 GtkBox *box = GTK_BOX (widget);
392 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
396 gint nexpand_children;
398 widget->allocation = *allocation;
400 count_expand_children (box, &nvis_children, &nexpand_children);
402 if (nvis_children > 0)
404 gint border_width = GTK_CONTAINER (box)->border_width;
405 GtkTextDirection direction = gtk_widget_get_direction (widget);
406 GtkAllocation child_allocation;
407 GtkBoxSpreading *spreading = g_newa (GtkBoxSpreading, nvis_children);
408 GtkBoxDesiredSizes *sizes = g_newa (GtkBoxDesiredSizes, nvis_children);
417 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
418 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
420 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
422 /* Retrieve desired size for visible children */
424 children = box->children;
427 child = children->data;
428 children = children->next;
430 if (gtk_widget_get_visible (child->widget))
432 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
433 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
435 &sizes[i].minimum_size,
436 &sizes[i].natural_size);
438 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
440 &sizes[i].minimum_size,
441 &sizes[i].natural_size);
444 /* Assert the api is working properly */
445 if (sizes[i].minimum_size < 0)
446 g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
447 gtk_widget_get_name (GTK_WIDGET (child->widget)),
448 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
449 sizes[i].minimum_size,
450 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
451 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
453 if (sizes[i].natural_size < sizes[i].minimum_size)
454 g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
455 gtk_widget_get_name (GTK_WIDGET (child->widget)),
456 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
457 sizes[i].natural_size,
458 sizes[i].minimum_size,
459 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
460 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
462 size -= sizes[i].minimum_size;
463 size -= child->padding * 2;
465 spreading[i].index = i;
466 spreading[i].child = child;
472 if (box->homogeneous)
474 /* If were homogenous we still need to run the above loop to get the minimum sizes
475 * for children that are not going to fill
477 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
478 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
480 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
482 extra = size / nvis_children;
487 /* Distribute the container's extra space c_gap. We want to assign
488 * this space such that the sum of extra space assigned to children
489 * (c^i_gap) is equal to c_cap. The case that there's not enough
490 * space for all children to take their natural size needs some
491 * attention. The goals we want to achieve are:
493 * a) Maximize number of children taking their natural size.
494 * b) The allocated size of children should be a continuous
495 * function of c_gap. That is, increasing the container size by
496 * one pixel should never make drastic changes in the distribution.
497 * c) If child i takes its natural size and child j doesn't,
498 * child j should have received at least as much gap as child i.
500 * The following code distributes the additional space by following
504 /* Sort descending by gap and position. */
506 g_qsort_with_data (spreading,
507 nvis_children, sizeof (GtkBoxSpreading),
508 gtk_box_compare_gap, sizes);
510 /* Distribute available space.
511 * This master piece of a loop was conceived by Behdad Esfahbod.
513 for (i = nvis_children - 1; i >= 0; --i)
515 /* Divide remaining space by number of remaining children.
516 * Sort order and reducing remaining space by assigned space
517 * ensures that space is distributed equally.
519 gint glue = (size + i) / (i + 1);
520 gint gap = sizes[spreading[i].index].natural_size
521 - sizes[spreading[i].index].minimum_size;
523 extra = MIN (glue, gap);
524 sizes[spreading[i].index].minimum_size += extra;
529 /* Calculate space which hasn't distributed yet,
530 * and is available for expanding children.
532 if (nexpand_children > 0)
533 extra = size / nexpand_children;
538 /* Allocate child positions. */
540 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
542 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
544 child_allocation.y = allocation->y + border_width;
545 child_allocation.height = MAX (1, allocation->height - border_width * 2);
546 if (packing == GTK_PACK_START)
547 x = allocation->x + border_width;
549 x = allocation->x + allocation->width - border_width;
553 child_allocation.x = allocation->x + border_width;
554 child_allocation.width = MAX (1, allocation->width - border_width * 2);
555 if (packing == GTK_PACK_START)
556 y = allocation->y + border_width;
558 y = allocation->y + allocation->height - border_width;
562 children = box->children;
565 child = children->data;
566 children = children->next;
568 if (gtk_widget_get_visible (child->widget))
570 if (child->pack == packing)
572 /* Assign the child's size. */
573 if (box->homogeneous)
575 if (nvis_children == 1)
585 child_size = sizes[i].minimum_size + child->padding * 2;
589 if (nexpand_children == 1)
594 nexpand_children -= 1;
599 /* Assign the child's position. */
600 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
604 child_allocation.width = MAX (1, child_size - child->padding * 2);
605 child_allocation.x = x + child->padding;
609 child_allocation.width = sizes[i].minimum_size;
610 child_allocation.x = x + (child_size - child_allocation.width) / 2;
613 if (packing == GTK_PACK_START)
615 x += child_size + box->spacing;
619 x -= child_size + box->spacing;
621 child_allocation.x -= child_size;
624 if (direction == GTK_TEXT_DIR_RTL)
625 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
628 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
632 child_allocation.height = MAX (1, child_size - child->padding * 2);
633 child_allocation.y = y + child->padding;
637 child_allocation.height = sizes[i].minimum_size;
638 child_allocation.y = y + (child_size - child_allocation.height) / 2;
641 if (packing == GTK_PACK_START)
643 y += child_size + box->spacing;
647 y -= child_size + box->spacing;
649 child_allocation.y -= child_size;
652 gtk_widget_size_allocate (child->widget, &child_allocation);
663 gtk_box_child_type (GtkContainer *container)
665 return GTK_TYPE_WIDGET;
669 gtk_box_set_child_property (GtkContainer *container,
678 GtkPackType pack_type = 0;
680 if (property_id != CHILD_PROP_POSITION)
681 gtk_box_query_child_packing (GTK_BOX (container),
689 case CHILD_PROP_EXPAND:
690 gtk_box_set_child_packing (GTK_BOX (container),
692 g_value_get_boolean (value),
697 case CHILD_PROP_FILL:
698 gtk_box_set_child_packing (GTK_BOX (container),
701 g_value_get_boolean (value),
705 case CHILD_PROP_PADDING:
706 gtk_box_set_child_packing (GTK_BOX (container),
710 g_value_get_uint (value),
713 case CHILD_PROP_PACK_TYPE:
714 gtk_box_set_child_packing (GTK_BOX (container),
719 g_value_get_enum (value));
721 case CHILD_PROP_POSITION:
722 gtk_box_reorder_child (GTK_BOX (container),
724 g_value_get_int (value));
727 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
733 gtk_box_get_child_property (GtkContainer *container,
742 GtkPackType pack_type = 0;
745 if (property_id != CHILD_PROP_POSITION)
746 gtk_box_query_child_packing (GTK_BOX (container),
755 case CHILD_PROP_EXPAND:
756 g_value_set_boolean (value, expand);
758 case CHILD_PROP_FILL:
759 g_value_set_boolean (value, fill);
761 case CHILD_PROP_PADDING:
762 g_value_set_uint (value, padding);
764 case CHILD_PROP_PACK_TYPE:
765 g_value_set_enum (value, pack_type);
767 case CHILD_PROP_POSITION:
769 for (list = GTK_BOX (container)->children; list; list = list->next)
771 GtkBoxChild *child_entry;
773 child_entry = list->data;
774 if (child_entry->widget == child)
778 g_value_set_int (value, list ? i : -1);
781 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
787 gtk_box_pack (GtkBox *box,
792 GtkPackType pack_type)
794 GtkBoxChild *child_info;
796 g_return_if_fail (GTK_IS_BOX (box));
797 g_return_if_fail (GTK_IS_WIDGET (child));
798 g_return_if_fail (child->parent == NULL);
800 child_info = g_new (GtkBoxChild, 1);
801 child_info->widget = child;
802 child_info->padding = padding;
803 child_info->expand = expand ? TRUE : FALSE;
804 child_info->fill = fill ? TRUE : FALSE;
805 child_info->pack = pack_type;
806 child_info->is_secondary = FALSE;
808 box->children = g_list_append (box->children, child_info);
810 gtk_widget_freeze_child_notify (child);
812 gtk_widget_set_parent (child, GTK_WIDGET (box));
814 gtk_widget_child_notify (child, "expand");
815 gtk_widget_child_notify (child, "fill");
816 gtk_widget_child_notify (child, "padding");
817 gtk_widget_child_notify (child, "pack-type");
818 gtk_widget_child_notify (child, "position");
819 gtk_widget_thaw_child_notify (child);
824 gtk_box_extended_layout_init (GtkExtendedLayoutIface *iface)
826 parent_extended_layout_iface = g_type_interface_peek_parent (iface);
828 iface->is_height_for_width = gtk_box_is_height_for_width;
829 iface->get_desired_width = gtk_box_get_desired_width;
830 iface->get_desired_height = gtk_box_get_desired_height;
831 iface->get_height_for_width = gtk_box_get_height_for_width;
832 iface->get_width_for_height = gtk_box_get_width_for_height;
836 gtk_box_is_height_for_width (GtkExtendedLayout *layout)
838 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
840 return (private->orientation == GTK_ORIENTATION_VERTICAL);
844 gtk_box_get_desired_size (GtkExtendedLayout *layout,
845 GtkOrientation orientation,
850 GtkBoxPrivate *private;
854 gint minimum, natural;
856 box = GTK_BOX (layout);
857 private = GTK_BOX_GET_PRIVATE (box);
858 border_width = GTK_CONTAINER (box)->border_width;
860 minimum = natural = 0;
864 for (children = box->children; children; children = children->next)
866 GtkBoxChild *child = children->data;
868 if (gtk_widget_get_visible (child->widget))
870 gint child_minimum, child_natural;
872 if (orientation == GTK_ORIENTATION_HORIZONTAL)
873 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child->widget),
874 &child_minimum, &child_natural);
876 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child->widget),
877 &child_minimum, &child_natural);
879 if (private->orientation == orientation)
881 if (box->homogeneous)
885 largest = child_minimum + child->padding * 2;
886 minimum = MAX (minimum, largest);
888 largest = child_natural + child->padding * 2;
889 natural = MAX (natural, largest);
893 minimum += child_minimum + child->padding * 2;
894 natural += child_natural + child->padding * 2;
899 /* The biggest mins and naturals in the opposing orientation */
900 minimum = MAX (minimum, child_minimum);
901 natural = MAX (natural, child_natural);
908 if (nvis_children > 0 && private->orientation == orientation)
910 if (box->homogeneous)
912 minimum *= nvis_children;
913 natural *= nvis_children;
915 minimum += (nvis_children - 1) * box->spacing;
916 natural += (nvis_children - 1) * box->spacing;
919 minimum += border_width * 2;
920 natural += border_width * 2;
923 *minimum_size = minimum;
926 *natural_size = natural;
930 gtk_box_get_desired_width (GtkExtendedLayout *layout,
934 gtk_box_get_desired_size (layout, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
938 gtk_box_get_desired_height (GtkExtendedLayout *layout,
942 gtk_box_get_desired_size (layout, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
946 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
951 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
955 gint nexpand_children;
956 gint computed_minimum = 0, computed_natural = 0;
957 gint border_width = GTK_CONTAINER (box)->border_width;
959 count_expand_children (box, &nvis_children, &nexpand_children);
961 if (nvis_children > 0)
963 GtkBoxSpreading *spreading = g_newa (GtkBoxSpreading, nvis_children);
964 GtkBoxDesiredSizes *sizes = g_newa (GtkBoxDesiredSizes, nvis_children);
968 gint child_size, child_minimum, child_natural;
970 size = avail_size - border_width * 2 - (nvis_children - 1) * box->spacing;
972 /* Retrieve desired size for visible children */
973 for (i = 0, children = box->children; children; children = children->next)
975 child = children->data;
977 if (gtk_widget_get_visible (child->widget))
979 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
980 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child->widget),
981 &sizes[i].minimum_size,
982 &sizes[i].natural_size);
984 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child->widget),
985 &sizes[i].minimum_size,
986 &sizes[i].natural_size);
988 /* Assert the api is working properly */
989 if (sizes[i].minimum_size < 0)
990 g_error ("GtkBox child %s minimum %s: %d < 0",
991 gtk_widget_get_name (GTK_WIDGET (child->widget)),
992 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
993 sizes[i].minimum_size);
995 if (sizes[i].natural_size < sizes[i].minimum_size)
996 g_error ("GtkBox child %s natural %s: %d < minimum %d",
997 gtk_widget_get_name (GTK_WIDGET (child->widget)),
998 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
999 sizes[i].natural_size,
1000 sizes[i].minimum_size);
1002 size -= sizes[i].minimum_size;
1003 size -= child->padding * 2;
1005 spreading[i].index = i;
1006 spreading[i].child = child;
1012 if (box->homogeneous)
1014 /* If were homogenous we still need to run the above loop to get the minimum sizes
1015 * for children that are not going to fill
1017 size = avail_size - border_width * 2 - (nvis_children - 1) * box->spacing;
1018 extra = size / nvis_children;
1023 /* Distribute the container's extra space c_gap. We want to assign
1024 * this space such that the sum of extra space assigned to children
1025 * (c^i_gap) is equal to c_cap. The case that there's not enough
1026 * space for all children to take their natural size needs some
1027 * attention. The goals we want to achieve are:
1029 * a) Maximize number of children taking their natural size.
1030 * b) The allocated size of children should be a continuous
1031 * function of c_gap. That is, increasing the container size by
1032 * one pixel should never make drastic changes in the distribution.
1033 * c) If child i takes its natural size and child j doesn't,
1034 * child j should have received at least as much gap as child i.
1036 * The following code distributes the additional space by following
1040 /* Sort descending by gap and position. */
1042 g_qsort_with_data (spreading,
1043 nvis_children, sizeof (GtkBoxSpreading),
1044 gtk_box_compare_gap, sizes);
1046 /* Distribute available space.
1047 * This master piece of a loop was conceived by Behdad Esfahbod.
1049 for (i = nvis_children - 1; i >= 0; --i)
1051 /* Divide remaining space by number of remaining children.
1052 * Sort order and reducing remaining space by assigned space
1053 * ensures that space is distributed equally.
1055 gint glue = (size + i) / (i + 1);
1056 gint gap = sizes[spreading[i].index].natural_size
1057 - sizes[spreading[i].index].minimum_size;
1059 extra = MIN (glue, gap);
1060 sizes[spreading[i].index].minimum_size += extra;
1065 /* Calculate space which hasn't distributed yet,
1066 * and is available for expanding children.
1068 if (nexpand_children > 0)
1069 extra = size / nexpand_children;
1074 /* Allocate child positions. */
1075 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1077 for (i = 0, children = box->children; children; children = children->next)
1079 child = children->data;
1081 if (gtk_widget_get_visible (child->widget))
1083 if (child->pack == packing)
1085 /* Assign the child's size. */
1086 if (box->homogeneous)
1088 if (nvis_children == 1)
1098 child_size = sizes[i].minimum_size + child->padding * 2;
1102 if (nexpand_children == 1)
1105 child_size += extra;
1107 nexpand_children -= 1;
1114 child_size = MAX (1, child_size - child->padding * 2);
1118 child_size = sizes[i].minimum_size;
1122 /* Assign the child's position. */
1123 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1124 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
1125 child_size, &child_minimum, &child_natural);
1126 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1127 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
1128 child_size, &child_minimum, &child_natural);
1131 computed_minimum = MAX (computed_minimum, child_minimum);
1132 computed_natural = MAX (computed_natural, child_natural);
1140 computed_minimum += border_width * 2;
1141 computed_natural += border_width * 2;
1144 *minimum_size = computed_minimum;
1146 *natural_size = computed_natural;
1150 gtk_box_compute_size_for_orientation (GtkBox *box,
1155 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
1157 gint nvis_children = 0;
1158 gint required_size = 0, required_natural = 0, child_size, child_natural;
1159 gint largest_child = 0, largest_natural = 0;
1161 avail_size -= GTK_CONTAINER (box)->border_width * 2;
1163 for (children = box->children; children != NULL;
1164 children = children->next, nvis_children++)
1166 GtkBoxChild *child = children->data;
1168 if (gtk_widget_get_visible (child->widget))
1171 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1172 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
1173 avail_size, &child_size, &child_natural);
1175 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
1176 avail_size, &child_size, &child_natural);
1179 child_size += child->padding * 2;
1180 child_natural += child->padding * 2;
1182 if (child_size > largest_child)
1183 largest_child = child_size;
1185 if (child_natural > largest_natural)
1186 largest_natural = child_natural;
1188 required_size += child_size;
1189 required_natural += child_natural;
1193 if (nvis_children > 0)
1195 if (box->homogeneous)
1197 required_size = largest_child * nvis_children;
1198 required_natural = largest_natural * nvis_children;
1201 required_size += (nvis_children - 1) * box->spacing;
1202 required_natural += (nvis_children - 1) * box->spacing;
1205 required_size += GTK_CONTAINER (box)->border_width * 2;
1206 required_natural += GTK_CONTAINER (box)->border_width * 2;
1209 *minimum_size = required_size;
1212 *natural_size = required_natural;
1216 gtk_box_get_width_for_height (GtkExtendedLayout *layout,
1218 gint *minimum_width,
1219 gint *natural_width)
1221 GtkBox *box = GTK_BOX (layout);
1222 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1224 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1225 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1227 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1231 gtk_box_get_height_for_width (GtkExtendedLayout *layout,
1233 gint *minimum_height,
1234 gint *natural_height)
1236 GtkBox *box = GTK_BOX (layout);
1237 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1239 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1240 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1242 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1247 * @orientation: the box' orientation.
1248 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1249 * @spacing: the number of pixels to place by default between children.
1251 * Creates a new #GtkHBox.
1253 * Return value: a new #GtkHBox.
1258 _gtk_box_new (GtkOrientation orientation,
1259 gboolean homogeneous,
1262 return g_object_new (GTK_TYPE_BOX,
1263 "orientation", orientation,
1265 "homogeneous", homogeneous ? TRUE : FALSE,
1270 * gtk_box_pack_start:
1272 * @child: the #GtkWidget to be added to @box
1273 * @expand: %TRUE if the new child is to be given extra space allocated to
1274 * @box. The extra space will be divided evenly between all children of
1275 * @box that use this option
1276 * @fill: %TRUE if space given to @child by the @expand option is
1277 * actually allocated to @child, rather than just padding it. This
1278 * parameter has no effect if @expand is set to %FALSE. A child is
1279 * always allocated the full height of a #GtkHBox and the full width
1280 * of a #GtkVBox. This option affects the other dimension
1281 * @padding: extra space in pixels to put between this child and its
1282 * neighbors, over and above the global amount specified by
1283 * #GtkBox:spacing property. If @child is a widget at one of the
1284 * reference ends of @box, then @padding pixels are also put between
1285 * @child and the reference edge of @box
1287 * Adds @child to @box, packed with reference to the start of @box.
1288 * The @child is packed after any other child packed with reference
1289 * to the start of @box.
1292 gtk_box_pack_start (GtkBox *box,
1298 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1304 * @child: the #GtkWidget to be added to @box
1305 * @expand: %TRUE if the new child is to be given extra space allocated
1306 * to @box. The extra space will be divided evenly between all children
1307 * of @box that use this option
1308 * @fill: %TRUE if space given to @child by the @expand option is
1309 * actually allocated to @child, rather than just padding it. This
1310 * parameter has no effect if @expand is set to %FALSE. A child is
1311 * always allocated the full height of a #GtkHBox and the full width
1312 * of a #GtkVBox. This option affects the other dimension
1313 * @padding: extra space in pixels to put between this child and its
1314 * neighbors, over and above the global amount specified by
1315 * #GtkBox:spacing property. If @child is a widget at one of the
1316 * reference ends of @box, then @padding pixels are also put between
1317 * @child and the reference edge of @box
1319 * Adds @child to @box, packed with reference to the end of @box.
1320 * The @child is packed after (away from end of) any other child
1321 * packed with reference to the end of @box.
1324 gtk_box_pack_end (GtkBox *box,
1330 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1334 * gtk_box_pack_start_defaults:
1336 * @widget: the #GtkWidget to be added to @box
1338 * Adds @widget to @box, packed with reference to the start of @box.
1339 * The child is packed after any other child packed with reference
1340 * to the start of @box.
1342 * Parameters for how to pack the child @widget, #GtkBox:expand,
1343 * #GtkBox:fill and #GtkBox:padding, are given their default
1344 * values, %TRUE, %TRUE, and 0, respectively.
1346 * Deprecated: 2.14: Use gtk_box_pack_start()
1349 gtk_box_pack_start_defaults (GtkBox *box,
1352 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
1356 * gtk_box_pack_end_defaults:
1358 * @widget: the #GtkWidget to be added to @box
1360 * Adds @widget to @box, packed with reference to the end of @box.
1361 * The child is packed after any other child packed with reference
1362 * to the start of @box.
1364 * Parameters for how to pack the child @widget, #GtkBox:expand,
1365 * #GtkBox:fill and #GtkBox:padding, are given their default
1366 * values, %TRUE, %TRUE, and 0, respectively.
1368 * Deprecated: 2.14: Use gtk_box_pack_end()
1371 gtk_box_pack_end_defaults (GtkBox *box,
1374 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
1378 * gtk_box_set_homogeneous:
1380 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1381 * %FALSE for variable allotments
1383 * Sets the #GtkBox:homogeneous property of @box, controlling
1384 * whether or not all children of @box are given equal space
1388 gtk_box_set_homogeneous (GtkBox *box,
1389 gboolean homogeneous)
1391 g_return_if_fail (GTK_IS_BOX (box));
1393 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
1395 box->homogeneous = homogeneous ? TRUE : FALSE;
1396 g_object_notify (G_OBJECT (box), "homogeneous");
1397 gtk_widget_queue_resize (GTK_WIDGET (box));
1402 * gtk_box_get_homogeneous:
1405 * Returns whether the box is homogeneous (all children are the
1406 * same size). See gtk_box_set_homogeneous().
1408 * Return value: %TRUE if the box is homogeneous.
1411 gtk_box_get_homogeneous (GtkBox *box)
1413 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1415 return box->homogeneous;
1419 * gtk_box_set_spacing:
1421 * @spacing: the number of pixels to put between children
1423 * Sets the #GtkBox:spacing property of @box, which is the
1424 * number of pixels to place between children of @box.
1427 gtk_box_set_spacing (GtkBox *box,
1430 g_return_if_fail (GTK_IS_BOX (box));
1432 if (spacing != box->spacing)
1434 box->spacing = spacing;
1435 _gtk_box_set_spacing_set (box, TRUE);
1437 g_object_notify (G_OBJECT (box), "spacing");
1439 gtk_widget_queue_resize (GTK_WIDGET (box));
1444 * gtk_box_get_spacing:
1447 * Gets the value set by gtk_box_set_spacing().
1449 * Return value: spacing between children
1452 gtk_box_get_spacing (GtkBox *box)
1454 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1456 return box->spacing;
1460 _gtk_box_set_spacing_set (GtkBox *box,
1461 gboolean spacing_set)
1463 GtkBoxPrivate *private;
1465 g_return_if_fail (GTK_IS_BOX (box));
1467 private = GTK_BOX_GET_PRIVATE (box);
1469 private->spacing_set = spacing_set ? TRUE : FALSE;
1473 _gtk_box_get_spacing_set (GtkBox *box)
1475 GtkBoxPrivate *private;
1477 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1479 private = GTK_BOX_GET_PRIVATE (box);
1481 return private->spacing_set;
1485 * gtk_box_reorder_child:
1487 * @child: the #GtkWidget to move
1488 * @position: the new position for @child in the list of children
1489 * of @box, starting from 0. If negative, indicates the end of
1492 * Moves @child to a new @position in the list of @box children.
1493 * The list is the <structfield>children</structfield> field of
1494 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1495 * as well as widgets packed #GTK_PACK_END, in the order that these
1496 * widgets were added to @box.
1498 * A widget's position in the @box children list determines where
1499 * the widget is packed into @box. A child widget at some position
1500 * in the list will be packed just after all other widgets of the
1501 * same packing type that appear earlier in the list.
1504 gtk_box_reorder_child (GtkBox *box,
1510 GtkBoxChild *child_info = NULL;
1513 g_return_if_fail (GTK_IS_BOX (box));
1514 g_return_if_fail (GTK_IS_WIDGET (child));
1516 old_link = box->children;
1520 child_info = old_link->data;
1521 if (child_info->widget == child)
1524 old_link = old_link->next;
1528 g_return_if_fail (old_link != NULL);
1530 if (position == old_position)
1533 box->children = g_list_delete_link (box->children, old_link);
1538 new_link = g_list_nth (box->children, position);
1540 box->children = g_list_insert_before (box->children, new_link, child_info);
1542 gtk_widget_child_notify (child, "position");
1543 if (gtk_widget_get_visible (child)
1544 && gtk_widget_get_visible (GTK_WIDGET (box)))
1545 gtk_widget_queue_resize (child);
1549 * gtk_box_query_child_packing:
1551 * @child: the #GtkWidget of the child to query
1552 * @expand: pointer to return location for #GtkBox:expand child property
1553 * @fill: pointer to return location for #GtkBox:fill child property
1554 * @padding: pointer to return location for #GtkBox:padding child property
1555 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1557 * Obtains information about how @child is packed into @box.
1560 gtk_box_query_child_packing (GtkBox *box,
1565 GtkPackType *pack_type)
1568 GtkBoxChild *child_info = NULL;
1570 g_return_if_fail (GTK_IS_BOX (box));
1571 g_return_if_fail (GTK_IS_WIDGET (child));
1573 list = box->children;
1576 child_info = list->data;
1577 if (child_info->widget == child)
1586 *expand = child_info->expand;
1588 *fill = child_info->fill;
1590 *padding = child_info->padding;
1592 *pack_type = child_info->pack;
1597 * gtk_box_set_child_packing:
1599 * @child: the #GtkWidget of the child to set
1600 * @expand: the new value of the #GtkBox:expand child property
1601 * @fill: the new value of the #GtkBox:fill child property
1602 * @padding: the new value of the #GtkBox:padding child property
1603 * @pack_type: the new value of the #GtkBox:pack-type child property
1605 * Sets the way @child is packed into @box.
1608 gtk_box_set_child_packing (GtkBox *box,
1613 GtkPackType pack_type)
1616 GtkBoxChild *child_info = NULL;
1618 g_return_if_fail (GTK_IS_BOX (box));
1619 g_return_if_fail (GTK_IS_WIDGET (child));
1621 list = box->children;
1624 child_info = list->data;
1625 if (child_info->widget == child)
1631 gtk_widget_freeze_child_notify (child);
1634 child_info->expand = expand != FALSE;
1635 gtk_widget_child_notify (child, "expand");
1636 child_info->fill = fill != FALSE;
1637 gtk_widget_child_notify (child, "fill");
1638 child_info->padding = padding;
1639 gtk_widget_child_notify (child, "padding");
1640 if (pack_type == GTK_PACK_END)
1641 child_info->pack = GTK_PACK_END;
1643 child_info->pack = GTK_PACK_START;
1644 gtk_widget_child_notify (child, "pack-type");
1646 if (gtk_widget_get_visible (child)
1647 && gtk_widget_get_visible (GTK_WIDGET (box)))
1648 gtk_widget_queue_resize (child);
1650 gtk_widget_thaw_child_notify (child);
1654 _gtk_box_set_old_defaults (GtkBox *box)
1656 GtkBoxPrivate *private;
1658 g_return_if_fail (GTK_IS_BOX (box));
1660 private = GTK_BOX_GET_PRIVATE (box);
1662 private->default_expand = TRUE;
1666 gtk_box_add (GtkContainer *container,
1669 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (container);
1671 gtk_box_pack_start (GTK_BOX (container), widget,
1672 private->default_expand,
1673 private->default_expand,
1678 gtk_box_remove (GtkContainer *container,
1681 GtkBox *box = GTK_BOX (container);
1685 children = box->children;
1688 child = children->data;
1690 if (child->widget == widget)
1692 gboolean was_visible;
1694 was_visible = gtk_widget_get_visible (widget);
1695 gtk_widget_unparent (widget);
1697 box->children = g_list_remove_link (box->children, children);
1698 g_list_free (children);
1701 /* queue resize regardless of gtk_widget_get_visible (container),
1702 * since that's what is needed by toplevels.
1705 gtk_widget_queue_resize (GTK_WIDGET (container));
1710 children = children->next;
1715 gtk_box_forall (GtkContainer *container,
1716 gboolean include_internals,
1717 GtkCallback callback,
1718 gpointer callback_data)
1720 GtkBox *box = GTK_BOX (container);
1724 children = box->children;
1727 child = children->data;
1728 children = children->next;
1730 if (child->pack == GTK_PACK_START)
1731 (* callback) (child->widget, callback_data);
1734 children = g_list_last (box->children);
1737 child = children->data;
1738 children = children->prev;
1740 if (child->pack == GTK_PACK_END)
1741 (* callback) (child->widget, callback_data);
1745 #define __GTK_BOX_C__
1746 #include "gtkaliasdef.c"