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 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://library.gnome.org/devel/gtk-faq/stable/ "
1150 "explains how to reparent a widget.",
1151 g_type_name (G_OBJECT_TYPE (widget)),
1152 g_type_name (G_OBJECT_TYPE (container)),
1153 g_type_name (G_OBJECT_TYPE (widget->parent)));
1157 g_signal_emit (container, container_signals[ADD], 0, widget);
1161 * gtk_container_remove:
1162 * @container: a #GtkContainer
1163 * @widget: a current child of @container
1165 * Removes @widget from @container. @widget must be inside @container.
1166 * Note that @container will own a reference to @widget, and that this
1167 * may be the last reference held; so removing a widget from its
1168 * container can destroy that widget. If you want to use @widget
1169 * again, you need to add a reference to it while it's not inside
1170 * a container, using g_object_ref(). If you don't want to use @widget
1171 * again it's usually more efficient to simply destroy it directly
1172 * using gtk_widget_destroy() since this will remove it from the
1173 * container and help break any circular reference count cycles.
1176 gtk_container_remove (GtkContainer *container,
1179 g_return_if_fail (GTK_IS_CONTAINER (container));
1180 g_return_if_fail (GTK_IS_WIDGET (widget));
1182 /* When using the deprecated API of the toolbar, it is possible
1183 * to legitimately call this function with a widget that is not
1184 * a direct child of the container.
1186 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1187 widget->parent == GTK_WIDGET (container));
1189 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1193 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1195 g_return_if_fail (GTK_IS_CONTAINER (container));
1196 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1198 container_resize_queue = g_slist_remove (container_resize_queue, container);
1199 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1203 * gtk_container_set_resize_mode:
1204 * @container: a #GtkContainer
1205 * @resize_mode: the new resize mode
1207 * Sets the resize mode for the container.
1209 * The resize mode of a container determines whether a resize request
1210 * will be passed to the container's parent, queued for later execution
1211 * or executed immediately.
1214 gtk_container_set_resize_mode (GtkContainer *container,
1215 GtkResizeMode resize_mode)
1217 g_return_if_fail (GTK_IS_CONTAINER (container));
1218 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1220 if (GTK_WIDGET_TOPLEVEL (container) &&
1221 resize_mode == GTK_RESIZE_PARENT)
1223 resize_mode = GTK_RESIZE_QUEUE;
1226 if (container->resize_mode != resize_mode)
1228 container->resize_mode = resize_mode;
1230 gtk_widget_queue_resize (GTK_WIDGET (container));
1231 g_object_notify (G_OBJECT (container), "resize-mode");
1236 * gtk_container_get_resize_mode:
1237 * @container: a #GtkContainer
1239 * Returns the resize mode for the container. See
1240 * gtk_container_set_resize_mode ().
1242 * Return value: the current resize mode
1245 gtk_container_get_resize_mode (GtkContainer *container)
1247 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1249 return container->resize_mode;
1253 * gtk_container_set_reallocate_redraws:
1254 * @container: a #GtkContainer
1255 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1257 * Sets the @reallocate_redraws flag of the container to the given value.
1259 * Containers requesting reallocation redraws get automatically
1260 * redrawn if any of their children changed allocation.
1263 gtk_container_set_reallocate_redraws (GtkContainer *container,
1264 gboolean needs_redraws)
1266 g_return_if_fail (GTK_IS_CONTAINER (container));
1268 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1271 static GtkContainer*
1272 gtk_container_get_resize_container (GtkContainer *container)
1274 GtkWidget *widget = GTK_WIDGET (container);
1276 while (widget->parent)
1278 widget = widget->parent;
1279 if (GTK_IS_RESIZE_CONTAINER (widget))
1283 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1287 gtk_container_idle_sizer (gpointer data)
1289 /* we may be invoked with a container_resize_queue of NULL, because
1290 * queue_resize could have been adding an extra idle function while
1291 * the queue still got processed. we better just ignore such case
1292 * than trying to explicitely work around them with some extra flags,
1293 * since it doesn't cause any actual harm.
1295 while (container_resize_queue)
1300 slist = container_resize_queue;
1301 container_resize_queue = slist->next;
1302 widget = slist->data;
1303 g_slist_free_1 (slist);
1305 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1306 gtk_container_check_resize (GTK_CONTAINER (widget));
1309 gdk_window_process_all_updates ();
1315 _gtk_container_queue_resize (GtkContainer *container)
1317 GtkContainer *resize_container;
1320 g_return_if_fail (GTK_IS_CONTAINER (container));
1322 widget = GTK_WIDGET (container);
1323 resize_container = gtk_container_get_resize_container (container);
1327 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1328 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1329 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1333 widget = widget->parent;
1336 if (resize_container)
1338 if (GTK_WIDGET_VISIBLE (resize_container) &&
1339 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1341 switch (resize_container->resize_mode)
1343 case GTK_RESIZE_QUEUE:
1344 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1346 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1347 if (container_resize_queue == NULL)
1348 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1349 gtk_container_idle_sizer,
1351 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1355 case GTK_RESIZE_IMMEDIATE:
1356 gtk_container_check_resize (resize_container);
1359 case GTK_RESIZE_PARENT:
1360 g_assert_not_reached ();
1366 /* we need to let hidden resize containers know that something
1367 * changed while they where hidden (currently only evaluated by
1370 resize_container->need_resize = TRUE;
1376 gtk_container_check_resize (GtkContainer *container)
1378 g_return_if_fail (GTK_IS_CONTAINER (container));
1380 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1384 gtk_container_real_check_resize (GtkContainer *container)
1386 GtkWidget *widget = GTK_WIDGET (container);
1387 GtkRequisition requisition;
1389 gtk_widget_size_request (widget, &requisition);
1391 if (requisition.width > widget->allocation.width ||
1392 requisition.height > widget->allocation.height)
1394 if (GTK_IS_RESIZE_CONTAINER (container))
1395 gtk_widget_size_allocate (GTK_WIDGET (container),
1396 >K_WIDGET (container)->allocation);
1398 gtk_widget_queue_resize (widget);
1402 gtk_container_resize_children (container);
1406 /* The container hasn't changed size but one of its children
1407 * queued a resize request. Which means that the allocation
1408 * is not sufficient for the requisition of some child.
1409 * We've already performed a size request at this point,
1410 * so we simply need to reallocate and let the allocation
1411 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1414 gtk_container_resize_children (GtkContainer *container)
1418 /* resizing invariants:
1419 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1420 * containers that have an idle sizer pending must be flagged with
1423 g_return_if_fail (GTK_IS_CONTAINER (container));
1425 widget = GTK_WIDGET (container);
1426 gtk_widget_size_allocate (widget, &widget->allocation);
1430 * gtk_container_forall:
1431 * @container: a #GtkContainer
1432 * @callback: a callback
1433 * @callback_data: callback user data
1435 * Invokes @callback on each child of @container, including children
1436 * that are considered "internal" (implementation details of the
1437 * container). "Internal" children generally weren't added by the user
1438 * of the container, but were added by the container implementation
1439 * itself. Most applications should use gtk_container_foreach(),
1440 * rather than gtk_container_forall().
1443 gtk_container_forall (GtkContainer *container,
1444 GtkCallback callback,
1445 gpointer callback_data)
1447 GtkContainerClass *class;
1449 g_return_if_fail (GTK_IS_CONTAINER (container));
1450 g_return_if_fail (callback != NULL);
1452 class = GTK_CONTAINER_GET_CLASS (container);
1455 class->forall (container, TRUE, callback, callback_data);
1459 * gtk_container_foreach:
1460 * @container: a #GtkContainer
1461 * @callback: a callback
1462 * @callback_data: callback user data
1464 * Invokes @callback on each non-internal child of @container. See
1465 * gtk_container_forall() for details on what constitutes an
1466 * "internal" child. Most applications should use
1467 * gtk_container_foreach(), rather than gtk_container_forall().
1470 gtk_container_foreach (GtkContainer *container,
1471 GtkCallback callback,
1472 gpointer callback_data)
1474 GtkContainerClass *class;
1476 g_return_if_fail (GTK_IS_CONTAINER (container));
1477 g_return_if_fail (callback != NULL);
1479 class = GTK_CONTAINER_GET_CLASS (container);
1482 class->forall (container, FALSE, callback, callback_data);
1485 typedef struct _GtkForeachData GtkForeachData;
1486 struct _GtkForeachData
1488 GtkObject *container;
1489 GtkCallbackMarshal callback;
1490 gpointer callback_data;
1494 gtk_container_foreach_unmarshal (GtkWidget *child,
1497 GtkForeachData *fdata = (GtkForeachData*) data;
1500 /* first argument */
1501 args[0].name = NULL;
1502 args[0].type = G_TYPE_FROM_INSTANCE (child);
1503 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1505 /* location for return value */
1506 args[1].name = NULL;
1507 args[1].type = G_TYPE_NONE;
1509 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1513 gtk_container_foreach_full (GtkContainer *container,
1514 GtkCallback callback,
1515 GtkCallbackMarshal marshal,
1516 gpointer callback_data,
1517 GDestroyNotify notify)
1519 g_return_if_fail (GTK_IS_CONTAINER (container));
1523 GtkForeachData fdata;
1525 fdata.container = GTK_OBJECT (container);
1526 fdata.callback = marshal;
1527 fdata.callback_data = callback_data;
1529 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1533 g_return_if_fail (callback != NULL);
1535 gtk_container_foreach (container, callback, &callback_data);
1539 notify (callback_data);
1543 * gtk_container_set_focus_child:
1544 * @container: a #GtkContainer
1545 * @child: a #GtkWidget, or %NULL
1547 * Sets, or unsets if @child is %NULL, the focused child of @container.
1549 * This function emits the GtkContainer::set_focus_child signal of
1550 * @container. Implementations of #GtkContainer can override the
1551 * default behaviour by overriding the class closure of this signal.
1554 gtk_container_set_focus_child (GtkContainer *container,
1557 g_return_if_fail (GTK_IS_CONTAINER (container));
1559 g_return_if_fail (GTK_IS_WIDGET (child));
1561 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1565 * gtk_container_get_focus_child:
1566 * @container: a #GtkContainer
1568 * Returns the current focus child widget inside @container.
1570 * Returns: The child widget which has the focus
1571 * inside @container, or %NULL if none is set.
1576 gtk_container_get_focus_child (GtkContainer *container)
1578 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1580 return container->focus_child;
1584 * gtk_container_get_children:
1585 * @container: a #GtkContainer
1587 * Returns the container's non-internal children. See
1588 * gtk_container_forall() for details on what constitutes an "internal" child.
1590 * Return value: a newly-allocated list of the container's non-internal children.
1593 gtk_container_get_children (GtkContainer *container)
1595 GList *children = NULL;
1597 gtk_container_foreach (container,
1598 gtk_container_children_callback,
1601 return g_list_reverse (children);
1605 gtk_container_child_position_callback (GtkWidget *widget,
1606 gpointer client_data)
1612 } *data = client_data;
1615 if (data->child == widget)
1616 data->index = data->i;
1620 gtk_container_child_default_composite_name (GtkContainer *container,
1630 /* fallback implementation */
1634 gtk_container_forall (container,
1635 gtk_container_child_position_callback,
1638 name = g_strdup_printf ("%s-%u",
1639 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1646 _gtk_container_child_composite_name (GtkContainer *container,
1649 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1650 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1651 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1653 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1655 static GQuark quark_composite_name = 0;
1658 if (!quark_composite_name)
1659 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1661 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1664 GtkContainerClass *class;
1666 class = GTK_CONTAINER_GET_CLASS (container);
1667 if (class->composite_name)
1668 name = class->composite_name (container, child);
1671 name = g_strdup (name);
1680 gtk_container_real_set_focus_child (GtkContainer *container,
1683 g_return_if_fail (GTK_IS_CONTAINER (container));
1684 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1686 if (child != container->focus_child)
1688 if (container->focus_child)
1689 g_object_unref (container->focus_child);
1690 container->focus_child = child;
1691 if (container->focus_child)
1692 g_object_ref (container->focus_child);
1696 /* check for h/v adjustments
1698 if (container->focus_child)
1700 GtkAdjustment *hadj;
1701 GtkAdjustment *vadj;
1702 GtkWidget *focus_child;
1705 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1706 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1710 focus_child = container->focus_child;
1711 while (GTK_IS_CONTAINER (focus_child) &&
1712 GTK_CONTAINER (focus_child)->focus_child)
1714 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1717 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1720 x += container->focus_child->allocation.x;
1721 y += container->focus_child->allocation.y;
1724 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1727 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1733 get_focus_chain (GtkContainer *container)
1735 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1738 /* same as gtk_container_get_children, except it includes internals
1741 gtk_container_get_all_children (GtkContainer *container)
1743 GList *children = NULL;
1745 gtk_container_forall (container,
1746 gtk_container_children_callback,
1753 gtk_container_focus (GtkWidget *widget,
1754 GtkDirectionType direction)
1757 GList *sorted_children;
1759 GtkContainer *container;
1761 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1763 container = GTK_CONTAINER (widget);
1767 if (GTK_WIDGET_CAN_FOCUS (container))
1769 if (!GTK_WIDGET_HAS_FOCUS (container))
1771 gtk_widget_grab_focus (GTK_WIDGET (container));
1777 /* Get a list of the containers children, allowing focus
1778 * chain to override.
1780 if (container->has_focus_chain)
1781 children = g_list_copy (get_focus_chain (container));
1783 children = gtk_container_get_all_children (container);
1785 if (container->has_focus_chain &&
1786 (direction == GTK_DIR_TAB_FORWARD ||
1787 direction == GTK_DIR_TAB_BACKWARD))
1789 sorted_children = g_list_copy (children);
1791 if (direction == GTK_DIR_TAB_BACKWARD)
1792 sorted_children = g_list_reverse (sorted_children);
1795 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1797 return_val = gtk_container_focus_move (container, sorted_children, direction);
1799 g_list_free (sorted_children);
1800 g_list_free (children);
1807 tab_compare (gconstpointer a,
1811 const GtkWidget *child1 = a;
1812 const GtkWidget *child2 = b;
1813 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1815 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1816 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1820 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1821 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1823 if (text_direction == GTK_TEXT_DIR_RTL)
1824 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1826 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1829 return (y1 < y2) ? -1 : 1;
1833 gtk_container_focus_sort_tab (GtkContainer *container,
1835 GtkDirectionType direction,
1836 GtkWidget *old_focus)
1838 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1839 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1841 /* if we are going backwards then reverse the order
1844 if (direction == GTK_DIR_TAB_BACKWARD)
1845 children = g_list_reverse (children);
1850 /* Get coordinates of @widget's allocation with respect to
1851 * allocation of @container.
1854 get_allocation_coords (GtkContainer *container,
1856 GdkRectangle *allocation)
1858 *allocation = widget->allocation;
1860 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1861 0, 0, &allocation->x, &allocation->y);
1864 /* Look for a child in @children that is intermediate between
1865 * the focus widget and container. This widget, if it exists,
1866 * acts as the starting widget for focus navigation.
1869 find_old_focus (GtkContainer *container,
1872 GList *tmp_list = children;
1875 GtkWidget *child = tmp_list->data;
1876 GtkWidget *widget = child;
1878 while (widget && widget != (GtkWidget *)container)
1880 GtkWidget *parent = widget->parent;
1881 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1890 tmp_list = tmp_list->next;
1897 old_focus_coords (GtkContainer *container,
1898 GdkRectangle *old_focus_rect)
1900 GtkWidget *widget = GTK_WIDGET (container);
1901 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1903 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1905 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1907 return get_allocation_coords (container, old_focus, old_focus_rect);
1913 typedef struct _CompareInfo CompareInfo;
1917 GtkContainer *container;
1924 up_down_compare (gconstpointer a,
1928 GdkRectangle allocation1;
1929 GdkRectangle allocation2;
1930 CompareInfo *compare = data;
1933 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1934 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1936 y1 = allocation1.y + allocation1.height / 2;
1937 y2 = allocation2.y + allocation2.height / 2;
1941 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1942 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1944 if (compare->reverse)
1945 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1947 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1950 return (y1 < y2) ? -1 : 1;
1954 gtk_container_focus_sort_up_down (GtkContainer *container,
1956 GtkDirectionType direction,
1957 GtkWidget *old_focus)
1959 CompareInfo compare;
1961 GdkRectangle old_allocation;
1963 compare.container = container;
1964 compare.reverse = (direction == GTK_DIR_UP);
1967 old_focus = find_old_focus (container, children);
1969 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1975 /* Delete widgets from list that don't match minimum criteria */
1977 compare_x1 = old_allocation.x;
1978 compare_x2 = old_allocation.x + old_allocation.width;
1980 if (direction == GTK_DIR_UP)
1981 compare_y = old_allocation.y;
1983 compare_y = old_allocation.y + old_allocation.height;
1985 tmp_list = children;
1988 GtkWidget *child = tmp_list->data;
1989 GList *next = tmp_list->next;
1990 gint child_x1, child_x2;
1991 GdkRectangle child_allocation;
1993 if (child != old_focus)
1995 if (get_allocation_coords (container, child, &child_allocation))
1997 child_x1 = child_allocation.x;
1998 child_x2 = child_allocation.x + child_allocation.width;
2000 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2001 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2002 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2004 children = g_list_delete_link (children, tmp_list);
2008 children = g_list_delete_link (children, tmp_list);
2014 compare.x = (compare_x1 + compare_x2) / 2;
2015 compare.y = old_allocation.y + old_allocation.height / 2;
2019 /* No old focus widget, need to figure out starting x,y some other way
2021 GtkWidget *widget = GTK_WIDGET (container);
2022 GdkRectangle old_focus_rect;
2024 if (old_focus_coords (container, &old_focus_rect))
2026 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2030 if (GTK_WIDGET_NO_WINDOW (widget))
2031 compare.x = widget->allocation.x + widget->allocation.width / 2;
2033 compare.x = widget->allocation.width / 2;
2036 if (GTK_WIDGET_NO_WINDOW (widget))
2037 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2039 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2042 children = g_list_sort_with_data (children, up_down_compare, &compare);
2044 if (compare.reverse)
2045 children = g_list_reverse (children);
2051 left_right_compare (gconstpointer a,
2055 GdkRectangle allocation1;
2056 GdkRectangle allocation2;
2057 CompareInfo *compare = data;
2060 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2061 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2063 x1 = allocation1.x + allocation1.width / 2;
2064 x2 = allocation2.x + allocation2.width / 2;
2068 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2069 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2071 if (compare->reverse)
2072 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2074 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2077 return (x1 < x2) ? -1 : 1;
2081 gtk_container_focus_sort_left_right (GtkContainer *container,
2083 GtkDirectionType direction,
2084 GtkWidget *old_focus)
2086 CompareInfo compare;
2088 GdkRectangle old_allocation;
2090 compare.container = container;
2091 compare.reverse = (direction == GTK_DIR_LEFT);
2094 old_focus = find_old_focus (container, children);
2096 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2102 /* Delete widgets from list that don't match minimum criteria */
2104 compare_y1 = old_allocation.y;
2105 compare_y2 = old_allocation.y + old_allocation.height;
2107 if (direction == GTK_DIR_LEFT)
2108 compare_x = old_allocation.x;
2110 compare_x = old_allocation.x + old_allocation.width;
2112 tmp_list = children;
2115 GtkWidget *child = tmp_list->data;
2116 GList *next = tmp_list->next;
2117 gint child_y1, child_y2;
2118 GdkRectangle child_allocation;
2120 if (child != old_focus)
2122 if (get_allocation_coords (container, child, &child_allocation))
2124 child_y1 = child_allocation.y;
2125 child_y2 = child_allocation.y + child_allocation.height;
2127 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2128 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2129 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2131 children = g_list_delete_link (children, tmp_list);
2135 children = g_list_delete_link (children, tmp_list);
2141 compare.y = (compare_y1 + compare_y2) / 2;
2142 compare.x = old_allocation.x + old_allocation.width / 2;
2146 /* No old focus widget, need to figure out starting x,y some other way
2148 GtkWidget *widget = GTK_WIDGET (container);
2149 GdkRectangle old_focus_rect;
2151 if (old_focus_coords (container, &old_focus_rect))
2153 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2157 if (GTK_WIDGET_NO_WINDOW (widget))
2158 compare.y = widget->allocation.y + widget->allocation.height / 2;
2160 compare.y = widget->allocation.height / 2;
2163 if (GTK_WIDGET_NO_WINDOW (widget))
2164 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2166 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2169 children = g_list_sort_with_data (children, left_right_compare, &compare);
2171 if (compare.reverse)
2172 children = g_list_reverse (children);
2178 * gtk_container_focus_sort:
2179 * @container: a #GtkContainer
2180 * @children: a list of descendents of @container (they don't
2181 * have to be direct children)
2182 * @direction: focus direction
2183 * @old_focus: widget to use for the starting position, or %NULL
2184 * to determine this automatically.
2185 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2186 * which is the only @direction we use currently,
2187 * so perhaps this argument should be removed)
2189 * Sorts @children in the correct order for focusing with
2190 * direction type @direction.
2192 * Return value: a copy of @children, sorted in correct focusing order,
2193 * with children that aren't suitable for focusing in this direction
2197 _gtk_container_focus_sort (GtkContainer *container,
2199 GtkDirectionType direction,
2200 GtkWidget *old_focus)
2202 GList *visible_children = NULL;
2206 if (GTK_WIDGET_REALIZED (children->data))
2207 visible_children = g_list_prepend (visible_children, children->data);
2208 children = children->next;
2213 case GTK_DIR_TAB_FORWARD:
2214 case GTK_DIR_TAB_BACKWARD:
2215 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2218 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2221 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2224 g_assert_not_reached ();
2230 gtk_container_focus_move (GtkContainer *container,
2232 GtkDirectionType direction)
2234 GtkWidget *focus_child;
2237 focus_child = container->focus_child;
2241 child = children->data;
2242 children = children->next;
2249 if (focus_child == child)
2253 if (gtk_widget_child_focus (child, direction))
2257 else if (GTK_WIDGET_DRAWABLE (child) &&
2258 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2260 if (gtk_widget_child_focus (child, direction))
2270 gtk_container_children_callback (GtkWidget *widget,
2271 gpointer client_data)
2275 children = (GList**) client_data;
2276 *children = g_list_prepend (*children, widget);
2280 chain_widget_destroyed (GtkWidget *widget,
2283 GtkContainer *container;
2286 container = GTK_CONTAINER (user_data);
2288 chain = g_object_get_data (G_OBJECT (container),
2289 "gtk-container-focus-chain");
2291 chain = g_list_remove (chain, widget);
2293 g_signal_handlers_disconnect_by_func (widget,
2294 chain_widget_destroyed,
2297 g_object_set_data (G_OBJECT (container),
2298 I_("gtk-container-focus-chain"),
2303 * gtk_container_set_focus_chain:
2304 * @container: a #GtkContainer
2305 * @focusable_widgets: the new focus chain
2307 * Sets a focus chain, overriding the one computed automatically by GTK+.
2309 * In principle each widget in the chain should be a descendant of the
2310 * container, but this is not enforced by this method, since it's allowed
2311 * to set the focus chain before you pack the widgets, or have a widget
2312 * in the chain that isn't always packed. The necessary checks are done
2313 * when the focus chain is actually traversed.
2316 gtk_container_set_focus_chain (GtkContainer *container,
2317 GList *focusable_widgets)
2322 g_return_if_fail (GTK_IS_CONTAINER (container));
2324 if (container->has_focus_chain)
2325 gtk_container_unset_focus_chain (container);
2327 container->has_focus_chain = TRUE;
2330 tmp_list = focusable_widgets;
2331 while (tmp_list != NULL)
2333 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2335 /* In principle each widget in the chain should be a descendant
2336 * of the container, but we don't want to check that here, it's
2337 * expensive and also it's allowed to set the focus chain before
2338 * you pack the widgets, or have a widget in the chain that isn't
2339 * always packed. So we check for ancestor during actual traversal.
2342 chain = g_list_prepend (chain, tmp_list->data);
2344 g_signal_connect (tmp_list->data,
2346 G_CALLBACK (chain_widget_destroyed),
2349 tmp_list = g_list_next (tmp_list);
2352 chain = g_list_reverse (chain);
2354 g_object_set_data (G_OBJECT (container),
2355 I_("gtk-container-focus-chain"),
2360 * gtk_container_get_focus_chain:
2361 * @container: a #GtkContainer
2362 * @focusable_widgets: location to store the focus chain of the
2363 * container, or %NULL. You should free this list
2364 * using g_list_free() when you are done with it, however
2365 * no additional reference count is added to the
2366 * individual widgets in the focus chain.
2368 * Retrieves the focus chain of the container, if one has been
2369 * set explicitly. If no focus chain has been explicitly
2370 * set, GTK+ computes the focus chain based on the positions
2371 * of the children. In that case, GTK+ stores %NULL in
2372 * @focusable_widgets and returns %FALSE.
2374 * Return value: %TRUE if the focus chain of the container
2375 * has been set explicitly.
2378 gtk_container_get_focus_chain (GtkContainer *container,
2379 GList **focus_chain)
2381 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2385 if (container->has_focus_chain)
2386 *focus_chain = g_list_copy (get_focus_chain (container));
2388 *focus_chain = NULL;
2391 return container->has_focus_chain;
2395 * gtk_container_unset_focus_chain:
2396 * @container: a #GtkContainer
2398 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2401 gtk_container_unset_focus_chain (GtkContainer *container)
2403 g_return_if_fail (GTK_IS_CONTAINER (container));
2405 if (container->has_focus_chain)
2410 chain = get_focus_chain (container);
2412 container->has_focus_chain = FALSE;
2414 g_object_set_data (G_OBJECT (container),
2415 I_("gtk-container-focus-chain"),
2419 while (tmp_list != NULL)
2421 g_signal_handlers_disconnect_by_func (tmp_list->data,
2422 chain_widget_destroyed,
2425 tmp_list = g_list_next (tmp_list);
2428 g_list_free (chain);
2433 * gtk_container_set_focus_vadjustment:
2434 * @container: a #GtkContainer
2435 * @adjustment: an adjustment which should be adjusted when the focus
2436 * is moved among the descendents of @container
2438 * Hooks up an adjustment to focus handling in a container, so when a
2439 * child of the container is focused, the adjustment is scrolled to
2440 * show that widget. This function sets the vertical alignment. See
2441 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2442 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2443 * the horizontal adjustment.
2445 * The adjustments have to be in pixel units and in the same coordinate
2446 * system as the allocation for immediate children of the container.
2449 gtk_container_set_focus_vadjustment (GtkContainer *container,
2450 GtkAdjustment *adjustment)
2452 g_return_if_fail (GTK_IS_CONTAINER (container));
2454 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2457 g_object_ref (adjustment);
2459 g_object_set_qdata_full (G_OBJECT (container),
2466 * gtk_container_get_focus_vadjustment:
2467 * @container: a #GtkContainer
2469 * Retrieves the vertical focus adjustment for the container. See
2470 * gtk_container_set_focus_vadjustment().
2472 * Return value: the vertical focus adjustment, or %NULL if
2473 * none has been set.
2476 gtk_container_get_focus_vadjustment (GtkContainer *container)
2478 GtkAdjustment *vadjustment;
2480 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2482 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2488 * gtk_container_set_focus_hadjustment:
2489 * @container: a #GtkContainer
2490 * @adjustment: an adjustment which should be adjusted when the focus is
2491 * moved among the descendents of @container
2493 * Hooks up an adjustment to focus handling in a container, so when a child
2494 * of the container is focused, the adjustment is scrolled to show that
2495 * widget. This function sets the horizontal alignment.
2496 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2497 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2498 * the vertical adjustment.
2500 * The adjustments have to be in pixel units and in the same coordinate
2501 * system as the allocation for immediate children of the container.
2504 gtk_container_set_focus_hadjustment (GtkContainer *container,
2505 GtkAdjustment *adjustment)
2507 g_return_if_fail (GTK_IS_CONTAINER (container));
2509 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2512 g_object_ref (adjustment);
2514 g_object_set_qdata_full (G_OBJECT (container),
2521 * gtk_container_get_focus_hadjustment:
2522 * @container: a #GtkContainer
2524 * Retrieves the horizontal focus adjustment for the container. See
2525 * gtk_container_set_focus_hadjustment ().
2527 * Return value: the horizontal focus adjustment, or %NULL if
2528 * none has been set.
2531 gtk_container_get_focus_hadjustment (GtkContainer *container)
2533 GtkAdjustment *hadjustment;
2535 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2537 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2544 gtk_container_show_all (GtkWidget *widget)
2546 g_return_if_fail (GTK_IS_CONTAINER (widget));
2548 gtk_container_foreach (GTK_CONTAINER (widget),
2549 (GtkCallback) gtk_widget_show_all,
2551 gtk_widget_show (widget);
2555 gtk_container_hide_all (GtkWidget *widget)
2557 g_return_if_fail (GTK_IS_CONTAINER (widget));
2559 gtk_widget_hide (widget);
2560 gtk_container_foreach (GTK_CONTAINER (widget),
2561 (GtkCallback) gtk_widget_hide_all,
2567 gtk_container_expose_child (GtkWidget *child,
2568 gpointer client_data)
2571 GtkWidget *container;
2572 GdkEventExpose *event;
2573 } *data = client_data;
2575 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2581 gtk_container_expose (GtkWidget *widget,
2582 GdkEventExpose *event)
2585 GtkWidget *container;
2586 GdkEventExpose *event;
2589 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2590 g_return_val_if_fail (event != NULL, FALSE);
2593 if (GTK_WIDGET_DRAWABLE (widget))
2595 data.container = widget;
2598 gtk_container_forall (GTK_CONTAINER (widget),
2599 gtk_container_expose_child,
2607 gtk_container_map_child (GtkWidget *child,
2608 gpointer client_data)
2610 if (GTK_WIDGET_VISIBLE (child) &&
2611 GTK_WIDGET_CHILD_VISIBLE (child) &&
2612 !GTK_WIDGET_MAPPED (child))
2613 gtk_widget_map (child);
2617 gtk_container_map (GtkWidget *widget)
2619 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2621 gtk_container_forall (GTK_CONTAINER (widget),
2622 gtk_container_map_child,
2625 if (!GTK_WIDGET_NO_WINDOW (widget))
2626 gdk_window_show (widget->window);
2630 gtk_container_unmap (GtkWidget *widget)
2632 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2634 if (!GTK_WIDGET_NO_WINDOW (widget))
2635 gdk_window_hide (widget->window);
2637 gtk_container_forall (GTK_CONTAINER (widget),
2638 (GtkCallback)gtk_widget_unmap,
2643 * gtk_container_propagate_expose:
2644 * @container: a #GtkContainer
2645 * @child: a child of @container
2646 * @event: a expose event sent to container
2648 * When a container receives an expose event, it must send synthetic
2649 * expose events to all children that don't have their own #GdkWindows.
2650 * This function provides a convenient way of doing this. A container,
2651 * when it receives an expose event, calls gtk_container_propagate_expose()
2652 * once for each child, passing in the event the container received.
2654 * gtk_container_propagate_expose() takes care of deciding whether
2655 * an expose event needs to be sent to the child, intersecting
2656 * the event's area with the child area, and sending the event.
2658 * In most cases, a container can simply either simply inherit the
2659 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2660 * and then chain to the ::expose implementation from #GtkContainer.
2663 gtk_container_propagate_expose (GtkContainer *container,
2665 GdkEventExpose *event)
2667 GdkEvent *child_event;
2669 g_return_if_fail (GTK_IS_CONTAINER (container));
2670 g_return_if_fail (GTK_IS_WIDGET (child));
2671 g_return_if_fail (event != NULL);
2673 g_assert (child->parent == GTK_WIDGET (container));
2675 if (GTK_WIDGET_DRAWABLE (child) &&
2676 GTK_WIDGET_NO_WINDOW (child) &&
2677 (child->window == event->window))
2679 child_event = gdk_event_new (GDK_EXPOSE);
2680 child_event->expose = *event;
2681 g_object_ref (child_event->expose.window);
2683 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2684 if (!gdk_region_empty (child_event->expose.region))
2686 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2687 gtk_widget_send_expose (child, child_event);
2689 gdk_event_free (child_event);
2693 #define __GTK_CONTAINER_C__
2694 #include "gtkaliasdef.c"