]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Add a function to determine if a window is the focus widget within its
[~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 "gtkwindow.h"
36
37 enum {
38   ADD,
39   REMOVE,
40   CHECK_RESIZE,
41   FOCUS,
42   SET_FOCUS_CHILD,
43   LAST_SIGNAL
44 };
45 enum {
46   ARG_0,
47   ARG_BORDER_WIDTH,
48   ARG_RESIZE_MODE,
49   ARG_CHILD,
50   ARG_REALLOCATE_REDRAWS
51 };
52
53 typedef struct _GtkChildArgInfo GtkChildArgInfo;
54 struct _GtkChildArgInfo
55 {
56   gchar *name;
57   GtkType type;
58   GtkType class_type;
59   guint arg_flags;
60   guint arg_id;
61   guint seq_id;
62 };
63
64 static void     gtk_container_base_class_init      (GtkContainerClass *klass);
65 static void     gtk_container_class_init           (GtkContainerClass *klass);
66 static void     gtk_container_init                 (GtkContainer      *container);
67 static void     gtk_container_destroy              (GtkObject         *object);
68 static void     gtk_container_get_arg              (GtkObject         *object,
69                                                     GtkArg            *arg,
70                                                     guint              arg_id);
71 static void     gtk_container_set_arg              (GtkObject         *object,
72                                                     GtkArg            *arg,
73                                                     guint              arg_id);
74 static void     gtk_container_add_unimplemented    (GtkContainer      *container,
75                                                     GtkWidget         *widget);
76 static void     gtk_container_remove_unimplemented (GtkContainer      *container,
77                                                     GtkWidget         *widget);
78 static void     gtk_container_real_check_resize    (GtkContainer      *container);
79 static gboolean gtk_container_real_focus           (GtkContainer      *container,
80                                                     GtkDirectionType   direction);
81 static void     gtk_container_real_set_focus_child (GtkContainer      *container,
82                                                     GtkWidget         *widget);
83 static gboolean gtk_container_focus_tab            (GtkContainer      *container,
84                                                     GList             *children,
85                                                     GtkDirectionType   direction);
86 static gboolean gtk_container_focus_up_down        (GtkContainer      *container,
87                                                     GList            **children,
88                                                     GtkDirectionType   direction);
89 static gboolean gtk_container_focus_left_right     (GtkContainer      *container,
90                                                     GList            **children,
91                                                     GtkDirectionType   direction);
92 static gboolean gtk_container_focus_move           (GtkContainer      *container,
93                                                     GList             *children,
94                                                     GtkDirectionType   direction);
95 static void     gtk_container_children_callback    (GtkWidget         *widget,
96                                                     gpointer           client_data);
97 static void     gtk_container_show_all             (GtkWidget         *widget);
98 static void     gtk_container_hide_all             (GtkWidget         *widget);
99
100
101 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
102                                                           GtkWidget    *child);
103
104
105
106 static guint container_signals[LAST_SIGNAL] = { 0 };
107 static GHashTable *container_child_arg_info_ht = NULL;
108
109 static GtkWidgetClass *parent_class = NULL;
110
111 static const gchar *vadjustment_key = "gtk-vadjustment";
112 static guint        vadjustment_key_id = 0;
113 static const gchar *hadjustment_key = "gtk-hadjustment";
114 static guint        hadjustment_key_id = 0;
115 static GSList      *container_resize_queue = NULL;
116
117 GtkType
118 gtk_container_get_type (void)
119 {
120   static GtkType container_type = 0;
121
122   if (!container_type)
123     {
124       static const GtkTypeInfo container_info =
125       {
126         "GtkContainer",
127         sizeof (GtkContainer),
128         sizeof (GtkContainerClass),
129         (GtkClassInitFunc) gtk_container_class_init,
130         (GtkObjectInitFunc) gtk_container_init,
131         /* reserved_1 */ NULL,
132         /* reserved_2 */ NULL,
133         (GtkClassInitFunc) gtk_container_base_class_init,
134       };
135
136       container_type = gtk_type_unique (gtk_widget_get_type (), &container_info);
137     }
138
139   return container_type;
140 }
141
142 static void
143 gtk_container_base_class_init (GtkContainerClass *class)
144 {
145   /* reset instance specifc class fields that don't get inherited */
146   class->n_child_args = 0;
147   class->set_child_arg = NULL;
148   class->get_child_arg = NULL;
149 }
150
151 static void
152 gtk_container_class_init (GtkContainerClass *class)
153 {
154   GtkObjectClass *object_class;
155   GtkWidgetClass *widget_class;
156
157   object_class = (GtkObjectClass*) class;
158   widget_class = (GtkWidgetClass*) class;
159
160   parent_class = gtk_type_class (gtk_widget_get_type ());
161
162   container_child_arg_info_ht = g_hash_table_new (gtk_arg_info_hash,
163                                                   gtk_arg_info_equal);
164
165   vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
166   hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
167   
168   gtk_object_add_arg_type ("GtkContainer::border_width", GTK_TYPE_ULONG, GTK_ARG_READWRITE, ARG_BORDER_WIDTH);
169   gtk_object_add_arg_type ("GtkContainer::resize_mode", GTK_TYPE_RESIZE_MODE, GTK_ARG_READWRITE, ARG_RESIZE_MODE);
170   gtk_object_add_arg_type ("GtkContainer::child", GTK_TYPE_WIDGET, GTK_ARG_WRITABLE, ARG_CHILD);
171   gtk_object_add_arg_type ("GtkContainer::reallocate_redraws", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_REALLOCATE_REDRAWS);
172
173   container_signals[ADD] =
174     gtk_signal_new ("add",
175                     GTK_RUN_FIRST,
176                     GTK_CLASS_TYPE (object_class),
177                     GTK_SIGNAL_OFFSET (GtkContainerClass, add),
178                     gtk_marshal_VOID__POINTER,
179                     GTK_TYPE_NONE, 1,
180                     GTK_TYPE_WIDGET);
181   container_signals[REMOVE] =
182     gtk_signal_new ("remove",
183                     GTK_RUN_FIRST,
184                     GTK_CLASS_TYPE (object_class),
185                     GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
186                     gtk_marshal_VOID__POINTER,
187                     GTK_TYPE_NONE, 1,
188                     GTK_TYPE_WIDGET);
189   container_signals[CHECK_RESIZE] =
190     gtk_signal_new ("check_resize",
191                     GTK_RUN_LAST,
192                     GTK_CLASS_TYPE (object_class),
193                     GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
194                     gtk_marshal_VOID__VOID,
195                     GTK_TYPE_NONE, 0);
196   container_signals[FOCUS] =
197     gtk_signal_new ("focus",
198                     GTK_RUN_LAST,
199                     GTK_CLASS_TYPE (object_class),
200                     GTK_SIGNAL_OFFSET (GtkContainerClass, focus),
201                     gtk_marshal_ENUM__ENUM,
202                     GTK_TYPE_DIRECTION_TYPE, 1,
203                     GTK_TYPE_DIRECTION_TYPE);
204   container_signals[SET_FOCUS_CHILD] =
205     gtk_signal_new ("set-focus-child",
206                     GTK_RUN_FIRST,
207                     GTK_CLASS_TYPE (object_class),
208                     GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
209                     gtk_marshal_VOID__POINTER,
210                     GTK_TYPE_NONE, 1,
211                     GTK_TYPE_WIDGET);
212   gtk_object_class_add_signals (object_class, container_signals, LAST_SIGNAL);
213
214   object_class->get_arg = gtk_container_get_arg;
215   object_class->set_arg = gtk_container_set_arg;
216   object_class->destroy = gtk_container_destroy;
217
218   widget_class->show_all = gtk_container_show_all;
219   widget_class->hide_all = gtk_container_hide_all;
220   
221   class->add = gtk_container_add_unimplemented;
222   class->remove = gtk_container_remove_unimplemented;
223   class->check_resize = gtk_container_real_check_resize;
224   class->forall = NULL;
225   class->focus = gtk_container_real_focus;
226   class->set_focus_child = gtk_container_real_set_focus_child;
227   class->child_type = NULL;
228   class->composite_name = gtk_container_child_default_composite_name;
229 }
230
231 GtkType
232 gtk_container_child_type (GtkContainer      *container)
233 {
234   GtkType slot;
235   GtkContainerClass *class;
236
237   g_return_val_if_fail (container != NULL, 0);
238   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
239
240   class = GTK_CONTAINER_GET_CLASS (container);
241   if (class->child_type)
242     slot = class->child_type (container);
243   else
244     slot = GTK_TYPE_NONE;
245
246   return slot;
247 }
248
249 /****************************************************
250  * GtkContainer child argument mechanism
251  *
252  ****************************************************/
253
254 void
255 gtk_container_add_with_args (GtkContainer      *container,
256                              GtkWidget         *widget,
257                              const gchar       *first_arg_name,
258                              ...)
259 {
260   g_return_if_fail (container != NULL);
261   g_return_if_fail (GTK_IS_CONTAINER (container));
262   g_return_if_fail (widget != NULL);
263   g_return_if_fail (GTK_IS_WIDGET (widget));
264   g_return_if_fail (widget->parent == NULL);
265
266   gtk_widget_ref (GTK_WIDGET (container));
267   gtk_widget_ref (widget);
268
269   if (!GTK_OBJECT_CONSTRUCTED (widget))
270     gtk_object_default_construct (GTK_OBJECT (widget));
271   gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
272   
273   if (widget->parent)
274     {
275       va_list var_args;
276       GSList *arg_list = NULL;
277       GSList *info_list = NULL;
278       gchar *error;
279       
280       va_start (var_args, first_arg_name);
281       error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
282                                                 &arg_list,
283                                                 &info_list,
284                                                 first_arg_name,
285                                                 var_args);
286       va_end (var_args);
287
288       if (error)
289         {
290           g_warning ("gtk_container_add_with_args(): %s", error);
291           g_free (error);
292         }
293       else
294         {
295           GSList *slist_arg;
296           GSList *slist_info;
297
298           slist_arg = arg_list;
299           slist_info = info_list;
300           while (slist_arg)
301             {
302               gtk_container_arg_set (container, widget, slist_arg->data, slist_info->data);
303               slist_arg = slist_arg->next;
304               slist_info = slist_info->next;
305             }
306           gtk_args_collect_cleanup (arg_list, info_list);
307         }
308     }
309
310   gtk_widget_unref (widget);
311   gtk_widget_unref (GTK_WIDGET (container));
312 }
313
314 void
315 gtk_container_addv (GtkContainer      *container,
316                     GtkWidget         *widget,
317                     guint              n_args,
318                     GtkArg            *args)
319 {
320   g_return_if_fail (container != NULL);
321   g_return_if_fail (GTK_IS_CONTAINER (container));
322   g_return_if_fail (widget != NULL);
323   g_return_if_fail (GTK_IS_WIDGET (widget));
324   g_return_if_fail (widget->parent == NULL);
325
326   gtk_widget_ref (GTK_WIDGET (container));
327   gtk_widget_ref (widget);
328
329   if (!GTK_OBJECT_CONSTRUCTED (widget))
330     gtk_object_default_construct (GTK_OBJECT (widget));
331   gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
332   
333   if (widget->parent)
334     {
335       GtkArg *max_args;
336
337       for (max_args = args + n_args; args < max_args; args++)
338         gtk_container_arg_set (container, widget, args, NULL);
339     }
340
341   gtk_widget_unref (widget);
342   gtk_widget_unref (GTK_WIDGET (container));
343 }
344
345 void
346 gtk_container_child_setv (GtkContainer      *container,
347                           GtkWidget         *child,
348                           guint              n_args,
349                           GtkArg            *args)
350 {
351   GtkArg *max_args;
352
353   g_return_if_fail (container != NULL);
354   g_return_if_fail (GTK_IS_CONTAINER (container));
355   g_return_if_fail (child != NULL);
356   g_return_if_fail (GTK_IS_WIDGET (child));
357   g_return_if_fail (child->parent != NULL);
358   if (n_args)
359     g_return_if_fail (args != NULL);
360
361   for (max_args = args + n_args; args < max_args; args++)
362     gtk_container_arg_set (container, child, args, NULL);
363 }
364
365 void
366 gtk_container_child_getv (GtkContainer      *container,
367                           GtkWidget         *child,
368                           guint              n_args,
369                           GtkArg            *args)
370 {
371   GtkArg *max_args;
372
373   g_return_if_fail (container != NULL);
374   g_return_if_fail (GTK_IS_CONTAINER (container));
375   g_return_if_fail (child != NULL);
376   g_return_if_fail (GTK_IS_WIDGET (child));
377   g_return_if_fail (child->parent != NULL);
378   if (n_args)
379     g_return_if_fail (args != NULL);
380
381   for (max_args = args + n_args; args < max_args; args++)
382     gtk_container_arg_get (container, child, args, NULL);
383 }
384
385 void
386 gtk_container_child_set (GtkContainer      *container,
387                          GtkWidget         *child,
388                          const gchar       *first_arg_name,
389                          ...)
390 {
391   va_list var_args;
392   GSList *arg_list = NULL;
393   GSList *info_list = NULL;
394   gchar *error;
395   
396   g_return_if_fail (container != NULL);
397   g_return_if_fail (GTK_IS_CONTAINER (container));
398   g_return_if_fail (child != NULL);
399   g_return_if_fail (GTK_IS_WIDGET (child));
400   g_return_if_fail (child->parent != NULL);
401
402   va_start (var_args, first_arg_name);
403   error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
404                                             &arg_list,
405                                             &info_list,
406                                             first_arg_name,
407                                             var_args);
408   va_end (var_args);
409
410   if (error)
411     {
412       g_warning ("gtk_container_child_set(): %s", error);
413       g_free (error);
414     }
415   else
416     {
417       GSList *slist_arg;
418       GSList *slist_info;
419
420       slist_arg = arg_list;
421       slist_info = info_list;
422       while (slist_arg)
423         {
424           gtk_container_arg_set (container, child, slist_arg->data, slist_info->data);
425           slist_arg = slist_arg->next;
426           slist_info = slist_info->next;
427         }
428       gtk_args_collect_cleanup (arg_list, info_list);
429     }
430 }
431
432 void
433 gtk_container_arg_set (GtkContainer *container,
434                        GtkWidget    *child,
435                        GtkArg       *arg,
436                        GtkArgInfo   *info)
437 {
438   GtkContainerClass *class;
439   
440   g_return_if_fail (container != NULL);
441   g_return_if_fail (GTK_IS_CONTAINER (container));
442   g_return_if_fail (child != NULL);
443   g_return_if_fail (GTK_IS_WIDGET (child));
444   g_return_if_fail (arg != NULL);
445   
446   if (!info)
447     {
448       gchar *error;
449       
450       error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
451                                 container_child_arg_info_ht,
452                                 arg->name,
453                                 &info);
454       if (error)
455         {
456           g_warning ("gtk_container_arg_set(): %s", error);
457           g_free (error);
458           return;
459         }
460     }
461   g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
462   
463   if (! (info->arg_flags & GTK_ARG_WRITABLE))
464     {
465       g_warning ("gtk_container_arg_set(): argument \"%s\" is not writable",
466                  info->full_name);
467       return;
468     }
469   if (info->type != arg->type)
470     {
471       g_warning ("gtk_container_arg_set(): argument \"%s\" has invalid type `%s'",
472                  info->full_name,
473                  gtk_type_name (arg->type));
474       return;
475     }
476   
477   class = gtk_type_class (info->class_type);
478   g_assert (class->set_child_arg != NULL);
479   class->set_child_arg (container, child, arg, info->arg_id);
480 }
481
482 void
483 gtk_container_arg_get (GtkContainer *container,
484                        GtkWidget    *child,
485                        GtkArg       *arg,
486                        GtkArgInfo   *info)
487 {
488   GtkContainerClass *class;
489   
490   g_return_if_fail (container != NULL);
491   g_return_if_fail (GTK_IS_CONTAINER (container));
492   g_return_if_fail (child != NULL);
493   g_return_if_fail (GTK_IS_WIDGET (child));
494   g_return_if_fail (arg != NULL);
495   
496   if (!info)
497     {
498       gchar *error;
499       
500       error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
501                                 container_child_arg_info_ht,
502                                 arg->name,
503                                 &info);
504       if (error)
505         {
506           g_warning ("gtk_container_arg_get(): %s", error);
507           g_free (error);
508           arg->type = GTK_TYPE_INVALID;
509           return;
510         }
511     }
512   g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
513   
514   if (! (info->arg_flags & GTK_ARG_READABLE))
515     {
516       g_warning ("gtk_container_arg_get(): argument \"%s\" is not readable",
517                  info->full_name);
518       arg->type = GTK_TYPE_INVALID;
519       return;
520     }
521   
522   class = gtk_type_class (info->class_type);
523   g_assert (class->get_child_arg != NULL);
524   arg->type = info->type;
525   class->get_child_arg (container, child, arg, info->arg_id);
526 }
527
528 void
529 gtk_container_add_child_arg_type (const gchar       *arg_name,
530                                   GtkType            arg_type,
531                                   guint              arg_flags,
532                                   guint              arg_id)
533 {
534   g_return_if_fail (arg_name != NULL);
535   g_return_if_fail (arg_type > GTK_TYPE_NONE);
536   g_return_if_fail (arg_id > 0);
537   g_return_if_fail ((arg_flags & GTK_ARG_READWRITE) == GTK_ARG_READWRITE);
538   /* g_return_if_fail ((arg_flags & GTK_ARG_CHILD_ARG) != 0); */
539
540   arg_flags |= GTK_ARG_CHILD_ARG;
541   arg_flags &= GTK_ARG_MASK;
542
543   gtk_arg_type_new_static (GTK_TYPE_CONTAINER,
544                            arg_name,
545                            GTK_STRUCT_OFFSET (GtkContainerClass, n_child_args),
546                            container_child_arg_info_ht,
547                            arg_type,
548                            arg_flags,
549                            arg_id);
550 }
551
552 gchar*
553 gtk_container_child_args_collect (GtkType       object_type,
554                                   GSList      **arg_list_p,
555                                   GSList      **info_list_p,
556                                   const gchar  *first_arg_name,
557                                   va_list       var_args)
558 {
559   return gtk_args_collect (object_type,
560                            container_child_arg_info_ht,
561                            arg_list_p,
562                            info_list_p,
563                            first_arg_name,
564                            var_args);
565 }
566
567 gchar*
568 gtk_container_child_arg_get_info (GtkType       object_type,
569                                   const gchar  *arg_name,
570                                   GtkArgInfo  **info_p)
571 {
572   return gtk_arg_get_info (object_type,
573                            container_child_arg_info_ht,
574                            arg_name,
575                            info_p);
576 }
577
578 GtkArg*
579 gtk_container_query_child_args (GtkType            class_type,
580                                 guint32          **arg_flags,
581                                 guint             *n_args)
582 {
583   g_return_val_if_fail (n_args != NULL, NULL);
584   *n_args = 0;
585   g_return_val_if_fail (gtk_type_is_a (class_type, GTK_TYPE_CONTAINER), NULL);
586
587   return gtk_args_query (class_type, container_child_arg_info_ht, arg_flags, n_args);
588 }
589
590
591 static void
592 gtk_container_add_unimplemented (GtkContainer     *container,
593                                  GtkWidget        *widget)
594 {
595   g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
596 }
597
598 static void
599 gtk_container_remove_unimplemented (GtkContainer     *container,
600                                     GtkWidget        *widget)
601 {
602   g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
603 }
604
605 static void
606 gtk_container_init (GtkContainer *container)
607 {
608   container->focus_child = NULL;
609   container->border_width = 0;
610   container->need_resize = FALSE;
611   container->resize_mode = GTK_RESIZE_PARENT;
612   container->reallocate_redraws = FALSE;
613   container->resize_widgets = NULL;
614 }
615
616 static void
617 gtk_container_destroy (GtkObject *object)
618 {
619   GtkContainer *container;
620
621   g_return_if_fail (object != NULL);
622   g_return_if_fail (GTK_IS_CONTAINER (object));
623
624   container = GTK_CONTAINER (object);
625   
626   if (GTK_CONTAINER_RESIZE_PENDING (container))
627     gtk_container_dequeue_resize_handler (container);
628   if (container->resize_widgets)
629     gtk_container_clear_resize_widgets (container);
630   
631   gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
632   
633   if (GTK_OBJECT_CLASS (parent_class)->destroy)
634     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
635 }
636
637 static void
638 gtk_container_set_arg (GtkObject    *object,
639                        GtkArg       *arg,
640                        guint         arg_id)
641 {
642   GtkContainer *container;
643
644   container = GTK_CONTAINER (object);
645
646   switch (arg_id)
647     {
648     case ARG_BORDER_WIDTH:
649       gtk_container_set_border_width (container, GTK_VALUE_ULONG (*arg));
650       break;
651     case ARG_RESIZE_MODE:
652       gtk_container_set_resize_mode (container, GTK_VALUE_ENUM (*arg));
653       break;
654     case ARG_REALLOCATE_REDRAWS:
655       gtk_container_set_reallocate_redraws (container, GTK_VALUE_BOOL (*arg));
656       break;
657     case ARG_CHILD:
658       gtk_container_add (container, GTK_WIDGET (GTK_VALUE_OBJECT (*arg)));
659       break;
660     default:
661       break;
662     }
663 }
664
665 static void
666 gtk_container_get_arg (GtkObject    *object,
667                        GtkArg       *arg,
668                        guint         arg_id)
669 {
670   GtkContainer *container;
671
672   container = GTK_CONTAINER (object);
673   
674   switch (arg_id)
675     {
676     case ARG_BORDER_WIDTH:
677       GTK_VALUE_ULONG (*arg) = container->border_width;
678       break;
679     case ARG_RESIZE_MODE:
680       GTK_VALUE_ENUM (*arg) = container->resize_mode;
681       break;
682     case ARG_REALLOCATE_REDRAWS:
683       GTK_VALUE_BOOL (*arg) = container->reallocate_redraws;
684       break;
685     default:
686       arg->type = GTK_TYPE_INVALID;
687       break;
688     }
689 }
690
691 void
692 gtk_container_set_border_width (GtkContainer *container,
693                                 guint         border_width)
694 {
695   g_return_if_fail (container != NULL);
696   g_return_if_fail (GTK_IS_CONTAINER (container));
697
698   if (container->border_width != border_width)
699     {
700       container->border_width = border_width;
701
702       if (GTK_WIDGET_REALIZED (container))
703         gtk_widget_queue_resize (GTK_WIDGET (container));
704     }
705 }
706
707 void
708 gtk_container_add (GtkContainer *container,
709                    GtkWidget    *widget)
710 {
711   g_return_if_fail (container != NULL);
712   g_return_if_fail (GTK_IS_CONTAINER (container));
713   g_return_if_fail (widget != NULL);
714   g_return_if_fail (GTK_IS_WIDGET (widget));
715   g_return_if_fail (widget->parent == NULL);
716
717   if (!GTK_OBJECT_CONSTRUCTED (widget))
718     gtk_object_default_construct (GTK_OBJECT (widget));
719   gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
720 }
721
722 void
723 gtk_container_remove (GtkContainer *container,
724                       GtkWidget    *widget)
725 {
726   g_return_if_fail (container != NULL);
727   g_return_if_fail (GTK_IS_CONTAINER (container));
728   g_return_if_fail (widget != NULL);
729   g_return_if_fail (GTK_IS_WIDGET (widget));
730   g_return_if_fail (widget->parent == GTK_WIDGET (container));
731   
732   gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
733 }
734
735 void
736 gtk_container_dequeue_resize_handler (GtkContainer *container)
737 {
738   g_return_if_fail (GTK_IS_CONTAINER (container));
739   g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
740
741   container_resize_queue = g_slist_remove (container_resize_queue, container);
742   GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
743 }
744
745 void
746 gtk_container_clear_resize_widgets (GtkContainer *container)
747 {
748   GSList *node;
749
750   g_return_if_fail (container != NULL);
751   g_return_if_fail (GTK_IS_CONTAINER (container));
752
753   node = container->resize_widgets;
754
755   while (node)
756     {
757       GtkWidget *widget = node->data;
758
759       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
760       node = node->next;
761     }
762   
763   g_slist_free (container->resize_widgets);
764   container->resize_widgets = NULL;
765 }
766
767 void
768 gtk_container_set_resize_mode (GtkContainer  *container,
769                                GtkResizeMode  resize_mode)
770 {
771   g_return_if_fail (container != NULL);
772   g_return_if_fail (GTK_IS_CONTAINER (container));
773   g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
774   
775   if (GTK_WIDGET_TOPLEVEL (container) &&
776       resize_mode == GTK_RESIZE_PARENT)
777     resize_mode = GTK_RESIZE_QUEUE;
778   
779   if (container->resize_mode != resize_mode)
780     {
781       container->resize_mode = resize_mode;
782       
783       if (resize_mode == GTK_RESIZE_IMMEDIATE)
784         gtk_container_check_resize (container);
785       else
786         {
787           gtk_container_clear_resize_widgets (container);
788           gtk_widget_queue_resize (GTK_WIDGET (container));
789         }
790     }
791 }
792
793 void
794 gtk_container_set_reallocate_redraws (GtkContainer *container,
795                                       gboolean      needs_redraws)
796 {
797   g_return_if_fail (GTK_IS_CONTAINER (container));
798
799   needs_redraws = needs_redraws ? TRUE : FALSE;
800   if (needs_redraws != container->reallocate_redraws)
801     {
802       container->reallocate_redraws = needs_redraws;
803       if (container->reallocate_redraws)
804         gtk_widget_queue_draw (GTK_WIDGET (container));
805     }
806 }
807
808 static GtkContainer*
809 gtk_container_get_resize_container (GtkContainer *container)
810 {
811   GtkWidget *widget;
812
813   widget = GTK_WIDGET (container);
814
815   while (widget->parent)
816     {
817       widget = widget->parent;
818       if (GTK_IS_RESIZE_CONTAINER (widget) && !GTK_WIDGET_RESIZE_NEEDED (widget))
819         break;
820     }
821
822   return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
823 }
824
825 static gboolean
826 gtk_container_idle_sizer (gpointer data)
827 {
828   GDK_THREADS_ENTER ();
829
830   /* we may be invoked with a container_resize_queue of NULL, because
831    * queue_resize could have been adding an extra idle function while
832    * the queue still got processed. we better just ignore such case
833    * than trying to explicitely work around them with some extra flags,
834    * since it doesn't cause any actual harm.
835    */
836   while (container_resize_queue)
837     {
838       GSList *slist;
839       GtkWidget *widget;
840
841       slist = container_resize_queue;
842       container_resize_queue = slist->next;
843       widget = slist->data;
844       g_slist_free_1 (slist);
845
846       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
847       gtk_container_check_resize (GTK_CONTAINER (widget));
848     }
849
850   gdk_window_process_all_updates ();
851
852   GDK_THREADS_LEAVE ();
853
854   return FALSE;
855 }
856
857 void
858 gtk_container_queue_resize (GtkContainer *container)
859 {
860   GtkContainer *resize_container;
861   
862   g_return_if_fail (container != NULL);
863   g_return_if_fail (GTK_IS_CONTAINER (container));
864
865   /* clear resize widgets for resize containers
866    * before aborting prematurely. this is especially
867    * important for toplevels which may need imemdiate
868    * processing or their resize handler to be queued.
869    */
870   if (GTK_IS_RESIZE_CONTAINER (container))
871     gtk_container_clear_resize_widgets (container);
872   if (GTK_OBJECT_DESTROYED (container) ||
873       GTK_WIDGET_RESIZE_NEEDED (container))
874     return;
875   
876   resize_container = gtk_container_get_resize_container (container);
877   
878   if (resize_container)
879     {
880       if (GTK_WIDGET_VISIBLE (resize_container) &&
881           (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_DRAWABLE (resize_container)))
882         {
883           switch (resize_container->resize_mode)
884             {
885             case GTK_RESIZE_QUEUE:
886               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
887                 {
888                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
889                   if (container_resize_queue == NULL)
890                     gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
891                                            gtk_container_idle_sizer,
892                                            NULL);
893                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
894                 }
895               
896               GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
897               resize_container->resize_widgets =
898                 g_slist_prepend (resize_container->resize_widgets, container);
899               break;
900
901             case GTK_RESIZE_IMMEDIATE:
902               GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
903               resize_container->resize_widgets =
904                 g_slist_prepend (resize_container->resize_widgets, container);
905               gtk_container_check_resize (resize_container);
906               break;
907
908             case GTK_RESIZE_PARENT:
909               /* Ignore, should not be reached */
910               break;
911             }
912         }
913       else
914         {
915           /* we need to let hidden resize containers know that something
916            * changed while they where hidden (currently only evaluated by
917            * toplevels).
918            */
919           resize_container->need_resize = TRUE;
920         }
921     }
922 }
923
924 void
925 gtk_container_check_resize (GtkContainer *container)
926 {
927   g_return_if_fail (container != NULL);
928   g_return_if_fail (GTK_IS_CONTAINER (container));
929   
930   gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
931 }
932
933 static void
934 gtk_container_real_check_resize (GtkContainer *container)
935 {
936   GtkWidget *widget;
937   GtkRequisition requisition;
938   
939   g_return_if_fail (container != NULL);
940   g_return_if_fail (GTK_IS_CONTAINER (container));
941   
942   widget = GTK_WIDGET (container);
943   
944   gtk_widget_size_request (widget, &requisition);
945   
946   if (requisition.width > widget->allocation.width ||
947       requisition.height > widget->allocation.height)
948     {
949       if (GTK_IS_RESIZE_CONTAINER (container))
950         gtk_widget_size_allocate (GTK_WIDGET (container),
951                                   &GTK_WIDGET (container)->allocation);
952       else
953         gtk_widget_queue_resize (widget);
954     }
955   else
956     {
957       gtk_container_resize_children (container);
958     }
959 }
960
961 /* The container hasn't changed size but one of its children
962  *  queued a resize request. Which means that the allocation
963  *  is not sufficient for the requisition of some child.
964  *  We've already performed a size request at this point,
965  *  so we simply need to run through the list of resize
966  *  widgets and reallocate their sizes appropriately. We
967  *  make the optimization of not performing reallocation
968  *  for a widget who also has a parent in the resize widgets
969  *  list. GTK_RESIZE_NEEDED is used for flagging those
970  *  parents inside this function.
971  */
972 void
973 gtk_container_resize_children (GtkContainer *container)
974 {
975   GtkWidget *widget;
976   GtkWidget *resize_container;
977   GSList *resize_widgets;
978   GSList *resize_containers;
979   GSList *node;
980   
981   /* resizing invariants:
982    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
983    * containers with resize_mode==GTK_RESIZE_PARENT have to have resize_widgets
984    * set to NULL.
985    * containers that are flagged RESIZE_NEEDED must have resize_widgets set to
986    * NULL, or are toplevels (thus have ->parent set to NULL).
987    * widgets that are in some container->resize_widgets list must be flagged with
988    * RESIZE_NEEDED.
989    * widgets that have RESIZE_NEEDED set must be referenced in some
990    * GTK_IS_RESIZE_CONTAINER (container)->resize_widgets list.
991    * containers that have an idle sizer pending must be flagged with
992    * RESIZE_PENDING.
993    */
994   
995   g_return_if_fail (container != NULL);
996   g_return_if_fail (GTK_IS_CONTAINER (container));
997
998   /* we first check out if we actually need to perform a resize,
999    * which is not the case if we got another container queued for
1000    * a resize in our anchestry. also we can skip the whole
1001    * resize_widgets checks if we are a toplevel and NEED_RESIZE.
1002    * this code assumes that our allocation is sufficient for our
1003    * requisition, since otherwise we would NEED_RESIZE.
1004    */
1005   resize_container = GTK_WIDGET (container);
1006   while (resize_container)
1007     {
1008       if (GTK_WIDGET_RESIZE_NEEDED (resize_container))
1009         break;
1010       resize_container = resize_container->parent;
1011     }
1012   if (resize_container)
1013     {
1014       /* queue_resize and size_allocate both clear our
1015        * resize_widgets list.
1016        */
1017       if (resize_container->parent)
1018         gtk_container_queue_resize (container);
1019       else
1020         gtk_widget_size_allocate (GTK_WIDGET (container),
1021                                   &GTK_WIDGET (container)->allocation);
1022       return;
1023     }
1024
1025   resize_container = GTK_WIDGET (container);
1026
1027   /* we now walk the anchestry for all resize widgets as long
1028    * as they are our children and as long as their allocation
1029    * is insufficient, since we don't need to reallocate below that.
1030    */
1031   resize_widgets = container->resize_widgets;
1032   container->resize_widgets = NULL;
1033   for (node = resize_widgets; node; node = node->next)
1034     {
1035       widget = node->data;
1036
1037       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1038
1039       while (widget->parent != resize_container &&
1040              ((widget->allocation.width < widget->requisition.width) ||
1041               (widget->allocation.height < widget->requisition.height)))
1042         widget = widget->parent;
1043       
1044       GTK_PRIVATE_SET_FLAG (widget, GTK_RESIZE_NEEDED);
1045       node->data = widget;
1046     }
1047
1048   /* for the newly setup resize_widgets list, we now walk each widget's
1049    * anchestry to sort those widgets out that have RESIZE_NEEDED parents.
1050    * we can safely stop the walk if we are the parent, since we checked
1051    * our own anchestry already.
1052    */
1053   resize_containers = NULL;
1054   for (node = resize_widgets; node; node = node->next)
1055     {
1056       GtkWidget *parent;
1057
1058       widget = node->data;
1059       
1060       if (!GTK_WIDGET_RESIZE_NEEDED (widget))
1061         continue;
1062       
1063       parent = widget->parent;
1064       
1065       while (parent != resize_container)
1066         {
1067           if (GTK_WIDGET_RESIZE_NEEDED (parent))
1068             {
1069               GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1070               widget = parent;
1071             }
1072           parent = parent->parent;
1073         }
1074       
1075       if (!g_slist_find (resize_containers, widget))
1076         {
1077           resize_containers = g_slist_prepend (resize_containers, widget);
1078           gtk_widget_ref (widget);
1079         }
1080     }
1081   g_slist_free (resize_widgets);
1082   
1083   for (node = resize_containers; node; node = node->next)
1084     {
1085       widget = node->data;
1086       
1087       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1088
1089       gtk_widget_size_allocate (widget, &widget->allocation);
1090
1091       gtk_widget_unref (widget);
1092     }
1093   g_slist_free (resize_containers);
1094 }
1095
1096 void
1097 gtk_container_forall (GtkContainer *container,
1098                       GtkCallback   callback,
1099                       gpointer      callback_data)
1100 {
1101   GtkContainerClass *class;
1102
1103   g_return_if_fail (container != NULL);
1104   g_return_if_fail (GTK_IS_CONTAINER (container));
1105   g_return_if_fail (callback != NULL);
1106
1107   class = GTK_CONTAINER_GET_CLASS (container);
1108
1109   if (class->forall)
1110     class->forall (container, TRUE, callback, callback_data);
1111 }
1112
1113 void
1114 gtk_container_foreach (GtkContainer *container,
1115                        GtkCallback   callback,
1116                        gpointer      callback_data)
1117 {
1118   GtkContainerClass *class;
1119   
1120   g_return_if_fail (container != NULL);
1121   g_return_if_fail (GTK_IS_CONTAINER (container));
1122   g_return_if_fail (callback != NULL);
1123
1124   class = GTK_CONTAINER_GET_CLASS (container);
1125
1126   if (class->forall)
1127     class->forall (container, FALSE, callback, callback_data);
1128 }
1129
1130 typedef struct _GtkForeachData  GtkForeachData;
1131 struct _GtkForeachData
1132 {
1133   GtkObject         *container;
1134   GtkCallbackMarshal callback;
1135   gpointer           callback_data;
1136 };
1137
1138 static void
1139 gtk_container_foreach_unmarshal (GtkWidget *child,
1140                                  gpointer data)
1141 {
1142   GtkForeachData *fdata = (GtkForeachData*) data;
1143   GtkArg args[2];
1144   
1145   /* first argument */
1146   args[0].name = NULL;
1147   args[0].type = GTK_OBJECT_TYPE (child);
1148   GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1149   
1150   /* location for return value */
1151   args[1].name = NULL;
1152   args[1].type = GTK_TYPE_NONE;
1153   
1154   fdata->callback (fdata->container, fdata->callback_data, 1, args);
1155 }
1156
1157 void
1158 gtk_container_foreach_full (GtkContainer       *container,
1159                             GtkCallback         callback,
1160                             GtkCallbackMarshal  marshal,
1161                             gpointer            callback_data,
1162                             GtkDestroyNotify    notify)
1163 {
1164   g_return_if_fail (container != NULL);
1165   g_return_if_fail (GTK_IS_CONTAINER (container));
1166
1167   if (marshal)
1168     {
1169       GtkForeachData fdata;
1170   
1171       fdata.container     = GTK_OBJECT (container);
1172       fdata.callback      = marshal;
1173       fdata.callback_data = callback_data;
1174
1175       gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1176     }
1177   else
1178     {
1179       g_return_if_fail (callback != NULL);
1180
1181       gtk_container_foreach (container, callback, &callback_data);
1182     }
1183
1184   if (notify)
1185     notify (callback_data);
1186 }
1187
1188 gboolean
1189 gtk_container_focus (GtkContainer     *container,
1190                      GtkDirectionType  direction)
1191 {
1192   gint return_val;
1193
1194   g_return_val_if_fail (container != NULL, FALSE);
1195   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1196   
1197   gtk_signal_emit (GTK_OBJECT (container),
1198                    container_signals[FOCUS],
1199                    direction, &return_val);
1200
1201   return return_val;
1202 }
1203
1204 void
1205 gtk_container_set_focus_child (GtkContainer *container,
1206                                GtkWidget    *widget)
1207 {
1208   g_return_if_fail (container != NULL);
1209   g_return_if_fail (GTK_IS_CONTAINER (container));
1210   if (widget)
1211     g_return_if_fail (GTK_IS_WIDGET (widget));
1212
1213   gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1214 }
1215
1216 GList*
1217 gtk_container_children (GtkContainer *container)
1218 {
1219   GList *children;
1220
1221   children = NULL;
1222
1223   gtk_container_foreach (container,
1224                          gtk_container_children_callback,
1225                          &children);
1226
1227   return g_list_reverse (children);
1228 }
1229
1230 static void
1231 gtk_container_child_position_callback (GtkWidget *widget,
1232                                        gpointer   client_data)
1233 {
1234   struct {
1235     GtkWidget *child;
1236     guint i;
1237     guint index;
1238   } *data = client_data;
1239
1240   data->i++;
1241   if (data->child == widget)
1242     data->index = data->i;
1243 }
1244
1245 static gchar*
1246 gtk_container_child_default_composite_name (GtkContainer *container,
1247                                             GtkWidget    *child)
1248 {
1249   struct {
1250     GtkWidget *child;
1251     guint i;
1252     guint index;
1253   } data;
1254   gchar *name;
1255
1256   /* fallback implementation */
1257   data.child = child;
1258   data.i = 0;
1259   data.index = 0;
1260   gtk_container_forall (container,
1261                         gtk_container_child_position_callback,
1262                         &data);
1263   
1264   name = g_strdup_printf ("%s-%u",
1265                           gtk_type_name (GTK_OBJECT_TYPE (child)),
1266                           data.index);
1267
1268   return name;
1269 }
1270
1271 gchar*
1272 gtk_container_child_composite_name (GtkContainer *container,
1273                                     GtkWidget    *child)
1274 {
1275   g_return_val_if_fail (container != NULL, NULL);
1276   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1277   g_return_val_if_fail (child != NULL, NULL);
1278   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1279   g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1280
1281   if (GTK_WIDGET_COMPOSITE_CHILD (child))
1282     {
1283       static GQuark quark_composite_name = 0;
1284       gchar *name;
1285
1286       if (!quark_composite_name)
1287         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1288
1289       name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1290       if (!name)
1291         {
1292           GtkContainerClass *class;
1293
1294           class = GTK_CONTAINER_GET_CLASS (container);
1295           if (class->composite_name)
1296             name = class->composite_name (container, child);
1297         }
1298       else
1299         name = g_strdup (name);
1300
1301       return name;
1302     }
1303   
1304   return NULL;
1305 }
1306
1307 void
1308 gtk_container_real_set_focus_child (GtkContainer     *container,
1309                                     GtkWidget        *child)
1310 {
1311   g_return_if_fail (container != NULL);
1312   g_return_if_fail (GTK_IS_CONTAINER (container));
1313   if (child)
1314     g_return_if_fail (GTK_IS_WIDGET (child));
1315
1316   if (child != container->focus_child)
1317     {
1318       if (container->focus_child)
1319         gtk_widget_unref (container->focus_child);
1320       container->focus_child = child;
1321       if (container->focus_child)
1322         gtk_widget_ref (container->focus_child);
1323     }
1324
1325
1326   /* check for h/v adjustments
1327    */
1328   if (container->focus_child)
1329     {
1330       GtkAdjustment *adjustment;
1331       
1332       adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1333       if (adjustment)
1334         gtk_adjustment_clamp_page (adjustment,
1335                                    container->focus_child->allocation.y,
1336                                    (container->focus_child->allocation.y +
1337                                     container->focus_child->allocation.height));
1338
1339       adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1340       if (adjustment)
1341         gtk_adjustment_clamp_page (adjustment,
1342                                    container->focus_child->allocation.x,
1343                                    (container->focus_child->allocation.x +
1344                                     container->focus_child->allocation.width));
1345     }
1346 }
1347
1348 static gboolean
1349 gtk_container_real_focus (GtkContainer     *container,
1350                           GtkDirectionType  direction)
1351 {
1352   GList *children;
1353   GList *tmp_list;
1354   GList *tmp_list2;
1355   gint return_val;
1356
1357   g_return_val_if_fail (container != NULL, FALSE);
1358   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1359
1360   /* Fail if the container is inappropriate for focus movement
1361    */
1362   if (!GTK_WIDGET_DRAWABLE (container) ||
1363       !GTK_WIDGET_IS_SENSITIVE (container))
1364     return FALSE;
1365
1366   return_val = FALSE;
1367
1368   if (GTK_WIDGET_CAN_FOCUS (container))
1369     {
1370       if (!GTK_WIDGET_HAS_FOCUS (container))
1371         {
1372           gtk_widget_grab_focus (GTK_WIDGET (container));
1373           return_val = TRUE;
1374         }
1375     }
1376   else
1377     {
1378       /* Get a list of the containers children
1379        */
1380       children = NULL;
1381       gtk_container_forall (container,
1382                             gtk_container_children_callback,
1383                             &children);
1384       children = g_list_reverse (children);
1385       /* children = gtk_container_children (container); */
1386
1387       if (children)
1388         {
1389           /* Remove any children which are inappropriate for focus movement
1390            */
1391           tmp_list = children;
1392           while (tmp_list)
1393             {
1394               if (GTK_WIDGET_IS_SENSITIVE (tmp_list->data) &&
1395                   GTK_WIDGET_DRAWABLE (tmp_list->data) &&
1396                   (GTK_IS_CONTAINER (tmp_list->data) || GTK_WIDGET_CAN_FOCUS (tmp_list->data)))
1397                 tmp_list = tmp_list->next;
1398               else
1399                 {
1400                   tmp_list2 = tmp_list;
1401                   tmp_list = tmp_list->next;
1402                   
1403                   children = g_list_remove_link (children, tmp_list2);
1404                   g_list_free_1 (tmp_list2);
1405                 }
1406             }
1407
1408           switch (direction)
1409             {
1410             case GTK_DIR_TAB_FORWARD:
1411             case GTK_DIR_TAB_BACKWARD:
1412               return_val = gtk_container_focus_tab (container, children, direction);
1413               break;
1414             case GTK_DIR_UP:
1415             case GTK_DIR_DOWN:
1416               return_val = gtk_container_focus_up_down (container, &children, direction);
1417               break;
1418             case GTK_DIR_LEFT:
1419             case GTK_DIR_RIGHT:
1420               return_val = gtk_container_focus_left_right (container, &children, direction);
1421               break;
1422             }
1423
1424           g_list_free (children);
1425         }
1426     }
1427
1428   return return_val;
1429 }
1430
1431 static gboolean
1432 gtk_container_focus_tab (GtkContainer     *container,
1433                          GList            *children,
1434                          GtkDirectionType  direction)
1435 {
1436   GtkWidget *child;
1437   GtkWidget *child2;
1438   GList *tmp_list;
1439   guint length;
1440   guint i, j;
1441
1442   length = g_list_length (children);
1443
1444   /* sort the children in the y direction */
1445   for (i = 1; i < length; i++)
1446     {
1447       j = i;
1448       tmp_list = g_list_nth (children, j);
1449       child = tmp_list->data;
1450
1451       while (j > 0)
1452         {
1453           child2 = tmp_list->prev->data;
1454           if (child->allocation.y < child2->allocation.y)
1455             {
1456               tmp_list->data = tmp_list->prev->data;
1457               tmp_list = tmp_list->prev;
1458               j--;
1459             }
1460           else
1461             break;
1462         }
1463
1464       tmp_list->data = child;
1465     }
1466
1467   /* sort the children in the x direction while
1468    *  maintaining the y direction sort.
1469    */
1470   for (i = 1; i < length; i++)
1471     {
1472       j = i;
1473       tmp_list = g_list_nth (children, j);
1474       child = tmp_list->data;
1475
1476       while (j > 0)
1477         {
1478           child2 = tmp_list->prev->data;
1479           if ((child->allocation.x < child2->allocation.x) &&
1480               (child->allocation.y == child2->allocation.y))
1481             {
1482               tmp_list->data = tmp_list->prev->data;
1483               tmp_list = tmp_list->prev;
1484               j--;
1485             }
1486           else
1487             break;
1488         }
1489
1490       tmp_list->data = child;
1491     }
1492
1493   /* if we are going backwards then reverse the order
1494    *  of the children.
1495    */
1496   if (direction == GTK_DIR_TAB_BACKWARD)
1497     children = g_list_reverse (children);
1498
1499   return gtk_container_focus_move (container, children, direction);
1500 }
1501
1502 static gboolean
1503 old_focus_coords (GtkContainer *container, GdkRectangle *old_focus_rect)
1504 {
1505   GtkWidget *widget = GTK_WIDGET (container);
1506   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1507   
1508   if (toplevel &&
1509       GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget &&
1510       GTK_WIDGET_REALIZED (container) &&
1511       GTK_WIDGET_REALIZED (GTK_WINDOW (toplevel)->focus_widget))
1512     {
1513       GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1514       GdkWindow *old_parent_window = old_focus->parent ? old_focus->parent->window : old_focus->window;
1515       GdkWindow *new_parent_window = widget->window;
1516       GdkWindow *toplevel_window = toplevel->window;
1517       
1518       *old_focus_rect = old_focus->allocation;
1519       
1520       /* Translate coordinates to the toplevel */
1521       
1522       while (old_parent_window != toplevel_window)
1523         {
1524           gint dx, dy;
1525           
1526           gdk_window_get_position (old_parent_window, &dx, &dy);
1527           
1528           old_focus_rect->x += dx;
1529           old_focus_rect->y += dy;
1530           
1531           old_parent_window = gdk_window_get_parent (old_parent_window);
1532         }
1533       
1534       /* Translate coordinates back to the new container */
1535       
1536       while (new_parent_window != toplevel_window)
1537         {
1538           gint dx, dy;
1539           
1540           gdk_window_get_position (new_parent_window, &dx, &dy);
1541           
1542           old_focus_rect->x -= dx;
1543           old_focus_rect->y -= dy;
1544           
1545           new_parent_window = gdk_window_get_parent (new_parent_window);
1546         }
1547
1548       return TRUE;
1549     }
1550
1551   return FALSE;
1552 }
1553
1554 typedef struct _CompareInfo CompareInfo;
1555
1556 struct _CompareInfo
1557 {
1558   gint x;
1559   gint y;
1560 };
1561
1562 static gint
1563 up_down_compare (gconstpointer a,
1564                  gconstpointer b,
1565                  gpointer      data)
1566 {
1567   const GtkWidget *child1 = a;
1568   const GtkWidget *child2 = b;
1569   CompareInfo *compare = data;
1570
1571   gint y1 = child1->allocation.y + child1->allocation.height / 2;
1572   gint y2 = child2->allocation.y + child2->allocation.height / 2;
1573
1574   if (y1 == y2)
1575     {
1576       gint x1 = abs (child1->allocation.x + child1->allocation.width / 2 - compare->x);
1577       gint x2 = abs (child2->allocation.x + child2->allocation.width / 2 - compare->x);
1578
1579       if (compare->y < y1)
1580         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1581       else
1582         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1583     }
1584   else
1585     return (y1 < y2) ? -1 : 1;
1586 }
1587
1588 static gboolean
1589 gtk_container_focus_up_down (GtkContainer     *container,
1590                              GList           **children,
1591                              GtkDirectionType  direction)
1592 {
1593   CompareInfo compare;
1594   GList *tmp_list;
1595
1596   if (container->focus_child)
1597     {
1598       gint compare_x1;
1599       gint compare_x2;
1600       gint compare_y;
1601       
1602       /* Delete widgets from list that don't match minimum criteria */
1603
1604       compare_x1 = container->focus_child->allocation.x;
1605       compare_x2 = container->focus_child->allocation.x + container->focus_child->allocation.width;
1606
1607       if (direction == GTK_DIR_UP)
1608         compare_y = container->focus_child->allocation.y;
1609       else
1610         compare_y = container->focus_child->allocation.y + container->focus_child->allocation.height;
1611       
1612       tmp_list = *children;
1613       while (tmp_list)
1614         {
1615           GtkWidget *child = tmp_list->data;
1616           GList *next = tmp_list->next;
1617           gint child_x1, child_x2;
1618           
1619           if (child != container->focus_child)
1620             {
1621               child_x1 = child->allocation.x;
1622               child_x2 = child->allocation.x + child->allocation.width;
1623               
1624               if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1625                   (direction == GTK_DIR_DOWN && child->allocation.y + child->allocation.height < compare_y) || /* Not below */
1626                   (direction == GTK_DIR_UP && child->allocation.y > compare_y)) /* Not above */
1627                 {
1628                   *children = g_list_delete_link (*children, tmp_list);
1629                 }
1630             }
1631           
1632           tmp_list = next;
1633         }
1634
1635       compare.x = (compare_x1 + compare_x2) / 2;
1636       compare.y = container->focus_child->allocation.y + container->focus_child->allocation.height / 2;
1637     }
1638   else
1639     {
1640       /* No old focus widget, need to figure out starting x,y some other way
1641        */
1642       GtkWidget *widget = GTK_WIDGET (container);
1643       GdkRectangle old_focus_rect;
1644
1645       if (old_focus_coords (container, &old_focus_rect))
1646         {
1647           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1648         }
1649       else
1650         {
1651           if (GTK_WIDGET_NO_WINDOW (widget))
1652             compare.x = widget->allocation.x + widget->allocation.width / 2;
1653           else
1654             compare.x = widget->allocation.width / 2;
1655         }
1656       
1657       if (GTK_WIDGET_NO_WINDOW (widget))
1658         compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1659       else
1660         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1661     }
1662
1663   *children = g_list_sort_with_data (*children, up_down_compare, &compare);
1664
1665   if (direction == GTK_DIR_UP)
1666     *children = g_list_reverse (*children);
1667
1668   return gtk_container_focus_move (container, *children, direction);
1669 }
1670
1671 static gint
1672 left_right_compare (gconstpointer a,
1673                     gconstpointer b,
1674                     gpointer      data)
1675 {
1676   const GtkWidget *child1 = a;
1677   const GtkWidget *child2 = b;
1678   CompareInfo *compare = data;
1679
1680   gint x1 = child1->allocation.x + child1->allocation.width / 2;
1681   gint x2 = child2->allocation.x + child2->allocation.width / 2;
1682
1683   if (x1 == x2)
1684     {
1685       gint y1 = abs (child1->allocation.y + child1->allocation.height / 2 - compare->y);
1686       gint y2 = abs (child2->allocation.y + child2->allocation.height / 2 - compare->y);
1687
1688       if (compare->x < x1)
1689         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1690       else
1691         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1692     }
1693   else
1694     return (x1 < x2) ? -1 : 1;
1695 }
1696
1697 static gboolean
1698 gtk_container_focus_left_right (GtkContainer     *container,
1699                                 GList           **children,
1700                                 GtkDirectionType  direction)
1701 {
1702   CompareInfo compare;
1703   GList *tmp_list;
1704
1705   if (container->focus_child)
1706     {
1707       gint compare_y1;
1708       gint compare_y2;
1709       gint compare_x;
1710       
1711       /* Delete widgets from list that don't match minimum criteria */
1712
1713       compare_y1 = container->focus_child->allocation.y;
1714       compare_y2 = container->focus_child->allocation.y + container->focus_child->allocation.height;
1715
1716       if (direction == GTK_DIR_LEFT)
1717         compare_x = container->focus_child->allocation.x;
1718       else
1719         compare_x = container->focus_child->allocation.x + container->focus_child->allocation.width;
1720       
1721       tmp_list = *children;
1722       while (tmp_list)
1723         {
1724           GtkWidget *child = tmp_list->data;
1725           GList *next = tmp_list->next;
1726           gint child_y1, child_y2;
1727           
1728           if (child != container->focus_child)
1729             {
1730               child_y1 = child->allocation.y;
1731               child_y2 = child->allocation.y + child->allocation.height;
1732               
1733               if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1734                   (direction == GTK_DIR_RIGHT && child->allocation.x + child->allocation.width < compare_x) || /* Not to left */
1735                   (direction == GTK_DIR_LEFT && child->allocation.x > compare_x)) /* Not to right */
1736                 {
1737                   *children = g_list_delete_link (*children, tmp_list);
1738                 }
1739             }
1740           
1741           tmp_list = next;
1742         }
1743
1744       compare.y = (compare_y1 + compare_y2) / 2;
1745       compare.x = container->focus_child->allocation.x + container->focus_child->allocation.width / 2;
1746     }
1747   else
1748     {
1749       /* No old focus widget, need to figure out starting x,y some other way
1750        */
1751       GtkWidget *widget = GTK_WIDGET (container);
1752       GdkRectangle old_focus_rect;
1753
1754       if (old_focus_coords (container, &old_focus_rect))
1755         {
1756           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1757         }
1758       else
1759         {
1760           if (GTK_WIDGET_NO_WINDOW (widget))
1761             compare.y = widget->allocation.y + widget->allocation.height / 2;
1762           else
1763             compare.y = widget->allocation.height / 2;
1764         }
1765       
1766       if (GTK_WIDGET_NO_WINDOW (widget))
1767         compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1768       else
1769         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1770     }
1771
1772   *children = g_list_sort_with_data (*children, left_right_compare, &compare);
1773
1774   if (direction == GTK_DIR_LEFT)
1775     *children = g_list_reverse (*children);
1776
1777   return gtk_container_focus_move (container, *children, direction);
1778 }
1779
1780 static gboolean
1781 gtk_container_focus_move (GtkContainer     *container,
1782                           GList            *children,
1783                           GtkDirectionType  direction)
1784 {
1785   GtkWidget *focus_child;
1786   GtkWidget *child;
1787
1788   focus_child = container->focus_child;
1789
1790   while (children)
1791     {
1792       child = children->data;
1793       children = children->next;
1794
1795       if (!child)
1796         continue;
1797
1798       if (focus_child)
1799         {
1800           if (focus_child == child)
1801             {
1802               focus_child = NULL;
1803
1804               if (GTK_WIDGET_DRAWABLE (child) &&
1805                   GTK_IS_CONTAINER (child))
1806                 if (gtk_container_focus (GTK_CONTAINER (child), direction))
1807                   return TRUE;
1808             }
1809         }
1810       else if (GTK_WIDGET_DRAWABLE (child))
1811         {
1812           if (GTK_IS_CONTAINER (child))
1813             {
1814               if (gtk_container_focus (GTK_CONTAINER (child), direction))
1815                 return TRUE;
1816             }
1817           else if (GTK_WIDGET_CAN_FOCUS (child))
1818             {
1819               gtk_widget_grab_focus (child);
1820               return TRUE;
1821             }
1822         }
1823     }
1824
1825   return FALSE;
1826 }
1827
1828
1829 static void
1830 gtk_container_children_callback (GtkWidget *widget,
1831                                  gpointer   client_data)
1832 {
1833   GList **children;
1834
1835   children = (GList**) client_data;
1836   *children = g_list_prepend (*children, widget);
1837 }
1838
1839 void
1840 gtk_container_set_focus_vadjustment (GtkContainer  *container,
1841                                      GtkAdjustment *adjustment)
1842 {
1843   g_return_if_fail (container != NULL);
1844   g_return_if_fail (GTK_IS_CONTAINER (container));
1845   if (adjustment)
1846     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1847
1848   if (adjustment)
1849     gtk_object_ref (GTK_OBJECT(adjustment));
1850
1851   gtk_object_set_data_by_id_full (GTK_OBJECT (container),
1852                                   vadjustment_key_id,
1853                                   adjustment,
1854                                   (GtkDestroyNotify) gtk_object_unref);
1855 }
1856
1857 void
1858 gtk_container_set_focus_hadjustment (GtkContainer  *container,
1859                                      GtkAdjustment *adjustment)
1860 {
1861   g_return_if_fail (container != NULL);
1862   g_return_if_fail (GTK_IS_CONTAINER (container));
1863   if (adjustment)
1864     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1865
1866   if (adjustment)
1867     gtk_object_ref (GTK_OBJECT (adjustment));
1868
1869   gtk_object_set_data_by_id_full (GTK_OBJECT (container),
1870                                   hadjustment_key_id,
1871                                   adjustment,
1872                                   (GtkDestroyNotify) gtk_object_unref);
1873 }
1874
1875
1876 static void
1877 gtk_container_show_all (GtkWidget *widget)
1878 {
1879   g_return_if_fail (widget != NULL);
1880   g_return_if_fail (GTK_IS_CONTAINER (widget));
1881
1882   gtk_container_foreach (GTK_CONTAINER (widget),
1883                          (GtkCallback) gtk_widget_show_all,
1884                          NULL);
1885   gtk_widget_show (widget);
1886 }
1887
1888 static void
1889 gtk_container_hide_all (GtkWidget *widget)
1890 {
1891   g_return_if_fail (widget != NULL);
1892   g_return_if_fail (GTK_IS_CONTAINER (widget));
1893
1894   gtk_widget_hide (widget);
1895   gtk_container_foreach (GTK_CONTAINER (widget),
1896                          (GtkCallback) gtk_widget_hide_all,
1897                          NULL);
1898 }