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