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