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 "gtkbuilderprivate.h"
35 #include "gtkprivate.h"
37 #include "gtkmarshalers.h"
38 #include "gtkwindow.h"
40 #include "gtktoolbar.h"
41 #include <gobject/gobjectnotifyqueue.c>
42 #include <gobject/gvaluecollector.h>
61 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
62 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
65 /* --- prototypes --- */
66 static void gtk_container_base_class_init (GtkContainerClass *klass);
67 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
68 static void gtk_container_class_init (GtkContainerClass *klass);
69 static void gtk_container_init (GtkContainer *container);
70 static void gtk_container_destroy (GtkObject *object);
71 static void gtk_container_set_property (GObject *object,
75 static void gtk_container_get_property (GObject *object,
79 static void gtk_container_add_unimplemented (GtkContainer *container,
81 static void gtk_container_remove_unimplemented (GtkContainer *container,
83 static void gtk_container_real_check_resize (GtkContainer *container);
84 static gboolean gtk_container_focus (GtkWidget *widget,
85 GtkDirectionType direction);
86 static void gtk_container_real_set_focus_child (GtkContainer *container,
89 static gboolean gtk_container_focus_move (GtkContainer *container,
91 GtkDirectionType direction);
92 static void gtk_container_children_callback (GtkWidget *widget,
93 gpointer client_data);
94 static void gtk_container_show_all (GtkWidget *widget);
95 static void gtk_container_hide_all (GtkWidget *widget);
96 static gint gtk_container_expose (GtkWidget *widget,
97 GdkEventExpose *event);
98 static void gtk_container_map (GtkWidget *widget);
99 static void gtk_container_unmap (GtkWidget *widget);
101 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
105 static void gtk_container_buildable_init (GtkBuildableIface *iface);
106 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
110 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
113 const gchar *tagname,
114 GMarkupParser *parser,
116 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
119 const gchar *tagname,
123 /* --- variables --- */
124 static const gchar vadjustment_key[] = "gtk-vadjustment";
125 static guint vadjustment_key_id = 0;
126 static const gchar hadjustment_key[] = "gtk-hadjustment";
127 static guint hadjustment_key_id = 0;
128 static GSList *container_resize_queue = NULL;
129 static guint container_signals[LAST_SIGNAL] = { 0 };
130 static GtkWidgetClass *parent_class = NULL;
131 extern GParamSpecPool *_gtk_widget_child_property_pool;
132 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
133 static GtkBuildableIface *parent_buildable_iface;
136 /* --- functions --- */
138 gtk_container_get_type (void)
140 static GType container_type = 0;
144 const GTypeInfo container_info =
146 sizeof (GtkContainerClass),
147 (GBaseInitFunc) gtk_container_base_class_init,
148 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
149 (GClassInitFunc) gtk_container_class_init,
150 NULL /* class_finalize */,
151 NULL /* class_data */,
152 sizeof (GtkContainer),
154 (GInstanceInitFunc) gtk_container_init,
155 NULL, /* value_table */
158 const GInterfaceInfo buildable_info =
160 (GInterfaceInitFunc) gtk_container_buildable_init,
166 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
167 &container_info, G_TYPE_FLAG_ABSTRACT);
169 g_type_add_interface_static (container_type,
175 return container_type;
179 gtk_container_base_class_init (GtkContainerClass *class)
181 /* reset instance specifc class fields that don't get inherited */
182 class->set_child_property = NULL;
183 class->get_child_property = NULL;
187 gtk_container_base_class_finalize (GtkContainerClass *class)
191 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
192 for (node = list; node; node = node->next)
194 GParamSpec *pspec = node->data;
196 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
197 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
198 g_param_spec_unref (pspec);
204 gtk_container_class_init (GtkContainerClass *class)
206 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
207 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
208 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
210 parent_class = g_type_class_peek_parent (class);
212 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
213 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
215 gobject_class->set_property = gtk_container_set_property;
216 gobject_class->get_property = gtk_container_get_property;
218 object_class->destroy = gtk_container_destroy;
220 widget_class->show_all = gtk_container_show_all;
221 widget_class->hide_all = gtk_container_hide_all;
222 widget_class->expose_event = gtk_container_expose;
223 widget_class->map = gtk_container_map;
224 widget_class->unmap = gtk_container_unmap;
225 widget_class->focus = gtk_container_focus;
227 class->add = gtk_container_add_unimplemented;
228 class->remove = gtk_container_remove_unimplemented;
229 class->check_resize = gtk_container_real_check_resize;
230 class->forall = NULL;
231 class->set_focus_child = gtk_container_real_set_focus_child;
232 class->child_type = NULL;
233 class->composite_name = gtk_container_child_default_composite_name;
235 g_object_class_install_property (gobject_class,
237 g_param_spec_enum ("resize-mode",
239 P_("Specify how resize events are handled"),
240 GTK_TYPE_RESIZE_MODE,
242 GTK_PARAM_READWRITE));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_uint ("border-width",
247 P_("The width of the empty border outside the containers children"),
251 GTK_PARAM_READWRITE));
252 g_object_class_install_property (gobject_class,
254 g_param_spec_object ("child",
256 P_("Can be used to add a new child to the container"),
258 GTK_PARAM_WRITABLE));
259 container_signals[ADD] =
260 g_signal_new (I_("add"),
261 G_OBJECT_CLASS_TYPE (object_class),
263 G_STRUCT_OFFSET (GtkContainerClass, add),
265 _gtk_marshal_VOID__OBJECT,
268 container_signals[REMOVE] =
269 g_signal_new (I_("remove"),
270 G_OBJECT_CLASS_TYPE (object_class),
272 G_STRUCT_OFFSET (GtkContainerClass, remove),
274 _gtk_marshal_VOID__OBJECT,
277 container_signals[CHECK_RESIZE] =
278 g_signal_new (I_("check-resize"),
279 G_OBJECT_CLASS_TYPE (object_class),
281 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
283 _gtk_marshal_VOID__VOID,
285 container_signals[SET_FOCUS_CHILD] =
286 g_signal_new (I_("set-focus-child"),
287 G_OBJECT_CLASS_TYPE (object_class),
289 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
291 _gtk_marshal_VOID__OBJECT,
297 gtk_container_buildable_init (GtkBuildableIface *iface)
299 parent_buildable_iface = g_type_interface_peek_parent (iface);
300 iface->add_child = gtk_container_buildable_add_child;
301 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
302 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
306 gtk_container_buildable_add_child (GtkBuildable *buildable,
313 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
315 else if (GTK_IS_WIDGET (child) && GTK_WIDGET (child)->parent == NULL)
317 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
320 g_warning ("Cannot add an object of type %s to a container of type %s",
321 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
325 gtk_container_buildable_set_child_property (GtkContainer *container,
332 GValue gvalue = { 0, };
333 GError *error = NULL;
335 pspec = gtk_container_class_find_child_property
336 (G_OBJECT_GET_CLASS (container), name);
339 g_warning ("%s does not have a property called %s",
340 g_type_name (G_OBJECT_TYPE (container)), name);
344 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
346 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
347 g_type_name (G_OBJECT_TYPE (container)),
350 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
352 g_error_free (error);
356 gtk_container_child_set_property (container, child, name, &gvalue);
357 g_value_unset (&gvalue);
362 GtkContainer *container;
364 gchar *child_prop_name;
366 gboolean translatable;
367 } PackingPropertiesData;
370 attributes_start_element (GMarkupParseContext *context,
371 const gchar *element_name,
373 const gchar **values,
377 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
380 if (strcmp (element_name, "property") == 0)
382 for (i = 0; names[i]; i++)
383 if (strcmp (names[i], "name") == 0)
384 parser_data->child_prop_name = g_strdup (values[i]);
385 else if (strcmp (names[i], "translatable") == 0)
387 if (!_gtk_builder_boolean_from_string (values[1],
388 &parser_data->translatable,
392 else if (strcmp (names[i], "comments") == 0)
393 ; /* for translators */
394 else if (strcmp (names[i], "context") == 0)
395 parser_data->context = g_strdup (values[1]);
397 g_warning ("Unsupported attribute for GtkContainer Child "
398 "property: %s\n", names[i]);
400 else if (strcmp (element_name, "packing") == 0)
403 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
407 attributes_text_element (GMarkupParseContext *context,
413 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
416 if (!parser_data->child_prop_name)
419 if (parser_data->translatable && text_len)
422 domain = gtk_builder_get_translation_domain (parser_data->builder);
424 value = _gtk_builder_parser_translate (domain,
425 parser_data->context,
430 value = g_strdup (text);
433 gtk_container_buildable_set_child_property (parser_data->container,
434 parser_data->builder,
436 parser_data->child_prop_name,
439 g_free (parser_data->child_prop_name);
440 g_free (parser_data->context);
442 parser_data->child_prop_name = NULL;
443 parser_data->context = NULL;
444 parser_data->translatable = FALSE;
447 static const GMarkupParser attributes_parser =
449 attributes_start_element,
451 attributes_text_element,
455 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
458 const gchar *tagname,
459 GMarkupParser *parser,
462 PackingPropertiesData *parser_data;
464 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
465 tagname, parser, data))
468 if (child && strcmp (tagname, "packing") == 0)
470 parser_data = g_slice_new0 (PackingPropertiesData);
471 parser_data->builder = builder;
472 parser_data->container = GTK_CONTAINER (buildable);
473 parser_data->child = GTK_WIDGET (child);
474 parser_data->child_prop_name = NULL;
476 *parser = attributes_parser;
485 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
488 const gchar *tagname,
491 if (strcmp (tagname, "packing") == 0)
493 g_slice_free (PackingPropertiesData, (gpointer)data);
498 if (parent_buildable_iface->custom_tag_end)
499 parent_buildable_iface->custom_tag_end (buildable, builder,
500 child, tagname, data);
505 * gtk_container_child_type:
506 * @container: a #GtkContainer
508 * Returns the type of the children supported by the container.
510 * Note that this may return %G_TYPE_NONE to indicate that no more
511 * children can be added, e.g. for a #GtkPaned which already has two
514 * Return value: a #GType.
517 gtk_container_child_type (GtkContainer *container)
520 GtkContainerClass *class;
522 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
524 class = GTK_CONTAINER_GET_CLASS (container);
525 if (class->child_type)
526 slot = class->child_type (container);
533 /* --- GtkContainer child property mechanism --- */
535 container_get_child_property (GtkContainer *container,
540 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
542 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
546 container_set_child_property (GtkContainer *container,
550 GObjectNotifyQueue *nqueue)
552 GValue tmp_value = { 0, };
553 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
555 /* provide a copy to work from, convert (if necessary) and validate */
556 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
557 if (!g_value_transform (value, &tmp_value))
558 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
560 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
561 G_VALUE_TYPE_NAME (value));
562 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
564 gchar *contents = g_strdup_value_contents (value);
566 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
568 G_VALUE_TYPE_NAME (value),
570 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
575 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
576 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
578 g_value_unset (&tmp_value);
582 * gtk_container_child_get_valist:
583 * @container: a #GtkContainer
584 * @child: a widget which is a child of @container
585 * @first_property_name: the name of the first property to get
586 * @var_args: return location for the first property, followed
587 * optionally by more name/return location pairs, followed by %NULL
589 * Gets the values of one or more child properties for @child and @container.
592 gtk_container_child_get_valist (GtkContainer *container,
594 const gchar *first_property_name,
599 g_return_if_fail (GTK_IS_CONTAINER (container));
600 g_return_if_fail (GTK_IS_WIDGET (child));
601 g_return_if_fail (child->parent == GTK_WIDGET (container));
603 g_object_ref (container);
604 g_object_ref (child);
606 name = first_property_name;
609 GValue value = { 0, };
613 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
615 G_OBJECT_TYPE (container),
619 g_warning ("%s: container class `%s' has no child property named `%s'",
621 G_OBJECT_TYPE_NAME (container),
625 if (!(pspec->flags & G_PARAM_READABLE))
627 g_warning ("%s: child property `%s' of container class `%s' is not readable",
630 G_OBJECT_TYPE_NAME (container));
633 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
634 container_get_child_property (container, child, pspec, &value);
635 G_VALUE_LCOPY (&value, var_args, 0, &error);
638 g_warning ("%s: %s", G_STRLOC, error);
640 g_value_unset (&value);
643 g_value_unset (&value);
644 name = va_arg (var_args, gchar*);
647 g_object_unref (child);
648 g_object_unref (container);
652 * gtk_container_child_get_property:
653 * @container: a #GtkContainer
654 * @child: a widget which is a child of @container
655 * @property_name: the name of the property to get
656 * @value: a location to return the value
658 * Gets the value of a child property for @child and @container.
661 gtk_container_child_get_property (GtkContainer *container,
663 const gchar *property_name,
668 g_return_if_fail (GTK_IS_CONTAINER (container));
669 g_return_if_fail (GTK_IS_WIDGET (child));
670 g_return_if_fail (child->parent == GTK_WIDGET (container));
671 g_return_if_fail (property_name != NULL);
672 g_return_if_fail (G_IS_VALUE (value));
674 g_object_ref (container);
675 g_object_ref (child);
676 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
677 G_OBJECT_TYPE (container), TRUE);
679 g_warning ("%s: container class `%s' has no child property named `%s'",
681 G_OBJECT_TYPE_NAME (container),
683 else if (!(pspec->flags & G_PARAM_READABLE))
684 g_warning ("%s: child property `%s' of container class `%s' is not readable",
687 G_OBJECT_TYPE_NAME (container));
690 GValue *prop_value, tmp_value = { 0, };
692 /* auto-conversion of the callers value type
694 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
696 g_value_reset (value);
699 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
701 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
703 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
704 G_VALUE_TYPE_NAME (value));
705 g_object_unref (child);
706 g_object_unref (container);
711 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
712 prop_value = &tmp_value;
714 container_get_child_property (container, child, pspec, prop_value);
715 if (prop_value != value)
717 g_value_transform (prop_value, value);
718 g_value_unset (&tmp_value);
721 g_object_unref (child);
722 g_object_unref (container);
726 * gtk_container_child_set_valist:
727 * @container: a #GtkContainer
728 * @child: a widget which is a child of @container
729 * @first_property_name: the name of the first property to set
730 * @var_args: a %NULL-terminated list of property names and values, starting
731 * with @first_prop_name
733 * Sets one or more child properties for @child and @container.
736 gtk_container_child_set_valist (GtkContainer *container,
738 const gchar *first_property_name,
741 GObjectNotifyQueue *nqueue;
744 g_return_if_fail (GTK_IS_CONTAINER (container));
745 g_return_if_fail (GTK_IS_WIDGET (child));
746 g_return_if_fail (child->parent == GTK_WIDGET (container));
748 g_object_ref (container);
749 g_object_ref (child);
751 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
752 name = first_property_name;
755 GValue value = { 0, };
757 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
759 G_OBJECT_TYPE (container),
763 g_warning ("%s: container class `%s' has no child property named `%s'",
765 G_OBJECT_TYPE_NAME (container),
769 if (!(pspec->flags & G_PARAM_WRITABLE))
771 g_warning ("%s: child property `%s' of container class `%s' is not writable",
774 G_OBJECT_TYPE_NAME (container));
777 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
778 G_VALUE_COLLECT (&value, var_args, 0, &error);
781 g_warning ("%s: %s", G_STRLOC, error);
784 /* we purposely leak the value here, it might not be
785 * in a sane state if an error condition occoured
789 container_set_child_property (container, child, pspec, &value, nqueue);
790 g_value_unset (&value);
791 name = va_arg (var_args, gchar*);
793 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
795 g_object_unref (container);
796 g_object_unref (child);
800 * gtk_container_child_set_property:
801 * @container: a #GtkContainer
802 * @child: a widget which is a child of @container
803 * @property_name: the name of the property to set
804 * @value: the value to set the property to
806 * Sets a child property for @child and @container.
809 gtk_container_child_set_property (GtkContainer *container,
811 const gchar *property_name,
814 GObjectNotifyQueue *nqueue;
817 g_return_if_fail (GTK_IS_CONTAINER (container));
818 g_return_if_fail (GTK_IS_WIDGET (child));
819 g_return_if_fail (child->parent == GTK_WIDGET (container));
820 g_return_if_fail (property_name != NULL);
821 g_return_if_fail (G_IS_VALUE (value));
823 g_object_ref (container);
824 g_object_ref (child);
826 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
827 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
828 G_OBJECT_TYPE (container), TRUE);
830 g_warning ("%s: container class `%s' has no child property named `%s'",
832 G_OBJECT_TYPE_NAME (container),
834 else if (!(pspec->flags & G_PARAM_WRITABLE))
835 g_warning ("%s: child property `%s' of container class `%s' is not writable",
838 G_OBJECT_TYPE_NAME (container));
841 container_set_child_property (container, child, pspec, value, nqueue);
843 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
844 g_object_unref (container);
845 g_object_unref (child);
849 * gtk_container_add_with_properties:
850 * @container: a #GtkContainer
851 * @widget: a widget to be placed inside @container
852 * @first_prop_name: the name of the first child property to set
853 * @Varargs: a %NULL-terminated list of property names and values, starting
854 * with @first_prop_name
856 * Adds @widget to @container, setting child properties at the same time.
857 * See gtk_container_add() and gtk_container_child_set() for more details.
860 gtk_container_add_with_properties (GtkContainer *container,
862 const gchar *first_prop_name,
865 g_return_if_fail (GTK_IS_CONTAINER (container));
866 g_return_if_fail (GTK_IS_WIDGET (widget));
867 g_return_if_fail (widget->parent == NULL);
869 g_object_ref (container);
870 g_object_ref (widget);
871 gtk_widget_freeze_child_notify (widget);
873 g_signal_emit (container, container_signals[ADD], 0, widget);
878 va_start (var_args, first_prop_name);
879 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
883 gtk_widget_thaw_child_notify (widget);
884 g_object_unref (widget);
885 g_object_unref (container);
889 * gtk_container_child_set:
890 * @container: a #GtkContainer
891 * @child: a widget which is a child of @container
892 * @first_prop_name: the name of the first property to set
893 * @Varargs: a %NULL-terminated list of property names and values, starting
894 * with @first_prop_name
896 * Sets one or more child properties for @child and @container.
899 gtk_container_child_set (GtkContainer *container,
901 const gchar *first_prop_name,
906 g_return_if_fail (GTK_IS_CONTAINER (container));
907 g_return_if_fail (GTK_IS_WIDGET (child));
908 g_return_if_fail (child->parent == GTK_WIDGET (container));
910 va_start (var_args, first_prop_name);
911 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
916 * gtk_container_child_get:
917 * @container: a #GtkContainer
918 * @child: a widget which is a child of @container
919 * @first_prop_name: the name of the first property to get
920 * @Varargs: return location for the first property, followed
921 * optionally by more name/return location pairs, followed by %NULL
923 * Gets the values of one or more child properties for @child and @container.
926 gtk_container_child_get (GtkContainer *container,
928 const gchar *first_prop_name,
933 g_return_if_fail (GTK_IS_CONTAINER (container));
934 g_return_if_fail (GTK_IS_WIDGET (child));
935 g_return_if_fail (child->parent == GTK_WIDGET (container));
937 va_start (var_args, first_prop_name);
938 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
943 * gtk_container_class_install_child_property:
944 * @cclass: a #GtkContainerClass
945 * @property_id: the id for the property
946 * @pspec: the #GParamSpec for the property
948 * Installs a child property on a container class.
951 gtk_container_class_install_child_property (GtkContainerClass *cclass,
955 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
956 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
957 if (pspec->flags & G_PARAM_WRITABLE)
958 g_return_if_fail (cclass->set_child_property != NULL);
959 if (pspec->flags & G_PARAM_READABLE)
960 g_return_if_fail (cclass->get_child_property != NULL);
961 g_return_if_fail (property_id > 0);
962 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
963 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
964 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
966 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
968 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
969 G_OBJECT_CLASS_NAME (cclass),
973 g_param_spec_ref (pspec);
974 g_param_spec_sink (pspec);
975 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
976 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
980 * gtk_container_class_find_child_property:
981 * @cclass: a #GtkContainerClass
982 * @property_name: the name of the child property to find
983 * @returns: the #GParamSpec of the child property or %NULL if @class has no
984 * child property with that name.
986 * Finds a child property of a container class by name.
989 gtk_container_class_find_child_property (GObjectClass *cclass,
990 const gchar *property_name)
992 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
993 g_return_val_if_fail (property_name != NULL, NULL);
995 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
997 G_OBJECT_CLASS_TYPE (cclass),
1002 * gtk_container_class_list_child_properties:
1003 * @cclass: a #GtkContainerClass
1004 * @n_properties: location to return the number of child properties found
1005 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
1006 * The array must be freed with g_free().
1008 * Returns all child properties of a container class.
1011 gtk_container_class_list_child_properties (GObjectClass *cclass,
1012 guint *n_properties)
1014 GParamSpec **pspecs;
1017 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1019 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1020 G_OBJECT_CLASS_TYPE (cclass),
1029 gtk_container_add_unimplemented (GtkContainer *container,
1032 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1036 gtk_container_remove_unimplemented (GtkContainer *container,
1039 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1043 gtk_container_init (GtkContainer *container)
1045 container->focus_child = NULL;
1046 container->border_width = 0;
1047 container->need_resize = FALSE;
1048 container->resize_mode = GTK_RESIZE_PARENT;
1049 container->reallocate_redraws = FALSE;
1053 gtk_container_destroy (GtkObject *object)
1055 GtkContainer *container = GTK_CONTAINER (object);
1057 if (GTK_CONTAINER_RESIZE_PENDING (container))
1058 _gtk_container_dequeue_resize_handler (container);
1060 /* do this before walking child widgets, to avoid
1061 * removing children from focus chain one by one.
1063 if (container->has_focus_chain)
1064 gtk_container_unset_focus_chain (container);
1066 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1068 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1072 gtk_container_set_property (GObject *object,
1074 const GValue *value,
1077 GtkContainer *container = GTK_CONTAINER (object);
1081 case PROP_BORDER_WIDTH:
1082 gtk_container_set_border_width (container, g_value_get_uint (value));
1084 case PROP_RESIZE_MODE:
1085 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1088 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1091 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1097 gtk_container_get_property (GObject *object,
1102 GtkContainer *container = GTK_CONTAINER (object);
1106 case PROP_BORDER_WIDTH:
1107 g_value_set_uint (value, container->border_width);
1109 case PROP_RESIZE_MODE:
1110 g_value_set_enum (value, container->resize_mode);
1113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1119 * gtk_container_set_border_width:
1120 * @container: a #GtkContainer
1121 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1122 * the container. Valid values are in the range 0-65535 pixels.
1124 * Sets the border width of the container.
1126 * The border width of a container is the amount of space to leave
1127 * around the outside of the container. The only exception to this is
1128 * #GtkWindow; because toplevel windows can't leave space outside,
1129 * they leave the space inside. The border is added on all sides of
1130 * the container. To add space to only one side, one approach is to
1131 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1132 * to give it a size, and place it on the side of the container as
1136 gtk_container_set_border_width (GtkContainer *container,
1139 g_return_if_fail (GTK_IS_CONTAINER (container));
1141 if (container->border_width != border_width)
1143 container->border_width = border_width;
1144 g_object_notify (G_OBJECT (container), "border-width");
1146 if (GTK_WIDGET_REALIZED (container))
1147 gtk_widget_queue_resize (GTK_WIDGET (container));
1152 * gtk_container_get_border_width:
1153 * @container: a #GtkContainer
1155 * Retrieves the border width of the container. See
1156 * gtk_container_set_border_width().
1158 * Return value: the current border width
1161 gtk_container_get_border_width (GtkContainer *container)
1163 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1165 return container->border_width;
1169 * gtk_container_add:
1170 * @container: a #GtkContainer
1171 * @widget: a widget to be placed inside @container
1173 * Adds @widget to @container. Typically used for simple containers
1174 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1175 * layout containers such as #GtkBox or #GtkTable, this function will
1176 * pick default packing parameters that may not be correct. So
1177 * consider functions such as gtk_box_pack_start() and
1178 * gtk_table_attach() as an alternative to gtk_container_add() in
1179 * those cases. A widget may be added to only one container at a time;
1180 * you can't place the same widget inside two different containers.
1183 gtk_container_add (GtkContainer *container,
1186 g_return_if_fail (GTK_IS_CONTAINER (container));
1187 g_return_if_fail (GTK_IS_WIDGET (widget));
1189 if (widget->parent != NULL)
1191 g_warning ("Attempting to add a widget with type %s to a container of "
1192 "type %s, but the widget is already inside a container of type %s, "
1193 "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1194 "explains how to reparent a widget.",
1195 g_type_name (G_OBJECT_TYPE (widget)),
1196 g_type_name (G_OBJECT_TYPE (container)),
1197 g_type_name (G_OBJECT_TYPE (widget->parent)));
1201 g_signal_emit (container, container_signals[ADD], 0, widget);
1205 * gtk_container_remove:
1206 * @container: a #GtkContainer
1207 * @widget: a current child of @container
1209 * Removes @widget from @container. @widget must be inside @container.
1210 * Note that @container will own a reference to @widget, and that this
1211 * may be the last reference held; so removing a widget from its
1212 * container can destroy that widget. If you want to use @widget
1213 * again, you need to add a reference to it while it's not inside
1214 * a container, using g_object_ref(). If you don't want to use @widget
1215 * again it's usually more efficient to simply destroy it directly
1216 * using gtk_widget_destroy() since this will remove it from the
1217 * container and help break any circular reference count cycles.
1220 gtk_container_remove (GtkContainer *container,
1223 g_return_if_fail (GTK_IS_CONTAINER (container));
1224 g_return_if_fail (GTK_IS_WIDGET (widget));
1226 /* When using the deprecated API of the toolbar, it is possible
1227 * to legitimately call this function with a widget that is not
1228 * a direct child of the container.
1230 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
1231 widget->parent == GTK_WIDGET (container));
1233 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1237 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1239 g_return_if_fail (GTK_IS_CONTAINER (container));
1240 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1242 container_resize_queue = g_slist_remove (container_resize_queue, container);
1243 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1247 * gtk_container_set_resize_mode:
1248 * @container: a #GtkContainer
1249 * @resize_mode: the new resize mode
1251 * Sets the resize mode for the container.
1253 * The resize mode of a container determines whether a resize request
1254 * will be passed to the container's parent, queued for later execution
1255 * or executed immediately.
1258 gtk_container_set_resize_mode (GtkContainer *container,
1259 GtkResizeMode resize_mode)
1261 g_return_if_fail (GTK_IS_CONTAINER (container));
1262 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1264 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1265 resize_mode == GTK_RESIZE_PARENT)
1267 resize_mode = GTK_RESIZE_QUEUE;
1270 if (container->resize_mode != resize_mode)
1272 container->resize_mode = resize_mode;
1274 gtk_widget_queue_resize (GTK_WIDGET (container));
1275 g_object_notify (G_OBJECT (container), "resize-mode");
1280 * gtk_container_get_resize_mode:
1281 * @container: a #GtkContainer
1283 * Returns the resize mode for the container. See
1284 * gtk_container_set_resize_mode ().
1286 * Return value: the current resize mode
1289 gtk_container_get_resize_mode (GtkContainer *container)
1291 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1293 return container->resize_mode;
1297 * gtk_container_set_reallocate_redraws:
1298 * @container: a #GtkContainer
1299 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1301 * Sets the @reallocate_redraws flag of the container to the given value.
1303 * Containers requesting reallocation redraws get automatically
1304 * redrawn if any of their children changed allocation.
1307 gtk_container_set_reallocate_redraws (GtkContainer *container,
1308 gboolean needs_redraws)
1310 g_return_if_fail (GTK_IS_CONTAINER (container));
1312 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1315 static GtkContainer*
1316 gtk_container_get_resize_container (GtkContainer *container)
1318 GtkWidget *widget = GTK_WIDGET (container);
1320 while (widget->parent)
1322 widget = widget->parent;
1323 if (GTK_IS_RESIZE_CONTAINER (widget))
1327 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1331 gtk_container_idle_sizer (gpointer data)
1333 /* we may be invoked with a container_resize_queue of NULL, because
1334 * queue_resize could have been adding an extra idle function while
1335 * the queue still got processed. we better just ignore such case
1336 * than trying to explicitely work around them with some extra flags,
1337 * since it doesn't cause any actual harm.
1339 while (container_resize_queue)
1344 slist = container_resize_queue;
1345 container_resize_queue = slist->next;
1346 widget = slist->data;
1347 g_slist_free_1 (slist);
1349 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1350 gtk_container_check_resize (GTK_CONTAINER (widget));
1353 gdk_window_process_all_updates ();
1359 _gtk_container_queue_resize (GtkContainer *container)
1361 GtkContainer *resize_container;
1364 g_return_if_fail (GTK_IS_CONTAINER (container));
1366 widget = GTK_WIDGET (container);
1367 resize_container = gtk_container_get_resize_container (container);
1371 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1372 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1373 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1377 widget = widget->parent;
1380 if (resize_container)
1382 if (GTK_WIDGET_VISIBLE (resize_container) &&
1383 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) || GTK_WIDGET_REALIZED (resize_container)))
1385 switch (resize_container->resize_mode)
1387 case GTK_RESIZE_QUEUE:
1388 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1390 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1391 if (container_resize_queue == NULL)
1392 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1393 gtk_container_idle_sizer,
1395 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1399 case GTK_RESIZE_IMMEDIATE:
1400 gtk_container_check_resize (resize_container);
1403 case GTK_RESIZE_PARENT:
1404 g_assert_not_reached ();
1410 /* we need to let hidden resize containers know that something
1411 * changed while they where hidden (currently only evaluated by
1414 resize_container->need_resize = TRUE;
1420 gtk_container_check_resize (GtkContainer *container)
1422 g_return_if_fail (GTK_IS_CONTAINER (container));
1424 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1428 gtk_container_real_check_resize (GtkContainer *container)
1430 GtkWidget *widget = GTK_WIDGET (container);
1431 GtkRequisition requisition;
1433 gtk_widget_size_request (widget, &requisition);
1435 if (requisition.width > widget->allocation.width ||
1436 requisition.height > widget->allocation.height)
1438 if (GTK_IS_RESIZE_CONTAINER (container))
1439 gtk_widget_size_allocate (GTK_WIDGET (container),
1440 >K_WIDGET (container)->allocation);
1442 gtk_widget_queue_resize (widget);
1446 gtk_container_resize_children (container);
1450 /* The container hasn't changed size but one of its children
1451 * queued a resize request. Which means that the allocation
1452 * is not sufficient for the requisition of some child.
1453 * We've already performed a size request at this point,
1454 * so we simply need to reallocate and let the allocation
1455 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1458 gtk_container_resize_children (GtkContainer *container)
1462 /* resizing invariants:
1463 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1464 * containers that have an idle sizer pending must be flagged with
1467 g_return_if_fail (GTK_IS_CONTAINER (container));
1469 widget = GTK_WIDGET (container);
1470 gtk_widget_size_allocate (widget, &widget->allocation);
1474 * gtk_container_forall:
1475 * @container: a #GtkContainer
1476 * @callback: a callback
1477 * @callback_data: callback user data
1479 * Invokes @callback on each child of @container, including children
1480 * that are considered "internal" (implementation details of the
1481 * container). "Internal" children generally weren't added by the user
1482 * of the container, but were added by the container implementation
1483 * itself. Most applications should use gtk_container_foreach(),
1484 * rather than gtk_container_forall().
1487 gtk_container_forall (GtkContainer *container,
1488 GtkCallback callback,
1489 gpointer callback_data)
1491 GtkContainerClass *class;
1493 g_return_if_fail (GTK_IS_CONTAINER (container));
1494 g_return_if_fail (callback != NULL);
1496 class = GTK_CONTAINER_GET_CLASS (container);
1499 class->forall (container, TRUE, callback, callback_data);
1503 * gtk_container_foreach:
1504 * @container: a #GtkContainer
1505 * @callback: a callback
1506 * @callback_data: callback user data
1508 * Invokes @callback on each non-internal child of @container. See
1509 * gtk_container_forall() for details on what constitutes an
1510 * "internal" child. Most applications should use
1511 * gtk_container_foreach(), rather than gtk_container_forall().
1514 gtk_container_foreach (GtkContainer *container,
1515 GtkCallback callback,
1516 gpointer callback_data)
1518 GtkContainerClass *class;
1520 g_return_if_fail (GTK_IS_CONTAINER (container));
1521 g_return_if_fail (callback != NULL);
1523 class = GTK_CONTAINER_GET_CLASS (container);
1526 class->forall (container, FALSE, callback, callback_data);
1529 typedef struct _GtkForeachData GtkForeachData;
1530 struct _GtkForeachData
1532 GtkObject *container;
1533 GtkCallbackMarshal callback;
1534 gpointer callback_data;
1538 gtk_container_foreach_unmarshal (GtkWidget *child,
1541 GtkForeachData *fdata = (GtkForeachData*) data;
1544 /* first argument */
1545 args[0].name = NULL;
1546 args[0].type = G_TYPE_FROM_INSTANCE (child);
1547 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1549 /* location for return value */
1550 args[1].name = NULL;
1551 args[1].type = G_TYPE_NONE;
1553 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1557 gtk_container_foreach_full (GtkContainer *container,
1558 GtkCallback callback,
1559 GtkCallbackMarshal marshal,
1560 gpointer callback_data,
1561 GDestroyNotify notify)
1563 g_return_if_fail (GTK_IS_CONTAINER (container));
1567 GtkForeachData fdata;
1569 fdata.container = GTK_OBJECT (container);
1570 fdata.callback = marshal;
1571 fdata.callback_data = callback_data;
1573 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1577 g_return_if_fail (callback != NULL);
1579 gtk_container_foreach (container, callback, &callback_data);
1583 notify (callback_data);
1587 * gtk_container_set_focus_child:
1588 * @container: a #GtkContainer
1589 * @child: (allow-none): a #GtkWidget, or %NULL
1591 * Sets, or unsets if @child is %NULL, the focused child of @container.
1593 * This function emits the GtkContainer::set_focus_child signal of
1594 * @container. Implementations of #GtkContainer can override the
1595 * default behaviour by overriding the class closure of this signal.
1598 gtk_container_set_focus_child (GtkContainer *container,
1601 g_return_if_fail (GTK_IS_CONTAINER (container));
1603 g_return_if_fail (GTK_IS_WIDGET (child));
1605 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1609 * gtk_container_get_focus_child:
1610 * @container: a #GtkContainer
1612 * Returns the current focus child widget inside @container.
1614 * Returns: The child widget which has the focus
1615 * inside @container, or %NULL if none is set.
1620 gtk_container_get_focus_child (GtkContainer *container)
1622 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1624 return container->focus_child;
1628 * gtk_container_get_children:
1629 * @container: a #GtkContainer
1631 * Returns the container's non-internal children. See
1632 * gtk_container_forall() for details on what constitutes an "internal" child.
1634 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1637 gtk_container_get_children (GtkContainer *container)
1639 GList *children = NULL;
1641 gtk_container_foreach (container,
1642 gtk_container_children_callback,
1645 return g_list_reverse (children);
1649 gtk_container_child_position_callback (GtkWidget *widget,
1650 gpointer client_data)
1656 } *data = client_data;
1659 if (data->child == widget)
1660 data->index = data->i;
1664 gtk_container_child_default_composite_name (GtkContainer *container,
1674 /* fallback implementation */
1678 gtk_container_forall (container,
1679 gtk_container_child_position_callback,
1682 name = g_strdup_printf ("%s-%u",
1683 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1690 _gtk_container_child_composite_name (GtkContainer *container,
1693 gboolean composite_child;
1695 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1696 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1697 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1699 g_object_get (child, "composite-child", &composite_child, NULL);
1700 if (composite_child)
1702 static GQuark quark_composite_name = 0;
1705 if (!quark_composite_name)
1706 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1708 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1711 GtkContainerClass *class;
1713 class = GTK_CONTAINER_GET_CLASS (container);
1714 if (class->composite_name)
1715 name = class->composite_name (container, child);
1718 name = g_strdup (name);
1727 gtk_container_real_set_focus_child (GtkContainer *container,
1730 g_return_if_fail (GTK_IS_CONTAINER (container));
1731 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1733 if (child != container->focus_child)
1735 if (container->focus_child)
1736 g_object_unref (container->focus_child);
1737 container->focus_child = child;
1738 if (container->focus_child)
1739 g_object_ref (container->focus_child);
1743 /* check for h/v adjustments
1745 if (container->focus_child)
1747 GtkAdjustment *hadj;
1748 GtkAdjustment *vadj;
1749 GtkWidget *focus_child;
1752 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1753 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1757 focus_child = container->focus_child;
1758 while (GTK_IS_CONTAINER (focus_child) &&
1759 GTK_CONTAINER (focus_child)->focus_child)
1761 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1764 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1767 x += container->focus_child->allocation.x;
1768 y += container->focus_child->allocation.y;
1771 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1774 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1780 get_focus_chain (GtkContainer *container)
1782 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1785 /* same as gtk_container_get_children, except it includes internals
1788 gtk_container_get_all_children (GtkContainer *container)
1790 GList *children = NULL;
1792 gtk_container_forall (container,
1793 gtk_container_children_callback,
1800 gtk_container_focus (GtkWidget *widget,
1801 GtkDirectionType direction)
1804 GList *sorted_children;
1806 GtkContainer *container;
1808 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1810 container = GTK_CONTAINER (widget);
1814 if (gtk_widget_get_can_focus (GTK_WIDGET (container)))
1816 if (!GTK_WIDGET_HAS_FOCUS (container))
1818 gtk_widget_grab_focus (GTK_WIDGET (container));
1824 /* Get a list of the containers children, allowing focus
1825 * chain to override.
1827 if (container->has_focus_chain)
1828 children = g_list_copy (get_focus_chain (container));
1830 children = gtk_container_get_all_children (container);
1832 if (container->has_focus_chain &&
1833 (direction == GTK_DIR_TAB_FORWARD ||
1834 direction == GTK_DIR_TAB_BACKWARD))
1836 sorted_children = g_list_copy (children);
1838 if (direction == GTK_DIR_TAB_BACKWARD)
1839 sorted_children = g_list_reverse (sorted_children);
1842 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1844 return_val = gtk_container_focus_move (container, sorted_children, direction);
1846 g_list_free (sorted_children);
1847 g_list_free (children);
1854 tab_compare (gconstpointer a,
1858 const GtkWidget *child1 = a;
1859 const GtkWidget *child2 = b;
1860 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1862 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1863 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1867 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1868 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1870 if (text_direction == GTK_TEXT_DIR_RTL)
1871 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1873 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1876 return (y1 < y2) ? -1 : 1;
1880 gtk_container_focus_sort_tab (GtkContainer *container,
1882 GtkDirectionType direction,
1883 GtkWidget *old_focus)
1885 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1886 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1888 /* if we are going backwards then reverse the order
1891 if (direction == GTK_DIR_TAB_BACKWARD)
1892 children = g_list_reverse (children);
1897 /* Get coordinates of @widget's allocation with respect to
1898 * allocation of @container.
1901 get_allocation_coords (GtkContainer *container,
1903 GdkRectangle *allocation)
1905 *allocation = widget->allocation;
1907 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1908 0, 0, &allocation->x, &allocation->y);
1911 /* Look for a child in @children that is intermediate between
1912 * the focus widget and container. This widget, if it exists,
1913 * acts as the starting widget for focus navigation.
1916 find_old_focus (GtkContainer *container,
1919 GList *tmp_list = children;
1922 GtkWidget *child = tmp_list->data;
1923 GtkWidget *widget = child;
1925 while (widget && widget != (GtkWidget *)container)
1927 GtkWidget *parent = widget->parent;
1928 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1937 tmp_list = tmp_list->next;
1944 old_focus_coords (GtkContainer *container,
1945 GdkRectangle *old_focus_rect)
1947 GtkWidget *widget = GTK_WIDGET (container);
1948 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1950 if (GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1952 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1954 return get_allocation_coords (container, old_focus, old_focus_rect);
1960 typedef struct _CompareInfo CompareInfo;
1964 GtkContainer *container;
1971 up_down_compare (gconstpointer a,
1975 GdkRectangle allocation1;
1976 GdkRectangle allocation2;
1977 CompareInfo *compare = data;
1980 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1981 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1983 y1 = allocation1.y + allocation1.height / 2;
1984 y2 = allocation2.y + allocation2.height / 2;
1988 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1989 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1991 if (compare->reverse)
1992 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1994 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1997 return (y1 < y2) ? -1 : 1;
2001 gtk_container_focus_sort_up_down (GtkContainer *container,
2003 GtkDirectionType direction,
2004 GtkWidget *old_focus)
2006 CompareInfo compare;
2008 GdkRectangle old_allocation;
2010 compare.container = container;
2011 compare.reverse = (direction == GTK_DIR_UP);
2014 old_focus = find_old_focus (container, children);
2016 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2022 /* Delete widgets from list that don't match minimum criteria */
2024 compare_x1 = old_allocation.x;
2025 compare_x2 = old_allocation.x + old_allocation.width;
2027 if (direction == GTK_DIR_UP)
2028 compare_y = old_allocation.y;
2030 compare_y = old_allocation.y + old_allocation.height;
2032 tmp_list = children;
2035 GtkWidget *child = tmp_list->data;
2036 GList *next = tmp_list->next;
2037 gint child_x1, child_x2;
2038 GdkRectangle child_allocation;
2040 if (child != old_focus)
2042 if (get_allocation_coords (container, child, &child_allocation))
2044 child_x1 = child_allocation.x;
2045 child_x2 = child_allocation.x + child_allocation.width;
2047 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2048 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2049 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2051 children = g_list_delete_link (children, tmp_list);
2055 children = g_list_delete_link (children, tmp_list);
2061 compare.x = (compare_x1 + compare_x2) / 2;
2062 compare.y = old_allocation.y + old_allocation.height / 2;
2066 /* No old focus widget, need to figure out starting x,y some other way
2068 GtkWidget *widget = GTK_WIDGET (container);
2069 GdkRectangle old_focus_rect;
2071 if (old_focus_coords (container, &old_focus_rect))
2073 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2077 if (!gtk_widget_get_has_window (widget))
2078 compare.x = widget->allocation.x + widget->allocation.width / 2;
2080 compare.x = widget->allocation.width / 2;
2083 if (!gtk_widget_get_has_window (widget))
2084 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
2086 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
2089 children = g_list_sort_with_data (children, up_down_compare, &compare);
2091 if (compare.reverse)
2092 children = g_list_reverse (children);
2098 left_right_compare (gconstpointer a,
2102 GdkRectangle allocation1;
2103 GdkRectangle allocation2;
2104 CompareInfo *compare = data;
2107 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2108 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2110 x1 = allocation1.x + allocation1.width / 2;
2111 x2 = allocation2.x + allocation2.width / 2;
2115 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2116 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2118 if (compare->reverse)
2119 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2121 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2124 return (x1 < x2) ? -1 : 1;
2128 gtk_container_focus_sort_left_right (GtkContainer *container,
2130 GtkDirectionType direction,
2131 GtkWidget *old_focus)
2133 CompareInfo compare;
2135 GdkRectangle old_allocation;
2137 compare.container = container;
2138 compare.reverse = (direction == GTK_DIR_LEFT);
2141 old_focus = find_old_focus (container, children);
2143 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2149 /* Delete widgets from list that don't match minimum criteria */
2151 compare_y1 = old_allocation.y;
2152 compare_y2 = old_allocation.y + old_allocation.height;
2154 if (direction == GTK_DIR_LEFT)
2155 compare_x = old_allocation.x;
2157 compare_x = old_allocation.x + old_allocation.width;
2159 tmp_list = children;
2162 GtkWidget *child = tmp_list->data;
2163 GList *next = tmp_list->next;
2164 gint child_y1, child_y2;
2165 GdkRectangle child_allocation;
2167 if (child != old_focus)
2169 if (get_allocation_coords (container, child, &child_allocation))
2171 child_y1 = child_allocation.y;
2172 child_y2 = child_allocation.y + child_allocation.height;
2174 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2175 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2176 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2178 children = g_list_delete_link (children, tmp_list);
2182 children = g_list_delete_link (children, tmp_list);
2188 compare.y = (compare_y1 + compare_y2) / 2;
2189 compare.x = old_allocation.x + old_allocation.width / 2;
2193 /* No old focus widget, need to figure out starting x,y some other way
2195 GtkWidget *widget = GTK_WIDGET (container);
2196 GdkRectangle old_focus_rect;
2198 if (old_focus_coords (container, &old_focus_rect))
2200 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2204 if (!gtk_widget_get_has_window (widget))
2205 compare.y = widget->allocation.y + widget->allocation.height / 2;
2207 compare.y = widget->allocation.height / 2;
2210 if (!gtk_widget_get_has_window (widget))
2211 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
2213 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
2216 children = g_list_sort_with_data (children, left_right_compare, &compare);
2218 if (compare.reverse)
2219 children = g_list_reverse (children);
2225 * gtk_container_focus_sort:
2226 * @container: a #GtkContainer
2227 * @children: a list of descendents of @container (they don't
2228 * have to be direct children)
2229 * @direction: focus direction
2230 * @old_focus: widget to use for the starting position, or %NULL
2231 * to determine this automatically.
2232 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2233 * which is the only @direction we use currently,
2234 * so perhaps this argument should be removed)
2236 * Sorts @children in the correct order for focusing with
2237 * direction type @direction.
2239 * Return value: a copy of @children, sorted in correct focusing order,
2240 * with children that aren't suitable for focusing in this direction
2244 _gtk_container_focus_sort (GtkContainer *container,
2246 GtkDirectionType direction,
2247 GtkWidget *old_focus)
2249 GList *visible_children = NULL;
2253 if (GTK_WIDGET_REALIZED (children->data))
2254 visible_children = g_list_prepend (visible_children, children->data);
2255 children = children->next;
2260 case GTK_DIR_TAB_FORWARD:
2261 case GTK_DIR_TAB_BACKWARD:
2262 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2265 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2268 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2271 g_assert_not_reached ();
2277 gtk_container_focus_move (GtkContainer *container,
2279 GtkDirectionType direction)
2281 GtkWidget *focus_child;
2284 focus_child = container->focus_child;
2288 child = children->data;
2289 children = children->next;
2296 if (focus_child == child)
2300 if (gtk_widget_child_focus (child, direction))
2304 else if (GTK_WIDGET_DRAWABLE (child) &&
2305 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2307 if (gtk_widget_child_focus (child, direction))
2317 gtk_container_children_callback (GtkWidget *widget,
2318 gpointer client_data)
2322 children = (GList**) client_data;
2323 *children = g_list_prepend (*children, widget);
2327 chain_widget_destroyed (GtkWidget *widget,
2330 GtkContainer *container;
2333 container = GTK_CONTAINER (user_data);
2335 chain = g_object_get_data (G_OBJECT (container),
2336 "gtk-container-focus-chain");
2338 chain = g_list_remove (chain, widget);
2340 g_signal_handlers_disconnect_by_func (widget,
2341 chain_widget_destroyed,
2344 g_object_set_data (G_OBJECT (container),
2345 I_("gtk-container-focus-chain"),
2350 * gtk_container_set_focus_chain:
2351 * @container: a #GtkContainer
2352 * @focusable_widgets: the new focus chain
2354 * Sets a focus chain, overriding the one computed automatically by GTK+.
2356 * In principle each widget in the chain should be a descendant of the
2357 * container, but this is not enforced by this method, since it's allowed
2358 * to set the focus chain before you pack the widgets, or have a widget
2359 * in the chain that isn't always packed. The necessary checks are done
2360 * when the focus chain is actually traversed.
2363 gtk_container_set_focus_chain (GtkContainer *container,
2364 GList *focusable_widgets)
2369 g_return_if_fail (GTK_IS_CONTAINER (container));
2371 if (container->has_focus_chain)
2372 gtk_container_unset_focus_chain (container);
2374 container->has_focus_chain = TRUE;
2377 tmp_list = focusable_widgets;
2378 while (tmp_list != NULL)
2380 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2382 /* In principle each widget in the chain should be a descendant
2383 * of the container, but we don't want to check that here, it's
2384 * expensive and also it's allowed to set the focus chain before
2385 * you pack the widgets, or have a widget in the chain that isn't
2386 * always packed. So we check for ancestor during actual traversal.
2389 chain = g_list_prepend (chain, tmp_list->data);
2391 g_signal_connect (tmp_list->data,
2393 G_CALLBACK (chain_widget_destroyed),
2396 tmp_list = g_list_next (tmp_list);
2399 chain = g_list_reverse (chain);
2401 g_object_set_data (G_OBJECT (container),
2402 I_("gtk-container-focus-chain"),
2407 * gtk_container_get_focus_chain:
2408 * @container: a #GtkContainer
2409 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2410 * to store the focus chain of the
2411 * container, or %NULL. You should free this list
2412 * using g_list_free() when you are done with it, however
2413 * no additional reference count is added to the
2414 * individual widgets in the focus chain.
2416 * Retrieves the focus chain of the container, if one has been
2417 * set explicitly. If no focus chain has been explicitly
2418 * set, GTK+ computes the focus chain based on the positions
2419 * of the children. In that case, GTK+ stores %NULL in
2420 * @focusable_widgets and returns %FALSE.
2422 * Return value: %TRUE if the focus chain of the container
2423 * has been set explicitly.
2426 gtk_container_get_focus_chain (GtkContainer *container,
2427 GList **focus_chain)
2429 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2433 if (container->has_focus_chain)
2434 *focus_chain = g_list_copy (get_focus_chain (container));
2436 *focus_chain = NULL;
2439 return container->has_focus_chain;
2443 * gtk_container_unset_focus_chain:
2444 * @container: a #GtkContainer
2446 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2449 gtk_container_unset_focus_chain (GtkContainer *container)
2451 g_return_if_fail (GTK_IS_CONTAINER (container));
2453 if (container->has_focus_chain)
2458 chain = get_focus_chain (container);
2460 container->has_focus_chain = FALSE;
2462 g_object_set_data (G_OBJECT (container),
2463 I_("gtk-container-focus-chain"),
2467 while (tmp_list != NULL)
2469 g_signal_handlers_disconnect_by_func (tmp_list->data,
2470 chain_widget_destroyed,
2473 tmp_list = g_list_next (tmp_list);
2476 g_list_free (chain);
2481 * gtk_container_set_focus_vadjustment:
2482 * @container: a #GtkContainer
2483 * @adjustment: an adjustment which should be adjusted when the focus
2484 * is moved among the descendents of @container
2486 * Hooks up an adjustment to focus handling in a container, so when a
2487 * child of the container is focused, the adjustment is scrolled to
2488 * show that widget. This function sets the vertical alignment. See
2489 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2490 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2491 * the horizontal adjustment.
2493 * The adjustments have to be in pixel units and in the same coordinate
2494 * system as the allocation for immediate children of the container.
2497 gtk_container_set_focus_vadjustment (GtkContainer *container,
2498 GtkAdjustment *adjustment)
2500 g_return_if_fail (GTK_IS_CONTAINER (container));
2502 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2505 g_object_ref (adjustment);
2507 g_object_set_qdata_full (G_OBJECT (container),
2514 * gtk_container_get_focus_vadjustment:
2515 * @container: a #GtkContainer
2517 * Retrieves the vertical focus adjustment for the container. See
2518 * gtk_container_set_focus_vadjustment().
2520 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2521 * none has been set.
2524 gtk_container_get_focus_vadjustment (GtkContainer *container)
2526 GtkAdjustment *vadjustment;
2528 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2530 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2536 * gtk_container_set_focus_hadjustment:
2537 * @container: a #GtkContainer
2538 * @adjustment: an adjustment which should be adjusted when the focus is
2539 * moved among the descendents of @container
2541 * Hooks up an adjustment to focus handling in a container, so when a child
2542 * of the container is focused, the adjustment is scrolled to show that
2543 * widget. This function sets the horizontal alignment.
2544 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2545 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2546 * the vertical adjustment.
2548 * The adjustments have to be in pixel units and in the same coordinate
2549 * system as the allocation for immediate children of the container.
2552 gtk_container_set_focus_hadjustment (GtkContainer *container,
2553 GtkAdjustment *adjustment)
2555 g_return_if_fail (GTK_IS_CONTAINER (container));
2557 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2560 g_object_ref (adjustment);
2562 g_object_set_qdata_full (G_OBJECT (container),
2569 * gtk_container_get_focus_hadjustment:
2570 * @container: a #GtkContainer
2572 * Retrieves the horizontal focus adjustment for the container. See
2573 * gtk_container_set_focus_hadjustment ().
2575 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
2576 * none has been set.
2579 gtk_container_get_focus_hadjustment (GtkContainer *container)
2581 GtkAdjustment *hadjustment;
2583 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2585 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2592 gtk_container_show_all (GtkWidget *widget)
2594 g_return_if_fail (GTK_IS_CONTAINER (widget));
2596 gtk_container_foreach (GTK_CONTAINER (widget),
2597 (GtkCallback) gtk_widget_show_all,
2599 gtk_widget_show (widget);
2603 gtk_container_hide_all (GtkWidget *widget)
2605 g_return_if_fail (GTK_IS_CONTAINER (widget));
2607 gtk_widget_hide (widget);
2608 gtk_container_foreach (GTK_CONTAINER (widget),
2609 (GtkCallback) gtk_widget_hide_all,
2615 gtk_container_expose_child (GtkWidget *child,
2616 gpointer client_data)
2619 GtkWidget *container;
2620 GdkEventExpose *event;
2621 } *data = client_data;
2623 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2629 gtk_container_expose (GtkWidget *widget,
2630 GdkEventExpose *event)
2633 GtkWidget *container;
2634 GdkEventExpose *event;
2637 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2638 g_return_val_if_fail (event != NULL, FALSE);
2641 if (GTK_WIDGET_DRAWABLE (widget))
2643 data.container = widget;
2646 gtk_container_forall (GTK_CONTAINER (widget),
2647 gtk_container_expose_child,
2655 gtk_container_map_child (GtkWidget *child,
2656 gpointer client_data)
2658 if (GTK_WIDGET_VISIBLE (child) &&
2659 GTK_WIDGET_CHILD_VISIBLE (child) &&
2660 !GTK_WIDGET_MAPPED (child))
2661 gtk_widget_map (child);
2665 gtk_container_map (GtkWidget *widget)
2667 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2669 gtk_container_forall (GTK_CONTAINER (widget),
2670 gtk_container_map_child,
2673 if (gtk_widget_get_has_window (widget))
2674 gdk_window_show (widget->window);
2678 gtk_container_unmap (GtkWidget *widget)
2680 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2682 if (gtk_widget_get_has_window (widget))
2683 gdk_window_hide (widget->window);
2685 gtk_container_forall (GTK_CONTAINER (widget),
2686 (GtkCallback)gtk_widget_unmap,
2691 * gtk_container_propagate_expose:
2692 * @container: a #GtkContainer
2693 * @child: a child of @container
2694 * @event: a expose event sent to container
2696 * When a container receives an expose event, it must send synthetic
2697 * expose events to all children that don't have their own #GdkWindows.
2698 * This function provides a convenient way of doing this. A container,
2699 * when it receives an expose event, calls gtk_container_propagate_expose()
2700 * once for each child, passing in the event the container received.
2702 * gtk_container_propagate_expose() takes care of deciding whether
2703 * an expose event needs to be sent to the child, intersecting
2704 * the event's area with the child area, and sending the event.
2706 * In most cases, a container can simply either simply inherit the
2707 * #GtkWidget::expose implementation from #GtkContainer, or, do some drawing
2708 * and then chain to the ::expose implementation from #GtkContainer.
2711 gtk_container_propagate_expose (GtkContainer *container,
2713 GdkEventExpose *event)
2715 GdkEvent *child_event;
2717 g_return_if_fail (GTK_IS_CONTAINER (container));
2718 g_return_if_fail (GTK_IS_WIDGET (child));
2719 g_return_if_fail (event != NULL);
2721 g_assert (child->parent == GTK_WIDGET (container));
2723 if (GTK_WIDGET_DRAWABLE (child) &&
2724 !gtk_widget_get_has_window (child) &&
2725 (child->window == event->window))
2727 child_event = gdk_event_new (GDK_EXPOSE);
2728 child_event->expose = *event;
2729 g_object_ref (child_event->expose.window);
2731 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2732 if (!gdk_region_empty (child_event->expose.region))
2734 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2735 gtk_widget_send_expose (child, child_event);
2737 gdk_event_free (child_event);
2741 #define __GTK_CONTAINER_C__
2742 #include "gtkaliasdef.c"