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