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