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