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