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 "gtkwindow.h"
43 #include "gtktoolbar.h"
44 #include <gobject/gobjectnotifyqueue.c>
45 #include <gobject/gvaluecollector.h>
49 * SECTION:gtkcontainer
50 * @Short_description: Base class for widgets which contain other widgets
51 * @Title: GtkContainer
53 * A GTK+ user interface is constructed by nesting widgets inside widgets.
54 * Container widgets are the inner nodes in the resulting tree of widgets:
55 * they contain other widgets. So, for example, you might have a #GtkWindow
56 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
57 * of a textual label inside the frame, you might replace the #GtkLabel widget
58 * with a #GtkImage widget.
60 * There are two major kinds of container widgets in GTK+. Both are subclasses
61 * of the abstract GtkContainer base class.
63 * The first type of container widget has a single child widget and derives
64 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
65 * add some kind of functionality to the child. For example, a #GtkButton makes
66 * it's child into a clickable button; a #GtkFrame draws a frame around it's child
67 * and a #GtkWindow places it's child widget inside a top-level window.
69 * The second type of container can have more than one child; it's purpose is to
70 * manage <emphasis>layout</emphasis>. This means that these containers assign
71 * sizes and positions to their children. For example, a #GtkHBox arranges it's
72 * children in a horizontal row, and a #GtkTable arranges the widgets it contains
73 * in a two-dimensional grid.
75 * <refsect2 id="container-geometry-management">
76 * <title>Height for width geometry management</title>
78 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
79 * Height-for-width means that a widget can change how much vertical space it needs,
80 * depending on the amount of horizontal space that it is given (and similar for
83 * There are some things to keep in mind when implementing container widgets
84 * that make use of GTK+'s height for width geometry management system; first
85 * of all it's important to note that a container must prioritize one of it's
86 * dimensions, that is to say that a widget or container can only have a
87 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
88 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
89 * must be able to respond to the APIs for both dimensions, i.e. even if a
90 * widget has a request mode that is height-for-width, it is possible that
91 * it's parent will request it's sizes using the width-for-height APIs.
93 * To ensure that everything works properly, here are some guidelines to follow
94 * when implementing height-for-width (or width-for-height) containers.
96 * Each request mode has 2 virtual methods involved. Height-for-width apis run
97 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
98 * When handling requests in the opposite #GtkSizeRequestMode it is important that
99 * every widget request at least enough space to display all of it's content at all times.
101 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
102 * the container must return the height for minimum width, this is easily achieved by
103 * simply calling the reverse apis implemented for itself as follows:
105 * <programlisting><![CDATA[
107 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
109 * if (i_am_in_height_for_width_mode)
113 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
114 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
115 * min_height, nat_height);
119 * ... many containers support both request modes, execute the real width-for-height
120 * request here by returning the collective heights of all widgets that are
121 * stacked vertically (or whatever is appropriate for this container) ...
124 * ]]></programlisting>
126 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
127 * that is height-for-width, it then only needs to return the base minimum width like so:
129 * <programlisting><![CDATA[
131 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
132 * gint *min_width, gint *nat_width)
134 * if (i_am_in_height_for_width_mode)
136 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
140 * ... execute the real width-for-height request here based on required width
141 * of the children collectively if the container were to be allocated the said height ...
144 * ]]></programlisting>
146 * Furthermore, in order to ensure correct height-for-width requests it is important
147 * to check the input width against the real required minimum width, this can
148 * easily be achieved as follows:
150 * <programlisting><![CDATA[
152 * foo_container_get_preferred_height_for_width (GtkWidget *widget, gint for_width,
153 * gint *min_height, gint *nat_height)
155 * if (i_am_in_height_for_width_mode)
159 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
161 * for_width = MAX (min_width, for_width);
163 * execute_real_height_for_width_request_code (widget, for_width, min_height, nat_height);
167 * ... fall back on virtual results as mentioned in the previous example ...
170 * ]]></programlisting>
172 * Height for width requests are generally implemented in terms of a virtual allocation
173 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
174 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
175 * gtk_widget_get_preferred_width() for each of its children.
177 * For each potential group of children that are lined up horizontally the values returned by
178 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures;
179 * any child spacing should be removed from the input @for_width and then the collective size be
180 * allocated using the gtk_distribute_natural_allocation() convenience function
182 * The container will then move on to request the preferred height for each child by using
183 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
185 * When it comes time to allocate a height-for-width container, it's again important
186 * to consider that a container has to prioritize one dimension over the other. So if
187 * a container is a height-for-width container it must first allocate all widgets horizontally
188 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
189 * extra space (if and where appropriate) for the widget to expand.
191 * After adding all the expand space, the container assumes it was allocated sufficient
192 * height to fit all of its content. At this time, the container must use the total horizontal sizes
193 * of each widget to request the height-for-width of each of its children and store the requests in a
194 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
195 * be generalized into the heights and widths of rows and columns).
196 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
197 * while this time considering the allocated height of the widget minus any vertical spacing
198 * that the container adds. Then vertical expand space should be added where appropriate if available
199 * and go on to actually allocating the child widgets.
201 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
202 * to learn more about implementing height-for-width geometry management for widgets.
205 * <refsect2 id="child-properties">
206 * <title>Child properties</title>
208 * GtkContainer introduces <emphasis>child properties</emphasis>.
209 * These are object properties that are not specific
210 * to either the container or the contained widget, but rather to their relation.
211 * Typical examples of child properties are the position or pack-type of a widget
212 * which is contained in a #GtkBox.
214 * Use gtk_container_class_install_child_property() to install child properties
215 * for a container class and gtk_container_class_find_child_property() or
216 * gtk_container_class_list_child_properties() to get information about existing
219 * To set the value of a child property, use gtk_container_child_set_property(),
220 * gtk_container_child_set() or gtk_container_child_set_valist().
221 * To obtain the value of a child property, use
222 * gtk_container_child_get_property(), gtk_container_child_get() or
223 * gtk_container_child_get_valist(). To emit notification about child property
224 * changes, use gtk_widget_child_notify().
227 * <refsect2 id="GtkContainer-BUILDER-UI">
228 * <title>GtkContainer as GtkBuildable</title>
230 * The GtkContainer implementation of the GtkBuildable interface
231 * supports a <packing> element for children, which can
232 * contain multiple <property> elements that specify
233 * child properties for the child.
235 * <title>Child properties in UI definitions</title>
236 * <programlisting><![CDATA[
237 * <object class="GtkVBox">
239 * <object class="GtkLabel"/>
241 * <property name="pack-type">start</property>
245 * ]]></programlisting>
247 * Since 2.16, child properties can also be marked as translatable using
248 * the same "translatable", "comments" and "context" attributes that are used
249 * for regular properties.
255 struct _GtkContainerPrivate
257 GtkWidget *focus_child;
259 guint border_width : 16;
261 guint has_focus_chain : 1;
262 guint need_resize : 1;
263 guint reallocate_redraws : 1;
264 guint resize_mode : 2;
282 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
283 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
286 /* --- prototypes --- */
287 static void gtk_container_base_class_init (GtkContainerClass *klass);
288 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
289 static void gtk_container_class_init (GtkContainerClass *klass);
290 static void gtk_container_init (GtkContainer *container);
291 static void gtk_container_destroy (GtkWidget *widget);
292 static void gtk_container_set_property (GObject *object,
296 static void gtk_container_get_property (GObject *object,
300 static void gtk_container_add_unimplemented (GtkContainer *container,
302 static void gtk_container_remove_unimplemented (GtkContainer *container,
304 static void gtk_container_real_check_resize (GtkContainer *container);
305 static gboolean gtk_container_focus (GtkWidget *widget,
306 GtkDirectionType direction);
307 static void gtk_container_real_set_focus_child (GtkContainer *container,
310 static gboolean gtk_container_focus_move (GtkContainer *container,
312 GtkDirectionType direction);
313 static void gtk_container_children_callback (GtkWidget *widget,
314 gpointer client_data);
315 static void gtk_container_show_all (GtkWidget *widget);
316 static void gtk_container_hide_all (GtkWidget *widget);
317 static gint gtk_container_draw (GtkWidget *widget,
319 static void gtk_container_map (GtkWidget *widget);
320 static void gtk_container_unmap (GtkWidget *widget);
321 static void gtk_container_adjust_size_request (GtkWidget *widget,
322 GtkOrientation orientation,
326 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
327 GtkAllocation *allocation);
329 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
333 static void gtk_container_buildable_init (GtkBuildableIface *iface);
334 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
338 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
341 const gchar *tagname,
342 GMarkupParser *parser,
344 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
347 const gchar *tagname,
351 /* --- variables --- */
352 static const gchar vadjustment_key[] = "gtk-vadjustment";
353 static guint vadjustment_key_id = 0;
354 static const gchar hadjustment_key[] = "gtk-hadjustment";
355 static guint hadjustment_key_id = 0;
356 static GSList *container_resize_queue = NULL;
357 static guint container_signals[LAST_SIGNAL] = { 0 };
358 static GtkWidgetClass *parent_class = NULL;
359 extern GParamSpecPool *_gtk_widget_child_property_pool;
360 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
361 static GtkBuildableIface *parent_buildable_iface;
364 /* --- functions --- */
366 gtk_container_get_type (void)
368 static GType container_type = 0;
372 const GTypeInfo container_info =
374 sizeof (GtkContainerClass),
375 (GBaseInitFunc) gtk_container_base_class_init,
376 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
377 (GClassInitFunc) gtk_container_class_init,
378 NULL /* class_finalize */,
379 NULL /* class_data */,
380 sizeof (GtkContainer),
382 (GInstanceInitFunc) gtk_container_init,
383 NULL, /* value_table */
386 const GInterfaceInfo buildable_info =
388 (GInterfaceInitFunc) gtk_container_buildable_init,
394 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
395 &container_info, G_TYPE_FLAG_ABSTRACT);
397 g_type_add_interface_static (container_type,
403 return container_type;
407 gtk_container_base_class_init (GtkContainerClass *class)
409 /* reset instance specifc class fields that don't get inherited */
410 class->set_child_property = NULL;
411 class->get_child_property = NULL;
415 gtk_container_base_class_finalize (GtkContainerClass *class)
419 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
420 for (node = list; node; node = node->next)
422 GParamSpec *pspec = node->data;
424 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
425 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
426 g_param_spec_unref (pspec);
432 gtk_container_class_init (GtkContainerClass *class)
434 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
435 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
437 parent_class = g_type_class_peek_parent (class);
439 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
440 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
442 gobject_class->set_property = gtk_container_set_property;
443 gobject_class->get_property = gtk_container_get_property;
445 widget_class->destroy = gtk_container_destroy;
446 widget_class->show_all = gtk_container_show_all;
447 widget_class->hide_all = gtk_container_hide_all;
448 widget_class->draw = gtk_container_draw;
449 widget_class->map = gtk_container_map;
450 widget_class->unmap = gtk_container_unmap;
451 widget_class->focus = gtk_container_focus;
453 widget_class->adjust_size_request = gtk_container_adjust_size_request;
454 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
456 class->add = gtk_container_add_unimplemented;
457 class->remove = gtk_container_remove_unimplemented;
458 class->check_resize = gtk_container_real_check_resize;
459 class->forall = NULL;
460 class->set_focus_child = gtk_container_real_set_focus_child;
461 class->child_type = NULL;
462 class->composite_name = gtk_container_child_default_composite_name;
464 g_object_class_install_property (gobject_class,
466 g_param_spec_enum ("resize-mode",
468 P_("Specify how resize events are handled"),
469 GTK_TYPE_RESIZE_MODE,
471 GTK_PARAM_READWRITE));
472 g_object_class_install_property (gobject_class,
474 g_param_spec_uint ("border-width",
476 P_("The width of the empty border outside the containers children"),
480 GTK_PARAM_READWRITE));
481 g_object_class_install_property (gobject_class,
483 g_param_spec_object ("child",
485 P_("Can be used to add a new child to the container"),
487 GTK_PARAM_WRITABLE));
488 container_signals[ADD] =
489 g_signal_new (I_("add"),
490 G_OBJECT_CLASS_TYPE (gobject_class),
492 G_STRUCT_OFFSET (GtkContainerClass, add),
494 _gtk_marshal_VOID__OBJECT,
497 container_signals[REMOVE] =
498 g_signal_new (I_("remove"),
499 G_OBJECT_CLASS_TYPE (gobject_class),
501 G_STRUCT_OFFSET (GtkContainerClass, remove),
503 _gtk_marshal_VOID__OBJECT,
506 container_signals[CHECK_RESIZE] =
507 g_signal_new (I_("check-resize"),
508 G_OBJECT_CLASS_TYPE (gobject_class),
510 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
512 _gtk_marshal_VOID__VOID,
514 container_signals[SET_FOCUS_CHILD] =
515 g_signal_new (I_("set-focus-child"),
516 G_OBJECT_CLASS_TYPE (gobject_class),
518 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
520 _gtk_marshal_VOID__OBJECT,
524 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
528 gtk_container_buildable_init (GtkBuildableIface *iface)
530 parent_buildable_iface = g_type_interface_peek_parent (iface);
531 iface->add_child = gtk_container_buildable_add_child;
532 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
533 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
537 gtk_container_buildable_add_child (GtkBuildable *buildable,
544 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
546 else if (GTK_IS_WIDGET (child) &&
547 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
549 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
552 g_warning ("Cannot add an object of type %s to a container of type %s",
553 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
557 gtk_container_buildable_set_child_property (GtkContainer *container,
564 GValue gvalue = { 0, };
565 GError *error = NULL;
567 pspec = gtk_container_class_find_child_property
568 (G_OBJECT_GET_CLASS (container), name);
571 g_warning ("%s does not have a property called %s",
572 g_type_name (G_OBJECT_TYPE (container)), name);
576 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
578 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
579 g_type_name (G_OBJECT_TYPE (container)),
582 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
584 g_error_free (error);
588 gtk_container_child_set_property (container, child, name, &gvalue);
589 g_value_unset (&gvalue);
594 GtkContainer *container;
596 gchar *child_prop_name;
598 gboolean translatable;
599 } PackingPropertiesData;
602 attributes_start_element (GMarkupParseContext *context,
603 const gchar *element_name,
605 const gchar **values,
609 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
612 if (strcmp (element_name, "property") == 0)
614 for (i = 0; names[i]; i++)
615 if (strcmp (names[i], "name") == 0)
616 parser_data->child_prop_name = g_strdup (values[i]);
617 else if (strcmp (names[i], "translatable") == 0)
619 if (!_gtk_builder_boolean_from_string (values[1],
620 &parser_data->translatable,
624 else if (strcmp (names[i], "comments") == 0)
625 ; /* for translators */
626 else if (strcmp (names[i], "context") == 0)
627 parser_data->context = g_strdup (values[1]);
629 g_warning ("Unsupported attribute for GtkContainer Child "
630 "property: %s\n", names[i]);
632 else if (strcmp (element_name, "packing") == 0)
635 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
639 attributes_text_element (GMarkupParseContext *context,
645 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
648 if (!parser_data->child_prop_name)
651 if (parser_data->translatable && text_len)
654 domain = gtk_builder_get_translation_domain (parser_data->builder);
656 value = _gtk_builder_parser_translate (domain,
657 parser_data->context,
662 value = g_strdup (text);
665 gtk_container_buildable_set_child_property (parser_data->container,
666 parser_data->builder,
668 parser_data->child_prop_name,
671 g_free (parser_data->child_prop_name);
672 g_free (parser_data->context);
674 parser_data->child_prop_name = NULL;
675 parser_data->context = NULL;
676 parser_data->translatable = FALSE;
679 static const GMarkupParser attributes_parser =
681 attributes_start_element,
683 attributes_text_element,
687 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
690 const gchar *tagname,
691 GMarkupParser *parser,
694 PackingPropertiesData *parser_data;
696 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
697 tagname, parser, data))
700 if (child && strcmp (tagname, "packing") == 0)
702 parser_data = g_slice_new0 (PackingPropertiesData);
703 parser_data->builder = builder;
704 parser_data->container = GTK_CONTAINER (buildable);
705 parser_data->child = GTK_WIDGET (child);
706 parser_data->child_prop_name = NULL;
708 *parser = attributes_parser;
717 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
720 const gchar *tagname,
723 if (strcmp (tagname, "packing") == 0)
725 g_slice_free (PackingPropertiesData, (gpointer)data);
730 if (parent_buildable_iface->custom_tag_end)
731 parent_buildable_iface->custom_tag_end (buildable, builder,
732 child, tagname, data);
737 * gtk_container_child_type:
738 * @container: a #GtkContainer
740 * Returns the type of the children supported by the container.
742 * Note that this may return %G_TYPE_NONE to indicate that no more
743 * children can be added, e.g. for a #GtkPaned which already has two
746 * Return value: a #GType.
749 gtk_container_child_type (GtkContainer *container)
752 GtkContainerClass *class;
754 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
756 class = GTK_CONTAINER_GET_CLASS (container);
757 if (class->child_type)
758 slot = class->child_type (container);
765 /* --- GtkContainer child property mechanism --- */
767 container_get_child_property (GtkContainer *container,
772 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
774 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
778 container_set_child_property (GtkContainer *container,
782 GObjectNotifyQueue *nqueue)
784 GValue tmp_value = { 0, };
785 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
787 /* provide a copy to work from, convert (if necessary) and validate */
788 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
789 if (!g_value_transform (value, &tmp_value))
790 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
792 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
793 G_VALUE_TYPE_NAME (value));
794 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
796 gchar *contents = g_strdup_value_contents (value);
798 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
800 G_VALUE_TYPE_NAME (value),
802 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
807 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
808 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
810 g_value_unset (&tmp_value);
814 * gtk_container_child_get_valist:
815 * @container: a #GtkContainer
816 * @child: a widget which is a child of @container
817 * @first_property_name: the name of the first property to get
818 * @var_args: return location for the first property, followed
819 * optionally by more name/return location pairs, followed by %NULL
821 * Gets the values of one or more child properties for @child and @container.
824 gtk_container_child_get_valist (GtkContainer *container,
826 const gchar *first_property_name,
831 g_return_if_fail (GTK_IS_CONTAINER (container));
832 g_return_if_fail (GTK_IS_WIDGET (child));
833 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
835 g_object_ref (container);
836 g_object_ref (child);
838 name = first_property_name;
841 GValue value = { 0, };
845 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
847 G_OBJECT_TYPE (container),
851 g_warning ("%s: container class `%s' has no child property named `%s'",
853 G_OBJECT_TYPE_NAME (container),
857 if (!(pspec->flags & G_PARAM_READABLE))
859 g_warning ("%s: child property `%s' of container class `%s' is not readable",
862 G_OBJECT_TYPE_NAME (container));
865 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
866 container_get_child_property (container, child, pspec, &value);
867 G_VALUE_LCOPY (&value, var_args, 0, &error);
870 g_warning ("%s: %s", G_STRLOC, error);
872 g_value_unset (&value);
875 g_value_unset (&value);
876 name = va_arg (var_args, gchar*);
879 g_object_unref (child);
880 g_object_unref (container);
884 * gtk_container_child_get_property:
885 * @container: a #GtkContainer
886 * @child: a widget which is a child of @container
887 * @property_name: the name of the property to get
888 * @value: a location to return the value
890 * Gets the value of a child property for @child and @container.
893 gtk_container_child_get_property (GtkContainer *container,
895 const gchar *property_name,
900 g_return_if_fail (GTK_IS_CONTAINER (container));
901 g_return_if_fail (GTK_IS_WIDGET (child));
902 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
903 g_return_if_fail (property_name != NULL);
904 g_return_if_fail (G_IS_VALUE (value));
906 g_object_ref (container);
907 g_object_ref (child);
908 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
909 G_OBJECT_TYPE (container), TRUE);
911 g_warning ("%s: container class `%s' has no child property named `%s'",
913 G_OBJECT_TYPE_NAME (container),
915 else if (!(pspec->flags & G_PARAM_READABLE))
916 g_warning ("%s: child property `%s' of container class `%s' is not readable",
919 G_OBJECT_TYPE_NAME (container));
922 GValue *prop_value, tmp_value = { 0, };
924 /* auto-conversion of the callers value type
926 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
928 g_value_reset (value);
931 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
933 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
935 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
936 G_VALUE_TYPE_NAME (value));
937 g_object_unref (child);
938 g_object_unref (container);
943 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
944 prop_value = &tmp_value;
946 container_get_child_property (container, child, pspec, prop_value);
947 if (prop_value != value)
949 g_value_transform (prop_value, value);
950 g_value_unset (&tmp_value);
953 g_object_unref (child);
954 g_object_unref (container);
958 * gtk_container_child_set_valist:
959 * @container: a #GtkContainer
960 * @child: a widget which is a child of @container
961 * @first_property_name: the name of the first property to set
962 * @var_args: a %NULL-terminated list of property names and values, starting
963 * with @first_prop_name
965 * Sets one or more child properties for @child and @container.
968 gtk_container_child_set_valist (GtkContainer *container,
970 const gchar *first_property_name,
973 GObjectNotifyQueue *nqueue;
976 g_return_if_fail (GTK_IS_CONTAINER (container));
977 g_return_if_fail (GTK_IS_WIDGET (child));
978 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
980 g_object_ref (container);
981 g_object_ref (child);
983 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
984 name = first_property_name;
987 GValue value = { 0, };
989 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
991 G_OBJECT_TYPE (container),
995 g_warning ("%s: container class `%s' has no child property named `%s'",
997 G_OBJECT_TYPE_NAME (container),
1001 if (!(pspec->flags & G_PARAM_WRITABLE))
1003 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1006 G_OBJECT_TYPE_NAME (container));
1009 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1010 G_VALUE_COLLECT (&value, var_args, 0, &error);
1013 g_warning ("%s: %s", G_STRLOC, error);
1016 /* we purposely leak the value here, it might not be
1017 * in a sane state if an error condition occoured
1021 container_set_child_property (container, child, pspec, &value, nqueue);
1022 g_value_unset (&value);
1023 name = va_arg (var_args, gchar*);
1025 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1027 g_object_unref (container);
1028 g_object_unref (child);
1032 * gtk_container_child_set_property:
1033 * @container: a #GtkContainer
1034 * @child: a widget which is a child of @container
1035 * @property_name: the name of the property to set
1036 * @value: the value to set the property to
1038 * Sets a child property for @child and @container.
1041 gtk_container_child_set_property (GtkContainer *container,
1043 const gchar *property_name,
1044 const GValue *value)
1046 GObjectNotifyQueue *nqueue;
1049 g_return_if_fail (GTK_IS_CONTAINER (container));
1050 g_return_if_fail (GTK_IS_WIDGET (child));
1051 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1052 g_return_if_fail (property_name != NULL);
1053 g_return_if_fail (G_IS_VALUE (value));
1055 g_object_ref (container);
1056 g_object_ref (child);
1058 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1059 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1060 G_OBJECT_TYPE (container), TRUE);
1062 g_warning ("%s: container class `%s' has no child property named `%s'",
1064 G_OBJECT_TYPE_NAME (container),
1066 else if (!(pspec->flags & G_PARAM_WRITABLE))
1067 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1070 G_OBJECT_TYPE_NAME (container));
1073 container_set_child_property (container, child, pspec, value, nqueue);
1075 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1076 g_object_unref (container);
1077 g_object_unref (child);
1081 * gtk_container_add_with_properties:
1082 * @container: a #GtkContainer
1083 * @widget: a widget to be placed inside @container
1084 * @first_prop_name: the name of the first child property to set
1085 * @Varargs: a %NULL-terminated list of property names and values, starting
1086 * with @first_prop_name
1088 * Adds @widget to @container, setting child properties at the same time.
1089 * See gtk_container_add() and gtk_container_child_set() for more details.
1092 gtk_container_add_with_properties (GtkContainer *container,
1094 const gchar *first_prop_name,
1097 g_return_if_fail (GTK_IS_CONTAINER (container));
1098 g_return_if_fail (GTK_IS_WIDGET (widget));
1099 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1101 g_object_ref (container);
1102 g_object_ref (widget);
1103 gtk_widget_freeze_child_notify (widget);
1105 g_signal_emit (container, container_signals[ADD], 0, widget);
1106 if (gtk_widget_get_parent (widget))
1110 va_start (var_args, first_prop_name);
1111 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1115 gtk_widget_thaw_child_notify (widget);
1116 g_object_unref (widget);
1117 g_object_unref (container);
1121 * gtk_container_child_set:
1122 * @container: a #GtkContainer
1123 * @child: a widget which is a child of @container
1124 * @first_prop_name: the name of the first property to set
1125 * @Varargs: a %NULL-terminated list of property names and values, starting
1126 * with @first_prop_name
1128 * Sets one or more child properties for @child and @container.
1131 gtk_container_child_set (GtkContainer *container,
1133 const gchar *first_prop_name,
1138 g_return_if_fail (GTK_IS_CONTAINER (container));
1139 g_return_if_fail (GTK_IS_WIDGET (child));
1140 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1142 va_start (var_args, first_prop_name);
1143 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1148 * gtk_container_child_get:
1149 * @container: a #GtkContainer
1150 * @child: a widget which is a child of @container
1151 * @first_prop_name: the name of the first property to get
1152 * @Varargs: return location for the first property, followed
1153 * optionally by more name/return location pairs, followed by %NULL
1155 * Gets the values of one or more child properties for @child and @container.
1158 gtk_container_child_get (GtkContainer *container,
1160 const gchar *first_prop_name,
1165 g_return_if_fail (GTK_IS_CONTAINER (container));
1166 g_return_if_fail (GTK_IS_WIDGET (child));
1167 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1169 va_start (var_args, first_prop_name);
1170 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1175 * gtk_container_class_install_child_property:
1176 * @cclass: a #GtkContainerClass
1177 * @property_id: the id for the property
1178 * @pspec: the #GParamSpec for the property
1180 * Installs a child property on a container class.
1183 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1187 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1188 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1189 if (pspec->flags & G_PARAM_WRITABLE)
1190 g_return_if_fail (cclass->set_child_property != NULL);
1191 if (pspec->flags & G_PARAM_READABLE)
1192 g_return_if_fail (cclass->get_child_property != NULL);
1193 g_return_if_fail (property_id > 0);
1194 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1195 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1196 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1198 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1200 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1201 G_OBJECT_CLASS_NAME (cclass),
1205 g_param_spec_ref (pspec);
1206 g_param_spec_sink (pspec);
1207 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1208 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1212 * gtk_container_class_find_child_property:
1213 * @cclass: a #GtkContainerClass
1214 * @property_name: the name of the child property to find
1215 * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
1216 * child property with that name.
1218 * Finds a child property of a container class by name.
1221 gtk_container_class_find_child_property (GObjectClass *cclass,
1222 const gchar *property_name)
1224 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1225 g_return_val_if_fail (property_name != NULL, NULL);
1227 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1229 G_OBJECT_CLASS_TYPE (cclass),
1234 * gtk_container_class_list_child_properties:
1235 * @cclass: a #GtkContainerClass
1236 * @n_properties: location to return the number of child properties found
1237 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
1238 * The array must be freed with g_free().
1240 * Returns all child properties of a container class.
1243 gtk_container_class_list_child_properties (GObjectClass *cclass,
1244 guint *n_properties)
1246 GParamSpec **pspecs;
1249 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1251 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1252 G_OBJECT_CLASS_TYPE (cclass),
1261 gtk_container_add_unimplemented (GtkContainer *container,
1264 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1268 gtk_container_remove_unimplemented (GtkContainer *container,
1271 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1275 gtk_container_init (GtkContainer *container)
1277 GtkContainerPrivate *priv;
1279 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1281 GtkContainerPrivate);
1282 priv = container->priv;
1284 priv->focus_child = NULL;
1285 priv->border_width = 0;
1286 priv->need_resize = FALSE;
1287 priv->resize_mode = GTK_RESIZE_PARENT;
1288 priv->reallocate_redraws = FALSE;
1292 gtk_container_destroy (GtkWidget *widget)
1294 GtkContainer *container = GTK_CONTAINER (widget);
1295 GtkContainerPrivate *priv = container->priv;
1297 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1298 _gtk_container_dequeue_resize_handler (container);
1300 if (priv->focus_child)
1302 g_object_unref (priv->focus_child);
1303 priv->focus_child = NULL;
1306 /* do this before walking child widgets, to avoid
1307 * removing children from focus chain one by one.
1309 if (priv->has_focus_chain)
1310 gtk_container_unset_focus_chain (container);
1312 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1314 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1318 gtk_container_set_property (GObject *object,
1320 const GValue *value,
1323 GtkContainer *container = GTK_CONTAINER (object);
1327 case PROP_BORDER_WIDTH:
1328 gtk_container_set_border_width (container, g_value_get_uint (value));
1330 case PROP_RESIZE_MODE:
1331 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1334 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1343 gtk_container_get_property (GObject *object,
1348 GtkContainer *container = GTK_CONTAINER (object);
1349 GtkContainerPrivate *priv = container->priv;
1353 case PROP_BORDER_WIDTH:
1354 g_value_set_uint (value, priv->border_width);
1356 case PROP_RESIZE_MODE:
1357 g_value_set_enum (value, priv->resize_mode);
1360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1366 * gtk_container_set_border_width:
1367 * @container: a #GtkContainer
1368 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1369 * the container. Valid values are in the range 0-65535 pixels.
1371 * Sets the border width of the container.
1373 * The border width of a container is the amount of space to leave
1374 * around the outside of the container. The only exception to this is
1375 * #GtkWindow; because toplevel windows can't leave space outside,
1376 * they leave the space inside. The border is added on all sides of
1377 * the container. To add space to only one side, one approach is to
1378 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1379 * to give it a size, and place it on the side of the container as
1383 gtk_container_set_border_width (GtkContainer *container,
1386 GtkContainerPrivate *priv;
1388 g_return_if_fail (GTK_IS_CONTAINER (container));
1390 priv = container->priv;
1392 if (priv->border_width != border_width)
1394 priv->border_width = border_width;
1395 g_object_notify (G_OBJECT (container), "border-width");
1397 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1398 gtk_widget_queue_resize (GTK_WIDGET (container));
1403 * gtk_container_get_border_width:
1404 * @container: a #GtkContainer
1406 * Retrieves the border width of the container. See
1407 * gtk_container_set_border_width().
1409 * Return value: the current border width
1412 gtk_container_get_border_width (GtkContainer *container)
1414 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1416 return container->priv->border_width;
1420 * gtk_container_add:
1421 * @container: a #GtkContainer
1422 * @widget: a widget to be placed inside @container
1424 * Adds @widget to @container. Typically used for simple containers
1425 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1426 * layout containers such as #GtkBox or #GtkTable, this function will
1427 * pick default packing parameters that may not be correct. So
1428 * consider functions such as gtk_box_pack_start() and
1429 * gtk_table_attach() as an alternative to gtk_container_add() in
1430 * those cases. A widget may be added to only one container at a time;
1431 * you can't place the same widget inside two different containers.
1434 gtk_container_add (GtkContainer *container,
1439 g_return_if_fail (GTK_IS_CONTAINER (container));
1440 g_return_if_fail (GTK_IS_WIDGET (widget));
1442 parent = gtk_widget_get_parent (widget);
1446 g_warning ("Attempting to add a widget with type %s to a container of "
1447 "type %s, but the widget is already inside a container of type %s, "
1448 "the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ "
1449 "explains how to reparent a widget.",
1450 g_type_name (G_OBJECT_TYPE (widget)),
1451 g_type_name (G_OBJECT_TYPE (container)),
1452 g_type_name (G_OBJECT_TYPE (parent)));
1456 g_signal_emit (container, container_signals[ADD], 0, widget);
1460 * gtk_container_remove:
1461 * @container: a #GtkContainer
1462 * @widget: a current child of @container
1464 * Removes @widget from @container. @widget must be inside @container.
1465 * Note that @container will own a reference to @widget, and that this
1466 * may be the last reference held; so removing a widget from its
1467 * container can destroy that widget. If you want to use @widget
1468 * again, you need to add a reference to it while it's not inside
1469 * a container, using g_object_ref(). If you don't want to use @widget
1470 * again it's usually more efficient to simply destroy it directly
1471 * using gtk_widget_destroy() since this will remove it from the
1472 * container and help break any circular reference count cycles.
1475 gtk_container_remove (GtkContainer *container,
1478 g_return_if_fail (GTK_IS_CONTAINER (container));
1479 g_return_if_fail (GTK_IS_WIDGET (widget));
1480 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1482 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1486 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1488 g_return_if_fail (GTK_IS_CONTAINER (container));
1489 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1491 container_resize_queue = g_slist_remove (container_resize_queue, container);
1492 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1496 * gtk_container_set_resize_mode:
1497 * @container: a #GtkContainer
1498 * @resize_mode: the new resize mode
1500 * Sets the resize mode for the container.
1502 * The resize mode of a container determines whether a resize request
1503 * will be passed to the container's parent, queued for later execution
1504 * or executed immediately.
1507 gtk_container_set_resize_mode (GtkContainer *container,
1508 GtkResizeMode resize_mode)
1510 GtkContainerPrivate *priv;
1512 g_return_if_fail (GTK_IS_CONTAINER (container));
1513 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1515 priv = container->priv;
1517 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1518 resize_mode == GTK_RESIZE_PARENT)
1520 resize_mode = GTK_RESIZE_QUEUE;
1523 if (priv->resize_mode != resize_mode)
1525 priv->resize_mode = resize_mode;
1527 gtk_widget_queue_resize (GTK_WIDGET (container));
1528 g_object_notify (G_OBJECT (container), "resize-mode");
1533 * gtk_container_get_resize_mode:
1534 * @container: a #GtkContainer
1536 * Returns the resize mode for the container. See
1537 * gtk_container_set_resize_mode ().
1539 * Return value: the current resize mode
1542 gtk_container_get_resize_mode (GtkContainer *container)
1544 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1546 return container->priv->resize_mode;
1550 * gtk_container_set_reallocate_redraws:
1551 * @container: a #GtkContainer
1552 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1554 * Sets the @reallocate_redraws flag of the container to the given value.
1556 * Containers requesting reallocation redraws get automatically
1557 * redrawn if any of their children changed allocation.
1560 gtk_container_set_reallocate_redraws (GtkContainer *container,
1561 gboolean needs_redraws)
1563 g_return_if_fail (GTK_IS_CONTAINER (container));
1565 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1568 static GtkContainer*
1569 gtk_container_get_resize_container (GtkContainer *container)
1572 GtkWidget *widget = GTK_WIDGET (container);
1574 while ((parent = gtk_widget_get_parent (widget)))
1577 if (GTK_IS_RESIZE_CONTAINER (widget))
1581 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1585 gtk_container_idle_sizer (gpointer data)
1587 /* we may be invoked with a container_resize_queue of NULL, because
1588 * queue_resize could have been adding an extra idle function while
1589 * the queue still got processed. we better just ignore such case
1590 * than trying to explicitely work around them with some extra flags,
1591 * since it doesn't cause any actual harm.
1593 while (container_resize_queue)
1598 slist = container_resize_queue;
1599 container_resize_queue = slist->next;
1600 widget = slist->data;
1601 g_slist_free_1 (slist);
1603 _gtk_widget_set_resize_pending (widget, FALSE);
1604 gtk_container_check_resize (GTK_CONTAINER (widget));
1607 gdk_window_process_all_updates ();
1613 _gtk_container_queue_resize (GtkContainer *container)
1615 GtkContainerPrivate *priv;
1616 GtkContainer *resize_container;
1620 g_return_if_fail (GTK_IS_CONTAINER (container));
1622 priv = container->priv;
1623 widget = GTK_WIDGET (container);
1625 resize_container = gtk_container_get_resize_container (container);
1629 _gtk_widget_set_alloc_needed (widget, TRUE);
1630 _gtk_widget_set_width_request_needed (widget, TRUE);
1631 _gtk_widget_set_height_request_needed (widget, TRUE);
1633 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1634 !(parent = gtk_widget_get_parent (widget)))
1640 if (resize_container)
1642 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1643 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1644 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1646 switch (resize_container->priv->resize_mode)
1648 case GTK_RESIZE_QUEUE:
1649 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1651 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1652 if (container_resize_queue == NULL)
1653 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1654 gtk_container_idle_sizer,
1656 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1660 case GTK_RESIZE_IMMEDIATE:
1661 gtk_container_check_resize (resize_container);
1664 case GTK_RESIZE_PARENT:
1665 g_assert_not_reached ();
1671 /* we need to let hidden resize containers know that something
1672 * changed while they where hidden (currently only evaluated by
1675 resize_container->priv->need_resize = TRUE;
1681 gtk_container_check_resize (GtkContainer *container)
1683 g_return_if_fail (GTK_IS_CONTAINER (container));
1685 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1689 gtk_container_real_check_resize (GtkContainer *container)
1691 GtkWidget *widget = GTK_WIDGET (container);
1692 GtkAllocation allocation;
1693 GtkRequisition requisition;
1695 gtk_widget_get_preferred_size (widget,
1696 &requisition, NULL);
1697 gtk_widget_get_allocation (widget, &allocation);
1699 if (requisition.width > allocation.width ||
1700 requisition.height > allocation.height)
1702 if (GTK_IS_RESIZE_CONTAINER (container))
1704 gtk_widget_size_allocate (widget, &allocation);
1705 gtk_widget_set_allocation (widget, &allocation);
1708 gtk_widget_queue_resize (widget);
1712 gtk_container_resize_children (container);
1716 /* The container hasn't changed size but one of its children
1717 * queued a resize request. Which means that the allocation
1718 * is not sufficient for the requisition of some child.
1719 * We've already performed a size request at this point,
1720 * so we simply need to reallocate and let the allocation
1721 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1724 gtk_container_resize_children (GtkContainer *container)
1726 GtkAllocation allocation;
1729 /* resizing invariants:
1730 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1731 * containers that have an idle sizer pending must be flagged with
1734 g_return_if_fail (GTK_IS_CONTAINER (container));
1736 widget = GTK_WIDGET (container);
1737 gtk_widget_get_allocation (widget, &allocation);
1739 gtk_widget_size_allocate (widget, &allocation);
1740 gtk_widget_set_allocation (widget, &allocation);
1744 gtk_container_adjust_size_request (GtkWidget *widget,
1745 GtkOrientation orientation,
1750 GtkContainer *container;
1752 container = GTK_CONTAINER (widget);
1754 if (GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1758 border_width = container->priv->border_width;
1760 *minimum_size += border_width * 2;
1761 *natural_size += border_width * 2;
1764 /* chain up last so gtk_widget_set_size_request() values
1765 * will have a chance to overwrite our border width.
1767 parent_class->adjust_size_request (widget, orientation, for_size,
1768 minimum_size, natural_size);
1772 gtk_container_adjust_size_allocation (GtkWidget *widget,
1773 GtkAllocation *allocation)
1775 GtkContainer *container;
1778 container = GTK_CONTAINER (widget);
1780 parent_class->adjust_size_allocation (widget, allocation);
1782 if (!GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1785 border_width = container->priv->border_width;
1787 allocation->width -= border_width * 2;
1788 allocation->height -= border_width * 2;
1790 /* If we get a pathological too-small allocation to hold
1791 * even the border width, leave all allocation to the actual
1792 * widget, and leave x,y unchanged. (GtkWidget's min size is
1793 * 1x1 if you're wondering why <1 and not <0)
1795 * As long as we have space, set x,y properly.
1798 if (allocation->width < 1)
1800 allocation->width += border_width * 2;
1804 allocation->x += border_width;
1807 if (allocation->height < 1)
1809 allocation->height += border_width * 2;
1813 allocation->y += border_width;
1818 * gtk_container_class_handle_border_width:
1819 * @klass: the class struct of a #GtkContainer subclass
1821 * Modifies a subclass of #GtkContainerClass to automatically add and
1822 * remove the border-width setting on GtkContainer. This allows the
1823 * subclass to ignore the border width in its size request and
1824 * allocate methods. The intent is for a subclass to invoke this
1825 * in its class_init function.
1827 * gtk_container_class_handle_border_width() is necessary because it
1828 * would break API too badly to make this behavior the default. So
1829 * subclasses must "opt in" to the parent class handling border_width
1833 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1835 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1837 klass->handle_border_width = TRUE;
1841 * gtk_container_forall:
1842 * @container: a #GtkContainer
1843 * @callback: a callback
1844 * @callback_data: callback user data
1846 * Invokes @callback on each child of @container, including children
1847 * that are considered "internal" (implementation details of the
1848 * container). "Internal" children generally weren't added by the user
1849 * of the container, but were added by the container implementation
1850 * itself. Most applications should use gtk_container_foreach(),
1851 * rather than gtk_container_forall().
1854 gtk_container_forall (GtkContainer *container,
1855 GtkCallback callback,
1856 gpointer callback_data)
1858 GtkContainerClass *class;
1860 g_return_if_fail (GTK_IS_CONTAINER (container));
1861 g_return_if_fail (callback != NULL);
1863 class = GTK_CONTAINER_GET_CLASS (container);
1866 class->forall (container, TRUE, callback, callback_data);
1870 * gtk_container_foreach:
1871 * @container: a #GtkContainer
1872 * @callback: (scope call): a callback
1873 * @callback_data: callback user data
1875 * Invokes @callback on each non-internal child of @container. See
1876 * gtk_container_forall() for details on what constitutes an
1877 * "internal" child. Most applications should use
1878 * gtk_container_foreach(), rather than gtk_container_forall().
1881 gtk_container_foreach (GtkContainer *container,
1882 GtkCallback callback,
1883 gpointer callback_data)
1885 GtkContainerClass *class;
1887 g_return_if_fail (GTK_IS_CONTAINER (container));
1888 g_return_if_fail (callback != NULL);
1890 class = GTK_CONTAINER_GET_CLASS (container);
1893 class->forall (container, FALSE, callback, callback_data);
1897 * gtk_container_set_focus_child:
1898 * @container: a #GtkContainer
1899 * @child: (allow-none): a #GtkWidget, or %NULL
1901 * Sets, or unsets if @child is %NULL, the focused child of @container.
1903 * This function emits the GtkContainer::set_focus_child signal of
1904 * @container. Implementations of #GtkContainer can override the
1905 * default behaviour by overriding the class closure of this signal.
1907 * This is function is mostly meant to be used by widgets. Applications can use
1908 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1911 gtk_container_set_focus_child (GtkContainer *container,
1914 g_return_if_fail (GTK_IS_CONTAINER (container));
1916 g_return_if_fail (GTK_IS_WIDGET (child));
1918 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1922 * gtk_container_get_focus_child:
1923 * @container: a #GtkContainer
1925 * Returns the current focus child widget inside @container. This is not the
1926 * currently focused widget. That can be obtained by calling
1927 * gtk_window_get_focus().
1929 * Returns: The child widget which will recieve the focus inside @container when
1930 * the @conatiner is focussed, or %NULL if none is set.
1935 gtk_container_get_focus_child (GtkContainer *container)
1937 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1939 return container->priv->focus_child;
1943 * gtk_container_get_children:
1944 * @container: a #GtkContainer
1946 * Returns the container's non-internal children. See
1947 * gtk_container_forall() for details on what constitutes an "internal" child.
1949 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1952 gtk_container_get_children (GtkContainer *container)
1954 GList *children = NULL;
1956 gtk_container_foreach (container,
1957 gtk_container_children_callback,
1960 return g_list_reverse (children);
1964 gtk_container_child_position_callback (GtkWidget *widget,
1965 gpointer client_data)
1971 } *data = client_data;
1974 if (data->child == widget)
1975 data->index = data->i;
1979 gtk_container_child_default_composite_name (GtkContainer *container,
1989 /* fallback implementation */
1993 gtk_container_forall (container,
1994 gtk_container_child_position_callback,
1997 name = g_strdup_printf ("%s-%u",
1998 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2005 _gtk_container_child_composite_name (GtkContainer *container,
2008 gboolean composite_child;
2010 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2011 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2012 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2014 g_object_get (child, "composite-child", &composite_child, NULL);
2015 if (composite_child)
2017 static GQuark quark_composite_name = 0;
2020 if (!quark_composite_name)
2021 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2023 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2026 GtkContainerClass *class;
2028 class = GTK_CONTAINER_GET_CLASS (container);
2029 if (class->composite_name)
2030 name = class->composite_name (container, child);
2033 name = g_strdup (name);
2042 gtk_container_real_set_focus_child (GtkContainer *container,
2045 GtkContainerPrivate *priv;
2047 g_return_if_fail (GTK_IS_CONTAINER (container));
2048 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2050 priv = container->priv;
2052 if (child != priv->focus_child)
2054 if (priv->focus_child)
2055 g_object_unref (priv->focus_child);
2056 priv->focus_child = child;
2057 if (priv->focus_child)
2058 g_object_ref (priv->focus_child);
2062 /* check for h/v adjustments
2064 if (priv->focus_child)
2066 GtkAdjustment *hadj;
2067 GtkAdjustment *vadj;
2068 GtkAllocation allocation;
2069 GtkWidget *focus_child;
2072 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2073 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2077 focus_child = priv->focus_child;
2078 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2080 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2083 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2086 gtk_widget_get_allocation (priv->focus_child, &allocation);
2090 gtk_widget_get_allocation (focus_child, &allocation);
2093 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2096 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2102 get_focus_chain (GtkContainer *container)
2104 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2107 /* same as gtk_container_get_children, except it includes internals
2110 gtk_container_get_all_children (GtkContainer *container)
2112 GList *children = NULL;
2114 gtk_container_forall (container,
2115 gtk_container_children_callback,
2122 gtk_container_focus (GtkWidget *widget,
2123 GtkDirectionType direction)
2126 GList *sorted_children;
2128 GtkContainer *container;
2129 GtkContainerPrivate *priv;
2131 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2133 container = GTK_CONTAINER (widget);
2134 priv = container->priv;
2138 if (gtk_widget_get_can_focus (widget))
2140 if (!gtk_widget_has_focus (widget))
2142 gtk_widget_grab_focus (widget);
2148 /* Get a list of the containers children, allowing focus
2149 * chain to override.
2151 if (priv->has_focus_chain)
2152 children = g_list_copy (get_focus_chain (container));
2154 children = gtk_container_get_all_children (container);
2156 if (priv->has_focus_chain &&
2157 (direction == GTK_DIR_TAB_FORWARD ||
2158 direction == GTK_DIR_TAB_BACKWARD))
2160 sorted_children = g_list_copy (children);
2162 if (direction == GTK_DIR_TAB_BACKWARD)
2163 sorted_children = g_list_reverse (sorted_children);
2166 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2168 return_val = gtk_container_focus_move (container, sorted_children, direction);
2170 g_list_free (sorted_children);
2171 g_list_free (children);
2178 tab_compare (gconstpointer a,
2182 GtkAllocation child1_allocation, child2_allocation;
2183 const GtkWidget *child1 = a;
2184 const GtkWidget *child2 = b;
2185 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2187 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2188 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2190 gint y1 = child1_allocation.y + child1_allocation.height / 2;
2191 gint y2 = child2_allocation.y + child2_allocation.height / 2;
2195 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2196 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2198 if (text_direction == GTK_TEXT_DIR_RTL)
2199 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2201 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2204 return (y1 < y2) ? -1 : 1;
2208 gtk_container_focus_sort_tab (GtkContainer *container,
2210 GtkDirectionType direction,
2211 GtkWidget *old_focus)
2213 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2214 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2216 /* if we are going backwards then reverse the order
2219 if (direction == GTK_DIR_TAB_BACKWARD)
2220 children = g_list_reverse (children);
2225 /* Get coordinates of @widget's allocation with respect to
2226 * allocation of @container.
2229 get_allocation_coords (GtkContainer *container,
2231 GdkRectangle *allocation)
2233 gtk_widget_set_allocation (widget, allocation);
2235 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2236 0, 0, &allocation->x, &allocation->y);
2239 /* Look for a child in @children that is intermediate between
2240 * the focus widget and container. This widget, if it exists,
2241 * acts as the starting widget for focus navigation.
2244 find_old_focus (GtkContainer *container,
2247 GList *tmp_list = children;
2250 GtkWidget *child = tmp_list->data;
2251 GtkWidget *widget = child;
2253 while (widget && widget != (GtkWidget *)container)
2257 parent = gtk_widget_get_parent (widget);
2259 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2268 tmp_list = tmp_list->next;
2275 old_focus_coords (GtkContainer *container,
2276 GdkRectangle *old_focus_rect)
2278 GtkWidget *widget = GTK_WIDGET (container);
2279 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2280 GtkWidget *old_focus;
2282 if (GTK_IS_WINDOW (toplevel))
2284 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2286 return get_allocation_coords (container, old_focus, old_focus_rect);
2292 typedef struct _CompareInfo CompareInfo;
2296 GtkContainer *container;
2303 up_down_compare (gconstpointer a,
2307 GdkRectangle allocation1;
2308 GdkRectangle allocation2;
2309 CompareInfo *compare = data;
2312 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2313 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2315 y1 = allocation1.y + allocation1.height / 2;
2316 y2 = allocation2.y + allocation2.height / 2;
2320 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2321 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2323 if (compare->reverse)
2324 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2326 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2329 return (y1 < y2) ? -1 : 1;
2333 gtk_container_focus_sort_up_down (GtkContainer *container,
2335 GtkDirectionType direction,
2336 GtkWidget *old_focus)
2338 CompareInfo compare;
2340 GdkRectangle old_allocation;
2342 compare.container = container;
2343 compare.reverse = (direction == GTK_DIR_UP);
2346 old_focus = find_old_focus (container, children);
2348 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2354 /* Delete widgets from list that don't match minimum criteria */
2356 compare_x1 = old_allocation.x;
2357 compare_x2 = old_allocation.x + old_allocation.width;
2359 if (direction == GTK_DIR_UP)
2360 compare_y = old_allocation.y;
2362 compare_y = old_allocation.y + old_allocation.height;
2364 tmp_list = children;
2367 GtkWidget *child = tmp_list->data;
2368 GList *next = tmp_list->next;
2369 gint child_x1, child_x2;
2370 GdkRectangle child_allocation;
2372 if (child != old_focus)
2374 if (get_allocation_coords (container, child, &child_allocation))
2376 child_x1 = child_allocation.x;
2377 child_x2 = child_allocation.x + child_allocation.width;
2379 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2380 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2381 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2383 children = g_list_delete_link (children, tmp_list);
2387 children = g_list_delete_link (children, tmp_list);
2393 compare.x = (compare_x1 + compare_x2) / 2;
2394 compare.y = old_allocation.y + old_allocation.height / 2;
2398 /* No old focus widget, need to figure out starting x,y some other way
2400 GtkAllocation allocation;
2401 GtkWidget *widget = GTK_WIDGET (container);
2402 GdkRectangle old_focus_rect;
2404 gtk_widget_get_allocation (widget, &allocation);
2406 if (old_focus_coords (container, &old_focus_rect))
2408 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2412 if (!gtk_widget_get_has_window (widget))
2413 compare.x = allocation.x + allocation.width / 2;
2415 compare.x = allocation.width / 2;
2418 if (!gtk_widget_get_has_window (widget))
2419 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2421 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2424 children = g_list_sort_with_data (children, up_down_compare, &compare);
2426 if (compare.reverse)
2427 children = g_list_reverse (children);
2433 left_right_compare (gconstpointer a,
2437 GdkRectangle allocation1;
2438 GdkRectangle allocation2;
2439 CompareInfo *compare = data;
2442 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2443 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2445 x1 = allocation1.x + allocation1.width / 2;
2446 x2 = allocation2.x + allocation2.width / 2;
2450 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2451 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2453 if (compare->reverse)
2454 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2456 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2459 return (x1 < x2) ? -1 : 1;
2463 gtk_container_focus_sort_left_right (GtkContainer *container,
2465 GtkDirectionType direction,
2466 GtkWidget *old_focus)
2468 CompareInfo compare;
2470 GdkRectangle old_allocation;
2472 compare.container = container;
2473 compare.reverse = (direction == GTK_DIR_LEFT);
2476 old_focus = find_old_focus (container, children);
2478 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2484 /* Delete widgets from list that don't match minimum criteria */
2486 compare_y1 = old_allocation.y;
2487 compare_y2 = old_allocation.y + old_allocation.height;
2489 if (direction == GTK_DIR_LEFT)
2490 compare_x = old_allocation.x;
2492 compare_x = old_allocation.x + old_allocation.width;
2494 tmp_list = children;
2497 GtkWidget *child = tmp_list->data;
2498 GList *next = tmp_list->next;
2499 gint child_y1, child_y2;
2500 GdkRectangle child_allocation;
2502 if (child != old_focus)
2504 if (get_allocation_coords (container, child, &child_allocation))
2506 child_y1 = child_allocation.y;
2507 child_y2 = child_allocation.y + child_allocation.height;
2509 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2510 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2511 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2513 children = g_list_delete_link (children, tmp_list);
2517 children = g_list_delete_link (children, tmp_list);
2523 compare.y = (compare_y1 + compare_y2) / 2;
2524 compare.x = old_allocation.x + old_allocation.width / 2;
2528 /* No old focus widget, need to figure out starting x,y some other way
2530 GtkAllocation allocation;
2531 GtkWidget *widget = GTK_WIDGET (container);
2532 GdkRectangle old_focus_rect;
2534 gtk_widget_get_allocation (widget, &allocation);
2536 if (old_focus_coords (container, &old_focus_rect))
2538 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2542 if (!gtk_widget_get_has_window (widget))
2543 compare.y = allocation.y + allocation.height / 2;
2545 compare.y = allocation.height / 2;
2548 if (!gtk_widget_get_has_window (widget))
2549 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2551 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2554 children = g_list_sort_with_data (children, left_right_compare, &compare);
2556 if (compare.reverse)
2557 children = g_list_reverse (children);
2563 * gtk_container_focus_sort:
2564 * @container: a #GtkContainer
2565 * @children: a list of descendents of @container (they don't
2566 * have to be direct children)
2567 * @direction: focus direction
2568 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2569 * to determine this automatically.
2570 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2571 * which is the only @direction we use currently,
2572 * so perhaps this argument should be removed)
2574 * Sorts @children in the correct order for focusing with
2575 * direction type @direction.
2577 * Return value: a copy of @children, sorted in correct focusing order,
2578 * with children that aren't suitable for focusing in this direction
2582 _gtk_container_focus_sort (GtkContainer *container,
2584 GtkDirectionType direction,
2585 GtkWidget *old_focus)
2587 GList *visible_children = NULL;
2591 if (gtk_widget_get_realized (children->data))
2592 visible_children = g_list_prepend (visible_children, children->data);
2593 children = children->next;
2598 case GTK_DIR_TAB_FORWARD:
2599 case GTK_DIR_TAB_BACKWARD:
2600 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2603 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2606 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2609 g_assert_not_reached ();
2615 gtk_container_focus_move (GtkContainer *container,
2617 GtkDirectionType direction)
2619 GtkContainerPrivate *priv = container->priv;
2620 GtkWidget *focus_child;
2623 focus_child = priv->focus_child;
2627 child = children->data;
2628 children = children->next;
2635 if (focus_child == child)
2639 if (gtk_widget_child_focus (child, direction))
2643 else if (gtk_widget_is_drawable (child) &&
2644 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2646 if (gtk_widget_child_focus (child, direction))
2656 gtk_container_children_callback (GtkWidget *widget,
2657 gpointer client_data)
2661 children = (GList**) client_data;
2662 *children = g_list_prepend (*children, widget);
2666 chain_widget_destroyed (GtkWidget *widget,
2669 GtkContainer *container;
2672 container = GTK_CONTAINER (user_data);
2674 chain = g_object_get_data (G_OBJECT (container),
2675 "gtk-container-focus-chain");
2677 chain = g_list_remove (chain, widget);
2679 g_signal_handlers_disconnect_by_func (widget,
2680 chain_widget_destroyed,
2683 g_object_set_data (G_OBJECT (container),
2684 I_("gtk-container-focus-chain"),
2689 * gtk_container_set_focus_chain:
2690 * @container: a #GtkContainer
2691 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2692 * the new focus chain
2694 * Sets a focus chain, overriding the one computed automatically by GTK+.
2696 * In principle each widget in the chain should be a descendant of the
2697 * container, but this is not enforced by this method, since it's allowed
2698 * to set the focus chain before you pack the widgets, or have a widget
2699 * in the chain that isn't always packed. The necessary checks are done
2700 * when the focus chain is actually traversed.
2703 gtk_container_set_focus_chain (GtkContainer *container,
2704 GList *focusable_widgets)
2708 GtkContainerPrivate *priv;
2710 g_return_if_fail (GTK_IS_CONTAINER (container));
2712 priv = container->priv;
2714 if (priv->has_focus_chain)
2715 gtk_container_unset_focus_chain (container);
2717 priv->has_focus_chain = TRUE;
2720 tmp_list = focusable_widgets;
2721 while (tmp_list != NULL)
2723 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2725 /* In principle each widget in the chain should be a descendant
2726 * of the container, but we don't want to check that here, it's
2727 * expensive and also it's allowed to set the focus chain before
2728 * you pack the widgets, or have a widget in the chain that isn't
2729 * always packed. So we check for ancestor during actual traversal.
2732 chain = g_list_prepend (chain, tmp_list->data);
2734 g_signal_connect (tmp_list->data,
2736 G_CALLBACK (chain_widget_destroyed),
2739 tmp_list = g_list_next (tmp_list);
2742 chain = g_list_reverse (chain);
2744 g_object_set_data (G_OBJECT (container),
2745 I_("gtk-container-focus-chain"),
2750 * gtk_container_get_focus_chain:
2751 * @container: a #GtkContainer
2752 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2753 * to store the focus chain of the
2754 * container, or %NULL. You should free this list
2755 * using g_list_free() when you are done with it, however
2756 * no additional reference count is added to the
2757 * individual widgets in the focus chain.
2759 * Retrieves the focus chain of the container, if one has been
2760 * set explicitly. If no focus chain has been explicitly
2761 * set, GTK+ computes the focus chain based on the positions
2762 * of the children. In that case, GTK+ stores %NULL in
2763 * @focusable_widgets and returns %FALSE.
2765 * Return value: %TRUE if the focus chain of the container
2766 * has been set explicitly.
2769 gtk_container_get_focus_chain (GtkContainer *container,
2770 GList **focus_chain)
2772 GtkContainerPrivate *priv;
2774 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2776 priv = container->priv;
2780 if (priv->has_focus_chain)
2781 *focus_chain = g_list_copy (get_focus_chain (container));
2783 *focus_chain = NULL;
2786 return priv->has_focus_chain;
2790 * gtk_container_unset_focus_chain:
2791 * @container: a #GtkContainer
2793 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2796 gtk_container_unset_focus_chain (GtkContainer *container)
2798 GtkContainerPrivate *priv;
2800 g_return_if_fail (GTK_IS_CONTAINER (container));
2802 priv = container->priv;
2804 if (priv->has_focus_chain)
2809 chain = get_focus_chain (container);
2811 priv->has_focus_chain = FALSE;
2813 g_object_set_data (G_OBJECT (container),
2814 I_("gtk-container-focus-chain"),
2818 while (tmp_list != NULL)
2820 g_signal_handlers_disconnect_by_func (tmp_list->data,
2821 chain_widget_destroyed,
2824 tmp_list = g_list_next (tmp_list);
2827 g_list_free (chain);
2832 * gtk_container_set_focus_vadjustment:
2833 * @container: a #GtkContainer
2834 * @adjustment: an adjustment which should be adjusted when the focus
2835 * is moved among the descendents of @container
2837 * Hooks up an adjustment to focus handling in a container, so when a
2838 * child of the container is focused, the adjustment is scrolled to
2839 * show that widget. This function sets the vertical alignment. See
2840 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2841 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2842 * the horizontal adjustment.
2844 * The adjustments have to be in pixel units and in the same coordinate
2845 * system as the allocation for immediate children of the container.
2848 gtk_container_set_focus_vadjustment (GtkContainer *container,
2849 GtkAdjustment *adjustment)
2851 g_return_if_fail (GTK_IS_CONTAINER (container));
2853 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2856 g_object_ref (adjustment);
2858 g_object_set_qdata_full (G_OBJECT (container),
2865 * gtk_container_get_focus_vadjustment:
2866 * @container: a #GtkContainer
2868 * Retrieves the vertical focus adjustment for the container. See
2869 * gtk_container_set_focus_vadjustment().
2871 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2872 * none has been set.
2875 gtk_container_get_focus_vadjustment (GtkContainer *container)
2877 GtkAdjustment *vadjustment;
2879 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2881 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2887 * gtk_container_set_focus_hadjustment:
2888 * @container: a #GtkContainer
2889 * @adjustment: an adjustment which should be adjusted when the focus is
2890 * moved among the descendents of @container
2892 * Hooks up an adjustment to focus handling in a container, so when a child
2893 * of the container is focused, the adjustment is scrolled to show that
2894 * widget. This function sets the horizontal alignment.
2895 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2896 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2897 * the vertical adjustment.
2899 * The adjustments have to be in pixel units and in the same coordinate
2900 * system as the allocation for immediate children of the container.
2903 gtk_container_set_focus_hadjustment (GtkContainer *container,
2904 GtkAdjustment *adjustment)
2906 g_return_if_fail (GTK_IS_CONTAINER (container));
2908 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2911 g_object_ref (adjustment);
2913 g_object_set_qdata_full (G_OBJECT (container),
2920 * gtk_container_get_focus_hadjustment:
2921 * @container: a #GtkContainer
2923 * Retrieves the horizontal focus adjustment for the container. See
2924 * gtk_container_set_focus_hadjustment ().
2926 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
2927 * none has been set.
2930 gtk_container_get_focus_hadjustment (GtkContainer *container)
2932 GtkAdjustment *hadjustment;
2934 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2936 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2943 gtk_container_show_all (GtkWidget *widget)
2945 g_return_if_fail (GTK_IS_CONTAINER (widget));
2947 gtk_container_foreach (GTK_CONTAINER (widget),
2948 (GtkCallback) gtk_widget_show_all,
2950 gtk_widget_show (widget);
2954 gtk_container_hide_all (GtkWidget *widget)
2956 g_return_if_fail (GTK_IS_CONTAINER (widget));
2958 gtk_widget_hide (widget);
2959 gtk_container_foreach (GTK_CONTAINER (widget),
2960 (GtkCallback) gtk_widget_hide_all,
2966 gtk_container_draw_child (GtkWidget *child,
2967 gpointer client_data)
2970 GtkWidget *container;
2972 } *data = client_data;
2974 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
2980 gtk_container_draw (GtkWidget *widget,
2984 GtkWidget *container;
2988 data.container = widget;
2991 gtk_container_forall (GTK_CONTAINER (widget),
2992 gtk_container_draw_child,
2999 gtk_container_map_child (GtkWidget *child,
3000 gpointer client_data)
3002 if (gtk_widget_get_visible (child) &&
3003 gtk_widget_get_child_visible (child) &&
3004 !gtk_widget_get_mapped (child))
3005 gtk_widget_map (child);
3009 gtk_container_map (GtkWidget *widget)
3011 gtk_widget_set_mapped (widget, TRUE);
3013 gtk_container_forall (GTK_CONTAINER (widget),
3014 gtk_container_map_child,
3017 if (gtk_widget_get_has_window (widget))
3018 gdk_window_show (gtk_widget_get_window (widget));
3022 gtk_container_unmap (GtkWidget *widget)
3024 gtk_widget_set_mapped (widget, FALSE);
3026 if (gtk_widget_get_has_window (widget))
3027 gdk_window_hide (gtk_widget_get_window (widget));
3029 gtk_container_forall (GTK_CONTAINER (widget),
3030 (GtkCallback)gtk_widget_unmap,
3035 * gtk_container_propagate_draw:
3036 * @container: a #GtkContainer
3037 * @child: a child of @container
3038 * @cr: Cairo context as passed to the container. If you want to use @cr
3039 * in container's draw function, consider using cairo_save() and
3040 * cairo_restore() before calling this function.
3042 * When a container receives a call to the draw function, it must send
3043 * synthetic #GtkWidget::draw calls to all children that don't have their
3044 * own #GdkWindows. This function provides a convenient way of doing this.
3045 * A container, when it receives a call to its #GtkWidget::draw function,
3046 * calls gtk_container_propagate_draw() once for each child, passing in
3047 * the @cr the container received.
3049 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3050 * and deciding whether the draw needs to be sent to the child. It is a
3051 * convenient and optimized way of getting the same effect as calling
3052 * gtk_widget_draw() on the child directly.
3054 * In most cases, a container can simply either inherit the
3055 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3056 * and then chain to the ::draw implementation from #GtkContainer.
3059 gtk_container_propagate_draw (GtkContainer *container,
3063 GdkEventExpose *event;
3064 GtkAllocation allocation;
3065 GdkWindow *window, *w;
3068 g_return_if_fail (GTK_IS_CONTAINER (container));
3069 g_return_if_fail (GTK_IS_WIDGET (child));
3070 g_return_if_fail (cr != NULL);
3072 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3074 event = _gtk_cairo_get_event (cr);
3077 if (gtk_widget_get_has_window (child) ||
3078 gtk_widget_get_window (child) != event->window)
3084 /* translate coordinates. Ugly business, that. */
3085 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3087 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3097 window = gtk_widget_get_window (GTK_WIDGET (container));
3099 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3102 gdk_window_get_position (w, &wx, &wy);
3113 if (!gtk_widget_get_has_window (child))
3115 gtk_widget_get_allocation (child, &allocation);
3120 cairo_translate (cr, x, y);
3122 _gtk_widget_draw_internal (child, cr, TRUE);
3128 _gtk_container_get_need_resize (GtkContainer *container)
3130 return container->priv->need_resize;
3134 _gtk_container_set_need_resize (GtkContainer *container,
3135 gboolean need_resize)
3137 container->priv->need_resize = need_resize;
3141 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3143 return container->priv->reallocate_redraws;