1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
31 #include "gtkcontainer.h"
32 #include "gtkprivate.h"
34 #include "gtkmarshalers.h"
35 #include "gtkwindow.h"
37 #include <gobject/gobjectnotifyqueue.c>
38 #include <gobject/gvaluecollector.h>
56 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
57 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
60 /* --- prototypes --- */
61 static void gtk_container_base_class_init (GtkContainerClass *klass);
62 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
63 static void gtk_container_class_init (GtkContainerClass *klass);
64 static void gtk_container_init (GtkContainer *container);
65 static void gtk_container_destroy (GtkObject *object);
66 static void gtk_container_set_property (GObject *object,
70 static void gtk_container_get_property (GObject *object,
74 static void gtk_container_add_unimplemented (GtkContainer *container,
76 static void gtk_container_remove_unimplemented (GtkContainer *container,
78 static void gtk_container_real_check_resize (GtkContainer *container);
79 static gboolean gtk_container_focus (GtkWidget *widget,
80 GtkDirectionType direction);
81 static void gtk_container_real_set_focus_child (GtkContainer *container,
84 static gboolean gtk_container_focus_move (GtkContainer *container,
86 GtkDirectionType direction);
87 static void gtk_container_children_callback (GtkWidget *widget,
88 gpointer client_data);
89 static void gtk_container_show_all (GtkWidget *widget);
90 static void gtk_container_hide_all (GtkWidget *widget);
91 static gint gtk_container_expose (GtkWidget *widget,
92 GdkEventExpose *event);
93 static void gtk_container_map (GtkWidget *widget);
94 static void gtk_container_unmap (GtkWidget *widget);
96 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
100 /* --- variables --- */
101 static const gchar *vadjustment_key = "gtk-vadjustment";
102 static guint vadjustment_key_id = 0;
103 static const gchar *hadjustment_key = "gtk-hadjustment";
104 static guint hadjustment_key_id = 0;
105 static GSList *container_resize_queue = NULL;
106 static guint container_signals[LAST_SIGNAL] = { 0 };
107 static GtkWidgetClass *parent_class = NULL;
108 extern GParamSpecPool *_gtk_widget_child_property_pool;
109 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
112 /* --- functions --- */
114 gtk_container_get_type (void)
116 static GType container_type = 0;
120 static const GTypeInfo container_info =
122 sizeof (GtkContainerClass),
123 (GBaseInitFunc) gtk_container_base_class_init,
124 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
125 (GClassInitFunc) gtk_container_class_init,
126 NULL /* class_finalize */,
127 NULL /* class_data */,
128 sizeof (GtkContainer),
130 (GInstanceInitFunc) gtk_container_init,
131 NULL, /* value_table */
135 g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer",
136 &container_info, G_TYPE_FLAG_ABSTRACT);
139 return container_type;
143 gtk_container_base_class_init (GtkContainerClass *class)
145 /* reset instance specifc class fields that don't get inherited */
146 class->set_child_property = NULL;
147 class->get_child_property = NULL;
151 gtk_container_base_class_finalize (GtkContainerClass *class)
155 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
156 for (node = list; node; node = node->next)
158 GParamSpec *pspec = node->data;
160 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
161 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
162 g_param_spec_unref (pspec);
168 gtk_container_class_init (GtkContainerClass *class)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
171 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
172 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
174 parent_class = g_type_class_peek_parent (class);
176 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
177 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
179 gobject_class->set_property = gtk_container_set_property;
180 gobject_class->get_property = gtk_container_get_property;
182 object_class->destroy = gtk_container_destroy;
184 widget_class->show_all = gtk_container_show_all;
185 widget_class->hide_all = gtk_container_hide_all;
186 widget_class->expose_event = gtk_container_expose;
187 widget_class->map = gtk_container_map;
188 widget_class->unmap = gtk_container_unmap;
189 widget_class->focus = gtk_container_focus;
191 class->add = gtk_container_add_unimplemented;
192 class->remove = gtk_container_remove_unimplemented;
193 class->check_resize = gtk_container_real_check_resize;
194 class->forall = NULL;
195 class->set_focus_child = gtk_container_real_set_focus_child;
196 class->child_type = NULL;
197 class->composite_name = gtk_container_child_default_composite_name;
199 g_object_class_install_property (gobject_class,
201 g_param_spec_enum ("resize_mode",
203 _("Specify how resize events are handled"),
204 GTK_TYPE_RESIZE_MODE,
207 g_object_class_install_property (gobject_class,
209 g_param_spec_uint ("border_width",
211 _("The width of the empty border outside the containers children"),
216 g_object_class_install_property (gobject_class,
218 g_param_spec_object ("child",
220 _("Can be used to add a new child to the container"),
223 container_signals[ADD] =
225 G_OBJECT_CLASS_TYPE (object_class),
227 G_STRUCT_OFFSET (GtkContainerClass, add),
229 _gtk_marshal_VOID__OBJECT,
232 container_signals[REMOVE] =
233 g_signal_new ("remove",
234 G_OBJECT_CLASS_TYPE (object_class),
236 G_STRUCT_OFFSET (GtkContainerClass, remove),
238 _gtk_marshal_VOID__OBJECT,
241 container_signals[CHECK_RESIZE] =
242 g_signal_new ("check_resize",
243 G_OBJECT_CLASS_TYPE (object_class),
245 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
247 _gtk_marshal_VOID__VOID,
249 container_signals[SET_FOCUS_CHILD] =
250 g_signal_new ("set-focus-child",
251 G_OBJECT_CLASS_TYPE (object_class),
253 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
255 _gtk_marshal_VOID__OBJECT,
261 * gtk_container_child_type:
262 * @container: a #GtkContainer.
264 * Returns the type of the children supported by the container.
266 * Note that this may return %G_TYPE_NONE to indicate that no more
267 * children can be added, e.g. for a #GtkPaned which already has two
270 * Return value: a #GType.
273 gtk_container_child_type (GtkContainer *container)
276 GtkContainerClass *class;
278 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
280 class = GTK_CONTAINER_GET_CLASS (container);
281 if (class->child_type)
282 slot = class->child_type (container);
289 /* --- GtkContainer child property mechanism --- */
291 container_get_child_property (GtkContainer *container,
296 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
298 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
302 container_set_child_property (GtkContainer *container,
306 GObjectNotifyQueue *nqueue)
308 GValue tmp_value = { 0, };
309 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
311 /* provide a copy to work from, convert (if necessary) and validate */
312 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
313 if (!g_value_transform (value, &tmp_value))
314 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
316 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
317 G_VALUE_TYPE_NAME (value));
318 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
320 gchar *contents = g_strdup_value_contents (value);
322 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
324 G_VALUE_TYPE_NAME (value),
326 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
331 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
332 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
334 g_value_unset (&tmp_value);
338 * gtk_container_child_get_valist:
339 * @container: a #GtkContainer
340 * @child: a widget which is a child of @container
341 * @first_prop_name: the name of the first property to get
342 * @va_list: a %NULL-terminated list of property names and #GValue*,
343 * starting with @first_prop_name.
345 * Gets the values of one or more child properties for @child and @container.
348 gtk_container_child_get_valist (GtkContainer *container,
350 const gchar *first_property_name,
355 g_return_if_fail (GTK_IS_CONTAINER (container));
356 g_return_if_fail (GTK_IS_WIDGET (child));
357 g_return_if_fail (child->parent == GTK_WIDGET (container));
359 g_object_ref (container);
360 g_object_ref (child);
362 name = first_property_name;
365 GValue value = { 0, };
369 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
371 G_OBJECT_TYPE (container),
375 g_warning ("%s: container class `%s' has no child property named `%s'",
377 G_OBJECT_TYPE_NAME (container),
381 if (!(pspec->flags & G_PARAM_READABLE))
383 g_warning ("%s: child property `%s' of container class `%s' is not readable",
386 G_OBJECT_TYPE_NAME (container));
389 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
390 container_get_child_property (container, child, pspec, &value);
391 G_VALUE_LCOPY (&value, var_args, 0, &error);
394 g_warning ("%s: %s", G_STRLOC, error);
396 g_value_unset (&value);
399 g_value_unset (&value);
400 name = va_arg (var_args, gchar*);
403 g_object_unref (child);
404 g_object_unref (container);
408 * gtk_container_child_get_property:
409 * @container: a #GtkContainer
410 * @child: a widget which is a child of @container
411 * @property_name: the name of the property to get
412 * @value: a location to return the value
414 * Gets the value of a child property for @child and @container.
417 gtk_container_child_get_property (GtkContainer *container,
419 const gchar *property_name,
424 g_return_if_fail (GTK_IS_CONTAINER (container));
425 g_return_if_fail (GTK_IS_WIDGET (child));
426 g_return_if_fail (child->parent == GTK_WIDGET (container));
427 g_return_if_fail (property_name != NULL);
428 g_return_if_fail (G_IS_VALUE (value));
430 g_object_ref (container);
431 g_object_ref (child);
432 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
433 G_OBJECT_TYPE (container), TRUE);
435 g_warning ("%s: container class `%s' has no child property named `%s'",
437 G_OBJECT_TYPE_NAME (container),
439 else if (!(pspec->flags & G_PARAM_READABLE))
440 g_warning ("%s: child property `%s' of container class `%s' is not readable",
443 G_OBJECT_TYPE_NAME (container));
446 GValue *prop_value, tmp_value = { 0, };
448 /* auto-conversion of the callers value type
450 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
452 g_value_reset (value);
455 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
457 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
459 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
460 G_VALUE_TYPE_NAME (value));
461 g_object_unref (child);
462 g_object_unref (container);
467 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
468 prop_value = &tmp_value;
470 container_get_child_property (container, child, pspec, prop_value);
471 if (prop_value != value)
473 g_value_transform (prop_value, value);
474 g_value_unset (&tmp_value);
477 g_object_unref (child);
478 g_object_unref (container);
482 * gtk_container_child_set_valist:
483 * @container: a #GtkContainer
484 * @child: a widget which is a child of @container
485 * @first_prop_name: the name of the first property to set
486 * @va_list: a %NULL-terminated list of property names and values, starting
487 * with @first_prop_name.
489 * Sets one or more child properties for @child and @container.
492 gtk_container_child_set_valist (GtkContainer *container,
494 const gchar *first_property_name,
498 GObjectNotifyQueue *nqueue;
501 g_return_if_fail (GTK_IS_CONTAINER (container));
502 g_return_if_fail (GTK_IS_WIDGET (child));
503 g_return_if_fail (child->parent == GTK_WIDGET (container));
505 g_object_ref (container);
506 g_object_ref (child);
508 object = G_OBJECT (container);
509 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
510 name = first_property_name;
513 GValue value = { 0, };
515 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
517 G_OBJECT_TYPE (container),
521 g_warning ("%s: container class `%s' has no child property named `%s'",
523 G_OBJECT_TYPE_NAME (container),
527 if (!(pspec->flags & G_PARAM_WRITABLE))
529 g_warning ("%s: child property `%s' of container class `%s' is not writable",
532 G_OBJECT_TYPE_NAME (container));
535 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
536 G_VALUE_COLLECT (&value, var_args, 0, &error);
539 g_warning ("%s: %s", G_STRLOC, error);
542 /* we purposely leak the value here, it might not be
543 * in a sane state if an error condition occoured
547 container_set_child_property (container, child, pspec, &value, nqueue);
548 g_value_unset (&value);
549 name = va_arg (var_args, gchar*);
551 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
553 g_object_unref (container);
554 g_object_unref (child);
558 * gtk_container_child_set_property:
559 * @container: a #GtkContainer
560 * @child: a widget which is a child of @container
561 * @property_name: the name of the property to set
562 * @value: the value to set the property to
564 * Sets a child property for @child and @container.
567 gtk_container_child_set_property (GtkContainer *container,
569 const gchar *property_name,
573 GObjectNotifyQueue *nqueue;
576 g_return_if_fail (GTK_IS_CONTAINER (container));
577 g_return_if_fail (GTK_IS_WIDGET (child));
578 g_return_if_fail (child->parent == GTK_WIDGET (container));
579 g_return_if_fail (property_name != NULL);
580 g_return_if_fail (G_IS_VALUE (value));
582 g_object_ref (container);
583 g_object_ref (child);
585 object = G_OBJECT (container);
586 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
587 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
588 G_OBJECT_TYPE (container), TRUE);
590 g_warning ("%s: container class `%s' has no child property named `%s'",
592 G_OBJECT_TYPE_NAME (container),
594 else if (!(pspec->flags & G_PARAM_WRITABLE))
595 g_warning ("%s: child property `%s' of container class `%s' is not writable",
598 G_OBJECT_TYPE_NAME (container));
601 container_set_child_property (container, child, pspec, value, nqueue);
603 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
604 g_object_unref (container);
605 g_object_unref (child);
609 * gtk_container_add_with_properties:
610 * @container: a #GtkContainer
611 * @widget: a widget to be placed inside @container
612 * @first_prop_name: the name of the first child property to set
613 * @Varargs: a %NULL-terminated list of property names and values, starting
614 * with @first_prop_name.
616 * Adds @widget to @container, setting child properties at the same time.
617 * See gtk_container_add() and gtk_container_child_set() for more details.
620 gtk_container_add_with_properties (GtkContainer *container,
622 const gchar *first_prop_name,
625 g_return_if_fail (GTK_IS_CONTAINER (container));
626 g_return_if_fail (GTK_IS_WIDGET (widget));
627 g_return_if_fail (widget->parent == NULL);
629 g_object_ref (container);
630 g_object_ref (widget);
631 gtk_widget_freeze_child_notify (widget);
633 g_signal_emit (container, container_signals[ADD], 0, widget);
638 va_start (var_args, first_prop_name);
639 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
643 gtk_widget_thaw_child_notify (widget);
644 g_object_unref (widget);
645 g_object_unref (container);
649 * gtk_container_child_set:
650 * @container: a #GtkContainer
651 * @child: a widget which is a child of @container
652 * @first_prop_name: the name of the first property to set
653 * @Varargs: a %NULL-terminated list of property names and values, starting
654 * with @first_prop_name.
656 * Sets one or more child properties for @child and @container.
659 gtk_container_child_set (GtkContainer *container,
661 const gchar *first_prop_name,
666 g_return_if_fail (GTK_IS_CONTAINER (container));
667 g_return_if_fail (GTK_IS_WIDGET (child));
668 g_return_if_fail (child->parent == GTK_WIDGET (container));
670 va_start (var_args, first_prop_name);
671 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
676 * gtk_container_child_get:
677 * @container: a #GtkContainer
678 * @child: a widget which is a child of @container
679 * @first_prop_name: the name of the first property to get
680 * @Varargs: a %NULL-terminated list of property names and #GValue*,
681 * starting with @first_prop_name.
683 * Gets the values of one or more child properties for @child and @container.
686 gtk_container_child_get (GtkContainer *container,
688 const gchar *first_prop_name,
693 g_return_if_fail (GTK_IS_CONTAINER (container));
694 g_return_if_fail (GTK_IS_WIDGET (child));
695 g_return_if_fail (child->parent == GTK_WIDGET (container));
697 va_start (var_args, first_prop_name);
698 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
703 * gtk_container_class_install_child_property:
704 * @cclass: a #GtkContainerClass
705 * @property_id: the id for the property
706 * @pspec: the #GParamSpec for the property
708 * Installs a child property on a container class.
711 gtk_container_class_install_child_property (GtkContainerClass *cclass,
715 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
716 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
717 if (pspec->flags & G_PARAM_WRITABLE)
718 g_return_if_fail (cclass->set_child_property != NULL);
719 if (pspec->flags & G_PARAM_READABLE)
720 g_return_if_fail (cclass->get_child_property != NULL);
721 g_return_if_fail (property_id > 0);
722 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
723 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
724 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
726 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
728 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
729 G_OBJECT_CLASS_NAME (cclass),
733 g_param_spec_ref (pspec);
734 g_param_spec_sink (pspec);
735 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
736 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
740 * gtk_container_class_find_child_property:
741 * @cclass: a #GtkContainerClass
742 * @property_name: the name of the child property to find
743 * @returns: the #GParamSpec of the child property or %NULL if @class has no
744 * child property with that name.
746 * Finds a child property of a container class by name.
749 gtk_container_class_find_child_property (GObjectClass *cclass,
750 const gchar *property_name)
752 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
753 g_return_val_if_fail (property_name != NULL, NULL);
755 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
757 G_OBJECT_CLASS_TYPE (cclass),
762 * gtk_container_class_list_child_properties:
763 * @cclass: a #GtkContainerClass
764 * @n_properties: location to return the number of child properties found
765 * @returns: a newly allocated array of #GParamSpec*. The array must be
766 * freed with g_free().
768 * Returns all child properties of a container class.
771 gtk_container_class_list_child_properties (GObjectClass *cclass,
777 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
779 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
780 G_OBJECT_CLASS_TYPE (cclass),
789 gtk_container_add_unimplemented (GtkContainer *container,
792 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
796 gtk_container_remove_unimplemented (GtkContainer *container,
799 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
803 gtk_container_init (GtkContainer *container)
805 container->focus_child = NULL;
806 container->border_width = 0;
807 container->need_resize = FALSE;
808 container->resize_mode = GTK_RESIZE_PARENT;
809 container->reallocate_redraws = FALSE;
813 gtk_container_destroy (GtkObject *object)
815 GtkContainer *container = GTK_CONTAINER (object);
817 if (GTK_CONTAINER_RESIZE_PENDING (container))
818 _gtk_container_dequeue_resize_handler (container);
820 /* do this before walking child widgets, to avoid
821 * removing children from focus chain one by one.
823 if (container->has_focus_chain)
824 gtk_container_unset_focus_chain (container);
826 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
828 if (GTK_OBJECT_CLASS (parent_class)->destroy)
829 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
833 gtk_container_set_property (GObject *object,
838 GtkContainer *container = GTK_CONTAINER (object);
842 case PROP_BORDER_WIDTH:
843 gtk_container_set_border_width (container, g_value_get_uint (value));
845 case PROP_RESIZE_MODE:
846 gtk_container_set_resize_mode (container, g_value_get_enum (value));
849 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
852 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
858 gtk_container_get_property (GObject *object,
863 GtkContainer *container = GTK_CONTAINER (object);
867 case PROP_BORDER_WIDTH:
868 g_value_set_uint (value, container->border_width);
870 case PROP_RESIZE_MODE:
871 g_value_set_enum (value, container->resize_mode);
874 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
880 * gtk_container_set_border_width:
881 * @container: a #GtkContainer
882 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
883 * Valid values are in the range 0-65535 pixels.
885 * Sets the border width of the container.
887 * The border width of a container is the amount of space to leave
888 * around the outside of the container. The only exception to this is
889 * #GtkWindow; because toplevel windows can't leave space outside,
890 * they leave the space inside. The border is added on all sides of
891 * the container. To add space to only one side, one approach is to
892 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
893 * it a size, and place it on the side of the container as a spacer.
896 gtk_container_set_border_width (GtkContainer *container,
899 g_return_if_fail (GTK_IS_CONTAINER (container));
901 if (container->border_width != border_width)
903 container->border_width = border_width;
904 g_object_notify (G_OBJECT (container), "border_width");
906 if (GTK_WIDGET_REALIZED (container))
907 gtk_widget_queue_resize (GTK_WIDGET (container));
912 * gtk_container_get_border_width:
913 * @container: a #GtkContainer
915 * Retrieves the border width of the container. See
916 * gtk_container_set_border_width().
918 * Return value: the current border width
921 gtk_container_get_border_width (GtkContainer *container)
923 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
925 return container->border_width;
930 * @container: a #GtkContainer
931 * @widget: a widget to be placed inside @container
933 * Adds @widget to @container. Typically used for simple containers
934 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
935 * layout containers such as #GtkBox or #GtkTable, this function will
936 * pick default packing parameters that may not be correct. So
937 * consider functions such as gtk_box_pack_start() and
938 * gtk_table_attach() as an alternative to gtk_container_add() in
939 * those cases. A widget may be added to only one container at a time;
940 * you can't place the same widget inside two different containers.
943 gtk_container_add (GtkContainer *container,
946 g_return_if_fail (GTK_IS_CONTAINER (container));
947 g_return_if_fail (GTK_IS_WIDGET (widget));
949 if (widget->parent != NULL)
951 g_warning ("Attempting to add a widget with type %s to a container of "
952 "type %s, but the widget is already inside a container of type %s, "
953 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
954 g_type_name (G_OBJECT_TYPE (widget)),
955 g_type_name (G_OBJECT_TYPE (container)),
956 g_type_name (G_OBJECT_TYPE (widget->parent)));
960 g_signal_emit (container, container_signals[ADD], 0, widget);
964 * gtk_container_remove:
965 * @container: a #GtkContainer
966 * @widget: a current child of @container
968 * Removes @widget from @container. @widget must be inside @container.
969 * Note that @container will own a reference to @widget, and that this
970 * may be the last reference held; so removing a widget from its
971 * container can destroy that widget. If you want to use @widget
972 * again, you need to add a reference to it while it's not inside
973 * a container, using g_object_ref().
976 gtk_container_remove (GtkContainer *container,
979 g_return_if_fail (GTK_IS_CONTAINER (container));
980 g_return_if_fail (GTK_IS_WIDGET (widget));
981 g_return_if_fail (widget->parent == GTK_WIDGET (container));
983 g_signal_emit (container, container_signals[REMOVE], 0, widget);
987 _gtk_container_dequeue_resize_handler (GtkContainer *container)
989 g_return_if_fail (GTK_IS_CONTAINER (container));
990 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
992 container_resize_queue = g_slist_remove (container_resize_queue, container);
993 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
997 * gtk_container_set_resize_mode:
998 * @container: a #GtkContainer.
999 * @resize_mode: the new resize mode.
1001 * Sets the resize mode for the container.
1003 * The resize mode of a container determines whether a resize request
1004 * will be passed to the container's parent, queued for later execution
1005 * or executed immediately.
1008 gtk_container_set_resize_mode (GtkContainer *container,
1009 GtkResizeMode resize_mode)
1011 g_return_if_fail (GTK_IS_CONTAINER (container));
1012 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1014 if (GTK_WIDGET_TOPLEVEL (container) &&
1015 resize_mode == GTK_RESIZE_PARENT)
1017 resize_mode = GTK_RESIZE_QUEUE;
1020 if (container->resize_mode != resize_mode)
1022 container->resize_mode = resize_mode;
1024 gtk_widget_queue_resize (GTK_WIDGET (container));
1025 g_object_notify (G_OBJECT (container), "resize_mode");
1030 * gtk_container_get_resize_mode:
1031 * @container: a #GtkContainer
1033 * Returns the resize mode for the container. See
1034 * gtk_container_set_resize_mode ().
1036 * Return value: the current resize mode
1039 gtk_container_get_resize_mode (GtkContainer *container)
1041 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1043 return container->resize_mode;
1047 * gtk_container_set_reallocate_redraws:
1048 * @container: a #GtkContainer.
1049 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
1051 * Sets the @reallocate_redraws flag of the container to the given value.
1053 * Containers requesting reallocation redraws get automatically
1054 * redrawn if any of their children changed allocation.
1057 gtk_container_set_reallocate_redraws (GtkContainer *container,
1058 gboolean needs_redraws)
1060 g_return_if_fail (GTK_IS_CONTAINER (container));
1062 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1065 static GtkContainer*
1066 gtk_container_get_resize_container (GtkContainer *container)
1068 GtkWidget *widget = GTK_WIDGET (container);
1070 while (widget->parent)
1072 widget = widget->parent;
1073 if (GTK_IS_RESIZE_CONTAINER (widget))
1077 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1081 gtk_container_idle_sizer (gpointer data)
1083 GDK_THREADS_ENTER ();
1085 /* we may be invoked with a container_resize_queue of NULL, because
1086 * queue_resize could have been adding an extra idle function while
1087 * the queue still got processed. we better just ignore such case
1088 * than trying to explicitely work around them with some extra flags,
1089 * since it doesn't cause any actual harm.
1091 while (container_resize_queue)
1096 slist = container_resize_queue;
1097 container_resize_queue = slist->next;
1098 widget = slist->data;
1099 g_slist_free_1 (slist);
1101 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1102 gtk_container_check_resize (GTK_CONTAINER (widget));
1105 gdk_window_process_all_updates ();
1107 GDK_THREADS_LEAVE ();
1113 _gtk_container_queue_resize (GtkContainer *container)
1115 GtkContainer *resize_container;
1118 g_return_if_fail (GTK_IS_CONTAINER (container));
1120 widget = GTK_WIDGET (container);
1121 resize_container = gtk_container_get_resize_container (container);
1123 while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1125 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1126 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1127 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1131 widget = widget->parent;
1134 if (resize_container)
1136 if (GTK_WIDGET_VISIBLE (resize_container) &&
1137 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1139 switch (resize_container->resize_mode)
1141 case GTK_RESIZE_QUEUE:
1142 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1144 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1145 if (container_resize_queue == NULL)
1146 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1147 gtk_container_idle_sizer,
1149 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1153 case GTK_RESIZE_IMMEDIATE:
1154 gtk_container_check_resize (resize_container);
1157 case GTK_RESIZE_PARENT:
1158 g_assert_not_reached ();
1164 /* we need to let hidden resize containers know that something
1165 * changed while they where hidden (currently only evaluated by
1168 resize_container->need_resize = TRUE;
1174 gtk_container_check_resize (GtkContainer *container)
1176 g_return_if_fail (GTK_IS_CONTAINER (container));
1178 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1182 gtk_container_real_check_resize (GtkContainer *container)
1184 GtkWidget *widget = GTK_WIDGET (container);
1185 GtkRequisition requisition;
1187 gtk_widget_size_request (widget, &requisition);
1189 if (requisition.width > widget->allocation.width ||
1190 requisition.height > widget->allocation.height)
1192 if (GTK_IS_RESIZE_CONTAINER (container))
1193 gtk_widget_size_allocate (GTK_WIDGET (container),
1194 >K_WIDGET (container)->allocation);
1196 gtk_widget_queue_resize (widget);
1200 gtk_container_resize_children (container);
1204 /* The container hasn't changed size but one of its children
1205 * queued a resize request. Which means that the allocation
1206 * is not sufficient for the requisition of some child.
1207 * We've already performed a size request at this point,
1208 * so we simply need to reallocate and let the allocation
1209 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1212 gtk_container_resize_children (GtkContainer *container)
1216 /* resizing invariants:
1217 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1218 * containers that have an idle sizer pending must be flagged with
1221 g_return_if_fail (GTK_IS_CONTAINER (container));
1223 widget = GTK_WIDGET (container);
1224 gtk_widget_size_allocate (widget, &widget->allocation);
1228 * gtk_container_forall:
1229 * @container: a #GtkContainer
1230 * @callback: a callback
1231 * @callback_data: callback user data
1233 * Invokes @callback on each child of @container, including children
1234 * that are considered "internal" (implementation details of the
1235 * container). "Internal" children generally weren't added by the user
1236 * of the container, but were added by the container implementation
1237 * itself. Most applications should use gtk_container_foreach(),
1238 * rather than gtk_container_forall().
1241 gtk_container_forall (GtkContainer *container,
1242 GtkCallback callback,
1243 gpointer callback_data)
1245 GtkContainerClass *class;
1247 g_return_if_fail (GTK_IS_CONTAINER (container));
1248 g_return_if_fail (callback != NULL);
1250 class = GTK_CONTAINER_GET_CLASS (container);
1253 class->forall (container, TRUE, callback, callback_data);
1257 * gtk_container_foreach:
1258 * @container: a #GtkContainer
1259 * @callback: a callback
1260 * @callback_data: callback user data
1262 * Invokes @callback on each non-internal child of @container. See
1263 * gtk_container_forall() for details on what constitutes an
1264 * "internal" child. Most applications should use
1265 * gtk_container_foreach(), rather than gtk_container_forall().
1268 gtk_container_foreach (GtkContainer *container,
1269 GtkCallback callback,
1270 gpointer callback_data)
1272 GtkContainerClass *class;
1274 g_return_if_fail (GTK_IS_CONTAINER (container));
1275 g_return_if_fail (callback != NULL);
1277 class = GTK_CONTAINER_GET_CLASS (container);
1280 class->forall (container, FALSE, callback, callback_data);
1283 typedef struct _GtkForeachData GtkForeachData;
1284 struct _GtkForeachData
1286 GtkObject *container;
1287 GtkCallbackMarshal callback;
1288 gpointer callback_data;
1292 gtk_container_foreach_unmarshal (GtkWidget *child,
1295 GtkForeachData *fdata = (GtkForeachData*) data;
1298 /* first argument */
1299 args[0].name = NULL;
1300 args[0].type = G_TYPE_FROM_INSTANCE (child);
1301 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1303 /* location for return value */
1304 args[1].name = NULL;
1305 args[1].type = G_TYPE_NONE;
1307 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1311 gtk_container_foreach_full (GtkContainer *container,
1312 GtkCallback callback,
1313 GtkCallbackMarshal marshal,
1314 gpointer callback_data,
1315 GtkDestroyNotify notify)
1317 g_return_if_fail (GTK_IS_CONTAINER (container));
1321 GtkForeachData fdata;
1323 fdata.container = GTK_OBJECT (container);
1324 fdata.callback = marshal;
1325 fdata.callback_data = callback_data;
1327 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1331 g_return_if_fail (callback != NULL);
1333 gtk_container_foreach (container, callback, &callback_data);
1337 notify (callback_data);
1341 gtk_container_set_focus_child (GtkContainer *container,
1344 g_return_if_fail (GTK_IS_CONTAINER (container));
1346 g_return_if_fail (GTK_IS_WIDGET (widget));
1348 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1352 * gtk_container_get_children:
1353 * @container: a #GtkContainer.
1355 * Returns the the container's non-internal children. See
1356 * gtk_container_forall() for details on what constitutes an "internal" child.
1358 * Return value: a newly-allocated list of the container's non-internal children.
1361 gtk_container_get_children (GtkContainer *container)
1363 GList *children = NULL;
1365 gtk_container_foreach (container,
1366 gtk_container_children_callback,
1369 return g_list_reverse (children);
1373 gtk_container_child_position_callback (GtkWidget *widget,
1374 gpointer client_data)
1380 } *data = client_data;
1383 if (data->child == widget)
1384 data->index = data->i;
1388 gtk_container_child_default_composite_name (GtkContainer *container,
1398 /* fallback implementation */
1402 gtk_container_forall (container,
1403 gtk_container_child_position_callback,
1406 name = g_strdup_printf ("%s-%u",
1407 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1414 _gtk_container_child_composite_name (GtkContainer *container,
1417 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1418 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1419 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1421 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1423 static GQuark quark_composite_name = 0;
1426 if (!quark_composite_name)
1427 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1429 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1432 GtkContainerClass *class;
1434 class = GTK_CONTAINER_GET_CLASS (container);
1435 if (class->composite_name)
1436 name = class->composite_name (container, child);
1439 name = g_strdup (name);
1448 gtk_container_real_set_focus_child (GtkContainer *container,
1451 g_return_if_fail (GTK_IS_CONTAINER (container));
1453 g_return_if_fail (GTK_IS_WIDGET (child));
1455 if (child != container->focus_child)
1457 if (container->focus_child)
1458 g_object_unref (container->focus_child);
1459 container->focus_child = child;
1460 if (container->focus_child)
1461 g_object_ref (container->focus_child);
1465 /* check for h/v adjustments
1467 if (container->focus_child)
1469 GtkAdjustment *adjustment;
1471 adjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1473 gtk_adjustment_clamp_page (adjustment,
1474 container->focus_child->allocation.y,
1475 (container->focus_child->allocation.y +
1476 container->focus_child->allocation.height));
1478 adjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1480 gtk_adjustment_clamp_page (adjustment,
1481 container->focus_child->allocation.x,
1482 (container->focus_child->allocation.x +
1483 container->focus_child->allocation.width));
1488 get_focus_chain (GtkContainer *container)
1490 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1493 /* same as gtk_container_get_children, except it includes internals
1496 gtk_container_get_all_children (GtkContainer *container)
1498 GList *children = NULL;
1500 gtk_container_forall (container,
1501 gtk_container_children_callback,
1508 gtk_container_focus (GtkWidget *widget,
1509 GtkDirectionType direction)
1512 GList *sorted_children;
1514 GtkContainer *container;
1516 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1518 container = GTK_CONTAINER (widget);
1522 if (GTK_WIDGET_CAN_FOCUS (container))
1524 if (!GTK_WIDGET_HAS_FOCUS (container))
1526 gtk_widget_grab_focus (GTK_WIDGET (container));
1532 /* Get a list of the containers children, allowing focus
1533 * chain to override.
1535 if (container->has_focus_chain)
1536 children = g_list_copy (get_focus_chain (container));
1538 children = gtk_container_get_all_children (container);
1540 if (container->has_focus_chain &&
1541 (direction == GTK_DIR_TAB_FORWARD ||
1542 direction == GTK_DIR_TAB_BACKWARD))
1544 sorted_children = g_list_copy (children);
1546 if (direction == GTK_DIR_TAB_BACKWARD)
1547 sorted_children = g_list_reverse (sorted_children);
1550 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1552 return_val = gtk_container_focus_move (container, sorted_children, direction);
1554 g_list_free (sorted_children);
1555 g_list_free (children);
1562 tab_compare (gconstpointer a,
1565 const GtkWidget *child1 = a;
1566 const GtkWidget *child2 = b;
1568 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1569 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1573 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1574 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1576 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1579 return (y1 < y2) ? -1 : 1;
1583 gtk_container_focus_sort_tab (GtkContainer *container,
1585 GtkDirectionType direction,
1586 GtkWidget *old_focus)
1588 children = g_list_sort (children, tab_compare);
1590 /* if we are going backwards then reverse the order
1593 if (direction == GTK_DIR_TAB_BACKWARD)
1594 children = g_list_reverse (children);
1599 /* Get coordinates of @widget's allocation with respect to
1600 * allocation of @container.
1603 get_allocation_coords (GtkContainer *container,
1605 GdkRectangle *allocation)
1607 *allocation = widget->allocation;
1609 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1610 0, 0, &allocation->x, &allocation->y);
1613 /* Look for a child in @children that is intermediate between
1614 * the focus widget and container. This widget, if it exists,
1615 * acts as the starting widget for focus navigation.
1618 find_old_focus (GtkContainer *container,
1621 GList *tmp_list = children;
1624 GtkWidget *child = tmp_list->data;
1625 GtkWidget *widget = child;
1627 while (widget && widget != (GtkWidget *)container)
1629 GtkWidget *parent = widget->parent;
1630 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1639 tmp_list = tmp_list->next;
1646 old_focus_coords (GtkContainer *container,
1647 GdkRectangle *old_focus_rect)
1649 GtkWidget *widget = GTK_WIDGET (container);
1650 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1652 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1654 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1656 return get_allocation_coords (container, old_focus, old_focus_rect);
1662 typedef struct _CompareInfo CompareInfo;
1666 GtkContainer *container;
1673 up_down_compare (gconstpointer a,
1677 GdkRectangle allocation1;
1678 GdkRectangle allocation2;
1679 CompareInfo *compare = data;
1682 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1684 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1687 y1 = allocation1.y + allocation1.height / 2;
1688 y2 = allocation2.y + allocation2.height / 2;
1692 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1693 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1695 if (compare->reverse)
1696 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1698 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1701 return (y1 < y2) ? -1 : 1;
1705 gtk_container_focus_sort_up_down (GtkContainer *container,
1707 GtkDirectionType direction,
1708 GtkWidget *old_focus)
1710 CompareInfo compare;
1712 GdkRectangle old_allocation;
1714 compare.container = container;
1715 compare.reverse = (direction == GTK_DIR_UP);
1718 old_focus = find_old_focus (container, children);
1720 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1726 /* Delete widgets from list that don't match minimum criteria */
1728 compare_x1 = old_allocation.x;
1729 compare_x2 = old_allocation.x + old_allocation.width;
1731 if (direction == GTK_DIR_UP)
1732 compare_y = old_allocation.y;
1734 compare_y = old_allocation.y + old_allocation.height;
1736 tmp_list = children;
1739 GtkWidget *child = tmp_list->data;
1740 GList *next = tmp_list->next;
1741 gint child_x1, child_x2;
1742 GdkRectangle child_allocation;
1744 if (child != old_focus)
1746 if (get_allocation_coords (container, child, &child_allocation))
1748 child_x1 = child_allocation.x;
1749 child_x2 = child_allocation.x + child_allocation.width;
1751 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1752 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1753 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1755 children = g_list_delete_link (children, tmp_list);
1759 children = g_list_delete_link (children, tmp_list);
1765 compare.x = (compare_x1 + compare_x2) / 2;
1766 compare.y = old_allocation.y + old_allocation.height / 2;
1770 /* No old focus widget, need to figure out starting x,y some other way
1772 GtkWidget *widget = GTK_WIDGET (container);
1773 GdkRectangle old_focus_rect;
1775 if (old_focus_coords (container, &old_focus_rect))
1777 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1781 if (GTK_WIDGET_NO_WINDOW (widget))
1782 compare.x = widget->allocation.x + widget->allocation.width / 2;
1784 compare.x = widget->allocation.width / 2;
1787 if (GTK_WIDGET_NO_WINDOW (widget))
1788 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1790 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1793 children = g_list_sort_with_data (children, up_down_compare, &compare);
1795 if (compare.reverse)
1796 children = g_list_reverse (children);
1802 left_right_compare (gconstpointer a,
1806 GdkRectangle allocation1;
1807 GdkRectangle allocation2;
1808 CompareInfo *compare = data;
1811 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1813 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1816 x1 = allocation1.x + allocation1.width / 2;
1817 x2 = allocation2.x + allocation2.width / 2;
1821 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1822 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1824 if (compare->reverse)
1825 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1827 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1830 return (x1 < x2) ? -1 : 1;
1834 gtk_container_focus_sort_left_right (GtkContainer *container,
1836 GtkDirectionType direction,
1837 GtkWidget *old_focus)
1839 CompareInfo compare;
1841 GdkRectangle old_allocation;
1843 compare.container = container;
1844 compare.reverse = (direction == GTK_DIR_LEFT);
1847 old_focus = find_old_focus (container, children);
1849 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1855 /* Delete widgets from list that don't match minimum criteria */
1857 compare_y1 = old_allocation.y;
1858 compare_y2 = old_allocation.y + old_allocation.height;
1860 if (direction == GTK_DIR_LEFT)
1861 compare_x = old_allocation.x;
1863 compare_x = old_allocation.x + old_allocation.width;
1865 tmp_list = children;
1868 GtkWidget *child = tmp_list->data;
1869 GList *next = tmp_list->next;
1870 gint child_y1, child_y2;
1871 GdkRectangle child_allocation;
1873 if (child != old_focus)
1875 if (get_allocation_coords (container, child, &child_allocation))
1877 child_y1 = child_allocation.y;
1878 child_y2 = child_allocation.y + child_allocation.height;
1880 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1881 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1882 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1884 children = g_list_delete_link (children, tmp_list);
1888 children = g_list_delete_link (children, tmp_list);
1894 compare.y = (compare_y1 + compare_y2) / 2;
1895 compare.x = old_allocation.x + old_allocation.width / 2;
1899 /* No old focus widget, need to figure out starting x,y some other way
1901 GtkWidget *widget = GTK_WIDGET (container);
1902 GdkRectangle old_focus_rect;
1904 if (old_focus_coords (container, &old_focus_rect))
1906 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1910 if (GTK_WIDGET_NO_WINDOW (widget))
1911 compare.y = widget->allocation.y + widget->allocation.height / 2;
1913 compare.y = widget->allocation.height / 2;
1916 if (GTK_WIDGET_NO_WINDOW (widget))
1917 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1919 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1922 children = g_list_sort_with_data (children, left_right_compare, &compare);
1924 if (compare.reverse)
1925 children = g_list_reverse (children);
1931 * gtk_container_focus_sort:
1932 * @container: a #GtkContainer
1933 * @children: a list of descendents of @container (they don't
1934 * have to be direct children.
1935 * @direction: focus direction
1936 * @old_focus: widget to use for the starting position, or %NULL
1937 * to determine this automatically.
1938 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1939 * which is the only @direction we use currently,
1940 * so perhaps this argument should be removed ]
1942 * Sorts @children in the correct order for focusing with
1943 * direction type @direction.
1945 * Return value: a copy of @children, sorted in correct focusing order,
1946 * with children that aren't suitable for focusing in this direction
1950 _gtk_container_focus_sort (GtkContainer *container,
1952 GtkDirectionType direction,
1953 GtkWidget *old_focus)
1955 children = g_list_copy (children);
1959 case GTK_DIR_TAB_FORWARD:
1960 case GTK_DIR_TAB_BACKWARD:
1961 return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1964 return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1967 return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
1970 g_assert_not_reached ();
1976 gtk_container_focus_move (GtkContainer *container,
1978 GtkDirectionType direction)
1980 GtkWidget *focus_child;
1983 focus_child = container->focus_child;
1987 child = children->data;
1988 children = children->next;
1995 if (focus_child == child)
1999 if (gtk_widget_child_focus (child, direction))
2003 else if (GTK_WIDGET_DRAWABLE (child) &&
2004 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2006 if (gtk_widget_child_focus (child, direction))
2016 gtk_container_children_callback (GtkWidget *widget,
2017 gpointer client_data)
2021 children = (GList**) client_data;
2022 *children = g_list_prepend (*children, widget);
2026 chain_widget_destroyed (GtkWidget *widget,
2029 GtkContainer *container;
2032 container = GTK_CONTAINER (user_data);
2034 chain = g_object_get_data (G_OBJECT (container),
2035 "gtk-container-focus-chain");
2037 chain = g_list_remove (chain, widget);
2039 g_signal_handlers_disconnect_by_func (widget,
2040 chain_widget_destroyed,
2043 g_object_set_data (G_OBJECT (container),
2044 "gtk-container-focus-chain",
2049 * gtk_container_set_focus_chain:
2050 * @container: a #GtkContainer.
2051 * @focusable_widgets: the new focus chain.
2053 * Sets a focus chain, overriding the one computed automatically by GTK+.
2055 * In principle each widget in the chain should be a descendant of the
2056 * container, but this is not enforced by this method, since it's allowed
2057 * to set the focus chain before you pack the widgets, or have a widget
2058 * in the chain that isn't always packed. The necessary checks are done
2059 * when the focus chain is actually traversed.
2062 gtk_container_set_focus_chain (GtkContainer *container,
2063 GList *focusable_widgets)
2068 g_return_if_fail (GTK_IS_CONTAINER (container));
2070 if (container->has_focus_chain)
2071 gtk_container_unset_focus_chain (container);
2073 container->has_focus_chain = TRUE;
2076 tmp_list = focusable_widgets;
2077 while (tmp_list != NULL)
2079 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2081 /* In principle each widget in the chain should be a descendant
2082 * of the container, but we don't want to check that here, it's
2083 * expensive and also it's allowed to set the focus chain before
2084 * you pack the widgets, or have a widget in the chain that isn't
2085 * always packed. So we check for ancestor during actual traversal.
2088 chain = g_list_prepend (chain, tmp_list->data);
2090 g_signal_connect (tmp_list->data,
2092 G_CALLBACK (chain_widget_destroyed),
2095 tmp_list = g_list_next (tmp_list);
2098 chain = g_list_reverse (chain);
2100 g_object_set_data (G_OBJECT (container),
2101 "gtk-container-focus-chain",
2106 * gtk_container_get_focus_chain:
2107 * @container: a #GtkContainer
2108 * @focusable_widgets: location to store the focus chain of the
2109 * container, or %NULL. You should free this list
2110 * using g_list_free() when you are done with it, however
2111 * no additional reference count is added to the
2112 * individual widgets in the focus chain.
2114 * Retrieves the focus chain of the container, if one has been
2115 * set explicitly. If no focus chain has been explicitly
2116 * set, GTK+ computes the focus chain based on the positions
2117 * of the children. In that case, GTK+ stores %NULL in
2118 * @focusable_widgets and returns %FALSE.
2120 * Return value: %TRUE if the focus chain of the container
2121 * has been set explicitly.
2124 gtk_container_get_focus_chain (GtkContainer *container,
2125 GList **focus_chain)
2127 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2131 if (container->has_focus_chain)
2132 *focus_chain = g_list_copy (get_focus_chain (container));
2134 *focus_chain = NULL;
2137 return container->has_focus_chain;
2141 * gtk_container_unset_focus_chain:
2142 * @container: a #GtkContainer.
2144 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2147 gtk_container_unset_focus_chain (GtkContainer *container)
2149 g_return_if_fail (GTK_IS_CONTAINER (container));
2151 if (container->has_focus_chain)
2156 chain = get_focus_chain (container);
2158 container->has_focus_chain = FALSE;
2160 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2164 while (tmp_list != NULL)
2166 g_signal_handlers_disconnect_by_func (tmp_list->data,
2167 chain_widget_destroyed,
2170 tmp_list = g_list_next (tmp_list);
2173 g_list_free (chain);
2178 gtk_container_set_focus_vadjustment (GtkContainer *container,
2179 GtkAdjustment *adjustment)
2181 g_return_if_fail (GTK_IS_CONTAINER (container));
2183 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2186 g_object_ref (adjustment);
2188 g_object_set_qdata_full (G_OBJECT (container),
2195 * gtk_container_get_focus_vadjustment:
2196 * @container: a #GtkContainer
2198 * Retrieves the vertical focus adjustment for the container. See
2199 * gtk_container_set_focus_vadjustment ().
2201 * Return value: the vertical focus adjustment, or %NULL if
2202 * none has been set.
2205 gtk_container_get_focus_vadjustment (GtkContainer *container)
2207 GtkAdjustment *vadjustment;
2209 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2211 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2217 gtk_container_set_focus_hadjustment (GtkContainer *container,
2218 GtkAdjustment *adjustment)
2220 g_return_if_fail (GTK_IS_CONTAINER (container));
2222 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2225 g_object_ref (adjustment);
2227 g_object_set_qdata_full (G_OBJECT (container),
2234 * gtk_container_get_focus_hadjustment:
2235 * @container: a #GtkContainer
2237 * Retrieves the horizontal focus adjustment for the container. See
2238 * gtk_container_set_focus_hadjustment ().
2240 * Return value: the horizontal focus adjustment, or %NULL if
2241 * none has been set.
2244 gtk_container_get_focus_hadjustment (GtkContainer *container)
2246 GtkAdjustment *hadjustment;
2248 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2250 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2257 gtk_container_show_all (GtkWidget *widget)
2259 g_return_if_fail (GTK_IS_CONTAINER (widget));
2261 gtk_container_foreach (GTK_CONTAINER (widget),
2262 (GtkCallback) gtk_widget_show_all,
2264 gtk_widget_show (widget);
2268 gtk_container_hide_all (GtkWidget *widget)
2270 g_return_if_fail (GTK_IS_CONTAINER (widget));
2272 gtk_widget_hide (widget);
2273 gtk_container_foreach (GTK_CONTAINER (widget),
2274 (GtkCallback) gtk_widget_hide_all,
2280 gtk_container_expose_child (GtkWidget *child,
2281 gpointer client_data)
2284 GtkWidget *container;
2285 GdkEventExpose *event;
2286 } *data = client_data;
2288 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2294 gtk_container_expose (GtkWidget *widget,
2295 GdkEventExpose *event)
2298 GtkWidget *container;
2299 GdkEventExpose *event;
2302 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2303 g_return_val_if_fail (event != NULL, FALSE);
2306 if (GTK_WIDGET_DRAWABLE (widget))
2308 data.container = widget;
2311 gtk_container_forall (GTK_CONTAINER (widget),
2312 gtk_container_expose_child,
2320 gtk_container_map_child (GtkWidget *child,
2321 gpointer client_data)
2323 if (GTK_WIDGET_VISIBLE (child) &&
2324 GTK_WIDGET_CHILD_VISIBLE (child) &&
2325 !GTK_WIDGET_MAPPED (child))
2326 gtk_widget_map (child);
2330 gtk_container_map (GtkWidget *widget)
2332 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2334 gtk_container_forall (GTK_CONTAINER (widget),
2335 gtk_container_map_child,
2338 if (!GTK_WIDGET_NO_WINDOW (widget))
2339 gdk_window_show (widget->window);
2343 gtk_container_unmap (GtkWidget *widget)
2345 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2347 if (!GTK_WIDGET_NO_WINDOW (widget))
2348 gdk_window_hide (widget->window);
2350 gtk_container_forall (GTK_CONTAINER (widget),
2351 (GtkCallback)gtk_widget_unmap,
2356 * gtk_container_propagate_expose:
2357 * @container: a #GtkContainer
2358 * @child: a child of @container
2359 * @event: a expose event sent to container
2361 * When a container receives an expose event, it must send synthetic
2362 * expose events to all children that don't have their own #GdkWindows.
2363 * This function provides a convenient way of doing this. A container,
2364 * when it receives an expose event, calls gtk_container_propagate_expose()
2365 * once for each child, passing in the event the container received.
2367 * gtk_container_propagate_expose() takes care of deciding whether
2368 * an expose event needs to be sent to the child, intersecting
2369 * the event's area with the child area, and sending the event.
2371 * In most cases, a container can simply either simply inherit the
2372 * ::expose implementation from #GtkContainer, or, do some drawing
2373 * and then chain to the ::expose implementation from #GtkContainer.
2376 gtk_container_propagate_expose (GtkContainer *container,
2378 GdkEventExpose *event)
2380 GdkEvent *child_event;
2382 g_return_if_fail (GTK_IS_CONTAINER (container));
2383 g_return_if_fail (GTK_IS_WIDGET (child));
2384 g_return_if_fail (event != NULL);
2386 g_assert (child->parent == GTK_WIDGET (container));
2388 if (GTK_WIDGET_DRAWABLE (child) &&
2389 GTK_WIDGET_NO_WINDOW (child) &&
2390 (child->window == event->window))
2392 child_event = gdk_event_new (GDK_EXPOSE);
2393 child_event->expose = *event;
2394 g_object_ref (child_event->expose.window);
2396 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2397 if (!gdk_region_empty (child_event->expose.region))
2399 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2400 gtk_widget_send_expose (child, child_event);
2402 gdk_event_free (child_event);