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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkcontainer.h"
28 #include "gtkcontainerprivate.h"
34 #include <gobject/gobjectnotifyqueue.c>
35 #include <gobject/gvaluecollector.h>
37 #include "gtkadjustment.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"
47 #include "gtkassistant.h"
49 #include "gtkstylecontextprivate.h"
50 #include "gtkwidgetpath.h"
51 #include "a11y/gtkcontaineraccessible.h"
54 * SECTION:gtkcontainer
55 * @Short_description: Base class for widgets which contain other widgets
56 * @Title: GtkContainer
58 * A GTK+ user interface is constructed by nesting widgets inside widgets.
59 * Container widgets are the inner nodes in the resulting tree of widgets:
60 * they contain other widgets. So, for example, you might have a #GtkWindow
61 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
62 * of a textual label inside the frame, you might replace the #GtkLabel widget
63 * with a #GtkImage widget.
65 * There are two major kinds of container widgets in GTK+. Both are subclasses
66 * of the abstract GtkContainer base class.
68 * The first type of container widget has a single child widget and derives
69 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
70 * add some kind of functionality to the child. For example, a #GtkButton makes
71 * its child into a clickable button; a #GtkFrame draws a frame around its child
72 * and a #GtkWindow places its child widget inside a top-level window.
74 * The second type of container can have more than one child; its purpose is to
75 * manage <emphasis>layout</emphasis>. This means that these containers assign
76 * sizes and positions to their children. For example, a #GtkHBox arranges its
77 * children in a horizontal row, and a #GtkGrid arranges the widgets it contains
78 * in a two-dimensional grid.
80 * <refsect2 id="container-geometry-management">
81 * <title>Height for width geometry management</title>
83 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
84 * Height-for-width means that a widget can change how much vertical space it needs,
85 * depending on the amount of horizontal space that it is given (and similar for
88 * There are some things to keep in mind when implementing container widgets
89 * that make use of GTK+'s height for width geometry management system. First,
90 * it's important to note that a container must prioritize one of its
91 * dimensions, that is to say that a widget or container can only have a
92 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
93 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
94 * must be able to respond to the APIs for both dimensions, i.e. even if a
95 * widget has a request mode that is height-for-width, it is possible that
96 * its parent will request its sizes using the width-for-height APIs.
98 * To ensure that everything works properly, here are some guidelines to follow
99 * when implementing height-for-width (or width-for-height) containers.
101 * Each request mode involves 2 virtual methods. Height-for-width apis run
102 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
103 * When handling requests in the opposite #GtkSizeRequestMode it is important that
104 * every widget request at least enough space to display all of its content at all times.
106 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
107 * the container must return the height for its minimum width. This is easily achieved by
108 * simply calling the reverse apis implemented for itself as follows:
110 * <programlisting><![CDATA[
112 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
114 * if (i_am_in_height_for_width_mode)
118 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
119 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
120 * min_height, nat_height);
124 * ... many containers support both request modes, execute the real width-for-height
125 * request here by returning the collective heights of all widgets that are
126 * stacked vertically (or whatever is appropriate for this container) ...
129 * ]]></programlisting>
131 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
132 * that is height-for-width, it then only needs to return the base minimum width like so:
134 * <programlisting><![CDATA[
136 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
137 * gint *min_width, gint *nat_width)
139 * if (i_am_in_height_for_width_mode)
141 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
145 * ... execute the real width-for-height request here based on the required width
146 * of the children collectively if the container were to be allocated the said height ...
149 * ]]></programlisting>
151 * Height for width requests are generally implemented in terms of a virtual allocation
152 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
153 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
154 * gtk_widget_get_preferred_width() for each of its children.
156 * For each potential group of children that are lined up horizontally, the values returned by
157 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures.
158 * Any child spacing should be removed from the input @for_width and then the collective size should be
159 * allocated using the gtk_distribute_natural_allocation() convenience function.
161 * The container will then move on to request the preferred height for each child by using
162 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
164 * To allocate a height-for-width container, it's again important
165 * to consider that a container must prioritize one dimension over the other. So if
166 * a container is a height-for-width container it must first allocate all widgets horizontally
167 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
168 * extra space (if and where appropriate) for the widget to expand.
170 * After adding all the expand space, the container assumes it was allocated sufficient
171 * height to fit all of its content. At this time, the container must use the total horizontal sizes
172 * of each widget to request the height-for-width of each of its children and store the requests in a
173 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
174 * be generalized into the heights and widths of rows and columns).
175 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
176 * while this time considering the allocated height of the widget minus any vertical spacing
177 * that the container adds. Then vertical expand space should be added where appropriate and available
178 * and the container should go on to actually allocating the child widgets.
180 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
181 * to learn more about implementing height-for-width geometry management for widgets.
184 * <refsect2 id="child-properties">
185 * <title>Child properties</title>
187 * GtkContainer introduces <emphasis>child properties</emphasis>.
188 * These are object properties that are not specific
189 * to either the container or the contained widget, but rather to their relation.
190 * Typical examples of child properties are the position or pack-type of a widget
191 * which is contained in a #GtkBox.
193 * Use gtk_container_class_install_child_property() to install child properties
194 * for a container class and gtk_container_class_find_child_property() or
195 * gtk_container_class_list_child_properties() to get information about existing
198 * To set the value of a child property, use gtk_container_child_set_property(),
199 * gtk_container_child_set() or gtk_container_child_set_valist().
200 * To obtain the value of a child property, use
201 * gtk_container_child_get_property(), gtk_container_child_get() or
202 * gtk_container_child_get_valist(). To emit notification about child property
203 * changes, use gtk_widget_child_notify().
206 * <refsect2 id="GtkContainer-BUILDER-UI">
207 * <title>GtkContainer as GtkBuildable</title>
209 * The GtkContainer implementation of the GtkBuildable interface
210 * supports a <packing> element for children, which can
211 * contain multiple <property> elements that specify
212 * child properties for the child.
214 * <title>Child properties in UI definitions</title>
215 * <programlisting><![CDATA[
216 * <object class="GtkVBox">
218 * <object class="GtkLabel"/>
220 * <property name="pack-type">start</property>
224 * ]]></programlisting>
226 * Since 2.16, child properties can also be marked as translatable using
227 * the same "translatable", "comments" and "context" attributes that are used
228 * for regular properties.
234 struct _GtkContainerPrivate
236 GtkWidget *focus_child;
238 guint border_width : 16;
240 guint has_focus_chain : 1;
241 guint need_resize : 1;
242 guint reallocate_redraws : 1;
243 guint resize_mode : 2;
244 guint request_mode : 2;
262 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
263 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
266 /* --- prototypes --- */
267 static void gtk_container_base_class_init (GtkContainerClass *klass);
268 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
269 static void gtk_container_class_init (GtkContainerClass *klass);
270 static void gtk_container_init (GtkContainer *container);
271 static void gtk_container_destroy (GtkWidget *widget);
272 static void gtk_container_set_property (GObject *object,
276 static void gtk_container_get_property (GObject *object,
280 static void gtk_container_add_unimplemented (GtkContainer *container,
282 static void gtk_container_remove_unimplemented (GtkContainer *container,
284 static void gtk_container_real_check_resize (GtkContainer *container);
285 static void gtk_container_compute_expand (GtkWidget *widget,
287 gboolean *vexpand_p);
288 static gboolean gtk_container_focus (GtkWidget *widget,
289 GtkDirectionType direction);
290 static void gtk_container_real_set_focus_child (GtkContainer *container,
293 static gboolean gtk_container_focus_move (GtkContainer *container,
295 GtkDirectionType direction);
296 static void gtk_container_children_callback (GtkWidget *widget,
297 gpointer client_data);
298 static void gtk_container_show_all (GtkWidget *widget);
299 static gint gtk_container_draw (GtkWidget *widget,
301 static void gtk_container_map (GtkWidget *widget);
302 static void gtk_container_unmap (GtkWidget *widget);
303 static void gtk_container_adjust_size_request (GtkWidget *widget,
304 GtkOrientation orientation,
307 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
308 GtkOrientation orientation,
312 gint *allocated_size);
313 static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget *widget);
315 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
318 static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
322 static void gtk_container_buildable_init (GtkBuildableIface *iface);
323 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
327 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
330 const gchar *tagname,
331 GMarkupParser *parser,
333 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
336 const gchar *tagname,
340 /* --- variables --- */
341 static const gchar vadjustment_key[] = "gtk-vadjustment";
342 static guint vadjustment_key_id = 0;
343 static const gchar hadjustment_key[] = "gtk-hadjustment";
344 static guint hadjustment_key_id = 0;
345 static GSList *container_resize_queue = NULL;
346 static guint container_signals[LAST_SIGNAL] = { 0 };
347 static GtkWidgetClass *parent_class = NULL;
348 extern GParamSpecPool *_gtk_widget_child_property_pool;
349 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
350 static GtkBuildableIface *parent_buildable_iface;
353 /* --- functions --- */
355 gtk_container_get_type (void)
357 static GType container_type = 0;
361 const GTypeInfo container_info =
363 sizeof (GtkContainerClass),
364 (GBaseInitFunc) gtk_container_base_class_init,
365 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
366 (GClassInitFunc) gtk_container_class_init,
367 NULL /* class_finalize */,
368 NULL /* class_data */,
369 sizeof (GtkContainer),
371 (GInstanceInitFunc) gtk_container_init,
372 NULL, /* value_table */
375 const GInterfaceInfo buildable_info =
377 (GInterfaceInitFunc) gtk_container_buildable_init,
383 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
384 &container_info, G_TYPE_FLAG_ABSTRACT);
386 g_type_add_interface_static (container_type,
392 return container_type;
396 gtk_container_base_class_init (GtkContainerClass *class)
398 /* reset instance specifc class fields that don't get inherited */
399 class->set_child_property = NULL;
400 class->get_child_property = NULL;
404 gtk_container_base_class_finalize (GtkContainerClass *class)
408 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
409 for (node = list; node; node = node->next)
411 GParamSpec *pspec = node->data;
413 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
414 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
415 g_param_spec_unref (pspec);
421 gtk_container_class_init (GtkContainerClass *class)
423 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
424 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
426 parent_class = g_type_class_peek_parent (class);
428 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
429 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
431 gobject_class->set_property = gtk_container_set_property;
432 gobject_class->get_property = gtk_container_get_property;
434 widget_class->destroy = gtk_container_destroy;
435 widget_class->compute_expand = gtk_container_compute_expand;
436 widget_class->show_all = gtk_container_show_all;
437 widget_class->draw = gtk_container_draw;
438 widget_class->map = gtk_container_map;
439 widget_class->unmap = gtk_container_unmap;
440 widget_class->focus = gtk_container_focus;
442 widget_class->adjust_size_request = gtk_container_adjust_size_request;
443 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
444 widget_class->get_request_mode = gtk_container_get_request_mode;
446 class->add = gtk_container_add_unimplemented;
447 class->remove = gtk_container_remove_unimplemented;
448 class->check_resize = gtk_container_real_check_resize;
449 class->forall = NULL;
450 class->set_focus_child = gtk_container_real_set_focus_child;
451 class->child_type = NULL;
452 class->composite_name = gtk_container_child_default_composite_name;
453 class->get_path_for_child = gtk_container_real_get_path_for_child;
455 g_object_class_install_property (gobject_class,
457 g_param_spec_enum ("resize-mode",
459 P_("Specify how resize events are handled"),
460 GTK_TYPE_RESIZE_MODE,
462 GTK_PARAM_READWRITE));
463 g_object_class_install_property (gobject_class,
465 g_param_spec_uint ("border-width",
467 P_("The width of the empty border outside the containers children"),
471 GTK_PARAM_READWRITE));
472 g_object_class_install_property (gobject_class,
474 g_param_spec_object ("child",
476 P_("Can be used to add a new child to the container"),
478 GTK_PARAM_WRITABLE));
479 container_signals[ADD] =
480 g_signal_new (I_("add"),
481 G_OBJECT_CLASS_TYPE (gobject_class),
483 G_STRUCT_OFFSET (GtkContainerClass, add),
485 _gtk_marshal_VOID__OBJECT,
488 container_signals[REMOVE] =
489 g_signal_new (I_("remove"),
490 G_OBJECT_CLASS_TYPE (gobject_class),
492 G_STRUCT_OFFSET (GtkContainerClass, remove),
494 _gtk_marshal_VOID__OBJECT,
497 container_signals[CHECK_RESIZE] =
498 g_signal_new (I_("check-resize"),
499 G_OBJECT_CLASS_TYPE (gobject_class),
501 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
503 _gtk_marshal_VOID__VOID,
505 container_signals[SET_FOCUS_CHILD] =
506 g_signal_new (I_("set-focus-child"),
507 G_OBJECT_CLASS_TYPE (gobject_class),
509 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
511 _gtk_marshal_VOID__OBJECT,
515 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
517 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE);
521 gtk_container_buildable_init (GtkBuildableIface *iface)
523 parent_buildable_iface = g_type_interface_peek_parent (iface);
524 iface->add_child = gtk_container_buildable_add_child;
525 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
526 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
530 gtk_container_buildable_add_child (GtkBuildable *buildable,
537 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
539 else if (GTK_IS_WIDGET (child) &&
540 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
542 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
545 g_warning ("Cannot add an object of type %s to a container of type %s",
546 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
550 gtk_container_buildable_set_child_property (GtkContainer *container,
557 GValue gvalue = G_VALUE_INIT;
558 GError *error = NULL;
560 pspec = gtk_container_class_find_child_property
561 (G_OBJECT_GET_CLASS (container), name);
564 g_warning ("%s does not have a property called %s",
565 g_type_name (G_OBJECT_TYPE (container)), name);
569 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
571 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
572 g_type_name (G_OBJECT_TYPE (container)),
575 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
577 g_error_free (error);
581 gtk_container_child_set_property (container, child, name, &gvalue);
582 g_value_unset (&gvalue);
587 GtkContainer *container;
590 gchar *child_prop_name;
592 gboolean translatable;
593 } PackingPropertiesData;
596 attributes_start_element (GMarkupParseContext *context,
597 const gchar *element_name,
599 const gchar **values,
603 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
606 if (strcmp (element_name, "property") == 0)
608 for (i = 0; names[i]; i++)
609 if (strcmp (names[i], "name") == 0)
610 parser_data->child_prop_name = g_strdup (values[i]);
611 else if (strcmp (names[i], "translatable") == 0)
613 if (!_gtk_builder_boolean_from_string (values[1],
614 &parser_data->translatable,
618 else if (strcmp (names[i], "comments") == 0)
619 ; /* for translators */
620 else if (strcmp (names[i], "context") == 0)
621 parser_data->context = g_strdup (values[1]);
623 g_warning ("Unsupported attribute for GtkContainer Child "
624 "property: %s\n", names[i]);
626 else if (strcmp (element_name, "packing") == 0)
629 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
633 attributes_text_element (GMarkupParseContext *context,
639 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
641 if (parser_data->child_prop_name)
642 g_string_append_len (parser_data->string, text, text_len);
646 attributes_end_element (GMarkupParseContext *context,
647 const gchar *element_name,
651 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
653 /* translate the string */
654 if (parser_data->string->len && parser_data->translatable)
659 domain = gtk_builder_get_translation_domain (parser_data->builder);
661 translated = _gtk_builder_parser_translate (domain,
662 parser_data->context,
663 parser_data->string->str);
664 g_string_set_size (parser_data->string, 0);
665 g_string_append (parser_data->string, translated);
668 if (parser_data->child_prop_name)
669 gtk_container_buildable_set_child_property (parser_data->container,
670 parser_data->builder,
672 parser_data->child_prop_name,
673 parser_data->string->str);
675 g_string_set_size (parser_data->string, 0);
676 g_free (parser_data->child_prop_name);
677 g_free (parser_data->context);
678 parser_data->child_prop_name = NULL;
679 parser_data->context = NULL;
680 parser_data->translatable = FALSE;
683 static const GMarkupParser attributes_parser =
685 attributes_start_element,
686 attributes_end_element,
687 attributes_text_element,
691 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
694 const gchar *tagname,
695 GMarkupParser *parser,
698 PackingPropertiesData *parser_data;
700 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
701 tagname, parser, data))
704 if (child && strcmp (tagname, "packing") == 0)
706 parser_data = g_slice_new0 (PackingPropertiesData);
707 parser_data->string = g_string_new ("");
708 parser_data->builder = builder;
709 parser_data->container = GTK_CONTAINER (buildable);
710 parser_data->child = GTK_WIDGET (child);
711 parser_data->child_prop_name = NULL;
713 *parser = attributes_parser;
722 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
725 const gchar *tagname,
728 if (strcmp (tagname, "packing") == 0)
730 PackingPropertiesData *parser_data = (PackingPropertiesData*)data;
731 g_string_free (parser_data->string, TRUE);
732 g_slice_free (PackingPropertiesData, parser_data);
736 if (parent_buildable_iface->custom_tag_end)
737 parent_buildable_iface->custom_tag_end (buildable, builder,
738 child, tagname, data);
742 * gtk_container_child_type:
743 * @container: a #GtkContainer
745 * Returns the type of the children supported by the container.
747 * Note that this may return %G_TYPE_NONE to indicate that no more
748 * children can be added, e.g. for a #GtkPaned which already has two
751 * Return value: a #GType.
754 gtk_container_child_type (GtkContainer *container)
757 GtkContainerClass *class;
759 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
761 class = GTK_CONTAINER_GET_CLASS (container);
762 if (class->child_type)
763 slot = class->child_type (container);
770 /* --- GtkContainer child property mechanism --- */
773 * gtk_container_child_notify:
774 * @container: the #GtkContainer
775 * @child: the child widget
776 * @child_property: the name of a child property installed on
777 * the class of @container
779 * Emits a #GtkWidget::child-notify signal for the
780 * <link linkend="child-properties">child property</link>
781 * @child_property on widget.
783 * This is an analogue of g_object_notify() for child properties.
785 * Also see gtk_widget_child_notify().
790 gtk_container_child_notify (GtkContainer *container,
792 const gchar *child_property)
797 g_return_if_fail (GTK_IS_CONTAINER (container));
798 g_return_if_fail (GTK_IS_WIDGET (child));
799 g_return_if_fail (child_property != NULL);
801 obj = G_OBJECT (child);
803 if (obj->ref_count == 0)
808 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
810 G_OBJECT_TYPE (container),
815 g_warning ("%s: container class `%s' has no child property named `%s'",
817 G_OBJECT_TYPE_NAME (container),
822 GObjectNotifyQueue *nqueue;
824 nqueue = g_object_notify_queue_freeze (obj, _gtk_widget_child_property_notify_context);
826 g_object_notify_queue_add (obj, nqueue, pspec);
827 g_object_notify_queue_thaw (obj, nqueue);
830 g_object_unref (obj);
834 container_get_child_property (GtkContainer *container,
839 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
841 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
845 container_set_child_property (GtkContainer *container,
849 GObjectNotifyQueue *nqueue)
851 GValue tmp_value = G_VALUE_INIT;
852 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
854 /* provide a copy to work from, convert (if necessary) and validate */
855 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
856 if (!g_value_transform (value, &tmp_value))
857 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
859 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
860 G_VALUE_TYPE_NAME (value));
861 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
863 gchar *contents = g_strdup_value_contents (value);
865 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
867 G_VALUE_TYPE_NAME (value),
869 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
874 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
875 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
877 g_value_unset (&tmp_value);
881 * gtk_container_child_get_valist:
882 * @container: a #GtkContainer
883 * @child: a widget which is a child of @container
884 * @first_property_name: the name of the first property to get
885 * @var_args: return location for the first property, followed
886 * optionally by more name/return location pairs, followed by %NULL
888 * Gets the values of one or more child properties for @child and @container.
891 gtk_container_child_get_valist (GtkContainer *container,
893 const gchar *first_property_name,
898 g_return_if_fail (GTK_IS_CONTAINER (container));
899 g_return_if_fail (GTK_IS_WIDGET (child));
901 g_object_ref (container);
902 g_object_ref (child);
904 name = first_property_name;
907 GValue value = G_VALUE_INIT;
911 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
913 G_OBJECT_TYPE (container),
917 g_warning ("%s: container class `%s' has no child property named `%s'",
919 G_OBJECT_TYPE_NAME (container),
923 if (!(pspec->flags & G_PARAM_READABLE))
925 g_warning ("%s: child property `%s' of container class `%s' is not readable",
928 G_OBJECT_TYPE_NAME (container));
931 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
932 container_get_child_property (container, child, pspec, &value);
933 G_VALUE_LCOPY (&value, var_args, 0, &error);
936 g_warning ("%s: %s", G_STRLOC, error);
938 g_value_unset (&value);
941 g_value_unset (&value);
942 name = va_arg (var_args, gchar*);
945 g_object_unref (child);
946 g_object_unref (container);
950 * gtk_container_child_get_property:
951 * @container: a #GtkContainer
952 * @child: a widget which is a child of @container
953 * @property_name: the name of the property to get
954 * @value: a location to return the value
956 * Gets the value of a child property for @child and @container.
959 gtk_container_child_get_property (GtkContainer *container,
961 const gchar *property_name,
966 g_return_if_fail (GTK_IS_CONTAINER (container));
967 g_return_if_fail (GTK_IS_WIDGET (child));
968 g_return_if_fail (property_name != NULL);
969 g_return_if_fail (G_IS_VALUE (value));
971 g_object_ref (container);
972 g_object_ref (child);
973 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
974 G_OBJECT_TYPE (container), TRUE);
976 g_warning ("%s: container class `%s' has no child property named `%s'",
978 G_OBJECT_TYPE_NAME (container),
980 else if (!(pspec->flags & G_PARAM_READABLE))
981 g_warning ("%s: child property `%s' of container class `%s' is not readable",
984 G_OBJECT_TYPE_NAME (container));
987 GValue *prop_value, tmp_value = G_VALUE_INIT;
989 /* auto-conversion of the callers value type
991 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
993 g_value_reset (value);
996 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
998 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
1000 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1001 G_VALUE_TYPE_NAME (value));
1002 g_object_unref (child);
1003 g_object_unref (container);
1008 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1009 prop_value = &tmp_value;
1011 container_get_child_property (container, child, pspec, prop_value);
1012 if (prop_value != value)
1014 g_value_transform (prop_value, value);
1015 g_value_unset (&tmp_value);
1018 g_object_unref (child);
1019 g_object_unref (container);
1023 * gtk_container_child_set_valist:
1024 * @container: a #GtkContainer
1025 * @child: a widget which is a child of @container
1026 * @first_property_name: the name of the first property to set
1027 * @var_args: a %NULL-terminated list of property names and values, starting
1028 * with @first_prop_name
1030 * Sets one or more child properties for @child and @container.
1033 gtk_container_child_set_valist (GtkContainer *container,
1035 const gchar *first_property_name,
1038 GObjectNotifyQueue *nqueue;
1041 g_return_if_fail (GTK_IS_CONTAINER (container));
1042 g_return_if_fail (GTK_IS_WIDGET (child));
1044 g_object_ref (container);
1045 g_object_ref (child);
1047 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1048 name = first_property_name;
1051 GValue value = G_VALUE_INIT;
1052 gchar *error = NULL;
1053 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1055 G_OBJECT_TYPE (container),
1059 g_warning ("%s: container class `%s' has no child property named `%s'",
1061 G_OBJECT_TYPE_NAME (container),
1065 if (!(pspec->flags & G_PARAM_WRITABLE))
1067 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1070 G_OBJECT_TYPE_NAME (container));
1074 G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
1075 var_args, 0, &error);
1078 g_warning ("%s: %s", G_STRLOC, error);
1081 /* we purposely leak the value here, it might not be
1082 * in a sane state if an error condition occoured
1086 container_set_child_property (container, child, pspec, &value, nqueue);
1087 g_value_unset (&value);
1088 name = va_arg (var_args, gchar*);
1090 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1092 g_object_unref (container);
1093 g_object_unref (child);
1097 * gtk_container_child_set_property:
1098 * @container: a #GtkContainer
1099 * @child: a widget which is a child of @container
1100 * @property_name: the name of the property to set
1101 * @value: the value to set the property to
1103 * Sets a child property for @child and @container.
1106 gtk_container_child_set_property (GtkContainer *container,
1108 const gchar *property_name,
1109 const GValue *value)
1111 GObjectNotifyQueue *nqueue;
1114 g_return_if_fail (GTK_IS_CONTAINER (container));
1115 g_return_if_fail (GTK_IS_WIDGET (child));
1116 g_return_if_fail (property_name != NULL);
1117 g_return_if_fail (G_IS_VALUE (value));
1119 g_object_ref (container);
1120 g_object_ref (child);
1122 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1123 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1124 G_OBJECT_TYPE (container), TRUE);
1126 g_warning ("%s: container class `%s' has no child property named `%s'",
1128 G_OBJECT_TYPE_NAME (container),
1130 else if (!(pspec->flags & G_PARAM_WRITABLE))
1131 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1134 G_OBJECT_TYPE_NAME (container));
1137 container_set_child_property (container, child, pspec, value, nqueue);
1139 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1140 g_object_unref (container);
1141 g_object_unref (child);
1145 * gtk_container_add_with_properties:
1146 * @container: a #GtkContainer
1147 * @widget: a widget to be placed inside @container
1148 * @first_prop_name: the name of the first child property to set
1149 * @...: a %NULL-terminated list of property names and values, starting
1150 * with @first_prop_name
1152 * Adds @widget to @container, setting child properties at the same time.
1153 * See gtk_container_add() and gtk_container_child_set() for more details.
1156 gtk_container_add_with_properties (GtkContainer *container,
1158 const gchar *first_prop_name,
1161 g_return_if_fail (GTK_IS_CONTAINER (container));
1162 g_return_if_fail (GTK_IS_WIDGET (widget));
1163 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1165 g_object_ref (container);
1166 g_object_ref (widget);
1167 gtk_widget_freeze_child_notify (widget);
1169 g_signal_emit (container, container_signals[ADD], 0, widget);
1170 if (gtk_widget_get_parent (widget))
1174 va_start (var_args, first_prop_name);
1175 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1179 gtk_widget_thaw_child_notify (widget);
1180 g_object_unref (widget);
1181 g_object_unref (container);
1185 * gtk_container_child_set:
1186 * @container: a #GtkContainer
1187 * @child: a widget which is a child of @container
1188 * @first_prop_name: the name of the first property to set
1189 * @...: a %NULL-terminated list of property names and values, starting
1190 * with @first_prop_name
1192 * Sets one or more child properties for @child and @container.
1195 gtk_container_child_set (GtkContainer *container,
1197 const gchar *first_prop_name,
1202 va_start (var_args, first_prop_name);
1203 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1208 * gtk_container_child_get:
1209 * @container: a #GtkContainer
1210 * @child: a widget which is a child of @container
1211 * @first_prop_name: the name of the first property to get
1212 * @...: return location for the first property, followed
1213 * optionally by more name/return location pairs, followed by %NULL
1215 * Gets the values of one or more child properties for @child and @container.
1218 gtk_container_child_get (GtkContainer *container,
1220 const gchar *first_prop_name,
1225 va_start (var_args, first_prop_name);
1226 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1231 * gtk_container_class_install_child_property:
1232 * @cclass: a #GtkContainerClass
1233 * @property_id: the id for the property
1234 * @pspec: the #GParamSpec for the property
1236 * Installs a child property on a container class.
1239 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1243 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1244 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1245 if (pspec->flags & G_PARAM_WRITABLE)
1246 g_return_if_fail (cclass->set_child_property != NULL);
1247 if (pspec->flags & G_PARAM_READABLE)
1248 g_return_if_fail (cclass->get_child_property != NULL);
1249 g_return_if_fail (property_id > 0);
1250 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1251 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1252 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1254 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1256 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1257 G_OBJECT_CLASS_NAME (cclass),
1261 g_param_spec_ref (pspec);
1262 g_param_spec_sink (pspec);
1263 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1264 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1268 * gtk_container_class_find_child_property:
1269 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1270 * @property_name: the name of the child property to find
1272 * Finds a child property of a container class by name.
1274 * Returns: (transfer none): the #GParamSpec of the child property
1275 * or %NULL if @class has no child property with that name.
1278 gtk_container_class_find_child_property (GObjectClass *cclass,
1279 const gchar *property_name)
1281 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1282 g_return_val_if_fail (property_name != NULL, NULL);
1284 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1286 G_OBJECT_CLASS_TYPE (cclass),
1291 * gtk_container_class_list_child_properties:
1292 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1293 * @n_properties: location to return the number of child properties found
1295 * Returns all child properties of a container class.
1297 * Returns: (array length=n_properties) (transfer container):
1298 * a newly allocated %NULL-terminated array of #GParamSpec*.
1299 * The array must be freed with g_free().
1302 gtk_container_class_list_child_properties (GObjectClass *cclass,
1303 guint *n_properties)
1305 GParamSpec **pspecs;
1308 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1310 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1311 G_OBJECT_CLASS_TYPE (cclass),
1320 gtk_container_add_unimplemented (GtkContainer *container,
1323 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1327 gtk_container_remove_unimplemented (GtkContainer *container,
1330 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1334 gtk_container_init (GtkContainer *container)
1336 GtkContainerPrivate *priv;
1338 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1340 GtkContainerPrivate);
1341 priv = container->priv;
1343 priv->focus_child = NULL;
1344 priv->border_width = 0;
1345 priv->need_resize = FALSE;
1346 priv->resize_mode = GTK_RESIZE_PARENT;
1347 priv->reallocate_redraws = FALSE;
1351 gtk_container_destroy (GtkWidget *widget)
1353 GtkContainer *container = GTK_CONTAINER (widget);
1354 GtkContainerPrivate *priv = container->priv;
1356 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1357 _gtk_container_dequeue_resize_handler (container);
1359 if (priv->focus_child)
1361 g_object_unref (priv->focus_child);
1362 priv->focus_child = NULL;
1365 /* do this before walking child widgets, to avoid
1366 * removing children from focus chain one by one.
1368 if (priv->has_focus_chain)
1369 gtk_container_unset_focus_chain (container);
1371 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1373 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1377 gtk_container_set_property (GObject *object,
1379 const GValue *value,
1382 GtkContainer *container = GTK_CONTAINER (object);
1386 case PROP_BORDER_WIDTH:
1387 gtk_container_set_border_width (container, g_value_get_uint (value));
1389 case PROP_RESIZE_MODE:
1390 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1393 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1402 gtk_container_get_property (GObject *object,
1407 GtkContainer *container = GTK_CONTAINER (object);
1408 GtkContainerPrivate *priv = container->priv;
1412 case PROP_BORDER_WIDTH:
1413 g_value_set_uint (value, priv->border_width);
1415 case PROP_RESIZE_MODE:
1416 g_value_set_enum (value, priv->resize_mode);
1419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1425 * gtk_container_set_border_width:
1426 * @container: a #GtkContainer
1427 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1428 * the container. Valid values are in the range 0-65535 pixels.
1430 * Sets the border width of the container.
1432 * The border width of a container is the amount of space to leave
1433 * around the outside of the container. The only exception to this is
1434 * #GtkWindow; because toplevel windows can't leave space outside,
1435 * they leave the space inside. The border is added on all sides of
1436 * the container. To add space to only one side, one approach is to
1437 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1438 * to give it a size, and place it on the side of the container as
1442 gtk_container_set_border_width (GtkContainer *container,
1445 GtkContainerPrivate *priv;
1447 g_return_if_fail (GTK_IS_CONTAINER (container));
1449 priv = container->priv;
1451 if (priv->border_width != border_width)
1453 priv->border_width = border_width;
1454 g_object_notify (G_OBJECT (container), "border-width");
1456 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1457 gtk_widget_queue_resize (GTK_WIDGET (container));
1462 * gtk_container_get_border_width:
1463 * @container: a #GtkContainer
1465 * Retrieves the border width of the container. See
1466 * gtk_container_set_border_width().
1468 * Return value: the current border width
1471 gtk_container_get_border_width (GtkContainer *container)
1473 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1475 return container->priv->border_width;
1479 * gtk_container_add:
1480 * @container: a #GtkContainer
1481 * @widget: a widget to be placed inside @container
1483 * Adds @widget to @container. Typically used for simple containers
1484 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1485 * layout containers such as #GtkBox or #GtkGrid, this function will
1486 * pick default packing parameters that may not be correct. So
1487 * consider functions such as gtk_box_pack_start() and
1488 * gtk_grid_attach() as an alternative to gtk_container_add() in
1489 * those cases. A widget may be added to only one container at a time;
1490 * you can't place the same widget inside two different containers.
1493 gtk_container_add (GtkContainer *container,
1498 g_return_if_fail (GTK_IS_CONTAINER (container));
1499 g_return_if_fail (GTK_IS_WIDGET (widget));
1501 parent = gtk_widget_get_parent (widget);
1505 g_warning ("Attempting to add a widget with type %s to a container of "
1506 "type %s, but the widget is already inside a container of type %s, "
1507 "please use gtk_widget_reparent()" ,
1508 g_type_name (G_OBJECT_TYPE (widget)),
1509 g_type_name (G_OBJECT_TYPE (container)),
1510 g_type_name (G_OBJECT_TYPE (parent)));
1514 g_signal_emit (container, container_signals[ADD], 0, widget);
1518 * gtk_container_remove:
1519 * @container: a #GtkContainer
1520 * @widget: a current child of @container
1522 * Removes @widget from @container. @widget must be inside @container.
1523 * Note that @container will own a reference to @widget, and that this
1524 * may be the last reference held; so removing a widget from its
1525 * container can destroy that widget. If you want to use @widget
1526 * again, you need to add a reference to it while it's not inside
1527 * a container, using g_object_ref(). If you don't want to use @widget
1528 * again it's usually more efficient to simply destroy it directly
1529 * using gtk_widget_destroy() since this will remove it from the
1530 * container and help break any circular reference count cycles.
1533 gtk_container_remove (GtkContainer *container,
1536 g_return_if_fail (GTK_IS_CONTAINER (container));
1537 g_return_if_fail (GTK_IS_WIDGET (widget));
1538 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container) || GTK_IS_ASSISTANT (container));
1540 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1544 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1546 g_return_if_fail (GTK_IS_CONTAINER (container));
1547 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1549 container_resize_queue = g_slist_remove (container_resize_queue, container);
1550 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1554 * gtk_container_set_resize_mode:
1555 * @container: a #GtkContainer
1556 * @resize_mode: the new resize mode
1558 * Sets the resize mode for the container.
1560 * The resize mode of a container determines whether a resize request
1561 * will be passed to the container's parent, queued for later execution
1562 * or executed immediately.
1565 gtk_container_set_resize_mode (GtkContainer *container,
1566 GtkResizeMode resize_mode)
1568 GtkContainerPrivate *priv;
1570 g_return_if_fail (GTK_IS_CONTAINER (container));
1571 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1573 priv = container->priv;
1575 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1576 resize_mode == GTK_RESIZE_PARENT)
1578 resize_mode = GTK_RESIZE_QUEUE;
1581 if (priv->resize_mode != resize_mode)
1583 priv->resize_mode = resize_mode;
1585 gtk_widget_queue_resize (GTK_WIDGET (container));
1586 g_object_notify (G_OBJECT (container), "resize-mode");
1591 * gtk_container_get_resize_mode:
1592 * @container: a #GtkContainer
1594 * Returns the resize mode for the container. See
1595 * gtk_container_set_resize_mode ().
1597 * Return value: the current resize mode
1600 gtk_container_get_resize_mode (GtkContainer *container)
1602 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1604 return container->priv->resize_mode;
1608 * gtk_container_set_reallocate_redraws:
1609 * @container: a #GtkContainer
1610 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1612 * Sets the @reallocate_redraws flag of the container to the given value.
1614 * Containers requesting reallocation redraws get automatically
1615 * redrawn if any of their children changed allocation.
1618 gtk_container_set_reallocate_redraws (GtkContainer *container,
1619 gboolean needs_redraws)
1621 g_return_if_fail (GTK_IS_CONTAINER (container));
1623 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1626 static GtkContainer*
1627 gtk_container_get_resize_container (GtkContainer *container)
1630 GtkWidget *widget = GTK_WIDGET (container);
1632 while ((parent = gtk_widget_get_parent (widget)))
1635 if (GTK_IS_RESIZE_CONTAINER (widget))
1639 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1643 gtk_container_idle_sizer (gpointer data)
1647 /* we may be invoked with a container_resize_queue of NULL, because
1648 * queue_resize could have been adding an extra idle function while
1649 * the queue still got processed. we better just ignore such case
1650 * than trying to explicitely work around them with some extra flags,
1651 * since it doesn't cause any actual harm.
1654 /* We validate the style contexts in a single loop before even trying
1655 * to handle resizes instead of doing validations inline.
1656 * This is mostly necessary for compatibility reasons with old code,
1657 * because size_allocate functions often change styles and so could
1658 * cause infinite loops in this function.
1660 for (slist = container_resize_queue; slist; slist = slist->next)
1662 _gtk_style_context_validate (gtk_widget_get_style_context (slist->data), 0);
1665 while (container_resize_queue)
1669 slist = container_resize_queue;
1670 container_resize_queue = slist->next;
1671 widget = slist->data;
1672 g_slist_free_1 (slist);
1674 _gtk_widget_set_resize_pending (widget, FALSE);
1675 gtk_container_check_resize (GTK_CONTAINER (widget));
1678 gdk_window_process_all_updates ();
1684 _gtk_container_queue_resize_internal (GtkContainer *container,
1685 gboolean invalidate_only)
1687 GtkContainer *resize_container;
1691 g_return_if_fail (GTK_IS_CONTAINER (container));
1693 widget = GTK_WIDGET (container);
1695 resize_container = gtk_container_get_resize_container (container);
1699 _gtk_widget_set_alloc_needed (widget, TRUE);
1700 _gtk_widget_set_width_request_needed (widget, TRUE);
1701 _gtk_widget_set_height_request_needed (widget, TRUE);
1703 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1704 !(parent = gtk_widget_get_parent (widget)))
1710 if (resize_container && !invalidate_only)
1712 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1713 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1714 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1716 switch (resize_container->priv->resize_mode)
1718 case GTK_RESIZE_QUEUE:
1719 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1721 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1722 if (container_resize_queue == NULL)
1723 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1724 gtk_container_idle_sizer,
1726 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1730 case GTK_RESIZE_IMMEDIATE:
1731 _gtk_style_context_validate (gtk_widget_get_style_context (GTK_WIDGET (resize_container)), 0);
1732 gtk_container_check_resize (resize_container);
1735 case GTK_RESIZE_PARENT:
1736 g_assert_not_reached ();
1742 /* we need to let hidden resize containers know that something
1743 * changed while they where hidden (currently only evaluated by
1746 resize_container->priv->need_resize = TRUE;
1752 * _gtk_container_queue_resize:
1753 * @container: a #GtkContainer
1755 * Determines the "resize container" in the hierarchy above this container
1756 * (typically the toplevel, but other containers can be set as resize
1757 * containers with gtk_container_set_resize_mode()), marks the container
1758 * and all parents up to and including the resize container as needing
1759 * to have sizes recompted, and if necessary adds the resize container
1760 * to the queue of containers that will be resized out at idle.
1763 _gtk_container_queue_resize (GtkContainer *container)
1765 _gtk_container_queue_resize_internal (container, FALSE);
1769 * _gtk_container_resize_invalidate:
1770 * @container: a #GtkContainer
1772 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1773 * actually queue the resize container for resize.
1776 _gtk_container_resize_invalidate (GtkContainer *container)
1778 _gtk_container_queue_resize_internal (container, TRUE);
1782 gtk_container_check_resize (GtkContainer *container)
1784 g_return_if_fail (GTK_IS_CONTAINER (container));
1786 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1790 gtk_container_real_check_resize (GtkContainer *container)
1792 GtkWidget *widget = GTK_WIDGET (container);
1793 GtkAllocation allocation;
1794 GtkRequisition requisition;
1796 gtk_widget_get_preferred_size (widget,
1797 &requisition, NULL);
1798 gtk_widget_get_allocation (widget, &allocation);
1800 if (requisition.width > allocation.width ||
1801 requisition.height > allocation.height)
1803 if (GTK_IS_RESIZE_CONTAINER (container))
1805 gtk_widget_size_allocate (widget, &allocation);
1806 gtk_widget_set_allocation (widget, &allocation);
1809 gtk_widget_queue_resize (widget);
1813 gtk_container_resize_children (container);
1817 /* The container hasn't changed size but one of its children
1818 * queued a resize request. Which means that the allocation
1819 * is not sufficient for the requisition of some child.
1820 * We've already performed a size request at this point,
1821 * so we simply need to reallocate and let the allocation
1822 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1825 gtk_container_resize_children (GtkContainer *container)
1827 GtkAllocation allocation;
1830 /* resizing invariants:
1831 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1832 * containers that have an idle sizer pending must be flagged with
1835 g_return_if_fail (GTK_IS_CONTAINER (container));
1837 widget = GTK_WIDGET (container);
1838 gtk_widget_get_allocation (widget, &allocation);
1840 gtk_widget_size_allocate (widget, &allocation);
1841 gtk_widget_set_allocation (widget, &allocation);
1845 gtk_container_adjust_size_request (GtkWidget *widget,
1846 GtkOrientation orientation,
1850 GtkContainer *container;
1852 container = GTK_CONTAINER (widget);
1854 if (GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1858 border_width = container->priv->border_width;
1860 *minimum_size += border_width * 2;
1861 *natural_size += border_width * 2;
1864 /* chain up last so gtk_widget_set_size_request() values
1865 * will have a chance to overwrite our border width.
1867 parent_class->adjust_size_request (widget, orientation,
1868 minimum_size, natural_size);
1872 gtk_container_adjust_size_allocation (GtkWidget *widget,
1873 GtkOrientation orientation,
1876 gint *allocated_pos,
1877 gint *allocated_size)
1879 GtkContainer *container;
1882 container = GTK_CONTAINER (widget);
1884 if (!GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1886 parent_class->adjust_size_allocation (widget, orientation,
1887 minimum_size, natural_size, allocated_pos,
1892 border_width = container->priv->border_width;
1894 *allocated_size -= border_width * 2;
1896 /* If we get a pathological too-small allocation to hold
1897 * even the border width, leave all allocation to the actual
1898 * widget, and leave x,y unchanged. (GtkWidget's min size is
1899 * 1x1 if you're wondering why <1 and not <0)
1901 * As long as we have space, set x,y properly.
1904 if (*allocated_size < 1)
1906 *allocated_size += border_width * 2;
1910 *allocated_pos += border_width;
1911 *minimum_size -= border_width * 2;
1912 *natural_size -= border_width * 2;
1915 /* Chain up to GtkWidgetClass *after* removing our border width from
1916 * the proposed allocation size. This is because it's possible that the
1917 * widget was allocated more space than it needs in a said orientation,
1918 * if GtkWidgetClass does any alignments and thus limits the size to the
1919 * natural size... then we need that to be done *after* removing any margins
1920 * and padding values.
1922 parent_class->adjust_size_allocation (widget, orientation,
1923 minimum_size, natural_size, allocated_pos,
1933 count_request_modes (GtkWidget *widget,
1934 RequestModeCount *count)
1936 GtkSizeRequestMode mode = gtk_widget_get_request_mode (widget);
1940 case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
1943 case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
1946 case GTK_SIZE_REQUEST_CONSTANT_SIZE:
1952 static GtkSizeRequestMode
1953 gtk_container_get_request_mode (GtkWidget *widget)
1955 GtkContainer *container = GTK_CONTAINER (widget);
1956 GtkContainerPrivate *priv = container->priv;
1958 /* Recalculate the request mode of the children by majority
1959 * vote whenever the internal content changes */
1960 if (_gtk_widget_get_width_request_needed (widget) ||
1961 _gtk_widget_get_height_request_needed (widget))
1963 RequestModeCount count = { 0, 0 };
1965 gtk_container_forall (container, (GtkCallback)count_request_modes, &count);
1967 if (!count.hfw && !count.wfh)
1968 priv->request_mode = GTK_SIZE_REQUEST_CONSTANT_SIZE;
1970 priv->request_mode = count.wfh > count.hfw ?
1971 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
1972 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1975 return priv->request_mode;
1979 * gtk_container_class_handle_border_width:
1980 * @klass: the class struct of a #GtkContainer subclass
1982 * Modifies a subclass of #GtkContainerClass to automatically add and
1983 * remove the border-width setting on GtkContainer. This allows the
1984 * subclass to ignore the border width in its size request and
1985 * allocate methods. The intent is for a subclass to invoke this
1986 * in its class_init function.
1988 * gtk_container_class_handle_border_width() is necessary because it
1989 * would break API too badly to make this behavior the default. So
1990 * subclasses must "opt in" to the parent class handling border_width
1994 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1996 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1998 klass->_handle_border_width = TRUE;
2002 * gtk_container_forall:
2003 * @container: a #GtkContainer
2004 * @callback: (scope call) (closure callback_data): a callback
2005 * @callback_data: callback user data
2007 * Invokes @callback on each child of @container, including children
2008 * that are considered "internal" (implementation details of the
2009 * container). "Internal" children generally weren't added by the user
2010 * of the container, but were added by the container implementation
2011 * itself. Most applications should use gtk_container_foreach(),
2012 * rather than gtk_container_forall().
2017 gtk_container_forall (GtkContainer *container,
2018 GtkCallback callback,
2019 gpointer callback_data)
2021 GtkContainerClass *class;
2023 g_return_if_fail (GTK_IS_CONTAINER (container));
2024 g_return_if_fail (callback != NULL);
2026 class = GTK_CONTAINER_GET_CLASS (container);
2029 class->forall (container, TRUE, callback, callback_data);
2033 * gtk_container_foreach:
2034 * @container: a #GtkContainer
2035 * @callback: (scope call): a callback
2036 * @callback_data: callback user data
2038 * Invokes @callback on each non-internal child of @container. See
2039 * gtk_container_forall() for details on what constitutes an
2040 * "internal" child. Most applications should use
2041 * gtk_container_foreach(), rather than gtk_container_forall().
2044 gtk_container_foreach (GtkContainer *container,
2045 GtkCallback callback,
2046 gpointer callback_data)
2048 GtkContainerClass *class;
2050 g_return_if_fail (GTK_IS_CONTAINER (container));
2051 g_return_if_fail (callback != NULL);
2053 class = GTK_CONTAINER_GET_CLASS (container);
2056 class->forall (container, FALSE, callback, callback_data);
2060 * gtk_container_set_focus_child:
2061 * @container: a #GtkContainer
2062 * @child: (allow-none): a #GtkWidget, or %NULL
2064 * Sets, or unsets if @child is %NULL, the focused child of @container.
2066 * This function emits the GtkContainer::set_focus_child signal of
2067 * @container. Implementations of #GtkContainer can override the
2068 * default behaviour by overriding the class closure of this signal.
2070 * This is function is mostly meant to be used by widgets. Applications can use
2071 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
2074 gtk_container_set_focus_child (GtkContainer *container,
2077 g_return_if_fail (GTK_IS_CONTAINER (container));
2079 g_return_if_fail (GTK_IS_WIDGET (child));
2081 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
2085 * gtk_container_get_focus_child:
2086 * @container: a #GtkContainer
2088 * Returns the current focus child widget inside @container. This is not the
2089 * currently focused widget. That can be obtained by calling
2090 * gtk_window_get_focus().
2092 * Returns: (transfer none): The child widget which will receive the
2093 * focus inside @container when the @conatiner is focussed,
2094 * or %NULL if none is set.
2099 gtk_container_get_focus_child (GtkContainer *container)
2101 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2103 return container->priv->focus_child;
2107 * gtk_container_get_children:
2108 * @container: a #GtkContainer
2110 * Returns the container's non-internal children. See
2111 * gtk_container_forall() for details on what constitutes an "internal" child.
2113 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
2116 gtk_container_get_children (GtkContainer *container)
2118 GList *children = NULL;
2120 gtk_container_foreach (container,
2121 gtk_container_children_callback,
2124 return g_list_reverse (children);
2128 gtk_container_child_position_callback (GtkWidget *widget,
2129 gpointer client_data)
2135 } *data = client_data;
2138 if (data->child == widget)
2139 data->index = data->i;
2143 gtk_container_child_default_composite_name (GtkContainer *container,
2153 /* fallback implementation */
2157 gtk_container_forall (container,
2158 gtk_container_child_position_callback,
2161 name = g_strdup_printf ("%s-%u",
2162 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2169 _gtk_container_child_composite_name (GtkContainer *container,
2172 gboolean composite_child;
2174 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2175 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2176 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2178 g_object_get (child, "composite-child", &composite_child, NULL);
2179 if (composite_child)
2181 static GQuark quark_composite_name = 0;
2184 if (!quark_composite_name)
2185 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2187 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2190 GtkContainerClass *class;
2192 class = GTK_CONTAINER_GET_CLASS (container);
2193 if (class->composite_name)
2194 name = class->composite_name (container, child);
2197 name = g_strdup (name);
2208 } ComputeExpandData;
2211 gtk_container_compute_expand_callback (GtkWidget *widget,
2212 gpointer client_data)
2214 ComputeExpandData *data = client_data;
2216 /* note that we don't get_expand on the child if we already know we
2217 * have to expand, so we only recurse into children until we find
2218 * one that expands and then we basically don't do any more
2219 * work. This means that we can leave some children in a
2220 * need_compute_expand state, which is fine, as long as GtkWidget
2221 * doesn't rely on an invariant that "if a child has
2222 * need_compute_expand, its parents also do"
2224 * gtk_widget_compute_expand() always returns FALSE if the
2225 * child is !visible so that's taken care of.
2227 data->hexpand = data->hexpand ||
2228 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2230 data->vexpand = data->vexpand ||
2231 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2235 gtk_container_compute_expand (GtkWidget *widget,
2236 gboolean *hexpand_p,
2237 gboolean *vexpand_p)
2239 ComputeExpandData data;
2241 data.hexpand = FALSE;
2242 data.vexpand = FALSE;
2244 gtk_container_forall (GTK_CONTAINER (widget),
2245 gtk_container_compute_expand_callback,
2248 *hexpand_p = data.hexpand;
2249 *vexpand_p = data.vexpand;
2253 gtk_container_real_set_focus_child (GtkContainer *container,
2256 GtkContainerPrivate *priv;
2258 g_return_if_fail (GTK_IS_CONTAINER (container));
2259 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2261 priv = container->priv;
2263 if (child != priv->focus_child)
2265 if (priv->focus_child)
2266 g_object_unref (priv->focus_child);
2267 priv->focus_child = child;
2268 if (priv->focus_child)
2269 g_object_ref (priv->focus_child);
2273 /* check for h/v adjustments
2275 if (priv->focus_child)
2277 GtkAdjustment *hadj;
2278 GtkAdjustment *vadj;
2279 GtkAllocation allocation;
2280 GtkWidget *focus_child;
2283 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2284 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2288 focus_child = priv->focus_child;
2289 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2291 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2294 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2297 gtk_widget_get_allocation (priv->focus_child, &allocation);
2301 gtk_widget_get_allocation (focus_child, &allocation);
2304 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2307 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2313 get_focus_chain (GtkContainer *container)
2315 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2318 /* same as gtk_container_get_children, except it includes internals
2321 _gtk_container_get_all_children (GtkContainer *container)
2323 GList *children = NULL;
2325 gtk_container_forall (container,
2326 gtk_container_children_callback,
2332 static GtkWidgetPath *
2333 gtk_container_real_get_path_for_child (GtkContainer *container,
2336 GtkStyleContext *context;
2337 GtkWidgetPath *path;
2340 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2341 path = _gtk_widget_create_path (GTK_WIDGET (container));
2343 /* Copy any permanent classes to the path */
2344 classes = gtk_style_context_list_classes (context);
2351 classes = classes->next;
2353 gtk_widget_path_iter_add_class (path, -1, cur->data);
2354 g_list_free_1 (cur);
2357 gtk_widget_path_append_for_widget (path, child);
2363 gtk_container_focus (GtkWidget *widget,
2364 GtkDirectionType direction)
2367 GList *sorted_children;
2369 GtkContainer *container;
2370 GtkContainerPrivate *priv;
2372 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2374 container = GTK_CONTAINER (widget);
2375 priv = container->priv;
2379 if (gtk_widget_get_can_focus (widget))
2381 if (!gtk_widget_has_focus (widget))
2383 gtk_widget_grab_focus (widget);
2389 /* Get a list of the containers children, allowing focus
2390 * chain to override.
2392 if (priv->has_focus_chain)
2393 children = g_list_copy (get_focus_chain (container));
2395 children = _gtk_container_get_all_children (container);
2397 if (priv->has_focus_chain &&
2398 (direction == GTK_DIR_TAB_FORWARD ||
2399 direction == GTK_DIR_TAB_BACKWARD))
2401 sorted_children = g_list_copy (children);
2403 if (direction == GTK_DIR_TAB_BACKWARD)
2404 sorted_children = g_list_reverse (sorted_children);
2407 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2409 return_val = gtk_container_focus_move (container, sorted_children, direction);
2411 g_list_free (sorted_children);
2412 g_list_free (children);
2419 tab_compare (gconstpointer a,
2423 GtkAllocation child1_allocation, child2_allocation;
2424 const GtkWidget *child1 = a;
2425 const GtkWidget *child2 = b;
2426 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2429 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2430 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2432 y1 = child1_allocation.y + child1_allocation.height / 2;
2433 y2 = child2_allocation.y + child2_allocation.height / 2;
2437 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2438 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2440 if (text_direction == GTK_TEXT_DIR_RTL)
2441 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2443 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2446 return (y1 < y2) ? -1 : 1;
2450 gtk_container_focus_sort_tab (GtkContainer *container,
2452 GtkDirectionType direction,
2453 GtkWidget *old_focus)
2455 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2456 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2458 /* if we are going backwards then reverse the order
2461 if (direction == GTK_DIR_TAB_BACKWARD)
2462 children = g_list_reverse (children);
2467 /* Get coordinates of @widget's allocation with respect to
2468 * allocation of @container.
2471 get_allocation_coords (GtkContainer *container,
2473 GdkRectangle *allocation)
2475 gtk_widget_get_allocation (widget, allocation);
2477 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2478 0, 0, &allocation->x, &allocation->y);
2481 /* Look for a child in @children that is intermediate between
2482 * the focus widget and container. This widget, if it exists,
2483 * acts as the starting widget for focus navigation.
2486 find_old_focus (GtkContainer *container,
2489 GList *tmp_list = children;
2492 GtkWidget *child = tmp_list->data;
2493 GtkWidget *widget = child;
2495 while (widget && widget != (GtkWidget *)container)
2499 parent = gtk_widget_get_parent (widget);
2501 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2510 tmp_list = tmp_list->next;
2517 old_focus_coords (GtkContainer *container,
2518 GdkRectangle *old_focus_rect)
2520 GtkWidget *widget = GTK_WIDGET (container);
2521 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2522 GtkWidget *old_focus;
2524 if (GTK_IS_WINDOW (toplevel))
2526 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2528 return get_allocation_coords (container, old_focus, old_focus_rect);
2534 typedef struct _CompareInfo CompareInfo;
2538 GtkContainer *container;
2545 up_down_compare (gconstpointer a,
2549 GdkRectangle allocation1;
2550 GdkRectangle allocation2;
2551 CompareInfo *compare = data;
2554 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2555 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2557 y1 = allocation1.y + allocation1.height / 2;
2558 y2 = allocation2.y + allocation2.height / 2;
2562 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2563 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2565 if (compare->reverse)
2566 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2568 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2571 return (y1 < y2) ? -1 : 1;
2575 gtk_container_focus_sort_up_down (GtkContainer *container,
2577 GtkDirectionType direction,
2578 GtkWidget *old_focus)
2580 CompareInfo compare;
2582 GdkRectangle old_allocation;
2584 compare.container = container;
2585 compare.reverse = (direction == GTK_DIR_UP);
2588 old_focus = find_old_focus (container, children);
2590 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2596 /* Delete widgets from list that don't match minimum criteria */
2598 compare_x1 = old_allocation.x;
2599 compare_x2 = old_allocation.x + old_allocation.width;
2601 if (direction == GTK_DIR_UP)
2602 compare_y = old_allocation.y;
2604 compare_y = old_allocation.y + old_allocation.height;
2606 tmp_list = children;
2609 GtkWidget *child = tmp_list->data;
2610 GList *next = tmp_list->next;
2611 gint child_x1, child_x2;
2612 GdkRectangle child_allocation;
2614 if (child != old_focus)
2616 if (get_allocation_coords (container, child, &child_allocation))
2618 child_x1 = child_allocation.x;
2619 child_x2 = child_allocation.x + child_allocation.width;
2621 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2622 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2623 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2625 children = g_list_delete_link (children, tmp_list);
2629 children = g_list_delete_link (children, tmp_list);
2635 compare.x = (compare_x1 + compare_x2) / 2;
2636 compare.y = old_allocation.y + old_allocation.height / 2;
2640 /* No old focus widget, need to figure out starting x,y some other way
2642 GtkAllocation allocation;
2643 GtkWidget *widget = GTK_WIDGET (container);
2644 GdkRectangle old_focus_rect;
2646 gtk_widget_get_allocation (widget, &allocation);
2648 if (old_focus_coords (container, &old_focus_rect))
2650 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2654 if (!gtk_widget_get_has_window (widget))
2655 compare.x = allocation.x + allocation.width / 2;
2657 compare.x = allocation.width / 2;
2660 if (!gtk_widget_get_has_window (widget))
2661 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2663 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2666 children = g_list_sort_with_data (children, up_down_compare, &compare);
2668 if (compare.reverse)
2669 children = g_list_reverse (children);
2675 left_right_compare (gconstpointer a,
2679 GdkRectangle allocation1;
2680 GdkRectangle allocation2;
2681 CompareInfo *compare = data;
2684 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2685 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2687 x1 = allocation1.x + allocation1.width / 2;
2688 x2 = allocation2.x + allocation2.width / 2;
2692 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2693 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2695 if (compare->reverse)
2696 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2698 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2701 return (x1 < x2) ? -1 : 1;
2705 gtk_container_focus_sort_left_right (GtkContainer *container,
2707 GtkDirectionType direction,
2708 GtkWidget *old_focus)
2710 CompareInfo compare;
2712 GdkRectangle old_allocation;
2714 compare.container = container;
2715 compare.reverse = (direction == GTK_DIR_LEFT);
2718 old_focus = find_old_focus (container, children);
2720 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2726 /* Delete widgets from list that don't match minimum criteria */
2728 compare_y1 = old_allocation.y;
2729 compare_y2 = old_allocation.y + old_allocation.height;
2731 if (direction == GTK_DIR_LEFT)
2732 compare_x = old_allocation.x;
2734 compare_x = old_allocation.x + old_allocation.width;
2736 tmp_list = children;
2739 GtkWidget *child = tmp_list->data;
2740 GList *next = tmp_list->next;
2741 gint child_y1, child_y2;
2742 GdkRectangle child_allocation;
2744 if (child != old_focus)
2746 if (get_allocation_coords (container, child, &child_allocation))
2748 child_y1 = child_allocation.y;
2749 child_y2 = child_allocation.y + child_allocation.height;
2751 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2752 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2753 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2755 children = g_list_delete_link (children, tmp_list);
2759 children = g_list_delete_link (children, tmp_list);
2765 compare.y = (compare_y1 + compare_y2) / 2;
2766 compare.x = old_allocation.x + old_allocation.width / 2;
2770 /* No old focus widget, need to figure out starting x,y some other way
2772 GtkAllocation allocation;
2773 GtkWidget *widget = GTK_WIDGET (container);
2774 GdkRectangle old_focus_rect;
2776 gtk_widget_get_allocation (widget, &allocation);
2778 if (old_focus_coords (container, &old_focus_rect))
2780 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2784 if (!gtk_widget_get_has_window (widget))
2785 compare.y = allocation.y + allocation.height / 2;
2787 compare.y = allocation.height / 2;
2790 if (!gtk_widget_get_has_window (widget))
2791 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2793 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2796 children = g_list_sort_with_data (children, left_right_compare, &compare);
2798 if (compare.reverse)
2799 children = g_list_reverse (children);
2805 * gtk_container_focus_sort:
2806 * @container: a #GtkContainer
2807 * @children: a list of descendents of @container (they don't
2808 * have to be direct children)
2809 * @direction: focus direction
2810 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2811 * to determine this automatically.
2812 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2813 * which is the only @direction we use currently,
2814 * so perhaps this argument should be removed)
2816 * Sorts @children in the correct order for focusing with
2817 * direction type @direction.
2819 * Return value: a copy of @children, sorted in correct focusing order,
2820 * with children that aren't suitable for focusing in this direction
2824 _gtk_container_focus_sort (GtkContainer *container,
2826 GtkDirectionType direction,
2827 GtkWidget *old_focus)
2829 GList *visible_children = NULL;
2833 if (gtk_widget_get_realized (children->data))
2834 visible_children = g_list_prepend (visible_children, children->data);
2835 children = children->next;
2840 case GTK_DIR_TAB_FORWARD:
2841 case GTK_DIR_TAB_BACKWARD:
2842 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2845 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2848 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2851 g_assert_not_reached ();
2857 gtk_container_focus_move (GtkContainer *container,
2859 GtkDirectionType direction)
2861 GtkContainerPrivate *priv = container->priv;
2862 GtkWidget *focus_child;
2865 focus_child = priv->focus_child;
2869 child = children->data;
2870 children = children->next;
2877 if (focus_child == child)
2881 if (gtk_widget_child_focus (child, direction))
2885 else if (gtk_widget_is_drawable (child) &&
2886 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2888 if (gtk_widget_child_focus (child, direction))
2898 gtk_container_children_callback (GtkWidget *widget,
2899 gpointer client_data)
2903 children = (GList**) client_data;
2904 *children = g_list_prepend (*children, widget);
2908 chain_widget_destroyed (GtkWidget *widget,
2911 GtkContainer *container;
2914 container = GTK_CONTAINER (user_data);
2916 chain = g_object_get_data (G_OBJECT (container),
2917 "gtk-container-focus-chain");
2919 chain = g_list_remove (chain, widget);
2921 g_signal_handlers_disconnect_by_func (widget,
2922 chain_widget_destroyed,
2925 g_object_set_data (G_OBJECT (container),
2926 I_("gtk-container-focus-chain"),
2931 * gtk_container_set_focus_chain:
2932 * @container: a #GtkContainer
2933 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2934 * the new focus chain
2936 * Sets a focus chain, overriding the one computed automatically by GTK+.
2938 * In principle each widget in the chain should be a descendant of the
2939 * container, but this is not enforced by this method, since it's allowed
2940 * to set the focus chain before you pack the widgets, or have a widget
2941 * in the chain that isn't always packed. The necessary checks are done
2942 * when the focus chain is actually traversed.
2945 gtk_container_set_focus_chain (GtkContainer *container,
2946 GList *focusable_widgets)
2950 GtkContainerPrivate *priv;
2952 g_return_if_fail (GTK_IS_CONTAINER (container));
2954 priv = container->priv;
2956 if (priv->has_focus_chain)
2957 gtk_container_unset_focus_chain (container);
2959 priv->has_focus_chain = TRUE;
2962 tmp_list = focusable_widgets;
2963 while (tmp_list != NULL)
2965 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2967 /* In principle each widget in the chain should be a descendant
2968 * of the container, but we don't want to check that here. It's
2969 * expensive and also it's allowed to set the focus chain before
2970 * you pack the widgets, or have a widget in the chain that isn't
2971 * always packed. So we check for ancestor during actual traversal.
2974 chain = g_list_prepend (chain, tmp_list->data);
2976 g_signal_connect (tmp_list->data,
2978 G_CALLBACK (chain_widget_destroyed),
2981 tmp_list = g_list_next (tmp_list);
2984 chain = g_list_reverse (chain);
2986 g_object_set_data (G_OBJECT (container),
2987 I_("gtk-container-focus-chain"),
2992 * gtk_container_get_focus_chain:
2993 * @container: a #GtkContainer
2994 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2995 * to store the focus chain of the
2996 * container, or %NULL. You should free this list
2997 * using g_list_free() when you are done with it, however
2998 * no additional reference count is added to the
2999 * individual widgets in the focus chain.
3001 * Retrieves the focus chain of the container, if one has been
3002 * set explicitly. If no focus chain has been explicitly
3003 * set, GTK+ computes the focus chain based on the positions
3004 * of the children. In that case, GTK+ stores %NULL in
3005 * @focusable_widgets and returns %FALSE.
3007 * Return value: %TRUE if the focus chain of the container
3008 * has been set explicitly.
3011 gtk_container_get_focus_chain (GtkContainer *container,
3012 GList **focus_chain)
3014 GtkContainerPrivate *priv;
3016 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
3018 priv = container->priv;
3022 if (priv->has_focus_chain)
3023 *focus_chain = g_list_copy (get_focus_chain (container));
3025 *focus_chain = NULL;
3028 return priv->has_focus_chain;
3032 * gtk_container_unset_focus_chain:
3033 * @container: a #GtkContainer
3035 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
3038 gtk_container_unset_focus_chain (GtkContainer *container)
3040 GtkContainerPrivate *priv;
3042 g_return_if_fail (GTK_IS_CONTAINER (container));
3044 priv = container->priv;
3046 if (priv->has_focus_chain)
3051 chain = get_focus_chain (container);
3053 priv->has_focus_chain = FALSE;
3055 g_object_set_data (G_OBJECT (container),
3056 I_("gtk-container-focus-chain"),
3060 while (tmp_list != NULL)
3062 g_signal_handlers_disconnect_by_func (tmp_list->data,
3063 chain_widget_destroyed,
3066 tmp_list = g_list_next (tmp_list);
3069 g_list_free (chain);
3074 * gtk_container_set_focus_vadjustment:
3075 * @container: a #GtkContainer
3076 * @adjustment: an adjustment which should be adjusted when the focus
3077 * is moved among the descendents of @container
3079 * Hooks up an adjustment to focus handling in a container, so when a
3080 * child of the container is focused, the adjustment is scrolled to
3081 * show that widget. This function sets the vertical alignment. See
3082 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
3083 * the adjustment and gtk_container_set_focus_hadjustment() for setting
3084 * the horizontal adjustment.
3086 * The adjustments have to be in pixel units and in the same coordinate
3087 * system as the allocation for immediate children of the container.
3090 gtk_container_set_focus_vadjustment (GtkContainer *container,
3091 GtkAdjustment *adjustment)
3093 g_return_if_fail (GTK_IS_CONTAINER (container));
3095 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3098 g_object_ref (adjustment);
3100 g_object_set_qdata_full (G_OBJECT (container),
3107 * gtk_container_get_focus_vadjustment:
3108 * @container: a #GtkContainer
3110 * Retrieves the vertical focus adjustment for the container. See
3111 * gtk_container_set_focus_vadjustment().
3113 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
3114 * none has been set.
3117 gtk_container_get_focus_vadjustment (GtkContainer *container)
3119 GtkAdjustment *vadjustment;
3121 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3123 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
3129 * gtk_container_set_focus_hadjustment:
3130 * @container: a #GtkContainer
3131 * @adjustment: an adjustment which should be adjusted when the focus is
3132 * moved among the descendents of @container
3134 * Hooks up an adjustment to focus handling in a container, so when a child
3135 * of the container is focused, the adjustment is scrolled to show that
3136 * widget. This function sets the horizontal alignment.
3137 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
3138 * the adjustment and gtk_container_set_focus_vadjustment() for setting
3139 * the vertical adjustment.
3141 * The adjustments have to be in pixel units and in the same coordinate
3142 * system as the allocation for immediate children of the container.
3145 gtk_container_set_focus_hadjustment (GtkContainer *container,
3146 GtkAdjustment *adjustment)
3148 g_return_if_fail (GTK_IS_CONTAINER (container));
3150 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3153 g_object_ref (adjustment);
3155 g_object_set_qdata_full (G_OBJECT (container),
3162 * gtk_container_get_focus_hadjustment:
3163 * @container: a #GtkContainer
3165 * Retrieves the horizontal focus adjustment for the container. See
3166 * gtk_container_set_focus_hadjustment ().
3168 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3169 * none has been set.
3172 gtk_container_get_focus_hadjustment (GtkContainer *container)
3174 GtkAdjustment *hadjustment;
3176 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3178 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3185 gtk_container_show_all (GtkWidget *widget)
3187 g_return_if_fail (GTK_IS_CONTAINER (widget));
3189 gtk_container_foreach (GTK_CONTAINER (widget),
3190 (GtkCallback) gtk_widget_show_all,
3192 gtk_widget_show (widget);
3196 gtk_container_draw_child (GtkWidget *child,
3197 gpointer client_data)
3200 GtkWidget *container;
3202 } *data = client_data;
3204 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3210 gtk_container_draw (GtkWidget *widget,
3214 GtkWidget *container;
3218 data.container = widget;
3221 gtk_container_forall (GTK_CONTAINER (widget),
3222 gtk_container_draw_child,
3229 gtk_container_map_child (GtkWidget *child,
3230 gpointer client_data)
3232 if (gtk_widget_get_visible (child) &&
3233 gtk_widget_get_child_visible (child) &&
3234 !gtk_widget_get_mapped (child))
3235 gtk_widget_map (child);
3239 gtk_container_map (GtkWidget *widget)
3241 gtk_widget_set_mapped (widget, TRUE);
3243 gtk_container_forall (GTK_CONTAINER (widget),
3244 gtk_container_map_child,
3247 if (gtk_widget_get_has_window (widget))
3248 gdk_window_show (gtk_widget_get_window (widget));
3252 gtk_container_unmap (GtkWidget *widget)
3254 gtk_widget_set_mapped (widget, FALSE);
3256 /* hide our window first so user doesn't see all the child windows
3257 * vanishing one by one. (only matters these days if one of the
3258 * children has an actual native window instead of client-side
3259 * window, e.g. a GtkSocket would)
3261 if (gtk_widget_get_has_window (widget))
3262 gdk_window_hide (gtk_widget_get_window (widget));
3264 gtk_container_forall (GTK_CONTAINER (widget),
3265 (GtkCallback)gtk_widget_unmap,
3270 * gtk_container_propagate_draw:
3271 * @container: a #GtkContainer
3272 * @child: a child of @container
3273 * @cr: Cairo context as passed to the container. If you want to use @cr
3274 * in container's draw function, consider using cairo_save() and
3275 * cairo_restore() before calling this function.
3277 * When a container receives a call to the draw function, it must send
3278 * synthetic #GtkWidget::draw calls to all children that don't have their
3279 * own #GdkWindows. This function provides a convenient way of doing this.
3280 * A container, when it receives a call to its #GtkWidget::draw function,
3281 * calls gtk_container_propagate_draw() once for each child, passing in
3282 * the @cr the container received.
3284 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3285 * and deciding whether the draw needs to be sent to the child. It is a
3286 * convenient and optimized way of getting the same effect as calling
3287 * gtk_widget_draw() on the child directly.
3289 * In most cases, a container can simply either inherit the
3290 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3291 * and then chain to the ::draw implementation from #GtkContainer.
3294 gtk_container_propagate_draw (GtkContainer *container,
3298 GdkEventExpose *event;
3299 GtkAllocation allocation;
3300 GdkWindow *window, *w;
3303 g_return_if_fail (GTK_IS_CONTAINER (container));
3304 g_return_if_fail (GTK_IS_WIDGET (child));
3305 g_return_if_fail (cr != NULL);
3307 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3309 event = _gtk_cairo_get_event (cr);
3312 if (gtk_widget_get_has_window (child) ||
3313 gtk_widget_get_window (child) != event->window)
3319 /* translate coordinates. Ugly business, that. */
3320 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3322 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3332 window = gtk_widget_get_window (GTK_WIDGET (container));
3334 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3337 gdk_window_get_position (w, &wx, &wy);
3348 if (!gtk_widget_get_has_window (child))
3350 gtk_widget_get_allocation (child, &allocation);
3355 cairo_translate (cr, x, y);
3357 _gtk_widget_draw_internal (child, cr, TRUE);
3363 _gtk_container_get_need_resize (GtkContainer *container)
3365 return container->priv->need_resize;
3369 _gtk_container_set_need_resize (GtkContainer *container,
3370 gboolean need_resize)
3372 container->priv->need_resize = need_resize;
3376 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3378 return container->priv->reallocate_redraws;
3382 * gtk_container_get_path_for_child:
3383 * @container: a #GtkContainer
3384 * @child: a child of @container
3386 * Returns a newly created widget path representing all the widget hierarchy
3387 * from the toplevel down to and including @child.
3389 * Returns: A newly created #GtkWidgetPath
3392 gtk_container_get_path_for_child (GtkContainer *container,
3395 GtkWidgetPath *path;
3397 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3398 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3399 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3401 path = GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);
3402 if (gtk_widget_path_get_object_type (path) != G_OBJECT_TYPE (child))
3404 g_critical ("%s %p returned a widget path for type %s, but child is %s",
3405 G_OBJECT_TYPE_NAME (container),
3407 g_type_name (gtk_widget_path_get_object_type (path)),
3408 G_OBJECT_TYPE_NAME (child));