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