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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A container box
29 * @See_also: #GtkFrame, #GtkGrid, #GtkLayout
31 * The GtkBox widget organizes child widgets into a rectangular area.
33 * The rectangular area of a GtkBox is organized into either a single row
34 * or a single column of child widgets depending upon the orientation.
35 * Thus, all children of a GtkBox are allocated one dimension in common,
36 * which is the height of a row, or the width of a column.
38 * GtkBox uses a notion of <emphasis>packing</emphasis>. Packing refers
39 * to adding widgets with reference to a particular position in a
40 * #GtkContainer. For a GtkBox, there are two reference positions: the
41 * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
42 * For a vertical #GtkBox, the start is defined as the top of the box and
43 * the end is defined as the bottom. For a horizontal #GtkBox the start
44 * is defined as the left side and the end is defined as the right side.
46 * Use repeated calls to gtk_box_pack_start() to pack widgets into a
47 * GtkBox from start to end. Use gtk_box_pack_end() to add widgets from
48 * end to start. You may intersperse these calls and add widgets from
49 * both ends of the same GtkBox.
51 * Because GtkBox is a #GtkContainer, you may also use gtk_container_add()
52 * to insert widgets into the box, and they will be packed with the default
53 * values for #GtkBox:expand and #GtkBox:fill. Use gtk_container_remove()
54 * to remove widgets from the GtkBox.
56 * Use gtk_box_set_homogeneous() to specify whether or not all children
57 * of the GtkBox are forced to get the same amount of space.
59 * Use gtk_box_set_spacing() to determine how much space will be
60 * minimally placed between all children in the GtkBox. Note that
61 * spacing is added <emphasis>between</emphasis> the children, while
62 * padding added by gtk_box_pack_start() or gtk_box_pack_end() is added
63 * <emphasis>on either side</emphasis> of the widget it belongs to.
65 * Use gtk_box_reorder_child() to move a GtkBox child to a different
68 * Use gtk_box_set_child_packing() to reset the #GtkBox:expand,
69 * #GtkBox:fill and #GtkBox:padding child properties.
70 * Use gtk_box_query_child_packing() to query these fields.
73 * Note that a single-row or single-column #GtkGrid provides exactly
74 * the same functionality as #GtkBox.
81 #include "gtkboxprivate.h"
83 #include "gtkorientable.h"
84 #include "gtkorientableprivate.h"
85 #include "gtkprivate.h"
86 #include "gtktypebuiltins.h"
87 #include "gtksizerequest.h"
88 #include "gtkwidgetpath.h"
89 #include "gtkwidgetprivate.h"
90 #include "a11y/gtkcontaineraccessible.h"
105 CHILD_PROP_PACK_TYPE,
109 struct _GtkBoxPrivate
113 GtkOrientation orientation;
116 guint default_expand : 1;
117 guint homogeneous : 1;
118 guint spacing_set : 1;
121 typedef struct _GtkBoxChild GtkBoxChild;
125 * @widget: the child widget, packed into the GtkBox.
126 * @padding: the number of extra pixels to put between this child and its
127 * neighbors, set when packed, zero by default.
128 * @expand: flag indicates whether extra space should be given to this child.
129 * Any extra space given to the parent GtkBox is divided up among all children
130 * with this attribute set to %TRUE; set when packed, %TRUE by default.
131 * @fill: flag indicates whether any extra space given to this child due to its
132 * @expand attribute being set is actually allocated to the child, rather than
133 * being used as padding around the widget; set when packed, %TRUE by default.
134 * @pack: one of #GtkPackType indicating whether the child is packed with
135 * reference to the start (top/left) or end (bottom/right) of the GtkBox.
148 static void gtk_box_size_allocate (GtkWidget *widget,
149 GtkAllocation *allocation);
151 static void gtk_box_compute_expand (GtkWidget *widget,
155 static void gtk_box_set_property (GObject *object,
159 static void gtk_box_get_property (GObject *object,
163 static void gtk_box_add (GtkContainer *container,
165 static void gtk_box_remove (GtkContainer *container,
167 static void gtk_box_forall (GtkContainer *container,
168 gboolean include_internals,
169 GtkCallback callback,
170 gpointer callback_data);
171 static void gtk_box_set_child_property (GtkContainer *container,
176 static void gtk_box_get_child_property (GtkContainer *container,
181 static GType gtk_box_child_type (GtkContainer *container);
182 static GtkWidgetPath * gtk_box_get_path_for_child
183 (GtkContainer *container,
187 static void gtk_box_get_preferred_width (GtkWidget *widget,
190 static void gtk_box_get_preferred_height (GtkWidget *widget,
193 static void gtk_box_get_preferred_width_for_height (GtkWidget *widget,
196 gint *natural_width);
197 static void gtk_box_get_preferred_height_for_width (GtkWidget *widget,
199 gint *minimum_height,
200 gint *natural_height);
203 G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
204 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
208 gtk_box_class_init (GtkBoxClass *class)
210 GObjectClass *object_class = G_OBJECT_CLASS (class);
211 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
212 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
214 object_class->set_property = gtk_box_set_property;
215 object_class->get_property = gtk_box_get_property;
217 widget_class->size_allocate = gtk_box_size_allocate;
218 widget_class->get_preferred_width = gtk_box_get_preferred_width;
219 widget_class->get_preferred_height = gtk_box_get_preferred_height;
220 widget_class->get_preferred_height_for_width = gtk_box_get_preferred_height_for_width;
221 widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
222 widget_class->compute_expand = gtk_box_compute_expand;
224 container_class->add = gtk_box_add;
225 container_class->remove = gtk_box_remove;
226 container_class->forall = gtk_box_forall;
227 container_class->child_type = gtk_box_child_type;
228 container_class->set_child_property = gtk_box_set_child_property;
229 container_class->get_child_property = gtk_box_get_child_property;
230 container_class->get_path_for_child = gtk_box_get_path_for_child;
231 gtk_container_class_handle_border_width (container_class);
233 g_object_class_override_property (object_class,
237 g_object_class_install_property (object_class,
239 g_param_spec_int ("spacing",
241 P_("The amount of space between children"),
245 GTK_PARAM_READWRITE));
247 g_object_class_install_property (object_class,
249 g_param_spec_boolean ("homogeneous",
251 P_("Whether the children should all be the same size"),
253 GTK_PARAM_READWRITE));
258 * Whether the child should receive extra space when the parent grows.
260 * Note that the default value for this property is %FALSE for GtkBox,
261 * but #GtkHBox, #GtkVBox and other subclasses use the old default
264 * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
265 * and #GtkWidget:vexpand properties are the preferred way to influence
266 * child size allocation in containers.
268 gtk_container_class_install_child_property (container_class,
270 g_param_spec_boolean ("expand",
272 P_("Whether the child should receive extra space when the parent grows"),
274 GTK_PARAM_READWRITE));
279 * Whether the child should receive extra space when the parent grows.
281 * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
282 * and #GtkWidget:vexpand properties are the preferred way to influence
283 * child size allocation in containers.
285 gtk_container_class_install_child_property (container_class,
287 g_param_spec_boolean ("fill",
289 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
291 GTK_PARAM_READWRITE));
293 gtk_container_class_install_child_property (container_class,
295 g_param_spec_uint ("padding",
297 P_("Extra space to put between the child and its neighbors, in pixels"),
299 GTK_PARAM_READWRITE));
300 gtk_container_class_install_child_property (container_class,
301 CHILD_PROP_PACK_TYPE,
302 g_param_spec_enum ("pack-type",
304 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
305 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
306 GTK_PARAM_READWRITE));
307 gtk_container_class_install_child_property (container_class,
309 g_param_spec_int ("position",
311 P_("The index of the child in the parent"),
313 GTK_PARAM_READWRITE));
315 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
317 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILLER);
321 gtk_box_init (GtkBox *box)
323 GtkBoxPrivate *private;
325 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
330 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
331 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
333 private->orientation = GTK_ORIENTATION_HORIZONTAL;
334 private->children = NULL;
336 private->default_expand = FALSE;
337 private->homogeneous = FALSE;
338 private->spacing = 0;
339 private->spacing_set = FALSE;
343 gtk_box_set_property (GObject *object,
348 GtkBox *box = GTK_BOX (object);
349 GtkBoxPrivate *private = box->priv;
353 case PROP_ORIENTATION:
354 private->orientation = g_value_get_enum (value);
355 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
356 gtk_widget_queue_resize (GTK_WIDGET (box));
359 gtk_box_set_spacing (box, g_value_get_int (value));
361 case PROP_HOMOGENEOUS:
362 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gtk_box_get_property (GObject *object,
376 GtkBox *box = GTK_BOX (object);
377 GtkBoxPrivate *private = box->priv;
381 case PROP_ORIENTATION:
382 g_value_set_enum (value, private->orientation);
385 g_value_set_int (value, private->spacing);
387 case PROP_HOMOGENEOUS:
388 g_value_set_boolean (value, private->homogeneous);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 count_expand_children (GtkBox *box,
399 gint *visible_children,
400 gint *expand_children)
402 GtkBoxPrivate *private = box->priv;
406 *visible_children = *expand_children = 0;
408 for (children = private->children; children; children = children->next)
410 child = children->data;
412 if (gtk_widget_get_visible (child->widget))
414 *visible_children += 1;
415 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
416 *expand_children += 1;
422 gtk_box_size_allocate (GtkWidget *widget,
423 GtkAllocation *allocation)
425 GtkBox *box = GTK_BOX (widget);
426 GtkBoxPrivate *private = box->priv;
430 gint nexpand_children;
432 GtkTextDirection direction;
433 GtkAllocation child_allocation;
434 GtkRequestedSize *sizes;
440 gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
441 gint x = 0, y = 0, i;
445 gtk_widget_set_allocation (widget, allocation);
447 count_expand_children (box, &nvis_children, &nexpand_children);
449 /* If there is no visible child, simply return. */
450 if (nvis_children <= 0)
453 direction = gtk_widget_get_direction (widget);
454 sizes = g_newa (GtkRequestedSize, nvis_children);
456 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
457 size = allocation->width - (nvis_children - 1) * private->spacing;
459 size = allocation->height - (nvis_children - 1) * private->spacing;
461 /* Retrieve desired size for visible children. */
462 for (i = 0, children = private->children; children; children = children->next)
464 child = children->data;
466 if (!gtk_widget_get_visible (child->widget))
469 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
470 gtk_widget_get_preferred_width_for_height (child->widget,
472 &sizes[i].minimum_size,
473 &sizes[i].natural_size);
475 gtk_widget_get_preferred_height_for_width (child->widget,
477 &sizes[i].minimum_size,
478 &sizes[i].natural_size);
481 /* Assert the api is working properly */
482 if (sizes[i].minimum_size < 0)
483 g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
484 gtk_widget_get_name (GTK_WIDGET (child->widget)),
485 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
486 sizes[i].minimum_size,
487 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
488 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
490 if (sizes[i].natural_size < sizes[i].minimum_size)
491 g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
492 gtk_widget_get_name (GTK_WIDGET (child->widget)),
493 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
494 sizes[i].natural_size,
495 sizes[i].minimum_size,
496 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
497 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
499 size -= sizes[i].minimum_size;
500 size -= child->padding * 2;
502 sizes[i].data = child;
507 if (private->homogeneous)
509 /* If were homogenous we still need to run the above loop to get the
510 * minimum sizes for children that are not going to fill
512 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
513 size = allocation->width - (nvis_children - 1) * private->spacing;
515 size = allocation->height - (nvis_children - 1) * private->spacing;
517 extra = size / nvis_children;
518 n_extra_widgets = size % nvis_children;
522 /* Bring children up to size first */
523 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
525 /* Calculate space which hasn't distributed yet,
526 * and is available for expanding children.
528 if (nexpand_children > 0)
530 extra = size / nexpand_children;
531 n_extra_widgets = size % nexpand_children;
537 /* Allocate child positions. */
538 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
540 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
542 child_allocation.y = allocation->y;
543 child_allocation.height = MAX (1, allocation->height);
544 if (packing == GTK_PACK_START)
547 x = allocation->x + allocation->width;
551 child_allocation.x = allocation->x;
552 child_allocation.width = MAX (1, allocation->width);
553 if (packing == GTK_PACK_START)
556 y = allocation->y + allocation->height;
559 for (i = 0, children = private->children;
561 children = children->next)
563 child = children->data;
565 /* If widget is not visible, skip it. */
566 if (!gtk_widget_get_visible (child->widget))
569 /* If widget is packed differently skip it, but still increment i,
570 * since widget is visible and will be handled in next loop iteration.
572 if (child->pack != packing)
578 /* Assign the child's size. */
579 if (private->homogeneous)
583 if (n_extra_widgets > 0)
591 child_size = sizes[i].minimum_size + child->padding * 2;
593 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
597 if (n_extra_widgets > 0)
605 /* Assign the child's position. */
606 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
610 child_allocation.width = MAX (1, child_size - child->padding * 2);
611 child_allocation.x = x + child->padding;
615 child_allocation.width = sizes[i].minimum_size;
616 child_allocation.x = x + (child_size - child_allocation.width) / 2;
619 if (packing == GTK_PACK_START)
621 x += child_size + private->spacing;
625 x -= child_size + private->spacing;
627 child_allocation.x -= child_size;
630 if (direction == GTK_TEXT_DIR_RTL)
631 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
634 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
638 child_allocation.height = MAX (1, child_size - child->padding * 2);
639 child_allocation.y = y + child->padding;
643 child_allocation.height = sizes[i].minimum_size;
644 child_allocation.y = y + (child_size - child_allocation.height) / 2;
647 if (packing == GTK_PACK_START)
649 y += child_size + private->spacing;
653 y -= child_size + private->spacing;
655 child_allocation.y -= child_size;
658 gtk_widget_size_allocate (child->widget, &child_allocation);
666 gtk_box_compute_expand (GtkWidget *widget,
670 GtkBoxPrivate *private = GTK_BOX (widget)->priv;
674 gboolean opposite_expand;
675 GtkOrientation opposite_orientation;
677 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
678 opposite_orientation = GTK_ORIENTATION_VERTICAL;
680 opposite_orientation = GTK_ORIENTATION_HORIZONTAL;
683 opposite_expand = FALSE;
685 for (children = private->children; children; children = children->next)
687 child = children->data;
689 /* we don't recurse into children anymore as soon as we know
690 * expand=TRUE in an orientation
693 if (child->expand || (!our_expand && gtk_widget_compute_expand (child->widget, private->orientation)))
696 if (!opposite_expand && gtk_widget_compute_expand (child->widget, opposite_orientation))
697 opposite_expand = TRUE;
699 if (our_expand && opposite_expand)
703 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
705 *hexpand_p = our_expand;
706 *vexpand_p = opposite_expand;
710 *hexpand_p = opposite_expand;
711 *vexpand_p = our_expand;
716 gtk_box_child_type (GtkContainer *container)
718 return GTK_TYPE_WIDGET;
722 gtk_box_set_child_property (GtkContainer *container,
731 GtkPackType pack_type = 0;
733 if (property_id != CHILD_PROP_POSITION)
734 gtk_box_query_child_packing (GTK_BOX (container),
742 case CHILD_PROP_EXPAND:
743 gtk_box_set_child_packing (GTK_BOX (container),
745 g_value_get_boolean (value),
750 case CHILD_PROP_FILL:
751 gtk_box_set_child_packing (GTK_BOX (container),
754 g_value_get_boolean (value),
758 case CHILD_PROP_PADDING:
759 gtk_box_set_child_packing (GTK_BOX (container),
763 g_value_get_uint (value),
766 case CHILD_PROP_PACK_TYPE:
767 gtk_box_set_child_packing (GTK_BOX (container),
772 g_value_get_enum (value));
774 case CHILD_PROP_POSITION:
775 gtk_box_reorder_child (GTK_BOX (container),
777 g_value_get_int (value));
780 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
786 gtk_box_get_child_property (GtkContainer *container,
792 gboolean expand = FALSE;
793 gboolean fill = FALSE;
795 GtkPackType pack_type = 0;
798 if (property_id != CHILD_PROP_POSITION)
799 gtk_box_query_child_packing (GTK_BOX (container),
808 case CHILD_PROP_EXPAND:
809 g_value_set_boolean (value, expand);
811 case CHILD_PROP_FILL:
812 g_value_set_boolean (value, fill);
814 case CHILD_PROP_PADDING:
815 g_value_set_uint (value, padding);
817 case CHILD_PROP_PACK_TYPE:
818 g_value_set_enum (value, pack_type);
820 case CHILD_PROP_POSITION:
822 for (list = GTK_BOX (container)->priv->children; list; list = list->next)
824 GtkBoxChild *child_entry;
826 child_entry = list->data;
827 if (child_entry->widget == child)
831 g_value_set_int (value, list ? i : -1);
834 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
839 typedef struct _CountingData CountingData;
840 struct _CountingData {
848 count_widget_position (GtkWidget *widget,
851 CountingData *count = data;
853 if (!gtk_widget_get_visible (widget))
856 if (count->widget == widget)
858 else if (!count->found)
863 gtk_box_get_visible_position (GtkBox *box,
866 CountingData count = { child, FALSE, 0, 0 };
868 /* foreach iterates in visible order */
869 gtk_container_foreach (GTK_CONTAINER (box),
870 count_widget_position,
873 /* the child wasn't found, it's likely an internal child of some
874 * subclass, return -1 to indicate that there is no sibling relation
875 * to the regular box children
883 static GtkWidgetPath *
884 gtk_box_get_path_for_child (GtkContainer *container,
887 GtkWidgetPath *path, *sibling_path;
889 GList *list, *children;
891 box = GTK_BOX (container);
893 path = _gtk_widget_create_path (GTK_WIDGET (container));
895 if (gtk_widget_get_visible (child))
899 sibling_path = gtk_widget_path_new ();
901 /* get_children works in visible order */
902 children = gtk_container_get_children (container);
904 for (list = children; list; list = list->next)
906 if (!gtk_widget_get_visible (list->data))
909 gtk_widget_path_append_for_widget (sibling_path, list->data);
912 g_list_free (children);
914 position = gtk_box_get_visible_position (box, child);
917 gtk_widget_path_append_with_siblings (path, sibling_path, position);
919 gtk_widget_path_append_for_widget (path, child);
921 gtk_widget_path_unref (sibling_path);
924 gtk_widget_path_append_for_widget (path, child);
930 gtk_box_invalidate_order_foreach (GtkWidget *widget)
932 _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
936 gtk_box_invalidate_order (GtkBox *box)
938 gtk_container_foreach (GTK_CONTAINER (box),
939 (GtkCallback) gtk_box_invalidate_order_foreach,
944 box_child_visibility_notify_cb (GObject *obj,
948 GtkBox *box = user_data;
950 gtk_box_invalidate_order (box);
954 gtk_box_pack (GtkBox *box,
959 GtkPackType pack_type)
961 GtkBoxPrivate *private = box->priv;
962 GtkBoxChild *child_info;
964 g_return_if_fail (GTK_IS_BOX (box));
965 g_return_if_fail (GTK_IS_WIDGET (child));
966 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
968 child_info = g_new (GtkBoxChild, 1);
969 child_info->widget = child;
970 child_info->padding = padding;
971 child_info->expand = expand ? TRUE : FALSE;
972 child_info->fill = fill ? TRUE : FALSE;
973 child_info->pack = pack_type;
975 private->children = g_list_append (private->children, child_info);
977 gtk_widget_freeze_child_notify (child);
979 gtk_box_invalidate_order (box);
980 gtk_widget_set_parent (child, GTK_WIDGET (box));
982 g_signal_connect (child, "notify::visible",
983 G_CALLBACK (box_child_visibility_notify_cb), box);
985 gtk_widget_child_notify (child, "expand");
986 gtk_widget_child_notify (child, "fill");
987 gtk_widget_child_notify (child, "padding");
988 gtk_widget_child_notify (child, "pack-type");
989 gtk_widget_child_notify (child, "position");
990 gtk_widget_thaw_child_notify (child);
994 gtk_box_get_size (GtkWidget *widget,
995 GtkOrientation orientation,
1000 GtkBoxPrivate *private;
1003 gint minimum, natural;
1005 box = GTK_BOX (widget);
1006 private = box->priv;
1008 minimum = natural = 0;
1012 for (children = private->children; children; children = children->next)
1014 GtkBoxChild *child = children->data;
1016 if (gtk_widget_get_visible (child->widget))
1018 gint child_minimum, child_natural;
1020 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1021 gtk_widget_get_preferred_width (child->widget,
1022 &child_minimum, &child_natural);
1024 gtk_widget_get_preferred_height (child->widget,
1025 &child_minimum, &child_natural);
1027 if (private->orientation == orientation)
1029 if (private->homogeneous)
1033 largest = child_minimum + child->padding * 2;
1034 minimum = MAX (minimum, largest);
1036 largest = child_natural + child->padding * 2;
1037 natural = MAX (natural, largest);
1041 minimum += child_minimum + child->padding * 2;
1042 natural += child_natural + child->padding * 2;
1047 /* The biggest mins and naturals in the opposing orientation */
1048 minimum = MAX (minimum, child_minimum);
1049 natural = MAX (natural, child_natural);
1056 if (nvis_children > 0 && private->orientation == orientation)
1058 if (private->homogeneous)
1060 minimum *= nvis_children;
1061 natural *= nvis_children;
1063 minimum += (nvis_children - 1) * private->spacing;
1064 natural += (nvis_children - 1) * private->spacing;
1068 *minimum_size = minimum;
1071 *natural_size = natural;
1075 gtk_box_get_preferred_width (GtkWidget *widget,
1079 gtk_box_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1083 gtk_box_get_preferred_height (GtkWidget *widget,
1087 gtk_box_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1091 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
1096 GtkBoxPrivate *private = box->priv;
1100 gint nexpand_children;
1101 gint computed_minimum = 0, computed_natural = 0;
1102 GtkRequestedSize *sizes;
1103 GtkPackType packing;
1104 gint size, extra, i;
1105 gint child_size, child_minimum, child_natural;
1106 gint n_extra_widgets = 0;
1108 count_expand_children (box, &nvis_children, &nexpand_children);
1110 if (nvis_children <= 0)
1113 sizes = g_newa (GtkRequestedSize, nvis_children);
1114 size = avail_size - (nvis_children - 1) * private->spacing;
1116 /* Retrieve desired size for visible children */
1117 for (i = 0, children = private->children; children; children = children->next)
1119 child = children->data;
1121 if (gtk_widget_get_visible (child->widget))
1123 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1124 gtk_widget_get_preferred_width (child->widget,
1125 &sizes[i].minimum_size,
1126 &sizes[i].natural_size);
1128 gtk_widget_get_preferred_height (child->widget,
1129 &sizes[i].minimum_size,
1130 &sizes[i].natural_size);
1132 /* Assert the api is working properly */
1133 if (sizes[i].minimum_size < 0)
1134 g_error ("GtkBox child %s minimum %s: %d < 0",
1135 gtk_widget_get_name (GTK_WIDGET (child->widget)),
1136 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1137 sizes[i].minimum_size);
1139 if (sizes[i].natural_size < sizes[i].minimum_size)
1140 g_error ("GtkBox child %s natural %s: %d < minimum %d",
1141 gtk_widget_get_name (GTK_WIDGET (child->widget)),
1142 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1143 sizes[i].natural_size,
1144 sizes[i].minimum_size);
1146 size -= sizes[i].minimum_size;
1147 size -= child->padding * 2;
1149 sizes[i].data = child;
1155 if (private->homogeneous)
1157 /* If were homogenous we still need to run the above loop to get the
1158 * minimum sizes for children that are not going to fill
1160 size = avail_size - (nvis_children - 1) * private->spacing;
1161 extra = size / nvis_children;
1162 n_extra_widgets = size % nvis_children;
1166 /* Bring children up to size first */
1167 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
1169 /* Calculate space which hasn't distributed yet,
1170 * and is available for expanding children.
1172 if (nexpand_children > 0)
1174 extra = size / nexpand_children;
1175 n_extra_widgets = size % nexpand_children;
1181 /* Allocate child positions. */
1182 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1184 for (i = 0, children = private->children;
1186 children = children->next)
1188 child = children->data;
1190 /* If widget is not visible, skip it. */
1191 if (!gtk_widget_get_visible (child->widget))
1194 /* If widget is packed differently skip it, but still increment i,
1195 * since widget is visible and will be handled in next loop iteration.
1197 if (child->pack != packing)
1203 if (child->pack == packing)
1205 /* Assign the child's size. */
1206 if (private->homogeneous)
1210 if (n_extra_widgets > 0)
1218 child_size = sizes[i].minimum_size + child->padding * 2;
1220 if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
1222 child_size += extra;
1224 if (n_extra_widgets > 0)
1234 child_size = MAX (1, child_size - child->padding * 2);
1238 child_size = sizes[i].minimum_size;
1242 /* Assign the child's position. */
1243 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1244 gtk_widget_get_preferred_height_for_width (child->widget,
1245 child_size, &child_minimum, &child_natural);
1246 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1247 gtk_widget_get_preferred_width_for_height (child->widget,
1248 child_size, &child_minimum, &child_natural);
1251 computed_minimum = MAX (computed_minimum, child_minimum);
1252 computed_natural = MAX (computed_natural, child_natural);
1259 *minimum_size = computed_minimum;
1261 *natural_size = computed_natural;
1265 gtk_box_compute_size_for_orientation (GtkBox *box,
1270 GtkBoxPrivate *private = box->priv;
1272 gint nvis_children = 0;
1273 gint required_size = 0, required_natural = 0, child_size, child_natural;
1274 gint largest_child = 0, largest_natural = 0;
1276 for (children = private->children; children != NULL;
1277 children = children->next)
1279 GtkBoxChild *child = children->data;
1281 if (gtk_widget_get_visible (child->widget))
1284 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1285 gtk_widget_get_preferred_width_for_height (child->widget,
1286 avail_size, &child_size, &child_natural);
1288 gtk_widget_get_preferred_height_for_width (child->widget,
1289 avail_size, &child_size, &child_natural);
1292 child_size += child->padding * 2;
1293 child_natural += child->padding * 2;
1295 if (child_size > largest_child)
1296 largest_child = child_size;
1298 if (child_natural > largest_natural)
1299 largest_natural = child_natural;
1301 required_size += child_size;
1302 required_natural += child_natural;
1308 if (nvis_children > 0)
1310 if (private->homogeneous)
1312 required_size = largest_child * nvis_children;
1313 required_natural = largest_natural * nvis_children;
1316 required_size += (nvis_children - 1) * private->spacing;
1317 required_natural += (nvis_children - 1) * private->spacing;
1321 *minimum_size = required_size;
1324 *natural_size = required_natural;
1328 gtk_box_get_preferred_width_for_height (GtkWidget *widget,
1330 gint *minimum_width,
1331 gint *natural_width)
1333 GtkBox *box = GTK_BOX (widget);
1334 GtkBoxPrivate *private = box->priv;
1336 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1337 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1339 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1343 gtk_box_get_preferred_height_for_width (GtkWidget *widget,
1345 gint *minimum_height,
1346 gint *natural_height)
1348 GtkBox *box = GTK_BOX (widget);
1349 GtkBoxPrivate *private = box->priv;
1351 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1352 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1354 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1359 * @orientation: the box's orientation.
1360 * @spacing: the number of pixels to place by default between children.
1362 * Creates a new #GtkBox.
1364 * Return value: a new #GtkBox.
1369 gtk_box_new (GtkOrientation orientation,
1372 return g_object_new (GTK_TYPE_BOX,
1373 "orientation", orientation,
1379 * gtk_box_pack_start:
1381 * @child: the #GtkWidget to be added to @box
1382 * @expand: %TRUE if the new child is to be given extra space allocated
1383 * to @box. The extra space will be divided evenly between all children
1384 * that use this option
1385 * @fill: %TRUE if space given to @child by the @expand option is
1386 * actually allocated to @child, rather than just padding it. This
1387 * parameter has no effect if @expand is set to %FALSE. A child is
1388 * always allocated the full height of a horizontal #GtkBox and the full width
1389 * of a vertical #GtkBox. This option affects the other dimension
1390 * @padding: extra space in pixels to put between this child and its
1391 * neighbors, over and above the global amount specified by
1392 * #GtkBox:spacing property. If @child is a widget at one of the
1393 * reference ends of @box, then @padding pixels are also put between
1394 * @child and the reference edge of @box
1396 * Adds @child to @box, packed with reference to the start of @box.
1397 * The @child is packed after any other child packed with reference
1398 * to the start of @box.
1401 gtk_box_pack_start (GtkBox *box,
1407 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1413 * @child: the #GtkWidget to be added to @box
1414 * @expand: %TRUE if the new child is to be given extra space allocated
1415 * to @box. The extra space will be divided evenly between all children
1416 * of @box that use this option
1417 * @fill: %TRUE if space given to @child by the @expand option is
1418 * actually allocated to @child, rather than just padding it. This
1419 * parameter has no effect if @expand is set to %FALSE. A child is
1420 * always allocated the full height of a horizontal #GtkBox and the full width
1421 * of a vertical #GtkBox. This option affects the other dimension
1422 * @padding: extra space in pixels to put between this child and its
1423 * neighbors, over and above the global amount specified by
1424 * #GtkBox:spacing property. If @child is a widget at one of the
1425 * reference ends of @box, then @padding pixels are also put between
1426 * @child and the reference edge of @box
1428 * Adds @child to @box, packed with reference to the end of @box.
1429 * The @child is packed after (away from end of) any other child
1430 * packed with reference to the end of @box.
1433 gtk_box_pack_end (GtkBox *box,
1439 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1443 * gtk_box_set_homogeneous:
1445 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1446 * %FALSE for variable allotments
1448 * Sets the #GtkBox:homogeneous property of @box, controlling
1449 * whether or not all children of @box are given equal space
1453 gtk_box_set_homogeneous (GtkBox *box,
1454 gboolean homogeneous)
1456 GtkBoxPrivate *private;
1458 g_return_if_fail (GTK_IS_BOX (box));
1460 private = box->priv;
1462 if ((homogeneous ? TRUE : FALSE) != private->homogeneous)
1464 private->homogeneous = homogeneous ? TRUE : FALSE;
1465 g_object_notify (G_OBJECT (box), "homogeneous");
1466 gtk_widget_queue_resize (GTK_WIDGET (box));
1471 * gtk_box_get_homogeneous:
1474 * Returns whether the box is homogeneous (all children are the
1475 * same size). See gtk_box_set_homogeneous().
1477 * Return value: %TRUE if the box is homogeneous.
1480 gtk_box_get_homogeneous (GtkBox *box)
1482 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1484 return box->priv->homogeneous;
1488 * gtk_box_set_spacing:
1490 * @spacing: the number of pixels to put between children
1492 * Sets the #GtkBox:spacing property of @box, which is the
1493 * number of pixels to place between children of @box.
1496 gtk_box_set_spacing (GtkBox *box,
1499 GtkBoxPrivate *private;
1501 g_return_if_fail (GTK_IS_BOX (box));
1503 private = box->priv;
1505 if (spacing != private->spacing)
1507 private->spacing = spacing;
1508 _gtk_box_set_spacing_set (box, TRUE);
1510 g_object_notify (G_OBJECT (box), "spacing");
1512 gtk_widget_queue_resize (GTK_WIDGET (box));
1517 * gtk_box_get_spacing:
1520 * Gets the value set by gtk_box_set_spacing().
1522 * Return value: spacing between children
1525 gtk_box_get_spacing (GtkBox *box)
1527 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1529 return box->priv->spacing;
1533 _gtk_box_set_spacing_set (GtkBox *box,
1534 gboolean spacing_set)
1536 GtkBoxPrivate *private;
1538 g_return_if_fail (GTK_IS_BOX (box));
1540 private = box->priv;
1542 private->spacing_set = spacing_set ? TRUE : FALSE;
1546 _gtk_box_get_spacing_set (GtkBox *box)
1548 GtkBoxPrivate *private;
1550 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1552 private = box->priv;
1554 return private->spacing_set;
1558 * gtk_box_reorder_child:
1560 * @child: the #GtkWidget to move
1561 * @position: the new position for @child in the list of children
1562 * of @box, starting from 0. If negative, indicates the end of
1565 * Moves @child to a new @position in the list of @box children.
1566 * The list is the <structfield>children</structfield> field of
1567 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1568 * as well as widgets packed #GTK_PACK_END, in the order that these
1569 * widgets were added to @box.
1571 * A widget's position in the @box children list determines where
1572 * the widget is packed into @box. A child widget at some position
1573 * in the list will be packed just after all other widgets of the
1574 * same packing type that appear earlier in the list.
1577 gtk_box_reorder_child (GtkBox *box,
1581 GtkBoxPrivate *priv;
1584 GtkBoxChild *child_info = NULL;
1587 g_return_if_fail (GTK_IS_BOX (box));
1588 g_return_if_fail (GTK_IS_WIDGET (child));
1592 old_link = priv->children;
1596 child_info = old_link->data;
1597 if (child_info->widget == child)
1600 old_link = old_link->next;
1604 g_return_if_fail (old_link != NULL);
1606 if (position == old_position)
1609 priv->children = g_list_delete_link (priv->children, old_link);
1614 new_link = g_list_nth (priv->children, position);
1616 priv->children = g_list_insert_before (priv->children, new_link, child_info);
1618 gtk_widget_child_notify (child, "position");
1619 if (gtk_widget_get_visible (child)
1620 && gtk_widget_get_visible (GTK_WIDGET (box)))
1622 gtk_box_invalidate_order (box);
1623 gtk_widget_queue_resize (child);
1628 * gtk_box_query_child_packing:
1630 * @child: the #GtkWidget of the child to query
1631 * @expand: (out): pointer to return location for #GtkBox:expand child
1633 * @fill: (out): pointer to return location for #GtkBox:fill child
1635 * @padding: (out): pointer to return location for #GtkBox:padding
1637 * @pack_type: (out): pointer to return location for #GtkBox:pack-type
1640 * Obtains information about how @child is packed into @box.
1643 gtk_box_query_child_packing (GtkBox *box,
1648 GtkPackType *pack_type)
1650 GtkBoxPrivate *private;
1652 GtkBoxChild *child_info = NULL;
1654 g_return_if_fail (GTK_IS_BOX (box));
1655 g_return_if_fail (GTK_IS_WIDGET (child));
1657 private = box->priv;
1659 list = private->children;
1662 child_info = list->data;
1663 if (child_info->widget == child)
1672 *expand = child_info->expand;
1674 *fill = child_info->fill;
1676 *padding = child_info->padding;
1678 *pack_type = child_info->pack;
1683 * gtk_box_set_child_packing:
1685 * @child: the #GtkWidget of the child to set
1686 * @expand: the new value of the #GtkBox:expand child property
1687 * @fill: the new value of the #GtkBox:fill child property
1688 * @padding: the new value of the #GtkBox:padding child property
1689 * @pack_type: the new value of the #GtkBox:pack-type child property
1691 * Sets the way @child is packed into @box.
1694 gtk_box_set_child_packing (GtkBox *box,
1699 GtkPackType pack_type)
1701 GtkBoxPrivate *private;
1703 GtkBoxChild *child_info = NULL;
1705 g_return_if_fail (GTK_IS_BOX (box));
1706 g_return_if_fail (GTK_IS_WIDGET (child));
1708 private = box->priv;
1710 list = private->children;
1713 child_info = list->data;
1714 if (child_info->widget == child)
1720 gtk_widget_freeze_child_notify (child);
1725 expanded = expand != FALSE;
1727 /* avoid setting expand if unchanged, since queue_compute_expand
1728 * can be expensive-ish
1730 if (child_info->expand != expanded)
1732 child_info->expand = expand != FALSE;
1733 gtk_widget_queue_compute_expand (GTK_WIDGET (box));
1734 gtk_widget_child_notify (child, "expand");
1737 child_info->fill = fill != FALSE;
1738 gtk_widget_child_notify (child, "fill");
1739 child_info->padding = padding;
1740 gtk_widget_child_notify (child, "padding");
1741 if (pack_type != GTK_PACK_END)
1742 pack_type = GTK_PACK_START;
1743 if (child_info->pack != pack_type)
1745 child_info->pack = pack_type;
1746 gtk_widget_child_notify (child, "pack-type");
1747 gtk_box_invalidate_order (box);
1750 if (gtk_widget_get_visible (child)
1751 && gtk_widget_get_visible (GTK_WIDGET (box)))
1752 gtk_widget_queue_resize (child);
1754 gtk_widget_thaw_child_notify (child);
1758 _gtk_box_set_old_defaults (GtkBox *box)
1760 GtkBoxPrivate *private;
1762 g_return_if_fail (GTK_IS_BOX (box));
1764 private = box->priv;
1766 private->default_expand = TRUE;
1770 gtk_box_add (GtkContainer *container,
1773 GtkBoxPrivate *priv = GTK_BOX (container)->priv;
1775 gtk_box_pack_start (GTK_BOX (container), widget,
1776 priv->default_expand,
1782 gtk_box_remove (GtkContainer *container,
1785 GtkBox *box = GTK_BOX (container);
1786 GtkBoxPrivate *priv = box->priv;
1790 children = priv->children;
1793 child = children->data;
1795 if (child->widget == widget)
1797 gboolean was_visible;
1799 g_signal_handlers_disconnect_by_func (widget,
1800 box_child_visibility_notify_cb,
1803 was_visible = gtk_widget_get_visible (widget);
1804 gtk_widget_unparent (widget);
1806 priv->children = g_list_remove_link (priv->children, children);
1807 g_list_free (children);
1810 /* queue resize regardless of gtk_widget_get_visible (container),
1811 * since that's what is needed by toplevels.
1815 gtk_box_invalidate_order (box);
1816 gtk_widget_queue_resize (GTK_WIDGET (container));
1822 children = children->next;
1827 gtk_box_forall (GtkContainer *container,
1828 gboolean include_internals,
1829 GtkCallback callback,
1830 gpointer callback_data)
1832 GtkBox *box = GTK_BOX (container);
1833 GtkBoxPrivate *priv = box->priv;
1837 children = priv->children;
1840 child = children->data;
1841 children = children->next;
1843 if (child->pack == GTK_PACK_START)
1844 (* callback) (child->widget, callback_data);
1847 children = g_list_last (priv->children);
1850 child = children->data;
1851 children = children->prev;
1853 if (child->pack == GTK_PACK_END)
1854 (* callback) (child->widget, callback_data);
1859 _gtk_box_get_children (GtkBox *box)
1861 GtkBoxPrivate *priv;
1864 GList *retval = NULL;
1866 g_return_val_if_fail (GTK_IS_BOX (box), NULL);
1870 children = priv->children;
1873 child = children->data;
1874 children = children->next;
1876 retval = g_list_prepend (retval, child->widget);
1879 return g_list_reverse (retval);