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 * it's child into a clickable button; a #GtkFrame draws a frame around it's child
68 * and a #GtkWindow places it's child widget inside a top-level window.
70 * The second type of container can have more than one child; it's 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 it's
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 * of all it's important to note that a container must prioritize one of it's
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 * it's parent will request it's 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 has 2 virtual methods involved. 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 it's 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 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 required width
142 * of the children collectively if the container were to be allocated the said height ...
145 * ]]></programlisting>
147 * Furthermore, in order to ensure correct height-for-width requests it is important
148 * to check the input width against the real required minimum width, this can
149 * easily be achieved as follows:
151 * <programlisting><![CDATA[
153 * foo_container_get_preferred_height_for_width (GtkWidget *widget, gint for_width,
154 * gint *min_height, gint *nat_height)
156 * if (i_am_in_height_for_width_mode)
160 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
162 * for_width = MAX (min_width, for_width);
164 * execute_real_height_for_width_request_code (widget, for_width, min_height, nat_height);
168 * ... fall back on virtual results as mentioned in the previous example ...
171 * ]]></programlisting>
173 * Height for width requests are generally implemented in terms of a virtual allocation
174 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
175 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
176 * gtk_widget_get_preferred_width() for each of its children.
178 * For each potential group of children that are lined up horizontally the values returned by
179 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures;
180 * any child spacing should be removed from the input @for_width and then the collective size be
181 * allocated using the gtk_distribute_natural_allocation() convenience function
183 * The container will then move on to request the preferred height for each child by using
184 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
186 * When it comes time to allocate a height-for-width container, it's again important
187 * to consider that a container has to prioritize one dimension over the other. So if
188 * a container is a height-for-width container it must first allocate all widgets horizontally
189 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
190 * extra space (if and where appropriate) for the widget to expand.
192 * After adding all the expand space, the container assumes it was allocated sufficient
193 * height to fit all of its content. At this time, the container must use the total horizontal sizes
194 * of each widget to request the height-for-width of each of its children and store the requests in a
195 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
196 * be generalized into the heights and widths of rows and columns).
197 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
198 * while this time considering the allocated height of the widget minus any vertical spacing
199 * that the container adds. Then vertical expand space should be added where appropriate if available
200 * and go on to actually allocating the child widgets.
202 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
203 * to learn more about implementing height-for-width geometry management for widgets.
206 * <refsect2 id="child-properties">
207 * <title>Child properties</title>
209 * GtkContainer introduces <emphasis>child properties</emphasis>.
210 * These are object properties that are not specific
211 * to either the container or the contained widget, but rather to their relation.
212 * Typical examples of child properties are the position or pack-type of a widget
213 * which is contained in a #GtkBox.
215 * Use gtk_container_class_install_child_property() to install child properties
216 * for a container class and gtk_container_class_find_child_property() or
217 * gtk_container_class_list_child_properties() to get information about existing
220 * To set the value of a child property, use gtk_container_child_set_property(),
221 * gtk_container_child_set() or gtk_container_child_set_valist().
222 * To obtain the value of a child property, use
223 * gtk_container_child_get_property(), gtk_container_child_get() or
224 * gtk_container_child_get_valist(). To emit notification about child property
225 * changes, use gtk_widget_child_notify().
228 * <refsect2 id="GtkContainer-BUILDER-UI">
229 * <title>GtkContainer as GtkBuildable</title>
231 * The GtkContainer implementation of the GtkBuildable interface
232 * supports a <packing> element for children, which can
233 * contain multiple <property> elements that specify
234 * child properties for the child.
236 * <title>Child properties in UI definitions</title>
237 * <programlisting><![CDATA[
238 * <object class="GtkVBox">
240 * <object class="GtkLabel"/>
242 * <property name="pack-type">start</property>
246 * ]]></programlisting>
248 * Since 2.16, child properties can also be marked as translatable using
249 * the same "translatable", "comments" and "context" attributes that are used
250 * for regular properties.
256 struct _GtkContainerPrivate
258 GtkWidget *focus_child;
260 guint border_width : 16;
262 guint has_focus_chain : 1;
263 guint need_resize : 1;
264 guint reallocate_redraws : 1;
265 guint resize_mode : 2;
283 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
284 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
287 /* --- prototypes --- */
288 static void gtk_container_base_class_init (GtkContainerClass *klass);
289 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
290 static void gtk_container_class_init (GtkContainerClass *klass);
291 static void gtk_container_init (GtkContainer *container);
292 static void gtk_container_destroy (GtkWidget *widget);
293 static void gtk_container_set_property (GObject *object,
297 static void gtk_container_get_property (GObject *object,
301 static void gtk_container_add_unimplemented (GtkContainer *container,
303 static void gtk_container_remove_unimplemented (GtkContainer *container,
305 static void gtk_container_real_check_resize (GtkContainer *container);
306 static void gtk_container_compute_expand (GtkWidget *widget,
308 gboolean *vexpand_p);
309 static gboolean gtk_container_focus (GtkWidget *widget,
310 GtkDirectionType direction);
311 static void gtk_container_real_set_focus_child (GtkContainer *container,
314 static gboolean gtk_container_focus_move (GtkContainer *container,
316 GtkDirectionType direction);
317 static void gtk_container_children_callback (GtkWidget *widget,
318 gpointer client_data);
319 static void gtk_container_show_all (GtkWidget *widget);
320 static gint gtk_container_draw (GtkWidget *widget,
322 static void gtk_container_map (GtkWidget *widget);
323 static void gtk_container_unmap (GtkWidget *widget);
324 static void gtk_container_adjust_size_request (GtkWidget *widget,
325 GtkOrientation orientation,
328 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
329 GtkOrientation orientation,
332 gint *allocated_size);
334 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
338 static void gtk_container_buildable_init (GtkBuildableIface *iface);
339 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
343 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
346 const gchar *tagname,
347 GMarkupParser *parser,
349 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
352 const gchar *tagname,
356 /* --- variables --- */
357 static const gchar vadjustment_key[] = "gtk-vadjustment";
358 static guint vadjustment_key_id = 0;
359 static const gchar hadjustment_key[] = "gtk-hadjustment";
360 static guint hadjustment_key_id = 0;
361 static GSList *container_resize_queue = NULL;
362 static guint container_signals[LAST_SIGNAL] = { 0 };
363 static GtkWidgetClass *parent_class = NULL;
364 extern GParamSpecPool *_gtk_widget_child_property_pool;
365 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
366 static GtkBuildableIface *parent_buildable_iface;
369 /* --- functions --- */
371 gtk_container_get_type (void)
373 static GType container_type = 0;
377 const GTypeInfo container_info =
379 sizeof (GtkContainerClass),
380 (GBaseInitFunc) gtk_container_base_class_init,
381 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
382 (GClassInitFunc) gtk_container_class_init,
383 NULL /* class_finalize */,
384 NULL /* class_data */,
385 sizeof (GtkContainer),
387 (GInstanceInitFunc) gtk_container_init,
388 NULL, /* value_table */
391 const GInterfaceInfo buildable_info =
393 (GInterfaceInitFunc) gtk_container_buildable_init,
399 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
400 &container_info, G_TYPE_FLAG_ABSTRACT);
402 g_type_add_interface_static (container_type,
408 return container_type;
412 gtk_container_base_class_init (GtkContainerClass *class)
414 /* reset instance specifc class fields that don't get inherited */
415 class->set_child_property = NULL;
416 class->get_child_property = NULL;
420 gtk_container_base_class_finalize (GtkContainerClass *class)
424 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
425 for (node = list; node; node = node->next)
427 GParamSpec *pspec = node->data;
429 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
430 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
431 g_param_spec_unref (pspec);
437 gtk_container_class_init (GtkContainerClass *class)
439 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
440 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
442 parent_class = g_type_class_peek_parent (class);
444 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
445 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
447 gobject_class->set_property = gtk_container_set_property;
448 gobject_class->get_property = gtk_container_get_property;
450 widget_class->destroy = gtk_container_destroy;
451 widget_class->compute_expand = gtk_container_compute_expand;
452 widget_class->show_all = gtk_container_show_all;
453 widget_class->draw = gtk_container_draw;
454 widget_class->map = gtk_container_map;
455 widget_class->unmap = gtk_container_unmap;
456 widget_class->focus = gtk_container_focus;
458 widget_class->adjust_size_request = gtk_container_adjust_size_request;
459 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
461 class->add = gtk_container_add_unimplemented;
462 class->remove = gtk_container_remove_unimplemented;
463 class->check_resize = gtk_container_real_check_resize;
464 class->forall = NULL;
465 class->set_focus_child = gtk_container_real_set_focus_child;
466 class->child_type = NULL;
467 class->composite_name = gtk_container_child_default_composite_name;
469 g_object_class_install_property (gobject_class,
471 g_param_spec_enum ("resize-mode",
473 P_("Specify how resize events are handled"),
474 GTK_TYPE_RESIZE_MODE,
476 GTK_PARAM_READWRITE));
477 g_object_class_install_property (gobject_class,
479 g_param_spec_uint ("border-width",
481 P_("The width of the empty border outside the containers children"),
485 GTK_PARAM_READWRITE));
486 g_object_class_install_property (gobject_class,
488 g_param_spec_object ("child",
490 P_("Can be used to add a new child to the container"),
492 GTK_PARAM_WRITABLE));
493 container_signals[ADD] =
494 g_signal_new (I_("add"),
495 G_OBJECT_CLASS_TYPE (gobject_class),
497 G_STRUCT_OFFSET (GtkContainerClass, add),
499 _gtk_marshal_VOID__OBJECT,
502 container_signals[REMOVE] =
503 g_signal_new (I_("remove"),
504 G_OBJECT_CLASS_TYPE (gobject_class),
506 G_STRUCT_OFFSET (GtkContainerClass, remove),
508 _gtk_marshal_VOID__OBJECT,
511 container_signals[CHECK_RESIZE] =
512 g_signal_new (I_("check-resize"),
513 G_OBJECT_CLASS_TYPE (gobject_class),
515 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
517 _gtk_marshal_VOID__VOID,
519 container_signals[SET_FOCUS_CHILD] =
520 g_signal_new (I_("set-focus-child"),
521 G_OBJECT_CLASS_TYPE (gobject_class),
523 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
525 _gtk_marshal_VOID__OBJECT,
529 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
533 gtk_container_buildable_init (GtkBuildableIface *iface)
535 parent_buildable_iface = g_type_interface_peek_parent (iface);
536 iface->add_child = gtk_container_buildable_add_child;
537 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
538 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
542 gtk_container_buildable_add_child (GtkBuildable *buildable,
549 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
551 else if (GTK_IS_WIDGET (child) &&
552 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
554 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
557 g_warning ("Cannot add an object of type %s to a container of type %s",
558 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
562 gtk_container_buildable_set_child_property (GtkContainer *container,
569 GValue gvalue = { 0, };
570 GError *error = NULL;
572 pspec = gtk_container_class_find_child_property
573 (G_OBJECT_GET_CLASS (container), name);
576 g_warning ("%s does not have a property called %s",
577 g_type_name (G_OBJECT_TYPE (container)), name);
581 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
583 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
584 g_type_name (G_OBJECT_TYPE (container)),
587 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
589 g_error_free (error);
593 gtk_container_child_set_property (container, child, name, &gvalue);
594 g_value_unset (&gvalue);
599 GtkContainer *container;
601 gchar *child_prop_name;
603 gboolean translatable;
604 } PackingPropertiesData;
607 attributes_start_element (GMarkupParseContext *context,
608 const gchar *element_name,
610 const gchar **values,
614 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
617 if (strcmp (element_name, "property") == 0)
619 for (i = 0; names[i]; i++)
620 if (strcmp (names[i], "name") == 0)
621 parser_data->child_prop_name = g_strdup (values[i]);
622 else if (strcmp (names[i], "translatable") == 0)
624 if (!_gtk_builder_boolean_from_string (values[1],
625 &parser_data->translatable,
629 else if (strcmp (names[i], "comments") == 0)
630 ; /* for translators */
631 else if (strcmp (names[i], "context") == 0)
632 parser_data->context = g_strdup (values[1]);
634 g_warning ("Unsupported attribute for GtkContainer Child "
635 "property: %s\n", names[i]);
637 else if (strcmp (element_name, "packing") == 0)
640 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
644 attributes_text_element (GMarkupParseContext *context,
650 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
653 if (!parser_data->child_prop_name)
656 if (parser_data->translatable && text_len)
659 domain = gtk_builder_get_translation_domain (parser_data->builder);
661 value = _gtk_builder_parser_translate (domain,
662 parser_data->context,
667 value = g_strdup (text);
670 gtk_container_buildable_set_child_property (parser_data->container,
671 parser_data->builder,
673 parser_data->child_prop_name,
676 g_free (parser_data->child_prop_name);
677 g_free (parser_data->context);
679 parser_data->child_prop_name = NULL;
680 parser_data->context = NULL;
681 parser_data->translatable = FALSE;
684 static const GMarkupParser attributes_parser =
686 attributes_start_element,
688 attributes_text_element,
692 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
695 const gchar *tagname,
696 GMarkupParser *parser,
699 PackingPropertiesData *parser_data;
701 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
702 tagname, parser, data))
705 if (child && strcmp (tagname, "packing") == 0)
707 parser_data = g_slice_new0 (PackingPropertiesData);
708 parser_data->builder = builder;
709 parser_data->container = GTK_CONTAINER (buildable);
710 parser_data->child = GTK_WIDGET (child);
711 parser_data->child_prop_name = NULL;
713 *parser = attributes_parser;
722 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
725 const gchar *tagname,
728 if (strcmp (tagname, "packing") == 0)
730 g_slice_free (PackingPropertiesData, (gpointer)data);
735 if (parent_buildable_iface->custom_tag_end)
736 parent_buildable_iface->custom_tag_end (buildable, builder,
737 child, tagname, data);
742 * gtk_container_child_type:
743 * @container: a #GtkContainer
745 * Returns the type of the children supported by the container.
747 * Note that this may return %G_TYPE_NONE to indicate that no more
748 * children can be added, e.g. for a #GtkPaned which already has two
751 * Return value: a #GType.
754 gtk_container_child_type (GtkContainer *container)
757 GtkContainerClass *class;
759 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
761 class = GTK_CONTAINER_GET_CLASS (container);
762 if (class->child_type)
763 slot = class->child_type (container);
770 /* --- GtkContainer child property mechanism --- */
772 container_get_child_property (GtkContainer *container,
777 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
779 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
783 container_set_child_property (GtkContainer *container,
787 GObjectNotifyQueue *nqueue)
789 GValue tmp_value = { 0, };
790 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
792 /* provide a copy to work from, convert (if necessary) and validate */
793 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
794 if (!g_value_transform (value, &tmp_value))
795 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
797 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
798 G_VALUE_TYPE_NAME (value));
799 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
801 gchar *contents = g_strdup_value_contents (value);
803 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
805 G_VALUE_TYPE_NAME (value),
807 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
812 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
813 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
815 g_value_unset (&tmp_value);
819 * gtk_container_child_get_valist:
820 * @container: a #GtkContainer
821 * @child: a widget which is a child of @container
822 * @first_property_name: the name of the first property to get
823 * @var_args: return location for the first property, followed
824 * optionally by more name/return location pairs, followed by %NULL
826 * Gets the values of one or more child properties for @child and @container.
829 gtk_container_child_get_valist (GtkContainer *container,
831 const gchar *first_property_name,
836 g_return_if_fail (GTK_IS_CONTAINER (container));
837 g_return_if_fail (GTK_IS_WIDGET (child));
838 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
840 g_object_ref (container);
841 g_object_ref (child);
843 name = first_property_name;
846 GValue value = { 0, };
850 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
852 G_OBJECT_TYPE (container),
856 g_warning ("%s: container class `%s' has no child property named `%s'",
858 G_OBJECT_TYPE_NAME (container),
862 if (!(pspec->flags & G_PARAM_READABLE))
864 g_warning ("%s: child property `%s' of container class `%s' is not readable",
867 G_OBJECT_TYPE_NAME (container));
870 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
871 container_get_child_property (container, child, pspec, &value);
872 G_VALUE_LCOPY (&value, var_args, 0, &error);
875 g_warning ("%s: %s", G_STRLOC, error);
877 g_value_unset (&value);
880 g_value_unset (&value);
881 name = va_arg (var_args, gchar*);
884 g_object_unref (child);
885 g_object_unref (container);
889 * gtk_container_child_get_property:
890 * @container: a #GtkContainer
891 * @child: a widget which is a child of @container
892 * @property_name: the name of the property to get
893 * @value: a location to return the value
895 * Gets the value of a child property for @child and @container.
898 gtk_container_child_get_property (GtkContainer *container,
900 const gchar *property_name,
905 g_return_if_fail (GTK_IS_CONTAINER (container));
906 g_return_if_fail (GTK_IS_WIDGET (child));
907 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
908 g_return_if_fail (property_name != NULL);
909 g_return_if_fail (G_IS_VALUE (value));
911 g_object_ref (container);
912 g_object_ref (child);
913 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
914 G_OBJECT_TYPE (container), TRUE);
916 g_warning ("%s: container class `%s' has no child property named `%s'",
918 G_OBJECT_TYPE_NAME (container),
920 else if (!(pspec->flags & G_PARAM_READABLE))
921 g_warning ("%s: child property `%s' of container class `%s' is not readable",
924 G_OBJECT_TYPE_NAME (container));
927 GValue *prop_value, tmp_value = { 0, };
929 /* auto-conversion of the callers value type
931 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
933 g_value_reset (value);
936 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
938 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
940 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
941 G_VALUE_TYPE_NAME (value));
942 g_object_unref (child);
943 g_object_unref (container);
948 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
949 prop_value = &tmp_value;
951 container_get_child_property (container, child, pspec, prop_value);
952 if (prop_value != value)
954 g_value_transform (prop_value, value);
955 g_value_unset (&tmp_value);
958 g_object_unref (child);
959 g_object_unref (container);
963 * gtk_container_child_set_valist:
964 * @container: a #GtkContainer
965 * @child: a widget which is a child of @container
966 * @first_property_name: the name of the first property to set
967 * @var_args: a %NULL-terminated list of property names and values, starting
968 * with @first_prop_name
970 * Sets one or more child properties for @child and @container.
973 gtk_container_child_set_valist (GtkContainer *container,
975 const gchar *first_property_name,
978 GObjectNotifyQueue *nqueue;
981 g_return_if_fail (GTK_IS_CONTAINER (container));
982 g_return_if_fail (GTK_IS_WIDGET (child));
983 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
985 g_object_ref (container);
986 g_object_ref (child);
988 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
989 name = first_property_name;
992 GValue value = { 0, };
994 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
996 G_OBJECT_TYPE (container),
1000 g_warning ("%s: container class `%s' has no child property named `%s'",
1002 G_OBJECT_TYPE_NAME (container),
1006 if (!(pspec->flags & G_PARAM_WRITABLE))
1008 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1011 G_OBJECT_TYPE_NAME (container));
1014 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1015 G_VALUE_COLLECT (&value, var_args, 0, &error);
1018 g_warning ("%s: %s", G_STRLOC, error);
1021 /* we purposely leak the value here, it might not be
1022 * in a sane state if an error condition occoured
1026 container_set_child_property (container, child, pspec, &value, nqueue);
1027 g_value_unset (&value);
1028 name = va_arg (var_args, gchar*);
1030 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1032 g_object_unref (container);
1033 g_object_unref (child);
1037 * gtk_container_child_set_property:
1038 * @container: a #GtkContainer
1039 * @child: a widget which is a child of @container
1040 * @property_name: the name of the property to set
1041 * @value: the value to set the property to
1043 * Sets a child property for @child and @container.
1046 gtk_container_child_set_property (GtkContainer *container,
1048 const gchar *property_name,
1049 const GValue *value)
1051 GObjectNotifyQueue *nqueue;
1054 g_return_if_fail (GTK_IS_CONTAINER (container));
1055 g_return_if_fail (GTK_IS_WIDGET (child));
1056 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1057 g_return_if_fail (property_name != NULL);
1058 g_return_if_fail (G_IS_VALUE (value));
1060 g_object_ref (container);
1061 g_object_ref (child);
1063 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1064 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1065 G_OBJECT_TYPE (container), TRUE);
1067 g_warning ("%s: container class `%s' has no child property named `%s'",
1069 G_OBJECT_TYPE_NAME (container),
1071 else if (!(pspec->flags & G_PARAM_WRITABLE))
1072 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1075 G_OBJECT_TYPE_NAME (container));
1078 container_set_child_property (container, child, pspec, value, nqueue);
1080 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1081 g_object_unref (container);
1082 g_object_unref (child);
1086 * gtk_container_add_with_properties:
1087 * @container: a #GtkContainer
1088 * @widget: a widget to be placed inside @container
1089 * @first_prop_name: the name of the first child property to set
1090 * @Varargs: a %NULL-terminated list of property names and values, starting
1091 * with @first_prop_name
1093 * Adds @widget to @container, setting child properties at the same time.
1094 * See gtk_container_add() and gtk_container_child_set() for more details.
1097 gtk_container_add_with_properties (GtkContainer *container,
1099 const gchar *first_prop_name,
1102 g_return_if_fail (GTK_IS_CONTAINER (container));
1103 g_return_if_fail (GTK_IS_WIDGET (widget));
1104 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1106 g_object_ref (container);
1107 g_object_ref (widget);
1108 gtk_widget_freeze_child_notify (widget);
1110 g_signal_emit (container, container_signals[ADD], 0, widget);
1111 if (gtk_widget_get_parent (widget))
1115 va_start (var_args, first_prop_name);
1116 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1120 gtk_widget_thaw_child_notify (widget);
1121 g_object_unref (widget);
1122 g_object_unref (container);
1126 * gtk_container_child_set:
1127 * @container: a #GtkContainer
1128 * @child: a widget which is a child of @container
1129 * @first_prop_name: the name of the first property to set
1130 * @Varargs: a %NULL-terminated list of property names and values, starting
1131 * with @first_prop_name
1133 * Sets one or more child properties for @child and @container.
1136 gtk_container_child_set (GtkContainer *container,
1138 const gchar *first_prop_name,
1143 g_return_if_fail (GTK_IS_CONTAINER (container));
1144 g_return_if_fail (GTK_IS_WIDGET (child));
1145 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1147 va_start (var_args, first_prop_name);
1148 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1153 * gtk_container_child_get:
1154 * @container: a #GtkContainer
1155 * @child: a widget which is a child of @container
1156 * @first_prop_name: the name of the first property to get
1157 * @Varargs: return location for the first property, followed
1158 * optionally by more name/return location pairs, followed by %NULL
1160 * Gets the values of one or more child properties for @child and @container.
1163 gtk_container_child_get (GtkContainer *container,
1165 const gchar *first_prop_name,
1170 g_return_if_fail (GTK_IS_CONTAINER (container));
1171 g_return_if_fail (GTK_IS_WIDGET (child));
1172 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1174 va_start (var_args, first_prop_name);
1175 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1180 * gtk_container_class_install_child_property:
1181 * @cclass: a #GtkContainerClass
1182 * @property_id: the id for the property
1183 * @pspec: the #GParamSpec for the property
1185 * Installs a child property on a container class.
1188 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1192 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1193 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1194 if (pspec->flags & G_PARAM_WRITABLE)
1195 g_return_if_fail (cclass->set_child_property != NULL);
1196 if (pspec->flags & G_PARAM_READABLE)
1197 g_return_if_fail (cclass->get_child_property != NULL);
1198 g_return_if_fail (property_id > 0);
1199 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1200 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1201 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1203 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1205 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1206 G_OBJECT_CLASS_NAME (cclass),
1210 g_param_spec_ref (pspec);
1211 g_param_spec_sink (pspec);
1212 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1213 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1217 * gtk_container_class_find_child_property:
1218 * @cclass: a #GtkContainerClass
1219 * @property_name: the name of the child property to find
1220 * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
1221 * child property with that name.
1223 * Finds a child property of a container class by name.
1226 gtk_container_class_find_child_property (GObjectClass *cclass,
1227 const gchar *property_name)
1229 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1230 g_return_val_if_fail (property_name != NULL, NULL);
1232 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1234 G_OBJECT_CLASS_TYPE (cclass),
1239 * gtk_container_class_list_child_properties:
1240 * @cclass: a #GtkContainerClass
1241 * @n_properties: location to return the number of child properties found
1242 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
1243 * The array must be freed with g_free().
1245 * Returns all child properties of a container class.
1248 gtk_container_class_list_child_properties (GObjectClass *cclass,
1249 guint *n_properties)
1251 GParamSpec **pspecs;
1254 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1256 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1257 G_OBJECT_CLASS_TYPE (cclass),
1266 gtk_container_add_unimplemented (GtkContainer *container,
1269 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1273 gtk_container_remove_unimplemented (GtkContainer *container,
1276 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1280 gtk_container_init (GtkContainer *container)
1282 GtkContainerPrivate *priv;
1284 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1286 GtkContainerPrivate);
1287 priv = container->priv;
1289 priv->focus_child = NULL;
1290 priv->border_width = 0;
1291 priv->need_resize = FALSE;
1292 priv->resize_mode = GTK_RESIZE_PARENT;
1293 priv->reallocate_redraws = FALSE;
1297 gtk_container_destroy (GtkWidget *widget)
1299 GtkContainer *container = GTK_CONTAINER (widget);
1300 GtkContainerPrivate *priv = container->priv;
1302 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1303 _gtk_container_dequeue_resize_handler (container);
1305 if (priv->focus_child)
1307 g_object_unref (priv->focus_child);
1308 priv->focus_child = NULL;
1311 /* do this before walking child widgets, to avoid
1312 * removing children from focus chain one by one.
1314 if (priv->has_focus_chain)
1315 gtk_container_unset_focus_chain (container);
1317 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1319 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1323 gtk_container_set_property (GObject *object,
1325 const GValue *value,
1328 GtkContainer *container = GTK_CONTAINER (object);
1332 case PROP_BORDER_WIDTH:
1333 gtk_container_set_border_width (container, g_value_get_uint (value));
1335 case PROP_RESIZE_MODE:
1336 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1339 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1348 gtk_container_get_property (GObject *object,
1353 GtkContainer *container = GTK_CONTAINER (object);
1354 GtkContainerPrivate *priv = container->priv;
1358 case PROP_BORDER_WIDTH:
1359 g_value_set_uint (value, priv->border_width);
1361 case PROP_RESIZE_MODE:
1362 g_value_set_enum (value, priv->resize_mode);
1365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1371 * gtk_container_set_border_width:
1372 * @container: a #GtkContainer
1373 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1374 * the container. Valid values are in the range 0-65535 pixels.
1376 * Sets the border width of the container.
1378 * The border width of a container is the amount of space to leave
1379 * around the outside of the container. The only exception to this is
1380 * #GtkWindow; because toplevel windows can't leave space outside,
1381 * they leave the space inside. The border is added on all sides of
1382 * the container. To add space to only one side, one approach is to
1383 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1384 * to give it a size, and place it on the side of the container as
1388 gtk_container_set_border_width (GtkContainer *container,
1391 GtkContainerPrivate *priv;
1393 g_return_if_fail (GTK_IS_CONTAINER (container));
1395 priv = container->priv;
1397 if (priv->border_width != border_width)
1399 priv->border_width = border_width;
1400 g_object_notify (G_OBJECT (container), "border-width");
1402 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1403 gtk_widget_queue_resize (GTK_WIDGET (container));
1408 * gtk_container_get_border_width:
1409 * @container: a #GtkContainer
1411 * Retrieves the border width of the container. See
1412 * gtk_container_set_border_width().
1414 * Return value: the current border width
1417 gtk_container_get_border_width (GtkContainer *container)
1419 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1421 return container->priv->border_width;
1425 * gtk_container_add:
1426 * @container: a #GtkContainer
1427 * @widget: a widget to be placed inside @container
1429 * Adds @widget to @container. Typically used for simple containers
1430 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1431 * layout containers such as #GtkBox or #GtkTable, this function will
1432 * pick default packing parameters that may not be correct. So
1433 * consider functions such as gtk_box_pack_start() and
1434 * gtk_table_attach() as an alternative to gtk_container_add() in
1435 * those cases. A widget may be added to only one container at a time;
1436 * you can't place the same widget inside two different containers.
1439 gtk_container_add (GtkContainer *container,
1444 g_return_if_fail (GTK_IS_CONTAINER (container));
1445 g_return_if_fail (GTK_IS_WIDGET (widget));
1447 parent = gtk_widget_get_parent (widget);
1451 g_warning ("Attempting to add a widget with type %s to a container of "
1452 "type %s, but the widget is already inside a container of type %s, "
1453 "please use gtk_widget_reparent()" ,
1454 g_type_name (G_OBJECT_TYPE (widget)),
1455 g_type_name (G_OBJECT_TYPE (container)),
1456 g_type_name (G_OBJECT_TYPE (parent)));
1460 g_signal_emit (container, container_signals[ADD], 0, widget);
1464 * gtk_container_remove:
1465 * @container: a #GtkContainer
1466 * @widget: a current child of @container
1468 * Removes @widget from @container. @widget must be inside @container.
1469 * Note that @container will own a reference to @widget, and that this
1470 * may be the last reference held; so removing a widget from its
1471 * container can destroy that widget. If you want to use @widget
1472 * again, you need to add a reference to it while it's not inside
1473 * a container, using g_object_ref(). If you don't want to use @widget
1474 * again it's usually more efficient to simply destroy it directly
1475 * using gtk_widget_destroy() since this will remove it from the
1476 * container and help break any circular reference count cycles.
1479 gtk_container_remove (GtkContainer *container,
1482 g_return_if_fail (GTK_IS_CONTAINER (container));
1483 g_return_if_fail (GTK_IS_WIDGET (widget));
1484 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1486 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1490 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1492 g_return_if_fail (GTK_IS_CONTAINER (container));
1493 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1495 container_resize_queue = g_slist_remove (container_resize_queue, container);
1496 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1500 * gtk_container_set_resize_mode:
1501 * @container: a #GtkContainer
1502 * @resize_mode: the new resize mode
1504 * Sets the resize mode for the container.
1506 * The resize mode of a container determines whether a resize request
1507 * will be passed to the container's parent, queued for later execution
1508 * or executed immediately.
1511 gtk_container_set_resize_mode (GtkContainer *container,
1512 GtkResizeMode resize_mode)
1514 GtkContainerPrivate *priv;
1516 g_return_if_fail (GTK_IS_CONTAINER (container));
1517 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1519 priv = container->priv;
1521 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1522 resize_mode == GTK_RESIZE_PARENT)
1524 resize_mode = GTK_RESIZE_QUEUE;
1527 if (priv->resize_mode != resize_mode)
1529 priv->resize_mode = resize_mode;
1531 gtk_widget_queue_resize (GTK_WIDGET (container));
1532 g_object_notify (G_OBJECT (container), "resize-mode");
1537 * gtk_container_get_resize_mode:
1538 * @container: a #GtkContainer
1540 * Returns the resize mode for the container. See
1541 * gtk_container_set_resize_mode ().
1543 * Return value: the current resize mode
1546 gtk_container_get_resize_mode (GtkContainer *container)
1548 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1550 return container->priv->resize_mode;
1554 * gtk_container_set_reallocate_redraws:
1555 * @container: a #GtkContainer
1556 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1558 * Sets the @reallocate_redraws flag of the container to the given value.
1560 * Containers requesting reallocation redraws get automatically
1561 * redrawn if any of their children changed allocation.
1564 gtk_container_set_reallocate_redraws (GtkContainer *container,
1565 gboolean needs_redraws)
1567 g_return_if_fail (GTK_IS_CONTAINER (container));
1569 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1572 static GtkContainer*
1573 gtk_container_get_resize_container (GtkContainer *container)
1576 GtkWidget *widget = GTK_WIDGET (container);
1578 while ((parent = gtk_widget_get_parent (widget)))
1581 if (GTK_IS_RESIZE_CONTAINER (widget))
1585 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1589 gtk_container_idle_sizer (gpointer data)
1591 /* we may be invoked with a container_resize_queue of NULL, because
1592 * queue_resize could have been adding an extra idle function while
1593 * the queue still got processed. we better just ignore such case
1594 * than trying to explicitely work around them with some extra flags,
1595 * since it doesn't cause any actual harm.
1597 while (container_resize_queue)
1602 slist = container_resize_queue;
1603 container_resize_queue = slist->next;
1604 widget = slist->data;
1605 g_slist_free_1 (slist);
1607 _gtk_widget_set_resize_pending (widget, FALSE);
1608 gtk_container_check_resize (GTK_CONTAINER (widget));
1611 gdk_window_process_all_updates ();
1617 _gtk_container_queue_resize_internal (GtkContainer *container,
1618 gboolean invalidate_only)
1620 GtkContainerPrivate *priv;
1621 GtkContainer *resize_container;
1625 g_return_if_fail (GTK_IS_CONTAINER (container));
1627 priv = container->priv;
1628 widget = GTK_WIDGET (container);
1630 resize_container = gtk_container_get_resize_container (container);
1634 _gtk_widget_set_alloc_needed (widget, TRUE);
1635 _gtk_widget_set_width_request_needed (widget, TRUE);
1636 _gtk_widget_set_height_request_needed (widget, TRUE);
1638 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1639 !(parent = gtk_widget_get_parent (widget)))
1645 if (resize_container && !invalidate_only)
1647 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1648 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1649 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1651 switch (resize_container->priv->resize_mode)
1653 case GTK_RESIZE_QUEUE:
1654 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1656 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1657 if (container_resize_queue == NULL)
1658 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1659 gtk_container_idle_sizer,
1661 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1665 case GTK_RESIZE_IMMEDIATE:
1666 gtk_container_check_resize (resize_container);
1669 case GTK_RESIZE_PARENT:
1670 g_assert_not_reached ();
1676 /* we need to let hidden resize containers know that something
1677 * changed while they where hidden (currently only evaluated by
1680 resize_container->priv->need_resize = TRUE;
1686 * _gtk_container_queue_resize:
1687 * @container: a #GtkContainer
1689 * Determines the "resize container" in the hierarchy above this container
1690 * (typically the toplevel, but other containers can be set as resize
1691 * containers with gtk_container_set_resize_mode()), marks the container
1692 * and all parents up to and including the resize container as needing
1693 * to have sizes recompted, and if necessary adds the resize container
1694 * to the queue of containers that will be resized out at idle.
1697 _gtk_container_queue_resize (GtkContainer *container)
1699 _gtk_container_queue_resize_internal (container, FALSE);
1703 * _gtk_container_resize_invalidate:
1704 * @container: a #GtkContainer
1706 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1707 * actually queue the resize container for resize.
1710 _gtk_container_resize_invalidate (GtkContainer *container)
1712 _gtk_container_queue_resize_internal (container, TRUE);
1716 gtk_container_check_resize (GtkContainer *container)
1718 g_return_if_fail (GTK_IS_CONTAINER (container));
1720 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1724 gtk_container_real_check_resize (GtkContainer *container)
1726 GtkWidget *widget = GTK_WIDGET (container);
1727 GtkAllocation allocation;
1728 GtkRequisition requisition;
1730 gtk_widget_get_preferred_size (widget,
1731 &requisition, NULL);
1732 gtk_widget_get_allocation (widget, &allocation);
1734 if (requisition.width > allocation.width ||
1735 requisition.height > allocation.height)
1737 if (GTK_IS_RESIZE_CONTAINER (container))
1739 gtk_widget_size_allocate (widget, &allocation);
1740 gtk_widget_set_allocation (widget, &allocation);
1743 gtk_widget_queue_resize (widget);
1747 gtk_container_resize_children (container);
1751 /* The container hasn't changed size but one of its children
1752 * queued a resize request. Which means that the allocation
1753 * is not sufficient for the requisition of some child.
1754 * We've already performed a size request at this point,
1755 * so we simply need to reallocate and let the allocation
1756 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1759 gtk_container_resize_children (GtkContainer *container)
1761 GtkAllocation allocation;
1764 /* resizing invariants:
1765 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1766 * containers that have an idle sizer pending must be flagged with
1769 g_return_if_fail (GTK_IS_CONTAINER (container));
1771 widget = GTK_WIDGET (container);
1772 gtk_widget_get_allocation (widget, &allocation);
1774 gtk_widget_size_allocate (widget, &allocation);
1775 gtk_widget_set_allocation (widget, &allocation);
1779 gtk_container_adjust_size_request (GtkWidget *widget,
1780 GtkOrientation orientation,
1784 GtkContainer *container;
1786 container = GTK_CONTAINER (widget);
1788 if (GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1792 border_width = container->priv->border_width;
1794 *minimum_size += border_width * 2;
1795 *natural_size += border_width * 2;
1798 /* chain up last so gtk_widget_set_size_request() values
1799 * will have a chance to overwrite our border width.
1801 parent_class->adjust_size_request (widget, orientation,
1802 minimum_size, natural_size);
1806 gtk_container_adjust_size_allocation (GtkWidget *widget,
1807 GtkOrientation orientation,
1809 gint *allocated_pos,
1810 gint *allocated_size)
1812 GtkContainer *container;
1815 container = GTK_CONTAINER (widget);
1817 if (!GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1819 parent_class->adjust_size_allocation (widget, orientation,
1820 natural_size, allocated_pos,
1825 border_width = container->priv->border_width;
1827 *allocated_size -= border_width * 2;
1829 /* If we get a pathological too-small allocation to hold
1830 * even the border width, leave all allocation to the actual
1831 * widget, and leave x,y unchanged. (GtkWidget's min size is
1832 * 1x1 if you're wondering why <1 and not <0)
1834 * As long as we have space, set x,y properly.
1837 if (*allocated_size < 1)
1839 *allocated_size += border_width * 2;
1843 *allocated_pos += border_width;
1844 *natural_size -= border_width * 2;
1847 /* Chain up to GtkWidgetClass *after* removing our border width from
1848 * the proposed allocation size. This is because it's possible that the
1849 * widget was allocated more space than it needs in a said orientation,
1850 * if GtkWidgetClass does any alignments and thus limits the size to the
1851 * natural size... then we need that to be done *after* removing any margins
1852 * and padding values.
1854 parent_class->adjust_size_allocation (widget, orientation,
1855 natural_size, allocated_pos,
1860 * gtk_container_class_handle_border_width:
1861 * @klass: the class struct of a #GtkContainer subclass
1863 * Modifies a subclass of #GtkContainerClass to automatically add and
1864 * remove the border-width setting on GtkContainer. This allows the
1865 * subclass to ignore the border width in its size request and
1866 * allocate methods. The intent is for a subclass to invoke this
1867 * in its class_init function.
1869 * gtk_container_class_handle_border_width() is necessary because it
1870 * would break API too badly to make this behavior the default. So
1871 * subclasses must "opt in" to the parent class handling border_width
1875 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1877 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1879 klass->handle_border_width = TRUE;
1883 * gtk_container_forall:
1884 * @container: a #GtkContainer
1885 * @callback: a callback
1886 * @callback_data: callback user data
1888 * Invokes @callback on each child of @container, including children
1889 * that are considered "internal" (implementation details of the
1890 * container). "Internal" children generally weren't added by the user
1891 * of the container, but were added by the container implementation
1892 * itself. Most applications should use gtk_container_foreach(),
1893 * rather than gtk_container_forall().
1896 gtk_container_forall (GtkContainer *container,
1897 GtkCallback callback,
1898 gpointer callback_data)
1900 GtkContainerClass *class;
1902 g_return_if_fail (GTK_IS_CONTAINER (container));
1903 g_return_if_fail (callback != NULL);
1905 class = GTK_CONTAINER_GET_CLASS (container);
1908 class->forall (container, TRUE, callback, callback_data);
1912 * gtk_container_foreach:
1913 * @container: a #GtkContainer
1914 * @callback: (scope call): a callback
1915 * @callback_data: callback user data
1917 * Invokes @callback on each non-internal child of @container. See
1918 * gtk_container_forall() for details on what constitutes an
1919 * "internal" child. Most applications should use
1920 * gtk_container_foreach(), rather than gtk_container_forall().
1923 gtk_container_foreach (GtkContainer *container,
1924 GtkCallback callback,
1925 gpointer callback_data)
1927 GtkContainerClass *class;
1929 g_return_if_fail (GTK_IS_CONTAINER (container));
1930 g_return_if_fail (callback != NULL);
1932 class = GTK_CONTAINER_GET_CLASS (container);
1935 class->forall (container, FALSE, callback, callback_data);
1939 * gtk_container_set_focus_child:
1940 * @container: a #GtkContainer
1941 * @child: (allow-none): a #GtkWidget, or %NULL
1943 * Sets, or unsets if @child is %NULL, the focused child of @container.
1945 * This function emits the GtkContainer::set_focus_child signal of
1946 * @container. Implementations of #GtkContainer can override the
1947 * default behaviour by overriding the class closure of this signal.
1949 * This is function is mostly meant to be used by widgets. Applications can use
1950 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1953 gtk_container_set_focus_child (GtkContainer *container,
1956 g_return_if_fail (GTK_IS_CONTAINER (container));
1958 g_return_if_fail (GTK_IS_WIDGET (child));
1960 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1964 * gtk_container_get_focus_child:
1965 * @container: a #GtkContainer
1967 * Returns the current focus child widget inside @container. This is not the
1968 * currently focused widget. That can be obtained by calling
1969 * gtk_window_get_focus().
1971 * Returns: The child widget which will recieve the focus inside @container when
1972 * the @conatiner is focussed, or %NULL if none is set.
1977 gtk_container_get_focus_child (GtkContainer *container)
1979 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1981 return container->priv->focus_child;
1985 * gtk_container_get_children:
1986 * @container: a #GtkContainer
1988 * Returns the container's non-internal children. See
1989 * gtk_container_forall() for details on what constitutes an "internal" child.
1991 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1994 gtk_container_get_children (GtkContainer *container)
1996 GList *children = NULL;
1998 gtk_container_foreach (container,
1999 gtk_container_children_callback,
2002 return g_list_reverse (children);
2006 gtk_container_child_position_callback (GtkWidget *widget,
2007 gpointer client_data)
2013 } *data = client_data;
2016 if (data->child == widget)
2017 data->index = data->i;
2021 gtk_container_child_default_composite_name (GtkContainer *container,
2031 /* fallback implementation */
2035 gtk_container_forall (container,
2036 gtk_container_child_position_callback,
2039 name = g_strdup_printf ("%s-%u",
2040 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2047 _gtk_container_child_composite_name (GtkContainer *container,
2050 gboolean composite_child;
2052 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2053 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2054 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2056 g_object_get (child, "composite-child", &composite_child, NULL);
2057 if (composite_child)
2059 static GQuark quark_composite_name = 0;
2062 if (!quark_composite_name)
2063 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2065 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2068 GtkContainerClass *class;
2070 class = GTK_CONTAINER_GET_CLASS (container);
2071 if (class->composite_name)
2072 name = class->composite_name (container, child);
2075 name = g_strdup (name);
2086 } ComputeExpandData;
2089 gtk_container_compute_expand_callback (GtkWidget *widget,
2090 gpointer client_data)
2092 ComputeExpandData *data = client_data;
2094 /* note that we don't get_expand on the child if we already know we
2095 * have to expand, so we only recurse into children until we find
2096 * one that expands and then we basically don't do any more
2097 * work. This means that we can leave some children in a
2098 * need_compute_expand state, which is fine, as long as GtkWidget
2099 * doesn't rely on an invariant that "if a child has
2100 * need_compute_expand, its parents also do"
2102 * gtk_widget_compute_expand() always returns FALSE if the
2103 * child is !visible so that's taken care of.
2105 data->hexpand = data->hexpand ||
2106 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2108 data->vexpand = data->vexpand ||
2109 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2113 gtk_container_compute_expand (GtkWidget *widget,
2114 gboolean *hexpand_p,
2115 gboolean *vexpand_p)
2117 ComputeExpandData data;
2119 data.hexpand = FALSE;
2120 data.vexpand = FALSE;
2122 gtk_container_forall (GTK_CONTAINER (widget),
2123 gtk_container_compute_expand_callback,
2126 *hexpand_p = data.hexpand;
2127 *vexpand_p = data.vexpand;
2131 gtk_container_real_set_focus_child (GtkContainer *container,
2134 GtkContainerPrivate *priv;
2136 g_return_if_fail (GTK_IS_CONTAINER (container));
2137 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2139 priv = container->priv;
2141 if (child != priv->focus_child)
2143 if (priv->focus_child)
2144 g_object_unref (priv->focus_child);
2145 priv->focus_child = child;
2146 if (priv->focus_child)
2147 g_object_ref (priv->focus_child);
2151 /* check for h/v adjustments
2153 if (priv->focus_child)
2155 GtkAdjustment *hadj;
2156 GtkAdjustment *vadj;
2157 GtkAllocation allocation;
2158 GtkWidget *focus_child;
2161 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2162 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2166 focus_child = priv->focus_child;
2167 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2169 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2172 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2175 gtk_widget_get_allocation (priv->focus_child, &allocation);
2179 gtk_widget_get_allocation (focus_child, &allocation);
2182 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2185 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2191 get_focus_chain (GtkContainer *container)
2193 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2196 /* same as gtk_container_get_children, except it includes internals
2199 gtk_container_get_all_children (GtkContainer *container)
2201 GList *children = NULL;
2203 gtk_container_forall (container,
2204 gtk_container_children_callback,
2211 gtk_container_focus (GtkWidget *widget,
2212 GtkDirectionType direction)
2215 GList *sorted_children;
2217 GtkContainer *container;
2218 GtkContainerPrivate *priv;
2220 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2222 container = GTK_CONTAINER (widget);
2223 priv = container->priv;
2227 if (gtk_widget_get_can_focus (widget))
2229 if (!gtk_widget_has_focus (widget))
2231 gtk_widget_grab_focus (widget);
2237 /* Get a list of the containers children, allowing focus
2238 * chain to override.
2240 if (priv->has_focus_chain)
2241 children = g_list_copy (get_focus_chain (container));
2243 children = gtk_container_get_all_children (container);
2245 if (priv->has_focus_chain &&
2246 (direction == GTK_DIR_TAB_FORWARD ||
2247 direction == GTK_DIR_TAB_BACKWARD))
2249 sorted_children = g_list_copy (children);
2251 if (direction == GTK_DIR_TAB_BACKWARD)
2252 sorted_children = g_list_reverse (sorted_children);
2255 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2257 return_val = gtk_container_focus_move (container, sorted_children, direction);
2259 g_list_free (sorted_children);
2260 g_list_free (children);
2267 tab_compare (gconstpointer a,
2271 GtkAllocation child1_allocation, child2_allocation;
2272 const GtkWidget *child1 = a;
2273 const GtkWidget *child2 = b;
2274 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2276 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2277 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2279 gint y1 = child1_allocation.y + child1_allocation.height / 2;
2280 gint y2 = child2_allocation.y + child2_allocation.height / 2;
2284 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2285 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2287 if (text_direction == GTK_TEXT_DIR_RTL)
2288 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2290 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2293 return (y1 < y2) ? -1 : 1;
2297 gtk_container_focus_sort_tab (GtkContainer *container,
2299 GtkDirectionType direction,
2300 GtkWidget *old_focus)
2302 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2303 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2305 /* if we are going backwards then reverse the order
2308 if (direction == GTK_DIR_TAB_BACKWARD)
2309 children = g_list_reverse (children);
2314 /* Get coordinates of @widget's allocation with respect to
2315 * allocation of @container.
2318 get_allocation_coords (GtkContainer *container,
2320 GdkRectangle *allocation)
2322 gtk_widget_get_allocation (widget, allocation);
2324 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2325 0, 0, &allocation->x, &allocation->y);
2328 /* Look for a child in @children that is intermediate between
2329 * the focus widget and container. This widget, if it exists,
2330 * acts as the starting widget for focus navigation.
2333 find_old_focus (GtkContainer *container,
2336 GList *tmp_list = children;
2339 GtkWidget *child = tmp_list->data;
2340 GtkWidget *widget = child;
2342 while (widget && widget != (GtkWidget *)container)
2346 parent = gtk_widget_get_parent (widget);
2348 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2357 tmp_list = tmp_list->next;
2364 old_focus_coords (GtkContainer *container,
2365 GdkRectangle *old_focus_rect)
2367 GtkWidget *widget = GTK_WIDGET (container);
2368 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2369 GtkWidget *old_focus;
2371 if (GTK_IS_WINDOW (toplevel))
2373 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2375 return get_allocation_coords (container, old_focus, old_focus_rect);
2381 typedef struct _CompareInfo CompareInfo;
2385 GtkContainer *container;
2392 up_down_compare (gconstpointer a,
2396 GdkRectangle allocation1;
2397 GdkRectangle allocation2;
2398 CompareInfo *compare = data;
2401 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2402 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2404 y1 = allocation1.y + allocation1.height / 2;
2405 y2 = allocation2.y + allocation2.height / 2;
2409 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2410 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2412 if (compare->reverse)
2413 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2415 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2418 return (y1 < y2) ? -1 : 1;
2422 gtk_container_focus_sort_up_down (GtkContainer *container,
2424 GtkDirectionType direction,
2425 GtkWidget *old_focus)
2427 CompareInfo compare;
2429 GdkRectangle old_allocation;
2431 compare.container = container;
2432 compare.reverse = (direction == GTK_DIR_UP);
2435 old_focus = find_old_focus (container, children);
2437 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2443 /* Delete widgets from list that don't match minimum criteria */
2445 compare_x1 = old_allocation.x;
2446 compare_x2 = old_allocation.x + old_allocation.width;
2448 if (direction == GTK_DIR_UP)
2449 compare_y = old_allocation.y;
2451 compare_y = old_allocation.y + old_allocation.height;
2453 tmp_list = children;
2456 GtkWidget *child = tmp_list->data;
2457 GList *next = tmp_list->next;
2458 gint child_x1, child_x2;
2459 GdkRectangle child_allocation;
2461 if (child != old_focus)
2463 if (get_allocation_coords (container, child, &child_allocation))
2465 child_x1 = child_allocation.x;
2466 child_x2 = child_allocation.x + child_allocation.width;
2468 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2469 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2470 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2472 children = g_list_delete_link (children, tmp_list);
2476 children = g_list_delete_link (children, tmp_list);
2482 compare.x = (compare_x1 + compare_x2) / 2;
2483 compare.y = old_allocation.y + old_allocation.height / 2;
2487 /* No old focus widget, need to figure out starting x,y some other way
2489 GtkAllocation allocation;
2490 GtkWidget *widget = GTK_WIDGET (container);
2491 GdkRectangle old_focus_rect;
2493 gtk_widget_get_allocation (widget, &allocation);
2495 if (old_focus_coords (container, &old_focus_rect))
2497 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2501 if (!gtk_widget_get_has_window (widget))
2502 compare.x = allocation.x + allocation.width / 2;
2504 compare.x = allocation.width / 2;
2507 if (!gtk_widget_get_has_window (widget))
2508 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2510 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2513 children = g_list_sort_with_data (children, up_down_compare, &compare);
2515 if (compare.reverse)
2516 children = g_list_reverse (children);
2522 left_right_compare (gconstpointer a,
2526 GdkRectangle allocation1;
2527 GdkRectangle allocation2;
2528 CompareInfo *compare = data;
2531 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2532 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2534 x1 = allocation1.x + allocation1.width / 2;
2535 x2 = allocation2.x + allocation2.width / 2;
2539 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2540 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2542 if (compare->reverse)
2543 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2545 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2548 return (x1 < x2) ? -1 : 1;
2552 gtk_container_focus_sort_left_right (GtkContainer *container,
2554 GtkDirectionType direction,
2555 GtkWidget *old_focus)
2557 CompareInfo compare;
2559 GdkRectangle old_allocation;
2561 compare.container = container;
2562 compare.reverse = (direction == GTK_DIR_LEFT);
2565 old_focus = find_old_focus (container, children);
2567 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2573 /* Delete widgets from list that don't match minimum criteria */
2575 compare_y1 = old_allocation.y;
2576 compare_y2 = old_allocation.y + old_allocation.height;
2578 if (direction == GTK_DIR_LEFT)
2579 compare_x = old_allocation.x;
2581 compare_x = old_allocation.x + old_allocation.width;
2583 tmp_list = children;
2586 GtkWidget *child = tmp_list->data;
2587 GList *next = tmp_list->next;
2588 gint child_y1, child_y2;
2589 GdkRectangle child_allocation;
2591 if (child != old_focus)
2593 if (get_allocation_coords (container, child, &child_allocation))
2595 child_y1 = child_allocation.y;
2596 child_y2 = child_allocation.y + child_allocation.height;
2598 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2599 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2600 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2602 children = g_list_delete_link (children, tmp_list);
2606 children = g_list_delete_link (children, tmp_list);
2612 compare.y = (compare_y1 + compare_y2) / 2;
2613 compare.x = old_allocation.x + old_allocation.width / 2;
2617 /* No old focus widget, need to figure out starting x,y some other way
2619 GtkAllocation allocation;
2620 GtkWidget *widget = GTK_WIDGET (container);
2621 GdkRectangle old_focus_rect;
2623 gtk_widget_get_allocation (widget, &allocation);
2625 if (old_focus_coords (container, &old_focus_rect))
2627 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2631 if (!gtk_widget_get_has_window (widget))
2632 compare.y = allocation.y + allocation.height / 2;
2634 compare.y = allocation.height / 2;
2637 if (!gtk_widget_get_has_window (widget))
2638 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2640 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2643 children = g_list_sort_with_data (children, left_right_compare, &compare);
2645 if (compare.reverse)
2646 children = g_list_reverse (children);
2652 * gtk_container_focus_sort:
2653 * @container: a #GtkContainer
2654 * @children: a list of descendents of @container (they don't
2655 * have to be direct children)
2656 * @direction: focus direction
2657 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2658 * to determine this automatically.
2659 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2660 * which is the only @direction we use currently,
2661 * so perhaps this argument should be removed)
2663 * Sorts @children in the correct order for focusing with
2664 * direction type @direction.
2666 * Return value: a copy of @children, sorted in correct focusing order,
2667 * with children that aren't suitable for focusing in this direction
2671 _gtk_container_focus_sort (GtkContainer *container,
2673 GtkDirectionType direction,
2674 GtkWidget *old_focus)
2676 GList *visible_children = NULL;
2680 if (gtk_widget_get_realized (children->data))
2681 visible_children = g_list_prepend (visible_children, children->data);
2682 children = children->next;
2687 case GTK_DIR_TAB_FORWARD:
2688 case GTK_DIR_TAB_BACKWARD:
2689 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2692 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2695 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2698 g_assert_not_reached ();
2704 gtk_container_focus_move (GtkContainer *container,
2706 GtkDirectionType direction)
2708 GtkContainerPrivate *priv = container->priv;
2709 GtkWidget *focus_child;
2712 focus_child = priv->focus_child;
2716 child = children->data;
2717 children = children->next;
2724 if (focus_child == child)
2728 if (gtk_widget_child_focus (child, direction))
2732 else if (gtk_widget_is_drawable (child) &&
2733 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2735 if (gtk_widget_child_focus (child, direction))
2745 gtk_container_children_callback (GtkWidget *widget,
2746 gpointer client_data)
2750 children = (GList**) client_data;
2751 *children = g_list_prepend (*children, widget);
2755 chain_widget_destroyed (GtkWidget *widget,
2758 GtkContainer *container;
2761 container = GTK_CONTAINER (user_data);
2763 chain = g_object_get_data (G_OBJECT (container),
2764 "gtk-container-focus-chain");
2766 chain = g_list_remove (chain, widget);
2768 g_signal_handlers_disconnect_by_func (widget,
2769 chain_widget_destroyed,
2772 g_object_set_data (G_OBJECT (container),
2773 I_("gtk-container-focus-chain"),
2778 * gtk_container_set_focus_chain:
2779 * @container: a #GtkContainer
2780 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2781 * the new focus chain
2783 * Sets a focus chain, overriding the one computed automatically by GTK+.
2785 * In principle each widget in the chain should be a descendant of the
2786 * container, but this is not enforced by this method, since it's allowed
2787 * to set the focus chain before you pack the widgets, or have a widget
2788 * in the chain that isn't always packed. The necessary checks are done
2789 * when the focus chain is actually traversed.
2792 gtk_container_set_focus_chain (GtkContainer *container,
2793 GList *focusable_widgets)
2797 GtkContainerPrivate *priv;
2799 g_return_if_fail (GTK_IS_CONTAINER (container));
2801 priv = container->priv;
2803 if (priv->has_focus_chain)
2804 gtk_container_unset_focus_chain (container);
2806 priv->has_focus_chain = TRUE;
2809 tmp_list = focusable_widgets;
2810 while (tmp_list != NULL)
2812 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2814 /* In principle each widget in the chain should be a descendant
2815 * of the container, but we don't want to check that here, it's
2816 * expensive and also it's allowed to set the focus chain before
2817 * you pack the widgets, or have a widget in the chain that isn't
2818 * always packed. So we check for ancestor during actual traversal.
2821 chain = g_list_prepend (chain, tmp_list->data);
2823 g_signal_connect (tmp_list->data,
2825 G_CALLBACK (chain_widget_destroyed),
2828 tmp_list = g_list_next (tmp_list);
2831 chain = g_list_reverse (chain);
2833 g_object_set_data (G_OBJECT (container),
2834 I_("gtk-container-focus-chain"),
2839 * gtk_container_get_focus_chain:
2840 * @container: a #GtkContainer
2841 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2842 * to store the focus chain of the
2843 * container, or %NULL. You should free this list
2844 * using g_list_free() when you are done with it, however
2845 * no additional reference count is added to the
2846 * individual widgets in the focus chain.
2848 * Retrieves the focus chain of the container, if one has been
2849 * set explicitly. If no focus chain has been explicitly
2850 * set, GTK+ computes the focus chain based on the positions
2851 * of the children. In that case, GTK+ stores %NULL in
2852 * @focusable_widgets and returns %FALSE.
2854 * Return value: %TRUE if the focus chain of the container
2855 * has been set explicitly.
2858 gtk_container_get_focus_chain (GtkContainer *container,
2859 GList **focus_chain)
2861 GtkContainerPrivate *priv;
2863 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2865 priv = container->priv;
2869 if (priv->has_focus_chain)
2870 *focus_chain = g_list_copy (get_focus_chain (container));
2872 *focus_chain = NULL;
2875 return priv->has_focus_chain;
2879 * gtk_container_unset_focus_chain:
2880 * @container: a #GtkContainer
2882 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2885 gtk_container_unset_focus_chain (GtkContainer *container)
2887 GtkContainerPrivate *priv;
2889 g_return_if_fail (GTK_IS_CONTAINER (container));
2891 priv = container->priv;
2893 if (priv->has_focus_chain)
2898 chain = get_focus_chain (container);
2900 priv->has_focus_chain = FALSE;
2902 g_object_set_data (G_OBJECT (container),
2903 I_("gtk-container-focus-chain"),
2907 while (tmp_list != NULL)
2909 g_signal_handlers_disconnect_by_func (tmp_list->data,
2910 chain_widget_destroyed,
2913 tmp_list = g_list_next (tmp_list);
2916 g_list_free (chain);
2921 * gtk_container_set_focus_vadjustment:
2922 * @container: a #GtkContainer
2923 * @adjustment: an adjustment which should be adjusted when the focus
2924 * is moved among the descendents of @container
2926 * Hooks up an adjustment to focus handling in a container, so when a
2927 * child of the container is focused, the adjustment is scrolled to
2928 * show that widget. This function sets the vertical alignment. See
2929 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2930 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2931 * the horizontal adjustment.
2933 * The adjustments have to be in pixel units and in the same coordinate
2934 * system as the allocation for immediate children of the container.
2937 gtk_container_set_focus_vadjustment (GtkContainer *container,
2938 GtkAdjustment *adjustment)
2940 g_return_if_fail (GTK_IS_CONTAINER (container));
2942 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2945 g_object_ref (adjustment);
2947 g_object_set_qdata_full (G_OBJECT (container),
2954 * gtk_container_get_focus_vadjustment:
2955 * @container: a #GtkContainer
2957 * Retrieves the vertical focus adjustment for the container. See
2958 * gtk_container_set_focus_vadjustment().
2960 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2961 * none has been set.
2964 gtk_container_get_focus_vadjustment (GtkContainer *container)
2966 GtkAdjustment *vadjustment;
2968 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2970 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2976 * gtk_container_set_focus_hadjustment:
2977 * @container: a #GtkContainer
2978 * @adjustment: an adjustment which should be adjusted when the focus is
2979 * moved among the descendents of @container
2981 * Hooks up an adjustment to focus handling in a container, so when a child
2982 * of the container is focused, the adjustment is scrolled to show that
2983 * widget. This function sets the horizontal alignment.
2984 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2985 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2986 * the vertical adjustment.
2988 * The adjustments have to be in pixel units and in the same coordinate
2989 * system as the allocation for immediate children of the container.
2992 gtk_container_set_focus_hadjustment (GtkContainer *container,
2993 GtkAdjustment *adjustment)
2995 g_return_if_fail (GTK_IS_CONTAINER (container));
2997 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3000 g_object_ref (adjustment);
3002 g_object_set_qdata_full (G_OBJECT (container),
3009 * gtk_container_get_focus_hadjustment:
3010 * @container: a #GtkContainer
3012 * Retrieves the horizontal focus adjustment for the container. See
3013 * gtk_container_set_focus_hadjustment ().
3015 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3016 * none has been set.
3019 gtk_container_get_focus_hadjustment (GtkContainer *container)
3021 GtkAdjustment *hadjustment;
3023 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3025 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3032 gtk_container_show_all (GtkWidget *widget)
3034 g_return_if_fail (GTK_IS_CONTAINER (widget));
3036 gtk_container_foreach (GTK_CONTAINER (widget),
3037 (GtkCallback) gtk_widget_show_all,
3039 gtk_widget_show (widget);
3043 gtk_container_draw_child (GtkWidget *child,
3044 gpointer client_data)
3047 GtkWidget *container;
3049 } *data = client_data;
3051 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3057 gtk_container_draw (GtkWidget *widget,
3061 GtkWidget *container;
3065 data.container = widget;
3068 gtk_container_forall (GTK_CONTAINER (widget),
3069 gtk_container_draw_child,
3076 gtk_container_map_child (GtkWidget *child,
3077 gpointer client_data)
3079 if (gtk_widget_get_visible (child) &&
3080 gtk_widget_get_child_visible (child) &&
3081 !gtk_widget_get_mapped (child))
3082 gtk_widget_map (child);
3086 gtk_container_map (GtkWidget *widget)
3088 gtk_widget_set_mapped (widget, TRUE);
3090 gtk_container_forall (GTK_CONTAINER (widget),
3091 gtk_container_map_child,
3094 if (gtk_widget_get_has_window (widget))
3095 gdk_window_show (gtk_widget_get_window (widget));
3099 gtk_container_unmap (GtkWidget *widget)
3101 gtk_widget_set_mapped (widget, FALSE);
3103 if (gtk_widget_get_has_window (widget))
3104 gdk_window_hide (gtk_widget_get_window (widget));
3106 gtk_container_forall (GTK_CONTAINER (widget),
3107 (GtkCallback)gtk_widget_unmap,
3112 * gtk_container_propagate_draw:
3113 * @container: a #GtkContainer
3114 * @child: a child of @container
3115 * @cr: Cairo context as passed to the container. If you want to use @cr
3116 * in container's draw function, consider using cairo_save() and
3117 * cairo_restore() before calling this function.
3119 * When a container receives a call to the draw function, it must send
3120 * synthetic #GtkWidget::draw calls to all children that don't have their
3121 * own #GdkWindows. This function provides a convenient way of doing this.
3122 * A container, when it receives a call to its #GtkWidget::draw function,
3123 * calls gtk_container_propagate_draw() once for each child, passing in
3124 * the @cr the container received.
3126 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3127 * and deciding whether the draw needs to be sent to the child. It is a
3128 * convenient and optimized way of getting the same effect as calling
3129 * gtk_widget_draw() on the child directly.
3131 * In most cases, a container can simply either inherit the
3132 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3133 * and then chain to the ::draw implementation from #GtkContainer.
3136 gtk_container_propagate_draw (GtkContainer *container,
3140 GdkEventExpose *event;
3141 GtkAllocation allocation;
3142 GdkWindow *window, *w;
3145 g_return_if_fail (GTK_IS_CONTAINER (container));
3146 g_return_if_fail (GTK_IS_WIDGET (child));
3147 g_return_if_fail (cr != NULL);
3149 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3151 event = _gtk_cairo_get_event (cr);
3154 if (gtk_widget_get_has_window (child) ||
3155 gtk_widget_get_window (child) != event->window)
3161 /* translate coordinates. Ugly business, that. */
3162 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3164 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3174 window = gtk_widget_get_window (GTK_WIDGET (container));
3176 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3179 gdk_window_get_position (w, &wx, &wy);
3190 if (!gtk_widget_get_has_window (child))
3192 gtk_widget_get_allocation (child, &allocation);
3197 cairo_translate (cr, x, y);
3199 _gtk_widget_draw_internal (child, cr, TRUE);
3205 _gtk_container_get_need_resize (GtkContainer *container)
3207 return container->priv->need_resize;
3211 _gtk_container_set_need_resize (GtkContainer *container,
3212 gboolean need_resize)
3214 container->priv->need_resize = need_resize;
3218 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3220 return container->priv->reallocate_redraws;