]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
include gdkkeysysms.h if compiling gdk_keyval_convert_case.
[~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   
1016   g_return_if_fail (GTK_IS_CONTAINER (container));
1017
1018   resize_container = gtk_container_get_resize_container (container);
1019   
1020   if (resize_container)
1021     {
1022       GtkWidget *widget = GTK_WIDGET (container);
1023       
1024       while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1025         {
1026           GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1027           GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1028           if (widget == GTK_WIDGET (resize_container))
1029             break;
1030           
1031           widget = widget->parent;
1032         }
1033       
1034       if (GTK_WIDGET_VISIBLE (resize_container) &&
1035           (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1036         {
1037           switch (resize_container->resize_mode)
1038             {
1039             case GTK_RESIZE_QUEUE:
1040               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1041                 {
1042                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1043                   if (container_resize_queue == NULL)
1044                     gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1045                                            gtk_container_idle_sizer,
1046                                            NULL);
1047                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1048                 }
1049               break;
1050
1051             case GTK_RESIZE_IMMEDIATE:
1052               gtk_container_check_resize (resize_container);
1053               break;
1054
1055             case GTK_RESIZE_PARENT:
1056               g_assert_not_reached ();
1057               break;
1058             }
1059         }
1060       else
1061         {
1062           /* we need to let hidden resize containers know that something
1063            * changed while they where hidden (currently only evaluated by
1064            * toplevels).
1065            */
1066           resize_container->need_resize = TRUE;
1067         }
1068     }
1069 }
1070
1071 void
1072 gtk_container_check_resize (GtkContainer *container)
1073 {
1074   g_return_if_fail (GTK_IS_CONTAINER (container));
1075   
1076   gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1077 }
1078
1079 static void
1080 gtk_container_real_check_resize (GtkContainer *container)
1081 {
1082   GtkWidget *widget = GTK_WIDGET (container);
1083   GtkRequisition requisition;
1084   
1085   gtk_widget_size_request (widget, &requisition);
1086   
1087   if (requisition.width > widget->allocation.width ||
1088       requisition.height > widget->allocation.height)
1089     {
1090       if (GTK_IS_RESIZE_CONTAINER (container))
1091         gtk_widget_size_allocate (GTK_WIDGET (container),
1092                                   &GTK_WIDGET (container)->allocation);
1093       else
1094         gtk_widget_queue_resize (widget);
1095     }
1096   else
1097     {
1098       gtk_container_resize_children (container);
1099     }
1100 }
1101
1102 /* The container hasn't changed size but one of its children
1103  *  queued a resize request. Which means that the allocation
1104  *  is not sufficient for the requisition of some child.
1105  *  We've already performed a size request at this point,
1106  *  so we simply need to reallocate and let the allocation
1107  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1108  */
1109 void
1110 gtk_container_resize_children (GtkContainer *container)
1111 {
1112   GtkWidget *widget;
1113   
1114   /* resizing invariants:
1115    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1116    * containers that have an idle sizer pending must be flagged with
1117    * RESIZE_PENDING.
1118    */
1119   g_return_if_fail (GTK_IS_CONTAINER (container));
1120
1121   widget = GTK_WIDGET (container);
1122   gtk_widget_size_allocate (widget, &widget->allocation);
1123 }
1124
1125 /**
1126  * gtk_container_forall:
1127  * @container: a #GtkContainer
1128  * @callback: a callback
1129  * @callback_data: callback user data
1130  * 
1131  * Invokes @callback on each child of @container, including children
1132  * that are considered "internal" (implementation details of the
1133  * container). "Internal" children generally weren't added by the user
1134  * of the container, but were added by the container implementation
1135  * itself.  Most applications should use gtk_container_foreach(),
1136  * rather than gtk_container_forall().
1137  **/
1138 void
1139 gtk_container_forall (GtkContainer *container,
1140                       GtkCallback   callback,
1141                       gpointer      callback_data)
1142 {
1143   GtkContainerClass *class;
1144
1145   g_return_if_fail (GTK_IS_CONTAINER (container));
1146   g_return_if_fail (callback != NULL);
1147
1148   class = GTK_CONTAINER_GET_CLASS (container);
1149
1150   if (class->forall)
1151     class->forall (container, TRUE, callback, callback_data);
1152 }
1153
1154 /**
1155  * gtk_container_foreach:
1156  * @container: a #GtkContainer
1157  * @callback: a callback
1158  * @callback_data: callback user data
1159  * 
1160  * Invokes @callback on each non-internal child of @container.  See
1161  * gtk_container_forall() for details on what constitutes an
1162  * "internal" child.  Most applications should use
1163  * gtk_container_foreach(), rather than gtk_container_forall().
1164  **/
1165 void
1166 gtk_container_foreach (GtkContainer *container,
1167                        GtkCallback   callback,
1168                        gpointer      callback_data)
1169 {
1170   GtkContainerClass *class;
1171   
1172   g_return_if_fail (GTK_IS_CONTAINER (container));
1173   g_return_if_fail (callback != NULL);
1174
1175   class = GTK_CONTAINER_GET_CLASS (container);
1176
1177   if (class->forall)
1178     class->forall (container, FALSE, callback, callback_data);
1179 }
1180
1181 typedef struct _GtkForeachData  GtkForeachData;
1182 struct _GtkForeachData
1183 {
1184   GtkObject         *container;
1185   GtkCallbackMarshal callback;
1186   gpointer           callback_data;
1187 };
1188
1189 static void
1190 gtk_container_foreach_unmarshal (GtkWidget *child,
1191                                  gpointer data)
1192 {
1193   GtkForeachData *fdata = (GtkForeachData*) data;
1194   GtkArg args[2];
1195   
1196   /* first argument */
1197   args[0].name = NULL;
1198   args[0].type = GTK_OBJECT_TYPE (child);
1199   GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1200   
1201   /* location for return value */
1202   args[1].name = NULL;
1203   args[1].type = GTK_TYPE_NONE;
1204   
1205   fdata->callback (fdata->container, fdata->callback_data, 1, args);
1206 }
1207
1208 void
1209 gtk_container_foreach_full (GtkContainer       *container,
1210                             GtkCallback         callback,
1211                             GtkCallbackMarshal  marshal,
1212                             gpointer            callback_data,
1213                             GtkDestroyNotify    notify)
1214 {
1215   g_return_if_fail (GTK_IS_CONTAINER (container));
1216
1217   if (marshal)
1218     {
1219       GtkForeachData fdata;
1220   
1221       fdata.container     = GTK_OBJECT (container);
1222       fdata.callback      = marshal;
1223       fdata.callback_data = callback_data;
1224
1225       gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1226     }
1227   else
1228     {
1229       g_return_if_fail (callback != NULL);
1230
1231       gtk_container_foreach (container, callback, &callback_data);
1232     }
1233
1234   if (notify)
1235     notify (callback_data);
1236 }
1237
1238 void
1239 gtk_container_set_focus_child (GtkContainer *container,
1240                                GtkWidget    *widget)
1241 {
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 /**
1250  * gtk_container_get_children:
1251  * @container: a #GtkContainer.
1252  * 
1253  * Returns the the container's non-internal children. See
1254  * gtk_container_forall() for details on what constitutes an "internal" child. 
1255  *
1256  * Return value: a newly-allocated list of the container's non-internal children.
1257  **/
1258 GList*
1259 gtk_container_get_children (GtkContainer *container)
1260 {
1261   GList *children = NULL;
1262
1263   gtk_container_foreach (container,
1264                          gtk_container_children_callback,
1265                          &children);
1266
1267   return g_list_reverse (children);
1268 }
1269
1270 static void
1271 gtk_container_child_position_callback (GtkWidget *widget,
1272                                        gpointer   client_data)
1273 {
1274   struct {
1275     GtkWidget *child;
1276     guint i;
1277     guint index;
1278   } *data = client_data;
1279
1280   data->i++;
1281   if (data->child == widget)
1282     data->index = data->i;
1283 }
1284
1285 static gchar*
1286 gtk_container_child_default_composite_name (GtkContainer *container,
1287                                             GtkWidget    *child)
1288 {
1289   struct {
1290     GtkWidget *child;
1291     guint i;
1292     guint index;
1293   } data;
1294   gchar *name;
1295
1296   /* fallback implementation */
1297   data.child = child;
1298   data.i = 0;
1299   data.index = 0;
1300   gtk_container_forall (container,
1301                         gtk_container_child_position_callback,
1302                         &data);
1303   
1304   name = g_strdup_printf ("%s-%u",
1305                           gtk_type_name (GTK_OBJECT_TYPE (child)),
1306                           data.index);
1307
1308   return name;
1309 }
1310
1311 gchar*
1312 _gtk_container_child_composite_name (GtkContainer *container,
1313                                     GtkWidget    *child)
1314 {
1315   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1316   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1317   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1318
1319   if (GTK_WIDGET_COMPOSITE_CHILD (child))
1320     {
1321       static GQuark quark_composite_name = 0;
1322       gchar *name;
1323
1324       if (!quark_composite_name)
1325         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1326
1327       name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1328       if (!name)
1329         {
1330           GtkContainerClass *class;
1331
1332           class = GTK_CONTAINER_GET_CLASS (container);
1333           if (class->composite_name)
1334             name = class->composite_name (container, child);
1335         }
1336       else
1337         name = g_strdup (name);
1338
1339       return name;
1340     }
1341   
1342   return NULL;
1343 }
1344
1345 static void
1346 gtk_container_real_set_focus_child (GtkContainer     *container,
1347                                     GtkWidget        *child)
1348 {
1349   g_return_if_fail (GTK_IS_CONTAINER (container));
1350   if (child)
1351     g_return_if_fail (GTK_IS_WIDGET (child));
1352
1353   if (child != container->focus_child)
1354     {
1355       if (container->focus_child)
1356         gtk_widget_unref (container->focus_child);
1357       container->focus_child = child;
1358       if (container->focus_child)
1359         gtk_widget_ref (container->focus_child);
1360     }
1361
1362
1363   /* check for h/v adjustments
1364    */
1365   if (container->focus_child)
1366     {
1367       GtkAdjustment *adjustment;
1368       
1369       adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1370       if (adjustment)
1371         gtk_adjustment_clamp_page (adjustment,
1372                                    container->focus_child->allocation.y,
1373                                    (container->focus_child->allocation.y +
1374                                     container->focus_child->allocation.height));
1375
1376       adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1377       if (adjustment)
1378         gtk_adjustment_clamp_page (adjustment,
1379                                    container->focus_child->allocation.x,
1380                                    (container->focus_child->allocation.x +
1381                                     container->focus_child->allocation.width));
1382     }
1383 }
1384
1385 static GList*
1386 get_focus_chain (GtkContainer *container)
1387 {
1388   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1389 }
1390
1391 static gboolean
1392 gtk_container_focus (GtkWidget        *widget,
1393                      GtkDirectionType  direction)
1394 {
1395   GList *children;
1396   GList *sorted_children;
1397   gint return_val;
1398   GtkContainer *container;
1399
1400   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1401
1402   container = GTK_CONTAINER (widget);
1403
1404   return_val = FALSE;
1405
1406   if (GTK_WIDGET_CAN_FOCUS (container))
1407     {
1408       if (!GTK_WIDGET_HAS_FOCUS (container))
1409         {
1410           gtk_widget_grab_focus (GTK_WIDGET (container));
1411           return_val = TRUE;
1412         }
1413     }
1414   else
1415     {
1416       /* Get a list of the containers children, allowing focus
1417        * chain to override.
1418        */
1419       if (container->has_focus_chain)
1420         children = g_list_copy (get_focus_chain (container));
1421       else
1422         children = gtk_container_get_children (container);
1423
1424       if (container->has_focus_chain &&
1425           (direction == GTK_DIR_TAB_FORWARD ||
1426            direction == GTK_DIR_TAB_BACKWARD))
1427         {
1428           sorted_children = g_list_copy (children);
1429           
1430           if (direction == GTK_DIR_TAB_BACKWARD)
1431             sorted_children = g_list_reverse (sorted_children);
1432         }
1433       else
1434         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1435       
1436       return_val = gtk_container_focus_move (container, sorted_children, direction);
1437
1438       g_list_free (sorted_children);
1439       g_list_free (children);
1440     }
1441
1442   return return_val;
1443 }
1444
1445 static gint
1446 tab_compare (gconstpointer a,
1447              gconstpointer b)
1448 {
1449   const GtkWidget *child1 = a;
1450   const GtkWidget *child2 = b;
1451
1452   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1453   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1454
1455   if (y1 == y2)
1456     {
1457       gint x1 = child1->allocation.x + child1->allocation.width / 2;
1458       gint x2 = child2->allocation.x + child2->allocation.width / 2;
1459       
1460       return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1461     }
1462   else
1463     return (y1 < y2) ? -1 : 1;
1464 }
1465
1466 static GList *
1467 gtk_container_focus_sort_tab (GtkContainer     *container,
1468                               GList            *children,
1469                               GtkDirectionType  direction,
1470                               GtkWidget        *old_focus)
1471 {
1472   children = g_list_sort (children, tab_compare);
1473
1474   /* if we are going backwards then reverse the order
1475    *  of the children.
1476    */
1477   if (direction == GTK_DIR_TAB_BACKWARD)
1478     children = g_list_reverse (children);
1479
1480   return children;
1481 }
1482
1483 /* Get coordinates of @widget's allocation with respect to
1484  * allocation of @container.
1485  */
1486 static gboolean
1487 get_allocation_coords (GtkContainer  *container,
1488                        GtkWidget     *widget,
1489                        GdkRectangle  *allocation)
1490 {
1491   *allocation = widget->allocation;
1492
1493   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1494                                            0, 0, &allocation->x, &allocation->y);
1495 }
1496
1497 /* Look for a child in @children that is intermediate between
1498  * the focus widget and container. This widget, if it exists,
1499  * acts as the starting widget for focus navigation.
1500  */
1501 static GtkWidget *
1502 find_old_focus (GtkContainer *container,
1503                 GList        *children)
1504 {
1505   GList *tmp_list = children;
1506   while (tmp_list)
1507     {
1508       GtkWidget *child = tmp_list->data;
1509       GtkWidget *widget = child;
1510
1511       while (widget && widget != (GtkWidget *)container)
1512         {
1513           GtkWidget *parent = widget->parent;
1514           if (parent && ((GtkContainer *)parent)->focus_child != widget)
1515             goto next;
1516
1517           widget = parent;
1518         }
1519
1520       return child;
1521
1522     next:
1523       tmp_list = tmp_list->next;
1524     }
1525
1526   return NULL;
1527 }
1528
1529 static gboolean
1530 old_focus_coords (GtkContainer *container,
1531                   GdkRectangle *old_focus_rect)
1532 {
1533   GtkWidget *widget = GTK_WIDGET (container);
1534   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1535   
1536   if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1537     {
1538       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1539       
1540       return get_allocation_coords (container, old_focus, old_focus_rect);
1541     }
1542   else
1543     return FALSE;
1544 }
1545
1546 typedef struct _CompareInfo CompareInfo;
1547
1548 struct _CompareInfo
1549 {
1550   GtkContainer *container;
1551   gint x;
1552   gint y;
1553   gboolean reverse;
1554 };
1555
1556 static gint
1557 up_down_compare (gconstpointer a,
1558                  gconstpointer b,
1559                  gpointer      data)
1560 {
1561   GdkRectangle allocation1;
1562   GdkRectangle allocation2;
1563   CompareInfo *compare = data;
1564   gint y1, y2;
1565
1566   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1567   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1568
1569   y1 = allocation1.y + allocation1.height / 2;
1570   y2 = allocation2.y + allocation2.height / 2;
1571
1572   if (y1 == y2)
1573     {
1574       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1575       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1576
1577       if (compare->reverse)
1578         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1579       else
1580         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1581     }
1582   else
1583     return (y1 < y2) ? -1 : 1;
1584 }
1585
1586 static GList *
1587 gtk_container_focus_sort_up_down (GtkContainer     *container,
1588                                   GList            *children,
1589                                   GtkDirectionType  direction,
1590                                   GtkWidget        *old_focus)
1591 {
1592   CompareInfo compare;
1593   GList *tmp_list;
1594
1595   compare.container = container;
1596   compare.reverse = (direction == GTK_DIR_UP);
1597
1598   if (!old_focus)
1599       old_focus = find_old_focus (container, children);
1600   
1601   if (old_focus)
1602     {
1603       GdkRectangle old_allocation;
1604       gint compare_x1;
1605       gint compare_x2;
1606       gint compare_y;
1607
1608       /* Delete widgets from list that don't match minimum criteria */
1609
1610       get_allocation_coords (container, old_focus, &old_allocation);
1611
1612       compare_x1 = old_allocation.x;
1613       compare_x2 = old_allocation.x + old_allocation.width;
1614
1615       if (direction == GTK_DIR_UP)
1616         compare_y = old_allocation.y;
1617       else
1618         compare_y = old_allocation.y + old_allocation.height;
1619       
1620       tmp_list = children;
1621       while (tmp_list)
1622         {
1623           GtkWidget *child = tmp_list->data;
1624           GList *next = tmp_list->next;
1625           gint child_x1, child_x2;
1626           GdkRectangle child_allocation;
1627           
1628           if (child != old_focus)
1629             {
1630               get_allocation_coords (container, child, &child_allocation);
1631               
1632               child_x1 = child_allocation.x;
1633               child_x2 = child_allocation.x + child_allocation.width;
1634               
1635               if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1636                   (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1637                   (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1638                 {
1639                   children = g_list_delete_link (children, tmp_list);
1640                 }
1641             }
1642           
1643           tmp_list = next;
1644         }
1645
1646       compare.x = (compare_x1 + compare_x2) / 2;
1647       compare.y = old_allocation.y + old_allocation.height / 2;
1648     }
1649   else
1650     {
1651       /* No old focus widget, need to figure out starting x,y some other way
1652        */
1653       GtkWidget *widget = GTK_WIDGET (container);
1654       GdkRectangle old_focus_rect;
1655
1656       if (old_focus_coords (container, &old_focus_rect))
1657         {
1658           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1659         }
1660       else
1661         {
1662           if (GTK_WIDGET_NO_WINDOW (widget))
1663             compare.x = widget->allocation.x + widget->allocation.width / 2;
1664           else
1665             compare.x = widget->allocation.width / 2;
1666         }
1667       
1668       if (GTK_WIDGET_NO_WINDOW (widget))
1669         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1670       else
1671         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1672     }
1673
1674   children = g_list_sort_with_data (children, up_down_compare, &compare);
1675
1676   if (compare.reverse)
1677     children = g_list_reverse (children);
1678
1679   return children;
1680 }
1681
1682 static gint
1683 left_right_compare (gconstpointer a,
1684                     gconstpointer b,
1685                     gpointer      data)
1686 {
1687   GdkRectangle allocation1;
1688   GdkRectangle allocation2;
1689   CompareInfo *compare = data;
1690   gint x1, x2;
1691
1692   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1693   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1694
1695   x1 = allocation1.x + allocation1.width / 2;
1696   x2 = allocation2.x + allocation2.width / 2;
1697
1698   if (x1 == x2)
1699     {
1700       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1701       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1702
1703       if (compare->reverse)
1704         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1705       else
1706         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1707     }
1708   else
1709     return (x1 < x2) ? -1 : 1;
1710 }
1711
1712 static GList *
1713 gtk_container_focus_sort_left_right (GtkContainer     *container,
1714                                      GList            *children,
1715                                      GtkDirectionType  direction,
1716                                      GtkWidget        *old_focus)
1717 {
1718   CompareInfo compare;
1719   GList *tmp_list;
1720
1721   compare.container = container;
1722   compare.reverse = (direction == GTK_DIR_LEFT);
1723
1724   if (!old_focus)
1725     old_focus = find_old_focus (container, children);
1726   
1727   if (old_focus)
1728     {
1729       GdkRectangle old_allocation;
1730
1731       gint compare_y1;
1732       gint compare_y2;
1733       gint compare_x;
1734       
1735       /* Delete widgets from list that don't match minimum criteria */
1736
1737       get_allocation_coords (container, old_focus, &old_allocation);
1738
1739       compare_y1 = old_allocation.y;
1740       compare_y2 = old_allocation.y + old_allocation.height;
1741
1742       if (direction == GTK_DIR_LEFT)
1743         compare_x = old_allocation.x;
1744       else
1745         compare_x = old_allocation.x + old_allocation.width;
1746       
1747       tmp_list = children;
1748       while (tmp_list)
1749         {
1750           GtkWidget *child = tmp_list->data;
1751           GList *next = tmp_list->next;
1752           gint child_y1, child_y2;
1753           GdkRectangle child_allocation;
1754           
1755           if (child != old_focus)
1756             {
1757               get_allocation_coords (container, child, &child_allocation);
1758               
1759               child_y1 = child_allocation.y;
1760               child_y2 = child_allocation.y + child_allocation.height;
1761               
1762               if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1763                   (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1764                   (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1765                 {
1766                   children = g_list_delete_link (children, tmp_list);
1767                 }
1768             }
1769           
1770           tmp_list = next;
1771         }
1772
1773       compare.y = (compare_y1 + compare_y2) / 2;
1774       compare.x = old_allocation.x + old_allocation.width / 2;
1775     }
1776   else
1777     {
1778       /* No old focus widget, need to figure out starting x,y some other way
1779        */
1780       GtkWidget *widget = GTK_WIDGET (container);
1781       GdkRectangle old_focus_rect;
1782
1783       if (old_focus_coords (container, &old_focus_rect))
1784         {
1785           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1786         }
1787       else
1788         {
1789           if (GTK_WIDGET_NO_WINDOW (widget))
1790             compare.y = widget->allocation.y + widget->allocation.height / 2;
1791           else
1792             compare.y = widget->allocation.height / 2;
1793         }
1794       
1795       if (GTK_WIDGET_NO_WINDOW (widget))
1796         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1797       else
1798         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1799     }
1800
1801   children = g_list_sort_with_data (children, left_right_compare, &compare);
1802
1803   if (compare.reverse)
1804     children = g_list_reverse (children);
1805
1806   return children;
1807 }
1808
1809 /**
1810  * gtk_container_focus_sort:
1811  * @container: a #GtkContainer
1812  * @children:  a list of descendents of @container (they don't
1813  *             have to be direct children.
1814  * @direction: focus direction
1815  * @old_focus: widget to use for the starting position, or %NULL
1816  *             to determine this automatically.
1817  *             [ Note, this argument isn't used for GTK_DIR_TAB_*,
1818  *               which is the only @direction we use currently,
1819  *               so perhaps this argument should be removed ]
1820  * 
1821  * Sorts @children in the correct order for focusing with
1822  * direction type @direction.
1823  * 
1824  * Return value: a copy of @children, sorted in correct focusing order,
1825  *   with children that aren't suitable for focusing in this direction
1826  *   removed.
1827  **/
1828 GList *
1829 _gtk_container_focus_sort (GtkContainer     *container,
1830                            GList            *children,
1831                            GtkDirectionType  direction,
1832                            GtkWidget        *old_focus)
1833 {
1834   children = g_list_copy (children);
1835   
1836   switch (direction)
1837     {
1838     case GTK_DIR_TAB_FORWARD:
1839     case GTK_DIR_TAB_BACKWARD:
1840       return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1841     case GTK_DIR_UP:
1842     case GTK_DIR_DOWN:
1843       return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1844     case GTK_DIR_LEFT:
1845     case GTK_DIR_RIGHT:
1846       return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
1847     }
1848
1849   g_assert_not_reached ();
1850
1851   return NULL;
1852 }
1853
1854 static gboolean
1855 gtk_container_focus_move (GtkContainer     *container,
1856                           GList            *children,
1857                           GtkDirectionType  direction)
1858 {
1859   GtkWidget *focus_child;
1860   GtkWidget *child;
1861
1862   focus_child = container->focus_child;
1863
1864   while (children)
1865     {
1866       child = children->data;
1867       children = children->next;
1868
1869       if (!child)
1870         continue;
1871       
1872       if (focus_child)
1873         {
1874           if (focus_child == child)
1875             {
1876               focus_child = NULL;
1877
1878                 if (gtk_widget_child_focus (child, direction))
1879                   return TRUE;
1880             }
1881         }
1882       else if (GTK_WIDGET_DRAWABLE (child) &&
1883                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1884         {
1885           if (gtk_widget_child_focus (child, direction))
1886             return TRUE;
1887         }
1888     }
1889
1890   return FALSE;
1891 }
1892
1893
1894 static void
1895 gtk_container_children_callback (GtkWidget *widget,
1896                                  gpointer   client_data)
1897 {
1898   GList **children;
1899
1900   children = (GList**) client_data;
1901   *children = g_list_prepend (*children, widget);
1902 }
1903
1904 static void
1905 chain_widget_destroyed (GtkWidget *widget,
1906                         gpointer   user_data)
1907 {
1908   GtkContainer *container;
1909   GList *chain;
1910   
1911   container = GTK_CONTAINER (user_data);
1912
1913   chain = g_object_get_data (G_OBJECT (container),
1914                              "gtk-container-focus-chain");
1915
1916   chain = g_list_remove (chain, widget);
1917
1918   g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1919                                         chain_widget_destroyed,
1920                                         user_data);
1921   
1922   g_object_set_data (G_OBJECT (container),
1923                      "gtk-container-focus-chain",
1924                      chain);  
1925 }
1926
1927 /**
1928  * gtk_container_set_focus_chain: 
1929  * @container: a #GtkContainer.
1930  * @focusable_widgets: the new focus chain.
1931  *
1932  * Sets a focus chain, overriding the one computed automatically by GTK+.
1933  * 
1934  * In principle each widget in the chain should be a descendant of the 
1935  * container, but this is not enforced by this method, since it's allowed 
1936  * to set the focus chain before you pack the widgets, or have a widget 
1937  * in the chain that isn't always packed. The necessary checks are done 
1938  * when the focus chain is actually traversed.
1939  **/
1940 void
1941 gtk_container_set_focus_chain (GtkContainer *container,
1942                                GList        *focusable_widgets)
1943 {
1944   GList *chain;
1945   GList *tmp_list;
1946   
1947   g_return_if_fail (GTK_IS_CONTAINER (container));
1948   
1949   if (container->has_focus_chain)
1950     gtk_container_unset_focus_chain (container);
1951
1952   container->has_focus_chain = TRUE;
1953   
1954   chain = NULL;
1955   tmp_list = focusable_widgets;
1956   while (tmp_list != NULL)
1957     {
1958       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
1959       
1960       /* In principle each widget in the chain should be a descendant
1961        * of the container, but we don't want to check that here, it's
1962        * expensive and also it's allowed to set the focus chain before
1963        * you pack the widgets, or have a widget in the chain that isn't
1964        * always packed. So we check for ancestor during actual traversal.
1965        */
1966
1967       chain = g_list_prepend (chain, tmp_list->data);
1968
1969       gtk_signal_connect (GTK_OBJECT (tmp_list->data),
1970                           "destroy",
1971                           GTK_SIGNAL_FUNC (chain_widget_destroyed),
1972                           container);
1973       
1974       tmp_list = g_list_next (tmp_list);
1975     }
1976
1977   chain = g_list_reverse (chain);
1978   
1979   g_object_set_data (G_OBJECT (container),
1980                      "gtk-container-focus-chain",
1981                      chain);
1982 }
1983
1984 /**
1985  * gtk_container_get_focus_chain:
1986  * @container:         a #GtkContainer
1987  * @focusable_widgets: location to store the focus chain of the
1988  *                     container, or %NULL. You should free this list
1989  *                     using g_list_free() when you are done with it, however
1990  *                     no additional reference count is added to the
1991  *                     individual widgets in the focus chain.
1992  * 
1993  * Retrieves the focus chain of the container, if one has been
1994  * set explicitly. If no focus chain has been explicitly
1995  * set, GTK+ computes the focus chain based on the positions
1996  * of the children. In that case, GTK+ stores %NULL in
1997  * @focusable_widgets and returns %FALSE.
1998  *
1999  * Return value: %TRUE if the focus chain of the container 
2000  * has been set explicitly.
2001  **/
2002 gboolean
2003 gtk_container_get_focus_chain (GtkContainer *container,
2004                                GList       **focus_chain)
2005 {
2006   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2007
2008   if (focus_chain)
2009     {
2010       if (container->has_focus_chain)
2011         *focus_chain = g_list_copy (get_focus_chain (container));
2012       else
2013         *focus_chain = NULL;
2014     }
2015
2016   return container->has_focus_chain;
2017 }
2018
2019 /**
2020  * gtk_container_unset_focus_chain:
2021  * @container: a #GtkContainer.
2022  * 
2023  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2024  **/
2025 void
2026 gtk_container_unset_focus_chain (GtkContainer  *container)
2027 {  
2028   g_return_if_fail (GTK_IS_CONTAINER (container));
2029
2030   if (container->has_focus_chain)
2031     {
2032       GList *chain;
2033       GList *tmp_list;
2034       
2035       chain = get_focus_chain (container);
2036       
2037       container->has_focus_chain = FALSE;
2038       
2039       g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2040                          NULL);
2041
2042       tmp_list = chain;
2043       while (tmp_list != NULL)
2044         {
2045           g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2046                                                 chain_widget_destroyed,
2047                                                 container);
2048           
2049           tmp_list = g_list_next (tmp_list);
2050         }
2051
2052       g_list_free (chain);
2053     }
2054 }
2055
2056 void
2057 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2058                                      GtkAdjustment *adjustment)
2059 {
2060   g_return_if_fail (GTK_IS_CONTAINER (container));
2061   if (adjustment)
2062     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2063
2064   if (adjustment)
2065     gtk_object_ref (GTK_OBJECT(adjustment));
2066
2067   gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2068                                   vadjustment_key_id,
2069                                   adjustment,
2070                                   (GtkDestroyNotify) gtk_object_unref);
2071 }
2072
2073 /**
2074  * gtk_container_get_focus_vadjustment:
2075  * @container: a #GtkContainer
2076  *
2077  * Retrieves the vertical focus adjustment for the container. See
2078  * gtk_container_set_focus_vadjustment ().
2079  *
2080  * Return value: the vertical focus adjustment, or %NULL if
2081  *   none has been set.
2082  **/
2083 GtkAdjustment *
2084 gtk_container_get_focus_vadjustment (GtkContainer *container)
2085 {
2086   GtkAdjustment *vadjustment;
2087     
2088   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2089
2090   vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2091                                            vadjustment_key_id);
2092
2093   return vadjustment;
2094 }
2095
2096 void
2097 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2098                                      GtkAdjustment *adjustment)
2099 {
2100   g_return_if_fail (GTK_IS_CONTAINER (container));
2101   if (adjustment)
2102     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2103
2104   if (adjustment)
2105     gtk_object_ref (GTK_OBJECT (adjustment));
2106
2107   gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2108                                   hadjustment_key_id,
2109                                   adjustment,
2110                                   (GtkDestroyNotify) gtk_object_unref);
2111 }
2112
2113 /**
2114  * gtk_container_get_focus_hadjustment:
2115  * @container: a #GtkContainer
2116  *
2117  * Retrieves the horizontal focus adjustment for the container. See
2118  * gtk_container_set_focus_hadjustment ().
2119  *
2120  * Return value: the horizontal focus adjustment, or %NULL if
2121  *   none has been set.
2122  **/
2123 GtkAdjustment *
2124 gtk_container_get_focus_hadjustment (GtkContainer *container)
2125 {
2126   GtkAdjustment *hadjustment;
2127
2128   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2129
2130   hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2131                                            hadjustment_key_id);
2132
2133   return hadjustment;
2134 }
2135
2136
2137 static void
2138 gtk_container_show_all (GtkWidget *widget)
2139 {
2140   g_return_if_fail (GTK_IS_CONTAINER (widget));
2141
2142   gtk_container_foreach (GTK_CONTAINER (widget),
2143                          (GtkCallback) gtk_widget_show_all,
2144                          NULL);
2145   gtk_widget_show (widget);
2146 }
2147
2148 static void
2149 gtk_container_hide_all (GtkWidget *widget)
2150 {
2151   g_return_if_fail (GTK_IS_CONTAINER (widget));
2152
2153   gtk_widget_hide (widget);
2154   gtk_container_foreach (GTK_CONTAINER (widget),
2155                          (GtkCallback) gtk_widget_hide_all,
2156                          NULL);
2157 }
2158
2159
2160 static void
2161 gtk_container_expose_child (GtkWidget *child,
2162                             gpointer   client_data)
2163 {
2164   struct {
2165     GtkWidget *container;
2166     GdkEventExpose *event;
2167   } *data = client_data;
2168   
2169   gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2170                                   child,
2171                                   data->event);
2172 }
2173
2174 static gint 
2175 gtk_container_expose (GtkWidget      *widget,
2176                       GdkEventExpose *event)
2177 {
2178   struct {
2179     GtkWidget *container;
2180     GdkEventExpose *event;
2181   } data;
2182
2183   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2184   g_return_val_if_fail (event != NULL, FALSE);
2185
2186   
2187   if (GTK_WIDGET_DRAWABLE (widget)) 
2188     {
2189       data.container = widget;
2190       data.event = event;
2191       
2192       gtk_container_forall (GTK_CONTAINER (widget),
2193                             gtk_container_expose_child,
2194                             &data);
2195     }   
2196   
2197   return FALSE;
2198 }
2199
2200 static void
2201 gtk_container_map_child (GtkWidget *child,
2202                          gpointer   client_data)
2203 {
2204   if (GTK_WIDGET_VISIBLE (child) &&
2205       GTK_WIDGET_CHILD_VISIBLE (child) &&
2206       !GTK_WIDGET_MAPPED (child))
2207     gtk_widget_map (child);
2208 }
2209
2210 static void
2211 gtk_container_map (GtkWidget *widget)
2212 {
2213   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2214
2215   gtk_container_forall (GTK_CONTAINER (widget),
2216                         gtk_container_map_child,
2217                         NULL);
2218
2219   if (!GTK_WIDGET_NO_WINDOW (widget))
2220     gdk_window_show (widget->window);
2221 }
2222
2223 static void
2224 gtk_container_unmap (GtkWidget *widget)
2225 {
2226   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2227
2228   if (!GTK_WIDGET_NO_WINDOW (widget))
2229     gdk_window_hide (widget->window);
2230   else
2231     gtk_container_forall (GTK_CONTAINER (widget),
2232                           (GtkCallback)gtk_widget_unmap,
2233                           NULL);
2234 }
2235
2236 /**
2237  * gtk_container_propagate_expose:
2238  * @container: a #GtkContainer
2239  * @child: a child of @container
2240  * @event: a expose event sent to container
2241  *
2242  * When a container receives an expose event, it must send synthetic
2243  * expose events to all children that don't have their own #GdkWindows.
2244  * This function provides a convenient way of doing this. A container,
2245  * when it receives an expose event, calls gtk_container_propagate_expose() 
2246  * once for each child, passing in the event the container received.
2247  *
2248  * gtk_container_propagate_expose() takes care of deciding whether
2249  * an expose event needs to be sent to the child, intersecting
2250  * the event's area with the child area, and sending the event.
2251  * 
2252  * In most cases, a container can simply either simply inherit the
2253  * ::expose implementation from #GtkContainer, or, do some drawing 
2254  * and then chain to the ::expose implementation from #GtkContainer.
2255  **/
2256 void
2257 gtk_container_propagate_expose (GtkContainer   *container,
2258                                 GtkWidget      *child,
2259                                 GdkEventExpose *event)
2260 {
2261   GdkEventExpose child_event;
2262
2263   g_return_if_fail (GTK_IS_CONTAINER (container));
2264   g_return_if_fail (GTK_IS_WIDGET (child));
2265   g_return_if_fail (event != NULL);
2266
2267   g_assert (child->parent == GTK_WIDGET (container));
2268   
2269   if (GTK_WIDGET_DRAWABLE (child) &&
2270       GTK_WIDGET_NO_WINDOW (child) &&
2271       (child->window == event->window))
2272     {
2273       child_event = *event;
2274
2275       child_event.region = gtk_widget_region_intersect (child, event->region);
2276       if (!gdk_region_empty (child_event.region))
2277         {
2278           gdk_region_get_clipbox (child_event.region, &child_event.area);
2279           gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2280         }
2281       gdk_region_destroy (child_event.region);
2282     }
2283 }