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