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