]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Add GtkBuilder, fixes #172535
[~andy/gtk] / gtk / gtkcontainer.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 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "gtkcontainer.h"
33 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
38 #include "gtkintl.h"
39 #include "gtktoolbar.h"
40 #include <gobject/gobjectnotifyqueue.c>
41 #include <gobject/gvaluecollector.h>
42 #include "gtkalias.h"
43
44
45 enum {
46   ADD,
47   REMOVE,
48   CHECK_RESIZE,
49   SET_FOCUS_CHILD,
50   LAST_SIGNAL
51 };
52
53 enum {
54   PROP_0,
55   PROP_BORDER_WIDTH,
56   PROP_RESIZE_MODE,
57   PROP_CHILD
58 };
59
60 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
61 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
62
63
64 /* --- prototypes --- */
65 static void     gtk_container_base_class_init      (GtkContainerClass *klass);
66 static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
67 static void     gtk_container_class_init           (GtkContainerClass *klass);
68 static void     gtk_container_init                 (GtkContainer      *container);
69 static void     gtk_container_destroy              (GtkObject         *object);
70 static void     gtk_container_set_property         (GObject         *object,
71                                                     guint            prop_id,
72                                                     const GValue    *value,
73                                                     GParamSpec      *pspec);
74 static void     gtk_container_get_property         (GObject         *object,
75                                                     guint            prop_id,
76                                                     GValue          *value,
77                                                     GParamSpec      *pspec);
78 static void     gtk_container_add_unimplemented    (GtkContainer      *container,
79                                                     GtkWidget         *widget);
80 static void     gtk_container_remove_unimplemented (GtkContainer      *container,
81                                                     GtkWidget         *widget);
82 static void     gtk_container_real_check_resize    (GtkContainer      *container);
83 static gboolean gtk_container_focus                (GtkWidget         *widget,
84                                                     GtkDirectionType   direction);
85 static void     gtk_container_real_set_focus_child (GtkContainer      *container,
86                                                     GtkWidget         *widget);
87
88 static gboolean gtk_container_focus_move           (GtkContainer      *container,
89                                                     GList             *children,
90                                                     GtkDirectionType   direction);
91 static void     gtk_container_children_callback    (GtkWidget         *widget,
92                                                     gpointer           client_data);
93 static void     gtk_container_show_all             (GtkWidget         *widget);
94 static void     gtk_container_hide_all             (GtkWidget         *widget);
95 static gint     gtk_container_expose               (GtkWidget         *widget,
96                                                     GdkEventExpose    *event);
97 static void     gtk_container_map                  (GtkWidget         *widget);
98 static void     gtk_container_unmap                (GtkWidget         *widget);
99
100 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
101                                                           GtkWidget    *child);
102
103 /* GtkBuildable */
104 static void gtk_container_buildable_init           (GtkBuildableIface *iface);
105 static void gtk_container_buildable_add            (GtkBuildable *buildable,
106                                                     GtkBuilder   *builder,
107                                                     GObject      *child,
108                                                     const gchar  *type);
109 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
110                                                           GtkBuilder    *builder,
111                                                           GObject       *child,
112                                                           const gchar   *tagname,
113                                                           GMarkupParser *parser,
114                                                           gpointer      *data);
115 static void    gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
116                                                        GtkBuilder   *builder,
117                                                        GObject      *child,
118                                                        const gchar  *tagname,
119                                                        gpointer     *data);
120
121
122 /* --- variables --- */
123 static const gchar           vadjustment_key[] = "gtk-vadjustment";
124 static guint                 vadjustment_key_id = 0;
125 static const gchar           hadjustment_key[] = "gtk-hadjustment";
126 static guint                 hadjustment_key_id = 0;
127 static GSList               *container_resize_queue = NULL;
128 static guint                 container_signals[LAST_SIGNAL] = { 0 };
129 static GtkWidgetClass       *parent_class = NULL;
130 extern GParamSpecPool       *_gtk_widget_child_property_pool;
131 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
132 static GtkBuildableIface    *parent_buildable_iface;
133
134
135 /* --- functions --- */
136 GType
137 gtk_container_get_type (void)
138 {
139   static GType container_type = 0;
140
141   if (!container_type)
142     {
143       const GTypeInfo container_info =
144       {
145         sizeof (GtkContainerClass),
146         (GBaseInitFunc) gtk_container_base_class_init,
147         (GBaseFinalizeFunc) gtk_container_base_class_finalize,
148         (GClassInitFunc) gtk_container_class_init,
149         NULL        /* class_finalize */,
150         NULL        /* class_data */,
151         sizeof (GtkContainer),
152         0           /* n_preallocs */,
153         (GInstanceInitFunc) gtk_container_init,
154         NULL,       /* value_table */
155       };
156
157       static const GInterfaceInfo buildable_info =
158       {
159         (GInterfaceInitFunc) gtk_container_buildable_init,
160         NULL,
161         NULL
162       };
163
164       container_type =
165         g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"), 
166                                 &container_info, G_TYPE_FLAG_ABSTRACT);
167
168       g_type_add_interface_static (container_type,
169                                    GTK_TYPE_BUILDABLE,
170                                    &buildable_info);
171
172     }
173
174   return container_type;
175 }
176
177 static void
178 gtk_container_base_class_init (GtkContainerClass *class)
179 {
180   /* reset instance specifc class fields that don't get inherited */
181   class->set_child_property = NULL;
182   class->get_child_property = NULL;
183 }
184
185 static void
186 gtk_container_base_class_finalize (GtkContainerClass *class)
187 {
188   GList *list, *node;
189
190   list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
191   for (node = list; node; node = node->next)
192     {
193       GParamSpec *pspec = node->data;
194
195       g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
196       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
197       g_param_spec_unref (pspec);
198     }
199   g_list_free (list);
200 }
201
202 static void
203 gtk_container_class_init (GtkContainerClass *class)
204 {
205   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
206   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
207   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
208
209   parent_class = g_type_class_peek_parent (class);
210
211   vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
212   hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
213   
214   gobject_class->set_property = gtk_container_set_property;
215   gobject_class->get_property = gtk_container_get_property;
216
217   object_class->destroy = gtk_container_destroy;
218
219   widget_class->show_all = gtk_container_show_all;
220   widget_class->hide_all = gtk_container_hide_all;
221   widget_class->expose_event = gtk_container_expose;
222   widget_class->map = gtk_container_map;
223   widget_class->unmap = gtk_container_unmap;
224   widget_class->focus = gtk_container_focus;
225   
226   class->add = gtk_container_add_unimplemented;
227   class->remove = gtk_container_remove_unimplemented;
228   class->check_resize = gtk_container_real_check_resize;
229   class->forall = NULL;
230   class->set_focus_child = gtk_container_real_set_focus_child;
231   class->child_type = NULL;
232   class->composite_name = gtk_container_child_default_composite_name;
233
234   g_object_class_install_property (gobject_class,
235                                    PROP_RESIZE_MODE,
236                                    g_param_spec_enum ("resize-mode",
237                                                       P_("Resize mode"),
238                                                       P_("Specify how resize events are handled"),
239                                                       GTK_TYPE_RESIZE_MODE,
240                                                       GTK_RESIZE_PARENT,
241                                                       GTK_PARAM_READWRITE));
242   g_object_class_install_property (gobject_class,
243                                    PROP_BORDER_WIDTH,
244                                    g_param_spec_uint ("border-width",
245                                                       P_("Border width"),
246                                                       P_("The width of the empty border outside the containers children"),
247                                                       0,
248                                                       G_MAXINT,
249                                                       0,
250                                                       GTK_PARAM_READWRITE));
251   g_object_class_install_property (gobject_class,
252                                    PROP_CHILD,
253                                    g_param_spec_object ("child",
254                                                       P_("Child"),
255                                                       P_("Can be used to add a new child to the container"),
256                                                       GTK_TYPE_WIDGET,
257                                                       GTK_PARAM_WRITABLE));
258   container_signals[ADD] =
259     g_signal_new (I_("add"),
260                   G_OBJECT_CLASS_TYPE (object_class),
261                   G_SIGNAL_RUN_FIRST,
262                   G_STRUCT_OFFSET (GtkContainerClass, add),
263                   NULL, NULL,
264                   _gtk_marshal_VOID__OBJECT,
265                   G_TYPE_NONE, 1,
266                   GTK_TYPE_WIDGET);
267   container_signals[REMOVE] =
268     g_signal_new (I_("remove"),
269                   G_OBJECT_CLASS_TYPE (object_class),
270                   G_SIGNAL_RUN_FIRST,
271                   G_STRUCT_OFFSET (GtkContainerClass, remove),
272                   NULL, NULL,
273                   _gtk_marshal_VOID__OBJECT,
274                   G_TYPE_NONE, 1,
275                   GTK_TYPE_WIDGET);
276   container_signals[CHECK_RESIZE] =
277     g_signal_new (I_("check_resize"),
278                   G_OBJECT_CLASS_TYPE (object_class),
279                   G_SIGNAL_RUN_LAST,
280                   G_STRUCT_OFFSET (GtkContainerClass, check_resize),
281                   NULL, NULL,
282                   _gtk_marshal_VOID__VOID,
283                   G_TYPE_NONE, 0);
284   container_signals[SET_FOCUS_CHILD] =
285     g_signal_new (I_("set-focus-child"),
286                   G_OBJECT_CLASS_TYPE (object_class),
287                   G_SIGNAL_RUN_FIRST,
288                   G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
289                   NULL, NULL,
290                   _gtk_marshal_VOID__OBJECT,
291                   G_TYPE_NONE, 1,
292                   GTK_TYPE_WIDGET);
293 }
294
295 static void
296 gtk_container_buildable_init (GtkBuildableIface *iface)
297 {
298   parent_buildable_iface = g_type_interface_peek_parent (iface);
299   iface->add = gtk_container_buildable_add;
300   iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
301   iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
302 }
303
304 static void
305 gtk_container_buildable_add (GtkBuildable  *buildable,
306                              GtkBuilder    *builder,
307                              GObject       *child,
308                              const gchar   *type)
309 {
310   g_return_if_fail (GTK_IS_WIDGET (child));
311
312   gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
313 }
314
315 static void
316 gtk_container_buildable_set_child_property (GtkContainer *container,
317                                             GtkWidget    *child,
318                                             gchar        *name,
319                                             const gchar  *value)
320 {
321   GParamSpec *pspec;
322   GValue gvalue = { 0, };
323
324   pspec = gtk_container_class_find_child_property
325     (G_OBJECT_GET_CLASS (container), name);
326   if (!pspec)
327     {
328       g_warning ("%s does not have a property called %s",
329                  g_type_name (G_OBJECT_TYPE (container)), name);
330       return;
331     }
332
333   if (!gtk_builder_value_from_string (pspec, value, &gvalue))
334     {
335       g_warning ("Could not read property %s:%s with value %s of type %s",
336                  g_type_name (G_OBJECT_TYPE (container)),
337                  name,
338                  value,
339                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
340       return;
341     }
342
343   gtk_container_child_set_property (container, child, name, &gvalue);
344   g_value_unset (&gvalue);
345 }
346
347 typedef struct {
348   GtkBuilder   *builder;
349   GtkContainer *container;
350   GtkWidget    *child;
351   gchar        *child_prop_name;
352 } PackingPropertiesData;
353
354 static void
355 attributes_start_element (GMarkupParseContext *context,
356                           const gchar         *element_name,
357                           const gchar        **names,
358                           const gchar        **values,
359                           gpointer             user_data,
360                           GError             **error)
361 {
362   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
363   guint i;
364
365   if (strcmp (element_name, "property") == 0)
366     for (i = 0; names[i]; i++)
367       if (strcmp (names[i], "name") == 0)
368         parser_data->child_prop_name = g_strdup (values[i]);
369   else if (strcmp (element_name, "packing") == 0)
370     return;
371   else
372     g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
373 }
374
375 static void
376 attributes_text_element (GMarkupParseContext *context,
377                          const gchar         *text,
378                          gsize                text_len,
379                          gpointer             user_data,
380                          GError             **error)
381 {
382   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
383
384   if (!parser_data->child_prop_name)
385     return;
386
387   gtk_container_buildable_set_child_property (parser_data->container,
388                                               parser_data->child,
389                                               parser_data->child_prop_name,
390                                               text);
391
392   g_free (parser_data->child_prop_name);
393   parser_data->child_prop_name = NULL;
394 }
395
396 static const GMarkupParser attributes_parser =
397   {
398     attributes_start_element,
399     NULL,
400     attributes_text_element,
401   };
402
403 static gboolean
404 gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
405                                           GtkBuilder    *builder,
406                                           GObject       *child,
407                                           const gchar   *tagname,
408                                           GMarkupParser *parser,
409                                           gpointer      *data)
410 {
411   PackingPropertiesData *parser_data;
412
413   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
414                                                 tagname, parser, data))
415     return TRUE;
416
417   if (child && strcmp (tagname, "packing") == 0)
418     {
419       parser_data = g_slice_new0 (PackingPropertiesData);
420       parser_data->builder = builder;
421       parser_data->container = GTK_CONTAINER (buildable);
422       parser_data->child = GTK_WIDGET (child);
423       parser_data->child_prop_name = NULL;
424
425       *parser = attributes_parser;
426       *data = parser_data;
427       return TRUE;
428     }
429
430   return FALSE;
431 }
432
433 static void
434 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
435                                         GtkBuilder   *builder,
436                                         GObject      *child,
437                                         const gchar  *tagname,
438                                         gpointer     *data)
439 {
440   if (strcmp (tagname, "packing") == 0)
441     {
442       g_slice_free (PackingPropertiesData, (gpointer)data);
443       return;
444
445     }
446
447   if (parent_buildable_iface->custom_tag_end)
448     parent_buildable_iface->custom_tag_end (buildable, builder,
449                                             child, tagname, data);
450
451 }
452
453 /**
454  * gtk_container_child_type: 
455  * @container: a #GtkContainer
456  *
457  * Returns the type of the children supported by the container.
458  *
459  * Note that this may return %G_TYPE_NONE to indicate that no more
460  * children can be added, e.g. for a #GtkPaned which already has two 
461  * children.
462  *
463  * Return value: a #GType.
464  **/
465 GType
466 gtk_container_child_type (GtkContainer *container)
467 {
468   GType slot;
469   GtkContainerClass *class;
470
471   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
472
473   class = GTK_CONTAINER_GET_CLASS (container);
474   if (class->child_type)
475     slot = class->child_type (container);
476   else
477     slot = G_TYPE_NONE;
478
479   return slot;
480 }
481
482 /* --- GtkContainer child property mechanism --- */
483 static inline void
484 container_get_child_property (GtkContainer *container,
485                               GtkWidget    *child,
486                               GParamSpec   *pspec,
487                               GValue       *value)
488 {
489   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
490   
491   class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
492 }
493
494 static inline void
495 container_set_child_property (GtkContainer       *container,
496                               GtkWidget          *child,
497                               GParamSpec         *pspec,
498                               const GValue       *value,
499                               GObjectNotifyQueue *nqueue)
500 {
501   GValue tmp_value = { 0, };
502   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
503
504   /* provide a copy to work from, convert (if necessary) and validate */
505   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
506   if (!g_value_transform (value, &tmp_value))
507     g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
508                pspec->name,
509                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
510                G_VALUE_TYPE_NAME (value));
511   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
512     {
513       gchar *contents = g_strdup_value_contents (value);
514
515       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
516                  contents,
517                  G_VALUE_TYPE_NAME (value),
518                  pspec->name,
519                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
520       g_free (contents);
521     }
522   else
523     {
524       class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
525       g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
526     }
527   g_value_unset (&tmp_value);
528 }
529
530 /**
531  * gtk_container_child_get_valist:
532  * @container: a #GtkContainer
533  * @child: a widget which is a child of @container
534  * @first_property_name: the name of the first property to get
535  * @var_args: a %NULL-terminated list of property names and #GValue*, 
536  *           starting with @first_prop_name.
537  * 
538  * Gets the values of one or more child properties for @child and @container.
539  **/
540 void
541 gtk_container_child_get_valist (GtkContainer *container,
542                                 GtkWidget    *child,
543                                 const gchar  *first_property_name,
544                                 va_list       var_args)
545 {
546   const gchar *name;
547
548   g_return_if_fail (GTK_IS_CONTAINER (container));
549   g_return_if_fail (GTK_IS_WIDGET (child));
550   g_return_if_fail (child->parent == GTK_WIDGET (container));
551
552   g_object_ref (container);
553   g_object_ref (child);
554
555   name = first_property_name;
556   while (name)
557     {
558       GValue value = { 0, };
559       GParamSpec *pspec;
560       gchar *error;
561
562       pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
563                                         name,
564                                         G_OBJECT_TYPE (container),
565                                         TRUE);
566       if (!pspec)
567         {
568           g_warning ("%s: container class `%s' has no child property named `%s'",
569                      G_STRLOC,
570                      G_OBJECT_TYPE_NAME (container),
571                      name);
572           break;
573         }
574       if (!(pspec->flags & G_PARAM_READABLE))
575         {
576           g_warning ("%s: child property `%s' of container class `%s' is not readable",
577                      G_STRLOC,
578                      pspec->name,
579                      G_OBJECT_TYPE_NAME (container));
580           break;
581         }
582       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
583       container_get_child_property (container, child, pspec, &value);
584       G_VALUE_LCOPY (&value, var_args, 0, &error);
585       if (error)
586         {
587           g_warning ("%s: %s", G_STRLOC, error);
588           g_free (error);
589           g_value_unset (&value);
590           break;
591         }
592       g_value_unset (&value);
593       name = va_arg (var_args, gchar*);
594     }
595
596   g_object_unref (child);
597   g_object_unref (container);
598 }
599
600 /**
601  * gtk_container_child_get_property:
602  * @container: a #GtkContainer
603  * @child: a widget which is a child of @container
604  * @property_name: the name of the property to get
605  * @value: a location to return the value
606  * 
607  * Gets the value of a child property for @child and @container.
608  **/
609 void
610 gtk_container_child_get_property (GtkContainer *container,
611                                   GtkWidget    *child,
612                                   const gchar  *property_name,
613                                   GValue       *value)
614 {
615   GParamSpec *pspec;
616
617   g_return_if_fail (GTK_IS_CONTAINER (container));
618   g_return_if_fail (GTK_IS_WIDGET (child));
619   g_return_if_fail (child->parent == GTK_WIDGET (container));
620   g_return_if_fail (property_name != NULL);
621   g_return_if_fail (G_IS_VALUE (value));
622   
623   g_object_ref (container);
624   g_object_ref (child);
625   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
626                                     G_OBJECT_TYPE (container), TRUE);
627   if (!pspec)
628     g_warning ("%s: container class `%s' has no child property named `%s'",
629                G_STRLOC,
630                G_OBJECT_TYPE_NAME (container),
631                property_name);
632   else if (!(pspec->flags & G_PARAM_READABLE))
633     g_warning ("%s: child property `%s' of container class `%s' is not readable",
634                G_STRLOC,
635                pspec->name,
636                G_OBJECT_TYPE_NAME (container));
637   else
638     {
639       GValue *prop_value, tmp_value = { 0, };
640
641       /* auto-conversion of the callers value type
642        */
643       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
644         {
645           g_value_reset (value);
646           prop_value = value;
647         }
648       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
649         {
650           g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
651                      pspec->name,
652                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
653                      G_VALUE_TYPE_NAME (value));
654           g_object_unref (child);
655           g_object_unref (container);
656           return;
657         }
658       else
659         {
660           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
661           prop_value = &tmp_value;
662         }
663       container_get_child_property (container, child, pspec, prop_value);
664       if (prop_value != value)
665         {
666           g_value_transform (prop_value, value);
667           g_value_unset (&tmp_value);
668         }
669     }
670   g_object_unref (child);
671   g_object_unref (container);
672 }
673
674 /**
675  * gtk_container_child_set_valist:
676  * @container: a #GtkContainer
677  * @child: a widget which is a child of @container
678  * @first_property_name: the name of the first property to set
679  * @var_args: a %NULL-terminated list of property names and values, starting
680  *           with @first_prop_name
681  * 
682  * Sets one or more child properties for @child and @container.
683  **/
684 void
685 gtk_container_child_set_valist (GtkContainer *container,
686                                 GtkWidget    *child,
687                                 const gchar  *first_property_name,
688                                 va_list       var_args)
689 {
690   GObjectNotifyQueue *nqueue;
691   const gchar *name;
692
693   g_return_if_fail (GTK_IS_CONTAINER (container));
694   g_return_if_fail (GTK_IS_WIDGET (child));
695   g_return_if_fail (child->parent == GTK_WIDGET (container));
696
697   g_object_ref (container);
698   g_object_ref (child);
699
700   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
701   name = first_property_name;
702   while (name)
703     {
704       GValue value = { 0, };
705       gchar *error = NULL;
706       GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
707                                                     name,
708                                                     G_OBJECT_TYPE (container),
709                                                     TRUE);
710       if (!pspec)
711         {
712           g_warning ("%s: container class `%s' has no child property named `%s'",
713                      G_STRLOC,
714                      G_OBJECT_TYPE_NAME (container),
715                      name);
716           break;
717         }
718       if (!(pspec->flags & G_PARAM_WRITABLE))
719         {
720           g_warning ("%s: child property `%s' of container class `%s' is not writable",
721                      G_STRLOC,
722                      pspec->name,
723                      G_OBJECT_TYPE_NAME (container));
724           break;
725         }
726       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
727       G_VALUE_COLLECT (&value, var_args, 0, &error);
728       if (error)
729         {
730           g_warning ("%s: %s", G_STRLOC, error);
731           g_free (error);
732
733           /* we purposely leak the value here, it might not be
734            * in a sane state if an error condition occoured
735            */
736           break;
737         }
738       container_set_child_property (container, child, pspec, &value, nqueue);
739       g_value_unset (&value);
740       name = va_arg (var_args, gchar*);
741     }
742   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
743
744   g_object_unref (container);
745   g_object_unref (child);
746 }
747
748 /**
749  * gtk_container_child_set_property:
750  * @container: a #GtkContainer
751  * @child: a widget which is a child of @container
752  * @property_name: the name of the property to set
753  * @value: the value to set the property to
754  * 
755  * Sets a child property for @child and @container.
756  **/
757 void
758 gtk_container_child_set_property (GtkContainer *container,
759                                   GtkWidget    *child,
760                                   const gchar  *property_name,
761                                   const GValue *value)
762 {
763   GObjectNotifyQueue *nqueue;
764   GParamSpec *pspec;
765
766   g_return_if_fail (GTK_IS_CONTAINER (container));
767   g_return_if_fail (GTK_IS_WIDGET (child));
768   g_return_if_fail (child->parent == GTK_WIDGET (container));
769   g_return_if_fail (property_name != NULL);
770   g_return_if_fail (G_IS_VALUE (value));
771   
772   g_object_ref (container);
773   g_object_ref (child);
774
775   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
776   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
777                                     G_OBJECT_TYPE (container), TRUE);
778   if (!pspec)
779     g_warning ("%s: container class `%s' has no child property named `%s'",
780                G_STRLOC,
781                G_OBJECT_TYPE_NAME (container),
782                property_name);
783   else if (!(pspec->flags & G_PARAM_WRITABLE))
784     g_warning ("%s: child property `%s' of container class `%s' is not writable",
785                G_STRLOC,
786                pspec->name,
787                G_OBJECT_TYPE_NAME (container));
788   else
789     {
790       container_set_child_property (container, child, pspec, value, nqueue);
791     }
792   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
793   g_object_unref (container);
794   g_object_unref (child);
795 }
796
797 /**
798  * gtk_container_add_with_properties:
799  * @container: a #GtkContainer 
800  * @widget: a widget to be placed inside @container 
801  * @first_prop_name: the name of the first child property to set 
802  * @Varargs: a %NULL-terminated list of property names and values, starting
803  *           with @first_prop_name
804  * 
805  * Adds @widget to @container, setting child properties at the same time.
806  * See gtk_container_add() and gtk_container_child_set() for more details.
807  **/
808 void
809 gtk_container_add_with_properties (GtkContainer *container,
810                                    GtkWidget    *widget,
811                                    const gchar  *first_prop_name,
812                                    ...)
813 {
814   g_return_if_fail (GTK_IS_CONTAINER (container));
815   g_return_if_fail (GTK_IS_WIDGET (widget));
816   g_return_if_fail (widget->parent == NULL);
817
818   g_object_ref (container);
819   g_object_ref (widget);
820   gtk_widget_freeze_child_notify (widget);
821
822   g_signal_emit (container, container_signals[ADD], 0, widget);
823   if (widget->parent)
824     {
825       va_list var_args;
826
827       va_start (var_args, first_prop_name);
828       gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
829       va_end (var_args);
830     }
831
832   gtk_widget_thaw_child_notify (widget);
833   g_object_unref (widget);
834   g_object_unref (container);
835 }
836
837 /**
838  * gtk_container_child_set:
839  * @container: a #GtkContainer
840  * @child: a widget which is a child of @container
841  * @first_prop_name: the name of the first property to set
842  * @Varargs: a %NULL-terminated list of property names and values, starting
843  *           with @first_prop_name
844  * 
845  * Sets one or more child properties for @child and @container.
846  **/
847 void
848 gtk_container_child_set (GtkContainer      *container,
849                          GtkWidget         *child,
850                          const gchar       *first_prop_name,
851                          ...)
852 {
853   va_list var_args;
854   
855   g_return_if_fail (GTK_IS_CONTAINER (container));
856   g_return_if_fail (GTK_IS_WIDGET (child));
857   g_return_if_fail (child->parent == GTK_WIDGET (container));
858
859   va_start (var_args, first_prop_name);
860   gtk_container_child_set_valist (container, child, first_prop_name, var_args);
861   va_end (var_args);
862 }
863
864 /**
865  * gtk_container_child_get:
866  * @container: a #GtkContainer
867  * @child: a widget which is a child of @container
868  * @first_prop_name: the name of the first property to get
869  * @Varargs: a %NULL-terminated list of property names and #GValue*, 
870  *           starting with @first_prop_name
871  * 
872  * Gets the values of one or more child properties for @child and @container.
873  **/
874 void
875 gtk_container_child_get (GtkContainer      *container,
876                          GtkWidget         *child,
877                          const gchar       *first_prop_name,
878                          ...)
879 {
880   va_list var_args;
881   
882   g_return_if_fail (GTK_IS_CONTAINER (container));
883   g_return_if_fail (GTK_IS_WIDGET (child));
884   g_return_if_fail (child->parent == GTK_WIDGET (container));
885
886   va_start (var_args, first_prop_name);
887   gtk_container_child_get_valist (container, child, first_prop_name, var_args);
888   va_end (var_args);
889 }
890
891 /**
892  * gtk_container_class_install_child_property:
893  * @cclass: a #GtkContainerClass
894  * @property_id: the id for the property
895  * @pspec: the #GParamSpec for the property
896  * 
897  * Installs a child property on a container class. 
898  **/
899 void
900 gtk_container_class_install_child_property (GtkContainerClass *cclass,
901                                             guint              property_id,
902                                             GParamSpec        *pspec)
903 {
904   g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
905   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
906   if (pspec->flags & G_PARAM_WRITABLE)
907     g_return_if_fail (cclass->set_child_property != NULL);
908   if (pspec->flags & G_PARAM_READABLE)
909     g_return_if_fail (cclass->get_child_property != NULL);
910   g_return_if_fail (property_id > 0);
911   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
912   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
913     g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
914
915   if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
916     {
917       g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
918                  G_OBJECT_CLASS_NAME (cclass),
919                  pspec->name);
920       return;
921     }
922   g_param_spec_ref (pspec);
923   g_param_spec_sink (pspec);
924   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
925   g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
926 }
927
928 /**
929  * gtk_container_class_find_child_property:
930  * @cclass: a #GtkContainerClass
931  * @property_name: the name of the child property to find
932  * @returns: the #GParamSpec of the child property or %NULL if @class has no
933  *   child property with that name.
934  *
935  * Finds a child property of a container class by name.
936  */
937 GParamSpec*
938 gtk_container_class_find_child_property (GObjectClass *cclass,
939                                          const gchar  *property_name)
940 {
941   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
942   g_return_val_if_fail (property_name != NULL, NULL);
943
944   return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
945                                    property_name,
946                                    G_OBJECT_CLASS_TYPE (cclass),
947                                    TRUE);
948 }
949
950 /**
951  * gtk_container_class_list_child_properties:
952  * @cclass: a #GtkContainerClass
953  * @n_properties: location to return the number of child properties found
954  * @returns: a newly allocated %NULL-terminated array of #GParamSpec*. 
955  *           The array must be freed with g_free().
956  *
957  * Returns all child properties of a container class.
958  */
959 GParamSpec**
960 gtk_container_class_list_child_properties (GObjectClass *cclass,
961                                            guint        *n_properties)
962 {
963   GParamSpec **pspecs;
964   guint n;
965
966   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
967
968   pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
969                                    G_OBJECT_CLASS_TYPE (cclass),
970                                    &n);
971   if (n_properties)
972     *n_properties = n;
973
974   return pspecs;
975 }
976
977 static void
978 gtk_container_add_unimplemented (GtkContainer     *container,
979                                  GtkWidget        *widget)
980 {
981   g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
982 }
983
984 static void
985 gtk_container_remove_unimplemented (GtkContainer     *container,
986                                     GtkWidget        *widget)
987 {
988   g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
989 }
990
991 static void
992 gtk_container_init (GtkContainer *container)
993 {
994   container->focus_child = NULL;
995   container->border_width = 0;
996   container->need_resize = FALSE;
997   container->resize_mode = GTK_RESIZE_PARENT;
998   container->reallocate_redraws = FALSE;
999 }
1000
1001 static void
1002 gtk_container_destroy (GtkObject *object)
1003 {
1004   GtkContainer *container = GTK_CONTAINER (object);
1005   
1006   if (GTK_CONTAINER_RESIZE_PENDING (container))
1007     _gtk_container_dequeue_resize_handler (container);
1008
1009   /* do this before walking child widgets, to avoid
1010    * removing children from focus chain one by one.
1011    */
1012   if (container->has_focus_chain)
1013     gtk_container_unset_focus_chain (container);
1014   
1015   gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1016   
1017   if (GTK_OBJECT_CLASS (parent_class)->destroy)
1018     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
1019 }
1020
1021 static void
1022 gtk_container_set_property (GObject         *object,
1023                             guint            prop_id,
1024                             const GValue    *value,
1025                             GParamSpec      *pspec)
1026 {
1027   GtkContainer *container = GTK_CONTAINER (object);
1028
1029   switch (prop_id)
1030     {
1031     case PROP_BORDER_WIDTH:
1032       gtk_container_set_border_width (container, g_value_get_uint (value));
1033       break;
1034     case PROP_RESIZE_MODE:
1035       gtk_container_set_resize_mode (container, g_value_get_enum (value));
1036       break;
1037     case PROP_CHILD:
1038       gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1039       break;
1040     default:
1041       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1042       break;
1043     }
1044 }
1045
1046 static void
1047 gtk_container_get_property (GObject         *object,
1048                             guint            prop_id,
1049                             GValue          *value,
1050                             GParamSpec      *pspec)
1051 {
1052   GtkContainer *container = GTK_CONTAINER (object);
1053   
1054   switch (prop_id)
1055     {
1056     case PROP_BORDER_WIDTH:
1057       g_value_set_uint (value, container->border_width);
1058       break;
1059     case PROP_RESIZE_MODE:
1060       g_value_set_enum (value, container->resize_mode);
1061       break;
1062     default:
1063       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1064       break;
1065     }
1066 }
1067
1068 /**
1069  * gtk_container_set_border_width:
1070  * @container: a #GtkContainer
1071  * @border_width: amount of blank space to leave <emphasis>outside</emphasis> 
1072  *   the container. Valid values are in the range 0-65535 pixels.
1073  *
1074  * Sets the border width of the container.
1075  *
1076  * The border width of a container is the amount of space to leave
1077  * around the outside of the container. The only exception to this is
1078  * #GtkWindow; because toplevel windows can't leave space outside,
1079  * they leave the space inside. The border is added on all sides of
1080  * the container. To add space to only one side, one approach is to
1081  * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
1082  * it a size, and place it on the side of the container as a spacer.
1083  **/
1084 void
1085 gtk_container_set_border_width (GtkContainer *container,
1086                                 guint         border_width)
1087 {
1088   g_return_if_fail (GTK_IS_CONTAINER (container));
1089
1090   if (container->border_width != border_width)
1091     {
1092       container->border_width = border_width;
1093       g_object_notify (G_OBJECT (container), "border-width");
1094       
1095       if (GTK_WIDGET_REALIZED (container))
1096         gtk_widget_queue_resize (GTK_WIDGET (container));
1097     }
1098 }
1099
1100 /**
1101  * gtk_container_get_border_width:
1102  * @container: a #GtkContainer
1103  * 
1104  * Retrieves the border width of the container. See
1105  * gtk_container_set_border_width().
1106  *
1107  * Return value: the current border width
1108  **/
1109 guint
1110 gtk_container_get_border_width (GtkContainer *container)
1111 {
1112   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1113
1114   return container->border_width;
1115 }
1116
1117 /**
1118  * gtk_container_add:
1119  * @container: a #GtkContainer
1120  * @widget: a widget to be placed inside @container
1121  * 
1122  * Adds @widget to @container. Typically used for simple containers
1123  * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1124  * layout containers such as #GtkBox or #GtkTable, this function will
1125  * pick default packing parameters that may not be correct.  So
1126  * consider functions such as gtk_box_pack_start() and
1127  * gtk_table_attach() as an alternative to gtk_container_add() in
1128  * those cases. A widget may be added to only one container at a time;
1129  * you can't place the same widget inside two different containers.
1130  **/
1131 void
1132 gtk_container_add (GtkContainer *container,
1133                    GtkWidget    *widget)
1134 {
1135   g_return_if_fail (GTK_IS_CONTAINER (container));
1136   g_return_if_fail (GTK_IS_WIDGET (widget));
1137
1138   if (widget->parent != NULL)
1139     {
1140       g_warning ("Attempting to add a widget with type %s to a container of "
1141                  "type %s, but the widget is already inside a container of type %s, "
1142                  "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
1143                  g_type_name (G_OBJECT_TYPE (widget)),
1144                  g_type_name (G_OBJECT_TYPE (container)),
1145                  g_type_name (G_OBJECT_TYPE (widget->parent)));
1146       return;
1147     }
1148
1149   g_signal_emit (container, container_signals[ADD], 0, widget);
1150 }
1151
1152 /**
1153  * gtk_container_remove:
1154  * @container: a #GtkContainer
1155  * @widget: a current child of @container
1156  * 
1157  * Removes @widget from @container. @widget must be inside @container.
1158  * Note that @container will own a reference to @widget, and that this
1159  * may be the last reference held; so removing a widget from its
1160  * container can destroy that widget. If you want to use @widget
1161  * again, you need to add a reference to it while it's not inside
1162  * a container, using g_object_ref(). If you don't want to use @widget
1163  * again it's usually more efficient to simply destroy it directly
1164  * using gtk_widget_destroy() since this will remove it from the
1165  * container and help break any circular reference count cycles.
1166  **/
1167 void
1168 gtk_container_remove (GtkContainer *container,
1169                       GtkWidget    *widget)
1170 {
1171   g_return_if_fail (GTK_IS_CONTAINER (container));
1172   g_return_if_fail (GTK_IS_WIDGET (widget));
1173
1174   /* When using the deprecated API of the toolbar, it is possible
1175    * to legitimately call this function with a widget that is not
1176    * a direct child of the container.
1177    */
1178   g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1179                     widget->parent == GTK_WIDGET (container));
1180   
1181   g_signal_emit (container, container_signals[REMOVE], 0, widget);
1182 }
1183
1184 void
1185 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1186 {
1187   g_return_if_fail (GTK_IS_CONTAINER (container));
1188   g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1189
1190   container_resize_queue = g_slist_remove (container_resize_queue, container);
1191   GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1192 }
1193
1194 /**
1195  * gtk_container_set_resize_mode:
1196  * @container: a #GtkContainer
1197  * @resize_mode: the new resize mode
1198  * 
1199  * Sets the resize mode for the container.
1200  *
1201  * The resize mode of a container determines whether a resize request 
1202  * will be passed to the container's parent, queued for later execution
1203  * or executed immediately.
1204  **/
1205 void
1206 gtk_container_set_resize_mode (GtkContainer  *container,
1207                                GtkResizeMode  resize_mode)
1208 {
1209   g_return_if_fail (GTK_IS_CONTAINER (container));
1210   g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1211   
1212   if (GTK_WIDGET_TOPLEVEL (container) &&
1213       resize_mode == GTK_RESIZE_PARENT)
1214     {
1215       resize_mode = GTK_RESIZE_QUEUE;
1216     }
1217   
1218   if (container->resize_mode != resize_mode)
1219     {
1220       container->resize_mode = resize_mode;
1221       
1222       gtk_widget_queue_resize (GTK_WIDGET (container));
1223       g_object_notify (G_OBJECT (container), "resize-mode");
1224     }
1225 }
1226
1227 /**
1228  * gtk_container_get_resize_mode:
1229  * @container: a #GtkContainer
1230  * 
1231  * Returns the resize mode for the container. See
1232  * gtk_container_set_resize_mode ().
1233  *
1234  * Return value: the current resize mode
1235  **/
1236 GtkResizeMode
1237 gtk_container_get_resize_mode (GtkContainer *container)
1238 {
1239   g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1240
1241   return container->resize_mode;
1242 }
1243
1244 /**
1245  * gtk_container_set_reallocate_redraws:
1246  * @container: a #GtkContainer
1247  * @needs_redraws: the new value for the container's @reallocate_redraws flag
1248  *
1249  * Sets the @reallocate_redraws flag of the container to the given value.
1250  * 
1251  * Containers requesting reallocation redraws get automatically
1252  * redrawn if any of their children changed allocation. 
1253  **/ 
1254 void
1255 gtk_container_set_reallocate_redraws (GtkContainer *container,
1256                                       gboolean      needs_redraws)
1257 {
1258   g_return_if_fail (GTK_IS_CONTAINER (container));
1259
1260   container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1261 }
1262
1263 static GtkContainer*
1264 gtk_container_get_resize_container (GtkContainer *container)
1265 {
1266   GtkWidget *widget = GTK_WIDGET (container);
1267
1268   while (widget->parent)
1269     {
1270       widget = widget->parent;
1271       if (GTK_IS_RESIZE_CONTAINER (widget))
1272         break;
1273     }
1274
1275   return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1276 }
1277
1278 static gboolean
1279 gtk_container_idle_sizer (gpointer data)
1280 {
1281   /* we may be invoked with a container_resize_queue of NULL, because
1282    * queue_resize could have been adding an extra idle function while
1283    * the queue still got processed. we better just ignore such case
1284    * than trying to explicitely work around them with some extra flags,
1285    * since it doesn't cause any actual harm.
1286    */
1287   while (container_resize_queue)
1288     {
1289       GSList *slist;
1290       GtkWidget *widget;
1291
1292       slist = container_resize_queue;
1293       container_resize_queue = slist->next;
1294       widget = slist->data;
1295       g_slist_free_1 (slist);
1296
1297       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1298       gtk_container_check_resize (GTK_CONTAINER (widget));
1299     }
1300
1301   gdk_window_process_all_updates ();
1302
1303   return FALSE;
1304 }
1305
1306 void
1307 _gtk_container_queue_resize (GtkContainer *container)
1308 {
1309   GtkContainer *resize_container;
1310   GtkWidget *widget;
1311   
1312   g_return_if_fail (GTK_IS_CONTAINER (container));
1313
1314   widget = GTK_WIDGET (container);
1315   resize_container = gtk_container_get_resize_container (container);
1316   
1317   while (TRUE)
1318     {
1319       GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1320       GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1321       if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1322           !widget->parent)
1323         break;
1324       
1325       widget = widget->parent;
1326     }
1327       
1328   if (resize_container)
1329     {
1330       if (GTK_WIDGET_VISIBLE (resize_container) &&
1331           (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1332         {
1333           switch (resize_container->resize_mode)
1334             {
1335             case GTK_RESIZE_QUEUE:
1336               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1337                 {
1338                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1339                   if (container_resize_queue == NULL)
1340                     gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1341                                      gtk_container_idle_sizer,
1342                                      NULL, NULL);
1343                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1344                 }
1345               break;
1346
1347             case GTK_RESIZE_IMMEDIATE:
1348               gtk_container_check_resize (resize_container);
1349               break;
1350
1351             case GTK_RESIZE_PARENT:
1352               g_assert_not_reached ();
1353               break;
1354             }
1355         }
1356       else
1357         {
1358           /* we need to let hidden resize containers know that something
1359            * changed while they where hidden (currently only evaluated by
1360            * toplevels).
1361            */
1362           resize_container->need_resize = TRUE;
1363         }
1364     }
1365 }
1366
1367 void
1368 gtk_container_check_resize (GtkContainer *container)
1369 {
1370   g_return_if_fail (GTK_IS_CONTAINER (container));
1371   
1372   g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1373 }
1374
1375 static void
1376 gtk_container_real_check_resize (GtkContainer *container)
1377 {
1378   GtkWidget *widget = GTK_WIDGET (container);
1379   GtkRequisition requisition;
1380   
1381   gtk_widget_size_request (widget, &requisition);
1382   
1383   if (requisition.width > widget->allocation.width ||
1384       requisition.height > widget->allocation.height)
1385     {
1386       if (GTK_IS_RESIZE_CONTAINER (container))
1387         gtk_widget_size_allocate (GTK_WIDGET (container),
1388                                   &GTK_WIDGET (container)->allocation);
1389       else
1390         gtk_widget_queue_resize (widget);
1391     }
1392   else
1393     {
1394       gtk_container_resize_children (container);
1395     }
1396 }
1397
1398 /* The container hasn't changed size but one of its children
1399  *  queued a resize request. Which means that the allocation
1400  *  is not sufficient for the requisition of some child.
1401  *  We've already performed a size request at this point,
1402  *  so we simply need to reallocate and let the allocation
1403  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1404  */
1405 void
1406 gtk_container_resize_children (GtkContainer *container)
1407 {
1408   GtkWidget *widget;
1409   
1410   /* resizing invariants:
1411    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1412    * containers that have an idle sizer pending must be flagged with
1413    * RESIZE_PENDING.
1414    */
1415   g_return_if_fail (GTK_IS_CONTAINER (container));
1416
1417   widget = GTK_WIDGET (container);
1418   gtk_widget_size_allocate (widget, &widget->allocation);
1419 }
1420
1421 /**
1422  * gtk_container_forall:
1423  * @container: a #GtkContainer
1424  * @callback: a callback
1425  * @callback_data: callback user data
1426  * 
1427  * Invokes @callback on each child of @container, including children
1428  * that are considered "internal" (implementation details of the
1429  * container). "Internal" children generally weren't added by the user
1430  * of the container, but were added by the container implementation
1431  * itself.  Most applications should use gtk_container_foreach(),
1432  * rather than gtk_container_forall().
1433  **/
1434 void
1435 gtk_container_forall (GtkContainer *container,
1436                       GtkCallback   callback,
1437                       gpointer      callback_data)
1438 {
1439   GtkContainerClass *class;
1440
1441   g_return_if_fail (GTK_IS_CONTAINER (container));
1442   g_return_if_fail (callback != NULL);
1443
1444   class = GTK_CONTAINER_GET_CLASS (container);
1445
1446   if (class->forall)
1447     class->forall (container, TRUE, callback, callback_data);
1448 }
1449
1450 /**
1451  * gtk_container_foreach:
1452  * @container: a #GtkContainer
1453  * @callback: a callback
1454  * @callback_data: callback user data
1455  * 
1456  * Invokes @callback on each non-internal child of @container. See
1457  * gtk_container_forall() for details on what constitutes an
1458  * "internal" child.  Most applications should use
1459  * gtk_container_foreach(), rather than gtk_container_forall().
1460  **/
1461 void
1462 gtk_container_foreach (GtkContainer *container,
1463                        GtkCallback   callback,
1464                        gpointer      callback_data)
1465 {
1466   GtkContainerClass *class;
1467   
1468   g_return_if_fail (GTK_IS_CONTAINER (container));
1469   g_return_if_fail (callback != NULL);
1470
1471   class = GTK_CONTAINER_GET_CLASS (container);
1472
1473   if (class->forall)
1474     class->forall (container, FALSE, callback, callback_data);
1475 }
1476
1477 typedef struct _GtkForeachData  GtkForeachData;
1478 struct _GtkForeachData
1479 {
1480   GtkObject         *container;
1481   GtkCallbackMarshal callback;
1482   gpointer           callback_data;
1483 };
1484
1485 static void
1486 gtk_container_foreach_unmarshal (GtkWidget *child,
1487                                  gpointer data)
1488 {
1489   GtkForeachData *fdata = (GtkForeachData*) data;
1490   GtkArg args[2];
1491   
1492   /* first argument */
1493   args[0].name = NULL;
1494   args[0].type = G_TYPE_FROM_INSTANCE (child);
1495   GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1496   
1497   /* location for return value */
1498   args[1].name = NULL;
1499   args[1].type = G_TYPE_NONE;
1500   
1501   fdata->callback (fdata->container, fdata->callback_data, 1, args);
1502 }
1503
1504 void
1505 gtk_container_foreach_full (GtkContainer       *container,
1506                             GtkCallback         callback,
1507                             GtkCallbackMarshal  marshal,
1508                             gpointer            callback_data,
1509                             GtkDestroyNotify    notify)
1510 {
1511   g_return_if_fail (GTK_IS_CONTAINER (container));
1512
1513   if (marshal)
1514     {
1515       GtkForeachData fdata;
1516   
1517       fdata.container     = GTK_OBJECT (container);
1518       fdata.callback      = marshal;
1519       fdata.callback_data = callback_data;
1520
1521       gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1522     }
1523   else
1524     {
1525       g_return_if_fail (callback != NULL);
1526
1527       gtk_container_foreach (container, callback, &callback_data);
1528     }
1529
1530   if (notify)
1531     notify (callback_data);
1532 }
1533
1534 void
1535 gtk_container_set_focus_child (GtkContainer *container,
1536                                GtkWidget    *widget)
1537 {
1538   g_return_if_fail (GTK_IS_CONTAINER (container));
1539   if (widget)
1540     g_return_if_fail (GTK_IS_WIDGET (widget));
1541
1542   g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1543 }
1544
1545 /**
1546  * gtk_container_get_children:
1547  * @container: a #GtkContainer
1548  * 
1549  * Returns the container's non-internal children. See
1550  * gtk_container_forall() for details on what constitutes an "internal" child. 
1551  *
1552  * Return value: a newly-allocated list of the container's non-internal children.
1553  **/
1554 GList*
1555 gtk_container_get_children (GtkContainer *container)
1556 {
1557   GList *children = NULL;
1558
1559   gtk_container_foreach (container,
1560                          gtk_container_children_callback,
1561                          &children);
1562
1563   return g_list_reverse (children);
1564 }
1565
1566 static void
1567 gtk_container_child_position_callback (GtkWidget *widget,
1568                                        gpointer   client_data)
1569 {
1570   struct {
1571     GtkWidget *child;
1572     guint i;
1573     guint index;
1574   } *data = client_data;
1575
1576   data->i++;
1577   if (data->child == widget)
1578     data->index = data->i;
1579 }
1580
1581 static gchar*
1582 gtk_container_child_default_composite_name (GtkContainer *container,
1583                                             GtkWidget    *child)
1584 {
1585   struct {
1586     GtkWidget *child;
1587     guint i;
1588     guint index;
1589   } data;
1590   gchar *name;
1591
1592   /* fallback implementation */
1593   data.child = child;
1594   data.i = 0;
1595   data.index = 0;
1596   gtk_container_forall (container,
1597                         gtk_container_child_position_callback,
1598                         &data);
1599   
1600   name = g_strdup_printf ("%s-%u",
1601                           g_type_name (G_TYPE_FROM_INSTANCE (child)),
1602                           data.index);
1603
1604   return name;
1605 }
1606
1607 gchar*
1608 _gtk_container_child_composite_name (GtkContainer *container,
1609                                     GtkWidget    *child)
1610 {
1611   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1612   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1613   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1614
1615   if (GTK_WIDGET_COMPOSITE_CHILD (child))
1616     {
1617       static GQuark quark_composite_name = 0;
1618       gchar *name;
1619
1620       if (!quark_composite_name)
1621         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1622
1623       name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1624       if (!name)
1625         {
1626           GtkContainerClass *class;
1627
1628           class = GTK_CONTAINER_GET_CLASS (container);
1629           if (class->composite_name)
1630             name = class->composite_name (container, child);
1631         }
1632       else
1633         name = g_strdup (name);
1634
1635       return name;
1636     }
1637   
1638   return NULL;
1639 }
1640
1641 static void
1642 gtk_container_real_set_focus_child (GtkContainer     *container,
1643                                     GtkWidget        *child)
1644 {
1645   g_return_if_fail (GTK_IS_CONTAINER (container));
1646   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1647
1648   if (child != container->focus_child)
1649     {
1650       if (container->focus_child)
1651         g_object_unref (container->focus_child);
1652       container->focus_child = child;
1653       if (container->focus_child)
1654         g_object_ref (container->focus_child);
1655     }
1656
1657
1658   /* check for h/v adjustments
1659    */
1660   if (container->focus_child)
1661     {
1662       GtkAdjustment *hadj;
1663       GtkAdjustment *vadj;
1664       GtkWidget *focus_child;
1665       gint x, y;
1666
1667       hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);   
1668       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1669       if (hadj || vadj) 
1670         {
1671
1672           focus_child = container->focus_child;
1673           while (GTK_IS_CONTAINER (focus_child) && 
1674                  GTK_CONTAINER (focus_child)->focus_child)
1675             {
1676               focus_child = GTK_CONTAINER (focus_child)->focus_child;
1677             }
1678           
1679           gtk_widget_translate_coordinates (focus_child, container->focus_child, 
1680                                             0, 0, &x, &y);
1681
1682            x += container->focus_child->allocation.x;
1683            y += container->focus_child->allocation.y;
1684           
1685           if (vadj)
1686             gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1687           
1688           if (hadj)
1689             gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1690         }
1691     }
1692 }
1693
1694 static GList*
1695 get_focus_chain (GtkContainer *container)
1696 {
1697   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1698 }
1699
1700 /* same as gtk_container_get_children, except it includes internals
1701  */
1702 static GList *
1703 gtk_container_get_all_children (GtkContainer *container)
1704 {
1705   GList *children = NULL;
1706
1707   gtk_container_forall (container,
1708                          gtk_container_children_callback,
1709                          &children);
1710
1711   return children;
1712 }
1713
1714 static gboolean
1715 gtk_container_focus (GtkWidget        *widget,
1716                      GtkDirectionType  direction)
1717 {
1718   GList *children;
1719   GList *sorted_children;
1720   gint return_val;
1721   GtkContainer *container;
1722
1723   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1724
1725   container = GTK_CONTAINER (widget);
1726
1727   return_val = FALSE;
1728
1729   if (GTK_WIDGET_CAN_FOCUS (container))
1730     {
1731       if (!GTK_WIDGET_HAS_FOCUS (container))
1732         {
1733           gtk_widget_grab_focus (GTK_WIDGET (container));
1734           return_val = TRUE;
1735         }
1736     }
1737   else
1738     {
1739       /* Get a list of the containers children, allowing focus
1740        * chain to override.
1741        */
1742       if (container->has_focus_chain)
1743         children = g_list_copy (get_focus_chain (container));
1744       else
1745         children = gtk_container_get_all_children (container);
1746
1747       if (container->has_focus_chain &&
1748           (direction == GTK_DIR_TAB_FORWARD ||
1749            direction == GTK_DIR_TAB_BACKWARD))
1750         {
1751           sorted_children = g_list_copy (children);
1752           
1753           if (direction == GTK_DIR_TAB_BACKWARD)
1754             sorted_children = g_list_reverse (sorted_children);
1755         }
1756       else
1757         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1758       
1759       return_val = gtk_container_focus_move (container, sorted_children, direction);
1760
1761       g_list_free (sorted_children);
1762       g_list_free (children);
1763     }
1764
1765   return return_val;
1766 }
1767
1768 static gint
1769 tab_compare (gconstpointer a,
1770              gconstpointer b,
1771              gpointer      data)
1772 {
1773   const GtkWidget *child1 = a;
1774   const GtkWidget *child2 = b;
1775   GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1776
1777   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1778   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1779
1780   if (y1 == y2)
1781     {
1782       gint x1 = child1->allocation.x + child1->allocation.width / 2;
1783       gint x2 = child2->allocation.x + child2->allocation.width / 2;
1784       
1785       if (text_direction == GTK_TEXT_DIR_RTL) 
1786         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1787       else
1788         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1789     }
1790   else
1791     return (y1 < y2) ? -1 : 1;
1792 }
1793
1794 static GList *
1795 gtk_container_focus_sort_tab (GtkContainer     *container,
1796                               GList            *children,
1797                               GtkDirectionType  direction,
1798                               GtkWidget        *old_focus)
1799 {
1800   GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1801   children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1802
1803   /* if we are going backwards then reverse the order
1804    *  of the children.
1805    */
1806   if (direction == GTK_DIR_TAB_BACKWARD)
1807     children = g_list_reverse (children);
1808
1809   return children;
1810 }
1811
1812 /* Get coordinates of @widget's allocation with respect to
1813  * allocation of @container.
1814  */
1815 static gboolean
1816 get_allocation_coords (GtkContainer  *container,
1817                        GtkWidget     *widget,
1818                        GdkRectangle  *allocation)
1819 {
1820   *allocation = widget->allocation;
1821
1822   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1823                                            0, 0, &allocation->x, &allocation->y);
1824 }
1825
1826 /* Look for a child in @children that is intermediate between
1827  * the focus widget and container. This widget, if it exists,
1828  * acts as the starting widget for focus navigation.
1829  */
1830 static GtkWidget *
1831 find_old_focus (GtkContainer *container,
1832                 GList        *children)
1833 {
1834   GList *tmp_list = children;
1835   while (tmp_list)
1836     {
1837       GtkWidget *child = tmp_list->data;
1838       GtkWidget *widget = child;
1839
1840       while (widget && widget != (GtkWidget *)container)
1841         {
1842           GtkWidget *parent = widget->parent;
1843           if (parent && ((GtkContainer *)parent)->focus_child != widget)
1844             goto next;
1845
1846           widget = parent;
1847         }
1848
1849       return child;
1850
1851     next:
1852       tmp_list = tmp_list->next;
1853     }
1854
1855   return NULL;
1856 }
1857
1858 static gboolean
1859 old_focus_coords (GtkContainer *container,
1860                   GdkRectangle *old_focus_rect)
1861 {
1862   GtkWidget *widget = GTK_WIDGET (container);
1863   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1864   
1865   if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1866     {
1867       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1868       
1869       return get_allocation_coords (container, old_focus, old_focus_rect);
1870     }
1871   else
1872     return FALSE;
1873 }
1874
1875 typedef struct _CompareInfo CompareInfo;
1876
1877 struct _CompareInfo
1878 {
1879   GtkContainer *container;
1880   gint x;
1881   gint y;
1882   gboolean reverse;
1883 };
1884
1885 static gint
1886 up_down_compare (gconstpointer a,
1887                  gconstpointer b,
1888                  gpointer      data)
1889 {
1890   GdkRectangle allocation1;
1891   GdkRectangle allocation2;
1892   CompareInfo *compare = data;
1893   gint y1, y2;
1894
1895   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1896   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1897
1898   y1 = allocation1.y + allocation1.height / 2;
1899   y2 = allocation2.y + allocation2.height / 2;
1900
1901   if (y1 == y2)
1902     {
1903       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1904       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1905
1906       if (compare->reverse)
1907         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1908       else
1909         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1910     }
1911   else
1912     return (y1 < y2) ? -1 : 1;
1913 }
1914
1915 static GList *
1916 gtk_container_focus_sort_up_down (GtkContainer     *container,
1917                                   GList            *children,
1918                                   GtkDirectionType  direction,
1919                                   GtkWidget        *old_focus)
1920 {
1921   CompareInfo compare;
1922   GList *tmp_list;
1923   GdkRectangle old_allocation;
1924
1925   compare.container = container;
1926   compare.reverse = (direction == GTK_DIR_UP);
1927
1928   if (!old_focus)
1929       old_focus = find_old_focus (container, children);
1930   
1931   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1932     {
1933       gint compare_x1;
1934       gint compare_x2;
1935       gint compare_y;
1936
1937       /* Delete widgets from list that don't match minimum criteria */
1938
1939       compare_x1 = old_allocation.x;
1940       compare_x2 = old_allocation.x + old_allocation.width;
1941
1942       if (direction == GTK_DIR_UP)
1943         compare_y = old_allocation.y;
1944       else
1945         compare_y = old_allocation.y + old_allocation.height;
1946       
1947       tmp_list = children;
1948       while (tmp_list)
1949         {
1950           GtkWidget *child = tmp_list->data;
1951           GList *next = tmp_list->next;
1952           gint child_x1, child_x2;
1953           GdkRectangle child_allocation;
1954           
1955           if (child != old_focus)
1956             {
1957               if (get_allocation_coords (container, child, &child_allocation))
1958                 {
1959                   child_x1 = child_allocation.x;
1960                   child_x2 = child_allocation.x + child_allocation.width;
1961                   
1962                   if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1963                       (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1964                       (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1965                     {
1966                       children = g_list_delete_link (children, tmp_list);
1967                     }
1968                 }
1969               else
1970                 children = g_list_delete_link (children, tmp_list);
1971             }
1972           
1973           tmp_list = next;
1974         }
1975
1976       compare.x = (compare_x1 + compare_x2) / 2;
1977       compare.y = old_allocation.y + old_allocation.height / 2;
1978     }
1979   else
1980     {
1981       /* No old focus widget, need to figure out starting x,y some other way
1982        */
1983       GtkWidget *widget = GTK_WIDGET (container);
1984       GdkRectangle old_focus_rect;
1985
1986       if (old_focus_coords (container, &old_focus_rect))
1987         {
1988           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1989         }
1990       else
1991         {
1992           if (GTK_WIDGET_NO_WINDOW (widget))
1993             compare.x = widget->allocation.x + widget->allocation.width / 2;
1994           else
1995             compare.x = widget->allocation.width / 2;
1996         }
1997       
1998       if (GTK_WIDGET_NO_WINDOW (widget))
1999         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2000       else
2001         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2002     }
2003
2004   children = g_list_sort_with_data (children, up_down_compare, &compare);
2005
2006   if (compare.reverse)
2007     children = g_list_reverse (children);
2008
2009   return children;
2010 }
2011
2012 static gint
2013 left_right_compare (gconstpointer a,
2014                     gconstpointer b,
2015                     gpointer      data)
2016 {
2017   GdkRectangle allocation1;
2018   GdkRectangle allocation2;
2019   CompareInfo *compare = data;
2020   gint x1, x2;
2021
2022   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2023   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2024
2025   x1 = allocation1.x + allocation1.width / 2;
2026   x2 = allocation2.x + allocation2.width / 2;
2027
2028   if (x1 == x2)
2029     {
2030       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2031       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2032
2033       if (compare->reverse)
2034         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2035       else
2036         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2037     }
2038   else
2039     return (x1 < x2) ? -1 : 1;
2040 }
2041
2042 static GList *
2043 gtk_container_focus_sort_left_right (GtkContainer     *container,
2044                                      GList            *children,
2045                                      GtkDirectionType  direction,
2046                                      GtkWidget        *old_focus)
2047 {
2048   CompareInfo compare;
2049   GList *tmp_list;
2050   GdkRectangle old_allocation;
2051
2052   compare.container = container;
2053   compare.reverse = (direction == GTK_DIR_LEFT);
2054
2055   if (!old_focus)
2056     old_focus = find_old_focus (container, children);
2057   
2058   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2059     {
2060       gint compare_y1;
2061       gint compare_y2;
2062       gint compare_x;
2063       
2064       /* Delete widgets from list that don't match minimum criteria */
2065
2066       compare_y1 = old_allocation.y;
2067       compare_y2 = old_allocation.y + old_allocation.height;
2068
2069       if (direction == GTK_DIR_LEFT)
2070         compare_x = old_allocation.x;
2071       else
2072         compare_x = old_allocation.x + old_allocation.width;
2073       
2074       tmp_list = children;
2075       while (tmp_list)
2076         {
2077           GtkWidget *child = tmp_list->data;
2078           GList *next = tmp_list->next;
2079           gint child_y1, child_y2;
2080           GdkRectangle child_allocation;
2081           
2082           if (child != old_focus)
2083             {
2084               if (get_allocation_coords (container, child, &child_allocation))
2085                 {
2086                   child_y1 = child_allocation.y;
2087                   child_y2 = child_allocation.y + child_allocation.height;
2088                   
2089                   if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2090                       (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2091                       (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2092                     {
2093                       children = g_list_delete_link (children, tmp_list);
2094                     }
2095                 }
2096               else
2097                 children = g_list_delete_link (children, tmp_list);
2098             }
2099           
2100           tmp_list = next;
2101         }
2102
2103       compare.y = (compare_y1 + compare_y2) / 2;
2104       compare.x = old_allocation.x + old_allocation.width / 2;
2105     }
2106   else
2107     {
2108       /* No old focus widget, need to figure out starting x,y some other way
2109        */
2110       GtkWidget *widget = GTK_WIDGET (container);
2111       GdkRectangle old_focus_rect;
2112
2113       if (old_focus_coords (container, &old_focus_rect))
2114         {
2115           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2116         }
2117       else
2118         {
2119           if (GTK_WIDGET_NO_WINDOW (widget))
2120             compare.y = widget->allocation.y + widget->allocation.height / 2;
2121           else
2122             compare.y = widget->allocation.height / 2;
2123         }
2124       
2125       if (GTK_WIDGET_NO_WINDOW (widget))
2126         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2127       else
2128         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2129     }
2130
2131   children = g_list_sort_with_data (children, left_right_compare, &compare);
2132
2133   if (compare.reverse)
2134     children = g_list_reverse (children);
2135
2136   return children;
2137 }
2138
2139 /**
2140  * gtk_container_focus_sort:
2141  * @container: a #GtkContainer
2142  * @children:  a list of descendents of @container (they don't
2143  *             have to be direct children)
2144  * @direction: focus direction
2145  * @old_focus: widget to use for the starting position, or %NULL
2146  *             to determine this automatically.
2147  *             (Note, this argument isn't used for GTK_DIR_TAB_*,
2148  *              which is the only @direction we use currently,
2149  *              so perhaps this argument should be removed)
2150  * 
2151  * Sorts @children in the correct order for focusing with
2152  * direction type @direction.
2153  * 
2154  * Return value: a copy of @children, sorted in correct focusing order,
2155  *   with children that aren't suitable for focusing in this direction
2156  *   removed.
2157  **/
2158 GList *
2159 _gtk_container_focus_sort (GtkContainer     *container,
2160                            GList            *children,
2161                            GtkDirectionType  direction,
2162                            GtkWidget        *old_focus)
2163 {
2164   GList *visible_children = NULL;
2165
2166   while (children)
2167     {
2168       if (GTK_WIDGET_REALIZED (children->data))
2169         visible_children = g_list_prepend (visible_children, children->data);
2170       children = children->next;
2171     }
2172   
2173   switch (direction)
2174     {
2175     case GTK_DIR_TAB_FORWARD:
2176     case GTK_DIR_TAB_BACKWARD:
2177       return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2178     case GTK_DIR_UP:
2179     case GTK_DIR_DOWN:
2180       return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2181     case GTK_DIR_LEFT:
2182     case GTK_DIR_RIGHT:
2183       return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2184     }
2185
2186   g_assert_not_reached ();
2187
2188   return NULL;
2189 }
2190
2191 static gboolean
2192 gtk_container_focus_move (GtkContainer     *container,
2193                           GList            *children,
2194                           GtkDirectionType  direction)
2195 {
2196   GtkWidget *focus_child;
2197   GtkWidget *child;
2198
2199   focus_child = container->focus_child;
2200
2201   while (children)
2202     {
2203       child = children->data;
2204       children = children->next;
2205
2206       if (!child)
2207         continue;
2208       
2209       if (focus_child)
2210         {
2211           if (focus_child == child)
2212             {
2213               focus_child = NULL;
2214
2215                 if (gtk_widget_child_focus (child, direction))
2216                   return TRUE;
2217             }
2218         }
2219       else if (GTK_WIDGET_DRAWABLE (child) &&
2220                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2221         {
2222           if (gtk_widget_child_focus (child, direction))
2223             return TRUE;
2224         }
2225     }
2226
2227   return FALSE;
2228 }
2229
2230
2231 static void
2232 gtk_container_children_callback (GtkWidget *widget,
2233                                  gpointer   client_data)
2234 {
2235   GList **children;
2236
2237   children = (GList**) client_data;
2238   *children = g_list_prepend (*children, widget);
2239 }
2240
2241 static void
2242 chain_widget_destroyed (GtkWidget *widget,
2243                         gpointer   user_data)
2244 {
2245   GtkContainer *container;
2246   GList *chain;
2247   
2248   container = GTK_CONTAINER (user_data);
2249
2250   chain = g_object_get_data (G_OBJECT (container),
2251                              "gtk-container-focus-chain");
2252
2253   chain = g_list_remove (chain, widget);
2254
2255   g_signal_handlers_disconnect_by_func (widget,
2256                                         chain_widget_destroyed,
2257                                         user_data);
2258   
2259   g_object_set_data (G_OBJECT (container),
2260                      I_("gtk-container-focus-chain"),
2261                      chain);  
2262 }
2263
2264 /**
2265  * gtk_container_set_focus_chain: 
2266  * @container: a #GtkContainer
2267  * @focusable_widgets: the new focus chain
2268  *
2269  * Sets a focus chain, overriding the one computed automatically by GTK+.
2270  * 
2271  * In principle each widget in the chain should be a descendant of the 
2272  * container, but this is not enforced by this method, since it's allowed 
2273  * to set the focus chain before you pack the widgets, or have a widget 
2274  * in the chain that isn't always packed. The necessary checks are done 
2275  * when the focus chain is actually traversed.
2276  **/
2277 void
2278 gtk_container_set_focus_chain (GtkContainer *container,
2279                                GList        *focusable_widgets)
2280 {
2281   GList *chain;
2282   GList *tmp_list;
2283   
2284   g_return_if_fail (GTK_IS_CONTAINER (container));
2285   
2286   if (container->has_focus_chain)
2287     gtk_container_unset_focus_chain (container);
2288
2289   container->has_focus_chain = TRUE;
2290   
2291   chain = NULL;
2292   tmp_list = focusable_widgets;
2293   while (tmp_list != NULL)
2294     {
2295       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2296       
2297       /* In principle each widget in the chain should be a descendant
2298        * of the container, but we don't want to check that here, it's
2299        * expensive and also it's allowed to set the focus chain before
2300        * you pack the widgets, or have a widget in the chain that isn't
2301        * always packed. So we check for ancestor during actual traversal.
2302        */
2303
2304       chain = g_list_prepend (chain, tmp_list->data);
2305
2306       g_signal_connect (tmp_list->data,
2307                         "destroy",
2308                         G_CALLBACK (chain_widget_destroyed),
2309                         container);
2310       
2311       tmp_list = g_list_next (tmp_list);
2312     }
2313
2314   chain = g_list_reverse (chain);
2315   
2316   g_object_set_data (G_OBJECT (container),
2317                      I_("gtk-container-focus-chain"),
2318                      chain);
2319 }
2320
2321 /**
2322  * gtk_container_get_focus_chain:
2323  * @container:         a #GtkContainer
2324  * @focusable_widgets: location to store the focus chain of the
2325  *                     container, or %NULL. You should free this list
2326  *                     using g_list_free() when you are done with it, however
2327  *                     no additional reference count is added to the
2328  *                     individual widgets in the focus chain.
2329  * 
2330  * Retrieves the focus chain of the container, if one has been
2331  * set explicitly. If no focus chain has been explicitly
2332  * set, GTK+ computes the focus chain based on the positions
2333  * of the children. In that case, GTK+ stores %NULL in
2334  * @focusable_widgets and returns %FALSE.
2335  *
2336  * Return value: %TRUE if the focus chain of the container 
2337  * has been set explicitly.
2338  **/
2339 gboolean
2340 gtk_container_get_focus_chain (GtkContainer *container,
2341                                GList       **focus_chain)
2342 {
2343   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2344
2345   if (focus_chain)
2346     {
2347       if (container->has_focus_chain)
2348         *focus_chain = g_list_copy (get_focus_chain (container));
2349       else
2350         *focus_chain = NULL;
2351     }
2352
2353   return container->has_focus_chain;
2354 }
2355
2356 /**
2357  * gtk_container_unset_focus_chain:
2358  * @container: a #GtkContainer
2359  * 
2360  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2361  **/
2362 void
2363 gtk_container_unset_focus_chain (GtkContainer  *container)
2364 {  
2365   g_return_if_fail (GTK_IS_CONTAINER (container));
2366
2367   if (container->has_focus_chain)
2368     {
2369       GList *chain;
2370       GList *tmp_list;
2371       
2372       chain = get_focus_chain (container);
2373       
2374       container->has_focus_chain = FALSE;
2375       
2376       g_object_set_data (G_OBJECT (container), 
2377                          I_("gtk-container-focus-chain"),
2378                          NULL);
2379
2380       tmp_list = chain;
2381       while (tmp_list != NULL)
2382         {
2383           g_signal_handlers_disconnect_by_func (tmp_list->data,
2384                                                 chain_widget_destroyed,
2385                                                 container);
2386           
2387           tmp_list = g_list_next (tmp_list);
2388         }
2389
2390       g_list_free (chain);
2391     }
2392 }
2393
2394 /**
2395  * gtk_container_set_focus_vadjustment:
2396  * @container: a #GtkContainer
2397  * @adjustment: an adjustment which should be adjusted when the focus 
2398  *   is moved among the descendents of @container
2399  * 
2400  * Hooks up an adjustment to focus handling in a container, so when a 
2401  * child of the container is focused, the adjustment is scrolled to 
2402  * show that widget. This function sets the vertical alignment. See 
2403  * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining 
2404  * the adjustment and gtk_container_set_focus_hadjustment() for setting
2405  * the horizontal adjustment.
2406  *
2407  * The adjustments have to be in pixel units and in the same coordinate 
2408  * system as the allocation for immediate children of the container. 
2409  */
2410 void
2411 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2412                                      GtkAdjustment *adjustment)
2413 {
2414   g_return_if_fail (GTK_IS_CONTAINER (container));
2415   if (adjustment)
2416     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2417
2418   if (adjustment)
2419     g_object_ref (adjustment);
2420
2421   g_object_set_qdata_full (G_OBJECT (container),
2422                            vadjustment_key_id,
2423                            adjustment,
2424                            g_object_unref);
2425 }
2426
2427 /**
2428  * gtk_container_get_focus_vadjustment:
2429  * @container: a #GtkContainer
2430  *
2431  * Retrieves the vertical focus adjustment for the container. See
2432  * gtk_container_set_focus_vadjustment().
2433  *
2434  * Return value: the vertical focus adjustment, or %NULL if
2435  *   none has been set.
2436  **/
2437 GtkAdjustment *
2438 gtk_container_get_focus_vadjustment (GtkContainer *container)
2439 {
2440   GtkAdjustment *vadjustment;
2441     
2442   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2443
2444   vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2445
2446   return vadjustment;
2447 }
2448
2449 /**
2450  * gtk_container_set_focus_hadjustment:
2451  * @container: a #GtkContainer
2452  * @adjustment: an adjustment which should be adjusted when the focus is 
2453  *   moved among the descendents of @container
2454  * 
2455  * Hooks up an adjustment to focus handling in a container, so when a child 
2456  * of the container is focused, the adjustment is scrolled to show that 
2457  * widget. This function sets the horizontal alignment. See gt
2458  * k_scrolled_window_get_hadjustment() for a typical way of obtaining the 
2459  * adjustment and gtk_container_set_focus_vadjustment() for setting
2460  * the vertical adjustment.
2461  *
2462  * The adjustments have to be in pixel units and in the same coordinate 
2463  * system as the allocation for immediate children of the container. 
2464  */
2465 void
2466 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2467                                      GtkAdjustment *adjustment)
2468 {
2469   g_return_if_fail (GTK_IS_CONTAINER (container));
2470   if (adjustment)
2471     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2472
2473   if (adjustment)
2474     g_object_ref (adjustment);
2475
2476   g_object_set_qdata_full (G_OBJECT (container),
2477                            hadjustment_key_id,
2478                            adjustment,
2479                            g_object_unref);
2480 }
2481
2482 /**
2483  * gtk_container_get_focus_hadjustment:
2484  * @container: a #GtkContainer
2485  *
2486  * Retrieves the horizontal focus adjustment for the container. See
2487  * gtk_container_set_focus_hadjustment ().
2488  *
2489  * Return value: the horizontal focus adjustment, or %NULL if
2490  *   none has been set.
2491  **/
2492 GtkAdjustment *
2493 gtk_container_get_focus_hadjustment (GtkContainer *container)
2494 {
2495   GtkAdjustment *hadjustment;
2496
2497   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2498
2499   hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2500
2501   return hadjustment;
2502 }
2503
2504
2505 static void
2506 gtk_container_show_all (GtkWidget *widget)
2507 {
2508   g_return_if_fail (GTK_IS_CONTAINER (widget));
2509
2510   gtk_container_foreach (GTK_CONTAINER (widget),
2511                          (GtkCallback) gtk_widget_show_all,
2512                          NULL);
2513   gtk_widget_show (widget);
2514 }
2515
2516 static void
2517 gtk_container_hide_all (GtkWidget *widget)
2518 {
2519   g_return_if_fail (GTK_IS_CONTAINER (widget));
2520
2521   gtk_widget_hide (widget);
2522   gtk_container_foreach (GTK_CONTAINER (widget),
2523                          (GtkCallback) gtk_widget_hide_all,
2524                          NULL);
2525 }
2526
2527
2528 static void
2529 gtk_container_expose_child (GtkWidget *child,
2530                             gpointer   client_data)
2531 {
2532   struct {
2533     GtkWidget *container;
2534     GdkEventExpose *event;
2535   } *data = client_data;
2536   
2537   gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2538                                   child,
2539                                   data->event);
2540 }
2541
2542 static gint 
2543 gtk_container_expose (GtkWidget      *widget,
2544                       GdkEventExpose *event)
2545 {
2546   struct {
2547     GtkWidget *container;
2548     GdkEventExpose *event;
2549   } data;
2550
2551   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2552   g_return_val_if_fail (event != NULL, FALSE);
2553
2554   
2555   if (GTK_WIDGET_DRAWABLE (widget)) 
2556     {
2557       data.container = widget;
2558       data.event = event;
2559       
2560       gtk_container_forall (GTK_CONTAINER (widget),
2561                             gtk_container_expose_child,
2562                             &data);
2563     }   
2564   
2565   return FALSE;
2566 }
2567
2568 static void
2569 gtk_container_map_child (GtkWidget *child,
2570                          gpointer   client_data)
2571 {
2572   if (GTK_WIDGET_VISIBLE (child) &&
2573       GTK_WIDGET_CHILD_VISIBLE (child) &&
2574       !GTK_WIDGET_MAPPED (child))
2575     gtk_widget_map (child);
2576 }
2577
2578 static void
2579 gtk_container_map (GtkWidget *widget)
2580 {
2581   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2582
2583   gtk_container_forall (GTK_CONTAINER (widget),
2584                         gtk_container_map_child,
2585                         NULL);
2586
2587   if (!GTK_WIDGET_NO_WINDOW (widget))
2588     gdk_window_show (widget->window);
2589 }
2590
2591 static void
2592 gtk_container_unmap (GtkWidget *widget)
2593 {
2594   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2595
2596   if (!GTK_WIDGET_NO_WINDOW (widget))
2597     gdk_window_hide (widget->window);
2598   else
2599     gtk_container_forall (GTK_CONTAINER (widget),
2600                           (GtkCallback)gtk_widget_unmap,
2601                           NULL);
2602 }
2603
2604 /**
2605  * gtk_container_propagate_expose:
2606  * @container: a #GtkContainer
2607  * @child: a child of @container
2608  * @event: a expose event sent to container
2609  *
2610  * When a container receives an expose event, it must send synthetic
2611  * expose events to all children that don't have their own #GdkWindows.
2612  * This function provides a convenient way of doing this. A container,
2613  * when it receives an expose event, calls gtk_container_propagate_expose() 
2614  * once for each child, passing in the event the container received.
2615  *
2616  * gtk_container_propagate_expose() takes care of deciding whether
2617  * an expose event needs to be sent to the child, intersecting
2618  * the event's area with the child area, and sending the event.
2619  * 
2620  * In most cases, a container can simply either simply inherit the
2621  * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing 
2622  * and then chain to the ::expose implementation from #GtkContainer.
2623  **/
2624 void
2625 gtk_container_propagate_expose (GtkContainer   *container,
2626                                 GtkWidget      *child,
2627                                 GdkEventExpose *event)
2628 {
2629   GdkEvent *child_event;
2630
2631   g_return_if_fail (GTK_IS_CONTAINER (container));
2632   g_return_if_fail (GTK_IS_WIDGET (child));
2633   g_return_if_fail (event != NULL);
2634
2635   g_assert (child->parent == GTK_WIDGET (container));
2636   
2637   if (GTK_WIDGET_DRAWABLE (child) &&
2638       GTK_WIDGET_NO_WINDOW (child) &&
2639       (child->window == event->window))
2640     {
2641       child_event = gdk_event_new (GDK_EXPOSE);
2642       child_event->expose = *event;
2643       g_object_ref (child_event->expose.window);
2644
2645       child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2646       if (!gdk_region_empty (child_event->expose.region))
2647         {
2648           gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2649           gtk_widget_send_expose (child, child_event);
2650         }
2651       gdk_event_free (child_event);
2652     }
2653 }
2654
2655 #define __GTK_CONTAINER_C__
2656 #include "gtkaliasdef.c"