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/.
32 #include "gtkcontainer.h"
33 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
36 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
39 #include "gtktoolbar.h"
40 #include <gobject/gobjectnotifyqueue.c>
41 #include <gobject/gvaluecollector.h>
60 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
61 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
64 /* --- prototypes --- */
65 static void gtk_container_base_class_init (GtkContainerClass *klass);
66 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
67 static void gtk_container_class_init (GtkContainerClass *klass);
68 static void gtk_container_init (GtkContainer *container);
69 static void gtk_container_destroy (GtkObject *object);
70 static void gtk_container_set_property (GObject *object,
74 static void gtk_container_get_property (GObject *object,
78 static void gtk_container_add_unimplemented (GtkContainer *container,
80 static void gtk_container_remove_unimplemented (GtkContainer *container,
82 static void gtk_container_real_check_resize (GtkContainer *container);
83 static gboolean gtk_container_focus (GtkWidget *widget,
84 GtkDirectionType direction);
85 static void gtk_container_real_set_focus_child (GtkContainer *container,
88 static gboolean gtk_container_focus_move (GtkContainer *container,
90 GtkDirectionType direction);
91 static void gtk_container_children_callback (GtkWidget *widget,
92 gpointer client_data);
93 static void gtk_container_show_all (GtkWidget *widget);
94 static void gtk_container_hide_all (GtkWidget *widget);
95 static gint gtk_container_expose (GtkWidget *widget,
96 GdkEventExpose *event);
97 static void gtk_container_map (GtkWidget *widget);
98 static void gtk_container_unmap (GtkWidget *widget);
100 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
104 static void gtk_container_buildable_init (GtkBuildableIface *iface);
105 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
109 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
112 const gchar *tagname,
113 GMarkupParser *parser,
115 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
118 const gchar *tagname,
122 /* --- variables --- */
123 static const gchar vadjustment_key[] = "gtk-vadjustment";
124 static guint vadjustment_key_id = 0;
125 static const gchar hadjustment_key[] = "gtk-hadjustment";
126 static guint hadjustment_key_id = 0;
127 static GSList *container_resize_queue = NULL;
128 static guint container_signals[LAST_SIGNAL] = { 0 };
129 static GtkWidgetClass *parent_class = NULL;
130 extern GParamSpecPool *_gtk_widget_child_property_pool;
131 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
132 static GtkBuildableIface *parent_buildable_iface;
135 /* --- functions --- */
137 gtk_container_get_type (void)
139 static GType container_type = 0;
143 const GTypeInfo container_info =
145 sizeof (GtkContainerClass),
146 (GBaseInitFunc) gtk_container_base_class_init,
147 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
148 (GClassInitFunc) gtk_container_class_init,
149 NULL /* class_finalize */,
150 NULL /* class_data */,
151 sizeof (GtkContainer),
153 (GInstanceInitFunc) gtk_container_init,
154 NULL, /* value_table */
157 static const GInterfaceInfo buildable_info =
159 (GInterfaceInitFunc) gtk_container_buildable_init,
165 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
166 &container_info, G_TYPE_FLAG_ABSTRACT);
168 g_type_add_interface_static (container_type,
174 return container_type;
178 gtk_container_base_class_init (GtkContainerClass *class)
180 /* reset instance specifc class fields that don't get inherited */
181 class->set_child_property = NULL;
182 class->get_child_property = NULL;
186 gtk_container_base_class_finalize (GtkContainerClass *class)
190 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
191 for (node = list; node; node = node->next)
193 GParamSpec *pspec = node->data;
195 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
196 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
197 g_param_spec_unref (pspec);
203 gtk_container_class_init (GtkContainerClass *class)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
206 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
207 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
209 parent_class = g_type_class_peek_parent (class);
211 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
212 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
214 gobject_class->set_property = gtk_container_set_property;
215 gobject_class->get_property = gtk_container_get_property;
217 object_class->destroy = gtk_container_destroy;
219 widget_class->show_all = gtk_container_show_all;
220 widget_class->hide_all = gtk_container_hide_all;
221 widget_class->expose_event = gtk_container_expose;
222 widget_class->map = gtk_container_map;
223 widget_class->unmap = gtk_container_unmap;
224 widget_class->focus = gtk_container_focus;
226 class->add = gtk_container_add_unimplemented;
227 class->remove = gtk_container_remove_unimplemented;
228 class->check_resize = gtk_container_real_check_resize;
229 class->forall = NULL;
230 class->set_focus_child = gtk_container_real_set_focus_child;
231 class->child_type = NULL;
232 class->composite_name = gtk_container_child_default_composite_name;
234 g_object_class_install_property (gobject_class,
236 g_param_spec_enum ("resize-mode",
238 P_("Specify how resize events are handled"),
239 GTK_TYPE_RESIZE_MODE,
241 GTK_PARAM_READWRITE));
242 g_object_class_install_property (gobject_class,
244 g_param_spec_uint ("border-width",
246 P_("The width of the empty border outside the containers children"),
250 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
253 g_param_spec_object ("child",
255 P_("Can be used to add a new child to the container"),
257 GTK_PARAM_WRITABLE));
258 container_signals[ADD] =
259 g_signal_new (I_("add"),
260 G_OBJECT_CLASS_TYPE (object_class),
262 G_STRUCT_OFFSET (GtkContainerClass, add),
264 _gtk_marshal_VOID__OBJECT,
267 container_signals[REMOVE] =
268 g_signal_new (I_("remove"),
269 G_OBJECT_CLASS_TYPE (object_class),
271 G_STRUCT_OFFSET (GtkContainerClass, remove),
273 _gtk_marshal_VOID__OBJECT,
276 container_signals[CHECK_RESIZE] =
277 g_signal_new (I_("check-resize"),
278 G_OBJECT_CLASS_TYPE (object_class),
280 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
282 _gtk_marshal_VOID__VOID,
284 container_signals[SET_FOCUS_CHILD] =
285 g_signal_new (I_("set-focus-child"),
286 G_OBJECT_CLASS_TYPE (object_class),
288 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
290 _gtk_marshal_VOID__OBJECT,
296 gtk_container_buildable_init (GtkBuildableIface *iface)
298 parent_buildable_iface = g_type_interface_peek_parent (iface);
299 iface->add_child = gtk_container_buildable_add_child;
300 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
301 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
305 gtk_container_buildable_add_child (GtkBuildable *buildable,
312 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
314 else if (GTK_IS_WIDGET (child) && GTK_WIDGET_TOPLEVEL (child) == FALSE)
316 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
319 g_warning ("Cannot add an object of type %s to a container of type %s",
320 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
324 gtk_container_buildable_set_child_property (GtkContainer *container,
331 GValue gvalue = { 0, };
332 GError *error = NULL;
334 pspec = gtk_container_class_find_child_property
335 (G_OBJECT_GET_CLASS (container), name);
338 g_warning ("%s does not have a property called %s",
339 g_type_name (G_OBJECT_TYPE (container)), name);
343 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
345 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
346 g_type_name (G_OBJECT_TYPE (container)),
349 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
351 g_error_free (error);
355 gtk_container_child_set_property (container, child, name, &gvalue);
356 g_value_unset (&gvalue);
361 GtkContainer *container;
363 gchar *child_prop_name;
364 } PackingPropertiesData;
367 attributes_start_element (GMarkupParseContext *context,
368 const gchar *element_name,
370 const gchar **values,
374 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
377 if (strcmp (element_name, "property") == 0)
379 for (i = 0; names[i]; i++)
380 if (strcmp (names[i], "name") == 0)
381 parser_data->child_prop_name = g_strdup (values[i]);
383 else if (strcmp (element_name, "packing") == 0)
386 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
390 attributes_text_element (GMarkupParseContext *context,
396 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
398 if (!parser_data->child_prop_name)
401 gtk_container_buildable_set_child_property (parser_data->container,
402 parser_data->builder,
404 parser_data->child_prop_name,
407 g_free (parser_data->child_prop_name);
408 parser_data->child_prop_name = NULL;
411 static const GMarkupParser attributes_parser =
413 attributes_start_element,
415 attributes_text_element,
419 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
422 const gchar *tagname,
423 GMarkupParser *parser,
426 PackingPropertiesData *parser_data;
428 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
429 tagname, parser, data))
432 if (child && strcmp (tagname, "packing") == 0)
434 parser_data = g_slice_new0 (PackingPropertiesData);
435 parser_data->builder = builder;
436 parser_data->container = GTK_CONTAINER (buildable);
437 parser_data->child = GTK_WIDGET (child);
438 parser_data->child_prop_name = NULL;
440 *parser = attributes_parser;
449 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
452 const gchar *tagname,
455 if (strcmp (tagname, "packing") == 0)
457 g_slice_free (PackingPropertiesData, (gpointer)data);
462 if (parent_buildable_iface->custom_tag_end)
463 parent_buildable_iface->custom_tag_end (buildable, builder,
464 child, tagname, data);
469 * gtk_container_child_type:
470 * @container: a #GtkContainer
472 * Returns the type of the children supported by the container.
474 * Note that this may return %G_TYPE_NONE to indicate that no more
475 * children can be added, e.g. for a #GtkPaned which already has two
478 * Return value: a #GType.
481 gtk_container_child_type (GtkContainer *container)
484 GtkContainerClass *class;
486 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
488 class = GTK_CONTAINER_GET_CLASS (container);
489 if (class->child_type)
490 slot = class->child_type (container);
497 /* --- GtkContainer child property mechanism --- */
499 container_get_child_property (GtkContainer *container,
504 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
506 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
510 container_set_child_property (GtkContainer *container,
514 GObjectNotifyQueue *nqueue)
516 GValue tmp_value = { 0, };
517 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
519 /* provide a copy to work from, convert (if necessary) and validate */
520 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
521 if (!g_value_transform (value, &tmp_value))
522 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
524 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
525 G_VALUE_TYPE_NAME (value));
526 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
528 gchar *contents = g_strdup_value_contents (value);
530 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
532 G_VALUE_TYPE_NAME (value),
534 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
539 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
540 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
542 g_value_unset (&tmp_value);
546 * gtk_container_child_get_valist:
547 * @container: a #GtkContainer
548 * @child: a widget which is a child of @container
549 * @first_property_name: the name of the first property to get
550 * @var_args: return location for the first property, followed
551 * optionally by more name/return location pairs, followed by %NULL
553 * Gets the values of one or more child properties for @child and @container.
556 gtk_container_child_get_valist (GtkContainer *container,
558 const gchar *first_property_name,
563 g_return_if_fail (GTK_IS_CONTAINER (container));
564 g_return_if_fail (GTK_IS_WIDGET (child));
565 g_return_if_fail (child->parent == GTK_WIDGET (container));
567 g_object_ref (container);
568 g_object_ref (child);
570 name = first_property_name;
573 GValue value = { 0, };
577 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
579 G_OBJECT_TYPE (container),
583 g_warning ("%s: container class `%s' has no child property named `%s'",
585 G_OBJECT_TYPE_NAME (container),
589 if (!(pspec->flags & G_PARAM_READABLE))
591 g_warning ("%s: child property `%s' of container class `%s' is not readable",
594 G_OBJECT_TYPE_NAME (container));
597 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
598 container_get_child_property (container, child, pspec, &value);
599 G_VALUE_LCOPY (&value, var_args, 0, &error);
602 g_warning ("%s: %s", G_STRLOC, error);
604 g_value_unset (&value);
607 g_value_unset (&value);
608 name = va_arg (var_args, gchar*);
611 g_object_unref (child);
612 g_object_unref (container);
616 * gtk_container_child_get_property:
617 * @container: a #GtkContainer
618 * @child: a widget which is a child of @container
619 * @property_name: the name of the property to get
620 * @value: a location to return the value
622 * Gets the value of a child property for @child and @container.
625 gtk_container_child_get_property (GtkContainer *container,
627 const gchar *property_name,
632 g_return_if_fail (GTK_IS_CONTAINER (container));
633 g_return_if_fail (GTK_IS_WIDGET (child));
634 g_return_if_fail (child->parent == GTK_WIDGET (container));
635 g_return_if_fail (property_name != NULL);
636 g_return_if_fail (G_IS_VALUE (value));
638 g_object_ref (container);
639 g_object_ref (child);
640 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
641 G_OBJECT_TYPE (container), TRUE);
643 g_warning ("%s: container class `%s' has no child property named `%s'",
645 G_OBJECT_TYPE_NAME (container),
647 else if (!(pspec->flags & G_PARAM_READABLE))
648 g_warning ("%s: child property `%s' of container class `%s' is not readable",
651 G_OBJECT_TYPE_NAME (container));
654 GValue *prop_value, tmp_value = { 0, };
656 /* auto-conversion of the callers value type
658 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
660 g_value_reset (value);
663 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
665 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
667 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
668 G_VALUE_TYPE_NAME (value));
669 g_object_unref (child);
670 g_object_unref (container);
675 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
676 prop_value = &tmp_value;
678 container_get_child_property (container, child, pspec, prop_value);
679 if (prop_value != value)
681 g_value_transform (prop_value, value);
682 g_value_unset (&tmp_value);
685 g_object_unref (child);
686 g_object_unref (container);
690 * gtk_container_child_set_valist:
691 * @container: a #GtkContainer
692 * @child: a widget which is a child of @container
693 * @first_property_name: the name of the first property to set
694 * @var_args: a %NULL-terminated list of property names and values, starting
695 * with @first_prop_name
697 * Sets one or more child properties for @child and @container.
700 gtk_container_child_set_valist (GtkContainer *container,
702 const gchar *first_property_name,
705 GObjectNotifyQueue *nqueue;
708 g_return_if_fail (GTK_IS_CONTAINER (container));
709 g_return_if_fail (GTK_IS_WIDGET (child));
710 g_return_if_fail (child->parent == GTK_WIDGET (container));
712 g_object_ref (container);
713 g_object_ref (child);
715 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
716 name = first_property_name;
719 GValue value = { 0, };
721 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
723 G_OBJECT_TYPE (container),
727 g_warning ("%s: container class `%s' has no child property named `%s'",
729 G_OBJECT_TYPE_NAME (container),
733 if (!(pspec->flags & G_PARAM_WRITABLE))
735 g_warning ("%s: child property `%s' of container class `%s' is not writable",
738 G_OBJECT_TYPE_NAME (container));
741 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
742 G_VALUE_COLLECT (&value, var_args, 0, &error);
745 g_warning ("%s: %s", G_STRLOC, error);
748 /* we purposely leak the value here, it might not be
749 * in a sane state if an error condition occoured
753 container_set_child_property (container, child, pspec, &value, nqueue);
754 g_value_unset (&value);
755 name = va_arg (var_args, gchar*);
757 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
759 g_object_unref (container);
760 g_object_unref (child);
764 * gtk_container_child_set_property:
765 * @container: a #GtkContainer
766 * @child: a widget which is a child of @container
767 * @property_name: the name of the property to set
768 * @value: the value to set the property to
770 * Sets a child property for @child and @container.
773 gtk_container_child_set_property (GtkContainer *container,
775 const gchar *property_name,
778 GObjectNotifyQueue *nqueue;
781 g_return_if_fail (GTK_IS_CONTAINER (container));
782 g_return_if_fail (GTK_IS_WIDGET (child));
783 g_return_if_fail (child->parent == GTK_WIDGET (container));
784 g_return_if_fail (property_name != NULL);
785 g_return_if_fail (G_IS_VALUE (value));
787 g_object_ref (container);
788 g_object_ref (child);
790 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
791 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
792 G_OBJECT_TYPE (container), TRUE);
794 g_warning ("%s: container class `%s' has no child property named `%s'",
796 G_OBJECT_TYPE_NAME (container),
798 else if (!(pspec->flags & G_PARAM_WRITABLE))
799 g_warning ("%s: child property `%s' of container class `%s' is not writable",
802 G_OBJECT_TYPE_NAME (container));
805 container_set_child_property (container, child, pspec, value, nqueue);
807 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
808 g_object_unref (container);
809 g_object_unref (child);
813 * gtk_container_add_with_properties:
814 * @container: a #GtkContainer
815 * @widget: a widget to be placed inside @container
816 * @first_prop_name: the name of the first child property to set
817 * @Varargs: a %NULL-terminated list of property names and values, starting
818 * with @first_prop_name
820 * Adds @widget to @container, setting child properties at the same time.
821 * See gtk_container_add() and gtk_container_child_set() for more details.
824 gtk_container_add_with_properties (GtkContainer *container,
826 const gchar *first_prop_name,
829 g_return_if_fail (GTK_IS_CONTAINER (container));
830 g_return_if_fail (GTK_IS_WIDGET (widget));
831 g_return_if_fail (widget->parent == NULL);
833 g_object_ref (container);
834 g_object_ref (widget);
835 gtk_widget_freeze_child_notify (widget);
837 g_signal_emit (container, container_signals[ADD], 0, widget);
842 va_start (var_args, first_prop_name);
843 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
847 gtk_widget_thaw_child_notify (widget);
848 g_object_unref (widget);
849 g_object_unref (container);
853 * gtk_container_child_set:
854 * @container: a #GtkContainer
855 * @child: a widget which is a child of @container
856 * @first_prop_name: the name of the first property to set
857 * @Varargs: a %NULL-terminated list of property names and values, starting
858 * with @first_prop_name
860 * Sets one or more child properties for @child and @container.
863 gtk_container_child_set (GtkContainer *container,
865 const gchar *first_prop_name,
870 g_return_if_fail (GTK_IS_CONTAINER (container));
871 g_return_if_fail (GTK_IS_WIDGET (child));
872 g_return_if_fail (child->parent == GTK_WIDGET (container));
874 va_start (var_args, first_prop_name);
875 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
880 * gtk_container_child_get:
881 * @container: a #GtkContainer
882 * @child: a widget which is a child of @container
883 * @first_prop_name: the name of the first property to get
884 * @Varargs: return location for the first property, followed
885 * optionally by more name/return location pairs, followed by %NULL
887 * Gets the values of one or more child properties for @child and @container.
890 gtk_container_child_get (GtkContainer *container,
892 const gchar *first_prop_name,
897 g_return_if_fail (GTK_IS_CONTAINER (container));
898 g_return_if_fail (GTK_IS_WIDGET (child));
899 g_return_if_fail (child->parent == GTK_WIDGET (container));
901 va_start (var_args, first_prop_name);
902 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
907 * gtk_container_class_install_child_property:
908 * @cclass: a #GtkContainerClass
909 * @property_id: the id for the property
910 * @pspec: the #GParamSpec for the property
912 * Installs a child property on a container class.
915 gtk_container_class_install_child_property (GtkContainerClass *cclass,
919 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
920 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
921 if (pspec->flags & G_PARAM_WRITABLE)
922 g_return_if_fail (cclass->set_child_property != NULL);
923 if (pspec->flags & G_PARAM_READABLE)
924 g_return_if_fail (cclass->get_child_property != NULL);
925 g_return_if_fail (property_id > 0);
926 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
927 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
928 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
930 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
932 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
933 G_OBJECT_CLASS_NAME (cclass),
937 g_param_spec_ref (pspec);
938 g_param_spec_sink (pspec);
939 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
940 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
944 * gtk_container_class_find_child_property:
945 * @cclass: a #GtkContainerClass
946 * @property_name: the name of the child property to find
947 * @returns: the #GParamSpec of the child property or %NULL if @class has no
948 * child property with that name.
950 * Finds a child property of a container class by name.
953 gtk_container_class_find_child_property (GObjectClass *cclass,
954 const gchar *property_name)
956 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
957 g_return_val_if_fail (property_name != NULL, NULL);
959 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
961 G_OBJECT_CLASS_TYPE (cclass),
966 * gtk_container_class_list_child_properties:
967 * @cclass: a #GtkContainerClass
968 * @n_properties: location to return the number of child properties found
969 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
970 * The array must be freed with g_free().
972 * Returns all child properties of a container class.
975 gtk_container_class_list_child_properties (GObjectClass *cclass,
981 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
983 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
984 G_OBJECT_CLASS_TYPE (cclass),
993 gtk_container_add_unimplemented (GtkContainer *container,
996 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1000 gtk_container_remove_unimplemented (GtkContainer *container,
1003 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1007 gtk_container_init (GtkContainer *container)
1009 container->focus_child = NULL;
1010 container->border_width = 0;
1011 container->need_resize = FALSE;
1012 container->resize_mode = GTK_RESIZE_PARENT;
1013 container->reallocate_redraws = FALSE;
1017 gtk_container_destroy (GtkObject *object)
1019 GtkContainer *container = GTK_CONTAINER (object);
1021 if (GTK_CONTAINER_RESIZE_PENDING (container))
1022 _gtk_container_dequeue_resize_handler (container);
1024 /* do this before walking child widgets, to avoid
1025 * removing children from focus chain one by one.
1027 if (container->has_focus_chain)
1028 gtk_container_unset_focus_chain (container);
1030 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1032 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1036 gtk_container_set_property (GObject *object,
1038 const GValue *value,
1041 GtkContainer *container = GTK_CONTAINER (object);
1045 case PROP_BORDER_WIDTH:
1046 gtk_container_set_border_width (container, g_value_get_uint (value));
1048 case PROP_RESIZE_MODE:
1049 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1052 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1055 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1061 gtk_container_get_property (GObject *object,
1066 GtkContainer *container = GTK_CONTAINER (object);
1070 case PROP_BORDER_WIDTH:
1071 g_value_set_uint (value, container->border_width);
1073 case PROP_RESIZE_MODE:
1074 g_value_set_enum (value, container->resize_mode);
1077 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1083 * gtk_container_set_border_width:
1084 * @container: a #GtkContainer
1085 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1086 * the container. Valid values are in the range 0-65535 pixels.
1088 * Sets the border width of the container.
1090 * The border width of a container is the amount of space to leave
1091 * around the outside of the container. The only exception to this is
1092 * #GtkWindow; because toplevel windows can't leave space outside,
1093 * they leave the space inside. The border is added on all sides of
1094 * the container. To add space to only one side, one approach is to
1095 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1096 * to give it a size, and place it on the side of the container as
1100 gtk_container_set_border_width (GtkContainer *container,
1103 g_return_if_fail (GTK_IS_CONTAINER (container));
1105 if (container->border_width != border_width)
1107 container->border_width = border_width;
1108 g_object_notify (G_OBJECT (container), "border-width");
1110 if (GTK_WIDGET_REALIZED (container))
1111 gtk_widget_queue_resize (GTK_WIDGET (container));
1116 * gtk_container_get_border_width:
1117 * @container: a #GtkContainer
1119 * Retrieves the border width of the container. See
1120 * gtk_container_set_border_width().
1122 * Return value: the current border width
1125 gtk_container_get_border_width (GtkContainer *container)
1127 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1129 return container->border_width;
1133 * gtk_container_add:
1134 * @container: a #GtkContainer
1135 * @widget: a widget to be placed inside @container
1137 * Adds @widget to @container. Typically used for simple containers
1138 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1139 * layout containers such as #GtkBox or #GtkTable, this function will
1140 * pick default packing parameters that may not be correct. So
1141 * consider functions such as gtk_box_pack_start() and
1142 * gtk_table_attach() as an alternative to gtk_container_add() in
1143 * those cases. A widget may be added to only one container at a time;
1144 * you can't place the same widget inside two different containers.
1147 gtk_container_add (GtkContainer *container,
1150 g_return_if_fail (GTK_IS_CONTAINER (container));
1151 g_return_if_fail (GTK_IS_WIDGET (widget));
1153 if (widget->parent != NULL)
1155 g_warning ("Attempting to add a widget with type %s to a container of "
1156 "type %s, but the widget is already inside a container of type %s, "
1157 "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1158 "explains how to reparent a widget.",
1159 g_type_name (G_OBJECT_TYPE (widget)),
1160 g_type_name (G_OBJECT_TYPE (container)),
1161 g_type_name (G_OBJECT_TYPE (widget->parent)));
1165 g_signal_emit (container, container_signals[ADD], 0, widget);
1169 * gtk_container_remove:
1170 * @container: a #GtkContainer
1171 * @widget: a current child of @container
1173 * Removes @widget from @container. @widget must be inside @container.
1174 * Note that @container will own a reference to @widget, and that this
1175 * may be the last reference held; so removing a widget from its
1176 * container can destroy that widget. If you want to use @widget
1177 * again, you need to add a reference to it while it's not inside
1178 * a container, using g_object_ref(). If you don't want to use @widget
1179 * again it's usually more efficient to simply destroy it directly
1180 * using gtk_widget_destroy() since this will remove it from the
1181 * container and help break any circular reference count cycles.
1184 gtk_container_remove (GtkContainer *container,
1187 g_return_if_fail (GTK_IS_CONTAINER (container));
1188 g_return_if_fail (GTK_IS_WIDGET (widget));
1190 /* When using the deprecated API of the toolbar, it is possible
1191 * to legitimately call this function with a widget that is not
1192 * a direct child of the container.
1194 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1195 widget->parent == GTK_WIDGET (container));
1197 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1201 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1203 g_return_if_fail (GTK_IS_CONTAINER (container));
1204 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1206 container_resize_queue = g_slist_remove (container_resize_queue, container);
1207 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1211 * gtk_container_set_resize_mode:
1212 * @container: a #GtkContainer
1213 * @resize_mode: the new resize mode
1215 * Sets the resize mode for the container.
1217 * The resize mode of a container determines whether a resize request
1218 * will be passed to the container's parent, queued for later execution
1219 * or executed immediately.
1222 gtk_container_set_resize_mode (GtkContainer *container,
1223 GtkResizeMode resize_mode)
1225 g_return_if_fail (GTK_IS_CONTAINER (container));
1226 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1228 if (GTK_WIDGET_TOPLEVEL (container) &&
1229 resize_mode == GTK_RESIZE_PARENT)
1231 resize_mode = GTK_RESIZE_QUEUE;
1234 if (container->resize_mode != resize_mode)
1236 container->resize_mode = resize_mode;
1238 gtk_widget_queue_resize (GTK_WIDGET (container));
1239 g_object_notify (G_OBJECT (container), "resize-mode");
1244 * gtk_container_get_resize_mode:
1245 * @container: a #GtkContainer
1247 * Returns the resize mode for the container. See
1248 * gtk_container_set_resize_mode ().
1250 * Return value: the current resize mode
1253 gtk_container_get_resize_mode (GtkContainer *container)
1255 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1257 return container->resize_mode;
1261 * gtk_container_set_reallocate_redraws:
1262 * @container: a #GtkContainer
1263 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1265 * Sets the @reallocate_redraws flag of the container to the given value.
1267 * Containers requesting reallocation redraws get automatically
1268 * redrawn if any of their children changed allocation.
1271 gtk_container_set_reallocate_redraws (GtkContainer *container,
1272 gboolean needs_redraws)
1274 g_return_if_fail (GTK_IS_CONTAINER (container));
1276 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1279 static GtkContainer*
1280 gtk_container_get_resize_container (GtkContainer *container)
1282 GtkWidget *widget = GTK_WIDGET (container);
1284 while (widget->parent)
1286 widget = widget->parent;
1287 if (GTK_IS_RESIZE_CONTAINER (widget))
1291 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1295 gtk_container_idle_sizer (gpointer data)
1297 /* we may be invoked with a container_resize_queue of NULL, because
1298 * queue_resize could have been adding an extra idle function while
1299 * the queue still got processed. we better just ignore such case
1300 * than trying to explicitely work around them with some extra flags,
1301 * since it doesn't cause any actual harm.
1303 while (container_resize_queue)
1308 slist = container_resize_queue;
1309 container_resize_queue = slist->next;
1310 widget = slist->data;
1311 g_slist_free_1 (slist);
1313 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1314 gtk_container_check_resize (GTK_CONTAINER (widget));
1317 gdk_window_process_all_updates ();
1323 _gtk_container_queue_resize (GtkContainer *container)
1325 GtkContainer *resize_container;
1328 g_return_if_fail (GTK_IS_CONTAINER (container));
1330 widget = GTK_WIDGET (container);
1331 resize_container = gtk_container_get_resize_container (container);
1335 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1336 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1337 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1341 widget = widget->parent;
1344 if (resize_container)
1346 if (GTK_WIDGET_VISIBLE (resize_container) &&
1347 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1349 switch (resize_container->resize_mode)
1351 case GTK_RESIZE_QUEUE:
1352 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1354 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1355 if (container_resize_queue == NULL)
1356 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1357 gtk_container_idle_sizer,
1359 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1363 case GTK_RESIZE_IMMEDIATE:
1364 gtk_container_check_resize (resize_container);
1367 case GTK_RESIZE_PARENT:
1368 g_assert_not_reached ();
1374 /* we need to let hidden resize containers know that something
1375 * changed while they where hidden (currently only evaluated by
1378 resize_container->need_resize = TRUE;
1384 gtk_container_check_resize (GtkContainer *container)
1386 g_return_if_fail (GTK_IS_CONTAINER (container));
1388 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1392 gtk_container_real_check_resize (GtkContainer *container)
1394 GtkWidget *widget = GTK_WIDGET (container);
1395 GtkRequisition requisition;
1397 gtk_widget_size_request (widget, &requisition);
1399 if (requisition.width > widget->allocation.width ||
1400 requisition.height > widget->allocation.height)
1402 if (GTK_IS_RESIZE_CONTAINER (container))
1403 gtk_widget_size_allocate (GTK_WIDGET (container),
1404 >K_WIDGET (container)->allocation);
1406 gtk_widget_queue_resize (widget);
1410 gtk_container_resize_children (container);
1414 /* The container hasn't changed size but one of its children
1415 * queued a resize request. Which means that the allocation
1416 * is not sufficient for the requisition of some child.
1417 * We've already performed a size request at this point,
1418 * so we simply need to reallocate and let the allocation
1419 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1422 gtk_container_resize_children (GtkContainer *container)
1426 /* resizing invariants:
1427 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1428 * containers that have an idle sizer pending must be flagged with
1431 g_return_if_fail (GTK_IS_CONTAINER (container));
1433 widget = GTK_WIDGET (container);
1434 gtk_widget_size_allocate (widget, &widget->allocation);
1438 * gtk_container_forall:
1439 * @container: a #GtkContainer
1440 * @callback: a callback
1441 * @callback_data: callback user data
1443 * Invokes @callback on each child of @container, including children
1444 * that are considered "internal" (implementation details of the
1445 * container). "Internal" children generally weren't added by the user
1446 * of the container, but were added by the container implementation
1447 * itself. Most applications should use gtk_container_foreach(),
1448 * rather than gtk_container_forall().
1451 gtk_container_forall (GtkContainer *container,
1452 GtkCallback callback,
1453 gpointer callback_data)
1455 GtkContainerClass *class;
1457 g_return_if_fail (GTK_IS_CONTAINER (container));
1458 g_return_if_fail (callback != NULL);
1460 class = GTK_CONTAINER_GET_CLASS (container);
1463 class->forall (container, TRUE, callback, callback_data);
1467 * gtk_container_foreach:
1468 * @container: a #GtkContainer
1469 * @callback: a callback
1470 * @callback_data: callback user data
1472 * Invokes @callback on each non-internal child of @container. See
1473 * gtk_container_forall() for details on what constitutes an
1474 * "internal" child. Most applications should use
1475 * gtk_container_foreach(), rather than gtk_container_forall().
1478 gtk_container_foreach (GtkContainer *container,
1479 GtkCallback callback,
1480 gpointer callback_data)
1482 GtkContainerClass *class;
1484 g_return_if_fail (GTK_IS_CONTAINER (container));
1485 g_return_if_fail (callback != NULL);
1487 class = GTK_CONTAINER_GET_CLASS (container);
1490 class->forall (container, FALSE, callback, callback_data);
1493 typedef struct _GtkForeachData GtkForeachData;
1494 struct _GtkForeachData
1496 GtkObject *container;
1497 GtkCallbackMarshal callback;
1498 gpointer callback_data;
1502 gtk_container_foreach_unmarshal (GtkWidget *child,
1505 GtkForeachData *fdata = (GtkForeachData*) data;
1508 /* first argument */
1509 args[0].name = NULL;
1510 args[0].type = G_TYPE_FROM_INSTANCE (child);
1511 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1513 /* location for return value */
1514 args[1].name = NULL;
1515 args[1].type = G_TYPE_NONE;
1517 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1521 gtk_container_foreach_full (GtkContainer *container,
1522 GtkCallback callback,
1523 GtkCallbackMarshal marshal,
1524 gpointer callback_data,
1525 GDestroyNotify notify)
1527 g_return_if_fail (GTK_IS_CONTAINER (container));
1531 GtkForeachData fdata;
1533 fdata.container = GTK_OBJECT (container);
1534 fdata.callback = marshal;
1535 fdata.callback_data = callback_data;
1537 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1541 g_return_if_fail (callback != NULL);
1543 gtk_container_foreach (container, callback, &callback_data);
1547 notify (callback_data);
1551 * gtk_container_set_focus_child:
1552 * @container: a #GtkContainer
1553 * @child: a #GtkWidget, or %NULL
1555 * Sets, or unsets if @child is %NULL, the focused child of @container.
1557 * This function emits the GtkContainer::set_focus_child signal of
1558 * @container. Implementations of #GtkContainer can override the
1559 * default behaviour by overriding the class closure of this signal.
1562 gtk_container_set_focus_child (GtkContainer *container,
1565 g_return_if_fail (GTK_IS_CONTAINER (container));
1567 g_return_if_fail (GTK_IS_WIDGET (child));
1569 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1573 * gtk_container_get_focus_child:
1574 * @container: a #GtkContainer
1576 * Returns the current focus child widget inside @container.
1578 * Returns: The child widget which has the focus
1579 * inside @container, or %NULL if none is set.
1584 gtk_container_get_focus_child (GtkContainer *container)
1586 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1588 return container->focus_child;
1592 * gtk_container_get_children:
1593 * @container: a #GtkContainer
1595 * Returns the container's non-internal children. See
1596 * gtk_container_forall() for details on what constitutes an "internal" child.
1598 * Return value: a newly-allocated list of the container's non-internal children.
1601 gtk_container_get_children (GtkContainer *container)
1603 GList *children = NULL;
1605 gtk_container_foreach (container,
1606 gtk_container_children_callback,
1609 return g_list_reverse (children);
1613 gtk_container_child_position_callback (GtkWidget *widget,
1614 gpointer client_data)
1620 } *data = client_data;
1623 if (data->child == widget)
1624 data->index = data->i;
1628 gtk_container_child_default_composite_name (GtkContainer *container,
1638 /* fallback implementation */
1642 gtk_container_forall (container,
1643 gtk_container_child_position_callback,
1646 name = g_strdup_printf ("%s-%u",
1647 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1654 _gtk_container_child_composite_name (GtkContainer *container,
1657 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1658 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1659 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1661 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1663 static GQuark quark_composite_name = 0;
1666 if (!quark_composite_name)
1667 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1669 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1672 GtkContainerClass *class;
1674 class = GTK_CONTAINER_GET_CLASS (container);
1675 if (class->composite_name)
1676 name = class->composite_name (container, child);
1679 name = g_strdup (name);
1688 gtk_container_real_set_focus_child (GtkContainer *container,
1691 g_return_if_fail (GTK_IS_CONTAINER (container));
1692 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1694 if (child != container->focus_child)
1696 if (container->focus_child)
1697 g_object_unref (container->focus_child);
1698 container->focus_child = child;
1699 if (container->focus_child)
1700 g_object_ref (container->focus_child);
1704 /* check for h/v adjustments
1706 if (container->focus_child)
1708 GtkAdjustment *hadj;
1709 GtkAdjustment *vadj;
1710 GtkWidget *focus_child;
1713 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1714 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1718 focus_child = container->focus_child;
1719 while (GTK_IS_CONTAINER (focus_child) &&
1720 GTK_CONTAINER (focus_child)->focus_child)
1722 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1725 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1728 x += container->focus_child->allocation.x;
1729 y += container->focus_child->allocation.y;
1732 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1735 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1741 get_focus_chain (GtkContainer *container)
1743 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1746 /* same as gtk_container_get_children, except it includes internals
1749 gtk_container_get_all_children (GtkContainer *container)
1751 GList *children = NULL;
1753 gtk_container_forall (container,
1754 gtk_container_children_callback,
1761 gtk_container_focus (GtkWidget *widget,
1762 GtkDirectionType direction)
1765 GList *sorted_children;
1767 GtkContainer *container;
1769 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1771 container = GTK_CONTAINER (widget);
1775 if (GTK_WIDGET_CAN_FOCUS (container))
1777 if (!GTK_WIDGET_HAS_FOCUS (container))
1779 gtk_widget_grab_focus (GTK_WIDGET (container));
1785 /* Get a list of the containers children, allowing focus
1786 * chain to override.
1788 if (container->has_focus_chain)
1789 children = g_list_copy (get_focus_chain (container));
1791 children = gtk_container_get_all_children (container);
1793 if (container->has_focus_chain &&
1794 (direction == GTK_DIR_TAB_FORWARD ||
1795 direction == GTK_DIR_TAB_BACKWARD))
1797 sorted_children = g_list_copy (children);
1799 if (direction == GTK_DIR_TAB_BACKWARD)
1800 sorted_children = g_list_reverse (sorted_children);
1803 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1805 return_val = gtk_container_focus_move (container, sorted_children, direction);
1807 g_list_free (sorted_children);
1808 g_list_free (children);
1815 tab_compare (gconstpointer a,
1819 const GtkWidget *child1 = a;
1820 const GtkWidget *child2 = b;
1821 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1823 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1824 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1828 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1829 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1831 if (text_direction == GTK_TEXT_DIR_RTL)
1832 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1834 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1837 return (y1 < y2) ? -1 : 1;
1841 gtk_container_focus_sort_tab (GtkContainer *container,
1843 GtkDirectionType direction,
1844 GtkWidget *old_focus)
1846 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1847 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1849 /* if we are going backwards then reverse the order
1852 if (direction == GTK_DIR_TAB_BACKWARD)
1853 children = g_list_reverse (children);
1858 /* Get coordinates of @widget's allocation with respect to
1859 * allocation of @container.
1862 get_allocation_coords (GtkContainer *container,
1864 GdkRectangle *allocation)
1866 *allocation = widget->allocation;
1868 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1869 0, 0, &allocation->x, &allocation->y);
1872 /* Look for a child in @children that is intermediate between
1873 * the focus widget and container. This widget, if it exists,
1874 * acts as the starting widget for focus navigation.
1877 find_old_focus (GtkContainer *container,
1880 GList *tmp_list = children;
1883 GtkWidget *child = tmp_list->data;
1884 GtkWidget *widget = child;
1886 while (widget && widget != (GtkWidget *)container)
1888 GtkWidget *parent = widget->parent;
1889 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1898 tmp_list = tmp_list->next;
1905 old_focus_coords (GtkContainer *container,
1906 GdkRectangle *old_focus_rect)
1908 GtkWidget *widget = GTK_WIDGET (container);
1909 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1911 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1913 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1915 return get_allocation_coords (container, old_focus, old_focus_rect);
1921 typedef struct _CompareInfo CompareInfo;
1925 GtkContainer *container;
1932 up_down_compare (gconstpointer a,
1936 GdkRectangle allocation1;
1937 GdkRectangle allocation2;
1938 CompareInfo *compare = data;
1941 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1942 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1944 y1 = allocation1.y + allocation1.height / 2;
1945 y2 = allocation2.y + allocation2.height / 2;
1949 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1950 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1952 if (compare->reverse)
1953 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1955 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1958 return (y1 < y2) ? -1 : 1;
1962 gtk_container_focus_sort_up_down (GtkContainer *container,
1964 GtkDirectionType direction,
1965 GtkWidget *old_focus)
1967 CompareInfo compare;
1969 GdkRectangle old_allocation;
1971 compare.container = container;
1972 compare.reverse = (direction == GTK_DIR_UP);
1975 old_focus = find_old_focus (container, children);
1977 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1983 /* Delete widgets from list that don't match minimum criteria */
1985 compare_x1 = old_allocation.x;
1986 compare_x2 = old_allocation.x + old_allocation.width;
1988 if (direction == GTK_DIR_UP)
1989 compare_y = old_allocation.y;
1991 compare_y = old_allocation.y + old_allocation.height;
1993 tmp_list = children;
1996 GtkWidget *child = tmp_list->data;
1997 GList *next = tmp_list->next;
1998 gint child_x1, child_x2;
1999 GdkRectangle child_allocation;
2001 if (child != old_focus)
2003 if (get_allocation_coords (container, child, &child_allocation))
2005 child_x1 = child_allocation.x;
2006 child_x2 = child_allocation.x + child_allocation.width;
2008 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2009 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2010 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2012 children = g_list_delete_link (children, tmp_list);
2016 children = g_list_delete_link (children, tmp_list);
2022 compare.x = (compare_x1 + compare_x2) / 2;
2023 compare.y = old_allocation.y + old_allocation.height / 2;
2027 /* No old focus widget, need to figure out starting x,y some other way
2029 GtkWidget *widget = GTK_WIDGET (container);
2030 GdkRectangle old_focus_rect;
2032 if (old_focus_coords (container, &old_focus_rect))
2034 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2038 if (GTK_WIDGET_NO_WINDOW (widget))
2039 compare.x = widget->allocation.x + widget->allocation.width / 2;
2041 compare.x = widget->allocation.width / 2;
2044 if (GTK_WIDGET_NO_WINDOW (widget))
2045 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2047 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2050 children = g_list_sort_with_data (children, up_down_compare, &compare);
2052 if (compare.reverse)
2053 children = g_list_reverse (children);
2059 left_right_compare (gconstpointer a,
2063 GdkRectangle allocation1;
2064 GdkRectangle allocation2;
2065 CompareInfo *compare = data;
2068 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2069 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2071 x1 = allocation1.x + allocation1.width / 2;
2072 x2 = allocation2.x + allocation2.width / 2;
2076 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2077 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2079 if (compare->reverse)
2080 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2082 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2085 return (x1 < x2) ? -1 : 1;
2089 gtk_container_focus_sort_left_right (GtkContainer *container,
2091 GtkDirectionType direction,
2092 GtkWidget *old_focus)
2094 CompareInfo compare;
2096 GdkRectangle old_allocation;
2098 compare.container = container;
2099 compare.reverse = (direction == GTK_DIR_LEFT);
2102 old_focus = find_old_focus (container, children);
2104 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2110 /* Delete widgets from list that don't match minimum criteria */
2112 compare_y1 = old_allocation.y;
2113 compare_y2 = old_allocation.y + old_allocation.height;
2115 if (direction == GTK_DIR_LEFT)
2116 compare_x = old_allocation.x;
2118 compare_x = old_allocation.x + old_allocation.width;
2120 tmp_list = children;
2123 GtkWidget *child = tmp_list->data;
2124 GList *next = tmp_list->next;
2125 gint child_y1, child_y2;
2126 GdkRectangle child_allocation;
2128 if (child != old_focus)
2130 if (get_allocation_coords (container, child, &child_allocation))
2132 child_y1 = child_allocation.y;
2133 child_y2 = child_allocation.y + child_allocation.height;
2135 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2136 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2137 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2139 children = g_list_delete_link (children, tmp_list);
2143 children = g_list_delete_link (children, tmp_list);
2149 compare.y = (compare_y1 + compare_y2) / 2;
2150 compare.x = old_allocation.x + old_allocation.width / 2;
2154 /* No old focus widget, need to figure out starting x,y some other way
2156 GtkWidget *widget = GTK_WIDGET (container);
2157 GdkRectangle old_focus_rect;
2159 if (old_focus_coords (container, &old_focus_rect))
2161 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2165 if (GTK_WIDGET_NO_WINDOW (widget))
2166 compare.y = widget->allocation.y + widget->allocation.height / 2;
2168 compare.y = widget->allocation.height / 2;
2171 if (GTK_WIDGET_NO_WINDOW (widget))
2172 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2174 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2177 children = g_list_sort_with_data (children, left_right_compare, &compare);
2179 if (compare.reverse)
2180 children = g_list_reverse (children);
2186 * gtk_container_focus_sort:
2187 * @container: a #GtkContainer
2188 * @children: a list of descendents of @container (they don't
2189 * have to be direct children)
2190 * @direction: focus direction
2191 * @old_focus: widget to use for the starting position, or %NULL
2192 * to determine this automatically.
2193 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2194 * which is the only @direction we use currently,
2195 * so perhaps this argument should be removed)
2197 * Sorts @children in the correct order for focusing with
2198 * direction type @direction.
2200 * Return value: a copy of @children, sorted in correct focusing order,
2201 * with children that aren't suitable for focusing in this direction
2205 _gtk_container_focus_sort (GtkContainer *container,
2207 GtkDirectionType direction,
2208 GtkWidget *old_focus)
2210 GList *visible_children = NULL;
2214 if (GTK_WIDGET_REALIZED (children->data))
2215 visible_children = g_list_prepend (visible_children, children->data);
2216 children = children->next;
2221 case GTK_DIR_TAB_FORWARD:
2222 case GTK_DIR_TAB_BACKWARD:
2223 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2226 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2229 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2232 g_assert_not_reached ();
2238 gtk_container_focus_move (GtkContainer *container,
2240 GtkDirectionType direction)
2242 GtkWidget *focus_child;
2245 focus_child = container->focus_child;
2249 child = children->data;
2250 children = children->next;
2257 if (focus_child == child)
2261 if (gtk_widget_child_focus (child, direction))
2265 else if (GTK_WIDGET_DRAWABLE (child) &&
2266 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2268 if (gtk_widget_child_focus (child, direction))
2278 gtk_container_children_callback (GtkWidget *widget,
2279 gpointer client_data)
2283 children = (GList**) client_data;
2284 *children = g_list_prepend (*children, widget);
2288 chain_widget_destroyed (GtkWidget *widget,
2291 GtkContainer *container;
2294 container = GTK_CONTAINER (user_data);
2296 chain = g_object_get_data (G_OBJECT (container),
2297 "gtk-container-focus-chain");
2299 chain = g_list_remove (chain, widget);
2301 g_signal_handlers_disconnect_by_func (widget,
2302 chain_widget_destroyed,
2305 g_object_set_data (G_OBJECT (container),
2306 I_("gtk-container-focus-chain"),
2311 * gtk_container_set_focus_chain:
2312 * @container: a #GtkContainer
2313 * @focusable_widgets: the new focus chain
2315 * Sets a focus chain, overriding the one computed automatically by GTK+.
2317 * In principle each widget in the chain should be a descendant of the
2318 * container, but this is not enforced by this method, since it's allowed
2319 * to set the focus chain before you pack the widgets, or have a widget
2320 * in the chain that isn't always packed. The necessary checks are done
2321 * when the focus chain is actually traversed.
2324 gtk_container_set_focus_chain (GtkContainer *container,
2325 GList *focusable_widgets)
2330 g_return_if_fail (GTK_IS_CONTAINER (container));
2332 if (container->has_focus_chain)
2333 gtk_container_unset_focus_chain (container);
2335 container->has_focus_chain = TRUE;
2338 tmp_list = focusable_widgets;
2339 while (tmp_list != NULL)
2341 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2343 /* In principle each widget in the chain should be a descendant
2344 * of the container, but we don't want to check that here, it's
2345 * expensive and also it's allowed to set the focus chain before
2346 * you pack the widgets, or have a widget in the chain that isn't
2347 * always packed. So we check for ancestor during actual traversal.
2350 chain = g_list_prepend (chain, tmp_list->data);
2352 g_signal_connect (tmp_list->data,
2354 G_CALLBACK (chain_widget_destroyed),
2357 tmp_list = g_list_next (tmp_list);
2360 chain = g_list_reverse (chain);
2362 g_object_set_data (G_OBJECT (container),
2363 I_("gtk-container-focus-chain"),
2368 * gtk_container_get_focus_chain:
2369 * @container: a #GtkContainer
2370 * @focusable_widgets: location to store the focus chain of the
2371 * container, or %NULL. You should free this list
2372 * using g_list_free() when you are done with it, however
2373 * no additional reference count is added to the
2374 * individual widgets in the focus chain.
2376 * Retrieves the focus chain of the container, if one has been
2377 * set explicitly. If no focus chain has been explicitly
2378 * set, GTK+ computes the focus chain based on the positions
2379 * of the children. In that case, GTK+ stores %NULL in
2380 * @focusable_widgets and returns %FALSE.
2382 * Return value: %TRUE if the focus chain of the container
2383 * has been set explicitly.
2386 gtk_container_get_focus_chain (GtkContainer *container,
2387 GList **focus_chain)
2389 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2393 if (container->has_focus_chain)
2394 *focus_chain = g_list_copy (get_focus_chain (container));
2396 *focus_chain = NULL;
2399 return container->has_focus_chain;
2403 * gtk_container_unset_focus_chain:
2404 * @container: a #GtkContainer
2406 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2409 gtk_container_unset_focus_chain (GtkContainer *container)
2411 g_return_if_fail (GTK_IS_CONTAINER (container));
2413 if (container->has_focus_chain)
2418 chain = get_focus_chain (container);
2420 container->has_focus_chain = FALSE;
2422 g_object_set_data (G_OBJECT (container),
2423 I_("gtk-container-focus-chain"),
2427 while (tmp_list != NULL)
2429 g_signal_handlers_disconnect_by_func (tmp_list->data,
2430 chain_widget_destroyed,
2433 tmp_list = g_list_next (tmp_list);
2436 g_list_free (chain);
2441 * gtk_container_set_focus_vadjustment:
2442 * @container: a #GtkContainer
2443 * @adjustment: an adjustment which should be adjusted when the focus
2444 * is moved among the descendents of @container
2446 * Hooks up an adjustment to focus handling in a container, so when a
2447 * child of the container is focused, the adjustment is scrolled to
2448 * show that widget. This function sets the vertical alignment. See
2449 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2450 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2451 * the horizontal adjustment.
2453 * The adjustments have to be in pixel units and in the same coordinate
2454 * system as the allocation for immediate children of the container.
2457 gtk_container_set_focus_vadjustment (GtkContainer *container,
2458 GtkAdjustment *adjustment)
2460 g_return_if_fail (GTK_IS_CONTAINER (container));
2462 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2465 g_object_ref (adjustment);
2467 g_object_set_qdata_full (G_OBJECT (container),
2474 * gtk_container_get_focus_vadjustment:
2475 * @container: a #GtkContainer
2477 * Retrieves the vertical focus adjustment for the container. See
2478 * gtk_container_set_focus_vadjustment().
2480 * Return value: the vertical focus adjustment, or %NULL if
2481 * none has been set.
2484 gtk_container_get_focus_vadjustment (GtkContainer *container)
2486 GtkAdjustment *vadjustment;
2488 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2490 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2496 * gtk_container_set_focus_hadjustment:
2497 * @container: a #GtkContainer
2498 * @adjustment: an adjustment which should be adjusted when the focus is
2499 * moved among the descendents of @container
2501 * Hooks up an adjustment to focus handling in a container, so when a child
2502 * of the container is focused, the adjustment is scrolled to show that
2503 * widget. This function sets the horizontal alignment.
2504 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2505 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2506 * the vertical adjustment.
2508 * The adjustments have to be in pixel units and in the same coordinate
2509 * system as the allocation for immediate children of the container.
2512 gtk_container_set_focus_hadjustment (GtkContainer *container,
2513 GtkAdjustment *adjustment)
2515 g_return_if_fail (GTK_IS_CONTAINER (container));
2517 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2520 g_object_ref (adjustment);
2522 g_object_set_qdata_full (G_OBJECT (container),
2529 * gtk_container_get_focus_hadjustment:
2530 * @container: a #GtkContainer
2532 * Retrieves the horizontal focus adjustment for the container. See
2533 * gtk_container_set_focus_hadjustment ().
2535 * Return value: the horizontal focus adjustment, or %NULL if
2536 * none has been set.
2539 gtk_container_get_focus_hadjustment (GtkContainer *container)
2541 GtkAdjustment *hadjustment;
2543 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2545 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2552 gtk_container_show_all (GtkWidget *widget)
2554 g_return_if_fail (GTK_IS_CONTAINER (widget));
2556 gtk_container_foreach (GTK_CONTAINER (widget),
2557 (GtkCallback) gtk_widget_show_all,
2559 gtk_widget_show (widget);
2563 gtk_container_hide_all (GtkWidget *widget)
2565 g_return_if_fail (GTK_IS_CONTAINER (widget));
2567 gtk_widget_hide (widget);
2568 gtk_container_foreach (GTK_CONTAINER (widget),
2569 (GtkCallback) gtk_widget_hide_all,
2575 gtk_container_expose_child (GtkWidget *child,
2576 gpointer client_data)
2579 GtkWidget *container;
2580 GdkEventExpose *event;
2581 } *data = client_data;
2583 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2589 gtk_container_expose (GtkWidget *widget,
2590 GdkEventExpose *event)
2593 GtkWidget *container;
2594 GdkEventExpose *event;
2597 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2598 g_return_val_if_fail (event != NULL, FALSE);
2601 if (GTK_WIDGET_DRAWABLE (widget))
2603 data.container = widget;
2606 gtk_container_forall (GTK_CONTAINER (widget),
2607 gtk_container_expose_child,
2615 gtk_container_map_child (GtkWidget *child,
2616 gpointer client_data)
2618 if (GTK_WIDGET_VISIBLE (child) &&
2619 GTK_WIDGET_CHILD_VISIBLE (child) &&
2620 !GTK_WIDGET_MAPPED (child))
2621 gtk_widget_map (child);
2625 gtk_container_map (GtkWidget *widget)
2627 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2629 gtk_container_forall (GTK_CONTAINER (widget),
2630 gtk_container_map_child,
2633 if (!GTK_WIDGET_NO_WINDOW (widget))
2634 gdk_window_show (widget->window);
2638 gtk_container_unmap (GtkWidget *widget)
2640 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2642 if (!GTK_WIDGET_NO_WINDOW (widget))
2643 gdk_window_hide (widget->window);
2645 gtk_container_forall (GTK_CONTAINER (widget),
2646 (GtkCallback)gtk_widget_unmap,
2651 * gtk_container_propagate_expose:
2652 * @container: a #GtkContainer
2653 * @child: a child of @container
2654 * @event: a expose event sent to container
2656 * When a container receives an expose event, it must send synthetic
2657 * expose events to all children that don't have their own #GdkWindows.
2658 * This function provides a convenient way of doing this. A container,
2659 * when it receives an expose event, calls gtk_container_propagate_expose()
2660 * once for each child, passing in the event the container received.
2662 * gtk_container_propagate_expose() takes care of deciding whether
2663 * an expose event needs to be sent to the child, intersecting
2664 * the event's area with the child area, and sending the event.
2666 * In most cases, a container can simply either simply inherit the
2667 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2668 * and then chain to the ::expose implementation from #GtkContainer.
2671 gtk_container_propagate_expose (GtkContainer *container,
2673 GdkEventExpose *event)
2675 GdkEvent *child_event;
2677 g_return_if_fail (GTK_IS_CONTAINER (container));
2678 g_return_if_fail (GTK_IS_WIDGET (child));
2679 g_return_if_fail (event != NULL);
2681 g_assert (child->parent == GTK_WIDGET (container));
2683 if (GTK_WIDGET_DRAWABLE (child) &&
2684 GTK_WIDGET_NO_WINDOW (child) &&
2685 (child->window == event->window))
2687 child_event = gdk_event_new (GDK_EXPOSE);
2688 child_event->expose = *event;
2689 g_object_ref (child_event->expose.window);
2691 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2692 if (!gdk_region_empty (child_event->expose.region))
2694 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2695 gtk_widget_send_expose (child, child_event);
2697 gdk_event_free (child_event);
2701 #define __GTK_CONTAINER_C__
2702 #include "gtkaliasdef.c"