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