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