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