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