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