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