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/.
30 #include "gtkorientable.h"
31 #include "gtkextendedlayout.h"
32 #include "gtkprivate.h"
53 typedef struct _GtkBoxPrivate GtkBoxPrivate;
57 GtkOrientation orientation;
58 guint default_expand : 1;
59 guint spacing_set : 1;
62 #define GTK_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_BOX, GtkBoxPrivate))
64 typedef struct _GtkBoxDesiredSizes GtkBoxDesiredSizes;
65 typedef struct _GtkBoxSpreading GtkBoxSpreading;
67 struct _GtkBoxDesiredSizes
73 struct _GtkBoxSpreading
79 static void gtk_box_size_allocate (GtkWidget *widget,
80 GtkAllocation *allocation);
82 static void gtk_box_set_property (GObject *object,
86 static void gtk_box_get_property (GObject *object,
91 static void gtk_box_add (GtkContainer *container,
93 static void gtk_box_remove (GtkContainer *container,
95 static void gtk_box_forall (GtkContainer *container,
96 gboolean include_internals,
98 gpointer callback_data);
99 static void gtk_box_set_child_property (GtkContainer *container,
104 static void gtk_box_get_child_property (GtkContainer *container,
109 static GType gtk_box_child_type (GtkContainer *container);
112 static void gtk_box_extended_layout_init (GtkExtendedLayoutIface *iface);
113 static void gtk_box_get_desired_size (GtkExtendedLayout *layout,
114 GtkRequisition *minimum_size,
115 GtkRequisition *natural_size);
116 static gboolean gtk_box_is_height_for_width (GtkExtendedLayout *layout);
117 static void gtk_box_get_width_for_height (GtkExtendedLayout *layout,
120 gint *natural_width);
121 static void gtk_box_get_height_for_width (GtkExtendedLayout *layout,
123 gint *minimum_height,
124 gint *natural_height);
126 static GtkExtendedLayoutIface *parent_extended_layout_iface;
128 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
129 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
131 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
132 gtk_box_extended_layout_init));
135 gtk_box_class_init (GtkBoxClass *class)
137 GObjectClass *object_class = G_OBJECT_CLASS (class);
138 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
139 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
141 object_class->set_property = gtk_box_set_property;
142 object_class->get_property = gtk_box_get_property;
144 widget_class->size_allocate = gtk_box_size_allocate;
146 container_class->add = gtk_box_add;
147 container_class->remove = gtk_box_remove;
148 container_class->forall = gtk_box_forall;
149 container_class->child_type = gtk_box_child_type;
150 container_class->set_child_property = gtk_box_set_child_property;
151 container_class->get_child_property = gtk_box_get_child_property;
153 g_object_class_override_property (object_class,
157 g_object_class_install_property (object_class,
159 g_param_spec_int ("spacing",
161 P_("The amount of space between children"),
165 GTK_PARAM_READWRITE));
167 g_object_class_install_property (object_class,
169 g_param_spec_boolean ("homogeneous",
171 P_("Whether the children should all be the same size"),
173 GTK_PARAM_READWRITE));
175 gtk_container_class_install_child_property (container_class,
177 g_param_spec_boolean ("expand",
179 P_("Whether the child should receive extra space when the parent grows"),
181 GTK_PARAM_READWRITE));
182 gtk_container_class_install_child_property (container_class,
184 g_param_spec_boolean ("fill",
186 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
188 GTK_PARAM_READWRITE));
189 gtk_container_class_install_child_property (container_class,
191 g_param_spec_uint ("padding",
193 P_("Extra space to put between the child and its neighbors, in pixels"),
195 GTK_PARAM_READWRITE));
196 gtk_container_class_install_child_property (container_class,
197 CHILD_PROP_PACK_TYPE,
198 g_param_spec_enum ("pack-type",
200 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
201 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
202 GTK_PARAM_READWRITE));
203 gtk_container_class_install_child_property (container_class,
205 g_param_spec_int ("position",
207 P_("The index of the child in the parent"),
209 GTK_PARAM_READWRITE));
211 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
215 gtk_box_init (GtkBox *box)
217 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
219 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
220 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
222 box->children = NULL;
224 box->homogeneous = FALSE;
226 private->orientation = GTK_ORIENTATION_HORIZONTAL;
227 private->default_expand = FALSE;
228 private->spacing_set = FALSE;
232 gtk_box_set_property (GObject *object,
237 GtkBox *box = GTK_BOX (object);
238 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
242 case PROP_ORIENTATION:
243 private->orientation = g_value_get_enum (value);
244 gtk_widget_queue_resize (GTK_WIDGET (box));
247 gtk_box_set_spacing (box, g_value_get_int (value));
249 case PROP_HOMOGENEOUS:
250 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 gtk_box_get_property (GObject *object,
264 GtkBox *box = GTK_BOX (object);
265 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
269 case PROP_ORIENTATION:
270 g_value_set_enum (value, private->orientation);
273 g_value_set_int (value, box->spacing);
275 case PROP_HOMOGENEOUS:
276 g_value_set_boolean (value, box->homogeneous);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gtk_box_compare_gap (gconstpointer p1,
290 GtkBoxDesiredSizes *sizes = data;
291 const GtkBoxSpreading *c1 = p1;
292 const GtkBoxSpreading *c2 = p2;
294 const gint d1 = MAX (sizes[c1->index].natural_size -
295 sizes[c1->index].minimum_size,
297 const gint d2 = MAX (sizes[c2->index].natural_size -
298 sizes[c2->index].minimum_size,
301 gint delta = (d2 - d1);
304 delta = (c2->index - c1->index);
311 gtk_box_size_allocate (GtkWidget *widget,
312 GtkAllocation *allocation)
314 GtkBox *box = GTK_BOX (widget);
315 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
319 gint nexpand_children;
321 widget->allocation = *allocation;
324 nexpand_children = 0;
326 for (children = box->children; children; children = children->next)
328 child = children->data;
330 if (gtk_widget_get_visible (child->widget))
334 nexpand_children += 1;
338 if (nvis_children > 0)
340 gint border_width = GTK_CONTAINER (box)->border_width;
341 GtkTextDirection direction = gtk_widget_get_direction (widget);
342 GtkAllocation child_allocation;
343 GtkBoxSpreading *spreading = g_newa (GtkBoxSpreading, nvis_children);
344 GtkBoxDesiredSizes *sizes = g_newa (GtkBoxDesiredSizes, nvis_children);
353 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
354 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
356 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
358 /* Retrieve desired size for visible children */
360 children = box->children;
363 child = children->data;
364 children = children->next;
366 if (gtk_widget_get_visible (child->widget))
368 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
369 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
371 &sizes[i].minimum_size,
372 &sizes[i].natural_size);
374 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
376 &sizes[i].minimum_size,
377 &sizes[i].natural_size);
380 /* Assert the api is working properly */
381 g_assert (sizes[i].minimum_size >= 0);
382 g_assert (sizes[i].natural_size >= sizes[i].minimum_size);
384 size -= sizes[i].minimum_size;
385 size -= child->padding * 2;
387 spreading[i].index = i;
388 spreading[i].child = child;
394 if (box->homogeneous)
396 /* If were homogenous we still need to run the above loop to get the minimum sizes
397 * for children that are not going to fill
399 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
400 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
402 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
404 extra = size / nvis_children;
409 /* Distribute the container's extra space c_gap. We want to assign
410 * this space such that the sum of extra space assigned to children
411 * (c^i_gap) is equal to c_cap. The case that there's not enough
412 * space for all children to take their natural size needs some
413 * attention. The goals we want to achieve are:
415 * a) Maximize number of children taking their natural size.
416 * b) The allocated size of children should be a continuous
417 * function of c_gap. That is, increasing the container size by
418 * one pixel should never make drastic changes in the distribution.
419 * c) If child i takes its natural size and child j doesn't,
420 * child j should have received at least as much gap as child i.
422 * The following code distributes the additional space by following
426 /* Sort descending by gap and position. */
428 g_qsort_with_data (spreading,
429 nvis_children, sizeof (GtkBoxSpreading),
430 gtk_box_compare_gap, sizes);
432 /* Distribute available space.
433 * This master piece of a loop was conceived by Behdad Esfahbod.
435 for (i = nvis_children - 1; i >= 0; --i)
437 /* Divide remaining space by number of remaining children.
438 * Sort order and reducing remaining space by assigned space
439 * ensures that space is distributed equally.
441 gint glue = (size + i) / (i + 1);
442 gint gap = sizes[spreading[i].index].natural_size
443 - sizes[spreading[i].index].minimum_size;
445 extra = MIN (glue, gap);
446 sizes[spreading[i].index].minimum_size += extra;
451 /* Calculate space which hasn't distributed yet,
452 * and is available for expanding children.
454 if (nexpand_children > 0)
455 extra = size / nexpand_children;
460 /* Allocate child positions. */
462 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
464 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
466 child_allocation.y = allocation->y + border_width;
467 child_allocation.height = MAX (1, allocation->height - border_width * 2);
468 if (packing == GTK_PACK_START)
469 x = allocation->x + border_width;
471 x = allocation->x + allocation->width - border_width;
475 child_allocation.x = allocation->x + border_width;
476 child_allocation.width = MAX (1, allocation->width - border_width * 2);
477 if (packing == GTK_PACK_START)
478 y = allocation->y + border_width;
480 y = allocation->y + allocation->height - border_width;
484 children = box->children;
487 child = children->data;
488 children = children->next;
490 if (gtk_widget_get_visible (child->widget))
492 if (child->pack == packing)
494 /* Assign the child's size. */
495 if (box->homogeneous)
497 if (nvis_children == 1)
507 child_size = sizes[i].minimum_size + child->padding * 2;
511 if (nexpand_children == 1)
516 nexpand_children -= 1;
521 /* Assign the child's position. */
522 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
526 child_allocation.width = MAX (1, child_size - child->padding * 2);
527 child_allocation.x = x + child->padding;
531 child_allocation.width = sizes[i].minimum_size;
532 child_allocation.x = x + (child_size - child_allocation.width) / 2;
535 if (direction == GTK_TEXT_DIR_RTL)
536 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
538 if (packing == GTK_PACK_START)
540 x += child_size + box->spacing;
544 x -= child_size + box->spacing;
546 child_allocation.x -= child_allocation.width;
549 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
553 child_allocation.height = MAX (1, child_size - child->padding * 2);
554 child_allocation.y = y + child->padding;
558 child_allocation.height = sizes[i].minimum_size;
559 child_allocation.y = y + (child_size - child_allocation.height) / 2;
562 if (packing == GTK_PACK_START)
564 y += child_size + box->spacing;
568 y -= child_size + box->spacing;
570 child_allocation.y -= child_allocation.height;
573 gtk_widget_size_allocate (child->widget, &child_allocation);
585 gtk_box_child_type (GtkContainer *container)
587 return GTK_TYPE_WIDGET;
591 gtk_box_set_child_property (GtkContainer *container,
600 GtkPackType pack_type = 0;
602 if (property_id != CHILD_PROP_POSITION)
603 gtk_box_query_child_packing (GTK_BOX (container),
611 case CHILD_PROP_EXPAND:
612 gtk_box_set_child_packing (GTK_BOX (container),
614 g_value_get_boolean (value),
619 case CHILD_PROP_FILL:
620 gtk_box_set_child_packing (GTK_BOX (container),
623 g_value_get_boolean (value),
627 case CHILD_PROP_PADDING:
628 gtk_box_set_child_packing (GTK_BOX (container),
632 g_value_get_uint (value),
635 case CHILD_PROP_PACK_TYPE:
636 gtk_box_set_child_packing (GTK_BOX (container),
641 g_value_get_enum (value));
643 case CHILD_PROP_POSITION:
644 gtk_box_reorder_child (GTK_BOX (container),
646 g_value_get_int (value));
649 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
655 gtk_box_get_child_property (GtkContainer *container,
664 GtkPackType pack_type = 0;
667 if (property_id != CHILD_PROP_POSITION)
668 gtk_box_query_child_packing (GTK_BOX (container),
677 case CHILD_PROP_EXPAND:
678 g_value_set_boolean (value, expand);
680 case CHILD_PROP_FILL:
681 g_value_set_boolean (value, fill);
683 case CHILD_PROP_PADDING:
684 g_value_set_uint (value, padding);
686 case CHILD_PROP_PACK_TYPE:
687 g_value_set_enum (value, pack_type);
689 case CHILD_PROP_POSITION:
691 for (list = GTK_BOX (container)->children; list; list = list->next)
693 GtkBoxChild *child_entry;
695 child_entry = list->data;
696 if (child_entry->widget == child)
700 g_value_set_int (value, list ? i : -1);
703 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
709 gtk_box_pack (GtkBox *box,
714 GtkPackType pack_type)
716 GtkBoxChild *child_info;
718 g_return_if_fail (GTK_IS_BOX (box));
719 g_return_if_fail (GTK_IS_WIDGET (child));
720 g_return_if_fail (child->parent == NULL);
722 child_info = g_new (GtkBoxChild, 1);
723 child_info->widget = child;
724 child_info->padding = padding;
725 child_info->expand = expand ? TRUE : FALSE;
726 child_info->fill = fill ? TRUE : FALSE;
727 child_info->pack = pack_type;
728 child_info->is_secondary = FALSE;
730 box->children = g_list_append (box->children, child_info);
732 gtk_widget_freeze_child_notify (child);
734 gtk_widget_set_parent (child, GTK_WIDGET (box));
736 gtk_widget_child_notify (child, "expand");
737 gtk_widget_child_notify (child, "fill");
738 gtk_widget_child_notify (child, "padding");
739 gtk_widget_child_notify (child, "pack-type");
740 gtk_widget_child_notify (child, "position");
741 gtk_widget_thaw_child_notify (child);
747 gtk_box_extended_layout_init (GtkExtendedLayoutIface *iface)
749 parent_extended_layout_iface = g_type_interface_peek_parent (iface);
751 iface->is_height_for_width = gtk_box_is_height_for_width;
752 iface->get_desired_size = gtk_box_get_desired_size;
753 iface->get_height_for_width = gtk_box_get_height_for_width;
754 iface->get_width_for_height = gtk_box_get_width_for_height;
758 gtk_box_is_height_for_width (GtkExtendedLayout *layout)
760 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
762 return (private->orientation == GTK_ORIENTATION_HORIZONTAL);
766 gtk_box_get_desired_size (GtkExtendedLayout *layout,
767 GtkRequisition *minimum_size,
768 GtkRequisition *natural_size)
771 GtkBoxPrivate *private;
776 box = GTK_BOX (layout);
777 private = GTK_BOX_GET_PRIVATE (box);
778 border_width = GTK_CONTAINER (box)->border_width;
780 minimum_size->width = minimum_size->height = 0;
781 natural_size->width = natural_size->height = 0;
784 children = box->children;
789 child = children->data;
790 children = children->next;
792 if (gtk_widget_get_visible (child->widget))
794 GtkRequisition child_minimum_size;
795 GtkRequisition child_natural_size;
797 gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (child->widget),
799 &child_natural_size);
801 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
803 if (box->homogeneous)
807 width = child_minimum_size.width + child->padding * 2;
808 minimum_size->width = MAX (minimum_size->width, width);
810 width = child_natural_size.width + child->padding * 2;
811 natural_size->width = MAX (natural_size->width, width);
815 minimum_size->width += child_minimum_size.width + child->padding * 2;
816 natural_size->width += child_natural_size.width + child->padding * 2;
819 minimum_size->height = MAX (minimum_size->height, child_minimum_size.height);
820 natural_size->height = MAX (natural_size->height, child_natural_size.height);
824 if (box->homogeneous)
828 height = child_minimum_size.height + child->padding * 2;
829 minimum_size->height = MAX (minimum_size->height, height);
831 height = child_natural_size.height + child->padding * 2;
832 natural_size->height = MAX (natural_size->height, height);
836 minimum_size->height += child_minimum_size.height + child->padding * 2;
837 natural_size->height += child_natural_size.height + child->padding * 2;
840 minimum_size->width = MAX (minimum_size->width, child_minimum_size.width);
841 natural_size->width = MAX (natural_size->width, child_natural_size.width);
848 if (nvis_children > 0)
850 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
852 if (box->homogeneous)
854 minimum_size->width *= nvis_children;
855 natural_size->width *= nvis_children;
858 minimum_size->width += (nvis_children - 1) * box->spacing;
859 natural_size->width += (nvis_children - 1) * box->spacing;
863 if (box->homogeneous)
865 minimum_size->height *= nvis_children;
866 natural_size->height *= nvis_children;
869 minimum_size->height += (nvis_children - 1) * box->spacing;
870 natural_size->height += (nvis_children - 1) * box->spacing;
874 minimum_size->width += border_width * 2;
875 minimum_size->height += border_width * 2;
877 natural_size->width += border_width * 2;
878 natural_size->height += border_width * 2;
883 * size_fits_for_dimension:
885 * @avail_size: the allocated size in @box's opposing orientation
886 * @check_size: the size in @box's orientation to check
887 * @check_natural: whether to check natural sizes or minimum sizes.
889 * This checks if the required size of @box and its children fit into @avail_size
890 * in @box's opposing orientation if @box were given @check_size as an allocation
891 * in @box's orientation.
893 * In context: A GtkVBox will check if it fits into the available allocated height
894 * if it were given the @check_size in width.
898 size_fits_for_dimension (GtkBox *box,
901 gboolean check_natural)
903 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
905 gint nvis_children = 0;
906 gint required_size = 0, child_size;
907 gint largest_child = 0;
909 avail_size -= GTK_CONTAINER (box)->border_width * 2;
911 for (children = box->children; children != NULL;
912 children = children->next, nvis_children++)
914 GtkBoxChild *child = children->data;
916 if (gtk_widget_get_visible (child->widget))
919 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
922 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
923 avail_size, NULL, &child_size);
925 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
926 avail_size, &child_size, NULL);
931 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
932 avail_size, NULL, &child_size);
934 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
935 avail_size, &child_size, NULL);
938 child_size += child->padding * 2;
940 if (child_size > largest_child)
941 largest_child = child_size;
943 required_size += child_size;
947 if (nvis_children > 0)
949 if (box->homogeneous)
950 required_size = largest_child * nvis_children;
952 required_size += (nvis_children - 1) * box->spacing;
955 required_size += GTK_CONTAINER (box)->border_width * 2;
957 return required_size <= check_size;
962 gtk_box_bisect_for_size_in_opposing_orientation (GtkBox *box,
963 gboolean check_natural,
969 if (ceiling - floor <= 1)
973 gint check_size = floor + (ceiling - floor) / 2;
975 if (size_fits_for_dimension (box, avail_size, check_size, check_natural))
977 /* If check_size is large enough for box to fit into avail_size, we go on
978 * to check between the given floor and check_size as the new ceiling
981 gtk_box_bisect_for_size_in_opposing_orientation (box, check_natural, avail_size,
982 floor, check_size, size);
984 gtk_box_bisect_for_size_in_opposing_orientation (box, check_natural, avail_size,
985 check_size, ceiling, size);
991 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
996 gint minimum, natural;
997 gint min_ceiling, nat_ceiling;
1000 /* First a large gap to search inside of
1002 for (min_ceiling = (step + 1); !size_fits_for_dimension (box, avail_size, min_ceiling, FALSE); min_ceiling += step);
1004 /* This will find the minimum sizes by halfing the guesses until they are found
1006 gtk_box_bisect_for_size_in_opposing_orientation (box, FALSE, avail_size,
1011 /* Basing the natural size on the found minimum, do the same operation for the natural size */
1012 for (nat_ceiling = minimum + step; !size_fits_for_dimension (box, avail_size, nat_ceiling, TRUE); nat_ceiling += step);
1013 gtk_box_bisect_for_size_in_opposing_orientation (box, TRUE, avail_size,
1019 *minimum_size = minimum;
1022 *natural_size = natural;
1027 gtk_box_compute_size_for_orientation (GtkBox *box,
1032 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
1034 gint nvis_children = 0;
1035 gint required_size = 0, required_natural = 0, child_size, child_natural;
1036 gint largest_child = 0, largest_natural = 0;
1038 avail_size -= GTK_CONTAINER (box)->border_width * 2;
1040 for (children = box->children; children != NULL;
1041 children = children->next, nvis_children++)
1043 GtkBoxChild *child = children->data;
1045 if (gtk_widget_get_visible (child->widget))
1048 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1049 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
1050 avail_size, &child_size, &child_natural);
1052 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
1053 avail_size, &child_size, &child_natural);
1056 child_size += child->padding * 2;
1057 child_natural += child->padding * 2;
1059 if (child_size > largest_child)
1060 largest_child = child_size;
1062 if (child_natural > largest_natural)
1063 largest_natural = child_natural;
1065 required_size += child_size;
1066 required_natural += child_natural;
1070 if (nvis_children > 0)
1072 if (box->homogeneous)
1074 required_size = largest_child * nvis_children;
1075 required_natural = largest_natural * nvis_children;
1078 required_size += (nvis_children - 1) * box->spacing;
1079 required_natural += (nvis_children - 1) * box->spacing;
1082 required_size += GTK_CONTAINER (box)->border_width * 2;
1083 required_natural += GTK_CONTAINER (box)->border_width * 2;
1086 *minimum_size = required_size;
1089 *natural_size = required_natural;
1093 gtk_box_get_width_for_height (GtkExtendedLayout *layout,
1095 gint *minimum_width,
1096 gint *natural_width)
1098 GtkBox *box = GTK_BOX (layout);
1099 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1101 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1104 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1106 /* Have the base class return the values previously computed by get_desired_size() */
1107 parent_extended_layout_iface->get_width_for_height (layout, height, minimum_width, natural_width);
1111 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1115 gtk_box_get_height_for_width (GtkExtendedLayout *layout,
1117 gint *minimum_height,
1118 gint *natural_height)
1120 GtkBox *box = GTK_BOX (layout);
1121 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1123 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1126 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1128 /* Have the base class return the values previously computed by get_desired_size() */
1129 parent_extended_layout_iface->get_height_for_width (layout, width, minimum_height, natural_height);
1133 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1138 * @orientation: the box' orientation.
1139 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1140 * @spacing: the number of pixels to place by default between children.
1142 * Creates a new #GtkHBox.
1144 * Return value: a new #GtkHBox.
1149 _gtk_box_new (GtkOrientation orientation,
1150 gboolean homogeneous,
1153 return g_object_new (GTK_TYPE_BOX,
1154 "orientation", orientation,
1156 "homogeneous", homogeneous ? TRUE : FALSE,
1161 * gtk_box_pack_start:
1163 * @child: the #GtkWidget to be added to @box
1164 * @expand: %TRUE if the new child is to be given extra space allocated to
1165 * @box. The extra space will be divided evenly between all children of
1166 * @box that use this option
1167 * @fill: %TRUE if space given to @child by the @expand option is
1168 * actually allocated to @child, rather than just padding it. This
1169 * parameter has no effect if @expand is set to %FALSE. A child is
1170 * always allocated the full height of a #GtkHBox and the full width
1171 * of a #GtkVBox. This option affects the other dimension
1172 * @padding: extra space in pixels to put between this child and its
1173 * neighbors, over and above the global amount specified by
1174 * #GtkBox:spacing property. If @child is a widget at one of the
1175 * reference ends of @box, then @padding pixels are also put between
1176 * @child and the reference edge of @box
1178 * Adds @child to @box, packed with reference to the start of @box.
1179 * The @child is packed after any other child packed with reference
1180 * to the start of @box.
1183 gtk_box_pack_start (GtkBox *box,
1189 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1195 * @child: the #GtkWidget to be added to @box
1196 * @expand: %TRUE if the new child is to be given extra space allocated
1197 * to @box. The extra space will be divided evenly between all children
1198 * of @box that use this option
1199 * @fill: %TRUE if space given to @child by the @expand option is
1200 * actually allocated to @child, rather than just padding it. This
1201 * parameter has no effect if @expand is set to %FALSE. A child is
1202 * always allocated the full height of a #GtkHBox and the full width
1203 * of a #GtkVBox. This option affects the other dimension
1204 * @padding: extra space in pixels to put between this child and its
1205 * neighbors, over and above the global amount specified by
1206 * #GtkBox:spacing property. If @child is a widget at one of the
1207 * reference ends of @box, then @padding pixels are also put between
1208 * @child and the reference edge of @box
1210 * Adds @child to @box, packed with reference to the end of @box.
1211 * The @child is packed after (away from end of) any other child
1212 * packed with reference to the end of @box.
1215 gtk_box_pack_end (GtkBox *box,
1221 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1225 * gtk_box_pack_start_defaults:
1227 * @widget: the #GtkWidget to be added to @box
1229 * Adds @widget to @box, packed with reference to the start of @box.
1230 * The child is packed after any other child packed with reference
1231 * to the start of @box.
1233 * Parameters for how to pack the child @widget, #GtkBox:expand,
1234 * #GtkBox:fill and #GtkBox:padding, are given their default
1235 * values, %TRUE, %TRUE, and 0, respectively.
1237 * Deprecated: 2.14: Use gtk_box_pack_start()
1240 gtk_box_pack_start_defaults (GtkBox *box,
1243 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
1247 * gtk_box_pack_end_defaults:
1249 * @widget: the #GtkWidget to be added to @box
1251 * Adds @widget to @box, packed with reference to the end of @box.
1252 * The child is packed after any other child packed with reference
1253 * to the start of @box.
1255 * Parameters for how to pack the child @widget, #GtkBox:expand,
1256 * #GtkBox:fill and #GtkBox:padding, are given their default
1257 * values, %TRUE, %TRUE, and 0, respectively.
1259 * Deprecated: 2.14: Use gtk_box_pack_end()
1262 gtk_box_pack_end_defaults (GtkBox *box,
1265 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
1269 * gtk_box_set_homogeneous:
1271 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1272 * %FALSE for variable allotments
1274 * Sets the #GtkBox:homogeneous property of @box, controlling
1275 * whether or not all children of @box are given equal space
1279 gtk_box_set_homogeneous (GtkBox *box,
1280 gboolean homogeneous)
1282 g_return_if_fail (GTK_IS_BOX (box));
1284 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
1286 box->homogeneous = homogeneous ? TRUE : FALSE;
1287 g_object_notify (G_OBJECT (box), "homogeneous");
1288 gtk_widget_queue_resize (GTK_WIDGET (box));
1293 * gtk_box_get_homogeneous:
1296 * Returns whether the box is homogeneous (all children are the
1297 * same size). See gtk_box_set_homogeneous().
1299 * Return value: %TRUE if the box is homogeneous.
1302 gtk_box_get_homogeneous (GtkBox *box)
1304 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1306 return box->homogeneous;
1310 * gtk_box_set_spacing:
1312 * @spacing: the number of pixels to put between children
1314 * Sets the #GtkBox:spacing property of @box, which is the
1315 * number of pixels to place between children of @box.
1318 gtk_box_set_spacing (GtkBox *box,
1321 g_return_if_fail (GTK_IS_BOX (box));
1323 if (spacing != box->spacing)
1325 box->spacing = spacing;
1326 _gtk_box_set_spacing_set (box, TRUE);
1328 g_object_notify (G_OBJECT (box), "spacing");
1330 gtk_widget_queue_resize (GTK_WIDGET (box));
1335 * gtk_box_get_spacing:
1338 * Gets the value set by gtk_box_set_spacing().
1340 * Return value: spacing between children
1343 gtk_box_get_spacing (GtkBox *box)
1345 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1347 return box->spacing;
1351 _gtk_box_set_spacing_set (GtkBox *box,
1352 gboolean spacing_set)
1354 GtkBoxPrivate *private;
1356 g_return_if_fail (GTK_IS_BOX (box));
1358 private = GTK_BOX_GET_PRIVATE (box);
1360 private->spacing_set = spacing_set ? TRUE : FALSE;
1364 _gtk_box_get_spacing_set (GtkBox *box)
1366 GtkBoxPrivate *private;
1368 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1370 private = GTK_BOX_GET_PRIVATE (box);
1372 return private->spacing_set;
1376 * gtk_box_reorder_child:
1378 * @child: the #GtkWidget to move
1379 * @position: the new position for @child in the list of children
1380 * of @box, starting from 0. If negative, indicates the end of
1383 * Moves @child to a new @position in the list of @box children.
1384 * The list is the <structfield>children</structfield> field of
1385 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1386 * as well as widgets packed #GTK_PACK_END, in the order that these
1387 * widgets were added to @box.
1389 * A widget's position in the @box children list determines where
1390 * the widget is packed into @box. A child widget at some position
1391 * in the list will be packed just after all other widgets of the
1392 * same packing type that appear earlier in the list.
1395 gtk_box_reorder_child (GtkBox *box,
1401 GtkBoxChild *child_info = NULL;
1404 g_return_if_fail (GTK_IS_BOX (box));
1405 g_return_if_fail (GTK_IS_WIDGET (child));
1407 old_link = box->children;
1411 child_info = old_link->data;
1412 if (child_info->widget == child)
1415 old_link = old_link->next;
1419 g_return_if_fail (old_link != NULL);
1421 if (position == old_position)
1424 box->children = g_list_delete_link (box->children, old_link);
1429 new_link = g_list_nth (box->children, position);
1431 box->children = g_list_insert_before (box->children, new_link, child_info);
1433 gtk_widget_child_notify (child, "position");
1434 if (gtk_widget_get_visible (child)
1435 && gtk_widget_get_visible (GTK_WIDGET (box)))
1436 gtk_widget_queue_resize (child);
1440 * gtk_box_query_child_packing:
1442 * @child: the #GtkWidget of the child to query
1443 * @expand: pointer to return location for #GtkBox:expand child property
1444 * @fill: pointer to return location for #GtkBox:fill child property
1445 * @padding: pointer to return location for #GtkBox:padding child property
1446 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1448 * Obtains information about how @child is packed into @box.
1451 gtk_box_query_child_packing (GtkBox *box,
1456 GtkPackType *pack_type)
1459 GtkBoxChild *child_info = NULL;
1461 g_return_if_fail (GTK_IS_BOX (box));
1462 g_return_if_fail (GTK_IS_WIDGET (child));
1464 list = box->children;
1467 child_info = list->data;
1468 if (child_info->widget == child)
1477 *expand = child_info->expand;
1479 *fill = child_info->fill;
1481 *padding = child_info->padding;
1483 *pack_type = child_info->pack;
1488 * gtk_box_set_child_packing:
1490 * @child: the #GtkWidget of the child to set
1491 * @expand: the new value of the #GtkBox:expand child property
1492 * @fill: the new value of the #GtkBox:fill child property
1493 * @padding: the new value of the #GtkBox:padding child property
1494 * @pack_type: the new value of the #GtkBox:pack-type child property
1496 * Sets the way @child is packed into @box.
1499 gtk_box_set_child_packing (GtkBox *box,
1504 GtkPackType pack_type)
1507 GtkBoxChild *child_info = NULL;
1509 g_return_if_fail (GTK_IS_BOX (box));
1510 g_return_if_fail (GTK_IS_WIDGET (child));
1512 list = box->children;
1515 child_info = list->data;
1516 if (child_info->widget == child)
1522 gtk_widget_freeze_child_notify (child);
1525 child_info->expand = expand != FALSE;
1526 gtk_widget_child_notify (child, "expand");
1527 child_info->fill = fill != FALSE;
1528 gtk_widget_child_notify (child, "fill");
1529 child_info->padding = padding;
1530 gtk_widget_child_notify (child, "padding");
1531 if (pack_type == GTK_PACK_END)
1532 child_info->pack = GTK_PACK_END;
1534 child_info->pack = GTK_PACK_START;
1535 gtk_widget_child_notify (child, "pack-type");
1537 if (gtk_widget_get_visible (child)
1538 && gtk_widget_get_visible (GTK_WIDGET (box)))
1539 gtk_widget_queue_resize (child);
1541 gtk_widget_thaw_child_notify (child);
1545 _gtk_box_set_old_defaults (GtkBox *box)
1547 GtkBoxPrivate *private;
1549 g_return_if_fail (GTK_IS_BOX (box));
1551 private = GTK_BOX_GET_PRIVATE (box);
1553 private->default_expand = TRUE;
1557 gtk_box_add (GtkContainer *container,
1560 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (container);
1562 gtk_box_pack_start (GTK_BOX (container), widget,
1563 private->default_expand,
1564 private->default_expand,
1569 gtk_box_remove (GtkContainer *container,
1572 GtkBox *box = GTK_BOX (container);
1576 children = box->children;
1579 child = children->data;
1581 if (child->widget == widget)
1583 gboolean was_visible;
1585 was_visible = gtk_widget_get_visible (widget);
1586 gtk_widget_unparent (widget);
1588 box->children = g_list_remove_link (box->children, children);
1589 g_list_free (children);
1592 /* queue resize regardless of gtk_widget_get_visible (container),
1593 * since that's what is needed by toplevels.
1596 gtk_widget_queue_resize (GTK_WIDGET (container));
1601 children = children->next;
1606 gtk_box_forall (GtkContainer *container,
1607 gboolean include_internals,
1608 GtkCallback callback,
1609 gpointer callback_data)
1611 GtkBox *box = GTK_BOX (container);
1615 children = box->children;
1618 child = children->data;
1619 children = children->next;
1621 if (child->pack == GTK_PACK_START)
1622 (* callback) (child->widget, callback_data);
1625 children = g_list_last (box->children);
1628 child = children->data;
1629 children = children->prev;
1631 if (child->pack == GTK_PACK_END)
1632 (* callback) (child->widget, callback_data);
1636 #define __GTK_BOX_C__
1637 #include "gtkaliasdef.c"