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