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