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