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 "gtkbuildable.h"
36 #include "gtkbuilderprivate.h"
37 #include "gtkprivate.h"
39 #include "gtkmarshalers.h"
40 #include "gtksizerequest.h"
41 #include "gtkwidgetprivate.h"
42 #include "gtkwindow.h"
44 #include "gtktoolbar.h"
45 #include <gobject/gobjectnotifyqueue.c>
46 #include <gobject/gvaluecollector.h>
50 * SECTION:gtkcontainer
51 * @Short_description: Base class for widgets which contain other widgets
52 * @Title: GtkContainer
54 * A GTK+ user interface is constructed by nesting widgets inside widgets.
55 * Container widgets are the inner nodes in the resulting tree of widgets:
56 * they contain other widgets. So, for example, you might have a #GtkWindow
57 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
58 * of a textual label inside the frame, you might replace the #GtkLabel widget
59 * with a #GtkImage widget.
61 * There are two major kinds of container widgets in GTK+. Both are subclasses
62 * of the abstract GtkContainer base class.
64 * The first type of container widget has a single child widget and derives
65 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
66 * add some kind of functionality to the child. For example, a #GtkButton makes
67 * its child into a clickable button; a #GtkFrame draws a frame around its child
68 * and a #GtkWindow places its child widget inside a top-level window.
70 * The second type of container can have more than one child; its purpose is to
71 * manage <emphasis>layout</emphasis>. This means that these containers assign
72 * sizes and positions to their children. For example, a #GtkHBox arranges its
73 * children in a horizontal row, and a #GtkTable arranges the widgets it contains
74 * in a two-dimensional grid.
76 * <refsect2 id="container-geometry-management">
77 * <title>Height for width geometry management</title>
79 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
80 * Height-for-width means that a widget can change how much vertical space it needs,
81 * depending on the amount of horizontal space that it is given (and similar for
84 * There are some things to keep in mind when implementing container widgets
85 * that make use of GTK+'s height for width geometry management system. First,
86 * it's important to note that a container must prioritize one of its
87 * dimensions, that is to say that a widget or container can only have a
88 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
89 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
90 * must be able to respond to the APIs for both dimensions, i.e. even if a
91 * widget has a request mode that is height-for-width, it is possible that
92 * its parent will request its sizes using the width-for-height APIs.
94 * To ensure that everything works properly, here are some guidelines to follow
95 * when implementing height-for-width (or width-for-height) containers.
97 * Each request mode involves 2 virtual methods. Height-for-width apis run
98 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
99 * When handling requests in the opposite #GtkSizeRequestMode it is important that
100 * every widget request at least enough space to display all of its content at all times.
102 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
103 * the container must return the height for its minimum width. This is easily achieved by
104 * simply calling the reverse apis implemented for itself as follows:
106 * <programlisting><![CDATA[
108 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
110 * if (i_am_in_height_for_width_mode)
114 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
115 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
116 * min_height, nat_height);
120 * ... many containers support both request modes, execute the real width-for-height
121 * request here by returning the collective heights of all widgets that are
122 * stacked vertically (or whatever is appropriate for this container) ...
125 * ]]></programlisting>
127 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
128 * that is height-for-width, it then only needs to return the base minimum width like so:
130 * <programlisting><![CDATA[
132 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
133 * gint *min_width, gint *nat_width)
135 * if (i_am_in_height_for_width_mode)
137 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
141 * ... execute the real width-for-height request here based on the required width
142 * of the children collectively if the container were to be allocated the said height ...
145 * ]]></programlisting>
147 * Height for width requests are generally implemented in terms of a virtual allocation
148 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
149 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
150 * gtk_widget_get_preferred_width() for each of its children.
152 * For each potential group of children that are lined up horizontally, the values returned by
153 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures.
154 * Any child spacing should be removed from the input @for_width and then the collective size should be
155 * allocated using the gtk_distribute_natural_allocation() convenience function.
157 * The container will then move on to request the preferred height for each child by using
158 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
160 * To allocate a height-for-width container, it's again important
161 * to consider that a container must prioritize one dimension over the other. So if
162 * a container is a height-for-width container it must first allocate all widgets horizontally
163 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
164 * extra space (if and where appropriate) for the widget to expand.
166 * After adding all the expand space, the container assumes it was allocated sufficient
167 * height to fit all of its content. At this time, the container must use the total horizontal sizes
168 * of each widget to request the height-for-width of each of its children and store the requests in a
169 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
170 * be generalized into the heights and widths of rows and columns).
171 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
172 * while this time considering the allocated height of the widget minus any vertical spacing
173 * that the container adds. Then vertical expand space should be added where appropriate and available
174 * and the container should go on to actually allocating the child widgets.
176 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
177 * to learn more about implementing height-for-width geometry management for widgets.
180 * <refsect2 id="child-properties">
181 * <title>Child properties</title>
183 * GtkContainer introduces <emphasis>child properties</emphasis>.
184 * These are object properties that are not specific
185 * to either the container or the contained widget, but rather to their relation.
186 * Typical examples of child properties are the position or pack-type of a widget
187 * which is contained in a #GtkBox.
189 * Use gtk_container_class_install_child_property() to install child properties
190 * for a container class and gtk_container_class_find_child_property() or
191 * gtk_container_class_list_child_properties() to get information about existing
194 * To set the value of a child property, use gtk_container_child_set_property(),
195 * gtk_container_child_set() or gtk_container_child_set_valist().
196 * To obtain the value of a child property, use
197 * gtk_container_child_get_property(), gtk_container_child_get() or
198 * gtk_container_child_get_valist(). To emit notification about child property
199 * changes, use gtk_widget_child_notify().
202 * <refsect2 id="GtkContainer-BUILDER-UI">
203 * <title>GtkContainer as GtkBuildable</title>
205 * The GtkContainer implementation of the GtkBuildable interface
206 * supports a <packing> element for children, which can
207 * contain multiple <property> elements that specify
208 * child properties for the child.
210 * <title>Child properties in UI definitions</title>
211 * <programlisting><![CDATA[
212 * <object class="GtkVBox">
214 * <object class="GtkLabel"/>
216 * <property name="pack-type">start</property>
220 * ]]></programlisting>
222 * Since 2.16, child properties can also be marked as translatable using
223 * the same "translatable", "comments" and "context" attributes that are used
224 * for regular properties.
230 struct _GtkContainerPrivate
232 GtkWidget *focus_child;
234 guint border_width : 16;
236 guint has_focus_chain : 1;
237 guint need_resize : 1;
238 guint reallocate_redraws : 1;
239 guint resize_mode : 2;
257 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
258 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
261 /* --- prototypes --- */
262 static void gtk_container_base_class_init (GtkContainerClass *klass);
263 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
264 static void gtk_container_class_init (GtkContainerClass *klass);
265 static void gtk_container_init (GtkContainer *container);
266 static void gtk_container_destroy (GtkWidget *widget);
267 static void gtk_container_set_property (GObject *object,
271 static void gtk_container_get_property (GObject *object,
275 static void gtk_container_add_unimplemented (GtkContainer *container,
277 static void gtk_container_remove_unimplemented (GtkContainer *container,
279 static void gtk_container_real_check_resize (GtkContainer *container);
280 static void gtk_container_compute_expand (GtkWidget *widget,
282 gboolean *vexpand_p);
283 static gboolean gtk_container_focus (GtkWidget *widget,
284 GtkDirectionType direction);
285 static void gtk_container_real_set_focus_child (GtkContainer *container,
288 static gboolean gtk_container_focus_move (GtkContainer *container,
290 GtkDirectionType direction);
291 static void gtk_container_children_callback (GtkWidget *widget,
292 gpointer client_data);
293 static void gtk_container_show_all (GtkWidget *widget);
294 static gint gtk_container_draw (GtkWidget *widget,
296 static void gtk_container_map (GtkWidget *widget);
297 static void gtk_container_unmap (GtkWidget *widget);
298 static void gtk_container_adjust_size_request (GtkWidget *widget,
299 GtkOrientation orientation,
302 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
303 GtkOrientation orientation,
307 gint *allocated_size);
309 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
312 static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
316 static void gtk_container_buildable_init (GtkBuildableIface *iface);
317 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
321 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
324 const gchar *tagname,
325 GMarkupParser *parser,
327 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
330 const gchar *tagname,
334 /* --- variables --- */
335 static const gchar vadjustment_key[] = "gtk-vadjustment";
336 static guint vadjustment_key_id = 0;
337 static const gchar hadjustment_key[] = "gtk-hadjustment";
338 static guint hadjustment_key_id = 0;
339 static GSList *container_resize_queue = NULL;
340 static guint container_signals[LAST_SIGNAL] = { 0 };
341 static GtkWidgetClass *parent_class = NULL;
342 extern GParamSpecPool *_gtk_widget_child_property_pool;
343 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
344 static GtkBuildableIface *parent_buildable_iface;
347 /* --- functions --- */
349 gtk_container_get_type (void)
351 static GType container_type = 0;
355 const GTypeInfo container_info =
357 sizeof (GtkContainerClass),
358 (GBaseInitFunc) gtk_container_base_class_init,
359 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
360 (GClassInitFunc) gtk_container_class_init,
361 NULL /* class_finalize */,
362 NULL /* class_data */,
363 sizeof (GtkContainer),
365 (GInstanceInitFunc) gtk_container_init,
366 NULL, /* value_table */
369 const GInterfaceInfo buildable_info =
371 (GInterfaceInitFunc) gtk_container_buildable_init,
377 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
378 &container_info, G_TYPE_FLAG_ABSTRACT);
380 g_type_add_interface_static (container_type,
386 return container_type;
390 gtk_container_base_class_init (GtkContainerClass *class)
392 /* reset instance specifc class fields that don't get inherited */
393 class->set_child_property = NULL;
394 class->get_child_property = NULL;
398 gtk_container_base_class_finalize (GtkContainerClass *class)
402 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
403 for (node = list; node; node = node->next)
405 GParamSpec *pspec = node->data;
407 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
408 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
409 g_param_spec_unref (pspec);
415 gtk_container_class_init (GtkContainerClass *class)
417 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
418 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
420 parent_class = g_type_class_peek_parent (class);
422 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
423 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
425 gobject_class->set_property = gtk_container_set_property;
426 gobject_class->get_property = gtk_container_get_property;
428 widget_class->destroy = gtk_container_destroy;
429 widget_class->compute_expand = gtk_container_compute_expand;
430 widget_class->show_all = gtk_container_show_all;
431 widget_class->draw = gtk_container_draw;
432 widget_class->map = gtk_container_map;
433 widget_class->unmap = gtk_container_unmap;
434 widget_class->focus = gtk_container_focus;
436 widget_class->adjust_size_request = gtk_container_adjust_size_request;
437 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
439 class->add = gtk_container_add_unimplemented;
440 class->remove = gtk_container_remove_unimplemented;
441 class->check_resize = gtk_container_real_check_resize;
442 class->forall = NULL;
443 class->set_focus_child = gtk_container_real_set_focus_child;
444 class->child_type = NULL;
445 class->composite_name = gtk_container_child_default_composite_name;
446 class->get_path_for_child = gtk_container_real_get_path_for_child;
448 g_object_class_install_property (gobject_class,
450 g_param_spec_enum ("resize-mode",
452 P_("Specify how resize events are handled"),
453 GTK_TYPE_RESIZE_MODE,
455 GTK_PARAM_READWRITE));
456 g_object_class_install_property (gobject_class,
458 g_param_spec_uint ("border-width",
460 P_("The width of the empty border outside the containers children"),
464 GTK_PARAM_READWRITE));
465 g_object_class_install_property (gobject_class,
467 g_param_spec_object ("child",
469 P_("Can be used to add a new child to the container"),
471 GTK_PARAM_WRITABLE));
472 container_signals[ADD] =
473 g_signal_new (I_("add"),
474 G_OBJECT_CLASS_TYPE (gobject_class),
476 G_STRUCT_OFFSET (GtkContainerClass, add),
478 _gtk_marshal_VOID__OBJECT,
481 container_signals[REMOVE] =
482 g_signal_new (I_("remove"),
483 G_OBJECT_CLASS_TYPE (gobject_class),
485 G_STRUCT_OFFSET (GtkContainerClass, remove),
487 _gtk_marshal_VOID__OBJECT,
490 container_signals[CHECK_RESIZE] =
491 g_signal_new (I_("check-resize"),
492 G_OBJECT_CLASS_TYPE (gobject_class),
494 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
496 _gtk_marshal_VOID__VOID,
498 container_signals[SET_FOCUS_CHILD] =
499 g_signal_new (I_("set-focus-child"),
500 G_OBJECT_CLASS_TYPE (gobject_class),
502 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
504 _gtk_marshal_VOID__OBJECT,
508 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
512 gtk_container_buildable_init (GtkBuildableIface *iface)
514 parent_buildable_iface = g_type_interface_peek_parent (iface);
515 iface->add_child = gtk_container_buildable_add_child;
516 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
517 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
521 gtk_container_buildable_add_child (GtkBuildable *buildable,
528 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
530 else if (GTK_IS_WIDGET (child) &&
531 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
533 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
536 g_warning ("Cannot add an object of type %s to a container of type %s",
537 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
541 gtk_container_buildable_set_child_property (GtkContainer *container,
548 GValue gvalue = { 0, };
549 GError *error = NULL;
551 pspec = gtk_container_class_find_child_property
552 (G_OBJECT_GET_CLASS (container), name);
555 g_warning ("%s does not have a property called %s",
556 g_type_name (G_OBJECT_TYPE (container)), name);
560 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
562 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
563 g_type_name (G_OBJECT_TYPE (container)),
566 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
568 g_error_free (error);
572 gtk_container_child_set_property (container, child, name, &gvalue);
573 g_value_unset (&gvalue);
578 GtkContainer *container;
580 gchar *child_prop_name;
582 gboolean translatable;
583 } PackingPropertiesData;
586 attributes_start_element (GMarkupParseContext *context,
587 const gchar *element_name,
589 const gchar **values,
593 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
596 if (strcmp (element_name, "property") == 0)
598 for (i = 0; names[i]; i++)
599 if (strcmp (names[i], "name") == 0)
600 parser_data->child_prop_name = g_strdup (values[i]);
601 else if (strcmp (names[i], "translatable") == 0)
603 if (!_gtk_builder_boolean_from_string (values[1],
604 &parser_data->translatable,
608 else if (strcmp (names[i], "comments") == 0)
609 ; /* for translators */
610 else if (strcmp (names[i], "context") == 0)
611 parser_data->context = g_strdup (values[1]);
613 g_warning ("Unsupported attribute for GtkContainer Child "
614 "property: %s\n", names[i]);
616 else if (strcmp (element_name, "packing") == 0)
619 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
623 attributes_text_element (GMarkupParseContext *context,
629 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
632 if (!parser_data->child_prop_name)
635 if (parser_data->translatable && text_len)
638 domain = gtk_builder_get_translation_domain (parser_data->builder);
640 value = _gtk_builder_parser_translate (domain,
641 parser_data->context,
646 value = g_strdup (text);
649 gtk_container_buildable_set_child_property (parser_data->container,
650 parser_data->builder,
652 parser_data->child_prop_name,
655 g_free (parser_data->child_prop_name);
656 g_free (parser_data->context);
658 parser_data->child_prop_name = NULL;
659 parser_data->context = NULL;
660 parser_data->translatable = FALSE;
663 static const GMarkupParser attributes_parser =
665 attributes_start_element,
667 attributes_text_element,
671 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
674 const gchar *tagname,
675 GMarkupParser *parser,
678 PackingPropertiesData *parser_data;
680 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
681 tagname, parser, data))
684 if (child && strcmp (tagname, "packing") == 0)
686 parser_data = g_slice_new0 (PackingPropertiesData);
687 parser_data->builder = builder;
688 parser_data->container = GTK_CONTAINER (buildable);
689 parser_data->child = GTK_WIDGET (child);
690 parser_data->child_prop_name = NULL;
692 *parser = attributes_parser;
701 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
704 const gchar *tagname,
707 if (strcmp (tagname, "packing") == 0)
709 g_slice_free (PackingPropertiesData, (gpointer)data);
714 if (parent_buildable_iface->custom_tag_end)
715 parent_buildable_iface->custom_tag_end (buildable, builder,
716 child, tagname, data);
721 * gtk_container_child_type:
722 * @container: a #GtkContainer
724 * Returns the type of the children supported by the container.
726 * Note that this may return %G_TYPE_NONE to indicate that no more
727 * children can be added, e.g. for a #GtkPaned which already has two
730 * Return value: a #GType.
733 gtk_container_child_type (GtkContainer *container)
736 GtkContainerClass *class;
738 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
740 class = GTK_CONTAINER_GET_CLASS (container);
741 if (class->child_type)
742 slot = class->child_type (container);
749 /* --- GtkContainer child property mechanism --- */
751 container_get_child_property (GtkContainer *container,
756 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
758 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
762 container_set_child_property (GtkContainer *container,
766 GObjectNotifyQueue *nqueue)
768 GValue tmp_value = { 0, };
769 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
771 /* provide a copy to work from, convert (if necessary) and validate */
772 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
773 if (!g_value_transform (value, &tmp_value))
774 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
776 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
777 G_VALUE_TYPE_NAME (value));
778 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
780 gchar *contents = g_strdup_value_contents (value);
782 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
784 G_VALUE_TYPE_NAME (value),
786 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
791 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
792 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
794 g_value_unset (&tmp_value);
798 * gtk_container_child_get_valist:
799 * @container: a #GtkContainer
800 * @child: a widget which is a child of @container
801 * @first_property_name: the name of the first property to get
802 * @var_args: return location for the first property, followed
803 * optionally by more name/return location pairs, followed by %NULL
805 * Gets the values of one or more child properties for @child and @container.
808 gtk_container_child_get_valist (GtkContainer *container,
810 const gchar *first_property_name,
815 g_return_if_fail (GTK_IS_CONTAINER (container));
816 g_return_if_fail (GTK_IS_WIDGET (child));
817 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
819 g_object_ref (container);
820 g_object_ref (child);
822 name = first_property_name;
825 GValue value = { 0, };
829 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
831 G_OBJECT_TYPE (container),
835 g_warning ("%s: container class `%s' has no child property named `%s'",
837 G_OBJECT_TYPE_NAME (container),
841 if (!(pspec->flags & G_PARAM_READABLE))
843 g_warning ("%s: child property `%s' of container class `%s' is not readable",
846 G_OBJECT_TYPE_NAME (container));
849 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
850 container_get_child_property (container, child, pspec, &value);
851 G_VALUE_LCOPY (&value, var_args, 0, &error);
854 g_warning ("%s: %s", G_STRLOC, error);
856 g_value_unset (&value);
859 g_value_unset (&value);
860 name = va_arg (var_args, gchar*);
863 g_object_unref (child);
864 g_object_unref (container);
868 * gtk_container_child_get_property:
869 * @container: a #GtkContainer
870 * @child: a widget which is a child of @container
871 * @property_name: the name of the property to get
872 * @value: a location to return the value
874 * Gets the value of a child property for @child and @container.
877 gtk_container_child_get_property (GtkContainer *container,
879 const gchar *property_name,
884 g_return_if_fail (GTK_IS_CONTAINER (container));
885 g_return_if_fail (GTK_IS_WIDGET (child));
886 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
887 g_return_if_fail (property_name != NULL);
888 g_return_if_fail (G_IS_VALUE (value));
890 g_object_ref (container);
891 g_object_ref (child);
892 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
893 G_OBJECT_TYPE (container), TRUE);
895 g_warning ("%s: container class `%s' has no child property named `%s'",
897 G_OBJECT_TYPE_NAME (container),
899 else if (!(pspec->flags & G_PARAM_READABLE))
900 g_warning ("%s: child property `%s' of container class `%s' is not readable",
903 G_OBJECT_TYPE_NAME (container));
906 GValue *prop_value, tmp_value = { 0, };
908 /* auto-conversion of the callers value type
910 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
912 g_value_reset (value);
915 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
917 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
919 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
920 G_VALUE_TYPE_NAME (value));
921 g_object_unref (child);
922 g_object_unref (container);
927 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
928 prop_value = &tmp_value;
930 container_get_child_property (container, child, pspec, prop_value);
931 if (prop_value != value)
933 g_value_transform (prop_value, value);
934 g_value_unset (&tmp_value);
937 g_object_unref (child);
938 g_object_unref (container);
942 * gtk_container_child_set_valist:
943 * @container: a #GtkContainer
944 * @child: a widget which is a child of @container
945 * @first_property_name: the name of the first property to set
946 * @var_args: a %NULL-terminated list of property names and values, starting
947 * with @first_prop_name
949 * Sets one or more child properties for @child and @container.
952 gtk_container_child_set_valist (GtkContainer *container,
954 const gchar *first_property_name,
957 GObjectNotifyQueue *nqueue;
960 g_return_if_fail (GTK_IS_CONTAINER (container));
961 g_return_if_fail (GTK_IS_WIDGET (child));
962 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
964 g_object_ref (container);
965 g_object_ref (child);
967 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
968 name = first_property_name;
971 GValue value = { 0, };
973 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
975 G_OBJECT_TYPE (container),
979 g_warning ("%s: container class `%s' has no child property named `%s'",
981 G_OBJECT_TYPE_NAME (container),
985 if (!(pspec->flags & G_PARAM_WRITABLE))
987 g_warning ("%s: child property `%s' of container class `%s' is not writable",
990 G_OBJECT_TYPE_NAME (container));
993 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
994 G_VALUE_COLLECT (&value, var_args, 0, &error);
997 g_warning ("%s: %s", G_STRLOC, error);
1000 /* we purposely leak the value here, it might not be
1001 * in a sane state if an error condition occoured
1005 container_set_child_property (container, child, pspec, &value, nqueue);
1006 g_value_unset (&value);
1007 name = va_arg (var_args, gchar*);
1009 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1011 g_object_unref (container);
1012 g_object_unref (child);
1016 * gtk_container_child_set_property:
1017 * @container: a #GtkContainer
1018 * @child: a widget which is a child of @container
1019 * @property_name: the name of the property to set
1020 * @value: the value to set the property to
1022 * Sets a child property for @child and @container.
1025 gtk_container_child_set_property (GtkContainer *container,
1027 const gchar *property_name,
1028 const GValue *value)
1030 GObjectNotifyQueue *nqueue;
1033 g_return_if_fail (GTK_IS_CONTAINER (container));
1034 g_return_if_fail (GTK_IS_WIDGET (child));
1035 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1036 g_return_if_fail (property_name != NULL);
1037 g_return_if_fail (G_IS_VALUE (value));
1039 g_object_ref (container);
1040 g_object_ref (child);
1042 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1043 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1044 G_OBJECT_TYPE (container), TRUE);
1046 g_warning ("%s: container class `%s' has no child property named `%s'",
1048 G_OBJECT_TYPE_NAME (container),
1050 else if (!(pspec->flags & G_PARAM_WRITABLE))
1051 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1054 G_OBJECT_TYPE_NAME (container));
1057 container_set_child_property (container, child, pspec, value, nqueue);
1059 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1060 g_object_unref (container);
1061 g_object_unref (child);
1065 * gtk_container_add_with_properties:
1066 * @container: a #GtkContainer
1067 * @widget: a widget to be placed inside @container
1068 * @first_prop_name: the name of the first child property to set
1069 * @Varargs: a %NULL-terminated list of property names and values, starting
1070 * with @first_prop_name
1072 * Adds @widget to @container, setting child properties at the same time.
1073 * See gtk_container_add() and gtk_container_child_set() for more details.
1076 gtk_container_add_with_properties (GtkContainer *container,
1078 const gchar *first_prop_name,
1081 g_return_if_fail (GTK_IS_CONTAINER (container));
1082 g_return_if_fail (GTK_IS_WIDGET (widget));
1083 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1085 g_object_ref (container);
1086 g_object_ref (widget);
1087 gtk_widget_freeze_child_notify (widget);
1089 g_signal_emit (container, container_signals[ADD], 0, widget);
1090 if (gtk_widget_get_parent (widget))
1094 va_start (var_args, first_prop_name);
1095 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1099 gtk_widget_thaw_child_notify (widget);
1100 g_object_unref (widget);
1101 g_object_unref (container);
1105 * gtk_container_child_set:
1106 * @container: a #GtkContainer
1107 * @child: a widget which is a child of @container
1108 * @first_prop_name: the name of the first property to set
1109 * @Varargs: a %NULL-terminated list of property names and values, starting
1110 * with @first_prop_name
1112 * Sets one or more child properties for @child and @container.
1115 gtk_container_child_set (GtkContainer *container,
1117 const gchar *first_prop_name,
1122 g_return_if_fail (GTK_IS_CONTAINER (container));
1123 g_return_if_fail (GTK_IS_WIDGET (child));
1124 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1126 va_start (var_args, first_prop_name);
1127 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1132 * gtk_container_child_get:
1133 * @container: a #GtkContainer
1134 * @child: a widget which is a child of @container
1135 * @first_prop_name: the name of the first property to get
1136 * @Varargs: return location for the first property, followed
1137 * optionally by more name/return location pairs, followed by %NULL
1139 * Gets the values of one or more child properties for @child and @container.
1142 gtk_container_child_get (GtkContainer *container,
1144 const gchar *first_prop_name,
1149 g_return_if_fail (GTK_IS_CONTAINER (container));
1150 g_return_if_fail (GTK_IS_WIDGET (child));
1151 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1153 va_start (var_args, first_prop_name);
1154 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1159 * gtk_container_class_install_child_property:
1160 * @cclass: a #GtkContainerClass
1161 * @property_id: the id for the property
1162 * @pspec: the #GParamSpec for the property
1164 * Installs a child property on a container class.
1167 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1171 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1172 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1173 if (pspec->flags & G_PARAM_WRITABLE)
1174 g_return_if_fail (cclass->set_child_property != NULL);
1175 if (pspec->flags & G_PARAM_READABLE)
1176 g_return_if_fail (cclass->get_child_property != NULL);
1177 g_return_if_fail (property_id > 0);
1178 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1179 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1180 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1182 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1184 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1185 G_OBJECT_CLASS_NAME (cclass),
1189 g_param_spec_ref (pspec);
1190 g_param_spec_sink (pspec);
1191 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1192 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1196 * gtk_container_class_find_child_property:
1197 * @cclass: a #GtkContainerClass
1198 * @property_name: the name of the child property to find
1199 * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
1200 * child property with that name.
1202 * Finds a child property of a container class by name.
1205 gtk_container_class_find_child_property (GObjectClass *cclass,
1206 const gchar *property_name)
1208 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1209 g_return_val_if_fail (property_name != NULL, NULL);
1211 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1213 G_OBJECT_CLASS_TYPE (cclass),
1218 * gtk_container_class_list_child_properties:
1219 * @cclass: a #GtkContainerClass
1220 * @n_properties: location to return the number of child properties found
1221 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
1222 * The array must be freed with g_free().
1224 * Returns all child properties of a container class.
1227 gtk_container_class_list_child_properties (GObjectClass *cclass,
1228 guint *n_properties)
1230 GParamSpec **pspecs;
1233 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1235 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1236 G_OBJECT_CLASS_TYPE (cclass),
1245 gtk_container_add_unimplemented (GtkContainer *container,
1248 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1252 gtk_container_remove_unimplemented (GtkContainer *container,
1255 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1259 gtk_container_init (GtkContainer *container)
1261 GtkContainerPrivate *priv;
1263 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1265 GtkContainerPrivate);
1266 priv = container->priv;
1268 priv->focus_child = NULL;
1269 priv->border_width = 0;
1270 priv->need_resize = FALSE;
1271 priv->resize_mode = GTK_RESIZE_PARENT;
1272 priv->reallocate_redraws = FALSE;
1276 gtk_container_destroy (GtkWidget *widget)
1278 GtkContainer *container = GTK_CONTAINER (widget);
1279 GtkContainerPrivate *priv = container->priv;
1281 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1282 _gtk_container_dequeue_resize_handler (container);
1284 if (priv->focus_child)
1286 g_object_unref (priv->focus_child);
1287 priv->focus_child = NULL;
1290 /* do this before walking child widgets, to avoid
1291 * removing children from focus chain one by one.
1293 if (priv->has_focus_chain)
1294 gtk_container_unset_focus_chain (container);
1296 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1298 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1302 gtk_container_set_property (GObject *object,
1304 const GValue *value,
1307 GtkContainer *container = GTK_CONTAINER (object);
1311 case PROP_BORDER_WIDTH:
1312 gtk_container_set_border_width (container, g_value_get_uint (value));
1314 case PROP_RESIZE_MODE:
1315 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1318 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1327 gtk_container_get_property (GObject *object,
1332 GtkContainer *container = GTK_CONTAINER (object);
1333 GtkContainerPrivate *priv = container->priv;
1337 case PROP_BORDER_WIDTH:
1338 g_value_set_uint (value, priv->border_width);
1340 case PROP_RESIZE_MODE:
1341 g_value_set_enum (value, priv->resize_mode);
1344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1350 * gtk_container_set_border_width:
1351 * @container: a #GtkContainer
1352 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1353 * the container. Valid values are in the range 0-65535 pixels.
1355 * Sets the border width of the container.
1357 * The border width of a container is the amount of space to leave
1358 * around the outside of the container. The only exception to this is
1359 * #GtkWindow; because toplevel windows can't leave space outside,
1360 * they leave the space inside. The border is added on all sides of
1361 * the container. To add space to only one side, one approach is to
1362 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1363 * to give it a size, and place it on the side of the container as
1367 gtk_container_set_border_width (GtkContainer *container,
1370 GtkContainerPrivate *priv;
1372 g_return_if_fail (GTK_IS_CONTAINER (container));
1374 priv = container->priv;
1376 if (priv->border_width != border_width)
1378 priv->border_width = border_width;
1379 g_object_notify (G_OBJECT (container), "border-width");
1381 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1382 gtk_widget_queue_resize (GTK_WIDGET (container));
1387 * gtk_container_get_border_width:
1388 * @container: a #GtkContainer
1390 * Retrieves the border width of the container. See
1391 * gtk_container_set_border_width().
1393 * Return value: the current border width
1396 gtk_container_get_border_width (GtkContainer *container)
1398 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1400 return container->priv->border_width;
1404 * gtk_container_add:
1405 * @container: a #GtkContainer
1406 * @widget: a widget to be placed inside @container
1408 * Adds @widget to @container. Typically used for simple containers
1409 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1410 * layout containers such as #GtkBox or #GtkTable, this function will
1411 * pick default packing parameters that may not be correct. So
1412 * consider functions such as gtk_box_pack_start() and
1413 * gtk_table_attach() as an alternative to gtk_container_add() in
1414 * those cases. A widget may be added to only one container at a time;
1415 * you can't place the same widget inside two different containers.
1418 gtk_container_add (GtkContainer *container,
1423 g_return_if_fail (GTK_IS_CONTAINER (container));
1424 g_return_if_fail (GTK_IS_WIDGET (widget));
1426 parent = gtk_widget_get_parent (widget);
1430 g_warning ("Attempting to add a widget with type %s to a container of "
1431 "type %s, but the widget is already inside a container of type %s, "
1432 "please use gtk_widget_reparent()" ,
1433 g_type_name (G_OBJECT_TYPE (widget)),
1434 g_type_name (G_OBJECT_TYPE (container)),
1435 g_type_name (G_OBJECT_TYPE (parent)));
1439 g_signal_emit (container, container_signals[ADD], 0, widget);
1443 * gtk_container_remove:
1444 * @container: a #GtkContainer
1445 * @widget: a current child of @container
1447 * Removes @widget from @container. @widget must be inside @container.
1448 * Note that @container will own a reference to @widget, and that this
1449 * may be the last reference held; so removing a widget from its
1450 * container can destroy that widget. If you want to use @widget
1451 * again, you need to add a reference to it while it's not inside
1452 * a container, using g_object_ref(). If you don't want to use @widget
1453 * again it's usually more efficient to simply destroy it directly
1454 * using gtk_widget_destroy() since this will remove it from the
1455 * container and help break any circular reference count cycles.
1458 gtk_container_remove (GtkContainer *container,
1461 g_return_if_fail (GTK_IS_CONTAINER (container));
1462 g_return_if_fail (GTK_IS_WIDGET (widget));
1463 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1465 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1469 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1471 g_return_if_fail (GTK_IS_CONTAINER (container));
1472 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1474 container_resize_queue = g_slist_remove (container_resize_queue, container);
1475 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1479 * gtk_container_set_resize_mode:
1480 * @container: a #GtkContainer
1481 * @resize_mode: the new resize mode
1483 * Sets the resize mode for the container.
1485 * The resize mode of a container determines whether a resize request
1486 * will be passed to the container's parent, queued for later execution
1487 * or executed immediately.
1490 gtk_container_set_resize_mode (GtkContainer *container,
1491 GtkResizeMode resize_mode)
1493 GtkContainerPrivate *priv;
1495 g_return_if_fail (GTK_IS_CONTAINER (container));
1496 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1498 priv = container->priv;
1500 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1501 resize_mode == GTK_RESIZE_PARENT)
1503 resize_mode = GTK_RESIZE_QUEUE;
1506 if (priv->resize_mode != resize_mode)
1508 priv->resize_mode = resize_mode;
1510 gtk_widget_queue_resize (GTK_WIDGET (container));
1511 g_object_notify (G_OBJECT (container), "resize-mode");
1516 * gtk_container_get_resize_mode:
1517 * @container: a #GtkContainer
1519 * Returns the resize mode for the container. See
1520 * gtk_container_set_resize_mode ().
1522 * Return value: the current resize mode
1525 gtk_container_get_resize_mode (GtkContainer *container)
1527 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1529 return container->priv->resize_mode;
1533 * gtk_container_set_reallocate_redraws:
1534 * @container: a #GtkContainer
1535 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1537 * Sets the @reallocate_redraws flag of the container to the given value.
1539 * Containers requesting reallocation redraws get automatically
1540 * redrawn if any of their children changed allocation.
1543 gtk_container_set_reallocate_redraws (GtkContainer *container,
1544 gboolean needs_redraws)
1546 g_return_if_fail (GTK_IS_CONTAINER (container));
1548 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1551 static GtkContainer*
1552 gtk_container_get_resize_container (GtkContainer *container)
1555 GtkWidget *widget = GTK_WIDGET (container);
1557 while ((parent = gtk_widget_get_parent (widget)))
1560 if (GTK_IS_RESIZE_CONTAINER (widget))
1564 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1568 gtk_container_idle_sizer (gpointer data)
1570 /* we may be invoked with a container_resize_queue of NULL, because
1571 * queue_resize could have been adding an extra idle function while
1572 * the queue still got processed. we better just ignore such case
1573 * than trying to explicitely work around them with some extra flags,
1574 * since it doesn't cause any actual harm.
1576 while (container_resize_queue)
1581 slist = container_resize_queue;
1582 container_resize_queue = slist->next;
1583 widget = slist->data;
1584 g_slist_free_1 (slist);
1586 _gtk_widget_set_resize_pending (widget, FALSE);
1587 gtk_container_check_resize (GTK_CONTAINER (widget));
1590 gdk_window_process_all_updates ();
1596 _gtk_container_queue_resize_internal (GtkContainer *container,
1597 gboolean invalidate_only)
1599 GtkContainerPrivate *priv;
1600 GtkContainer *resize_container;
1604 g_return_if_fail (GTK_IS_CONTAINER (container));
1606 priv = container->priv;
1607 widget = GTK_WIDGET (container);
1609 resize_container = gtk_container_get_resize_container (container);
1613 _gtk_widget_set_alloc_needed (widget, TRUE);
1614 _gtk_widget_set_width_request_needed (widget, TRUE);
1615 _gtk_widget_set_height_request_needed (widget, TRUE);
1617 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1618 !(parent = gtk_widget_get_parent (widget)))
1624 if (resize_container && !invalidate_only)
1626 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1627 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1628 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1630 switch (resize_container->priv->resize_mode)
1632 case GTK_RESIZE_QUEUE:
1633 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1635 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1636 if (container_resize_queue == NULL)
1637 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1638 gtk_container_idle_sizer,
1640 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1644 case GTK_RESIZE_IMMEDIATE:
1645 gtk_container_check_resize (resize_container);
1648 case GTK_RESIZE_PARENT:
1649 g_assert_not_reached ();
1655 /* we need to let hidden resize containers know that something
1656 * changed while they where hidden (currently only evaluated by
1659 resize_container->priv->need_resize = TRUE;
1665 * _gtk_container_queue_resize:
1666 * @container: a #GtkContainer
1668 * Determines the "resize container" in the hierarchy above this container
1669 * (typically the toplevel, but other containers can be set as resize
1670 * containers with gtk_container_set_resize_mode()), marks the container
1671 * and all parents up to and including the resize container as needing
1672 * to have sizes recompted, and if necessary adds the resize container
1673 * to the queue of containers that will be resized out at idle.
1676 _gtk_container_queue_resize (GtkContainer *container)
1678 _gtk_container_queue_resize_internal (container, FALSE);
1682 * _gtk_container_resize_invalidate:
1683 * @container: a #GtkContainer
1685 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1686 * actually queue the resize container for resize.
1689 _gtk_container_resize_invalidate (GtkContainer *container)
1691 _gtk_container_queue_resize_internal (container, TRUE);
1695 gtk_container_check_resize (GtkContainer *container)
1697 g_return_if_fail (GTK_IS_CONTAINER (container));
1699 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1703 gtk_container_real_check_resize (GtkContainer *container)
1705 GtkWidget *widget = GTK_WIDGET (container);
1706 GtkAllocation allocation;
1707 GtkRequisition requisition;
1709 gtk_widget_get_preferred_size (widget,
1710 &requisition, NULL);
1711 gtk_widget_get_allocation (widget, &allocation);
1713 if (requisition.width > allocation.width ||
1714 requisition.height > allocation.height)
1716 if (GTK_IS_RESIZE_CONTAINER (container))
1718 gtk_widget_size_allocate (widget, &allocation);
1719 gtk_widget_set_allocation (widget, &allocation);
1722 gtk_widget_queue_resize (widget);
1726 gtk_container_resize_children (container);
1730 /* The container hasn't changed size but one of its children
1731 * queued a resize request. Which means that the allocation
1732 * is not sufficient for the requisition of some child.
1733 * We've already performed a size request at this point,
1734 * so we simply need to reallocate and let the allocation
1735 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1738 gtk_container_resize_children (GtkContainer *container)
1740 GtkAllocation allocation;
1743 /* resizing invariants:
1744 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1745 * containers that have an idle sizer pending must be flagged with
1748 g_return_if_fail (GTK_IS_CONTAINER (container));
1750 widget = GTK_WIDGET (container);
1751 gtk_widget_get_allocation (widget, &allocation);
1753 gtk_widget_size_allocate (widget, &allocation);
1754 gtk_widget_set_allocation (widget, &allocation);
1758 gtk_container_adjust_size_request (GtkWidget *widget,
1759 GtkOrientation orientation,
1763 GtkContainer *container;
1765 container = GTK_CONTAINER (widget);
1767 if (GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1771 border_width = container->priv->border_width;
1773 *minimum_size += border_width * 2;
1774 *natural_size += border_width * 2;
1777 /* chain up last so gtk_widget_set_size_request() values
1778 * will have a chance to overwrite our border width.
1780 parent_class->adjust_size_request (widget, orientation,
1781 minimum_size, natural_size);
1785 gtk_container_adjust_size_allocation (GtkWidget *widget,
1786 GtkOrientation orientation,
1789 gint *allocated_pos,
1790 gint *allocated_size)
1792 GtkContainer *container;
1795 container = GTK_CONTAINER (widget);
1797 if (!GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1799 parent_class->adjust_size_allocation (widget, orientation,
1800 minimum_size, natural_size, allocated_pos,
1805 border_width = container->priv->border_width;
1807 *allocated_size -= border_width * 2;
1809 /* If we get a pathological too-small allocation to hold
1810 * even the border width, leave all allocation to the actual
1811 * widget, and leave x,y unchanged. (GtkWidget's min size is
1812 * 1x1 if you're wondering why <1 and not <0)
1814 * As long as we have space, set x,y properly.
1817 if (*allocated_size < 1)
1819 *allocated_size += border_width * 2;
1823 *allocated_pos += border_width;
1824 *minimum_size -= border_width * 2;
1825 *natural_size -= border_width * 2;
1828 /* Chain up to GtkWidgetClass *after* removing our border width from
1829 * the proposed allocation size. This is because it's possible that the
1830 * widget was allocated more space than it needs in a said orientation,
1831 * if GtkWidgetClass does any alignments and thus limits the size to the
1832 * natural size... then we need that to be done *after* removing any margins
1833 * and padding values.
1835 parent_class->adjust_size_allocation (widget, orientation,
1836 minimum_size, natural_size, allocated_pos,
1841 * gtk_container_class_handle_border_width:
1842 * @klass: the class struct of a #GtkContainer subclass
1844 * Modifies a subclass of #GtkContainerClass to automatically add and
1845 * remove the border-width setting on GtkContainer. This allows the
1846 * subclass to ignore the border width in its size request and
1847 * allocate methods. The intent is for a subclass to invoke this
1848 * in its class_init function.
1850 * gtk_container_class_handle_border_width() is necessary because it
1851 * would break API too badly to make this behavior the default. So
1852 * subclasses must "opt in" to the parent class handling border_width
1856 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1858 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1860 klass->handle_border_width = TRUE;
1864 * gtk_container_forall:
1865 * @container: a #GtkContainer
1866 * @callback: a callback
1867 * @callback_data: callback user data
1869 * Invokes @callback on each child of @container, including children
1870 * that are considered "internal" (implementation details of the
1871 * container). "Internal" children generally weren't added by the user
1872 * of the container, but were added by the container implementation
1873 * itself. Most applications should use gtk_container_foreach(),
1874 * rather than gtk_container_forall().
1877 gtk_container_forall (GtkContainer *container,
1878 GtkCallback callback,
1879 gpointer callback_data)
1881 GtkContainerClass *class;
1883 g_return_if_fail (GTK_IS_CONTAINER (container));
1884 g_return_if_fail (callback != NULL);
1886 class = GTK_CONTAINER_GET_CLASS (container);
1889 class->forall (container, TRUE, callback, callback_data);
1893 * gtk_container_foreach:
1894 * @container: a #GtkContainer
1895 * @callback: (scope call): a callback
1896 * @callback_data: callback user data
1898 * Invokes @callback on each non-internal child of @container. See
1899 * gtk_container_forall() for details on what constitutes an
1900 * "internal" child. Most applications should use
1901 * gtk_container_foreach(), rather than gtk_container_forall().
1904 gtk_container_foreach (GtkContainer *container,
1905 GtkCallback callback,
1906 gpointer callback_data)
1908 GtkContainerClass *class;
1910 g_return_if_fail (GTK_IS_CONTAINER (container));
1911 g_return_if_fail (callback != NULL);
1913 class = GTK_CONTAINER_GET_CLASS (container);
1916 class->forall (container, FALSE, callback, callback_data);
1920 * gtk_container_set_focus_child:
1921 * @container: a #GtkContainer
1922 * @child: (allow-none): a #GtkWidget, or %NULL
1924 * Sets, or unsets if @child is %NULL, the focused child of @container.
1926 * This function emits the GtkContainer::set_focus_child signal of
1927 * @container. Implementations of #GtkContainer can override the
1928 * default behaviour by overriding the class closure of this signal.
1930 * This is function is mostly meant to be used by widgets. Applications can use
1931 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1934 gtk_container_set_focus_child (GtkContainer *container,
1937 g_return_if_fail (GTK_IS_CONTAINER (container));
1939 g_return_if_fail (GTK_IS_WIDGET (child));
1941 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1945 * gtk_container_get_focus_child:
1946 * @container: a #GtkContainer
1948 * Returns the current focus child widget inside @container. This is not the
1949 * currently focused widget. That can be obtained by calling
1950 * gtk_window_get_focus().
1952 * Returns: The child widget which will recieve the focus inside @container when
1953 * the @conatiner is focussed, or %NULL if none is set.
1958 gtk_container_get_focus_child (GtkContainer *container)
1960 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1962 return container->priv->focus_child;
1966 * gtk_container_get_children:
1967 * @container: a #GtkContainer
1969 * Returns the container's non-internal children. See
1970 * gtk_container_forall() for details on what constitutes an "internal" child.
1972 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1975 gtk_container_get_children (GtkContainer *container)
1977 GList *children = NULL;
1979 gtk_container_foreach (container,
1980 gtk_container_children_callback,
1983 return g_list_reverse (children);
1987 gtk_container_child_position_callback (GtkWidget *widget,
1988 gpointer client_data)
1994 } *data = client_data;
1997 if (data->child == widget)
1998 data->index = data->i;
2002 gtk_container_child_default_composite_name (GtkContainer *container,
2012 /* fallback implementation */
2016 gtk_container_forall (container,
2017 gtk_container_child_position_callback,
2020 name = g_strdup_printf ("%s-%u",
2021 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2028 _gtk_container_child_composite_name (GtkContainer *container,
2031 gboolean composite_child;
2033 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2034 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2035 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2037 g_object_get (child, "composite-child", &composite_child, NULL);
2038 if (composite_child)
2040 static GQuark quark_composite_name = 0;
2043 if (!quark_composite_name)
2044 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2046 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2049 GtkContainerClass *class;
2051 class = GTK_CONTAINER_GET_CLASS (container);
2052 if (class->composite_name)
2053 name = class->composite_name (container, child);
2056 name = g_strdup (name);
2067 } ComputeExpandData;
2070 gtk_container_compute_expand_callback (GtkWidget *widget,
2071 gpointer client_data)
2073 ComputeExpandData *data = client_data;
2075 /* note that we don't get_expand on the child if we already know we
2076 * have to expand, so we only recurse into children until we find
2077 * one that expands and then we basically don't do any more
2078 * work. This means that we can leave some children in a
2079 * need_compute_expand state, which is fine, as long as GtkWidget
2080 * doesn't rely on an invariant that "if a child has
2081 * need_compute_expand, its parents also do"
2083 * gtk_widget_compute_expand() always returns FALSE if the
2084 * child is !visible so that's taken care of.
2086 data->hexpand = data->hexpand ||
2087 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2089 data->vexpand = data->vexpand ||
2090 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2094 gtk_container_compute_expand (GtkWidget *widget,
2095 gboolean *hexpand_p,
2096 gboolean *vexpand_p)
2098 ComputeExpandData data;
2100 data.hexpand = FALSE;
2101 data.vexpand = FALSE;
2103 gtk_container_forall (GTK_CONTAINER (widget),
2104 gtk_container_compute_expand_callback,
2107 *hexpand_p = data.hexpand;
2108 *vexpand_p = data.vexpand;
2112 gtk_container_real_set_focus_child (GtkContainer *container,
2115 GtkContainerPrivate *priv;
2117 g_return_if_fail (GTK_IS_CONTAINER (container));
2118 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2120 priv = container->priv;
2122 if (child != priv->focus_child)
2124 if (priv->focus_child)
2125 g_object_unref (priv->focus_child);
2126 priv->focus_child = child;
2127 if (priv->focus_child)
2128 g_object_ref (priv->focus_child);
2132 /* check for h/v adjustments
2134 if (priv->focus_child)
2136 GtkAdjustment *hadj;
2137 GtkAdjustment *vadj;
2138 GtkAllocation allocation;
2139 GtkWidget *focus_child;
2142 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2143 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2147 focus_child = priv->focus_child;
2148 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2150 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2153 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2156 gtk_widget_get_allocation (priv->focus_child, &allocation);
2160 gtk_widget_get_allocation (focus_child, &allocation);
2163 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2166 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2172 get_focus_chain (GtkContainer *container)
2174 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2177 /* same as gtk_container_get_children, except it includes internals
2180 gtk_container_get_all_children (GtkContainer *container)
2182 GList *children = NULL;
2184 gtk_container_forall (container,
2185 gtk_container_children_callback,
2191 static GtkWidgetPath *
2192 gtk_container_real_get_path_for_child (GtkContainer *container,
2195 GtkStyleContext *context;
2196 GtkWidgetPath *path;
2199 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2200 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
2202 /* Copy any permanent classes to the path */
2203 classes = gtk_style_context_list_classes (context);
2210 classes = classes->next;
2212 gtk_widget_path_iter_add_class (path, -1, cur->data);
2213 g_list_free_1 (cur);
2220 gtk_container_focus (GtkWidget *widget,
2221 GtkDirectionType direction)
2224 GList *sorted_children;
2226 GtkContainer *container;
2227 GtkContainerPrivate *priv;
2229 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2231 container = GTK_CONTAINER (widget);
2232 priv = container->priv;
2236 if (gtk_widget_get_can_focus (widget))
2238 if (!gtk_widget_has_focus (widget))
2240 gtk_widget_grab_focus (widget);
2246 /* Get a list of the containers children, allowing focus
2247 * chain to override.
2249 if (priv->has_focus_chain)
2250 children = g_list_copy (get_focus_chain (container));
2252 children = gtk_container_get_all_children (container);
2254 if (priv->has_focus_chain &&
2255 (direction == GTK_DIR_TAB_FORWARD ||
2256 direction == GTK_DIR_TAB_BACKWARD))
2258 sorted_children = g_list_copy (children);
2260 if (direction == GTK_DIR_TAB_BACKWARD)
2261 sorted_children = g_list_reverse (sorted_children);
2264 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2266 return_val = gtk_container_focus_move (container, sorted_children, direction);
2268 g_list_free (sorted_children);
2269 g_list_free (children);
2276 tab_compare (gconstpointer a,
2280 GtkAllocation child1_allocation, child2_allocation;
2281 const GtkWidget *child1 = a;
2282 const GtkWidget *child2 = b;
2283 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2286 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2287 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2289 y1 = child1_allocation.y + child1_allocation.height / 2;
2290 y2 = child2_allocation.y + child2_allocation.height / 2;
2294 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2295 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2297 if (text_direction == GTK_TEXT_DIR_RTL)
2298 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2300 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2303 return (y1 < y2) ? -1 : 1;
2307 gtk_container_focus_sort_tab (GtkContainer *container,
2309 GtkDirectionType direction,
2310 GtkWidget *old_focus)
2312 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2313 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2315 /* if we are going backwards then reverse the order
2318 if (direction == GTK_DIR_TAB_BACKWARD)
2319 children = g_list_reverse (children);
2324 /* Get coordinates of @widget's allocation with respect to
2325 * allocation of @container.
2328 get_allocation_coords (GtkContainer *container,
2330 GdkRectangle *allocation)
2332 gtk_widget_get_allocation (widget, allocation);
2334 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2335 0, 0, &allocation->x, &allocation->y);
2338 /* Look for a child in @children that is intermediate between
2339 * the focus widget and container. This widget, if it exists,
2340 * acts as the starting widget for focus navigation.
2343 find_old_focus (GtkContainer *container,
2346 GList *tmp_list = children;
2349 GtkWidget *child = tmp_list->data;
2350 GtkWidget *widget = child;
2352 while (widget && widget != (GtkWidget *)container)
2356 parent = gtk_widget_get_parent (widget);
2358 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2367 tmp_list = tmp_list->next;
2374 old_focus_coords (GtkContainer *container,
2375 GdkRectangle *old_focus_rect)
2377 GtkWidget *widget = GTK_WIDGET (container);
2378 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2379 GtkWidget *old_focus;
2381 if (GTK_IS_WINDOW (toplevel))
2383 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2385 return get_allocation_coords (container, old_focus, old_focus_rect);
2391 typedef struct _CompareInfo CompareInfo;
2395 GtkContainer *container;
2402 up_down_compare (gconstpointer a,
2406 GdkRectangle allocation1;
2407 GdkRectangle allocation2;
2408 CompareInfo *compare = data;
2411 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2412 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2414 y1 = allocation1.y + allocation1.height / 2;
2415 y2 = allocation2.y + allocation2.height / 2;
2419 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2420 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2422 if (compare->reverse)
2423 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2425 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2428 return (y1 < y2) ? -1 : 1;
2432 gtk_container_focus_sort_up_down (GtkContainer *container,
2434 GtkDirectionType direction,
2435 GtkWidget *old_focus)
2437 CompareInfo compare;
2439 GdkRectangle old_allocation;
2441 compare.container = container;
2442 compare.reverse = (direction == GTK_DIR_UP);
2445 old_focus = find_old_focus (container, children);
2447 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2453 /* Delete widgets from list that don't match minimum criteria */
2455 compare_x1 = old_allocation.x;
2456 compare_x2 = old_allocation.x + old_allocation.width;
2458 if (direction == GTK_DIR_UP)
2459 compare_y = old_allocation.y;
2461 compare_y = old_allocation.y + old_allocation.height;
2463 tmp_list = children;
2466 GtkWidget *child = tmp_list->data;
2467 GList *next = tmp_list->next;
2468 gint child_x1, child_x2;
2469 GdkRectangle child_allocation;
2471 if (child != old_focus)
2473 if (get_allocation_coords (container, child, &child_allocation))
2475 child_x1 = child_allocation.x;
2476 child_x2 = child_allocation.x + child_allocation.width;
2478 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2479 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2480 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2482 children = g_list_delete_link (children, tmp_list);
2486 children = g_list_delete_link (children, tmp_list);
2492 compare.x = (compare_x1 + compare_x2) / 2;
2493 compare.y = old_allocation.y + old_allocation.height / 2;
2497 /* No old focus widget, need to figure out starting x,y some other way
2499 GtkAllocation allocation;
2500 GtkWidget *widget = GTK_WIDGET (container);
2501 GdkRectangle old_focus_rect;
2503 gtk_widget_get_allocation (widget, &allocation);
2505 if (old_focus_coords (container, &old_focus_rect))
2507 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2511 if (!gtk_widget_get_has_window (widget))
2512 compare.x = allocation.x + allocation.width / 2;
2514 compare.x = allocation.width / 2;
2517 if (!gtk_widget_get_has_window (widget))
2518 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2520 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2523 children = g_list_sort_with_data (children, up_down_compare, &compare);
2525 if (compare.reverse)
2526 children = g_list_reverse (children);
2532 left_right_compare (gconstpointer a,
2536 GdkRectangle allocation1;
2537 GdkRectangle allocation2;
2538 CompareInfo *compare = data;
2541 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2542 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2544 x1 = allocation1.x + allocation1.width / 2;
2545 x2 = allocation2.x + allocation2.width / 2;
2549 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2550 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2552 if (compare->reverse)
2553 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2555 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2558 return (x1 < x2) ? -1 : 1;
2562 gtk_container_focus_sort_left_right (GtkContainer *container,
2564 GtkDirectionType direction,
2565 GtkWidget *old_focus)
2567 CompareInfo compare;
2569 GdkRectangle old_allocation;
2571 compare.container = container;
2572 compare.reverse = (direction == GTK_DIR_LEFT);
2575 old_focus = find_old_focus (container, children);
2577 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2583 /* Delete widgets from list that don't match minimum criteria */
2585 compare_y1 = old_allocation.y;
2586 compare_y2 = old_allocation.y + old_allocation.height;
2588 if (direction == GTK_DIR_LEFT)
2589 compare_x = old_allocation.x;
2591 compare_x = old_allocation.x + old_allocation.width;
2593 tmp_list = children;
2596 GtkWidget *child = tmp_list->data;
2597 GList *next = tmp_list->next;
2598 gint child_y1, child_y2;
2599 GdkRectangle child_allocation;
2601 if (child != old_focus)
2603 if (get_allocation_coords (container, child, &child_allocation))
2605 child_y1 = child_allocation.y;
2606 child_y2 = child_allocation.y + child_allocation.height;
2608 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2609 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2610 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2612 children = g_list_delete_link (children, tmp_list);
2616 children = g_list_delete_link (children, tmp_list);
2622 compare.y = (compare_y1 + compare_y2) / 2;
2623 compare.x = old_allocation.x + old_allocation.width / 2;
2627 /* No old focus widget, need to figure out starting x,y some other way
2629 GtkAllocation allocation;
2630 GtkWidget *widget = GTK_WIDGET (container);
2631 GdkRectangle old_focus_rect;
2633 gtk_widget_get_allocation (widget, &allocation);
2635 if (old_focus_coords (container, &old_focus_rect))
2637 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2641 if (!gtk_widget_get_has_window (widget))
2642 compare.y = allocation.y + allocation.height / 2;
2644 compare.y = allocation.height / 2;
2647 if (!gtk_widget_get_has_window (widget))
2648 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2650 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2653 children = g_list_sort_with_data (children, left_right_compare, &compare);
2655 if (compare.reverse)
2656 children = g_list_reverse (children);
2662 * gtk_container_focus_sort:
2663 * @container: a #GtkContainer
2664 * @children: a list of descendents of @container (they don't
2665 * have to be direct children)
2666 * @direction: focus direction
2667 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2668 * to determine this automatically.
2669 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2670 * which is the only @direction we use currently,
2671 * so perhaps this argument should be removed)
2673 * Sorts @children in the correct order for focusing with
2674 * direction type @direction.
2676 * Return value: a copy of @children, sorted in correct focusing order,
2677 * with children that aren't suitable for focusing in this direction
2681 _gtk_container_focus_sort (GtkContainer *container,
2683 GtkDirectionType direction,
2684 GtkWidget *old_focus)
2686 GList *visible_children = NULL;
2690 if (gtk_widget_get_realized (children->data))
2691 visible_children = g_list_prepend (visible_children, children->data);
2692 children = children->next;
2697 case GTK_DIR_TAB_FORWARD:
2698 case GTK_DIR_TAB_BACKWARD:
2699 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2702 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2705 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2708 g_assert_not_reached ();
2714 gtk_container_focus_move (GtkContainer *container,
2716 GtkDirectionType direction)
2718 GtkContainerPrivate *priv = container->priv;
2719 GtkWidget *focus_child;
2722 focus_child = priv->focus_child;
2726 child = children->data;
2727 children = children->next;
2734 if (focus_child == child)
2738 if (gtk_widget_child_focus (child, direction))
2742 else if (gtk_widget_is_drawable (child) &&
2743 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2745 if (gtk_widget_child_focus (child, direction))
2755 gtk_container_children_callback (GtkWidget *widget,
2756 gpointer client_data)
2760 children = (GList**) client_data;
2761 *children = g_list_prepend (*children, widget);
2765 chain_widget_destroyed (GtkWidget *widget,
2768 GtkContainer *container;
2771 container = GTK_CONTAINER (user_data);
2773 chain = g_object_get_data (G_OBJECT (container),
2774 "gtk-container-focus-chain");
2776 chain = g_list_remove (chain, widget);
2778 g_signal_handlers_disconnect_by_func (widget,
2779 chain_widget_destroyed,
2782 g_object_set_data (G_OBJECT (container),
2783 I_("gtk-container-focus-chain"),
2788 * gtk_container_set_focus_chain:
2789 * @container: a #GtkContainer
2790 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2791 * the new focus chain
2793 * Sets a focus chain, overriding the one computed automatically by GTK+.
2795 * In principle each widget in the chain should be a descendant of the
2796 * container, but this is not enforced by this method, since it's allowed
2797 * to set the focus chain before you pack the widgets, or have a widget
2798 * in the chain that isn't always packed. The necessary checks are done
2799 * when the focus chain is actually traversed.
2802 gtk_container_set_focus_chain (GtkContainer *container,
2803 GList *focusable_widgets)
2807 GtkContainerPrivate *priv;
2809 g_return_if_fail (GTK_IS_CONTAINER (container));
2811 priv = container->priv;
2813 if (priv->has_focus_chain)
2814 gtk_container_unset_focus_chain (container);
2816 priv->has_focus_chain = TRUE;
2819 tmp_list = focusable_widgets;
2820 while (tmp_list != NULL)
2822 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2824 /* In principle each widget in the chain should be a descendant
2825 * of the container, but we don't want to check that here, it's
2826 * expensive and also it's allowed to set the focus chain before
2827 * you pack the widgets, or have a widget in the chain that isn't
2828 * always packed. So we check for ancestor during actual traversal.
2831 chain = g_list_prepend (chain, tmp_list->data);
2833 g_signal_connect (tmp_list->data,
2835 G_CALLBACK (chain_widget_destroyed),
2838 tmp_list = g_list_next (tmp_list);
2841 chain = g_list_reverse (chain);
2843 g_object_set_data (G_OBJECT (container),
2844 I_("gtk-container-focus-chain"),
2849 * gtk_container_get_focus_chain:
2850 * @container: a #GtkContainer
2851 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2852 * to store the focus chain of the
2853 * container, or %NULL. You should free this list
2854 * using g_list_free() when you are done with it, however
2855 * no additional reference count is added to the
2856 * individual widgets in the focus chain.
2858 * Retrieves the focus chain of the container, if one has been
2859 * set explicitly. If no focus chain has been explicitly
2860 * set, GTK+ computes the focus chain based on the positions
2861 * of the children. In that case, GTK+ stores %NULL in
2862 * @focusable_widgets and returns %FALSE.
2864 * Return value: %TRUE if the focus chain of the container
2865 * has been set explicitly.
2868 gtk_container_get_focus_chain (GtkContainer *container,
2869 GList **focus_chain)
2871 GtkContainerPrivate *priv;
2873 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2875 priv = container->priv;
2879 if (priv->has_focus_chain)
2880 *focus_chain = g_list_copy (get_focus_chain (container));
2882 *focus_chain = NULL;
2885 return priv->has_focus_chain;
2889 * gtk_container_unset_focus_chain:
2890 * @container: a #GtkContainer
2892 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2895 gtk_container_unset_focus_chain (GtkContainer *container)
2897 GtkContainerPrivate *priv;
2899 g_return_if_fail (GTK_IS_CONTAINER (container));
2901 priv = container->priv;
2903 if (priv->has_focus_chain)
2908 chain = get_focus_chain (container);
2910 priv->has_focus_chain = FALSE;
2912 g_object_set_data (G_OBJECT (container),
2913 I_("gtk-container-focus-chain"),
2917 while (tmp_list != NULL)
2919 g_signal_handlers_disconnect_by_func (tmp_list->data,
2920 chain_widget_destroyed,
2923 tmp_list = g_list_next (tmp_list);
2926 g_list_free (chain);
2931 * gtk_container_set_focus_vadjustment:
2932 * @container: a #GtkContainer
2933 * @adjustment: an adjustment which should be adjusted when the focus
2934 * is moved among the descendents of @container
2936 * Hooks up an adjustment to focus handling in a container, so when a
2937 * child of the container is focused, the adjustment is scrolled to
2938 * show that widget. This function sets the vertical alignment. See
2939 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2940 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2941 * the horizontal adjustment.
2943 * The adjustments have to be in pixel units and in the same coordinate
2944 * system as the allocation for immediate children of the container.
2947 gtk_container_set_focus_vadjustment (GtkContainer *container,
2948 GtkAdjustment *adjustment)
2950 g_return_if_fail (GTK_IS_CONTAINER (container));
2952 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2955 g_object_ref (adjustment);
2957 g_object_set_qdata_full (G_OBJECT (container),
2964 * gtk_container_get_focus_vadjustment:
2965 * @container: a #GtkContainer
2967 * Retrieves the vertical focus adjustment for the container. See
2968 * gtk_container_set_focus_vadjustment().
2970 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2971 * none has been set.
2974 gtk_container_get_focus_vadjustment (GtkContainer *container)
2976 GtkAdjustment *vadjustment;
2978 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2980 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2986 * gtk_container_set_focus_hadjustment:
2987 * @container: a #GtkContainer
2988 * @adjustment: an adjustment which should be adjusted when the focus is
2989 * moved among the descendents of @container
2991 * Hooks up an adjustment to focus handling in a container, so when a child
2992 * of the container is focused, the adjustment is scrolled to show that
2993 * widget. This function sets the horizontal alignment.
2994 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2995 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2996 * the vertical adjustment.
2998 * The adjustments have to be in pixel units and in the same coordinate
2999 * system as the allocation for immediate children of the container.
3002 gtk_container_set_focus_hadjustment (GtkContainer *container,
3003 GtkAdjustment *adjustment)
3005 g_return_if_fail (GTK_IS_CONTAINER (container));
3007 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3010 g_object_ref (adjustment);
3012 g_object_set_qdata_full (G_OBJECT (container),
3019 * gtk_container_get_focus_hadjustment:
3020 * @container: a #GtkContainer
3022 * Retrieves the horizontal focus adjustment for the container. See
3023 * gtk_container_set_focus_hadjustment ().
3025 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3026 * none has been set.
3029 gtk_container_get_focus_hadjustment (GtkContainer *container)
3031 GtkAdjustment *hadjustment;
3033 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3035 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3042 gtk_container_show_all (GtkWidget *widget)
3044 g_return_if_fail (GTK_IS_CONTAINER (widget));
3046 gtk_container_foreach (GTK_CONTAINER (widget),
3047 (GtkCallback) gtk_widget_show_all,
3049 gtk_widget_show (widget);
3053 gtk_container_draw_child (GtkWidget *child,
3054 gpointer client_data)
3057 GtkWidget *container;
3059 } *data = client_data;
3061 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3067 gtk_container_draw (GtkWidget *widget,
3071 GtkWidget *container;
3075 data.container = widget;
3078 gtk_container_forall (GTK_CONTAINER (widget),
3079 gtk_container_draw_child,
3086 gtk_container_map_child (GtkWidget *child,
3087 gpointer client_data)
3089 if (gtk_widget_get_visible (child) &&
3090 gtk_widget_get_child_visible (child) &&
3091 !gtk_widget_get_mapped (child))
3092 gtk_widget_map (child);
3096 gtk_container_map (GtkWidget *widget)
3098 gtk_widget_set_mapped (widget, TRUE);
3100 gtk_container_forall (GTK_CONTAINER (widget),
3101 gtk_container_map_child,
3104 if (gtk_widget_get_has_window (widget))
3105 gdk_window_show (gtk_widget_get_window (widget));
3109 gtk_container_unmap (GtkWidget *widget)
3111 gtk_widget_set_mapped (widget, FALSE);
3113 if (gtk_widget_get_has_window (widget))
3114 gdk_window_hide (gtk_widget_get_window (widget));
3116 gtk_container_forall (GTK_CONTAINER (widget),
3117 (GtkCallback)gtk_widget_unmap,
3122 * gtk_container_propagate_draw:
3123 * @container: a #GtkContainer
3124 * @child: a child of @container
3125 * @cr: Cairo context as passed to the container. If you want to use @cr
3126 * in container's draw function, consider using cairo_save() and
3127 * cairo_restore() before calling this function.
3129 * When a container receives a call to the draw function, it must send
3130 * synthetic #GtkWidget::draw calls to all children that don't have their
3131 * own #GdkWindows. This function provides a convenient way of doing this.
3132 * A container, when it receives a call to its #GtkWidget::draw function,
3133 * calls gtk_container_propagate_draw() once for each child, passing in
3134 * the @cr the container received.
3136 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3137 * and deciding whether the draw needs to be sent to the child. It is a
3138 * convenient and optimized way of getting the same effect as calling
3139 * gtk_widget_draw() on the child directly.
3141 * In most cases, a container can simply either inherit the
3142 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3143 * and then chain to the ::draw implementation from #GtkContainer.
3146 gtk_container_propagate_draw (GtkContainer *container,
3150 GdkEventExpose *event;
3151 GtkAllocation allocation;
3152 GdkWindow *window, *w;
3155 g_return_if_fail (GTK_IS_CONTAINER (container));
3156 g_return_if_fail (GTK_IS_WIDGET (child));
3157 g_return_if_fail (cr != NULL);
3159 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3161 event = _gtk_cairo_get_event (cr);
3164 if (gtk_widget_get_has_window (child) ||
3165 gtk_widget_get_window (child) != event->window)
3171 /* translate coordinates. Ugly business, that. */
3172 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3174 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3184 window = gtk_widget_get_window (GTK_WIDGET (container));
3186 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3189 gdk_window_get_position (w, &wx, &wy);
3200 if (!gtk_widget_get_has_window (child))
3202 gtk_widget_get_allocation (child, &allocation);
3207 cairo_translate (cr, x, y);
3209 _gtk_widget_draw_internal (child, cr, TRUE);
3215 _gtk_container_get_need_resize (GtkContainer *container)
3217 return container->priv->need_resize;
3221 _gtk_container_set_need_resize (GtkContainer *container,
3222 gboolean need_resize)
3224 container->priv->need_resize = need_resize;
3228 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3230 return container->priv->reallocate_redraws;
3234 * gtk_container_get_path_for_child:
3235 * @container: a #GtkContainer
3236 * @child: a child of @container
3238 * Returns a newly created widget path representing all the widget hierarchy
3239 * from the toplevel down to @child (this one not being included).
3241 * Returns: A newly created #GtkWidgetPath
3244 gtk_container_get_path_for_child (GtkContainer *container,
3247 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3248 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3249 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3251 return GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);