]> Pileus Git - ~andy/gtk/blob - gtk/gtkcontainer.c
Move destroy signal to GtkWidget
[~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 "config.h"
28
29 #include "gtkcontainer.h"
30
31 #include <stdarg.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "gtkbuildable.h"
36 #include "gtkbuilderprivate.h"
37 #include "gtkprivate.h"
38 #include "gtkmain.h"
39 #include "gtkmarshalers.h"
40 #include "gtksizerequest.h"
41 #include "gtkwindow.h"
42 #include "gtkintl.h"
43 #include "gtktoolbar.h"
44 #include <gobject/gobjectnotifyqueue.c>
45 #include <gobject/gvaluecollector.h>
46
47 struct _GtkContainerPrivate
48 {
49   GtkWidget *focus_child;
50
51   guint border_width : 16;
52
53   guint has_focus_chain    : 1;
54   guint need_resize        : 1;
55   guint reallocate_redraws : 1;
56   guint resize_mode        : 2;
57 };
58
59 enum {
60   ADD,
61   REMOVE,
62   CHECK_RESIZE,
63   SET_FOCUS_CHILD,
64   LAST_SIGNAL
65 };
66
67 enum {
68   PROP_0,
69   PROP_BORDER_WIDTH,
70   PROP_RESIZE_MODE,
71   PROP_CHILD
72 };
73
74 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
75 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
76
77
78 /* --- prototypes --- */
79 static void     gtk_container_base_class_init      (GtkContainerClass *klass);
80 static void     gtk_container_base_class_finalize  (GtkContainerClass *klass);
81 static void     gtk_container_class_init           (GtkContainerClass *klass);
82 static void     gtk_container_init                 (GtkContainer      *container);
83 static void     gtk_container_destroy              (GtkWidget         *widget);
84 static void     gtk_container_set_property         (GObject         *object,
85                                                     guint            prop_id,
86                                                     const GValue    *value,
87                                                     GParamSpec      *pspec);
88 static void     gtk_container_get_property         (GObject         *object,
89                                                     guint            prop_id,
90                                                     GValue          *value,
91                                                     GParamSpec      *pspec);
92 static void     gtk_container_add_unimplemented    (GtkContainer      *container,
93                                                     GtkWidget         *widget);
94 static void     gtk_container_remove_unimplemented (GtkContainer      *container,
95                                                     GtkWidget         *widget);
96 static void     gtk_container_real_check_resize    (GtkContainer      *container);
97 static gboolean gtk_container_focus                (GtkWidget         *widget,
98                                                     GtkDirectionType   direction);
99 static void     gtk_container_real_set_focus_child (GtkContainer      *container,
100                                                     GtkWidget         *widget);
101
102 static gboolean gtk_container_focus_move           (GtkContainer      *container,
103                                                     GList             *children,
104                                                     GtkDirectionType   direction);
105 static void     gtk_container_children_callback    (GtkWidget         *widget,
106                                                     gpointer           client_data);
107 static void     gtk_container_show_all             (GtkWidget         *widget);
108 static void     gtk_container_hide_all             (GtkWidget         *widget);
109 static gint     gtk_container_draw                 (GtkWidget         *widget,
110                                                     cairo_t           *cr);
111 static void     gtk_container_map                  (GtkWidget         *widget);
112 static void     gtk_container_unmap                (GtkWidget         *widget);
113 static void     gtk_container_adjust_size_request  (GtkWidget         *widget,
114                                                     GtkOrientation     orientation,
115                                                     gint               for_size,
116                                                     gint              *minimum_size,
117                                                     gint              *natural_size);
118 static void     gtk_container_adjust_size_allocation (GtkWidget       *widget,
119                                                       GtkAllocation   *allocation);
120
121 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
122                                                           GtkWidget    *child);
123
124 /* GtkBuildable */
125 static void gtk_container_buildable_init           (GtkBuildableIface *iface);
126 static void gtk_container_buildable_add_child      (GtkBuildable *buildable,
127                                                     GtkBuilder   *builder,
128                                                     GObject      *child,
129                                                     const gchar  *type);
130 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
131                                                           GtkBuilder    *builder,
132                                                           GObject       *child,
133                                                           const gchar   *tagname,
134                                                           GMarkupParser *parser,
135                                                           gpointer      *data);
136 static void    gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
137                                                        GtkBuilder   *builder,
138                                                        GObject      *child,
139                                                        const gchar  *tagname,
140                                                        gpointer     *data);
141
142
143 /* --- variables --- */
144 static const gchar           vadjustment_key[] = "gtk-vadjustment";
145 static guint                 vadjustment_key_id = 0;
146 static const gchar           hadjustment_key[] = "gtk-hadjustment";
147 static guint                 hadjustment_key_id = 0;
148 static GSList               *container_resize_queue = NULL;
149 static guint                 container_signals[LAST_SIGNAL] = { 0 };
150 static GtkWidgetClass       *parent_class = NULL;
151 extern GParamSpecPool       *_gtk_widget_child_property_pool;
152 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
153 static GtkBuildableIface    *parent_buildable_iface;
154
155
156 /* --- functions --- */
157 GType
158 gtk_container_get_type (void)
159 {
160   static GType container_type = 0;
161
162   if (!container_type)
163     {
164       const GTypeInfo container_info =
165       {
166         sizeof (GtkContainerClass),
167         (GBaseInitFunc) gtk_container_base_class_init,
168         (GBaseFinalizeFunc) gtk_container_base_class_finalize,
169         (GClassInitFunc) gtk_container_class_init,
170         NULL        /* class_finalize */,
171         NULL        /* class_data */,
172         sizeof (GtkContainer),
173         0           /* n_preallocs */,
174         (GInstanceInitFunc) gtk_container_init,
175         NULL,       /* value_table */
176       };
177
178       const GInterfaceInfo buildable_info =
179       {
180         (GInterfaceInitFunc) gtk_container_buildable_init,
181         NULL,
182         NULL
183       };
184
185       container_type =
186         g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"), 
187                                 &container_info, G_TYPE_FLAG_ABSTRACT);
188
189       g_type_add_interface_static (container_type,
190                                    GTK_TYPE_BUILDABLE,
191                                    &buildable_info);
192
193     }
194
195   return container_type;
196 }
197
198 static void
199 gtk_container_base_class_init (GtkContainerClass *class)
200 {
201   /* reset instance specifc class fields that don't get inherited */
202   class->set_child_property = NULL;
203   class->get_child_property = NULL;
204 }
205
206 static void
207 gtk_container_base_class_finalize (GtkContainerClass *class)
208 {
209   GList *list, *node;
210
211   list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
212   for (node = list; node; node = node->next)
213     {
214       GParamSpec *pspec = node->data;
215
216       g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
217       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
218       g_param_spec_unref (pspec);
219     }
220   g_list_free (list);
221 }
222
223 static void
224 gtk_container_class_init (GtkContainerClass *class)
225 {
226   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
227   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
228
229   parent_class = g_type_class_peek_parent (class);
230
231   vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
232   hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
233   
234   gobject_class->set_property = gtk_container_set_property;
235   gobject_class->get_property = gtk_container_get_property;
236
237   widget_class->destroy = gtk_container_destroy;
238   widget_class->show_all = gtk_container_show_all;
239   widget_class->hide_all = gtk_container_hide_all;
240   widget_class->draw = gtk_container_draw;
241   widget_class->map = gtk_container_map;
242   widget_class->unmap = gtk_container_unmap;
243   widget_class->focus = gtk_container_focus;
244
245   widget_class->adjust_size_request = gtk_container_adjust_size_request;
246   widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
247
248   class->add = gtk_container_add_unimplemented;
249   class->remove = gtk_container_remove_unimplemented;
250   class->check_resize = gtk_container_real_check_resize;
251   class->forall = NULL;
252   class->set_focus_child = gtk_container_real_set_focus_child;
253   class->child_type = NULL;
254   class->composite_name = gtk_container_child_default_composite_name;
255
256   g_object_class_install_property (gobject_class,
257                                    PROP_RESIZE_MODE,
258                                    g_param_spec_enum ("resize-mode",
259                                                       P_("Resize mode"),
260                                                       P_("Specify how resize events are handled"),
261                                                       GTK_TYPE_RESIZE_MODE,
262                                                       GTK_RESIZE_PARENT,
263                                                       GTK_PARAM_READWRITE));
264   g_object_class_install_property (gobject_class,
265                                    PROP_BORDER_WIDTH,
266                                    g_param_spec_uint ("border-width",
267                                                       P_("Border width"),
268                                                       P_("The width of the empty border outside the containers children"),
269                                                       0,
270                                                       65535,
271                                                       0,
272                                                       GTK_PARAM_READWRITE));
273   g_object_class_install_property (gobject_class,
274                                    PROP_CHILD,
275                                    g_param_spec_object ("child",
276                                                       P_("Child"),
277                                                       P_("Can be used to add a new child to the container"),
278                                                       GTK_TYPE_WIDGET,
279                                                       GTK_PARAM_WRITABLE));
280   container_signals[ADD] =
281     g_signal_new (I_("add"),
282                   G_OBJECT_CLASS_TYPE (gobject_class),
283                   G_SIGNAL_RUN_FIRST,
284                   G_STRUCT_OFFSET (GtkContainerClass, add),
285                   NULL, NULL,
286                   _gtk_marshal_VOID__OBJECT,
287                   G_TYPE_NONE, 1,
288                   GTK_TYPE_WIDGET);
289   container_signals[REMOVE] =
290     g_signal_new (I_("remove"),
291                   G_OBJECT_CLASS_TYPE (gobject_class),
292                   G_SIGNAL_RUN_FIRST,
293                   G_STRUCT_OFFSET (GtkContainerClass, remove),
294                   NULL, NULL,
295                   _gtk_marshal_VOID__OBJECT,
296                   G_TYPE_NONE, 1,
297                   GTK_TYPE_WIDGET);
298   container_signals[CHECK_RESIZE] =
299     g_signal_new (I_("check-resize"),
300                   G_OBJECT_CLASS_TYPE (gobject_class),
301                   G_SIGNAL_RUN_LAST,
302                   G_STRUCT_OFFSET (GtkContainerClass, check_resize),
303                   NULL, NULL,
304                   _gtk_marshal_VOID__VOID,
305                   G_TYPE_NONE, 0);
306   container_signals[SET_FOCUS_CHILD] =
307     g_signal_new (I_("set-focus-child"),
308                   G_OBJECT_CLASS_TYPE (gobject_class),
309                   G_SIGNAL_RUN_FIRST,
310                   G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
311                   NULL, NULL,
312                   _gtk_marshal_VOID__OBJECT,
313                   G_TYPE_NONE, 1,
314                   GTK_TYPE_WIDGET);
315
316   g_type_class_add_private (class, sizeof (GtkContainerPrivate));
317 }
318
319 static void
320 gtk_container_buildable_init (GtkBuildableIface *iface)
321 {
322   parent_buildable_iface = g_type_interface_peek_parent (iface);
323   iface->add_child = gtk_container_buildable_add_child;
324   iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
325   iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
326 }
327
328 static void
329 gtk_container_buildable_add_child (GtkBuildable  *buildable,
330                                    GtkBuilder    *builder,
331                                    GObject       *child,
332                                    const gchar   *type)
333 {
334   if (type)
335     {
336       GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
337     }
338   else if (GTK_IS_WIDGET (child) &&
339            gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
340     {
341       gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
342     }
343   else
344     g_warning ("Cannot add an object of type %s to a container of type %s", 
345                g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
346 }
347
348 static void
349 gtk_container_buildable_set_child_property (GtkContainer *container,
350                                             GtkBuilder   *builder,
351                                             GtkWidget    *child,
352                                             gchar        *name,
353                                             const gchar  *value)
354 {
355   GParamSpec *pspec;
356   GValue gvalue = { 0, };
357   GError *error = NULL;
358   
359   pspec = gtk_container_class_find_child_property
360     (G_OBJECT_GET_CLASS (container), name);
361   if (!pspec)
362     {
363       g_warning ("%s does not have a property called %s",
364                  g_type_name (G_OBJECT_TYPE (container)), name);
365       return;
366     }
367
368   if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
369     {
370       g_warning ("Could not read property %s:%s with value %s of type %s: %s",
371                  g_type_name (G_OBJECT_TYPE (container)),
372                  name,
373                  value,
374                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
375                  error->message);
376       g_error_free (error);
377       return;
378     }
379
380   gtk_container_child_set_property (container, child, name, &gvalue);
381   g_value_unset (&gvalue);
382 }
383
384 typedef struct {
385   GtkBuilder   *builder;
386   GtkContainer *container;
387   GtkWidget    *child;
388   gchar        *child_prop_name;
389   gchar        *context;
390   gboolean     translatable;
391 } PackingPropertiesData;
392
393 static void
394 attributes_start_element (GMarkupParseContext *context,
395                           const gchar         *element_name,
396                           const gchar        **names,
397                           const gchar        **values,
398                           gpointer             user_data,
399                           GError             **error)
400 {
401   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
402   guint i;
403
404   if (strcmp (element_name, "property") == 0)
405     {
406       for (i = 0; names[i]; i++)
407         if (strcmp (names[i], "name") == 0)
408           parser_data->child_prop_name = g_strdup (values[i]);
409         else if (strcmp (names[i], "translatable") == 0)
410           {
411             if (!_gtk_builder_boolean_from_string (values[1],
412                                                    &parser_data->translatable,
413                                                    error))
414               return;
415           }
416         else if (strcmp (names[i], "comments") == 0)
417           ; /* for translators */
418         else if (strcmp (names[i], "context") == 0)
419           parser_data->context = g_strdup (values[1]);
420         else
421           g_warning ("Unsupported attribute for GtkContainer Child "
422                      "property: %s\n", names[i]);
423     }
424   else if (strcmp (element_name, "packing") == 0)
425     return;
426   else
427     g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
428 }
429
430 static void
431 attributes_text_element (GMarkupParseContext *context,
432                          const gchar         *text,
433                          gsize                text_len,
434                          gpointer             user_data,
435                          GError             **error)
436 {
437   PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
438   gchar* value;
439
440   if (!parser_data->child_prop_name)
441     return;
442   
443   if (parser_data->translatable && text_len)
444     {
445       const gchar* domain;
446       domain = gtk_builder_get_translation_domain (parser_data->builder);
447       
448       value = _gtk_builder_parser_translate (domain,
449                                              parser_data->context,
450                                              text);
451     }
452   else
453     {
454       value = g_strdup (text);
455     }
456
457   gtk_container_buildable_set_child_property (parser_data->container,
458                                               parser_data->builder,
459                                               parser_data->child,
460                                               parser_data->child_prop_name,
461                                               value);
462
463   g_free (parser_data->child_prop_name);
464   g_free (parser_data->context);
465   g_free (value);
466   parser_data->child_prop_name = NULL;
467   parser_data->context = NULL;
468   parser_data->translatable = FALSE;
469 }
470
471 static const GMarkupParser attributes_parser =
472   {
473     attributes_start_element,
474     NULL,
475     attributes_text_element,
476   };
477
478 static gboolean
479 gtk_container_buildable_custom_tag_start (GtkBuildable  *buildable,
480                                           GtkBuilder    *builder,
481                                           GObject       *child,
482                                           const gchar   *tagname,
483                                           GMarkupParser *parser,
484                                           gpointer      *data)
485 {
486   PackingPropertiesData *parser_data;
487
488   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
489                                                 tagname, parser, data))
490     return TRUE;
491
492   if (child && strcmp (tagname, "packing") == 0)
493     {
494       parser_data = g_slice_new0 (PackingPropertiesData);
495       parser_data->builder = builder;
496       parser_data->container = GTK_CONTAINER (buildable);
497       parser_data->child = GTK_WIDGET (child);
498       parser_data->child_prop_name = NULL;
499
500       *parser = attributes_parser;
501       *data = parser_data;
502       return TRUE;
503     }
504
505   return FALSE;
506 }
507
508 static void
509 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
510                                         GtkBuilder   *builder,
511                                         GObject      *child,
512                                         const gchar  *tagname,
513                                         gpointer     *data)
514 {
515   if (strcmp (tagname, "packing") == 0)
516     {
517       g_slice_free (PackingPropertiesData, (gpointer)data);
518       return;
519
520     }
521
522   if (parent_buildable_iface->custom_tag_end)
523     parent_buildable_iface->custom_tag_end (buildable, builder,
524                                             child, tagname, data);
525
526 }
527
528 /**
529  * gtk_container_child_type: 
530  * @container: a #GtkContainer
531  *
532  * Returns the type of the children supported by the container.
533  *
534  * Note that this may return %G_TYPE_NONE to indicate that no more
535  * children can be added, e.g. for a #GtkPaned which already has two 
536  * children.
537  *
538  * Return value: a #GType.
539  **/
540 GType
541 gtk_container_child_type (GtkContainer *container)
542 {
543   GType slot;
544   GtkContainerClass *class;
545
546   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
547
548   class = GTK_CONTAINER_GET_CLASS (container);
549   if (class->child_type)
550     slot = class->child_type (container);
551   else
552     slot = G_TYPE_NONE;
553
554   return slot;
555 }
556
557 /* --- GtkContainer child property mechanism --- */
558 static inline void
559 container_get_child_property (GtkContainer *container,
560                               GtkWidget    *child,
561                               GParamSpec   *pspec,
562                               GValue       *value)
563 {
564   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
565   
566   class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
567 }
568
569 static inline void
570 container_set_child_property (GtkContainer       *container,
571                               GtkWidget          *child,
572                               GParamSpec         *pspec,
573                               const GValue       *value,
574                               GObjectNotifyQueue *nqueue)
575 {
576   GValue tmp_value = { 0, };
577   GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
578
579   /* provide a copy to work from, convert (if necessary) and validate */
580   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
581   if (!g_value_transform (value, &tmp_value))
582     g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
583                pspec->name,
584                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
585                G_VALUE_TYPE_NAME (value));
586   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
587     {
588       gchar *contents = g_strdup_value_contents (value);
589
590       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
591                  contents,
592                  G_VALUE_TYPE_NAME (value),
593                  pspec->name,
594                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
595       g_free (contents);
596     }
597   else
598     {
599       class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
600       g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
601     }
602   g_value_unset (&tmp_value);
603 }
604
605 /**
606  * gtk_container_child_get_valist:
607  * @container: a #GtkContainer
608  * @child: a widget which is a child of @container
609  * @first_property_name: the name of the first property to get
610  * @var_args: return location for the first property, followed 
611  *     optionally by more name/return location pairs, followed by %NULL
612  * 
613  * Gets the values of one or more child properties for @child and @container.
614  **/
615 void
616 gtk_container_child_get_valist (GtkContainer *container,
617                                 GtkWidget    *child,
618                                 const gchar  *first_property_name,
619                                 va_list       var_args)
620 {
621   const gchar *name;
622
623   g_return_if_fail (GTK_IS_CONTAINER (container));
624   g_return_if_fail (GTK_IS_WIDGET (child));
625   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
626
627   g_object_ref (container);
628   g_object_ref (child);
629
630   name = first_property_name;
631   while (name)
632     {
633       GValue value = { 0, };
634       GParamSpec *pspec;
635       gchar *error;
636
637       pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
638                                         name,
639                                         G_OBJECT_TYPE (container),
640                                         TRUE);
641       if (!pspec)
642         {
643           g_warning ("%s: container class `%s' has no child property named `%s'",
644                      G_STRLOC,
645                      G_OBJECT_TYPE_NAME (container),
646                      name);
647           break;
648         }
649       if (!(pspec->flags & G_PARAM_READABLE))
650         {
651           g_warning ("%s: child property `%s' of container class `%s' is not readable",
652                      G_STRLOC,
653                      pspec->name,
654                      G_OBJECT_TYPE_NAME (container));
655           break;
656         }
657       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
658       container_get_child_property (container, child, pspec, &value);
659       G_VALUE_LCOPY (&value, var_args, 0, &error);
660       if (error)
661         {
662           g_warning ("%s: %s", G_STRLOC, error);
663           g_free (error);
664           g_value_unset (&value);
665           break;
666         }
667       g_value_unset (&value);
668       name = va_arg (var_args, gchar*);
669     }
670
671   g_object_unref (child);
672   g_object_unref (container);
673 }
674
675 /**
676  * gtk_container_child_get_property:
677  * @container: a #GtkContainer
678  * @child: a widget which is a child of @container
679  * @property_name: the name of the property to get
680  * @value: a location to return the value
681  * 
682  * Gets the value of a child property for @child and @container.
683  **/
684 void
685 gtk_container_child_get_property (GtkContainer *container,
686                                   GtkWidget    *child,
687                                   const gchar  *property_name,
688                                   GValue       *value)
689 {
690   GParamSpec *pspec;
691
692   g_return_if_fail (GTK_IS_CONTAINER (container));
693   g_return_if_fail (GTK_IS_WIDGET (child));
694   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
695   g_return_if_fail (property_name != NULL);
696   g_return_if_fail (G_IS_VALUE (value));
697   
698   g_object_ref (container);
699   g_object_ref (child);
700   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
701                                     G_OBJECT_TYPE (container), TRUE);
702   if (!pspec)
703     g_warning ("%s: container class `%s' has no child property named `%s'",
704                G_STRLOC,
705                G_OBJECT_TYPE_NAME (container),
706                property_name);
707   else if (!(pspec->flags & G_PARAM_READABLE))
708     g_warning ("%s: child property `%s' of container class `%s' is not readable",
709                G_STRLOC,
710                pspec->name,
711                G_OBJECT_TYPE_NAME (container));
712   else
713     {
714       GValue *prop_value, tmp_value = { 0, };
715
716       /* auto-conversion of the callers value type
717        */
718       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
719         {
720           g_value_reset (value);
721           prop_value = value;
722         }
723       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
724         {
725           g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
726                      pspec->name,
727                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
728                      G_VALUE_TYPE_NAME (value));
729           g_object_unref (child);
730           g_object_unref (container);
731           return;
732         }
733       else
734         {
735           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
736           prop_value = &tmp_value;
737         }
738       container_get_child_property (container, child, pspec, prop_value);
739       if (prop_value != value)
740         {
741           g_value_transform (prop_value, value);
742           g_value_unset (&tmp_value);
743         }
744     }
745   g_object_unref (child);
746   g_object_unref (container);
747 }
748
749 /**
750  * gtk_container_child_set_valist:
751  * @container: a #GtkContainer
752  * @child: a widget which is a child of @container
753  * @first_property_name: the name of the first property to set
754  * @var_args: a %NULL-terminated list of property names and values, starting
755  *           with @first_prop_name
756  * 
757  * Sets one or more child properties for @child and @container.
758  **/
759 void
760 gtk_container_child_set_valist (GtkContainer *container,
761                                 GtkWidget    *child,
762                                 const gchar  *first_property_name,
763                                 va_list       var_args)
764 {
765   GObjectNotifyQueue *nqueue;
766   const gchar *name;
767
768   g_return_if_fail (GTK_IS_CONTAINER (container));
769   g_return_if_fail (GTK_IS_WIDGET (child));
770   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
771
772   g_object_ref (container);
773   g_object_ref (child);
774
775   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
776   name = first_property_name;
777   while (name)
778     {
779       GValue value = { 0, };
780       gchar *error = NULL;
781       GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
782                                                     name,
783                                                     G_OBJECT_TYPE (container),
784                                                     TRUE);
785       if (!pspec)
786         {
787           g_warning ("%s: container class `%s' has no child property named `%s'",
788                      G_STRLOC,
789                      G_OBJECT_TYPE_NAME (container),
790                      name);
791           break;
792         }
793       if (!(pspec->flags & G_PARAM_WRITABLE))
794         {
795           g_warning ("%s: child property `%s' of container class `%s' is not writable",
796                      G_STRLOC,
797                      pspec->name,
798                      G_OBJECT_TYPE_NAME (container));
799           break;
800         }
801       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
802       G_VALUE_COLLECT (&value, var_args, 0, &error);
803       if (error)
804         {
805           g_warning ("%s: %s", G_STRLOC, error);
806           g_free (error);
807
808           /* we purposely leak the value here, it might not be
809            * in a sane state if an error condition occoured
810            */
811           break;
812         }
813       container_set_child_property (container, child, pspec, &value, nqueue);
814       g_value_unset (&value);
815       name = va_arg (var_args, gchar*);
816     }
817   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
818
819   g_object_unref (container);
820   g_object_unref (child);
821 }
822
823 /**
824  * gtk_container_child_set_property:
825  * @container: a #GtkContainer
826  * @child: a widget which is a child of @container
827  * @property_name: the name of the property to set
828  * @value: the value to set the property to
829  * 
830  * Sets a child property for @child and @container.
831  **/
832 void
833 gtk_container_child_set_property (GtkContainer *container,
834                                   GtkWidget    *child,
835                                   const gchar  *property_name,
836                                   const GValue *value)
837 {
838   GObjectNotifyQueue *nqueue;
839   GParamSpec *pspec;
840
841   g_return_if_fail (GTK_IS_CONTAINER (container));
842   g_return_if_fail (GTK_IS_WIDGET (child));
843   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
844   g_return_if_fail (property_name != NULL);
845   g_return_if_fail (G_IS_VALUE (value));
846   
847   g_object_ref (container);
848   g_object_ref (child);
849
850   nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
851   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
852                                     G_OBJECT_TYPE (container), TRUE);
853   if (!pspec)
854     g_warning ("%s: container class `%s' has no child property named `%s'",
855                G_STRLOC,
856                G_OBJECT_TYPE_NAME (container),
857                property_name);
858   else if (!(pspec->flags & G_PARAM_WRITABLE))
859     g_warning ("%s: child property `%s' of container class `%s' is not writable",
860                G_STRLOC,
861                pspec->name,
862                G_OBJECT_TYPE_NAME (container));
863   else
864     {
865       container_set_child_property (container, child, pspec, value, nqueue);
866     }
867   g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
868   g_object_unref (container);
869   g_object_unref (child);
870 }
871
872 /**
873  * gtk_container_add_with_properties:
874  * @container: a #GtkContainer 
875  * @widget: a widget to be placed inside @container 
876  * @first_prop_name: the name of the first child property to set 
877  * @Varargs: a %NULL-terminated list of property names and values, starting
878  *           with @first_prop_name
879  * 
880  * Adds @widget to @container, setting child properties at the same time.
881  * See gtk_container_add() and gtk_container_child_set() for more details.
882  **/
883 void
884 gtk_container_add_with_properties (GtkContainer *container,
885                                    GtkWidget    *widget,
886                                    const gchar  *first_prop_name,
887                                    ...)
888 {
889   g_return_if_fail (GTK_IS_CONTAINER (container));
890   g_return_if_fail (GTK_IS_WIDGET (widget));
891   g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
892
893   g_object_ref (container);
894   g_object_ref (widget);
895   gtk_widget_freeze_child_notify (widget);
896
897   g_signal_emit (container, container_signals[ADD], 0, widget);
898   if (gtk_widget_get_parent (widget))
899     {
900       va_list var_args;
901
902       va_start (var_args, first_prop_name);
903       gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
904       va_end (var_args);
905     }
906
907   gtk_widget_thaw_child_notify (widget);
908   g_object_unref (widget);
909   g_object_unref (container);
910 }
911
912 /**
913  * gtk_container_child_set:
914  * @container: a #GtkContainer
915  * @child: a widget which is a child of @container
916  * @first_prop_name: the name of the first property to set
917  * @Varargs: a %NULL-terminated list of property names and values, starting
918  *           with @first_prop_name
919  * 
920  * Sets one or more child properties for @child and @container.
921  **/
922 void
923 gtk_container_child_set (GtkContainer      *container,
924                          GtkWidget         *child,
925                          const gchar       *first_prop_name,
926                          ...)
927 {
928   va_list var_args;
929   
930   g_return_if_fail (GTK_IS_CONTAINER (container));
931   g_return_if_fail (GTK_IS_WIDGET (child));
932   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
933
934   va_start (var_args, first_prop_name);
935   gtk_container_child_set_valist (container, child, first_prop_name, var_args);
936   va_end (var_args);
937 }
938
939 /**
940  * gtk_container_child_get:
941  * @container: a #GtkContainer
942  * @child: a widget which is a child of @container
943  * @first_prop_name: the name of the first property to get
944  * @Varargs: return location for the first property, followed 
945  *     optionally by more name/return location pairs, followed by %NULL
946  * 
947  * Gets the values of one or more child properties for @child and @container.
948  **/
949 void
950 gtk_container_child_get (GtkContainer      *container,
951                          GtkWidget         *child,
952                          const gchar       *first_prop_name,
953                          ...)
954 {
955   va_list var_args;
956   
957   g_return_if_fail (GTK_IS_CONTAINER (container));
958   g_return_if_fail (GTK_IS_WIDGET (child));
959   g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
960
961   va_start (var_args, first_prop_name);
962   gtk_container_child_get_valist (container, child, first_prop_name, var_args);
963   va_end (var_args);
964 }
965
966 /**
967  * gtk_container_class_install_child_property:
968  * @cclass: a #GtkContainerClass
969  * @property_id: the id for the property
970  * @pspec: the #GParamSpec for the property
971  * 
972  * Installs a child property on a container class. 
973  **/
974 void
975 gtk_container_class_install_child_property (GtkContainerClass *cclass,
976                                             guint              property_id,
977                                             GParamSpec        *pspec)
978 {
979   g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
980   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
981   if (pspec->flags & G_PARAM_WRITABLE)
982     g_return_if_fail (cclass->set_child_property != NULL);
983   if (pspec->flags & G_PARAM_READABLE)
984     g_return_if_fail (cclass->get_child_property != NULL);
985   g_return_if_fail (property_id > 0);
986   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
987   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
988     g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
989
990   if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
991     {
992       g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
993                  G_OBJECT_CLASS_NAME (cclass),
994                  pspec->name);
995       return;
996     }
997   g_param_spec_ref (pspec);
998   g_param_spec_sink (pspec);
999   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1000   g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1001 }
1002
1003 /**
1004  * gtk_container_class_find_child_property:
1005  * @cclass: a #GtkContainerClass
1006  * @property_name: the name of the child property to find
1007  * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
1008  *   child property with that name.
1009  *
1010  * Finds a child property of a container class by name.
1011  */
1012 GParamSpec*
1013 gtk_container_class_find_child_property (GObjectClass *cclass,
1014                                          const gchar  *property_name)
1015 {
1016   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1017   g_return_val_if_fail (property_name != NULL, NULL);
1018
1019   return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1020                                    property_name,
1021                                    G_OBJECT_CLASS_TYPE (cclass),
1022                                    TRUE);
1023 }
1024
1025 /**
1026  * gtk_container_class_list_child_properties:
1027  * @cclass: a #GtkContainerClass
1028  * @n_properties: location to return the number of child properties found
1029  * @returns: a newly allocated %NULL-terminated array of #GParamSpec*. 
1030  *           The array must be freed with g_free().
1031  *
1032  * Returns all child properties of a container class.
1033  */
1034 GParamSpec**
1035 gtk_container_class_list_child_properties (GObjectClass *cclass,
1036                                            guint        *n_properties)
1037 {
1038   GParamSpec **pspecs;
1039   guint n;
1040
1041   g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1042
1043   pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1044                                    G_OBJECT_CLASS_TYPE (cclass),
1045                                    &n);
1046   if (n_properties)
1047     *n_properties = n;
1048
1049   return pspecs;
1050 }
1051
1052 static void
1053 gtk_container_add_unimplemented (GtkContainer     *container,
1054                                  GtkWidget        *widget)
1055 {
1056   g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1057 }
1058
1059 static void
1060 gtk_container_remove_unimplemented (GtkContainer     *container,
1061                                     GtkWidget        *widget)
1062 {
1063   g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1064 }
1065
1066 static void
1067 gtk_container_init (GtkContainer *container)
1068 {
1069   GtkContainerPrivate *priv;
1070
1071   container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1072                                                  GTK_TYPE_CONTAINER,
1073                                                  GtkContainerPrivate);
1074   priv = container->priv;
1075
1076   priv->focus_child = NULL;
1077   priv->border_width = 0;
1078   priv->need_resize = FALSE;
1079   priv->resize_mode = GTK_RESIZE_PARENT;
1080   priv->reallocate_redraws = FALSE;
1081 }
1082
1083 static void
1084 gtk_container_destroy (GtkWidget *widget)
1085 {
1086   GtkContainer *container = GTK_CONTAINER (widget);
1087   GtkContainerPrivate *priv = container->priv;
1088
1089   if (GTK_CONTAINER_RESIZE_PENDING (container))
1090     _gtk_container_dequeue_resize_handler (container);
1091
1092   if (priv->focus_child)
1093     {
1094       g_object_unref (priv->focus_child);
1095       priv->focus_child = NULL;
1096     }
1097
1098   /* do this before walking child widgets, to avoid
1099    * removing children from focus chain one by one.
1100    */
1101   if (priv->has_focus_chain)
1102     gtk_container_unset_focus_chain (container);
1103
1104   gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1105
1106   GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1107 }
1108
1109 static void
1110 gtk_container_set_property (GObject         *object,
1111                             guint            prop_id,
1112                             const GValue    *value,
1113                             GParamSpec      *pspec)
1114 {
1115   GtkContainer *container = GTK_CONTAINER (object);
1116
1117   switch (prop_id)
1118     {
1119     case PROP_BORDER_WIDTH:
1120       gtk_container_set_border_width (container, g_value_get_uint (value));
1121       break;
1122     case PROP_RESIZE_MODE:
1123       gtk_container_set_resize_mode (container, g_value_get_enum (value));
1124       break;
1125     case PROP_CHILD:
1126       gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1127       break;
1128     default:
1129       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1130       break;
1131     }
1132 }
1133
1134 static void
1135 gtk_container_get_property (GObject         *object,
1136                             guint            prop_id,
1137                             GValue          *value,
1138                             GParamSpec      *pspec)
1139 {
1140   GtkContainer *container = GTK_CONTAINER (object);
1141   GtkContainerPrivate *priv = container->priv;
1142   
1143   switch (prop_id)
1144     {
1145     case PROP_BORDER_WIDTH:
1146       g_value_set_uint (value, priv->border_width);
1147       break;
1148     case PROP_RESIZE_MODE:
1149       g_value_set_enum (value, priv->resize_mode);
1150       break;
1151     default:
1152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1153       break;
1154     }
1155 }
1156
1157 /**
1158  * gtk_container_set_border_width:
1159  * @container: a #GtkContainer
1160  * @border_width: amount of blank space to leave <emphasis>outside</emphasis> 
1161  *   the container. Valid values are in the range 0-65535 pixels.
1162  *
1163  * Sets the border width of the container.
1164  *
1165  * The border width of a container is the amount of space to leave
1166  * around the outside of the container. The only exception to this is
1167  * #GtkWindow; because toplevel windows can't leave space outside,
1168  * they leave the space inside. The border is added on all sides of
1169  * the container. To add space to only one side, one approach is to
1170  * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1171  * to give it a size, and place it on the side of the container as
1172  * a spacer.
1173  **/
1174 void
1175 gtk_container_set_border_width (GtkContainer *container,
1176                                 guint         border_width)
1177 {
1178   GtkContainerPrivate *priv;
1179
1180   g_return_if_fail (GTK_IS_CONTAINER (container));
1181
1182   priv = container->priv;
1183
1184   if (priv->border_width != border_width)
1185     {
1186       priv->border_width = border_width;
1187       g_object_notify (G_OBJECT (container), "border-width");
1188       
1189       if (gtk_widget_get_realized (GTK_WIDGET (container)))
1190         gtk_widget_queue_resize (GTK_WIDGET (container));
1191     }
1192 }
1193
1194 /**
1195  * gtk_container_get_border_width:
1196  * @container: a #GtkContainer
1197  * 
1198  * Retrieves the border width of the container. See
1199  * gtk_container_set_border_width().
1200  *
1201  * Return value: the current border width
1202  **/
1203 guint
1204 gtk_container_get_border_width (GtkContainer *container)
1205 {
1206   g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1207
1208   return container->priv->border_width;
1209 }
1210
1211 /**
1212  * gtk_container_add:
1213  * @container: a #GtkContainer
1214  * @widget: a widget to be placed inside @container
1215  * 
1216  * Adds @widget to @container. Typically used for simple containers
1217  * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1218  * layout containers such as #GtkBox or #GtkTable, this function will
1219  * pick default packing parameters that may not be correct.  So
1220  * consider functions such as gtk_box_pack_start() and
1221  * gtk_table_attach() as an alternative to gtk_container_add() in
1222  * those cases. A widget may be added to only one container at a time;
1223  * you can't place the same widget inside two different containers.
1224  **/
1225 void
1226 gtk_container_add (GtkContainer *container,
1227                    GtkWidget    *widget)
1228 {
1229   GtkWidget *parent;
1230
1231   g_return_if_fail (GTK_IS_CONTAINER (container));
1232   g_return_if_fail (GTK_IS_WIDGET (widget));
1233
1234   parent = gtk_widget_get_parent (widget);
1235
1236   if (parent != NULL)
1237     {
1238       g_warning ("Attempting to add a widget with type %s to a container of "
1239                  "type %s, but the widget is already inside a container of type %s, "
1240                  "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1241                  "explains how to reparent a widget.",
1242                  g_type_name (G_OBJECT_TYPE (widget)),
1243                  g_type_name (G_OBJECT_TYPE (container)),
1244                  g_type_name (G_OBJECT_TYPE (parent)));
1245       return;
1246     }
1247
1248   g_signal_emit (container, container_signals[ADD], 0, widget);
1249 }
1250
1251 /**
1252  * gtk_container_remove:
1253  * @container: a #GtkContainer
1254  * @widget: a current child of @container
1255  * 
1256  * Removes @widget from @container. @widget must be inside @container.
1257  * Note that @container will own a reference to @widget, and that this
1258  * may be the last reference held; so removing a widget from its
1259  * container can destroy that widget. If you want to use @widget
1260  * again, you need to add a reference to it while it's not inside
1261  * a container, using g_object_ref(). If you don't want to use @widget
1262  * again it's usually more efficient to simply destroy it directly
1263  * using gtk_widget_destroy() since this will remove it from the
1264  * container and help break any circular reference count cycles.
1265  **/
1266 void
1267 gtk_container_remove (GtkContainer *container,
1268                       GtkWidget    *widget)
1269 {
1270   g_return_if_fail (GTK_IS_CONTAINER (container));
1271   g_return_if_fail (GTK_IS_WIDGET (widget));
1272   g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1273
1274   g_signal_emit (container, container_signals[REMOVE], 0, widget);
1275 }
1276
1277 void
1278 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1279 {
1280   g_return_if_fail (GTK_IS_CONTAINER (container));
1281   g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1282
1283   container_resize_queue = g_slist_remove (container_resize_queue, container);
1284   GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1285 }
1286
1287 /**
1288  * gtk_container_set_resize_mode:
1289  * @container: a #GtkContainer
1290  * @resize_mode: the new resize mode
1291  * 
1292  * Sets the resize mode for the container.
1293  *
1294  * The resize mode of a container determines whether a resize request 
1295  * will be passed to the container's parent, queued for later execution
1296  * or executed immediately.
1297  **/
1298 void
1299 gtk_container_set_resize_mode (GtkContainer  *container,
1300                                GtkResizeMode  resize_mode)
1301 {
1302   GtkContainerPrivate *priv;
1303
1304   g_return_if_fail (GTK_IS_CONTAINER (container));
1305   g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1306
1307   priv = container->priv;
1308   
1309   if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1310       resize_mode == GTK_RESIZE_PARENT)
1311     {
1312       resize_mode = GTK_RESIZE_QUEUE;
1313     }
1314   
1315   if (priv->resize_mode != resize_mode)
1316     {
1317       priv->resize_mode = resize_mode;
1318       
1319       gtk_widget_queue_resize (GTK_WIDGET (container));
1320       g_object_notify (G_OBJECT (container), "resize-mode");
1321     }
1322 }
1323
1324 /**
1325  * gtk_container_get_resize_mode:
1326  * @container: a #GtkContainer
1327  * 
1328  * Returns the resize mode for the container. See
1329  * gtk_container_set_resize_mode ().
1330  *
1331  * Return value: the current resize mode
1332  **/
1333 GtkResizeMode
1334 gtk_container_get_resize_mode (GtkContainer *container)
1335 {
1336   g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1337
1338   return container->priv->resize_mode;
1339 }
1340
1341 /**
1342  * gtk_container_set_reallocate_redraws:
1343  * @container: a #GtkContainer
1344  * @needs_redraws: the new value for the container's @reallocate_redraws flag
1345  *
1346  * Sets the @reallocate_redraws flag of the container to the given value.
1347  * 
1348  * Containers requesting reallocation redraws get automatically
1349  * redrawn if any of their children changed allocation. 
1350  **/ 
1351 void
1352 gtk_container_set_reallocate_redraws (GtkContainer *container,
1353                                       gboolean      needs_redraws)
1354 {
1355   g_return_if_fail (GTK_IS_CONTAINER (container));
1356
1357   container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1358 }
1359
1360 static GtkContainer*
1361 gtk_container_get_resize_container (GtkContainer *container)
1362 {
1363   GtkWidget *parent;
1364   GtkWidget *widget = GTK_WIDGET (container);
1365
1366   while ((parent = gtk_widget_get_parent (widget)))
1367     {
1368       widget = parent;
1369       if (GTK_IS_RESIZE_CONTAINER (widget))
1370         break;
1371     }
1372
1373   return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1374 }
1375
1376 static gboolean
1377 gtk_container_idle_sizer (gpointer data)
1378 {
1379   /* we may be invoked with a container_resize_queue of NULL, because
1380    * queue_resize could have been adding an extra idle function while
1381    * the queue still got processed. we better just ignore such case
1382    * than trying to explicitely work around them with some extra flags,
1383    * since it doesn't cause any actual harm.
1384    */
1385   while (container_resize_queue)
1386     {
1387       GSList *slist;
1388       GtkWidget *widget;
1389
1390       slist = container_resize_queue;
1391       container_resize_queue = slist->next;
1392       widget = slist->data;
1393       g_slist_free_1 (slist);
1394
1395       GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1396       gtk_container_check_resize (GTK_CONTAINER (widget));
1397     }
1398
1399   gdk_window_process_all_updates ();
1400
1401   return FALSE;
1402 }
1403
1404 void
1405 _gtk_container_queue_resize (GtkContainer *container)
1406 {
1407   GtkContainerPrivate *priv;
1408   GtkContainer *resize_container;
1409   GtkWidget *parent;
1410   GtkWidget *widget;
1411   
1412   g_return_if_fail (GTK_IS_CONTAINER (container));
1413
1414   priv = container->priv;
1415   widget = GTK_WIDGET (container);
1416
1417   resize_container = gtk_container_get_resize_container (container);
1418   
1419   while (TRUE)
1420     {
1421       GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1422       GTK_PRIVATE_SET_FLAG (widget, GTK_WIDTH_REQUEST_NEEDED);
1423       GTK_PRIVATE_SET_FLAG (widget, GTK_HEIGHT_REQUEST_NEEDED);
1424
1425       if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1426           !(parent = gtk_widget_get_parent (widget)))
1427         break;
1428
1429       widget = parent;
1430     }
1431       
1432   if (resize_container)
1433     {
1434       if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1435           (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1436            gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1437         {
1438           switch (resize_container->priv->resize_mode)
1439             {
1440             case GTK_RESIZE_QUEUE:
1441               if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1442                 {
1443                   GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1444                   if (container_resize_queue == NULL)
1445                     gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1446                                      gtk_container_idle_sizer,
1447                                      NULL, NULL);
1448                   container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1449                 }
1450               break;
1451
1452             case GTK_RESIZE_IMMEDIATE:
1453               gtk_container_check_resize (resize_container);
1454               break;
1455
1456             case GTK_RESIZE_PARENT:
1457               g_assert_not_reached ();
1458               break;
1459             }
1460         }
1461       else
1462         {
1463           /* we need to let hidden resize containers know that something
1464            * changed while they where hidden (currently only evaluated by
1465            * toplevels).
1466            */
1467           resize_container->priv->need_resize = TRUE;
1468         }
1469     }
1470 }
1471
1472 void
1473 gtk_container_check_resize (GtkContainer *container)
1474 {
1475   g_return_if_fail (GTK_IS_CONTAINER (container));
1476   
1477   g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1478 }
1479
1480 static void
1481 gtk_container_real_check_resize (GtkContainer *container)
1482 {
1483   GtkWidget *widget = GTK_WIDGET (container);
1484   GtkAllocation allocation;
1485   GtkRequisition requisition;
1486
1487   gtk_widget_get_preferred_size (widget,
1488                                  &requisition, NULL);
1489   gtk_widget_get_allocation (widget, &allocation);
1490
1491   if (requisition.width > allocation.width ||
1492       requisition.height > allocation.height)
1493     {
1494       if (GTK_IS_RESIZE_CONTAINER (container))
1495         {
1496           gtk_widget_size_allocate (widget, &allocation);
1497           gtk_widget_set_allocation (widget, &allocation);
1498         }
1499       else
1500         gtk_widget_queue_resize (widget);
1501     }
1502   else
1503     {
1504       gtk_container_resize_children (container);
1505     }
1506 }
1507
1508 /* The container hasn't changed size but one of its children
1509  *  queued a resize request. Which means that the allocation
1510  *  is not sufficient for the requisition of some child.
1511  *  We've already performed a size request at this point,
1512  *  so we simply need to reallocate and let the allocation
1513  *  trickle down via GTK_WIDGET_ALLOC_NEEDED flags. 
1514  */
1515 void
1516 gtk_container_resize_children (GtkContainer *container)
1517 {
1518   GtkAllocation allocation;
1519   GtkWidget *widget;
1520   
1521   /* resizing invariants:
1522    * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1523    * containers that have an idle sizer pending must be flagged with
1524    * RESIZE_PENDING.
1525    */
1526   g_return_if_fail (GTK_IS_CONTAINER (container));
1527
1528   widget = GTK_WIDGET (container);
1529   gtk_widget_get_allocation (widget, &allocation);
1530
1531   gtk_widget_size_allocate (widget, &allocation);
1532   gtk_widget_set_allocation (widget, &allocation);
1533 }
1534
1535 static void
1536 gtk_container_adjust_size_request (GtkWidget         *widget,
1537                                    GtkOrientation     orientation,
1538                                    gint               for_size,
1539                                    gint              *minimum_size,
1540                                    gint              *natural_size)
1541 {
1542   GtkContainer *container;
1543
1544   container = GTK_CONTAINER (widget);
1545
1546   if (GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1547     {
1548       int border_width;
1549
1550       border_width = container->priv->border_width;
1551
1552       *minimum_size += border_width * 2;
1553       *natural_size += border_width * 2;
1554     }
1555
1556   /* chain up last so gtk_widget_set_size_request() values
1557    * will have a chance to overwrite our border width.
1558    */
1559   parent_class->adjust_size_request (widget, orientation, for_size,
1560                                      minimum_size, natural_size);
1561 }
1562
1563 static void
1564 gtk_container_adjust_size_allocation (GtkWidget         *widget,
1565                                       GtkAllocation     *allocation)
1566 {
1567   GtkContainer *container;
1568   int border_width;
1569
1570   container = GTK_CONTAINER (widget);
1571
1572   parent_class->adjust_size_allocation (widget, allocation);
1573
1574   if (!GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1575     return;
1576
1577   border_width = container->priv->border_width;
1578
1579   allocation->width -= border_width * 2;
1580   allocation->height -= border_width * 2;
1581
1582   /* If we get a pathological too-small allocation to hold
1583    * even the border width, leave all allocation to the actual
1584    * widget, and leave x,y unchanged. (GtkWidget's min size is
1585    * 1x1 if you're wondering why <1 and not <0)
1586    *
1587    * As long as we have space, set x,y properly.
1588    */
1589
1590   if (allocation->width < 1)
1591     {
1592       allocation->width += border_width * 2;
1593     }
1594   else
1595     {
1596       allocation->x += border_width;
1597     }
1598
1599   if (allocation->height < 1)
1600     {
1601       allocation->height += border_width * 2;
1602     }
1603   else
1604     {
1605       allocation->y += border_width;
1606     }
1607 }
1608
1609 /**
1610  * gtk_container_class_handle_border_width:
1611  * @klass: the class struct of a #GtkContainer subclass
1612  *
1613  * Modifies a subclass of #GtkContainerClass to automatically add and
1614  * remove the border-width setting on GtkContainer.  This allows the
1615  * subclass to ignore the border width in its size request and
1616  * allocate methods. The intent is for a subclass to invoke this
1617  * in its class_init function.
1618  *
1619  * gtk_container_class_handle_border_width() is necessary because it
1620  * would break API too badly to make this behavior the default. So
1621  * subclasses must "opt in" to the parent class handling border_width
1622  * for them.
1623  */
1624 void
1625 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1626 {
1627   g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1628
1629   klass->handle_border_width = TRUE;
1630 }
1631
1632 /**
1633  * gtk_container_forall:
1634  * @container: a #GtkContainer
1635  * @callback: a callback
1636  * @callback_data: callback user data
1637  * 
1638  * Invokes @callback on each child of @container, including children
1639  * that are considered "internal" (implementation details of the
1640  * container). "Internal" children generally weren't added by the user
1641  * of the container, but were added by the container implementation
1642  * itself.  Most applications should use gtk_container_foreach(),
1643  * rather than gtk_container_forall().
1644  **/
1645 void
1646 gtk_container_forall (GtkContainer *container,
1647                       GtkCallback   callback,
1648                       gpointer      callback_data)
1649 {
1650   GtkContainerClass *class;
1651
1652   g_return_if_fail (GTK_IS_CONTAINER (container));
1653   g_return_if_fail (callback != NULL);
1654
1655   class = GTK_CONTAINER_GET_CLASS (container);
1656
1657   if (class->forall)
1658     class->forall (container, TRUE, callback, callback_data);
1659 }
1660
1661 /**
1662  * gtk_container_foreach:
1663  * @container: a #GtkContainer
1664  * @callback: (scope call):  a callback
1665  * @callback_data: callback user data
1666  * 
1667  * Invokes @callback on each non-internal child of @container. See
1668  * gtk_container_forall() for details on what constitutes an
1669  * "internal" child.  Most applications should use
1670  * gtk_container_foreach(), rather than gtk_container_forall().
1671  **/
1672 void
1673 gtk_container_foreach (GtkContainer *container,
1674                        GtkCallback   callback,
1675                        gpointer      callback_data)
1676 {
1677   GtkContainerClass *class;
1678   
1679   g_return_if_fail (GTK_IS_CONTAINER (container));
1680   g_return_if_fail (callback != NULL);
1681
1682   class = GTK_CONTAINER_GET_CLASS (container);
1683
1684   if (class->forall)
1685     class->forall (container, FALSE, callback, callback_data);
1686 }
1687
1688 /**
1689  * gtk_container_set_focus_child:
1690  * @container: a #GtkContainer
1691  * @child: (allow-none): a #GtkWidget, or %NULL
1692  *
1693  * Sets, or unsets if @child is %NULL, the focused child of @container.
1694  *
1695  * This function emits the GtkContainer::set_focus_child signal of
1696  * @container. Implementations of #GtkContainer can override the
1697  * default behaviour by overriding the class closure of this signal.
1698  *
1699  * This is function is mostly meant to be used by widgets. Applications can use
1700  * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1701  */
1702 void
1703 gtk_container_set_focus_child (GtkContainer *container,
1704                                GtkWidget    *child)
1705 {
1706   g_return_if_fail (GTK_IS_CONTAINER (container));
1707   if (child)
1708     g_return_if_fail (GTK_IS_WIDGET (child));
1709
1710   g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1711 }
1712
1713 /**
1714  * gtk_container_get_focus_child:
1715  * @container: a #GtkContainer
1716  *
1717  * Returns the current focus child widget inside @container. This is not the
1718  * currently focused widget. That can be obtained by calling
1719  * gtk_window_get_focus().
1720  *
1721  * Returns: The child widget which will recieve the focus inside @container when
1722  *          the @conatiner is focussed, or %NULL if none is set.
1723  *
1724  * Since: 2.14
1725  **/
1726 GtkWidget *
1727 gtk_container_get_focus_child (GtkContainer *container)
1728 {
1729   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1730
1731   return container->priv->focus_child;
1732 }
1733
1734 /**
1735  * gtk_container_get_children:
1736  * @container: a #GtkContainer
1737  * 
1738  * Returns the container's non-internal children. See
1739  * gtk_container_forall() for details on what constitutes an "internal" child.
1740  *
1741  * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1742  **/
1743 GList*
1744 gtk_container_get_children (GtkContainer *container)
1745 {
1746   GList *children = NULL;
1747
1748   gtk_container_foreach (container,
1749                          gtk_container_children_callback,
1750                          &children);
1751
1752   return g_list_reverse (children);
1753 }
1754
1755 static void
1756 gtk_container_child_position_callback (GtkWidget *widget,
1757                                        gpointer   client_data)
1758 {
1759   struct {
1760     GtkWidget *child;
1761     guint i;
1762     guint index;
1763   } *data = client_data;
1764
1765   data->i++;
1766   if (data->child == widget)
1767     data->index = data->i;
1768 }
1769
1770 static gchar*
1771 gtk_container_child_default_composite_name (GtkContainer *container,
1772                                             GtkWidget    *child)
1773 {
1774   struct {
1775     GtkWidget *child;
1776     guint i;
1777     guint index;
1778   } data;
1779   gchar *name;
1780
1781   /* fallback implementation */
1782   data.child = child;
1783   data.i = 0;
1784   data.index = 0;
1785   gtk_container_forall (container,
1786                         gtk_container_child_position_callback,
1787                         &data);
1788   
1789   name = g_strdup_printf ("%s-%u",
1790                           g_type_name (G_TYPE_FROM_INSTANCE (child)),
1791                           data.index);
1792
1793   return name;
1794 }
1795
1796 gchar*
1797 _gtk_container_child_composite_name (GtkContainer *container,
1798                                     GtkWidget    *child)
1799 {
1800   gboolean composite_child;
1801
1802   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1803   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1804   g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
1805
1806   g_object_get (child, "composite-child", &composite_child, NULL);
1807   if (composite_child)
1808     {
1809       static GQuark quark_composite_name = 0;
1810       gchar *name;
1811
1812       if (!quark_composite_name)
1813         quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1814
1815       name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1816       if (!name)
1817         {
1818           GtkContainerClass *class;
1819
1820           class = GTK_CONTAINER_GET_CLASS (container);
1821           if (class->composite_name)
1822             name = class->composite_name (container, child);
1823         }
1824       else
1825         name = g_strdup (name);
1826
1827       return name;
1828     }
1829   
1830   return NULL;
1831 }
1832
1833 static void
1834 gtk_container_real_set_focus_child (GtkContainer     *container,
1835                                     GtkWidget        *child)
1836 {
1837   GtkContainerPrivate *priv;
1838
1839   g_return_if_fail (GTK_IS_CONTAINER (container));
1840   g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1841
1842   priv = container->priv;
1843
1844   if (child != priv->focus_child)
1845     {
1846       if (priv->focus_child)
1847         g_object_unref (priv->focus_child);
1848       priv->focus_child = child;
1849       if (priv->focus_child)
1850         g_object_ref (priv->focus_child);
1851     }
1852
1853
1854   /* check for h/v adjustments
1855    */
1856   if (priv->focus_child)
1857     {
1858       GtkAdjustment *hadj;
1859       GtkAdjustment *vadj;
1860       GtkAllocation allocation;
1861       GtkWidget *focus_child;
1862       gint x, y;
1863
1864       hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);   
1865       vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1866       if (hadj || vadj) 
1867         {
1868
1869           focus_child = priv->focus_child;
1870           while (gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
1871             {
1872               focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
1873             }
1874           
1875           gtk_widget_translate_coordinates (focus_child, priv->focus_child,
1876                                             0, 0, &x, &y);
1877
1878           gtk_widget_get_allocation (priv->focus_child, &allocation);
1879           x += allocation.x;
1880           y += allocation.y;
1881
1882           gtk_widget_get_allocation (focus_child, &allocation);
1883
1884           if (vadj)
1885             gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
1886
1887           if (hadj)
1888             gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
1889         }
1890     }
1891 }
1892
1893 static GList*
1894 get_focus_chain (GtkContainer *container)
1895 {
1896   return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1897 }
1898
1899 /* same as gtk_container_get_children, except it includes internals
1900  */
1901 static GList *
1902 gtk_container_get_all_children (GtkContainer *container)
1903 {
1904   GList *children = NULL;
1905
1906   gtk_container_forall (container,
1907                          gtk_container_children_callback,
1908                          &children);
1909
1910   return children;
1911 }
1912
1913 static gboolean
1914 gtk_container_focus (GtkWidget        *widget,
1915                      GtkDirectionType  direction)
1916 {
1917   GList *children;
1918   GList *sorted_children;
1919   gint return_val;
1920   GtkContainer *container;
1921   GtkContainerPrivate *priv;
1922
1923   g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1924
1925   container = GTK_CONTAINER (widget);
1926   priv = container->priv;
1927
1928   return_val = FALSE;
1929
1930   if (gtk_widget_get_can_focus (widget))
1931     {
1932       if (!gtk_widget_has_focus (widget))
1933         {
1934           gtk_widget_grab_focus (widget);
1935           return_val = TRUE;
1936         }
1937     }
1938   else
1939     {
1940       /* Get a list of the containers children, allowing focus
1941        * chain to override.
1942        */
1943       if (priv->has_focus_chain)
1944         children = g_list_copy (get_focus_chain (container));
1945       else
1946         children = gtk_container_get_all_children (container);
1947
1948       if (priv->has_focus_chain &&
1949           (direction == GTK_DIR_TAB_FORWARD ||
1950            direction == GTK_DIR_TAB_BACKWARD))
1951         {
1952           sorted_children = g_list_copy (children);
1953           
1954           if (direction == GTK_DIR_TAB_BACKWARD)
1955             sorted_children = g_list_reverse (sorted_children);
1956         }
1957       else
1958         sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1959       
1960       return_val = gtk_container_focus_move (container, sorted_children, direction);
1961
1962       g_list_free (sorted_children);
1963       g_list_free (children);
1964     }
1965
1966   return return_val;
1967 }
1968
1969 static gint
1970 tab_compare (gconstpointer a,
1971              gconstpointer b,
1972              gpointer      data)
1973 {
1974   GtkAllocation child1_allocation, child2_allocation;
1975   const GtkWidget *child1 = a;
1976   const GtkWidget *child2 = b;
1977   GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1978
1979   gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
1980   gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
1981
1982   gint y1 = child1_allocation.y + child1_allocation.height / 2;
1983   gint y2 = child2_allocation.y + child2_allocation.height / 2;
1984
1985   if (y1 == y2)
1986     {
1987       gint x1 = child1_allocation.x + child1_allocation.width / 2;
1988       gint x2 = child2_allocation.x + child2_allocation.width / 2;
1989
1990       if (text_direction == GTK_TEXT_DIR_RTL) 
1991         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1992       else
1993         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1994     }
1995   else
1996     return (y1 < y2) ? -1 : 1;
1997 }
1998
1999 static GList *
2000 gtk_container_focus_sort_tab (GtkContainer     *container,
2001                               GList            *children,
2002                               GtkDirectionType  direction,
2003                               GtkWidget        *old_focus)
2004 {
2005   GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2006   children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2007
2008   /* if we are going backwards then reverse the order
2009    *  of the children.
2010    */
2011   if (direction == GTK_DIR_TAB_BACKWARD)
2012     children = g_list_reverse (children);
2013
2014   return children;
2015 }
2016
2017 /* Get coordinates of @widget's allocation with respect to
2018  * allocation of @container.
2019  */
2020 static gboolean
2021 get_allocation_coords (GtkContainer  *container,
2022                        GtkWidget     *widget,
2023                        GdkRectangle  *allocation)
2024 {
2025   gtk_widget_set_allocation (widget, allocation);
2026
2027   return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2028                                            0, 0, &allocation->x, &allocation->y);
2029 }
2030
2031 /* Look for a child in @children that is intermediate between
2032  * the focus widget and container. This widget, if it exists,
2033  * acts as the starting widget for focus navigation.
2034  */
2035 static GtkWidget *
2036 find_old_focus (GtkContainer *container,
2037                 GList        *children)
2038 {
2039   GList *tmp_list = children;
2040   while (tmp_list)
2041     {
2042       GtkWidget *child = tmp_list->data;
2043       GtkWidget *widget = child;
2044
2045       while (widget && widget != (GtkWidget *)container)
2046         {
2047           GtkWidget *parent;
2048
2049           parent = gtk_widget_get_parent (widget);
2050
2051           if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2052             goto next;
2053
2054           widget = parent;
2055         }
2056
2057       return child;
2058
2059     next:
2060       tmp_list = tmp_list->next;
2061     }
2062
2063   return NULL;
2064 }
2065
2066 static gboolean
2067 old_focus_coords (GtkContainer *container,
2068                   GdkRectangle *old_focus_rect)
2069 {
2070   GtkWidget *widget = GTK_WIDGET (container);
2071   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2072   GtkWidget *old_focus;
2073
2074   if (GTK_IS_WINDOW (toplevel))
2075     {
2076       old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2077       if (old_focus)
2078         return get_allocation_coords (container, old_focus, old_focus_rect);
2079     }
2080
2081   return FALSE;
2082 }
2083
2084 typedef struct _CompareInfo CompareInfo;
2085
2086 struct _CompareInfo
2087 {
2088   GtkContainer *container;
2089   gint x;
2090   gint y;
2091   gboolean reverse;
2092 };
2093
2094 static gint
2095 up_down_compare (gconstpointer a,
2096                  gconstpointer b,
2097                  gpointer      data)
2098 {
2099   GdkRectangle allocation1;
2100   GdkRectangle allocation2;
2101   CompareInfo *compare = data;
2102   gint y1, y2;
2103
2104   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2105   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2106
2107   y1 = allocation1.y + allocation1.height / 2;
2108   y2 = allocation2.y + allocation2.height / 2;
2109
2110   if (y1 == y2)
2111     {
2112       gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2113       gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2114
2115       if (compare->reverse)
2116         return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2117       else
2118         return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2119     }
2120   else
2121     return (y1 < y2) ? -1 : 1;
2122 }
2123
2124 static GList *
2125 gtk_container_focus_sort_up_down (GtkContainer     *container,
2126                                   GList            *children,
2127                                   GtkDirectionType  direction,
2128                                   GtkWidget        *old_focus)
2129 {
2130   CompareInfo compare;
2131   GList *tmp_list;
2132   GdkRectangle old_allocation;
2133
2134   compare.container = container;
2135   compare.reverse = (direction == GTK_DIR_UP);
2136
2137   if (!old_focus)
2138       old_focus = find_old_focus (container, children);
2139   
2140   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2141     {
2142       gint compare_x1;
2143       gint compare_x2;
2144       gint compare_y;
2145
2146       /* Delete widgets from list that don't match minimum criteria */
2147
2148       compare_x1 = old_allocation.x;
2149       compare_x2 = old_allocation.x + old_allocation.width;
2150
2151       if (direction == GTK_DIR_UP)
2152         compare_y = old_allocation.y;
2153       else
2154         compare_y = old_allocation.y + old_allocation.height;
2155       
2156       tmp_list = children;
2157       while (tmp_list)
2158         {
2159           GtkWidget *child = tmp_list->data;
2160           GList *next = tmp_list->next;
2161           gint child_x1, child_x2;
2162           GdkRectangle child_allocation;
2163           
2164           if (child != old_focus)
2165             {
2166               if (get_allocation_coords (container, child, &child_allocation))
2167                 {
2168                   child_x1 = child_allocation.x;
2169                   child_x2 = child_allocation.x + child_allocation.width;
2170                   
2171                   if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2172                       (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2173                       (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2174                     {
2175                       children = g_list_delete_link (children, tmp_list);
2176                     }
2177                 }
2178               else
2179                 children = g_list_delete_link (children, tmp_list);
2180             }
2181           
2182           tmp_list = next;
2183         }
2184
2185       compare.x = (compare_x1 + compare_x2) / 2;
2186       compare.y = old_allocation.y + old_allocation.height / 2;
2187     }
2188   else
2189     {
2190       /* No old focus widget, need to figure out starting x,y some other way
2191        */
2192       GtkAllocation allocation;
2193       GtkWidget *widget = GTK_WIDGET (container);
2194       GdkRectangle old_focus_rect;
2195
2196       gtk_widget_get_allocation (widget, &allocation);
2197
2198       if (old_focus_coords (container, &old_focus_rect))
2199         {
2200           compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2201         }
2202       else
2203         {
2204           if (!gtk_widget_get_has_window (widget))
2205             compare.x = allocation.x + allocation.width / 2;
2206           else
2207             compare.x = allocation.width / 2;
2208         }
2209       
2210       if (!gtk_widget_get_has_window (widget))
2211         compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2212       else
2213         compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2214     }
2215
2216   children = g_list_sort_with_data (children, up_down_compare, &compare);
2217
2218   if (compare.reverse)
2219     children = g_list_reverse (children);
2220
2221   return children;
2222 }
2223
2224 static gint
2225 left_right_compare (gconstpointer a,
2226                     gconstpointer b,
2227                     gpointer      data)
2228 {
2229   GdkRectangle allocation1;
2230   GdkRectangle allocation2;
2231   CompareInfo *compare = data;
2232   gint x1, x2;
2233
2234   get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2235   get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2236
2237   x1 = allocation1.x + allocation1.width / 2;
2238   x2 = allocation2.x + allocation2.width / 2;
2239
2240   if (x1 == x2)
2241     {
2242       gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2243       gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2244
2245       if (compare->reverse)
2246         return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2247       else
2248         return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2249     }
2250   else
2251     return (x1 < x2) ? -1 : 1;
2252 }
2253
2254 static GList *
2255 gtk_container_focus_sort_left_right (GtkContainer     *container,
2256                                      GList            *children,
2257                                      GtkDirectionType  direction,
2258                                      GtkWidget        *old_focus)
2259 {
2260   CompareInfo compare;
2261   GList *tmp_list;
2262   GdkRectangle old_allocation;
2263
2264   compare.container = container;
2265   compare.reverse = (direction == GTK_DIR_LEFT);
2266
2267   if (!old_focus)
2268     old_focus = find_old_focus (container, children);
2269   
2270   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2271     {
2272       gint compare_y1;
2273       gint compare_y2;
2274       gint compare_x;
2275       
2276       /* Delete widgets from list that don't match minimum criteria */
2277
2278       compare_y1 = old_allocation.y;
2279       compare_y2 = old_allocation.y + old_allocation.height;
2280
2281       if (direction == GTK_DIR_LEFT)
2282         compare_x = old_allocation.x;
2283       else
2284         compare_x = old_allocation.x + old_allocation.width;
2285       
2286       tmp_list = children;
2287       while (tmp_list)
2288         {
2289           GtkWidget *child = tmp_list->data;
2290           GList *next = tmp_list->next;
2291           gint child_y1, child_y2;
2292           GdkRectangle child_allocation;
2293           
2294           if (child != old_focus)
2295             {
2296               if (get_allocation_coords (container, child, &child_allocation))
2297                 {
2298                   child_y1 = child_allocation.y;
2299                   child_y2 = child_allocation.y + child_allocation.height;
2300                   
2301                   if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2302                       (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2303                       (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2304                     {
2305                       children = g_list_delete_link (children, tmp_list);
2306                     }
2307                 }
2308               else
2309                 children = g_list_delete_link (children, tmp_list);
2310             }
2311           
2312           tmp_list = next;
2313         }
2314
2315       compare.y = (compare_y1 + compare_y2) / 2;
2316       compare.x = old_allocation.x + old_allocation.width / 2;
2317     }
2318   else
2319     {
2320       /* No old focus widget, need to figure out starting x,y some other way
2321        */
2322       GtkAllocation allocation;
2323       GtkWidget *widget = GTK_WIDGET (container);
2324       GdkRectangle old_focus_rect;
2325
2326       gtk_widget_get_allocation (widget, &allocation);
2327
2328       if (old_focus_coords (container, &old_focus_rect))
2329         {
2330           compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2331         }
2332       else
2333         {
2334           if (!gtk_widget_get_has_window (widget))
2335             compare.y = allocation.y + allocation.height / 2;
2336           else
2337             compare.y = allocation.height / 2;
2338         }
2339       
2340       if (!gtk_widget_get_has_window (widget))
2341         compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2342       else
2343         compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2344     }
2345
2346   children = g_list_sort_with_data (children, left_right_compare, &compare);
2347
2348   if (compare.reverse)
2349     children = g_list_reverse (children);
2350
2351   return children;
2352 }
2353
2354 /**
2355  * gtk_container_focus_sort:
2356  * @container: a #GtkContainer
2357  * @children:  a list of descendents of @container (they don't
2358  *             have to be direct children)
2359  * @direction: focus direction
2360  * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2361  *             to determine this automatically.
2362  *             (Note, this argument isn't used for GTK_DIR_TAB_*,
2363  *              which is the only @direction we use currently,
2364  *              so perhaps this argument should be removed)
2365  * 
2366  * Sorts @children in the correct order for focusing with
2367  * direction type @direction.
2368  * 
2369  * Return value: a copy of @children, sorted in correct focusing order,
2370  *   with children that aren't suitable for focusing in this direction
2371  *   removed.
2372  **/
2373 GList *
2374 _gtk_container_focus_sort (GtkContainer     *container,
2375                            GList            *children,
2376                            GtkDirectionType  direction,
2377                            GtkWidget        *old_focus)
2378 {
2379   GList *visible_children = NULL;
2380
2381   while (children)
2382     {
2383       if (gtk_widget_get_realized (children->data))
2384         visible_children = g_list_prepend (visible_children, children->data);
2385       children = children->next;
2386     }
2387   
2388   switch (direction)
2389     {
2390     case GTK_DIR_TAB_FORWARD:
2391     case GTK_DIR_TAB_BACKWARD:
2392       return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2393     case GTK_DIR_UP:
2394     case GTK_DIR_DOWN:
2395       return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2396     case GTK_DIR_LEFT:
2397     case GTK_DIR_RIGHT:
2398       return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2399     }
2400
2401   g_assert_not_reached ();
2402
2403   return NULL;
2404 }
2405
2406 static gboolean
2407 gtk_container_focus_move (GtkContainer     *container,
2408                           GList            *children,
2409                           GtkDirectionType  direction)
2410 {
2411   GtkContainerPrivate *priv = container->priv;
2412   GtkWidget *focus_child;
2413   GtkWidget *child;
2414
2415   focus_child = priv->focus_child;
2416
2417   while (children)
2418     {
2419       child = children->data;
2420       children = children->next;
2421
2422       if (!child)
2423         continue;
2424       
2425       if (focus_child)
2426         {
2427           if (focus_child == child)
2428             {
2429               focus_child = NULL;
2430
2431                 if (gtk_widget_child_focus (child, direction))
2432                   return TRUE;
2433             }
2434         }
2435       else if (gtk_widget_is_drawable (child) &&
2436                gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2437         {
2438           if (gtk_widget_child_focus (child, direction))
2439             return TRUE;
2440         }
2441     }
2442
2443   return FALSE;
2444 }
2445
2446
2447 static void
2448 gtk_container_children_callback (GtkWidget *widget,
2449                                  gpointer   client_data)
2450 {
2451   GList **children;
2452
2453   children = (GList**) client_data;
2454   *children = g_list_prepend (*children, widget);
2455 }
2456
2457 static void
2458 chain_widget_destroyed (GtkWidget *widget,
2459                         gpointer   user_data)
2460 {
2461   GtkContainer *container;
2462   GList *chain;
2463   
2464   container = GTK_CONTAINER (user_data);
2465
2466   chain = g_object_get_data (G_OBJECT (container),
2467                              "gtk-container-focus-chain");
2468
2469   chain = g_list_remove (chain, widget);
2470
2471   g_signal_handlers_disconnect_by_func (widget,
2472                                         chain_widget_destroyed,
2473                                         user_data);
2474   
2475   g_object_set_data (G_OBJECT (container),
2476                      I_("gtk-container-focus-chain"),
2477                      chain);  
2478 }
2479
2480 /**
2481  * gtk_container_set_focus_chain:
2482  * @container: a #GtkContainer
2483  * @focusable_widgets: (transfer none) (element-type GtkWidget):
2484  *     the new focus chain
2485  *
2486  * Sets a focus chain, overriding the one computed automatically by GTK+.
2487  * 
2488  * In principle each widget in the chain should be a descendant of the 
2489  * container, but this is not enforced by this method, since it's allowed 
2490  * to set the focus chain before you pack the widgets, or have a widget 
2491  * in the chain that isn't always packed. The necessary checks are done 
2492  * when the focus chain is actually traversed.
2493  **/
2494 void
2495 gtk_container_set_focus_chain (GtkContainer *container,
2496                                GList        *focusable_widgets)
2497 {
2498   GList *chain;
2499   GList *tmp_list;
2500   GtkContainerPrivate *priv;
2501   
2502   g_return_if_fail (GTK_IS_CONTAINER (container));
2503
2504   priv = container->priv;
2505   
2506   if (priv->has_focus_chain)
2507     gtk_container_unset_focus_chain (container);
2508
2509   priv->has_focus_chain = TRUE;
2510   
2511   chain = NULL;
2512   tmp_list = focusable_widgets;
2513   while (tmp_list != NULL)
2514     {
2515       g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2516       
2517       /* In principle each widget in the chain should be a descendant
2518        * of the container, but we don't want to check that here, it's
2519        * expensive and also it's allowed to set the focus chain before
2520        * you pack the widgets, or have a widget in the chain that isn't
2521        * always packed. So we check for ancestor during actual traversal.
2522        */
2523
2524       chain = g_list_prepend (chain, tmp_list->data);
2525
2526       g_signal_connect (tmp_list->data,
2527                         "destroy",
2528                         G_CALLBACK (chain_widget_destroyed),
2529                         container);
2530       
2531       tmp_list = g_list_next (tmp_list);
2532     }
2533
2534   chain = g_list_reverse (chain);
2535   
2536   g_object_set_data (G_OBJECT (container),
2537                      I_("gtk-container-focus-chain"),
2538                      chain);
2539 }
2540
2541 /**
2542  * gtk_container_get_focus_chain:
2543  * @container:         a #GtkContainer
2544  * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2545  *                     to store the focus chain of the
2546  *                     container, or %NULL. You should free this list
2547  *                     using g_list_free() when you are done with it, however
2548  *                     no additional reference count is added to the
2549  *                     individual widgets in the focus chain.
2550  * 
2551  * Retrieves the focus chain of the container, if one has been
2552  * set explicitly. If no focus chain has been explicitly
2553  * set, GTK+ computes the focus chain based on the positions
2554  * of the children. In that case, GTK+ stores %NULL in
2555  * @focusable_widgets and returns %FALSE.
2556  *
2557  * Return value: %TRUE if the focus chain of the container 
2558  * has been set explicitly.
2559  **/
2560 gboolean
2561 gtk_container_get_focus_chain (GtkContainer *container,
2562                                GList       **focus_chain)
2563 {
2564   GtkContainerPrivate *priv;
2565
2566   g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2567
2568   priv = container->priv;
2569
2570   if (focus_chain)
2571     {
2572       if (priv->has_focus_chain)
2573         *focus_chain = g_list_copy (get_focus_chain (container));
2574       else
2575         *focus_chain = NULL;
2576     }
2577
2578   return priv->has_focus_chain;
2579 }
2580
2581 /**
2582  * gtk_container_unset_focus_chain:
2583  * @container: a #GtkContainer
2584  * 
2585  * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2586  **/
2587 void
2588 gtk_container_unset_focus_chain (GtkContainer  *container)
2589 {
2590   GtkContainerPrivate *priv;
2591
2592   g_return_if_fail (GTK_IS_CONTAINER (container));
2593
2594   priv = container->priv;
2595
2596   if (priv->has_focus_chain)
2597     {
2598       GList *chain;
2599       GList *tmp_list;
2600       
2601       chain = get_focus_chain (container);
2602       
2603       priv->has_focus_chain = FALSE;
2604       
2605       g_object_set_data (G_OBJECT (container), 
2606                          I_("gtk-container-focus-chain"),
2607                          NULL);
2608
2609       tmp_list = chain;
2610       while (tmp_list != NULL)
2611         {
2612           g_signal_handlers_disconnect_by_func (tmp_list->data,
2613                                                 chain_widget_destroyed,
2614                                                 container);
2615           
2616           tmp_list = g_list_next (tmp_list);
2617         }
2618
2619       g_list_free (chain);
2620     }
2621 }
2622
2623 /**
2624  * gtk_container_set_focus_vadjustment:
2625  * @container: a #GtkContainer
2626  * @adjustment: an adjustment which should be adjusted when the focus 
2627  *   is moved among the descendents of @container
2628  * 
2629  * Hooks up an adjustment to focus handling in a container, so when a 
2630  * child of the container is focused, the adjustment is scrolled to 
2631  * show that widget. This function sets the vertical alignment. See 
2632  * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining 
2633  * the adjustment and gtk_container_set_focus_hadjustment() for setting
2634  * the horizontal adjustment.
2635  *
2636  * The adjustments have to be in pixel units and in the same coordinate 
2637  * system as the allocation for immediate children of the container. 
2638  */
2639 void
2640 gtk_container_set_focus_vadjustment (GtkContainer  *container,
2641                                      GtkAdjustment *adjustment)
2642 {
2643   g_return_if_fail (GTK_IS_CONTAINER (container));
2644   if (adjustment)
2645     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2646
2647   if (adjustment)
2648     g_object_ref (adjustment);
2649
2650   g_object_set_qdata_full (G_OBJECT (container),
2651                            vadjustment_key_id,
2652                            adjustment,
2653                            g_object_unref);
2654 }
2655
2656 /**
2657  * gtk_container_get_focus_vadjustment:
2658  * @container: a #GtkContainer
2659  *
2660  * Retrieves the vertical focus adjustment for the container. See
2661  * gtk_container_set_focus_vadjustment().
2662  *
2663  * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2664  *   none has been set.
2665  **/
2666 GtkAdjustment *
2667 gtk_container_get_focus_vadjustment (GtkContainer *container)
2668 {
2669   GtkAdjustment *vadjustment;
2670     
2671   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2672
2673   vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2674
2675   return vadjustment;
2676 }
2677
2678 /**
2679  * gtk_container_set_focus_hadjustment:
2680  * @container: a #GtkContainer
2681  * @adjustment: an adjustment which should be adjusted when the focus is 
2682  *   moved among the descendents of @container
2683  * 
2684  * Hooks up an adjustment to focus handling in a container, so when a child 
2685  * of the container is focused, the adjustment is scrolled to show that 
2686  * widget. This function sets the horizontal alignment. 
2687  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
2688  * the adjustment and gtk_container_set_focus_vadjustment() for setting
2689  * the vertical adjustment.
2690  *
2691  * The adjustments have to be in pixel units and in the same coordinate 
2692  * system as the allocation for immediate children of the container. 
2693  */
2694 void
2695 gtk_container_set_focus_hadjustment (GtkContainer  *container,
2696                                      GtkAdjustment *adjustment)
2697 {
2698   g_return_if_fail (GTK_IS_CONTAINER (container));
2699   if (adjustment)
2700     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2701
2702   if (adjustment)
2703     g_object_ref (adjustment);
2704
2705   g_object_set_qdata_full (G_OBJECT (container),
2706                            hadjustment_key_id,
2707                            adjustment,
2708                            g_object_unref);
2709 }
2710
2711 /**
2712  * gtk_container_get_focus_hadjustment:
2713  * @container: a #GtkContainer
2714  *
2715  * Retrieves the horizontal focus adjustment for the container. See
2716  * gtk_container_set_focus_hadjustment ().
2717  *
2718  * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
2719  *   none has been set.
2720  **/
2721 GtkAdjustment *
2722 gtk_container_get_focus_hadjustment (GtkContainer *container)
2723 {
2724   GtkAdjustment *hadjustment;
2725
2726   g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2727
2728   hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2729
2730   return hadjustment;
2731 }
2732
2733
2734 static void
2735 gtk_container_show_all (GtkWidget *widget)
2736 {
2737   g_return_if_fail (GTK_IS_CONTAINER (widget));
2738
2739   gtk_container_foreach (GTK_CONTAINER (widget),
2740                          (GtkCallback) gtk_widget_show_all,
2741                          NULL);
2742   gtk_widget_show (widget);
2743 }
2744
2745 static void
2746 gtk_container_hide_all (GtkWidget *widget)
2747 {
2748   g_return_if_fail (GTK_IS_CONTAINER (widget));
2749
2750   gtk_widget_hide (widget);
2751   gtk_container_foreach (GTK_CONTAINER (widget),
2752                          (GtkCallback) gtk_widget_hide_all,
2753                          NULL);
2754 }
2755
2756
2757 static void
2758 gtk_container_draw_child (GtkWidget *child,
2759                           gpointer   client_data)
2760 {
2761   struct {
2762     GtkWidget *container;
2763     cairo_t *cr;
2764   } *data = client_data;
2765   
2766   gtk_container_propagate_draw (GTK_CONTAINER (data->container),
2767                                 child,
2768                                 data->cr);
2769 }
2770
2771 static gint 
2772 gtk_container_draw (GtkWidget *widget,
2773                     cairo_t   *cr)
2774 {
2775   struct {
2776     GtkWidget *container;
2777     cairo_t *cr;
2778   } data;
2779
2780   data.container = widget;
2781   data.cr = cr;
2782   
2783   gtk_container_forall (GTK_CONTAINER (widget),
2784                         gtk_container_draw_child,
2785                         &data);
2786   
2787   return FALSE;
2788 }
2789
2790 static void
2791 gtk_container_map_child (GtkWidget *child,
2792                          gpointer   client_data)
2793 {
2794   if (gtk_widget_get_visible (child) &&
2795       gtk_widget_get_child_visible (child) &&
2796       !gtk_widget_get_mapped (child))
2797     gtk_widget_map (child);
2798 }
2799
2800 static void
2801 gtk_container_map (GtkWidget *widget)
2802 {
2803   gtk_widget_set_mapped (widget, TRUE);
2804
2805   gtk_container_forall (GTK_CONTAINER (widget),
2806                         gtk_container_map_child,
2807                         NULL);
2808
2809   if (gtk_widget_get_has_window (widget))
2810     gdk_window_show (gtk_widget_get_window (widget));
2811 }
2812
2813 static void
2814 gtk_container_unmap (GtkWidget *widget)
2815 {
2816   gtk_widget_set_mapped (widget, FALSE);
2817
2818   if (gtk_widget_get_has_window (widget))
2819     gdk_window_hide (gtk_widget_get_window (widget));
2820   else
2821     gtk_container_forall (GTK_CONTAINER (widget),
2822                           (GtkCallback)gtk_widget_unmap,
2823                           NULL);
2824 }
2825
2826 /**
2827  * gtk_container_propagate_draw:
2828  * @container: a #GtkContainer
2829  * @child: a child of @container
2830  * @cr: Cairo context as passed to the container. If you want to use @cr
2831  *   in container's draw function, consider using cairo_save() and 
2832  *   cairo_restore() before calling this function.
2833  *
2834  * When a container receives a call to the draw function, it must send
2835  * synthetic #GtkWidget::draw calls to all children that don't have their
2836  * own #GdkWindows. This function provides a convenient way of doing this.
2837  * A container, when it receives a call to its #GtkWidget::draw function,
2838  * calls gtk_container_propagate_draw() once for each child, passing in
2839  * the @cr the container received.
2840  *
2841  * gtk_container_propagate_draw() takes care of translating the origin of @cr,
2842  * and deciding whether the draw needs to be sent to the child. It is a
2843  * convenient and optimized way of getting the same effect as calling
2844  * gtk_widget_draw() on the child directly.
2845  * 
2846  * In most cases, a container can simply either inherit the
2847  * #GtkWidget::draw implementation from #GtkContainer, or do some drawing 
2848  * and then chain to the ::draw implementation from #GtkContainer.
2849  **/
2850 void
2851 gtk_container_propagate_draw (GtkContainer   *container,
2852                               GtkWidget      *child,
2853                               cairo_t        *cr)
2854 {
2855   GdkEventExpose *event;
2856   GtkAllocation allocation;
2857   GdkWindow *window, *w;
2858   int x, y;
2859
2860   g_return_if_fail (GTK_IS_CONTAINER (container));
2861   g_return_if_fail (GTK_IS_WIDGET (child));
2862   g_return_if_fail (cr != NULL);
2863
2864   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
2865
2866   event = _gtk_cairo_get_event (cr);
2867   if (event)
2868     {
2869       if (gtk_widget_get_has_window (child) ||
2870           gtk_widget_get_window (child) != event->window)
2871         return;
2872     }
2873
2874   cairo_save (cr);
2875
2876   /* translate coordinates. Ugly business, that. */
2877   if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
2878     {
2879       gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
2880       x = -allocation.x;
2881       y = -allocation.y;
2882     }
2883   else
2884     {
2885       x = 0;
2886       y = 0;
2887     }
2888
2889   window = gtk_widget_get_window (GTK_WIDGET (container));
2890   
2891   for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
2892     {
2893       int wx, wy;
2894       gdk_window_get_position (w, &wx, &wy);
2895       x += wx;
2896       y += wy;
2897     }
2898
2899   if (w == NULL)
2900     {
2901       x = 0;
2902       y = 0;
2903     }
2904
2905   if (!gtk_widget_get_has_window (child))
2906     {
2907       gtk_widget_get_allocation (child, &allocation);
2908       x += allocation.x;
2909       y += allocation.y;
2910     }
2911
2912   cairo_translate (cr, x, y);
2913
2914   _gtk_widget_draw_internal (child, cr, TRUE);
2915
2916   cairo_restore (cr);
2917 }
2918
2919 gboolean
2920 _gtk_container_get_need_resize (GtkContainer *container)
2921 {
2922   return container->priv->need_resize;
2923 }
2924
2925 void
2926 _gtk_container_set_need_resize (GtkContainer *container,
2927                                 gboolean      need_resize)
2928 {
2929   container->priv->need_resize = need_resize;
2930 }
2931
2932 gboolean
2933 _gtk_container_get_reallocate_redraws (GtkContainer *container)
2934 {
2935   return container->priv->reallocate_redraws;
2936 }