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 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.
68 * Use gtk_box_reorder_child() to move a GtkBox child to a different
71 * Use gtk_box_set_child_packing() to reset the #GtkBox:expand,
72 * #GtkBox:fill and #GtkBox:padding child properties.
73 * Use gtk_box_query_child_packing() to query these fields.
79 #include "gtkorientable.h"
80 #include "gtksizerequest.h"
81 #include "gtktypeutils.h"
82 #include "gtkprivate.h"
102 struct _GtkBoxPrivate
104 GtkOrientation orientation;
110 guint default_expand : 1;
111 guint homogeneous : 1;
112 guint spacing_set : 1;
115 typedef struct _GtkBoxChild GtkBoxChild;
119 * @widget: the child widget, packed into the GtkBox.
120 * @padding: the number of extra pixels to put between this child and its
121 * neighbors, set when packed, zero by default.
122 * @expand: flag indicates whether extra space should be given to this child.
123 * Any extra space given to the parent GtkBox is divided up among all children
124 * with this attribute set to %TRUE; set when packed, %TRUE by default.
125 * @fill: flag indicates whether any extra space given to this child due to its
126 * @expand attribute being set is actually allocated to the child, rather than
127 * being used as padding around the widget; set when packed, %TRUE by default.
128 * @pack: one of #GtkPackType indicating whether the child is packed with
129 * reference to the start (top/left) or end (bottom/right) of the GtkBox.
142 static void gtk_box_size_allocate (GtkWidget *widget,
143 GtkAllocation *allocation);
145 static void gtk_box_set_property (GObject *object,
149 static void gtk_box_get_property (GObject *object,
154 static void gtk_box_add (GtkContainer *container,
156 static void gtk_box_remove (GtkContainer *container,
158 static void gtk_box_forall (GtkContainer *container,
159 gboolean include_internals,
160 GtkCallback callback,
161 gpointer callback_data);
162 static void gtk_box_set_child_property (GtkContainer *container,
167 static void gtk_box_get_child_property (GtkContainer *container,
172 static GType gtk_box_child_type (GtkContainer *container);
175 static GtkSizeRequestMode gtk_box_get_request_mode (GtkWidget *widget);
176 static void gtk_box_get_preferred_width (GtkWidget *widget,
179 static void gtk_box_get_preferred_height (GtkWidget *widget,
182 static void gtk_box_get_preferred_width_for_height (GtkWidget *widget,
185 gint *natural_width);
186 static void gtk_box_get_preferred_height_for_width (GtkWidget *widget,
188 gint *minimum_height,
189 gint *natural_height);
191 G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
192 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
196 gtk_box_class_init (GtkBoxClass *class)
198 GObjectClass *object_class = G_OBJECT_CLASS (class);
199 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
200 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
202 object_class->set_property = gtk_box_set_property;
203 object_class->get_property = gtk_box_get_property;
205 widget_class->size_allocate = gtk_box_size_allocate;
206 widget_class->get_request_mode = gtk_box_get_request_mode;
207 widget_class->get_preferred_width = gtk_box_get_preferred_width;
208 widget_class->get_preferred_height = gtk_box_get_preferred_height;
209 widget_class->get_preferred_height_for_width = gtk_box_get_preferred_height_for_width;
210 widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
212 container_class->add = gtk_box_add;
213 container_class->remove = gtk_box_remove;
214 container_class->forall = gtk_box_forall;
215 container_class->child_type = gtk_box_child_type;
216 container_class->set_child_property = gtk_box_set_child_property;
217 container_class->get_child_property = gtk_box_get_child_property;
218 gtk_container_class_handle_border_width (container_class);
220 g_object_class_override_property (object_class,
224 g_object_class_install_property (object_class,
226 g_param_spec_int ("spacing",
228 P_("The amount of space between children"),
232 GTK_PARAM_READWRITE));
234 g_object_class_install_property (object_class,
236 g_param_spec_boolean ("homogeneous",
238 P_("Whether the children should all be the same size"),
240 GTK_PARAM_READWRITE));
245 * Whether the child should receive extra space when the parent grows.
247 * Note that the default value for this property is %FALSE for GtkBox,
248 * but #GtkHBox, #GtkVBox and other subclasses use the old default
251 gtk_container_class_install_child_property (container_class,
253 g_param_spec_boolean ("expand",
255 P_("Whether the child should receive extra space when the parent grows"),
257 GTK_PARAM_READWRITE));
262 * Whether the child should receive extra space when the parent grows.
264 * Note that the default value for this property is %FALSE for GtkBox,
265 * but #GtkHBox, #GtkVBox and other subclasses use the old default
268 gtk_container_class_install_child_property (container_class,
270 g_param_spec_boolean ("fill",
272 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
274 GTK_PARAM_READWRITE));
276 gtk_container_class_install_child_property (container_class,
278 g_param_spec_uint ("padding",
280 P_("Extra space to put between the child and its neighbors, in pixels"),
282 GTK_PARAM_READWRITE));
283 gtk_container_class_install_child_property (container_class,
284 CHILD_PROP_PACK_TYPE,
285 g_param_spec_enum ("pack-type",
287 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
288 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
289 GTK_PARAM_READWRITE));
290 gtk_container_class_install_child_property (container_class,
292 g_param_spec_int ("position",
294 P_("The index of the child in the parent"),
296 GTK_PARAM_READWRITE));
298 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
302 gtk_box_init (GtkBox *box)
304 GtkBoxPrivate *private;
306 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
311 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
312 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
314 private->orientation = GTK_ORIENTATION_HORIZONTAL;
315 private->children = NULL;
317 private->default_expand = FALSE;
318 private->homogeneous = FALSE;
319 private->spacing = 0;
320 private->spacing_set = FALSE;
324 gtk_box_set_property (GObject *object,
329 GtkBox *box = GTK_BOX (object);
330 GtkBoxPrivate *private = box->priv;
334 case PROP_ORIENTATION:
335 private->orientation = g_value_get_enum (value);
336 gtk_widget_queue_resize (GTK_WIDGET (box));
339 gtk_box_set_spacing (box, g_value_get_int (value));
341 case PROP_HOMOGENEOUS:
342 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gtk_box_get_property (GObject *object,
356 GtkBox *box = GTK_BOX (object);
357 GtkBoxPrivate *private = box->priv;
361 case PROP_ORIENTATION:
362 g_value_set_enum (value, private->orientation);
365 g_value_set_int (value, private->spacing);
367 case PROP_HOMOGENEOUS:
368 g_value_set_boolean (value, private->homogeneous);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 count_expand_children (GtkBox *box,
379 gint *visible_children,
380 gint *expand_children)
382 GtkBoxPrivate *private = box->priv;
386 *visible_children = *expand_children = 0;
388 for (children = private->children; children; children = children->next)
390 child = children->data;
392 if (gtk_widget_get_visible (child->widget))
394 *visible_children += 1;
396 *expand_children += 1;
402 gtk_box_size_allocate (GtkWidget *widget,
403 GtkAllocation *allocation)
405 GtkBox *box = GTK_BOX (widget);
406 GtkBoxPrivate *private = box->priv;
410 gint nexpand_children;
412 GtkTextDirection direction;
413 GtkAllocation child_allocation;
414 GtkRequestedSize *sizes;
420 gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
421 gint x = 0, y = 0, i;
425 gtk_widget_set_allocation (widget, allocation);
427 count_expand_children (box, &nvis_children, &nexpand_children);
429 /* If there is no visible child, simply return. */
430 if (nvis_children <= 0)
433 direction = gtk_widget_get_direction (widget);
434 sizes = g_newa (GtkRequestedSize, nvis_children);
436 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
437 size = allocation->width - (nvis_children - 1) * private->spacing;
439 size = allocation->height - (nvis_children - 1) * private->spacing;
441 /* Retrieve desired size for visible children. */
442 for (i = 0, children = private->children; children; children = children->next)
444 child = children->data;
446 if (!gtk_widget_get_visible (child->widget))
449 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
450 gtk_widget_get_preferred_width_for_height (child->widget,
452 &sizes[i].minimum_size,
453 &sizes[i].natural_size);
455 gtk_widget_get_preferred_height_for_width (child->widget,
457 &sizes[i].minimum_size,
458 &sizes[i].natural_size);
461 /* Assert the api is working properly */
462 if (sizes[i].minimum_size < 0)
463 g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
464 gtk_widget_get_name (GTK_WIDGET (child->widget)),
465 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
466 sizes[i].minimum_size,
467 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
468 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
470 if (sizes[i].natural_size < sizes[i].minimum_size)
471 g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
472 gtk_widget_get_name (GTK_WIDGET (child->widget)),
473 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
474 sizes[i].natural_size,
475 sizes[i].minimum_size,
476 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
477 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
479 size -= sizes[i].minimum_size;
480 size -= child->padding * 2;
482 sizes[i].data = child;
487 if (private->homogeneous)
489 /* If were homogenous we still need to run the above loop to get the
490 * minimum sizes for children that are not going to fill
492 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
493 size = allocation->width - (nvis_children - 1) * private->spacing;
495 size = allocation->height - (nvis_children - 1) * private->spacing;
497 extra = size / nvis_children;
498 n_extra_widgets = size % nvis_children;
502 /* Bring children up to size first */
503 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
505 /* Calculate space which hasn't distributed yet,
506 * and is available for expanding children.
508 if (nexpand_children > 0)
510 extra = size / nexpand_children;
511 n_extra_widgets = size % nexpand_children;
517 /* Allocate child positions. */
518 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
520 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
522 child_allocation.y = allocation->y;
523 child_allocation.height = MAX (1, allocation->height);
524 if (packing == GTK_PACK_START)
527 x = allocation->x + allocation->width;
531 child_allocation.x = allocation->x;
532 child_allocation.width = MAX (1, allocation->width);
533 if (packing == GTK_PACK_START)
536 y = allocation->y + allocation->height;
539 for (i = 0, children = private->children;
541 children = children->next)
543 child = children->data;
545 /* If widget is not visible, skip it. */
546 if (!gtk_widget_get_visible (child->widget))
549 /* If widget is packed differently skip it, but still increment i,
550 * since widget is visible and will be handled in next loop iteration.
552 if (child->pack != packing)
558 /* Assign the child's size. */
559 if (private->homogeneous)
563 if (n_extra_widgets > 0)
571 child_size = sizes[i].minimum_size + child->padding * 2;
577 if (n_extra_widgets > 0)
585 /* Assign the child's position. */
586 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
590 child_allocation.width = MAX (1, child_size - child->padding * 2);
591 child_allocation.x = x + child->padding;
595 child_allocation.width = sizes[i].minimum_size;
596 child_allocation.x = x + (child_size - child_allocation.width) / 2;
599 if (packing == GTK_PACK_START)
601 x += child_size + private->spacing;
605 x -= child_size + private->spacing;
607 child_allocation.x -= child_size;
610 if (direction == GTK_TEXT_DIR_RTL)
611 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
614 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
618 child_allocation.height = MAX (1, child_size - child->padding * 2);
619 child_allocation.y = y + child->padding;
623 child_allocation.height = sizes[i].minimum_size;
624 child_allocation.y = y + (child_size - child_allocation.height) / 2;
627 if (packing == GTK_PACK_START)
629 y += child_size + private->spacing;
633 y -= child_size + private->spacing;
635 child_allocation.y -= child_size;
638 gtk_widget_size_allocate (child->widget, &child_allocation);
646 gtk_box_child_type (GtkContainer *container)
648 return GTK_TYPE_WIDGET;
652 gtk_box_set_child_property (GtkContainer *container,
661 GtkPackType pack_type = 0;
663 if (property_id != CHILD_PROP_POSITION)
664 gtk_box_query_child_packing (GTK_BOX (container),
672 case CHILD_PROP_EXPAND:
673 gtk_box_set_child_packing (GTK_BOX (container),
675 g_value_get_boolean (value),
680 case CHILD_PROP_FILL:
681 gtk_box_set_child_packing (GTK_BOX (container),
684 g_value_get_boolean (value),
688 case CHILD_PROP_PADDING:
689 gtk_box_set_child_packing (GTK_BOX (container),
693 g_value_get_uint (value),
696 case CHILD_PROP_PACK_TYPE:
697 gtk_box_set_child_packing (GTK_BOX (container),
702 g_value_get_enum (value));
704 case CHILD_PROP_POSITION:
705 gtk_box_reorder_child (GTK_BOX (container),
707 g_value_get_int (value));
710 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
716 gtk_box_get_child_property (GtkContainer *container,
722 gboolean expand = FALSE;
723 gboolean fill = FALSE;
725 GtkPackType pack_type = 0;
728 if (property_id != CHILD_PROP_POSITION)
729 gtk_box_query_child_packing (GTK_BOX (container),
738 case CHILD_PROP_EXPAND:
739 g_value_set_boolean (value, expand);
741 case CHILD_PROP_FILL:
742 g_value_set_boolean (value, fill);
744 case CHILD_PROP_PADDING:
745 g_value_set_uint (value, padding);
747 case CHILD_PROP_PACK_TYPE:
748 g_value_set_enum (value, pack_type);
750 case CHILD_PROP_POSITION:
752 for (list = GTK_BOX (container)->priv->children; list; list = list->next)
754 GtkBoxChild *child_entry;
756 child_entry = list->data;
757 if (child_entry->widget == child)
761 g_value_set_int (value, list ? i : -1);
764 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
770 gtk_box_pack (GtkBox *box,
775 GtkPackType pack_type)
777 GtkBoxPrivate *private = box->priv;
778 GtkBoxChild *child_info;
780 g_return_if_fail (GTK_IS_BOX (box));
781 g_return_if_fail (GTK_IS_WIDGET (child));
782 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
784 child_info = g_new (GtkBoxChild, 1);
785 child_info->widget = child;
786 child_info->padding = padding;
787 child_info->expand = expand ? TRUE : FALSE;
788 child_info->fill = fill ? TRUE : FALSE;
789 child_info->pack = pack_type;
791 private->children = g_list_append (private->children, child_info);
793 gtk_widget_freeze_child_notify (child);
795 gtk_widget_set_parent (child, GTK_WIDGET (box));
797 gtk_widget_child_notify (child, "expand");
798 gtk_widget_child_notify (child, "fill");
799 gtk_widget_child_notify (child, "padding");
800 gtk_widget_child_notify (child, "pack-type");
801 gtk_widget_child_notify (child, "position");
802 gtk_widget_thaw_child_notify (child);
806 static GtkSizeRequestMode
807 gtk_box_get_request_mode (GtkWidget *widget)
809 GtkBoxPrivate *private = GTK_BOX (widget)->priv;
811 return (private->orientation == GTK_ORIENTATION_VERTICAL) ?
812 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH : GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
816 gtk_box_get_size (GtkWidget *widget,
817 GtkOrientation orientation,
822 GtkBoxPrivate *private;
825 gint minimum, natural;
827 box = GTK_BOX (widget);
830 minimum = natural = 0;
834 for (children = private->children; children; children = children->next)
836 GtkBoxChild *child = children->data;
838 if (gtk_widget_get_visible (child->widget))
840 gint child_minimum, child_natural;
842 if (orientation == GTK_ORIENTATION_HORIZONTAL)
843 gtk_widget_get_preferred_width (child->widget,
844 &child_minimum, &child_natural);
846 gtk_widget_get_preferred_height (child->widget,
847 &child_minimum, &child_natural);
849 if (private->orientation == orientation)
851 if (private->homogeneous)
855 largest = child_minimum + child->padding * 2;
856 minimum = MAX (minimum, largest);
858 largest = child_natural + child->padding * 2;
859 natural = MAX (natural, largest);
863 minimum += child_minimum + child->padding * 2;
864 natural += child_natural + child->padding * 2;
869 /* The biggest mins and naturals in the opposing orientation */
870 minimum = MAX (minimum, child_minimum);
871 natural = MAX (natural, child_natural);
878 if (nvis_children > 0 && private->orientation == orientation)
880 if (private->homogeneous)
882 minimum *= nvis_children;
883 natural *= nvis_children;
885 minimum += (nvis_children - 1) * private->spacing;
886 natural += (nvis_children - 1) * private->spacing;
890 *minimum_size = minimum;
893 *natural_size = natural;
897 gtk_box_get_preferred_width (GtkWidget *widget,
901 gtk_box_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
905 gtk_box_get_preferred_height (GtkWidget *widget,
909 gtk_box_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
913 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
918 GtkBoxPrivate *private = box->priv;
922 gint nexpand_children;
923 gint computed_minimum = 0, computed_natural = 0;
924 GtkRequestedSize *sizes;
927 gint child_size, child_minimum, child_natural;
928 gint n_extra_widgets = 0;
930 count_expand_children (box, &nvis_children, &nexpand_children);
932 if (nvis_children <= 0)
935 sizes = g_newa (GtkRequestedSize, nvis_children);
936 size = avail_size - (nvis_children - 1) * private->spacing;
938 /* Retrieve desired size for visible children */
939 for (i = 0, children = private->children; children; children = children->next)
941 child = children->data;
943 if (gtk_widget_get_visible (child->widget))
945 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
946 gtk_widget_get_preferred_width (child->widget,
947 &sizes[i].minimum_size,
948 &sizes[i].natural_size);
950 gtk_widget_get_preferred_height (child->widget,
951 &sizes[i].minimum_size,
952 &sizes[i].natural_size);
954 /* Assert the api is working properly */
955 if (sizes[i].minimum_size < 0)
956 g_error ("GtkBox child %s minimum %s: %d < 0",
957 gtk_widget_get_name (GTK_WIDGET (child->widget)),
958 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
959 sizes[i].minimum_size);
961 if (sizes[i].natural_size < sizes[i].minimum_size)
962 g_error ("GtkBox child %s natural %s: %d < minimum %d",
963 gtk_widget_get_name (GTK_WIDGET (child->widget)),
964 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
965 sizes[i].natural_size,
966 sizes[i].minimum_size);
968 size -= sizes[i].minimum_size;
969 size -= child->padding * 2;
971 sizes[i].data = child;
977 if (private->homogeneous)
979 /* If were homogenous we still need to run the above loop to get the
980 * minimum sizes for children that are not going to fill
982 size = avail_size - (nvis_children - 1) * private->spacing;
983 extra = size / nvis_children;
984 n_extra_widgets = size % nvis_children;
988 /* Bring children up to size first */
989 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
991 /* Calculate space which hasn't distributed yet,
992 * and is available for expanding children.
994 if (nexpand_children > 0)
996 extra = size / nexpand_children;
997 n_extra_widgets = size % nexpand_children;
1003 /* Allocate child positions. */
1004 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1006 for (i = 0, children = private->children;
1008 children = children->next)
1010 child = children->data;
1012 /* If widget is not visible, skip it. */
1013 if (!gtk_widget_get_visible (child->widget))
1016 /* If widget is packed differently skip it, but still increment i,
1017 * since widget is visible and will be handled in next loop iteration.
1019 if (child->pack != packing)
1025 if (child->pack == packing)
1027 /* Assign the child's size. */
1028 if (private->homogeneous)
1032 if (n_extra_widgets > 0)
1040 child_size = sizes[i].minimum_size + child->padding * 2;
1044 child_size += extra;
1046 if (n_extra_widgets > 0)
1056 child_size = MAX (1, child_size - child->padding * 2);
1060 child_size = sizes[i].minimum_size;
1064 /* Assign the child's position. */
1065 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1066 gtk_widget_get_preferred_height_for_width (child->widget,
1067 child_size, &child_minimum, &child_natural);
1068 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1069 gtk_widget_get_preferred_width_for_height (child->widget,
1070 child_size, &child_minimum, &child_natural);
1073 computed_minimum = MAX (computed_minimum, child_minimum);
1074 computed_natural = MAX (computed_natural, child_natural);
1081 *minimum_size = computed_minimum;
1083 *natural_size = computed_natural;
1087 gtk_box_compute_size_for_orientation (GtkBox *box,
1092 GtkBoxPrivate *private = box->priv;
1094 gint nvis_children = 0;
1095 gint required_size = 0, required_natural = 0, child_size, child_natural;
1096 gint largest_child = 0, largest_natural = 0;
1098 for (children = private->children; children != NULL;
1099 children = children->next, nvis_children++)
1101 GtkBoxChild *child = children->data;
1103 if (gtk_widget_get_visible (child->widget))
1106 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1107 gtk_widget_get_preferred_width_for_height (child->widget,
1108 avail_size, &child_size, &child_natural);
1110 gtk_widget_get_preferred_height_for_width (child->widget,
1111 avail_size, &child_size, &child_natural);
1114 child_size += child->padding * 2;
1115 child_natural += child->padding * 2;
1117 if (child_size > largest_child)
1118 largest_child = child_size;
1120 if (child_natural > largest_natural)
1121 largest_natural = child_natural;
1123 required_size += child_size;
1124 required_natural += child_natural;
1128 if (nvis_children > 0)
1130 if (private->homogeneous)
1132 required_size = largest_child * nvis_children;
1133 required_natural = largest_natural * nvis_children;
1136 required_size += (nvis_children - 1) * private->spacing;
1137 required_natural += (nvis_children - 1) * private->spacing;
1141 *minimum_size = required_size;
1144 *natural_size = required_natural;
1148 gtk_box_get_preferred_width_for_height (GtkWidget *widget,
1150 gint *minimum_width,
1151 gint *natural_width)
1153 GtkBox *box = GTK_BOX (widget);
1154 GtkBoxPrivate *private = box->priv;
1156 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1157 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1159 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1163 gtk_box_get_preferred_height_for_width (GtkWidget *widget,
1165 gint *minimum_height,
1166 gint *natural_height)
1168 GtkBox *box = GTK_BOX (widget);
1169 GtkBoxPrivate *private = box->priv;
1171 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1172 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1174 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1179 * @orientation: the box' orientation.
1180 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1181 * @spacing: the number of pixels to place by default between children.
1183 * Creates a new #GtkBox.
1185 * Return value: a new #GtkBox.
1190 gtk_box_new (GtkOrientation orientation,
1191 gboolean homogeneous,
1194 return g_object_new (GTK_TYPE_BOX,
1195 "orientation", orientation,
1197 "homogeneous", homogeneous ? TRUE : FALSE,
1202 * gtk_box_pack_start:
1204 * @child: the #GtkWidget to be added to @box
1205 * @expand: %TRUE if the new child is to be given extra space allocated to
1206 * @box. The extra space will be divided evenly between all children of
1207 * @box that use this option
1208 * @fill: %TRUE if space given to @child by the @expand option is
1209 * actually allocated to @child, rather than just padding it. This
1210 * parameter has no effect if @expand is set to %FALSE. A child is
1211 * always allocated the full height of a #GtkHBox and the full width
1212 * of a #GtkVBox. This option affects the other dimension
1213 * @padding: extra space in pixels to put between this child and its
1214 * neighbors, over and above the global amount specified by
1215 * #GtkBox:spacing property. If @child is a widget at one of the
1216 * reference ends of @box, then @padding pixels are also put between
1217 * @child and the reference edge of @box
1219 * Adds @child to @box, packed with reference to the start of @box.
1220 * The @child is packed after any other child packed with reference
1221 * to the start of @box.
1224 gtk_box_pack_start (GtkBox *box,
1230 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1236 * @child: the #GtkWidget to be added to @box
1237 * @expand: %TRUE if the new child is to be given extra space allocated
1238 * to @box. The extra space will be divided evenly between all children
1239 * of @box that use this option
1240 * @fill: %TRUE if space given to @child by the @expand option is
1241 * actually allocated to @child, rather than just padding it. This
1242 * parameter has no effect if @expand is set to %FALSE. A child is
1243 * always allocated the full height of a #GtkHBox and the full width
1244 * of a #GtkVBox. This option affects the other dimension
1245 * @padding: extra space in pixels to put between this child and its
1246 * neighbors, over and above the global amount specified by
1247 * #GtkBox:spacing property. If @child is a widget at one of the
1248 * reference ends of @box, then @padding pixels are also put between
1249 * @child and the reference edge of @box
1251 * Adds @child to @box, packed with reference to the end of @box.
1252 * The @child is packed after (away from end of) any other child
1253 * packed with reference to the end of @box.
1256 gtk_box_pack_end (GtkBox *box,
1262 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1266 * gtk_box_set_homogeneous:
1268 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1269 * %FALSE for variable allotments
1271 * Sets the #GtkBox:homogeneous property of @box, controlling
1272 * whether or not all children of @box are given equal space
1276 gtk_box_set_homogeneous (GtkBox *box,
1277 gboolean homogeneous)
1279 GtkBoxPrivate *private;
1281 g_return_if_fail (GTK_IS_BOX (box));
1283 private = box->priv;
1285 if ((homogeneous ? TRUE : FALSE) != private->homogeneous)
1287 private->homogeneous = homogeneous ? TRUE : FALSE;
1288 g_object_notify (G_OBJECT (box), "homogeneous");
1289 gtk_widget_queue_resize (GTK_WIDGET (box));
1294 * gtk_box_get_homogeneous:
1297 * Returns whether the box is homogeneous (all children are the
1298 * same size). See gtk_box_set_homogeneous().
1300 * Return value: %TRUE if the box is homogeneous.
1303 gtk_box_get_homogeneous (GtkBox *box)
1305 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1307 return box->priv->homogeneous;
1311 * gtk_box_set_spacing:
1313 * @spacing: the number of pixels to put between children
1315 * Sets the #GtkBox:spacing property of @box, which is the
1316 * number of pixels to place between children of @box.
1319 gtk_box_set_spacing (GtkBox *box,
1322 GtkBoxPrivate *private;
1324 g_return_if_fail (GTK_IS_BOX (box));
1326 private = box->priv;
1328 if (spacing != private->spacing)
1330 private->spacing = spacing;
1331 _gtk_box_set_spacing_set (box, TRUE);
1333 g_object_notify (G_OBJECT (box), "spacing");
1335 gtk_widget_queue_resize (GTK_WIDGET (box));
1340 * gtk_box_get_spacing:
1343 * Gets the value set by gtk_box_set_spacing().
1345 * Return value: spacing between children
1348 gtk_box_get_spacing (GtkBox *box)
1350 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1352 return box->priv->spacing;
1356 _gtk_box_set_spacing_set (GtkBox *box,
1357 gboolean spacing_set)
1359 GtkBoxPrivate *private;
1361 g_return_if_fail (GTK_IS_BOX (box));
1363 private = box->priv;
1365 private->spacing_set = spacing_set ? TRUE : FALSE;
1369 _gtk_box_get_spacing_set (GtkBox *box)
1371 GtkBoxPrivate *private;
1373 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1375 private = box->priv;
1377 return private->spacing_set;
1381 * gtk_box_reorder_child:
1383 * @child: the #GtkWidget to move
1384 * @position: the new position for @child in the list of children
1385 * of @box, starting from 0. If negative, indicates the end of
1388 * Moves @child to a new @position in the list of @box children.
1389 * The list is the <structfield>children</structfield> field of
1390 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1391 * as well as widgets packed #GTK_PACK_END, in the order that these
1392 * widgets were added to @box.
1394 * A widget's position in the @box children list determines where
1395 * the widget is packed into @box. A child widget at some position
1396 * in the list will be packed just after all other widgets of the
1397 * same packing type that appear earlier in the list.
1400 gtk_box_reorder_child (GtkBox *box,
1404 GtkBoxPrivate *priv;
1407 GtkBoxChild *child_info = NULL;
1410 g_return_if_fail (GTK_IS_BOX (box));
1411 g_return_if_fail (GTK_IS_WIDGET (child));
1415 old_link = priv->children;
1419 child_info = old_link->data;
1420 if (child_info->widget == child)
1423 old_link = old_link->next;
1427 g_return_if_fail (old_link != NULL);
1429 if (position == old_position)
1432 priv->children = g_list_delete_link (priv->children, old_link);
1437 new_link = g_list_nth (priv->children, position);
1439 priv->children = g_list_insert_before (priv->children, new_link, child_info);
1441 gtk_widget_child_notify (child, "position");
1442 if (gtk_widget_get_visible (child)
1443 && gtk_widget_get_visible (GTK_WIDGET (box)))
1444 gtk_widget_queue_resize (child);
1448 * gtk_box_query_child_packing:
1450 * @child: the #GtkWidget of the child to query
1451 * @expand: pointer to return location for #GtkBox:expand child property
1452 * @fill: pointer to return location for #GtkBox:fill child property
1453 * @padding: pointer to return location for #GtkBox:padding child property
1454 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1456 * Obtains information about how @child is packed into @box.
1459 gtk_box_query_child_packing (GtkBox *box,
1464 GtkPackType *pack_type)
1466 GtkBoxPrivate *private;
1468 GtkBoxChild *child_info = NULL;
1470 g_return_if_fail (GTK_IS_BOX (box));
1471 g_return_if_fail (GTK_IS_WIDGET (child));
1473 private = box->priv;
1475 list = private->children;
1478 child_info = list->data;
1479 if (child_info->widget == child)
1488 *expand = child_info->expand;
1490 *fill = child_info->fill;
1492 *padding = child_info->padding;
1494 *pack_type = child_info->pack;
1499 * gtk_box_set_child_packing:
1501 * @child: the #GtkWidget of the child to set
1502 * @expand: the new value of the #GtkBox:expand child property
1503 * @fill: the new value of the #GtkBox:fill child property
1504 * @padding: the new value of the #GtkBox:padding child property
1505 * @pack_type: the new value of the #GtkBox:pack-type child property
1507 * Sets the way @child is packed into @box.
1510 gtk_box_set_child_packing (GtkBox *box,
1515 GtkPackType pack_type)
1517 GtkBoxPrivate *private;
1519 GtkBoxChild *child_info = NULL;
1521 g_return_if_fail (GTK_IS_BOX (box));
1522 g_return_if_fail (GTK_IS_WIDGET (child));
1524 private = box->priv;
1526 list = private->children;
1529 child_info = list->data;
1530 if (child_info->widget == child)
1536 gtk_widget_freeze_child_notify (child);
1539 child_info->expand = expand != FALSE;
1540 gtk_widget_child_notify (child, "expand");
1541 child_info->fill = fill != FALSE;
1542 gtk_widget_child_notify (child, "fill");
1543 child_info->padding = padding;
1544 gtk_widget_child_notify (child, "padding");
1545 if (pack_type == GTK_PACK_END)
1546 child_info->pack = GTK_PACK_END;
1548 child_info->pack = GTK_PACK_START;
1549 gtk_widget_child_notify (child, "pack-type");
1551 if (gtk_widget_get_visible (child)
1552 && gtk_widget_get_visible (GTK_WIDGET (box)))
1553 gtk_widget_queue_resize (child);
1555 gtk_widget_thaw_child_notify (child);
1559 _gtk_box_set_old_defaults (GtkBox *box)
1561 GtkBoxPrivate *private;
1563 g_return_if_fail (GTK_IS_BOX (box));
1565 private = box->priv;
1567 private->default_expand = TRUE;
1571 gtk_box_add (GtkContainer *container,
1574 GtkBoxPrivate *priv = GTK_BOX (container)->priv;
1576 gtk_box_pack_start (GTK_BOX (container), widget,
1577 priv->default_expand,
1578 priv->default_expand,
1583 gtk_box_remove (GtkContainer *container,
1586 GtkBox *box = GTK_BOX (container);
1587 GtkBoxPrivate *priv = box->priv;
1591 children = priv->children;
1594 child = children->data;
1596 if (child->widget == widget)
1598 gboolean was_visible;
1600 was_visible = gtk_widget_get_visible (widget);
1601 gtk_widget_unparent (widget);
1603 priv->children = g_list_remove_link (priv->children, children);
1604 g_list_free (children);
1607 /* queue resize regardless of gtk_widget_get_visible (container),
1608 * since that's what is needed by toplevels.
1611 gtk_widget_queue_resize (GTK_WIDGET (container));
1616 children = children->next;
1621 gtk_box_forall (GtkContainer *container,
1622 gboolean include_internals,
1623 GtkCallback callback,
1624 gpointer callback_data)
1626 GtkBox *box = GTK_BOX (container);
1627 GtkBoxPrivate *priv = box->priv;
1631 children = priv->children;
1634 child = children->data;
1635 children = children->next;
1637 if (child->pack == GTK_PACK_START)
1638 (* callback) (child->widget, callback_data);
1641 children = g_list_last (priv->children);
1644 child = children->data;
1645 children = children->prev;
1647 if (child->pack == GTK_PACK_END)
1648 (* callback) (child->widget, callback_data);
1653 _gtk_box_get_children (GtkBox *box)
1655 GtkBoxPrivate *priv;
1658 GList *retval = NULL;
1660 g_return_val_if_fail (GTK_IS_BOX (box), NULL);
1664 children = priv->children;
1667 child = children->data;
1668 children = children->next;
1670 retval = g_list_prepend (retval, child->widget);
1673 return g_list_reverse (retval);