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;
582 gchar *child_prop_name;
584 gboolean translatable;
585 } PackingPropertiesData;
588 attributes_start_element (GMarkupParseContext *context,
589 const gchar *element_name,
591 const gchar **values,
595 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
598 if (strcmp (element_name, "property") == 0)
600 for (i = 0; names[i]; i++)
601 if (strcmp (names[i], "name") == 0)
602 parser_data->child_prop_name = g_strdup (values[i]);
603 else if (strcmp (names[i], "translatable") == 0)
605 if (!_gtk_builder_boolean_from_string (values[1],
606 &parser_data->translatable,
610 else if (strcmp (names[i], "comments") == 0)
611 ; /* for translators */
612 else if (strcmp (names[i], "context") == 0)
613 parser_data->context = g_strdup (values[1]);
615 g_warning ("Unsupported attribute for GtkContainer Child "
616 "property: %s\n", names[i]);
618 else if (strcmp (element_name, "packing") == 0)
621 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
625 attributes_text_element (GMarkupParseContext *context,
631 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
634 if (!parser_data->child_prop_name)
637 if (parser_data->translatable && text_len)
640 domain = gtk_builder_get_translation_domain (parser_data->builder);
642 value = _gtk_builder_parser_translate (domain,
643 parser_data->context,
648 value = g_strdup (text);
651 gtk_container_buildable_set_child_property (parser_data->container,
652 parser_data->builder,
654 parser_data->child_prop_name,
657 g_free (parser_data->child_prop_name);
658 g_free (parser_data->context);
660 parser_data->child_prop_name = NULL;
661 parser_data->context = NULL;
662 parser_data->translatable = FALSE;
665 static const GMarkupParser attributes_parser =
667 attributes_start_element,
669 attributes_text_element,
673 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
676 const gchar *tagname,
677 GMarkupParser *parser,
680 PackingPropertiesData *parser_data;
682 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
683 tagname, parser, data))
686 if (child && strcmp (tagname, "packing") == 0)
688 parser_data = g_slice_new0 (PackingPropertiesData);
689 parser_data->builder = builder;
690 parser_data->container = GTK_CONTAINER (buildable);
691 parser_data->child = GTK_WIDGET (child);
692 parser_data->child_prop_name = NULL;
694 *parser = attributes_parser;
703 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
706 const gchar *tagname,
709 if (strcmp (tagname, "packing") == 0)
711 g_slice_free (PackingPropertiesData, (gpointer)data);
716 if (parent_buildable_iface->custom_tag_end)
717 parent_buildable_iface->custom_tag_end (buildable, builder,
718 child, tagname, data);
723 * gtk_container_child_type:
724 * @container: a #GtkContainer
726 * Returns the type of the children supported by the container.
728 * Note that this may return %G_TYPE_NONE to indicate that no more
729 * children can be added, e.g. for a #GtkPaned which already has two
732 * Return value: a #GType.
735 gtk_container_child_type (GtkContainer *container)
738 GtkContainerClass *class;
740 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
742 class = GTK_CONTAINER_GET_CLASS (container);
743 if (class->child_type)
744 slot = class->child_type (container);
751 /* --- GtkContainer child property mechanism --- */
753 container_get_child_property (GtkContainer *container,
758 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
760 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
764 container_set_child_property (GtkContainer *container,
768 GObjectNotifyQueue *nqueue)
770 GValue tmp_value = { 0, };
771 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
773 /* provide a copy to work from, convert (if necessary) and validate */
774 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
775 if (!g_value_transform (value, &tmp_value))
776 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
778 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
779 G_VALUE_TYPE_NAME (value));
780 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
782 gchar *contents = g_strdup_value_contents (value);
784 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
786 G_VALUE_TYPE_NAME (value),
788 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
793 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
794 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
796 g_value_unset (&tmp_value);
800 * gtk_container_child_get_valist:
801 * @container: a #GtkContainer
802 * @child: a widget which is a child of @container
803 * @first_property_name: the name of the first property to get
804 * @var_args: return location for the first property, followed
805 * optionally by more name/return location pairs, followed by %NULL
807 * Gets the values of one or more child properties for @child and @container.
810 gtk_container_child_get_valist (GtkContainer *container,
812 const gchar *first_property_name,
817 g_return_if_fail (GTK_IS_CONTAINER (container));
818 g_return_if_fail (GTK_IS_WIDGET (child));
819 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
821 g_object_ref (container);
822 g_object_ref (child);
824 name = first_property_name;
827 GValue value = { 0, };
831 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
833 G_OBJECT_TYPE (container),
837 g_warning ("%s: container class `%s' has no child property named `%s'",
839 G_OBJECT_TYPE_NAME (container),
843 if (!(pspec->flags & G_PARAM_READABLE))
845 g_warning ("%s: child property `%s' of container class `%s' is not readable",
848 G_OBJECT_TYPE_NAME (container));
851 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
852 container_get_child_property (container, child, pspec, &value);
853 G_VALUE_LCOPY (&value, var_args, 0, &error);
856 g_warning ("%s: %s", G_STRLOC, error);
858 g_value_unset (&value);
861 g_value_unset (&value);
862 name = va_arg (var_args, gchar*);
865 g_object_unref (child);
866 g_object_unref (container);
870 * gtk_container_child_get_property:
871 * @container: a #GtkContainer
872 * @child: a widget which is a child of @container
873 * @property_name: the name of the property to get
874 * @value: a location to return the value
876 * Gets the value of a child property for @child and @container.
879 gtk_container_child_get_property (GtkContainer *container,
881 const gchar *property_name,
886 g_return_if_fail (GTK_IS_CONTAINER (container));
887 g_return_if_fail (GTK_IS_WIDGET (child));
888 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
889 g_return_if_fail (property_name != NULL);
890 g_return_if_fail (G_IS_VALUE (value));
892 g_object_ref (container);
893 g_object_ref (child);
894 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
895 G_OBJECT_TYPE (container), TRUE);
897 g_warning ("%s: container class `%s' has no child property named `%s'",
899 G_OBJECT_TYPE_NAME (container),
901 else if (!(pspec->flags & G_PARAM_READABLE))
902 g_warning ("%s: child property `%s' of container class `%s' is not readable",
905 G_OBJECT_TYPE_NAME (container));
908 GValue *prop_value, tmp_value = { 0, };
910 /* auto-conversion of the callers value type
912 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
914 g_value_reset (value);
917 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
919 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
921 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
922 G_VALUE_TYPE_NAME (value));
923 g_object_unref (child);
924 g_object_unref (container);
929 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
930 prop_value = &tmp_value;
932 container_get_child_property (container, child, pspec, prop_value);
933 if (prop_value != value)
935 g_value_transform (prop_value, value);
936 g_value_unset (&tmp_value);
939 g_object_unref (child);
940 g_object_unref (container);
944 * gtk_container_child_set_valist:
945 * @container: a #GtkContainer
946 * @child: a widget which is a child of @container
947 * @first_property_name: the name of the first property to set
948 * @var_args: a %NULL-terminated list of property names and values, starting
949 * with @first_prop_name
951 * Sets one or more child properties for @child and @container.
954 gtk_container_child_set_valist (GtkContainer *container,
956 const gchar *first_property_name,
959 GObjectNotifyQueue *nqueue;
962 g_return_if_fail (GTK_IS_CONTAINER (container));
963 g_return_if_fail (GTK_IS_WIDGET (child));
964 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
966 g_object_ref (container);
967 g_object_ref (child);
969 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
970 name = first_property_name;
973 GValue value = { 0, };
975 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
977 G_OBJECT_TYPE (container),
981 g_warning ("%s: container class `%s' has no child property named `%s'",
983 G_OBJECT_TYPE_NAME (container),
987 if (!(pspec->flags & G_PARAM_WRITABLE))
989 g_warning ("%s: child property `%s' of container class `%s' is not writable",
992 G_OBJECT_TYPE_NAME (container));
995 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
996 G_VALUE_COLLECT (&value, var_args, 0, &error);
999 g_warning ("%s: %s", G_STRLOC, error);
1002 /* we purposely leak the value here, it might not be
1003 * in a sane state if an error condition occoured
1007 container_set_child_property (container, child, pspec, &value, nqueue);
1008 g_value_unset (&value);
1009 name = va_arg (var_args, gchar*);
1011 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1013 g_object_unref (container);
1014 g_object_unref (child);
1018 * gtk_container_child_set_property:
1019 * @container: a #GtkContainer
1020 * @child: a widget which is a child of @container
1021 * @property_name: the name of the property to set
1022 * @value: the value to set the property to
1024 * Sets a child property for @child and @container.
1027 gtk_container_child_set_property (GtkContainer *container,
1029 const gchar *property_name,
1030 const GValue *value)
1032 GObjectNotifyQueue *nqueue;
1035 g_return_if_fail (GTK_IS_CONTAINER (container));
1036 g_return_if_fail (GTK_IS_WIDGET (child));
1037 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1038 g_return_if_fail (property_name != NULL);
1039 g_return_if_fail (G_IS_VALUE (value));
1041 g_object_ref (container);
1042 g_object_ref (child);
1044 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1045 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1046 G_OBJECT_TYPE (container), TRUE);
1048 g_warning ("%s: container class `%s' has no child property named `%s'",
1050 G_OBJECT_TYPE_NAME (container),
1052 else if (!(pspec->flags & G_PARAM_WRITABLE))
1053 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1056 G_OBJECT_TYPE_NAME (container));
1059 container_set_child_property (container, child, pspec, value, nqueue);
1061 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1062 g_object_unref (container);
1063 g_object_unref (child);
1067 * gtk_container_add_with_properties:
1068 * @container: a #GtkContainer
1069 * @widget: a widget to be placed inside @container
1070 * @first_prop_name: the name of the first child property to set
1071 * @Varargs: a %NULL-terminated list of property names and values, starting
1072 * with @first_prop_name
1074 * Adds @widget to @container, setting child properties at the same time.
1075 * See gtk_container_add() and gtk_container_child_set() for more details.
1078 gtk_container_add_with_properties (GtkContainer *container,
1080 const gchar *first_prop_name,
1083 g_return_if_fail (GTK_IS_CONTAINER (container));
1084 g_return_if_fail (GTK_IS_WIDGET (widget));
1085 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1087 g_object_ref (container);
1088 g_object_ref (widget);
1089 gtk_widget_freeze_child_notify (widget);
1091 g_signal_emit (container, container_signals[ADD], 0, widget);
1092 if (gtk_widget_get_parent (widget))
1096 va_start (var_args, first_prop_name);
1097 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1101 gtk_widget_thaw_child_notify (widget);
1102 g_object_unref (widget);
1103 g_object_unref (container);
1107 * gtk_container_child_set:
1108 * @container: a #GtkContainer
1109 * @child: a widget which is a child of @container
1110 * @first_prop_name: the name of the first property to set
1111 * @Varargs: a %NULL-terminated list of property names and values, starting
1112 * with @first_prop_name
1114 * Sets one or more child properties for @child and @container.
1117 gtk_container_child_set (GtkContainer *container,
1119 const gchar *first_prop_name,
1124 g_return_if_fail (GTK_IS_CONTAINER (container));
1125 g_return_if_fail (GTK_IS_WIDGET (child));
1126 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1128 va_start (var_args, first_prop_name);
1129 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1134 * gtk_container_child_get:
1135 * @container: a #GtkContainer
1136 * @child: a widget which is a child of @container
1137 * @first_prop_name: the name of the first property to get
1138 * @Varargs: return location for the first property, followed
1139 * optionally by more name/return location pairs, followed by %NULL
1141 * Gets the values of one or more child properties for @child and @container.
1144 gtk_container_child_get (GtkContainer *container,
1146 const gchar *first_prop_name,
1151 g_return_if_fail (GTK_IS_CONTAINER (container));
1152 g_return_if_fail (GTK_IS_WIDGET (child));
1153 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1155 va_start (var_args, first_prop_name);
1156 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1161 * gtk_container_class_install_child_property:
1162 * @cclass: a #GtkContainerClass
1163 * @property_id: the id for the property
1164 * @pspec: the #GParamSpec for the property
1166 * Installs a child property on a container class.
1169 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1173 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1174 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1175 if (pspec->flags & G_PARAM_WRITABLE)
1176 g_return_if_fail (cclass->set_child_property != NULL);
1177 if (pspec->flags & G_PARAM_READABLE)
1178 g_return_if_fail (cclass->get_child_property != NULL);
1179 g_return_if_fail (property_id > 0);
1180 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1181 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1182 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1184 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1186 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1187 G_OBJECT_CLASS_NAME (cclass),
1191 g_param_spec_ref (pspec);
1192 g_param_spec_sink (pspec);
1193 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1194 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1198 * gtk_container_class_find_child_property:
1199 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1200 * @property_name: the name of the child property to find
1201 * @returns: (transfer none): the #GParamSpec of the child property or
1202 * %NULL if @class has no child property with that name.
1204 * Finds a child property of a container class by name.
1207 gtk_container_class_find_child_property (GObjectClass *cclass,
1208 const gchar *property_name)
1210 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1211 g_return_val_if_fail (property_name != NULL, NULL);
1213 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1215 G_OBJECT_CLASS_TYPE (cclass),
1220 * gtk_container_class_list_child_properties:
1221 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1222 * @n_properties: location to return the number of child properties found
1223 * @returns: (array length=n_properties) (transfer container): a newly
1224 * allocated %NULL-terminated array of #GParamSpec*. The
1225 * array must be freed with g_free().
1227 * Returns all child properties of a container class.
1230 gtk_container_class_list_child_properties (GObjectClass *cclass,
1231 guint *n_properties)
1233 GParamSpec **pspecs;
1236 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1238 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1239 G_OBJECT_CLASS_TYPE (cclass),
1248 gtk_container_add_unimplemented (GtkContainer *container,
1251 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1255 gtk_container_remove_unimplemented (GtkContainer *container,
1258 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1262 gtk_container_init (GtkContainer *container)
1264 GtkContainerPrivate *priv;
1266 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1268 GtkContainerPrivate);
1269 priv = container->priv;
1271 priv->focus_child = NULL;
1272 priv->border_width = 0;
1273 priv->need_resize = FALSE;
1274 priv->resize_mode = GTK_RESIZE_PARENT;
1275 priv->reallocate_redraws = FALSE;
1279 gtk_container_destroy (GtkWidget *widget)
1281 GtkContainer *container = GTK_CONTAINER (widget);
1282 GtkContainerPrivate *priv = container->priv;
1284 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1285 _gtk_container_dequeue_resize_handler (container);
1287 if (priv->focus_child)
1289 g_object_unref (priv->focus_child);
1290 priv->focus_child = NULL;
1293 /* do this before walking child widgets, to avoid
1294 * removing children from focus chain one by one.
1296 if (priv->has_focus_chain)
1297 gtk_container_unset_focus_chain (container);
1299 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1301 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1305 gtk_container_set_property (GObject *object,
1307 const GValue *value,
1310 GtkContainer *container = GTK_CONTAINER (object);
1314 case PROP_BORDER_WIDTH:
1315 gtk_container_set_border_width (container, g_value_get_uint (value));
1317 case PROP_RESIZE_MODE:
1318 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1321 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1330 gtk_container_get_property (GObject *object,
1335 GtkContainer *container = GTK_CONTAINER (object);
1336 GtkContainerPrivate *priv = container->priv;
1340 case PROP_BORDER_WIDTH:
1341 g_value_set_uint (value, priv->border_width);
1343 case PROP_RESIZE_MODE:
1344 g_value_set_enum (value, priv->resize_mode);
1347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1353 * gtk_container_set_border_width:
1354 * @container: a #GtkContainer
1355 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1356 * the container. Valid values are in the range 0-65535 pixels.
1358 * Sets the border width of the container.
1360 * The border width of a container is the amount of space to leave
1361 * around the outside of the container. The only exception to this is
1362 * #GtkWindow; because toplevel windows can't leave space outside,
1363 * they leave the space inside. The border is added on all sides of
1364 * the container. To add space to only one side, one approach is to
1365 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1366 * to give it a size, and place it on the side of the container as
1370 gtk_container_set_border_width (GtkContainer *container,
1373 GtkContainerPrivate *priv;
1375 g_return_if_fail (GTK_IS_CONTAINER (container));
1377 priv = container->priv;
1379 if (priv->border_width != border_width)
1381 priv->border_width = border_width;
1382 g_object_notify (G_OBJECT (container), "border-width");
1384 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1385 gtk_widget_queue_resize (GTK_WIDGET (container));
1390 * gtk_container_get_border_width:
1391 * @container: a #GtkContainer
1393 * Retrieves the border width of the container. See
1394 * gtk_container_set_border_width().
1396 * Return value: the current border width
1399 gtk_container_get_border_width (GtkContainer *container)
1401 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1403 return container->priv->border_width;
1407 * gtk_container_add:
1408 * @container: a #GtkContainer
1409 * @widget: a widget to be placed inside @container
1411 * Adds @widget to @container. Typically used for simple containers
1412 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1413 * layout containers such as #GtkBox or #GtkTable, this function will
1414 * pick default packing parameters that may not be correct. So
1415 * consider functions such as gtk_box_pack_start() and
1416 * gtk_table_attach() as an alternative to gtk_container_add() in
1417 * those cases. A widget may be added to only one container at a time;
1418 * you can't place the same widget inside two different containers.
1421 gtk_container_add (GtkContainer *container,
1426 g_return_if_fail (GTK_IS_CONTAINER (container));
1427 g_return_if_fail (GTK_IS_WIDGET (widget));
1429 parent = gtk_widget_get_parent (widget);
1433 g_warning ("Attempting to add a widget with type %s to a container of "
1434 "type %s, but the widget is already inside a container of type %s, "
1435 "please use gtk_widget_reparent()" ,
1436 g_type_name (G_OBJECT_TYPE (widget)),
1437 g_type_name (G_OBJECT_TYPE (container)),
1438 g_type_name (G_OBJECT_TYPE (parent)));
1442 g_signal_emit (container, container_signals[ADD], 0, widget);
1446 * gtk_container_remove:
1447 * @container: a #GtkContainer
1448 * @widget: a current child of @container
1450 * Removes @widget from @container. @widget must be inside @container.
1451 * Note that @container will own a reference to @widget, and that this
1452 * may be the last reference held; so removing a widget from its
1453 * container can destroy that widget. If you want to use @widget
1454 * again, you need to add a reference to it while it's not inside
1455 * a container, using g_object_ref(). If you don't want to use @widget
1456 * again it's usually more efficient to simply destroy it directly
1457 * using gtk_widget_destroy() since this will remove it from the
1458 * container and help break any circular reference count cycles.
1461 gtk_container_remove (GtkContainer *container,
1464 g_return_if_fail (GTK_IS_CONTAINER (container));
1465 g_return_if_fail (GTK_IS_WIDGET (widget));
1466 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1468 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1472 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1474 g_return_if_fail (GTK_IS_CONTAINER (container));
1475 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1477 container_resize_queue = g_slist_remove (container_resize_queue, container);
1478 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1482 * gtk_container_set_resize_mode:
1483 * @container: a #GtkContainer
1484 * @resize_mode: the new resize mode
1486 * Sets the resize mode for the container.
1488 * The resize mode of a container determines whether a resize request
1489 * will be passed to the container's parent, queued for later execution
1490 * or executed immediately.
1493 gtk_container_set_resize_mode (GtkContainer *container,
1494 GtkResizeMode resize_mode)
1496 GtkContainerPrivate *priv;
1498 g_return_if_fail (GTK_IS_CONTAINER (container));
1499 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1501 priv = container->priv;
1503 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1504 resize_mode == GTK_RESIZE_PARENT)
1506 resize_mode = GTK_RESIZE_QUEUE;
1509 if (priv->resize_mode != resize_mode)
1511 priv->resize_mode = resize_mode;
1513 gtk_widget_queue_resize (GTK_WIDGET (container));
1514 g_object_notify (G_OBJECT (container), "resize-mode");
1519 * gtk_container_get_resize_mode:
1520 * @container: a #GtkContainer
1522 * Returns the resize mode for the container. See
1523 * gtk_container_set_resize_mode ().
1525 * Return value: the current resize mode
1528 gtk_container_get_resize_mode (GtkContainer *container)
1530 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1532 return container->priv->resize_mode;
1536 * gtk_container_set_reallocate_redraws:
1537 * @container: a #GtkContainer
1538 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1540 * Sets the @reallocate_redraws flag of the container to the given value.
1542 * Containers requesting reallocation redraws get automatically
1543 * redrawn if any of their children changed allocation.
1546 gtk_container_set_reallocate_redraws (GtkContainer *container,
1547 gboolean needs_redraws)
1549 g_return_if_fail (GTK_IS_CONTAINER (container));
1551 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1554 static GtkContainer*
1555 gtk_container_get_resize_container (GtkContainer *container)
1558 GtkWidget *widget = GTK_WIDGET (container);
1560 while ((parent = gtk_widget_get_parent (widget)))
1563 if (GTK_IS_RESIZE_CONTAINER (widget))
1567 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1571 gtk_container_idle_sizer (gpointer data)
1573 /* we may be invoked with a container_resize_queue of NULL, because
1574 * queue_resize could have been adding an extra idle function while
1575 * the queue still got processed. we better just ignore such case
1576 * than trying to explicitely work around them with some extra flags,
1577 * since it doesn't cause any actual harm.
1579 while (container_resize_queue)
1584 slist = container_resize_queue;
1585 container_resize_queue = slist->next;
1586 widget = slist->data;
1587 g_slist_free_1 (slist);
1589 _gtk_widget_set_resize_pending (widget, FALSE);
1590 gtk_container_check_resize (GTK_CONTAINER (widget));
1593 gdk_window_process_all_updates ();
1599 _gtk_container_queue_resize_internal (GtkContainer *container,
1600 gboolean invalidate_only)
1602 GtkContainerPrivate *priv;
1603 GtkContainer *resize_container;
1607 g_return_if_fail (GTK_IS_CONTAINER (container));
1609 priv = container->priv;
1610 widget = GTK_WIDGET (container);
1612 resize_container = gtk_container_get_resize_container (container);
1616 _gtk_widget_set_alloc_needed (widget, TRUE);
1617 _gtk_widget_set_width_request_needed (widget, TRUE);
1618 _gtk_widget_set_height_request_needed (widget, TRUE);
1620 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1621 !(parent = gtk_widget_get_parent (widget)))
1627 if (resize_container && !invalidate_only)
1629 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1630 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1631 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1633 switch (resize_container->priv->resize_mode)
1635 case GTK_RESIZE_QUEUE:
1636 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1638 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1639 if (container_resize_queue == NULL)
1640 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1641 gtk_container_idle_sizer,
1643 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1647 case GTK_RESIZE_IMMEDIATE:
1648 gtk_container_check_resize (resize_container);
1651 case GTK_RESIZE_PARENT:
1652 g_assert_not_reached ();
1658 /* we need to let hidden resize containers know that something
1659 * changed while they where hidden (currently only evaluated by
1662 resize_container->priv->need_resize = TRUE;
1668 * _gtk_container_queue_resize:
1669 * @container: a #GtkContainer
1671 * Determines the "resize container" in the hierarchy above this container
1672 * (typically the toplevel, but other containers can be set as resize
1673 * containers with gtk_container_set_resize_mode()), marks the container
1674 * and all parents up to and including the resize container as needing
1675 * to have sizes recompted, and if necessary adds the resize container
1676 * to the queue of containers that will be resized out at idle.
1679 _gtk_container_queue_resize (GtkContainer *container)
1681 _gtk_container_queue_resize_internal (container, FALSE);
1685 * _gtk_container_resize_invalidate:
1686 * @container: a #GtkContainer
1688 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1689 * actually queue the resize container for resize.
1692 _gtk_container_resize_invalidate (GtkContainer *container)
1694 _gtk_container_queue_resize_internal (container, TRUE);
1698 gtk_container_check_resize (GtkContainer *container)
1700 g_return_if_fail (GTK_IS_CONTAINER (container));
1702 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1706 gtk_container_real_check_resize (GtkContainer *container)
1708 GtkWidget *widget = GTK_WIDGET (container);
1709 GtkAllocation allocation;
1710 GtkRequisition requisition;
1712 gtk_widget_get_preferred_size (widget,
1713 &requisition, NULL);
1714 gtk_widget_get_allocation (widget, &allocation);
1716 if (requisition.width > allocation.width ||
1717 requisition.height > allocation.height)
1719 if (GTK_IS_RESIZE_CONTAINER (container))
1721 gtk_widget_size_allocate (widget, &allocation);
1722 gtk_widget_set_allocation (widget, &allocation);
1725 gtk_widget_queue_resize (widget);
1729 gtk_container_resize_children (container);
1733 /* The container hasn't changed size but one of its children
1734 * queued a resize request. Which means that the allocation
1735 * is not sufficient for the requisition of some child.
1736 * We've already performed a size request at this point,
1737 * so we simply need to reallocate and let the allocation
1738 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1741 gtk_container_resize_children (GtkContainer *container)
1743 GtkAllocation allocation;
1746 /* resizing invariants:
1747 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1748 * containers that have an idle sizer pending must be flagged with
1751 g_return_if_fail (GTK_IS_CONTAINER (container));
1753 widget = GTK_WIDGET (container);
1754 gtk_widget_get_allocation (widget, &allocation);
1756 gtk_widget_size_allocate (widget, &allocation);
1757 gtk_widget_set_allocation (widget, &allocation);
1761 gtk_container_adjust_size_request (GtkWidget *widget,
1762 GtkOrientation orientation,
1766 GtkContainer *container;
1768 container = GTK_CONTAINER (widget);
1770 if (GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1774 border_width = container->priv->border_width;
1776 *minimum_size += border_width * 2;
1777 *natural_size += border_width * 2;
1780 /* chain up last so gtk_widget_set_size_request() values
1781 * will have a chance to overwrite our border width.
1783 parent_class->adjust_size_request (widget, orientation,
1784 minimum_size, natural_size);
1788 gtk_container_adjust_size_allocation (GtkWidget *widget,
1789 GtkOrientation orientation,
1792 gint *allocated_pos,
1793 gint *allocated_size)
1795 GtkContainer *container;
1798 container = GTK_CONTAINER (widget);
1800 if (!GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1802 parent_class->adjust_size_allocation (widget, orientation,
1803 minimum_size, natural_size, allocated_pos,
1808 border_width = container->priv->border_width;
1810 *allocated_size -= border_width * 2;
1812 /* If we get a pathological too-small allocation to hold
1813 * even the border width, leave all allocation to the actual
1814 * widget, and leave x,y unchanged. (GtkWidget's min size is
1815 * 1x1 if you're wondering why <1 and not <0)
1817 * As long as we have space, set x,y properly.
1820 if (*allocated_size < 1)
1822 *allocated_size += border_width * 2;
1826 *allocated_pos += border_width;
1827 *minimum_size -= border_width * 2;
1828 *natural_size -= border_width * 2;
1831 /* Chain up to GtkWidgetClass *after* removing our border width from
1832 * the proposed allocation size. This is because it's possible that the
1833 * widget was allocated more space than it needs in a said orientation,
1834 * if GtkWidgetClass does any alignments and thus limits the size to the
1835 * natural size... then we need that to be done *after* removing any margins
1836 * and padding values.
1838 parent_class->adjust_size_allocation (widget, orientation,
1839 minimum_size, natural_size, allocated_pos,
1844 * gtk_container_class_handle_border_width:
1845 * @klass: the class struct of a #GtkContainer subclass
1847 * Modifies a subclass of #GtkContainerClass to automatically add and
1848 * remove the border-width setting on GtkContainer. This allows the
1849 * subclass to ignore the border width in its size request and
1850 * allocate methods. The intent is for a subclass to invoke this
1851 * in its class_init function.
1853 * gtk_container_class_handle_border_width() is necessary because it
1854 * would break API too badly to make this behavior the default. So
1855 * subclasses must "opt in" to the parent class handling border_width
1859 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1861 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1863 klass->_handle_border_width = TRUE;
1867 * gtk_container_forall:
1868 * @container: a #GtkContainer
1869 * @callback: (scope call): a callback
1870 * @callback_data: callback user data
1872 * Invokes @callback on each child of @container, including children
1873 * that are considered "internal" (implementation details of the
1874 * container). "Internal" children generally weren't added by the user
1875 * of the container, but were added by the container implementation
1876 * itself. Most applications should use gtk_container_foreach(),
1877 * rather than gtk_container_forall().
1880 gtk_container_forall (GtkContainer *container,
1881 GtkCallback callback,
1882 gpointer callback_data)
1884 GtkContainerClass *class;
1886 g_return_if_fail (GTK_IS_CONTAINER (container));
1887 g_return_if_fail (callback != NULL);
1889 class = GTK_CONTAINER_GET_CLASS (container);
1892 class->forall (container, TRUE, callback, callback_data);
1896 * gtk_container_foreach:
1897 * @container: a #GtkContainer
1898 * @callback: (scope call): a callback
1899 * @callback_data: callback user data
1901 * Invokes @callback on each non-internal child of @container. See
1902 * gtk_container_forall() for details on what constitutes an
1903 * "internal" child. Most applications should use
1904 * gtk_container_foreach(), rather than gtk_container_forall().
1907 gtk_container_foreach (GtkContainer *container,
1908 GtkCallback callback,
1909 gpointer callback_data)
1911 GtkContainerClass *class;
1913 g_return_if_fail (GTK_IS_CONTAINER (container));
1914 g_return_if_fail (callback != NULL);
1916 class = GTK_CONTAINER_GET_CLASS (container);
1919 class->forall (container, FALSE, callback, callback_data);
1923 * gtk_container_set_focus_child:
1924 * @container: a #GtkContainer
1925 * @child: (allow-none): a #GtkWidget, or %NULL
1927 * Sets, or unsets if @child is %NULL, the focused child of @container.
1929 * This function emits the GtkContainer::set_focus_child signal of
1930 * @container. Implementations of #GtkContainer can override the
1931 * default behaviour by overriding the class closure of this signal.
1933 * This is function is mostly meant to be used by widgets. Applications can use
1934 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1937 gtk_container_set_focus_child (GtkContainer *container,
1940 g_return_if_fail (GTK_IS_CONTAINER (container));
1942 g_return_if_fail (GTK_IS_WIDGET (child));
1944 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1948 * gtk_container_get_focus_child:
1949 * @container: a #GtkContainer
1951 * Returns the current focus child widget inside @container. This is not the
1952 * currently focused widget. That can be obtained by calling
1953 * gtk_window_get_focus().
1955 * Returns: (transfer none): The child widget which will receive the
1956 * focus inside @container when the @conatiner is focussed,
1957 * or %NULL if none is set.
1962 gtk_container_get_focus_child (GtkContainer *container)
1964 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1966 return container->priv->focus_child;
1970 * gtk_container_get_children:
1971 * @container: a #GtkContainer
1973 * Returns the container's non-internal children. See
1974 * gtk_container_forall() for details on what constitutes an "internal" child.
1976 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1979 gtk_container_get_children (GtkContainer *container)
1981 GList *children = NULL;
1983 gtk_container_foreach (container,
1984 gtk_container_children_callback,
1987 return g_list_reverse (children);
1991 gtk_container_child_position_callback (GtkWidget *widget,
1992 gpointer client_data)
1998 } *data = client_data;
2001 if (data->child == widget)
2002 data->index = data->i;
2006 gtk_container_child_default_composite_name (GtkContainer *container,
2016 /* fallback implementation */
2020 gtk_container_forall (container,
2021 gtk_container_child_position_callback,
2024 name = g_strdup_printf ("%s-%u",
2025 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2032 _gtk_container_child_composite_name (GtkContainer *container,
2035 gboolean composite_child;
2037 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2038 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2039 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2041 g_object_get (child, "composite-child", &composite_child, NULL);
2042 if (composite_child)
2044 static GQuark quark_composite_name = 0;
2047 if (!quark_composite_name)
2048 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2050 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2053 GtkContainerClass *class;
2055 class = GTK_CONTAINER_GET_CLASS (container);
2056 if (class->composite_name)
2057 name = class->composite_name (container, child);
2060 name = g_strdup (name);
2071 } ComputeExpandData;
2074 gtk_container_compute_expand_callback (GtkWidget *widget,
2075 gpointer client_data)
2077 ComputeExpandData *data = client_data;
2079 /* note that we don't get_expand on the child if we already know we
2080 * have to expand, so we only recurse into children until we find
2081 * one that expands and then we basically don't do any more
2082 * work. This means that we can leave some children in a
2083 * need_compute_expand state, which is fine, as long as GtkWidget
2084 * doesn't rely on an invariant that "if a child has
2085 * need_compute_expand, its parents also do"
2087 * gtk_widget_compute_expand() always returns FALSE if the
2088 * child is !visible so that's taken care of.
2090 data->hexpand = data->hexpand ||
2091 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2093 data->vexpand = data->vexpand ||
2094 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2098 gtk_container_compute_expand (GtkWidget *widget,
2099 gboolean *hexpand_p,
2100 gboolean *vexpand_p)
2102 ComputeExpandData data;
2104 data.hexpand = FALSE;
2105 data.vexpand = FALSE;
2107 gtk_container_forall (GTK_CONTAINER (widget),
2108 gtk_container_compute_expand_callback,
2111 *hexpand_p = data.hexpand;
2112 *vexpand_p = data.vexpand;
2116 gtk_container_real_set_focus_child (GtkContainer *container,
2119 GtkContainerPrivate *priv;
2121 g_return_if_fail (GTK_IS_CONTAINER (container));
2122 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2124 priv = container->priv;
2126 if (child != priv->focus_child)
2128 if (priv->focus_child)
2129 g_object_unref (priv->focus_child);
2130 priv->focus_child = child;
2131 if (priv->focus_child)
2132 g_object_ref (priv->focus_child);
2136 /* check for h/v adjustments
2138 if (priv->focus_child)
2140 GtkAdjustment *hadj;
2141 GtkAdjustment *vadj;
2142 GtkAllocation allocation;
2143 GtkWidget *focus_child;
2146 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2147 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2151 focus_child = priv->focus_child;
2152 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2154 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2157 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2160 gtk_widget_get_allocation (priv->focus_child, &allocation);
2164 gtk_widget_get_allocation (focus_child, &allocation);
2167 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2170 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2176 get_focus_chain (GtkContainer *container)
2178 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2181 /* same as gtk_container_get_children, except it includes internals
2184 gtk_container_get_all_children (GtkContainer *container)
2186 GList *children = NULL;
2188 gtk_container_forall (container,
2189 gtk_container_children_callback,
2195 static GtkWidgetPath *
2196 gtk_container_real_get_path_for_child (GtkContainer *container,
2199 GtkStyleContext *context;
2200 GtkWidgetPath *path;
2203 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2204 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
2206 /* Copy any permanent classes to the path */
2207 classes = gtk_style_context_list_classes (context);
2214 classes = classes->next;
2216 gtk_widget_path_iter_add_class (path, -1, cur->data);
2217 g_list_free_1 (cur);
2224 gtk_container_focus (GtkWidget *widget,
2225 GtkDirectionType direction)
2228 GList *sorted_children;
2230 GtkContainer *container;
2231 GtkContainerPrivate *priv;
2233 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2235 container = GTK_CONTAINER (widget);
2236 priv = container->priv;
2240 if (gtk_widget_get_can_focus (widget))
2242 if (!gtk_widget_has_focus (widget))
2244 gtk_widget_grab_focus (widget);
2250 /* Get a list of the containers children, allowing focus
2251 * chain to override.
2253 if (priv->has_focus_chain)
2254 children = g_list_copy (get_focus_chain (container));
2256 children = gtk_container_get_all_children (container);
2258 if (priv->has_focus_chain &&
2259 (direction == GTK_DIR_TAB_FORWARD ||
2260 direction == GTK_DIR_TAB_BACKWARD))
2262 sorted_children = g_list_copy (children);
2264 if (direction == GTK_DIR_TAB_BACKWARD)
2265 sorted_children = g_list_reverse (sorted_children);
2268 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2270 return_val = gtk_container_focus_move (container, sorted_children, direction);
2272 g_list_free (sorted_children);
2273 g_list_free (children);
2280 tab_compare (gconstpointer a,
2284 GtkAllocation child1_allocation, child2_allocation;
2285 const GtkWidget *child1 = a;
2286 const GtkWidget *child2 = b;
2287 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2290 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2291 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2293 y1 = child1_allocation.y + child1_allocation.height / 2;
2294 y2 = child2_allocation.y + child2_allocation.height / 2;
2298 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2299 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2301 if (text_direction == GTK_TEXT_DIR_RTL)
2302 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2304 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2307 return (y1 < y2) ? -1 : 1;
2311 gtk_container_focus_sort_tab (GtkContainer *container,
2313 GtkDirectionType direction,
2314 GtkWidget *old_focus)
2316 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2317 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2319 /* if we are going backwards then reverse the order
2322 if (direction == GTK_DIR_TAB_BACKWARD)
2323 children = g_list_reverse (children);
2328 /* Get coordinates of @widget's allocation with respect to
2329 * allocation of @container.
2332 get_allocation_coords (GtkContainer *container,
2334 GdkRectangle *allocation)
2336 gtk_widget_get_allocation (widget, allocation);
2338 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2339 0, 0, &allocation->x, &allocation->y);
2342 /* Look for a child in @children that is intermediate between
2343 * the focus widget and container. This widget, if it exists,
2344 * acts as the starting widget for focus navigation.
2347 find_old_focus (GtkContainer *container,
2350 GList *tmp_list = children;
2353 GtkWidget *child = tmp_list->data;
2354 GtkWidget *widget = child;
2356 while (widget && widget != (GtkWidget *)container)
2360 parent = gtk_widget_get_parent (widget);
2362 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2371 tmp_list = tmp_list->next;
2378 old_focus_coords (GtkContainer *container,
2379 GdkRectangle *old_focus_rect)
2381 GtkWidget *widget = GTK_WIDGET (container);
2382 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2383 GtkWidget *old_focus;
2385 if (GTK_IS_WINDOW (toplevel))
2387 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2389 return get_allocation_coords (container, old_focus, old_focus_rect);
2395 typedef struct _CompareInfo CompareInfo;
2399 GtkContainer *container;
2406 up_down_compare (gconstpointer a,
2410 GdkRectangle allocation1;
2411 GdkRectangle allocation2;
2412 CompareInfo *compare = data;
2415 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2416 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2418 y1 = allocation1.y + allocation1.height / 2;
2419 y2 = allocation2.y + allocation2.height / 2;
2423 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2424 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2426 if (compare->reverse)
2427 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2429 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2432 return (y1 < y2) ? -1 : 1;
2436 gtk_container_focus_sort_up_down (GtkContainer *container,
2438 GtkDirectionType direction,
2439 GtkWidget *old_focus)
2441 CompareInfo compare;
2443 GdkRectangle old_allocation;
2445 compare.container = container;
2446 compare.reverse = (direction == GTK_DIR_UP);
2449 old_focus = find_old_focus (container, children);
2451 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2457 /* Delete widgets from list that don't match minimum criteria */
2459 compare_x1 = old_allocation.x;
2460 compare_x2 = old_allocation.x + old_allocation.width;
2462 if (direction == GTK_DIR_UP)
2463 compare_y = old_allocation.y;
2465 compare_y = old_allocation.y + old_allocation.height;
2467 tmp_list = children;
2470 GtkWidget *child = tmp_list->data;
2471 GList *next = tmp_list->next;
2472 gint child_x1, child_x2;
2473 GdkRectangle child_allocation;
2475 if (child != old_focus)
2477 if (get_allocation_coords (container, child, &child_allocation))
2479 child_x1 = child_allocation.x;
2480 child_x2 = child_allocation.x + child_allocation.width;
2482 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2483 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2484 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2486 children = g_list_delete_link (children, tmp_list);
2490 children = g_list_delete_link (children, tmp_list);
2496 compare.x = (compare_x1 + compare_x2) / 2;
2497 compare.y = old_allocation.y + old_allocation.height / 2;
2501 /* No old focus widget, need to figure out starting x,y some other way
2503 GtkAllocation allocation;
2504 GtkWidget *widget = GTK_WIDGET (container);
2505 GdkRectangle old_focus_rect;
2507 gtk_widget_get_allocation (widget, &allocation);
2509 if (old_focus_coords (container, &old_focus_rect))
2511 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2515 if (!gtk_widget_get_has_window (widget))
2516 compare.x = allocation.x + allocation.width / 2;
2518 compare.x = allocation.width / 2;
2521 if (!gtk_widget_get_has_window (widget))
2522 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2524 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2527 children = g_list_sort_with_data (children, up_down_compare, &compare);
2529 if (compare.reverse)
2530 children = g_list_reverse (children);
2536 left_right_compare (gconstpointer a,
2540 GdkRectangle allocation1;
2541 GdkRectangle allocation2;
2542 CompareInfo *compare = data;
2545 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2546 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2548 x1 = allocation1.x + allocation1.width / 2;
2549 x2 = allocation2.x + allocation2.width / 2;
2553 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2554 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2556 if (compare->reverse)
2557 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2559 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2562 return (x1 < x2) ? -1 : 1;
2566 gtk_container_focus_sort_left_right (GtkContainer *container,
2568 GtkDirectionType direction,
2569 GtkWidget *old_focus)
2571 CompareInfo compare;
2573 GdkRectangle old_allocation;
2575 compare.container = container;
2576 compare.reverse = (direction == GTK_DIR_LEFT);
2579 old_focus = find_old_focus (container, children);
2581 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2587 /* Delete widgets from list that don't match minimum criteria */
2589 compare_y1 = old_allocation.y;
2590 compare_y2 = old_allocation.y + old_allocation.height;
2592 if (direction == GTK_DIR_LEFT)
2593 compare_x = old_allocation.x;
2595 compare_x = old_allocation.x + old_allocation.width;
2597 tmp_list = children;
2600 GtkWidget *child = tmp_list->data;
2601 GList *next = tmp_list->next;
2602 gint child_y1, child_y2;
2603 GdkRectangle child_allocation;
2605 if (child != old_focus)
2607 if (get_allocation_coords (container, child, &child_allocation))
2609 child_y1 = child_allocation.y;
2610 child_y2 = child_allocation.y + child_allocation.height;
2612 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2613 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2614 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2616 children = g_list_delete_link (children, tmp_list);
2620 children = g_list_delete_link (children, tmp_list);
2626 compare.y = (compare_y1 + compare_y2) / 2;
2627 compare.x = old_allocation.x + old_allocation.width / 2;
2631 /* No old focus widget, need to figure out starting x,y some other way
2633 GtkAllocation allocation;
2634 GtkWidget *widget = GTK_WIDGET (container);
2635 GdkRectangle old_focus_rect;
2637 gtk_widget_get_allocation (widget, &allocation);
2639 if (old_focus_coords (container, &old_focus_rect))
2641 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2645 if (!gtk_widget_get_has_window (widget))
2646 compare.y = allocation.y + allocation.height / 2;
2648 compare.y = allocation.height / 2;
2651 if (!gtk_widget_get_has_window (widget))
2652 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2654 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2657 children = g_list_sort_with_data (children, left_right_compare, &compare);
2659 if (compare.reverse)
2660 children = g_list_reverse (children);
2666 * gtk_container_focus_sort:
2667 * @container: a #GtkContainer
2668 * @children: a list of descendents of @container (they don't
2669 * have to be direct children)
2670 * @direction: focus direction
2671 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2672 * to determine this automatically.
2673 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2674 * which is the only @direction we use currently,
2675 * so perhaps this argument should be removed)
2677 * Sorts @children in the correct order for focusing with
2678 * direction type @direction.
2680 * Return value: a copy of @children, sorted in correct focusing order,
2681 * with children that aren't suitable for focusing in this direction
2685 _gtk_container_focus_sort (GtkContainer *container,
2687 GtkDirectionType direction,
2688 GtkWidget *old_focus)
2690 GList *visible_children = NULL;
2694 if (gtk_widget_get_realized (children->data))
2695 visible_children = g_list_prepend (visible_children, children->data);
2696 children = children->next;
2701 case GTK_DIR_TAB_FORWARD:
2702 case GTK_DIR_TAB_BACKWARD:
2703 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2706 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2709 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2712 g_assert_not_reached ();
2718 gtk_container_focus_move (GtkContainer *container,
2720 GtkDirectionType direction)
2722 GtkContainerPrivate *priv = container->priv;
2723 GtkWidget *focus_child;
2726 focus_child = priv->focus_child;
2730 child = children->data;
2731 children = children->next;
2738 if (focus_child == child)
2742 if (gtk_widget_child_focus (child, direction))
2746 else if (gtk_widget_is_drawable (child) &&
2747 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2749 if (gtk_widget_child_focus (child, direction))
2759 gtk_container_children_callback (GtkWidget *widget,
2760 gpointer client_data)
2764 children = (GList**) client_data;
2765 *children = g_list_prepend (*children, widget);
2769 chain_widget_destroyed (GtkWidget *widget,
2772 GtkContainer *container;
2775 container = GTK_CONTAINER (user_data);
2777 chain = g_object_get_data (G_OBJECT (container),
2778 "gtk-container-focus-chain");
2780 chain = g_list_remove (chain, widget);
2782 g_signal_handlers_disconnect_by_func (widget,
2783 chain_widget_destroyed,
2786 g_object_set_data (G_OBJECT (container),
2787 I_("gtk-container-focus-chain"),
2792 * gtk_container_set_focus_chain:
2793 * @container: a #GtkContainer
2794 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2795 * the new focus chain
2797 * Sets a focus chain, overriding the one computed automatically by GTK+.
2799 * In principle each widget in the chain should be a descendant of the
2800 * container, but this is not enforced by this method, since it's allowed
2801 * to set the focus chain before you pack the widgets, or have a widget
2802 * in the chain that isn't always packed. The necessary checks are done
2803 * when the focus chain is actually traversed.
2806 gtk_container_set_focus_chain (GtkContainer *container,
2807 GList *focusable_widgets)
2811 GtkContainerPrivate *priv;
2813 g_return_if_fail (GTK_IS_CONTAINER (container));
2815 priv = container->priv;
2817 if (priv->has_focus_chain)
2818 gtk_container_unset_focus_chain (container);
2820 priv->has_focus_chain = TRUE;
2823 tmp_list = focusable_widgets;
2824 while (tmp_list != NULL)
2826 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2828 /* In principle each widget in the chain should be a descendant
2829 * of the container, but we don't want to check that here, it's
2830 * expensive and also it's allowed to set the focus chain before
2831 * you pack the widgets, or have a widget in the chain that isn't
2832 * always packed. So we check for ancestor during actual traversal.
2835 chain = g_list_prepend (chain, tmp_list->data);
2837 g_signal_connect (tmp_list->data,
2839 G_CALLBACK (chain_widget_destroyed),
2842 tmp_list = g_list_next (tmp_list);
2845 chain = g_list_reverse (chain);
2847 g_object_set_data (G_OBJECT (container),
2848 I_("gtk-container-focus-chain"),
2853 * gtk_container_get_focus_chain:
2854 * @container: a #GtkContainer
2855 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2856 * to store the focus chain of the
2857 * container, or %NULL. You should free this list
2858 * using g_list_free() when you are done with it, however
2859 * no additional reference count is added to the
2860 * individual widgets in the focus chain.
2862 * Retrieves the focus chain of the container, if one has been
2863 * set explicitly. If no focus chain has been explicitly
2864 * set, GTK+ computes the focus chain based on the positions
2865 * of the children. In that case, GTK+ stores %NULL in
2866 * @focusable_widgets and returns %FALSE.
2868 * Return value: %TRUE if the focus chain of the container
2869 * has been set explicitly.
2872 gtk_container_get_focus_chain (GtkContainer *container,
2873 GList **focus_chain)
2875 GtkContainerPrivate *priv;
2877 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2879 priv = container->priv;
2883 if (priv->has_focus_chain)
2884 *focus_chain = g_list_copy (get_focus_chain (container));
2886 *focus_chain = NULL;
2889 return priv->has_focus_chain;
2893 * gtk_container_unset_focus_chain:
2894 * @container: a #GtkContainer
2896 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2899 gtk_container_unset_focus_chain (GtkContainer *container)
2901 GtkContainerPrivate *priv;
2903 g_return_if_fail (GTK_IS_CONTAINER (container));
2905 priv = container->priv;
2907 if (priv->has_focus_chain)
2912 chain = get_focus_chain (container);
2914 priv->has_focus_chain = FALSE;
2916 g_object_set_data (G_OBJECT (container),
2917 I_("gtk-container-focus-chain"),
2921 while (tmp_list != NULL)
2923 g_signal_handlers_disconnect_by_func (tmp_list->data,
2924 chain_widget_destroyed,
2927 tmp_list = g_list_next (tmp_list);
2930 g_list_free (chain);
2935 * gtk_container_set_focus_vadjustment:
2936 * @container: a #GtkContainer
2937 * @adjustment: an adjustment which should be adjusted when the focus
2938 * is moved among the descendents of @container
2940 * Hooks up an adjustment to focus handling in a container, so when a
2941 * child of the container is focused, the adjustment is scrolled to
2942 * show that widget. This function sets the vertical alignment. See
2943 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2944 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2945 * the horizontal adjustment.
2947 * The adjustments have to be in pixel units and in the same coordinate
2948 * system as the allocation for immediate children of the container.
2951 gtk_container_set_focus_vadjustment (GtkContainer *container,
2952 GtkAdjustment *adjustment)
2954 g_return_if_fail (GTK_IS_CONTAINER (container));
2956 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2959 g_object_ref (adjustment);
2961 g_object_set_qdata_full (G_OBJECT (container),
2968 * gtk_container_get_focus_vadjustment:
2969 * @container: a #GtkContainer
2971 * Retrieves the vertical focus adjustment for the container. See
2972 * gtk_container_set_focus_vadjustment().
2974 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2975 * none has been set.
2978 gtk_container_get_focus_vadjustment (GtkContainer *container)
2980 GtkAdjustment *vadjustment;
2982 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2984 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2990 * gtk_container_set_focus_hadjustment:
2991 * @container: a #GtkContainer
2992 * @adjustment: an adjustment which should be adjusted when the focus is
2993 * moved among the descendents of @container
2995 * Hooks up an adjustment to focus handling in a container, so when a child
2996 * of the container is focused, the adjustment is scrolled to show that
2997 * widget. This function sets the horizontal alignment.
2998 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2999 * the adjustment and gtk_container_set_focus_vadjustment() for setting
3000 * the vertical adjustment.
3002 * The adjustments have to be in pixel units and in the same coordinate
3003 * system as the allocation for immediate children of the container.
3006 gtk_container_set_focus_hadjustment (GtkContainer *container,
3007 GtkAdjustment *adjustment)
3009 g_return_if_fail (GTK_IS_CONTAINER (container));
3011 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3014 g_object_ref (adjustment);
3016 g_object_set_qdata_full (G_OBJECT (container),
3023 * gtk_container_get_focus_hadjustment:
3024 * @container: a #GtkContainer
3026 * Retrieves the horizontal focus adjustment for the container. See
3027 * gtk_container_set_focus_hadjustment ().
3029 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3030 * none has been set.
3033 gtk_container_get_focus_hadjustment (GtkContainer *container)
3035 GtkAdjustment *hadjustment;
3037 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3039 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3046 gtk_container_show_all (GtkWidget *widget)
3048 g_return_if_fail (GTK_IS_CONTAINER (widget));
3050 gtk_container_foreach (GTK_CONTAINER (widget),
3051 (GtkCallback) gtk_widget_show_all,
3053 gtk_widget_show (widget);
3057 gtk_container_draw_child (GtkWidget *child,
3058 gpointer client_data)
3061 GtkWidget *container;
3063 } *data = client_data;
3065 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3071 gtk_container_draw (GtkWidget *widget,
3075 GtkWidget *container;
3079 data.container = widget;
3082 gtk_container_forall (GTK_CONTAINER (widget),
3083 gtk_container_draw_child,
3090 gtk_container_map_child (GtkWidget *child,
3091 gpointer client_data)
3093 if (gtk_widget_get_visible (child) &&
3094 gtk_widget_get_child_visible (child) &&
3095 !gtk_widget_get_mapped (child))
3096 gtk_widget_map (child);
3100 gtk_container_map (GtkWidget *widget)
3102 gtk_widget_set_mapped (widget, TRUE);
3104 gtk_container_forall (GTK_CONTAINER (widget),
3105 gtk_container_map_child,
3108 if (gtk_widget_get_has_window (widget))
3109 gdk_window_show (gtk_widget_get_window (widget));
3113 gtk_container_unmap (GtkWidget *widget)
3115 gtk_widget_set_mapped (widget, FALSE);
3117 /* hide our window first so user doesn't see all the child windows
3118 * vanishing one by one. (only matters these days if one of the
3119 * children has an actual native window instead of client-side
3120 * window, e.g. a GtkSocket would)
3122 if (gtk_widget_get_has_window (widget))
3123 gdk_window_hide (gtk_widget_get_window (widget));
3125 gtk_container_forall (GTK_CONTAINER (widget),
3126 (GtkCallback)gtk_widget_unmap,
3131 * gtk_container_propagate_draw:
3132 * @container: a #GtkContainer
3133 * @child: a child of @container
3134 * @cr: Cairo context as passed to the container. If you want to use @cr
3135 * in container's draw function, consider using cairo_save() and
3136 * cairo_restore() before calling this function.
3138 * When a container receives a call to the draw function, it must send
3139 * synthetic #GtkWidget::draw calls to all children that don't have their
3140 * own #GdkWindows. This function provides a convenient way of doing this.
3141 * A container, when it receives a call to its #GtkWidget::draw function,
3142 * calls gtk_container_propagate_draw() once for each child, passing in
3143 * the @cr the container received.
3145 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3146 * and deciding whether the draw needs to be sent to the child. It is a
3147 * convenient and optimized way of getting the same effect as calling
3148 * gtk_widget_draw() on the child directly.
3150 * In most cases, a container can simply either inherit the
3151 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3152 * and then chain to the ::draw implementation from #GtkContainer.
3155 gtk_container_propagate_draw (GtkContainer *container,
3159 GdkEventExpose *event;
3160 GtkAllocation allocation;
3161 GdkWindow *window, *w;
3164 g_return_if_fail (GTK_IS_CONTAINER (container));
3165 g_return_if_fail (GTK_IS_WIDGET (child));
3166 g_return_if_fail (cr != NULL);
3168 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3170 event = _gtk_cairo_get_event (cr);
3173 if (gtk_widget_get_has_window (child) ||
3174 gtk_widget_get_window (child) != event->window)
3180 /* translate coordinates. Ugly business, that. */
3181 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3183 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3193 window = gtk_widget_get_window (GTK_WIDGET (container));
3195 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3198 gdk_window_get_position (w, &wx, &wy);
3209 if (!gtk_widget_get_has_window (child))
3211 gtk_widget_get_allocation (child, &allocation);
3216 cairo_translate (cr, x, y);
3218 _gtk_widget_draw_internal (child, cr, TRUE);
3224 _gtk_container_get_need_resize (GtkContainer *container)
3226 return container->priv->need_resize;
3230 _gtk_container_set_need_resize (GtkContainer *container,
3231 gboolean need_resize)
3233 container->priv->need_resize = need_resize;
3237 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3239 return container->priv->reallocate_redraws;
3243 * gtk_container_get_path_for_child:
3244 * @container: a #GtkContainer
3245 * @child: a child of @container
3247 * Returns a newly created widget path representing all the widget hierarchy
3248 * from the toplevel down to @child (this one not being included).
3250 * Returns: A newly created #GtkWidgetPath
3253 gtk_container_get_path_for_child (GtkContainer *container,
3256 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3257 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3258 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3260 return GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);