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