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