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