]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Deprecate widget flag: GTK_WIDGET_CAN_FOCUS
[~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 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "gtkcontainer.h"
33 #include "gtkbuildable.h"
34 #include "gtkbuilderprivate.h"
35 #include "gtkprivate.h"
36 #include "gtkmain.h"
37 #include "gtkmarshalers.h"
38 #include "gtkwindow.h"
39 #include "gtkintl.h"
40 #include "gtktoolbar.h"
41 #include <gobject/gobjectnotifyqueue.c>
42 #include <gobject/gvaluecollector.h>
43 #include "gtkalias.h"
44
45
46 enum {
47   ADD,
48   REMOVE,
49   CHECK_RESIZE,
50   SET_FOCUS_CHILD,
51   LAST_SIGNAL
52 };
53
54 enum {
55   PROP_0,
56   PROP_BORDER_WIDTH,
57   PROP_RESIZE_MODE,
58   PROP_CHILD
59 };
60
61 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
62 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
63
64
65 /* --- prototypes --- */
66 static void     gtk_container_base_class_init      (GtkContainerClass *klass);
67 static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
68 static void     gtk_container_class_init           (GtkContainerClass *klass);
69 static void     gtk_container_init                 (GtkContainer      *container);
70 static void     gtk_container_destroy              (GtkObject         *object);
71 static void     gtk_container_set_property         (GObject         *object,
72                                                     guint            prop_id,
73                                                     const GValue    *value,
74                                                     GParamSpec      *pspec);
75 static void     gtk_container_get_property         (GObject         *object,
76                                                     guint            prop_id,
77                                                     GValue          *value,
78                                                     GParamSpec      *pspec);
79 static void     gtk_container_add_unimplemented    (GtkContainer      *container,
80                                                     GtkWidget         *widget);
81 static void     gtk_container_remove_unimplemented (GtkContainer      *container,
82                                                     GtkWidget         *widget);
83 static void     gtk_container_real_check_resize    (GtkContainer      *container);
84 static gboolean gtk_container_focus                (GtkWidget         *widget,
85                                                     GtkDirectionType   direction);
86 static void     gtk_container_real_set_focus_child (GtkContainer      *container,
87                                                     GtkWidget         *widget);
88
89 static gboolean gtk_container_focus_move           (GtkContainer      *container,
90                                                     GList             *children,
91                                                     GtkDirectionType   direction);
92 static void     gtk_container_children_callback    (GtkWidget         *widget,
93                                                     gpointer           client_data);
94 static void     gtk_container_show_all             (GtkWidget         *widget);
95 static void     gtk_container_hide_all             (GtkWidget         *widget);
96 static gint     gtk_container_expose               (GtkWidget         *widget,
97                                                     GdkEventExpose    *event);
98 static void     gtk_container_map                  (GtkWidget         *widget);
99 static void     gtk_container_unmap                (GtkWidget         *widget);
100
101 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
102                                                           GtkWidget    *child);
103
104 /* GtkBuildable */
105 static void gtk_container_buildable_init           (GtkBuildableIface *iface);
106 static void gtk_container_buildable_add_child      (GtkBuildable *buildable,
107                                                     GtkBuilder   *builder,
108                                                     GObject      *child,
109                                                     const gchar  *type);
110 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
111                                                           GtkBuilder    *builder,
112                                                           GObject       *child,
113                                                           const gchar   *tagname,
114                                                           GMarkupParser *parser,
115                                                           gpointer      *data);
116 static void    gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
117                                                        GtkBuilder   *builder,
118                                                        GObject      *child,
119                                                        const gchar  *tagname,
120                                                        gpointer     *data);
121
122
123 /* --- variables --- */
124 static const gchar           vadjustment_key[] = "gtk-vadjustment";
125 static guint                 vadjustment_key_id = 0;
126 static const gchar           hadjustment_key[] = "gtk-hadjustment";
127 static guint                 hadjustment_key_id = 0;
128 static GSList               *container_resize_queue = NULL;
129 static guint                 container_signals[LAST_SIGNAL] = { 0 };
130 static GtkWidgetClass       *parent_class = NULL;
131 extern GParamSpecPool       *_gtk_widget_child_property_pool;
132 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
133 static GtkBuildableIface    *parent_buildable_iface;
134
135
136 /* --- functions --- */
137 GType
138 gtk_container_get_type (void)
139 {
140   static GType container_type = 0;
141
142   if (!container_type)
143     {
144       const GTypeInfo container_info =
145       {
146         sizeof (GtkContainerClass),
147         (GBaseInitFunc) gtk_container_base_class_init,
148         (GBaseFinalizeFunc) gtk_container_base_class_finalize,
149         (GClassInitFunc) gtk_container_class_init,
150         NULL        /* class_finalize */,
151         NULL        /* class_data */,
152         sizeof (GtkContainer),
153         0           /* n_preallocs */,
154         (GInstanceInitFunc) gtk_container_init,
155         NULL,       /* value_table */
156       };
157
158       const GInterfaceInfo buildable_info =
159       {
160         (GInterfaceInitFunc) gtk_container_buildable_init,
161         NULL,
162         NULL
163       };
164
165       container_type =
166         g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"), 
167                                 &container_info, G_TYPE_FLAG_ABSTRACT);
168
169       g_type_add_interface_static (container_type,
170                                    GTK_TYPE_BUILDABLE,
171                                    &buildable_info);
172
173     }
174
175   return container_type;
176 }
177
178 static void
179 gtk_container_base_class_init (GtkContainerClass *class)
180 {
181   /* reset instance specifc class fields that don't get inherited */
182   class->set_child_property = NULL;
183   class->get_child_property = NULL;
184 }
185
186 static void
187 gtk_container_base_class_finalize (GtkContainerClass *class)
188 {
189   GList *list, *node;
190
191   list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
192   for (node = list; node; node = node->next)
193     {
194       GParamSpec *pspec = node->data;
195
196       g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
197       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
198       g_param_spec_unref (pspec);
199     }
200   g_list_free (list);
201 }
202
203 static void
204 gtk_container_class_init (GtkContainerClass *class)
205 {
206   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
207   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
208   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
209
210   parent_class = g_type_class_peek_parent (class);
211
212   vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
213   hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
214   
215   gobject_class->set_property = gtk_container_set_property;
216   gobject_class->get_property = gtk_container_get_property;
217
218   object_class->destroy = gtk_container_destroy;
219
220   widget_class->show_all = gtk_container_show_all;
221   widget_class->hide_all = gtk_container_hide_all;
222   widget_class->expose_event = gtk_container_expose;
223   widget_class->map = gtk_container_map;
224   widget_class->unmap = gtk_container_unmap;
225   widget_class->focus = gtk_container_focus;
226   
227   class->add = gtk_container_add_unimplemented;
228   class->remove = gtk_container_remove_unimplemented;
229   class->check_resize = gtk_container_real_check_resize;
230   class->forall = NULL;
231   class->set_focus_child = gtk_container_real_set_focus_child;
232   class->child_type = NULL;
233   class->composite_name = gtk_container_child_default_composite_name;
234
235   g_object_class_install_property (gobject_class,
236                                    PROP_RESIZE_MODE,
237                                    g_param_spec_enum ("resize-mode",
238                                                       P_("Resize mode"),
239                                                       P_("Specify how resize events are handled"),
240                                                       GTK_TYPE_RESIZE_MODE,
241                                                       GTK_RESIZE_PARENT,
242                                                       GTK_PARAM_READWRITE));
243   g_object_class_install_property (gobject_class,
244                                    PROP_BORDER_WIDTH,
245                                    g_param_spec_uint ("border-width",
246                                                       P_("Border width"),
247                                                       P_("The width of the empty border outside the containers children"),
248                                                       0,
249                                                       G_MAXINT,
250                                                       0,
251                                                       GTK_PARAM_READWRITE));
252   g_object_class_install_property (gobject_class,
253                                    PROP_CHILD,
254                                    g_param_spec_object ("child",
255                                                       P_("Child"),
256                                                       P_("Can be used to add a new child to the container"),
257                                                       GTK_TYPE_WIDGET,
258                                                       GTK_PARAM_WRITABLE));
259   container_signals[ADD] =
260     g_signal_new (I_("add"),
261                   G_OBJECT_CLASS_TYPE (object_class),
262                   G_SIGNAL_RUN_FIRST,
263                   G_STRUCT_OFFSET (GtkContainerClass, add),
264                   NULL, NULL,
265                   _gtk_marshal_VOID__OBJECT,
266                   G_TYPE_NONE, 1,
267                   GTK_TYPE_WIDGET);
268   container_signals[REMOVE] =
269     g_signal_new (I_("remove"),
270                   G_OBJECT_CLASS_TYPE (object_class),
271                   G_SIGNAL_RUN_FIRST,
272                   G_STRUCT_OFFSET (GtkContainerClass, remove),
273                   NULL, NULL,
274                   _gtk_marshal_VOID__OBJECT,
275                   G_TYPE_NONE, 1,
276                   GTK_TYPE_WIDGET);
277   container_signals[CHECK_RESIZE] =
278     g_signal_new (I_("check-resize"),
279                   G_OBJECT_CLASS_TYPE (object_class),
280                   G_SIGNAL_RUN_LAST,
281                   G_STRUCT_OFFSET (GtkContainerClass, check_resize),
282                   NULL, NULL,
283                   _gtk_marshal_VOID__VOID,
284                   G_TYPE_NONE, 0);
285   container_signals[SET_FOCUS_CHILD] =
286     g_signal_new (I_("set-focus-child"),
287                   G_OBJECT_CLASS_TYPE (object_class),
288                   G_SIGNAL_RUN_FIRST,
289                   G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
290                   NULL, NULL,
291                   _gtk_marshal_VOID__OBJECT,
292                   G_TYPE_NONE, 1,
293                   GTK_TYPE_WIDGET);
294 }
295
296 static void
297 gtk_container_buildable_init (GtkBuildableIface *iface)
298 {
299   parent_buildable_iface = g_type_interface_peek_parent (iface);
300   iface->add_child = gtk_container_buildable_add_child;
301   iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
302   iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
303 }
304
305 static void
306 gtk_container_buildable_add_child (GtkBuildable  *buildable,
307                                    GtkBuilder    *builder,
308                                    GObject       *child,
309                                    const gchar   *type)
310 {
311   if (type)
312     {
313       GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
314     }
315   else if (GTK_IS_WIDGET (child) && GTK_WIDGET (child)->parent == NULL)
316     {
317       gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
318     }
319   else
320     g_warning ("Cannot add an object of type %s to a container of type %s", 
321                g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
322 }
323
324 static void
325 gtk_container_buildable_set_child_property (GtkContainer *container,
326                                             GtkBuilder   *builder,
327                                             GtkWidget    *child,
328                                             gchar        *name,
329                                             const gchar  *value)
330 {
331   GParamSpec *pspec;
332   GValue gvalue = { 0, };
333   GError *error = NULL;
334   
335   pspec = gtk_container_class_find_child_property
336     (G_OBJECT_GET_CLASS (container), name);
337   if (!pspec)
338     {
339       g_warning ("%s does not have a property called %s",
340                  g_type_name (G_OBJECT_TYPE (container)), name);
341       return;
342     }
343
344   if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
345     {
346       g_warning ("Could not read property %s:%s with value %s of type %s: %s",
347                  g_type_name (G_OBJECT_TYPE (container)),
348                  name,
349                  value,
350                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
351                  error->message);
352       g_error_free (error);
353       return;
354     }
355
356   gtk_container_child_set_property (container, child, name, &gvalue);
357   g_value_unset (&gvalue);
358 }
359
360 typedef struct {
361   GtkBuilder   *builder;
362   GtkContainer *container;
363   GtkWidget    *child;
364   gchar        *child_prop_name;
365   gchar        *context;
366   gboolean     translatable;
367 } PackingPropertiesData;
368
369 static void
370 attributes_start_element (GMarkupParseContext *context,
371                           const gchar         *element_name,
372                           const gchar        **names,
373                           const gchar        **values,
374                           gpointer             user_data,
375                           GError             **error)
376 {
377   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
378   guint i;
379
380   if (strcmp (element_name, "property") == 0)
381     {
382       for (i = 0; names[i]; i++)
383         if (strcmp (names[i], "name") == 0)
384           parser_data->child_prop_name = g_strdup (values[i]);
385         else if (strcmp (names[i], "translatable") == 0)
386           {
387             if (!_gtk_builder_boolean_from_string (values[1],
388                                                    &parser_data->translatable,
389                                                    error))
390               return;
391           }
392         else if (strcmp (names[i], "comments") == 0)
393           ; /* for translators */
394         else if (strcmp (names[i], "context") == 0)
395           parser_data->context = g_strdup (values[1]);
396         else
397           g_warning ("Unsupported attribute for GtkContainer Child "
398                      "property: %s\n", names[i]);
399     }
400   else if (strcmp (element_name, "packing") == 0)
401     return;
402   else
403     g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
404 }
405
406 static void
407 attributes_text_element (GMarkupParseContext *context,
408                          const gchar         *text,
409                          gsize                text_len,
410                          gpointer             user_data,
411                          GError             **error)
412 {
413   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
414   gchar* value;
415
416   if (!parser_data->child_prop_name)
417     return;
418   
419   if (parser_data->translatable && text_len)
420     {
421       const gchar* domain;
422       domain = gtk_builder_get_translation_domain (parser_data->builder);
423       
424       value = _gtk_builder_parser_translate (domain,
425                                              parser_data->context,
426                                              text);
427     }
428   else
429     {
430       value = g_strdup (text);
431     }
432
433   gtk_container_buildable_set_child_property (parser_data->container,
434                                               parser_data->builder,
435                                               parser_data->child,
436                                               parser_data->child_prop_name,
437                                               value);
438
439   g_free (parser_data->child_prop_name);
440   g_free (parser_data->context);
441   g_free (value);
442   parser_data->child_prop_name = NULL;
443   parser_data->context = NULL;
444   parser_data->translatable = FALSE;
445 }
446
447 static const GMarkupParser attributes_parser =
448   {
449     attributes_start_element,
450     NULL,
451     attributes_text_element,
452   };
453
454 static gboolean
455 gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
456                                           GtkBuilder    *builder,
457                                           GObject       *child,
458                                           const gchar   *tagname,
459                                           GMarkupParser *parser,
460                                           gpointer      *data)
461 {
462   PackingPropertiesData *parser_data;
463
464   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
465                                                 tagname, parser, data))
466     return TRUE;
467
468   if (child && strcmp (tagname, "packing") == 0)
469     {
470       parser_data = g_slice_new0 (PackingPropertiesData);
471       parser_data->builder = builder;
472       parser_data->container = GTK_CONTAINER (buildable);
473       parser_data->child = GTK_WIDGET (child);
474       parser_data->child_prop_name = NULL;
475
476       *parser = attributes_parser;
477       *data = parser_data;
478       return TRUE;
479     }
480
481   return FALSE;
482 }
483
484 static void
485 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
486                                         GtkBuilder   *builder,
487                                         GObject      *child,
488                                         const gchar  *tagname,
489                                         gpointer     *data)
490 {
491   if (strcmp (tagname, "packing") == 0)
492     {
493       g_slice_free (PackingPropertiesData, (gpointer)data);
494       return;
495
496     }
497
498   if (parent_buildable_iface->custom_tag_end)
499     parent_buildable_iface->custom_tag_end (buildable, builder,
500                                             child, tagname, data);
501
502 }
503
504 /**
505  * gtk_container_child_type: 
506  * @container: a #GtkContainer
507  *
508  * Returns the type of the children supported by the container.
509  *
510  * Note that this may return %G_TYPE_NONE to indicate that no more
511  * children can be added, e.g. for a #GtkPaned which already has two 
512  * children.
513  *
514  * Return value: a #GType.
515  **/
516 GType
517 gtk_container_child_type (GtkContainer *container)
518 {
519   GType slot;
520   GtkContainerClass *class;
521
522   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
523
524   class = GTK_CONTAINER_GET_CLASS (container);
525   if (class->child_type)
526     slot = class->child_type (container);
527   else
528     slot = G_TYPE_NONE;
529
530   return slot;
531 }
532
533 /* --- GtkContainer child property mechanism --- */
534 static inline void
535 container_get_child_property (GtkContainer *container,
536                               GtkWidget    *child,
537                               GParamSpec   *pspec,
538                               GValue       *value)
539 {
540   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
541   
542   class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
543 }
544
545 static inline void
546 container_set_child_property (GtkContainer       *container,
547                               GtkWidget          *child,
548                               GParamSpec         *pspec,
549                               const GValue       *value,
550                               GObjectNotifyQueue *nqueue)
551 {
552   GValue tmp_value = { 0, };
553   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
554
555   /* provide a copy to work from, convert (if necessary) and validate */
556   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
557   if (!g_value_transform (value, &tmp_value))
558     g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
559                pspec->name,
560                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
561                G_VALUE_TYPE_NAME (value));
562   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
563     {
564       gchar *contents = g_strdup_value_contents (value);
565
566       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
567                  contents,
568                  G_VALUE_TYPE_NAME (value),
569                  pspec->name,
570                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
571       g_free (contents);
572     }
573   else
574     {
575       class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
576       g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
577     }
578   g_value_unset (&tmp_value);
579 }
580
581 /**
582  * gtk_container_child_get_valist:
583  * @container: a #GtkContainer
584  * @child: a widget which is a child of @container
585  * @first_property_name: the name of the first property to get
586  * @var_args: return location for the first property, followed 
587  *     optionally by more name/return location pairs, followed by %NULL
588  * 
589  * Gets the values of one or more child properties for @child and @container.
590  **/
591 void
592 gtk_container_child_get_valist (GtkContainer *container,
593                                 GtkWidget    *child,
594                                 const gchar  *first_property_name,
595                                 va_list       var_args)
596 {
597   const gchar *name;
598
599   g_return_if_fail (GTK_IS_CONTAINER (container));
600   g_return_if_fail (GTK_IS_WIDGET (child));
601   g_return_if_fail (child->parent == GTK_WIDGET (container));
602
603   g_object_ref (container);
604   g_object_ref (child);
605
606   name = first_property_name;
607   while (name)
608     {
609       GValue value = { 0, };
610       GParamSpec *pspec;
611       gchar *error;
612
613       pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
614                                         name,
615                                         G_OBJECT_TYPE (container),
616                                         TRUE);
617       if (!pspec)
618         {
619           g_warning ("%s: container class `%s' has no child property named `%s'",
620                      G_STRLOC,
621                      G_OBJECT_TYPE_NAME (container),
622                      name);
623           break;
624         }
625       if (!(pspec->flags & G_PARAM_READABLE))
626         {
627           g_warning ("%s: child property `%s' of container class `%s' is not readable",
628                      G_STRLOC,
629                      pspec->name,
630                      G_OBJECT_TYPE_NAME (container));
631           break;
632         }
633       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
634       container_get_child_property (container, child, pspec, &value);
635       G_VALUE_LCOPY (&value, var_args, 0, &error);
636       if (error)
637         {
638           g_warning ("%s: %s", G_STRLOC, error);
639           g_free (error);
640           g_value_unset (&value);
641           break;
642         }
643       g_value_unset (&value);
644       name = va_arg (var_args, gchar*);
645     }
646
647   g_object_unref (child);
648   g_object_unref (container);
649 }
650
651 /**
652  * gtk_container_child_get_property:
653  * @container: a #GtkContainer
654  * @child: a widget which is a child of @container
655  * @property_name: the name of the property to get
656  * @value: a location to return the value
657  * 
658  * Gets the value of a child property for @child and @container.
659  **/
660 void
661 gtk_container_child_get_property (GtkContainer *container,
662                                   GtkWidget    *child,
663                                   const gchar  *property_name,
664                                   GValue       *value)
665 {
666   GParamSpec *pspec;
667
668   g_return_if_fail (GTK_IS_CONTAINER (container));
669   g_return_if_fail (GTK_IS_WIDGET (child));
670   g_return_if_fail (child->parent == GTK_WIDGET (container));
671   g_return_if_fail (property_name != NULL);
672   g_return_if_fail (G_IS_VALUE (value));
673   
674   g_object_ref (container);
675   g_object_ref (child);
676   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
677                                     G_OBJECT_TYPE (container), TRUE);
678   if (!pspec)
679     g_warning ("%s: container class `%s' has no child property named `%s'",
680                G_STRLOC,
681                G_OBJECT_TYPE_NAME (container),
682                property_name);
683   else if (!(pspec->flags & G_PARAM_READABLE))
684     g_warning ("%s: child property `%s' of container class `%s' is not readable",
685                G_STRLOC,
686                pspec->name,
687                G_OBJECT_TYPE_NAME (container));
688   else
689     {
690       GValue *prop_value, tmp_value = { 0, };
691
692       /* auto-conversion of the callers value type
693        */
694       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
695         {
696           g_value_reset (value);
697           prop_value = value;
698         }
699       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
700         {
701           g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
702                      pspec->name,
703                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
704                      G_VALUE_TYPE_NAME (value));
705           g_object_unref (child);
706           g_object_unref (container);
707           return;
708         }
709       else
710         {
711           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
712           prop_value = &tmp_value;
713         }
714       container_get_child_property (container, child, pspec, prop_value);
715       if (prop_value != value)
716         {
717           g_value_transform (prop_value, value);
718           g_value_unset (&tmp_value);
719         }
720     }
721   g_object_unref (child);
722   g_object_unref (container);
723 }
724
725 /**
726  * gtk_container_child_set_valist:
727  * @container: a #GtkContainer
728  * @child: a widget which is a child of @container
729  * @first_property_name: the name of the first property to set
730  * @var_args: a %NULL-terminated list of property names and values, starting
731  *           with @first_prop_name
732  * 
733  * Sets one or more child properties for @child and @container.
734  **/
735 void
736 gtk_container_child_set_valist (GtkContainer *container,
737                                 GtkWidget    *child,
738                                 const gchar  *first_property_name,
739                                 va_list       var_args)
740 {
741   GObjectNotifyQueue *nqueue;
742   const gchar *name;
743
744   g_return_if_fail (GTK_IS_CONTAINER (container));
745   g_return_if_fail (GTK_IS_WIDGET (child));
746   g_return_if_fail (child->parent == GTK_WIDGET (container));
747
748   g_object_ref (container);
749   g_object_ref (child);
750
751   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
752   name = first_property_name;
753   while (name)
754     {
755       GValue value = { 0, };
756       gchar *error = NULL;
757       GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
758                                                     name,
759                                                     G_OBJECT_TYPE (container),
760                                                     TRUE);
761       if (!pspec)
762         {
763           g_warning ("%s: container class `%s' has no child property named `%s'",
764                      G_STRLOC,
765                      G_OBJECT_TYPE_NAME (container),
766                      name);
767           break;
768         }
769       if (!(pspec->flags & G_PARAM_WRITABLE))
770         {
771           g_warning ("%s: child property `%s' of container class `%s' is not writable",
772                      G_STRLOC,
773                      pspec->name,
774                      G_OBJECT_TYPE_NAME (container));
775           break;
776         }
777       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
778       G_VALUE_COLLECT (&value, var_args, 0, &error);
779       if (error)
780         {
781           g_warning ("%s: %s", G_STRLOC, error);
782           g_free (error);
783
784           /* we purposely leak the value here, it might not be
785            * in a sane state if an error condition occoured
786            */
787           break;
788         }
789       container_set_child_property (container, child, pspec, &value, nqueue);
790       g_value_unset (&value);
791       name = va_arg (var_args, gchar*);
792     }
793   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
794
795   g_object_unref (container);
796   g_object_unref (child);
797 }
798
799 /**
800  * gtk_container_child_set_property:
801  * @container: a #GtkContainer
802  * @child: a widget which is a child of @container
803  * @property_name: the name of the property to set
804  * @value: the value to set the property to
805  * 
806  * Sets a child property for @child and @container.
807  **/
808 void
809 gtk_container_child_set_property (GtkContainer *container,
810                                   GtkWidget    *child,
811                                   const gchar  *property_name,
812                                   const GValue *value)
813 {
814   GObjectNotifyQueue *nqueue;
815   GParamSpec *pspec;
816
817   g_return_if_fail (GTK_IS_CONTAINER (container));
818   g_return_if_fail (GTK_IS_WIDGET (child));
819   g_return_if_fail (child->parent == GTK_WIDGET (container));
820   g_return_if_fail (property_name != NULL);
821   g_return_if_fail (G_IS_VALUE (value));
822   
823   g_object_ref (container);
824   g_object_ref (child);
825
826   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
827   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
828                                     G_OBJECT_TYPE (container), TRUE);
829   if (!pspec)
830     g_warning ("%s: container class `%s' has no child property named `%s'",
831                G_STRLOC,
832                G_OBJECT_TYPE_NAME (container),
833                property_name);
834   else if (!(pspec->flags & G_PARAM_WRITABLE))
835     g_warning ("%s: child property `%s' of container class `%s' is not writable",
836                G_STRLOC,
837                pspec->name,
838                G_OBJECT_TYPE_NAME (container));
839   else
840     {
841       container_set_child_property (container, child, pspec, value, nqueue);
842     }
843   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
844   g_object_unref (container);
845   g_object_unref (child);
846 }
847
848 /**
849  * gtk_container_add_with_properties:
850  * @container: a #GtkContainer 
851  * @widget: a widget to be placed inside @container 
852  * @first_prop_name: the name of the first child property to set 
853  * @Varargs: a %NULL-terminated list of property names and values, starting
854  *           with @first_prop_name
855  * 
856  * Adds @widget to @container, setting child properties at the same time.
857  * See gtk_container_add() and gtk_container_child_set() for more details.
858  **/
859 void
860 gtk_container_add_with_properties (GtkContainer *container,
861                                    GtkWidget    *widget,
862                                    const gchar  *first_prop_name,
863                                    ...)
864 {
865   g_return_if_fail (GTK_IS_CONTAINER (container));
866   g_return_if_fail (GTK_IS_WIDGET (widget));
867   g_return_if_fail (widget->parent == NULL);
868
869   g_object_ref (container);
870   g_object_ref (widget);
871   gtk_widget_freeze_child_notify (widget);
872
873   g_signal_emit (container, container_signals[ADD], 0, widget);
874   if (widget->parent)
875     {
876       va_list var_args;
877
878       va_start (var_args, first_prop_name);
879       gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
880       va_end (var_args);
881     }
882
883   gtk_widget_thaw_child_notify (widget);
884   g_object_unref (widget);
885   g_object_unref (container);
886 }
887
888 /**
889  * gtk_container_child_set:
890  * @container: a #GtkContainer
891  * @child: a widget which is a child of @container
892  * @first_prop_name: the name of the first property to set
893  * @Varargs: a %NULL-terminated list of property names and values, starting
894  *           with @first_prop_name
895  * 
896  * Sets one or more child properties for @child and @container.
897  **/
898 void
899 gtk_container_child_set (GtkContainer      *container,
900                          GtkWidget         *child,
901                          const gchar       *first_prop_name,
902                          ...)
903 {
904   va_list var_args;
905   
906   g_return_if_fail (GTK_IS_CONTAINER (container));
907   g_return_if_fail (GTK_IS_WIDGET (child));
908   g_return_if_fail (child->parent == GTK_WIDGET (container));
909
910   va_start (var_args, first_prop_name);
911   gtk_container_child_set_valist (container, child, first_prop_name, var_args);
912   va_end (var_args);
913 }
914
915 /**
916  * gtk_container_child_get:
917  * @container: a #GtkContainer
918  * @child: a widget which is a child of @container
919  * @first_prop_name: the name of the first property to get
920  * @Varargs: return location for the first property, followed 
921  *     optionally by more name/return location pairs, followed by %NULL
922  * 
923  * Gets the values of one or more child properties for @child and @container.
924  **/
925 void
926 gtk_container_child_get (GtkContainer      *container,
927                          GtkWidget         *child,
928                          const gchar       *first_prop_name,
929                          ...)
930 {
931   va_list var_args;
932   
933   g_return_if_fail (GTK_IS_CONTAINER (container));
934   g_return_if_fail (GTK_IS_WIDGET (child));
935   g_return_if_fail (child->parent == GTK_WIDGET (container));
936
937   va_start (var_args, first_prop_name);
938   gtk_container_child_get_valist (container, child, first_prop_name, var_args);
939   va_end (var_args);
940 }
941
942 /**
943  * gtk_container_class_install_child_property:
944  * @cclass: a #GtkContainerClass
945  * @property_id: the id for the property
946  * @pspec: the #GParamSpec for the property
947  * 
948  * Installs a child property on a container class. 
949  **/
950 void
951 gtk_container_class_install_child_property (GtkContainerClass *cclass,
952                                             guint              property_id,
953                                             GParamSpec        *pspec)
954 {
955   g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
956   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
957   if (pspec->flags & G_PARAM_WRITABLE)
958     g_return_if_fail (cclass->set_child_property != NULL);
959   if (pspec->flags & G_PARAM_READABLE)
960     g_return_if_fail (cclass->get_child_property != NULL);
961   g_return_if_fail (property_id > 0);
962   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
963   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
964     g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
965
966   if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
967     {
968       g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
969                  G_OBJECT_CLASS_NAME (cclass),
970                  pspec->name);
971       return;
972     }
973   g_param_spec_ref (pspec);
974   g_param_spec_sink (pspec);
975   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
976   g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
977 }
978
979 /**
980  * gtk_container_class_find_child_property:
981  * @cclass: a #GtkContainerClass
982  * @property_name: the name of the child property to find
983  * @returns: the #GParamSpec of the child property or %NULL if @class has no
984  *   child property with that name.
985  *
986  * Finds a child property of a container class by name.
987  */
988 GParamSpec*
989 gtk_container_class_find_child_property (GObjectClass *cclass,
990                                          const gchar  *property_name)
991 {
992   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
993   g_return_val_if_fail (property_name != NULL, NULL);
994
995   return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
996                                    property_name,
997                                    G_OBJECT_CLASS_TYPE (cclass),
998                                    TRUE);
999 }
1000
1001 /**
1002  * gtk_container_class_list_child_properties:
1003  * @cclass: a #GtkContainerClass
1004  * @n_properties: location to return the number of child properties found
1005  * @returns: a newly allocated %NULL-terminated array of #GParamSpec*. 
1006  *           The array must be freed with g_free().
1007  *
1008  * Returns all child properties of a container class.
1009  */
1010 GParamSpec**
1011 gtk_container_class_list_child_properties (GObjectClass *cclass,
1012                                            guint        *n_properties)
1013 {
1014   GParamSpec **pspecs;
1015   guint n;
1016
1017   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1018
1019   pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1020                                    G_OBJECT_CLASS_TYPE (cclass),
1021                                    &n);
1022   if (n_properties)
1023     *n_properties = n;
1024
1025   return pspecs;
1026 }
1027
1028 static void
1029 gtk_container_add_unimplemented (GtkContainer     *container,
1030                                  GtkWidget        *widget)
1031 {
1032   g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1033 }
1034
1035 static void
1036 gtk_container_remove_unimplemented (GtkContainer     *container,
1037                                     GtkWidget        *widget)
1038 {
1039   g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1040 }
1041
1042 static void
1043 gtk_container_init (GtkContainer *container)
1044 {
1045   container->focus_child = NULL;
1046   container->border_width = 0;
1047   container->need_resize = FALSE;
1048   container->resize_mode = GTK_RESIZE_PARENT;
1049   container->reallocate_redraws = FALSE;
1050 }
1051
1052 static void
1053 gtk_container_destroy (GtkObject *object)
1054 {
1055   GtkContainer *container = GTK_CONTAINER (object);
1056
1057   if (GTK_CONTAINER_RESIZE_PENDING (container))
1058     _gtk_container_dequeue_resize_handler (container);
1059
1060   /* do this before walking child widgets, to avoid
1061    * removing children from focus chain one by one.
1062    */
1063   if (container->has_focus_chain)
1064     gtk_container_unset_focus_chain (container);
1065
1066   gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1067
1068   GTK_OBJECT_CLASS (parent_class)->destroy (object);
1069 }
1070
1071 static void
1072 gtk_container_set_property (GObject         *object,
1073                             guint            prop_id,
1074                             const GValue    *value,
1075                             GParamSpec      *pspec)
1076 {
1077   GtkContainer *container = GTK_CONTAINER (object);
1078
1079   switch (prop_id)
1080     {
1081     case PROP_BORDER_WIDTH:
1082       gtk_container_set_border_width (container, g_value_get_uint (value));
1083       break;
1084     case PROP_RESIZE_MODE:
1085       gtk_container_set_resize_mode (container, g_value_get_enum (value));
1086       break;
1087     case PROP_CHILD:
1088       gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1089       break;
1090     default:
1091       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1092       break;
1093     }
1094 }
1095
1096 static void
1097 gtk_container_get_property (GObject         *object,
1098                             guint            prop_id,
1099                             GValue          *value,
1100                             GParamSpec      *pspec)
1101 {
1102   GtkContainer *container = GTK_CONTAINER (object);
1103   
1104   switch (prop_id)
1105     {
1106     case PROP_BORDER_WIDTH:
1107       g_value_set_uint (value, container->border_width);
1108       break;
1109     case PROP_RESIZE_MODE:
1110       g_value_set_enum (value, container->resize_mode);
1111       break;
1112     default:
1113       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1114       break;
1115     }
1116 }
1117
1118 /**
1119  * gtk_container_set_border_width:
1120  * @container: a #GtkContainer
1121  * @border_width: amount of blank space to leave <emphasis>outside</emphasis> 
1122  *   the container. Valid values are in the range 0-65535 pixels.
1123  *
1124  * Sets the border width of the container.
1125  *
1126  * The border width of a container is the amount of space to leave
1127  * around the outside of the container. The only exception to this is
1128  * #GtkWindow; because toplevel windows can't leave space outside,
1129  * they leave the space inside. The border is added on all sides of
1130  * the container. To add space to only one side, one approach is to
1131  * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1132  * to give it a size, and place it on the side of the container as
1133  * a spacer.
1134  **/
1135 void
1136 gtk_container_set_border_width (GtkContainer *container,
1137                                 guint         border_width)
1138 {
1139   g_return_if_fail (GTK_IS_CONTAINER (container));
1140
1141   if (container->border_width != border_width)
1142     {
1143       container->border_width = border_width;
1144       g_object_notify (G_OBJECT (container), "border-width");
1145       
1146       if (GTK_WIDGET_REALIZED (container))
1147         gtk_widget_queue_resize (GTK_WIDGET (container));
1148     }
1149 }
1150
1151 /**
1152  * gtk_container_get_border_width:
1153  * @container: a #GtkContainer
1154  * 
1155  * Retrieves the border width of the container. See
1156  * gtk_container_set_border_width().
1157  *
1158  * Return value: the current border width
1159  **/
1160 guint
1161 gtk_container_get_border_width (GtkContainer *container)
1162 {
1163   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1164
1165   return container->border_width;
1166 }
1167
1168 /**
1169  * gtk_container_add:
1170  * @container: a #GtkContainer
1171  * @widget: a widget to be placed inside @container
1172  * 
1173  * Adds @widget to @container. Typically used for simple containers
1174  * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1175  * layout containers such as #GtkBox or #GtkTable, this function will
1176  * pick default packing parameters that may not be correct.  So
1177  * consider functions such as gtk_box_pack_start() and
1178  * gtk_table_attach() as an alternative to gtk_container_add() in
1179  * those cases. A widget may be added to only one container at a time;
1180  * you can't place the same widget inside two different containers.
1181  **/
1182 void
1183 gtk_container_add (GtkContainer *container,
1184                    GtkWidget    *widget)
1185 {
1186   g_return_if_fail (GTK_IS_CONTAINER (container));
1187   g_return_if_fail (GTK_IS_WIDGET (widget));
1188
1189   if (widget->parent != NULL)
1190     {
1191       g_warning ("Attempting to add a widget with type %s to a container of "
1192                  "type %s, but the widget is already inside a container of type %s, "
1193                  "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1194                  "explains how to reparent a widget.",
1195                  g_type_name (G_OBJECT_TYPE (widget)),
1196                  g_type_name (G_OBJECT_TYPE (container)),
1197                  g_type_name (G_OBJECT_TYPE (widget->parent)));
1198       return;
1199     }
1200
1201   g_signal_emit (container, container_signals[ADD], 0, widget);
1202 }
1203
1204 /**
1205  * gtk_container_remove:
1206  * @container: a #GtkContainer
1207  * @widget: a current child of @container
1208  * 
1209  * Removes @widget from @container. @widget must be inside @container.
1210  * Note that @container will own a reference to @widget, and that this
1211  * may be the last reference held; so removing a widget from its
1212  * container can destroy that widget. If you want to use @widget
1213  * again, you need to add a reference to it while it's not inside
1214  * a container, using g_object_ref(). If you don't want to use @widget
1215  * again it's usually more efficient to simply destroy it directly
1216  * using gtk_widget_destroy() since this will remove it from the
1217  * container and help break any circular reference count cycles.
1218  **/
1219 void
1220 gtk_container_remove (GtkContainer *container,
1221                       GtkWidget    *widget)
1222 {
1223   g_return_if_fail (GTK_IS_CONTAINER (container));
1224   g_return_if_fail (GTK_IS_WIDGET (widget));
1225
1226   /* When using the deprecated API of the toolbar, it is possible
1227    * to legitimately call this function with a widget that is not
1228    * a direct child of the container.
1229    */
1230   g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1231                     widget->parent == GTK_WIDGET (container));
1232   
1233   g_signal_emit (container, container_signals[REMOVE], 0, widget);
1234 }
1235
1236 void
1237 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1238 {
1239   g_return_if_fail (GTK_IS_CONTAINER (container));
1240   g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1241
1242   container_resize_queue = g_slist_remove (container_resize_queue, container);
1243   GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1244 }
1245
1246 /**
1247  * gtk_container_set_resize_mode:
1248  * @container: a #GtkContainer
1249  * @resize_mode: the new resize mode
1250  * 
1251  * Sets the resize mode for the container.
1252  *
1253  * The resize mode of a container determines whether a resize request 
1254  * will be passed to the container's parent, queued for later execution
1255  * or executed immediately.
1256  **/
1257 void
1258 gtk_container_set_resize_mode (GtkContainer  *container,
1259                                GtkResizeMode  resize_mode)
1260 {
1261   g_return_if_fail (GTK_IS_CONTAINER (container));
1262   g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1263   
1264   if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1265       resize_mode == GTK_RESIZE_PARENT)
1266     {
1267       resize_mode = GTK_RESIZE_QUEUE;
1268     }
1269   
1270   if (container->resize_mode != resize_mode)
1271     {
1272       container->resize_mode = resize_mode;
1273       
1274       gtk_widget_queue_resize (GTK_WIDGET (container));
1275       g_object_notify (G_OBJECT (container), "resize-mode");
1276     }
1277 }
1278
1279 /**
1280  * gtk_container_get_resize_mode:
1281  * @container: a #GtkContainer
1282  * 
1283  * Returns the resize mode for the container. See
1284  * gtk_container_set_resize_mode ().
1285  *
1286  * Return value: the current resize mode
1287  **/
1288 GtkResizeMode
1289 gtk_container_get_resize_mode (GtkContainer *container)
1290 {
1291   g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1292
1293   return container->resize_mode;
1294 }
1295
1296 /**
1297  * gtk_container_set_reallocate_redraws:
1298  * @container: a #GtkContainer
1299  * @needs_redraws: the new value for the container's @reallocate_redraws flag
1300  *
1301  * Sets the @reallocate_redraws flag of the container to the given value.
1302  * 
1303  * Containers requesting reallocation redraws get automatically
1304  * redrawn if any of their children changed allocation. 
1305  **/ 
1306 void
1307 gtk_container_set_reallocate_redraws (GtkContainer *container,
1308                                       gboolean      needs_redraws)
1309 {
1310   g_return_if_fail (GTK_IS_CONTAINER (container));
1311
1312   container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1313 }
1314
1315 static GtkContainer*
1316 gtk_container_get_resize_container (GtkContainer *container)
1317 {
1318   GtkWidget *widget = GTK_WIDGET (container);
1319
1320   while (widget->parent)
1321     {
1322       widget = widget->parent;
1323       if (GTK_IS_RESIZE_CONTAINER (widget))
1324         break;
1325     }
1326
1327   return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1328 }
1329
1330 static gboolean
1331 gtk_container_idle_sizer (gpointer data)
1332 {
1333   /* we may be invoked with a container_resize_queue of NULL, because
1334    * queue_resize could have been adding an extra idle function while
1335    * the queue still got processed. we better just ignore such case
1336    * than trying to explicitely work around them with some extra flags,
1337    * since it doesn't cause any actual harm.
1338    */
1339   while (container_resize_queue)
1340     {
1341       GSList *slist;
1342       GtkWidget *widget;
1343
1344       slist = container_resize_queue;
1345       container_resize_queue = slist->next;
1346       widget = slist->data;
1347       g_slist_free_1 (slist);
1348
1349       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1350       gtk_container_check_resize (GTK_CONTAINER (widget));
1351     }
1352
1353   gdk_window_process_all_updates ();
1354
1355   return FALSE;
1356 }
1357
1358 void
1359 _gtk_container_queue_resize (GtkContainer *container)
1360 {
1361   GtkContainer *resize_container;
1362   GtkWidget *widget;
1363   
1364   g_return_if_fail (GTK_IS_CONTAINER (container));
1365
1366   widget = GTK_WIDGET (container);
1367   resize_container = gtk_container_get_resize_container (container);
1368   
1369   while (TRUE)
1370     {
1371       GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1372       GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1373       if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1374           !widget->parent)
1375         break;
1376       
1377       widget = widget->parent;
1378     }
1379       
1380   if (resize_container)
1381     {
1382       if (GTK_WIDGET_VISIBLE (resize_container) &&
1383           (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) || GTK_WIDGET_REALIZED (resize_container)))
1384         {
1385           switch (resize_container->resize_mode)
1386             {
1387             case GTK_RESIZE_QUEUE:
1388               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1389                 {
1390                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1391                   if (container_resize_queue == NULL)
1392                     gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1393                                      gtk_container_idle_sizer,
1394                                      NULL, NULL);
1395                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1396                 }
1397               break;
1398
1399             case GTK_RESIZE_IMMEDIATE:
1400               gtk_container_check_resize (resize_container);
1401               break;
1402
1403             case GTK_RESIZE_PARENT:
1404               g_assert_not_reached ();
1405               break;
1406             }
1407         }
1408       else
1409         {
1410           /* we need to let hidden resize containers know that something
1411            * changed while they where hidden (currently only evaluated by
1412            * toplevels).
1413            */
1414           resize_container->need_resize = TRUE;
1415         }
1416     }
1417 }
1418
1419 void
1420 gtk_container_check_resize (GtkContainer *container)
1421 {
1422   g_return_if_fail (GTK_IS_CONTAINER (container));
1423   
1424   g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1425 }
1426
1427 static void
1428 gtk_container_real_check_resize (GtkContainer *container)
1429 {
1430   GtkWidget *widget = GTK_WIDGET (container);
1431   GtkRequisition requisition;
1432   
1433   gtk_widget_size_request (widget, &requisition);
1434   
1435   if (requisition.width > widget->allocation.width ||
1436       requisition.height > widget->allocation.height)
1437     {
1438       if (GTK_IS_RESIZE_CONTAINER (container))
1439         gtk_widget_size_allocate (GTK_WIDGET (container),
1440                                   &GTK_WIDGET (container)->allocation);
1441       else
1442         gtk_widget_queue_resize (widget);
1443     }
1444   else
1445     {
1446       gtk_container_resize_children (container);
1447     }
1448 }
1449
1450 /* The container hasn't changed size but one of its children
1451  *  queued a resize request. Which means that the allocation
1452  *  is not sufficient for the requisition of some child.
1453  *  We've already performed a size request at this point,
1454  *  so we simply need to reallocate and let the allocation
1455  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1456  */
1457 void
1458 gtk_container_resize_children (GtkContainer *container)
1459 {
1460   GtkWidget *widget;
1461   
1462   /* resizing invariants:
1463    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1464    * containers that have an idle sizer pending must be flagged with
1465    * RESIZE_PENDING.
1466    */
1467   g_return_if_fail (GTK_IS_CONTAINER (container));
1468
1469   widget = GTK_WIDGET (container);
1470   gtk_widget_size_allocate (widget, &widget->allocation);
1471 }
1472
1473 /**
1474  * gtk_container_forall:
1475  * @container: a #GtkContainer
1476  * @callback: a callback
1477  * @callback_data: callback user data
1478  * 
1479  * Invokes @callback on each child of @container, including children
1480  * that are considered "internal" (implementation details of the
1481  * container). "Internal" children generally weren't added by the user
1482  * of the container, but were added by the container implementation
1483  * itself.  Most applications should use gtk_container_foreach(),
1484  * rather than gtk_container_forall().
1485  **/
1486 void
1487 gtk_container_forall (GtkContainer *container,
1488                       GtkCallback   callback,
1489                       gpointer      callback_data)
1490 {
1491   GtkContainerClass *class;
1492
1493   g_return_if_fail (GTK_IS_CONTAINER (container));
1494   g_return_if_fail (callback != NULL);
1495
1496   class = GTK_CONTAINER_GET_CLASS (container);
1497
1498   if (class->forall)
1499     class->forall (container, TRUE, callback, callback_data);
1500 }
1501
1502 /**
1503  * gtk_container_foreach:
1504  * @container: a #GtkContainer
1505  * @callback: a callback
1506  * @callback_data: callback user data
1507  * 
1508  * Invokes @callback on each non-internal child of @container. See
1509  * gtk_container_forall() for details on what constitutes an
1510  * "internal" child.  Most applications should use
1511  * gtk_container_foreach(), rather than gtk_container_forall().
1512  **/
1513 void
1514 gtk_container_foreach (GtkContainer *container,
1515                        GtkCallback   callback,
1516                        gpointer      callback_data)
1517 {
1518   GtkContainerClass *class;
1519   
1520   g_return_if_fail (GTK_IS_CONTAINER (container));
1521   g_return_if_fail (callback != NULL);
1522
1523   class = GTK_CONTAINER_GET_CLASS (container);
1524
1525   if (class->forall)
1526     class->forall (container, FALSE, callback, callback_data);
1527 }
1528
1529 typedef struct _GtkForeachData  GtkForeachData;
1530 struct _GtkForeachData
1531 {
1532   GtkObject         *container;
1533   GtkCallbackMarshal callback;
1534   gpointer           callback_data;
1535 };
1536
1537 static void
1538 gtk_container_foreach_unmarshal (GtkWidget *child,
1539                                  gpointer data)
1540 {
1541   GtkForeachData *fdata = (GtkForeachData*) data;
1542   GtkArg args[2];
1543   
1544   /* first argument */
1545   args[0].name = NULL;
1546   args[0].type = G_TYPE_FROM_INSTANCE (child);
1547   GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1548   
1549   /* location for return value */
1550   args[1].name = NULL;
1551   args[1].type = G_TYPE_NONE;
1552   
1553   fdata->callback (fdata->container, fdata->callback_data, 1, args);
1554 }
1555
1556 void
1557 gtk_container_foreach_full (GtkContainer       *container,
1558                             GtkCallback         callback,
1559                             GtkCallbackMarshal  marshal,
1560                             gpointer            callback_data,
1561                             GDestroyNotify      notify)
1562 {
1563   g_return_if_fail (GTK_IS_CONTAINER (container));
1564
1565   if (marshal)
1566     {
1567       GtkForeachData fdata;
1568   
1569       fdata.container     = GTK_OBJECT (container);
1570       fdata.callback      = marshal;
1571       fdata.callback_data = callback_data;
1572
1573       gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1574     }
1575   else
1576     {
1577       g_return_if_fail (callback != NULL);
1578
1579       gtk_container_foreach (container, callback, &callback_data);
1580     }
1581
1582   if (notify)
1583     notify (callback_data);
1584 }
1585
1586 /**
1587  * gtk_container_set_focus_child:
1588  * @container: a #GtkContainer
1589  * @child: (allow-none): a #GtkWidget, or %NULL
1590  *
1591  * Sets, or unsets if @child is %NULL, the focused child of @container.
1592  *
1593  * This function emits the GtkContainer::set_focus_child signal of
1594  * @container. Implementations of #GtkContainer can override the
1595  * default behaviour by overriding the class closure of this signal.
1596  */
1597 void
1598 gtk_container_set_focus_child (GtkContainer *container,
1599                                GtkWidget    *child)
1600 {
1601   g_return_if_fail (GTK_IS_CONTAINER (container));
1602   if (child)
1603     g_return_if_fail (GTK_IS_WIDGET (child));
1604
1605   g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1606 }
1607
1608 /**
1609  * gtk_container_get_focus_child:
1610  * @container: a #GtkContainer
1611  *
1612  * Returns the current focus child widget inside @container.
1613  *
1614  * Returns: The child widget which has the focus
1615  *          inside @container, or %NULL if none is set.
1616  *
1617  * Since: 2.14
1618  **/
1619 GtkWidget *
1620 gtk_container_get_focus_child (GtkContainer *container)
1621 {
1622   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1623
1624   return container->focus_child;
1625 }
1626
1627 /**
1628  * gtk_container_get_children:
1629  * @container: a #GtkContainer
1630  * 
1631  * Returns the container's non-internal children. See
1632  * gtk_container_forall() for details on what constitutes an "internal" child.
1633  *
1634  * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1635  **/
1636 GList*
1637 gtk_container_get_children (GtkContainer *container)
1638 {
1639   GList *children = NULL;
1640
1641   gtk_container_foreach (container,
1642                          gtk_container_children_callback,
1643                          &children);
1644
1645   return g_list_reverse (children);
1646 }
1647
1648 static void
1649 gtk_container_child_position_callback (GtkWidget *widget,
1650                                        gpointer   client_data)
1651 {
1652   struct {
1653     GtkWidget *child;
1654     guint i;
1655     guint index;
1656   } *data = client_data;
1657
1658   data->i++;
1659   if (data->child == widget)
1660     data->index = data->i;
1661 }
1662
1663 static gchar*
1664 gtk_container_child_default_composite_name (GtkContainer *container,
1665                                             GtkWidget    *child)
1666 {
1667   struct {
1668     GtkWidget *child;
1669     guint i;
1670     guint index;
1671   } data;
1672   gchar *name;
1673
1674   /* fallback implementation */
1675   data.child = child;
1676   data.i = 0;
1677   data.index = 0;
1678   gtk_container_forall (container,
1679                         gtk_container_child_position_callback,
1680                         &data);
1681   
1682   name = g_strdup_printf ("%s-%u",
1683                           g_type_name (G_TYPE_FROM_INSTANCE (child)),
1684                           data.index);
1685
1686   return name;
1687 }
1688
1689 gchar*
1690 _gtk_container_child_composite_name (GtkContainer *container,
1691                                     GtkWidget    *child)
1692 {
1693   gboolean composite_child;
1694
1695   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1696   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1697   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1698
1699   g_object_get (child, "composite-child", &composite_child, NULL);
1700   if (composite_child)
1701     {
1702       static GQuark quark_composite_name = 0;
1703       gchar *name;
1704
1705       if (!quark_composite_name)
1706         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1707
1708       name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1709       if (!name)
1710         {
1711           GtkContainerClass *class;
1712
1713           class = GTK_CONTAINER_GET_CLASS (container);
1714           if (class->composite_name)
1715             name = class->composite_name (container, child);
1716         }
1717       else
1718         name = g_strdup (name);
1719
1720       return name;
1721     }
1722   
1723   return NULL;
1724 }
1725
1726 static void
1727 gtk_container_real_set_focus_child (GtkContainer     *container,
1728                                     GtkWidget        *child)
1729 {
1730   g_return_if_fail (GTK_IS_CONTAINER (container));
1731   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1732
1733   if (child != container->focus_child)
1734     {
1735       if (container->focus_child)
1736         g_object_unref (container->focus_child);
1737       container->focus_child = child;
1738       if (container->focus_child)
1739         g_object_ref (container->focus_child);
1740     }
1741
1742
1743   /* check for h/v adjustments
1744    */
1745   if (container->focus_child)
1746     {
1747       GtkAdjustment *hadj;
1748       GtkAdjustment *vadj;
1749       GtkWidget *focus_child;
1750       gint x, y;
1751
1752       hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);   
1753       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1754       if (hadj || vadj) 
1755         {
1756
1757           focus_child = container->focus_child;
1758           while (GTK_IS_CONTAINER (focus_child) && 
1759                  GTK_CONTAINER (focus_child)->focus_child)
1760             {
1761               focus_child = GTK_CONTAINER (focus_child)->focus_child;
1762             }
1763           
1764           gtk_widget_translate_coordinates (focus_child, container->focus_child, 
1765                                             0, 0, &x, &y);
1766
1767            x += container->focus_child->allocation.x;
1768            y += container->focus_child->allocation.y;
1769           
1770           if (vadj)
1771             gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1772           
1773           if (hadj)
1774             gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1775         }
1776     }
1777 }
1778
1779 static GList*
1780 get_focus_chain (GtkContainer *container)
1781 {
1782   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1783 }
1784
1785 /* same as gtk_container_get_children, except it includes internals
1786  */
1787 static GList *
1788 gtk_container_get_all_children (GtkContainer *container)
1789 {
1790   GList *children = NULL;
1791
1792   gtk_container_forall (container,
1793                          gtk_container_children_callback,
1794                          &children);
1795
1796   return children;
1797 }
1798
1799 static gboolean
1800 gtk_container_focus (GtkWidget        *widget,
1801                      GtkDirectionType  direction)
1802 {
1803   GList *children;
1804   GList *sorted_children;
1805   gint return_val;
1806   GtkContainer *container;
1807
1808   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1809
1810   container = GTK_CONTAINER (widget);
1811
1812   return_val = FALSE;
1813
1814   if (gtk_widget_get_can_focus (GTK_WIDGET (container)))
1815     {
1816       if (!GTK_WIDGET_HAS_FOCUS (container))
1817         {
1818           gtk_widget_grab_focus (GTK_WIDGET (container));
1819           return_val = TRUE;
1820         }
1821     }
1822   else
1823     {
1824       /* Get a list of the containers children, allowing focus
1825        * chain to override.
1826        */
1827       if (container->has_focus_chain)
1828         children = g_list_copy (get_focus_chain (container));
1829       else
1830         children = gtk_container_get_all_children (container);
1831
1832       if (container->has_focus_chain &&
1833           (direction == GTK_DIR_TAB_FORWARD ||
1834            direction == GTK_DIR_TAB_BACKWARD))
1835         {
1836           sorted_children = g_list_copy (children);
1837           
1838           if (direction == GTK_DIR_TAB_BACKWARD)
1839             sorted_children = g_list_reverse (sorted_children);
1840         }
1841       else
1842         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1843       
1844       return_val = gtk_container_focus_move (container, sorted_children, direction);
1845
1846       g_list_free (sorted_children);
1847       g_list_free (children);
1848     }
1849
1850   return return_val;
1851 }
1852
1853 static gint
1854 tab_compare (gconstpointer a,
1855              gconstpointer b,
1856              gpointer      data)
1857 {
1858   const GtkWidget *child1 = a;
1859   const GtkWidget *child2 = b;
1860   GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1861
1862   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1863   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1864
1865   if (y1 == y2)
1866     {
1867       gint x1 = child1->allocation.x + child1->allocation.width / 2;
1868       gint x2 = child2->allocation.x + child2->allocation.width / 2;
1869       
1870       if (text_direction == GTK_TEXT_DIR_RTL) 
1871         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1872       else
1873         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1874     }
1875   else
1876     return (y1 < y2) ? -1 : 1;
1877 }
1878
1879 static GList *
1880 gtk_container_focus_sort_tab (GtkContainer     *container,
1881                               GList            *children,
1882                               GtkDirectionType  direction,
1883                               GtkWidget        *old_focus)
1884 {
1885   GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1886   children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1887
1888   /* if we are going backwards then reverse the order
1889    *  of the children.
1890    */
1891   if (direction == GTK_DIR_TAB_BACKWARD)
1892     children = g_list_reverse (children);
1893
1894   return children;
1895 }
1896
1897 /* Get coordinates of @widget's allocation with respect to
1898  * allocation of @container.
1899  */
1900 static gboolean
1901 get_allocation_coords (GtkContainer  *container,
1902                        GtkWidget     *widget,
1903                        GdkRectangle  *allocation)
1904 {
1905   *allocation = widget->allocation;
1906
1907   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1908                                            0, 0, &allocation->x, &allocation->y);
1909 }
1910
1911 /* Look for a child in @children that is intermediate between
1912  * the focus widget and container. This widget, if it exists,
1913  * acts as the starting widget for focus navigation.
1914  */
1915 static GtkWidget *
1916 find_old_focus (GtkContainer *container,
1917                 GList        *children)
1918 {
1919   GList *tmp_list = children;
1920   while (tmp_list)
1921     {
1922       GtkWidget *child = tmp_list->data;
1923       GtkWidget *widget = child;
1924
1925       while (widget && widget != (GtkWidget *)container)
1926         {
1927           GtkWidget *parent = widget->parent;
1928           if (parent && ((GtkContainer *)parent)->focus_child != widget)
1929             goto next;
1930
1931           widget = parent;
1932         }
1933
1934       return child;
1935
1936     next:
1937       tmp_list = tmp_list->next;
1938     }
1939
1940   return NULL;
1941 }
1942
1943 static gboolean
1944 old_focus_coords (GtkContainer *container,
1945                   GdkRectangle *old_focus_rect)
1946 {
1947   GtkWidget *widget = GTK_WIDGET (container);
1948   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1949
1950   if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1951     {
1952       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1953       
1954       return get_allocation_coords (container, old_focus, old_focus_rect);
1955     }
1956   else
1957     return FALSE;
1958 }
1959
1960 typedef struct _CompareInfo CompareInfo;
1961
1962 struct _CompareInfo
1963 {
1964   GtkContainer *container;
1965   gint x;
1966   gint y;
1967   gboolean reverse;
1968 };
1969
1970 static gint
1971 up_down_compare (gconstpointer a,
1972                  gconstpointer b,
1973                  gpointer      data)
1974 {
1975   GdkRectangle allocation1;
1976   GdkRectangle allocation2;
1977   CompareInfo *compare = data;
1978   gint y1, y2;
1979
1980   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1981   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1982
1983   y1 = allocation1.y + allocation1.height / 2;
1984   y2 = allocation2.y + allocation2.height / 2;
1985
1986   if (y1 == y2)
1987     {
1988       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1989       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1990
1991       if (compare->reverse)
1992         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1993       else
1994         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1995     }
1996   else
1997     return (y1 < y2) ? -1 : 1;
1998 }
1999
2000 static GList *
2001 gtk_container_focus_sort_up_down (GtkContainer     *container,
2002                                   GList            *children,
2003                                   GtkDirectionType  direction,
2004                                   GtkWidget        *old_focus)
2005 {
2006   CompareInfo compare;
2007   GList *tmp_list;
2008   GdkRectangle old_allocation;
2009
2010   compare.container = container;
2011   compare.reverse = (direction == GTK_DIR_UP);
2012
2013   if (!old_focus)
2014       old_focus = find_old_focus (container, children);
2015   
2016   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2017     {
2018       gint compare_x1;
2019       gint compare_x2;
2020       gint compare_y;
2021
2022       /* Delete widgets from list that don't match minimum criteria */
2023
2024       compare_x1 = old_allocation.x;
2025       compare_x2 = old_allocation.x + old_allocation.width;
2026
2027       if (direction == GTK_DIR_UP)
2028         compare_y = old_allocation.y;
2029       else
2030         compare_y = old_allocation.y + old_allocation.height;
2031       
2032       tmp_list = children;
2033       while (tmp_list)
2034         {
2035           GtkWidget *child = tmp_list->data;
2036           GList *next = tmp_list->next;
2037           gint child_x1, child_x2;
2038           GdkRectangle child_allocation;
2039           
2040           if (child != old_focus)
2041             {
2042               if (get_allocation_coords (container, child, &child_allocation))
2043                 {
2044                   child_x1 = child_allocation.x;
2045                   child_x2 = child_allocation.x + child_allocation.width;
2046                   
2047                   if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2048                       (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2049                       (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2050                     {
2051                       children = g_list_delete_link (children, tmp_list);
2052                     }
2053                 }
2054               else
2055                 children = g_list_delete_link (children, tmp_list);
2056             }
2057           
2058           tmp_list = next;
2059         }
2060
2061       compare.x = (compare_x1 + compare_x2) / 2;
2062       compare.y = old_allocation.y + old_allocation.height / 2;
2063     }
2064   else
2065     {
2066       /* No old focus widget, need to figure out starting x,y some other way
2067        */
2068       GtkWidget *widget = GTK_WIDGET (container);
2069       GdkRectangle old_focus_rect;
2070
2071       if (old_focus_coords (container, &old_focus_rect))
2072         {
2073           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2074         }
2075       else
2076         {
2077           if (!gtk_widget_get_has_window (widget))
2078             compare.x = widget->allocation.x + widget->allocation.width / 2;
2079           else
2080             compare.x = widget->allocation.width / 2;
2081         }
2082       
2083       if (!gtk_widget_get_has_window (widget))
2084         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2085       else
2086         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2087     }
2088
2089   children = g_list_sort_with_data (children, up_down_compare, &compare);
2090
2091   if (compare.reverse)
2092     children = g_list_reverse (children);
2093
2094   return children;
2095 }
2096
2097 static gint
2098 left_right_compare (gconstpointer a,
2099                     gconstpointer b,
2100                     gpointer      data)
2101 {
2102   GdkRectangle allocation1;
2103   GdkRectangle allocation2;
2104   CompareInfo *compare = data;
2105   gint x1, x2;
2106
2107   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2108   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2109
2110   x1 = allocation1.x + allocation1.width / 2;
2111   x2 = allocation2.x + allocation2.width / 2;
2112
2113   if (x1 == x2)
2114     {
2115       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2116       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2117
2118       if (compare->reverse)
2119         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2120       else
2121         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2122     }
2123   else
2124     return (x1 < x2) ? -1 : 1;
2125 }
2126
2127 static GList *
2128 gtk_container_focus_sort_left_right (GtkContainer     *container,
2129                                      GList            *children,
2130                                      GtkDirectionType  direction,
2131                                      GtkWidget        *old_focus)
2132 {
2133   CompareInfo compare;
2134   GList *tmp_list;
2135   GdkRectangle old_allocation;
2136
2137   compare.container = container;
2138   compare.reverse = (direction == GTK_DIR_LEFT);
2139
2140   if (!old_focus)
2141     old_focus = find_old_focus (container, children);
2142   
2143   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2144     {
2145       gint compare_y1;
2146       gint compare_y2;
2147       gint compare_x;
2148       
2149       /* Delete widgets from list that don't match minimum criteria */
2150
2151       compare_y1 = old_allocation.y;
2152       compare_y2 = old_allocation.y + old_allocation.height;
2153
2154       if (direction == GTK_DIR_LEFT)
2155         compare_x = old_allocation.x;
2156       else
2157         compare_x = old_allocation.x + old_allocation.width;
2158       
2159       tmp_list = children;
2160       while (tmp_list)
2161         {
2162           GtkWidget *child = tmp_list->data;
2163           GList *next = tmp_list->next;
2164           gint child_y1, child_y2;
2165           GdkRectangle child_allocation;
2166           
2167           if (child != old_focus)
2168             {
2169               if (get_allocation_coords (container, child, &child_allocation))
2170                 {
2171                   child_y1 = child_allocation.y;
2172                   child_y2 = child_allocation.y + child_allocation.height;
2173                   
2174                   if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2175                       (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2176                       (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2177                     {
2178                       children = g_list_delete_link (children, tmp_list);
2179                     }
2180                 }
2181               else
2182                 children = g_list_delete_link (children, tmp_list);
2183             }
2184           
2185           tmp_list = next;
2186         }
2187
2188       compare.y = (compare_y1 + compare_y2) / 2;
2189       compare.x = old_allocation.x + old_allocation.width / 2;
2190     }
2191   else
2192     {
2193       /* No old focus widget, need to figure out starting x,y some other way
2194        */
2195       GtkWidget *widget = GTK_WIDGET (container);
2196       GdkRectangle old_focus_rect;
2197
2198       if (old_focus_coords (container, &old_focus_rect))
2199         {
2200           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2201         }
2202       else
2203         {
2204           if (!gtk_widget_get_has_window (widget))
2205             compare.y = widget->allocation.y + widget->allocation.height / 2;
2206           else
2207             compare.y = widget->allocation.height / 2;
2208         }
2209       
2210       if (!gtk_widget_get_has_window (widget))
2211         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2212       else
2213         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2214     }
2215
2216   children = g_list_sort_with_data (children, left_right_compare, &compare);
2217
2218   if (compare.reverse)
2219     children = g_list_reverse (children);
2220
2221   return children;
2222 }
2223
2224 /**
2225  * gtk_container_focus_sort:
2226  * @container: a #GtkContainer
2227  * @children:  a list of descendents of @container (they don't
2228  *             have to be direct children)
2229  * @direction: focus direction
2230  * @old_focus: widget to use for the starting position, or %NULL
2231  *             to determine this automatically.
2232  *             (Note, this argument isn't used for GTK_DIR_TAB_*,
2233  *              which is the only @direction we use currently,
2234  *              so perhaps this argument should be removed)
2235  * 
2236  * Sorts @children in the correct order for focusing with
2237  * direction type @direction.
2238  * 
2239  * Return value: a copy of @children, sorted in correct focusing order,
2240  *   with children that aren't suitable for focusing in this direction
2241  *   removed.
2242  **/
2243 GList *
2244 _gtk_container_focus_sort (GtkContainer     *container,
2245                            GList            *children,
2246                            GtkDirectionType  direction,
2247                            GtkWidget        *old_focus)
2248 {
2249   GList *visible_children = NULL;
2250
2251   while (children)
2252     {
2253       if (GTK_WIDGET_REALIZED (children->data))
2254         visible_children = g_list_prepend (visible_children, children->data);
2255       children = children->next;
2256     }
2257   
2258   switch (direction)
2259     {
2260     case GTK_DIR_TAB_FORWARD:
2261     case GTK_DIR_TAB_BACKWARD:
2262       return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2263     case GTK_DIR_UP:
2264     case GTK_DIR_DOWN:
2265       return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2266     case GTK_DIR_LEFT:
2267     case GTK_DIR_RIGHT:
2268       return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2269     }
2270
2271   g_assert_not_reached ();
2272
2273   return NULL;
2274 }
2275
2276 static gboolean
2277 gtk_container_focus_move (GtkContainer     *container,
2278                           GList            *children,
2279                           GtkDirectionType  direction)
2280 {
2281   GtkWidget *focus_child;
2282   GtkWidget *child;
2283
2284   focus_child = container->focus_child;
2285
2286   while (children)
2287     {
2288       child = children->data;
2289       children = children->next;
2290
2291       if (!child)
2292         continue;
2293       
2294       if (focus_child)
2295         {
2296           if (focus_child == child)
2297             {
2298               focus_child = NULL;
2299
2300                 if (gtk_widget_child_focus (child, direction))
2301                   return TRUE;
2302             }
2303         }
2304       else if (GTK_WIDGET_DRAWABLE (child) &&
2305                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2306         {
2307           if (gtk_widget_child_focus (child, direction))
2308             return TRUE;
2309         }
2310     }
2311
2312   return FALSE;
2313 }
2314
2315
2316 static void
2317 gtk_container_children_callback (GtkWidget *widget,
2318                                  gpointer   client_data)
2319 {
2320   GList **children;
2321
2322   children = (GList**) client_data;
2323   *children = g_list_prepend (*children, widget);
2324 }
2325
2326 static void
2327 chain_widget_destroyed (GtkWidget *widget,
2328                         gpointer   user_data)
2329 {
2330   GtkContainer *container;
2331   GList *chain;
2332   
2333   container = GTK_CONTAINER (user_data);
2334
2335   chain = g_object_get_data (G_OBJECT (container),
2336                              "gtk-container-focus-chain");
2337
2338   chain = g_list_remove (chain, widget);
2339
2340   g_signal_handlers_disconnect_by_func (widget,
2341                                         chain_widget_destroyed,
2342                                         user_data);
2343   
2344   g_object_set_data (G_OBJECT (container),
2345                      I_("gtk-container-focus-chain"),
2346                      chain);  
2347 }
2348
2349 /**
2350  * gtk_container_set_focus_chain: 
2351  * @container: a #GtkContainer
2352  * @focusable_widgets: the new focus chain
2353  *
2354  * Sets a focus chain, overriding the one computed automatically by GTK+.
2355  * 
2356  * In principle each widget in the chain should be a descendant of the 
2357  * container, but this is not enforced by this method, since it's allowed 
2358  * to set the focus chain before you pack the widgets, or have a widget 
2359  * in the chain that isn't always packed. The necessary checks are done 
2360  * when the focus chain is actually traversed.
2361  **/
2362 void
2363 gtk_container_set_focus_chain (GtkContainer *container,
2364                                GList        *focusable_widgets)
2365 {
2366   GList *chain;
2367   GList *tmp_list;
2368   
2369   g_return_if_fail (GTK_IS_CONTAINER (container));
2370   
2371   if (container->has_focus_chain)
2372     gtk_container_unset_focus_chain (container);
2373
2374   container->has_focus_chain = TRUE;
2375   
2376   chain = NULL;
2377   tmp_list = focusable_widgets;
2378   while (tmp_list != NULL)
2379     {
2380       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2381       
2382       /* In principle each widget in the chain should be a descendant
2383        * of the container, but we don't want to check that here, it's
2384        * expensive and also it's allowed to set the focus chain before
2385        * you pack the widgets, or have a widget in the chain that isn't
2386        * always packed. So we check for ancestor during actual traversal.
2387        */
2388
2389       chain = g_list_prepend (chain, tmp_list->data);
2390
2391       g_signal_connect (tmp_list->data,
2392                         "destroy",
2393                         G_CALLBACK (chain_widget_destroyed),
2394                         container);
2395       
2396       tmp_list = g_list_next (tmp_list);
2397     }
2398
2399   chain = g_list_reverse (chain);
2400   
2401   g_object_set_data (G_OBJECT (container),
2402                      I_("gtk-container-focus-chain"),
2403                      chain);
2404 }
2405
2406 /**
2407  * gtk_container_get_focus_chain:
2408  * @container:         a #GtkContainer
2409  * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2410  *                     to store the focus chain of the
2411  *                     container, or %NULL. You should free this list
2412  *                     using g_list_free() when you are done with it, however
2413  *                     no additional reference count is added to the
2414  *                     individual widgets in the focus chain.
2415  * 
2416  * Retrieves the focus chain of the container, if one has been
2417  * set explicitly. If no focus chain has been explicitly
2418  * set, GTK+ computes the focus chain based on the positions
2419  * of the children. In that case, GTK+ stores %NULL in
2420  * @focusable_widgets and returns %FALSE.
2421  *
2422  * Return value: %TRUE if the focus chain of the container 
2423  * has been set explicitly.
2424  **/
2425 gboolean
2426 gtk_container_get_focus_chain (GtkContainer *container,
2427                                GList       **focus_chain)
2428 {
2429   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2430
2431   if (focus_chain)
2432     {
2433       if (container->has_focus_chain)
2434         *focus_chain = g_list_copy (get_focus_chain (container));
2435       else
2436         *focus_chain = NULL;
2437     }
2438
2439   return container->has_focus_chain;
2440 }
2441
2442 /**
2443  * gtk_container_unset_focus_chain:
2444  * @container: a #GtkContainer
2445  * 
2446  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2447  **/
2448 void
2449 gtk_container_unset_focus_chain (GtkContainer  *container)
2450 {  
2451   g_return_if_fail (GTK_IS_CONTAINER (container));
2452
2453   if (container->has_focus_chain)
2454     {
2455       GList *chain;
2456       GList *tmp_list;
2457       
2458       chain = get_focus_chain (container);
2459       
2460       container->has_focus_chain = FALSE;
2461       
2462       g_object_set_data (G_OBJECT (container), 
2463                          I_("gtk-container-focus-chain"),
2464                          NULL);
2465
2466       tmp_list = chain;
2467       while (tmp_list != NULL)
2468         {
2469           g_signal_handlers_disconnect_by_func (tmp_list->data,
2470                                                 chain_widget_destroyed,
2471                                                 container);
2472           
2473           tmp_list = g_list_next (tmp_list);
2474         }
2475
2476       g_list_free (chain);
2477     }
2478 }
2479
2480 /**
2481  * gtk_container_set_focus_vadjustment:
2482  * @container: a #GtkContainer
2483  * @adjustment: an adjustment which should be adjusted when the focus 
2484  *   is moved among the descendents of @container
2485  * 
2486  * Hooks up an adjustment to focus handling in a container, so when a 
2487  * child of the container is focused, the adjustment is scrolled to 
2488  * show that widget. This function sets the vertical alignment. See 
2489  * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining 
2490  * the adjustment and gtk_container_set_focus_hadjustment() for setting
2491  * the horizontal adjustment.
2492  *
2493  * The adjustments have to be in pixel units and in the same coordinate 
2494  * system as the allocation for immediate children of the container. 
2495  */
2496 void
2497 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2498                                      GtkAdjustment *adjustment)
2499 {
2500   g_return_if_fail (GTK_IS_CONTAINER (container));
2501   if (adjustment)
2502     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2503
2504   if (adjustment)
2505     g_object_ref (adjustment);
2506
2507   g_object_set_qdata_full (G_OBJECT (container),
2508                            vadjustment_key_id,
2509                            adjustment,
2510                            g_object_unref);
2511 }
2512
2513 /**
2514  * gtk_container_get_focus_vadjustment:
2515  * @container: a #GtkContainer
2516  *
2517  * Retrieves the vertical focus adjustment for the container. See
2518  * gtk_container_set_focus_vadjustment().
2519  *
2520  * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2521  *   none has been set.
2522  **/
2523 GtkAdjustment *
2524 gtk_container_get_focus_vadjustment (GtkContainer *container)
2525 {
2526   GtkAdjustment *vadjustment;
2527     
2528   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2529
2530   vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2531
2532   return vadjustment;
2533 }
2534
2535 /**
2536  * gtk_container_set_focus_hadjustment:
2537  * @container: a #GtkContainer
2538  * @adjustment: an adjustment which should be adjusted when the focus is 
2539  *   moved among the descendents of @container
2540  * 
2541  * Hooks up an adjustment to focus handling in a container, so when a child 
2542  * of the container is focused, the adjustment is scrolled to show that 
2543  * widget. This function sets the horizontal alignment. 
2544  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
2545  * the adjustment and gtk_container_set_focus_vadjustment() for setting
2546  * the vertical adjustment.
2547  *
2548  * The adjustments have to be in pixel units and in the same coordinate 
2549  * system as the allocation for immediate children of the container. 
2550  */
2551 void
2552 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2553                                      GtkAdjustment *adjustment)
2554 {
2555   g_return_if_fail (GTK_IS_CONTAINER (container));
2556   if (adjustment)
2557     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2558
2559   if (adjustment)
2560     g_object_ref (adjustment);
2561
2562   g_object_set_qdata_full (G_OBJECT (container),
2563                            hadjustment_key_id,
2564                            adjustment,
2565                            g_object_unref);
2566 }
2567
2568 /**
2569  * gtk_container_get_focus_hadjustment:
2570  * @container: a #GtkContainer
2571  *
2572  * Retrieves the horizontal focus adjustment for the container. See
2573  * gtk_container_set_focus_hadjustment ().
2574  *
2575  * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
2576  *   none has been set.
2577  **/
2578 GtkAdjustment *
2579 gtk_container_get_focus_hadjustment (GtkContainer *container)
2580 {
2581   GtkAdjustment *hadjustment;
2582
2583   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2584
2585   hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2586
2587   return hadjustment;
2588 }
2589
2590
2591 static void
2592 gtk_container_show_all (GtkWidget *widget)
2593 {
2594   g_return_if_fail (GTK_IS_CONTAINER (widget));
2595
2596   gtk_container_foreach (GTK_CONTAINER (widget),
2597                          (GtkCallback) gtk_widget_show_all,
2598                          NULL);
2599   gtk_widget_show (widget);
2600 }
2601
2602 static void
2603 gtk_container_hide_all (GtkWidget *widget)
2604 {
2605   g_return_if_fail (GTK_IS_CONTAINER (widget));
2606
2607   gtk_widget_hide (widget);
2608   gtk_container_foreach (GTK_CONTAINER (widget),
2609                          (GtkCallback) gtk_widget_hide_all,
2610                          NULL);
2611 }
2612
2613
2614 static void
2615 gtk_container_expose_child (GtkWidget *child,
2616                             gpointer   client_data)
2617 {
2618   struct {
2619     GtkWidget *container;
2620     GdkEventExpose *event;
2621   } *data = client_data;
2622   
2623   gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2624                                   child,
2625                                   data->event);
2626 }
2627
2628 static gint 
2629 gtk_container_expose (GtkWidget      *widget,
2630                       GdkEventExpose *event)
2631 {
2632   struct {
2633     GtkWidget *container;
2634     GdkEventExpose *event;
2635   } data;
2636
2637   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2638   g_return_val_if_fail (event != NULL, FALSE);
2639
2640   
2641   if (GTK_WIDGET_DRAWABLE (widget)) 
2642     {
2643       data.container = widget;
2644       data.event = event;
2645       
2646       gtk_container_forall (GTK_CONTAINER (widget),
2647                             gtk_container_expose_child,
2648                             &data);
2649     }   
2650   
2651   return FALSE;
2652 }
2653
2654 static void
2655 gtk_container_map_child (GtkWidget *child,
2656                          gpointer   client_data)
2657 {
2658   if (GTK_WIDGET_VISIBLE (child) &&
2659       GTK_WIDGET_CHILD_VISIBLE (child) &&
2660       !GTK_WIDGET_MAPPED (child))
2661     gtk_widget_map (child);
2662 }
2663
2664 static void
2665 gtk_container_map (GtkWidget *widget)
2666 {
2667   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2668
2669   gtk_container_forall (GTK_CONTAINER (widget),
2670                         gtk_container_map_child,
2671                         NULL);
2672
2673   if (gtk_widget_get_has_window (widget))
2674     gdk_window_show (widget->window);
2675 }
2676
2677 static void
2678 gtk_container_unmap (GtkWidget *widget)
2679 {
2680   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2681
2682   if (gtk_widget_get_has_window (widget))
2683     gdk_window_hide (widget->window);
2684   else
2685     gtk_container_forall (GTK_CONTAINER (widget),
2686                           (GtkCallback)gtk_widget_unmap,
2687                           NULL);
2688 }
2689
2690 /**
2691  * gtk_container_propagate_expose:
2692  * @container: a #GtkContainer
2693  * @child: a child of @container
2694  * @event: a expose event sent to container
2695  *
2696  * When a container receives an expose event, it must send synthetic
2697  * expose events to all children that don't have their own #GdkWindows.
2698  * This function provides a convenient way of doing this. A container,
2699  * when it receives an expose event, calls gtk_container_propagate_expose() 
2700  * once for each child, passing in the event the container received.
2701  *
2702  * gtk_container_propagate_expose() takes care of deciding whether
2703  * an expose event needs to be sent to the child, intersecting
2704  * the event's area with the child area, and sending the event.
2705  * 
2706  * In most cases, a container can simply either simply inherit the
2707  * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing 
2708  * and then chain to the ::expose implementation from #GtkContainer.
2709  **/
2710 void
2711 gtk_container_propagate_expose (GtkContainer   *container,
2712                                 GtkWidget      *child,
2713                                 GdkEventExpose *event)
2714 {
2715   GdkEvent *child_event;
2716
2717   g_return_if_fail (GTK_IS_CONTAINER (container));
2718   g_return_if_fail (GTK_IS_WIDGET (child));
2719   g_return_if_fail (event != NULL);
2720
2721   g_assert (child->parent == GTK_WIDGET (container));
2722   
2723   if (GTK_WIDGET_DRAWABLE (child) &&
2724       !gtk_widget_get_has_window (child) &&
2725       (child->window == event->window))
2726     {
2727       child_event = gdk_event_new (GDK_EXPOSE);
2728       child_event->expose = *event;
2729       g_object_ref (child_event->expose.window);
2730
2731       child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2732       if (!gdk_region_empty (child_event->expose.region))
2733         {
2734           gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2735           gtk_widget_send_expose (child, child_event);
2736         }
2737       gdk_event_free (child_event);
2738     }
2739 }
2740
2741 #define __GTK_CONTAINER_C__
2742 #include "gtkaliasdef.c"