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