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 gboolean gtk_box_is_height_for_width (GtkExtendedLayout *layout);
114 static void gtk_box_get_desired_width (GtkExtendedLayout *layout,
117 static void gtk_box_get_desired_height (GtkExtendedLayout *layout,
120 static void gtk_box_get_width_for_height (GtkExtendedLayout *layout,
123 gint *natural_width);
124 static void gtk_box_get_height_for_width (GtkExtendedLayout *layout,
126 gint *minimum_height,
127 gint *natural_height);
129 static GtkExtendedLayoutIface *parent_extended_layout_iface;
131 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
132 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
134 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
135 gtk_box_extended_layout_init));
138 gtk_box_class_init (GtkBoxClass *class)
140 GObjectClass *object_class = G_OBJECT_CLASS (class);
141 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
142 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
144 object_class->set_property = gtk_box_set_property;
145 object_class->get_property = gtk_box_get_property;
147 widget_class->size_allocate = gtk_box_size_allocate;
149 container_class->add = gtk_box_add;
150 container_class->remove = gtk_box_remove;
151 container_class->forall = gtk_box_forall;
152 container_class->child_type = gtk_box_child_type;
153 container_class->set_child_property = gtk_box_set_child_property;
154 container_class->get_child_property = gtk_box_get_child_property;
156 g_object_class_override_property (object_class,
160 g_object_class_install_property (object_class,
162 g_param_spec_int ("spacing",
164 P_("The amount of space between children"),
168 GTK_PARAM_READWRITE));
170 g_object_class_install_property (object_class,
172 g_param_spec_boolean ("homogeneous",
174 P_("Whether the children should all be the same size"),
176 GTK_PARAM_READWRITE));
178 gtk_container_class_install_child_property (container_class,
180 g_param_spec_boolean ("expand",
182 P_("Whether the child should receive extra space when the parent grows"),
184 GTK_PARAM_READWRITE));
185 gtk_container_class_install_child_property (container_class,
187 g_param_spec_boolean ("fill",
189 P_("Whether extra space given to the child should be allocated to the child or used as padding"),
191 GTK_PARAM_READWRITE));
192 gtk_container_class_install_child_property (container_class,
194 g_param_spec_uint ("padding",
196 P_("Extra space to put between the child and its neighbors, in pixels"),
198 GTK_PARAM_READWRITE));
199 gtk_container_class_install_child_property (container_class,
200 CHILD_PROP_PACK_TYPE,
201 g_param_spec_enum ("pack-type",
203 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
204 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
205 GTK_PARAM_READWRITE));
206 gtk_container_class_install_child_property (container_class,
208 g_param_spec_int ("position",
210 P_("The index of the child in the parent"),
212 GTK_PARAM_READWRITE));
214 g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
218 gtk_box_init (GtkBox *box)
220 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
222 gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
223 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
225 box->children = NULL;
227 box->homogeneous = FALSE;
229 private->orientation = GTK_ORIENTATION_HORIZONTAL;
230 private->default_expand = FALSE;
231 private->spacing_set = FALSE;
235 gtk_box_set_property (GObject *object,
240 GtkBox *box = GTK_BOX (object);
241 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
245 case PROP_ORIENTATION:
246 private->orientation = g_value_get_enum (value);
247 gtk_widget_queue_resize (GTK_WIDGET (box));
250 gtk_box_set_spacing (box, g_value_get_int (value));
252 case PROP_HOMOGENEOUS:
253 gtk_box_set_homogeneous (box, g_value_get_boolean (value));
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 gtk_box_get_property (GObject *object,
267 GtkBox *box = GTK_BOX (object);
268 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
272 case PROP_ORIENTATION:
273 g_value_set_enum (value, private->orientation);
276 g_value_set_int (value, box->spacing);
278 case PROP_HOMOGENEOUS:
279 g_value_set_boolean (value, box->homogeneous);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 count_expand_children (GtkBox *box, gint *visible_children, gint *expand_children)
294 *visible_children = *expand_children = 0;
296 for (children = box->children; children; children = children->next)
298 child = children->data;
300 if (gtk_widget_get_visible (child->widget))
302 *visible_children += 1;
304 *expand_children += 1;
310 gtk_box_compare_gap (gconstpointer p1,
314 GtkBoxDesiredSizes *sizes = data;
315 const GtkBoxSpreading *c1 = p1;
316 const GtkBoxSpreading *c2 = p2;
318 const gint d1 = MAX (sizes[c1->index].natural_size -
319 sizes[c1->index].minimum_size,
321 const gint d2 = MAX (sizes[c2->index].natural_size -
322 sizes[c2->index].minimum_size,
325 gint delta = (d2 - d1);
328 delta = (c2->index - c1->index);
334 gtk_box_size_allocate (GtkWidget *widget,
335 GtkAllocation *allocation)
337 GtkBox *box = GTK_BOX (widget);
338 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
342 gint nexpand_children;
344 widget->allocation = *allocation;
346 count_expand_children (box, &nvis_children, &nexpand_children);
348 if (nvis_children > 0)
350 gint border_width = GTK_CONTAINER (box)->border_width;
351 GtkTextDirection direction = gtk_widget_get_direction (widget);
352 GtkAllocation child_allocation;
353 GtkBoxSpreading *spreading = g_newa (GtkBoxSpreading, nvis_children);
354 GtkBoxDesiredSizes *sizes = g_newa (GtkBoxDesiredSizes, nvis_children);
363 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
364 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
366 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
368 /* Retrieve desired size for visible children */
370 children = box->children;
373 child = children->data;
374 children = children->next;
376 if (gtk_widget_get_visible (child->widget))
378 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
379 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
381 &sizes[i].minimum_size,
382 &sizes[i].natural_size);
384 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
386 &sizes[i].minimum_size,
387 &sizes[i].natural_size);
390 /* Assert the api is working properly */
391 if (sizes[i].minimum_size < 0)
392 g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
393 gtk_widget_get_name (GTK_WIDGET (child->widget)),
394 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
395 sizes[i].minimum_size,
396 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
397 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
399 if (sizes[i].natural_size < sizes[i].minimum_size)
400 g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
401 gtk_widget_get_name (GTK_WIDGET (child->widget)),
402 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
403 sizes[i].natural_size,
404 sizes[i].minimum_size,
405 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
406 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
408 size -= sizes[i].minimum_size;
409 size -= child->padding * 2;
411 spreading[i].index = i;
412 spreading[i].child = child;
418 if (box->homogeneous)
420 /* If were homogenous we still need to run the above loop to get the minimum sizes
421 * for children that are not going to fill
423 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
424 size = allocation->width - border_width * 2 - (nvis_children - 1) * box->spacing;
426 size = allocation->height - border_width * 2 - (nvis_children - 1) * box->spacing;
428 extra = size / nvis_children;
433 /* Distribute the container's extra space c_gap. We want to assign
434 * this space such that the sum of extra space assigned to children
435 * (c^i_gap) is equal to c_cap. The case that there's not enough
436 * space for all children to take their natural size needs some
437 * attention. The goals we want to achieve are:
439 * a) Maximize number of children taking their natural size.
440 * b) The allocated size of children should be a continuous
441 * function of c_gap. That is, increasing the container size by
442 * one pixel should never make drastic changes in the distribution.
443 * c) If child i takes its natural size and child j doesn't,
444 * child j should have received at least as much gap as child i.
446 * The following code distributes the additional space by following
450 /* Sort descending by gap and position. */
452 g_qsort_with_data (spreading,
453 nvis_children, sizeof (GtkBoxSpreading),
454 gtk_box_compare_gap, sizes);
456 /* Distribute available space.
457 * This master piece of a loop was conceived by Behdad Esfahbod.
459 for (i = nvis_children - 1; i >= 0; --i)
461 /* Divide remaining space by number of remaining children.
462 * Sort order and reducing remaining space by assigned space
463 * ensures that space is distributed equally.
465 gint glue = (size + i) / (i + 1);
466 gint gap = sizes[spreading[i].index].natural_size
467 - sizes[spreading[i].index].minimum_size;
469 extra = MIN (glue, gap);
470 sizes[spreading[i].index].minimum_size += extra;
475 /* Calculate space which hasn't distributed yet,
476 * and is available for expanding children.
478 if (nexpand_children > 0)
479 extra = size / nexpand_children;
484 /* Allocate child positions. */
486 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
488 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
490 child_allocation.y = allocation->y + border_width;
491 child_allocation.height = MAX (1, allocation->height - border_width * 2);
492 if (packing == GTK_PACK_START)
493 x = allocation->x + border_width;
495 x = allocation->x + allocation->width - border_width;
499 child_allocation.x = allocation->x + border_width;
500 child_allocation.width = MAX (1, allocation->width - border_width * 2);
501 if (packing == GTK_PACK_START)
502 y = allocation->y + border_width;
504 y = allocation->y + allocation->height - border_width;
508 children = box->children;
511 child = children->data;
512 children = children->next;
514 if (gtk_widget_get_visible (child->widget))
516 if (child->pack == packing)
518 /* Assign the child's size. */
519 if (box->homogeneous)
521 if (nvis_children == 1)
531 child_size = sizes[i].minimum_size + child->padding * 2;
535 if (nexpand_children == 1)
540 nexpand_children -= 1;
545 /* Assign the child's position. */
546 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
550 child_allocation.width = MAX (1, child_size - child->padding * 2);
551 child_allocation.x = x + child->padding;
555 child_allocation.width = sizes[i].minimum_size;
556 child_allocation.x = x + (child_size - child_allocation.width) / 2;
559 if (packing == GTK_PACK_START)
561 x += child_size + box->spacing;
565 x -= child_size + box->spacing;
567 child_allocation.x -= child_size;
570 if (direction == GTK_TEXT_DIR_RTL)
571 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
574 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
578 child_allocation.height = MAX (1, child_size - child->padding * 2);
579 child_allocation.y = y + child->padding;
583 child_allocation.height = sizes[i].minimum_size;
584 child_allocation.y = y + (child_size - child_allocation.height) / 2;
587 if (packing == GTK_PACK_START)
589 y += child_size + box->spacing;
593 y -= child_size + box->spacing;
595 child_allocation.y -= child_size;
598 gtk_widget_size_allocate (child->widget, &child_allocation);
609 gtk_box_child_type (GtkContainer *container)
611 return GTK_TYPE_WIDGET;
615 gtk_box_set_child_property (GtkContainer *container,
624 GtkPackType pack_type = 0;
626 if (property_id != CHILD_PROP_POSITION)
627 gtk_box_query_child_packing (GTK_BOX (container),
635 case CHILD_PROP_EXPAND:
636 gtk_box_set_child_packing (GTK_BOX (container),
638 g_value_get_boolean (value),
643 case CHILD_PROP_FILL:
644 gtk_box_set_child_packing (GTK_BOX (container),
647 g_value_get_boolean (value),
651 case CHILD_PROP_PADDING:
652 gtk_box_set_child_packing (GTK_BOX (container),
656 g_value_get_uint (value),
659 case CHILD_PROP_PACK_TYPE:
660 gtk_box_set_child_packing (GTK_BOX (container),
665 g_value_get_enum (value));
667 case CHILD_PROP_POSITION:
668 gtk_box_reorder_child (GTK_BOX (container),
670 g_value_get_int (value));
673 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
679 gtk_box_get_child_property (GtkContainer *container,
688 GtkPackType pack_type = 0;
691 if (property_id != CHILD_PROP_POSITION)
692 gtk_box_query_child_packing (GTK_BOX (container),
701 case CHILD_PROP_EXPAND:
702 g_value_set_boolean (value, expand);
704 case CHILD_PROP_FILL:
705 g_value_set_boolean (value, fill);
707 case CHILD_PROP_PADDING:
708 g_value_set_uint (value, padding);
710 case CHILD_PROP_PACK_TYPE:
711 g_value_set_enum (value, pack_type);
713 case CHILD_PROP_POSITION:
715 for (list = GTK_BOX (container)->children; list; list = list->next)
717 GtkBoxChild *child_entry;
719 child_entry = list->data;
720 if (child_entry->widget == child)
724 g_value_set_int (value, list ? i : -1);
727 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
733 gtk_box_pack (GtkBox *box,
738 GtkPackType pack_type)
740 GtkBoxChild *child_info;
742 g_return_if_fail (GTK_IS_BOX (box));
743 g_return_if_fail (GTK_IS_WIDGET (child));
744 g_return_if_fail (child->parent == NULL);
746 child_info = g_new (GtkBoxChild, 1);
747 child_info->widget = child;
748 child_info->padding = padding;
749 child_info->expand = expand ? TRUE : FALSE;
750 child_info->fill = fill ? TRUE : FALSE;
751 child_info->pack = pack_type;
752 child_info->is_secondary = FALSE;
754 box->children = g_list_append (box->children, child_info);
756 gtk_widget_freeze_child_notify (child);
758 gtk_widget_set_parent (child, GTK_WIDGET (box));
760 gtk_widget_child_notify (child, "expand");
761 gtk_widget_child_notify (child, "fill");
762 gtk_widget_child_notify (child, "padding");
763 gtk_widget_child_notify (child, "pack-type");
764 gtk_widget_child_notify (child, "position");
765 gtk_widget_thaw_child_notify (child);
770 gtk_box_extended_layout_init (GtkExtendedLayoutIface *iface)
772 parent_extended_layout_iface = g_type_interface_peek_parent (iface);
774 iface->is_height_for_width = gtk_box_is_height_for_width;
775 iface->get_desired_width = gtk_box_get_desired_width;
776 iface->get_desired_height = gtk_box_get_desired_height;
777 iface->get_height_for_width = gtk_box_get_height_for_width;
778 iface->get_width_for_height = gtk_box_get_width_for_height;
782 gtk_box_is_height_for_width (GtkExtendedLayout *layout)
784 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
786 return (private->orientation == GTK_ORIENTATION_VERTICAL);
790 gtk_box_get_desired_size (GtkExtendedLayout *layout,
791 GtkOrientation orientation,
796 GtkBoxPrivate *private;
800 gint minimum, natural;
802 box = GTK_BOX (layout);
803 private = GTK_BOX_GET_PRIVATE (box);
804 border_width = GTK_CONTAINER (box)->border_width;
806 minimum = natural = 0;
810 for (children = box->children; children; children = children->next)
812 GtkBoxChild *child = children->data;
814 if (gtk_widget_get_visible (child->widget))
816 gint child_minimum, child_natural;
818 if (orientation == GTK_ORIENTATION_HORIZONTAL)
819 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child->widget),
820 &child_minimum, &child_natural);
822 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child->widget),
823 &child_minimum, &child_natural);
825 if (private->orientation == orientation)
827 if (box->homogeneous)
831 largest = child_minimum + child->padding * 2;
832 minimum = MAX (minimum, largest);
834 largest = child_natural + child->padding * 2;
835 natural = MAX (natural, largest);
839 minimum += child_minimum + child->padding * 2;
840 natural += child_natural + child->padding * 2;
845 /* The biggest mins and naturals in the opposing orientation */
846 minimum = MAX (minimum, child_minimum);
847 natural = MAX (natural, child_natural);
854 if (nvis_children > 0 && private->orientation == orientation)
856 if (box->homogeneous)
858 minimum *= nvis_children;
859 natural *= nvis_children;
861 minimum += (nvis_children - 1) * box->spacing;
862 natural += (nvis_children - 1) * box->spacing;
865 minimum += border_width * 2;
866 natural += border_width * 2;
869 *minimum_size = minimum;
872 *natural_size = natural;
876 gtk_box_get_desired_width (GtkExtendedLayout *layout,
880 gtk_box_get_desired_size (layout, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
884 gtk_box_get_desired_height (GtkExtendedLayout *layout,
888 gtk_box_get_desired_size (layout, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
892 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
897 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
901 gint nexpand_children;
902 gint computed_minimum = 0, computed_natural = 0;
903 gint border_width = GTK_CONTAINER (box)->border_width;
905 count_expand_children (box, &nvis_children, &nexpand_children);
907 if (nvis_children > 0)
909 GtkBoxSpreading *spreading = g_newa (GtkBoxSpreading, nvis_children);
910 GtkBoxDesiredSizes *sizes = g_newa (GtkBoxDesiredSizes, nvis_children);
914 gint child_size, child_minimum, child_natural;
916 size = avail_size - border_width * 2 - (nvis_children - 1) * box->spacing;
918 /* Retrieve desired size for visible children */
919 for (i = 0, children = box->children; children; children = children->next)
921 child = children->data;
923 if (gtk_widget_get_visible (child->widget))
925 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
926 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child->widget),
927 &sizes[i].minimum_size,
928 &sizes[i].natural_size);
930 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child->widget),
931 &sizes[i].minimum_size,
932 &sizes[i].natural_size);
934 /* Assert the api is working properly */
935 if (sizes[i].minimum_size < 0)
936 g_error ("GtkBox child %s minimum %s: %d < 0",
937 gtk_widget_get_name (GTK_WIDGET (child->widget)),
938 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
939 sizes[i].minimum_size);
941 if (sizes[i].natural_size < sizes[i].minimum_size)
942 g_error ("GtkBox child %s natural %s: %d < minimum %d",
943 gtk_widget_get_name (GTK_WIDGET (child->widget)),
944 (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
945 sizes[i].natural_size,
946 sizes[i].minimum_size);
948 size -= sizes[i].minimum_size;
949 size -= child->padding * 2;
951 spreading[i].index = i;
952 spreading[i].child = child;
958 if (box->homogeneous)
960 /* If were homogenous we still need to run the above loop to get the minimum sizes
961 * for children that are not going to fill
963 size = avail_size - border_width * 2 - (nvis_children - 1) * box->spacing;
964 extra = size / nvis_children;
969 /* Distribute the container's extra space c_gap. We want to assign
970 * this space such that the sum of extra space assigned to children
971 * (c^i_gap) is equal to c_cap. The case that there's not enough
972 * space for all children to take their natural size needs some
973 * attention. The goals we want to achieve are:
975 * a) Maximize number of children taking their natural size.
976 * b) The allocated size of children should be a continuous
977 * function of c_gap. That is, increasing the container size by
978 * one pixel should never make drastic changes in the distribution.
979 * c) If child i takes its natural size and child j doesn't,
980 * child j should have received at least as much gap as child i.
982 * The following code distributes the additional space by following
986 /* Sort descending by gap and position. */
988 g_qsort_with_data (spreading,
989 nvis_children, sizeof (GtkBoxSpreading),
990 gtk_box_compare_gap, sizes);
992 /* Distribute available space.
993 * This master piece of a loop was conceived by Behdad Esfahbod.
995 for (i = nvis_children - 1; i >= 0; --i)
997 /* Divide remaining space by number of remaining children.
998 * Sort order and reducing remaining space by assigned space
999 * ensures that space is distributed equally.
1001 gint glue = (size + i) / (i + 1);
1002 gint gap = sizes[spreading[i].index].natural_size
1003 - sizes[spreading[i].index].minimum_size;
1005 extra = MIN (glue, gap);
1006 sizes[spreading[i].index].minimum_size += extra;
1011 /* Calculate space which hasn't distributed yet,
1012 * and is available for expanding children.
1014 if (nexpand_children > 0)
1015 extra = size / nexpand_children;
1020 /* Allocate child positions. */
1021 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1023 for (i = 0, children = box->children; children; children = children->next)
1025 child = children->data;
1027 if (gtk_widget_get_visible (child->widget))
1029 if (child->pack == packing)
1031 /* Assign the child's size. */
1032 if (box->homogeneous)
1034 if (nvis_children == 1)
1044 child_size = sizes[i].minimum_size + child->padding * 2;
1048 if (nexpand_children == 1)
1051 child_size += extra;
1053 nexpand_children -= 1;
1060 child_size = MAX (1, child_size - child->padding * 2);
1064 child_size = sizes[i].minimum_size;
1068 /* Assign the child's position. */
1069 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1070 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
1071 child_size, &child_minimum, &child_natural);
1072 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1073 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
1074 child_size, &child_minimum, &child_natural);
1077 computed_minimum = MAX (computed_minimum, child_minimum);
1078 computed_natural = MAX (computed_natural, child_natural);
1086 computed_minimum += border_width * 2;
1087 computed_natural += border_width * 2;
1090 *minimum_size = computed_minimum;
1092 *natural_size = computed_natural;
1096 gtk_box_compute_size_for_orientation (GtkBox *box,
1101 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
1103 gint nvis_children = 0;
1104 gint required_size = 0, required_natural = 0, child_size, child_natural;
1105 gint largest_child = 0, largest_natural = 0;
1107 avail_size -= GTK_CONTAINER (box)->border_width * 2;
1109 for (children = box->children; children != NULL;
1110 children = children->next, nvis_children++)
1112 GtkBoxChild *child = children->data;
1114 if (gtk_widget_get_visible (child->widget))
1117 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1118 gtk_extended_layout_get_width_for_height (GTK_EXTENDED_LAYOUT (child->widget),
1119 avail_size, &child_size, &child_natural);
1121 gtk_extended_layout_get_height_for_width (GTK_EXTENDED_LAYOUT (child->widget),
1122 avail_size, &child_size, &child_natural);
1125 child_size += child->padding * 2;
1126 child_natural += child->padding * 2;
1128 if (child_size > largest_child)
1129 largest_child = child_size;
1131 if (child_natural > largest_natural)
1132 largest_natural = child_natural;
1134 required_size += child_size;
1135 required_natural += child_natural;
1139 if (nvis_children > 0)
1141 if (box->homogeneous)
1143 required_size = largest_child * nvis_children;
1144 required_natural = largest_natural * nvis_children;
1147 required_size += (nvis_children - 1) * box->spacing;
1148 required_natural += (nvis_children - 1) * box->spacing;
1151 required_size += GTK_CONTAINER (box)->border_width * 2;
1152 required_natural += GTK_CONTAINER (box)->border_width * 2;
1155 *minimum_size = required_size;
1158 *natural_size = required_natural;
1162 gtk_box_get_width_for_height (GtkExtendedLayout *layout,
1164 gint *minimum_width,
1165 gint *natural_width)
1167 GtkBox *box = GTK_BOX (layout);
1168 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1170 if (private->orientation == GTK_ORIENTATION_VERTICAL)
1171 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1173 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1177 gtk_box_get_height_for_width (GtkExtendedLayout *layout,
1179 gint *minimum_height,
1180 gint *natural_height)
1182 GtkBox *box = GTK_BOX (layout);
1183 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1185 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1186 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1188 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1193 * @orientation: the box' orientation.
1194 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1195 * @spacing: the number of pixels to place by default between children.
1197 * Creates a new #GtkHBox.
1199 * Return value: a new #GtkHBox.
1204 _gtk_box_new (GtkOrientation orientation,
1205 gboolean homogeneous,
1208 return g_object_new (GTK_TYPE_BOX,
1209 "orientation", orientation,
1211 "homogeneous", homogeneous ? TRUE : FALSE,
1216 * gtk_box_pack_start:
1218 * @child: the #GtkWidget to be added to @box
1219 * @expand: %TRUE if the new child is to be given extra space allocated to
1220 * @box. The extra space will be divided evenly between all children of
1221 * @box that use this option
1222 * @fill: %TRUE if space given to @child by the @expand option is
1223 * actually allocated to @child, rather than just padding it. This
1224 * parameter has no effect if @expand is set to %FALSE. A child is
1225 * always allocated the full height of a #GtkHBox and the full width
1226 * of a #GtkVBox. This option affects the other dimension
1227 * @padding: extra space in pixels to put between this child and its
1228 * neighbors, over and above the global amount specified by
1229 * #GtkBox:spacing property. If @child is a widget at one of the
1230 * reference ends of @box, then @padding pixels are also put between
1231 * @child and the reference edge of @box
1233 * Adds @child to @box, packed with reference to the start of @box.
1234 * The @child is packed after any other child packed with reference
1235 * to the start of @box.
1238 gtk_box_pack_start (GtkBox *box,
1244 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1250 * @child: the #GtkWidget to be added to @box
1251 * @expand: %TRUE if the new child is to be given extra space allocated
1252 * to @box. The extra space will be divided evenly between all children
1253 * of @box that use this option
1254 * @fill: %TRUE if space given to @child by the @expand option is
1255 * actually allocated to @child, rather than just padding it. This
1256 * parameter has no effect if @expand is set to %FALSE. A child is
1257 * always allocated the full height of a #GtkHBox and the full width
1258 * of a #GtkVBox. This option affects the other dimension
1259 * @padding: extra space in pixels to put between this child and its
1260 * neighbors, over and above the global amount specified by
1261 * #GtkBox:spacing property. If @child is a widget at one of the
1262 * reference ends of @box, then @padding pixels are also put between
1263 * @child and the reference edge of @box
1265 * Adds @child to @box, packed with reference to the end of @box.
1266 * The @child is packed after (away from end of) any other child
1267 * packed with reference to the end of @box.
1270 gtk_box_pack_end (GtkBox *box,
1276 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1280 * gtk_box_pack_start_defaults:
1282 * @widget: the #GtkWidget to be added to @box
1284 * Adds @widget to @box, packed with reference to the start of @box.
1285 * The child is packed after any other child packed with reference
1286 * to the start of @box.
1288 * Parameters for how to pack the child @widget, #GtkBox:expand,
1289 * #GtkBox:fill and #GtkBox:padding, are given their default
1290 * values, %TRUE, %TRUE, and 0, respectively.
1292 * Deprecated: 2.14: Use gtk_box_pack_start()
1295 gtk_box_pack_start_defaults (GtkBox *box,
1298 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
1302 * gtk_box_pack_end_defaults:
1304 * @widget: the #GtkWidget to be added to @box
1306 * Adds @widget to @box, packed with reference to the end of @box.
1307 * The child is packed after any other child packed with reference
1308 * to the start of @box.
1310 * Parameters for how to pack the child @widget, #GtkBox:expand,
1311 * #GtkBox:fill and #GtkBox:padding, are given their default
1312 * values, %TRUE, %TRUE, and 0, respectively.
1314 * Deprecated: 2.14: Use gtk_box_pack_end()
1317 gtk_box_pack_end_defaults (GtkBox *box,
1320 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
1324 * gtk_box_set_homogeneous:
1326 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1327 * %FALSE for variable allotments
1329 * Sets the #GtkBox:homogeneous property of @box, controlling
1330 * whether or not all children of @box are given equal space
1334 gtk_box_set_homogeneous (GtkBox *box,
1335 gboolean homogeneous)
1337 g_return_if_fail (GTK_IS_BOX (box));
1339 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
1341 box->homogeneous = homogeneous ? TRUE : FALSE;
1342 g_object_notify (G_OBJECT (box), "homogeneous");
1343 gtk_widget_queue_resize (GTK_WIDGET (box));
1348 * gtk_box_get_homogeneous:
1351 * Returns whether the box is homogeneous (all children are the
1352 * same size). See gtk_box_set_homogeneous().
1354 * Return value: %TRUE if the box is homogeneous.
1357 gtk_box_get_homogeneous (GtkBox *box)
1359 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1361 return box->homogeneous;
1365 * gtk_box_set_spacing:
1367 * @spacing: the number of pixels to put between children
1369 * Sets the #GtkBox:spacing property of @box, which is the
1370 * number of pixels to place between children of @box.
1373 gtk_box_set_spacing (GtkBox *box,
1376 g_return_if_fail (GTK_IS_BOX (box));
1378 if (spacing != box->spacing)
1380 box->spacing = spacing;
1381 _gtk_box_set_spacing_set (box, TRUE);
1383 g_object_notify (G_OBJECT (box), "spacing");
1385 gtk_widget_queue_resize (GTK_WIDGET (box));
1390 * gtk_box_get_spacing:
1393 * Gets the value set by gtk_box_set_spacing().
1395 * Return value: spacing between children
1398 gtk_box_get_spacing (GtkBox *box)
1400 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1402 return box->spacing;
1406 _gtk_box_set_spacing_set (GtkBox *box,
1407 gboolean spacing_set)
1409 GtkBoxPrivate *private;
1411 g_return_if_fail (GTK_IS_BOX (box));
1413 private = GTK_BOX_GET_PRIVATE (box);
1415 private->spacing_set = spacing_set ? TRUE : FALSE;
1419 _gtk_box_get_spacing_set (GtkBox *box)
1421 GtkBoxPrivate *private;
1423 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1425 private = GTK_BOX_GET_PRIVATE (box);
1427 return private->spacing_set;
1431 * gtk_box_reorder_child:
1433 * @child: the #GtkWidget to move
1434 * @position: the new position for @child in the list of children
1435 * of @box, starting from 0. If negative, indicates the end of
1438 * Moves @child to a new @position in the list of @box children.
1439 * The list is the <structfield>children</structfield> field of
1440 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1441 * as well as widgets packed #GTK_PACK_END, in the order that these
1442 * widgets were added to @box.
1444 * A widget's position in the @box children list determines where
1445 * the widget is packed into @box. A child widget at some position
1446 * in the list will be packed just after all other widgets of the
1447 * same packing type that appear earlier in the list.
1450 gtk_box_reorder_child (GtkBox *box,
1456 GtkBoxChild *child_info = NULL;
1459 g_return_if_fail (GTK_IS_BOX (box));
1460 g_return_if_fail (GTK_IS_WIDGET (child));
1462 old_link = box->children;
1466 child_info = old_link->data;
1467 if (child_info->widget == child)
1470 old_link = old_link->next;
1474 g_return_if_fail (old_link != NULL);
1476 if (position == old_position)
1479 box->children = g_list_delete_link (box->children, old_link);
1484 new_link = g_list_nth (box->children, position);
1486 box->children = g_list_insert_before (box->children, new_link, child_info);
1488 gtk_widget_child_notify (child, "position");
1489 if (gtk_widget_get_visible (child)
1490 && gtk_widget_get_visible (GTK_WIDGET (box)))
1491 gtk_widget_queue_resize (child);
1495 * gtk_box_query_child_packing:
1497 * @child: the #GtkWidget of the child to query
1498 * @expand: pointer to return location for #GtkBox:expand child property
1499 * @fill: pointer to return location for #GtkBox:fill child property
1500 * @padding: pointer to return location for #GtkBox:padding child property
1501 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1503 * Obtains information about how @child is packed into @box.
1506 gtk_box_query_child_packing (GtkBox *box,
1511 GtkPackType *pack_type)
1514 GtkBoxChild *child_info = NULL;
1516 g_return_if_fail (GTK_IS_BOX (box));
1517 g_return_if_fail (GTK_IS_WIDGET (child));
1519 list = box->children;
1522 child_info = list->data;
1523 if (child_info->widget == child)
1532 *expand = child_info->expand;
1534 *fill = child_info->fill;
1536 *padding = child_info->padding;
1538 *pack_type = child_info->pack;
1543 * gtk_box_set_child_packing:
1545 * @child: the #GtkWidget of the child to set
1546 * @expand: the new value of the #GtkBox:expand child property
1547 * @fill: the new value of the #GtkBox:fill child property
1548 * @padding: the new value of the #GtkBox:padding child property
1549 * @pack_type: the new value of the #GtkBox:pack-type child property
1551 * Sets the way @child is packed into @box.
1554 gtk_box_set_child_packing (GtkBox *box,
1559 GtkPackType pack_type)
1562 GtkBoxChild *child_info = NULL;
1564 g_return_if_fail (GTK_IS_BOX (box));
1565 g_return_if_fail (GTK_IS_WIDGET (child));
1567 list = box->children;
1570 child_info = list->data;
1571 if (child_info->widget == child)
1577 gtk_widget_freeze_child_notify (child);
1580 child_info->expand = expand != FALSE;
1581 gtk_widget_child_notify (child, "expand");
1582 child_info->fill = fill != FALSE;
1583 gtk_widget_child_notify (child, "fill");
1584 child_info->padding = padding;
1585 gtk_widget_child_notify (child, "padding");
1586 if (pack_type == GTK_PACK_END)
1587 child_info->pack = GTK_PACK_END;
1589 child_info->pack = GTK_PACK_START;
1590 gtk_widget_child_notify (child, "pack-type");
1592 if (gtk_widget_get_visible (child)
1593 && gtk_widget_get_visible (GTK_WIDGET (box)))
1594 gtk_widget_queue_resize (child);
1596 gtk_widget_thaw_child_notify (child);
1600 _gtk_box_set_old_defaults (GtkBox *box)
1602 GtkBoxPrivate *private;
1604 g_return_if_fail (GTK_IS_BOX (box));
1606 private = GTK_BOX_GET_PRIVATE (box);
1608 private->default_expand = TRUE;
1612 gtk_box_add (GtkContainer *container,
1615 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (container);
1617 gtk_box_pack_start (GTK_BOX (container), widget,
1618 private->default_expand,
1619 private->default_expand,
1624 gtk_box_remove (GtkContainer *container,
1627 GtkBox *box = GTK_BOX (container);
1631 children = box->children;
1634 child = children->data;
1636 if (child->widget == widget)
1638 gboolean was_visible;
1640 was_visible = gtk_widget_get_visible (widget);
1641 gtk_widget_unparent (widget);
1643 box->children = g_list_remove_link (box->children, children);
1644 g_list_free (children);
1647 /* queue resize regardless of gtk_widget_get_visible (container),
1648 * since that's what is needed by toplevels.
1651 gtk_widget_queue_resize (GTK_WIDGET (container));
1656 children = children->next;
1661 gtk_box_forall (GtkContainer *container,
1662 gboolean include_internals,
1663 GtkCallback callback,
1664 gpointer callback_data)
1666 GtkBox *box = GTK_BOX (container);
1670 children = box->children;
1673 child = children->data;
1674 children = children->next;
1676 if (child->pack == GTK_PACK_START)
1677 (* callback) (child->widget, callback_data);
1680 children = g_list_last (box->children);
1683 child = children->data;
1684 children = children->prev;
1686 if (child->pack == GTK_PACK_END)
1687 (* callback) (child->widget, callback_data);
1691 #define __GTK_BOX_C__
1692 #include "gtkaliasdef.c"