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