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