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)
371 for (i = 0; names[i]; i++)
372 if (strcmp (names[i], "name") == 0)
373 parser_data->child_prop_name = g_strdup (values[i]);
375 else if (strcmp (element_name, "packing") == 0)
378 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
382 attributes_text_element (GMarkupParseContext *context,
388 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
390 if (!parser_data->child_prop_name)
393 gtk_container_buildable_set_child_property (parser_data->container,
394 parser_data->builder,
396 parser_data->child_prop_name,
399 g_free (parser_data->child_prop_name);
400 parser_data->child_prop_name = NULL;
403 static const GMarkupParser attributes_parser =
405 attributes_start_element,
407 attributes_text_element,
411 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
414 const gchar *tagname,
415 GMarkupParser *parser,
418 PackingPropertiesData *parser_data;
420 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
421 tagname, parser, data))
424 if (child && strcmp (tagname, "packing") == 0)
426 parser_data = g_slice_new0 (PackingPropertiesData);
427 parser_data->builder = builder;
428 parser_data->container = GTK_CONTAINER (buildable);
429 parser_data->child = GTK_WIDGET (child);
430 parser_data->child_prop_name = NULL;
432 *parser = attributes_parser;
441 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
444 const gchar *tagname,
447 if (strcmp (tagname, "packing") == 0)
449 g_slice_free (PackingPropertiesData, (gpointer)data);
454 if (parent_buildable_iface->custom_tag_end)
455 parent_buildable_iface->custom_tag_end (buildable, builder,
456 child, tagname, data);
461 * gtk_container_child_type:
462 * @container: a #GtkContainer
464 * Returns the type of the children supported by the container.
466 * Note that this may return %G_TYPE_NONE to indicate that no more
467 * children can be added, e.g. for a #GtkPaned which already has two
470 * Return value: a #GType.
473 gtk_container_child_type (GtkContainer *container)
476 GtkContainerClass *class;
478 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
480 class = GTK_CONTAINER_GET_CLASS (container);
481 if (class->child_type)
482 slot = class->child_type (container);
489 /* --- GtkContainer child property mechanism --- */
491 container_get_child_property (GtkContainer *container,
496 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
498 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
502 container_set_child_property (GtkContainer *container,
506 GObjectNotifyQueue *nqueue)
508 GValue tmp_value = { 0, };
509 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
511 /* provide a copy to work from, convert (if necessary) and validate */
512 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
513 if (!g_value_transform (value, &tmp_value))
514 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
516 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
517 G_VALUE_TYPE_NAME (value));
518 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
520 gchar *contents = g_strdup_value_contents (value);
522 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
524 G_VALUE_TYPE_NAME (value),
526 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
531 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
532 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
534 g_value_unset (&tmp_value);
538 * gtk_container_child_get_valist:
539 * @container: a #GtkContainer
540 * @child: a widget which is a child of @container
541 * @first_property_name: the name of the first property to get
542 * @var_args: return location for the first property, followed
543 * optionally by more name/return location pairs, followed by %NULL
545 * Gets the values of one or more child properties for @child and @container.
548 gtk_container_child_get_valist (GtkContainer *container,
550 const gchar *first_property_name,
555 g_return_if_fail (GTK_IS_CONTAINER (container));
556 g_return_if_fail (GTK_IS_WIDGET (child));
557 g_return_if_fail (child->parent == GTK_WIDGET (container));
559 g_object_ref (container);
560 g_object_ref (child);
562 name = first_property_name;
565 GValue value = { 0, };
569 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
571 G_OBJECT_TYPE (container),
575 g_warning ("%s: container class `%s' has no child property named `%s'",
577 G_OBJECT_TYPE_NAME (container),
581 if (!(pspec->flags & G_PARAM_READABLE))
583 g_warning ("%s: child property `%s' of container class `%s' is not readable",
586 G_OBJECT_TYPE_NAME (container));
589 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
590 container_get_child_property (container, child, pspec, &value);
591 G_VALUE_LCOPY (&value, var_args, 0, &error);
594 g_warning ("%s: %s", G_STRLOC, error);
596 g_value_unset (&value);
599 g_value_unset (&value);
600 name = va_arg (var_args, gchar*);
603 g_object_unref (child);
604 g_object_unref (container);
608 * gtk_container_child_get_property:
609 * @container: a #GtkContainer
610 * @child: a widget which is a child of @container
611 * @property_name: the name of the property to get
612 * @value: a location to return the value
614 * Gets the value of a child property for @child and @container.
617 gtk_container_child_get_property (GtkContainer *container,
619 const gchar *property_name,
624 g_return_if_fail (GTK_IS_CONTAINER (container));
625 g_return_if_fail (GTK_IS_WIDGET (child));
626 g_return_if_fail (child->parent == GTK_WIDGET (container));
627 g_return_if_fail (property_name != NULL);
628 g_return_if_fail (G_IS_VALUE (value));
630 g_object_ref (container);
631 g_object_ref (child);
632 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
633 G_OBJECT_TYPE (container), TRUE);
635 g_warning ("%s: container class `%s' has no child property named `%s'",
637 G_OBJECT_TYPE_NAME (container),
639 else if (!(pspec->flags & G_PARAM_READABLE))
640 g_warning ("%s: child property `%s' of container class `%s' is not readable",
643 G_OBJECT_TYPE_NAME (container));
646 GValue *prop_value, tmp_value = { 0, };
648 /* auto-conversion of the callers value type
650 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
652 g_value_reset (value);
655 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
657 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
659 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
660 G_VALUE_TYPE_NAME (value));
661 g_object_unref (child);
662 g_object_unref (container);
667 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
668 prop_value = &tmp_value;
670 container_get_child_property (container, child, pspec, prop_value);
671 if (prop_value != value)
673 g_value_transform (prop_value, value);
674 g_value_unset (&tmp_value);
677 g_object_unref (child);
678 g_object_unref (container);
682 * gtk_container_child_set_valist:
683 * @container: a #GtkContainer
684 * @child: a widget which is a child of @container
685 * @first_property_name: the name of the first property to set
686 * @var_args: a %NULL-terminated list of property names and values, starting
687 * with @first_prop_name
689 * Sets one or more child properties for @child and @container.
692 gtk_container_child_set_valist (GtkContainer *container,
694 const gchar *first_property_name,
697 GObjectNotifyQueue *nqueue;
700 g_return_if_fail (GTK_IS_CONTAINER (container));
701 g_return_if_fail (GTK_IS_WIDGET (child));
702 g_return_if_fail (child->parent == GTK_WIDGET (container));
704 g_object_ref (container);
705 g_object_ref (child);
707 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
708 name = first_property_name;
711 GValue value = { 0, };
713 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
715 G_OBJECT_TYPE (container),
719 g_warning ("%s: container class `%s' has no child property named `%s'",
721 G_OBJECT_TYPE_NAME (container),
725 if (!(pspec->flags & G_PARAM_WRITABLE))
727 g_warning ("%s: child property `%s' of container class `%s' is not writable",
730 G_OBJECT_TYPE_NAME (container));
733 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
734 G_VALUE_COLLECT (&value, var_args, 0, &error);
737 g_warning ("%s: %s", G_STRLOC, error);
740 /* we purposely leak the value here, it might not be
741 * in a sane state if an error condition occoured
745 container_set_child_property (container, child, pspec, &value, nqueue);
746 g_value_unset (&value);
747 name = va_arg (var_args, gchar*);
749 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
751 g_object_unref (container);
752 g_object_unref (child);
756 * gtk_container_child_set_property:
757 * @container: a #GtkContainer
758 * @child: a widget which is a child of @container
759 * @property_name: the name of the property to set
760 * @value: the value to set the property to
762 * Sets a child property for @child and @container.
765 gtk_container_child_set_property (GtkContainer *container,
767 const gchar *property_name,
770 GObjectNotifyQueue *nqueue;
773 g_return_if_fail (GTK_IS_CONTAINER (container));
774 g_return_if_fail (GTK_IS_WIDGET (child));
775 g_return_if_fail (child->parent == GTK_WIDGET (container));
776 g_return_if_fail (property_name != NULL);
777 g_return_if_fail (G_IS_VALUE (value));
779 g_object_ref (container);
780 g_object_ref (child);
782 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
783 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
784 G_OBJECT_TYPE (container), TRUE);
786 g_warning ("%s: container class `%s' has no child property named `%s'",
788 G_OBJECT_TYPE_NAME (container),
790 else if (!(pspec->flags & G_PARAM_WRITABLE))
791 g_warning ("%s: child property `%s' of container class `%s' is not writable",
794 G_OBJECT_TYPE_NAME (container));
797 container_set_child_property (container, child, pspec, value, nqueue);
799 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
800 g_object_unref (container);
801 g_object_unref (child);
805 * gtk_container_add_with_properties:
806 * @container: a #GtkContainer
807 * @widget: a widget to be placed inside @container
808 * @first_prop_name: the name of the first child property to set
809 * @Varargs: a %NULL-terminated list of property names and values, starting
810 * with @first_prop_name
812 * Adds @widget to @container, setting child properties at the same time.
813 * See gtk_container_add() and gtk_container_child_set() for more details.
816 gtk_container_add_with_properties (GtkContainer *container,
818 const gchar *first_prop_name,
821 g_return_if_fail (GTK_IS_CONTAINER (container));
822 g_return_if_fail (GTK_IS_WIDGET (widget));
823 g_return_if_fail (widget->parent == NULL);
825 g_object_ref (container);
826 g_object_ref (widget);
827 gtk_widget_freeze_child_notify (widget);
829 g_signal_emit (container, container_signals[ADD], 0, widget);
834 va_start (var_args, first_prop_name);
835 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
839 gtk_widget_thaw_child_notify (widget);
840 g_object_unref (widget);
841 g_object_unref (container);
845 * gtk_container_child_set:
846 * @container: a #GtkContainer
847 * @child: a widget which is a child of @container
848 * @first_prop_name: the name of the first property to set
849 * @Varargs: a %NULL-terminated list of property names and values, starting
850 * with @first_prop_name
852 * Sets one or more child properties for @child and @container.
855 gtk_container_child_set (GtkContainer *container,
857 const gchar *first_prop_name,
862 g_return_if_fail (GTK_IS_CONTAINER (container));
863 g_return_if_fail (GTK_IS_WIDGET (child));
864 g_return_if_fail (child->parent == GTK_WIDGET (container));
866 va_start (var_args, first_prop_name);
867 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
872 * gtk_container_child_get:
873 * @container: a #GtkContainer
874 * @child: a widget which is a child of @container
875 * @first_prop_name: the name of the first property to get
876 * @Varargs: return location for the first property, followed
877 * optionally by more name/return location pairs, followed by %NULL
879 * Gets the values of one or more child properties for @child and @container.
882 gtk_container_child_get (GtkContainer *container,
884 const gchar *first_prop_name,
889 g_return_if_fail (GTK_IS_CONTAINER (container));
890 g_return_if_fail (GTK_IS_WIDGET (child));
891 g_return_if_fail (child->parent == GTK_WIDGET (container));
893 va_start (var_args, first_prop_name);
894 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
899 * gtk_container_class_install_child_property:
900 * @cclass: a #GtkContainerClass
901 * @property_id: the id for the property
902 * @pspec: the #GParamSpec for the property
904 * Installs a child property on a container class.
907 gtk_container_class_install_child_property (GtkContainerClass *cclass,
911 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
912 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
913 if (pspec->flags & G_PARAM_WRITABLE)
914 g_return_if_fail (cclass->set_child_property != NULL);
915 if (pspec->flags & G_PARAM_READABLE)
916 g_return_if_fail (cclass->get_child_property != NULL);
917 g_return_if_fail (property_id > 0);
918 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
919 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
920 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
922 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
924 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
925 G_OBJECT_CLASS_NAME (cclass),
929 g_param_spec_ref (pspec);
930 g_param_spec_sink (pspec);
931 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
932 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
936 * gtk_container_class_find_child_property:
937 * @cclass: a #GtkContainerClass
938 * @property_name: the name of the child property to find
939 * @returns: the #GParamSpec of the child property or %NULL if @class has no
940 * child property with that name.
942 * Finds a child property of a container class by name.
945 gtk_container_class_find_child_property (GObjectClass *cclass,
946 const gchar *property_name)
948 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
949 g_return_val_if_fail (property_name != NULL, NULL);
951 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
953 G_OBJECT_CLASS_TYPE (cclass),
958 * gtk_container_class_list_child_properties:
959 * @cclass: a #GtkContainerClass
960 * @n_properties: location to return the number of child properties found
961 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
962 * The array must be freed with g_free().
964 * Returns all child properties of a container class.
967 gtk_container_class_list_child_properties (GObjectClass *cclass,
973 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
975 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
976 G_OBJECT_CLASS_TYPE (cclass),
985 gtk_container_add_unimplemented (GtkContainer *container,
988 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
992 gtk_container_remove_unimplemented (GtkContainer *container,
995 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
999 gtk_container_init (GtkContainer *container)
1001 container->focus_child = NULL;
1002 container->border_width = 0;
1003 container->need_resize = FALSE;
1004 container->resize_mode = GTK_RESIZE_PARENT;
1005 container->reallocate_redraws = FALSE;
1009 gtk_container_destroy (GtkObject *object)
1011 GtkContainer *container = GTK_CONTAINER (object);
1013 if (GTK_CONTAINER_RESIZE_PENDING (container))
1014 _gtk_container_dequeue_resize_handler (container);
1016 /* do this before walking child widgets, to avoid
1017 * removing children from focus chain one by one.
1019 if (container->has_focus_chain)
1020 gtk_container_unset_focus_chain (container);
1022 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1024 if (GTK_OBJECT_CLASS (parent_class)->destroy)
1025 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
1029 gtk_container_set_property (GObject *object,
1031 const GValue *value,
1034 GtkContainer *container = GTK_CONTAINER (object);
1038 case PROP_BORDER_WIDTH:
1039 gtk_container_set_border_width (container, g_value_get_uint (value));
1041 case PROP_RESIZE_MODE:
1042 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1045 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1048 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1054 gtk_container_get_property (GObject *object,
1059 GtkContainer *container = GTK_CONTAINER (object);
1063 case PROP_BORDER_WIDTH:
1064 g_value_set_uint (value, container->border_width);
1066 case PROP_RESIZE_MODE:
1067 g_value_set_enum (value, container->resize_mode);
1070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1076 * gtk_container_set_border_width:
1077 * @container: a #GtkContainer
1078 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1079 * the container. Valid values are in the range 0-65535 pixels.
1081 * Sets the border width of the container.
1083 * The border width of a container is the amount of space to leave
1084 * around the outside of the container. The only exception to this is
1085 * #GtkWindow; because toplevel windows can't leave space outside,
1086 * they leave the space inside. The border is added on all sides of
1087 * the container. To add space to only one side, one approach is to
1088 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1089 * to give it a size, and place it on the side of the container as
1093 gtk_container_set_border_width (GtkContainer *container,
1096 g_return_if_fail (GTK_IS_CONTAINER (container));
1098 if (container->border_width != border_width)
1100 container->border_width = border_width;
1101 g_object_notify (G_OBJECT (container), "border-width");
1103 if (GTK_WIDGET_REALIZED (container))
1104 gtk_widget_queue_resize (GTK_WIDGET (container));
1109 * gtk_container_get_border_width:
1110 * @container: a #GtkContainer
1112 * Retrieves the border width of the container. See
1113 * gtk_container_set_border_width().
1115 * Return value: the current border width
1118 gtk_container_get_border_width (GtkContainer *container)
1120 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1122 return container->border_width;
1126 * gtk_container_add:
1127 * @container: a #GtkContainer
1128 * @widget: a widget to be placed inside @container
1130 * Adds @widget to @container. Typically used for simple containers
1131 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1132 * layout containers such as #GtkBox or #GtkTable, this function will
1133 * pick default packing parameters that may not be correct. So
1134 * consider functions such as gtk_box_pack_start() and
1135 * gtk_table_attach() as an alternative to gtk_container_add() in
1136 * those cases. A widget may be added to only one container at a time;
1137 * you can't place the same widget inside two different containers.
1140 gtk_container_add (GtkContainer *container,
1143 g_return_if_fail (GTK_IS_CONTAINER (container));
1144 g_return_if_fail (GTK_IS_WIDGET (widget));
1146 if (widget->parent != NULL)
1148 g_warning ("Attempting to add a widget with type %s to a container of "
1149 "type %s, but the widget is already inside a container of type %s, "
1150 "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1151 "explains how to reparent a widget.",
1152 g_type_name (G_OBJECT_TYPE (widget)),
1153 g_type_name (G_OBJECT_TYPE (container)),
1154 g_type_name (G_OBJECT_TYPE (widget->parent)));
1158 g_signal_emit (container, container_signals[ADD], 0, widget);
1162 * gtk_container_remove:
1163 * @container: a #GtkContainer
1164 * @widget: a current child of @container
1166 * Removes @widget from @container. @widget must be inside @container.
1167 * Note that @container will own a reference to @widget, and that this
1168 * may be the last reference held; so removing a widget from its
1169 * container can destroy that widget. If you want to use @widget
1170 * again, you need to add a reference to it while it's not inside
1171 * a container, using g_object_ref(). If you don't want to use @widget
1172 * again it's usually more efficient to simply destroy it directly
1173 * using gtk_widget_destroy() since this will remove it from the
1174 * container and help break any circular reference count cycles.
1177 gtk_container_remove (GtkContainer *container,
1180 g_return_if_fail (GTK_IS_CONTAINER (container));
1181 g_return_if_fail (GTK_IS_WIDGET (widget));
1183 /* When using the deprecated API of the toolbar, it is possible
1184 * to legitimately call this function with a widget that is not
1185 * a direct child of the container.
1187 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1188 widget->parent == GTK_WIDGET (container));
1190 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1194 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1196 g_return_if_fail (GTK_IS_CONTAINER (container));
1197 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1199 container_resize_queue = g_slist_remove (container_resize_queue, container);
1200 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1204 * gtk_container_set_resize_mode:
1205 * @container: a #GtkContainer
1206 * @resize_mode: the new resize mode
1208 * Sets the resize mode for the container.
1210 * The resize mode of a container determines whether a resize request
1211 * will be passed to the container's parent, queued for later execution
1212 * or executed immediately.
1215 gtk_container_set_resize_mode (GtkContainer *container,
1216 GtkResizeMode resize_mode)
1218 g_return_if_fail (GTK_IS_CONTAINER (container));
1219 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1221 if (GTK_WIDGET_TOPLEVEL (container) &&
1222 resize_mode == GTK_RESIZE_PARENT)
1224 resize_mode = GTK_RESIZE_QUEUE;
1227 if (container->resize_mode != resize_mode)
1229 container->resize_mode = resize_mode;
1231 gtk_widget_queue_resize (GTK_WIDGET (container));
1232 g_object_notify (G_OBJECT (container), "resize-mode");
1237 * gtk_container_get_resize_mode:
1238 * @container: a #GtkContainer
1240 * Returns the resize mode for the container. See
1241 * gtk_container_set_resize_mode ().
1243 * Return value: the current resize mode
1246 gtk_container_get_resize_mode (GtkContainer *container)
1248 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1250 return container->resize_mode;
1254 * gtk_container_set_reallocate_redraws:
1255 * @container: a #GtkContainer
1256 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1258 * Sets the @reallocate_redraws flag of the container to the given value.
1260 * Containers requesting reallocation redraws get automatically
1261 * redrawn if any of their children changed allocation.
1264 gtk_container_set_reallocate_redraws (GtkContainer *container,
1265 gboolean needs_redraws)
1267 g_return_if_fail (GTK_IS_CONTAINER (container));
1269 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1272 static GtkContainer*
1273 gtk_container_get_resize_container (GtkContainer *container)
1275 GtkWidget *widget = GTK_WIDGET (container);
1277 while (widget->parent)
1279 widget = widget->parent;
1280 if (GTK_IS_RESIZE_CONTAINER (widget))
1284 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1288 gtk_container_idle_sizer (gpointer data)
1290 /* we may be invoked with a container_resize_queue of NULL, because
1291 * queue_resize could have been adding an extra idle function while
1292 * the queue still got processed. we better just ignore such case
1293 * than trying to explicitely work around them with some extra flags,
1294 * since it doesn't cause any actual harm.
1296 while (container_resize_queue)
1301 slist = container_resize_queue;
1302 container_resize_queue = slist->next;
1303 widget = slist->data;
1304 g_slist_free_1 (slist);
1306 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1307 gtk_container_check_resize (GTK_CONTAINER (widget));
1310 gdk_window_process_all_updates ();
1316 _gtk_container_queue_resize (GtkContainer *container)
1318 GtkContainer *resize_container;
1321 g_return_if_fail (GTK_IS_CONTAINER (container));
1323 widget = GTK_WIDGET (container);
1324 resize_container = gtk_container_get_resize_container (container);
1328 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1329 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1330 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1334 widget = widget->parent;
1337 if (resize_container)
1339 if (GTK_WIDGET_VISIBLE (resize_container) &&
1340 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1342 switch (resize_container->resize_mode)
1344 case GTK_RESIZE_QUEUE:
1345 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1347 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1348 if (container_resize_queue == NULL)
1349 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1350 gtk_container_idle_sizer,
1352 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1356 case GTK_RESIZE_IMMEDIATE:
1357 gtk_container_check_resize (resize_container);
1360 case GTK_RESIZE_PARENT:
1361 g_assert_not_reached ();
1367 /* we need to let hidden resize containers know that something
1368 * changed while they where hidden (currently only evaluated by
1371 resize_container->need_resize = TRUE;
1377 gtk_container_check_resize (GtkContainer *container)
1379 g_return_if_fail (GTK_IS_CONTAINER (container));
1381 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1385 gtk_container_real_check_resize (GtkContainer *container)
1387 GtkWidget *widget = GTK_WIDGET (container);
1388 GtkRequisition requisition;
1390 gtk_widget_size_request (widget, &requisition);
1392 if (requisition.width > widget->allocation.width ||
1393 requisition.height > widget->allocation.height)
1395 if (GTK_IS_RESIZE_CONTAINER (container))
1396 gtk_widget_size_allocate (GTK_WIDGET (container),
1397 >K_WIDGET (container)->allocation);
1399 gtk_widget_queue_resize (widget);
1403 gtk_container_resize_children (container);
1407 /* The container hasn't changed size but one of its children
1408 * queued a resize request. Which means that the allocation
1409 * is not sufficient for the requisition of some child.
1410 * We've already performed a size request at this point,
1411 * so we simply need to reallocate and let the allocation
1412 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1415 gtk_container_resize_children (GtkContainer *container)
1419 /* resizing invariants:
1420 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1421 * containers that have an idle sizer pending must be flagged with
1424 g_return_if_fail (GTK_IS_CONTAINER (container));
1426 widget = GTK_WIDGET (container);
1427 gtk_widget_size_allocate (widget, &widget->allocation);
1431 * gtk_container_forall:
1432 * @container: a #GtkContainer
1433 * @callback: a callback
1434 * @callback_data: callback user data
1436 * Invokes @callback on each child of @container, including children
1437 * that are considered "internal" (implementation details of the
1438 * container). "Internal" children generally weren't added by the user
1439 * of the container, but were added by the container implementation
1440 * itself. Most applications should use gtk_container_foreach(),
1441 * rather than gtk_container_forall().
1444 gtk_container_forall (GtkContainer *container,
1445 GtkCallback callback,
1446 gpointer callback_data)
1448 GtkContainerClass *class;
1450 g_return_if_fail (GTK_IS_CONTAINER (container));
1451 g_return_if_fail (callback != NULL);
1453 class = GTK_CONTAINER_GET_CLASS (container);
1456 class->forall (container, TRUE, callback, callback_data);
1460 * gtk_container_foreach:
1461 * @container: a #GtkContainer
1462 * @callback: a callback
1463 * @callback_data: callback user data
1465 * Invokes @callback on each non-internal child of @container. See
1466 * gtk_container_forall() for details on what constitutes an
1467 * "internal" child. Most applications should use
1468 * gtk_container_foreach(), rather than gtk_container_forall().
1471 gtk_container_foreach (GtkContainer *container,
1472 GtkCallback callback,
1473 gpointer callback_data)
1475 GtkContainerClass *class;
1477 g_return_if_fail (GTK_IS_CONTAINER (container));
1478 g_return_if_fail (callback != NULL);
1480 class = GTK_CONTAINER_GET_CLASS (container);
1483 class->forall (container, FALSE, callback, callback_data);
1486 typedef struct _GtkForeachData GtkForeachData;
1487 struct _GtkForeachData
1489 GtkObject *container;
1490 GtkCallbackMarshal callback;
1491 gpointer callback_data;
1495 gtk_container_foreach_unmarshal (GtkWidget *child,
1498 GtkForeachData *fdata = (GtkForeachData*) data;
1501 /* first argument */
1502 args[0].name = NULL;
1503 args[0].type = G_TYPE_FROM_INSTANCE (child);
1504 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1506 /* location for return value */
1507 args[1].name = NULL;
1508 args[1].type = G_TYPE_NONE;
1510 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1514 gtk_container_foreach_full (GtkContainer *container,
1515 GtkCallback callback,
1516 GtkCallbackMarshal marshal,
1517 gpointer callback_data,
1518 GDestroyNotify notify)
1520 g_return_if_fail (GTK_IS_CONTAINER (container));
1524 GtkForeachData fdata;
1526 fdata.container = GTK_OBJECT (container);
1527 fdata.callback = marshal;
1528 fdata.callback_data = callback_data;
1530 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1534 g_return_if_fail (callback != NULL);
1536 gtk_container_foreach (container, callback, &callback_data);
1540 notify (callback_data);
1544 * gtk_container_set_focus_child:
1545 * @container: a #GtkContainer
1546 * @child: a #GtkWidget, or %NULL
1548 * Sets, or unsets if @child is %NULL, the focused child of @container.
1550 * This function emits the GtkContainer::set_focus_child signal of
1551 * @container. Implementations of #GtkContainer can override the
1552 * default behaviour by overriding the class closure of this signal.
1555 gtk_container_set_focus_child (GtkContainer *container,
1558 g_return_if_fail (GTK_IS_CONTAINER (container));
1560 g_return_if_fail (GTK_IS_WIDGET (child));
1562 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1566 * gtk_container_get_focus_child:
1567 * @container: a #GtkContainer
1569 * Returns the current focus child widget inside @container.
1571 * Returns: The child widget which has the focus
1572 * inside @container, or %NULL if none is set.
1577 gtk_container_get_focus_child (GtkContainer *container)
1579 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1581 return container->focus_child;
1585 * gtk_container_get_children:
1586 * @container: a #GtkContainer
1588 * Returns the container's non-internal children. See
1589 * gtk_container_forall() for details on what constitutes an "internal" child.
1591 * Return value: a newly-allocated list of the container's non-internal children.
1594 gtk_container_get_children (GtkContainer *container)
1596 GList *children = NULL;
1598 gtk_container_foreach (container,
1599 gtk_container_children_callback,
1602 return g_list_reverse (children);
1606 gtk_container_child_position_callback (GtkWidget *widget,
1607 gpointer client_data)
1613 } *data = client_data;
1616 if (data->child == widget)
1617 data->index = data->i;
1621 gtk_container_child_default_composite_name (GtkContainer *container,
1631 /* fallback implementation */
1635 gtk_container_forall (container,
1636 gtk_container_child_position_callback,
1639 name = g_strdup_printf ("%s-%u",
1640 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1647 _gtk_container_child_composite_name (GtkContainer *container,
1650 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1651 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1652 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1654 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1656 static GQuark quark_composite_name = 0;
1659 if (!quark_composite_name)
1660 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1662 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1665 GtkContainerClass *class;
1667 class = GTK_CONTAINER_GET_CLASS (container);
1668 if (class->composite_name)
1669 name = class->composite_name (container, child);
1672 name = g_strdup (name);
1681 gtk_container_real_set_focus_child (GtkContainer *container,
1684 g_return_if_fail (GTK_IS_CONTAINER (container));
1685 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1687 if (child != container->focus_child)
1689 if (container->focus_child)
1690 g_object_unref (container->focus_child);
1691 container->focus_child = child;
1692 if (container->focus_child)
1693 g_object_ref (container->focus_child);
1697 /* check for h/v adjustments
1699 if (container->focus_child)
1701 GtkAdjustment *hadj;
1702 GtkAdjustment *vadj;
1703 GtkWidget *focus_child;
1706 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1707 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1711 focus_child = container->focus_child;
1712 while (GTK_IS_CONTAINER (focus_child) &&
1713 GTK_CONTAINER (focus_child)->focus_child)
1715 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1718 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1721 x += container->focus_child->allocation.x;
1722 y += container->focus_child->allocation.y;
1725 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1728 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1734 get_focus_chain (GtkContainer *container)
1736 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1739 /* same as gtk_container_get_children, except it includes internals
1742 gtk_container_get_all_children (GtkContainer *container)
1744 GList *children = NULL;
1746 gtk_container_forall (container,
1747 gtk_container_children_callback,
1754 gtk_container_focus (GtkWidget *widget,
1755 GtkDirectionType direction)
1758 GList *sorted_children;
1760 GtkContainer *container;
1762 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1764 container = GTK_CONTAINER (widget);
1768 if (GTK_WIDGET_CAN_FOCUS (container))
1770 if (!GTK_WIDGET_HAS_FOCUS (container))
1772 gtk_widget_grab_focus (GTK_WIDGET (container));
1778 /* Get a list of the containers children, allowing focus
1779 * chain to override.
1781 if (container->has_focus_chain)
1782 children = g_list_copy (get_focus_chain (container));
1784 children = gtk_container_get_all_children (container);
1786 if (container->has_focus_chain &&
1787 (direction == GTK_DIR_TAB_FORWARD ||
1788 direction == GTK_DIR_TAB_BACKWARD))
1790 sorted_children = g_list_copy (children);
1792 if (direction == GTK_DIR_TAB_BACKWARD)
1793 sorted_children = g_list_reverse (sorted_children);
1796 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1798 return_val = gtk_container_focus_move (container, sorted_children, direction);
1800 g_list_free (sorted_children);
1801 g_list_free (children);
1808 tab_compare (gconstpointer a,
1812 const GtkWidget *child1 = a;
1813 const GtkWidget *child2 = b;
1814 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1816 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1817 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1821 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1822 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1824 if (text_direction == GTK_TEXT_DIR_RTL)
1825 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1827 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1830 return (y1 < y2) ? -1 : 1;
1834 gtk_container_focus_sort_tab (GtkContainer *container,
1836 GtkDirectionType direction,
1837 GtkWidget *old_focus)
1839 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1840 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1842 /* if we are going backwards then reverse the order
1845 if (direction == GTK_DIR_TAB_BACKWARD)
1846 children = g_list_reverse (children);
1851 /* Get coordinates of @widget's allocation with respect to
1852 * allocation of @container.
1855 get_allocation_coords (GtkContainer *container,
1857 GdkRectangle *allocation)
1859 *allocation = widget->allocation;
1861 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1862 0, 0, &allocation->x, &allocation->y);
1865 /* Look for a child in @children that is intermediate between
1866 * the focus widget and container. This widget, if it exists,
1867 * acts as the starting widget for focus navigation.
1870 find_old_focus (GtkContainer *container,
1873 GList *tmp_list = children;
1876 GtkWidget *child = tmp_list->data;
1877 GtkWidget *widget = child;
1879 while (widget && widget != (GtkWidget *)container)
1881 GtkWidget *parent = widget->parent;
1882 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1891 tmp_list = tmp_list->next;
1898 old_focus_coords (GtkContainer *container,
1899 GdkRectangle *old_focus_rect)
1901 GtkWidget *widget = GTK_WIDGET (container);
1902 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1904 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1906 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1908 return get_allocation_coords (container, old_focus, old_focus_rect);
1914 typedef struct _CompareInfo CompareInfo;
1918 GtkContainer *container;
1925 up_down_compare (gconstpointer a,
1929 GdkRectangle allocation1;
1930 GdkRectangle allocation2;
1931 CompareInfo *compare = data;
1934 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1935 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1937 y1 = allocation1.y + allocation1.height / 2;
1938 y2 = allocation2.y + allocation2.height / 2;
1942 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1943 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1945 if (compare->reverse)
1946 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1948 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1951 return (y1 < y2) ? -1 : 1;
1955 gtk_container_focus_sort_up_down (GtkContainer *container,
1957 GtkDirectionType direction,
1958 GtkWidget *old_focus)
1960 CompareInfo compare;
1962 GdkRectangle old_allocation;
1964 compare.container = container;
1965 compare.reverse = (direction == GTK_DIR_UP);
1968 old_focus = find_old_focus (container, children);
1970 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1976 /* Delete widgets from list that don't match minimum criteria */
1978 compare_x1 = old_allocation.x;
1979 compare_x2 = old_allocation.x + old_allocation.width;
1981 if (direction == GTK_DIR_UP)
1982 compare_y = old_allocation.y;
1984 compare_y = old_allocation.y + old_allocation.height;
1986 tmp_list = children;
1989 GtkWidget *child = tmp_list->data;
1990 GList *next = tmp_list->next;
1991 gint child_x1, child_x2;
1992 GdkRectangle child_allocation;
1994 if (child != old_focus)
1996 if (get_allocation_coords (container, child, &child_allocation))
1998 child_x1 = child_allocation.x;
1999 child_x2 = child_allocation.x + child_allocation.width;
2001 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2002 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2003 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2005 children = g_list_delete_link (children, tmp_list);
2009 children = g_list_delete_link (children, tmp_list);
2015 compare.x = (compare_x1 + compare_x2) / 2;
2016 compare.y = old_allocation.y + old_allocation.height / 2;
2020 /* No old focus widget, need to figure out starting x,y some other way
2022 GtkWidget *widget = GTK_WIDGET (container);
2023 GdkRectangle old_focus_rect;
2025 if (old_focus_coords (container, &old_focus_rect))
2027 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2031 if (GTK_WIDGET_NO_WINDOW (widget))
2032 compare.x = widget->allocation.x + widget->allocation.width / 2;
2034 compare.x = widget->allocation.width / 2;
2037 if (GTK_WIDGET_NO_WINDOW (widget))
2038 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2040 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2043 children = g_list_sort_with_data (children, up_down_compare, &compare);
2045 if (compare.reverse)
2046 children = g_list_reverse (children);
2052 left_right_compare (gconstpointer a,
2056 GdkRectangle allocation1;
2057 GdkRectangle allocation2;
2058 CompareInfo *compare = data;
2061 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2062 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2064 x1 = allocation1.x + allocation1.width / 2;
2065 x2 = allocation2.x + allocation2.width / 2;
2069 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2070 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2072 if (compare->reverse)
2073 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2075 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2078 return (x1 < x2) ? -1 : 1;
2082 gtk_container_focus_sort_left_right (GtkContainer *container,
2084 GtkDirectionType direction,
2085 GtkWidget *old_focus)
2087 CompareInfo compare;
2089 GdkRectangle old_allocation;
2091 compare.container = container;
2092 compare.reverse = (direction == GTK_DIR_LEFT);
2095 old_focus = find_old_focus (container, children);
2097 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2103 /* Delete widgets from list that don't match minimum criteria */
2105 compare_y1 = old_allocation.y;
2106 compare_y2 = old_allocation.y + old_allocation.height;
2108 if (direction == GTK_DIR_LEFT)
2109 compare_x = old_allocation.x;
2111 compare_x = old_allocation.x + old_allocation.width;
2113 tmp_list = children;
2116 GtkWidget *child = tmp_list->data;
2117 GList *next = tmp_list->next;
2118 gint child_y1, child_y2;
2119 GdkRectangle child_allocation;
2121 if (child != old_focus)
2123 if (get_allocation_coords (container, child, &child_allocation))
2125 child_y1 = child_allocation.y;
2126 child_y2 = child_allocation.y + child_allocation.height;
2128 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2129 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2130 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2132 children = g_list_delete_link (children, tmp_list);
2136 children = g_list_delete_link (children, tmp_list);
2142 compare.y = (compare_y1 + compare_y2) / 2;
2143 compare.x = old_allocation.x + old_allocation.width / 2;
2147 /* No old focus widget, need to figure out starting x,y some other way
2149 GtkWidget *widget = GTK_WIDGET (container);
2150 GdkRectangle old_focus_rect;
2152 if (old_focus_coords (container, &old_focus_rect))
2154 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2158 if (GTK_WIDGET_NO_WINDOW (widget))
2159 compare.y = widget->allocation.y + widget->allocation.height / 2;
2161 compare.y = widget->allocation.height / 2;
2164 if (GTK_WIDGET_NO_WINDOW (widget))
2165 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2167 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2170 children = g_list_sort_with_data (children, left_right_compare, &compare);
2172 if (compare.reverse)
2173 children = g_list_reverse (children);
2179 * gtk_container_focus_sort:
2180 * @container: a #GtkContainer
2181 * @children: a list of descendents of @container (they don't
2182 * have to be direct children)
2183 * @direction: focus direction
2184 * @old_focus: widget to use for the starting position, or %NULL
2185 * to determine this automatically.
2186 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2187 * which is the only @direction we use currently,
2188 * so perhaps this argument should be removed)
2190 * Sorts @children in the correct order for focusing with
2191 * direction type @direction.
2193 * Return value: a copy of @children, sorted in correct focusing order,
2194 * with children that aren't suitable for focusing in this direction
2198 _gtk_container_focus_sort (GtkContainer *container,
2200 GtkDirectionType direction,
2201 GtkWidget *old_focus)
2203 GList *visible_children = NULL;
2207 if (GTK_WIDGET_REALIZED (children->data))
2208 visible_children = g_list_prepend (visible_children, children->data);
2209 children = children->next;
2214 case GTK_DIR_TAB_FORWARD:
2215 case GTK_DIR_TAB_BACKWARD:
2216 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2219 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2222 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2225 g_assert_not_reached ();
2231 gtk_container_focus_move (GtkContainer *container,
2233 GtkDirectionType direction)
2235 GtkWidget *focus_child;
2238 focus_child = container->focus_child;
2242 child = children->data;
2243 children = children->next;
2250 if (focus_child == child)
2254 if (gtk_widget_child_focus (child, direction))
2258 else if (GTK_WIDGET_DRAWABLE (child) &&
2259 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2261 if (gtk_widget_child_focus (child, direction))
2271 gtk_container_children_callback (GtkWidget *widget,
2272 gpointer client_data)
2276 children = (GList**) client_data;
2277 *children = g_list_prepend (*children, widget);
2281 chain_widget_destroyed (GtkWidget *widget,
2284 GtkContainer *container;
2287 container = GTK_CONTAINER (user_data);
2289 chain = g_object_get_data (G_OBJECT (container),
2290 "gtk-container-focus-chain");
2292 chain = g_list_remove (chain, widget);
2294 g_signal_handlers_disconnect_by_func (widget,
2295 chain_widget_destroyed,
2298 g_object_set_data (G_OBJECT (container),
2299 I_("gtk-container-focus-chain"),
2304 * gtk_container_set_focus_chain:
2305 * @container: a #GtkContainer
2306 * @focusable_widgets: the new focus chain
2308 * Sets a focus chain, overriding the one computed automatically by GTK+.
2310 * In principle each widget in the chain should be a descendant of the
2311 * container, but this is not enforced by this method, since it's allowed
2312 * to set the focus chain before you pack the widgets, or have a widget
2313 * in the chain that isn't always packed. The necessary checks are done
2314 * when the focus chain is actually traversed.
2317 gtk_container_set_focus_chain (GtkContainer *container,
2318 GList *focusable_widgets)
2323 g_return_if_fail (GTK_IS_CONTAINER (container));
2325 if (container->has_focus_chain)
2326 gtk_container_unset_focus_chain (container);
2328 container->has_focus_chain = TRUE;
2331 tmp_list = focusable_widgets;
2332 while (tmp_list != NULL)
2334 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2336 /* In principle each widget in the chain should be a descendant
2337 * of the container, but we don't want to check that here, it's
2338 * expensive and also it's allowed to set the focus chain before
2339 * you pack the widgets, or have a widget in the chain that isn't
2340 * always packed. So we check for ancestor during actual traversal.
2343 chain = g_list_prepend (chain, tmp_list->data);
2345 g_signal_connect (tmp_list->data,
2347 G_CALLBACK (chain_widget_destroyed),
2350 tmp_list = g_list_next (tmp_list);
2353 chain = g_list_reverse (chain);
2355 g_object_set_data (G_OBJECT (container),
2356 I_("gtk-container-focus-chain"),
2361 * gtk_container_get_focus_chain:
2362 * @container: a #GtkContainer
2363 * @focusable_widgets: location to store the focus chain of the
2364 * container, or %NULL. You should free this list
2365 * using g_list_free() when you are done with it, however
2366 * no additional reference count is added to the
2367 * individual widgets in the focus chain.
2369 * Retrieves the focus chain of the container, if one has been
2370 * set explicitly. If no focus chain has been explicitly
2371 * set, GTK+ computes the focus chain based on the positions
2372 * of the children. In that case, GTK+ stores %NULL in
2373 * @focusable_widgets and returns %FALSE.
2375 * Return value: %TRUE if the focus chain of the container
2376 * has been set explicitly.
2379 gtk_container_get_focus_chain (GtkContainer *container,
2380 GList **focus_chain)
2382 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2386 if (container->has_focus_chain)
2387 *focus_chain = g_list_copy (get_focus_chain (container));
2389 *focus_chain = NULL;
2392 return container->has_focus_chain;
2396 * gtk_container_unset_focus_chain:
2397 * @container: a #GtkContainer
2399 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2402 gtk_container_unset_focus_chain (GtkContainer *container)
2404 g_return_if_fail (GTK_IS_CONTAINER (container));
2406 if (container->has_focus_chain)
2411 chain = get_focus_chain (container);
2413 container->has_focus_chain = FALSE;
2415 g_object_set_data (G_OBJECT (container),
2416 I_("gtk-container-focus-chain"),
2420 while (tmp_list != NULL)
2422 g_signal_handlers_disconnect_by_func (tmp_list->data,
2423 chain_widget_destroyed,
2426 tmp_list = g_list_next (tmp_list);
2429 g_list_free (chain);
2434 * gtk_container_set_focus_vadjustment:
2435 * @container: a #GtkContainer
2436 * @adjustment: an adjustment which should be adjusted when the focus
2437 * is moved among the descendents of @container
2439 * Hooks up an adjustment to focus handling in a container, so when a
2440 * child of the container is focused, the adjustment is scrolled to
2441 * show that widget. This function sets the vertical alignment. See
2442 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2443 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2444 * the horizontal adjustment.
2446 * The adjustments have to be in pixel units and in the same coordinate
2447 * system as the allocation for immediate children of the container.
2450 gtk_container_set_focus_vadjustment (GtkContainer *container,
2451 GtkAdjustment *adjustment)
2453 g_return_if_fail (GTK_IS_CONTAINER (container));
2455 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2458 g_object_ref (adjustment);
2460 g_object_set_qdata_full (G_OBJECT (container),
2467 * gtk_container_get_focus_vadjustment:
2468 * @container: a #GtkContainer
2470 * Retrieves the vertical focus adjustment for the container. See
2471 * gtk_container_set_focus_vadjustment().
2473 * Return value: the vertical focus adjustment, or %NULL if
2474 * none has been set.
2477 gtk_container_get_focus_vadjustment (GtkContainer *container)
2479 GtkAdjustment *vadjustment;
2481 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2483 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2489 * gtk_container_set_focus_hadjustment:
2490 * @container: a #GtkContainer
2491 * @adjustment: an adjustment which should be adjusted when the focus is
2492 * moved among the descendents of @container
2494 * Hooks up an adjustment to focus handling in a container, so when a child
2495 * of the container is focused, the adjustment is scrolled to show that
2496 * widget. This function sets the horizontal alignment.
2497 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2498 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2499 * the vertical adjustment.
2501 * The adjustments have to be in pixel units and in the same coordinate
2502 * system as the allocation for immediate children of the container.
2505 gtk_container_set_focus_hadjustment (GtkContainer *container,
2506 GtkAdjustment *adjustment)
2508 g_return_if_fail (GTK_IS_CONTAINER (container));
2510 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2513 g_object_ref (adjustment);
2515 g_object_set_qdata_full (G_OBJECT (container),
2522 * gtk_container_get_focus_hadjustment:
2523 * @container: a #GtkContainer
2525 * Retrieves the horizontal focus adjustment for the container. See
2526 * gtk_container_set_focus_hadjustment ().
2528 * Return value: the horizontal focus adjustment, or %NULL if
2529 * none has been set.
2532 gtk_container_get_focus_hadjustment (GtkContainer *container)
2534 GtkAdjustment *hadjustment;
2536 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2538 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2545 gtk_container_show_all (GtkWidget *widget)
2547 g_return_if_fail (GTK_IS_CONTAINER (widget));
2549 gtk_container_foreach (GTK_CONTAINER (widget),
2550 (GtkCallback) gtk_widget_show_all,
2552 gtk_widget_show (widget);
2556 gtk_container_hide_all (GtkWidget *widget)
2558 g_return_if_fail (GTK_IS_CONTAINER (widget));
2560 gtk_widget_hide (widget);
2561 gtk_container_foreach (GTK_CONTAINER (widget),
2562 (GtkCallback) gtk_widget_hide_all,
2568 gtk_container_expose_child (GtkWidget *child,
2569 gpointer client_data)
2572 GtkWidget *container;
2573 GdkEventExpose *event;
2574 } *data = client_data;
2576 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2582 gtk_container_expose (GtkWidget *widget,
2583 GdkEventExpose *event)
2586 GtkWidget *container;
2587 GdkEventExpose *event;
2590 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2591 g_return_val_if_fail (event != NULL, FALSE);
2594 if (GTK_WIDGET_DRAWABLE (widget))
2596 data.container = widget;
2599 gtk_container_forall (GTK_CONTAINER (widget),
2600 gtk_container_expose_child,
2608 gtk_container_map_child (GtkWidget *child,
2609 gpointer client_data)
2611 if (GTK_WIDGET_VISIBLE (child) &&
2612 GTK_WIDGET_CHILD_VISIBLE (child) &&
2613 !GTK_WIDGET_MAPPED (child))
2614 gtk_widget_map (child);
2618 gtk_container_map (GtkWidget *widget)
2620 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2622 gtk_container_forall (GTK_CONTAINER (widget),
2623 gtk_container_map_child,
2626 if (!GTK_WIDGET_NO_WINDOW (widget))
2627 gdk_window_show (widget->window);
2631 gtk_container_unmap (GtkWidget *widget)
2633 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2635 if (!GTK_WIDGET_NO_WINDOW (widget))
2636 gdk_window_hide (widget->window);
2638 gtk_container_forall (GTK_CONTAINER (widget),
2639 (GtkCallback)gtk_widget_unmap,
2644 * gtk_container_propagate_expose:
2645 * @container: a #GtkContainer
2646 * @child: a child of @container
2647 * @event: a expose event sent to container
2649 * When a container receives an expose event, it must send synthetic
2650 * expose events to all children that don't have their own #GdkWindows.
2651 * This function provides a convenient way of doing this. A container,
2652 * when it receives an expose event, calls gtk_container_propagate_expose()
2653 * once for each child, passing in the event the container received.
2655 * gtk_container_propagate_expose() takes care of deciding whether
2656 * an expose event needs to be sent to the child, intersecting
2657 * the event's area with the child area, and sending the event.
2659 * In most cases, a container can simply either simply inherit the
2660 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2661 * and then chain to the ::expose implementation from #GtkContainer.
2664 gtk_container_propagate_expose (GtkContainer *container,
2666 GdkEventExpose *event)
2668 GdkEvent *child_event;
2670 g_return_if_fail (GTK_IS_CONTAINER (container));
2671 g_return_if_fail (GTK_IS_WIDGET (child));
2672 g_return_if_fail (event != NULL);
2674 g_assert (child->parent == GTK_WIDGET (container));
2676 if (GTK_WIDGET_DRAWABLE (child) &&
2677 GTK_WIDGET_NO_WINDOW (child) &&
2678 (child->window == event->window))
2680 child_event = gdk_event_new (GDK_EXPOSE);
2681 child_event->expose = *event;
2682 g_object_ref (child_event->expose.window);
2684 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2685 if (!gdk_region_empty (child_event->expose.region))
2687 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2688 gtk_widget_send_expose (child, child_event);
2690 gdk_event_free (child_event);
2694 #define __GTK_CONTAINER_C__
2695 #include "gtkaliasdef.c"