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 "gtkbuildable.h"
38 #include "gtkbuilderprivate.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
42 #include "gtkmarshalers.h"
43 #include "gtksizerequest.h"
44 #include "gtkwidgetprivate.h"
45 #include "gtkwindow.h"
46 #include "gtkassistant.h"
48 #include "a11y/gtkcontaineraccessible.h"
51 * SECTION:gtkcontainer
52 * @Short_description: Base class for widgets which contain other widgets
53 * @Title: GtkContainer
55 * A GTK+ user interface is constructed by nesting widgets inside widgets.
56 * Container widgets are the inner nodes in the resulting tree of widgets:
57 * they contain other widgets. So, for example, you might have a #GtkWindow
58 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
59 * of a textual label inside the frame, you might replace the #GtkLabel widget
60 * with a #GtkImage widget.
62 * There are two major kinds of container widgets in GTK+. Both are subclasses
63 * of the abstract GtkContainer base class.
65 * The first type of container widget has a single child widget and derives
66 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
67 * add some kind of functionality to the child. For example, a #GtkButton makes
68 * its child into a clickable button; a #GtkFrame draws a frame around its child
69 * and a #GtkWindow places its child widget inside a top-level window.
71 * The second type of container can have more than one child; its purpose is to
72 * manage <emphasis>layout</emphasis>. This means that these containers assign
73 * sizes and positions to their children. For example, a #GtkHBox arranges its
74 * children in a horizontal row, and a #GtkGrid arranges the widgets it contains
75 * in a two-dimensional grid.
77 * <refsect2 id="container-geometry-management">
78 * <title>Height for width geometry management</title>
80 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
81 * Height-for-width means that a widget can change how much vertical space it needs,
82 * depending on the amount of horizontal space that it is given (and similar for
85 * There are some things to keep in mind when implementing container widgets
86 * that make use of GTK+'s height for width geometry management system. First,
87 * it's important to note that a container must prioritize one of its
88 * dimensions, that is to say that a widget or container can only have a
89 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
90 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
91 * must be able to respond to the APIs for both dimensions, i.e. even if a
92 * widget has a request mode that is height-for-width, it is possible that
93 * its parent will request its sizes using the width-for-height APIs.
95 * To ensure that everything works properly, here are some guidelines to follow
96 * when implementing height-for-width (or width-for-height) containers.
98 * Each request mode involves 2 virtual methods. Height-for-width apis run
99 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
100 * When handling requests in the opposite #GtkSizeRequestMode it is important that
101 * every widget request at least enough space to display all of its content at all times.
103 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
104 * the container must return the height for its minimum width. This is easily achieved by
105 * simply calling the reverse apis implemented for itself as follows:
107 * <programlisting><![CDATA[
109 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
111 * if (i_am_in_height_for_width_mode)
115 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
116 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
117 * min_height, nat_height);
121 * ... many containers support both request modes, execute the real width-for-height
122 * request here by returning the collective heights of all widgets that are
123 * stacked vertically (or whatever is appropriate for this container) ...
126 * ]]></programlisting>
128 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
129 * that is height-for-width, it then only needs to return the base minimum width like so:
131 * <programlisting><![CDATA[
133 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
134 * gint *min_width, gint *nat_width)
136 * if (i_am_in_height_for_width_mode)
138 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
142 * ... execute the real width-for-height request here based on the required width
143 * of the children collectively if the container were to be allocated the said height ...
146 * ]]></programlisting>
148 * Height for width requests are generally implemented in terms of a virtual allocation
149 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
150 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
151 * gtk_widget_get_preferred_width() for each of its children.
153 * For each potential group of children that are lined up horizontally, the values returned by
154 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures.
155 * Any child spacing should be removed from the input @for_width and then the collective size should be
156 * allocated using the gtk_distribute_natural_allocation() convenience function.
158 * The container will then move on to request the preferred height for each child by using
159 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
161 * To allocate a height-for-width container, it's again important
162 * to consider that a container must prioritize one dimension over the other. So if
163 * a container is a height-for-width container it must first allocate all widgets horizontally
164 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
165 * extra space (if and where appropriate) for the widget to expand.
167 * After adding all the expand space, the container assumes it was allocated sufficient
168 * height to fit all of its content. At this time, the container must use the total horizontal sizes
169 * of each widget to request the height-for-width of each of its children and store the requests in a
170 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
171 * be generalized into the heights and widths of rows and columns).
172 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
173 * while this time considering the allocated height of the widget minus any vertical spacing
174 * that the container adds. Then vertical expand space should be added where appropriate and available
175 * and the container should go on to actually allocating the child widgets.
177 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
178 * to learn more about implementing height-for-width geometry management for widgets.
181 * <refsect2 id="child-properties">
182 * <title>Child properties</title>
184 * GtkContainer introduces <emphasis>child properties</emphasis>.
185 * These are object properties that are not specific
186 * to either the container or the contained widget, but rather to their relation.
187 * Typical examples of child properties are the position or pack-type of a widget
188 * which is contained in a #GtkBox.
190 * Use gtk_container_class_install_child_property() to install child properties
191 * for a container class and gtk_container_class_find_child_property() or
192 * gtk_container_class_list_child_properties() to get information about existing
195 * To set the value of a child property, use gtk_container_child_set_property(),
196 * gtk_container_child_set() or gtk_container_child_set_valist().
197 * To obtain the value of a child property, use
198 * gtk_container_child_get_property(), gtk_container_child_get() or
199 * gtk_container_child_get_valist(). To emit notification about child property
200 * changes, use gtk_widget_child_notify().
203 * <refsect2 id="GtkContainer-BUILDER-UI">
204 * <title>GtkContainer as GtkBuildable</title>
206 * The GtkContainer implementation of the GtkBuildable interface
207 * supports a <packing> element for children, which can
208 * contain multiple <property> elements that specify
209 * child properties for the child.
211 * <title>Child properties in UI definitions</title>
212 * <programlisting><![CDATA[
213 * <object class="GtkVBox">
215 * <object class="GtkLabel"/>
217 * <property name="pack-type">start</property>
221 * ]]></programlisting>
223 * Since 2.16, child properties can also be marked as translatable using
224 * the same "translatable", "comments" and "context" attributes that are used
225 * for regular properties.
231 struct _GtkContainerPrivate
233 GtkWidget *focus_child;
235 guint border_width : 16;
237 guint has_focus_chain : 1;
238 guint need_resize : 1;
239 guint reallocate_redraws : 1;
240 guint resize_mode : 2;
241 guint request_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);
310 static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget *widget);
312 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
315 static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
319 static void gtk_container_buildable_init (GtkBuildableIface *iface);
320 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
324 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
327 const gchar *tagname,
328 GMarkupParser *parser,
330 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
333 const gchar *tagname,
337 /* --- variables --- */
338 static const gchar vadjustment_key[] = "gtk-vadjustment";
339 static guint vadjustment_key_id = 0;
340 static const gchar hadjustment_key[] = "gtk-hadjustment";
341 static guint hadjustment_key_id = 0;
342 static GSList *container_resize_queue = NULL;
343 static guint container_signals[LAST_SIGNAL] = { 0 };
344 static GtkWidgetClass *parent_class = NULL;
345 extern GParamSpecPool *_gtk_widget_child_property_pool;
346 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
347 static GtkBuildableIface *parent_buildable_iface;
350 /* --- functions --- */
352 gtk_container_get_type (void)
354 static GType container_type = 0;
358 const GTypeInfo container_info =
360 sizeof (GtkContainerClass),
361 (GBaseInitFunc) gtk_container_base_class_init,
362 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
363 (GClassInitFunc) gtk_container_class_init,
364 NULL /* class_finalize */,
365 NULL /* class_data */,
366 sizeof (GtkContainer),
368 (GInstanceInitFunc) gtk_container_init,
369 NULL, /* value_table */
372 const GInterfaceInfo buildable_info =
374 (GInterfaceInitFunc) gtk_container_buildable_init,
380 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
381 &container_info, G_TYPE_FLAG_ABSTRACT);
383 g_type_add_interface_static (container_type,
389 return container_type;
393 gtk_container_base_class_init (GtkContainerClass *class)
395 /* reset instance specifc class fields that don't get inherited */
396 class->set_child_property = NULL;
397 class->get_child_property = NULL;
401 gtk_container_base_class_finalize (GtkContainerClass *class)
405 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
406 for (node = list; node; node = node->next)
408 GParamSpec *pspec = node->data;
410 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
411 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
412 g_param_spec_unref (pspec);
418 gtk_container_class_init (GtkContainerClass *class)
420 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
421 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
423 parent_class = g_type_class_peek_parent (class);
425 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
426 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
428 gobject_class->set_property = gtk_container_set_property;
429 gobject_class->get_property = gtk_container_get_property;
431 widget_class->destroy = gtk_container_destroy;
432 widget_class->compute_expand = gtk_container_compute_expand;
433 widget_class->show_all = gtk_container_show_all;
434 widget_class->draw = gtk_container_draw;
435 widget_class->map = gtk_container_map;
436 widget_class->unmap = gtk_container_unmap;
437 widget_class->focus = gtk_container_focus;
439 widget_class->adjust_size_request = gtk_container_adjust_size_request;
440 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
441 widget_class->get_request_mode = gtk_container_get_request_mode;
443 class->add = gtk_container_add_unimplemented;
444 class->remove = gtk_container_remove_unimplemented;
445 class->check_resize = gtk_container_real_check_resize;
446 class->forall = NULL;
447 class->set_focus_child = gtk_container_real_set_focus_child;
448 class->child_type = NULL;
449 class->composite_name = gtk_container_child_default_composite_name;
450 class->get_path_for_child = gtk_container_real_get_path_for_child;
452 g_object_class_install_property (gobject_class,
454 g_param_spec_enum ("resize-mode",
456 P_("Specify how resize events are handled"),
457 GTK_TYPE_RESIZE_MODE,
459 GTK_PARAM_READWRITE));
460 g_object_class_install_property (gobject_class,
462 g_param_spec_uint ("border-width",
464 P_("The width of the empty border outside the containers children"),
468 GTK_PARAM_READWRITE));
469 g_object_class_install_property (gobject_class,
471 g_param_spec_object ("child",
473 P_("Can be used to add a new child to the container"),
475 GTK_PARAM_WRITABLE));
476 container_signals[ADD] =
477 g_signal_new (I_("add"),
478 G_OBJECT_CLASS_TYPE (gobject_class),
480 G_STRUCT_OFFSET (GtkContainerClass, add),
482 _gtk_marshal_VOID__OBJECT,
485 container_signals[REMOVE] =
486 g_signal_new (I_("remove"),
487 G_OBJECT_CLASS_TYPE (gobject_class),
489 G_STRUCT_OFFSET (GtkContainerClass, remove),
491 _gtk_marshal_VOID__OBJECT,
494 container_signals[CHECK_RESIZE] =
495 g_signal_new (I_("check-resize"),
496 G_OBJECT_CLASS_TYPE (gobject_class),
498 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
500 _gtk_marshal_VOID__VOID,
502 container_signals[SET_FOCUS_CHILD] =
503 g_signal_new (I_("set-focus-child"),
504 G_OBJECT_CLASS_TYPE (gobject_class),
506 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
508 _gtk_marshal_VOID__OBJECT,
512 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
514 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE);
518 gtk_container_buildable_init (GtkBuildableIface *iface)
520 parent_buildable_iface = g_type_interface_peek_parent (iface);
521 iface->add_child = gtk_container_buildable_add_child;
522 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
523 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
527 gtk_container_buildable_add_child (GtkBuildable *buildable,
534 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
536 else if (GTK_IS_WIDGET (child) &&
537 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
539 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
542 g_warning ("Cannot add an object of type %s to a container of type %s",
543 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
547 gtk_container_buildable_set_child_property (GtkContainer *container,
554 GValue gvalue = G_VALUE_INIT;
555 GError *error = NULL;
557 pspec = gtk_container_class_find_child_property
558 (G_OBJECT_GET_CLASS (container), name);
561 g_warning ("%s does not have a property called %s",
562 g_type_name (G_OBJECT_TYPE (container)), name);
566 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
568 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
569 g_type_name (G_OBJECT_TYPE (container)),
572 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
574 g_error_free (error);
578 gtk_container_child_set_property (container, child, name, &gvalue);
579 g_value_unset (&gvalue);
584 GtkContainer *container;
587 gchar *child_prop_name;
589 gboolean translatable;
590 } PackingPropertiesData;
593 attributes_start_element (GMarkupParseContext *context,
594 const gchar *element_name,
596 const gchar **values,
600 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
603 if (strcmp (element_name, "property") == 0)
605 for (i = 0; names[i]; i++)
606 if (strcmp (names[i], "name") == 0)
607 parser_data->child_prop_name = g_strdup (values[i]);
608 else if (strcmp (names[i], "translatable") == 0)
610 if (!_gtk_builder_boolean_from_string (values[1],
611 &parser_data->translatable,
615 else if (strcmp (names[i], "comments") == 0)
616 ; /* for translators */
617 else if (strcmp (names[i], "context") == 0)
618 parser_data->context = g_strdup (values[1]);
620 g_warning ("Unsupported attribute for GtkContainer Child "
621 "property: %s\n", names[i]);
623 else if (strcmp (element_name, "packing") == 0)
626 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
630 attributes_text_element (GMarkupParseContext *context,
636 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
638 if (parser_data->child_prop_name)
639 g_string_append_len (parser_data->string, text, text_len);
643 attributes_end_element (GMarkupParseContext *context,
644 const gchar *element_name,
648 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
650 /* translate the string */
651 if (parser_data->string->len && parser_data->translatable)
656 domain = gtk_builder_get_translation_domain (parser_data->builder);
658 translated = _gtk_builder_parser_translate (domain,
659 parser_data->context,
660 parser_data->string->str);
661 g_string_set_size (parser_data->string, 0);
662 g_string_append (parser_data->string, translated);
665 if (parser_data->child_prop_name)
666 gtk_container_buildable_set_child_property (parser_data->container,
667 parser_data->builder,
669 parser_data->child_prop_name,
670 parser_data->string->str);
672 g_string_set_size (parser_data->string, 0);
673 g_free (parser_data->child_prop_name);
674 g_free (parser_data->context);
675 parser_data->child_prop_name = NULL;
676 parser_data->context = NULL;
677 parser_data->translatable = FALSE;
680 static const GMarkupParser attributes_parser =
682 attributes_start_element,
683 attributes_end_element,
684 attributes_text_element,
688 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
691 const gchar *tagname,
692 GMarkupParser *parser,
695 PackingPropertiesData *parser_data;
697 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
698 tagname, parser, data))
701 if (child && strcmp (tagname, "packing") == 0)
703 parser_data = g_slice_new0 (PackingPropertiesData);
704 parser_data->string = g_string_new ("");
705 parser_data->builder = builder;
706 parser_data->container = GTK_CONTAINER (buildable);
707 parser_data->child = GTK_WIDGET (child);
708 parser_data->child_prop_name = NULL;
710 *parser = attributes_parser;
719 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
722 const gchar *tagname,
725 if (strcmp (tagname, "packing") == 0)
727 PackingPropertiesData *parser_data = (PackingPropertiesData*)data;
728 g_string_free (parser_data->string, TRUE);
729 g_slice_free (PackingPropertiesData, parser_data);
733 if (parent_buildable_iface->custom_tag_end)
734 parent_buildable_iface->custom_tag_end (buildable, builder,
735 child, tagname, data);
739 * gtk_container_child_type:
740 * @container: a #GtkContainer
742 * Returns the type of the children supported by the container.
744 * Note that this may return %G_TYPE_NONE to indicate that no more
745 * children can be added, e.g. for a #GtkPaned which already has two
748 * Return value: a #GType.
751 gtk_container_child_type (GtkContainer *container)
754 GtkContainerClass *class;
756 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
758 class = GTK_CONTAINER_GET_CLASS (container);
759 if (class->child_type)
760 slot = class->child_type (container);
767 /* --- GtkContainer child property mechanism --- */
770 * gtk_container_child_notify:
771 * @container: the #GtkContainer
772 * @child: the child widget
773 * @child_property: the name of a child property installed on
774 * the class of @container
776 * Emits a #GtkWidget::child-notify signal for the
777 * <link linkend="child-properties">child property</link>
778 * @child_property on widget.
780 * This is an analogue of g_object_notify() for child properties.
782 * Also see gtk_widget_child_notify().
787 gtk_container_child_notify (GtkContainer *container,
789 const gchar *child_property)
794 g_return_if_fail (GTK_IS_CONTAINER (container));
795 g_return_if_fail (GTK_IS_WIDGET (child));
796 g_return_if_fail (child_property != NULL);
798 obj = G_OBJECT (child);
800 if (obj->ref_count == 0)
805 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
807 G_OBJECT_TYPE (container),
812 g_warning ("%s: container class `%s' has no child property named `%s'",
814 G_OBJECT_TYPE_NAME (container),
819 GObjectNotifyQueue *nqueue;
821 nqueue = g_object_notify_queue_freeze (obj, _gtk_widget_child_property_notify_context);
823 g_object_notify_queue_add (obj, nqueue, pspec);
824 g_object_notify_queue_thaw (obj, nqueue);
827 g_object_unref (obj);
831 container_get_child_property (GtkContainer *container,
836 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
838 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
842 container_set_child_property (GtkContainer *container,
846 GObjectNotifyQueue *nqueue)
848 GValue tmp_value = G_VALUE_INIT;
849 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
851 /* provide a copy to work from, convert (if necessary) and validate */
852 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
853 if (!g_value_transform (value, &tmp_value))
854 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
856 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
857 G_VALUE_TYPE_NAME (value));
858 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
860 gchar *contents = g_strdup_value_contents (value);
862 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
864 G_VALUE_TYPE_NAME (value),
866 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
871 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
872 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
874 g_value_unset (&tmp_value);
878 * gtk_container_child_get_valist:
879 * @container: a #GtkContainer
880 * @child: a widget which is a child of @container
881 * @first_property_name: the name of the first property to get
882 * @var_args: return location for the first property, followed
883 * optionally by more name/return location pairs, followed by %NULL
885 * Gets the values of one or more child properties for @child and @container.
888 gtk_container_child_get_valist (GtkContainer *container,
890 const gchar *first_property_name,
895 g_return_if_fail (GTK_IS_CONTAINER (container));
896 g_return_if_fail (GTK_IS_WIDGET (child));
898 g_object_ref (container);
899 g_object_ref (child);
901 name = first_property_name;
904 GValue value = G_VALUE_INIT;
908 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
910 G_OBJECT_TYPE (container),
914 g_warning ("%s: container class `%s' has no child property named `%s'",
916 G_OBJECT_TYPE_NAME (container),
920 if (!(pspec->flags & G_PARAM_READABLE))
922 g_warning ("%s: child property `%s' of container class `%s' is not readable",
925 G_OBJECT_TYPE_NAME (container));
928 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
929 container_get_child_property (container, child, pspec, &value);
930 G_VALUE_LCOPY (&value, var_args, 0, &error);
933 g_warning ("%s: %s", G_STRLOC, error);
935 g_value_unset (&value);
938 g_value_unset (&value);
939 name = va_arg (var_args, gchar*);
942 g_object_unref (child);
943 g_object_unref (container);
947 * gtk_container_child_get_property:
948 * @container: a #GtkContainer
949 * @child: a widget which is a child of @container
950 * @property_name: the name of the property to get
951 * @value: a location to return the value
953 * Gets the value of a child property for @child and @container.
956 gtk_container_child_get_property (GtkContainer *container,
958 const gchar *property_name,
963 g_return_if_fail (GTK_IS_CONTAINER (container));
964 g_return_if_fail (GTK_IS_WIDGET (child));
965 g_return_if_fail (property_name != NULL);
966 g_return_if_fail (G_IS_VALUE (value));
968 g_object_ref (container);
969 g_object_ref (child);
970 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
971 G_OBJECT_TYPE (container), TRUE);
973 g_warning ("%s: container class `%s' has no child property named `%s'",
975 G_OBJECT_TYPE_NAME (container),
977 else if (!(pspec->flags & G_PARAM_READABLE))
978 g_warning ("%s: child property `%s' of container class `%s' is not readable",
981 G_OBJECT_TYPE_NAME (container));
984 GValue *prop_value, tmp_value = G_VALUE_INIT;
986 /* auto-conversion of the callers value type
988 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
990 g_value_reset (value);
993 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
995 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
997 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
998 G_VALUE_TYPE_NAME (value));
999 g_object_unref (child);
1000 g_object_unref (container);
1005 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1006 prop_value = &tmp_value;
1008 container_get_child_property (container, child, pspec, prop_value);
1009 if (prop_value != value)
1011 g_value_transform (prop_value, value);
1012 g_value_unset (&tmp_value);
1015 g_object_unref (child);
1016 g_object_unref (container);
1020 * gtk_container_child_set_valist:
1021 * @container: a #GtkContainer
1022 * @child: a widget which is a child of @container
1023 * @first_property_name: the name of the first property to set
1024 * @var_args: a %NULL-terminated list of property names and values, starting
1025 * with @first_prop_name
1027 * Sets one or more child properties for @child and @container.
1030 gtk_container_child_set_valist (GtkContainer *container,
1032 const gchar *first_property_name,
1035 GObjectNotifyQueue *nqueue;
1038 g_return_if_fail (GTK_IS_CONTAINER (container));
1039 g_return_if_fail (GTK_IS_WIDGET (child));
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 name = first_property_name;
1048 GValue value = G_VALUE_INIT;
1049 gchar *error = NULL;
1050 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1052 G_OBJECT_TYPE (container),
1056 g_warning ("%s: container class `%s' has no child property named `%s'",
1058 G_OBJECT_TYPE_NAME (container),
1062 if (!(pspec->flags & G_PARAM_WRITABLE))
1064 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1067 G_OBJECT_TYPE_NAME (container));
1071 G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
1072 var_args, 0, &error);
1075 g_warning ("%s: %s", G_STRLOC, error);
1078 /* we purposely leak the value here, it might not be
1079 * in a sane state if an error condition occoured
1083 container_set_child_property (container, child, pspec, &value, nqueue);
1084 g_value_unset (&value);
1085 name = va_arg (var_args, gchar*);
1087 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1089 g_object_unref (container);
1090 g_object_unref (child);
1094 * gtk_container_child_set_property:
1095 * @container: a #GtkContainer
1096 * @child: a widget which is a child of @container
1097 * @property_name: the name of the property to set
1098 * @value: the value to set the property to
1100 * Sets a child property for @child and @container.
1103 gtk_container_child_set_property (GtkContainer *container,
1105 const gchar *property_name,
1106 const GValue *value)
1108 GObjectNotifyQueue *nqueue;
1111 g_return_if_fail (GTK_IS_CONTAINER (container));
1112 g_return_if_fail (GTK_IS_WIDGET (child));
1113 g_return_if_fail (property_name != NULL);
1114 g_return_if_fail (G_IS_VALUE (value));
1116 g_object_ref (container);
1117 g_object_ref (child);
1119 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1120 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1121 G_OBJECT_TYPE (container), TRUE);
1123 g_warning ("%s: container class `%s' has no child property named `%s'",
1125 G_OBJECT_TYPE_NAME (container),
1127 else if (!(pspec->flags & G_PARAM_WRITABLE))
1128 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1131 G_OBJECT_TYPE_NAME (container));
1134 container_set_child_property (container, child, pspec, value, nqueue);
1136 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1137 g_object_unref (container);
1138 g_object_unref (child);
1142 * gtk_container_add_with_properties:
1143 * @container: a #GtkContainer
1144 * @widget: a widget to be placed inside @container
1145 * @first_prop_name: the name of the first child property to set
1146 * @...: a %NULL-terminated list of property names and values, starting
1147 * with @first_prop_name
1149 * Adds @widget to @container, setting child properties at the same time.
1150 * See gtk_container_add() and gtk_container_child_set() for more details.
1153 gtk_container_add_with_properties (GtkContainer *container,
1155 const gchar *first_prop_name,
1158 g_return_if_fail (GTK_IS_CONTAINER (container));
1159 g_return_if_fail (GTK_IS_WIDGET (widget));
1160 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1162 g_object_ref (container);
1163 g_object_ref (widget);
1164 gtk_widget_freeze_child_notify (widget);
1166 g_signal_emit (container, container_signals[ADD], 0, widget);
1167 if (gtk_widget_get_parent (widget))
1171 va_start (var_args, first_prop_name);
1172 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1176 gtk_widget_thaw_child_notify (widget);
1177 g_object_unref (widget);
1178 g_object_unref (container);
1182 * gtk_container_child_set:
1183 * @container: a #GtkContainer
1184 * @child: a widget which is a child of @container
1185 * @first_prop_name: the name of the first property to set
1186 * @...: a %NULL-terminated list of property names and values, starting
1187 * with @first_prop_name
1189 * Sets one or more child properties for @child and @container.
1192 gtk_container_child_set (GtkContainer *container,
1194 const gchar *first_prop_name,
1199 va_start (var_args, first_prop_name);
1200 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1205 * gtk_container_child_get:
1206 * @container: a #GtkContainer
1207 * @child: a widget which is a child of @container
1208 * @first_prop_name: the name of the first property to get
1209 * @...: return location for the first property, followed
1210 * optionally by more name/return location pairs, followed by %NULL
1212 * Gets the values of one or more child properties for @child and @container.
1215 gtk_container_child_get (GtkContainer *container,
1217 const gchar *first_prop_name,
1222 va_start (var_args, first_prop_name);
1223 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1228 * gtk_container_class_install_child_property:
1229 * @cclass: a #GtkContainerClass
1230 * @property_id: the id for the property
1231 * @pspec: the #GParamSpec for the property
1233 * Installs a child property on a container class.
1236 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1240 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1241 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1242 if (pspec->flags & G_PARAM_WRITABLE)
1243 g_return_if_fail (cclass->set_child_property != NULL);
1244 if (pspec->flags & G_PARAM_READABLE)
1245 g_return_if_fail (cclass->get_child_property != NULL);
1246 g_return_if_fail (property_id > 0);
1247 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1248 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1249 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1251 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1253 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1254 G_OBJECT_CLASS_NAME (cclass),
1258 g_param_spec_ref (pspec);
1259 g_param_spec_sink (pspec);
1260 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1261 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1265 * gtk_container_class_find_child_property:
1266 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1267 * @property_name: the name of the child property to find
1269 * Finds a child property of a container class by name.
1271 * Returns: (transfer none): the #GParamSpec of the child property
1272 * or %NULL if @class has no child property with that name.
1275 gtk_container_class_find_child_property (GObjectClass *cclass,
1276 const gchar *property_name)
1278 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1279 g_return_val_if_fail (property_name != NULL, NULL);
1281 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1283 G_OBJECT_CLASS_TYPE (cclass),
1288 * gtk_container_class_list_child_properties:
1289 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1290 * @n_properties: location to return the number of child properties found
1292 * Returns all child properties of a container class.
1294 * Returns: (array length=n_properties) (transfer container):
1295 * a newly allocated %NULL-terminated array of #GParamSpec*.
1296 * The array must be freed with g_free().
1299 gtk_container_class_list_child_properties (GObjectClass *cclass,
1300 guint *n_properties)
1302 GParamSpec **pspecs;
1305 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1307 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1308 G_OBJECT_CLASS_TYPE (cclass),
1317 gtk_container_add_unimplemented (GtkContainer *container,
1320 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1324 gtk_container_remove_unimplemented (GtkContainer *container,
1327 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1331 gtk_container_init (GtkContainer *container)
1333 GtkContainerPrivate *priv;
1335 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1337 GtkContainerPrivate);
1338 priv = container->priv;
1340 priv->focus_child = NULL;
1341 priv->border_width = 0;
1342 priv->need_resize = FALSE;
1343 priv->resize_mode = GTK_RESIZE_PARENT;
1344 priv->reallocate_redraws = FALSE;
1348 gtk_container_destroy (GtkWidget *widget)
1350 GtkContainer *container = GTK_CONTAINER (widget);
1351 GtkContainerPrivate *priv = container->priv;
1353 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1354 _gtk_container_dequeue_resize_handler (container);
1356 if (priv->focus_child)
1358 g_object_unref (priv->focus_child);
1359 priv->focus_child = NULL;
1362 /* do this before walking child widgets, to avoid
1363 * removing children from focus chain one by one.
1365 if (priv->has_focus_chain)
1366 gtk_container_unset_focus_chain (container);
1368 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1370 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1374 gtk_container_set_property (GObject *object,
1376 const GValue *value,
1379 GtkContainer *container = GTK_CONTAINER (object);
1383 case PROP_BORDER_WIDTH:
1384 gtk_container_set_border_width (container, g_value_get_uint (value));
1386 case PROP_RESIZE_MODE:
1387 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1390 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1399 gtk_container_get_property (GObject *object,
1404 GtkContainer *container = GTK_CONTAINER (object);
1405 GtkContainerPrivate *priv = container->priv;
1409 case PROP_BORDER_WIDTH:
1410 g_value_set_uint (value, priv->border_width);
1412 case PROP_RESIZE_MODE:
1413 g_value_set_enum (value, priv->resize_mode);
1416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1422 * gtk_container_set_border_width:
1423 * @container: a #GtkContainer
1424 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1425 * the container. Valid values are in the range 0-65535 pixels.
1427 * Sets the border width of the container.
1429 * The border width of a container is the amount of space to leave
1430 * around the outside of the container. The only exception to this is
1431 * #GtkWindow; because toplevel windows can't leave space outside,
1432 * they leave the space inside. The border is added on all sides of
1433 * the container. To add space to only one side, one approach is to
1434 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1435 * to give it a size, and place it on the side of the container as
1439 gtk_container_set_border_width (GtkContainer *container,
1442 GtkContainerPrivate *priv;
1444 g_return_if_fail (GTK_IS_CONTAINER (container));
1446 priv = container->priv;
1448 if (priv->border_width != border_width)
1450 priv->border_width = border_width;
1451 g_object_notify (G_OBJECT (container), "border-width");
1453 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1454 gtk_widget_queue_resize (GTK_WIDGET (container));
1459 * gtk_container_get_border_width:
1460 * @container: a #GtkContainer
1462 * Retrieves the border width of the container. See
1463 * gtk_container_set_border_width().
1465 * Return value: the current border width
1468 gtk_container_get_border_width (GtkContainer *container)
1470 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1472 return container->priv->border_width;
1476 * gtk_container_add:
1477 * @container: a #GtkContainer
1478 * @widget: a widget to be placed inside @container
1480 * Adds @widget to @container. Typically used for simple containers
1481 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1482 * layout containers such as #GtkBox or #GtkGrid, this function will
1483 * pick default packing parameters that may not be correct. So
1484 * consider functions such as gtk_box_pack_start() and
1485 * gtk_grid_attach() as an alternative to gtk_container_add() in
1486 * those cases. A widget may be added to only one container at a time;
1487 * you can't place the same widget inside two different containers.
1490 gtk_container_add (GtkContainer *container,
1495 g_return_if_fail (GTK_IS_CONTAINER (container));
1496 g_return_if_fail (GTK_IS_WIDGET (widget));
1498 parent = gtk_widget_get_parent (widget);
1502 g_warning ("Attempting to add a widget with type %s to a container of "
1503 "type %s, but the widget is already inside a container of type %s, "
1504 "please use gtk_widget_reparent()" ,
1505 g_type_name (G_OBJECT_TYPE (widget)),
1506 g_type_name (G_OBJECT_TYPE (container)),
1507 g_type_name (G_OBJECT_TYPE (parent)));
1511 g_signal_emit (container, container_signals[ADD], 0, widget);
1515 * gtk_container_remove:
1516 * @container: a #GtkContainer
1517 * @widget: a current child of @container
1519 * Removes @widget from @container. @widget must be inside @container.
1520 * Note that @container will own a reference to @widget, and that this
1521 * may be the last reference held; so removing a widget from its
1522 * container can destroy that widget. If you want to use @widget
1523 * again, you need to add a reference to it while it's not inside
1524 * a container, using g_object_ref(). If you don't want to use @widget
1525 * again it's usually more efficient to simply destroy it directly
1526 * using gtk_widget_destroy() since this will remove it from the
1527 * container and help break any circular reference count cycles.
1530 gtk_container_remove (GtkContainer *container,
1533 g_return_if_fail (GTK_IS_CONTAINER (container));
1534 g_return_if_fail (GTK_IS_WIDGET (widget));
1535 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container) || GTK_IS_ASSISTANT (container));
1537 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1541 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1543 g_return_if_fail (GTK_IS_CONTAINER (container));
1544 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1546 container_resize_queue = g_slist_remove (container_resize_queue, container);
1547 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1551 * gtk_container_set_resize_mode:
1552 * @container: a #GtkContainer
1553 * @resize_mode: the new resize mode
1555 * Sets the resize mode for the container.
1557 * The resize mode of a container determines whether a resize request
1558 * will be passed to the container's parent, queued for later execution
1559 * or executed immediately.
1562 gtk_container_set_resize_mode (GtkContainer *container,
1563 GtkResizeMode resize_mode)
1565 GtkContainerPrivate *priv;
1567 g_return_if_fail (GTK_IS_CONTAINER (container));
1568 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1570 priv = container->priv;
1572 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1573 resize_mode == GTK_RESIZE_PARENT)
1575 resize_mode = GTK_RESIZE_QUEUE;
1578 if (priv->resize_mode != resize_mode)
1580 priv->resize_mode = resize_mode;
1582 gtk_widget_queue_resize (GTK_WIDGET (container));
1583 g_object_notify (G_OBJECT (container), "resize-mode");
1588 * gtk_container_get_resize_mode:
1589 * @container: a #GtkContainer
1591 * Returns the resize mode for the container. See
1592 * gtk_container_set_resize_mode ().
1594 * Return value: the current resize mode
1597 gtk_container_get_resize_mode (GtkContainer *container)
1599 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1601 return container->priv->resize_mode;
1605 * gtk_container_set_reallocate_redraws:
1606 * @container: a #GtkContainer
1607 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1609 * Sets the @reallocate_redraws flag of the container to the given value.
1611 * Containers requesting reallocation redraws get automatically
1612 * redrawn if any of their children changed allocation.
1615 gtk_container_set_reallocate_redraws (GtkContainer *container,
1616 gboolean needs_redraws)
1618 g_return_if_fail (GTK_IS_CONTAINER (container));
1620 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1623 static GtkContainer*
1624 gtk_container_get_resize_container (GtkContainer *container)
1627 GtkWidget *widget = GTK_WIDGET (container);
1629 while ((parent = gtk_widget_get_parent (widget)))
1632 if (GTK_IS_RESIZE_CONTAINER (widget))
1636 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1640 gtk_container_idle_sizer (gpointer data)
1642 /* we may be invoked with a container_resize_queue of NULL, because
1643 * queue_resize could have been adding an extra idle function while
1644 * the queue still got processed. we better just ignore such case
1645 * than trying to explicitely work around them with some extra flags,
1646 * since it doesn't cause any actual harm.
1648 while (container_resize_queue)
1653 slist = container_resize_queue;
1654 container_resize_queue = slist->next;
1655 widget = slist->data;
1656 g_slist_free_1 (slist);
1658 _gtk_widget_set_resize_pending (widget, FALSE);
1659 gtk_container_check_resize (GTK_CONTAINER (widget));
1662 gdk_window_process_all_updates ();
1668 _gtk_container_queue_resize_internal (GtkContainer *container,
1669 gboolean invalidate_only)
1671 GtkContainer *resize_container;
1675 g_return_if_fail (GTK_IS_CONTAINER (container));
1677 widget = GTK_WIDGET (container);
1679 resize_container = gtk_container_get_resize_container (container);
1683 _gtk_widget_set_alloc_needed (widget, TRUE);
1684 _gtk_widget_set_width_request_needed (widget, TRUE);
1685 _gtk_widget_set_height_request_needed (widget, TRUE);
1687 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1688 !(parent = gtk_widget_get_parent (widget)))
1694 if (resize_container && !invalidate_only)
1696 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1697 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1698 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1700 switch (resize_container->priv->resize_mode)
1702 case GTK_RESIZE_QUEUE:
1703 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1705 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1706 if (container_resize_queue == NULL)
1707 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1708 gtk_container_idle_sizer,
1710 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1714 case GTK_RESIZE_IMMEDIATE:
1715 gtk_container_check_resize (resize_container);
1718 case GTK_RESIZE_PARENT:
1719 g_assert_not_reached ();
1725 /* we need to let hidden resize containers know that something
1726 * changed while they where hidden (currently only evaluated by
1729 resize_container->priv->need_resize = TRUE;
1735 * _gtk_container_queue_resize:
1736 * @container: a #GtkContainer
1738 * Determines the "resize container" in the hierarchy above this container
1739 * (typically the toplevel, but other containers can be set as resize
1740 * containers with gtk_container_set_resize_mode()), marks the container
1741 * and all parents up to and including the resize container as needing
1742 * to have sizes recompted, and if necessary adds the resize container
1743 * to the queue of containers that will be resized out at idle.
1746 _gtk_container_queue_resize (GtkContainer *container)
1748 _gtk_container_queue_resize_internal (container, FALSE);
1752 * _gtk_container_resize_invalidate:
1753 * @container: a #GtkContainer
1755 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1756 * actually queue the resize container for resize.
1759 _gtk_container_resize_invalidate (GtkContainer *container)
1761 _gtk_container_queue_resize_internal (container, TRUE);
1765 gtk_container_check_resize (GtkContainer *container)
1767 g_return_if_fail (GTK_IS_CONTAINER (container));
1769 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1773 gtk_container_real_check_resize (GtkContainer *container)
1775 GtkWidget *widget = GTK_WIDGET (container);
1776 GtkAllocation allocation;
1777 GtkRequisition requisition;
1779 gtk_widget_get_preferred_size (widget,
1780 &requisition, NULL);
1781 gtk_widget_get_allocation (widget, &allocation);
1783 if (requisition.width > allocation.width ||
1784 requisition.height > allocation.height)
1786 if (GTK_IS_RESIZE_CONTAINER (container))
1788 gtk_widget_size_allocate (widget, &allocation);
1789 gtk_widget_set_allocation (widget, &allocation);
1792 gtk_widget_queue_resize (widget);
1796 gtk_container_resize_children (container);
1800 /* The container hasn't changed size but one of its children
1801 * queued a resize request. Which means that the allocation
1802 * is not sufficient for the requisition of some child.
1803 * We've already performed a size request at this point,
1804 * so we simply need to reallocate and let the allocation
1805 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1808 gtk_container_resize_children (GtkContainer *container)
1810 GtkAllocation allocation;
1813 /* resizing invariants:
1814 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1815 * containers that have an idle sizer pending must be flagged with
1818 g_return_if_fail (GTK_IS_CONTAINER (container));
1820 widget = GTK_WIDGET (container);
1821 gtk_widget_get_allocation (widget, &allocation);
1823 gtk_widget_size_allocate (widget, &allocation);
1824 gtk_widget_set_allocation (widget, &allocation);
1828 gtk_container_adjust_size_request (GtkWidget *widget,
1829 GtkOrientation orientation,
1833 GtkContainer *container;
1835 container = GTK_CONTAINER (widget);
1837 if (GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1841 border_width = container->priv->border_width;
1843 *minimum_size += border_width * 2;
1844 *natural_size += border_width * 2;
1847 /* chain up last so gtk_widget_set_size_request() values
1848 * will have a chance to overwrite our border width.
1850 parent_class->adjust_size_request (widget, orientation,
1851 minimum_size, natural_size);
1855 gtk_container_adjust_size_allocation (GtkWidget *widget,
1856 GtkOrientation orientation,
1859 gint *allocated_pos,
1860 gint *allocated_size)
1862 GtkContainer *container;
1865 container = GTK_CONTAINER (widget);
1867 if (!GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1869 parent_class->adjust_size_allocation (widget, orientation,
1870 minimum_size, natural_size, allocated_pos,
1875 border_width = container->priv->border_width;
1877 *allocated_size -= border_width * 2;
1879 /* If we get a pathological too-small allocation to hold
1880 * even the border width, leave all allocation to the actual
1881 * widget, and leave x,y unchanged. (GtkWidget's min size is
1882 * 1x1 if you're wondering why <1 and not <0)
1884 * As long as we have space, set x,y properly.
1887 if (*allocated_size < 1)
1889 *allocated_size += border_width * 2;
1893 *allocated_pos += border_width;
1894 *minimum_size -= border_width * 2;
1895 *natural_size -= border_width * 2;
1898 /* Chain up to GtkWidgetClass *after* removing our border width from
1899 * the proposed allocation size. This is because it's possible that the
1900 * widget was allocated more space than it needs in a said orientation,
1901 * if GtkWidgetClass does any alignments and thus limits the size to the
1902 * natural size... then we need that to be done *after* removing any margins
1903 * and padding values.
1905 parent_class->adjust_size_allocation (widget, orientation,
1906 minimum_size, natural_size, allocated_pos,
1916 count_request_modes (GtkWidget *widget,
1917 RequestModeCount *count)
1919 GtkSizeRequestMode mode = gtk_widget_get_request_mode (widget);
1923 case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
1926 case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
1929 case GTK_SIZE_REQUEST_CONSTANT_SIZE:
1935 static GtkSizeRequestMode
1936 gtk_container_get_request_mode (GtkWidget *widget)
1938 GtkContainer *container = GTK_CONTAINER (widget);
1939 GtkContainerPrivate *priv = container->priv;
1941 /* Recalculate the request mode of the children by majority
1942 * vote whenever the internal content changes */
1943 if (_gtk_widget_get_width_request_needed (widget) ||
1944 _gtk_widget_get_height_request_needed (widget))
1946 RequestModeCount count = { 0, 0 };
1948 gtk_container_forall (container, (GtkCallback)count_request_modes, &count);
1950 if (!count.hfw && !count.wfh)
1951 priv->request_mode = GTK_SIZE_REQUEST_CONSTANT_SIZE;
1953 priv->request_mode = count.wfh > count.hfw ?
1954 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
1955 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1958 return priv->request_mode;
1962 * gtk_container_class_handle_border_width:
1963 * @klass: the class struct of a #GtkContainer subclass
1965 * Modifies a subclass of #GtkContainerClass to automatically add and
1966 * remove the border-width setting on GtkContainer. This allows the
1967 * subclass to ignore the border width in its size request and
1968 * allocate methods. The intent is for a subclass to invoke this
1969 * in its class_init function.
1971 * gtk_container_class_handle_border_width() is necessary because it
1972 * would break API too badly to make this behavior the default. So
1973 * subclasses must "opt in" to the parent class handling border_width
1977 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1979 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1981 klass->_handle_border_width = TRUE;
1985 * gtk_container_forall:
1986 * @container: a #GtkContainer
1987 * @callback: (scope call) (closure callback_data): a callback
1988 * @callback_data: callback user data
1990 * Invokes @callback on each child of @container, including children
1991 * that are considered "internal" (implementation details of the
1992 * container). "Internal" children generally weren't added by the user
1993 * of the container, but were added by the container implementation
1994 * itself. Most applications should use gtk_container_foreach(),
1995 * rather than gtk_container_forall().
2000 gtk_container_forall (GtkContainer *container,
2001 GtkCallback callback,
2002 gpointer callback_data)
2004 GtkContainerClass *class;
2006 g_return_if_fail (GTK_IS_CONTAINER (container));
2007 g_return_if_fail (callback != NULL);
2009 class = GTK_CONTAINER_GET_CLASS (container);
2012 class->forall (container, TRUE, callback, callback_data);
2016 * gtk_container_foreach:
2017 * @container: a #GtkContainer
2018 * @callback: (scope call): a callback
2019 * @callback_data: callback user data
2021 * Invokes @callback on each non-internal child of @container. See
2022 * gtk_container_forall() for details on what constitutes an
2023 * "internal" child. Most applications should use
2024 * gtk_container_foreach(), rather than gtk_container_forall().
2027 gtk_container_foreach (GtkContainer *container,
2028 GtkCallback callback,
2029 gpointer callback_data)
2031 GtkContainerClass *class;
2033 g_return_if_fail (GTK_IS_CONTAINER (container));
2034 g_return_if_fail (callback != NULL);
2036 class = GTK_CONTAINER_GET_CLASS (container);
2039 class->forall (container, FALSE, callback, callback_data);
2043 * gtk_container_set_focus_child:
2044 * @container: a #GtkContainer
2045 * @child: (allow-none): a #GtkWidget, or %NULL
2047 * Sets, or unsets if @child is %NULL, the focused child of @container.
2049 * This function emits the GtkContainer::set_focus_child signal of
2050 * @container. Implementations of #GtkContainer can override the
2051 * default behaviour by overriding the class closure of this signal.
2053 * This is function is mostly meant to be used by widgets. Applications can use
2054 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
2057 gtk_container_set_focus_child (GtkContainer *container,
2060 g_return_if_fail (GTK_IS_CONTAINER (container));
2062 g_return_if_fail (GTK_IS_WIDGET (child));
2064 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
2068 * gtk_container_get_focus_child:
2069 * @container: a #GtkContainer
2071 * Returns the current focus child widget inside @container. This is not the
2072 * currently focused widget. That can be obtained by calling
2073 * gtk_window_get_focus().
2075 * Returns: (transfer none): The child widget which will receive the
2076 * focus inside @container when the @conatiner is focussed,
2077 * or %NULL if none is set.
2082 gtk_container_get_focus_child (GtkContainer *container)
2084 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2086 return container->priv->focus_child;
2090 * gtk_container_get_children:
2091 * @container: a #GtkContainer
2093 * Returns the container's non-internal children. See
2094 * gtk_container_forall() for details on what constitutes an "internal" child.
2096 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
2099 gtk_container_get_children (GtkContainer *container)
2101 GList *children = NULL;
2103 gtk_container_foreach (container,
2104 gtk_container_children_callback,
2107 return g_list_reverse (children);
2111 gtk_container_child_position_callback (GtkWidget *widget,
2112 gpointer client_data)
2118 } *data = client_data;
2121 if (data->child == widget)
2122 data->index = data->i;
2126 gtk_container_child_default_composite_name (GtkContainer *container,
2136 /* fallback implementation */
2140 gtk_container_forall (container,
2141 gtk_container_child_position_callback,
2144 name = g_strdup_printf ("%s-%u",
2145 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2152 _gtk_container_child_composite_name (GtkContainer *container,
2155 gboolean composite_child;
2157 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2158 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2159 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2161 g_object_get (child, "composite-child", &composite_child, NULL);
2162 if (composite_child)
2164 static GQuark quark_composite_name = 0;
2167 if (!quark_composite_name)
2168 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2170 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2173 GtkContainerClass *class;
2175 class = GTK_CONTAINER_GET_CLASS (container);
2176 if (class->composite_name)
2177 name = class->composite_name (container, child);
2180 name = g_strdup (name);
2191 } ComputeExpandData;
2194 gtk_container_compute_expand_callback (GtkWidget *widget,
2195 gpointer client_data)
2197 ComputeExpandData *data = client_data;
2199 /* note that we don't get_expand on the child if we already know we
2200 * have to expand, so we only recurse into children until we find
2201 * one that expands and then we basically don't do any more
2202 * work. This means that we can leave some children in a
2203 * need_compute_expand state, which is fine, as long as GtkWidget
2204 * doesn't rely on an invariant that "if a child has
2205 * need_compute_expand, its parents also do"
2207 * gtk_widget_compute_expand() always returns FALSE if the
2208 * child is !visible so that's taken care of.
2210 data->hexpand = data->hexpand ||
2211 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2213 data->vexpand = data->vexpand ||
2214 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2218 gtk_container_compute_expand (GtkWidget *widget,
2219 gboolean *hexpand_p,
2220 gboolean *vexpand_p)
2222 ComputeExpandData data;
2224 data.hexpand = FALSE;
2225 data.vexpand = FALSE;
2227 gtk_container_forall (GTK_CONTAINER (widget),
2228 gtk_container_compute_expand_callback,
2231 *hexpand_p = data.hexpand;
2232 *vexpand_p = data.vexpand;
2236 gtk_container_real_set_focus_child (GtkContainer *container,
2239 GtkContainerPrivate *priv;
2241 g_return_if_fail (GTK_IS_CONTAINER (container));
2242 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2244 priv = container->priv;
2246 if (child != priv->focus_child)
2248 if (priv->focus_child)
2249 g_object_unref (priv->focus_child);
2250 priv->focus_child = child;
2251 if (priv->focus_child)
2252 g_object_ref (priv->focus_child);
2256 /* check for h/v adjustments
2258 if (priv->focus_child)
2260 GtkAdjustment *hadj;
2261 GtkAdjustment *vadj;
2262 GtkAllocation allocation;
2263 GtkWidget *focus_child;
2266 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2267 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2271 focus_child = priv->focus_child;
2272 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2274 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2277 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2280 gtk_widget_get_allocation (priv->focus_child, &allocation);
2284 gtk_widget_get_allocation (focus_child, &allocation);
2287 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2290 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2296 get_focus_chain (GtkContainer *container)
2298 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2301 /* same as gtk_container_get_children, except it includes internals
2304 _gtk_container_get_all_children (GtkContainer *container)
2306 GList *children = NULL;
2308 gtk_container_forall (container,
2309 gtk_container_children_callback,
2315 static GtkWidgetPath *
2316 gtk_container_real_get_path_for_child (GtkContainer *container,
2319 GtkStyleContext *context;
2320 GtkWidgetPath *path;
2323 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2324 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
2326 /* Copy any permanent classes to the path */
2327 classes = gtk_style_context_list_classes (context);
2334 classes = classes->next;
2336 gtk_widget_path_iter_add_class (path, -1, cur->data);
2337 g_list_free_1 (cur);
2340 gtk_widget_path_append_for_widget (path, child);
2346 gtk_container_focus (GtkWidget *widget,
2347 GtkDirectionType direction)
2350 GList *sorted_children;
2352 GtkContainer *container;
2353 GtkContainerPrivate *priv;
2355 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2357 container = GTK_CONTAINER (widget);
2358 priv = container->priv;
2362 if (gtk_widget_get_can_focus (widget))
2364 if (!gtk_widget_has_focus (widget))
2366 gtk_widget_grab_focus (widget);
2372 /* Get a list of the containers children, allowing focus
2373 * chain to override.
2375 if (priv->has_focus_chain)
2376 children = g_list_copy (get_focus_chain (container));
2378 children = _gtk_container_get_all_children (container);
2380 if (priv->has_focus_chain &&
2381 (direction == GTK_DIR_TAB_FORWARD ||
2382 direction == GTK_DIR_TAB_BACKWARD))
2384 sorted_children = g_list_copy (children);
2386 if (direction == GTK_DIR_TAB_BACKWARD)
2387 sorted_children = g_list_reverse (sorted_children);
2390 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2392 return_val = gtk_container_focus_move (container, sorted_children, direction);
2394 g_list_free (sorted_children);
2395 g_list_free (children);
2402 tab_compare (gconstpointer a,
2406 GtkAllocation child1_allocation, child2_allocation;
2407 const GtkWidget *child1 = a;
2408 const GtkWidget *child2 = b;
2409 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2412 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2413 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2415 y1 = child1_allocation.y + child1_allocation.height / 2;
2416 y2 = child2_allocation.y + child2_allocation.height / 2;
2420 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2421 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2423 if (text_direction == GTK_TEXT_DIR_RTL)
2424 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2426 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2429 return (y1 < y2) ? -1 : 1;
2433 gtk_container_focus_sort_tab (GtkContainer *container,
2435 GtkDirectionType direction,
2436 GtkWidget *old_focus)
2438 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2439 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2441 /* if we are going backwards then reverse the order
2444 if (direction == GTK_DIR_TAB_BACKWARD)
2445 children = g_list_reverse (children);
2450 /* Get coordinates of @widget's allocation with respect to
2451 * allocation of @container.
2454 get_allocation_coords (GtkContainer *container,
2456 GdkRectangle *allocation)
2458 gtk_widget_get_allocation (widget, allocation);
2460 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2461 0, 0, &allocation->x, &allocation->y);
2464 /* Look for a child in @children that is intermediate between
2465 * the focus widget and container. This widget, if it exists,
2466 * acts as the starting widget for focus navigation.
2469 find_old_focus (GtkContainer *container,
2472 GList *tmp_list = children;
2475 GtkWidget *child = tmp_list->data;
2476 GtkWidget *widget = child;
2478 while (widget && widget != (GtkWidget *)container)
2482 parent = gtk_widget_get_parent (widget);
2484 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2493 tmp_list = tmp_list->next;
2500 old_focus_coords (GtkContainer *container,
2501 GdkRectangle *old_focus_rect)
2503 GtkWidget *widget = GTK_WIDGET (container);
2504 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2505 GtkWidget *old_focus;
2507 if (GTK_IS_WINDOW (toplevel))
2509 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2511 return get_allocation_coords (container, old_focus, old_focus_rect);
2517 typedef struct _CompareInfo CompareInfo;
2521 GtkContainer *container;
2528 up_down_compare (gconstpointer a,
2532 GdkRectangle allocation1;
2533 GdkRectangle allocation2;
2534 CompareInfo *compare = data;
2537 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2538 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2540 y1 = allocation1.y + allocation1.height / 2;
2541 y2 = allocation2.y + allocation2.height / 2;
2545 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2546 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2548 if (compare->reverse)
2549 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2551 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2554 return (y1 < y2) ? -1 : 1;
2558 gtk_container_focus_sort_up_down (GtkContainer *container,
2560 GtkDirectionType direction,
2561 GtkWidget *old_focus)
2563 CompareInfo compare;
2565 GdkRectangle old_allocation;
2567 compare.container = container;
2568 compare.reverse = (direction == GTK_DIR_UP);
2571 old_focus = find_old_focus (container, children);
2573 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2579 /* Delete widgets from list that don't match minimum criteria */
2581 compare_x1 = old_allocation.x;
2582 compare_x2 = old_allocation.x + old_allocation.width;
2584 if (direction == GTK_DIR_UP)
2585 compare_y = old_allocation.y;
2587 compare_y = old_allocation.y + old_allocation.height;
2589 tmp_list = children;
2592 GtkWidget *child = tmp_list->data;
2593 GList *next = tmp_list->next;
2594 gint child_x1, child_x2;
2595 GdkRectangle child_allocation;
2597 if (child != old_focus)
2599 if (get_allocation_coords (container, child, &child_allocation))
2601 child_x1 = child_allocation.x;
2602 child_x2 = child_allocation.x + child_allocation.width;
2604 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2605 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2606 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2608 children = g_list_delete_link (children, tmp_list);
2612 children = g_list_delete_link (children, tmp_list);
2618 compare.x = (compare_x1 + compare_x2) / 2;
2619 compare.y = old_allocation.y + old_allocation.height / 2;
2623 /* No old focus widget, need to figure out starting x,y some other way
2625 GtkAllocation allocation;
2626 GtkWidget *widget = GTK_WIDGET (container);
2627 GdkRectangle old_focus_rect;
2629 gtk_widget_get_allocation (widget, &allocation);
2631 if (old_focus_coords (container, &old_focus_rect))
2633 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2637 if (!gtk_widget_get_has_window (widget))
2638 compare.x = allocation.x + allocation.width / 2;
2640 compare.x = allocation.width / 2;
2643 if (!gtk_widget_get_has_window (widget))
2644 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2646 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2649 children = g_list_sort_with_data (children, up_down_compare, &compare);
2651 if (compare.reverse)
2652 children = g_list_reverse (children);
2658 left_right_compare (gconstpointer a,
2662 GdkRectangle allocation1;
2663 GdkRectangle allocation2;
2664 CompareInfo *compare = data;
2667 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2668 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2670 x1 = allocation1.x + allocation1.width / 2;
2671 x2 = allocation2.x + allocation2.width / 2;
2675 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2676 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2678 if (compare->reverse)
2679 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2681 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2684 return (x1 < x2) ? -1 : 1;
2688 gtk_container_focus_sort_left_right (GtkContainer *container,
2690 GtkDirectionType direction,
2691 GtkWidget *old_focus)
2693 CompareInfo compare;
2695 GdkRectangle old_allocation;
2697 compare.container = container;
2698 compare.reverse = (direction == GTK_DIR_LEFT);
2701 old_focus = find_old_focus (container, children);
2703 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2709 /* Delete widgets from list that don't match minimum criteria */
2711 compare_y1 = old_allocation.y;
2712 compare_y2 = old_allocation.y + old_allocation.height;
2714 if (direction == GTK_DIR_LEFT)
2715 compare_x = old_allocation.x;
2717 compare_x = old_allocation.x + old_allocation.width;
2719 tmp_list = children;
2722 GtkWidget *child = tmp_list->data;
2723 GList *next = tmp_list->next;
2724 gint child_y1, child_y2;
2725 GdkRectangle child_allocation;
2727 if (child != old_focus)
2729 if (get_allocation_coords (container, child, &child_allocation))
2731 child_y1 = child_allocation.y;
2732 child_y2 = child_allocation.y + child_allocation.height;
2734 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2735 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2736 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2738 children = g_list_delete_link (children, tmp_list);
2742 children = g_list_delete_link (children, tmp_list);
2748 compare.y = (compare_y1 + compare_y2) / 2;
2749 compare.x = old_allocation.x + old_allocation.width / 2;
2753 /* No old focus widget, need to figure out starting x,y some other way
2755 GtkAllocation allocation;
2756 GtkWidget *widget = GTK_WIDGET (container);
2757 GdkRectangle old_focus_rect;
2759 gtk_widget_get_allocation (widget, &allocation);
2761 if (old_focus_coords (container, &old_focus_rect))
2763 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2767 if (!gtk_widget_get_has_window (widget))
2768 compare.y = allocation.y + allocation.height / 2;
2770 compare.y = allocation.height / 2;
2773 if (!gtk_widget_get_has_window (widget))
2774 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2776 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2779 children = g_list_sort_with_data (children, left_right_compare, &compare);
2781 if (compare.reverse)
2782 children = g_list_reverse (children);
2788 * gtk_container_focus_sort:
2789 * @container: a #GtkContainer
2790 * @children: a list of descendents of @container (they don't
2791 * have to be direct children)
2792 * @direction: focus direction
2793 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2794 * to determine this automatically.
2795 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2796 * which is the only @direction we use currently,
2797 * so perhaps this argument should be removed)
2799 * Sorts @children in the correct order for focusing with
2800 * direction type @direction.
2802 * Return value: a copy of @children, sorted in correct focusing order,
2803 * with children that aren't suitable for focusing in this direction
2807 _gtk_container_focus_sort (GtkContainer *container,
2809 GtkDirectionType direction,
2810 GtkWidget *old_focus)
2812 GList *visible_children = NULL;
2816 if (gtk_widget_get_realized (children->data))
2817 visible_children = g_list_prepend (visible_children, children->data);
2818 children = children->next;
2823 case GTK_DIR_TAB_FORWARD:
2824 case GTK_DIR_TAB_BACKWARD:
2825 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2828 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2831 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2834 g_assert_not_reached ();
2840 gtk_container_focus_move (GtkContainer *container,
2842 GtkDirectionType direction)
2844 GtkContainerPrivate *priv = container->priv;
2845 GtkWidget *focus_child;
2848 focus_child = priv->focus_child;
2852 child = children->data;
2853 children = children->next;
2860 if (focus_child == child)
2864 if (gtk_widget_child_focus (child, direction))
2868 else if (gtk_widget_is_drawable (child) &&
2869 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2871 if (gtk_widget_child_focus (child, direction))
2881 gtk_container_children_callback (GtkWidget *widget,
2882 gpointer client_data)
2886 children = (GList**) client_data;
2887 *children = g_list_prepend (*children, widget);
2891 chain_widget_destroyed (GtkWidget *widget,
2894 GtkContainer *container;
2897 container = GTK_CONTAINER (user_data);
2899 chain = g_object_get_data (G_OBJECT (container),
2900 "gtk-container-focus-chain");
2902 chain = g_list_remove (chain, widget);
2904 g_signal_handlers_disconnect_by_func (widget,
2905 chain_widget_destroyed,
2908 g_object_set_data (G_OBJECT (container),
2909 I_("gtk-container-focus-chain"),
2914 * gtk_container_set_focus_chain:
2915 * @container: a #GtkContainer
2916 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2917 * the new focus chain
2919 * Sets a focus chain, overriding the one computed automatically by GTK+.
2921 * In principle each widget in the chain should be a descendant of the
2922 * container, but this is not enforced by this method, since it's allowed
2923 * to set the focus chain before you pack the widgets, or have a widget
2924 * in the chain that isn't always packed. The necessary checks are done
2925 * when the focus chain is actually traversed.
2928 gtk_container_set_focus_chain (GtkContainer *container,
2929 GList *focusable_widgets)
2933 GtkContainerPrivate *priv;
2935 g_return_if_fail (GTK_IS_CONTAINER (container));
2937 priv = container->priv;
2939 if (priv->has_focus_chain)
2940 gtk_container_unset_focus_chain (container);
2942 priv->has_focus_chain = TRUE;
2945 tmp_list = focusable_widgets;
2946 while (tmp_list != NULL)
2948 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2950 /* In principle each widget in the chain should be a descendant
2951 * of the container, but we don't want to check that here. It's
2952 * expensive and also it's allowed to set the focus chain before
2953 * you pack the widgets, or have a widget in the chain that isn't
2954 * always packed. So we check for ancestor during actual traversal.
2957 chain = g_list_prepend (chain, tmp_list->data);
2959 g_signal_connect (tmp_list->data,
2961 G_CALLBACK (chain_widget_destroyed),
2964 tmp_list = g_list_next (tmp_list);
2967 chain = g_list_reverse (chain);
2969 g_object_set_data (G_OBJECT (container),
2970 I_("gtk-container-focus-chain"),
2975 * gtk_container_get_focus_chain:
2976 * @container: a #GtkContainer
2977 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2978 * to store the focus chain of the
2979 * container, or %NULL. You should free this list
2980 * using g_list_free() when you are done with it, however
2981 * no additional reference count is added to the
2982 * individual widgets in the focus chain.
2984 * Retrieves the focus chain of the container, if one has been
2985 * set explicitly. If no focus chain has been explicitly
2986 * set, GTK+ computes the focus chain based on the positions
2987 * of the children. In that case, GTK+ stores %NULL in
2988 * @focusable_widgets and returns %FALSE.
2990 * Return value: %TRUE if the focus chain of the container
2991 * has been set explicitly.
2994 gtk_container_get_focus_chain (GtkContainer *container,
2995 GList **focus_chain)
2997 GtkContainerPrivate *priv;
2999 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
3001 priv = container->priv;
3005 if (priv->has_focus_chain)
3006 *focus_chain = g_list_copy (get_focus_chain (container));
3008 *focus_chain = NULL;
3011 return priv->has_focus_chain;
3015 * gtk_container_unset_focus_chain:
3016 * @container: a #GtkContainer
3018 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
3021 gtk_container_unset_focus_chain (GtkContainer *container)
3023 GtkContainerPrivate *priv;
3025 g_return_if_fail (GTK_IS_CONTAINER (container));
3027 priv = container->priv;
3029 if (priv->has_focus_chain)
3034 chain = get_focus_chain (container);
3036 priv->has_focus_chain = FALSE;
3038 g_object_set_data (G_OBJECT (container),
3039 I_("gtk-container-focus-chain"),
3043 while (tmp_list != NULL)
3045 g_signal_handlers_disconnect_by_func (tmp_list->data,
3046 chain_widget_destroyed,
3049 tmp_list = g_list_next (tmp_list);
3052 g_list_free (chain);
3057 * gtk_container_set_focus_vadjustment:
3058 * @container: a #GtkContainer
3059 * @adjustment: an adjustment which should be adjusted when the focus
3060 * is moved among the descendents of @container
3062 * Hooks up an adjustment to focus handling in a container, so when a
3063 * child of the container is focused, the adjustment is scrolled to
3064 * show that widget. This function sets the vertical alignment. See
3065 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
3066 * the adjustment and gtk_container_set_focus_hadjustment() for setting
3067 * the horizontal adjustment.
3069 * The adjustments have to be in pixel units and in the same coordinate
3070 * system as the allocation for immediate children of the container.
3073 gtk_container_set_focus_vadjustment (GtkContainer *container,
3074 GtkAdjustment *adjustment)
3076 g_return_if_fail (GTK_IS_CONTAINER (container));
3078 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3081 g_object_ref (adjustment);
3083 g_object_set_qdata_full (G_OBJECT (container),
3090 * gtk_container_get_focus_vadjustment:
3091 * @container: a #GtkContainer
3093 * Retrieves the vertical focus adjustment for the container. See
3094 * gtk_container_set_focus_vadjustment().
3096 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
3097 * none has been set.
3100 gtk_container_get_focus_vadjustment (GtkContainer *container)
3102 GtkAdjustment *vadjustment;
3104 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3106 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
3112 * gtk_container_set_focus_hadjustment:
3113 * @container: a #GtkContainer
3114 * @adjustment: an adjustment which should be adjusted when the focus is
3115 * moved among the descendents of @container
3117 * Hooks up an adjustment to focus handling in a container, so when a child
3118 * of the container is focused, the adjustment is scrolled to show that
3119 * widget. This function sets the horizontal alignment.
3120 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
3121 * the adjustment and gtk_container_set_focus_vadjustment() for setting
3122 * the vertical adjustment.
3124 * The adjustments have to be in pixel units and in the same coordinate
3125 * system as the allocation for immediate children of the container.
3128 gtk_container_set_focus_hadjustment (GtkContainer *container,
3129 GtkAdjustment *adjustment)
3131 g_return_if_fail (GTK_IS_CONTAINER (container));
3133 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3136 g_object_ref (adjustment);
3138 g_object_set_qdata_full (G_OBJECT (container),
3145 * gtk_container_get_focus_hadjustment:
3146 * @container: a #GtkContainer
3148 * Retrieves the horizontal focus adjustment for the container. See
3149 * gtk_container_set_focus_hadjustment ().
3151 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3152 * none has been set.
3155 gtk_container_get_focus_hadjustment (GtkContainer *container)
3157 GtkAdjustment *hadjustment;
3159 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3161 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3168 gtk_container_show_all (GtkWidget *widget)
3170 g_return_if_fail (GTK_IS_CONTAINER (widget));
3172 gtk_container_foreach (GTK_CONTAINER (widget),
3173 (GtkCallback) gtk_widget_show_all,
3175 gtk_widget_show (widget);
3179 gtk_container_draw_child (GtkWidget *child,
3180 gpointer client_data)
3183 GtkWidget *container;
3185 } *data = client_data;
3187 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3193 gtk_container_draw (GtkWidget *widget,
3197 GtkWidget *container;
3201 data.container = widget;
3204 gtk_container_forall (GTK_CONTAINER (widget),
3205 gtk_container_draw_child,
3212 gtk_container_map_child (GtkWidget *child,
3213 gpointer client_data)
3215 if (gtk_widget_get_visible (child) &&
3216 gtk_widget_get_child_visible (child) &&
3217 !gtk_widget_get_mapped (child))
3218 gtk_widget_map (child);
3222 gtk_container_map (GtkWidget *widget)
3224 gtk_widget_set_mapped (widget, TRUE);
3226 gtk_container_forall (GTK_CONTAINER (widget),
3227 gtk_container_map_child,
3230 if (gtk_widget_get_has_window (widget))
3231 gdk_window_show (gtk_widget_get_window (widget));
3235 gtk_container_unmap (GtkWidget *widget)
3237 gtk_widget_set_mapped (widget, FALSE);
3239 /* hide our window first so user doesn't see all the child windows
3240 * vanishing one by one. (only matters these days if one of the
3241 * children has an actual native window instead of client-side
3242 * window, e.g. a GtkSocket would)
3244 if (gtk_widget_get_has_window (widget))
3245 gdk_window_hide (gtk_widget_get_window (widget));
3247 gtk_container_forall (GTK_CONTAINER (widget),
3248 (GtkCallback)gtk_widget_unmap,
3253 * gtk_container_propagate_draw:
3254 * @container: a #GtkContainer
3255 * @child: a child of @container
3256 * @cr: Cairo context as passed to the container. If you want to use @cr
3257 * in container's draw function, consider using cairo_save() and
3258 * cairo_restore() before calling this function.
3260 * When a container receives a call to the draw function, it must send
3261 * synthetic #GtkWidget::draw calls to all children that don't have their
3262 * own #GdkWindows. This function provides a convenient way of doing this.
3263 * A container, when it receives a call to its #GtkWidget::draw function,
3264 * calls gtk_container_propagate_draw() once for each child, passing in
3265 * the @cr the container received.
3267 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3268 * and deciding whether the draw needs to be sent to the child. It is a
3269 * convenient and optimized way of getting the same effect as calling
3270 * gtk_widget_draw() on the child directly.
3272 * In most cases, a container can simply either inherit the
3273 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3274 * and then chain to the ::draw implementation from #GtkContainer.
3277 gtk_container_propagate_draw (GtkContainer *container,
3281 GdkEventExpose *event;
3282 GtkAllocation allocation;
3283 GdkWindow *window, *w;
3286 g_return_if_fail (GTK_IS_CONTAINER (container));
3287 g_return_if_fail (GTK_IS_WIDGET (child));
3288 g_return_if_fail (cr != NULL);
3290 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3292 event = _gtk_cairo_get_event (cr);
3295 if (gtk_widget_get_has_window (child) ||
3296 gtk_widget_get_window (child) != event->window)
3302 /* translate coordinates. Ugly business, that. */
3303 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3305 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3315 window = gtk_widget_get_window (GTK_WIDGET (container));
3317 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3320 gdk_window_get_position (w, &wx, &wy);
3331 if (!gtk_widget_get_has_window (child))
3333 gtk_widget_get_allocation (child, &allocation);
3338 cairo_translate (cr, x, y);
3340 _gtk_widget_draw_internal (child, cr, TRUE);
3346 _gtk_container_get_need_resize (GtkContainer *container)
3348 return container->priv->need_resize;
3352 _gtk_container_set_need_resize (GtkContainer *container,
3353 gboolean need_resize)
3355 container->priv->need_resize = need_resize;
3359 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3361 return container->priv->reallocate_redraws;
3365 * gtk_container_get_path_for_child:
3366 * @container: a #GtkContainer
3367 * @child: a child of @container
3369 * Returns a newly created widget path representing all the widget hierarchy
3370 * from the toplevel down to and including @child.
3372 * Returns: A newly created #GtkWidgetPath
3375 gtk_container_get_path_for_child (GtkContainer *container,
3378 GtkWidgetPath *path;
3380 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3381 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3382 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3384 path = GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);
3385 if (gtk_widget_path_get_object_type (path) != G_OBJECT_TYPE (child))
3387 g_critical ("%s %p returned a widget path for type %s, but child is %s",
3388 G_OBJECT_TYPE_NAME (container),
3390 g_type_name (gtk_widget_path_get_object_type (path)),
3391 G_OBJECT_TYPE_NAME (child));