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