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