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 (direction == GTK_TEXT_DIR_RTL)
560 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
562 if (packing == GTK_PACK_START)
564 x += child_size + box->spacing;
568 x -= child_size + box->spacing;
570 child_allocation.x -= child_allocation.width;
573 else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
577 child_allocation.height = MAX (1, child_size - child->padding * 2);
578 child_allocation.y = y + child->padding;
582 child_allocation.height = sizes[i].minimum_size;
583 child_allocation.y = y + (child_size - child_allocation.height) / 2;
586 if (packing == GTK_PACK_START)
588 y += child_size + box->spacing;
592 y -= child_size + box->spacing;
594 child_allocation.y -= child_allocation.height;
597 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)
1172 #if __I_HAD_A_MILLION_DOLLARS__
1173 gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1175 /* Return the defaults instead of calculating in the opposing orientation */
1176 gtk_extended_layout_get_desired_width (layout, minimum_width, natural_width);
1180 gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1184 gtk_box_get_height_for_width (GtkExtendedLayout *layout,
1186 gint *minimum_height,
1187 gint *natural_height)
1189 GtkBox *box = GTK_BOX (layout);
1190 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (layout);
1192 if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1193 gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1195 gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1200 * @orientation: the box' orientation.
1201 * @homogeneous: %TRUE if all children are to be given equal space allocations.
1202 * @spacing: the number of pixels to place by default between children.
1204 * Creates a new #GtkHBox.
1206 * Return value: a new #GtkHBox.
1211 _gtk_box_new (GtkOrientation orientation,
1212 gboolean homogeneous,
1215 return g_object_new (GTK_TYPE_BOX,
1216 "orientation", orientation,
1218 "homogeneous", homogeneous ? TRUE : FALSE,
1223 * gtk_box_pack_start:
1225 * @child: the #GtkWidget to be added to @box
1226 * @expand: %TRUE if the new child is to be given extra space allocated to
1227 * @box. The extra space will be divided evenly between all children of
1228 * @box that use this option
1229 * @fill: %TRUE if space given to @child by the @expand option is
1230 * actually allocated to @child, rather than just padding it. This
1231 * parameter has no effect if @expand is set to %FALSE. A child is
1232 * always allocated the full height of a #GtkHBox and the full width
1233 * of a #GtkVBox. This option affects the other dimension
1234 * @padding: extra space in pixels to put between this child and its
1235 * neighbors, over and above the global amount specified by
1236 * #GtkBox:spacing property. If @child is a widget at one of the
1237 * reference ends of @box, then @padding pixels are also put between
1238 * @child and the reference edge of @box
1240 * Adds @child to @box, packed with reference to the start of @box.
1241 * The @child is packed after any other child packed with reference
1242 * to the start of @box.
1245 gtk_box_pack_start (GtkBox *box,
1251 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1257 * @child: the #GtkWidget to be added to @box
1258 * @expand: %TRUE if the new child is to be given extra space allocated
1259 * to @box. The extra space will be divided evenly between all children
1260 * of @box that use this option
1261 * @fill: %TRUE if space given to @child by the @expand option is
1262 * actually allocated to @child, rather than just padding it. This
1263 * parameter has no effect if @expand is set to %FALSE. A child is
1264 * always allocated the full height of a #GtkHBox and the full width
1265 * of a #GtkVBox. This option affects the other dimension
1266 * @padding: extra space in pixels to put between this child and its
1267 * neighbors, over and above the global amount specified by
1268 * #GtkBox:spacing property. If @child is a widget at one of the
1269 * reference ends of @box, then @padding pixels are also put between
1270 * @child and the reference edge of @box
1272 * Adds @child to @box, packed with reference to the end of @box.
1273 * The @child is packed after (away from end of) any other child
1274 * packed with reference to the end of @box.
1277 gtk_box_pack_end (GtkBox *box,
1283 gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1287 * gtk_box_pack_start_defaults:
1289 * @widget: the #GtkWidget to be added to @box
1291 * Adds @widget to @box, packed with reference to the start of @box.
1292 * The child is packed after any other child packed with reference
1293 * to the start of @box.
1295 * Parameters for how to pack the child @widget, #GtkBox:expand,
1296 * #GtkBox:fill and #GtkBox:padding, are given their default
1297 * values, %TRUE, %TRUE, and 0, respectively.
1299 * Deprecated: 2.14: Use gtk_box_pack_start()
1302 gtk_box_pack_start_defaults (GtkBox *box,
1305 gtk_box_pack_start (box, child, TRUE, TRUE, 0);
1309 * gtk_box_pack_end_defaults:
1311 * @widget: the #GtkWidget to be added to @box
1313 * Adds @widget to @box, packed with reference to the end of @box.
1314 * The child is packed after any other child packed with reference
1315 * to the start of @box.
1317 * Parameters for how to pack the child @widget, #GtkBox:expand,
1318 * #GtkBox:fill and #GtkBox:padding, are given their default
1319 * values, %TRUE, %TRUE, and 0, respectively.
1321 * Deprecated: 2.14: Use gtk_box_pack_end()
1324 gtk_box_pack_end_defaults (GtkBox *box,
1327 gtk_box_pack_end (box, child, TRUE, TRUE, 0);
1331 * gtk_box_set_homogeneous:
1333 * @homogeneous: a boolean value, %TRUE to create equal allotments,
1334 * %FALSE for variable allotments
1336 * Sets the #GtkBox:homogeneous property of @box, controlling
1337 * whether or not all children of @box are given equal space
1341 gtk_box_set_homogeneous (GtkBox *box,
1342 gboolean homogeneous)
1344 g_return_if_fail (GTK_IS_BOX (box));
1346 if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
1348 box->homogeneous = homogeneous ? TRUE : FALSE;
1349 g_object_notify (G_OBJECT (box), "homogeneous");
1350 gtk_widget_queue_resize (GTK_WIDGET (box));
1355 * gtk_box_get_homogeneous:
1358 * Returns whether the box is homogeneous (all children are the
1359 * same size). See gtk_box_set_homogeneous().
1361 * Return value: %TRUE if the box is homogeneous.
1364 gtk_box_get_homogeneous (GtkBox *box)
1366 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1368 return box->homogeneous;
1372 * gtk_box_set_spacing:
1374 * @spacing: the number of pixels to put between children
1376 * Sets the #GtkBox:spacing property of @box, which is the
1377 * number of pixels to place between children of @box.
1380 gtk_box_set_spacing (GtkBox *box,
1383 g_return_if_fail (GTK_IS_BOX (box));
1385 if (spacing != box->spacing)
1387 box->spacing = spacing;
1388 _gtk_box_set_spacing_set (box, TRUE);
1390 g_object_notify (G_OBJECT (box), "spacing");
1392 gtk_widget_queue_resize (GTK_WIDGET (box));
1397 * gtk_box_get_spacing:
1400 * Gets the value set by gtk_box_set_spacing().
1402 * Return value: spacing between children
1405 gtk_box_get_spacing (GtkBox *box)
1407 g_return_val_if_fail (GTK_IS_BOX (box), 0);
1409 return box->spacing;
1413 _gtk_box_set_spacing_set (GtkBox *box,
1414 gboolean spacing_set)
1416 GtkBoxPrivate *private;
1418 g_return_if_fail (GTK_IS_BOX (box));
1420 private = GTK_BOX_GET_PRIVATE (box);
1422 private->spacing_set = spacing_set ? TRUE : FALSE;
1426 _gtk_box_get_spacing_set (GtkBox *box)
1428 GtkBoxPrivate *private;
1430 g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1432 private = GTK_BOX_GET_PRIVATE (box);
1434 return private->spacing_set;
1438 * gtk_box_reorder_child:
1440 * @child: the #GtkWidget to move
1441 * @position: the new position for @child in the list of children
1442 * of @box, starting from 0. If negative, indicates the end of
1445 * Moves @child to a new @position in the list of @box children.
1446 * The list is the <structfield>children</structfield> field of
1447 * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1448 * as well as widgets packed #GTK_PACK_END, in the order that these
1449 * widgets were added to @box.
1451 * A widget's position in the @box children list determines where
1452 * the widget is packed into @box. A child widget at some position
1453 * in the list will be packed just after all other widgets of the
1454 * same packing type that appear earlier in the list.
1457 gtk_box_reorder_child (GtkBox *box,
1463 GtkBoxChild *child_info = NULL;
1466 g_return_if_fail (GTK_IS_BOX (box));
1467 g_return_if_fail (GTK_IS_WIDGET (child));
1469 old_link = box->children;
1473 child_info = old_link->data;
1474 if (child_info->widget == child)
1477 old_link = old_link->next;
1481 g_return_if_fail (old_link != NULL);
1483 if (position == old_position)
1486 box->children = g_list_delete_link (box->children, old_link);
1491 new_link = g_list_nth (box->children, position);
1493 box->children = g_list_insert_before (box->children, new_link, child_info);
1495 gtk_widget_child_notify (child, "position");
1496 if (gtk_widget_get_visible (child)
1497 && gtk_widget_get_visible (GTK_WIDGET (box)))
1498 gtk_widget_queue_resize (child);
1502 * gtk_box_query_child_packing:
1504 * @child: the #GtkWidget of the child to query
1505 * @expand: pointer to return location for #GtkBox:expand child property
1506 * @fill: pointer to return location for #GtkBox:fill child property
1507 * @padding: pointer to return location for #GtkBox:padding child property
1508 * @pack_type: pointer to return location for #GtkBox:pack-type child property
1510 * Obtains information about how @child is packed into @box.
1513 gtk_box_query_child_packing (GtkBox *box,
1518 GtkPackType *pack_type)
1521 GtkBoxChild *child_info = NULL;
1523 g_return_if_fail (GTK_IS_BOX (box));
1524 g_return_if_fail (GTK_IS_WIDGET (child));
1526 list = box->children;
1529 child_info = list->data;
1530 if (child_info->widget == child)
1539 *expand = child_info->expand;
1541 *fill = child_info->fill;
1543 *padding = child_info->padding;
1545 *pack_type = child_info->pack;
1550 * gtk_box_set_child_packing:
1552 * @child: the #GtkWidget of the child to set
1553 * @expand: the new value of the #GtkBox:expand child property
1554 * @fill: the new value of the #GtkBox:fill child property
1555 * @padding: the new value of the #GtkBox:padding child property
1556 * @pack_type: the new value of the #GtkBox:pack-type child property
1558 * Sets the way @child is packed into @box.
1561 gtk_box_set_child_packing (GtkBox *box,
1566 GtkPackType pack_type)
1569 GtkBoxChild *child_info = NULL;
1571 g_return_if_fail (GTK_IS_BOX (box));
1572 g_return_if_fail (GTK_IS_WIDGET (child));
1574 list = box->children;
1577 child_info = list->data;
1578 if (child_info->widget == child)
1584 gtk_widget_freeze_child_notify (child);
1587 child_info->expand = expand != FALSE;
1588 gtk_widget_child_notify (child, "expand");
1589 child_info->fill = fill != FALSE;
1590 gtk_widget_child_notify (child, "fill");
1591 child_info->padding = padding;
1592 gtk_widget_child_notify (child, "padding");
1593 if (pack_type == GTK_PACK_END)
1594 child_info->pack = GTK_PACK_END;
1596 child_info->pack = GTK_PACK_START;
1597 gtk_widget_child_notify (child, "pack-type");
1599 if (gtk_widget_get_visible (child)
1600 && gtk_widget_get_visible (GTK_WIDGET (box)))
1601 gtk_widget_queue_resize (child);
1603 gtk_widget_thaw_child_notify (child);
1607 _gtk_box_set_old_defaults (GtkBox *box)
1609 GtkBoxPrivate *private;
1611 g_return_if_fail (GTK_IS_BOX (box));
1613 private = GTK_BOX_GET_PRIVATE (box);
1615 private->default_expand = TRUE;
1619 gtk_box_add (GtkContainer *container,
1622 GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (container);
1624 gtk_box_pack_start (GTK_BOX (container), widget,
1625 private->default_expand,
1626 private->default_expand,
1631 gtk_box_remove (GtkContainer *container,
1634 GtkBox *box = GTK_BOX (container);
1638 children = box->children;
1641 child = children->data;
1643 if (child->widget == widget)
1645 gboolean was_visible;
1647 was_visible = gtk_widget_get_visible (widget);
1648 gtk_widget_unparent (widget);
1650 box->children = g_list_remove_link (box->children, children);
1651 g_list_free (children);
1654 /* queue resize regardless of gtk_widget_get_visible (container),
1655 * since that's what is needed by toplevels.
1658 gtk_widget_queue_resize (GTK_WIDGET (container));
1663 children = children->next;
1668 gtk_box_forall (GtkContainer *container,
1669 gboolean include_internals,
1670 GtkCallback callback,
1671 gpointer callback_data)
1673 GtkBox *box = GTK_BOX (container);
1677 children = box->children;
1680 child = children->data;
1681 children = children->next;
1683 if (child->pack == GTK_PACK_START)
1684 (* callback) (child->widget, callback_data);
1687 children = g_list_last (box->children);
1690 child = children->data;
1691 children = children->prev;
1693 if (child->pack == GTK_PACK_END)
1694 (* callback) (child->widget, callback_data);
1698 #define __GTK_BOX_C__
1699 #include "gtkaliasdef.c"