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/.
29 #include "gtkcontainer.h"
35 #include <gobject/gobjectnotifyqueue.c>
36 #include <gobject/gvaluecollector.h>
38 #include "gtkbuildable.h"
39 #include "gtkbuilderprivate.h"
40 #include "gtktypebuiltins.h"
41 #include "gtkprivate.h"
43 #include "gtkmarshalers.h"
44 #include "gtksizerequest.h"
45 #include "gtkwidgetprivate.h"
46 #include "gtkwindow.h"
48 #include "gtktoolbar.h"
52 * SECTION:gtkcontainer
53 * @Short_description: Base class for widgets which contain other widgets
54 * @Title: GtkContainer
56 * A GTK+ user interface is constructed by nesting widgets inside widgets.
57 * Container widgets are the inner nodes in the resulting tree of widgets:
58 * they contain other widgets. So, for example, you might have a #GtkWindow
59 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
60 * of a textual label inside the frame, you might replace the #GtkLabel widget
61 * with a #GtkImage widget.
63 * There are two major kinds of container widgets in GTK+. Both are subclasses
64 * of the abstract GtkContainer base class.
66 * The first type of container widget has a single child widget and derives
67 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
68 * add some kind of functionality to the child. For example, a #GtkButton makes
69 * its child into a clickable button; a #GtkFrame draws a frame around its child
70 * and a #GtkWindow places its child widget inside a top-level window.
72 * The second type of container can have more than one child; its purpose is to
73 * manage <emphasis>layout</emphasis>. This means that these containers assign
74 * sizes and positions to their children. For example, a #GtkHBox arranges its
75 * children in a horizontal row, and a #GtkTable arranges the widgets it contains
76 * in a two-dimensional grid.
78 * <refsect2 id="container-geometry-management">
79 * <title>Height for width geometry management</title>
81 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
82 * Height-for-width means that a widget can change how much vertical space it needs,
83 * depending on the amount of horizontal space that it is given (and similar for
86 * There are some things to keep in mind when implementing container widgets
87 * that make use of GTK+'s height for width geometry management system. First,
88 * it's important to note that a container must prioritize one of its
89 * dimensions, that is to say that a widget or container can only have a
90 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
91 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
92 * must be able to respond to the APIs for both dimensions, i.e. even if a
93 * widget has a request mode that is height-for-width, it is possible that
94 * its parent will request its sizes using the width-for-height APIs.
96 * To ensure that everything works properly, here are some guidelines to follow
97 * when implementing height-for-width (or width-for-height) containers.
99 * Each request mode involves 2 virtual methods. Height-for-width apis run
100 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
101 * When handling requests in the opposite #GtkSizeRequestMode it is important that
102 * every widget request at least enough space to display all of its content at all times.
104 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
105 * the container must return the height for its minimum width. This is easily achieved by
106 * simply calling the reverse apis implemented for itself as follows:
108 * <programlisting><![CDATA[
110 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
112 * if (i_am_in_height_for_width_mode)
116 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
117 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
118 * min_height, nat_height);
122 * ... many containers support both request modes, execute the real width-for-height
123 * request here by returning the collective heights of all widgets that are
124 * stacked vertically (or whatever is appropriate for this container) ...
127 * ]]></programlisting>
129 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
130 * that is height-for-width, it then only needs to return the base minimum width like so:
132 * <programlisting><![CDATA[
134 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
135 * gint *min_width, gint *nat_width)
137 * if (i_am_in_height_for_width_mode)
139 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
143 * ... execute the real width-for-height request here based on the required width
144 * of the children collectively if the container were to be allocated the said height ...
147 * ]]></programlisting>
149 * Height for width requests are generally implemented in terms of a virtual allocation
150 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
151 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
152 * gtk_widget_get_preferred_width() for each of its children.
154 * For each potential group of children that are lined up horizontally, the values returned by
155 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures.
156 * Any child spacing should be removed from the input @for_width and then the collective size should be
157 * allocated using the gtk_distribute_natural_allocation() convenience function.
159 * The container will then move on to request the preferred height for each child by using
160 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
162 * To allocate a height-for-width container, it's again important
163 * to consider that a container must prioritize one dimension over the other. So if
164 * a container is a height-for-width container it must first allocate all widgets horizontally
165 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
166 * extra space (if and where appropriate) for the widget to expand.
168 * After adding all the expand space, the container assumes it was allocated sufficient
169 * height to fit all of its content. At this time, the container must use the total horizontal sizes
170 * of each widget to request the height-for-width of each of its children and store the requests in a
171 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
172 * be generalized into the heights and widths of rows and columns).
173 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
174 * while this time considering the allocated height of the widget minus any vertical spacing
175 * that the container adds. Then vertical expand space should be added where appropriate and available
176 * and the container should go on to actually allocating the child widgets.
178 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
179 * to learn more about implementing height-for-width geometry management for widgets.
182 * <refsect2 id="child-properties">
183 * <title>Child properties</title>
185 * GtkContainer introduces <emphasis>child properties</emphasis>.
186 * These are object properties that are not specific
187 * to either the container or the contained widget, but rather to their relation.
188 * Typical examples of child properties are the position or pack-type of a widget
189 * which is contained in a #GtkBox.
191 * Use gtk_container_class_install_child_property() to install child properties
192 * for a container class and gtk_container_class_find_child_property() or
193 * gtk_container_class_list_child_properties() to get information about existing
196 * To set the value of a child property, use gtk_container_child_set_property(),
197 * gtk_container_child_set() or gtk_container_child_set_valist().
198 * To obtain the value of a child property, use
199 * gtk_container_child_get_property(), gtk_container_child_get() or
200 * gtk_container_child_get_valist(). To emit notification about child property
201 * changes, use gtk_widget_child_notify().
204 * <refsect2 id="GtkContainer-BUILDER-UI">
205 * <title>GtkContainer as GtkBuildable</title>
207 * The GtkContainer implementation of the GtkBuildable interface
208 * supports a <packing> element for children, which can
209 * contain multiple <property> elements that specify
210 * child properties for the child.
212 * <title>Child properties in UI definitions</title>
213 * <programlisting><![CDATA[
214 * <object class="GtkVBox">
216 * <object class="GtkLabel"/>
218 * <property name="pack-type">start</property>
222 * ]]></programlisting>
224 * Since 2.16, child properties can also be marked as translatable using
225 * the same "translatable", "comments" and "context" attributes that are used
226 * for regular properties.
232 struct _GtkContainerPrivate
234 GtkWidget *focus_child;
236 guint border_width : 16;
238 guint has_focus_chain : 1;
239 guint need_resize : 1;
240 guint reallocate_redraws : 1;
241 guint resize_mode : 2;
259 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
260 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
263 /* --- prototypes --- */
264 static void gtk_container_base_class_init (GtkContainerClass *klass);
265 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
266 static void gtk_container_class_init (GtkContainerClass *klass);
267 static void gtk_container_init (GtkContainer *container);
268 static void gtk_container_destroy (GtkWidget *widget);
269 static void gtk_container_set_property (GObject *object,
273 static void gtk_container_get_property (GObject *object,
277 static void gtk_container_add_unimplemented (GtkContainer *container,
279 static void gtk_container_remove_unimplemented (GtkContainer *container,
281 static void gtk_container_real_check_resize (GtkContainer *container);
282 static void gtk_container_compute_expand (GtkWidget *widget,
284 gboolean *vexpand_p);
285 static gboolean gtk_container_focus (GtkWidget *widget,
286 GtkDirectionType direction);
287 static void gtk_container_real_set_focus_child (GtkContainer *container,
290 static gboolean gtk_container_focus_move (GtkContainer *container,
292 GtkDirectionType direction);
293 static void gtk_container_children_callback (GtkWidget *widget,
294 gpointer client_data);
295 static void gtk_container_show_all (GtkWidget *widget);
296 static gint gtk_container_draw (GtkWidget *widget,
298 static void gtk_container_map (GtkWidget *widget);
299 static void gtk_container_unmap (GtkWidget *widget);
300 static void gtk_container_adjust_size_request (GtkWidget *widget,
301 GtkOrientation orientation,
304 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
305 GtkOrientation orientation,
309 gint *allocated_size);
311 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
314 static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
318 static void gtk_container_buildable_init (GtkBuildableIface *iface);
319 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
323 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
326 const gchar *tagname,
327 GMarkupParser *parser,
329 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
332 const gchar *tagname,
336 /* --- variables --- */
337 static const gchar vadjustment_key[] = "gtk-vadjustment";
338 static guint vadjustment_key_id = 0;
339 static const gchar hadjustment_key[] = "gtk-hadjustment";
340 static guint hadjustment_key_id = 0;
341 static GSList *container_resize_queue = NULL;
342 static guint container_signals[LAST_SIGNAL] = { 0 };
343 static GtkWidgetClass *parent_class = NULL;
344 extern GParamSpecPool *_gtk_widget_child_property_pool;
345 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
346 static GtkBuildableIface *parent_buildable_iface;
349 /* --- functions --- */
351 gtk_container_get_type (void)
353 static GType container_type = 0;
357 const GTypeInfo container_info =
359 sizeof (GtkContainerClass),
360 (GBaseInitFunc) gtk_container_base_class_init,
361 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
362 (GClassInitFunc) gtk_container_class_init,
363 NULL /* class_finalize */,
364 NULL /* class_data */,
365 sizeof (GtkContainer),
367 (GInstanceInitFunc) gtk_container_init,
368 NULL, /* value_table */
371 const GInterfaceInfo buildable_info =
373 (GInterfaceInitFunc) gtk_container_buildable_init,
379 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
380 &container_info, G_TYPE_FLAG_ABSTRACT);
382 g_type_add_interface_static (container_type,
388 return container_type;
392 gtk_container_base_class_init (GtkContainerClass *class)
394 /* reset instance specifc class fields that don't get inherited */
395 class->set_child_property = NULL;
396 class->get_child_property = NULL;
400 gtk_container_base_class_finalize (GtkContainerClass *class)
404 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
405 for (node = list; node; node = node->next)
407 GParamSpec *pspec = node->data;
409 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
410 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
411 g_param_spec_unref (pspec);
417 gtk_container_class_init (GtkContainerClass *class)
419 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
420 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
422 parent_class = g_type_class_peek_parent (class);
424 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
425 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
427 gobject_class->set_property = gtk_container_set_property;
428 gobject_class->get_property = gtk_container_get_property;
430 widget_class->destroy = gtk_container_destroy;
431 widget_class->compute_expand = gtk_container_compute_expand;
432 widget_class->show_all = gtk_container_show_all;
433 widget_class->draw = gtk_container_draw;
434 widget_class->map = gtk_container_map;
435 widget_class->unmap = gtk_container_unmap;
436 widget_class->focus = gtk_container_focus;
438 widget_class->adjust_size_request = gtk_container_adjust_size_request;
439 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
441 class->add = gtk_container_add_unimplemented;
442 class->remove = gtk_container_remove_unimplemented;
443 class->check_resize = gtk_container_real_check_resize;
444 class->forall = NULL;
445 class->set_focus_child = gtk_container_real_set_focus_child;
446 class->child_type = NULL;
447 class->composite_name = gtk_container_child_default_composite_name;
448 class->get_path_for_child = gtk_container_real_get_path_for_child;
450 g_object_class_install_property (gobject_class,
452 g_param_spec_enum ("resize-mode",
454 P_("Specify how resize events are handled"),
455 GTK_TYPE_RESIZE_MODE,
457 GTK_PARAM_READWRITE));
458 g_object_class_install_property (gobject_class,
460 g_param_spec_uint ("border-width",
462 P_("The width of the empty border outside the containers children"),
466 GTK_PARAM_READWRITE));
467 g_object_class_install_property (gobject_class,
469 g_param_spec_object ("child",
471 P_("Can be used to add a new child to the container"),
473 GTK_PARAM_WRITABLE));
474 container_signals[ADD] =
475 g_signal_new (I_("add"),
476 G_OBJECT_CLASS_TYPE (gobject_class),
478 G_STRUCT_OFFSET (GtkContainerClass, add),
480 _gtk_marshal_VOID__OBJECT,
483 container_signals[REMOVE] =
484 g_signal_new (I_("remove"),
485 G_OBJECT_CLASS_TYPE (gobject_class),
487 G_STRUCT_OFFSET (GtkContainerClass, remove),
489 _gtk_marshal_VOID__OBJECT,
492 container_signals[CHECK_RESIZE] =
493 g_signal_new (I_("check-resize"),
494 G_OBJECT_CLASS_TYPE (gobject_class),
496 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
498 _gtk_marshal_VOID__VOID,
500 container_signals[SET_FOCUS_CHILD] =
501 g_signal_new (I_("set-focus-child"),
502 G_OBJECT_CLASS_TYPE (gobject_class),
504 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
506 _gtk_marshal_VOID__OBJECT,
510 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
514 gtk_container_buildable_init (GtkBuildableIface *iface)
516 parent_buildable_iface = g_type_interface_peek_parent (iface);
517 iface->add_child = gtk_container_buildable_add_child;
518 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
519 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
523 gtk_container_buildable_add_child (GtkBuildable *buildable,
530 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
532 else if (GTK_IS_WIDGET (child) &&
533 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
535 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
538 g_warning ("Cannot add an object of type %s to a container of type %s",
539 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
543 gtk_container_buildable_set_child_property (GtkContainer *container,
550 GValue gvalue = { 0, };
551 GError *error = NULL;
553 pspec = gtk_container_class_find_child_property
554 (G_OBJECT_GET_CLASS (container), name);
557 g_warning ("%s does not have a property called %s",
558 g_type_name (G_OBJECT_TYPE (container)), name);
562 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
564 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
565 g_type_name (G_OBJECT_TYPE (container)),
568 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
570 g_error_free (error);
574 gtk_container_child_set_property (container, child, name, &gvalue);
575 g_value_unset (&gvalue);
580 GtkContainer *container;
583 gchar *child_prop_name;
585 gboolean translatable;
586 } PackingPropertiesData;
589 attributes_start_element (GMarkupParseContext *context,
590 const gchar *element_name,
592 const gchar **values,
596 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
599 if (strcmp (element_name, "property") == 0)
601 for (i = 0; names[i]; i++)
602 if (strcmp (names[i], "name") == 0)
603 parser_data->child_prop_name = g_strdup (values[i]);
604 else if (strcmp (names[i], "translatable") == 0)
606 if (!_gtk_builder_boolean_from_string (values[1],
607 &parser_data->translatable,
611 else if (strcmp (names[i], "comments") == 0)
612 ; /* for translators */
613 else if (strcmp (names[i], "context") == 0)
614 parser_data->context = g_strdup (values[1]);
616 g_warning ("Unsupported attribute for GtkContainer Child "
617 "property: %s\n", names[i]);
619 else if (strcmp (element_name, "packing") == 0)
622 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
626 attributes_text_element (GMarkupParseContext *context,
632 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
634 if (parser_data->child_prop_name)
635 g_string_append_len (parser_data->string, text, text_len);
639 attributes_end_element (GMarkupParseContext *context,
640 const gchar *element_name,
644 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
646 /* translate the string */
647 if (parser_data->string->len && parser_data->translatable)
652 domain = gtk_builder_get_translation_domain (parser_data->builder);
654 translated = _gtk_builder_parser_translate (domain,
655 parser_data->context,
656 parser_data->string->str);
657 g_string_set_size (parser_data->string, 0);
658 g_string_append (parser_data->string, translated);
661 if (parser_data->child_prop_name)
662 gtk_container_buildable_set_child_property (parser_data->container,
663 parser_data->builder,
665 parser_data->child_prop_name,
666 parser_data->string->str);
668 g_string_set_size (parser_data->string, 0);
669 g_free (parser_data->child_prop_name);
670 g_free (parser_data->context);
671 parser_data->child_prop_name = NULL;
672 parser_data->context = NULL;
673 parser_data->translatable = FALSE;
676 static const GMarkupParser attributes_parser =
678 attributes_start_element,
679 attributes_end_element,
680 attributes_text_element,
684 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
687 const gchar *tagname,
688 GMarkupParser *parser,
691 PackingPropertiesData *parser_data;
693 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
694 tagname, parser, data))
697 if (child && strcmp (tagname, "packing") == 0)
699 parser_data = g_slice_new0 (PackingPropertiesData);
700 parser_data->string = g_string_new ("");
701 parser_data->builder = builder;
702 parser_data->container = GTK_CONTAINER (buildable);
703 parser_data->child = GTK_WIDGET (child);
704 parser_data->child_prop_name = NULL;
706 *parser = attributes_parser;
715 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
718 const gchar *tagname,
721 if (strcmp (tagname, "packing") == 0)
723 PackingPropertiesData *parser_data = (PackingPropertiesData*)data;
724 g_string_free (parser_data->string, TRUE);
725 g_slice_free (PackingPropertiesData, parser_data);
729 if (parent_buildable_iface->custom_tag_end)
730 parent_buildable_iface->custom_tag_end (buildable, builder,
731 child, tagname, data);
735 * gtk_container_child_type:
736 * @container: a #GtkContainer
738 * Returns the type of the children supported by the container.
740 * Note that this may return %G_TYPE_NONE to indicate that no more
741 * children can be added, e.g. for a #GtkPaned which already has two
744 * Return value: a #GType.
747 gtk_container_child_type (GtkContainer *container)
750 GtkContainerClass *class;
752 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
754 class = GTK_CONTAINER_GET_CLASS (container);
755 if (class->child_type)
756 slot = class->child_type (container);
763 /* --- GtkContainer child property mechanism --- */
765 container_get_child_property (GtkContainer *container,
770 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
772 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
776 container_set_child_property (GtkContainer *container,
780 GObjectNotifyQueue *nqueue)
782 GValue tmp_value = { 0, };
783 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
785 /* provide a copy to work from, convert (if necessary) and validate */
786 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
787 if (!g_value_transform (value, &tmp_value))
788 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
790 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
791 G_VALUE_TYPE_NAME (value));
792 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
794 gchar *contents = g_strdup_value_contents (value);
796 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
798 G_VALUE_TYPE_NAME (value),
800 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
805 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
806 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
808 g_value_unset (&tmp_value);
812 * gtk_container_child_get_valist:
813 * @container: a #GtkContainer
814 * @child: a widget which is a child of @container
815 * @first_property_name: the name of the first property to get
816 * @var_args: return location for the first property, followed
817 * optionally by more name/return location pairs, followed by %NULL
819 * Gets the values of one or more child properties for @child and @container.
822 gtk_container_child_get_valist (GtkContainer *container,
824 const gchar *first_property_name,
829 g_return_if_fail (GTK_IS_CONTAINER (container));
830 g_return_if_fail (GTK_IS_WIDGET (child));
831 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
833 g_object_ref (container);
834 g_object_ref (child);
836 name = first_property_name;
839 GValue value = { 0, };
843 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
845 G_OBJECT_TYPE (container),
849 g_warning ("%s: container class `%s' has no child property named `%s'",
851 G_OBJECT_TYPE_NAME (container),
855 if (!(pspec->flags & G_PARAM_READABLE))
857 g_warning ("%s: child property `%s' of container class `%s' is not readable",
860 G_OBJECT_TYPE_NAME (container));
863 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
864 container_get_child_property (container, child, pspec, &value);
865 G_VALUE_LCOPY (&value, var_args, 0, &error);
868 g_warning ("%s: %s", G_STRLOC, error);
870 g_value_unset (&value);
873 g_value_unset (&value);
874 name = va_arg (var_args, gchar*);
877 g_object_unref (child);
878 g_object_unref (container);
882 * gtk_container_child_get_property:
883 * @container: a #GtkContainer
884 * @child: a widget which is a child of @container
885 * @property_name: the name of the property to get
886 * @value: a location to return the value
888 * Gets the value of a child property for @child and @container.
891 gtk_container_child_get_property (GtkContainer *container,
893 const gchar *property_name,
898 g_return_if_fail (GTK_IS_CONTAINER (container));
899 g_return_if_fail (GTK_IS_WIDGET (child));
900 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
901 g_return_if_fail (property_name != NULL);
902 g_return_if_fail (G_IS_VALUE (value));
904 g_object_ref (container);
905 g_object_ref (child);
906 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
907 G_OBJECT_TYPE (container), TRUE);
909 g_warning ("%s: container class `%s' has no child property named `%s'",
911 G_OBJECT_TYPE_NAME (container),
913 else if (!(pspec->flags & G_PARAM_READABLE))
914 g_warning ("%s: child property `%s' of container class `%s' is not readable",
917 G_OBJECT_TYPE_NAME (container));
920 GValue *prop_value, tmp_value = { 0, };
922 /* auto-conversion of the callers value type
924 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
926 g_value_reset (value);
929 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
931 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
933 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
934 G_VALUE_TYPE_NAME (value));
935 g_object_unref (child);
936 g_object_unref (container);
941 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
942 prop_value = &tmp_value;
944 container_get_child_property (container, child, pspec, prop_value);
945 if (prop_value != value)
947 g_value_transform (prop_value, value);
948 g_value_unset (&tmp_value);
951 g_object_unref (child);
952 g_object_unref (container);
956 * gtk_container_child_set_valist:
957 * @container: a #GtkContainer
958 * @child: a widget which is a child of @container
959 * @first_property_name: the name of the first property to set
960 * @var_args: a %NULL-terminated list of property names and values, starting
961 * with @first_prop_name
963 * Sets one or more child properties for @child and @container.
966 gtk_container_child_set_valist (GtkContainer *container,
968 const gchar *first_property_name,
971 GObjectNotifyQueue *nqueue;
974 g_return_if_fail (GTK_IS_CONTAINER (container));
975 g_return_if_fail (GTK_IS_WIDGET (child));
976 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
978 g_object_ref (container);
979 g_object_ref (child);
981 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
982 name = first_property_name;
985 GValue value = { 0, };
987 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
989 G_OBJECT_TYPE (container),
993 g_warning ("%s: container class `%s' has no child property named `%s'",
995 G_OBJECT_TYPE_NAME (container),
999 if (!(pspec->flags & G_PARAM_WRITABLE))
1001 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1004 G_OBJECT_TYPE_NAME (container));
1007 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1008 G_VALUE_COLLECT (&value, var_args, 0, &error);
1011 g_warning ("%s: %s", G_STRLOC, error);
1014 /* we purposely leak the value here, it might not be
1015 * in a sane state if an error condition occoured
1019 container_set_child_property (container, child, pspec, &value, nqueue);
1020 g_value_unset (&value);
1021 name = va_arg (var_args, gchar*);
1023 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1025 g_object_unref (container);
1026 g_object_unref (child);
1030 * gtk_container_child_set_property:
1031 * @container: a #GtkContainer
1032 * @child: a widget which is a child of @container
1033 * @property_name: the name of the property to set
1034 * @value: the value to set the property to
1036 * Sets a child property for @child and @container.
1039 gtk_container_child_set_property (GtkContainer *container,
1041 const gchar *property_name,
1042 const GValue *value)
1044 GObjectNotifyQueue *nqueue;
1047 g_return_if_fail (GTK_IS_CONTAINER (container));
1048 g_return_if_fail (GTK_IS_WIDGET (child));
1049 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1050 g_return_if_fail (property_name != NULL);
1051 g_return_if_fail (G_IS_VALUE (value));
1053 g_object_ref (container);
1054 g_object_ref (child);
1056 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1057 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1058 G_OBJECT_TYPE (container), TRUE);
1060 g_warning ("%s: container class `%s' has no child property named `%s'",
1062 G_OBJECT_TYPE_NAME (container),
1064 else if (!(pspec->flags & G_PARAM_WRITABLE))
1065 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1068 G_OBJECT_TYPE_NAME (container));
1071 container_set_child_property (container, child, pspec, value, nqueue);
1073 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1074 g_object_unref (container);
1075 g_object_unref (child);
1079 * gtk_container_add_with_properties:
1080 * @container: a #GtkContainer
1081 * @widget: a widget to be placed inside @container
1082 * @first_prop_name: the name of the first child property to set
1083 * @Varargs: a %NULL-terminated list of property names and values, starting
1084 * with @first_prop_name
1086 * Adds @widget to @container, setting child properties at the same time.
1087 * See gtk_container_add() and gtk_container_child_set() for more details.
1090 gtk_container_add_with_properties (GtkContainer *container,
1092 const gchar *first_prop_name,
1095 g_return_if_fail (GTK_IS_CONTAINER (container));
1096 g_return_if_fail (GTK_IS_WIDGET (widget));
1097 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1099 g_object_ref (container);
1100 g_object_ref (widget);
1101 gtk_widget_freeze_child_notify (widget);
1103 g_signal_emit (container, container_signals[ADD], 0, widget);
1104 if (gtk_widget_get_parent (widget))
1108 va_start (var_args, first_prop_name);
1109 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1113 gtk_widget_thaw_child_notify (widget);
1114 g_object_unref (widget);
1115 g_object_unref (container);
1119 * gtk_container_child_set:
1120 * @container: a #GtkContainer
1121 * @child: a widget which is a child of @container
1122 * @first_prop_name: the name of the first property to set
1123 * @Varargs: a %NULL-terminated list of property names and values, starting
1124 * with @first_prop_name
1126 * Sets one or more child properties for @child and @container.
1129 gtk_container_child_set (GtkContainer *container,
1131 const gchar *first_prop_name,
1136 g_return_if_fail (GTK_IS_CONTAINER (container));
1137 g_return_if_fail (GTK_IS_WIDGET (child));
1138 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1140 va_start (var_args, first_prop_name);
1141 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1146 * gtk_container_child_get:
1147 * @container: a #GtkContainer
1148 * @child: a widget which is a child of @container
1149 * @first_prop_name: the name of the first property to get
1150 * @Varargs: return location for the first property, followed
1151 * optionally by more name/return location pairs, followed by %NULL
1153 * Gets the values of one or more child properties for @child and @container.
1156 gtk_container_child_get (GtkContainer *container,
1158 const gchar *first_prop_name,
1163 g_return_if_fail (GTK_IS_CONTAINER (container));
1164 g_return_if_fail (GTK_IS_WIDGET (child));
1165 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1167 va_start (var_args, first_prop_name);
1168 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1173 * gtk_container_class_install_child_property:
1174 * @cclass: a #GtkContainerClass
1175 * @property_id: the id for the property
1176 * @pspec: the #GParamSpec for the property
1178 * Installs a child property on a container class.
1181 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1185 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1186 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1187 if (pspec->flags & G_PARAM_WRITABLE)
1188 g_return_if_fail (cclass->set_child_property != NULL);
1189 if (pspec->flags & G_PARAM_READABLE)
1190 g_return_if_fail (cclass->get_child_property != NULL);
1191 g_return_if_fail (property_id > 0);
1192 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1193 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1194 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1196 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1198 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1199 G_OBJECT_CLASS_NAME (cclass),
1203 g_param_spec_ref (pspec);
1204 g_param_spec_sink (pspec);
1205 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1206 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1210 * gtk_container_class_find_child_property:
1211 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1212 * @property_name: the name of the child property to find
1213 * @returns: (transfer none): the #GParamSpec of the child property or
1214 * %NULL if @class has no child property with that name.
1216 * Finds a child property of a container class by name.
1219 gtk_container_class_find_child_property (GObjectClass *cclass,
1220 const gchar *property_name)
1222 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1223 g_return_val_if_fail (property_name != NULL, NULL);
1225 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1227 G_OBJECT_CLASS_TYPE (cclass),
1232 * gtk_container_class_list_child_properties:
1233 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1234 * @n_properties: location to return the number of child properties found
1235 * @returns: (array length=n_properties) (transfer container): a newly
1236 * allocated %NULL-terminated array of #GParamSpec*. The
1237 * array must be freed with g_free().
1239 * Returns all child properties of a container class.
1242 gtk_container_class_list_child_properties (GObjectClass *cclass,
1243 guint *n_properties)
1245 GParamSpec **pspecs;
1248 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1250 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1251 G_OBJECT_CLASS_TYPE (cclass),
1260 gtk_container_add_unimplemented (GtkContainer *container,
1263 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1267 gtk_container_remove_unimplemented (GtkContainer *container,
1270 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1274 gtk_container_init (GtkContainer *container)
1276 GtkContainerPrivate *priv;
1278 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1280 GtkContainerPrivate);
1281 priv = container->priv;
1283 priv->focus_child = NULL;
1284 priv->border_width = 0;
1285 priv->need_resize = FALSE;
1286 priv->resize_mode = GTK_RESIZE_PARENT;
1287 priv->reallocate_redraws = FALSE;
1291 gtk_container_destroy (GtkWidget *widget)
1293 GtkContainer *container = GTK_CONTAINER (widget);
1294 GtkContainerPrivate *priv = container->priv;
1296 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1297 _gtk_container_dequeue_resize_handler (container);
1299 if (priv->focus_child)
1301 g_object_unref (priv->focus_child);
1302 priv->focus_child = NULL;
1305 /* do this before walking child widgets, to avoid
1306 * removing children from focus chain one by one.
1308 if (priv->has_focus_chain)
1309 gtk_container_unset_focus_chain (container);
1311 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1313 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1317 gtk_container_set_property (GObject *object,
1319 const GValue *value,
1322 GtkContainer *container = GTK_CONTAINER (object);
1326 case PROP_BORDER_WIDTH:
1327 gtk_container_set_border_width (container, g_value_get_uint (value));
1329 case PROP_RESIZE_MODE:
1330 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1333 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1342 gtk_container_get_property (GObject *object,
1347 GtkContainer *container = GTK_CONTAINER (object);
1348 GtkContainerPrivate *priv = container->priv;
1352 case PROP_BORDER_WIDTH:
1353 g_value_set_uint (value, priv->border_width);
1355 case PROP_RESIZE_MODE:
1356 g_value_set_enum (value, priv->resize_mode);
1359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1365 * gtk_container_set_border_width:
1366 * @container: a #GtkContainer
1367 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1368 * the container. Valid values are in the range 0-65535 pixels.
1370 * Sets the border width of the container.
1372 * The border width of a container is the amount of space to leave
1373 * around the outside of the container. The only exception to this is
1374 * #GtkWindow; because toplevel windows can't leave space outside,
1375 * they leave the space inside. The border is added on all sides of
1376 * the container. To add space to only one side, one approach is to
1377 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1378 * to give it a size, and place it on the side of the container as
1382 gtk_container_set_border_width (GtkContainer *container,
1385 GtkContainerPrivate *priv;
1387 g_return_if_fail (GTK_IS_CONTAINER (container));
1389 priv = container->priv;
1391 if (priv->border_width != border_width)
1393 priv->border_width = border_width;
1394 g_object_notify (G_OBJECT (container), "border-width");
1396 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1397 gtk_widget_queue_resize (GTK_WIDGET (container));
1402 * gtk_container_get_border_width:
1403 * @container: a #GtkContainer
1405 * Retrieves the border width of the container. See
1406 * gtk_container_set_border_width().
1408 * Return value: the current border width
1411 gtk_container_get_border_width (GtkContainer *container)
1413 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1415 return container->priv->border_width;
1419 * gtk_container_add:
1420 * @container: a #GtkContainer
1421 * @widget: a widget to be placed inside @container
1423 * Adds @widget to @container. Typically used for simple containers
1424 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1425 * layout containers such as #GtkBox or #GtkTable, this function will
1426 * pick default packing parameters that may not be correct. So
1427 * consider functions such as gtk_box_pack_start() and
1428 * gtk_table_attach() as an alternative to gtk_container_add() in
1429 * those cases. A widget may be added to only one container at a time;
1430 * you can't place the same widget inside two different containers.
1433 gtk_container_add (GtkContainer *container,
1438 g_return_if_fail (GTK_IS_CONTAINER (container));
1439 g_return_if_fail (GTK_IS_WIDGET (widget));
1441 parent = gtk_widget_get_parent (widget);
1445 g_warning ("Attempting to add a widget with type %s to a container of "
1446 "type %s, but the widget is already inside a container of type %s, "
1447 "please use gtk_widget_reparent()" ,
1448 g_type_name (G_OBJECT_TYPE (widget)),
1449 g_type_name (G_OBJECT_TYPE (container)),
1450 g_type_name (G_OBJECT_TYPE (parent)));
1454 g_signal_emit (container, container_signals[ADD], 0, widget);
1458 * gtk_container_remove:
1459 * @container: a #GtkContainer
1460 * @widget: a current child of @container
1462 * Removes @widget from @container. @widget must be inside @container.
1463 * Note that @container will own a reference to @widget, and that this
1464 * may be the last reference held; so removing a widget from its
1465 * container can destroy that widget. If you want to use @widget
1466 * again, you need to add a reference to it while it's not inside
1467 * a container, using g_object_ref(). If you don't want to use @widget
1468 * again it's usually more efficient to simply destroy it directly
1469 * using gtk_widget_destroy() since this will remove it from the
1470 * container and help break any circular reference count cycles.
1473 gtk_container_remove (GtkContainer *container,
1476 g_return_if_fail (GTK_IS_CONTAINER (container));
1477 g_return_if_fail (GTK_IS_WIDGET (widget));
1478 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1480 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1484 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1486 g_return_if_fail (GTK_IS_CONTAINER (container));
1487 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1489 container_resize_queue = g_slist_remove (container_resize_queue, container);
1490 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1494 * gtk_container_set_resize_mode:
1495 * @container: a #GtkContainer
1496 * @resize_mode: the new resize mode
1498 * Sets the resize mode for the container.
1500 * The resize mode of a container determines whether a resize request
1501 * will be passed to the container's parent, queued for later execution
1502 * or executed immediately.
1505 gtk_container_set_resize_mode (GtkContainer *container,
1506 GtkResizeMode resize_mode)
1508 GtkContainerPrivate *priv;
1510 g_return_if_fail (GTK_IS_CONTAINER (container));
1511 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1513 priv = container->priv;
1515 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1516 resize_mode == GTK_RESIZE_PARENT)
1518 resize_mode = GTK_RESIZE_QUEUE;
1521 if (priv->resize_mode != resize_mode)
1523 priv->resize_mode = resize_mode;
1525 gtk_widget_queue_resize (GTK_WIDGET (container));
1526 g_object_notify (G_OBJECT (container), "resize-mode");
1531 * gtk_container_get_resize_mode:
1532 * @container: a #GtkContainer
1534 * Returns the resize mode for the container. See
1535 * gtk_container_set_resize_mode ().
1537 * Return value: the current resize mode
1540 gtk_container_get_resize_mode (GtkContainer *container)
1542 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1544 return container->priv->resize_mode;
1548 * gtk_container_set_reallocate_redraws:
1549 * @container: a #GtkContainer
1550 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1552 * Sets the @reallocate_redraws flag of the container to the given value.
1554 * Containers requesting reallocation redraws get automatically
1555 * redrawn if any of their children changed allocation.
1558 gtk_container_set_reallocate_redraws (GtkContainer *container,
1559 gboolean needs_redraws)
1561 g_return_if_fail (GTK_IS_CONTAINER (container));
1563 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1566 static GtkContainer*
1567 gtk_container_get_resize_container (GtkContainer *container)
1570 GtkWidget *widget = GTK_WIDGET (container);
1572 while ((parent = gtk_widget_get_parent (widget)))
1575 if (GTK_IS_RESIZE_CONTAINER (widget))
1579 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1583 gtk_container_idle_sizer (gpointer data)
1585 /* we may be invoked with a container_resize_queue of NULL, because
1586 * queue_resize could have been adding an extra idle function while
1587 * the queue still got processed. we better just ignore such case
1588 * than trying to explicitely work around them with some extra flags,
1589 * since it doesn't cause any actual harm.
1591 while (container_resize_queue)
1596 slist = container_resize_queue;
1597 container_resize_queue = slist->next;
1598 widget = slist->data;
1599 g_slist_free_1 (slist);
1601 _gtk_widget_set_resize_pending (widget, FALSE);
1602 gtk_container_check_resize (GTK_CONTAINER (widget));
1605 gdk_window_process_all_updates ();
1611 _gtk_container_queue_resize_internal (GtkContainer *container,
1612 gboolean invalidate_only)
1614 GtkContainer *resize_container;
1618 g_return_if_fail (GTK_IS_CONTAINER (container));
1620 widget = GTK_WIDGET (container);
1622 resize_container = gtk_container_get_resize_container (container);
1626 _gtk_widget_set_alloc_needed (widget, TRUE);
1627 _gtk_widget_set_width_request_needed (widget, TRUE);
1628 _gtk_widget_set_height_request_needed (widget, TRUE);
1630 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1631 !(parent = gtk_widget_get_parent (widget)))
1637 if (resize_container && !invalidate_only)
1639 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1640 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1641 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1643 switch (resize_container->priv->resize_mode)
1645 case GTK_RESIZE_QUEUE:
1646 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1648 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1649 if (container_resize_queue == NULL)
1650 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1651 gtk_container_idle_sizer,
1653 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1657 case GTK_RESIZE_IMMEDIATE:
1658 gtk_container_check_resize (resize_container);
1661 case GTK_RESIZE_PARENT:
1662 g_assert_not_reached ();
1668 /* we need to let hidden resize containers know that something
1669 * changed while they where hidden (currently only evaluated by
1672 resize_container->priv->need_resize = TRUE;
1678 * _gtk_container_queue_resize:
1679 * @container: a #GtkContainer
1681 * Determines the "resize container" in the hierarchy above this container
1682 * (typically the toplevel, but other containers can be set as resize
1683 * containers with gtk_container_set_resize_mode()), marks the container
1684 * and all parents up to and including the resize container as needing
1685 * to have sizes recompted, and if necessary adds the resize container
1686 * to the queue of containers that will be resized out at idle.
1689 _gtk_container_queue_resize (GtkContainer *container)
1691 _gtk_container_queue_resize_internal (container, FALSE);
1695 * _gtk_container_resize_invalidate:
1696 * @container: a #GtkContainer
1698 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1699 * actually queue the resize container for resize.
1702 _gtk_container_resize_invalidate (GtkContainer *container)
1704 _gtk_container_queue_resize_internal (container, TRUE);
1708 gtk_container_check_resize (GtkContainer *container)
1710 g_return_if_fail (GTK_IS_CONTAINER (container));
1712 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1716 gtk_container_real_check_resize (GtkContainer *container)
1718 GtkWidget *widget = GTK_WIDGET (container);
1719 GtkAllocation allocation;
1720 GtkRequisition requisition;
1722 gtk_widget_get_preferred_size (widget,
1723 &requisition, NULL);
1724 gtk_widget_get_allocation (widget, &allocation);
1726 if (requisition.width > allocation.width ||
1727 requisition.height > allocation.height)
1729 if (GTK_IS_RESIZE_CONTAINER (container))
1731 gtk_widget_size_allocate (widget, &allocation);
1732 gtk_widget_set_allocation (widget, &allocation);
1735 gtk_widget_queue_resize (widget);
1739 gtk_container_resize_children (container);
1743 /* The container hasn't changed size but one of its children
1744 * queued a resize request. Which means that the allocation
1745 * is not sufficient for the requisition of some child.
1746 * We've already performed a size request at this point,
1747 * so we simply need to reallocate and let the allocation
1748 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1751 gtk_container_resize_children (GtkContainer *container)
1753 GtkAllocation allocation;
1756 /* resizing invariants:
1757 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1758 * containers that have an idle sizer pending must be flagged with
1761 g_return_if_fail (GTK_IS_CONTAINER (container));
1763 widget = GTK_WIDGET (container);
1764 gtk_widget_get_allocation (widget, &allocation);
1766 gtk_widget_size_allocate (widget, &allocation);
1767 gtk_widget_set_allocation (widget, &allocation);
1771 gtk_container_adjust_size_request (GtkWidget *widget,
1772 GtkOrientation orientation,
1776 GtkContainer *container;
1778 container = GTK_CONTAINER (widget);
1780 if (GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1784 border_width = container->priv->border_width;
1786 *minimum_size += border_width * 2;
1787 *natural_size += border_width * 2;
1790 /* chain up last so gtk_widget_set_size_request() values
1791 * will have a chance to overwrite our border width.
1793 parent_class->adjust_size_request (widget, orientation,
1794 minimum_size, natural_size);
1798 gtk_container_adjust_size_allocation (GtkWidget *widget,
1799 GtkOrientation orientation,
1802 gint *allocated_pos,
1803 gint *allocated_size)
1805 GtkContainer *container;
1808 container = GTK_CONTAINER (widget);
1810 if (!GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1812 parent_class->adjust_size_allocation (widget, orientation,
1813 minimum_size, natural_size, allocated_pos,
1818 border_width = container->priv->border_width;
1820 *allocated_size -= border_width * 2;
1822 /* If we get a pathological too-small allocation to hold
1823 * even the border width, leave all allocation to the actual
1824 * widget, and leave x,y unchanged. (GtkWidget's min size is
1825 * 1x1 if you're wondering why <1 and not <0)
1827 * As long as we have space, set x,y properly.
1830 if (*allocated_size < 1)
1832 *allocated_size += border_width * 2;
1836 *allocated_pos += border_width;
1837 *minimum_size -= border_width * 2;
1838 *natural_size -= border_width * 2;
1841 /* Chain up to GtkWidgetClass *after* removing our border width from
1842 * the proposed allocation size. This is because it's possible that the
1843 * widget was allocated more space than it needs in a said orientation,
1844 * if GtkWidgetClass does any alignments and thus limits the size to the
1845 * natural size... then we need that to be done *after* removing any margins
1846 * and padding values.
1848 parent_class->adjust_size_allocation (widget, orientation,
1849 minimum_size, natural_size, allocated_pos,
1854 * gtk_container_class_handle_border_width:
1855 * @klass: the class struct of a #GtkContainer subclass
1857 * Modifies a subclass of #GtkContainerClass to automatically add and
1858 * remove the border-width setting on GtkContainer. This allows the
1859 * subclass to ignore the border width in its size request and
1860 * allocate methods. The intent is for a subclass to invoke this
1861 * in its class_init function.
1863 * gtk_container_class_handle_border_width() is necessary because it
1864 * would break API too badly to make this behavior the default. So
1865 * subclasses must "opt in" to the parent class handling border_width
1869 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1871 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1873 klass->_handle_border_width = TRUE;
1877 * gtk_container_forall:
1878 * @container: a #GtkContainer
1879 * @callback: (scope call): a callback
1880 * @callback_data: callback user data
1882 * Invokes @callback on each child of @container, including children
1883 * that are considered "internal" (implementation details of the
1884 * container). "Internal" children generally weren't added by the user
1885 * of the container, but were added by the container implementation
1886 * itself. Most applications should use gtk_container_foreach(),
1887 * rather than gtk_container_forall().
1890 gtk_container_forall (GtkContainer *container,
1891 GtkCallback callback,
1892 gpointer callback_data)
1894 GtkContainerClass *class;
1896 g_return_if_fail (GTK_IS_CONTAINER (container));
1897 g_return_if_fail (callback != NULL);
1899 class = GTK_CONTAINER_GET_CLASS (container);
1902 class->forall (container, TRUE, callback, callback_data);
1906 * gtk_container_foreach:
1907 * @container: a #GtkContainer
1908 * @callback: (scope call): a callback
1909 * @callback_data: callback user data
1911 * Invokes @callback on each non-internal child of @container. See
1912 * gtk_container_forall() for details on what constitutes an
1913 * "internal" child. Most applications should use
1914 * gtk_container_foreach(), rather than gtk_container_forall().
1917 gtk_container_foreach (GtkContainer *container,
1918 GtkCallback callback,
1919 gpointer callback_data)
1921 GtkContainerClass *class;
1923 g_return_if_fail (GTK_IS_CONTAINER (container));
1924 g_return_if_fail (callback != NULL);
1926 class = GTK_CONTAINER_GET_CLASS (container);
1929 class->forall (container, FALSE, callback, callback_data);
1933 * gtk_container_set_focus_child:
1934 * @container: a #GtkContainer
1935 * @child: (allow-none): a #GtkWidget, or %NULL
1937 * Sets, or unsets if @child is %NULL, the focused child of @container.
1939 * This function emits the GtkContainer::set_focus_child signal of
1940 * @container. Implementations of #GtkContainer can override the
1941 * default behaviour by overriding the class closure of this signal.
1943 * This is function is mostly meant to be used by widgets. Applications can use
1944 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1947 gtk_container_set_focus_child (GtkContainer *container,
1950 g_return_if_fail (GTK_IS_CONTAINER (container));
1952 g_return_if_fail (GTK_IS_WIDGET (child));
1954 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1958 * gtk_container_get_focus_child:
1959 * @container: a #GtkContainer
1961 * Returns the current focus child widget inside @container. This is not the
1962 * currently focused widget. That can be obtained by calling
1963 * gtk_window_get_focus().
1965 * Returns: (transfer none): The child widget which will receive the
1966 * focus inside @container when the @conatiner is focussed,
1967 * or %NULL if none is set.
1972 gtk_container_get_focus_child (GtkContainer *container)
1974 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1976 return container->priv->focus_child;
1980 * gtk_container_get_children:
1981 * @container: a #GtkContainer
1983 * Returns the container's non-internal children. See
1984 * gtk_container_forall() for details on what constitutes an "internal" child.
1986 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1989 gtk_container_get_children (GtkContainer *container)
1991 GList *children = NULL;
1993 gtk_container_foreach (container,
1994 gtk_container_children_callback,
1997 return g_list_reverse (children);
2001 gtk_container_child_position_callback (GtkWidget *widget,
2002 gpointer client_data)
2008 } *data = client_data;
2011 if (data->child == widget)
2012 data->index = data->i;
2016 gtk_container_child_default_composite_name (GtkContainer *container,
2026 /* fallback implementation */
2030 gtk_container_forall (container,
2031 gtk_container_child_position_callback,
2034 name = g_strdup_printf ("%s-%u",
2035 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2042 _gtk_container_child_composite_name (GtkContainer *container,
2045 gboolean composite_child;
2047 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2048 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2049 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2051 g_object_get (child, "composite-child", &composite_child, NULL);
2052 if (composite_child)
2054 static GQuark quark_composite_name = 0;
2057 if (!quark_composite_name)
2058 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2060 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2063 GtkContainerClass *class;
2065 class = GTK_CONTAINER_GET_CLASS (container);
2066 if (class->composite_name)
2067 name = class->composite_name (container, child);
2070 name = g_strdup (name);
2081 } ComputeExpandData;
2084 gtk_container_compute_expand_callback (GtkWidget *widget,
2085 gpointer client_data)
2087 ComputeExpandData *data = client_data;
2089 /* note that we don't get_expand on the child if we already know we
2090 * have to expand, so we only recurse into children until we find
2091 * one that expands and then we basically don't do any more
2092 * work. This means that we can leave some children in a
2093 * need_compute_expand state, which is fine, as long as GtkWidget
2094 * doesn't rely on an invariant that "if a child has
2095 * need_compute_expand, its parents also do"
2097 * gtk_widget_compute_expand() always returns FALSE if the
2098 * child is !visible so that's taken care of.
2100 data->hexpand = data->hexpand ||
2101 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2103 data->vexpand = data->vexpand ||
2104 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2108 gtk_container_compute_expand (GtkWidget *widget,
2109 gboolean *hexpand_p,
2110 gboolean *vexpand_p)
2112 ComputeExpandData data;
2114 data.hexpand = FALSE;
2115 data.vexpand = FALSE;
2117 gtk_container_forall (GTK_CONTAINER (widget),
2118 gtk_container_compute_expand_callback,
2121 *hexpand_p = data.hexpand;
2122 *vexpand_p = data.vexpand;
2126 gtk_container_real_set_focus_child (GtkContainer *container,
2129 GtkContainerPrivate *priv;
2131 g_return_if_fail (GTK_IS_CONTAINER (container));
2132 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2134 priv = container->priv;
2136 if (child != priv->focus_child)
2138 if (priv->focus_child)
2139 g_object_unref (priv->focus_child);
2140 priv->focus_child = child;
2141 if (priv->focus_child)
2142 g_object_ref (priv->focus_child);
2146 /* check for h/v adjustments
2148 if (priv->focus_child)
2150 GtkAdjustment *hadj;
2151 GtkAdjustment *vadj;
2152 GtkAllocation allocation;
2153 GtkWidget *focus_child;
2156 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2157 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2161 focus_child = priv->focus_child;
2162 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2164 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2167 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2170 gtk_widget_get_allocation (priv->focus_child, &allocation);
2174 gtk_widget_get_allocation (focus_child, &allocation);
2177 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2180 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2186 get_focus_chain (GtkContainer *container)
2188 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2191 /* same as gtk_container_get_children, except it includes internals
2194 gtk_container_get_all_children (GtkContainer *container)
2196 GList *children = NULL;
2198 gtk_container_forall (container,
2199 gtk_container_children_callback,
2205 static GtkWidgetPath *
2206 gtk_container_real_get_path_for_child (GtkContainer *container,
2209 GtkStyleContext *context;
2210 GtkWidgetPath *path;
2213 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2214 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
2216 /* Copy any permanent classes to the path */
2217 classes = gtk_style_context_list_classes (context);
2224 classes = classes->next;
2226 gtk_widget_path_iter_add_class (path, -1, cur->data);
2227 g_list_free_1 (cur);
2234 gtk_container_focus (GtkWidget *widget,
2235 GtkDirectionType direction)
2238 GList *sorted_children;
2240 GtkContainer *container;
2241 GtkContainerPrivate *priv;
2243 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2245 container = GTK_CONTAINER (widget);
2246 priv = container->priv;
2250 if (gtk_widget_get_can_focus (widget))
2252 if (!gtk_widget_has_focus (widget))
2254 gtk_widget_grab_focus (widget);
2260 /* Get a list of the containers children, allowing focus
2261 * chain to override.
2263 if (priv->has_focus_chain)
2264 children = g_list_copy (get_focus_chain (container));
2266 children = gtk_container_get_all_children (container);
2268 if (priv->has_focus_chain &&
2269 (direction == GTK_DIR_TAB_FORWARD ||
2270 direction == GTK_DIR_TAB_BACKWARD))
2272 sorted_children = g_list_copy (children);
2274 if (direction == GTK_DIR_TAB_BACKWARD)
2275 sorted_children = g_list_reverse (sorted_children);
2278 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2280 return_val = gtk_container_focus_move (container, sorted_children, direction);
2282 g_list_free (sorted_children);
2283 g_list_free (children);
2290 tab_compare (gconstpointer a,
2294 GtkAllocation child1_allocation, child2_allocation;
2295 const GtkWidget *child1 = a;
2296 const GtkWidget *child2 = b;
2297 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2300 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2301 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2303 y1 = child1_allocation.y + child1_allocation.height / 2;
2304 y2 = child2_allocation.y + child2_allocation.height / 2;
2308 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2309 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2311 if (text_direction == GTK_TEXT_DIR_RTL)
2312 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2314 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2317 return (y1 < y2) ? -1 : 1;
2321 gtk_container_focus_sort_tab (GtkContainer *container,
2323 GtkDirectionType direction,
2324 GtkWidget *old_focus)
2326 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2327 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2329 /* if we are going backwards then reverse the order
2332 if (direction == GTK_DIR_TAB_BACKWARD)
2333 children = g_list_reverse (children);
2338 /* Get coordinates of @widget's allocation with respect to
2339 * allocation of @container.
2342 get_allocation_coords (GtkContainer *container,
2344 GdkRectangle *allocation)
2346 gtk_widget_get_allocation (widget, allocation);
2348 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2349 0, 0, &allocation->x, &allocation->y);
2352 /* Look for a child in @children that is intermediate between
2353 * the focus widget and container. This widget, if it exists,
2354 * acts as the starting widget for focus navigation.
2357 find_old_focus (GtkContainer *container,
2360 GList *tmp_list = children;
2363 GtkWidget *child = tmp_list->data;
2364 GtkWidget *widget = child;
2366 while (widget && widget != (GtkWidget *)container)
2370 parent = gtk_widget_get_parent (widget);
2372 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2381 tmp_list = tmp_list->next;
2388 old_focus_coords (GtkContainer *container,
2389 GdkRectangle *old_focus_rect)
2391 GtkWidget *widget = GTK_WIDGET (container);
2392 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2393 GtkWidget *old_focus;
2395 if (GTK_IS_WINDOW (toplevel))
2397 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2399 return get_allocation_coords (container, old_focus, old_focus_rect);
2405 typedef struct _CompareInfo CompareInfo;
2409 GtkContainer *container;
2416 up_down_compare (gconstpointer a,
2420 GdkRectangle allocation1;
2421 GdkRectangle allocation2;
2422 CompareInfo *compare = data;
2425 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2426 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2428 y1 = allocation1.y + allocation1.height / 2;
2429 y2 = allocation2.y + allocation2.height / 2;
2433 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2434 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2436 if (compare->reverse)
2437 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2439 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2442 return (y1 < y2) ? -1 : 1;
2446 gtk_container_focus_sort_up_down (GtkContainer *container,
2448 GtkDirectionType direction,
2449 GtkWidget *old_focus)
2451 CompareInfo compare;
2453 GdkRectangle old_allocation;
2455 compare.container = container;
2456 compare.reverse = (direction == GTK_DIR_UP);
2459 old_focus = find_old_focus (container, children);
2461 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2467 /* Delete widgets from list that don't match minimum criteria */
2469 compare_x1 = old_allocation.x;
2470 compare_x2 = old_allocation.x + old_allocation.width;
2472 if (direction == GTK_DIR_UP)
2473 compare_y = old_allocation.y;
2475 compare_y = old_allocation.y + old_allocation.height;
2477 tmp_list = children;
2480 GtkWidget *child = tmp_list->data;
2481 GList *next = tmp_list->next;
2482 gint child_x1, child_x2;
2483 GdkRectangle child_allocation;
2485 if (child != old_focus)
2487 if (get_allocation_coords (container, child, &child_allocation))
2489 child_x1 = child_allocation.x;
2490 child_x2 = child_allocation.x + child_allocation.width;
2492 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2493 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2494 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2496 children = g_list_delete_link (children, tmp_list);
2500 children = g_list_delete_link (children, tmp_list);
2506 compare.x = (compare_x1 + compare_x2) / 2;
2507 compare.y = old_allocation.y + old_allocation.height / 2;
2511 /* No old focus widget, need to figure out starting x,y some other way
2513 GtkAllocation allocation;
2514 GtkWidget *widget = GTK_WIDGET (container);
2515 GdkRectangle old_focus_rect;
2517 gtk_widget_get_allocation (widget, &allocation);
2519 if (old_focus_coords (container, &old_focus_rect))
2521 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2525 if (!gtk_widget_get_has_window (widget))
2526 compare.x = allocation.x + allocation.width / 2;
2528 compare.x = allocation.width / 2;
2531 if (!gtk_widget_get_has_window (widget))
2532 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2534 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2537 children = g_list_sort_with_data (children, up_down_compare, &compare);
2539 if (compare.reverse)
2540 children = g_list_reverse (children);
2546 left_right_compare (gconstpointer a,
2550 GdkRectangle allocation1;
2551 GdkRectangle allocation2;
2552 CompareInfo *compare = data;
2555 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2556 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2558 x1 = allocation1.x + allocation1.width / 2;
2559 x2 = allocation2.x + allocation2.width / 2;
2563 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2564 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2566 if (compare->reverse)
2567 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2569 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2572 return (x1 < x2) ? -1 : 1;
2576 gtk_container_focus_sort_left_right (GtkContainer *container,
2578 GtkDirectionType direction,
2579 GtkWidget *old_focus)
2581 CompareInfo compare;
2583 GdkRectangle old_allocation;
2585 compare.container = container;
2586 compare.reverse = (direction == GTK_DIR_LEFT);
2589 old_focus = find_old_focus (container, children);
2591 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2597 /* Delete widgets from list that don't match minimum criteria */
2599 compare_y1 = old_allocation.y;
2600 compare_y2 = old_allocation.y + old_allocation.height;
2602 if (direction == GTK_DIR_LEFT)
2603 compare_x = old_allocation.x;
2605 compare_x = old_allocation.x + old_allocation.width;
2607 tmp_list = children;
2610 GtkWidget *child = tmp_list->data;
2611 GList *next = tmp_list->next;
2612 gint child_y1, child_y2;
2613 GdkRectangle child_allocation;
2615 if (child != old_focus)
2617 if (get_allocation_coords (container, child, &child_allocation))
2619 child_y1 = child_allocation.y;
2620 child_y2 = child_allocation.y + child_allocation.height;
2622 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2623 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2624 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2626 children = g_list_delete_link (children, tmp_list);
2630 children = g_list_delete_link (children, tmp_list);
2636 compare.y = (compare_y1 + compare_y2) / 2;
2637 compare.x = old_allocation.x + old_allocation.width / 2;
2641 /* No old focus widget, need to figure out starting x,y some other way
2643 GtkAllocation allocation;
2644 GtkWidget *widget = GTK_WIDGET (container);
2645 GdkRectangle old_focus_rect;
2647 gtk_widget_get_allocation (widget, &allocation);
2649 if (old_focus_coords (container, &old_focus_rect))
2651 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2655 if (!gtk_widget_get_has_window (widget))
2656 compare.y = allocation.y + allocation.height / 2;
2658 compare.y = allocation.height / 2;
2661 if (!gtk_widget_get_has_window (widget))
2662 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2664 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2667 children = g_list_sort_with_data (children, left_right_compare, &compare);
2669 if (compare.reverse)
2670 children = g_list_reverse (children);
2676 * gtk_container_focus_sort:
2677 * @container: a #GtkContainer
2678 * @children: a list of descendents of @container (they don't
2679 * have to be direct children)
2680 * @direction: focus direction
2681 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2682 * to determine this automatically.
2683 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2684 * which is the only @direction we use currently,
2685 * so perhaps this argument should be removed)
2687 * Sorts @children in the correct order for focusing with
2688 * direction type @direction.
2690 * Return value: a copy of @children, sorted in correct focusing order,
2691 * with children that aren't suitable for focusing in this direction
2695 _gtk_container_focus_sort (GtkContainer *container,
2697 GtkDirectionType direction,
2698 GtkWidget *old_focus)
2700 GList *visible_children = NULL;
2704 if (gtk_widget_get_realized (children->data))
2705 visible_children = g_list_prepend (visible_children, children->data);
2706 children = children->next;
2711 case GTK_DIR_TAB_FORWARD:
2712 case GTK_DIR_TAB_BACKWARD:
2713 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2716 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2719 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2722 g_assert_not_reached ();
2728 gtk_container_focus_move (GtkContainer *container,
2730 GtkDirectionType direction)
2732 GtkContainerPrivate *priv = container->priv;
2733 GtkWidget *focus_child;
2736 focus_child = priv->focus_child;
2740 child = children->data;
2741 children = children->next;
2748 if (focus_child == child)
2752 if (gtk_widget_child_focus (child, direction))
2756 else if (gtk_widget_is_drawable (child) &&
2757 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2759 if (gtk_widget_child_focus (child, direction))
2769 gtk_container_children_callback (GtkWidget *widget,
2770 gpointer client_data)
2774 children = (GList**) client_data;
2775 *children = g_list_prepend (*children, widget);
2779 chain_widget_destroyed (GtkWidget *widget,
2782 GtkContainer *container;
2785 container = GTK_CONTAINER (user_data);
2787 chain = g_object_get_data (G_OBJECT (container),
2788 "gtk-container-focus-chain");
2790 chain = g_list_remove (chain, widget);
2792 g_signal_handlers_disconnect_by_func (widget,
2793 chain_widget_destroyed,
2796 g_object_set_data (G_OBJECT (container),
2797 I_("gtk-container-focus-chain"),
2802 * gtk_container_set_focus_chain:
2803 * @container: a #GtkContainer
2804 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2805 * the new focus chain
2807 * Sets a focus chain, overriding the one computed automatically by GTK+.
2809 * In principle each widget in the chain should be a descendant of the
2810 * container, but this is not enforced by this method, since it's allowed
2811 * to set the focus chain before you pack the widgets, or have a widget
2812 * in the chain that isn't always packed. The necessary checks are done
2813 * when the focus chain is actually traversed.
2816 gtk_container_set_focus_chain (GtkContainer *container,
2817 GList *focusable_widgets)
2821 GtkContainerPrivate *priv;
2823 g_return_if_fail (GTK_IS_CONTAINER (container));
2825 priv = container->priv;
2827 if (priv->has_focus_chain)
2828 gtk_container_unset_focus_chain (container);
2830 priv->has_focus_chain = TRUE;
2833 tmp_list = focusable_widgets;
2834 while (tmp_list != NULL)
2836 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2838 /* In principle each widget in the chain should be a descendant
2839 * of the container, but we don't want to check that here, it's
2840 * expensive and also it's allowed to set the focus chain before
2841 * you pack the widgets, or have a widget in the chain that isn't
2842 * always packed. So we check for ancestor during actual traversal.
2845 chain = g_list_prepend (chain, tmp_list->data);
2847 g_signal_connect (tmp_list->data,
2849 G_CALLBACK (chain_widget_destroyed),
2852 tmp_list = g_list_next (tmp_list);
2855 chain = g_list_reverse (chain);
2857 g_object_set_data (G_OBJECT (container),
2858 I_("gtk-container-focus-chain"),
2863 * gtk_container_get_focus_chain:
2864 * @container: a #GtkContainer
2865 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2866 * to store the focus chain of the
2867 * container, or %NULL. You should free this list
2868 * using g_list_free() when you are done with it, however
2869 * no additional reference count is added to the
2870 * individual widgets in the focus chain.
2872 * Retrieves the focus chain of the container, if one has been
2873 * set explicitly. If no focus chain has been explicitly
2874 * set, GTK+ computes the focus chain based on the positions
2875 * of the children. In that case, GTK+ stores %NULL in
2876 * @focusable_widgets and returns %FALSE.
2878 * Return value: %TRUE if the focus chain of the container
2879 * has been set explicitly.
2882 gtk_container_get_focus_chain (GtkContainer *container,
2883 GList **focus_chain)
2885 GtkContainerPrivate *priv;
2887 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2889 priv = container->priv;
2893 if (priv->has_focus_chain)
2894 *focus_chain = g_list_copy (get_focus_chain (container));
2896 *focus_chain = NULL;
2899 return priv->has_focus_chain;
2903 * gtk_container_unset_focus_chain:
2904 * @container: a #GtkContainer
2906 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2909 gtk_container_unset_focus_chain (GtkContainer *container)
2911 GtkContainerPrivate *priv;
2913 g_return_if_fail (GTK_IS_CONTAINER (container));
2915 priv = container->priv;
2917 if (priv->has_focus_chain)
2922 chain = get_focus_chain (container);
2924 priv->has_focus_chain = FALSE;
2926 g_object_set_data (G_OBJECT (container),
2927 I_("gtk-container-focus-chain"),
2931 while (tmp_list != NULL)
2933 g_signal_handlers_disconnect_by_func (tmp_list->data,
2934 chain_widget_destroyed,
2937 tmp_list = g_list_next (tmp_list);
2940 g_list_free (chain);
2945 * gtk_container_set_focus_vadjustment:
2946 * @container: a #GtkContainer
2947 * @adjustment: an adjustment which should be adjusted when the focus
2948 * is moved among the descendents of @container
2950 * Hooks up an adjustment to focus handling in a container, so when a
2951 * child of the container is focused, the adjustment is scrolled to
2952 * show that widget. This function sets the vertical alignment. See
2953 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2954 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2955 * the horizontal adjustment.
2957 * The adjustments have to be in pixel units and in the same coordinate
2958 * system as the allocation for immediate children of the container.
2961 gtk_container_set_focus_vadjustment (GtkContainer *container,
2962 GtkAdjustment *adjustment)
2964 g_return_if_fail (GTK_IS_CONTAINER (container));
2966 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2969 g_object_ref (adjustment);
2971 g_object_set_qdata_full (G_OBJECT (container),
2978 * gtk_container_get_focus_vadjustment:
2979 * @container: a #GtkContainer
2981 * Retrieves the vertical focus adjustment for the container. See
2982 * gtk_container_set_focus_vadjustment().
2984 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2985 * none has been set.
2988 gtk_container_get_focus_vadjustment (GtkContainer *container)
2990 GtkAdjustment *vadjustment;
2992 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2994 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
3000 * gtk_container_set_focus_hadjustment:
3001 * @container: a #GtkContainer
3002 * @adjustment: an adjustment which should be adjusted when the focus is
3003 * moved among the descendents of @container
3005 * Hooks up an adjustment to focus handling in a container, so when a child
3006 * of the container is focused, the adjustment is scrolled to show that
3007 * widget. This function sets the horizontal alignment.
3008 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
3009 * the adjustment and gtk_container_set_focus_vadjustment() for setting
3010 * the vertical adjustment.
3012 * The adjustments have to be in pixel units and in the same coordinate
3013 * system as the allocation for immediate children of the container.
3016 gtk_container_set_focus_hadjustment (GtkContainer *container,
3017 GtkAdjustment *adjustment)
3019 g_return_if_fail (GTK_IS_CONTAINER (container));
3021 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3024 g_object_ref (adjustment);
3026 g_object_set_qdata_full (G_OBJECT (container),
3033 * gtk_container_get_focus_hadjustment:
3034 * @container: a #GtkContainer
3036 * Retrieves the horizontal focus adjustment for the container. See
3037 * gtk_container_set_focus_hadjustment ().
3039 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3040 * none has been set.
3043 gtk_container_get_focus_hadjustment (GtkContainer *container)
3045 GtkAdjustment *hadjustment;
3047 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3049 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3056 gtk_container_show_all (GtkWidget *widget)
3058 g_return_if_fail (GTK_IS_CONTAINER (widget));
3060 gtk_container_foreach (GTK_CONTAINER (widget),
3061 (GtkCallback) gtk_widget_show_all,
3063 gtk_widget_show (widget);
3067 gtk_container_draw_child (GtkWidget *child,
3068 gpointer client_data)
3071 GtkWidget *container;
3073 } *data = client_data;
3075 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3081 gtk_container_draw (GtkWidget *widget,
3085 GtkWidget *container;
3089 data.container = widget;
3092 gtk_container_forall (GTK_CONTAINER (widget),
3093 gtk_container_draw_child,
3100 gtk_container_map_child (GtkWidget *child,
3101 gpointer client_data)
3103 if (gtk_widget_get_visible (child) &&
3104 gtk_widget_get_child_visible (child) &&
3105 !gtk_widget_get_mapped (child))
3106 gtk_widget_map (child);
3110 gtk_container_map (GtkWidget *widget)
3112 gtk_widget_set_mapped (widget, TRUE);
3114 gtk_container_forall (GTK_CONTAINER (widget),
3115 gtk_container_map_child,
3118 if (gtk_widget_get_has_window (widget))
3119 gdk_window_show (gtk_widget_get_window (widget));
3123 gtk_container_unmap (GtkWidget *widget)
3125 gtk_widget_set_mapped (widget, FALSE);
3127 /* hide our window first so user doesn't see all the child windows
3128 * vanishing one by one. (only matters these days if one of the
3129 * children has an actual native window instead of client-side
3130 * window, e.g. a GtkSocket would)
3132 if (gtk_widget_get_has_window (widget))
3133 gdk_window_hide (gtk_widget_get_window (widget));
3135 gtk_container_forall (GTK_CONTAINER (widget),
3136 (GtkCallback)gtk_widget_unmap,
3141 * gtk_container_propagate_draw:
3142 * @container: a #GtkContainer
3143 * @child: a child of @container
3144 * @cr: Cairo context as passed to the container. If you want to use @cr
3145 * in container's draw function, consider using cairo_save() and
3146 * cairo_restore() before calling this function.
3148 * When a container receives a call to the draw function, it must send
3149 * synthetic #GtkWidget::draw calls to all children that don't have their
3150 * own #GdkWindows. This function provides a convenient way of doing this.
3151 * A container, when it receives a call to its #GtkWidget::draw function,
3152 * calls gtk_container_propagate_draw() once for each child, passing in
3153 * the @cr the container received.
3155 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3156 * and deciding whether the draw needs to be sent to the child. It is a
3157 * convenient and optimized way of getting the same effect as calling
3158 * gtk_widget_draw() on the child directly.
3160 * In most cases, a container can simply either inherit the
3161 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3162 * and then chain to the ::draw implementation from #GtkContainer.
3165 gtk_container_propagate_draw (GtkContainer *container,
3169 GdkEventExpose *event;
3170 GtkAllocation allocation;
3171 GdkWindow *window, *w;
3174 g_return_if_fail (GTK_IS_CONTAINER (container));
3175 g_return_if_fail (GTK_IS_WIDGET (child));
3176 g_return_if_fail (cr != NULL);
3178 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3180 event = _gtk_cairo_get_event (cr);
3183 if (gtk_widget_get_has_window (child) ||
3184 gtk_widget_get_window (child) != event->window)
3190 /* translate coordinates. Ugly business, that. */
3191 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3193 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3203 window = gtk_widget_get_window (GTK_WIDGET (container));
3205 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3208 gdk_window_get_position (w, &wx, &wy);
3219 if (!gtk_widget_get_has_window (child))
3221 gtk_widget_get_allocation (child, &allocation);
3226 cairo_translate (cr, x, y);
3228 _gtk_widget_draw_internal (child, cr, TRUE);
3234 _gtk_container_get_need_resize (GtkContainer *container)
3236 return container->priv->need_resize;
3240 _gtk_container_set_need_resize (GtkContainer *container,
3241 gboolean need_resize)
3243 container->priv->need_resize = need_resize;
3247 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3249 return container->priv->reallocate_redraws;
3253 * gtk_container_get_path_for_child:
3254 * @container: a #GtkContainer
3255 * @child: a child of @container
3257 * Returns a newly created widget path representing all the widget hierarchy
3258 * from the toplevel down to @child (this one not being included).
3260 * Returns: A newly created #GtkWidgetPath
3263 gtk_container_get_path_for_child (GtkContainer *container,
3266 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3267 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3268 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3270 return GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);