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