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