]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Deprecate the GdkRegion API
[~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                                                       65535,
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: (allow-none): 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_get_realized (GTK_WIDGET (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_is_toplevel (GTK_WIDGET (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       GTK_PRIVATE_SET_FLAG (widget, GTK_WIDTH_REQUEST_NEEDED);
1374       GTK_PRIVATE_SET_FLAG (widget, GTK_HEIGHT_REQUEST_NEEDED);
1375
1376       if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1377           !widget->parent)
1378         break;
1379       
1380       widget = widget->parent;
1381     }
1382       
1383   if (resize_container)
1384     {
1385       if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1386           (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1387            gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1388         {
1389           switch (resize_container->resize_mode)
1390             {
1391             case GTK_RESIZE_QUEUE:
1392               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1393                 {
1394                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1395                   if (container_resize_queue == NULL)
1396                     gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1397                                      gtk_container_idle_sizer,
1398                                      NULL, NULL);
1399                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1400                 }
1401               break;
1402
1403             case GTK_RESIZE_IMMEDIATE:
1404               gtk_container_check_resize (resize_container);
1405               break;
1406
1407             case GTK_RESIZE_PARENT:
1408               g_assert_not_reached ();
1409               break;
1410             }
1411         }
1412       else
1413         {
1414           /* we need to let hidden resize containers know that something
1415            * changed while they where hidden (currently only evaluated by
1416            * toplevels).
1417            */
1418           resize_container->need_resize = TRUE;
1419         }
1420     }
1421 }
1422
1423 void
1424 gtk_container_check_resize (GtkContainer *container)
1425 {
1426   g_return_if_fail (GTK_IS_CONTAINER (container));
1427   
1428   g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1429 }
1430
1431 static void
1432 gtk_container_real_check_resize (GtkContainer *container)
1433 {
1434   GtkWidget *widget = GTK_WIDGET (container);
1435   GtkRequisition requisition;
1436   
1437   gtk_widget_size_request (widget, &requisition);
1438   
1439   if (requisition.width > widget->allocation.width ||
1440       requisition.height > widget->allocation.height)
1441     {
1442       if (GTK_IS_RESIZE_CONTAINER (container))
1443         gtk_widget_size_allocate (GTK_WIDGET (container),
1444                                   &GTK_WIDGET (container)->allocation);
1445       else
1446         gtk_widget_queue_resize (widget);
1447     }
1448   else
1449     {
1450       gtk_container_resize_children (container);
1451     }
1452 }
1453
1454 /* The container hasn't changed size but one of its children
1455  *  queued a resize request. Which means that the allocation
1456  *  is not sufficient for the requisition of some child.
1457  *  We've already performed a size request at this point,
1458  *  so we simply need to reallocate and let the allocation
1459  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1460  */
1461 void
1462 gtk_container_resize_children (GtkContainer *container)
1463 {
1464   GtkWidget *widget;
1465   
1466   /* resizing invariants:
1467    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1468    * containers that have an idle sizer pending must be flagged with
1469    * RESIZE_PENDING.
1470    */
1471   g_return_if_fail (GTK_IS_CONTAINER (container));
1472
1473   widget = GTK_WIDGET (container);
1474   gtk_widget_size_allocate (widget, &widget->allocation);
1475 }
1476
1477 /**
1478  * gtk_container_forall:
1479  * @container: a #GtkContainer
1480  * @callback: a callback
1481  * @callback_data: callback user data
1482  * 
1483  * Invokes @callback on each child of @container, including children
1484  * that are considered "internal" (implementation details of the
1485  * container). "Internal" children generally weren't added by the user
1486  * of the container, but were added by the container implementation
1487  * itself.  Most applications should use gtk_container_foreach(),
1488  * rather than gtk_container_forall().
1489  **/
1490 void
1491 gtk_container_forall (GtkContainer *container,
1492                       GtkCallback   callback,
1493                       gpointer      callback_data)
1494 {
1495   GtkContainerClass *class;
1496
1497   g_return_if_fail (GTK_IS_CONTAINER (container));
1498   g_return_if_fail (callback != NULL);
1499
1500   class = GTK_CONTAINER_GET_CLASS (container);
1501
1502   if (class->forall)
1503     class->forall (container, TRUE, callback, callback_data);
1504 }
1505
1506 /**
1507  * gtk_container_foreach:
1508  * @container: a #GtkContainer
1509  * @callback: a callback
1510  * @callback_data: callback user data
1511  * 
1512  * Invokes @callback on each non-internal child of @container. See
1513  * gtk_container_forall() for details on what constitutes an
1514  * "internal" child.  Most applications should use
1515  * gtk_container_foreach(), rather than gtk_container_forall().
1516  **/
1517 void
1518 gtk_container_foreach (GtkContainer *container,
1519                        GtkCallback   callback,
1520                        gpointer      callback_data)
1521 {
1522   GtkContainerClass *class;
1523   
1524   g_return_if_fail (GTK_IS_CONTAINER (container));
1525   g_return_if_fail (callback != NULL);
1526
1527   class = GTK_CONTAINER_GET_CLASS (container);
1528
1529   if (class->forall)
1530     class->forall (container, FALSE, callback, callback_data);
1531 }
1532
1533 /**
1534  * gtk_container_set_focus_child:
1535  * @container: a #GtkContainer
1536  * @child: (allow-none): a #GtkWidget, or %NULL
1537  *
1538  * Sets, or unsets if @child is %NULL, the focused child of @container.
1539  *
1540  * This function emits the GtkContainer::set_focus_child signal of
1541  * @container. Implementations of #GtkContainer can override the
1542  * default behaviour by overriding the class closure of this signal.
1543  */
1544 void
1545 gtk_container_set_focus_child (GtkContainer *container,
1546                                GtkWidget    *child)
1547 {
1548   g_return_if_fail (GTK_IS_CONTAINER (container));
1549   if (child)
1550     g_return_if_fail (GTK_IS_WIDGET (child));
1551
1552   g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1553 }
1554
1555 /**
1556  * gtk_container_get_focus_child:
1557  * @container: a #GtkContainer
1558  *
1559  * Returns the current focus child widget inside @container.
1560  *
1561  * Returns: The child widget which has the focus
1562  *          inside @container, or %NULL if none is set.
1563  *
1564  * Since: 2.14
1565  **/
1566 GtkWidget *
1567 gtk_container_get_focus_child (GtkContainer *container)
1568 {
1569   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1570
1571   return container->focus_child;
1572 }
1573
1574 /**
1575  * gtk_container_get_children:
1576  * @container: a #GtkContainer
1577  * 
1578  * Returns the container's non-internal children. See
1579  * gtk_container_forall() for details on what constitutes an "internal" child.
1580  *
1581  * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1582  **/
1583 GList*
1584 gtk_container_get_children (GtkContainer *container)
1585 {
1586   GList *children = NULL;
1587
1588   gtk_container_foreach (container,
1589                          gtk_container_children_callback,
1590                          &children);
1591
1592   return g_list_reverse (children);
1593 }
1594
1595 static void
1596 gtk_container_child_position_callback (GtkWidget *widget,
1597                                        gpointer   client_data)
1598 {
1599   struct {
1600     GtkWidget *child;
1601     guint i;
1602     guint index;
1603   } *data = client_data;
1604
1605   data->i++;
1606   if (data->child == widget)
1607     data->index = data->i;
1608 }
1609
1610 static gchar*
1611 gtk_container_child_default_composite_name (GtkContainer *container,
1612                                             GtkWidget    *child)
1613 {
1614   struct {
1615     GtkWidget *child;
1616     guint i;
1617     guint index;
1618   } data;
1619   gchar *name;
1620
1621   /* fallback implementation */
1622   data.child = child;
1623   data.i = 0;
1624   data.index = 0;
1625   gtk_container_forall (container,
1626                         gtk_container_child_position_callback,
1627                         &data);
1628   
1629   name = g_strdup_printf ("%s-%u",
1630                           g_type_name (G_TYPE_FROM_INSTANCE (child)),
1631                           data.index);
1632
1633   return name;
1634 }
1635
1636 gchar*
1637 _gtk_container_child_composite_name (GtkContainer *container,
1638                                     GtkWidget    *child)
1639 {
1640   gboolean composite_child;
1641
1642   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1643   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1644   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1645
1646   g_object_get (child, "composite-child", &composite_child, NULL);
1647   if (composite_child)
1648     {
1649       static GQuark quark_composite_name = 0;
1650       gchar *name;
1651
1652       if (!quark_composite_name)
1653         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1654
1655       name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1656       if (!name)
1657         {
1658           GtkContainerClass *class;
1659
1660           class = GTK_CONTAINER_GET_CLASS (container);
1661           if (class->composite_name)
1662             name = class->composite_name (container, child);
1663         }
1664       else
1665         name = g_strdup (name);
1666
1667       return name;
1668     }
1669   
1670   return NULL;
1671 }
1672
1673 static void
1674 gtk_container_real_set_focus_child (GtkContainer     *container,
1675                                     GtkWidget        *child)
1676 {
1677   g_return_if_fail (GTK_IS_CONTAINER (container));
1678   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1679
1680   if (child != container->focus_child)
1681     {
1682       if (container->focus_child)
1683         g_object_unref (container->focus_child);
1684       container->focus_child = child;
1685       if (container->focus_child)
1686         g_object_ref (container->focus_child);
1687     }
1688
1689
1690   /* check for h/v adjustments
1691    */
1692   if (container->focus_child)
1693     {
1694       GtkAdjustment *hadj;
1695       GtkAdjustment *vadj;
1696       GtkWidget *focus_child;
1697       gint x, y;
1698
1699       hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);   
1700       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1701       if (hadj || vadj) 
1702         {
1703
1704           focus_child = container->focus_child;
1705           while (GTK_IS_CONTAINER (focus_child) && 
1706                  GTK_CONTAINER (focus_child)->focus_child)
1707             {
1708               focus_child = GTK_CONTAINER (focus_child)->focus_child;
1709             }
1710           
1711           gtk_widget_translate_coordinates (focus_child, container->focus_child, 
1712                                             0, 0, &x, &y);
1713
1714            x += container->focus_child->allocation.x;
1715            y += container->focus_child->allocation.y;
1716           
1717           if (vadj)
1718             gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1719           
1720           if (hadj)
1721             gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1722         }
1723     }
1724 }
1725
1726 static GList*
1727 get_focus_chain (GtkContainer *container)
1728 {
1729   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1730 }
1731
1732 /* same as gtk_container_get_children, except it includes internals
1733  */
1734 static GList *
1735 gtk_container_get_all_children (GtkContainer *container)
1736 {
1737   GList *children = NULL;
1738
1739   gtk_container_forall (container,
1740                          gtk_container_children_callback,
1741                          &children);
1742
1743   return children;
1744 }
1745
1746 static gboolean
1747 gtk_container_focus (GtkWidget        *widget,
1748                      GtkDirectionType  direction)
1749 {
1750   GList *children;
1751   GList *sorted_children;
1752   gint return_val;
1753   GtkContainer *container;
1754
1755   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1756
1757   container = GTK_CONTAINER (widget);
1758
1759   return_val = FALSE;
1760
1761   if (gtk_widget_get_can_focus (widget))
1762     {
1763       if (!gtk_widget_has_focus (widget))
1764         {
1765           gtk_widget_grab_focus (widget);
1766           return_val = TRUE;
1767         }
1768     }
1769   else
1770     {
1771       /* Get a list of the containers children, allowing focus
1772        * chain to override.
1773        */
1774       if (container->has_focus_chain)
1775         children = g_list_copy (get_focus_chain (container));
1776       else
1777         children = gtk_container_get_all_children (container);
1778
1779       if (container->has_focus_chain &&
1780           (direction == GTK_DIR_TAB_FORWARD ||
1781            direction == GTK_DIR_TAB_BACKWARD))
1782         {
1783           sorted_children = g_list_copy (children);
1784           
1785           if (direction == GTK_DIR_TAB_BACKWARD)
1786             sorted_children = g_list_reverse (sorted_children);
1787         }
1788       else
1789         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1790       
1791       return_val = gtk_container_focus_move (container, sorted_children, direction);
1792
1793       g_list_free (sorted_children);
1794       g_list_free (children);
1795     }
1796
1797   return return_val;
1798 }
1799
1800 static gint
1801 tab_compare (gconstpointer a,
1802              gconstpointer b,
1803              gpointer      data)
1804 {
1805   const GtkWidget *child1 = a;
1806   const GtkWidget *child2 = b;
1807   GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1808
1809   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1810   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1811
1812   if (y1 == y2)
1813     {
1814       gint x1 = child1->allocation.x + child1->allocation.width / 2;
1815       gint x2 = child2->allocation.x + child2->allocation.width / 2;
1816       
1817       if (text_direction == GTK_TEXT_DIR_RTL) 
1818         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1819       else
1820         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1821     }
1822   else
1823     return (y1 < y2) ? -1 : 1;
1824 }
1825
1826 static GList *
1827 gtk_container_focus_sort_tab (GtkContainer     *container,
1828                               GList            *children,
1829                               GtkDirectionType  direction,
1830                               GtkWidget        *old_focus)
1831 {
1832   GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1833   children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1834
1835   /* if we are going backwards then reverse the order
1836    *  of the children.
1837    */
1838   if (direction == GTK_DIR_TAB_BACKWARD)
1839     children = g_list_reverse (children);
1840
1841   return children;
1842 }
1843
1844 /* Get coordinates of @widget's allocation with respect to
1845  * allocation of @container.
1846  */
1847 static gboolean
1848 get_allocation_coords (GtkContainer  *container,
1849                        GtkWidget     *widget,
1850                        GdkRectangle  *allocation)
1851 {
1852   *allocation = widget->allocation;
1853
1854   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1855                                            0, 0, &allocation->x, &allocation->y);
1856 }
1857
1858 /* Look for a child in @children that is intermediate between
1859  * the focus widget and container. This widget, if it exists,
1860  * acts as the starting widget for focus navigation.
1861  */
1862 static GtkWidget *
1863 find_old_focus (GtkContainer *container,
1864                 GList        *children)
1865 {
1866   GList *tmp_list = children;
1867   while (tmp_list)
1868     {
1869       GtkWidget *child = tmp_list->data;
1870       GtkWidget *widget = child;
1871
1872       while (widget && widget != (GtkWidget *)container)
1873         {
1874           GtkWidget *parent = widget->parent;
1875           if (parent && ((GtkContainer *)parent)->focus_child != widget)
1876             goto next;
1877
1878           widget = parent;
1879         }
1880
1881       return child;
1882
1883     next:
1884       tmp_list = tmp_list->next;
1885     }
1886
1887   return NULL;
1888 }
1889
1890 static gboolean
1891 old_focus_coords (GtkContainer *container,
1892                   GdkRectangle *old_focus_rect)
1893 {
1894   GtkWidget *widget = GTK_WIDGET (container);
1895   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1896
1897   if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1898     {
1899       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1900       
1901       return get_allocation_coords (container, old_focus, old_focus_rect);
1902     }
1903   else
1904     return FALSE;
1905 }
1906
1907 typedef struct _CompareInfo CompareInfo;
1908
1909 struct _CompareInfo
1910 {
1911   GtkContainer *container;
1912   gint x;
1913   gint y;
1914   gboolean reverse;
1915 };
1916
1917 static gint
1918 up_down_compare (gconstpointer a,
1919                  gconstpointer b,
1920                  gpointer      data)
1921 {
1922   GdkRectangle allocation1;
1923   GdkRectangle allocation2;
1924   CompareInfo *compare = data;
1925   gint y1, y2;
1926
1927   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1928   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1929
1930   y1 = allocation1.y + allocation1.height / 2;
1931   y2 = allocation2.y + allocation2.height / 2;
1932
1933   if (y1 == y2)
1934     {
1935       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1936       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1937
1938       if (compare->reverse)
1939         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1940       else
1941         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1942     }
1943   else
1944     return (y1 < y2) ? -1 : 1;
1945 }
1946
1947 static GList *
1948 gtk_container_focus_sort_up_down (GtkContainer     *container,
1949                                   GList            *children,
1950                                   GtkDirectionType  direction,
1951                                   GtkWidget        *old_focus)
1952 {
1953   CompareInfo compare;
1954   GList *tmp_list;
1955   GdkRectangle old_allocation;
1956
1957   compare.container = container;
1958   compare.reverse = (direction == GTK_DIR_UP);
1959
1960   if (!old_focus)
1961       old_focus = find_old_focus (container, children);
1962   
1963   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1964     {
1965       gint compare_x1;
1966       gint compare_x2;
1967       gint compare_y;
1968
1969       /* Delete widgets from list that don't match minimum criteria */
1970
1971       compare_x1 = old_allocation.x;
1972       compare_x2 = old_allocation.x + old_allocation.width;
1973
1974       if (direction == GTK_DIR_UP)
1975         compare_y = old_allocation.y;
1976       else
1977         compare_y = old_allocation.y + old_allocation.height;
1978       
1979       tmp_list = children;
1980       while (tmp_list)
1981         {
1982           GtkWidget *child = tmp_list->data;
1983           GList *next = tmp_list->next;
1984           gint child_x1, child_x2;
1985           GdkRectangle child_allocation;
1986           
1987           if (child != old_focus)
1988             {
1989               if (get_allocation_coords (container, child, &child_allocation))
1990                 {
1991                   child_x1 = child_allocation.x;
1992                   child_x2 = child_allocation.x + child_allocation.width;
1993                   
1994                   if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1995                       (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1996                       (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1997                     {
1998                       children = g_list_delete_link (children, tmp_list);
1999                     }
2000                 }
2001               else
2002                 children = g_list_delete_link (children, tmp_list);
2003             }
2004           
2005           tmp_list = next;
2006         }
2007
2008       compare.x = (compare_x1 + compare_x2) / 2;
2009       compare.y = old_allocation.y + old_allocation.height / 2;
2010     }
2011   else
2012     {
2013       /* No old focus widget, need to figure out starting x,y some other way
2014        */
2015       GtkWidget *widget = GTK_WIDGET (container);
2016       GdkRectangle old_focus_rect;
2017
2018       if (old_focus_coords (container, &old_focus_rect))
2019         {
2020           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2021         }
2022       else
2023         {
2024           if (!gtk_widget_get_has_window (widget))
2025             compare.x = widget->allocation.x + widget->allocation.width / 2;
2026           else
2027             compare.x = widget->allocation.width / 2;
2028         }
2029       
2030       if (!gtk_widget_get_has_window (widget))
2031         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2032       else
2033         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2034     }
2035
2036   children = g_list_sort_with_data (children, up_down_compare, &compare);
2037
2038   if (compare.reverse)
2039     children = g_list_reverse (children);
2040
2041   return children;
2042 }
2043
2044 static gint
2045 left_right_compare (gconstpointer a,
2046                     gconstpointer b,
2047                     gpointer      data)
2048 {
2049   GdkRectangle allocation1;
2050   GdkRectangle allocation2;
2051   CompareInfo *compare = data;
2052   gint x1, x2;
2053
2054   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2055   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2056
2057   x1 = allocation1.x + allocation1.width / 2;
2058   x2 = allocation2.x + allocation2.width / 2;
2059
2060   if (x1 == x2)
2061     {
2062       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2063       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2064
2065       if (compare->reverse)
2066         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2067       else
2068         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2069     }
2070   else
2071     return (x1 < x2) ? -1 : 1;
2072 }
2073
2074 static GList *
2075 gtk_container_focus_sort_left_right (GtkContainer     *container,
2076                                      GList            *children,
2077                                      GtkDirectionType  direction,
2078                                      GtkWidget        *old_focus)
2079 {
2080   CompareInfo compare;
2081   GList *tmp_list;
2082   GdkRectangle old_allocation;
2083
2084   compare.container = container;
2085   compare.reverse = (direction == GTK_DIR_LEFT);
2086
2087   if (!old_focus)
2088     old_focus = find_old_focus (container, children);
2089   
2090   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2091     {
2092       gint compare_y1;
2093       gint compare_y2;
2094       gint compare_x;
2095       
2096       /* Delete widgets from list that don't match minimum criteria */
2097
2098       compare_y1 = old_allocation.y;
2099       compare_y2 = old_allocation.y + old_allocation.height;
2100
2101       if (direction == GTK_DIR_LEFT)
2102         compare_x = old_allocation.x;
2103       else
2104         compare_x = old_allocation.x + old_allocation.width;
2105       
2106       tmp_list = children;
2107       while (tmp_list)
2108         {
2109           GtkWidget *child = tmp_list->data;
2110           GList *next = tmp_list->next;
2111           gint child_y1, child_y2;
2112           GdkRectangle child_allocation;
2113           
2114           if (child != old_focus)
2115             {
2116               if (get_allocation_coords (container, child, &child_allocation))
2117                 {
2118                   child_y1 = child_allocation.y;
2119                   child_y2 = child_allocation.y + child_allocation.height;
2120                   
2121                   if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2122                       (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2123                       (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2124                     {
2125                       children = g_list_delete_link (children, tmp_list);
2126                     }
2127                 }
2128               else
2129                 children = g_list_delete_link (children, tmp_list);
2130             }
2131           
2132           tmp_list = next;
2133         }
2134
2135       compare.y = (compare_y1 + compare_y2) / 2;
2136       compare.x = old_allocation.x + old_allocation.width / 2;
2137     }
2138   else
2139     {
2140       /* No old focus widget, need to figure out starting x,y some other way
2141        */
2142       GtkWidget *widget = GTK_WIDGET (container);
2143       GdkRectangle old_focus_rect;
2144
2145       if (old_focus_coords (container, &old_focus_rect))
2146         {
2147           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2148         }
2149       else
2150         {
2151           if (!gtk_widget_get_has_window (widget))
2152             compare.y = widget->allocation.y + widget->allocation.height / 2;
2153           else
2154             compare.y = widget->allocation.height / 2;
2155         }
2156       
2157       if (!gtk_widget_get_has_window (widget))
2158         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2159       else
2160         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2161     }
2162
2163   children = g_list_sort_with_data (children, left_right_compare, &compare);
2164
2165   if (compare.reverse)
2166     children = g_list_reverse (children);
2167
2168   return children;
2169 }
2170
2171 /**
2172  * gtk_container_focus_sort:
2173  * @container: a #GtkContainer
2174  * @children:  a list of descendents of @container (they don't
2175  *             have to be direct children)
2176  * @direction: focus direction
2177  * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2178  *             to determine this automatically.
2179  *             (Note, this argument isn't used for GTK_DIR_TAB_*,
2180  *              which is the only @direction we use currently,
2181  *              so perhaps this argument should be removed)
2182  * 
2183  * Sorts @children in the correct order for focusing with
2184  * direction type @direction.
2185  * 
2186  * Return value: a copy of @children, sorted in correct focusing order,
2187  *   with children that aren't suitable for focusing in this direction
2188  *   removed.
2189  **/
2190 GList *
2191 _gtk_container_focus_sort (GtkContainer     *container,
2192                            GList            *children,
2193                            GtkDirectionType  direction,
2194                            GtkWidget        *old_focus)
2195 {
2196   GList *visible_children = NULL;
2197
2198   while (children)
2199     {
2200       if (gtk_widget_get_realized (children->data))
2201         visible_children = g_list_prepend (visible_children, children->data);
2202       children = children->next;
2203     }
2204   
2205   switch (direction)
2206     {
2207     case GTK_DIR_TAB_FORWARD:
2208     case GTK_DIR_TAB_BACKWARD:
2209       return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2210     case GTK_DIR_UP:
2211     case GTK_DIR_DOWN:
2212       return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2213     case GTK_DIR_LEFT:
2214     case GTK_DIR_RIGHT:
2215       return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2216     }
2217
2218   g_assert_not_reached ();
2219
2220   return NULL;
2221 }
2222
2223 static gboolean
2224 gtk_container_focus_move (GtkContainer     *container,
2225                           GList            *children,
2226                           GtkDirectionType  direction)
2227 {
2228   GtkWidget *focus_child;
2229   GtkWidget *child;
2230
2231   focus_child = container->focus_child;
2232
2233   while (children)
2234     {
2235       child = children->data;
2236       children = children->next;
2237
2238       if (!child)
2239         continue;
2240       
2241       if (focus_child)
2242         {
2243           if (focus_child == child)
2244             {
2245               focus_child = NULL;
2246
2247                 if (gtk_widget_child_focus (child, direction))
2248                   return TRUE;
2249             }
2250         }
2251       else if (gtk_widget_is_drawable (child) &&
2252                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2253         {
2254           if (gtk_widget_child_focus (child, direction))
2255             return TRUE;
2256         }
2257     }
2258
2259   return FALSE;
2260 }
2261
2262
2263 static void
2264 gtk_container_children_callback (GtkWidget *widget,
2265                                  gpointer   client_data)
2266 {
2267   GList **children;
2268
2269   children = (GList**) client_data;
2270   *children = g_list_prepend (*children, widget);
2271 }
2272
2273 static void
2274 chain_widget_destroyed (GtkWidget *widget,
2275                         gpointer   user_data)
2276 {
2277   GtkContainer *container;
2278   GList *chain;
2279   
2280   container = GTK_CONTAINER (user_data);
2281
2282   chain = g_object_get_data (G_OBJECT (container),
2283                              "gtk-container-focus-chain");
2284
2285   chain = g_list_remove (chain, widget);
2286
2287   g_signal_handlers_disconnect_by_func (widget,
2288                                         chain_widget_destroyed,
2289                                         user_data);
2290   
2291   g_object_set_data (G_OBJECT (container),
2292                      I_("gtk-container-focus-chain"),
2293                      chain);  
2294 }
2295
2296 /**
2297  * gtk_container_set_focus_chain: 
2298  * @container: a #GtkContainer
2299  * @focusable_widgets: the new focus chain
2300  *
2301  * Sets a focus chain, overriding the one computed automatically by GTK+.
2302  * 
2303  * In principle each widget in the chain should be a descendant of the 
2304  * container, but this is not enforced by this method, since it's allowed 
2305  * to set the focus chain before you pack the widgets, or have a widget 
2306  * in the chain that isn't always packed. The necessary checks are done 
2307  * when the focus chain is actually traversed.
2308  **/
2309 void
2310 gtk_container_set_focus_chain (GtkContainer *container,
2311                                GList        *focusable_widgets)
2312 {
2313   GList *chain;
2314   GList *tmp_list;
2315   
2316   g_return_if_fail (GTK_IS_CONTAINER (container));
2317   
2318   if (container->has_focus_chain)
2319     gtk_container_unset_focus_chain (container);
2320
2321   container->has_focus_chain = TRUE;
2322   
2323   chain = NULL;
2324   tmp_list = focusable_widgets;
2325   while (tmp_list != NULL)
2326     {
2327       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2328       
2329       /* In principle each widget in the chain should be a descendant
2330        * of the container, but we don't want to check that here, it's
2331        * expensive and also it's allowed to set the focus chain before
2332        * you pack the widgets, or have a widget in the chain that isn't
2333        * always packed. So we check for ancestor during actual traversal.
2334        */
2335
2336       chain = g_list_prepend (chain, tmp_list->data);
2337
2338       g_signal_connect (tmp_list->data,
2339                         "destroy",
2340                         G_CALLBACK (chain_widget_destroyed),
2341                         container);
2342       
2343       tmp_list = g_list_next (tmp_list);
2344     }
2345
2346   chain = g_list_reverse (chain);
2347   
2348   g_object_set_data (G_OBJECT (container),
2349                      I_("gtk-container-focus-chain"),
2350                      chain);
2351 }
2352
2353 /**
2354  * gtk_container_get_focus_chain:
2355  * @container:         a #GtkContainer
2356  * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2357  *                     to store the focus chain of the
2358  *                     container, or %NULL. You should free this list
2359  *                     using g_list_free() when you are done with it, however
2360  *                     no additional reference count is added to the
2361  *                     individual widgets in the focus chain.
2362  * 
2363  * Retrieves the focus chain of the container, if one has been
2364  * set explicitly. If no focus chain has been explicitly
2365  * set, GTK+ computes the focus chain based on the positions
2366  * of the children. In that case, GTK+ stores %NULL in
2367  * @focusable_widgets and returns %FALSE.
2368  *
2369  * Return value: %TRUE if the focus chain of the container 
2370  * has been set explicitly.
2371  **/
2372 gboolean
2373 gtk_container_get_focus_chain (GtkContainer *container,
2374                                GList       **focus_chain)
2375 {
2376   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2377
2378   if (focus_chain)
2379     {
2380       if (container->has_focus_chain)
2381         *focus_chain = g_list_copy (get_focus_chain (container));
2382       else
2383         *focus_chain = NULL;
2384     }
2385
2386   return container->has_focus_chain;
2387 }
2388
2389 /**
2390  * gtk_container_unset_focus_chain:
2391  * @container: a #GtkContainer
2392  * 
2393  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2394  **/
2395 void
2396 gtk_container_unset_focus_chain (GtkContainer  *container)
2397 {  
2398   g_return_if_fail (GTK_IS_CONTAINER (container));
2399
2400   if (container->has_focus_chain)
2401     {
2402       GList *chain;
2403       GList *tmp_list;
2404       
2405       chain = get_focus_chain (container);
2406       
2407       container->has_focus_chain = FALSE;
2408       
2409       g_object_set_data (G_OBJECT (container), 
2410                          I_("gtk-container-focus-chain"),
2411                          NULL);
2412
2413       tmp_list = chain;
2414       while (tmp_list != NULL)
2415         {
2416           g_signal_handlers_disconnect_by_func (tmp_list->data,
2417                                                 chain_widget_destroyed,
2418                                                 container);
2419           
2420           tmp_list = g_list_next (tmp_list);
2421         }
2422
2423       g_list_free (chain);
2424     }
2425 }
2426
2427 /**
2428  * gtk_container_set_focus_vadjustment:
2429  * @container: a #GtkContainer
2430  * @adjustment: an adjustment which should be adjusted when the focus 
2431  *   is moved among the descendents of @container
2432  * 
2433  * Hooks up an adjustment to focus handling in a container, so when a 
2434  * child of the container is focused, the adjustment is scrolled to 
2435  * show that widget. This function sets the vertical alignment. See 
2436  * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining 
2437  * the adjustment and gtk_container_set_focus_hadjustment() for setting
2438  * the horizontal adjustment.
2439  *
2440  * The adjustments have to be in pixel units and in the same coordinate 
2441  * system as the allocation for immediate children of the container. 
2442  */
2443 void
2444 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2445                                      GtkAdjustment *adjustment)
2446 {
2447   g_return_if_fail (GTK_IS_CONTAINER (container));
2448   if (adjustment)
2449     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2450
2451   if (adjustment)
2452     g_object_ref (adjustment);
2453
2454   g_object_set_qdata_full (G_OBJECT (container),
2455                            vadjustment_key_id,
2456                            adjustment,
2457                            g_object_unref);
2458 }
2459
2460 /**
2461  * gtk_container_get_focus_vadjustment:
2462  * @container: a #GtkContainer
2463  *
2464  * Retrieves the vertical focus adjustment for the container. See
2465  * gtk_container_set_focus_vadjustment().
2466  *
2467  * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2468  *   none has been set.
2469  **/
2470 GtkAdjustment *
2471 gtk_container_get_focus_vadjustment (GtkContainer *container)
2472 {
2473   GtkAdjustment *vadjustment;
2474     
2475   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2476
2477   vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2478
2479   return vadjustment;
2480 }
2481
2482 /**
2483  * gtk_container_set_focus_hadjustment:
2484  * @container: a #GtkContainer
2485  * @adjustment: an adjustment which should be adjusted when the focus is 
2486  *   moved among the descendents of @container
2487  * 
2488  * Hooks up an adjustment to focus handling in a container, so when a child 
2489  * of the container is focused, the adjustment is scrolled to show that 
2490  * widget. This function sets the horizontal alignment. 
2491  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
2492  * the adjustment and gtk_container_set_focus_vadjustment() for setting
2493  * the vertical adjustment.
2494  *
2495  * The adjustments have to be in pixel units and in the same coordinate 
2496  * system as the allocation for immediate children of the container. 
2497  */
2498 void
2499 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2500                                      GtkAdjustment *adjustment)
2501 {
2502   g_return_if_fail (GTK_IS_CONTAINER (container));
2503   if (adjustment)
2504     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2505
2506   if (adjustment)
2507     g_object_ref (adjustment);
2508
2509   g_object_set_qdata_full (G_OBJECT (container),
2510                            hadjustment_key_id,
2511                            adjustment,
2512                            g_object_unref);
2513 }
2514
2515 /**
2516  * gtk_container_get_focus_hadjustment:
2517  * @container: a #GtkContainer
2518  *
2519  * Retrieves the horizontal focus adjustment for the container. See
2520  * gtk_container_set_focus_hadjustment ().
2521  *
2522  * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
2523  *   none has been set.
2524  **/
2525 GtkAdjustment *
2526 gtk_container_get_focus_hadjustment (GtkContainer *container)
2527 {
2528   GtkAdjustment *hadjustment;
2529
2530   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2531
2532   hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2533
2534   return hadjustment;
2535 }
2536
2537
2538 static void
2539 gtk_container_show_all (GtkWidget *widget)
2540 {
2541   g_return_if_fail (GTK_IS_CONTAINER (widget));
2542
2543   gtk_container_foreach (GTK_CONTAINER (widget),
2544                          (GtkCallback) gtk_widget_show_all,
2545                          NULL);
2546   gtk_widget_show (widget);
2547 }
2548
2549 static void
2550 gtk_container_hide_all (GtkWidget *widget)
2551 {
2552   g_return_if_fail (GTK_IS_CONTAINER (widget));
2553
2554   gtk_widget_hide (widget);
2555   gtk_container_foreach (GTK_CONTAINER (widget),
2556                          (GtkCallback) gtk_widget_hide_all,
2557                          NULL);
2558 }
2559
2560
2561 static void
2562 gtk_container_expose_child (GtkWidget *child,
2563                             gpointer   client_data)
2564 {
2565   struct {
2566     GtkWidget *container;
2567     GdkEventExpose *event;
2568   } *data = client_data;
2569   
2570   gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2571                                   child,
2572                                   data->event);
2573 }
2574
2575 static gint 
2576 gtk_container_expose (GtkWidget      *widget,
2577                       GdkEventExpose *event)
2578 {
2579   struct {
2580     GtkWidget *container;
2581     GdkEventExpose *event;
2582   } data;
2583
2584   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2585   g_return_val_if_fail (event != NULL, FALSE);
2586
2587   
2588   if (gtk_widget_is_drawable (widget))
2589     {
2590       data.container = widget;
2591       data.event = event;
2592       
2593       gtk_container_forall (GTK_CONTAINER (widget),
2594                             gtk_container_expose_child,
2595                             &data);
2596     }   
2597   
2598   return FALSE;
2599 }
2600
2601 static void
2602 gtk_container_map_child (GtkWidget *child,
2603                          gpointer   client_data)
2604 {
2605   if (gtk_widget_get_visible (child) &&
2606       GTK_WIDGET_CHILD_VISIBLE (child) &&
2607       !gtk_widget_get_mapped (child))
2608     gtk_widget_map (child);
2609 }
2610
2611 static void
2612 gtk_container_map (GtkWidget *widget)
2613 {
2614   gtk_widget_set_mapped (widget, TRUE);
2615
2616   gtk_container_forall (GTK_CONTAINER (widget),
2617                         gtk_container_map_child,
2618                         NULL);
2619
2620   if (gtk_widget_get_has_window (widget))
2621     gdk_window_show (widget->window);
2622 }
2623
2624 static void
2625 gtk_container_unmap (GtkWidget *widget)
2626 {
2627   gtk_widget_set_mapped (widget, FALSE);
2628
2629   if (gtk_widget_get_has_window (widget))
2630     gdk_window_hide (widget->window);
2631   else
2632     gtk_container_forall (GTK_CONTAINER (widget),
2633                           (GtkCallback)gtk_widget_unmap,
2634                           NULL);
2635 }
2636
2637 /**
2638  * gtk_container_propagate_expose:
2639  * @container: a #GtkContainer
2640  * @child: a child of @container
2641  * @event: a expose event sent to container
2642  *
2643  * When a container receives an expose event, it must send synthetic
2644  * expose events to all children that don't have their own #GdkWindows.
2645  * This function provides a convenient way of doing this. A container,
2646  * when it receives an expose event, calls gtk_container_propagate_expose() 
2647  * once for each child, passing in the event the container received.
2648  *
2649  * gtk_container_propagate_expose() takes care of deciding whether
2650  * an expose event needs to be sent to the child, intersecting
2651  * the event's area with the child area, and sending the event.
2652  * 
2653  * In most cases, a container can simply either simply inherit the
2654  * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing 
2655  * and then chain to the ::expose implementation from #GtkContainer.
2656  **/
2657 void
2658 gtk_container_propagate_expose (GtkContainer   *container,
2659                                 GtkWidget      *child,
2660                                 GdkEventExpose *event)
2661 {
2662   GdkEvent *child_event;
2663
2664   g_return_if_fail (GTK_IS_CONTAINER (container));
2665   g_return_if_fail (GTK_IS_WIDGET (child));
2666   g_return_if_fail (event != NULL);
2667
2668   g_assert (child->parent == GTK_WIDGET (container));
2669   
2670   if (gtk_widget_is_drawable (child) &&
2671       !gtk_widget_get_has_window (child) &&
2672       (child->window == event->window))
2673     {
2674       child_event = gdk_event_new (GDK_EXPOSE);
2675       child_event->expose = *event;
2676       g_object_ref (child_event->expose.window);
2677
2678       child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2679       if (!cairo_region_is_empty (child_event->expose.region))
2680         {
2681           cairo_region_get_extents (child_event->expose.region, &child_event->expose.area);
2682           gtk_widget_send_expose (child, child_event);
2683         }
2684       gdk_event_free (child_event);
2685     }
2686 }
2687
2688 #define __GTK_CONTAINER_C__
2689 #include "gtkaliasdef.c"