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 "gtkprivate.h"
101 struct _GtkBoxPrivate
103 GtkOrientation orientation;
109 guint default_expand : 1;
110 guint homogeneous : 1;
111 guint spacing_set : 1;
114 typedef struct _GtkBoxChild GtkBoxChild;
118 * @widget: the child widget, packed into the GtkBox.
119 * @padding: the number of extra pixels to put between this child and its
120 * neighbors, set when packed, zero by default.
121 * @expand: flag indicates whether extra space should be given to this child.
122 * Any extra space given to the parent GtkBox is divided up among all children
123 * with this attribute set to %TRUE; set when packed, %TRUE by default.
124 * @fill: flag indicates whether any extra space given to this child due to its
125 * @expand attribute being set is actually allocated to the child, rather than
126 * being used as padding around the widget; set when packed, %TRUE by default.
127 * @pack: one of #GtkPackType indicating whether the child is packed with
128 * reference to the start (top/left) or end (bottom/right) of the GtkBox.
141 static void gtk_box_size_allocate (GtkWidget *widget,
142 GtkAllocation *allocation);
144 static void gtk_box_set_property (GObject *object,
148 static void gtk_box_get_property (GObject *object,
153 static void gtk_box_add (GtkContainer *container,
155 static void gtk_box_remove (GtkContainer *container,
157 static void gtk_box_forall (GtkContainer *container,
158 gboolean include_internals,
159 GtkCallback callback,
160 gpointer callback_data);
161 static void gtk_box_set_child_property (GtkContainer *container,
166 static void gtk_box_get_child_property (GtkContainer *container,
171 static GType gtk_box_child_type (GtkContainer *container);
174 static void gtk_box_size_request_init (GtkSizeRequestIface *iface);
175 static GtkSizeRequestMode gtk_box_get_request_mode (GtkSizeRequest *widget);
176 static void gtk_box_get_width (GtkSizeRequest *widget,
179 static void gtk_box_get_height (GtkSizeRequest *widget,
182 static void gtk_box_get_width_for_height (GtkSizeRequest *widget,
185 gint *natural_width);
186 static void gtk_box_get_height_for_width (GtkSizeRequest *widget,
188 gint *minimum_height,
189 gint *natural_height);
191 static GtkSizeRequestIface *parent_size_request_iface;
193 G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
194 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
196 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
197 gtk_box_size_request_init));
200 gtk_box_class_init (GtkBoxClass *class)
202 GObjectClass *object_class = G_OBJECT_CLASS (class);
203 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
204 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
206 object_class->set_property = gtk_box_set_property;
207 object_class->get_property = gtk_box_get_property;
209 widget_class->size_allocate = gtk_box_size_allocate;
211 container_class->add = gtk_box_add;
212 container_class->remove = gtk_box_remove;
213 container_class->forall = gtk_box_forall;
214 container_class->child_type = gtk_box_child_type;
215 container_class->set_child_property = gtk_box_set_child_property;
216 container_class->get_child_property = gtk_box_get_child_property;
218 g_object_class_override_property (object_class,
222 g_object_class_install_property (object_class,
224 g_param_spec_int ("spacing",
226 P_("The amount of space between children"),
230 GTK_PARAM_READWRITE));
232 g_object_class_install_property (object_class,
234 g_param_spec_boolean ("homogeneous",
236 P_("Whether the children should all be the same size"),
238 GTK_PARAM_READWRITE));
243 * Whether the child should receive extra space when the parent grows.
245 * Note that the default value for this property is %FALSE for GtkBox,
246 * but #GtkHBox, #GtkVBox and other subclasses use the old default
249 gtk_container_class_install_child_property (container_class,
251 g_param_spec_boolean ("expand",
253 P_("Whether the child should receive extra space when the parent grows"),
255 GTK_PARAM_READWRITE));
260 * Whether the child should receive extra space when the parent grows.
262 * Note that the default value for this property is %FALSE for GtkBox,
263 * but #GtkHBox, #GtkVBox and other subclasses use the old default
266 gtk_container_class_install_child_property (container_class,
268 g_param_spec_boolean ("fill",
270 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
272 GTK_PARAM_READWRITE));
274 gtk_container_class_install_child_property (container_class,
276 g_param_spec_uint ("padding",
278 P_("Extra space to put between the child and its neighbors, in pixels"),
280 GTK_PARAM_READWRITE));
281 gtk_container_class_install_child_property (container_class,
282 CHILD_PROP_PACK_TYPE,
283 g_param_spec_enum ("pack-type",
285 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
286 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
287 GTK_PARAM_READWRITE));
288 gtk_container_class_install_child_property (container_class,
290 g_param_spec_int ("position",
292 P_("The index of the child in the parent"),
294 GTK_PARAM_READWRITE));
296 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
300 gtk_box_init (GtkBox *box)
302 GtkBoxPrivate *private;
304 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
309 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
310 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
312 private->orientation = GTK_ORIENTATION_HORIZONTAL;
313 private->children = NULL;
315 private->default_expand = FALSE;
316 private->homogeneous = FALSE;
317 private->spacing = 0;
318 private->spacing_set = FALSE;
322 gtk_box_set_property (GObject *object,
327 GtkBox *box = GTK_BOX (object);
328 GtkBoxPrivate *private = box->priv;
332 case PROP_ORIENTATION:
333 private->orientation = g_value_get_enum (value);
334 gtk_widget_queue_resize (GTK_WIDGET (box));
337 gtk_box_set_spacing (box, g_value_get_int (value));
339 case PROP_HOMOGENEOUS:
340 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 gtk_box_get_property (GObject *object,
354 GtkBox *box = GTK_BOX (object);
355 GtkBoxPrivate *private = box->priv;
359 case PROP_ORIENTATION:
360 g_value_set_enum (value, private->orientation);
363 g_value_set_int (value, private->spacing);
365 case PROP_HOMOGENEOUS:
366 g_value_set_boolean (value, private->homogeneous);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 count_expand_children (GtkBox *box,
377 gint *visible_children,
378 gint *expand_children)
380 GtkBoxPrivate *private = box->priv;
384 *visible_children = *expand_children = 0;
386 for (children = private->children; children; children = children->next)
388 child = children->data;
390 if (gtk_widget_get_visible (child->widget))
392 *visible_children += 1;
394 *expand_children += 1;
400 gtk_box_size_allocate (GtkWidget *widget,
401 GtkAllocation *allocation)
403 GtkBox *box = GTK_BOX (widget);
404 GtkBoxPrivate *private = box->priv;
408 gint nexpand_children;
411 GtkTextDirection direction;
412 GtkAllocation child_allocation;
413 GtkRequestedSize *sizes;
419 gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
420 gint x = 0, y = 0, i;
424 gtk_widget_set_allocation (widget, allocation);
426 count_expand_children (box, &nvis_children, &nexpand_children);
428 /* If there is no visible child, simply return. */
429 if (nvis_children <= 0)
432 border_width = gtk_container_get_border_width (GTK_CONTAINER (box));
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 - border_width * 2 - (nvis_children - 1) * private->spacing;
439 size = allocation->height - border_width * 2 - (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_size_request_get_width_for_height (GTK_SIZE_REQUEST (child->widget),
452 &sizes[i].minimum_size,
453 &sizes[i].natural_size);
455 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (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 - border_width * 2 - (nvis_children - 1) * private->spacing;
495 size = allocation->height - border_width * 2 - (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 + border_width;
523 child_allocation.height = MAX (1, allocation->height - border_width * 2);
524 if (packing == GTK_PACK_START)
525 x = allocation->x + border_width;
527 x = allocation->x + allocation->width - border_width;
531 child_allocation.x = allocation->x + border_width;
532 child_allocation.width = MAX (1, allocation->width - border_width * 2);
533 if (packing == GTK_PACK_START)
534 y = allocation->y + border_width;
536 y = allocation->y + allocation->height - border_width;
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);
807 gtk_box_size_request_init (GtkSizeRequestIface *iface)
809 parent_size_request_iface = g_type_interface_peek_parent (iface);
811 iface->get_request_mode = gtk_box_get_request_mode;
812 iface->get_width = gtk_box_get_width;
813 iface->get_height = gtk_box_get_height;
814 iface->get_height_for_width = gtk_box_get_height_for_width;
815 iface->get_width_for_height = gtk_box_get_width_for_height;
818 static GtkSizeRequestMode
819 gtk_box_get_request_mode (GtkSizeRequest *widget)
821 GtkBoxPrivate *private = GTK_BOX (widget)->priv;
823 return (private->orientation == GTK_ORIENTATION_VERTICAL) ?
824 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH : GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
828 gtk_box_get_size (GtkSizeRequest *widget,
829 GtkOrientation orientation,
834 GtkBoxPrivate *private;
838 gint minimum, natural;
840 box = GTK_BOX (widget);
842 border_width = gtk_container_get_border_width (GTK_CONTAINER (box));
844 minimum = natural = 0;
848 for (children = private->children; children; children = children->next)
850 GtkBoxChild *child = children->data;
852 if (gtk_widget_get_visible (child->widget))
854 gint child_minimum, child_natural;
856 if (orientation == GTK_ORIENTATION_HORIZONTAL)
857 gtk_size_request_get_width (GTK_SIZE_REQUEST (child->widget),
858 &child_minimum, &child_natural);
860 gtk_size_request_get_height (GTK_SIZE_REQUEST (child->widget),
861 &child_minimum, &child_natural);
863 if (private->orientation == orientation)
865 if (private->homogeneous)
869 largest = child_minimum + child->padding * 2;
870 minimum = MAX (minimum, largest);
872 largest = child_natural + child->padding * 2;
873 natural = MAX (natural, largest);
877 minimum += child_minimum + child->padding * 2;
878 natural += child_natural + child->padding * 2;
883 /* The biggest mins and naturals in the opposing orientation */
884 minimum = MAX (minimum, child_minimum);
885 natural = MAX (natural, child_natural);
892 if (nvis_children > 0 && private->orientation == orientation)
894 if (private->homogeneous)
896 minimum *= nvis_children;
897 natural *= nvis_children;
899 minimum += (nvis_children - 1) * private->spacing;
900 natural += (nvis_children - 1) * private->spacing;
903 minimum += border_width * 2;
904 natural += border_width * 2;
907 *minimum_size = minimum;
910 *natural_size = natural;
914 gtk_box_get_width (GtkSizeRequest *widget,
918 gtk_box_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
922 gtk_box_get_height (GtkSizeRequest *widget,
926 gtk_box_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
930 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
935 GtkBoxPrivate *private = box->priv;
939 gint nexpand_children;
940 gint computed_minimum = 0, computed_natural = 0;
941 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (box));
942 GtkRequestedSize *sizes;
945 gint child_size, child_minimum, child_natural;
946 gint n_extra_widgets = 0;
948 count_expand_children (box, &nvis_children, &nexpand_children);
950 if (nvis_children <= 0)
953 sizes = g_newa (GtkRequestedSize, nvis_children);
954 size = avail_size - border_width * 2 - (nvis_children - 1) * private->spacing;
956 /* Retrieve desired size for visible children */
957 for (i = 0, children = private->children; children; children = children->next)
959 child = children->data;
961 if (gtk_widget_get_visible (child->widget))
963 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
964 gtk_size_request_get_width (GTK_SIZE_REQUEST (child->widget),
965 &sizes[i].minimum_size,
966 &sizes[i].natural_size);
968 gtk_size_request_get_height (GTK_SIZE_REQUEST (child->widget),
969 &sizes[i].minimum_size,
970 &sizes[i].natural_size);
972 /* Assert the api is working properly */
973 if (sizes[i].minimum_size < 0)
974 g_error ("GtkBox child %s minimum %s: %d < 0",
975 gtk_widget_get_name (GTK_WIDGET (child->widget)),
976 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
977 sizes[i].minimum_size);
979 if (sizes[i].natural_size < sizes[i].minimum_size)
980 g_error ("GtkBox child %s natural %s: %d < minimum %d",
981 gtk_widget_get_name (GTK_WIDGET (child->widget)),
982 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
983 sizes[i].natural_size,
984 sizes[i].minimum_size);
986 size -= sizes[i].minimum_size;
987 size -= child->padding * 2;
989 sizes[i].data = child;
995 if (private->homogeneous)
997 /* If were homogenous we still need to run the above loop to get the
998 * minimum sizes for children that are not going to fill
1000 size = avail_size - border_width * 2 - (nvis_children - 1) * private->spacing;
1001 extra = size / nvis_children;
1002 n_extra_widgets = size % nvis_children;
1006 /* Bring children up to size first */
1007 size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
1009 /* Calculate space which hasn't distributed yet,
1010 * and is available for expanding children.
1012 if (nexpand_children > 0)
1014 extra = size / nexpand_children;
1015 n_extra_widgets = size % nexpand_children;
1021 /* Allocate child positions. */
1022 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1024 for (i = 0, children = private->children;
1026 children = children->next)
1028 child = children->data;
1030 /* If widget is not visible, skip it. */
1031 if (!gtk_widget_get_visible (child->widget))
1034 /* If widget is packed differently skip it, but still increment i,
1035 * since widget is visible and will be handled in next loop iteration.
1037 if (child->pack != packing)
1043 if (child->pack == packing)
1045 /* Assign the child's size. */
1046 if (private->homogeneous)
1050 if (n_extra_widgets > 0)
1058 child_size = sizes[i].minimum_size + child->padding * 2;
1062 child_size += extra;
1064 if (n_extra_widgets > 0)
1074 child_size = MAX (1, child_size - child->padding * 2);
1078 child_size = sizes[i].minimum_size;
1082 /* Assign the child's position. */
1083 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1084 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child->widget),
1085 child_size, &child_minimum, &child_natural);
1086 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1087 gtk_size_request_get_width_for_height (GTK_SIZE_REQUEST (child->widget),
1088 child_size, &child_minimum, &child_natural);
1091 computed_minimum = MAX (computed_minimum, child_minimum);
1092 computed_natural = MAX (computed_natural, child_natural);
1098 computed_minimum += border_width * 2;
1099 computed_natural += border_width * 2;
1102 *minimum_size = computed_minimum;
1104 *natural_size = computed_natural;
1108 gtk_box_compute_size_for_orientation (GtkBox *box,
1113 GtkBoxPrivate *private = box->priv;
1115 gint nvis_children = 0;
1116 gint required_size = 0, required_natural = 0, child_size, child_natural;
1117 gint largest_child = 0, largest_natural = 0;
1120 border_width = gtk_container_get_border_width (GTK_CONTAINER (box));
1121 avail_size -= border_width * 2;
1123 for (children = private->children; children != NULL;
1124 children = children->next, nvis_children++)
1126 GtkBoxChild *child = children->data;
1128 if (gtk_widget_get_visible (child->widget))
1131 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1132 gtk_size_request_get_width_for_height (GTK_SIZE_REQUEST (child->widget),
1133 avail_size, &child_size, &child_natural);
1135 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child->widget),
1136 avail_size, &child_size, &child_natural);
1139 child_size += child->padding * 2;
1140 child_natural += child->padding * 2;
1142 if (child_size > largest_child)
1143 largest_child = child_size;
1145 if (child_natural > largest_natural)
1146 largest_natural = child_natural;
1148 required_size += child_size;
1149 required_natural += child_natural;
1153 if (nvis_children > 0)
1155 if (private->homogeneous)
1157 required_size = largest_child * nvis_children;
1158 required_natural = largest_natural * nvis_children;
1161 required_size += (nvis_children - 1) * private->spacing;
1162 required_natural += (nvis_children - 1) * private->spacing;
1165 required_size += border_width * 2;
1166 required_natural += border_width * 2;
1169 *minimum_size = required_size;
1172 *natural_size = required_natural;
1176 gtk_box_get_width_for_height (GtkSizeRequest *widget,
1178 gint *minimum_width,
1179 gint *natural_width)
1181 GtkBox *box = GTK_BOX (widget);
1182 GtkBoxPrivate *private = box->priv;
1184 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1185 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1187 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1191 gtk_box_get_height_for_width (GtkSizeRequest *widget,
1193 gint *minimum_height,
1194 gint *natural_height)
1196 GtkBox *box = GTK_BOX (widget);
1197 GtkBoxPrivate *private = box->priv;
1199 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1200 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1202 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1207 * @orientation: the box' orientation.
1208 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1209 * @spacing: the number of pixels to place by default between children.
1211 * Creates a new #GtkBox.
1213 * Return value: a new #GtkBox.
1218 gtk_box_new (GtkOrientation orientation,
1219 gboolean homogeneous,
1222 return g_object_new (GTK_TYPE_BOX,
1223 "orientation", orientation,
1225 "homogeneous", homogeneous ? TRUE : FALSE,
1230 * gtk_box_pack_start:
1232 * @child: the #GtkWidget to be added to @box
1233 * @expand: %TRUE if the new child is to be given extra space allocated to
1234 * @box. The extra space will be divided evenly between all children of
1235 * @box that use this option
1236 * @fill: %TRUE if space given to @child by the @expand option is
1237 * actually allocated to @child, rather than just padding it. This
1238 * parameter has no effect if @expand is set to %FALSE. A child is
1239 * always allocated the full height of a #GtkHBox and the full width
1240 * of a #GtkVBox. This option affects the other dimension
1241 * @padding: extra space in pixels to put between this child and its
1242 * neighbors, over and above the global amount specified by
1243 * #GtkBox:spacing property. If @child is a widget at one of the
1244 * reference ends of @box, then @padding pixels are also put between
1245 * @child and the reference edge of @box
1247 * Adds @child to @box, packed with reference to the start of @box.
1248 * The @child is packed after any other child packed with reference
1249 * to the start of @box.
1252 gtk_box_pack_start (GtkBox *box,
1258 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1264 * @child: the #GtkWidget to be added to @box
1265 * @expand: %TRUE if the new child is to be given extra space allocated
1266 * to @box. The extra space will be divided evenly between all children
1267 * of @box that use this option
1268 * @fill: %TRUE if space given to @child by the @expand option is
1269 * actually allocated to @child, rather than just padding it. This
1270 * parameter has no effect if @expand is set to %FALSE. A child is
1271 * always allocated the full height of a #GtkHBox and the full width
1272 * of a #GtkVBox. This option affects the other dimension
1273 * @padding: extra space in pixels to put between this child and its
1274 * neighbors, over and above the global amount specified by
1275 * #GtkBox:spacing property. If @child is a widget at one of the
1276 * reference ends of @box, then @padding pixels are also put between
1277 * @child and the reference edge of @box
1279 * Adds @child to @box, packed with reference to the end of @box.
1280 * The @child is packed after (away from end of) any other child
1281 * packed with reference to the end of @box.
1284 gtk_box_pack_end (GtkBox *box,
1290 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1294 * gtk_box_set_homogeneous:
1296 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1297 * %FALSE for variable allotments
1299 * Sets the #GtkBox:homogeneous property of @box, controlling
1300 * whether or not all children of @box are given equal space
1304 gtk_box_set_homogeneous (GtkBox *box,
1305 gboolean homogeneous)
1307 GtkBoxPrivate *private;
1309 g_return_if_fail (GTK_IS_BOX (box));
1311 private = box->priv;
1313 if ((homogeneous ? TRUE : FALSE) != private->homogeneous)
1315 private->homogeneous = homogeneous ? TRUE : FALSE;
1316 g_object_notify (G_OBJECT (box), "homogeneous");
1317 gtk_widget_queue_resize (GTK_WIDGET (box));
1322 * gtk_box_get_homogeneous:
1325 * Returns whether the box is homogeneous (all children are the
1326 * same size). See gtk_box_set_homogeneous().
1328 * Return value: %TRUE if the box is homogeneous.
1331 gtk_box_get_homogeneous (GtkBox *box)
1333 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1335 return box->priv->homogeneous;
1339 * gtk_box_set_spacing:
1341 * @spacing: the number of pixels to put between children
1343 * Sets the #GtkBox:spacing property of @box, which is the
1344 * number of pixels to place between children of @box.
1347 gtk_box_set_spacing (GtkBox *box,
1350 GtkBoxPrivate *private;
1352 g_return_if_fail (GTK_IS_BOX (box));
1354 private = box->priv;
1356 if (spacing != private->spacing)
1358 private->spacing = spacing;
1359 _gtk_box_set_spacing_set (box, TRUE);
1361 g_object_notify (G_OBJECT (box), "spacing");
1363 gtk_widget_queue_resize (GTK_WIDGET (box));
1368 * gtk_box_get_spacing:
1371 * Gets the value set by gtk_box_set_spacing().
1373 * Return value: spacing between children
1376 gtk_box_get_spacing (GtkBox *box)
1378 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1380 return box->priv->spacing;
1384 _gtk_box_set_spacing_set (GtkBox *box,
1385 gboolean spacing_set)
1387 GtkBoxPrivate *private;
1389 g_return_if_fail (GTK_IS_BOX (box));
1391 private = box->priv;
1393 private->spacing_set = spacing_set ? TRUE : FALSE;
1397 _gtk_box_get_spacing_set (GtkBox *box)
1399 GtkBoxPrivate *private;
1401 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1403 private = box->priv;
1405 return private->spacing_set;
1409 * gtk_box_reorder_child:
1411 * @child: the #GtkWidget to move
1412 * @position: the new position for @child in the list of children
1413 * of @box, starting from 0. If negative, indicates the end of
1416 * Moves @child to a new @position in the list of @box children.
1417 * The list is the <structfield>children</structfield> field of
1418 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1419 * as well as widgets packed #GTK_PACK_END, in the order that these
1420 * widgets were added to @box.
1422 * A widget's position in the @box children list determines where
1423 * the widget is packed into @box. A child widget at some position
1424 * in the list will be packed just after all other widgets of the
1425 * same packing type that appear earlier in the list.
1428 gtk_box_reorder_child (GtkBox *box,
1432 GtkBoxPrivate *priv;
1435 GtkBoxChild *child_info = NULL;
1438 g_return_if_fail (GTK_IS_BOX (box));
1439 g_return_if_fail (GTK_IS_WIDGET (child));
1443 old_link = priv->children;
1447 child_info = old_link->data;
1448 if (child_info->widget == child)
1451 old_link = old_link->next;
1455 g_return_if_fail (old_link != NULL);
1457 if (position == old_position)
1460 priv->children = g_list_delete_link (priv->children, old_link);
1465 new_link = g_list_nth (priv->children, position);
1467 priv->children = g_list_insert_before (priv->children, new_link, child_info);
1469 gtk_widget_child_notify (child, "position");
1470 if (gtk_widget_get_visible (child)
1471 && gtk_widget_get_visible (GTK_WIDGET (box)))
1472 gtk_widget_queue_resize (child);
1476 * gtk_box_query_child_packing:
1478 * @child: the #GtkWidget of the child to query
1479 * @expand: pointer to return location for #GtkBox:expand child property
1480 * @fill: pointer to return location for #GtkBox:fill child property
1481 * @padding: pointer to return location for #GtkBox:padding child property
1482 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1484 * Obtains information about how @child is packed into @box.
1487 gtk_box_query_child_packing (GtkBox *box,
1492 GtkPackType *pack_type)
1494 GtkBoxPrivate *private;
1496 GtkBoxChild *child_info = NULL;
1498 g_return_if_fail (GTK_IS_BOX (box));
1499 g_return_if_fail (GTK_IS_WIDGET (child));
1501 private = box->priv;
1503 list = private->children;
1506 child_info = list->data;
1507 if (child_info->widget == child)
1516 *expand = child_info->expand;
1518 *fill = child_info->fill;
1520 *padding = child_info->padding;
1522 *pack_type = child_info->pack;
1527 * gtk_box_set_child_packing:
1529 * @child: the #GtkWidget of the child to set
1530 * @expand: the new value of the #GtkBox:expand child property
1531 * @fill: the new value of the #GtkBox:fill child property
1532 * @padding: the new value of the #GtkBox:padding child property
1533 * @pack_type: the new value of the #GtkBox:pack-type child property
1535 * Sets the way @child is packed into @box.
1538 gtk_box_set_child_packing (GtkBox *box,
1543 GtkPackType pack_type)
1545 GtkBoxPrivate *private;
1547 GtkBoxChild *child_info = NULL;
1549 g_return_if_fail (GTK_IS_BOX (box));
1550 g_return_if_fail (GTK_IS_WIDGET (child));
1552 private = box->priv;
1554 list = private->children;
1557 child_info = list->data;
1558 if (child_info->widget == child)
1564 gtk_widget_freeze_child_notify (child);
1567 child_info->expand = expand != FALSE;
1568 gtk_widget_child_notify (child, "expand");
1569 child_info->fill = fill != FALSE;
1570 gtk_widget_child_notify (child, "fill");
1571 child_info->padding = padding;
1572 gtk_widget_child_notify (child, "padding");
1573 if (pack_type == GTK_PACK_END)
1574 child_info->pack = GTK_PACK_END;
1576 child_info->pack = GTK_PACK_START;
1577 gtk_widget_child_notify (child, "pack-type");
1579 if (gtk_widget_get_visible (child)
1580 && gtk_widget_get_visible (GTK_WIDGET (box)))
1581 gtk_widget_queue_resize (child);
1583 gtk_widget_thaw_child_notify (child);
1587 _gtk_box_set_old_defaults (GtkBox *box)
1589 GtkBoxPrivate *private;
1591 g_return_if_fail (GTK_IS_BOX (box));
1593 private = box->priv;
1595 private->default_expand = TRUE;
1599 gtk_box_add (GtkContainer *container,
1602 GtkBoxPrivate *priv = GTK_BOX (container)->priv;
1604 gtk_box_pack_start (GTK_BOX (container), widget,
1605 priv->default_expand,
1606 priv->default_expand,
1611 gtk_box_remove (GtkContainer *container,
1614 GtkBox *box = GTK_BOX (container);
1615 GtkBoxPrivate *priv = box->priv;
1619 children = priv->children;
1622 child = children->data;
1624 if (child->widget == widget)
1626 gboolean was_visible;
1628 was_visible = gtk_widget_get_visible (widget);
1629 gtk_widget_unparent (widget);
1631 priv->children = g_list_remove_link (priv->children, children);
1632 g_list_free (children);
1635 /* queue resize regardless of gtk_widget_get_visible (container),
1636 * since that's what is needed by toplevels.
1639 gtk_widget_queue_resize (GTK_WIDGET (container));
1644 children = children->next;
1649 gtk_box_forall (GtkContainer *container,
1650 gboolean include_internals,
1651 GtkCallback callback,
1652 gpointer callback_data)
1654 GtkBox *box = GTK_BOX (container);
1655 GtkBoxPrivate *priv = box->priv;
1659 children = priv->children;
1662 child = children->data;
1663 children = children->next;
1665 if (child->pack == GTK_PACK_START)
1666 (* callback) (child->widget, callback_data);
1669 children = g_list_last (priv->children);
1672 child = children->data;
1673 children = children->prev;
1675 if (child->pack == GTK_PACK_END)
1676 (* callback) (child->widget, callback_data);
1681 _gtk_box_get_children (GtkBox *box)
1683 GtkBoxPrivate *priv;
1686 GList *retval = NULL;
1688 g_return_val_if_fail (GTK_IS_BOX (box), NULL);
1692 children = priv->children;
1695 child = children->data;
1696 children = children->next;
1698 retval = g_list_prepend (retval, child->widget);
1701 return g_list_reverse (retval);