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,
310 g_return_if_fail (GTK_IS_WIDGET (child));
312 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
316 gtk_container_buildable_set_child_property (GtkContainer *container,
323 GValue gvalue = { 0, };
324 GError *error = NULL;
326 pspec = gtk_container_class_find_child_property
327 (G_OBJECT_GET_CLASS (container), name);
330 g_warning ("%s does not have a property called %s",
331 g_type_name (G_OBJECT_TYPE (container)), name);
335 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
337 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
338 g_type_name (G_OBJECT_TYPE (container)),
341 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
343 g_error_free (error);
347 gtk_container_child_set_property (container, child, name, &gvalue);
348 g_value_unset (&gvalue);
353 GtkContainer *container;
355 gchar *child_prop_name;
356 } PackingPropertiesData;
359 attributes_start_element (GMarkupParseContext *context,
360 const gchar *element_name,
362 const gchar **values,
366 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
369 if (strcmp (element_name, "property") == 0)
370 for (i = 0; names[i]; i++)
371 if (strcmp (names[i], "name") == 0)
372 parser_data->child_prop_name = g_strdup (values[i]);
373 else if (strcmp (element_name, "packing") == 0)
376 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
380 attributes_text_element (GMarkupParseContext *context,
386 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
388 if (!parser_data->child_prop_name)
391 gtk_container_buildable_set_child_property (parser_data->container,
392 parser_data->builder,
394 parser_data->child_prop_name,
397 g_free (parser_data->child_prop_name);
398 parser_data->child_prop_name = NULL;
401 static const GMarkupParser attributes_parser =
403 attributes_start_element,
405 attributes_text_element,
409 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
412 const gchar *tagname,
413 GMarkupParser *parser,
416 PackingPropertiesData *parser_data;
418 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
419 tagname, parser, data))
422 if (child && strcmp (tagname, "packing") == 0)
424 parser_data = g_slice_new0 (PackingPropertiesData);
425 parser_data->builder = builder;
426 parser_data->container = GTK_CONTAINER (buildable);
427 parser_data->child = GTK_WIDGET (child);
428 parser_data->child_prop_name = NULL;
430 *parser = attributes_parser;
439 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
442 const gchar *tagname,
445 if (strcmp (tagname, "packing") == 0)
447 g_slice_free (PackingPropertiesData, (gpointer)data);
452 if (parent_buildable_iface->custom_tag_end)
453 parent_buildable_iface->custom_tag_end (buildable, builder,
454 child, tagname, data);
459 * gtk_container_child_type:
460 * @container: a #GtkContainer
462 * Returns the type of the children supported by the container.
464 * Note that this may return %G_TYPE_NONE to indicate that no more
465 * children can be added, e.g. for a #GtkPaned which already has two
468 * Return value: a #GType.
471 gtk_container_child_type (GtkContainer *container)
474 GtkContainerClass *class;
476 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
478 class = GTK_CONTAINER_GET_CLASS (container);
479 if (class->child_type)
480 slot = class->child_type (container);
487 /* --- GtkContainer child property mechanism --- */
489 container_get_child_property (GtkContainer *container,
494 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
496 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
500 container_set_child_property (GtkContainer *container,
504 GObjectNotifyQueue *nqueue)
506 GValue tmp_value = { 0, };
507 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
509 /* provide a copy to work from, convert (if necessary) and validate */
510 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
511 if (!g_value_transform (value, &tmp_value))
512 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
514 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
515 G_VALUE_TYPE_NAME (value));
516 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
518 gchar *contents = g_strdup_value_contents (value);
520 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
522 G_VALUE_TYPE_NAME (value),
524 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
529 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
530 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
532 g_value_unset (&tmp_value);
536 * gtk_container_child_get_valist:
537 * @container: a #GtkContainer
538 * @child: a widget which is a child of @container
539 * @first_property_name: the name of the first property to get
540 * @var_args: return location for the first property, followed
541 * optionally by more name/return location pairs, followed by %NULL
543 * Gets the values of one or more child properties for @child and @container.
546 gtk_container_child_get_valist (GtkContainer *container,
548 const gchar *first_property_name,
553 g_return_if_fail (GTK_IS_CONTAINER (container));
554 g_return_if_fail (GTK_IS_WIDGET (child));
555 g_return_if_fail (child->parent == GTK_WIDGET (container));
557 g_object_ref (container);
558 g_object_ref (child);
560 name = first_property_name;
563 GValue value = { 0, };
567 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
569 G_OBJECT_TYPE (container),
573 g_warning ("%s: container class `%s' has no child property named `%s'",
575 G_OBJECT_TYPE_NAME (container),
579 if (!(pspec->flags & G_PARAM_READABLE))
581 g_warning ("%s: child property `%s' of container class `%s' is not readable",
584 G_OBJECT_TYPE_NAME (container));
587 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
588 container_get_child_property (container, child, pspec, &value);
589 G_VALUE_LCOPY (&value, var_args, 0, &error);
592 g_warning ("%s: %s", G_STRLOC, error);
594 g_value_unset (&value);
597 g_value_unset (&value);
598 name = va_arg (var_args, gchar*);
601 g_object_unref (child);
602 g_object_unref (container);
606 * gtk_container_child_get_property:
607 * @container: a #GtkContainer
608 * @child: a widget which is a child of @container
609 * @property_name: the name of the property to get
610 * @value: a location to return the value
612 * Gets the value of a child property for @child and @container.
615 gtk_container_child_get_property (GtkContainer *container,
617 const gchar *property_name,
622 g_return_if_fail (GTK_IS_CONTAINER (container));
623 g_return_if_fail (GTK_IS_WIDGET (child));
624 g_return_if_fail (child->parent == GTK_WIDGET (container));
625 g_return_if_fail (property_name != NULL);
626 g_return_if_fail (G_IS_VALUE (value));
628 g_object_ref (container);
629 g_object_ref (child);
630 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
631 G_OBJECT_TYPE (container), TRUE);
633 g_warning ("%s: container class `%s' has no child property named `%s'",
635 G_OBJECT_TYPE_NAME (container),
637 else if (!(pspec->flags & G_PARAM_READABLE))
638 g_warning ("%s: child property `%s' of container class `%s' is not readable",
641 G_OBJECT_TYPE_NAME (container));
644 GValue *prop_value, tmp_value = { 0, };
646 /* auto-conversion of the callers value type
648 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
650 g_value_reset (value);
653 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
655 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
657 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
658 G_VALUE_TYPE_NAME (value));
659 g_object_unref (child);
660 g_object_unref (container);
665 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
666 prop_value = &tmp_value;
668 container_get_child_property (container, child, pspec, prop_value);
669 if (prop_value != value)
671 g_value_transform (prop_value, value);
672 g_value_unset (&tmp_value);
675 g_object_unref (child);
676 g_object_unref (container);
680 * gtk_container_child_set_valist:
681 * @container: a #GtkContainer
682 * @child: a widget which is a child of @container
683 * @first_property_name: the name of the first property to set
684 * @var_args: a %NULL-terminated list of property names and values, starting
685 * with @first_prop_name
687 * Sets one or more child properties for @child and @container.
690 gtk_container_child_set_valist (GtkContainer *container,
692 const gchar *first_property_name,
695 GObjectNotifyQueue *nqueue;
698 g_return_if_fail (GTK_IS_CONTAINER (container));
699 g_return_if_fail (GTK_IS_WIDGET (child));
700 g_return_if_fail (child->parent == GTK_WIDGET (container));
702 g_object_ref (container);
703 g_object_ref (child);
705 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
706 name = first_property_name;
709 GValue value = { 0, };
711 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
713 G_OBJECT_TYPE (container),
717 g_warning ("%s: container class `%s' has no child property named `%s'",
719 G_OBJECT_TYPE_NAME (container),
723 if (!(pspec->flags & G_PARAM_WRITABLE))
725 g_warning ("%s: child property `%s' of container class `%s' is not writable",
728 G_OBJECT_TYPE_NAME (container));
731 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
732 G_VALUE_COLLECT (&value, var_args, 0, &error);
735 g_warning ("%s: %s", G_STRLOC, error);
738 /* we purposely leak the value here, it might not be
739 * in a sane state if an error condition occoured
743 container_set_child_property (container, child, pspec, &value, nqueue);
744 g_value_unset (&value);
745 name = va_arg (var_args, gchar*);
747 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
749 g_object_unref (container);
750 g_object_unref (child);
754 * gtk_container_child_set_property:
755 * @container: a #GtkContainer
756 * @child: a widget which is a child of @container
757 * @property_name: the name of the property to set
758 * @value: the value to set the property to
760 * Sets a child property for @child and @container.
763 gtk_container_child_set_property (GtkContainer *container,
765 const gchar *property_name,
768 GObjectNotifyQueue *nqueue;
771 g_return_if_fail (GTK_IS_CONTAINER (container));
772 g_return_if_fail (GTK_IS_WIDGET (child));
773 g_return_if_fail (child->parent == GTK_WIDGET (container));
774 g_return_if_fail (property_name != NULL);
775 g_return_if_fail (G_IS_VALUE (value));
777 g_object_ref (container);
778 g_object_ref (child);
780 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
781 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
782 G_OBJECT_TYPE (container), TRUE);
784 g_warning ("%s: container class `%s' has no child property named `%s'",
786 G_OBJECT_TYPE_NAME (container),
788 else if (!(pspec->flags & G_PARAM_WRITABLE))
789 g_warning ("%s: child property `%s' of container class `%s' is not writable",
792 G_OBJECT_TYPE_NAME (container));
795 container_set_child_property (container, child, pspec, value, nqueue);
797 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
798 g_object_unref (container);
799 g_object_unref (child);
803 * gtk_container_add_with_properties:
804 * @container: a #GtkContainer
805 * @widget: a widget to be placed inside @container
806 * @first_prop_name: the name of the first child property to set
807 * @Varargs: a %NULL-terminated list of property names and values, starting
808 * with @first_prop_name
810 * Adds @widget to @container, setting child properties at the same time.
811 * See gtk_container_add() and gtk_container_child_set() for more details.
814 gtk_container_add_with_properties (GtkContainer *container,
816 const gchar *first_prop_name,
819 g_return_if_fail (GTK_IS_CONTAINER (container));
820 g_return_if_fail (GTK_IS_WIDGET (widget));
821 g_return_if_fail (widget->parent == NULL);
822 g_return_if_fail (widget->parent == container);
824 g_object_ref (container);
825 g_object_ref (widget);
826 gtk_widget_freeze_child_notify (widget);
828 g_signal_emit (container, container_signals[ADD], 0, widget);
833 va_start (var_args, first_prop_name);
834 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
838 gtk_widget_thaw_child_notify (widget);
839 g_object_unref (widget);
840 g_object_unref (container);
844 * gtk_container_child_set:
845 * @container: a #GtkContainer
846 * @child: a widget which is a child of @container
847 * @first_prop_name: the name of the first property to set
848 * @Varargs: a %NULL-terminated list of property names and values, starting
849 * with @first_prop_name
851 * Sets one or more child properties for @child and @container.
854 gtk_container_child_set (GtkContainer *container,
856 const gchar *first_prop_name,
861 g_return_if_fail (GTK_IS_CONTAINER (container));
862 g_return_if_fail (GTK_IS_WIDGET (child));
863 g_return_if_fail (child->parent == GTK_WIDGET (container));
865 va_start (var_args, first_prop_name);
866 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
871 * gtk_container_child_get:
872 * @container: a #GtkContainer
873 * @child: a widget which is a child of @container
874 * @first_prop_name: the name of the first property to get
875 * @Varargs: return location for the first property, followed
876 * optionally by more name/return location pairs, followed by %NULL
878 * Gets the values of one or more child properties for @child and @container.
881 gtk_container_child_get (GtkContainer *container,
883 const gchar *first_prop_name,
888 g_return_if_fail (GTK_IS_CONTAINER (container));
889 g_return_if_fail (GTK_IS_WIDGET (child));
890 g_return_if_fail (child->parent == GTK_WIDGET (container));
892 va_start (var_args, first_prop_name);
893 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
898 * gtk_container_class_install_child_property:
899 * @cclass: a #GtkContainerClass
900 * @property_id: the id for the property
901 * @pspec: the #GParamSpec for the property
903 * Installs a child property on a container class.
906 gtk_container_class_install_child_property (GtkContainerClass *cclass,
910 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
911 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
912 if (pspec->flags & G_PARAM_WRITABLE)
913 g_return_if_fail (cclass->set_child_property != NULL);
914 if (pspec->flags & G_PARAM_READABLE)
915 g_return_if_fail (cclass->get_child_property != NULL);
916 g_return_if_fail (property_id > 0);
917 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
918 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
919 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
921 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
923 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
924 G_OBJECT_CLASS_NAME (cclass),
928 g_param_spec_ref (pspec);
929 g_param_spec_sink (pspec);
930 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
931 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
935 * gtk_container_class_find_child_property:
936 * @cclass: a #GtkContainerClass
937 * @property_name: the name of the child property to find
938 * @returns: the #GParamSpec of the child property or %NULL if @class has no
939 * child property with that name.
941 * Finds a child property of a container class by name.
944 gtk_container_class_find_child_property (GObjectClass *cclass,
945 const gchar *property_name)
947 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
948 g_return_val_if_fail (property_name != NULL, NULL);
950 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
952 G_OBJECT_CLASS_TYPE (cclass),
957 * gtk_container_class_list_child_properties:
958 * @cclass: a #GtkContainerClass
959 * @n_properties: location to return the number of child properties found
960 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
961 * The array must be freed with g_free().
963 * Returns all child properties of a container class.
966 gtk_container_class_list_child_properties (GObjectClass *cclass,
972 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
974 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
975 G_OBJECT_CLASS_TYPE (cclass),
984 gtk_container_add_unimplemented (GtkContainer *container,
987 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
991 gtk_container_remove_unimplemented (GtkContainer *container,
994 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
998 gtk_container_init (GtkContainer *container)
1000 container->focus_child = NULL;
1001 container->border_width = 0;
1002 container->need_resize = FALSE;
1003 container->resize_mode = GTK_RESIZE_PARENT;
1004 container->reallocate_redraws = FALSE;
1008 gtk_container_destroy (GtkObject *object)
1010 GtkContainer *container = GTK_CONTAINER (object);
1012 if (GTK_CONTAINER_RESIZE_PENDING (container))
1013 _gtk_container_dequeue_resize_handler (container);
1015 /* do this before walking child widgets, to avoid
1016 * removing children from focus chain one by one.
1018 if (container->has_focus_chain)
1019 gtk_container_unset_focus_chain (container);
1021 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1023 if (GTK_OBJECT_CLASS (parent_class)->destroy)
1024 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
1028 gtk_container_set_property (GObject *object,
1030 const GValue *value,
1033 GtkContainer *container = GTK_CONTAINER (object);
1037 case PROP_BORDER_WIDTH:
1038 gtk_container_set_border_width (container, g_value_get_uint (value));
1040 case PROP_RESIZE_MODE:
1041 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1044 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1047 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1053 gtk_container_get_property (GObject *object,
1058 GtkContainer *container = GTK_CONTAINER (object);
1062 case PROP_BORDER_WIDTH:
1063 g_value_set_uint (value, container->border_width);
1065 case PROP_RESIZE_MODE:
1066 g_value_set_enum (value, container->resize_mode);
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075 * gtk_container_set_border_width:
1076 * @container: a #GtkContainer
1077 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1078 * the container. Valid values are in the range 0-65535 pixels.
1080 * Sets the border width of the container.
1082 * The border width of a container is the amount of space to leave
1083 * around the outside of the container. The only exception to this is
1084 * #GtkWindow; because toplevel windows can't leave space outside,
1085 * they leave the space inside. The border is added on all sides of
1086 * the container. To add space to only one side, one approach is to
1087 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1088 * to give it a size, and place it on the side of the container as
1092 gtk_container_set_border_width (GtkContainer *container,
1095 g_return_if_fail (GTK_IS_CONTAINER (container));
1097 if (container->border_width != border_width)
1099 container->border_width = border_width;
1100 g_object_notify (G_OBJECT (container), "border-width");
1102 if (GTK_WIDGET_REALIZED (container))
1103 gtk_widget_queue_resize (GTK_WIDGET (container));
1108 * gtk_container_get_border_width:
1109 * @container: a #GtkContainer
1111 * Retrieves the border width of the container. See
1112 * gtk_container_set_border_width().
1114 * Return value: the current border width
1117 gtk_container_get_border_width (GtkContainer *container)
1119 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1121 return container->border_width;
1125 * gtk_container_add:
1126 * @container: a #GtkContainer
1127 * @widget: a widget to be placed inside @container
1129 * Adds @widget to @container. Typically used for simple containers
1130 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1131 * layout containers such as #GtkBox or #GtkTable, this function will
1132 * pick default packing parameters that may not be correct. So
1133 * consider functions such as gtk_box_pack_start() and
1134 * gtk_table_attach() as an alternative to gtk_container_add() in
1135 * those cases. A widget may be added to only one container at a time;
1136 * you can't place the same widget inside two different containers.
1139 gtk_container_add (GtkContainer *container,
1142 g_return_if_fail (GTK_IS_CONTAINER (container));
1143 g_return_if_fail (GTK_IS_WIDGET (widget));
1145 if (widget->parent != NULL)
1147 g_warning ("Attempting to add a widget with type %s to a container of "
1148 "type %s, but the widget is already inside a container of type %s, "
1149 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
1150 g_type_name (G_OBJECT_TYPE (widget)),
1151 g_type_name (G_OBJECT_TYPE (container)),
1152 g_type_name (G_OBJECT_TYPE (widget->parent)));
1156 g_signal_emit (container, container_signals[ADD], 0, widget);
1160 * gtk_container_remove:
1161 * @container: a #GtkContainer
1162 * @widget: a current child of @container
1164 * Removes @widget from @container. @widget must be inside @container.
1165 * Note that @container will own a reference to @widget, and that this
1166 * may be the last reference held; so removing a widget from its
1167 * container can destroy that widget. If you want to use @widget
1168 * again, you need to add a reference to it while it's not inside
1169 * a container, using g_object_ref(). If you don't want to use @widget
1170 * again it's usually more efficient to simply destroy it directly
1171 * using gtk_widget_destroy() since this will remove it from the
1172 * container and help break any circular reference count cycles.
1175 gtk_container_remove (GtkContainer *container,
1178 g_return_if_fail (GTK_IS_CONTAINER (container));
1179 g_return_if_fail (GTK_IS_WIDGET (widget));
1181 /* When using the deprecated API of the toolbar, it is possible
1182 * to legitimately call this function with a widget that is not
1183 * a direct child of the container.
1185 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1186 widget->parent == GTK_WIDGET (container));
1188 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1192 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1194 g_return_if_fail (GTK_IS_CONTAINER (container));
1195 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1197 container_resize_queue = g_slist_remove (container_resize_queue, container);
1198 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1202 * gtk_container_set_resize_mode:
1203 * @container: a #GtkContainer
1204 * @resize_mode: the new resize mode
1206 * Sets the resize mode for the container.
1208 * The resize mode of a container determines whether a resize request
1209 * will be passed to the container's parent, queued for later execution
1210 * or executed immediately.
1213 gtk_container_set_resize_mode (GtkContainer *container,
1214 GtkResizeMode resize_mode)
1216 g_return_if_fail (GTK_IS_CONTAINER (container));
1217 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1219 if (GTK_WIDGET_TOPLEVEL (container) &&
1220 resize_mode == GTK_RESIZE_PARENT)
1222 resize_mode = GTK_RESIZE_QUEUE;
1225 if (container->resize_mode != resize_mode)
1227 container->resize_mode = resize_mode;
1229 gtk_widget_queue_resize (GTK_WIDGET (container));
1230 g_object_notify (G_OBJECT (container), "resize-mode");
1235 * gtk_container_get_resize_mode:
1236 * @container: a #GtkContainer
1238 * Returns the resize mode for the container. See
1239 * gtk_container_set_resize_mode ().
1241 * Return value: the current resize mode
1244 gtk_container_get_resize_mode (GtkContainer *container)
1246 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1248 return container->resize_mode;
1252 * gtk_container_set_reallocate_redraws:
1253 * @container: a #GtkContainer
1254 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1256 * Sets the @reallocate_redraws flag of the container to the given value.
1258 * Containers requesting reallocation redraws get automatically
1259 * redrawn if any of their children changed allocation.
1262 gtk_container_set_reallocate_redraws (GtkContainer *container,
1263 gboolean needs_redraws)
1265 g_return_if_fail (GTK_IS_CONTAINER (container));
1267 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1270 static GtkContainer*
1271 gtk_container_get_resize_container (GtkContainer *container)
1273 GtkWidget *widget = GTK_WIDGET (container);
1275 while (widget->parent)
1277 widget = widget->parent;
1278 if (GTK_IS_RESIZE_CONTAINER (widget))
1282 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1286 gtk_container_idle_sizer (gpointer data)
1288 /* we may be invoked with a container_resize_queue of NULL, because
1289 * queue_resize could have been adding an extra idle function while
1290 * the queue still got processed. we better just ignore such case
1291 * than trying to explicitely work around them with some extra flags,
1292 * since it doesn't cause any actual harm.
1294 while (container_resize_queue)
1299 slist = container_resize_queue;
1300 container_resize_queue = slist->next;
1301 widget = slist->data;
1302 g_slist_free_1 (slist);
1304 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1305 gtk_container_check_resize (GTK_CONTAINER (widget));
1308 gdk_window_process_all_updates ();
1314 _gtk_container_queue_resize (GtkContainer *container)
1316 GtkContainer *resize_container;
1319 g_return_if_fail (GTK_IS_CONTAINER (container));
1321 widget = GTK_WIDGET (container);
1322 resize_container = gtk_container_get_resize_container (container);
1326 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1327 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1328 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1332 widget = widget->parent;
1335 if (resize_container)
1337 if (GTK_WIDGET_VISIBLE (resize_container) &&
1338 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1340 switch (resize_container->resize_mode)
1342 case GTK_RESIZE_QUEUE:
1343 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1345 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1346 if (container_resize_queue == NULL)
1347 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1348 gtk_container_idle_sizer,
1350 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1354 case GTK_RESIZE_IMMEDIATE:
1355 gtk_container_check_resize (resize_container);
1358 case GTK_RESIZE_PARENT:
1359 g_assert_not_reached ();
1365 /* we need to let hidden resize containers know that something
1366 * changed while they where hidden (currently only evaluated by
1369 resize_container->need_resize = TRUE;
1375 gtk_container_check_resize (GtkContainer *container)
1377 g_return_if_fail (GTK_IS_CONTAINER (container));
1379 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1383 gtk_container_real_check_resize (GtkContainer *container)
1385 GtkWidget *widget = GTK_WIDGET (container);
1386 GtkRequisition requisition;
1388 gtk_widget_size_request (widget, &requisition);
1390 if (requisition.width > widget->allocation.width ||
1391 requisition.height > widget->allocation.height)
1393 if (GTK_IS_RESIZE_CONTAINER (container))
1394 gtk_widget_size_allocate (GTK_WIDGET (container),
1395 >K_WIDGET (container)->allocation);
1397 gtk_widget_queue_resize (widget);
1401 gtk_container_resize_children (container);
1405 /* The container hasn't changed size but one of its children
1406 * queued a resize request. Which means that the allocation
1407 * is not sufficient for the requisition of some child.
1408 * We've already performed a size request at this point,
1409 * so we simply need to reallocate and let the allocation
1410 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1413 gtk_container_resize_children (GtkContainer *container)
1417 /* resizing invariants:
1418 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1419 * containers that have an idle sizer pending must be flagged with
1422 g_return_if_fail (GTK_IS_CONTAINER (container));
1424 widget = GTK_WIDGET (container);
1425 gtk_widget_size_allocate (widget, &widget->allocation);
1429 * gtk_container_forall:
1430 * @container: a #GtkContainer
1431 * @callback: a callback
1432 * @callback_data: callback user data
1434 * Invokes @callback on each child of @container, including children
1435 * that are considered "internal" (implementation details of the
1436 * container). "Internal" children generally weren't added by the user
1437 * of the container, but were added by the container implementation
1438 * itself. Most applications should use gtk_container_foreach(),
1439 * rather than gtk_container_forall().
1442 gtk_container_forall (GtkContainer *container,
1443 GtkCallback callback,
1444 gpointer callback_data)
1446 GtkContainerClass *class;
1448 g_return_if_fail (GTK_IS_CONTAINER (container));
1449 g_return_if_fail (callback != NULL);
1451 class = GTK_CONTAINER_GET_CLASS (container);
1454 class->forall (container, TRUE, callback, callback_data);
1458 * gtk_container_foreach:
1459 * @container: a #GtkContainer
1460 * @callback: a callback
1461 * @callback_data: callback user data
1463 * Invokes @callback on each non-internal child of @container. See
1464 * gtk_container_forall() for details on what constitutes an
1465 * "internal" child. Most applications should use
1466 * gtk_container_foreach(), rather than gtk_container_forall().
1469 gtk_container_foreach (GtkContainer *container,
1470 GtkCallback callback,
1471 gpointer callback_data)
1473 GtkContainerClass *class;
1475 g_return_if_fail (GTK_IS_CONTAINER (container));
1476 g_return_if_fail (callback != NULL);
1478 class = GTK_CONTAINER_GET_CLASS (container);
1481 class->forall (container, FALSE, callback, callback_data);
1484 typedef struct _GtkForeachData GtkForeachData;
1485 struct _GtkForeachData
1487 GtkObject *container;
1488 GtkCallbackMarshal callback;
1489 gpointer callback_data;
1493 gtk_container_foreach_unmarshal (GtkWidget *child,
1496 GtkForeachData *fdata = (GtkForeachData*) data;
1499 /* first argument */
1500 args[0].name = NULL;
1501 args[0].type = G_TYPE_FROM_INSTANCE (child);
1502 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1504 /* location for return value */
1505 args[1].name = NULL;
1506 args[1].type = G_TYPE_NONE;
1508 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1512 gtk_container_foreach_full (GtkContainer *container,
1513 GtkCallback callback,
1514 GtkCallbackMarshal marshal,
1515 gpointer callback_data,
1516 GDestroyNotify notify)
1518 g_return_if_fail (GTK_IS_CONTAINER (container));
1522 GtkForeachData fdata;
1524 fdata.container = GTK_OBJECT (container);
1525 fdata.callback = marshal;
1526 fdata.callback_data = callback_data;
1528 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1532 g_return_if_fail (callback != NULL);
1534 gtk_container_foreach (container, callback, &callback_data);
1538 notify (callback_data);
1542 * gtk_container_set_focus_child:
1543 * @container: a #GtkContainer
1544 * @widget: a #GtkWidget, or %NULL
1546 * Sets, or unsets if @widget is %NULL, the focused child of @container.
1548 * This function emits the GtkContainer::set_focus_child signal of
1549 * @container. Implementations of #GtkContainer can override the
1550 * default behaviour by overriding the class closure of this signal.
1553 gtk_container_set_focus_child (GtkContainer *container,
1556 g_return_if_fail (GTK_IS_CONTAINER (container));
1558 g_return_if_fail (GTK_IS_WIDGET (widget));
1560 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1564 * gtk_container_get_focus_child:
1565 * @container: a #GtkContainer
1567 * Returns the current focus child widget inside @container.
1569 * Returns: The child widget which has the focus
1570 * inside @container, or %NULL if none is set.
1575 gtk_container_get_focus_child (GtkContainer *container)
1577 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1579 return container->focus_child;
1583 * gtk_container_get_children:
1584 * @container: a #GtkContainer
1586 * Returns the container's non-internal children. See
1587 * gtk_container_forall() for details on what constitutes an "internal" child.
1589 * Return value: a newly-allocated list of the container's non-internal children.
1592 gtk_container_get_children (GtkContainer *container)
1594 GList *children = NULL;
1596 gtk_container_foreach (container,
1597 gtk_container_children_callback,
1600 return g_list_reverse (children);
1604 gtk_container_child_position_callback (GtkWidget *widget,
1605 gpointer client_data)
1611 } *data = client_data;
1614 if (data->child == widget)
1615 data->index = data->i;
1619 gtk_container_child_default_composite_name (GtkContainer *container,
1629 /* fallback implementation */
1633 gtk_container_forall (container,
1634 gtk_container_child_position_callback,
1637 name = g_strdup_printf ("%s-%u",
1638 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1645 _gtk_container_child_composite_name (GtkContainer *container,
1648 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1649 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1650 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1652 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1654 static GQuark quark_composite_name = 0;
1657 if (!quark_composite_name)
1658 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1660 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1663 GtkContainerClass *class;
1665 class = GTK_CONTAINER_GET_CLASS (container);
1666 if (class->composite_name)
1667 name = class->composite_name (container, child);
1670 name = g_strdup (name);
1679 gtk_container_real_set_focus_child (GtkContainer *container,
1682 g_return_if_fail (GTK_IS_CONTAINER (container));
1683 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1685 if (child != container->focus_child)
1687 if (container->focus_child)
1688 g_object_unref (container->focus_child);
1689 container->focus_child = child;
1690 if (container->focus_child)
1691 g_object_ref (container->focus_child);
1695 /* check for h/v adjustments
1697 if (container->focus_child)
1699 GtkAdjustment *hadj;
1700 GtkAdjustment *vadj;
1701 GtkWidget *focus_child;
1704 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1705 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1709 focus_child = container->focus_child;
1710 while (GTK_IS_CONTAINER (focus_child) &&
1711 GTK_CONTAINER (focus_child)->focus_child)
1713 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1716 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1719 x += container->focus_child->allocation.x;
1720 y += container->focus_child->allocation.y;
1723 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1726 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1732 get_focus_chain (GtkContainer *container)
1734 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1737 /* same as gtk_container_get_children, except it includes internals
1740 gtk_container_get_all_children (GtkContainer *container)
1742 GList *children = NULL;
1744 gtk_container_forall (container,
1745 gtk_container_children_callback,
1752 gtk_container_focus (GtkWidget *widget,
1753 GtkDirectionType direction)
1756 GList *sorted_children;
1758 GtkContainer *container;
1760 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1762 container = GTK_CONTAINER (widget);
1766 if (GTK_WIDGET_CAN_FOCUS (container))
1768 if (!GTK_WIDGET_HAS_FOCUS (container))
1770 gtk_widget_grab_focus (GTK_WIDGET (container));
1776 /* Get a list of the containers children, allowing focus
1777 * chain to override.
1779 if (container->has_focus_chain)
1780 children = g_list_copy (get_focus_chain (container));
1782 children = gtk_container_get_all_children (container);
1784 if (container->has_focus_chain &&
1785 (direction == GTK_DIR_TAB_FORWARD ||
1786 direction == GTK_DIR_TAB_BACKWARD))
1788 sorted_children = g_list_copy (children);
1790 if (direction == GTK_DIR_TAB_BACKWARD)
1791 sorted_children = g_list_reverse (sorted_children);
1794 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1796 return_val = gtk_container_focus_move (container, sorted_children, direction);
1798 g_list_free (sorted_children);
1799 g_list_free (children);
1806 tab_compare (gconstpointer a,
1810 const GtkWidget *child1 = a;
1811 const GtkWidget *child2 = b;
1812 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1814 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1815 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1819 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1820 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1822 if (text_direction == GTK_TEXT_DIR_RTL)
1823 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1825 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1828 return (y1 < y2) ? -1 : 1;
1832 gtk_container_focus_sort_tab (GtkContainer *container,
1834 GtkDirectionType direction,
1835 GtkWidget *old_focus)
1837 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1838 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1840 /* if we are going backwards then reverse the order
1843 if (direction == GTK_DIR_TAB_BACKWARD)
1844 children = g_list_reverse (children);
1849 /* Get coordinates of @widget's allocation with respect to
1850 * allocation of @container.
1853 get_allocation_coords (GtkContainer *container,
1855 GdkRectangle *allocation)
1857 *allocation = widget->allocation;
1859 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1860 0, 0, &allocation->x, &allocation->y);
1863 /* Look for a child in @children that is intermediate between
1864 * the focus widget and container. This widget, if it exists,
1865 * acts as the starting widget for focus navigation.
1868 find_old_focus (GtkContainer *container,
1871 GList *tmp_list = children;
1874 GtkWidget *child = tmp_list->data;
1875 GtkWidget *widget = child;
1877 while (widget && widget != (GtkWidget *)container)
1879 GtkWidget *parent = widget->parent;
1880 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1889 tmp_list = tmp_list->next;
1896 old_focus_coords (GtkContainer *container,
1897 GdkRectangle *old_focus_rect)
1899 GtkWidget *widget = GTK_WIDGET (container);
1900 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1902 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1904 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1906 return get_allocation_coords (container, old_focus, old_focus_rect);
1912 typedef struct _CompareInfo CompareInfo;
1916 GtkContainer *container;
1923 up_down_compare (gconstpointer a,
1927 GdkRectangle allocation1;
1928 GdkRectangle allocation2;
1929 CompareInfo *compare = data;
1932 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1933 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1935 y1 = allocation1.y + allocation1.height / 2;
1936 y2 = allocation2.y + allocation2.height / 2;
1940 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1941 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1943 if (compare->reverse)
1944 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1946 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1949 return (y1 < y2) ? -1 : 1;
1953 gtk_container_focus_sort_up_down (GtkContainer *container,
1955 GtkDirectionType direction,
1956 GtkWidget *old_focus)
1958 CompareInfo compare;
1960 GdkRectangle old_allocation;
1962 compare.container = container;
1963 compare.reverse = (direction == GTK_DIR_UP);
1966 old_focus = find_old_focus (container, children);
1968 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1974 /* Delete widgets from list that don't match minimum criteria */
1976 compare_x1 = old_allocation.x;
1977 compare_x2 = old_allocation.x + old_allocation.width;
1979 if (direction == GTK_DIR_UP)
1980 compare_y = old_allocation.y;
1982 compare_y = old_allocation.y + old_allocation.height;
1984 tmp_list = children;
1987 GtkWidget *child = tmp_list->data;
1988 GList *next = tmp_list->next;
1989 gint child_x1, child_x2;
1990 GdkRectangle child_allocation;
1992 if (child != old_focus)
1994 if (get_allocation_coords (container, child, &child_allocation))
1996 child_x1 = child_allocation.x;
1997 child_x2 = child_allocation.x + child_allocation.width;
1999 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2000 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2001 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2003 children = g_list_delete_link (children, tmp_list);
2007 children = g_list_delete_link (children, tmp_list);
2013 compare.x = (compare_x1 + compare_x2) / 2;
2014 compare.y = old_allocation.y + old_allocation.height / 2;
2018 /* No old focus widget, need to figure out starting x,y some other way
2020 GtkWidget *widget = GTK_WIDGET (container);
2021 GdkRectangle old_focus_rect;
2023 if (old_focus_coords (container, &old_focus_rect))
2025 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2029 if (GTK_WIDGET_NO_WINDOW (widget))
2030 compare.x = widget->allocation.x + widget->allocation.width / 2;
2032 compare.x = widget->allocation.width / 2;
2035 if (GTK_WIDGET_NO_WINDOW (widget))
2036 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2038 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2041 children = g_list_sort_with_data (children, up_down_compare, &compare);
2043 if (compare.reverse)
2044 children = g_list_reverse (children);
2050 left_right_compare (gconstpointer a,
2054 GdkRectangle allocation1;
2055 GdkRectangle allocation2;
2056 CompareInfo *compare = data;
2059 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2060 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2062 x1 = allocation1.x + allocation1.width / 2;
2063 x2 = allocation2.x + allocation2.width / 2;
2067 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2068 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2070 if (compare->reverse)
2071 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2073 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2076 return (x1 < x2) ? -1 : 1;
2080 gtk_container_focus_sort_left_right (GtkContainer *container,
2082 GtkDirectionType direction,
2083 GtkWidget *old_focus)
2085 CompareInfo compare;
2087 GdkRectangle old_allocation;
2089 compare.container = container;
2090 compare.reverse = (direction == GTK_DIR_LEFT);
2093 old_focus = find_old_focus (container, children);
2095 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2101 /* Delete widgets from list that don't match minimum criteria */
2103 compare_y1 = old_allocation.y;
2104 compare_y2 = old_allocation.y + old_allocation.height;
2106 if (direction == GTK_DIR_LEFT)
2107 compare_x = old_allocation.x;
2109 compare_x = old_allocation.x + old_allocation.width;
2111 tmp_list = children;
2114 GtkWidget *child = tmp_list->data;
2115 GList *next = tmp_list->next;
2116 gint child_y1, child_y2;
2117 GdkRectangle child_allocation;
2119 if (child != old_focus)
2121 if (get_allocation_coords (container, child, &child_allocation))
2123 child_y1 = child_allocation.y;
2124 child_y2 = child_allocation.y + child_allocation.height;
2126 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2127 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2128 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2130 children = g_list_delete_link (children, tmp_list);
2134 children = g_list_delete_link (children, tmp_list);
2140 compare.y = (compare_y1 + compare_y2) / 2;
2141 compare.x = old_allocation.x + old_allocation.width / 2;
2145 /* No old focus widget, need to figure out starting x,y some other way
2147 GtkWidget *widget = GTK_WIDGET (container);
2148 GdkRectangle old_focus_rect;
2150 if (old_focus_coords (container, &old_focus_rect))
2152 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2156 if (GTK_WIDGET_NO_WINDOW (widget))
2157 compare.y = widget->allocation.y + widget->allocation.height / 2;
2159 compare.y = widget->allocation.height / 2;
2162 if (GTK_WIDGET_NO_WINDOW (widget))
2163 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2165 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2168 children = g_list_sort_with_data (children, left_right_compare, &compare);
2170 if (compare.reverse)
2171 children = g_list_reverse (children);
2177 * gtk_container_focus_sort:
2178 * @container: a #GtkContainer
2179 * @children: a list of descendents of @container (they don't
2180 * have to be direct children)
2181 * @direction: focus direction
2182 * @old_focus: widget to use for the starting position, or %NULL
2183 * to determine this automatically.
2184 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2185 * which is the only @direction we use currently,
2186 * so perhaps this argument should be removed)
2188 * Sorts @children in the correct order for focusing with
2189 * direction type @direction.
2191 * Return value: a copy of @children, sorted in correct focusing order,
2192 * with children that aren't suitable for focusing in this direction
2196 _gtk_container_focus_sort (GtkContainer *container,
2198 GtkDirectionType direction,
2199 GtkWidget *old_focus)
2201 GList *visible_children = NULL;
2205 if (GTK_WIDGET_REALIZED (children->data))
2206 visible_children = g_list_prepend (visible_children, children->data);
2207 children = children->next;
2212 case GTK_DIR_TAB_FORWARD:
2213 case GTK_DIR_TAB_BACKWARD:
2214 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2217 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2220 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2223 g_assert_not_reached ();
2229 gtk_container_focus_move (GtkContainer *container,
2231 GtkDirectionType direction)
2233 GtkWidget *focus_child;
2236 focus_child = container->focus_child;
2240 child = children->data;
2241 children = children->next;
2248 if (focus_child == child)
2252 if (gtk_widget_child_focus (child, direction))
2256 else if (GTK_WIDGET_DRAWABLE (child) &&
2257 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2259 if (gtk_widget_child_focus (child, direction))
2269 gtk_container_children_callback (GtkWidget *widget,
2270 gpointer client_data)
2274 children = (GList**) client_data;
2275 *children = g_list_prepend (*children, widget);
2279 chain_widget_destroyed (GtkWidget *widget,
2282 GtkContainer *container;
2285 container = GTK_CONTAINER (user_data);
2287 chain = g_object_get_data (G_OBJECT (container),
2288 "gtk-container-focus-chain");
2290 chain = g_list_remove (chain, widget);
2292 g_signal_handlers_disconnect_by_func (widget,
2293 chain_widget_destroyed,
2296 g_object_set_data (G_OBJECT (container),
2297 I_("gtk-container-focus-chain"),
2302 * gtk_container_set_focus_chain:
2303 * @container: a #GtkContainer
2304 * @focusable_widgets: the new focus chain
2306 * Sets a focus chain, overriding the one computed automatically by GTK+.
2308 * In principle each widget in the chain should be a descendant of the
2309 * container, but this is not enforced by this method, since it's allowed
2310 * to set the focus chain before you pack the widgets, or have a widget
2311 * in the chain that isn't always packed. The necessary checks are done
2312 * when the focus chain is actually traversed.
2315 gtk_container_set_focus_chain (GtkContainer *container,
2316 GList *focusable_widgets)
2321 g_return_if_fail (GTK_IS_CONTAINER (container));
2323 if (container->has_focus_chain)
2324 gtk_container_unset_focus_chain (container);
2326 container->has_focus_chain = TRUE;
2329 tmp_list = focusable_widgets;
2330 while (tmp_list != NULL)
2332 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2334 /* In principle each widget in the chain should be a descendant
2335 * of the container, but we don't want to check that here, it's
2336 * expensive and also it's allowed to set the focus chain before
2337 * you pack the widgets, or have a widget in the chain that isn't
2338 * always packed. So we check for ancestor during actual traversal.
2341 chain = g_list_prepend (chain, tmp_list->data);
2343 g_signal_connect (tmp_list->data,
2345 G_CALLBACK (chain_widget_destroyed),
2348 tmp_list = g_list_next (tmp_list);
2351 chain = g_list_reverse (chain);
2353 g_object_set_data (G_OBJECT (container),
2354 I_("gtk-container-focus-chain"),
2359 * gtk_container_get_focus_chain:
2360 * @container: a #GtkContainer
2361 * @focusable_widgets: location to store the focus chain of the
2362 * container, or %NULL. You should free this list
2363 * using g_list_free() when you are done with it, however
2364 * no additional reference count is added to the
2365 * individual widgets in the focus chain.
2367 * Retrieves the focus chain of the container, if one has been
2368 * set explicitly. If no focus chain has been explicitly
2369 * set, GTK+ computes the focus chain based on the positions
2370 * of the children. In that case, GTK+ stores %NULL in
2371 * @focusable_widgets and returns %FALSE.
2373 * Return value: %TRUE if the focus chain of the container
2374 * has been set explicitly.
2377 gtk_container_get_focus_chain (GtkContainer *container,
2378 GList **focus_chain)
2380 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2384 if (container->has_focus_chain)
2385 *focus_chain = g_list_copy (get_focus_chain (container));
2387 *focus_chain = NULL;
2390 return container->has_focus_chain;
2394 * gtk_container_unset_focus_chain:
2395 * @container: a #GtkContainer
2397 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2400 gtk_container_unset_focus_chain (GtkContainer *container)
2402 g_return_if_fail (GTK_IS_CONTAINER (container));
2404 if (container->has_focus_chain)
2409 chain = get_focus_chain (container);
2411 container->has_focus_chain = FALSE;
2413 g_object_set_data (G_OBJECT (container),
2414 I_("gtk-container-focus-chain"),
2418 while (tmp_list != NULL)
2420 g_signal_handlers_disconnect_by_func (tmp_list->data,
2421 chain_widget_destroyed,
2424 tmp_list = g_list_next (tmp_list);
2427 g_list_free (chain);
2432 * gtk_container_set_focus_vadjustment:
2433 * @container: a #GtkContainer
2434 * @adjustment: an adjustment which should be adjusted when the focus
2435 * is moved among the descendents of @container
2437 * Hooks up an adjustment to focus handling in a container, so when a
2438 * child of the container is focused, the adjustment is scrolled to
2439 * show that widget. This function sets the vertical alignment. See
2440 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2441 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2442 * the horizontal adjustment.
2444 * The adjustments have to be in pixel units and in the same coordinate
2445 * system as the allocation for immediate children of the container.
2448 gtk_container_set_focus_vadjustment (GtkContainer *container,
2449 GtkAdjustment *adjustment)
2451 g_return_if_fail (GTK_IS_CONTAINER (container));
2453 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2456 g_object_ref (adjustment);
2458 g_object_set_qdata_full (G_OBJECT (container),
2465 * gtk_container_get_focus_vadjustment:
2466 * @container: a #GtkContainer
2468 * Retrieves the vertical focus adjustment for the container. See
2469 * gtk_container_set_focus_vadjustment().
2471 * Return value: the vertical focus adjustment, or %NULL if
2472 * none has been set.
2475 gtk_container_get_focus_vadjustment (GtkContainer *container)
2477 GtkAdjustment *vadjustment;
2479 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2481 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2487 * gtk_container_set_focus_hadjustment:
2488 * @container: a #GtkContainer
2489 * @adjustment: an adjustment which should be adjusted when the focus is
2490 * moved among the descendents of @container
2492 * Hooks up an adjustment to focus handling in a container, so when a child
2493 * of the container is focused, the adjustment is scrolled to show that
2494 * widget. This function sets the horizontal alignment.
2495 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2496 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2497 * the vertical adjustment.
2499 * The adjustments have to be in pixel units and in the same coordinate
2500 * system as the allocation for immediate children of the container.
2503 gtk_container_set_focus_hadjustment (GtkContainer *container,
2504 GtkAdjustment *adjustment)
2506 g_return_if_fail (GTK_IS_CONTAINER (container));
2508 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2511 g_object_ref (adjustment);
2513 g_object_set_qdata_full (G_OBJECT (container),
2520 * gtk_container_get_focus_hadjustment:
2521 * @container: a #GtkContainer
2523 * Retrieves the horizontal focus adjustment for the container. See
2524 * gtk_container_set_focus_hadjustment ().
2526 * Return value: the horizontal focus adjustment, or %NULL if
2527 * none has been set.
2530 gtk_container_get_focus_hadjustment (GtkContainer *container)
2532 GtkAdjustment *hadjustment;
2534 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2536 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2543 gtk_container_show_all (GtkWidget *widget)
2545 g_return_if_fail (GTK_IS_CONTAINER (widget));
2547 gtk_container_foreach (GTK_CONTAINER (widget),
2548 (GtkCallback) gtk_widget_show_all,
2550 gtk_widget_show (widget);
2554 gtk_container_hide_all (GtkWidget *widget)
2556 g_return_if_fail (GTK_IS_CONTAINER (widget));
2558 gtk_widget_hide (widget);
2559 gtk_container_foreach (GTK_CONTAINER (widget),
2560 (GtkCallback) gtk_widget_hide_all,
2566 gtk_container_expose_child (GtkWidget *child,
2567 gpointer client_data)
2570 GtkWidget *container;
2571 GdkEventExpose *event;
2572 } *data = client_data;
2574 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2580 gtk_container_expose (GtkWidget *widget,
2581 GdkEventExpose *event)
2584 GtkWidget *container;
2585 GdkEventExpose *event;
2588 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2589 g_return_val_if_fail (event != NULL, FALSE);
2592 if (GTK_WIDGET_DRAWABLE (widget))
2594 data.container = widget;
2597 gtk_container_forall (GTK_CONTAINER (widget),
2598 gtk_container_expose_child,
2606 gtk_container_map_child (GtkWidget *child,
2607 gpointer client_data)
2609 if (GTK_WIDGET_VISIBLE (child) &&
2610 GTK_WIDGET_CHILD_VISIBLE (child) &&
2611 !GTK_WIDGET_MAPPED (child))
2612 gtk_widget_map (child);
2616 gtk_container_map (GtkWidget *widget)
2618 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2620 gtk_container_forall (GTK_CONTAINER (widget),
2621 gtk_container_map_child,
2624 if (!GTK_WIDGET_NO_WINDOW (widget))
2625 gdk_window_show (widget->window);
2629 gtk_container_unmap (GtkWidget *widget)
2631 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2633 if (!GTK_WIDGET_NO_WINDOW (widget))
2634 gdk_window_hide (widget->window);
2636 gtk_container_forall (GTK_CONTAINER (widget),
2637 (GtkCallback)gtk_widget_unmap,
2642 * gtk_container_propagate_expose:
2643 * @container: a #GtkContainer
2644 * @child: a child of @container
2645 * @event: a expose event sent to container
2647 * When a container receives an expose event, it must send synthetic
2648 * expose events to all children that don't have their own #GdkWindows.
2649 * This function provides a convenient way of doing this. A container,
2650 * when it receives an expose event, calls gtk_container_propagate_expose()
2651 * once for each child, passing in the event the container received.
2653 * gtk_container_propagate_expose() takes care of deciding whether
2654 * an expose event needs to be sent to the child, intersecting
2655 * the event's area with the child area, and sending the event.
2657 * In most cases, a container can simply either simply inherit the
2658 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2659 * and then chain to the ::expose implementation from #GtkContainer.
2662 gtk_container_propagate_expose (GtkContainer *container,
2664 GdkEventExpose *event)
2666 GdkEvent *child_event;
2668 g_return_if_fail (GTK_IS_CONTAINER (container));
2669 g_return_if_fail (GTK_IS_WIDGET (child));
2670 g_return_if_fail (event != NULL);
2672 g_assert (child->parent == GTK_WIDGET (container));
2674 if (GTK_WIDGET_DRAWABLE (child) &&
2675 GTK_WIDGET_NO_WINDOW (child) &&
2676 (child->window == event->window))
2678 child_event = gdk_event_new (GDK_EXPOSE);
2679 child_event->expose = *event;
2680 g_object_ref (child_event->expose.window);
2682 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2683 if (!gdk_region_empty (child_event->expose.region))
2685 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2686 gtk_widget_send_expose (child, child_event);
2688 gdk_event_free (child_event);
2692 #define __GTK_CONTAINER_C__
2693 #include "gtkaliasdef.c"