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