]> Pileus Git - ~andy/gtk/blob - gtk/gtkbox.c
Revert "toolbar: Don't special-case RTL toolbar child positions anymore"
[~andy/gtk] / gtk / gtkbox.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 /**
26  * SECTION:gtkbox
27  * @Short_description: A container box
28  * @Title: GtkBox
29  * @See_also: #GtkFrame, #GtkGrid, #GtkLayout
30  *
31  * The GtkBox widget organizes child widgets into a rectangular area.
32  *
33  * The rectangular area of a GtkBox is organized into either a single row
34  * or a single column of child widgets depending upon the orientation.
35  * Thus, all children of a GtkBox are allocated one dimension in common,
36  * which is the height of a row, or the width of a column.
37  *
38  * GtkBox uses a notion of <emphasis>packing</emphasis>. Packing refers
39  * to adding widgets with reference to a particular position in a
40  * #GtkContainer. For a GtkBox, there are two reference positions: the
41  * <emphasis>start</emphasis> and the <emphasis>end</emphasis> of the box.
42  * For a vertical #GtkBox, the start is defined as the top of the box and
43  * the end is defined as the bottom. For a horizontal #GtkBox the start
44  * is defined as the left side and the end is defined as the right side.
45  *
46  * Use repeated calls to gtk_box_pack_start() to pack widgets into a
47  * GtkBox from start to end. Use gtk_box_pack_end() to add widgets from
48  * end to start. You may intersperse these calls and add widgets from
49  * both ends of the same GtkBox.
50  *
51  * Because GtkBox is a #GtkContainer, you may also use gtk_container_add()
52  * to insert widgets into the box, and they will be packed with the default
53  * values for #GtkBox:expand and #GtkBox:fill. Use gtk_container_remove()
54  * to remove widgets from the GtkBox.
55  *
56  * Use gtk_box_set_homogeneous() to specify whether or not all children
57  * of the GtkBox are forced to get the same amount of space.
58  *
59  * Use gtk_box_set_spacing() to determine how much space will be
60  * minimally placed between all children in the GtkBox. Note that
61  * spacing is added <emphasis>between</emphasis> the children, while
62  * padding added by gtk_box_pack_start() or gtk_box_pack_end() is added
63  * <emphasis>on either side</emphasis> of the widget it belongs to.
64  *
65  * Use gtk_box_reorder_child() to move a GtkBox child to a different
66  * place in the box.
67  *
68  * Use gtk_box_set_child_packing() to reset the #GtkBox:expand,
69  * #GtkBox:fill and #GtkBox:padding child properties.
70  * Use gtk_box_query_child_packing() to query these fields.
71  *
72  * <note><para>
73  * Note that a single-row or single-column #GtkGrid provides exactly
74  * the same functionality as #GtkBox.
75  * </para></note>
76  */
77
78 #include "config.h"
79
80 #include "gtkbox.h"
81 #include "gtkboxprivate.h"
82 #include "gtkintl.h"
83 #include "gtkorientable.h"
84 #include "gtkorientableprivate.h"
85 #include "gtkprivate.h"
86 #include "gtktypebuiltins.h"
87 #include "gtksizerequest.h"
88 #include "gtkwidgetpath.h"
89 #include "gtkwidgetprivate.h"
90 #include "a11y/gtkcontaineraccessible.h"
91
92
93 enum {
94   PROP_0,
95   PROP_ORIENTATION,
96   PROP_SPACING,
97   PROP_HOMOGENEOUS
98 };
99
100 enum {
101   CHILD_PROP_0,
102   CHILD_PROP_EXPAND,
103   CHILD_PROP_FILL,
104   CHILD_PROP_PADDING,
105   CHILD_PROP_PACK_TYPE,
106   CHILD_PROP_POSITION
107 };
108
109 struct _GtkBoxPrivate
110 {
111   GList          *children;
112
113   GtkOrientation  orientation;
114   gint16          spacing;
115
116   guint           default_expand : 1;
117   guint           homogeneous    : 1;
118   guint           spacing_set    : 1;
119 };
120
121 typedef struct _GtkBoxChild        GtkBoxChild;
122
123 /*
124  * GtkBoxChild:
125  * @widget: the child widget, packed into the GtkBox.
126  * @padding: the number of extra pixels to put between this child and its
127  *  neighbors, set when packed, zero by default.
128  * @expand: flag indicates whether extra space should be given to this child.
129  *  Any extra space given to the parent GtkBox is divided up among all children
130  *  with this attribute set to %TRUE; set when packed, %TRUE by default.
131  * @fill: flag indicates whether any extra space given to this child due to its
132  *  @expand attribute being set is actually allocated to the child, rather than
133  *  being used as padding around the widget; set when packed, %TRUE by default.
134  * @pack: one of #GtkPackType indicating whether the child is packed with
135  *  reference to the start (top/left) or end (bottom/right) of the GtkBox.
136  */
137 struct _GtkBoxChild
138 {
139   GtkWidget *widget;
140
141   guint16    padding;
142
143   guint      expand : 1;
144   guint      fill   : 1;
145   guint      pack   : 1;
146 };
147
148 static void gtk_box_size_allocate         (GtkWidget              *widget,
149                                            GtkAllocation          *allocation);
150
151 static void gtk_box_compute_expand     (GtkWidget      *widget,
152                                         gboolean       *hexpand,
153                                         gboolean       *vexpand);
154
155 static void gtk_box_set_property       (GObject        *object,
156                                         guint           prop_id,
157                                         const GValue   *value,
158                                         GParamSpec     *pspec);
159 static void gtk_box_get_property       (GObject        *object,
160                                         guint           prop_id,
161                                         GValue         *value,
162                                         GParamSpec     *pspec);
163 static void gtk_box_add                (GtkContainer   *container,
164                                         GtkWidget      *widget);
165 static void gtk_box_remove             (GtkContainer   *container,
166                                         GtkWidget      *widget);
167 static void gtk_box_forall             (GtkContainer   *container,
168                                         gboolean        include_internals,
169                                         GtkCallback     callback,
170                                         gpointer        callback_data);
171 static void gtk_box_set_child_property (GtkContainer   *container,
172                                         GtkWidget      *child,
173                                         guint           property_id,
174                                         const GValue   *value,
175                                         GParamSpec     *pspec);
176 static void gtk_box_get_child_property (GtkContainer   *container,
177                                         GtkWidget      *child,
178                                         guint           property_id,
179                                         GValue         *value,
180                                         GParamSpec     *pspec);
181 static GType gtk_box_child_type        (GtkContainer   *container);
182 static GtkWidgetPath * gtk_box_get_path_for_child
183                                        (GtkContainer   *container,
184                                         GtkWidget      *child);
185
186
187 static void               gtk_box_get_preferred_width            (GtkWidget           *widget,
188                                                                   gint                *minimum_size,
189                                                                   gint                *natural_size);
190 static void               gtk_box_get_preferred_height           (GtkWidget           *widget,
191                                                                   gint                *minimum_size,
192                                                                   gint                *natural_size);
193 static void               gtk_box_get_preferred_width_for_height (GtkWidget           *widget,
194                                                                   gint                 height,
195                                                                   gint                *minimum_width,
196                                                                   gint                *natural_width);
197 static void               gtk_box_get_preferred_height_for_width (GtkWidget           *widget,
198                                                                   gint                 width,
199                                                                   gint                *minimum_height,
200                                                                   gint                *natural_height);
201
202
203 G_DEFINE_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
204                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
205                                                 NULL))
206
207 static void
208 gtk_box_class_init (GtkBoxClass *class)
209 {
210   GObjectClass *object_class = G_OBJECT_CLASS (class);
211   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
212   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
213
214   object_class->set_property = gtk_box_set_property;
215   object_class->get_property = gtk_box_get_property;
216
217   widget_class->size_allocate                  = gtk_box_size_allocate;
218   widget_class->get_preferred_width            = gtk_box_get_preferred_width;
219   widget_class->get_preferred_height           = gtk_box_get_preferred_height;
220   widget_class->get_preferred_height_for_width = gtk_box_get_preferred_height_for_width;
221   widget_class->get_preferred_width_for_height = gtk_box_get_preferred_width_for_height;
222   widget_class->compute_expand                 = gtk_box_compute_expand;
223
224   container_class->add = gtk_box_add;
225   container_class->remove = gtk_box_remove;
226   container_class->forall = gtk_box_forall;
227   container_class->child_type = gtk_box_child_type;
228   container_class->set_child_property = gtk_box_set_child_property;
229   container_class->get_child_property = gtk_box_get_child_property;
230   container_class->get_path_for_child = gtk_box_get_path_for_child;
231   gtk_container_class_handle_border_width (container_class);
232
233   g_object_class_override_property (object_class,
234                                     PROP_ORIENTATION,
235                                     "orientation");
236
237   g_object_class_install_property (object_class,
238                                    PROP_SPACING,
239                                    g_param_spec_int ("spacing",
240                                                      P_("Spacing"),
241                                                      P_("The amount of space between children"),
242                                                      0,
243                                                      G_MAXINT,
244                                                      0,
245                                                      GTK_PARAM_READWRITE));
246
247   g_object_class_install_property (object_class,
248                                    PROP_HOMOGENEOUS,
249                                    g_param_spec_boolean ("homogeneous",
250                                                          P_("Homogeneous"),
251                                                          P_("Whether the children should all be the same size"),
252                                                          FALSE,
253                                                          GTK_PARAM_READWRITE));
254
255   /**
256    * GtkBox:expand:
257    *
258    * Whether the child should receive extra space when the parent grows.
259    *
260    * Note that the default value for this property is %FALSE for GtkBox,
261    * but #GtkHBox, #GtkVBox and other subclasses use the old default
262    * of %TRUE.
263    *
264    * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
265    * and #GtkWidget:vexpand properties are the preferred way to influence
266    * child size allocation in containers.
267    */
268   gtk_container_class_install_child_property (container_class,
269                                               CHILD_PROP_EXPAND,
270                                               g_param_spec_boolean ("expand",
271                                                                     P_("Expand"),
272                                                                     P_("Whether the child should receive extra space when the parent grows"),
273                                                                     FALSE,
274                                                                     GTK_PARAM_READWRITE));
275
276   /**
277    * GtkBox:fill:
278    *
279    * Whether the child should receive extra space when the parent grows.
280    *
281    * Note that the #GtkWidget:halign, #GtkWidget:valign, #GtkWidget:hexpand
282    * and #GtkWidget:vexpand properties are the preferred way to influence
283    * child size allocation in containers.
284    */
285   gtk_container_class_install_child_property (container_class,
286                                               CHILD_PROP_FILL,
287                                               g_param_spec_boolean ("fill",
288                                                                     P_("Fill"),
289                                                                     P_("Whether extra space given to the child should be allocated to the child or used as padding"),
290                                                                     TRUE,
291                                                                     GTK_PARAM_READWRITE));
292
293   gtk_container_class_install_child_property (container_class,
294                                               CHILD_PROP_PADDING,
295                                               g_param_spec_uint ("padding",
296                                                                  P_("Padding"),
297                                                                  P_("Extra space to put between the child and its neighbors, in pixels"),
298                                                                  0, G_MAXINT, 0,
299                                                                  GTK_PARAM_READWRITE));
300   gtk_container_class_install_child_property (container_class,
301                                               CHILD_PROP_PACK_TYPE,
302                                               g_param_spec_enum ("pack-type",
303                                                                  P_("Pack type"),
304                                                                  P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
305                                                                  GTK_TYPE_PACK_TYPE, GTK_PACK_START,
306                                                                  GTK_PARAM_READWRITE));
307   gtk_container_class_install_child_property (container_class,
308                                               CHILD_PROP_POSITION,
309                                               g_param_spec_int ("position",
310                                                                 P_("Position"),
311                                                                 P_("The index of the child in the parent"),
312                                                                 -1, G_MAXINT, 0,
313                                                                 GTK_PARAM_READWRITE));
314
315   g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
316
317   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILLER);
318 }
319
320 static void
321 gtk_box_init (GtkBox *box)
322 {
323   GtkBoxPrivate *private;
324
325   box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
326                                            GTK_TYPE_BOX,
327                                            GtkBoxPrivate);
328   private = box->priv;
329
330   gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
331   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
332
333   private->orientation = GTK_ORIENTATION_HORIZONTAL;
334   private->children = NULL;
335
336   private->default_expand = FALSE;
337   private->homogeneous = FALSE;
338   private->spacing = 0;
339   private->spacing_set = FALSE;
340 }
341
342 static void
343 gtk_box_set_property (GObject      *object,
344                       guint         prop_id,
345                       const GValue *value,
346                       GParamSpec   *pspec)
347 {
348   GtkBox *box = GTK_BOX (object);
349   GtkBoxPrivate *private = box->priv;
350
351   switch (prop_id)
352     {
353     case PROP_ORIENTATION:
354       private->orientation = g_value_get_enum (value);
355       _gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
356       gtk_widget_queue_resize (GTK_WIDGET (box));
357       break;
358     case PROP_SPACING:
359       gtk_box_set_spacing (box, g_value_get_int (value));
360       break;
361     case PROP_HOMOGENEOUS:
362       gtk_box_set_homogeneous (box, g_value_get_boolean (value));
363       break;
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367     }
368 }
369
370 static void
371 gtk_box_get_property (GObject    *object,
372                       guint       prop_id,
373                       GValue     *value,
374                       GParamSpec *pspec)
375 {
376   GtkBox *box = GTK_BOX (object);
377   GtkBoxPrivate *private = box->priv;
378
379   switch (prop_id)
380     {
381     case PROP_ORIENTATION:
382       g_value_set_enum (value, private->orientation);
383       break;
384     case PROP_SPACING:
385       g_value_set_int (value, private->spacing);
386       break;
387     case PROP_HOMOGENEOUS:
388       g_value_set_boolean (value, private->homogeneous);
389       break;
390     default:
391       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392       break;
393     }
394 }
395
396
397 static void
398 count_expand_children (GtkBox *box,
399                        gint *visible_children,
400                        gint *expand_children)
401 {
402   GtkBoxPrivate  *private = box->priv;
403   GList       *children;
404   GtkBoxChild *child;
405
406   *visible_children = *expand_children = 0;
407
408   for (children = private->children; children; children = children->next)
409     {
410       child = children->data;
411
412       if (gtk_widget_get_visible (child->widget))
413         {
414           *visible_children += 1;
415           if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
416             *expand_children += 1;
417         }
418     }
419 }
420
421 static void
422 gtk_box_size_allocate (GtkWidget     *widget,
423                        GtkAllocation *allocation)
424 {
425   GtkBox *box = GTK_BOX (widget);
426   GtkBoxPrivate *private = box->priv;
427   GtkBoxChild *child;
428   GList *children;
429   gint nvis_children;
430   gint nexpand_children;
431
432   GtkTextDirection direction;
433   GtkAllocation child_allocation;
434   GtkRequestedSize *sizes;
435
436   GtkPackType packing;
437
438   gint size;
439   gint extra;
440   gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
441   gint x = 0, y = 0, i;
442   gint child_size;
443
444
445   gtk_widget_set_allocation (widget, allocation);
446
447   count_expand_children (box, &nvis_children, &nexpand_children);
448
449   /* If there is no visible child, simply return. */
450   if (nvis_children <= 0)
451     return;
452
453   direction = gtk_widget_get_direction (widget);
454   sizes = g_newa (GtkRequestedSize, nvis_children);
455
456   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
457     size = allocation->width - (nvis_children - 1) * private->spacing;
458   else
459     size = allocation->height - (nvis_children - 1) * private->spacing;
460
461   /* Retrieve desired size for visible children. */
462   for (i = 0, children = private->children; children; children = children->next)
463     {
464       child = children->data;
465
466       if (!gtk_widget_get_visible (child->widget))
467         continue;
468
469       if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
470         gtk_widget_get_preferred_width_for_height (child->widget,
471                                                    allocation->height,
472                                                    &sizes[i].minimum_size,
473                                                    &sizes[i].natural_size);
474       else
475         gtk_widget_get_preferred_height_for_width (child->widget,
476                                                    allocation->width,
477                                                    &sizes[i].minimum_size,
478                                                    &sizes[i].natural_size);
479
480
481       /* Assert the api is working properly */
482       if (sizes[i].minimum_size < 0)
483         g_error ("GtkBox child %s minimum %s: %d < 0 for %s %d",
484                  gtk_widget_get_name (GTK_WIDGET (child->widget)),
485                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
486                  sizes[i].minimum_size,
487                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
488                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
489
490       if (sizes[i].natural_size < sizes[i].minimum_size)
491         g_error ("GtkBox child %s natural %s: %d < minimum %d for %s %d",
492                  gtk_widget_get_name (GTK_WIDGET (child->widget)),
493                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
494                  sizes[i].natural_size,
495                  sizes[i].minimum_size,
496                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "height" : "width",
497                  (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? allocation->height : allocation->width);
498
499       size -= sizes[i].minimum_size;
500       size -= child->padding * 2;
501
502       sizes[i].data = child;
503
504       i++;
505     }
506
507   if (private->homogeneous)
508     {
509       /* If were homogenous we still need to run the above loop to get the
510        * minimum sizes for children that are not going to fill
511        */
512       if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
513         size = allocation->width - (nvis_children - 1) * private->spacing;
514       else
515         size = allocation->height - (nvis_children - 1) * private->spacing;
516
517       extra = size / nvis_children;
518       n_extra_widgets = size % nvis_children;
519     }
520   else
521     {
522       /* Bring children up to size first */
523       size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
524
525       /* Calculate space which hasn't distributed yet,
526        * and is available for expanding children.
527        */
528       if (nexpand_children > 0)
529         {
530           extra = size / nexpand_children;
531           n_extra_widgets = size % nexpand_children;
532         }
533       else
534         extra = 0;
535     }
536
537   /* Allocate child positions. */
538   for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
539     {
540       if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
541         {
542           child_allocation.y = allocation->y;
543           child_allocation.height = MAX (1, allocation->height);
544           if (packing == GTK_PACK_START)
545             x = allocation->x;
546           else
547             x = allocation->x + allocation->width;
548         }
549       else
550         {
551           child_allocation.x = allocation->x;
552           child_allocation.width = MAX (1, allocation->width);
553           if (packing == GTK_PACK_START)
554             y = allocation->y;
555           else
556             y = allocation->y + allocation->height;
557         }
558
559       for (i = 0, children = private->children;
560            children;
561            children = children->next)
562         {
563           child = children->data;
564
565           /* If widget is not visible, skip it. */
566           if (!gtk_widget_get_visible (child->widget))
567             continue;
568
569           /* If widget is packed differently skip it, but still increment i,
570            * since widget is visible and will be handled in next loop iteration.
571            */
572           if (child->pack != packing)
573             {
574               i++;
575               continue;
576             }
577
578           /* Assign the child's size. */
579           if (private->homogeneous)
580             {
581               child_size = extra;
582
583               if (n_extra_widgets > 0)
584                 {
585                   child_size++;
586                   n_extra_widgets--;
587                 }
588             }
589           else
590             {
591               child_size = sizes[i].minimum_size + child->padding * 2;
592
593               if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
594                 {
595                   child_size += extra;
596
597                   if (n_extra_widgets > 0)
598                     {
599                       child_size++;
600                       n_extra_widgets--;
601                     }
602                 }
603             }
604
605           /* Assign the child's position. */
606           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
607             {
608               if (child->fill)
609                 {
610                   child_allocation.width = MAX (1, child_size - child->padding * 2);
611                   child_allocation.x = x + child->padding;
612                 }
613               else
614                 {
615                   child_allocation.width = sizes[i].minimum_size;
616                   child_allocation.x = x + (child_size - child_allocation.width) / 2;
617                 }
618
619               if (packing == GTK_PACK_START)
620                 {
621                   x += child_size + private->spacing;
622                 }
623               else
624                 {
625                   x -= child_size + private->spacing;
626
627                   child_allocation.x -= child_size;
628                 }
629
630               if (direction == GTK_TEXT_DIR_RTL)
631                 child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
632
633             }
634           else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
635             {
636               if (child->fill)
637                 {
638                   child_allocation.height = MAX (1, child_size - child->padding * 2);
639                   child_allocation.y = y + child->padding;
640                 }
641               else
642                 {
643                   child_allocation.height = sizes[i].minimum_size;
644                   child_allocation.y = y + (child_size - child_allocation.height) / 2;
645                 }
646
647               if (packing == GTK_PACK_START)
648                 {
649                   y += child_size + private->spacing;
650                 }
651               else
652                 {
653                   y -= child_size + private->spacing;
654
655                   child_allocation.y -= child_size;
656                 }
657             }
658           gtk_widget_size_allocate (child->widget, &child_allocation);
659
660           i++;
661         }
662     }
663 }
664
665 static void
666 gtk_box_compute_expand (GtkWidget      *widget,
667                         gboolean       *hexpand_p,
668                         gboolean       *vexpand_p)
669 {
670   GtkBoxPrivate  *private = GTK_BOX (widget)->priv;
671   GList       *children;
672   GtkBoxChild *child;
673   gboolean our_expand;
674   gboolean opposite_expand;
675   GtkOrientation opposite_orientation;
676
677   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
678     opposite_orientation = GTK_ORIENTATION_VERTICAL;
679   else
680     opposite_orientation = GTK_ORIENTATION_HORIZONTAL;
681
682   our_expand = FALSE;
683   opposite_expand = FALSE;
684
685   for (children = private->children; children; children = children->next)
686     {
687       child = children->data;
688
689       /* we don't recurse into children anymore as soon as we know
690        * expand=TRUE in an orientation
691        */
692
693       if (child->expand || (!our_expand && gtk_widget_compute_expand (child->widget, private->orientation)))
694         our_expand = TRUE;
695
696       if (!opposite_expand && gtk_widget_compute_expand (child->widget, opposite_orientation))
697         opposite_expand = TRUE;
698
699       if (our_expand && opposite_expand)
700         break;
701     }
702
703   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
704     {
705       *hexpand_p = our_expand;
706       *vexpand_p = opposite_expand;
707     }
708   else
709     {
710       *hexpand_p = opposite_expand;
711       *vexpand_p = our_expand;
712     }
713 }
714
715 static GType
716 gtk_box_child_type (GtkContainer   *container)
717 {
718   return GTK_TYPE_WIDGET;
719 }
720
721 static void
722 gtk_box_set_child_property (GtkContainer *container,
723                             GtkWidget    *child,
724                             guint         property_id,
725                             const GValue *value,
726                             GParamSpec   *pspec)
727 {
728   gboolean expand = 0;
729   gboolean fill = 0;
730   guint padding = 0;
731   GtkPackType pack_type = 0;
732
733   if (property_id != CHILD_PROP_POSITION)
734     gtk_box_query_child_packing (GTK_BOX (container),
735                                  child,
736                                  &expand,
737                                  &fill,
738                                  &padding,
739                                  &pack_type);
740   switch (property_id)
741     {
742     case CHILD_PROP_EXPAND:
743       gtk_box_set_child_packing (GTK_BOX (container),
744                                  child,
745                                  g_value_get_boolean (value),
746                                  fill,
747                                  padding,
748                                  pack_type);
749       break;
750     case CHILD_PROP_FILL:
751       gtk_box_set_child_packing (GTK_BOX (container),
752                                  child,
753                                  expand,
754                                  g_value_get_boolean (value),
755                                  padding,
756                                  pack_type);
757       break;
758     case CHILD_PROP_PADDING:
759       gtk_box_set_child_packing (GTK_BOX (container),
760                                  child,
761                                  expand,
762                                  fill,
763                                  g_value_get_uint (value),
764                                  pack_type);
765       break;
766     case CHILD_PROP_PACK_TYPE:
767       gtk_box_set_child_packing (GTK_BOX (container),
768                                  child,
769                                  expand,
770                                  fill,
771                                  padding,
772                                  g_value_get_enum (value));
773       break;
774     case CHILD_PROP_POSITION:
775       gtk_box_reorder_child (GTK_BOX (container),
776                              child,
777                              g_value_get_int (value));
778       break;
779     default:
780       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
781       break;
782     }
783 }
784
785 static void
786 gtk_box_get_child_property (GtkContainer *container,
787                             GtkWidget    *child,
788                             guint         property_id,
789                             GValue       *value,
790                             GParamSpec   *pspec)
791 {
792   gboolean expand = FALSE;
793   gboolean fill = FALSE;
794   guint padding = 0;
795   GtkPackType pack_type = 0;
796   GList *list;
797
798   if (property_id != CHILD_PROP_POSITION)
799     gtk_box_query_child_packing (GTK_BOX (container),
800                                  child,
801                                  &expand,
802                                  &fill,
803                                  &padding,
804                                  &pack_type);
805   switch (property_id)
806     {
807       guint i;
808     case CHILD_PROP_EXPAND:
809       g_value_set_boolean (value, expand);
810       break;
811     case CHILD_PROP_FILL:
812       g_value_set_boolean (value, fill);
813       break;
814     case CHILD_PROP_PADDING:
815       g_value_set_uint (value, padding);
816       break;
817     case CHILD_PROP_PACK_TYPE:
818       g_value_set_enum (value, pack_type);
819       break;
820     case CHILD_PROP_POSITION:
821       i = 0;
822       for (list = GTK_BOX (container)->priv->children; list; list = list->next)
823         {
824           GtkBoxChild *child_entry;
825
826           child_entry = list->data;
827           if (child_entry->widget == child)
828             break;
829           i++;
830         }
831       g_value_set_int (value, list ? i : -1);
832       break;
833     default:
834       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
835       break;
836     }
837 }
838
839 typedef struct _CountingData CountingData;
840 struct _CountingData {
841   GtkWidget *widget;
842   gboolean found;
843   guint before;
844   guint after;
845 };
846
847 static void
848 count_widget_position (GtkWidget *widget,
849                        gpointer   data)
850 {
851   CountingData *count = data;
852
853   if (!gtk_widget_get_visible (widget))
854     return;
855
856   if (count->widget == widget)
857     count->found = TRUE;
858   else if (!count->found)
859     count->before++;
860 }
861
862 static gint
863 gtk_box_get_visible_position (GtkBox    *box,
864                               GtkWidget *child)
865 {
866   CountingData count = { child, FALSE, 0, 0 };
867
868   /* foreach iterates in visible order */
869   gtk_container_foreach (GTK_CONTAINER (box),
870                          count_widget_position,
871                          &count);
872
873   /* the child wasn't found, it's likely an internal child of some
874    * subclass, return -1 to indicate that there is no sibling relation
875    * to the regular box children
876    */
877   if (!count.found)
878     return -1;
879
880   return count.before;
881 }
882
883 static GtkWidgetPath *
884 gtk_box_get_path_for_child (GtkContainer *container,
885                             GtkWidget    *child)
886 {
887   GtkWidgetPath *path, *sibling_path;
888   GtkBox *box;
889   GList *list, *children;
890
891   box = GTK_BOX (container);
892
893   path = _gtk_widget_create_path (GTK_WIDGET (container));
894
895   if (gtk_widget_get_visible (child))
896     {
897       gint position;
898
899       sibling_path = gtk_widget_path_new ();
900
901       /* get_children works in visible order */
902       children = gtk_container_get_children (container);
903
904       for (list = children; list; list = list->next)
905         {
906           if (!gtk_widget_get_visible (list->data))
907             continue;
908
909           gtk_widget_path_append_for_widget (sibling_path, list->data);
910         }
911
912       g_list_free (children);
913
914       position = gtk_box_get_visible_position (box, child);
915
916       if (position >= 0)
917         gtk_widget_path_append_with_siblings (path, sibling_path, position);
918       else
919         gtk_widget_path_append_for_widget (path, child);
920
921       gtk_widget_path_unref (sibling_path);
922     }
923   else
924     gtk_widget_path_append_for_widget (path, child);
925
926   return path;
927 }
928
929 static void
930 gtk_box_invalidate_order_foreach (GtkWidget *widget)
931 {
932   _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
933 }
934
935 static void
936 gtk_box_invalidate_order (GtkBox *box)
937 {
938   gtk_container_foreach (GTK_CONTAINER (box),
939                          (GtkCallback) gtk_box_invalidate_order_foreach,
940                          NULL);
941 }
942
943 static void
944 box_child_visibility_notify_cb (GObject *obj,
945                                 GParamSpec *pspec,
946                                 gpointer user_data)
947 {
948   GtkBox *box = user_data;
949
950   gtk_box_invalidate_order (box);
951 }
952
953 static void
954 gtk_box_pack (GtkBox      *box,
955               GtkWidget   *child,
956               gboolean     expand,
957               gboolean     fill,
958               guint        padding,
959               GtkPackType  pack_type)
960 {
961   GtkBoxPrivate *private = box->priv;
962   GtkBoxChild *child_info;
963
964   g_return_if_fail (GTK_IS_BOX (box));
965   g_return_if_fail (GTK_IS_WIDGET (child));
966   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
967
968   child_info = g_new (GtkBoxChild, 1);
969   child_info->widget = child;
970   child_info->padding = padding;
971   child_info->expand = expand ? TRUE : FALSE;
972   child_info->fill = fill ? TRUE : FALSE;
973   child_info->pack = pack_type;
974
975   private->children = g_list_append (private->children, child_info);
976
977   gtk_widget_freeze_child_notify (child);
978
979   gtk_box_invalidate_order (box);
980   gtk_widget_set_parent (child, GTK_WIDGET (box));
981
982   g_signal_connect (child, "notify::visible",
983                     G_CALLBACK (box_child_visibility_notify_cb), box);
984
985   gtk_widget_child_notify (child, "expand");
986   gtk_widget_child_notify (child, "fill");
987   gtk_widget_child_notify (child, "padding");
988   gtk_widget_child_notify (child, "pack-type");
989   gtk_widget_child_notify (child, "position");
990   gtk_widget_thaw_child_notify (child);
991 }
992
993 static void
994 gtk_box_get_size (GtkWidget      *widget,
995                   GtkOrientation  orientation,
996                   gint           *minimum_size,
997                   gint           *natural_size)
998 {
999   GtkBox *box;
1000   GtkBoxPrivate *private;
1001   GList *children;
1002   gint nvis_children;
1003   gint minimum, natural;
1004
1005   box = GTK_BOX (widget);
1006   private = box->priv;
1007
1008   minimum = natural = 0;
1009
1010   nvis_children = 0;
1011
1012   for (children = private->children; children; children = children->next)
1013     {
1014       GtkBoxChild *child = children->data;
1015
1016       if (gtk_widget_get_visible (child->widget))
1017         {
1018           gint child_minimum, child_natural;
1019
1020           if (orientation == GTK_ORIENTATION_HORIZONTAL)
1021             gtk_widget_get_preferred_width (child->widget,
1022                                             &child_minimum, &child_natural);
1023           else
1024             gtk_widget_get_preferred_height (child->widget,
1025                                              &child_minimum, &child_natural);
1026
1027           if (private->orientation == orientation)
1028             {
1029               if (private->homogeneous)
1030                 {
1031                   gint largest;
1032
1033                   largest = child_minimum + child->padding * 2;
1034                   minimum = MAX (minimum, largest);
1035
1036                   largest = child_natural + child->padding * 2;
1037                   natural = MAX (natural, largest);
1038                 }
1039               else
1040                 {
1041                   minimum += child_minimum + child->padding * 2;
1042                   natural += child_natural + child->padding * 2;
1043                 }
1044             }
1045           else
1046             {
1047               /* The biggest mins and naturals in the opposing orientation */
1048               minimum = MAX (minimum, child_minimum);
1049               natural = MAX (natural, child_natural);
1050             }
1051
1052           nvis_children += 1;
1053         }
1054     }
1055
1056   if (nvis_children > 0 && private->orientation == orientation)
1057     {
1058       if (private->homogeneous)
1059         {
1060           minimum *= nvis_children;
1061           natural *= nvis_children;
1062         }
1063       minimum += (nvis_children - 1) * private->spacing;
1064       natural += (nvis_children - 1) * private->spacing;
1065     }
1066
1067   if (minimum_size)
1068     *minimum_size = minimum;
1069
1070   if (natural_size)
1071     *natural_size = natural;
1072 }
1073
1074 static void
1075 gtk_box_get_preferred_width (GtkWidget *widget,
1076                              gint      *minimum_size,
1077                              gint      *natural_size)
1078 {
1079   gtk_box_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1080 }
1081
1082 static void
1083 gtk_box_get_preferred_height (GtkWidget *widget,
1084                               gint      *minimum_size,
1085                               gint      *natural_size)
1086 {
1087   gtk_box_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1088 }
1089
1090 static void
1091 gtk_box_compute_size_for_opposing_orientation (GtkBox *box,
1092                                                gint    avail_size,
1093                                                gint   *minimum_size,
1094                                                gint   *natural_size)
1095 {
1096   GtkBoxPrivate       *private = box->priv;
1097   GtkBoxChild      *child;
1098   GList            *children;
1099   gint              nvis_children;
1100   gint              nexpand_children;
1101   gint              computed_minimum = 0, computed_natural = 0;
1102   GtkRequestedSize *sizes;
1103   GtkPackType       packing;
1104   gint              size, extra, i;
1105   gint              child_size, child_minimum, child_natural;
1106   gint              n_extra_widgets = 0;
1107
1108   count_expand_children (box, &nvis_children, &nexpand_children);
1109
1110   if (nvis_children <= 0)
1111     return;
1112
1113   sizes = g_newa (GtkRequestedSize, nvis_children);
1114   size = avail_size - (nvis_children - 1) * private->spacing;
1115
1116   /* Retrieve desired size for visible children */
1117   for (i = 0, children = private->children; children; children = children->next)
1118     {
1119       child = children->data;
1120
1121       if (gtk_widget_get_visible (child->widget))
1122         {
1123           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1124             gtk_widget_get_preferred_width (child->widget,
1125                                             &sizes[i].minimum_size,
1126                                             &sizes[i].natural_size);
1127           else
1128             gtk_widget_get_preferred_height (child->widget,
1129                                              &sizes[i].minimum_size,
1130                                              &sizes[i].natural_size);
1131
1132           /* Assert the api is working properly */
1133           if (sizes[i].minimum_size < 0)
1134             g_error ("GtkBox child %s minimum %s: %d < 0",
1135                      gtk_widget_get_name (GTK_WIDGET (child->widget)),
1136                      (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1137                      sizes[i].minimum_size);
1138
1139           if (sizes[i].natural_size < sizes[i].minimum_size)
1140             g_error ("GtkBox child %s natural %s: %d < minimum %d",
1141                      gtk_widget_get_name (GTK_WIDGET (child->widget)),
1142                      (private->orientation == GTK_ORIENTATION_HORIZONTAL) ? "width" : "height",
1143                      sizes[i].natural_size,
1144                      sizes[i].minimum_size);
1145
1146           size -= sizes[i].minimum_size;
1147           size -= child->padding * 2;
1148
1149           sizes[i].data = child;
1150
1151           i += 1;
1152         }
1153     }
1154
1155   if (private->homogeneous)
1156     {
1157       /* If were homogenous we still need to run the above loop to get the
1158        * minimum sizes for children that are not going to fill
1159        */
1160       size = avail_size - (nvis_children - 1) * private->spacing;
1161       extra = size / nvis_children;
1162       n_extra_widgets = size % nvis_children;
1163     }
1164   else
1165     {
1166       /* Bring children up to size first */
1167       size = gtk_distribute_natural_allocation (MAX (0, size), nvis_children, sizes);
1168
1169       /* Calculate space which hasn't distributed yet,
1170        * and is available for expanding children.
1171        */
1172       if (nexpand_children > 0)
1173         {
1174           extra = size / nexpand_children;
1175           n_extra_widgets = size % nexpand_children;
1176         }
1177       else
1178         extra = 0;
1179     }
1180
1181   /* Allocate child positions. */
1182   for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
1183     {
1184       for (i = 0, children = private->children;
1185            children;
1186            children = children->next)
1187         {
1188           child = children->data;
1189
1190           /* If widget is not visible, skip it. */
1191           if (!gtk_widget_get_visible (child->widget))
1192             continue;
1193
1194           /* If widget is packed differently skip it, but still increment i,
1195            * since widget is visible and will be handled in next loop iteration.
1196            */
1197           if (child->pack != packing)
1198             {
1199               i++;
1200               continue;
1201             }
1202
1203           if (child->pack == packing)
1204             {
1205               /* Assign the child's size. */
1206               if (private->homogeneous)
1207                 {
1208                   child_size = extra;
1209
1210                   if (n_extra_widgets > 0)
1211                     {
1212                       child_size++;
1213                       n_extra_widgets--;
1214                     }
1215                 }
1216               else
1217                 {
1218                   child_size = sizes[i].minimum_size + child->padding * 2;
1219
1220                   if (child->expand || gtk_widget_compute_expand (child->widget, private->orientation))
1221                     {
1222                       child_size += extra;
1223
1224                       if (n_extra_widgets > 0)
1225                         {
1226                           child_size++;
1227                           n_extra_widgets--;
1228                         }
1229                     }
1230                 }
1231
1232               if (child->fill)
1233                 {
1234                   child_size = MAX (1, child_size - child->padding * 2);
1235                 }
1236               else
1237                 {
1238                   child_size = sizes[i].minimum_size;
1239                 }
1240
1241
1242               /* Assign the child's position. */
1243               if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1244                 gtk_widget_get_preferred_height_for_width (child->widget,
1245                                                            child_size, &child_minimum, &child_natural);
1246               else /* (private->orientation == GTK_ORIENTATION_VERTICAL) */
1247                 gtk_widget_get_preferred_width_for_height (child->widget,
1248                                                            child_size, &child_minimum, &child_natural);
1249
1250
1251               computed_minimum = MAX (computed_minimum, child_minimum);
1252               computed_natural = MAX (computed_natural, child_natural);
1253             }
1254           i += 1;
1255         }
1256     }
1257
1258   if (minimum_size)
1259     *minimum_size = computed_minimum;
1260   if (natural_size)
1261     *natural_size = computed_natural;
1262 }
1263
1264 static void
1265 gtk_box_compute_size_for_orientation (GtkBox *box,
1266                                       gint    avail_size,
1267                                       gint   *minimum_size,
1268                                       gint   *natural_size)
1269 {
1270   GtkBoxPrivate    *private = box->priv;
1271   GList         *children;
1272   gint           nvis_children = 0;
1273   gint           required_size = 0, required_natural = 0, child_size, child_natural;
1274   gint           largest_child = 0, largest_natural = 0;
1275
1276   for (children = private->children; children != NULL;
1277        children = children->next)
1278     {
1279       GtkBoxChild *child = children->data;
1280
1281       if (gtk_widget_get_visible (child->widget))
1282         {
1283
1284           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1285             gtk_widget_get_preferred_width_for_height (child->widget,
1286                                                        avail_size, &child_size, &child_natural);
1287           else
1288             gtk_widget_get_preferred_height_for_width (child->widget,
1289                                                        avail_size, &child_size, &child_natural);
1290
1291
1292           child_size    += child->padding * 2;
1293           child_natural += child->padding * 2;
1294
1295           if (child_size > largest_child)
1296             largest_child = child_size;
1297
1298           if (child_natural > largest_natural)
1299             largest_natural = child_natural;
1300
1301           required_size    += child_size;
1302           required_natural += child_natural;
1303
1304           nvis_children += 1;
1305         }
1306     }
1307
1308   if (nvis_children > 0)
1309     {
1310       if (private->homogeneous)
1311         {
1312           required_size    = largest_child   * nvis_children;
1313           required_natural = largest_natural * nvis_children;
1314         }
1315
1316       required_size     += (nvis_children - 1) * private->spacing;
1317       required_natural  += (nvis_children - 1) * private->spacing;
1318     }
1319
1320   if (minimum_size)
1321     *minimum_size = required_size;
1322
1323   if (natural_size)
1324     *natural_size = required_natural;
1325 }
1326
1327 static void
1328 gtk_box_get_preferred_width_for_height (GtkWidget *widget,
1329                                         gint       height,
1330                                         gint      *minimum_width,
1331                                         gint      *natural_width)
1332 {
1333   GtkBox        *box     = GTK_BOX (widget);
1334   GtkBoxPrivate *private = box->priv;
1335
1336   if (private->orientation == GTK_ORIENTATION_VERTICAL)
1337     gtk_box_compute_size_for_opposing_orientation (box, height, minimum_width, natural_width);
1338   else
1339     gtk_box_compute_size_for_orientation (box, height, minimum_width, natural_width);
1340 }
1341
1342 static void
1343 gtk_box_get_preferred_height_for_width (GtkWidget *widget,
1344                                         gint       width,
1345                                         gint      *minimum_height,
1346                                         gint      *natural_height)
1347 {
1348   GtkBox        *box     = GTK_BOX (widget);
1349   GtkBoxPrivate *private = box->priv;
1350
1351   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
1352     gtk_box_compute_size_for_opposing_orientation (box, width, minimum_height, natural_height);
1353   else
1354     gtk_box_compute_size_for_orientation (box, width, minimum_height, natural_height);
1355 }
1356
1357 /**
1358  * gtk_box_new:
1359  * @orientation: the box's orientation.
1360  * @spacing: the number of pixels to place by default between children.
1361  *
1362  * Creates a new #GtkBox.
1363  *
1364  * Return value: a new #GtkBox.
1365  *
1366  * Since: 3.0
1367  **/
1368 GtkWidget*
1369 gtk_box_new (GtkOrientation orientation,
1370              gint           spacing)
1371 {
1372   return g_object_new (GTK_TYPE_BOX,
1373                        "orientation", orientation,
1374                        "spacing",     spacing,
1375                        NULL);
1376 }
1377
1378 /**
1379  * gtk_box_pack_start:
1380  * @box: a #GtkBox
1381  * @child: the #GtkWidget to be added to @box
1382  * @expand: %TRUE if the new child is to be given extra space allocated
1383  *     to @box. The extra space will be divided evenly between all children
1384  *     that use this option
1385  * @fill: %TRUE if space given to @child by the @expand option is
1386  *     actually allocated to @child, rather than just padding it.  This
1387  *     parameter has no effect if @expand is set to %FALSE.  A child is
1388  *     always allocated the full height of a horizontal #GtkBox and the full width
1389  *     of a vertical #GtkBox. This option affects the other dimension
1390  * @padding: extra space in pixels to put between this child and its
1391  *   neighbors, over and above the global amount specified by
1392  *   #GtkBox:spacing property.  If @child is a widget at one of the
1393  *   reference ends of @box, then @padding pixels are also put between
1394  *   @child and the reference edge of @box
1395  *
1396  * Adds @child to @box, packed with reference to the start of @box.
1397  * The @child is packed after any other child packed with reference
1398  * to the start of @box.
1399  */
1400 void
1401 gtk_box_pack_start (GtkBox    *box,
1402                     GtkWidget *child,
1403                     gboolean   expand,
1404                     gboolean   fill,
1405                     guint      padding)
1406 {
1407   gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
1408 }
1409
1410 /**
1411  * gtk_box_pack_end:
1412  * @box: a #GtkBox
1413  * @child: the #GtkWidget to be added to @box
1414  * @expand: %TRUE if the new child is to be given extra space allocated
1415  *   to @box. The extra space will be divided evenly between all children
1416  *   of @box that use this option
1417  * @fill: %TRUE if space given to @child by the @expand option is
1418  *   actually allocated to @child, rather than just padding it.  This
1419  *   parameter has no effect if @expand is set to %FALSE.  A child is
1420  *   always allocated the full height of a horizontal #GtkBox and the full width
1421  *   of a vertical #GtkBox.  This option affects the other dimension
1422  * @padding: extra space in pixels to put between this child and its
1423  *   neighbors, over and above the global amount specified by
1424  *   #GtkBox:spacing property.  If @child is a widget at one of the
1425  *   reference ends of @box, then @padding pixels are also put between
1426  *   @child and the reference edge of @box
1427  *
1428  * Adds @child to @box, packed with reference to the end of @box.
1429  * The @child is packed after (away from end of) any other child
1430  * packed with reference to the end of @box.
1431  */
1432 void
1433 gtk_box_pack_end (GtkBox    *box,
1434                   GtkWidget *child,
1435                   gboolean   expand,
1436                   gboolean   fill,
1437                   guint      padding)
1438 {
1439   gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
1440 }
1441
1442 /**
1443  * gtk_box_set_homogeneous:
1444  * @box: a #GtkBox
1445  * @homogeneous: a boolean value, %TRUE to create equal allotments,
1446  *   %FALSE for variable allotments
1447  *
1448  * Sets the #GtkBox:homogeneous property of @box, controlling
1449  * whether or not all children of @box are given equal space
1450  * in the box.
1451  */
1452 void
1453 gtk_box_set_homogeneous (GtkBox  *box,
1454                          gboolean homogeneous)
1455 {
1456   GtkBoxPrivate *private;
1457
1458   g_return_if_fail (GTK_IS_BOX (box));
1459
1460   private = box->priv;
1461
1462   if ((homogeneous ? TRUE : FALSE) != private->homogeneous)
1463     {
1464       private->homogeneous = homogeneous ? TRUE : FALSE;
1465       g_object_notify (G_OBJECT (box), "homogeneous");
1466       gtk_widget_queue_resize (GTK_WIDGET (box));
1467     }
1468 }
1469
1470 /**
1471  * gtk_box_get_homogeneous:
1472  * @box: a #GtkBox
1473  *
1474  * Returns whether the box is homogeneous (all children are the
1475  * same size). See gtk_box_set_homogeneous().
1476  *
1477  * Return value: %TRUE if the box is homogeneous.
1478  **/
1479 gboolean
1480 gtk_box_get_homogeneous (GtkBox *box)
1481 {
1482   g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1483
1484   return box->priv->homogeneous;
1485 }
1486
1487 /**
1488  * gtk_box_set_spacing:
1489  * @box: a #GtkBox
1490  * @spacing: the number of pixels to put between children
1491  *
1492  * Sets the #GtkBox:spacing property of @box, which is the
1493  * number of pixels to place between children of @box.
1494  */
1495 void
1496 gtk_box_set_spacing (GtkBox *box,
1497                      gint    spacing)
1498 {
1499   GtkBoxPrivate *private;
1500
1501   g_return_if_fail (GTK_IS_BOX (box));
1502
1503   private = box->priv;
1504
1505   if (spacing != private->spacing)
1506     {
1507       private->spacing = spacing;
1508       _gtk_box_set_spacing_set (box, TRUE);
1509
1510       g_object_notify (G_OBJECT (box), "spacing");
1511
1512       gtk_widget_queue_resize (GTK_WIDGET (box));
1513     }
1514 }
1515
1516 /**
1517  * gtk_box_get_spacing:
1518  * @box: a #GtkBox
1519  *
1520  * Gets the value set by gtk_box_set_spacing().
1521  *
1522  * Return value: spacing between children
1523  **/
1524 gint
1525 gtk_box_get_spacing (GtkBox *box)
1526 {
1527   g_return_val_if_fail (GTK_IS_BOX (box), 0);
1528
1529   return box->priv->spacing;
1530 }
1531
1532 void
1533 _gtk_box_set_spacing_set (GtkBox  *box,
1534                           gboolean spacing_set)
1535 {
1536   GtkBoxPrivate *private;
1537
1538   g_return_if_fail (GTK_IS_BOX (box));
1539
1540   private = box->priv;
1541
1542   private->spacing_set = spacing_set ? TRUE : FALSE;
1543 }
1544
1545 gboolean
1546 _gtk_box_get_spacing_set (GtkBox *box)
1547 {
1548   GtkBoxPrivate *private;
1549
1550   g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
1551
1552   private = box->priv;
1553
1554   return private->spacing_set;
1555 }
1556
1557 /**
1558  * gtk_box_reorder_child:
1559  * @box: a #GtkBox
1560  * @child: the #GtkWidget to move
1561  * @position: the new position for @child in the list of children
1562  *   of @box, starting from 0. If negative, indicates the end of
1563  *   the list
1564  *
1565  * Moves @child to a new @position in the list of @box children.
1566  * The list is the <structfield>children</structfield> field of
1567  * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START
1568  * as well as widgets packed #GTK_PACK_END, in the order that these
1569  * widgets were added to @box.
1570  *
1571  * A widget's position in the @box children list determines where
1572  * the widget is packed into @box.  A child widget at some position
1573  * in the list will be packed just after all other widgets of the
1574  * same packing type that appear earlier in the list.
1575  */
1576 void
1577 gtk_box_reorder_child (GtkBox    *box,
1578                        GtkWidget *child,
1579                        gint       position)
1580 {
1581   GtkBoxPrivate *priv;
1582   GList *old_link;
1583   GList *new_link;
1584   GtkBoxChild *child_info = NULL;
1585   gint old_position;
1586
1587   g_return_if_fail (GTK_IS_BOX (box));
1588   g_return_if_fail (GTK_IS_WIDGET (child));
1589
1590   priv = box->priv;
1591
1592   old_link = priv->children;
1593   old_position = 0;
1594   while (old_link)
1595     {
1596       child_info = old_link->data;
1597       if (child_info->widget == child)
1598         break;
1599
1600       old_link = old_link->next;
1601       old_position++;
1602     }
1603
1604   g_return_if_fail (old_link != NULL);
1605
1606   if (position == old_position)
1607     return;
1608
1609   priv->children = g_list_delete_link (priv->children, old_link);
1610
1611   if (position < 0)
1612     new_link = NULL;
1613   else
1614     new_link = g_list_nth (priv->children, position);
1615
1616   priv->children = g_list_insert_before (priv->children, new_link, child_info);
1617
1618   gtk_widget_child_notify (child, "position");
1619   if (gtk_widget_get_visible (child)
1620       && gtk_widget_get_visible (GTK_WIDGET (box)))
1621     {
1622       gtk_box_invalidate_order (box);
1623       gtk_widget_queue_resize (child);
1624     }
1625 }
1626
1627 /**
1628  * gtk_box_query_child_packing:
1629  * @box: a #GtkBox
1630  * @child: the #GtkWidget of the child to query
1631  * @expand: (out): pointer to return location for #GtkBox:expand child
1632  *     property
1633  * @fill: (out): pointer to return location for #GtkBox:fill child
1634  *     property
1635  * @padding: (out): pointer to return location for #GtkBox:padding
1636  *     child property
1637  * @pack_type: (out): pointer to return location for #GtkBox:pack-type
1638  *     child property
1639  *
1640  * Obtains information about how @child is packed into @box.
1641  */
1642 void
1643 gtk_box_query_child_packing (GtkBox      *box,
1644                              GtkWidget   *child,
1645                              gboolean    *expand,
1646                              gboolean    *fill,
1647                              guint       *padding,
1648                              GtkPackType *pack_type)
1649 {
1650   GtkBoxPrivate *private;
1651   GList *list;
1652   GtkBoxChild *child_info = NULL;
1653
1654   g_return_if_fail (GTK_IS_BOX (box));
1655   g_return_if_fail (GTK_IS_WIDGET (child));
1656
1657   private = box->priv;
1658
1659   list = private->children;
1660   while (list)
1661     {
1662       child_info = list->data;
1663       if (child_info->widget == child)
1664         break;
1665
1666       list = list->next;
1667     }
1668
1669   if (list)
1670     {
1671       if (expand)
1672         *expand = child_info->expand;
1673       if (fill)
1674         *fill = child_info->fill;
1675       if (padding)
1676         *padding = child_info->padding;
1677       if (pack_type)
1678         *pack_type = child_info->pack;
1679     }
1680 }
1681
1682 /**
1683  * gtk_box_set_child_packing:
1684  * @box: a #GtkBox
1685  * @child: the #GtkWidget of the child to set
1686  * @expand: the new value of the #GtkBox:expand child property
1687  * @fill: the new value of the #GtkBox:fill child property
1688  * @padding: the new value of the #GtkBox:padding child property
1689  * @pack_type: the new value of the #GtkBox:pack-type child property
1690  *
1691  * Sets the way @child is packed into @box.
1692  */
1693 void
1694 gtk_box_set_child_packing (GtkBox      *box,
1695                            GtkWidget   *child,
1696                            gboolean     expand,
1697                            gboolean     fill,
1698                            guint        padding,
1699                            GtkPackType  pack_type)
1700 {
1701   GtkBoxPrivate *private;
1702   GList *list;
1703   GtkBoxChild *child_info = NULL;
1704
1705   g_return_if_fail (GTK_IS_BOX (box));
1706   g_return_if_fail (GTK_IS_WIDGET (child));
1707
1708   private = box->priv;
1709
1710   list = private->children;
1711   while (list)
1712     {
1713       child_info = list->data;
1714       if (child_info->widget == child)
1715         break;
1716
1717       list = list->next;
1718     }
1719
1720   gtk_widget_freeze_child_notify (child);
1721   if (list)
1722     {
1723       gboolean expanded;
1724
1725       expanded = expand != FALSE;
1726
1727       /* avoid setting expand if unchanged, since queue_compute_expand
1728        * can be expensive-ish
1729        */
1730       if (child_info->expand != expanded)
1731         {
1732           child_info->expand = expand != FALSE;
1733           gtk_widget_queue_compute_expand (GTK_WIDGET (box));
1734           gtk_widget_child_notify (child, "expand");
1735         }
1736
1737       child_info->fill = fill != FALSE;
1738       gtk_widget_child_notify (child, "fill");
1739       child_info->padding = padding;
1740       gtk_widget_child_notify (child, "padding");
1741       if (pack_type != GTK_PACK_END)
1742         pack_type = GTK_PACK_START;
1743       if (child_info->pack != pack_type)
1744         {
1745           child_info->pack = pack_type;
1746           gtk_widget_child_notify (child, "pack-type");
1747           gtk_box_invalidate_order (box);
1748         }
1749
1750       if (gtk_widget_get_visible (child)
1751           && gtk_widget_get_visible (GTK_WIDGET (box)))
1752         gtk_widget_queue_resize (child);
1753     }
1754   gtk_widget_thaw_child_notify (child);
1755 }
1756
1757 void
1758 _gtk_box_set_old_defaults (GtkBox *box)
1759 {
1760   GtkBoxPrivate *private;
1761
1762   g_return_if_fail (GTK_IS_BOX (box));
1763
1764   private = box->priv;
1765
1766   private->default_expand = TRUE;
1767 }
1768
1769 static void
1770 gtk_box_add (GtkContainer *container,
1771              GtkWidget    *widget)
1772 {
1773   GtkBoxPrivate *priv = GTK_BOX (container)->priv;
1774
1775   gtk_box_pack_start (GTK_BOX (container), widget,
1776                       priv->default_expand,
1777                       TRUE,
1778                       0);
1779 }
1780
1781 static void
1782 gtk_box_remove (GtkContainer *container,
1783                 GtkWidget    *widget)
1784 {
1785   GtkBox *box = GTK_BOX (container);
1786   GtkBoxPrivate *priv = box->priv;
1787   GtkBoxChild *child;
1788   GList *children;
1789
1790   children = priv->children;
1791   while (children)
1792     {
1793       child = children->data;
1794
1795       if (child->widget == widget)
1796         {
1797           gboolean was_visible;
1798
1799           g_signal_handlers_disconnect_by_func (widget,
1800                                                 box_child_visibility_notify_cb,
1801                                                 box);
1802
1803           was_visible = gtk_widget_get_visible (widget);
1804           gtk_widget_unparent (widget);
1805
1806           priv->children = g_list_remove_link (priv->children, children);
1807           g_list_free (children);
1808           g_free (child);
1809
1810           /* queue resize regardless of gtk_widget_get_visible (container),
1811            * since that's what is needed by toplevels.
1812            */
1813           if (was_visible)
1814             {
1815               gtk_box_invalidate_order (box);
1816               gtk_widget_queue_resize (GTK_WIDGET (container));
1817             }
1818
1819           break;
1820         }
1821
1822       children = children->next;
1823     }
1824 }
1825
1826 static void
1827 gtk_box_forall (GtkContainer *container,
1828                 gboolean      include_internals,
1829                 GtkCallback   callback,
1830                 gpointer      callback_data)
1831 {
1832   GtkBox *box = GTK_BOX (container);
1833   GtkBoxPrivate *priv = box->priv;
1834   GtkBoxChild *child;
1835   GList *children;
1836
1837   children = priv->children;
1838   while (children)
1839     {
1840       child = children->data;
1841       children = children->next;
1842
1843       if (child->pack == GTK_PACK_START)
1844         (* callback) (child->widget, callback_data);
1845     }
1846
1847   children = g_list_last (priv->children);
1848   while (children)
1849     {
1850       child = children->data;
1851       children = children->prev;
1852
1853       if (child->pack == GTK_PACK_END)
1854         (* callback) (child->widget, callback_data);
1855     }
1856 }
1857
1858 GList *
1859 _gtk_box_get_children (GtkBox *box)
1860 {
1861   GtkBoxPrivate *priv;
1862   GtkBoxChild *child;
1863   GList *children;
1864   GList *retval = NULL;
1865
1866   g_return_val_if_fail (GTK_IS_BOX (box), NULL);
1867
1868   priv = box->priv;
1869
1870   children = priv->children;
1871   while (children)
1872     {
1873       child = children->data;
1874       children = children->next;
1875
1876       retval = g_list_prepend (retval, child->widget);
1877     }
1878
1879   return g_list_reverse (retval);
1880 }