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