]> Pileus Git - ~andy/gtk/blob - gtk/gtkbox.c
gtk/gtkbox.c: fixed type declaration.
[~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 #include "config.h"
28
29 #include "gtkbox.h"
30 #include "gtkorientable.h"
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33 #include "gtkalias.h"
34
35 enum {
36   PROP_0,
37   PROP_ORIENTATION,
38   PROP_SPACING,
39   PROP_HOMOGENEOUS
40 };
41
42 enum {
43   CHILD_PROP_0,
44   CHILD_PROP_EXPAND,
45   CHILD_PROP_FILL,
46   CHILD_PROP_PADDING,
47   CHILD_PROP_PACK_TYPE,
48   CHILD_PROP_POSITION
49 };
50
51
52 typedef struct _GtkBoxPrivate GtkBoxPrivate;
53
54 struct _GtkBoxPrivate
55 {
56   GtkOrientation orientation;
57   guint          default_expand : 1;
58   guint          spacing_set    : 1;
59 };
60
61 #define GTK_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_BOX, GtkBoxPrivate))
62
63
64 static void gtk_box_set_property       (GObject        *object,
65                                         guint           prop_id,
66                                         const GValue   *value,
67                                         GParamSpec     *pspec);
68 static void gtk_box_get_property       (GObject        *object,
69                                         guint           prop_id,
70                                         GValue         *value,
71                                         GParamSpec     *pspec);
72
73 static void gtk_box_size_request       (GtkWidget      *widget,
74                                         GtkRequisition *requisition);
75 static void gtk_box_size_allocate      (GtkWidget      *widget,
76                                         GtkAllocation  *allocation);
77
78 static void gtk_box_add                (GtkContainer   *container,
79                                         GtkWidget      *widget);
80 static void gtk_box_remove             (GtkContainer   *container,
81                                         GtkWidget      *widget);
82 static void gtk_box_forall             (GtkContainer   *container,
83                                         gboolean        include_internals,
84                                         GtkCallback     callback,
85                                         gpointer        callback_data);
86 static void gtk_box_set_child_property (GtkContainer   *container,
87                                         GtkWidget      *child,
88                                         guint           property_id,
89                                         const GValue   *value,
90                                         GParamSpec     *pspec);
91 static void gtk_box_get_child_property (GtkContainer   *container,
92                                         GtkWidget      *child,
93                                         guint           property_id,
94                                         GValue         *value,
95                                         GParamSpec     *pspec);
96 static GType gtk_box_child_type        (GtkContainer   *container);
97
98
99 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkBox, gtk_box, GTK_TYPE_CONTAINER,
100                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
101                                                          NULL));
102
103 static void
104 gtk_box_class_init (GtkBoxClass *class)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (class);
107   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
108   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
109
110   object_class->set_property = gtk_box_set_property;
111   object_class->get_property = gtk_box_get_property;
112
113   widget_class->size_request = gtk_box_size_request;
114   widget_class->size_allocate = gtk_box_size_allocate;
115
116   container_class->add = gtk_box_add;
117   container_class->remove = gtk_box_remove;
118   container_class->forall = gtk_box_forall;
119   container_class->child_type = gtk_box_child_type;
120   container_class->set_child_property = gtk_box_set_child_property;
121   container_class->get_child_property = gtk_box_get_child_property;
122
123   g_object_class_override_property (object_class,
124                                     PROP_ORIENTATION,
125                                     "orientation");
126
127   g_object_class_install_property (object_class,
128                                    PROP_SPACING,
129                                    g_param_spec_int ("spacing",
130                                                      P_("Spacing"),
131                                                      P_("The amount of space between children"),
132                                                      0,
133                                                      G_MAXINT,
134                                                      0,
135                                                      GTK_PARAM_READWRITE));
136
137   g_object_class_install_property (object_class,
138                                    PROP_HOMOGENEOUS,
139                                    g_param_spec_boolean ("homogeneous",
140                                                          P_("Homogeneous"),
141                                                          P_("Whether the children should all be the same size"),
142                                                          FALSE,
143                                                          GTK_PARAM_READWRITE));
144
145   gtk_container_class_install_child_property (container_class,
146                                               CHILD_PROP_EXPAND,
147                                               g_param_spec_boolean ("expand", 
148                                                                     P_("Expand"), 
149                                                                     P_("Whether the child should receive extra space when the parent grows"),
150                                                                     TRUE,
151                                                                     GTK_PARAM_READWRITE));
152   gtk_container_class_install_child_property (container_class,
153                                               CHILD_PROP_FILL,
154                                               g_param_spec_boolean ("fill", 
155                                                                     P_("Fill"), 
156                                                                     P_("Whether extra space given to the child should be allocated to the child or used as padding"),
157                                                                     TRUE,
158                                                                     GTK_PARAM_READWRITE));
159   gtk_container_class_install_child_property (container_class,
160                                               CHILD_PROP_PADDING,
161                                               g_param_spec_uint ("padding", 
162                                                                  P_("Padding"), 
163                                                                  P_("Extra space to put between the child and its neighbors, in pixels"),
164                                                                  0, G_MAXINT, 0,
165                                                                  GTK_PARAM_READWRITE));
166   gtk_container_class_install_child_property (container_class,
167                                               CHILD_PROP_PACK_TYPE,
168                                               g_param_spec_enum ("pack-type", 
169                                                                  P_("Pack type"), 
170                                                                  P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
171                                                                  GTK_TYPE_PACK_TYPE, GTK_PACK_START,
172                                                                  GTK_PARAM_READWRITE));
173   gtk_container_class_install_child_property (container_class,
174                                               CHILD_PROP_POSITION,
175                                               g_param_spec_int ("position", 
176                                                                 P_("Position"), 
177                                                                 P_("The index of the child in the parent"),
178                                                                 -1, G_MAXINT, 0,
179                                                                 GTK_PARAM_READWRITE));
180
181   g_type_class_add_private (object_class, sizeof (GtkBoxPrivate));
182 }
183
184 static void
185 gtk_box_init (GtkBox *box)
186 {
187   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
188
189   GTK_WIDGET_SET_FLAGS (box, GTK_NO_WINDOW);
190   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), FALSE);
191
192   box->children = NULL;
193   box->spacing = 0;
194   box->homogeneous = FALSE;
195
196   private->orientation = GTK_ORIENTATION_HORIZONTAL;
197   private->default_expand = FALSE;
198   private->spacing_set = FALSE;
199 }
200
201 static void
202 gtk_box_set_property (GObject      *object,
203                       guint         prop_id,
204                       const GValue *value,
205                       GParamSpec   *pspec)
206 {
207   GtkBox *box = GTK_BOX (object);
208   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
209
210   switch (prop_id)
211     {
212     case PROP_ORIENTATION:
213       private->orientation = g_value_get_enum (value);
214       gtk_widget_queue_resize (GTK_WIDGET (box));
215       break;
216     case PROP_SPACING:
217       gtk_box_set_spacing (box, g_value_get_int (value));
218       break;
219     case PROP_HOMOGENEOUS:
220       gtk_box_set_homogeneous (box, g_value_get_boolean (value));
221       break;
222     default:
223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224       break;
225     }
226 }
227
228 static void
229 gtk_box_get_property (GObject    *object,
230                       guint       prop_id,
231                       GValue     *value,
232                       GParamSpec *pspec)
233 {
234   GtkBox *box = GTK_BOX (object);
235   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
236
237   switch (prop_id)
238     {
239     case PROP_ORIENTATION:
240       g_value_set_enum (value, private->orientation);
241       break;
242     case PROP_SPACING:
243       g_value_set_int (value, box->spacing);
244       break;
245     case PROP_HOMOGENEOUS:
246       g_value_set_boolean (value, box->homogeneous);
247       break;
248     default:
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250       break;
251     }
252 }
253
254 static void
255 gtk_box_size_request (GtkWidget      *widget,
256                       GtkRequisition *requisition)
257 {
258   GtkBox *box = GTK_BOX (widget);
259   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
260   GtkBoxChild *child;
261   GList *children;
262   gint nvis_children;
263   gint width;
264   gint height;
265
266   requisition->width = 0;
267   requisition->height = 0;
268   nvis_children = 0;
269
270   children = box->children;
271   while (children)
272     {
273       child = children->data;
274       children = children->next;
275
276       if (GTK_WIDGET_VISIBLE (child->widget))
277         {
278           GtkRequisition child_requisition;
279
280           gtk_widget_size_request (child->widget, &child_requisition);
281
282           if (box->homogeneous)
283             {
284               width = child_requisition.width + child->padding * 2;
285               height = child_requisition.height + child->padding * 2;
286
287               if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
288                 requisition->width = MAX (requisition->width, width);
289               else
290                 requisition->height = MAX (requisition->height, height);
291             }
292           else
293             {
294               if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
295                 requisition->width += child_requisition.width + child->padding * 2;
296               else
297                 requisition->height += child_requisition.height + child->padding * 2;
298             }
299
300           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
301             requisition->height = MAX (requisition->height, child_requisition.height);
302           else
303             requisition->width = MAX (requisition->width, child_requisition.width);
304
305           nvis_children += 1;
306         }
307     }
308
309   if (nvis_children > 0)
310     {
311       if (box->homogeneous)
312         {
313           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
314             requisition->width *= nvis_children;
315           else
316             requisition->height *= nvis_children;
317         }
318
319       if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
320         requisition->width += (nvis_children - 1) * box->spacing;
321       else
322         requisition->height += (nvis_children - 1) * box->spacing;
323     }
324
325   requisition->width += GTK_CONTAINER (box)->border_width * 2;
326   requisition->height += GTK_CONTAINER (box)->border_width * 2;
327 }
328
329 static void
330 gtk_box_size_allocate (GtkWidget     *widget,
331                        GtkAllocation *allocation)
332 {
333   GtkBox *box = GTK_BOX (widget);
334   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (box);
335   GtkBoxChild *child;
336   GList *children;
337   GtkAllocation child_allocation;
338   gint nvis_children = 0;
339   gint nexpand_children = 0;
340   gint child_width = 0;
341   gint child_height = 0;
342   gint width = 0;
343   gint height = 0;
344   gint extra = 0;
345   gint x = 0;
346   gint y = 0;
347   GtkTextDirection direction;
348
349   widget->allocation = *allocation;
350
351   direction = gtk_widget_get_direction (widget);
352
353   for (children = box->children; children; children = children->next)
354     {
355       child = children->data;
356
357       if (GTK_WIDGET_VISIBLE (child->widget))
358         {
359           nvis_children += 1;
360           if (child->expand)
361             nexpand_children += 1;
362         }
363     }
364
365   if (nvis_children > 0)
366     {
367       if (box->homogeneous)
368         {
369           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
370             {
371               width = (allocation->width -
372                        GTK_CONTAINER (box)->border_width * 2 -
373                        (nvis_children - 1) * box->spacing);
374               extra = width / nvis_children;
375             }
376           else
377             {
378               height = (allocation->height -
379                         GTK_CONTAINER (box)->border_width * 2 -
380                         (nvis_children - 1) * box->spacing);
381               extra = height / nvis_children;
382             }
383         }
384       else if (nexpand_children > 0)
385         {
386           if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
387             {
388               width = (gint) allocation->width - (gint) widget->requisition.width;
389               extra = width / nexpand_children;
390             }
391           else
392             {
393               height = (gint) allocation->height - (gint) widget->requisition.height;
394               extra = height / nexpand_children;
395             }
396         }
397
398       if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
399         {
400           x = allocation->x + GTK_CONTAINER (box)->border_width;
401           child_allocation.y = allocation->y + GTK_CONTAINER (box)->border_width;
402           child_allocation.height = MAX (1, (gint) allocation->height - (gint) GTK_CONTAINER (box)->border_width * 2);
403         }
404       else
405         {
406           y = allocation->y + GTK_CONTAINER (box)->border_width;
407           child_allocation.x = allocation->x + GTK_CONTAINER (box)->border_width;
408           child_allocation.width = MAX (1, (gint) allocation->width - (gint) GTK_CONTAINER (box)->border_width * 2);
409         }
410
411       children = box->children;
412       while (children)
413         {
414           child = children->data;
415           children = children->next;
416
417           if ((child->pack == GTK_PACK_START) && GTK_WIDGET_VISIBLE (child->widget))
418             {
419               if (box->homogeneous)
420                 {
421                   if (nvis_children == 1)
422                     {
423                       child_width = width;
424                       child_height = height;
425                     }
426                   else
427                     {
428                       child_width = extra;
429                       child_height = extra;
430                     }
431
432                   nvis_children -= 1;
433                   width -= extra;
434                   height -= extra;
435                 }
436               else
437                 {
438                   GtkRequisition child_requisition;
439
440                   gtk_widget_get_child_requisition (child->widget, &child_requisition);
441
442                   child_width = child_requisition.width + child->padding * 2;
443                   child_height = child_requisition.height + child->padding * 2;
444
445                   if (child->expand)
446                     {
447                       if (nexpand_children == 1)
448                         {
449                           child_width += width;
450                           child_height += height;
451                         }
452                       else
453                         {
454                           child_width += extra;
455                           child_height += extra;
456                         }
457
458                       nexpand_children -= 1;
459                       width -= extra;
460                       height -= extra;
461                     }
462                 }
463
464               if (child->fill)
465                 {
466                   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
467                     {
468                       child_allocation.width = MAX (1, (gint) child_width - (gint) child->padding * 2);
469                       child_allocation.x = x + child->padding;
470                     }
471                   else
472                     {
473                       child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
474                       child_allocation.y = y + child->padding;
475                     }
476                 }
477               else
478                 {
479                   GtkRequisition child_requisition;
480
481                   gtk_widget_get_child_requisition (child->widget, &child_requisition);
482                   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
483                     {
484                       child_allocation.width = child_requisition.width;
485                       child_allocation.x = x + (child_width - child_allocation.width) / 2;
486                     }
487                   else
488                     {
489                       child_allocation.height = child_requisition.height;
490                       child_allocation.y = y + (child_height - child_allocation.height) / 2;
491                     }
492                 }
493
494               if (direction == GTK_TEXT_DIR_RTL &&
495                   private->orientation == GTK_ORIENTATION_HORIZONTAL)
496                 {
497                   child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
498                 }
499
500               gtk_widget_size_allocate (child->widget, &child_allocation);
501
502               x += child_width + box->spacing;
503               y += child_height + box->spacing;
504             }
505         }
506
507       x = allocation->x + allocation->width - GTK_CONTAINER (box)->border_width;
508       y = allocation->y + allocation->height - GTK_CONTAINER (box)->border_width;
509
510       children = box->children;
511       while (children)
512         {
513           child = children->data;
514           children = children->next;
515
516           if ((child->pack == GTK_PACK_END) && GTK_WIDGET_VISIBLE (child->widget))
517             {
518               GtkRequisition child_requisition;
519
520               gtk_widget_get_child_requisition (child->widget, &child_requisition);
521
522               if (box->homogeneous)
523                 {
524                   if (nvis_children == 1)
525                     {
526                       child_width = width;
527                       child_height = height;
528                     }
529                   else
530                     {
531                       child_width = extra;
532                       child_height = extra;
533                    }
534
535                   nvis_children -= 1;
536                   width -= extra;
537                   height -= extra;
538                 }
539               else
540                 {
541                   child_width = child_requisition.width + child->padding * 2;
542                   child_height = child_requisition.height + child->padding * 2;
543
544                   if (child->expand)
545                     {
546                       if (nexpand_children == 1)
547                         {
548                           child_width += width;
549                           child_height += height;
550                          }
551                       else
552                         {
553                           child_width += extra;
554                           child_height += extra;
555                         }
556
557                       nexpand_children -= 1;
558                       width -= extra;
559                       height -= extra;
560                     }
561                 }
562
563               if (child->fill)
564                 {
565                   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
566                     {
567                       child_allocation.width = MAX (1, (gint)child_width - (gint)child->padding * 2);
568                       child_allocation.x = x + child->padding - child_width;
569                     }
570                   else
571                     {
572                       child_allocation.height = MAX (1, child_height - (gint)child->padding * 2);
573                       child_allocation.y = y + child->padding - child_height;
574                      }
575                 }
576               else
577                 {
578                   if (private->orientation == GTK_ORIENTATION_HORIZONTAL)
579                     {
580                       child_allocation.width = child_requisition.width;
581                       child_allocation.x = x + (child_width - child_allocation.width) / 2 - child_width;
582                     }
583                   else
584                     {
585                       child_allocation.height = child_requisition.height;
586                       child_allocation.y = y + (child_height - child_allocation.height) / 2 - child_height;
587                     }
588                 }
589
590               if (direction == GTK_TEXT_DIR_RTL &&
591                   private->orientation == GTK_ORIENTATION_HORIZONTAL)
592                 {
593                   child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
594                 }
595
596               gtk_widget_size_allocate (child->widget, &child_allocation);
597
598               x -= (child_width + box->spacing);
599               y -= (child_height + box->spacing);
600             }
601         }
602     }
603 }
604
605 static GType
606 gtk_box_child_type (GtkContainer   *container)
607 {
608   return GTK_TYPE_WIDGET;
609 }
610
611 static void
612 gtk_box_set_child_property (GtkContainer *container,
613                             GtkWidget    *child,
614                             guint         property_id,
615                             const GValue *value,
616                             GParamSpec   *pspec)
617 {
618   gboolean expand = 0;
619   gboolean fill = 0;
620   guint padding = 0;
621   GtkPackType pack_type = 0;
622
623   if (property_id != CHILD_PROP_POSITION)
624     gtk_box_query_child_packing (GTK_BOX (container),
625                                  child,
626                                  &expand,
627                                  &fill,
628                                  &padding,
629                                  &pack_type);
630   switch (property_id)
631     {
632     case CHILD_PROP_EXPAND:
633       gtk_box_set_child_packing (GTK_BOX (container),
634                                  child,
635                                  g_value_get_boolean (value),
636                                  fill,
637                                  padding,
638                                  pack_type);
639       break;
640     case CHILD_PROP_FILL:
641       gtk_box_set_child_packing (GTK_BOX (container),
642                                  child,
643                                  expand,
644                                  g_value_get_boolean (value),
645                                  padding,
646                                  pack_type);
647       break;
648     case CHILD_PROP_PADDING:
649       gtk_box_set_child_packing (GTK_BOX (container),
650                                  child,
651                                  expand,
652                                  fill,
653                                  g_value_get_uint (value),
654                                  pack_type);
655       break;
656     case CHILD_PROP_PACK_TYPE:
657       gtk_box_set_child_packing (GTK_BOX (container),
658                                  child,
659                                  expand,
660                                  fill,
661                                  padding,
662                                  g_value_get_enum (value));
663       break;
664     case CHILD_PROP_POSITION:
665       gtk_box_reorder_child (GTK_BOX (container),
666                              child,
667                              g_value_get_int (value));
668       break;
669     default:
670       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
671       break;
672     }
673 }
674
675 static void
676 gtk_box_get_child_property (GtkContainer *container,
677                             GtkWidget    *child,
678                             guint         property_id,
679                             GValue       *value,
680                             GParamSpec   *pspec)
681 {
682   gboolean expand = 0;
683   gboolean fill = 0;
684   guint padding = 0;
685   GtkPackType pack_type = 0;
686   GList *list;
687
688   if (property_id != CHILD_PROP_POSITION)
689     gtk_box_query_child_packing (GTK_BOX (container),
690                                  child,
691                                  &expand,
692                                  &fill,
693                                  &padding,
694                                  &pack_type);
695   switch (property_id)
696     {
697       guint i;
698     case CHILD_PROP_EXPAND:
699       g_value_set_boolean (value, expand);
700       break;
701     case CHILD_PROP_FILL:
702       g_value_set_boolean (value, fill);
703       break;
704     case CHILD_PROP_PADDING:
705       g_value_set_uint (value, padding);
706       break;
707     case CHILD_PROP_PACK_TYPE:
708       g_value_set_enum (value, pack_type);
709       break;
710     case CHILD_PROP_POSITION:
711       i = 0;
712       for (list = GTK_BOX (container)->children; list; list = list->next)
713         {
714           GtkBoxChild *child_entry;
715
716           child_entry = list->data;
717           if (child_entry->widget == child)
718             break;
719           i++;
720         }
721       g_value_set_int (value, list ? i : -1);
722       break;
723     default:
724       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
725       break;
726     }
727 }
728
729 static void
730 gtk_box_pack (GtkBox      *box,
731               GtkWidget   *child,
732               gboolean     expand,
733               gboolean     fill,
734               guint        padding,
735               GtkPackType  pack_type)
736 {
737   GtkBoxChild *child_info;
738
739   g_return_if_fail (GTK_IS_BOX (box));
740   g_return_if_fail (GTK_IS_WIDGET (child));
741   g_return_if_fail (child->parent == NULL);
742
743   child_info = g_new (GtkBoxChild, 1);
744   child_info->widget = child;
745   child_info->padding = padding;
746   child_info->expand = expand ? TRUE : FALSE;
747   child_info->fill = fill ? TRUE : FALSE;
748   child_info->pack = pack_type;
749   child_info->is_secondary = FALSE;
750
751   box->children = g_list_append (box->children, child_info);
752
753   gtk_widget_freeze_child_notify (child);
754
755   gtk_widget_set_parent (child, GTK_WIDGET (box));
756   
757   gtk_widget_child_notify (child, "expand");
758   gtk_widget_child_notify (child, "fill");
759   gtk_widget_child_notify (child, "padding");
760   gtk_widget_child_notify (child, "pack-type");
761   gtk_widget_child_notify (child, "position");
762   gtk_widget_thaw_child_notify (child);
763 }
764
765 /**
766  * gtk_box_new:
767  * @orientation: the box' orientation.
768  * @homogeneous: %TRUE if all children are to be given equal space allocations.
769  * @spacing: the number of pixels to place by default between children.
770  *
771  * Creates a new #GtkHBox.
772  *
773  * Return value: a new #GtkHBox.
774  *
775  * Since: 2.16
776  **/
777 GtkWidget*
778 _gtk_box_new (GtkOrientation orientation,
779               gboolean       homogeneous,
780               gint           spacing)
781 {
782   return g_object_new (GTK_TYPE_BOX,
783                        "orientation", orientation,
784                        "spacing",     spacing,
785                        "homogeneous", homogeneous ? TRUE : FALSE,
786                        NULL);
787 }
788
789 /**
790  * gtk_box_pack_start:
791  * @box: a #GtkBox
792  * @child: the #GtkWidget to be added to @box
793  * @expand: %TRUE if the new child is to be given extra space allocated to
794  * @box.  The extra space will be divided evenly between all children of
795  * @box that use this option
796  * @fill: %TRUE if space given to @child by the @expand option is
797  *   actually allocated to @child, rather than just padding it.  This
798  *   parameter has no effect if @expand is set to %FALSE.  A child is
799  *   always allocated the full height of a #GtkHBox and the full width 
800  *   of a #GtkVBox. This option affects the other dimension
801  * @padding: extra space in pixels to put between this child and its
802  *   neighbors, over and above the global amount specified by
803  *   #GtkBox:spacing property.  If @child is a widget at one of the 
804  *   reference ends of @box, then @padding pixels are also put between 
805  *   @child and the reference edge of @box
806  *
807  * Adds @child to @box, packed with reference to the start of @box.
808  * The @child is packed after any other child packed with reference 
809  * to the start of @box.
810  */
811 void
812 gtk_box_pack_start (GtkBox    *box,
813                     GtkWidget *child,
814                     gboolean   expand,
815                     gboolean   fill,
816                     guint      padding)
817 {
818   gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_START);
819 }
820
821 /**
822  * gtk_box_pack_end:
823  * @box: a #GtkBox
824  * @child: the #GtkWidget to be added to @box
825  * @expand: %TRUE if the new child is to be given extra space allocated 
826  *   to @box. The extra space will be divided evenly between all children 
827  *   of @box that use this option
828  * @fill: %TRUE if space given to @child by the @expand option is
829  *   actually allocated to @child, rather than just padding it.  This
830  *   parameter has no effect if @expand is set to %FALSE.  A child is
831  *   always allocated the full height of a #GtkHBox and the full width 
832  *   of a #GtkVBox.  This option affects the other dimension
833  * @padding: extra space in pixels to put between this child and its
834  *   neighbors, over and above the global amount specified by
835  *   #GtkBox:spacing property.  If @child is a widget at one of the 
836  *   reference ends of @box, then @padding pixels are also put between 
837  *   @child and the reference edge of @box
838  *
839  * Adds @child to @box, packed with reference to the end of @box.  
840  * The @child is packed after (away from end of) any other child 
841  * packed with reference to the end of @box.
842  */
843 void
844 gtk_box_pack_end (GtkBox    *box,
845                   GtkWidget *child,
846                   gboolean   expand,
847                   gboolean   fill,
848                   guint      padding)
849 {
850   gtk_box_pack (box, child, expand, fill, padding, GTK_PACK_END);
851 }
852
853 /**
854  * gtk_box_pack_start_defaults:
855  * @box: a #GtkBox
856  * @widget: the #GtkWidget to be added to @box
857  *
858  * Adds @widget to @box, packed with reference to the start of @box.
859  * The child is packed after any other child packed with reference 
860  * to the start of @box. 
861  * 
862  * Parameters for how to pack the child @widget, #GtkBox:expand, 
863  * #GtkBox:fill and #GtkBox:padding, are given their default
864  * values, %TRUE, %TRUE, and 0, respectively.
865  *
866  * Deprecated: 2.14: Use gtk_box_pack_start()
867  */
868 void
869 gtk_box_pack_start_defaults (GtkBox    *box,
870                              GtkWidget *child)
871 {
872   gtk_box_pack_start (box, child, TRUE, TRUE, 0);
873 }
874
875 /**
876  * gtk_box_pack_end_defaults:
877  * @box: a #GtkBox
878  * @widget: the #GtkWidget to be added to @box
879  *
880  * Adds @widget to @box, packed with reference to the end of @box.
881  * The child is packed after any other child packed with reference 
882  * to the start of @box. 
883  * 
884  * Parameters for how to pack the child @widget, #GtkBox:expand, 
885  * #GtkBox:fill and #GtkBox:padding, are given their default
886  * values, %TRUE, %TRUE, and 0, respectively.
887  *
888  * Deprecated: 2.14: Use gtk_box_pack_end()
889  */
890 void
891 gtk_box_pack_end_defaults (GtkBox    *box,
892                            GtkWidget *child)
893 {
894   gtk_box_pack_end (box, child, TRUE, TRUE, 0);
895 }
896
897 /**
898  * gtk_box_set_homogeneous:
899  * @box: a #GtkBox
900  * @homogeneous: a boolean value, %TRUE to create equal allotments,
901  *   %FALSE for variable allotments
902  * 
903  * Sets the #GtkBox:homogeneous property of @box, controlling 
904  * whether or not all children of @box are given equal space 
905  * in the box.
906  */
907 void
908 gtk_box_set_homogeneous (GtkBox  *box,
909                          gboolean homogeneous)
910 {
911   g_return_if_fail (GTK_IS_BOX (box));
912
913   if ((homogeneous ? TRUE : FALSE) != box->homogeneous)
914     {
915       box->homogeneous = homogeneous ? TRUE : FALSE;
916       g_object_notify (G_OBJECT (box), "homogeneous");
917       gtk_widget_queue_resize (GTK_WIDGET (box));
918     }
919 }
920
921 /**
922  * gtk_box_get_homogeneous:
923  * @box: a #GtkBox
924  *
925  * Returns whether the box is homogeneous (all children are the
926  * same size). See gtk_box_set_homogeneous().
927  *
928  * Return value: %TRUE if the box is homogeneous.
929  **/
930 gboolean
931 gtk_box_get_homogeneous (GtkBox *box)
932 {
933   g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
934
935   return box->homogeneous;
936 }
937
938 /**
939  * gtk_box_set_spacing:
940  * @box: a #GtkBox
941  * @spacing: the number of pixels to put between children
942  *
943  * Sets the #GtkBox:spacing property of @box, which is the 
944  * number of pixels to place between children of @box.
945  */
946 void
947 gtk_box_set_spacing (GtkBox *box,
948                      gint    spacing)
949 {
950   g_return_if_fail (GTK_IS_BOX (box));
951
952   if (spacing != box->spacing)
953     {
954       box->spacing = spacing;
955       _gtk_box_set_spacing_set (box, TRUE);
956
957       g_object_notify (G_OBJECT (box), "spacing");
958
959       gtk_widget_queue_resize (GTK_WIDGET (box));
960     }
961 }
962
963 /**
964  * gtk_box_get_spacing:
965  * @box: a #GtkBox
966  * 
967  * Gets the value set by gtk_box_set_spacing().
968  * 
969  * Return value: spacing between children
970  **/
971 gint
972 gtk_box_get_spacing (GtkBox *box)
973 {
974   g_return_val_if_fail (GTK_IS_BOX (box), 0);
975
976   return box->spacing;
977 }
978
979 void
980 _gtk_box_set_spacing_set (GtkBox  *box,
981                           gboolean spacing_set)
982 {
983   GtkBoxPrivate *private;
984
985   g_return_if_fail (GTK_IS_BOX (box));
986
987   private = GTK_BOX_GET_PRIVATE (box);
988
989   private->spacing_set = spacing_set ? TRUE : FALSE;
990 }
991
992 gboolean
993 _gtk_box_get_spacing_set (GtkBox *box)
994 {
995   GtkBoxPrivate *private;
996
997   g_return_val_if_fail (GTK_IS_BOX (box), FALSE);
998
999   private = GTK_BOX_GET_PRIVATE (box);
1000
1001   return private->spacing_set;
1002 }
1003
1004 /**
1005  * gtk_box_reorder_child:
1006  * @box: a #GtkBox
1007  * @child: the #GtkWidget to move
1008  * @position: the new position for @child in the list of children 
1009  *   of @box, starting from 0. If negative, indicates the end of 
1010  *   the list
1011  *
1012  * Moves @child to a new @position in the list of @box children.  
1013  * The list is the <structfield>children</structfield> field of
1014  * #GtkBox-struct, and contains both widgets packed #GTK_PACK_START 
1015  * as well as widgets packed #GTK_PACK_END, in the order that these 
1016  * widgets were added to @box.
1017  * 
1018  * A widget's position in the @box children list determines where 
1019  * the widget is packed into @box.  A child widget at some position 
1020  * in the list will be packed just after all other widgets of the 
1021  * same packing type that appear earlier in the list.
1022  */ 
1023 void
1024 gtk_box_reorder_child (GtkBox    *box,
1025                        GtkWidget *child,
1026                        gint       position)
1027 {
1028   GList *old_link;
1029   GList *new_link;
1030   GtkBoxChild *child_info = NULL;
1031   gint old_position;
1032
1033   g_return_if_fail (GTK_IS_BOX (box));
1034   g_return_if_fail (GTK_IS_WIDGET (child));
1035
1036   old_link = box->children;
1037   old_position = 0;
1038   while (old_link)
1039     {
1040       child_info = old_link->data;
1041       if (child_info->widget == child)
1042         break;
1043
1044       old_link = old_link->next;
1045       old_position++;
1046     }
1047
1048   g_return_if_fail (old_link != NULL);
1049
1050   if (position == old_position)
1051     return;
1052
1053   box->children = g_list_delete_link (box->children, old_link);
1054
1055   if (position < 0)
1056     new_link = NULL;
1057   else
1058     new_link = g_list_nth (box->children, position);
1059
1060   box->children = g_list_insert_before (box->children, new_link, child_info);
1061
1062   gtk_widget_child_notify (child, "position");
1063   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
1064     gtk_widget_queue_resize (child);
1065 }
1066
1067 /**
1068  * gtk_box_query_child_packing:
1069  * @box: a #GtkBox
1070  * @child: the #GtkWidget of the child to query
1071  * @expand: pointer to return location for #GtkBox:expand child property 
1072  * @fill: pointer to return location for #GtkBox:fill child property 
1073  * @padding: pointer to return location for #GtkBox:padding child property 
1074  * @pack_type: pointer to return location for #GtkBox:pack-type child property 
1075  * 
1076  * Obtains information about how @child is packed into @box.
1077  */
1078 void
1079 gtk_box_query_child_packing (GtkBox      *box,
1080                              GtkWidget   *child,
1081                              gboolean    *expand,
1082                              gboolean    *fill,
1083                              guint       *padding,
1084                              GtkPackType *pack_type)
1085 {
1086   GList *list;
1087   GtkBoxChild *child_info = NULL;
1088
1089   g_return_if_fail (GTK_IS_BOX (box));
1090   g_return_if_fail (GTK_IS_WIDGET (child));
1091
1092   list = box->children;
1093   while (list)
1094     {
1095       child_info = list->data;
1096       if (child_info->widget == child)
1097         break;
1098
1099       list = list->next;
1100     }
1101
1102   if (list)
1103     {
1104       if (expand)
1105         *expand = child_info->expand;
1106       if (fill)
1107         *fill = child_info->fill;
1108       if (padding)
1109         *padding = child_info->padding;
1110       if (pack_type)
1111         *pack_type = child_info->pack;
1112     }
1113 }
1114
1115 /**
1116  * gtk_box_set_child_packing:
1117  * @box: a #GtkBox
1118  * @child: the #GtkWidget of the child to set
1119  * @expand: the new value of the #GtkBox:expand child property 
1120  * @fill: the new value of the #GtkBox:fill child property
1121  * @padding: the new value of the #GtkBox:padding child property
1122  * @pack_type: the new value of the #GtkBox:pack-type child property
1123  *
1124  * Sets the way @child is packed into @box.
1125  */
1126 void
1127 gtk_box_set_child_packing (GtkBox      *box,
1128                            GtkWidget   *child,
1129                            gboolean     expand,
1130                            gboolean     fill,
1131                            guint        padding,
1132                            GtkPackType  pack_type)
1133 {
1134   GList *list;
1135   GtkBoxChild *child_info = NULL;
1136
1137   g_return_if_fail (GTK_IS_BOX (box));
1138   g_return_if_fail (GTK_IS_WIDGET (child));
1139
1140   list = box->children;
1141   while (list)
1142     {
1143       child_info = list->data;
1144       if (child_info->widget == child)
1145         break;
1146
1147       list = list->next;
1148     }
1149
1150   gtk_widget_freeze_child_notify (child);
1151   if (list)
1152     {
1153       child_info->expand = expand != FALSE;
1154       gtk_widget_child_notify (child, "expand");
1155       child_info->fill = fill != FALSE;
1156       gtk_widget_child_notify (child, "fill");
1157       child_info->padding = padding;
1158       gtk_widget_child_notify (child, "padding");
1159       if (pack_type == GTK_PACK_END)
1160         child_info->pack = GTK_PACK_END;
1161       else
1162         child_info->pack = GTK_PACK_START;
1163       gtk_widget_child_notify (child, "pack-type");
1164
1165       if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (box))
1166         gtk_widget_queue_resize (child);
1167     }
1168   gtk_widget_thaw_child_notify (child);
1169 }
1170
1171 void
1172 _gtk_box_set_old_defaults (GtkBox *box)
1173 {
1174   GtkBoxPrivate *private;
1175
1176   g_return_if_fail (GTK_IS_BOX (box));
1177
1178   private = GTK_BOX_GET_PRIVATE (box);
1179
1180   private->default_expand = TRUE;
1181 }
1182
1183 static void
1184 gtk_box_add (GtkContainer *container,
1185              GtkWidget    *widget)
1186 {
1187   GtkBoxPrivate *private = GTK_BOX_GET_PRIVATE (container);
1188
1189   gtk_box_pack_start (GTK_BOX (container), widget,
1190                       private->default_expand,
1191                       private->default_expand,
1192                       0);
1193 }
1194
1195 static void
1196 gtk_box_remove (GtkContainer *container,
1197                 GtkWidget    *widget)
1198 {
1199   GtkBox *box = GTK_BOX (container);
1200   GtkBoxChild *child;
1201   GList *children;
1202
1203   children = box->children;
1204   while (children)
1205     {
1206       child = children->data;
1207
1208       if (child->widget == widget)
1209         {
1210           gboolean was_visible;
1211
1212           was_visible = GTK_WIDGET_VISIBLE (widget);
1213           gtk_widget_unparent (widget);
1214
1215           box->children = g_list_remove_link (box->children, children);
1216           g_list_free (children);
1217           g_free (child);
1218
1219           /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
1220            * since that's what is needed by toplevels.
1221            */
1222           if (was_visible)
1223             gtk_widget_queue_resize (GTK_WIDGET (container));
1224
1225           break;
1226         }
1227
1228       children = children->next;
1229     }
1230 }
1231
1232 static void
1233 gtk_box_forall (GtkContainer *container,
1234                 gboolean      include_internals,
1235                 GtkCallback   callback,
1236                 gpointer      callback_data)
1237 {
1238   GtkBox *box = GTK_BOX (container);
1239   GtkBoxChild *child;
1240   GList *children;
1241
1242   children = box->children;
1243   while (children)
1244     {
1245       child = children->data;
1246       children = children->next;
1247
1248       if (child->pack == GTK_PACK_START)
1249         (* callback) (child->widget, callback_data);
1250     }
1251
1252   children = g_list_last (box->children);
1253   while (children)
1254     {
1255       child = children->data;
1256       children = children->prev;
1257
1258       if (child->pack == GTK_PACK_END)
1259         (* callback) (child->widget, callback_data);
1260     }
1261 }
1262
1263 #define __GTK_BOX_C__
1264 #include "gtkaliasdef.c"