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: #GtkHBox, #GtkVBox, #GtkFrame, #GtkTable, #GtkLayout
33 * GtkBox is an 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
45 * refers to 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 * Because GtkBox is a #GtkContainer, you may also use
58 * gtk_container_add() to insert widgets into the box, and they will be
59 * packed with the default values for #GtkBox:expand and #GtkBox:fill.
60 * Use gtk_container_remove() to remove widgets from the GtkBox.
62 * Use gtk_box_set_homogeneous() to specify whether or not all children
63 * of the GtkBox are forced to get the same amount of space.
65 * Use gtk_box_set_spacing() to determine how much space will be
66 * minimally placed between all children in the GtkBox. Note that
67 * spacing is added <emphasis>between</emphasis> the children, while
68 * padding added by gtk_box_pack_start() or gtk_box_pack_end() is added
69 * <emphasis>on either side</emphasis> of the widget it belongs to.
71 * Use gtk_box_reorder_child() to move a GtkBox child to a different
74 * Use gtk_box_set_child_packing() to reset the #GtkBox:expand,
75 * #GtkBox:fill and #GtkBox:padding child properties.
76 * Use gtk_box_query_child_packing() to query these fields.
80 * Note that a single-row or single-column #GtkGrid provides exactly the
81 * same functionality as #GtkBox.
89 #include "gtkorientable.h"
90 #include "gtksizerequest.h"
91 #include "gtktypebuiltins.h"
92 #include "gtkprivate.h"
108 CHILD_PROP_PACK_TYPE,
112 struct _GtkBoxPrivate
114 GtkOrientation orientation;
120 guint default_expand : 1;
121 guint homogeneous : 1;
122 guint spacing_set : 1;
125 typedef struct _GtkBoxChild GtkBoxChild;
129 * @widget: the child widget, packed into the GtkBox.
130 * @padding: the number of extra pixels to put between this child and its
131 * neighbors, set when packed, zero by default.
132 * @expand: flag indicates whether extra space should be given to this child.
133 * Any extra space given to the parent GtkBox is divided up among all children
134 * with this attribute set to %TRUE; set when packed, %TRUE by default.
135 * @fill: flag indicates whether any extra space given to this child due to its
136 * @expand attribute being set is actually allocated to the child, rather than
137 * being used as padding around the widget; set when packed, %TRUE by default.
138 * @pack: one of #GtkPackType indicating whether the child is packed with
139 * reference to the start (top/left) or end (bottom/right) of the GtkBox.
152 static void gtk_box_size_allocate (GtkWidget *widget,
153 GtkAllocation *allocation);
155 static void gtk_box_compute_expand (GtkWidget *widget,
159 static void gtk_box_set_property (GObject *object,
163 static void gtk_box_get_property (GObject *object,
167 static void gtk_box_add (GtkContainer *container,
169 static void gtk_box_remove (GtkContainer *container,
171 static void gtk_box_forall (GtkContainer *container,
172 gboolean include_internals,
173 GtkCallback callback,
174 gpointer callback_data);
175 static void gtk_box_set_child_property (GtkContainer *container,
180 static void gtk_box_get_child_property (GtkContainer *container,
185 static GType gtk_box_child_type (GtkContainer *container);
188 static GtkSizeRequestMode gtk_box_get_request_mode (GtkWidget *widget);
189 static void gtk_box_get_preferred_width (GtkWidget *widget,
192 static void gtk_box_get_preferred_height (GtkWidget *widget,
195 static void gtk_box_get_preferred_width_for_height (GtkWidget *widget,
198 gint *natural_width);
199 static void gtk_box_get_preferred_height_for_width (GtkWidget *widget,
201 gint *minimum_height,
202 gint *natural_height);
204 G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
205 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
209 gtk_box_class_init (GtkBoxClass *class)
211 GObjectClass *object_class = G_OBJECT_CLASS (class);
212 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
213 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
215 object_class->set_property = gtk_box_set_property;
216 object_class->get_property = gtk_box_get_property;
218 widget_class->size_allocate = gtk_box_size_allocate;
219 widget_class->get_request_mode = gtk_box_get_request_mode;
220 widget_class->get_preferred_width = gtk_box_get_preferred_width;
221 widget_class->get_preferred_height = gtk_box_get_preferred_height;
222 widget_class->get_preferred_height_for_width = gtk_box_get_preferred_height_for_width;
223 widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
224 widget_class->compute_expand = gtk_box_compute_expand;
226 container_class->add = gtk_box_add;
227 container_class->remove = gtk_box_remove;
228 container_class->forall = gtk_box_forall;
229 container_class->child_type = gtk_box_child_type;
230 container_class->set_child_property = gtk_box_set_child_property;
231 container_class->get_child_property = gtk_box_get_child_property;
232 gtk_container_class_handle_border_width (container_class);
234 g_object_class_override_property (object_class,
238 g_object_class_install_property (object_class,
240 g_param_spec_int ("spacing",
242 P_("The amount of space between children"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (object_class,
250 g_param_spec_boolean ("homogeneous",
252 P_("Whether the children should all be the same size"),
254 GTK_PARAM_READWRITE));
259 * Whether the child should receive extra space when the parent grows.
261 * Note that the default value for this property is %FALSE for GtkBox,
262 * but #GtkHBox, #GtkVBox and other subclasses use the old default
265 * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
266 * and #GtkWidget:vexpand properties are the preferred way to influence
267 * child size allocation in containers.
269 gtk_container_class_install_child_property (container_class,
271 g_param_spec_boolean ("expand",
273 P_("Whether the child should receive extra space when the parent grows"),
275 GTK_PARAM_READWRITE));
280 * Whether the child should receive extra space when the parent grows.
282 * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
283 * and #GtkWidget:vexpand properties are the preferred way to influence
284 * child size allocation in containers.
286 gtk_container_class_install_child_property (container_class,
288 g_param_spec_boolean ("fill",
290 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
292 GTK_PARAM_READWRITE));
294 gtk_container_class_install_child_property (container_class,
296 g_param_spec_uint ("padding",
298 P_("Extra space to put between the child and its neighbors, in pixels"),
300 GTK_PARAM_READWRITE));
301 gtk_container_class_install_child_property (container_class,
302 CHILD_PROP_PACK_TYPE,
303 g_param_spec_enum ("pack-type",
305 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
306 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
307 GTK_PARAM_READWRITE));
308 gtk_container_class_install_child_property (container_class,
310 g_param_spec_int ("position",
312 P_("The index of the child in the parent"),
314 GTK_PARAM_READWRITE));
316 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
320 gtk_box_init (GtkBox *box)
322 GtkBoxPrivate *private;
324 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
329 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
330 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
332 private->orientation = GTK_ORIENTATION_HORIZONTAL;
333 private->children = NULL;
335 private->default_expand = FALSE;
336 private->homogeneous = FALSE;
337 private->spacing = 0;
338 private->spacing_set = FALSE;
342 gtk_box_set_property (GObject *object,
347 GtkBox *box = GTK_BOX (object);
348 GtkBoxPrivate *private = box->priv;
352 case PROP_ORIENTATION:
353 private->orientation = g_value_get_enum (value);
354 gtk_widget_queue_resize (GTK_WIDGET (box));
357 gtk_box_set_spacing (box, g_value_get_int (value));
359 case PROP_HOMOGENEOUS:
360 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369 gtk_box_get_property (GObject *object,
374 GtkBox *box = GTK_BOX (object);
375 GtkBoxPrivate *private = box->priv;
379 case PROP_ORIENTATION:
380 g_value_set_enum (value, private->orientation);
383 g_value_set_int (value, private->spacing);
385 case PROP_HOMOGENEOUS:
386 g_value_set_boolean (value, private->homogeneous);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396 count_expand_children (GtkBox *box,
397 gint *visible_children,
398 gint *expand_children)
400 GtkBoxPrivate *private = box->priv;
404 *visible_children = *expand_children = 0;
406 for (children = private->children; children; children = children->next)
408 child = children->data;
410 if (gtk_widget_get_visible (child->widget))
412 *visible_children += 1;
413 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
414 *expand_children += 1;
420 gtk_box_size_allocate (GtkWidget *widget,
421 GtkAllocation *allocation)
423 GtkBox *box = GTK_BOX (widget);
424 GtkBoxPrivate *private = box->priv;
428 gint nexpand_children;
430 GtkTextDirection direction;
431 GtkAllocation child_allocation;
432 GtkRequestedSize *sizes;
438 gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
439 gint x = 0, y = 0, i;
443 gtk_widget_set_allocation (widget, allocation);
445 count_expand_children (box, &nvis_children, &nexpand_children);
447 /* If there is no visible child, simply return. */
448 if (nvis_children <= 0)
451 direction = gtk_widget_get_direction (widget);
452 sizes = g_newa (GtkRequestedSize, nvis_children);
454 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
455 size = allocation->width - (nvis_children - 1) * private->spacing;
457 size = allocation->height - (nvis_children - 1) * private->spacing;
459 /* Retrieve desired size for visible children. */
460 for (i = 0, children = private->children; children; children = children->next)
462 child = children->data;
464 if (!gtk_widget_get_visible (child->widget))
467 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
468 gtk_widget_get_preferred_width_for_height (child->widget,
470 &sizes[i].minimum_size,
471 &sizes[i].natural_size);
473 gtk_widget_get_preferred_height_for_width (child->widget,
475 &sizes[i].minimum_size,
476 &sizes[i].natural_size);
479 /* Assert the api is working properly */
480 if (sizes[i].minimum_size < 0)
481 g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
482 gtk_widget_get_name (GTK_WIDGET (child->widget)),
483 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
484 sizes[i].minimum_size,
485 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
486 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
488 if (sizes[i].natural_size < sizes[i].minimum_size)
489 g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
490 gtk_widget_get_name (GTK_WIDGET (child->widget)),
491 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
492 sizes[i].natural_size,
493 sizes[i].minimum_size,
494 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
495 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
497 size -= sizes[i].minimum_size;
498 size -= child->padding * 2;
500 sizes[i].data = child;
505 if (private->homogeneous)
507 /* If were homogenous we still need to run the above loop to get the
508 * minimum sizes for children that are not going to fill
510 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
511 size = allocation->width - (nvis_children - 1) * private->spacing;
513 size = allocation->height - (nvis_children - 1) * private->spacing;
515 extra = size / nvis_children;
516 n_extra_widgets = size % nvis_children;
520 /* Bring children up to size first */
521 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
523 /* Calculate space which hasn't distributed yet,
524 * and is available for expanding children.
526 if (nexpand_children > 0)
528 extra = size / nexpand_children;
529 n_extra_widgets = size % nexpand_children;
535 /* Allocate child positions. */
536 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
538 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
540 child_allocation.y = allocation->y;
541 child_allocation.height = MAX (1, allocation->height);
542 if (packing == GTK_PACK_START)
545 x = allocation->x + allocation->width;
549 child_allocation.x = allocation->x;
550 child_allocation.width = MAX (1, allocation->width);
551 if (packing == GTK_PACK_START)
554 y = allocation->y + allocation->height;
557 for (i = 0, children = private->children;
559 children = children->next)
561 child = children->data;
563 /* If widget is not visible, skip it. */
564 if (!gtk_widget_get_visible (child->widget))
567 /* If widget is packed differently skip it, but still increment i,
568 * since widget is visible and will be handled in next loop iteration.
570 if (child->pack != packing)
576 /* Assign the child's size. */
577 if (private->homogeneous)
581 if (n_extra_widgets > 0)
589 child_size = sizes[i].minimum_size + child->padding * 2;
591 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
595 if (n_extra_widgets > 0)
603 /* Assign the child's position. */
604 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
608 child_allocation.width = MAX (1, child_size - child->padding * 2);
609 child_allocation.x = x + child->padding;
613 child_allocation.width = sizes[i].minimum_size;
614 child_allocation.x = x + (child_size - child_allocation.width) / 2;
617 if (packing == GTK_PACK_START)
619 x += child_size + private->spacing;
623 x -= child_size + private->spacing;
625 child_allocation.x -= child_size;
628 if (direction == GTK_TEXT_DIR_RTL)
629 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
632 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
636 child_allocation.height = MAX (1, child_size - child->padding * 2);
637 child_allocation.y = y + child->padding;
641 child_allocation.height = sizes[i].minimum_size;
642 child_allocation.y = y + (child_size - child_allocation.height) / 2;
645 if (packing == GTK_PACK_START)
647 y += child_size + private->spacing;
651 y -= child_size + private->spacing;
653 child_allocation.y -= child_size;
656 gtk_widget_size_allocate (child->widget, &child_allocation);
664 gtk_box_compute_expand (GtkWidget *widget,
668 GtkBoxPrivate *private = GTK_BOX (widget)->priv;
672 gboolean opposite_expand;
673 GtkOrientation opposite_orientation;
675 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
676 opposite_orientation = GTK_ORIENTATION_VERTICAL;
678 opposite_orientation = GTK_ORIENTATION_HORIZONTAL;
681 opposite_expand = FALSE;
683 for (children = private->children; children; children = children->next)
685 child = children->data;
687 /* we don't recurse into children anymore as soon as we know
688 * expand=TRUE in an orientation
691 if (child->expand || (!our_expand && gtk_widget_compute_expand (child->widget, private->orientation)))
694 if (!opposite_expand && gtk_widget_compute_expand (child->widget, opposite_orientation))
695 opposite_expand = TRUE;
697 if (our_expand && opposite_expand)
701 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
703 *hexpand_p = our_expand;
704 *vexpand_p = opposite_expand;
708 *hexpand_p = opposite_expand;
709 *vexpand_p = our_expand;
714 gtk_box_child_type (GtkContainer *container)
716 return GTK_TYPE_WIDGET;
720 gtk_box_set_child_property (GtkContainer *container,
729 GtkPackType pack_type = 0;
731 if (property_id != CHILD_PROP_POSITION)
732 gtk_box_query_child_packing (GTK_BOX (container),
740 case CHILD_PROP_EXPAND:
741 gtk_box_set_child_packing (GTK_BOX (container),
743 g_value_get_boolean (value),
748 case CHILD_PROP_FILL:
749 gtk_box_set_child_packing (GTK_BOX (container),
752 g_value_get_boolean (value),
756 case CHILD_PROP_PADDING:
757 gtk_box_set_child_packing (GTK_BOX (container),
761 g_value_get_uint (value),
764 case CHILD_PROP_PACK_TYPE:
765 gtk_box_set_child_packing (GTK_BOX (container),
770 g_value_get_enum (value));
772 case CHILD_PROP_POSITION:
773 gtk_box_reorder_child (GTK_BOX (container),
775 g_value_get_int (value));
778 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
784 gtk_box_get_child_property (GtkContainer *container,
790 gboolean expand = FALSE;
791 gboolean fill = FALSE;
793 GtkPackType pack_type = 0;
796 if (property_id != CHILD_PROP_POSITION)
797 gtk_box_query_child_packing (GTK_BOX (container),
806 case CHILD_PROP_EXPAND:
807 g_value_set_boolean (value, expand);
809 case CHILD_PROP_FILL:
810 g_value_set_boolean (value, fill);
812 case CHILD_PROP_PADDING:
813 g_value_set_uint (value, padding);
815 case CHILD_PROP_PACK_TYPE:
816 g_value_set_enum (value, pack_type);
818 case CHILD_PROP_POSITION:
820 for (list = GTK_BOX (container)->priv->children; list; list = list->next)
822 GtkBoxChild *child_entry;
824 child_entry = list->data;
825 if (child_entry->widget == child)
829 g_value_set_int (value, list ? i : -1);
832 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
838 gtk_box_pack (GtkBox *box,
843 GtkPackType pack_type)
845 GtkBoxPrivate *private = box->priv;
846 GtkBoxChild *child_info;
848 g_return_if_fail (GTK_IS_BOX (box));
849 g_return_if_fail (GTK_IS_WIDGET (child));
850 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
852 child_info = g_new (GtkBoxChild, 1);
853 child_info->widget = child;
854 child_info->padding = padding;
855 child_info->expand = expand ? TRUE : FALSE;
856 child_info->fill = fill ? TRUE : FALSE;
857 child_info->pack = pack_type;
859 private->children = g_list_append (private->children, child_info);
861 gtk_widget_freeze_child_notify (child);
863 gtk_widget_set_parent (child, GTK_WIDGET (box));
865 gtk_widget_child_notify (child, "expand");
866 gtk_widget_child_notify (child, "fill");
867 gtk_widget_child_notify (child, "padding");
868 gtk_widget_child_notify (child, "pack-type");
869 gtk_widget_child_notify (child, "position");
870 gtk_widget_thaw_child_notify (child);
874 static GtkSizeRequestMode
875 gtk_box_get_request_mode (GtkWidget *widget)
877 GtkBoxPrivate *private = GTK_BOX (widget)->priv;
879 return (private->orientation == GTK_ORIENTATION_VERTICAL) ?
880 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH : GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
884 gtk_box_get_size (GtkWidget *widget,
885 GtkOrientation orientation,
890 GtkBoxPrivate *private;
893 gint minimum, natural;
895 box = GTK_BOX (widget);
898 minimum = natural = 0;
902 for (children = private->children; children; children = children->next)
904 GtkBoxChild *child = children->data;
906 if (gtk_widget_get_visible (child->widget))
908 gint child_minimum, child_natural;
910 if (orientation == GTK_ORIENTATION_HORIZONTAL)
911 gtk_widget_get_preferred_width (child->widget,
912 &child_minimum, &child_natural);
914 gtk_widget_get_preferred_height (child->widget,
915 &child_minimum, &child_natural);
917 if (private->orientation == orientation)
919 if (private->homogeneous)
923 largest = child_minimum + child->padding * 2;
924 minimum = MAX (minimum, largest);
926 largest = child_natural + child->padding * 2;
927 natural = MAX (natural, largest);
931 minimum += child_minimum + child->padding * 2;
932 natural += child_natural + child->padding * 2;
937 /* The biggest mins and naturals in the opposing orientation */
938 minimum = MAX (minimum, child_minimum);
939 natural = MAX (natural, child_natural);
946 if (nvis_children > 0 && private->orientation == orientation)
948 if (private->homogeneous)
950 minimum *= nvis_children;
951 natural *= nvis_children;
953 minimum += (nvis_children - 1) * private->spacing;
954 natural += (nvis_children - 1) * private->spacing;
958 *minimum_size = minimum;
961 *natural_size = natural;
965 gtk_box_get_preferred_width (GtkWidget *widget,
969 gtk_box_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
973 gtk_box_get_preferred_height (GtkWidget *widget,
977 gtk_box_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
981 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
986 GtkBoxPrivate *private = box->priv;
990 gint nexpand_children;
991 gint computed_minimum = 0, computed_natural = 0;
992 GtkRequestedSize *sizes;
995 gint child_size, child_minimum, child_natural;
996 gint n_extra_widgets = 0;
998 count_expand_children (box, &nvis_children, &nexpand_children);
1000 if (nvis_children <= 0)
1003 sizes = g_newa (GtkRequestedSize, nvis_children);
1004 size = avail_size - (nvis_children - 1) * private->spacing;
1006 /* Retrieve desired size for visible children */
1007 for (i = 0, children = private->children; children; children = children->next)
1009 child = children->data;
1011 if (gtk_widget_get_visible (child->widget))
1013 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1014 gtk_widget_get_preferred_width (child->widget,
1015 &sizes[i].minimum_size,
1016 &sizes[i].natural_size);
1018 gtk_widget_get_preferred_height (child->widget,
1019 &sizes[i].minimum_size,
1020 &sizes[i].natural_size);
1022 /* Assert the api is working properly */
1023 if (sizes[i].minimum_size < 0)
1024 g_error ("GtkBox child %s minimum %s: %d < 0",
1025 gtk_widget_get_name (GTK_WIDGET (child->widget)),
1026 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1027 sizes[i].minimum_size);
1029 if (sizes[i].natural_size < sizes[i].minimum_size)
1030 g_error ("GtkBox child %s natural %s: %d < minimum %d",
1031 gtk_widget_get_name (GTK_WIDGET (child->widget)),
1032 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1033 sizes[i].natural_size,
1034 sizes[i].minimum_size);
1036 size -= sizes[i].minimum_size;
1037 size -= child->padding * 2;
1039 sizes[i].data = child;
1045 if (private->homogeneous)
1047 /* If were homogenous we still need to run the above loop to get the
1048 * minimum sizes for children that are not going to fill
1050 size = avail_size - (nvis_children - 1) * private->spacing;
1051 extra = size / nvis_children;
1052 n_extra_widgets = size % nvis_children;
1056 /* Bring children up to size first */
1057 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
1059 /* Calculate space which hasn't distributed yet,
1060 * and is available for expanding children.
1062 if (nexpand_children > 0)
1064 extra = size / nexpand_children;
1065 n_extra_widgets = size % nexpand_children;
1071 /* Allocate child positions. */
1072 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1074 for (i = 0, children = private->children;
1076 children = children->next)
1078 child = children->data;
1080 /* If widget is not visible, skip it. */
1081 if (!gtk_widget_get_visible (child->widget))
1084 /* If widget is packed differently skip it, but still increment i,
1085 * since widget is visible and will be handled in next loop iteration.
1087 if (child->pack != packing)
1093 if (child->pack == packing)
1095 /* Assign the child's size. */
1096 if (private->homogeneous)
1100 if (n_extra_widgets > 0)
1108 child_size = sizes[i].minimum_size + child->padding * 2;
1110 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
1112 child_size += extra;
1114 if (n_extra_widgets > 0)
1124 child_size = MAX (1, child_size - child->padding * 2);
1128 child_size = sizes[i].minimum_size;
1132 /* Assign the child's position. */
1133 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1134 gtk_widget_get_preferred_height_for_width (child->widget,
1135 child_size, &child_minimum, &child_natural);
1136 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1137 gtk_widget_get_preferred_width_for_height (child->widget,
1138 child_size, &child_minimum, &child_natural);
1141 computed_minimum = MAX (computed_minimum, child_minimum);
1142 computed_natural = MAX (computed_natural, child_natural);
1149 *minimum_size = computed_minimum;
1151 *natural_size = computed_natural;
1155 gtk_box_compute_size_for_orientation (GtkBox *box,
1160 GtkBoxPrivate *private = box->priv;
1162 gint nvis_children = 0;
1163 gint required_size = 0, required_natural = 0, child_size, child_natural;
1164 gint largest_child = 0, largest_natural = 0;
1166 for (children = private->children; children != NULL;
1167 children = children->next, nvis_children++)
1169 GtkBoxChild *child = children->data;
1171 if (gtk_widget_get_visible (child->widget))
1174 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1175 gtk_widget_get_preferred_width_for_height (child->widget,
1176 avail_size, &child_size, &child_natural);
1178 gtk_widget_get_preferred_height_for_width (child->widget,
1179 avail_size, &child_size, &child_natural);
1182 child_size += child->padding * 2;
1183 child_natural += child->padding * 2;
1185 if (child_size > largest_child)
1186 largest_child = child_size;
1188 if (child_natural > largest_natural)
1189 largest_natural = child_natural;
1191 required_size += child_size;
1192 required_natural += child_natural;
1196 if (nvis_children > 0)
1198 if (private->homogeneous)
1200 required_size = largest_child * nvis_children;
1201 required_natural = largest_natural * nvis_children;
1204 required_size += (nvis_children - 1) * private->spacing;
1205 required_natural += (nvis_children - 1) * private->spacing;
1209 *minimum_size = required_size;
1212 *natural_size = required_natural;
1216 gtk_box_get_preferred_width_for_height (GtkWidget *widget,
1218 gint *minimum_width,
1219 gint *natural_width)
1221 GtkBox *box = GTK_BOX (widget);
1222 GtkBoxPrivate *private = box->priv;
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_preferred_height_for_width (GtkWidget *widget,
1233 gint *minimum_height,
1234 gint *natural_height)
1236 GtkBox *box = GTK_BOX (widget);
1237 GtkBoxPrivate *private = box->priv;
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's orientation.
1248 * @spacing: the number of pixels to place by default between children.
1250 * Creates a new #GtkBox.
1252 * Return value: a new #GtkBox.
1257 gtk_box_new (GtkOrientation orientation,
1260 return g_object_new (GTK_TYPE_BOX,
1261 "orientation", orientation,
1267 * gtk_box_pack_start:
1269 * @child: the #GtkWidget to be added to @box
1270 * @expand: %TRUE if the new child is to be given extra space allocated
1271 * to @box. The extra space will be divided evenly between all children
1272 * that use this option
1273 * @fill: %TRUE if space given to @child by the @expand option is
1274 * actually allocated to @child, rather than just padding it. This
1275 * parameter has no effect if @expand is set to %FALSE. A child is
1276 * always allocated the full height of a #GtkHBox and the full width
1277 * of a #GtkVBox. This option affects the other dimension
1278 * @padding: extra space in pixels to put between this child and its
1279 * neighbors, over and above the global amount specified by
1280 * #GtkBox:spacing property. If @child is a widget at one of the
1281 * reference ends of @box, then @padding pixels are also put between
1282 * @child and the reference edge of @box
1284 * Adds @child to @box, packed with reference to the start of @box.
1285 * The @child is packed after any other child packed with reference
1286 * to the start of @box.
1289 gtk_box_pack_start (GtkBox *box,
1295 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1301 * @child: the #GtkWidget to be added to @box
1302 * @expand: %TRUE if the new child is to be given extra space allocated
1303 * to @box. The extra space will be divided evenly between all children
1304 * of @box that use this option
1305 * @fill: %TRUE if space given to @child by the @expand option is
1306 * actually allocated to @child, rather than just padding it. This
1307 * parameter has no effect if @expand is set to %FALSE. A child is
1308 * always allocated the full height of a #GtkHBox and the full width
1309 * of a #GtkVBox. This option affects the other dimension
1310 * @padding: extra space in pixels to put between this child and its
1311 * neighbors, over and above the global amount specified by
1312 * #GtkBox:spacing property. If @child is a widget at one of the
1313 * reference ends of @box, then @padding pixels are also put between
1314 * @child and the reference edge of @box
1316 * Adds @child to @box, packed with reference to the end of @box.
1317 * The @child is packed after (away from end of) any other child
1318 * packed with reference to the end of @box.
1321 gtk_box_pack_end (GtkBox *box,
1327 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1331 * gtk_box_set_homogeneous:
1333 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1334 * %FALSE for variable allotments
1336 * Sets the #GtkBox:homogeneous property of @box, controlling
1337 * whether or not all children of @box are given equal space
1341 gtk_box_set_homogeneous (GtkBox *box,
1342 gboolean homogeneous)
1344 GtkBoxPrivate *private;
1346 g_return_if_fail (GTK_IS_BOX (box));
1348 private = box->priv;
1350 if ((homogeneous ? TRUE : FALSE) != private->homogeneous)
1352 private->homogeneous = homogeneous ? TRUE : FALSE;
1353 g_object_notify (G_OBJECT (box), "homogeneous");
1354 gtk_widget_queue_resize (GTK_WIDGET (box));
1359 * gtk_box_get_homogeneous:
1362 * Returns whether the box is homogeneous (all children are the
1363 * same size). See gtk_box_set_homogeneous().
1365 * Return value: %TRUE if the box is homogeneous.
1368 gtk_box_get_homogeneous (GtkBox *box)
1370 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1372 return box->priv->homogeneous;
1376 * gtk_box_set_spacing:
1378 * @spacing: the number of pixels to put between children
1380 * Sets the #GtkBox:spacing property of @box, which is the
1381 * number of pixels to place between children of @box.
1384 gtk_box_set_spacing (GtkBox *box,
1387 GtkBoxPrivate *private;
1389 g_return_if_fail (GTK_IS_BOX (box));
1391 private = box->priv;
1393 if (spacing != private->spacing)
1395 private->spacing = spacing;
1396 _gtk_box_set_spacing_set (box, TRUE);
1398 g_object_notify (G_OBJECT (box), "spacing");
1400 gtk_widget_queue_resize (GTK_WIDGET (box));
1405 * gtk_box_get_spacing:
1408 * Gets the value set by gtk_box_set_spacing().
1410 * Return value: spacing between children
1413 gtk_box_get_spacing (GtkBox *box)
1415 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1417 return box->priv->spacing;
1421 _gtk_box_set_spacing_set (GtkBox *box,
1422 gboolean spacing_set)
1424 GtkBoxPrivate *private;
1426 g_return_if_fail (GTK_IS_BOX (box));
1428 private = box->priv;
1430 private->spacing_set = spacing_set ? TRUE : FALSE;
1434 _gtk_box_get_spacing_set (GtkBox *box)
1436 GtkBoxPrivate *private;
1438 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1440 private = box->priv;
1442 return private->spacing_set;
1446 * gtk_box_reorder_child:
1448 * @child: the #GtkWidget to move
1449 * @position: the new position for @child in the list of children
1450 * of @box, starting from 0. If negative, indicates the end of
1453 * Moves @child to a new @position in the list of @box children.
1454 * The list is the <structfield>children</structfield> field of
1455 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1456 * as well as widgets packed #GTK_PACK_END, in the order that these
1457 * widgets were added to @box.
1459 * A widget's position in the @box children list determines where
1460 * the widget is packed into @box. A child widget at some position
1461 * in the list will be packed just after all other widgets of the
1462 * same packing type that appear earlier in the list.
1465 gtk_box_reorder_child (GtkBox *box,
1469 GtkBoxPrivate *priv;
1472 GtkBoxChild *child_info = NULL;
1475 g_return_if_fail (GTK_IS_BOX (box));
1476 g_return_if_fail (GTK_IS_WIDGET (child));
1480 old_link = priv->children;
1484 child_info = old_link->data;
1485 if (child_info->widget == child)
1488 old_link = old_link->next;
1492 g_return_if_fail (old_link != NULL);
1494 if (position == old_position)
1497 priv->children = g_list_delete_link (priv->children, old_link);
1502 new_link = g_list_nth (priv->children, position);
1504 priv->children = g_list_insert_before (priv->children, new_link, child_info);
1506 gtk_widget_child_notify (child, "position");
1507 if (gtk_widget_get_visible (child)
1508 && gtk_widget_get_visible (GTK_WIDGET (box)))
1509 gtk_widget_queue_resize (child);
1513 * gtk_box_query_child_packing:
1515 * @child: the #GtkWidget of the child to query
1516 * @expand: (out): pointer to return location for #GtkBox:expand child
1518 * @fill: (out): pointer to return location for #GtkBox:fill child
1520 * @padding: (out): pointer to return location for #GtkBox:padding
1522 * @pack_type: (out): pointer to return location for #GtkBox:pack-type
1525 * Obtains information about how @child is packed into @box.
1528 gtk_box_query_child_packing (GtkBox *box,
1533 GtkPackType *pack_type)
1535 GtkBoxPrivate *private;
1537 GtkBoxChild *child_info = NULL;
1539 g_return_if_fail (GTK_IS_BOX (box));
1540 g_return_if_fail (GTK_IS_WIDGET (child));
1542 private = box->priv;
1544 list = private->children;
1547 child_info = list->data;
1548 if (child_info->widget == child)
1557 *expand = child_info->expand;
1559 *fill = child_info->fill;
1561 *padding = child_info->padding;
1563 *pack_type = child_info->pack;
1568 * gtk_box_set_child_packing:
1570 * @child: the #GtkWidget of the child to set
1571 * @expand: the new value of the #GtkBox:expand child property
1572 * @fill: the new value of the #GtkBox:fill child property
1573 * @padding: the new value of the #GtkBox:padding child property
1574 * @pack_type: the new value of the #GtkBox:pack-type child property
1576 * Sets the way @child is packed into @box.
1579 gtk_box_set_child_packing (GtkBox *box,
1584 GtkPackType pack_type)
1586 GtkBoxPrivate *private;
1588 GtkBoxChild *child_info = NULL;
1590 g_return_if_fail (GTK_IS_BOX (box));
1591 g_return_if_fail (GTK_IS_WIDGET (child));
1593 private = box->priv;
1595 list = private->children;
1598 child_info = list->data;
1599 if (child_info->widget == child)
1605 gtk_widget_freeze_child_notify (child);
1610 expanded = expand != FALSE;
1612 /* avoid setting expand if unchanged, since queue_compute_expand
1613 * can be expensive-ish
1615 if (child_info->expand != expanded)
1617 child_info->expand = expand != FALSE;
1618 gtk_widget_queue_compute_expand (GTK_WIDGET (box));
1619 gtk_widget_child_notify (child, "expand");
1622 child_info->fill = fill != FALSE;
1623 gtk_widget_child_notify (child, "fill");
1624 child_info->padding = padding;
1625 gtk_widget_child_notify (child, "padding");
1626 if (pack_type == GTK_PACK_END)
1627 child_info->pack = GTK_PACK_END;
1629 child_info->pack = GTK_PACK_START;
1630 gtk_widget_child_notify (child, "pack-type");
1632 if (gtk_widget_get_visible (child)
1633 && gtk_widget_get_visible (GTK_WIDGET (box)))
1634 gtk_widget_queue_resize (child);
1636 gtk_widget_thaw_child_notify (child);
1640 _gtk_box_set_old_defaults (GtkBox *box)
1642 GtkBoxPrivate *private;
1644 g_return_if_fail (GTK_IS_BOX (box));
1646 private = box->priv;
1648 private->default_expand = TRUE;
1652 gtk_box_add (GtkContainer *container,
1655 GtkBoxPrivate *priv = GTK_BOX (container)->priv;
1657 gtk_box_pack_start (GTK_BOX (container), widget,
1658 priv->default_expand,
1664 gtk_box_remove (GtkContainer *container,
1667 GtkBox *box = GTK_BOX (container);
1668 GtkBoxPrivate *priv = box->priv;
1672 children = priv->children;
1675 child = children->data;
1677 if (child->widget == widget)
1679 gboolean was_visible;
1681 was_visible = gtk_widget_get_visible (widget);
1682 gtk_widget_unparent (widget);
1684 priv->children = g_list_remove_link (priv->children, children);
1685 g_list_free (children);
1688 /* queue resize regardless of gtk_widget_get_visible (container),
1689 * since that's what is needed by toplevels.
1692 gtk_widget_queue_resize (GTK_WIDGET (container));
1697 children = children->next;
1702 gtk_box_forall (GtkContainer *container,
1703 gboolean include_internals,
1704 GtkCallback callback,
1705 gpointer callback_data)
1707 GtkBox *box = GTK_BOX (container);
1708 GtkBoxPrivate *priv = box->priv;
1712 children = priv->children;
1715 child = children->data;
1716 children = children->next;
1718 if (child->pack == GTK_PACK_START)
1719 (* callback) (child->widget, callback_data);
1722 children = g_list_last (priv->children);
1725 child = children->data;
1726 children = children->prev;
1728 if (child->pack == GTK_PACK_END)
1729 (* callback) (child->widget, callback_data);
1734 _gtk_box_get_children (GtkBox *box)
1736 GtkBoxPrivate *priv;
1739 GList *retval = NULL;
1741 g_return_val_if_fail (GTK_IS_BOX (box), NULL);
1745 children = priv->children;
1748 child = children->data;
1749 children = children->next;
1751 retval = g_list_prepend (retval, child->widget);
1754 return g_list_reverse (retval);