1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkcontainer.h"
33 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
36 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
39 #include "gtktoolbar.h"
40 #include <gobject/gobjectnotifyqueue.c>
41 #include <gobject/gvaluecollector.h>
60 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
61 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
64 /* --- prototypes --- */
65 static void gtk_container_base_class_init (GtkContainerClass *klass);
66 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
67 static void gtk_container_class_init (GtkContainerClass *klass);
68 static void gtk_container_init (GtkContainer *container);
69 static void gtk_container_destroy (GtkObject *object);
70 static void gtk_container_set_property (GObject *object,
74 static void gtk_container_get_property (GObject *object,
78 static void gtk_container_add_unimplemented (GtkContainer *container,
80 static void gtk_container_remove_unimplemented (GtkContainer *container,
82 static void gtk_container_real_check_resize (GtkContainer *container);
83 static gboolean gtk_container_focus (GtkWidget *widget,
84 GtkDirectionType direction);
85 static void gtk_container_real_set_focus_child (GtkContainer *container,
88 static gboolean gtk_container_focus_move (GtkContainer *container,
90 GtkDirectionType direction);
91 static void gtk_container_children_callback (GtkWidget *widget,
92 gpointer client_data);
93 static void gtk_container_show_all (GtkWidget *widget);
94 static void gtk_container_hide_all (GtkWidget *widget);
95 static gint gtk_container_expose (GtkWidget *widget,
96 GdkEventExpose *event);
97 static void gtk_container_map (GtkWidget *widget);
98 static void gtk_container_unmap (GtkWidget *widget);
100 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
104 static void gtk_container_buildable_init (GtkBuildableIface *iface);
105 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
109 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
112 const gchar *tagname,
113 GMarkupParser *parser,
115 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
118 const gchar *tagname,
122 /* --- variables --- */
123 static const gchar vadjustment_key[] = "gtk-vadjustment";
124 static guint vadjustment_key_id = 0;
125 static const gchar hadjustment_key[] = "gtk-hadjustment";
126 static guint hadjustment_key_id = 0;
127 static GSList *container_resize_queue = NULL;
128 static guint container_signals[LAST_SIGNAL] = { 0 };
129 static GtkWidgetClass *parent_class = NULL;
130 extern GParamSpecPool *_gtk_widget_child_property_pool;
131 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
132 static GtkBuildableIface *parent_buildable_iface;
135 /* --- functions --- */
137 gtk_container_get_type (void)
139 static GType container_type = 0;
143 const GTypeInfo container_info =
145 sizeof (GtkContainerClass),
146 (GBaseInitFunc) gtk_container_base_class_init,
147 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
148 (GClassInitFunc) gtk_container_class_init,
149 NULL /* class_finalize */,
150 NULL /* class_data */,
151 sizeof (GtkContainer),
153 (GInstanceInitFunc) gtk_container_init,
154 NULL, /* value_table */
157 static const GInterfaceInfo buildable_info =
159 (GInterfaceInitFunc) gtk_container_buildable_init,
165 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
166 &container_info, G_TYPE_FLAG_ABSTRACT);
168 g_type_add_interface_static (container_type,
174 return container_type;
178 gtk_container_base_class_init (GtkContainerClass *class)
180 /* reset instance specifc class fields that don't get inherited */
181 class->set_child_property = NULL;
182 class->get_child_property = NULL;
186 gtk_container_base_class_finalize (GtkContainerClass *class)
190 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
191 for (node = list; node; node = node->next)
193 GParamSpec *pspec = node->data;
195 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
196 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
197 g_param_spec_unref (pspec);
203 gtk_container_class_init (GtkContainerClass *class)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
206 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
207 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
209 parent_class = g_type_class_peek_parent (class);
211 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
212 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
214 gobject_class->set_property = gtk_container_set_property;
215 gobject_class->get_property = gtk_container_get_property;
217 object_class->destroy = gtk_container_destroy;
219 widget_class->show_all = gtk_container_show_all;
220 widget_class->hide_all = gtk_container_hide_all;
221 widget_class->expose_event = gtk_container_expose;
222 widget_class->map = gtk_container_map;
223 widget_class->unmap = gtk_container_unmap;
224 widget_class->focus = gtk_container_focus;
226 class->add = gtk_container_add_unimplemented;
227 class->remove = gtk_container_remove_unimplemented;
228 class->check_resize = gtk_container_real_check_resize;
229 class->forall = NULL;
230 class->set_focus_child = gtk_container_real_set_focus_child;
231 class->child_type = NULL;
232 class->composite_name = gtk_container_child_default_composite_name;
234 g_object_class_install_property (gobject_class,
236 g_param_spec_enum ("resize-mode",
238 P_("Specify how resize events are handled"),
239 GTK_TYPE_RESIZE_MODE,
241 GTK_PARAM_READWRITE));
242 g_object_class_install_property (gobject_class,
244 g_param_spec_uint ("border-width",
246 P_("The width of the empty border outside the containers children"),
250 GTK_PARAM_READWRITE));
251 g_object_class_install_property (gobject_class,
253 g_param_spec_object ("child",
255 P_("Can be used to add a new child to the container"),
257 GTK_PARAM_WRITABLE));
258 container_signals[ADD] =
259 g_signal_new (I_("add"),
260 G_OBJECT_CLASS_TYPE (object_class),
262 G_STRUCT_OFFSET (GtkContainerClass, add),
264 _gtk_marshal_VOID__OBJECT,
267 container_signals[REMOVE] =
268 g_signal_new (I_("remove"),
269 G_OBJECT_CLASS_TYPE (object_class),
271 G_STRUCT_OFFSET (GtkContainerClass, remove),
273 _gtk_marshal_VOID__OBJECT,
276 container_signals[CHECK_RESIZE] =
277 g_signal_new (I_("check_resize"),
278 G_OBJECT_CLASS_TYPE (object_class),
280 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
282 _gtk_marshal_VOID__VOID,
284 container_signals[SET_FOCUS_CHILD] =
285 g_signal_new (I_("set-focus-child"),
286 G_OBJECT_CLASS_TYPE (object_class),
288 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
290 _gtk_marshal_VOID__OBJECT,
296 gtk_container_buildable_init (GtkBuildableIface *iface)
298 parent_buildable_iface = g_type_interface_peek_parent (iface);
299 iface->add_child = gtk_container_buildable_add_child;
300 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
301 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
305 gtk_container_buildable_add_child (GtkBuildable *buildable,
310 g_return_if_fail (GTK_IS_WIDGET (child));
312 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
316 gtk_container_buildable_set_child_property (GtkContainer *container,
323 GValue gvalue = { 0, };
324 GError *error = NULL;
326 pspec = gtk_container_class_find_child_property
327 (G_OBJECT_GET_CLASS (container), name);
330 g_warning ("%s does not have a property called %s",
331 g_type_name (G_OBJECT_TYPE (container)), name);
335 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
337 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
338 g_type_name (G_OBJECT_TYPE (container)),
341 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
343 g_error_free (error);
347 gtk_container_child_set_property (container, child, name, &gvalue);
348 g_value_unset (&gvalue);
353 GtkContainer *container;
355 gchar *child_prop_name;
356 } PackingPropertiesData;
359 attributes_start_element (GMarkupParseContext *context,
360 const gchar *element_name,
362 const gchar **values,
366 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
369 if (strcmp (element_name, "property") == 0)
370 for (i = 0; names[i]; i++)
371 if (strcmp (names[i], "name") == 0)
372 parser_data->child_prop_name = g_strdup (values[i]);
373 else if (strcmp (element_name, "packing") == 0)
376 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
380 attributes_text_element (GMarkupParseContext *context,
386 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
388 if (!parser_data->child_prop_name)
391 gtk_container_buildable_set_child_property (parser_data->container,
392 parser_data->builder,
394 parser_data->child_prop_name,
397 g_free (parser_data->child_prop_name);
398 parser_data->child_prop_name = NULL;
401 static const GMarkupParser attributes_parser =
403 attributes_start_element,
405 attributes_text_element,
409 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
412 const gchar *tagname,
413 GMarkupParser *parser,
416 PackingPropertiesData *parser_data;
418 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
419 tagname, parser, data))
422 if (child && strcmp (tagname, "packing") == 0)
424 parser_data = g_slice_new0 (PackingPropertiesData);
425 parser_data->builder = builder;
426 parser_data->container = GTK_CONTAINER (buildable);
427 parser_data->child = GTK_WIDGET (child);
428 parser_data->child_prop_name = NULL;
430 *parser = attributes_parser;
439 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
442 const gchar *tagname,
445 if (strcmp (tagname, "packing") == 0)
447 g_slice_free (PackingPropertiesData, (gpointer)data);
452 if (parent_buildable_iface->custom_tag_end)
453 parent_buildable_iface->custom_tag_end (buildable, builder,
454 child, tagname, data);
459 * gtk_container_child_type:
460 * @container: a #GtkContainer
462 * Returns the type of the children supported by the container.
464 * Note that this may return %G_TYPE_NONE to indicate that no more
465 * children can be added, e.g. for a #GtkPaned which already has two
468 * Return value: a #GType.
471 gtk_container_child_type (GtkContainer *container)
474 GtkContainerClass *class;
476 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
478 class = GTK_CONTAINER_GET_CLASS (container);
479 if (class->child_type)
480 slot = class->child_type (container);
487 /* --- GtkContainer child property mechanism --- */
489 container_get_child_property (GtkContainer *container,
494 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
496 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
500 container_set_child_property (GtkContainer *container,
504 GObjectNotifyQueue *nqueue)
506 GValue tmp_value = { 0, };
507 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
509 /* provide a copy to work from, convert (if necessary) and validate */
510 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
511 if (!g_value_transform (value, &tmp_value))
512 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
514 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
515 G_VALUE_TYPE_NAME (value));
516 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
518 gchar *contents = g_strdup_value_contents (value);
520 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
522 G_VALUE_TYPE_NAME (value),
524 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
529 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
530 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
532 g_value_unset (&tmp_value);
536 * gtk_container_child_get_valist:
537 * @container: a #GtkContainer
538 * @child: a widget which is a child of @container
539 * @first_property_name: the name of the first property to get
540 * @var_args: return location for the first property, followed
541 * optionally by more name/return location pairs, followed by %NULL
543 * Gets the values of one or more child properties for @child and @container.
546 gtk_container_child_get_valist (GtkContainer *container,
548 const gchar *first_property_name,
553 g_return_if_fail (GTK_IS_CONTAINER (container));
554 g_return_if_fail (GTK_IS_WIDGET (child));
555 g_return_if_fail (child->parent == GTK_WIDGET (container));
557 g_object_ref (container);
558 g_object_ref (child);
560 name = first_property_name;
563 GValue value = { 0, };
567 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
569 G_OBJECT_TYPE (container),
573 g_warning ("%s: container class `%s' has no child property named `%s'",
575 G_OBJECT_TYPE_NAME (container),
579 if (!(pspec->flags & G_PARAM_READABLE))
581 g_warning ("%s: child property `%s' of container class `%s' is not readable",
584 G_OBJECT_TYPE_NAME (container));
587 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
588 container_get_child_property (container, child, pspec, &value);
589 G_VALUE_LCOPY (&value, var_args, 0, &error);
592 g_warning ("%s: %s", G_STRLOC, error);
594 g_value_unset (&value);
597 g_value_unset (&value);
598 name = va_arg (var_args, gchar*);
601 g_object_unref (child);
602 g_object_unref (container);
606 * gtk_container_child_get_property:
607 * @container: a #GtkContainer
608 * @child: a widget which is a child of @container
609 * @property_name: the name of the property to get
610 * @value: a location to return the value
612 * Gets the value of a child property for @child and @container.
615 gtk_container_child_get_property (GtkContainer *container,
617 const gchar *property_name,
622 g_return_if_fail (GTK_IS_CONTAINER (container));
623 g_return_if_fail (GTK_IS_WIDGET (child));
624 g_return_if_fail (child->parent == GTK_WIDGET (container));
625 g_return_if_fail (property_name != NULL);
626 g_return_if_fail (G_IS_VALUE (value));
628 g_object_ref (container);
629 g_object_ref (child);
630 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
631 G_OBJECT_TYPE (container), TRUE);
633 g_warning ("%s: container class `%s' has no child property named `%s'",
635 G_OBJECT_TYPE_NAME (container),
637 else if (!(pspec->flags & G_PARAM_READABLE))
638 g_warning ("%s: child property `%s' of container class `%s' is not readable",
641 G_OBJECT_TYPE_NAME (container));
644 GValue *prop_value, tmp_value = { 0, };
646 /* auto-conversion of the callers value type
648 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
650 g_value_reset (value);
653 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
655 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
657 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
658 G_VALUE_TYPE_NAME (value));
659 g_object_unref (child);
660 g_object_unref (container);
665 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
666 prop_value = &tmp_value;
668 container_get_child_property (container, child, pspec, prop_value);
669 if (prop_value != value)
671 g_value_transform (prop_value, value);
672 g_value_unset (&tmp_value);
675 g_object_unref (child);
676 g_object_unref (container);
680 * gtk_container_child_set_valist:
681 * @container: a #GtkContainer
682 * @child: a widget which is a child of @container
683 * @first_property_name: the name of the first property to set
684 * @var_args: a %NULL-terminated list of property names and values, starting
685 * with @first_prop_name
687 * Sets one or more child properties for @child and @container.
690 gtk_container_child_set_valist (GtkContainer *container,
692 const gchar *first_property_name,
695 GObjectNotifyQueue *nqueue;
698 g_return_if_fail (GTK_IS_CONTAINER (container));
699 g_return_if_fail (GTK_IS_WIDGET (child));
700 g_return_if_fail (child->parent == GTK_WIDGET (container));
702 g_object_ref (container);
703 g_object_ref (child);
705 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
706 name = first_property_name;
709 GValue value = { 0, };
711 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
713 G_OBJECT_TYPE (container),
717 g_warning ("%s: container class `%s' has no child property named `%s'",
719 G_OBJECT_TYPE_NAME (container),
723 if (!(pspec->flags & G_PARAM_WRITABLE))
725 g_warning ("%s: child property `%s' of container class `%s' is not writable",
728 G_OBJECT_TYPE_NAME (container));
731 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
732 G_VALUE_COLLECT (&value, var_args, 0, &error);
735 g_warning ("%s: %s", G_STRLOC, error);
738 /* we purposely leak the value here, it might not be
739 * in a sane state if an error condition occoured
743 container_set_child_property (container, child, pspec, &value, nqueue);
744 g_value_unset (&value);
745 name = va_arg (var_args, gchar*);
747 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
749 g_object_unref (container);
750 g_object_unref (child);
754 * gtk_container_child_set_property:
755 * @container: a #GtkContainer
756 * @child: a widget which is a child of @container
757 * @property_name: the name of the property to set
758 * @value: the value to set the property to
760 * Sets a child property for @child and @container.
763 gtk_container_child_set_property (GtkContainer *container,
765 const gchar *property_name,
768 GObjectNotifyQueue *nqueue;
771 g_return_if_fail (GTK_IS_CONTAINER (container));
772 g_return_if_fail (GTK_IS_WIDGET (child));
773 g_return_if_fail (child->parent == GTK_WIDGET (container));
774 g_return_if_fail (property_name != NULL);
775 g_return_if_fail (G_IS_VALUE (value));
777 g_object_ref (container);
778 g_object_ref (child);
780 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
781 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
782 G_OBJECT_TYPE (container), TRUE);
784 g_warning ("%s: container class `%s' has no child property named `%s'",
786 G_OBJECT_TYPE_NAME (container),
788 else if (!(pspec->flags & G_PARAM_WRITABLE))
789 g_warning ("%s: child property `%s' of container class `%s' is not writable",
792 G_OBJECT_TYPE_NAME (container));
795 container_set_child_property (container, child, pspec, value, nqueue);
797 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
798 g_object_unref (container);
799 g_object_unref (child);
803 * gtk_container_add_with_properties:
804 * @container: a #GtkContainer
805 * @widget: a widget to be placed inside @container
806 * @first_prop_name: the name of the first child property to set
807 * @Varargs: a %NULL-terminated list of property names and values, starting
808 * with @first_prop_name
810 * Adds @widget to @container, setting child properties at the same time.
811 * See gtk_container_add() and gtk_container_child_set() for more details.
814 gtk_container_add_with_properties (GtkContainer *container,
816 const gchar *first_prop_name,
819 g_return_if_fail (GTK_IS_CONTAINER (container));
820 g_return_if_fail (GTK_IS_WIDGET (widget));
821 g_return_if_fail (widget->parent == NULL);
823 g_object_ref (container);
824 g_object_ref (widget);
825 gtk_widget_freeze_child_notify (widget);
827 g_signal_emit (container, container_signals[ADD], 0, widget);
832 va_start (var_args, first_prop_name);
833 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
837 gtk_widget_thaw_child_notify (widget);
838 g_object_unref (widget);
839 g_object_unref (container);
843 * gtk_container_child_set:
844 * @container: a #GtkContainer
845 * @child: a widget which is a child of @container
846 * @first_prop_name: the name of the first property to set
847 * @Varargs: a %NULL-terminated list of property names and values, starting
848 * with @first_prop_name
850 * Sets one or more child properties for @child and @container.
853 gtk_container_child_set (GtkContainer *container,
855 const gchar *first_prop_name,
860 g_return_if_fail (GTK_IS_CONTAINER (container));
861 g_return_if_fail (GTK_IS_WIDGET (child));
862 g_return_if_fail (child->parent == GTK_WIDGET (container));
864 va_start (var_args, first_prop_name);
865 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
870 * gtk_container_child_get:
871 * @container: a #GtkContainer
872 * @child: a widget which is a child of @container
873 * @first_prop_name: the name of the first property to get
874 * @Varargs: return location for the first property, followed
875 * optionally by more name/return location pairs, followed by %NULL
877 * Gets the values of one or more child properties for @child and @container.
880 gtk_container_child_get (GtkContainer *container,
882 const gchar *first_prop_name,
887 g_return_if_fail (GTK_IS_CONTAINER (container));
888 g_return_if_fail (GTK_IS_WIDGET (child));
889 g_return_if_fail (child->parent == GTK_WIDGET (container));
891 va_start (var_args, first_prop_name);
892 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
897 * gtk_container_class_install_child_property:
898 * @cclass: a #GtkContainerClass
899 * @property_id: the id for the property
900 * @pspec: the #GParamSpec for the property
902 * Installs a child property on a container class.
905 gtk_container_class_install_child_property (GtkContainerClass *cclass,
909 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
910 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
911 if (pspec->flags & G_PARAM_WRITABLE)
912 g_return_if_fail (cclass->set_child_property != NULL);
913 if (pspec->flags & G_PARAM_READABLE)
914 g_return_if_fail (cclass->get_child_property != NULL);
915 g_return_if_fail (property_id > 0);
916 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
917 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
918 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
920 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
922 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
923 G_OBJECT_CLASS_NAME (cclass),
927 g_param_spec_ref (pspec);
928 g_param_spec_sink (pspec);
929 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
930 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
934 * gtk_container_class_find_child_property:
935 * @cclass: a #GtkContainerClass
936 * @property_name: the name of the child property to find
937 * @returns: the #GParamSpec of the child property or %NULL if @class has no
938 * child property with that name.
940 * Finds a child property of a container class by name.
943 gtk_container_class_find_child_property (GObjectClass *cclass,
944 const gchar *property_name)
946 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
947 g_return_val_if_fail (property_name != NULL, NULL);
949 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
951 G_OBJECT_CLASS_TYPE (cclass),
956 * gtk_container_class_list_child_properties:
957 * @cclass: a #GtkContainerClass
958 * @n_properties: location to return the number of child properties found
959 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
960 * The array must be freed with g_free().
962 * Returns all child properties of a container class.
965 gtk_container_class_list_child_properties (GObjectClass *cclass,
971 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
973 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
974 G_OBJECT_CLASS_TYPE (cclass),
983 gtk_container_add_unimplemented (GtkContainer *container,
986 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
990 gtk_container_remove_unimplemented (GtkContainer *container,
993 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
997 gtk_container_init (GtkContainer *container)
999 container->focus_child = NULL;
1000 container->border_width = 0;
1001 container->need_resize = FALSE;
1002 container->resize_mode = GTK_RESIZE_PARENT;
1003 container->reallocate_redraws = FALSE;
1007 gtk_container_destroy (GtkObject *object)
1009 GtkContainer *container = GTK_CONTAINER (object);
1011 if (GTK_CONTAINER_RESIZE_PENDING (container))
1012 _gtk_container_dequeue_resize_handler (container);
1014 /* do this before walking child widgets, to avoid
1015 * removing children from focus chain one by one.
1017 if (container->has_focus_chain)
1018 gtk_container_unset_focus_chain (container);
1020 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1022 if (GTK_OBJECT_CLASS (parent_class)->destroy)
1023 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
1027 gtk_container_set_property (GObject *object,
1029 const GValue *value,
1032 GtkContainer *container = GTK_CONTAINER (object);
1036 case PROP_BORDER_WIDTH:
1037 gtk_container_set_border_width (container, g_value_get_uint (value));
1039 case PROP_RESIZE_MODE:
1040 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1043 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1046 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1052 gtk_container_get_property (GObject *object,
1057 GtkContainer *container = GTK_CONTAINER (object);
1061 case PROP_BORDER_WIDTH:
1062 g_value_set_uint (value, container->border_width);
1064 case PROP_RESIZE_MODE:
1065 g_value_set_enum (value, container->resize_mode);
1068 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1074 * gtk_container_set_border_width:
1075 * @container: a #GtkContainer
1076 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1077 * the container. Valid values are in the range 0-65535 pixels.
1079 * Sets the border width of the container.
1081 * The border width of a container is the amount of space to leave
1082 * around the outside of the container. The only exception to this is
1083 * #GtkWindow; because toplevel windows can't leave space outside,
1084 * they leave the space inside. The border is added on all sides of
1085 * the container. To add space to only one side, one approach is to
1086 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1087 * to give it a size, and place it on the side of the container as
1091 gtk_container_set_border_width (GtkContainer *container,
1094 g_return_if_fail (GTK_IS_CONTAINER (container));
1096 if (container->border_width != border_width)
1098 container->border_width = border_width;
1099 g_object_notify (G_OBJECT (container), "border-width");
1101 if (GTK_WIDGET_REALIZED (container))
1102 gtk_widget_queue_resize (GTK_WIDGET (container));
1107 * gtk_container_get_border_width:
1108 * @container: a #GtkContainer
1110 * Retrieves the border width of the container. See
1111 * gtk_container_set_border_width().
1113 * Return value: the current border width
1116 gtk_container_get_border_width (GtkContainer *container)
1118 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1120 return container->border_width;
1124 * gtk_container_add:
1125 * @container: a #GtkContainer
1126 * @widget: a widget to be placed inside @container
1128 * Adds @widget to @container. Typically used for simple containers
1129 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1130 * layout containers such as #GtkBox or #GtkTable, this function will
1131 * pick default packing parameters that may not be correct. So
1132 * consider functions such as gtk_box_pack_start() and
1133 * gtk_table_attach() as an alternative to gtk_container_add() in
1134 * those cases. A widget may be added to only one container at a time;
1135 * you can't place the same widget inside two different containers.
1138 gtk_container_add (GtkContainer *container,
1141 g_return_if_fail (GTK_IS_CONTAINER (container));
1142 g_return_if_fail (GTK_IS_WIDGET (widget));
1144 if (widget->parent != NULL)
1146 g_warning ("Attempting to add a widget with type %s to a container of "
1147 "type %s, but the widget is already inside a container of type %s, "
1148 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
1149 g_type_name (G_OBJECT_TYPE (widget)),
1150 g_type_name (G_OBJECT_TYPE (container)),
1151 g_type_name (G_OBJECT_TYPE (widget->parent)));
1155 g_signal_emit (container, container_signals[ADD], 0, widget);
1159 * gtk_container_remove:
1160 * @container: a #GtkContainer
1161 * @widget: a current child of @container
1163 * Removes @widget from @container. @widget must be inside @container.
1164 * Note that @container will own a reference to @widget, and that this
1165 * may be the last reference held; so removing a widget from its
1166 * container can destroy that widget. If you want to use @widget
1167 * again, you need to add a reference to it while it's not inside
1168 * a container, using g_object_ref(). If you don't want to use @widget
1169 * again it's usually more efficient to simply destroy it directly
1170 * using gtk_widget_destroy() since this will remove it from the
1171 * container and help break any circular reference count cycles.
1174 gtk_container_remove (GtkContainer *container,
1177 g_return_if_fail (GTK_IS_CONTAINER (container));
1178 g_return_if_fail (GTK_IS_WIDGET (widget));
1180 /* When using the deprecated API of the toolbar, it is possible
1181 * to legitimately call this function with a widget that is not
1182 * a direct child of the container.
1184 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1185 widget->parent == GTK_WIDGET (container));
1187 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1191 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1193 g_return_if_fail (GTK_IS_CONTAINER (container));
1194 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1196 container_resize_queue = g_slist_remove (container_resize_queue, container);
1197 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1201 * gtk_container_set_resize_mode:
1202 * @container: a #GtkContainer
1203 * @resize_mode: the new resize mode
1205 * Sets the resize mode for the container.
1207 * The resize mode of a container determines whether a resize request
1208 * will be passed to the container's parent, queued for later execution
1209 * or executed immediately.
1212 gtk_container_set_resize_mode (GtkContainer *container,
1213 GtkResizeMode resize_mode)
1215 g_return_if_fail (GTK_IS_CONTAINER (container));
1216 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1218 if (GTK_WIDGET_TOPLEVEL (container) &&
1219 resize_mode == GTK_RESIZE_PARENT)
1221 resize_mode = GTK_RESIZE_QUEUE;
1224 if (container->resize_mode != resize_mode)
1226 container->resize_mode = resize_mode;
1228 gtk_widget_queue_resize (GTK_WIDGET (container));
1229 g_object_notify (G_OBJECT (container), "resize-mode");
1234 * gtk_container_get_resize_mode:
1235 * @container: a #GtkContainer
1237 * Returns the resize mode for the container. See
1238 * gtk_container_set_resize_mode ().
1240 * Return value: the current resize mode
1243 gtk_container_get_resize_mode (GtkContainer *container)
1245 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1247 return container->resize_mode;
1251 * gtk_container_set_reallocate_redraws:
1252 * @container: a #GtkContainer
1253 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1255 * Sets the @reallocate_redraws flag of the container to the given value.
1257 * Containers requesting reallocation redraws get automatically
1258 * redrawn if any of their children changed allocation.
1261 gtk_container_set_reallocate_redraws (GtkContainer *container,
1262 gboolean needs_redraws)
1264 g_return_if_fail (GTK_IS_CONTAINER (container));
1266 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1269 static GtkContainer*
1270 gtk_container_get_resize_container (GtkContainer *container)
1272 GtkWidget *widget = GTK_WIDGET (container);
1274 while (widget->parent)
1276 widget = widget->parent;
1277 if (GTK_IS_RESIZE_CONTAINER (widget))
1281 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1285 gtk_container_idle_sizer (gpointer data)
1287 /* we may be invoked with a container_resize_queue of NULL, because
1288 * queue_resize could have been adding an extra idle function while
1289 * the queue still got processed. we better just ignore such case
1290 * than trying to explicitely work around them with some extra flags,
1291 * since it doesn't cause any actual harm.
1293 while (container_resize_queue)
1298 slist = container_resize_queue;
1299 container_resize_queue = slist->next;
1300 widget = slist->data;
1301 g_slist_free_1 (slist);
1303 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1304 gtk_container_check_resize (GTK_CONTAINER (widget));
1307 gdk_window_process_all_updates ();
1313 _gtk_container_queue_resize (GtkContainer *container)
1315 GtkContainer *resize_container;
1318 g_return_if_fail (GTK_IS_CONTAINER (container));
1320 widget = GTK_WIDGET (container);
1321 resize_container = gtk_container_get_resize_container (container);
1325 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1326 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1327 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1331 widget = widget->parent;
1334 if (resize_container)
1336 if (GTK_WIDGET_VISIBLE (resize_container) &&
1337 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1339 switch (resize_container->resize_mode)
1341 case GTK_RESIZE_QUEUE:
1342 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1344 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1345 if (container_resize_queue == NULL)
1346 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1347 gtk_container_idle_sizer,
1349 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1353 case GTK_RESIZE_IMMEDIATE:
1354 gtk_container_check_resize (resize_container);
1357 case GTK_RESIZE_PARENT:
1358 g_assert_not_reached ();
1364 /* we need to let hidden resize containers know that something
1365 * changed while they where hidden (currently only evaluated by
1368 resize_container->need_resize = TRUE;
1374 gtk_container_check_resize (GtkContainer *container)
1376 g_return_if_fail (GTK_IS_CONTAINER (container));
1378 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1382 gtk_container_real_check_resize (GtkContainer *container)
1384 GtkWidget *widget = GTK_WIDGET (container);
1385 GtkRequisition requisition;
1387 gtk_widget_size_request (widget, &requisition);
1389 if (requisition.width > widget->allocation.width ||
1390 requisition.height > widget->allocation.height)
1392 if (GTK_IS_RESIZE_CONTAINER (container))
1393 gtk_widget_size_allocate (GTK_WIDGET (container),
1394 >K_WIDGET (container)->allocation);
1396 gtk_widget_queue_resize (widget);
1400 gtk_container_resize_children (container);
1404 /* The container hasn't changed size but one of its children
1405 * queued a resize request. Which means that the allocation
1406 * is not sufficient for the requisition of some child.
1407 * We've already performed a size request at this point,
1408 * so we simply need to reallocate and let the allocation
1409 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1412 gtk_container_resize_children (GtkContainer *container)
1416 /* resizing invariants:
1417 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1418 * containers that have an idle sizer pending must be flagged with
1421 g_return_if_fail (GTK_IS_CONTAINER (container));
1423 widget = GTK_WIDGET (container);
1424 gtk_widget_size_allocate (widget, &widget->allocation);
1428 * gtk_container_forall:
1429 * @container: a #GtkContainer
1430 * @callback: a callback
1431 * @callback_data: callback user data
1433 * Invokes @callback on each child of @container, including children
1434 * that are considered "internal" (implementation details of the
1435 * container). "Internal" children generally weren't added by the user
1436 * of the container, but were added by the container implementation
1437 * itself. Most applications should use gtk_container_foreach(),
1438 * rather than gtk_container_forall().
1441 gtk_container_forall (GtkContainer *container,
1442 GtkCallback callback,
1443 gpointer callback_data)
1445 GtkContainerClass *class;
1447 g_return_if_fail (GTK_IS_CONTAINER (container));
1448 g_return_if_fail (callback != NULL);
1450 class = GTK_CONTAINER_GET_CLASS (container);
1453 class->forall (container, TRUE, callback, callback_data);
1457 * gtk_container_foreach:
1458 * @container: a #GtkContainer
1459 * @callback: a callback
1460 * @callback_data: callback user data
1462 * Invokes @callback on each non-internal child of @container. See
1463 * gtk_container_forall() for details on what constitutes an
1464 * "internal" child. Most applications should use
1465 * gtk_container_foreach(), rather than gtk_container_forall().
1468 gtk_container_foreach (GtkContainer *container,
1469 GtkCallback callback,
1470 gpointer callback_data)
1472 GtkContainerClass *class;
1474 g_return_if_fail (GTK_IS_CONTAINER (container));
1475 g_return_if_fail (callback != NULL);
1477 class = GTK_CONTAINER_GET_CLASS (container);
1480 class->forall (container, FALSE, callback, callback_data);
1483 typedef struct _GtkForeachData GtkForeachData;
1484 struct _GtkForeachData
1486 GtkObject *container;
1487 GtkCallbackMarshal callback;
1488 gpointer callback_data;
1492 gtk_container_foreach_unmarshal (GtkWidget *child,
1495 GtkForeachData *fdata = (GtkForeachData*) data;
1498 /* first argument */
1499 args[0].name = NULL;
1500 args[0].type = G_TYPE_FROM_INSTANCE (child);
1501 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1503 /* location for return value */
1504 args[1].name = NULL;
1505 args[1].type = G_TYPE_NONE;
1507 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1511 gtk_container_foreach_full (GtkContainer *container,
1512 GtkCallback callback,
1513 GtkCallbackMarshal marshal,
1514 gpointer callback_data,
1515 GDestroyNotify notify)
1517 g_return_if_fail (GTK_IS_CONTAINER (container));
1521 GtkForeachData fdata;
1523 fdata.container = GTK_OBJECT (container);
1524 fdata.callback = marshal;
1525 fdata.callback_data = callback_data;
1527 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1531 g_return_if_fail (callback != NULL);
1533 gtk_container_foreach (container, callback, &callback_data);
1537 notify (callback_data);
1541 * gtk_container_set_focus_child:
1542 * @container: a #GtkContainer
1543 * @widget: a #GtkWidget, or %NULL
1545 * Sets, or unsets if @widget is %NULL, the focused child of @container.
1547 * This function emits the GtkContainer::set_focus_child signal of
1548 * @container. Implementations of #GtkContainer can override the
1549 * default behaviour by overriding the class closure of this signal.
1552 gtk_container_set_focus_child (GtkContainer *container,
1555 g_return_if_fail (GTK_IS_CONTAINER (container));
1557 g_return_if_fail (GTK_IS_WIDGET (widget));
1559 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1563 gtk_container_get_focus_child (GtkContainer *container)
1565 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1567 return container->focus_child;
1571 * gtk_container_get_children:
1572 * @container: a #GtkContainer
1574 * Returns the container's non-internal children. See
1575 * gtk_container_forall() for details on what constitutes an "internal" child.
1577 * Return value: a newly-allocated list of the container's non-internal children.
1580 gtk_container_get_children (GtkContainer *container)
1582 GList *children = NULL;
1584 gtk_container_foreach (container,
1585 gtk_container_children_callback,
1588 return g_list_reverse (children);
1592 gtk_container_child_position_callback (GtkWidget *widget,
1593 gpointer client_data)
1599 } *data = client_data;
1602 if (data->child == widget)
1603 data->index = data->i;
1607 gtk_container_child_default_composite_name (GtkContainer *container,
1617 /* fallback implementation */
1621 gtk_container_forall (container,
1622 gtk_container_child_position_callback,
1625 name = g_strdup_printf ("%s-%u",
1626 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1633 _gtk_container_child_composite_name (GtkContainer *container,
1636 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1637 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1638 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1640 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1642 static GQuark quark_composite_name = 0;
1645 if (!quark_composite_name)
1646 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1648 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1651 GtkContainerClass *class;
1653 class = GTK_CONTAINER_GET_CLASS (container);
1654 if (class->composite_name)
1655 name = class->composite_name (container, child);
1658 name = g_strdup (name);
1667 gtk_container_real_set_focus_child (GtkContainer *container,
1670 g_return_if_fail (GTK_IS_CONTAINER (container));
1671 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1673 if (child != container->focus_child)
1675 if (container->focus_child)
1676 g_object_unref (container->focus_child);
1677 container->focus_child = child;
1678 if (container->focus_child)
1679 g_object_ref (container->focus_child);
1683 /* check for h/v adjustments
1685 if (container->focus_child)
1687 GtkAdjustment *hadj;
1688 GtkAdjustment *vadj;
1689 GtkWidget *focus_child;
1692 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1693 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1697 focus_child = container->focus_child;
1698 while (GTK_IS_CONTAINER (focus_child) &&
1699 GTK_CONTAINER (focus_child)->focus_child)
1701 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1704 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1707 x += container->focus_child->allocation.x;
1708 y += container->focus_child->allocation.y;
1711 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1714 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1720 get_focus_chain (GtkContainer *container)
1722 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1725 /* same as gtk_container_get_children, except it includes internals
1728 gtk_container_get_all_children (GtkContainer *container)
1730 GList *children = NULL;
1732 gtk_container_forall (container,
1733 gtk_container_children_callback,
1740 gtk_container_focus (GtkWidget *widget,
1741 GtkDirectionType direction)
1744 GList *sorted_children;
1746 GtkContainer *container;
1748 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1750 container = GTK_CONTAINER (widget);
1754 if (GTK_WIDGET_CAN_FOCUS (container))
1756 if (!GTK_WIDGET_HAS_FOCUS (container))
1758 gtk_widget_grab_focus (GTK_WIDGET (container));
1764 /* Get a list of the containers children, allowing focus
1765 * chain to override.
1767 if (container->has_focus_chain)
1768 children = g_list_copy (get_focus_chain (container));
1770 children = gtk_container_get_all_children (container);
1772 if (container->has_focus_chain &&
1773 (direction == GTK_DIR_TAB_FORWARD ||
1774 direction == GTK_DIR_TAB_BACKWARD))
1776 sorted_children = g_list_copy (children);
1778 if (direction == GTK_DIR_TAB_BACKWARD)
1779 sorted_children = g_list_reverse (sorted_children);
1782 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1784 return_val = gtk_container_focus_move (container, sorted_children, direction);
1786 g_list_free (sorted_children);
1787 g_list_free (children);
1794 tab_compare (gconstpointer a,
1798 const GtkWidget *child1 = a;
1799 const GtkWidget *child2 = b;
1800 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1802 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1803 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1807 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1808 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1810 if (text_direction == GTK_TEXT_DIR_RTL)
1811 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1813 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1816 return (y1 < y2) ? -1 : 1;
1820 gtk_container_focus_sort_tab (GtkContainer *container,
1822 GtkDirectionType direction,
1823 GtkWidget *old_focus)
1825 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1826 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1828 /* if we are going backwards then reverse the order
1831 if (direction == GTK_DIR_TAB_BACKWARD)
1832 children = g_list_reverse (children);
1837 /* Get coordinates of @widget's allocation with respect to
1838 * allocation of @container.
1841 get_allocation_coords (GtkContainer *container,
1843 GdkRectangle *allocation)
1845 *allocation = widget->allocation;
1847 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1848 0, 0, &allocation->x, &allocation->y);
1851 /* Look for a child in @children that is intermediate between
1852 * the focus widget and container. This widget, if it exists,
1853 * acts as the starting widget for focus navigation.
1856 find_old_focus (GtkContainer *container,
1859 GList *tmp_list = children;
1862 GtkWidget *child = tmp_list->data;
1863 GtkWidget *widget = child;
1865 while (widget && widget != (GtkWidget *)container)
1867 GtkWidget *parent = widget->parent;
1868 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1877 tmp_list = tmp_list->next;
1884 old_focus_coords (GtkContainer *container,
1885 GdkRectangle *old_focus_rect)
1887 GtkWidget *widget = GTK_WIDGET (container);
1888 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1890 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1892 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1894 return get_allocation_coords (container, old_focus, old_focus_rect);
1900 typedef struct _CompareInfo CompareInfo;
1904 GtkContainer *container;
1911 up_down_compare (gconstpointer a,
1915 GdkRectangle allocation1;
1916 GdkRectangle allocation2;
1917 CompareInfo *compare = data;
1920 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1921 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1923 y1 = allocation1.y + allocation1.height / 2;
1924 y2 = allocation2.y + allocation2.height / 2;
1928 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1929 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1931 if (compare->reverse)
1932 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1934 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1937 return (y1 < y2) ? -1 : 1;
1941 gtk_container_focus_sort_up_down (GtkContainer *container,
1943 GtkDirectionType direction,
1944 GtkWidget *old_focus)
1946 CompareInfo compare;
1948 GdkRectangle old_allocation;
1950 compare.container = container;
1951 compare.reverse = (direction == GTK_DIR_UP);
1954 old_focus = find_old_focus (container, children);
1956 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1962 /* Delete widgets from list that don't match minimum criteria */
1964 compare_x1 = old_allocation.x;
1965 compare_x2 = old_allocation.x + old_allocation.width;
1967 if (direction == GTK_DIR_UP)
1968 compare_y = old_allocation.y;
1970 compare_y = old_allocation.y + old_allocation.height;
1972 tmp_list = children;
1975 GtkWidget *child = tmp_list->data;
1976 GList *next = tmp_list->next;
1977 gint child_x1, child_x2;
1978 GdkRectangle child_allocation;
1980 if (child != old_focus)
1982 if (get_allocation_coords (container, child, &child_allocation))
1984 child_x1 = child_allocation.x;
1985 child_x2 = child_allocation.x + child_allocation.width;
1987 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1988 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1989 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1991 children = g_list_delete_link (children, tmp_list);
1995 children = g_list_delete_link (children, tmp_list);
2001 compare.x = (compare_x1 + compare_x2) / 2;
2002 compare.y = old_allocation.y + old_allocation.height / 2;
2006 /* No old focus widget, need to figure out starting x,y some other way
2008 GtkWidget *widget = GTK_WIDGET (container);
2009 GdkRectangle old_focus_rect;
2011 if (old_focus_coords (container, &old_focus_rect))
2013 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2017 if (GTK_WIDGET_NO_WINDOW (widget))
2018 compare.x = widget->allocation.x + widget->allocation.width / 2;
2020 compare.x = widget->allocation.width / 2;
2023 if (GTK_WIDGET_NO_WINDOW (widget))
2024 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2026 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2029 children = g_list_sort_with_data (children, up_down_compare, &compare);
2031 if (compare.reverse)
2032 children = g_list_reverse (children);
2038 left_right_compare (gconstpointer a,
2042 GdkRectangle allocation1;
2043 GdkRectangle allocation2;
2044 CompareInfo *compare = data;
2047 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2048 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2050 x1 = allocation1.x + allocation1.width / 2;
2051 x2 = allocation2.x + allocation2.width / 2;
2055 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2056 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2058 if (compare->reverse)
2059 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2061 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2064 return (x1 < x2) ? -1 : 1;
2068 gtk_container_focus_sort_left_right (GtkContainer *container,
2070 GtkDirectionType direction,
2071 GtkWidget *old_focus)
2073 CompareInfo compare;
2075 GdkRectangle old_allocation;
2077 compare.container = container;
2078 compare.reverse = (direction == GTK_DIR_LEFT);
2081 old_focus = find_old_focus (container, children);
2083 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2089 /* Delete widgets from list that don't match minimum criteria */
2091 compare_y1 = old_allocation.y;
2092 compare_y2 = old_allocation.y + old_allocation.height;
2094 if (direction == GTK_DIR_LEFT)
2095 compare_x = old_allocation.x;
2097 compare_x = old_allocation.x + old_allocation.width;
2099 tmp_list = children;
2102 GtkWidget *child = tmp_list->data;
2103 GList *next = tmp_list->next;
2104 gint child_y1, child_y2;
2105 GdkRectangle child_allocation;
2107 if (child != old_focus)
2109 if (get_allocation_coords (container, child, &child_allocation))
2111 child_y1 = child_allocation.y;
2112 child_y2 = child_allocation.y + child_allocation.height;
2114 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2115 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2116 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2118 children = g_list_delete_link (children, tmp_list);
2122 children = g_list_delete_link (children, tmp_list);
2128 compare.y = (compare_y1 + compare_y2) / 2;
2129 compare.x = old_allocation.x + old_allocation.width / 2;
2133 /* No old focus widget, need to figure out starting x,y some other way
2135 GtkWidget *widget = GTK_WIDGET (container);
2136 GdkRectangle old_focus_rect;
2138 if (old_focus_coords (container, &old_focus_rect))
2140 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2144 if (GTK_WIDGET_NO_WINDOW (widget))
2145 compare.y = widget->allocation.y + widget->allocation.height / 2;
2147 compare.y = widget->allocation.height / 2;
2150 if (GTK_WIDGET_NO_WINDOW (widget))
2151 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2153 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2156 children = g_list_sort_with_data (children, left_right_compare, &compare);
2158 if (compare.reverse)
2159 children = g_list_reverse (children);
2165 * gtk_container_focus_sort:
2166 * @container: a #GtkContainer
2167 * @children: a list of descendents of @container (they don't
2168 * have to be direct children)
2169 * @direction: focus direction
2170 * @old_focus: widget to use for the starting position, or %NULL
2171 * to determine this automatically.
2172 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2173 * which is the only @direction we use currently,
2174 * so perhaps this argument should be removed)
2176 * Sorts @children in the correct order for focusing with
2177 * direction type @direction.
2179 * Return value: a copy of @children, sorted in correct focusing order,
2180 * with children that aren't suitable for focusing in this direction
2184 _gtk_container_focus_sort (GtkContainer *container,
2186 GtkDirectionType direction,
2187 GtkWidget *old_focus)
2189 GList *visible_children = NULL;
2193 if (GTK_WIDGET_REALIZED (children->data))
2194 visible_children = g_list_prepend (visible_children, children->data);
2195 children = children->next;
2200 case GTK_DIR_TAB_FORWARD:
2201 case GTK_DIR_TAB_BACKWARD:
2202 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2205 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2208 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2211 g_assert_not_reached ();
2217 gtk_container_focus_move (GtkContainer *container,
2219 GtkDirectionType direction)
2221 GtkWidget *focus_child;
2224 focus_child = container->focus_child;
2228 child = children->data;
2229 children = children->next;
2236 if (focus_child == child)
2240 if (gtk_widget_child_focus (child, direction))
2244 else if (GTK_WIDGET_DRAWABLE (child) &&
2245 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2247 if (gtk_widget_child_focus (child, direction))
2257 gtk_container_children_callback (GtkWidget *widget,
2258 gpointer client_data)
2262 children = (GList**) client_data;
2263 *children = g_list_prepend (*children, widget);
2267 chain_widget_destroyed (GtkWidget *widget,
2270 GtkContainer *container;
2273 container = GTK_CONTAINER (user_data);
2275 chain = g_object_get_data (G_OBJECT (container),
2276 "gtk-container-focus-chain");
2278 chain = g_list_remove (chain, widget);
2280 g_signal_handlers_disconnect_by_func (widget,
2281 chain_widget_destroyed,
2284 g_object_set_data (G_OBJECT (container),
2285 I_("gtk-container-focus-chain"),
2290 * gtk_container_set_focus_chain:
2291 * @container: a #GtkContainer
2292 * @focusable_widgets: the new focus chain
2294 * Sets a focus chain, overriding the one computed automatically by GTK+.
2296 * In principle each widget in the chain should be a descendant of the
2297 * container, but this is not enforced by this method, since it's allowed
2298 * to set the focus chain before you pack the widgets, or have a widget
2299 * in the chain that isn't always packed. The necessary checks are done
2300 * when the focus chain is actually traversed.
2303 gtk_container_set_focus_chain (GtkContainer *container,
2304 GList *focusable_widgets)
2309 g_return_if_fail (GTK_IS_CONTAINER (container));
2311 if (container->has_focus_chain)
2312 gtk_container_unset_focus_chain (container);
2314 container->has_focus_chain = TRUE;
2317 tmp_list = focusable_widgets;
2318 while (tmp_list != NULL)
2320 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2322 /* In principle each widget in the chain should be a descendant
2323 * of the container, but we don't want to check that here, it's
2324 * expensive and also it's allowed to set the focus chain before
2325 * you pack the widgets, or have a widget in the chain that isn't
2326 * always packed. So we check for ancestor during actual traversal.
2329 chain = g_list_prepend (chain, tmp_list->data);
2331 g_signal_connect (tmp_list->data,
2333 G_CALLBACK (chain_widget_destroyed),
2336 tmp_list = g_list_next (tmp_list);
2339 chain = g_list_reverse (chain);
2341 g_object_set_data (G_OBJECT (container),
2342 I_("gtk-container-focus-chain"),
2347 * gtk_container_get_focus_chain:
2348 * @container: a #GtkContainer
2349 * @focusable_widgets: location to store the focus chain of the
2350 * container, or %NULL. You should free this list
2351 * using g_list_free() when you are done with it, however
2352 * no additional reference count is added to the
2353 * individual widgets in the focus chain.
2355 * Retrieves the focus chain of the container, if one has been
2356 * set explicitly. If no focus chain has been explicitly
2357 * set, GTK+ computes the focus chain based on the positions
2358 * of the children. In that case, GTK+ stores %NULL in
2359 * @focusable_widgets and returns %FALSE.
2361 * Return value: %TRUE if the focus chain of the container
2362 * has been set explicitly.
2365 gtk_container_get_focus_chain (GtkContainer *container,
2366 GList **focus_chain)
2368 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2372 if (container->has_focus_chain)
2373 *focus_chain = g_list_copy (get_focus_chain (container));
2375 *focus_chain = NULL;
2378 return container->has_focus_chain;
2382 * gtk_container_unset_focus_chain:
2383 * @container: a #GtkContainer
2385 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2388 gtk_container_unset_focus_chain (GtkContainer *container)
2390 g_return_if_fail (GTK_IS_CONTAINER (container));
2392 if (container->has_focus_chain)
2397 chain = get_focus_chain (container);
2399 container->has_focus_chain = FALSE;
2401 g_object_set_data (G_OBJECT (container),
2402 I_("gtk-container-focus-chain"),
2406 while (tmp_list != NULL)
2408 g_signal_handlers_disconnect_by_func (tmp_list->data,
2409 chain_widget_destroyed,
2412 tmp_list = g_list_next (tmp_list);
2415 g_list_free (chain);
2420 * gtk_container_set_focus_vadjustment:
2421 * @container: a #GtkContainer
2422 * @adjustment: an adjustment which should be adjusted when the focus
2423 * is moved among the descendents of @container
2425 * Hooks up an adjustment to focus handling in a container, so when a
2426 * child of the container is focused, the adjustment is scrolled to
2427 * show that widget. This function sets the vertical alignment. See
2428 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2429 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2430 * the horizontal adjustment.
2432 * The adjustments have to be in pixel units and in the same coordinate
2433 * system as the allocation for immediate children of the container.
2436 gtk_container_set_focus_vadjustment (GtkContainer *container,
2437 GtkAdjustment *adjustment)
2439 g_return_if_fail (GTK_IS_CONTAINER (container));
2441 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2444 g_object_ref (adjustment);
2446 g_object_set_qdata_full (G_OBJECT (container),
2453 * gtk_container_get_focus_vadjustment:
2454 * @container: a #GtkContainer
2456 * Retrieves the vertical focus adjustment for the container. See
2457 * gtk_container_set_focus_vadjustment().
2459 * Return value: the vertical focus adjustment, or %NULL if
2460 * none has been set.
2463 gtk_container_get_focus_vadjustment (GtkContainer *container)
2465 GtkAdjustment *vadjustment;
2467 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2469 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2475 * gtk_container_set_focus_hadjustment:
2476 * @container: a #GtkContainer
2477 * @adjustment: an adjustment which should be adjusted when the focus is
2478 * moved among the descendents of @container
2480 * Hooks up an adjustment to focus handling in a container, so when a child
2481 * of the container is focused, the adjustment is scrolled to show that
2482 * widget. This function sets the horizontal alignment.
2483 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2484 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2485 * the vertical adjustment.
2487 * The adjustments have to be in pixel units and in the same coordinate
2488 * system as the allocation for immediate children of the container.
2491 gtk_container_set_focus_hadjustment (GtkContainer *container,
2492 GtkAdjustment *adjustment)
2494 g_return_if_fail (GTK_IS_CONTAINER (container));
2496 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2499 g_object_ref (adjustment);
2501 g_object_set_qdata_full (G_OBJECT (container),
2508 * gtk_container_get_focus_hadjustment:
2509 * @container: a #GtkContainer
2511 * Retrieves the horizontal focus adjustment for the container. See
2512 * gtk_container_set_focus_hadjustment ().
2514 * Return value: the horizontal focus adjustment, or %NULL if
2515 * none has been set.
2518 gtk_container_get_focus_hadjustment (GtkContainer *container)
2520 GtkAdjustment *hadjustment;
2522 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2524 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2531 gtk_container_show_all (GtkWidget *widget)
2533 g_return_if_fail (GTK_IS_CONTAINER (widget));
2535 gtk_container_foreach (GTK_CONTAINER (widget),
2536 (GtkCallback) gtk_widget_show_all,
2538 gtk_widget_show (widget);
2542 gtk_container_hide_all (GtkWidget *widget)
2544 g_return_if_fail (GTK_IS_CONTAINER (widget));
2546 gtk_widget_hide (widget);
2547 gtk_container_foreach (GTK_CONTAINER (widget),
2548 (GtkCallback) gtk_widget_hide_all,
2554 gtk_container_expose_child (GtkWidget *child,
2555 gpointer client_data)
2558 GtkWidget *container;
2559 GdkEventExpose *event;
2560 } *data = client_data;
2562 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2568 gtk_container_expose (GtkWidget *widget,
2569 GdkEventExpose *event)
2572 GtkWidget *container;
2573 GdkEventExpose *event;
2576 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2577 g_return_val_if_fail (event != NULL, FALSE);
2580 if (GTK_WIDGET_DRAWABLE (widget))
2582 data.container = widget;
2585 gtk_container_forall (GTK_CONTAINER (widget),
2586 gtk_container_expose_child,
2594 gtk_container_map_child (GtkWidget *child,
2595 gpointer client_data)
2597 if (GTK_WIDGET_VISIBLE (child) &&
2598 GTK_WIDGET_CHILD_VISIBLE (child) &&
2599 !GTK_WIDGET_MAPPED (child))
2600 gtk_widget_map (child);
2604 gtk_container_map (GtkWidget *widget)
2606 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2608 gtk_container_forall (GTK_CONTAINER (widget),
2609 gtk_container_map_child,
2612 if (!GTK_WIDGET_NO_WINDOW (widget))
2613 gdk_window_show (widget->window);
2617 gtk_container_unmap (GtkWidget *widget)
2619 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2621 if (!GTK_WIDGET_NO_WINDOW (widget))
2622 gdk_window_hide (widget->window);
2624 gtk_container_forall (GTK_CONTAINER (widget),
2625 (GtkCallback)gtk_widget_unmap,
2630 * gtk_container_propagate_expose:
2631 * @container: a #GtkContainer
2632 * @child: a child of @container
2633 * @event: a expose event sent to container
2635 * When a container receives an expose event, it must send synthetic
2636 * expose events to all children that don't have their own #GdkWindows.
2637 * This function provides a convenient way of doing this. A container,
2638 * when it receives an expose event, calls gtk_container_propagate_expose()
2639 * once for each child, passing in the event the container received.
2641 * gtk_container_propagate_expose() takes care of deciding whether
2642 * an expose event needs to be sent to the child, intersecting
2643 * the event's area with the child area, and sending the event.
2645 * In most cases, a container can simply either simply inherit the
2646 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2647 * and then chain to the ::expose implementation from #GtkContainer.
2650 gtk_container_propagate_expose (GtkContainer *container,
2652 GdkEventExpose *event)
2654 GdkEvent *child_event;
2656 g_return_if_fail (GTK_IS_CONTAINER (container));
2657 g_return_if_fail (GTK_IS_WIDGET (child));
2658 g_return_if_fail (event != NULL);
2660 g_assert (child->parent == GTK_WIDGET (container));
2662 if (GTK_WIDGET_DRAWABLE (child) &&
2663 GTK_WIDGET_NO_WINDOW (child) &&
2664 (child->window == event->window))
2666 child_event = gdk_event_new (GDK_EXPOSE);
2667 child_event->expose = *event;
2668 g_object_ref (child_event->expose.window);
2670 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2671 if (!gdk_region_empty (child_event->expose.region))
2673 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2674 gtk_widget_send_expose (child, child_event);
2676 gdk_event_free (child_event);
2680 #define __GTK_CONTAINER_C__
2681 #include "gtkaliasdef.c"