]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Revert GtkScaleButton accesses for plus/minus buttons, as per #539944
[~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: return location for the first property, followed 
541  *     optionally by more name/return location pairs, followed by %NULL
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   g_return_if_fail (widget->parent == container);
823
824   g_object_ref (container);
825   g_object_ref (widget);
826   gtk_widget_freeze_child_notify (widget);
827
828   g_signal_emit (container, container_signals[ADD], 0, widget);
829   if (widget->parent)
830     {
831       va_list var_args;
832
833       va_start (var_args, first_prop_name);
834       gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
835       va_end (var_args);
836     }
837
838   gtk_widget_thaw_child_notify (widget);
839   g_object_unref (widget);
840   g_object_unref (container);
841 }
842
843 /**
844  * gtk_container_child_set:
845  * @container: a #GtkContainer
846  * @child: a widget which is a child of @container
847  * @first_prop_name: the name of the first property to set
848  * @Varargs: a %NULL-terminated list of property names and values, starting
849  *           with @first_prop_name
850  * 
851  * Sets one or more child properties for @child and @container.
852  **/
853 void
854 gtk_container_child_set (GtkContainer      *container,
855                          GtkWidget         *child,
856                          const gchar       *first_prop_name,
857                          ...)
858 {
859   va_list var_args;
860   
861   g_return_if_fail (GTK_IS_CONTAINER (container));
862   g_return_if_fail (GTK_IS_WIDGET (child));
863   g_return_if_fail (child->parent == GTK_WIDGET (container));
864
865   va_start (var_args, first_prop_name);
866   gtk_container_child_set_valist (container, child, first_prop_name, var_args);
867   va_end (var_args);
868 }
869
870 /**
871  * gtk_container_child_get:
872  * @container: a #GtkContainer
873  * @child: a widget which is a child of @container
874  * @first_prop_name: the name of the first property to get
875  * @Varargs: return location for the first property, followed 
876  *     optionally by more name/return location pairs, followed by %NULL
877  * 
878  * Gets the values of one or more child properties for @child and @container.
879  **/
880 void
881 gtk_container_child_get (GtkContainer      *container,
882                          GtkWidget         *child,
883                          const gchar       *first_prop_name,
884                          ...)
885 {
886   va_list var_args;
887   
888   g_return_if_fail (GTK_IS_CONTAINER (container));
889   g_return_if_fail (GTK_IS_WIDGET (child));
890   g_return_if_fail (child->parent == GTK_WIDGET (container));
891
892   va_start (var_args, first_prop_name);
893   gtk_container_child_get_valist (container, child, first_prop_name, var_args);
894   va_end (var_args);
895 }
896
897 /**
898  * gtk_container_class_install_child_property:
899  * @cclass: a #GtkContainerClass
900  * @property_id: the id for the property
901  * @pspec: the #GParamSpec for the property
902  * 
903  * Installs a child property on a container class. 
904  **/
905 void
906 gtk_container_class_install_child_property (GtkContainerClass *cclass,
907                                             guint              property_id,
908                                             GParamSpec        *pspec)
909 {
910   g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
911   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
912   if (pspec->flags & G_PARAM_WRITABLE)
913     g_return_if_fail (cclass->set_child_property != NULL);
914   if (pspec->flags & G_PARAM_READABLE)
915     g_return_if_fail (cclass->get_child_property != NULL);
916   g_return_if_fail (property_id > 0);
917   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
918   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
919     g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
920
921   if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
922     {
923       g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
924                  G_OBJECT_CLASS_NAME (cclass),
925                  pspec->name);
926       return;
927     }
928   g_param_spec_ref (pspec);
929   g_param_spec_sink (pspec);
930   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
931   g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
932 }
933
934 /**
935  * gtk_container_class_find_child_property:
936  * @cclass: a #GtkContainerClass
937  * @property_name: the name of the child property to find
938  * @returns: the #GParamSpec of the child property or %NULL if @class has no
939  *   child property with that name.
940  *
941  * Finds a child property of a container class by name.
942  */
943 GParamSpec*
944 gtk_container_class_find_child_property (GObjectClass *cclass,
945                                          const gchar  *property_name)
946 {
947   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
948   g_return_val_if_fail (property_name != NULL, NULL);
949
950   return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
951                                    property_name,
952                                    G_OBJECT_CLASS_TYPE (cclass),
953                                    TRUE);
954 }
955
956 /**
957  * gtk_container_class_list_child_properties:
958  * @cclass: a #GtkContainerClass
959  * @n_properties: location to return the number of child properties found
960  * @returns: a newly allocated %NULL-terminated array of #GParamSpec*. 
961  *           The array must be freed with g_free().
962  *
963  * Returns all child properties of a container class.
964  */
965 GParamSpec**
966 gtk_container_class_list_child_properties (GObjectClass *cclass,
967                                            guint        *n_properties)
968 {
969   GParamSpec **pspecs;
970   guint n;
971
972   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
973
974   pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
975                                    G_OBJECT_CLASS_TYPE (cclass),
976                                    &n);
977   if (n_properties)
978     *n_properties = n;
979
980   return pspecs;
981 }
982
983 static void
984 gtk_container_add_unimplemented (GtkContainer     *container,
985                                  GtkWidget        *widget)
986 {
987   g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
988 }
989
990 static void
991 gtk_container_remove_unimplemented (GtkContainer     *container,
992                                     GtkWidget        *widget)
993 {
994   g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
995 }
996
997 static void
998 gtk_container_init (GtkContainer *container)
999 {
1000   container->focus_child = NULL;
1001   container->border_width = 0;
1002   container->need_resize = FALSE;
1003   container->resize_mode = GTK_RESIZE_PARENT;
1004   container->reallocate_redraws = FALSE;
1005 }
1006
1007 static void
1008 gtk_container_destroy (GtkObject *object)
1009 {
1010   GtkContainer *container = GTK_CONTAINER (object);
1011   
1012   if (GTK_CONTAINER_RESIZE_PENDING (container))
1013     _gtk_container_dequeue_resize_handler (container);
1014
1015   /* do this before walking child widgets, to avoid
1016    * removing children from focus chain one by one.
1017    */
1018   if (container->has_focus_chain)
1019     gtk_container_unset_focus_chain (container);
1020   
1021   gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1022   
1023   if (GTK_OBJECT_CLASS (parent_class)->destroy)
1024     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
1025 }
1026
1027 static void
1028 gtk_container_set_property (GObject         *object,
1029                             guint            prop_id,
1030                             const GValue    *value,
1031                             GParamSpec      *pspec)
1032 {
1033   GtkContainer *container = GTK_CONTAINER (object);
1034
1035   switch (prop_id)
1036     {
1037     case PROP_BORDER_WIDTH:
1038       gtk_container_set_border_width (container, g_value_get_uint (value));
1039       break;
1040     case PROP_RESIZE_MODE:
1041       gtk_container_set_resize_mode (container, g_value_get_enum (value));
1042       break;
1043     case PROP_CHILD:
1044       gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1045       break;
1046     default:
1047       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1048       break;
1049     }
1050 }
1051
1052 static void
1053 gtk_container_get_property (GObject         *object,
1054                             guint            prop_id,
1055                             GValue          *value,
1056                             GParamSpec      *pspec)
1057 {
1058   GtkContainer *container = GTK_CONTAINER (object);
1059   
1060   switch (prop_id)
1061     {
1062     case PROP_BORDER_WIDTH:
1063       g_value_set_uint (value, container->border_width);
1064       break;
1065     case PROP_RESIZE_MODE:
1066       g_value_set_enum (value, container->resize_mode);
1067       break;
1068     default:
1069       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1070       break;
1071     }
1072 }
1073
1074 /**
1075  * gtk_container_set_border_width:
1076  * @container: a #GtkContainer
1077  * @border_width: amount of blank space to leave <emphasis>outside</emphasis> 
1078  *   the container. Valid values are in the range 0-65535 pixels.
1079  *
1080  * Sets the border width of the container.
1081  *
1082  * The border width of a container is the amount of space to leave
1083  * around the outside of the container. The only exception to this is
1084  * #GtkWindow; because toplevel windows can't leave space outside,
1085  * they leave the space inside. The border is added on all sides of
1086  * the container. To add space to only one side, one approach is to
1087  * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1088  * to give it a size, and place it on the side of the container as
1089  * a spacer.
1090  **/
1091 void
1092 gtk_container_set_border_width (GtkContainer *container,
1093                                 guint         border_width)
1094 {
1095   g_return_if_fail (GTK_IS_CONTAINER (container));
1096
1097   if (container->border_width != border_width)
1098     {
1099       container->border_width = border_width;
1100       g_object_notify (G_OBJECT (container), "border-width");
1101       
1102       if (GTK_WIDGET_REALIZED (container))
1103         gtk_widget_queue_resize (GTK_WIDGET (container));
1104     }
1105 }
1106
1107 /**
1108  * gtk_container_get_border_width:
1109  * @container: a #GtkContainer
1110  * 
1111  * Retrieves the border width of the container. See
1112  * gtk_container_set_border_width().
1113  *
1114  * Return value: the current border width
1115  **/
1116 guint
1117 gtk_container_get_border_width (GtkContainer *container)
1118 {
1119   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1120
1121   return container->border_width;
1122 }
1123
1124 /**
1125  * gtk_container_add:
1126  * @container: a #GtkContainer
1127  * @widget: a widget to be placed inside @container
1128  * 
1129  * Adds @widget to @container. Typically used for simple containers
1130  * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1131  * layout containers such as #GtkBox or #GtkTable, this function will
1132  * pick default packing parameters that may not be correct.  So
1133  * consider functions such as gtk_box_pack_start() and
1134  * gtk_table_attach() as an alternative to gtk_container_add() in
1135  * those cases. A widget may be added to only one container at a time;
1136  * you can't place the same widget inside two different containers.
1137  **/
1138 void
1139 gtk_container_add (GtkContainer *container,
1140                    GtkWidget    *widget)
1141 {
1142   g_return_if_fail (GTK_IS_CONTAINER (container));
1143   g_return_if_fail (GTK_IS_WIDGET (widget));
1144
1145   if (widget->parent != NULL)
1146     {
1147       g_warning ("Attempting to add a widget with type %s to a container of "
1148                  "type %s, but the widget is already inside a container of type %s, "
1149                  "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
1150                  g_type_name (G_OBJECT_TYPE (widget)),
1151                  g_type_name (G_OBJECT_TYPE (container)),
1152                  g_type_name (G_OBJECT_TYPE (widget->parent)));
1153       return;
1154     }
1155
1156   g_signal_emit (container, container_signals[ADD], 0, widget);
1157 }
1158
1159 /**
1160  * gtk_container_remove:
1161  * @container: a #GtkContainer
1162  * @widget: a current child of @container
1163  * 
1164  * Removes @widget from @container. @widget must be inside @container.
1165  * Note that @container will own a reference to @widget, and that this
1166  * may be the last reference held; so removing a widget from its
1167  * container can destroy that widget. If you want to use @widget
1168  * again, you need to add a reference to it while it's not inside
1169  * a container, using g_object_ref(). If you don't want to use @widget
1170  * again it's usually more efficient to simply destroy it directly
1171  * using gtk_widget_destroy() since this will remove it from the
1172  * container and help break any circular reference count cycles.
1173  **/
1174 void
1175 gtk_container_remove (GtkContainer *container,
1176                       GtkWidget    *widget)
1177 {
1178   g_return_if_fail (GTK_IS_CONTAINER (container));
1179   g_return_if_fail (GTK_IS_WIDGET (widget));
1180
1181   /* When using the deprecated API of the toolbar, it is possible
1182    * to legitimately call this function with a widget that is not
1183    * a direct child of the container.
1184    */
1185   g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1186                     widget->parent == GTK_WIDGET (container));
1187   
1188   g_signal_emit (container, container_signals[REMOVE], 0, widget);
1189 }
1190
1191 void
1192 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1193 {
1194   g_return_if_fail (GTK_IS_CONTAINER (container));
1195   g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1196
1197   container_resize_queue = g_slist_remove (container_resize_queue, container);
1198   GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1199 }
1200
1201 /**
1202  * gtk_container_set_resize_mode:
1203  * @container: a #GtkContainer
1204  * @resize_mode: the new resize mode
1205  * 
1206  * Sets the resize mode for the container.
1207  *
1208  * The resize mode of a container determines whether a resize request 
1209  * will be passed to the container's parent, queued for later execution
1210  * or executed immediately.
1211  **/
1212 void
1213 gtk_container_set_resize_mode (GtkContainer  *container,
1214                                GtkResizeMode  resize_mode)
1215 {
1216   g_return_if_fail (GTK_IS_CONTAINER (container));
1217   g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1218   
1219   if (GTK_WIDGET_TOPLEVEL (container) &&
1220       resize_mode == GTK_RESIZE_PARENT)
1221     {
1222       resize_mode = GTK_RESIZE_QUEUE;
1223     }
1224   
1225   if (container->resize_mode != resize_mode)
1226     {
1227       container->resize_mode = resize_mode;
1228       
1229       gtk_widget_queue_resize (GTK_WIDGET (container));
1230       g_object_notify (G_OBJECT (container), "resize-mode");
1231     }
1232 }
1233
1234 /**
1235  * gtk_container_get_resize_mode:
1236  * @container: a #GtkContainer
1237  * 
1238  * Returns the resize mode for the container. See
1239  * gtk_container_set_resize_mode ().
1240  *
1241  * Return value: the current resize mode
1242  **/
1243 GtkResizeMode
1244 gtk_container_get_resize_mode (GtkContainer *container)
1245 {
1246   g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1247
1248   return container->resize_mode;
1249 }
1250
1251 /**
1252  * gtk_container_set_reallocate_redraws:
1253  * @container: a #GtkContainer
1254  * @needs_redraws: the new value for the container's @reallocate_redraws flag
1255  *
1256  * Sets the @reallocate_redraws flag of the container to the given value.
1257  * 
1258  * Containers requesting reallocation redraws get automatically
1259  * redrawn if any of their children changed allocation. 
1260  **/ 
1261 void
1262 gtk_container_set_reallocate_redraws (GtkContainer *container,
1263                                       gboolean      needs_redraws)
1264 {
1265   g_return_if_fail (GTK_IS_CONTAINER (container));
1266
1267   container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1268 }
1269
1270 static GtkContainer*
1271 gtk_container_get_resize_container (GtkContainer *container)
1272 {
1273   GtkWidget *widget = GTK_WIDGET (container);
1274
1275   while (widget->parent)
1276     {
1277       widget = widget->parent;
1278       if (GTK_IS_RESIZE_CONTAINER (widget))
1279         break;
1280     }
1281
1282   return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1283 }
1284
1285 static gboolean
1286 gtk_container_idle_sizer (gpointer data)
1287 {
1288   /* we may be invoked with a container_resize_queue of NULL, because
1289    * queue_resize could have been adding an extra idle function while
1290    * the queue still got processed. we better just ignore such case
1291    * than trying to explicitely work around them with some extra flags,
1292    * since it doesn't cause any actual harm.
1293    */
1294   while (container_resize_queue)
1295     {
1296       GSList *slist;
1297       GtkWidget *widget;
1298
1299       slist = container_resize_queue;
1300       container_resize_queue = slist->next;
1301       widget = slist->data;
1302       g_slist_free_1 (slist);
1303
1304       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1305       gtk_container_check_resize (GTK_CONTAINER (widget));
1306     }
1307
1308   gdk_window_process_all_updates ();
1309
1310   return FALSE;
1311 }
1312
1313 void
1314 _gtk_container_queue_resize (GtkContainer *container)
1315 {
1316   GtkContainer *resize_container;
1317   GtkWidget *widget;
1318   
1319   g_return_if_fail (GTK_IS_CONTAINER (container));
1320
1321   widget = GTK_WIDGET (container);
1322   resize_container = gtk_container_get_resize_container (container);
1323   
1324   while (TRUE)
1325     {
1326       GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1327       GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1328       if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1329           !widget->parent)
1330         break;
1331       
1332       widget = widget->parent;
1333     }
1334       
1335   if (resize_container)
1336     {
1337       if (GTK_WIDGET_VISIBLE (resize_container) &&
1338           (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1339         {
1340           switch (resize_container->resize_mode)
1341             {
1342             case GTK_RESIZE_QUEUE:
1343               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1344                 {
1345                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1346                   if (container_resize_queue == NULL)
1347                     gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1348                                      gtk_container_idle_sizer,
1349                                      NULL, NULL);
1350                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1351                 }
1352               break;
1353
1354             case GTK_RESIZE_IMMEDIATE:
1355               gtk_container_check_resize (resize_container);
1356               break;
1357
1358             case GTK_RESIZE_PARENT:
1359               g_assert_not_reached ();
1360               break;
1361             }
1362         }
1363       else
1364         {
1365           /* we need to let hidden resize containers know that something
1366            * changed while they where hidden (currently only evaluated by
1367            * toplevels).
1368            */
1369           resize_container->need_resize = TRUE;
1370         }
1371     }
1372 }
1373
1374 void
1375 gtk_container_check_resize (GtkContainer *container)
1376 {
1377   g_return_if_fail (GTK_IS_CONTAINER (container));
1378   
1379   g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1380 }
1381
1382 static void
1383 gtk_container_real_check_resize (GtkContainer *container)
1384 {
1385   GtkWidget *widget = GTK_WIDGET (container);
1386   GtkRequisition requisition;
1387   
1388   gtk_widget_size_request (widget, &requisition);
1389   
1390   if (requisition.width > widget->allocation.width ||
1391       requisition.height > widget->allocation.height)
1392     {
1393       if (GTK_IS_RESIZE_CONTAINER (container))
1394         gtk_widget_size_allocate (GTK_WIDGET (container),
1395                                   &GTK_WIDGET (container)->allocation);
1396       else
1397         gtk_widget_queue_resize (widget);
1398     }
1399   else
1400     {
1401       gtk_container_resize_children (container);
1402     }
1403 }
1404
1405 /* The container hasn't changed size but one of its children
1406  *  queued a resize request. Which means that the allocation
1407  *  is not sufficient for the requisition of some child.
1408  *  We've already performed a size request at this point,
1409  *  so we simply need to reallocate and let the allocation
1410  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1411  */
1412 void
1413 gtk_container_resize_children (GtkContainer *container)
1414 {
1415   GtkWidget *widget;
1416   
1417   /* resizing invariants:
1418    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1419    * containers that have an idle sizer pending must be flagged with
1420    * RESIZE_PENDING.
1421    */
1422   g_return_if_fail (GTK_IS_CONTAINER (container));
1423
1424   widget = GTK_WIDGET (container);
1425   gtk_widget_size_allocate (widget, &widget->allocation);
1426 }
1427
1428 /**
1429  * gtk_container_forall:
1430  * @container: a #GtkContainer
1431  * @callback: a callback
1432  * @callback_data: callback user data
1433  * 
1434  * Invokes @callback on each child of @container, including children
1435  * that are considered "internal" (implementation details of the
1436  * container). "Internal" children generally weren't added by the user
1437  * of the container, but were added by the container implementation
1438  * itself.  Most applications should use gtk_container_foreach(),
1439  * rather than gtk_container_forall().
1440  **/
1441 void
1442 gtk_container_forall (GtkContainer *container,
1443                       GtkCallback   callback,
1444                       gpointer      callback_data)
1445 {
1446   GtkContainerClass *class;
1447
1448   g_return_if_fail (GTK_IS_CONTAINER (container));
1449   g_return_if_fail (callback != NULL);
1450
1451   class = GTK_CONTAINER_GET_CLASS (container);
1452
1453   if (class->forall)
1454     class->forall (container, TRUE, callback, callback_data);
1455 }
1456
1457 /**
1458  * gtk_container_foreach:
1459  * @container: a #GtkContainer
1460  * @callback: a callback
1461  * @callback_data: callback user data
1462  * 
1463  * Invokes @callback on each non-internal child of @container. See
1464  * gtk_container_forall() for details on what constitutes an
1465  * "internal" child.  Most applications should use
1466  * gtk_container_foreach(), rather than gtk_container_forall().
1467  **/
1468 void
1469 gtk_container_foreach (GtkContainer *container,
1470                        GtkCallback   callback,
1471                        gpointer      callback_data)
1472 {
1473   GtkContainerClass *class;
1474   
1475   g_return_if_fail (GTK_IS_CONTAINER (container));
1476   g_return_if_fail (callback != NULL);
1477
1478   class = GTK_CONTAINER_GET_CLASS (container);
1479
1480   if (class->forall)
1481     class->forall (container, FALSE, callback, callback_data);
1482 }
1483
1484 typedef struct _GtkForeachData  GtkForeachData;
1485 struct _GtkForeachData
1486 {
1487   GtkObject         *container;
1488   GtkCallbackMarshal callback;
1489   gpointer           callback_data;
1490 };
1491
1492 static void
1493 gtk_container_foreach_unmarshal (GtkWidget *child,
1494                                  gpointer data)
1495 {
1496   GtkForeachData *fdata = (GtkForeachData*) data;
1497   GtkArg args[2];
1498   
1499   /* first argument */
1500   args[0].name = NULL;
1501   args[0].type = G_TYPE_FROM_INSTANCE (child);
1502   GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1503   
1504   /* location for return value */
1505   args[1].name = NULL;
1506   args[1].type = G_TYPE_NONE;
1507   
1508   fdata->callback (fdata->container, fdata->callback_data, 1, args);
1509 }
1510
1511 void
1512 gtk_container_foreach_full (GtkContainer       *container,
1513                             GtkCallback         callback,
1514                             GtkCallbackMarshal  marshal,
1515                             gpointer            callback_data,
1516                             GDestroyNotify      notify)
1517 {
1518   g_return_if_fail (GTK_IS_CONTAINER (container));
1519
1520   if (marshal)
1521     {
1522       GtkForeachData fdata;
1523   
1524       fdata.container     = GTK_OBJECT (container);
1525       fdata.callback      = marshal;
1526       fdata.callback_data = callback_data;
1527
1528       gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1529     }
1530   else
1531     {
1532       g_return_if_fail (callback != NULL);
1533
1534       gtk_container_foreach (container, callback, &callback_data);
1535     }
1536
1537   if (notify)
1538     notify (callback_data);
1539 }
1540
1541 /**
1542  * gtk_container_set_focus_child:
1543  * @container: a #GtkContainer
1544  * @widget: a #GtkWidget, or %NULL
1545  *
1546  * Sets, or unsets if @widget is %NULL, the focused child of @container.
1547  *
1548  * This function emits the GtkContainer::set_focus_child signal of
1549  * @container. Implementations of #GtkContainer can override the
1550  * default behaviour by overriding the class closure of this signal.
1551  */
1552 void
1553 gtk_container_set_focus_child (GtkContainer *container,
1554                                GtkWidget    *widget)
1555 {
1556   g_return_if_fail (GTK_IS_CONTAINER (container));
1557   if (widget)
1558     g_return_if_fail (GTK_IS_WIDGET (widget));
1559
1560   g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1561 }
1562
1563 /**
1564  * gtk_container_get_focus_child:
1565  * @container: a #GtkContainer
1566  *
1567  * Returns the current focus child widget inside @container.
1568  *
1569  * Returns: The child widget which has the focus
1570  *          inside @container, or %NULL if none is set.
1571  *
1572  * Since: 2.14
1573  **/
1574 GtkWidget *
1575 gtk_container_get_focus_child (GtkContainer *container)
1576 {
1577   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1578
1579   return container->focus_child;
1580 }
1581
1582 /**
1583  * gtk_container_get_children:
1584  * @container: a #GtkContainer
1585  * 
1586  * Returns the container's non-internal children. See
1587  * gtk_container_forall() for details on what constitutes an "internal" child. 
1588  *
1589  * Return value: a newly-allocated list of the container's non-internal children.
1590  **/
1591 GList*
1592 gtk_container_get_children (GtkContainer *container)
1593 {
1594   GList *children = NULL;
1595
1596   gtk_container_foreach (container,
1597                          gtk_container_children_callback,
1598                          &children);
1599
1600   return g_list_reverse (children);
1601 }
1602
1603 static void
1604 gtk_container_child_position_callback (GtkWidget *widget,
1605                                        gpointer   client_data)
1606 {
1607   struct {
1608     GtkWidget *child;
1609     guint i;
1610     guint index;
1611   } *data = client_data;
1612
1613   data->i++;
1614   if (data->child == widget)
1615     data->index = data->i;
1616 }
1617
1618 static gchar*
1619 gtk_container_child_default_composite_name (GtkContainer *container,
1620                                             GtkWidget    *child)
1621 {
1622   struct {
1623     GtkWidget *child;
1624     guint i;
1625     guint index;
1626   } data;
1627   gchar *name;
1628
1629   /* fallback implementation */
1630   data.child = child;
1631   data.i = 0;
1632   data.index = 0;
1633   gtk_container_forall (container,
1634                         gtk_container_child_position_callback,
1635                         &data);
1636   
1637   name = g_strdup_printf ("%s-%u",
1638                           g_type_name (G_TYPE_FROM_INSTANCE (child)),
1639                           data.index);
1640
1641   return name;
1642 }
1643
1644 gchar*
1645 _gtk_container_child_composite_name (GtkContainer *container,
1646                                     GtkWidget    *child)
1647 {
1648   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1649   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1650   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1651
1652   if (GTK_WIDGET_COMPOSITE_CHILD (child))
1653     {
1654       static GQuark quark_composite_name = 0;
1655       gchar *name;
1656
1657       if (!quark_composite_name)
1658         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1659
1660       name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1661       if (!name)
1662         {
1663           GtkContainerClass *class;
1664
1665           class = GTK_CONTAINER_GET_CLASS (container);
1666           if (class->composite_name)
1667             name = class->composite_name (container, child);
1668         }
1669       else
1670         name = g_strdup (name);
1671
1672       return name;
1673     }
1674   
1675   return NULL;
1676 }
1677
1678 static void
1679 gtk_container_real_set_focus_child (GtkContainer     *container,
1680                                     GtkWidget        *child)
1681 {
1682   g_return_if_fail (GTK_IS_CONTAINER (container));
1683   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1684
1685   if (child != container->focus_child)
1686     {
1687       if (container->focus_child)
1688         g_object_unref (container->focus_child);
1689       container->focus_child = child;
1690       if (container->focus_child)
1691         g_object_ref (container->focus_child);
1692     }
1693
1694
1695   /* check for h/v adjustments
1696    */
1697   if (container->focus_child)
1698     {
1699       GtkAdjustment *hadj;
1700       GtkAdjustment *vadj;
1701       GtkWidget *focus_child;
1702       gint x, y;
1703
1704       hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);   
1705       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1706       if (hadj || vadj) 
1707         {
1708
1709           focus_child = container->focus_child;
1710           while (GTK_IS_CONTAINER (focus_child) && 
1711                  GTK_CONTAINER (focus_child)->focus_child)
1712             {
1713               focus_child = GTK_CONTAINER (focus_child)->focus_child;
1714             }
1715           
1716           gtk_widget_translate_coordinates (focus_child, container->focus_child, 
1717                                             0, 0, &x, &y);
1718
1719            x += container->focus_child->allocation.x;
1720            y += container->focus_child->allocation.y;
1721           
1722           if (vadj)
1723             gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1724           
1725           if (hadj)
1726             gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1727         }
1728     }
1729 }
1730
1731 static GList*
1732 get_focus_chain (GtkContainer *container)
1733 {
1734   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1735 }
1736
1737 /* same as gtk_container_get_children, except it includes internals
1738  */
1739 static GList *
1740 gtk_container_get_all_children (GtkContainer *container)
1741 {
1742   GList *children = NULL;
1743
1744   gtk_container_forall (container,
1745                          gtk_container_children_callback,
1746                          &children);
1747
1748   return children;
1749 }
1750
1751 static gboolean
1752 gtk_container_focus (GtkWidget        *widget,
1753                      GtkDirectionType  direction)
1754 {
1755   GList *children;
1756   GList *sorted_children;
1757   gint return_val;
1758   GtkContainer *container;
1759
1760   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1761
1762   container = GTK_CONTAINER (widget);
1763
1764   return_val = FALSE;
1765
1766   if (GTK_WIDGET_CAN_FOCUS (container))
1767     {
1768       if (!GTK_WIDGET_HAS_FOCUS (container))
1769         {
1770           gtk_widget_grab_focus (GTK_WIDGET (container));
1771           return_val = TRUE;
1772         }
1773     }
1774   else
1775     {
1776       /* Get a list of the containers children, allowing focus
1777        * chain to override.
1778        */
1779       if (container->has_focus_chain)
1780         children = g_list_copy (get_focus_chain (container));
1781       else
1782         children = gtk_container_get_all_children (container);
1783
1784       if (container->has_focus_chain &&
1785           (direction == GTK_DIR_TAB_FORWARD ||
1786            direction == GTK_DIR_TAB_BACKWARD))
1787         {
1788           sorted_children = g_list_copy (children);
1789           
1790           if (direction == GTK_DIR_TAB_BACKWARD)
1791             sorted_children = g_list_reverse (sorted_children);
1792         }
1793       else
1794         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1795       
1796       return_val = gtk_container_focus_move (container, sorted_children, direction);
1797
1798       g_list_free (sorted_children);
1799       g_list_free (children);
1800     }
1801
1802   return return_val;
1803 }
1804
1805 static gint
1806 tab_compare (gconstpointer a,
1807              gconstpointer b,
1808              gpointer      data)
1809 {
1810   const GtkWidget *child1 = a;
1811   const GtkWidget *child2 = b;
1812   GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1813
1814   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1815   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1816
1817   if (y1 == y2)
1818     {
1819       gint x1 = child1->allocation.x + child1->allocation.width / 2;
1820       gint x2 = child2->allocation.x + child2->allocation.width / 2;
1821       
1822       if (text_direction == GTK_TEXT_DIR_RTL) 
1823         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1824       else
1825         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1826     }
1827   else
1828     return (y1 < y2) ? -1 : 1;
1829 }
1830
1831 static GList *
1832 gtk_container_focus_sort_tab (GtkContainer     *container,
1833                               GList            *children,
1834                               GtkDirectionType  direction,
1835                               GtkWidget        *old_focus)
1836 {
1837   GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1838   children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1839
1840   /* if we are going backwards then reverse the order
1841    *  of the children.
1842    */
1843   if (direction == GTK_DIR_TAB_BACKWARD)
1844     children = g_list_reverse (children);
1845
1846   return children;
1847 }
1848
1849 /* Get coordinates of @widget's allocation with respect to
1850  * allocation of @container.
1851  */
1852 static gboolean
1853 get_allocation_coords (GtkContainer  *container,
1854                        GtkWidget     *widget,
1855                        GdkRectangle  *allocation)
1856 {
1857   *allocation = widget->allocation;
1858
1859   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1860                                            0, 0, &allocation->x, &allocation->y);
1861 }
1862
1863 /* Look for a child in @children that is intermediate between
1864  * the focus widget and container. This widget, if it exists,
1865  * acts as the starting widget for focus navigation.
1866  */
1867 static GtkWidget *
1868 find_old_focus (GtkContainer *container,
1869                 GList        *children)
1870 {
1871   GList *tmp_list = children;
1872   while (tmp_list)
1873     {
1874       GtkWidget *child = tmp_list->data;
1875       GtkWidget *widget = child;
1876
1877       while (widget && widget != (GtkWidget *)container)
1878         {
1879           GtkWidget *parent = widget->parent;
1880           if (parent && ((GtkContainer *)parent)->focus_child != widget)
1881             goto next;
1882
1883           widget = parent;
1884         }
1885
1886       return child;
1887
1888     next:
1889       tmp_list = tmp_list->next;
1890     }
1891
1892   return NULL;
1893 }
1894
1895 static gboolean
1896 old_focus_coords (GtkContainer *container,
1897                   GdkRectangle *old_focus_rect)
1898 {
1899   GtkWidget *widget = GTK_WIDGET (container);
1900   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1901
1902   if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1903     {
1904       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1905       
1906       return get_allocation_coords (container, old_focus, old_focus_rect);
1907     }
1908   else
1909     return FALSE;
1910 }
1911
1912 typedef struct _CompareInfo CompareInfo;
1913
1914 struct _CompareInfo
1915 {
1916   GtkContainer *container;
1917   gint x;
1918   gint y;
1919   gboolean reverse;
1920 };
1921
1922 static gint
1923 up_down_compare (gconstpointer a,
1924                  gconstpointer b,
1925                  gpointer      data)
1926 {
1927   GdkRectangle allocation1;
1928   GdkRectangle allocation2;
1929   CompareInfo *compare = data;
1930   gint y1, y2;
1931
1932   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1933   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1934
1935   y1 = allocation1.y + allocation1.height / 2;
1936   y2 = allocation2.y + allocation2.height / 2;
1937
1938   if (y1 == y2)
1939     {
1940       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1941       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1942
1943       if (compare->reverse)
1944         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1945       else
1946         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1947     }
1948   else
1949     return (y1 < y2) ? -1 : 1;
1950 }
1951
1952 static GList *
1953 gtk_container_focus_sort_up_down (GtkContainer     *container,
1954                                   GList            *children,
1955                                   GtkDirectionType  direction,
1956                                   GtkWidget        *old_focus)
1957 {
1958   CompareInfo compare;
1959   GList *tmp_list;
1960   GdkRectangle old_allocation;
1961
1962   compare.container = container;
1963   compare.reverse = (direction == GTK_DIR_UP);
1964
1965   if (!old_focus)
1966       old_focus = find_old_focus (container, children);
1967   
1968   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1969     {
1970       gint compare_x1;
1971       gint compare_x2;
1972       gint compare_y;
1973
1974       /* Delete widgets from list that don't match minimum criteria */
1975
1976       compare_x1 = old_allocation.x;
1977       compare_x2 = old_allocation.x + old_allocation.width;
1978
1979       if (direction == GTK_DIR_UP)
1980         compare_y = old_allocation.y;
1981       else
1982         compare_y = old_allocation.y + old_allocation.height;
1983       
1984       tmp_list = children;
1985       while (tmp_list)
1986         {
1987           GtkWidget *child = tmp_list->data;
1988           GList *next = tmp_list->next;
1989           gint child_x1, child_x2;
1990           GdkRectangle child_allocation;
1991           
1992           if (child != old_focus)
1993             {
1994               if (get_allocation_coords (container, child, &child_allocation))
1995                 {
1996                   child_x1 = child_allocation.x;
1997                   child_x2 = child_allocation.x + child_allocation.width;
1998                   
1999                   if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2000                       (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2001                       (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2002                     {
2003                       children = g_list_delete_link (children, tmp_list);
2004                     }
2005                 }
2006               else
2007                 children = g_list_delete_link (children, tmp_list);
2008             }
2009           
2010           tmp_list = next;
2011         }
2012
2013       compare.x = (compare_x1 + compare_x2) / 2;
2014       compare.y = old_allocation.y + old_allocation.height / 2;
2015     }
2016   else
2017     {
2018       /* No old focus widget, need to figure out starting x,y some other way
2019        */
2020       GtkWidget *widget = GTK_WIDGET (container);
2021       GdkRectangle old_focus_rect;
2022
2023       if (old_focus_coords (container, &old_focus_rect))
2024         {
2025           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2026         }
2027       else
2028         {
2029           if (GTK_WIDGET_NO_WINDOW (widget))
2030             compare.x = widget->allocation.x + widget->allocation.width / 2;
2031           else
2032             compare.x = widget->allocation.width / 2;
2033         }
2034       
2035       if (GTK_WIDGET_NO_WINDOW (widget))
2036         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2037       else
2038         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2039     }
2040
2041   children = g_list_sort_with_data (children, up_down_compare, &compare);
2042
2043   if (compare.reverse)
2044     children = g_list_reverse (children);
2045
2046   return children;
2047 }
2048
2049 static gint
2050 left_right_compare (gconstpointer a,
2051                     gconstpointer b,
2052                     gpointer      data)
2053 {
2054   GdkRectangle allocation1;
2055   GdkRectangle allocation2;
2056   CompareInfo *compare = data;
2057   gint x1, x2;
2058
2059   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2060   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2061
2062   x1 = allocation1.x + allocation1.width / 2;
2063   x2 = allocation2.x + allocation2.width / 2;
2064
2065   if (x1 == x2)
2066     {
2067       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2068       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2069
2070       if (compare->reverse)
2071         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2072       else
2073         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2074     }
2075   else
2076     return (x1 < x2) ? -1 : 1;
2077 }
2078
2079 static GList *
2080 gtk_container_focus_sort_left_right (GtkContainer     *container,
2081                                      GList            *children,
2082                                      GtkDirectionType  direction,
2083                                      GtkWidget        *old_focus)
2084 {
2085   CompareInfo compare;
2086   GList *tmp_list;
2087   GdkRectangle old_allocation;
2088
2089   compare.container = container;
2090   compare.reverse = (direction == GTK_DIR_LEFT);
2091
2092   if (!old_focus)
2093     old_focus = find_old_focus (container, children);
2094   
2095   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2096     {
2097       gint compare_y1;
2098       gint compare_y2;
2099       gint compare_x;
2100       
2101       /* Delete widgets from list that don't match minimum criteria */
2102
2103       compare_y1 = old_allocation.y;
2104       compare_y2 = old_allocation.y + old_allocation.height;
2105
2106       if (direction == GTK_DIR_LEFT)
2107         compare_x = old_allocation.x;
2108       else
2109         compare_x = old_allocation.x + old_allocation.width;
2110       
2111       tmp_list = children;
2112       while (tmp_list)
2113         {
2114           GtkWidget *child = tmp_list->data;
2115           GList *next = tmp_list->next;
2116           gint child_y1, child_y2;
2117           GdkRectangle child_allocation;
2118           
2119           if (child != old_focus)
2120             {
2121               if (get_allocation_coords (container, child, &child_allocation))
2122                 {
2123                   child_y1 = child_allocation.y;
2124                   child_y2 = child_allocation.y + child_allocation.height;
2125                   
2126                   if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2127                       (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2128                       (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2129                     {
2130                       children = g_list_delete_link (children, tmp_list);
2131                     }
2132                 }
2133               else
2134                 children = g_list_delete_link (children, tmp_list);
2135             }
2136           
2137           tmp_list = next;
2138         }
2139
2140       compare.y = (compare_y1 + compare_y2) / 2;
2141       compare.x = old_allocation.x + old_allocation.width / 2;
2142     }
2143   else
2144     {
2145       /* No old focus widget, need to figure out starting x,y some other way
2146        */
2147       GtkWidget *widget = GTK_WIDGET (container);
2148       GdkRectangle old_focus_rect;
2149
2150       if (old_focus_coords (container, &old_focus_rect))
2151         {
2152           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2153         }
2154       else
2155         {
2156           if (GTK_WIDGET_NO_WINDOW (widget))
2157             compare.y = widget->allocation.y + widget->allocation.height / 2;
2158           else
2159             compare.y = widget->allocation.height / 2;
2160         }
2161       
2162       if (GTK_WIDGET_NO_WINDOW (widget))
2163         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2164       else
2165         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2166     }
2167
2168   children = g_list_sort_with_data (children, left_right_compare, &compare);
2169
2170   if (compare.reverse)
2171     children = g_list_reverse (children);
2172
2173   return children;
2174 }
2175
2176 /**
2177  * gtk_container_focus_sort:
2178  * @container: a #GtkContainer
2179  * @children:  a list of descendents of @container (they don't
2180  *             have to be direct children)
2181  * @direction: focus direction
2182  * @old_focus: widget to use for the starting position, or %NULL
2183  *             to determine this automatically.
2184  *             (Note, this argument isn't used for GTK_DIR_TAB_*,
2185  *              which is the only @direction we use currently,
2186  *              so perhaps this argument should be removed)
2187  * 
2188  * Sorts @children in the correct order for focusing with
2189  * direction type @direction.
2190  * 
2191  * Return value: a copy of @children, sorted in correct focusing order,
2192  *   with children that aren't suitable for focusing in this direction
2193  *   removed.
2194  **/
2195 GList *
2196 _gtk_container_focus_sort (GtkContainer     *container,
2197                            GList            *children,
2198                            GtkDirectionType  direction,
2199                            GtkWidget        *old_focus)
2200 {
2201   GList *visible_children = NULL;
2202
2203   while (children)
2204     {
2205       if (GTK_WIDGET_REALIZED (children->data))
2206         visible_children = g_list_prepend (visible_children, children->data);
2207       children = children->next;
2208     }
2209   
2210   switch (direction)
2211     {
2212     case GTK_DIR_TAB_FORWARD:
2213     case GTK_DIR_TAB_BACKWARD:
2214       return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2215     case GTK_DIR_UP:
2216     case GTK_DIR_DOWN:
2217       return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2218     case GTK_DIR_LEFT:
2219     case GTK_DIR_RIGHT:
2220       return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2221     }
2222
2223   g_assert_not_reached ();
2224
2225   return NULL;
2226 }
2227
2228 static gboolean
2229 gtk_container_focus_move (GtkContainer     *container,
2230                           GList            *children,
2231                           GtkDirectionType  direction)
2232 {
2233   GtkWidget *focus_child;
2234   GtkWidget *child;
2235
2236   focus_child = container->focus_child;
2237
2238   while (children)
2239     {
2240       child = children->data;
2241       children = children->next;
2242
2243       if (!child)
2244         continue;
2245       
2246       if (focus_child)
2247         {
2248           if (focus_child == child)
2249             {
2250               focus_child = NULL;
2251
2252                 if (gtk_widget_child_focus (child, direction))
2253                   return TRUE;
2254             }
2255         }
2256       else if (GTK_WIDGET_DRAWABLE (child) &&
2257                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2258         {
2259           if (gtk_widget_child_focus (child, direction))
2260             return TRUE;
2261         }
2262     }
2263
2264   return FALSE;
2265 }
2266
2267
2268 static void
2269 gtk_container_children_callback (GtkWidget *widget,
2270                                  gpointer   client_data)
2271 {
2272   GList **children;
2273
2274   children = (GList**) client_data;
2275   *children = g_list_prepend (*children, widget);
2276 }
2277
2278 static void
2279 chain_widget_destroyed (GtkWidget *widget,
2280                         gpointer   user_data)
2281 {
2282   GtkContainer *container;
2283   GList *chain;
2284   
2285   container = GTK_CONTAINER (user_data);
2286
2287   chain = g_object_get_data (G_OBJECT (container),
2288                              "gtk-container-focus-chain");
2289
2290   chain = g_list_remove (chain, widget);
2291
2292   g_signal_handlers_disconnect_by_func (widget,
2293                                         chain_widget_destroyed,
2294                                         user_data);
2295   
2296   g_object_set_data (G_OBJECT (container),
2297                      I_("gtk-container-focus-chain"),
2298                      chain);  
2299 }
2300
2301 /**
2302  * gtk_container_set_focus_chain: 
2303  * @container: a #GtkContainer
2304  * @focusable_widgets: the new focus chain
2305  *
2306  * Sets a focus chain, overriding the one computed automatically by GTK+.
2307  * 
2308  * In principle each widget in the chain should be a descendant of the 
2309  * container, but this is not enforced by this method, since it's allowed 
2310  * to set the focus chain before you pack the widgets, or have a widget 
2311  * in the chain that isn't always packed. The necessary checks are done 
2312  * when the focus chain is actually traversed.
2313  **/
2314 void
2315 gtk_container_set_focus_chain (GtkContainer *container,
2316                                GList        *focusable_widgets)
2317 {
2318   GList *chain;
2319   GList *tmp_list;
2320   
2321   g_return_if_fail (GTK_IS_CONTAINER (container));
2322   
2323   if (container->has_focus_chain)
2324     gtk_container_unset_focus_chain (container);
2325
2326   container->has_focus_chain = TRUE;
2327   
2328   chain = NULL;
2329   tmp_list = focusable_widgets;
2330   while (tmp_list != NULL)
2331     {
2332       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2333       
2334       /* In principle each widget in the chain should be a descendant
2335        * of the container, but we don't want to check that here, it's
2336        * expensive and also it's allowed to set the focus chain before
2337        * you pack the widgets, or have a widget in the chain that isn't
2338        * always packed. So we check for ancestor during actual traversal.
2339        */
2340
2341       chain = g_list_prepend (chain, tmp_list->data);
2342
2343       g_signal_connect (tmp_list->data,
2344                         "destroy",
2345                         G_CALLBACK (chain_widget_destroyed),
2346                         container);
2347       
2348       tmp_list = g_list_next (tmp_list);
2349     }
2350
2351   chain = g_list_reverse (chain);
2352   
2353   g_object_set_data (G_OBJECT (container),
2354                      I_("gtk-container-focus-chain"),
2355                      chain);
2356 }
2357
2358 /**
2359  * gtk_container_get_focus_chain:
2360  * @container:         a #GtkContainer
2361  * @focusable_widgets: location to store the focus chain of the
2362  *                     container, or %NULL. You should free this list
2363  *                     using g_list_free() when you are done with it, however
2364  *                     no additional reference count is added to the
2365  *                     individual widgets in the focus chain.
2366  * 
2367  * Retrieves the focus chain of the container, if one has been
2368  * set explicitly. If no focus chain has been explicitly
2369  * set, GTK+ computes the focus chain based on the positions
2370  * of the children. In that case, GTK+ stores %NULL in
2371  * @focusable_widgets and returns %FALSE.
2372  *
2373  * Return value: %TRUE if the focus chain of the container 
2374  * has been set explicitly.
2375  **/
2376 gboolean
2377 gtk_container_get_focus_chain (GtkContainer *container,
2378                                GList       **focus_chain)
2379 {
2380   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2381
2382   if (focus_chain)
2383     {
2384       if (container->has_focus_chain)
2385         *focus_chain = g_list_copy (get_focus_chain (container));
2386       else
2387         *focus_chain = NULL;
2388     }
2389
2390   return container->has_focus_chain;
2391 }
2392
2393 /**
2394  * gtk_container_unset_focus_chain:
2395  * @container: a #GtkContainer
2396  * 
2397  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2398  **/
2399 void
2400 gtk_container_unset_focus_chain (GtkContainer  *container)
2401 {  
2402   g_return_if_fail (GTK_IS_CONTAINER (container));
2403
2404   if (container->has_focus_chain)
2405     {
2406       GList *chain;
2407       GList *tmp_list;
2408       
2409       chain = get_focus_chain (container);
2410       
2411       container->has_focus_chain = FALSE;
2412       
2413       g_object_set_data (G_OBJECT (container), 
2414                          I_("gtk-container-focus-chain"),
2415                          NULL);
2416
2417       tmp_list = chain;
2418       while (tmp_list != NULL)
2419         {
2420           g_signal_handlers_disconnect_by_func (tmp_list->data,
2421                                                 chain_widget_destroyed,
2422                                                 container);
2423           
2424           tmp_list = g_list_next (tmp_list);
2425         }
2426
2427       g_list_free (chain);
2428     }
2429 }
2430
2431 /**
2432  * gtk_container_set_focus_vadjustment:
2433  * @container: a #GtkContainer
2434  * @adjustment: an adjustment which should be adjusted when the focus 
2435  *   is moved among the descendents of @container
2436  * 
2437  * Hooks up an adjustment to focus handling in a container, so when a 
2438  * child of the container is focused, the adjustment is scrolled to 
2439  * show that widget. This function sets the vertical alignment. See 
2440  * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining 
2441  * the adjustment and gtk_container_set_focus_hadjustment() for setting
2442  * the horizontal adjustment.
2443  *
2444  * The adjustments have to be in pixel units and in the same coordinate 
2445  * system as the allocation for immediate children of the container. 
2446  */
2447 void
2448 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2449                                      GtkAdjustment *adjustment)
2450 {
2451   g_return_if_fail (GTK_IS_CONTAINER (container));
2452   if (adjustment)
2453     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2454
2455   if (adjustment)
2456     g_object_ref (adjustment);
2457
2458   g_object_set_qdata_full (G_OBJECT (container),
2459                            vadjustment_key_id,
2460                            adjustment,
2461                            g_object_unref);
2462 }
2463
2464 /**
2465  * gtk_container_get_focus_vadjustment:
2466  * @container: a #GtkContainer
2467  *
2468  * Retrieves the vertical focus adjustment for the container. See
2469  * gtk_container_set_focus_vadjustment().
2470  *
2471  * Return value: the vertical focus adjustment, or %NULL if
2472  *   none has been set.
2473  **/
2474 GtkAdjustment *
2475 gtk_container_get_focus_vadjustment (GtkContainer *container)
2476 {
2477   GtkAdjustment *vadjustment;
2478     
2479   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2480
2481   vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2482
2483   return vadjustment;
2484 }
2485
2486 /**
2487  * gtk_container_set_focus_hadjustment:
2488  * @container: a #GtkContainer
2489  * @adjustment: an adjustment which should be adjusted when the focus is 
2490  *   moved among the descendents of @container
2491  * 
2492  * Hooks up an adjustment to focus handling in a container, so when a child 
2493  * of the container is focused, the adjustment is scrolled to show that 
2494  * widget. This function sets the horizontal alignment. 
2495  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
2496  * the adjustment and gtk_container_set_focus_vadjustment() for setting
2497  * the vertical adjustment.
2498  *
2499  * The adjustments have to be in pixel units and in the same coordinate 
2500  * system as the allocation for immediate children of the container. 
2501  */
2502 void
2503 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2504                                      GtkAdjustment *adjustment)
2505 {
2506   g_return_if_fail (GTK_IS_CONTAINER (container));
2507   if (adjustment)
2508     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2509
2510   if (adjustment)
2511     g_object_ref (adjustment);
2512
2513   g_object_set_qdata_full (G_OBJECT (container),
2514                            hadjustment_key_id,
2515                            adjustment,
2516                            g_object_unref);
2517 }
2518
2519 /**
2520  * gtk_container_get_focus_hadjustment:
2521  * @container: a #GtkContainer
2522  *
2523  * Retrieves the horizontal focus adjustment for the container. See
2524  * gtk_container_set_focus_hadjustment ().
2525  *
2526  * Return value: the horizontal focus adjustment, or %NULL if
2527  *   none has been set.
2528  **/
2529 GtkAdjustment *
2530 gtk_container_get_focus_hadjustment (GtkContainer *container)
2531 {
2532   GtkAdjustment *hadjustment;
2533
2534   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2535
2536   hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2537
2538   return hadjustment;
2539 }
2540
2541
2542 static void
2543 gtk_container_show_all (GtkWidget *widget)
2544 {
2545   g_return_if_fail (GTK_IS_CONTAINER (widget));
2546
2547   gtk_container_foreach (GTK_CONTAINER (widget),
2548                          (GtkCallback) gtk_widget_show_all,
2549                          NULL);
2550   gtk_widget_show (widget);
2551 }
2552
2553 static void
2554 gtk_container_hide_all (GtkWidget *widget)
2555 {
2556   g_return_if_fail (GTK_IS_CONTAINER (widget));
2557
2558   gtk_widget_hide (widget);
2559   gtk_container_foreach (GTK_CONTAINER (widget),
2560                          (GtkCallback) gtk_widget_hide_all,
2561                          NULL);
2562 }
2563
2564
2565 static void
2566 gtk_container_expose_child (GtkWidget *child,
2567                             gpointer   client_data)
2568 {
2569   struct {
2570     GtkWidget *container;
2571     GdkEventExpose *event;
2572   } *data = client_data;
2573   
2574   gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2575                                   child,
2576                                   data->event);
2577 }
2578
2579 static gint 
2580 gtk_container_expose (GtkWidget      *widget,
2581                       GdkEventExpose *event)
2582 {
2583   struct {
2584     GtkWidget *container;
2585     GdkEventExpose *event;
2586   } data;
2587
2588   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2589   g_return_val_if_fail (event != NULL, FALSE);
2590
2591   
2592   if (GTK_WIDGET_DRAWABLE (widget)) 
2593     {
2594       data.container = widget;
2595       data.event = event;
2596       
2597       gtk_container_forall (GTK_CONTAINER (widget),
2598                             gtk_container_expose_child,
2599                             &data);
2600     }   
2601   
2602   return FALSE;
2603 }
2604
2605 static void
2606 gtk_container_map_child (GtkWidget *child,
2607                          gpointer   client_data)
2608 {
2609   if (GTK_WIDGET_VISIBLE (child) &&
2610       GTK_WIDGET_CHILD_VISIBLE (child) &&
2611       !GTK_WIDGET_MAPPED (child))
2612     gtk_widget_map (child);
2613 }
2614
2615 static void
2616 gtk_container_map (GtkWidget *widget)
2617 {
2618   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2619
2620   gtk_container_forall (GTK_CONTAINER (widget),
2621                         gtk_container_map_child,
2622                         NULL);
2623
2624   if (!GTK_WIDGET_NO_WINDOW (widget))
2625     gdk_window_show (widget->window);
2626 }
2627
2628 static void
2629 gtk_container_unmap (GtkWidget *widget)
2630 {
2631   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2632
2633   if (!GTK_WIDGET_NO_WINDOW (widget))
2634     gdk_window_hide (widget->window);
2635   else
2636     gtk_container_forall (GTK_CONTAINER (widget),
2637                           (GtkCallback)gtk_widget_unmap,
2638                           NULL);
2639 }
2640
2641 /**
2642  * gtk_container_propagate_expose:
2643  * @container: a #GtkContainer
2644  * @child: a child of @container
2645  * @event: a expose event sent to container
2646  *
2647  * When a container receives an expose event, it must send synthetic
2648  * expose events to all children that don't have their own #GdkWindows.
2649  * This function provides a convenient way of doing this. A container,
2650  * when it receives an expose event, calls gtk_container_propagate_expose() 
2651  * once for each child, passing in the event the container received.
2652  *
2653  * gtk_container_propagate_expose() takes care of deciding whether
2654  * an expose event needs to be sent to the child, intersecting
2655  * the event's area with the child area, and sending the event.
2656  * 
2657  * In most cases, a container can simply either simply inherit the
2658  * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing 
2659  * and then chain to the ::expose implementation from #GtkContainer.
2660  **/
2661 void
2662 gtk_container_propagate_expose (GtkContainer   *container,
2663                                 GtkWidget      *child,
2664                                 GdkEventExpose *event)
2665 {
2666   GdkEvent *child_event;
2667
2668   g_return_if_fail (GTK_IS_CONTAINER (container));
2669   g_return_if_fail (GTK_IS_WIDGET (child));
2670   g_return_if_fail (event != NULL);
2671
2672   g_assert (child->parent == GTK_WIDGET (container));
2673   
2674   if (GTK_WIDGET_DRAWABLE (child) &&
2675       GTK_WIDGET_NO_WINDOW (child) &&
2676       (child->window == event->window))
2677     {
2678       child_event = gdk_event_new (GDK_EXPOSE);
2679       child_event->expose = *event;
2680       g_object_ref (child_event->expose.window);
2681
2682       child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2683       if (!gdk_region_empty (child_event->expose.region))
2684         {
2685           gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2686           gtk_widget_send_expose (child, child_event);
2687         }
2688       gdk_event_free (child_event);
2689     }
2690 }
2691
2692 #define __GTK_CONTAINER_C__
2693 #include "gtkaliasdef.c"