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 void gtk_container_compute_expand (GtkWidget *widget,
307 gboolean *vexpand_p);
308 static gboolean gtk_container_focus (GtkWidget *widget,
309 GtkDirectionType direction);
310 static void gtk_container_real_set_focus_child (GtkContainer *container,
313 static gboolean gtk_container_focus_move (GtkContainer *container,
315 GtkDirectionType direction);
316 static void gtk_container_children_callback (GtkWidget *widget,
317 gpointer client_data);
318 static void gtk_container_show_all (GtkWidget *widget);
319 static void gtk_container_hide_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,
329 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
330 GtkAllocation *allocation);
332 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
336 static void gtk_container_buildable_init (GtkBuildableIface *iface);
337 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
341 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
344 const gchar *tagname,
345 GMarkupParser *parser,
347 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
350 const gchar *tagname,
354 /* --- variables --- */
355 static const gchar vadjustment_key[] = "gtk-vadjustment";
356 static guint vadjustment_key_id = 0;
357 static const gchar hadjustment_key[] = "gtk-hadjustment";
358 static guint hadjustment_key_id = 0;
359 static GSList *container_resize_queue = NULL;
360 static guint container_signals[LAST_SIGNAL] = { 0 };
361 static GtkWidgetClass *parent_class = NULL;
362 extern GParamSpecPool *_gtk_widget_child_property_pool;
363 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
364 static GtkBuildableIface *parent_buildable_iface;
367 /* --- functions --- */
369 gtk_container_get_type (void)
371 static GType container_type = 0;
375 const GTypeInfo container_info =
377 sizeof (GtkContainerClass),
378 (GBaseInitFunc) gtk_container_base_class_init,
379 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
380 (GClassInitFunc) gtk_container_class_init,
381 NULL /* class_finalize */,
382 NULL /* class_data */,
383 sizeof (GtkContainer),
385 (GInstanceInitFunc) gtk_container_init,
386 NULL, /* value_table */
389 const GInterfaceInfo buildable_info =
391 (GInterfaceInitFunc) gtk_container_buildable_init,
397 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
398 &container_info, G_TYPE_FLAG_ABSTRACT);
400 g_type_add_interface_static (container_type,
406 return container_type;
410 gtk_container_base_class_init (GtkContainerClass *class)
412 /* reset instance specifc class fields that don't get inherited */
413 class->set_child_property = NULL;
414 class->get_child_property = NULL;
418 gtk_container_base_class_finalize (GtkContainerClass *class)
422 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
423 for (node = list; node; node = node->next)
425 GParamSpec *pspec = node->data;
427 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
428 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
429 g_param_spec_unref (pspec);
435 gtk_container_class_init (GtkContainerClass *class)
437 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
438 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
440 parent_class = g_type_class_peek_parent (class);
442 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
443 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
445 gobject_class->set_property = gtk_container_set_property;
446 gobject_class->get_property = gtk_container_get_property;
448 widget_class->destroy = gtk_container_destroy;
449 widget_class->compute_expand = gtk_container_compute_expand;
450 widget_class->show_all = gtk_container_show_all;
451 widget_class->hide_all = gtk_container_hide_all;
452 widget_class->draw = gtk_container_draw;
453 widget_class->map = gtk_container_map;
454 widget_class->unmap = gtk_container_unmap;
455 widget_class->focus = gtk_container_focus;
457 widget_class->adjust_size_request = gtk_container_adjust_size_request;
458 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
460 class->add = gtk_container_add_unimplemented;
461 class->remove = gtk_container_remove_unimplemented;
462 class->check_resize = gtk_container_real_check_resize;
463 class->forall = NULL;
464 class->set_focus_child = gtk_container_real_set_focus_child;
465 class->child_type = NULL;
466 class->composite_name = gtk_container_child_default_composite_name;
468 g_object_class_install_property (gobject_class,
470 g_param_spec_enum ("resize-mode",
472 P_("Specify how resize events are handled"),
473 GTK_TYPE_RESIZE_MODE,
475 GTK_PARAM_READWRITE));
476 g_object_class_install_property (gobject_class,
478 g_param_spec_uint ("border-width",
480 P_("The width of the empty border outside the containers children"),
484 GTK_PARAM_READWRITE));
485 g_object_class_install_property (gobject_class,
487 g_param_spec_object ("child",
489 P_("Can be used to add a new child to the container"),
491 GTK_PARAM_WRITABLE));
492 container_signals[ADD] =
493 g_signal_new (I_("add"),
494 G_OBJECT_CLASS_TYPE (gobject_class),
496 G_STRUCT_OFFSET (GtkContainerClass, add),
498 _gtk_marshal_VOID__OBJECT,
501 container_signals[REMOVE] =
502 g_signal_new (I_("remove"),
503 G_OBJECT_CLASS_TYPE (gobject_class),
505 G_STRUCT_OFFSET (GtkContainerClass, remove),
507 _gtk_marshal_VOID__OBJECT,
510 container_signals[CHECK_RESIZE] =
511 g_signal_new (I_("check-resize"),
512 G_OBJECT_CLASS_TYPE (gobject_class),
514 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
516 _gtk_marshal_VOID__VOID,
518 container_signals[SET_FOCUS_CHILD] =
519 g_signal_new (I_("set-focus-child"),
520 G_OBJECT_CLASS_TYPE (gobject_class),
522 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
524 _gtk_marshal_VOID__OBJECT,
528 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
532 gtk_container_buildable_init (GtkBuildableIface *iface)
534 parent_buildable_iface = g_type_interface_peek_parent (iface);
535 iface->add_child = gtk_container_buildable_add_child;
536 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
537 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
541 gtk_container_buildable_add_child (GtkBuildable *buildable,
548 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
550 else if (GTK_IS_WIDGET (child) &&
551 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
553 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
556 g_warning ("Cannot add an object of type %s to a container of type %s",
557 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
561 gtk_container_buildable_set_child_property (GtkContainer *container,
568 GValue gvalue = { 0, };
569 GError *error = NULL;
571 pspec = gtk_container_class_find_child_property
572 (G_OBJECT_GET_CLASS (container), name);
575 g_warning ("%s does not have a property called %s",
576 g_type_name (G_OBJECT_TYPE (container)), name);
580 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
582 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
583 g_type_name (G_OBJECT_TYPE (container)),
586 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
588 g_error_free (error);
592 gtk_container_child_set_property (container, child, name, &gvalue);
593 g_value_unset (&gvalue);
598 GtkContainer *container;
600 gchar *child_prop_name;
602 gboolean translatable;
603 } PackingPropertiesData;
606 attributes_start_element (GMarkupParseContext *context,
607 const gchar *element_name,
609 const gchar **values,
613 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
616 if (strcmp (element_name, "property") == 0)
618 for (i = 0; names[i]; i++)
619 if (strcmp (names[i], "name") == 0)
620 parser_data->child_prop_name = g_strdup (values[i]);
621 else if (strcmp (names[i], "translatable") == 0)
623 if (!_gtk_builder_boolean_from_string (values[1],
624 &parser_data->translatable,
628 else if (strcmp (names[i], "comments") == 0)
629 ; /* for translators */
630 else if (strcmp (names[i], "context") == 0)
631 parser_data->context = g_strdup (values[1]);
633 g_warning ("Unsupported attribute for GtkContainer Child "
634 "property: %s\n", names[i]);
636 else if (strcmp (element_name, "packing") == 0)
639 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
643 attributes_text_element (GMarkupParseContext *context,
649 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
652 if (!parser_data->child_prop_name)
655 if (parser_data->translatable && text_len)
658 domain = gtk_builder_get_translation_domain (parser_data->builder);
660 value = _gtk_builder_parser_translate (domain,
661 parser_data->context,
666 value = g_strdup (text);
669 gtk_container_buildable_set_child_property (parser_data->container,
670 parser_data->builder,
672 parser_data->child_prop_name,
675 g_free (parser_data->child_prop_name);
676 g_free (parser_data->context);
678 parser_data->child_prop_name = NULL;
679 parser_data->context = NULL;
680 parser_data->translatable = FALSE;
683 static const GMarkupParser attributes_parser =
685 attributes_start_element,
687 attributes_text_element,
691 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
694 const gchar *tagname,
695 GMarkupParser *parser,
698 PackingPropertiesData *parser_data;
700 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
701 tagname, parser, data))
704 if (child && strcmp (tagname, "packing") == 0)
706 parser_data = g_slice_new0 (PackingPropertiesData);
707 parser_data->builder = builder;
708 parser_data->container = GTK_CONTAINER (buildable);
709 parser_data->child = GTK_WIDGET (child);
710 parser_data->child_prop_name = NULL;
712 *parser = attributes_parser;
721 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
724 const gchar *tagname,
727 if (strcmp (tagname, "packing") == 0)
729 g_slice_free (PackingPropertiesData, (gpointer)data);
734 if (parent_buildable_iface->custom_tag_end)
735 parent_buildable_iface->custom_tag_end (buildable, builder,
736 child, tagname, data);
741 * gtk_container_child_type:
742 * @container: a #GtkContainer
744 * Returns the type of the children supported by the container.
746 * Note that this may return %G_TYPE_NONE to indicate that no more
747 * children can be added, e.g. for a #GtkPaned which already has two
750 * Return value: a #GType.
753 gtk_container_child_type (GtkContainer *container)
756 GtkContainerClass *class;
758 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
760 class = GTK_CONTAINER_GET_CLASS (container);
761 if (class->child_type)
762 slot = class->child_type (container);
769 /* --- GtkContainer child property mechanism --- */
771 container_get_child_property (GtkContainer *container,
776 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
778 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
782 container_set_child_property (GtkContainer *container,
786 GObjectNotifyQueue *nqueue)
788 GValue tmp_value = { 0, };
789 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
791 /* provide a copy to work from, convert (if necessary) and validate */
792 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
793 if (!g_value_transform (value, &tmp_value))
794 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
796 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
797 G_VALUE_TYPE_NAME (value));
798 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
800 gchar *contents = g_strdup_value_contents (value);
802 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
804 G_VALUE_TYPE_NAME (value),
806 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
811 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
812 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
814 g_value_unset (&tmp_value);
818 * gtk_container_child_get_valist:
819 * @container: a #GtkContainer
820 * @child: a widget which is a child of @container
821 * @first_property_name: the name of the first property to get
822 * @var_args: return location for the first property, followed
823 * optionally by more name/return location pairs, followed by %NULL
825 * Gets the values of one or more child properties for @child and @container.
828 gtk_container_child_get_valist (GtkContainer *container,
830 const gchar *first_property_name,
835 g_return_if_fail (GTK_IS_CONTAINER (container));
836 g_return_if_fail (GTK_IS_WIDGET (child));
837 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
839 g_object_ref (container);
840 g_object_ref (child);
842 name = first_property_name;
845 GValue value = { 0, };
849 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
851 G_OBJECT_TYPE (container),
855 g_warning ("%s: container class `%s' has no child property named `%s'",
857 G_OBJECT_TYPE_NAME (container),
861 if (!(pspec->flags & G_PARAM_READABLE))
863 g_warning ("%s: child property `%s' of container class `%s' is not readable",
866 G_OBJECT_TYPE_NAME (container));
869 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
870 container_get_child_property (container, child, pspec, &value);
871 G_VALUE_LCOPY (&value, var_args, 0, &error);
874 g_warning ("%s: %s", G_STRLOC, error);
876 g_value_unset (&value);
879 g_value_unset (&value);
880 name = va_arg (var_args, gchar*);
883 g_object_unref (child);
884 g_object_unref (container);
888 * gtk_container_child_get_property:
889 * @container: a #GtkContainer
890 * @child: a widget which is a child of @container
891 * @property_name: the name of the property to get
892 * @value: a location to return the value
894 * Gets the value of a child property for @child and @container.
897 gtk_container_child_get_property (GtkContainer *container,
899 const gchar *property_name,
904 g_return_if_fail (GTK_IS_CONTAINER (container));
905 g_return_if_fail (GTK_IS_WIDGET (child));
906 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
907 g_return_if_fail (property_name != NULL);
908 g_return_if_fail (G_IS_VALUE (value));
910 g_object_ref (container);
911 g_object_ref (child);
912 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
913 G_OBJECT_TYPE (container), TRUE);
915 g_warning ("%s: container class `%s' has no child property named `%s'",
917 G_OBJECT_TYPE_NAME (container),
919 else if (!(pspec->flags & G_PARAM_READABLE))
920 g_warning ("%s: child property `%s' of container class `%s' is not readable",
923 G_OBJECT_TYPE_NAME (container));
926 GValue *prop_value, tmp_value = { 0, };
928 /* auto-conversion of the callers value type
930 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
932 g_value_reset (value);
935 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
937 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
939 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
940 G_VALUE_TYPE_NAME (value));
941 g_object_unref (child);
942 g_object_unref (container);
947 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
948 prop_value = &tmp_value;
950 container_get_child_property (container, child, pspec, prop_value);
951 if (prop_value != value)
953 g_value_transform (prop_value, value);
954 g_value_unset (&tmp_value);
957 g_object_unref (child);
958 g_object_unref (container);
962 * gtk_container_child_set_valist:
963 * @container: a #GtkContainer
964 * @child: a widget which is a child of @container
965 * @first_property_name: the name of the first property to set
966 * @var_args: a %NULL-terminated list of property names and values, starting
967 * with @first_prop_name
969 * Sets one or more child properties for @child and @container.
972 gtk_container_child_set_valist (GtkContainer *container,
974 const gchar *first_property_name,
977 GObjectNotifyQueue *nqueue;
980 g_return_if_fail (GTK_IS_CONTAINER (container));
981 g_return_if_fail (GTK_IS_WIDGET (child));
982 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
984 g_object_ref (container);
985 g_object_ref (child);
987 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
988 name = first_property_name;
991 GValue value = { 0, };
993 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
995 G_OBJECT_TYPE (container),
999 g_warning ("%s: container class `%s' has no child property named `%s'",
1001 G_OBJECT_TYPE_NAME (container),
1005 if (!(pspec->flags & G_PARAM_WRITABLE))
1007 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1010 G_OBJECT_TYPE_NAME (container));
1013 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1014 G_VALUE_COLLECT (&value, var_args, 0, &error);
1017 g_warning ("%s: %s", G_STRLOC, error);
1020 /* we purposely leak the value here, it might not be
1021 * in a sane state if an error condition occoured
1025 container_set_child_property (container, child, pspec, &value, nqueue);
1026 g_value_unset (&value);
1027 name = va_arg (var_args, gchar*);
1029 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1031 g_object_unref (container);
1032 g_object_unref (child);
1036 * gtk_container_child_set_property:
1037 * @container: a #GtkContainer
1038 * @child: a widget which is a child of @container
1039 * @property_name: the name of the property to set
1040 * @value: the value to set the property to
1042 * Sets a child property for @child and @container.
1045 gtk_container_child_set_property (GtkContainer *container,
1047 const gchar *property_name,
1048 const GValue *value)
1050 GObjectNotifyQueue *nqueue;
1053 g_return_if_fail (GTK_IS_CONTAINER (container));
1054 g_return_if_fail (GTK_IS_WIDGET (child));
1055 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1056 g_return_if_fail (property_name != NULL);
1057 g_return_if_fail (G_IS_VALUE (value));
1059 g_object_ref (container);
1060 g_object_ref (child);
1062 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1063 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1064 G_OBJECT_TYPE (container), TRUE);
1066 g_warning ("%s: container class `%s' has no child property named `%s'",
1068 G_OBJECT_TYPE_NAME (container),
1070 else if (!(pspec->flags & G_PARAM_WRITABLE))
1071 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1074 G_OBJECT_TYPE_NAME (container));
1077 container_set_child_property (container, child, pspec, value, nqueue);
1079 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1080 g_object_unref (container);
1081 g_object_unref (child);
1085 * gtk_container_add_with_properties:
1086 * @container: a #GtkContainer
1087 * @widget: a widget to be placed inside @container
1088 * @first_prop_name: the name of the first child property to set
1089 * @Varargs: a %NULL-terminated list of property names and values, starting
1090 * with @first_prop_name
1092 * Adds @widget to @container, setting child properties at the same time.
1093 * See gtk_container_add() and gtk_container_child_set() for more details.
1096 gtk_container_add_with_properties (GtkContainer *container,
1098 const gchar *first_prop_name,
1101 g_return_if_fail (GTK_IS_CONTAINER (container));
1102 g_return_if_fail (GTK_IS_WIDGET (widget));
1103 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1105 g_object_ref (container);
1106 g_object_ref (widget);
1107 gtk_widget_freeze_child_notify (widget);
1109 g_signal_emit (container, container_signals[ADD], 0, widget);
1110 if (gtk_widget_get_parent (widget))
1114 va_start (var_args, first_prop_name);
1115 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1119 gtk_widget_thaw_child_notify (widget);
1120 g_object_unref (widget);
1121 g_object_unref (container);
1125 * gtk_container_child_set:
1126 * @container: a #GtkContainer
1127 * @child: a widget which is a child of @container
1128 * @first_prop_name: the name of the first property to set
1129 * @Varargs: a %NULL-terminated list of property names and values, starting
1130 * with @first_prop_name
1132 * Sets one or more child properties for @child and @container.
1135 gtk_container_child_set (GtkContainer *container,
1137 const gchar *first_prop_name,
1142 g_return_if_fail (GTK_IS_CONTAINER (container));
1143 g_return_if_fail (GTK_IS_WIDGET (child));
1144 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1146 va_start (var_args, first_prop_name);
1147 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1152 * gtk_container_child_get:
1153 * @container: a #GtkContainer
1154 * @child: a widget which is a child of @container
1155 * @first_prop_name: the name of the first property to get
1156 * @Varargs: return location for the first property, followed
1157 * optionally by more name/return location pairs, followed by %NULL
1159 * Gets the values of one or more child properties for @child and @container.
1162 gtk_container_child_get (GtkContainer *container,
1164 const gchar *first_prop_name,
1169 g_return_if_fail (GTK_IS_CONTAINER (container));
1170 g_return_if_fail (GTK_IS_WIDGET (child));
1171 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container));
1173 va_start (var_args, first_prop_name);
1174 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1179 * gtk_container_class_install_child_property:
1180 * @cclass: a #GtkContainerClass
1181 * @property_id: the id for the property
1182 * @pspec: the #GParamSpec for the property
1184 * Installs a child property on a container class.
1187 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1191 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1192 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1193 if (pspec->flags & G_PARAM_WRITABLE)
1194 g_return_if_fail (cclass->set_child_property != NULL);
1195 if (pspec->flags & G_PARAM_READABLE)
1196 g_return_if_fail (cclass->get_child_property != NULL);
1197 g_return_if_fail (property_id > 0);
1198 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1199 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1200 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1202 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1204 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1205 G_OBJECT_CLASS_NAME (cclass),
1209 g_param_spec_ref (pspec);
1210 g_param_spec_sink (pspec);
1211 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1212 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1216 * gtk_container_class_find_child_property:
1217 * @cclass: a #GtkContainerClass
1218 * @property_name: the name of the child property to find
1219 * @returns: (allow-none): the #GParamSpec of the child property or %NULL if @class has no
1220 * child property with that name.
1222 * Finds a child property of a container class by name.
1225 gtk_container_class_find_child_property (GObjectClass *cclass,
1226 const gchar *property_name)
1228 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1229 g_return_val_if_fail (property_name != NULL, NULL);
1231 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1233 G_OBJECT_CLASS_TYPE (cclass),
1238 * gtk_container_class_list_child_properties:
1239 * @cclass: a #GtkContainerClass
1240 * @n_properties: location to return the number of child properties found
1241 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
1242 * The array must be freed with g_free().
1244 * Returns all child properties of a container class.
1247 gtk_container_class_list_child_properties (GObjectClass *cclass,
1248 guint *n_properties)
1250 GParamSpec **pspecs;
1253 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1255 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1256 G_OBJECT_CLASS_TYPE (cclass),
1265 gtk_container_add_unimplemented (GtkContainer *container,
1268 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1272 gtk_container_remove_unimplemented (GtkContainer *container,
1275 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1279 gtk_container_init (GtkContainer *container)
1281 GtkContainerPrivate *priv;
1283 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1285 GtkContainerPrivate);
1286 priv = container->priv;
1288 priv->focus_child = NULL;
1289 priv->border_width = 0;
1290 priv->need_resize = FALSE;
1291 priv->resize_mode = GTK_RESIZE_PARENT;
1292 priv->reallocate_redraws = FALSE;
1296 gtk_container_destroy (GtkWidget *widget)
1298 GtkContainer *container = GTK_CONTAINER (widget);
1299 GtkContainerPrivate *priv = container->priv;
1301 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1302 _gtk_container_dequeue_resize_handler (container);
1304 if (priv->focus_child)
1306 g_object_unref (priv->focus_child);
1307 priv->focus_child = NULL;
1310 /* do this before walking child widgets, to avoid
1311 * removing children from focus chain one by one.
1313 if (priv->has_focus_chain)
1314 gtk_container_unset_focus_chain (container);
1316 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1318 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1322 gtk_container_set_property (GObject *object,
1324 const GValue *value,
1327 GtkContainer *container = GTK_CONTAINER (object);
1331 case PROP_BORDER_WIDTH:
1332 gtk_container_set_border_width (container, g_value_get_uint (value));
1334 case PROP_RESIZE_MODE:
1335 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1338 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1347 gtk_container_get_property (GObject *object,
1352 GtkContainer *container = GTK_CONTAINER (object);
1353 GtkContainerPrivate *priv = container->priv;
1357 case PROP_BORDER_WIDTH:
1358 g_value_set_uint (value, priv->border_width);
1360 case PROP_RESIZE_MODE:
1361 g_value_set_enum (value, priv->resize_mode);
1364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1370 * gtk_container_set_border_width:
1371 * @container: a #GtkContainer
1372 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1373 * the container. Valid values are in the range 0-65535 pixels.
1375 * Sets the border width of the container.
1377 * The border width of a container is the amount of space to leave
1378 * around the outside of the container. The only exception to this is
1379 * #GtkWindow; because toplevel windows can't leave space outside,
1380 * they leave the space inside. The border is added on all sides of
1381 * the container. To add space to only one side, one approach is to
1382 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1383 * to give it a size, and place it on the side of the container as
1387 gtk_container_set_border_width (GtkContainer *container,
1390 GtkContainerPrivate *priv;
1392 g_return_if_fail (GTK_IS_CONTAINER (container));
1394 priv = container->priv;
1396 if (priv->border_width != border_width)
1398 priv->border_width = border_width;
1399 g_object_notify (G_OBJECT (container), "border-width");
1401 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1402 gtk_widget_queue_resize (GTK_WIDGET (container));
1407 * gtk_container_get_border_width:
1408 * @container: a #GtkContainer
1410 * Retrieves the border width of the container. See
1411 * gtk_container_set_border_width().
1413 * Return value: the current border width
1416 gtk_container_get_border_width (GtkContainer *container)
1418 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1420 return container->priv->border_width;
1424 * gtk_container_add:
1425 * @container: a #GtkContainer
1426 * @widget: a widget to be placed inside @container
1428 * Adds @widget to @container. Typically used for simple containers
1429 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1430 * layout containers such as #GtkBox or #GtkTable, this function will
1431 * pick default packing parameters that may not be correct. So
1432 * consider functions such as gtk_box_pack_start() and
1433 * gtk_table_attach() as an alternative to gtk_container_add() in
1434 * those cases. A widget may be added to only one container at a time;
1435 * you can't place the same widget inside two different containers.
1438 gtk_container_add (GtkContainer *container,
1443 g_return_if_fail (GTK_IS_CONTAINER (container));
1444 g_return_if_fail (GTK_IS_WIDGET (widget));
1446 parent = gtk_widget_get_parent (widget);
1450 g_warning ("Attempting to add a widget with type %s to a container of "
1451 "type %s, but the widget is already inside a container of type %s, "
1452 "please use gtk_widget_reparent()" ,
1453 g_type_name (G_OBJECT_TYPE (widget)),
1454 g_type_name (G_OBJECT_TYPE (container)),
1455 g_type_name (G_OBJECT_TYPE (parent)));
1459 g_signal_emit (container, container_signals[ADD], 0, widget);
1463 * gtk_container_remove:
1464 * @container: a #GtkContainer
1465 * @widget: a current child of @container
1467 * Removes @widget from @container. @widget must be inside @container.
1468 * Note that @container will own a reference to @widget, and that this
1469 * may be the last reference held; so removing a widget from its
1470 * container can destroy that widget. If you want to use @widget
1471 * again, you need to add a reference to it while it's not inside
1472 * a container, using g_object_ref(). If you don't want to use @widget
1473 * again it's usually more efficient to simply destroy it directly
1474 * using gtk_widget_destroy() since this will remove it from the
1475 * container and help break any circular reference count cycles.
1478 gtk_container_remove (GtkContainer *container,
1481 g_return_if_fail (GTK_IS_CONTAINER (container));
1482 g_return_if_fail (GTK_IS_WIDGET (widget));
1483 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1485 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1489 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1491 g_return_if_fail (GTK_IS_CONTAINER (container));
1492 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1494 container_resize_queue = g_slist_remove (container_resize_queue, container);
1495 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1499 * gtk_container_set_resize_mode:
1500 * @container: a #GtkContainer
1501 * @resize_mode: the new resize mode
1503 * Sets the resize mode for the container.
1505 * The resize mode of a container determines whether a resize request
1506 * will be passed to the container's parent, queued for later execution
1507 * or executed immediately.
1510 gtk_container_set_resize_mode (GtkContainer *container,
1511 GtkResizeMode resize_mode)
1513 GtkContainerPrivate *priv;
1515 g_return_if_fail (GTK_IS_CONTAINER (container));
1516 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1518 priv = container->priv;
1520 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1521 resize_mode == GTK_RESIZE_PARENT)
1523 resize_mode = GTK_RESIZE_QUEUE;
1526 if (priv->resize_mode != resize_mode)
1528 priv->resize_mode = resize_mode;
1530 gtk_widget_queue_resize (GTK_WIDGET (container));
1531 g_object_notify (G_OBJECT (container), "resize-mode");
1536 * gtk_container_get_resize_mode:
1537 * @container: a #GtkContainer
1539 * Returns the resize mode for the container. See
1540 * gtk_container_set_resize_mode ().
1542 * Return value: the current resize mode
1545 gtk_container_get_resize_mode (GtkContainer *container)
1547 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1549 return container->priv->resize_mode;
1553 * gtk_container_set_reallocate_redraws:
1554 * @container: a #GtkContainer
1555 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1557 * Sets the @reallocate_redraws flag of the container to the given value.
1559 * Containers requesting reallocation redraws get automatically
1560 * redrawn if any of their children changed allocation.
1563 gtk_container_set_reallocate_redraws (GtkContainer *container,
1564 gboolean needs_redraws)
1566 g_return_if_fail (GTK_IS_CONTAINER (container));
1568 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1571 static GtkContainer*
1572 gtk_container_get_resize_container (GtkContainer *container)
1575 GtkWidget *widget = GTK_WIDGET (container);
1577 while ((parent = gtk_widget_get_parent (widget)))
1580 if (GTK_IS_RESIZE_CONTAINER (widget))
1584 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1588 gtk_container_idle_sizer (gpointer data)
1590 /* we may be invoked with a container_resize_queue of NULL, because
1591 * queue_resize could have been adding an extra idle function while
1592 * the queue still got processed. we better just ignore such case
1593 * than trying to explicitely work around them with some extra flags,
1594 * since it doesn't cause any actual harm.
1596 while (container_resize_queue)
1601 slist = container_resize_queue;
1602 container_resize_queue = slist->next;
1603 widget = slist->data;
1604 g_slist_free_1 (slist);
1606 _gtk_widget_set_resize_pending (widget, FALSE);
1607 gtk_container_check_resize (GTK_CONTAINER (widget));
1610 gdk_window_process_all_updates ();
1616 _gtk_container_queue_resize_internal (GtkContainer *container,
1617 gboolean invalidate_only)
1619 GtkContainerPrivate *priv;
1620 GtkContainer *resize_container;
1624 g_return_if_fail (GTK_IS_CONTAINER (container));
1626 priv = container->priv;
1627 widget = GTK_WIDGET (container);
1629 resize_container = gtk_container_get_resize_container (container);
1633 _gtk_widget_set_alloc_needed (widget, TRUE);
1634 _gtk_widget_set_width_request_needed (widget, TRUE);
1635 _gtk_widget_set_height_request_needed (widget, TRUE);
1637 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1638 !(parent = gtk_widget_get_parent (widget)))
1644 if (resize_container && !invalidate_only)
1646 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1647 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1648 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1650 switch (resize_container->priv->resize_mode)
1652 case GTK_RESIZE_QUEUE:
1653 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1655 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1656 if (container_resize_queue == NULL)
1657 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1658 gtk_container_idle_sizer,
1660 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1664 case GTK_RESIZE_IMMEDIATE:
1665 gtk_container_check_resize (resize_container);
1668 case GTK_RESIZE_PARENT:
1669 g_assert_not_reached ();
1675 /* we need to let hidden resize containers know that something
1676 * changed while they where hidden (currently only evaluated by
1679 resize_container->priv->need_resize = TRUE;
1685 * _gtk_container_queue_resize:
1686 * @container: a #GtkContainer
1688 * Determines the "resize container" in the hierarchy above this container
1689 * (typically the toplevel, but other containers can be set as resize
1690 * containers with gtk_container_set_resize_mode()), marks the container
1691 * and all parents up to and including the resize container as needing
1692 * to have sizes recompted, and if necessary adds the resize container
1693 * to the queue of containers that will be resized out at idle.
1696 _gtk_container_queue_resize (GtkContainer *container)
1698 _gtk_container_queue_resize_internal (container, FALSE);
1702 * _gtk_container_resize_invalidate:
1703 * @container: a #GtkContainer
1705 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1706 * actually queue the resize container for resize.
1709 _gtk_container_resize_invalidate (GtkContainer *container)
1711 _gtk_container_queue_resize_internal (container, TRUE);
1715 gtk_container_check_resize (GtkContainer *container)
1717 g_return_if_fail (GTK_IS_CONTAINER (container));
1719 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1723 gtk_container_real_check_resize (GtkContainer *container)
1725 GtkWidget *widget = GTK_WIDGET (container);
1726 GtkAllocation allocation;
1727 GtkRequisition requisition;
1729 gtk_widget_get_preferred_size (widget,
1730 &requisition, NULL);
1731 gtk_widget_get_allocation (widget, &allocation);
1733 if (requisition.width > allocation.width ||
1734 requisition.height > allocation.height)
1736 if (GTK_IS_RESIZE_CONTAINER (container))
1738 gtk_widget_size_allocate (widget, &allocation);
1739 gtk_widget_set_allocation (widget, &allocation);
1742 gtk_widget_queue_resize (widget);
1746 gtk_container_resize_children (container);
1750 /* The container hasn't changed size but one of its children
1751 * queued a resize request. Which means that the allocation
1752 * is not sufficient for the requisition of some child.
1753 * We've already performed a size request at this point,
1754 * so we simply need to reallocate and let the allocation
1755 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1758 gtk_container_resize_children (GtkContainer *container)
1760 GtkAllocation allocation;
1763 /* resizing invariants:
1764 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1765 * containers that have an idle sizer pending must be flagged with
1768 g_return_if_fail (GTK_IS_CONTAINER (container));
1770 widget = GTK_WIDGET (container);
1771 gtk_widget_get_allocation (widget, &allocation);
1773 gtk_widget_size_allocate (widget, &allocation);
1774 gtk_widget_set_allocation (widget, &allocation);
1778 gtk_container_adjust_size_request (GtkWidget *widget,
1779 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, for_size,
1802 minimum_size, natural_size);
1806 gtk_container_adjust_size_allocation (GtkWidget *widget,
1807 GtkAllocation *allocation)
1809 GtkContainer *container;
1812 container = GTK_CONTAINER (widget);
1814 parent_class->adjust_size_allocation (widget, allocation);
1816 if (!GTK_CONTAINER_GET_CLASS (widget)->handle_border_width)
1819 border_width = container->priv->border_width;
1821 allocation->width -= border_width * 2;
1822 allocation->height -= border_width * 2;
1824 /* If we get a pathological too-small allocation to hold
1825 * even the border width, leave all allocation to the actual
1826 * widget, and leave x,y unchanged. (GtkWidget's min size is
1827 * 1x1 if you're wondering why <1 and not <0)
1829 * As long as we have space, set x,y properly.
1832 if (allocation->width < 1)
1834 allocation->width += border_width * 2;
1838 allocation->x += border_width;
1841 if (allocation->height < 1)
1843 allocation->height += border_width * 2;
1847 allocation->y += border_width;
1852 * gtk_container_class_handle_border_width:
1853 * @klass: the class struct of a #GtkContainer subclass
1855 * Modifies a subclass of #GtkContainerClass to automatically add and
1856 * remove the border-width setting on GtkContainer. This allows the
1857 * subclass to ignore the border width in its size request and
1858 * allocate methods. The intent is for a subclass to invoke this
1859 * in its class_init function.
1861 * gtk_container_class_handle_border_width() is necessary because it
1862 * would break API too badly to make this behavior the default. So
1863 * subclasses must "opt in" to the parent class handling border_width
1867 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1869 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1871 klass->handle_border_width = TRUE;
1875 * gtk_container_forall:
1876 * @container: a #GtkContainer
1877 * @callback: a callback
1878 * @callback_data: callback user data
1880 * Invokes @callback on each child of @container, including children
1881 * that are considered "internal" (implementation details of the
1882 * container). "Internal" children generally weren't added by the user
1883 * of the container, but were added by the container implementation
1884 * itself. Most applications should use gtk_container_foreach(),
1885 * rather than gtk_container_forall().
1888 gtk_container_forall (GtkContainer *container,
1889 GtkCallback callback,
1890 gpointer callback_data)
1892 GtkContainerClass *class;
1894 g_return_if_fail (GTK_IS_CONTAINER (container));
1895 g_return_if_fail (callback != NULL);
1897 class = GTK_CONTAINER_GET_CLASS (container);
1900 class->forall (container, TRUE, callback, callback_data);
1904 * gtk_container_foreach:
1905 * @container: a #GtkContainer
1906 * @callback: (scope call): a callback
1907 * @callback_data: callback user data
1909 * Invokes @callback on each non-internal child of @container. See
1910 * gtk_container_forall() for details on what constitutes an
1911 * "internal" child. Most applications should use
1912 * gtk_container_foreach(), rather than gtk_container_forall().
1915 gtk_container_foreach (GtkContainer *container,
1916 GtkCallback callback,
1917 gpointer callback_data)
1919 GtkContainerClass *class;
1921 g_return_if_fail (GTK_IS_CONTAINER (container));
1922 g_return_if_fail (callback != NULL);
1924 class = GTK_CONTAINER_GET_CLASS (container);
1927 class->forall (container, FALSE, callback, callback_data);
1931 * gtk_container_set_focus_child:
1932 * @container: a #GtkContainer
1933 * @child: (allow-none): a #GtkWidget, or %NULL
1935 * Sets, or unsets if @child is %NULL, the focused child of @container.
1937 * This function emits the GtkContainer::set_focus_child signal of
1938 * @container. Implementations of #GtkContainer can override the
1939 * default behaviour by overriding the class closure of this signal.
1941 * This is function is mostly meant to be used by widgets. Applications can use
1942 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
1945 gtk_container_set_focus_child (GtkContainer *container,
1948 g_return_if_fail (GTK_IS_CONTAINER (container));
1950 g_return_if_fail (GTK_IS_WIDGET (child));
1952 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
1956 * gtk_container_get_focus_child:
1957 * @container: a #GtkContainer
1959 * Returns the current focus child widget inside @container. This is not the
1960 * currently focused widget. That can be obtained by calling
1961 * gtk_window_get_focus().
1963 * Returns: The child widget which will recieve the focus inside @container when
1964 * the @conatiner is focussed, or %NULL if none is set.
1969 gtk_container_get_focus_child (GtkContainer *container)
1971 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1973 return container->priv->focus_child;
1977 * gtk_container_get_children:
1978 * @container: a #GtkContainer
1980 * Returns the container's non-internal children. See
1981 * gtk_container_forall() for details on what constitutes an "internal" child.
1983 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
1986 gtk_container_get_children (GtkContainer *container)
1988 GList *children = NULL;
1990 gtk_container_foreach (container,
1991 gtk_container_children_callback,
1994 return g_list_reverse (children);
1998 gtk_container_child_position_callback (GtkWidget *widget,
1999 gpointer client_data)
2005 } *data = client_data;
2008 if (data->child == widget)
2009 data->index = data->i;
2013 gtk_container_child_default_composite_name (GtkContainer *container,
2023 /* fallback implementation */
2027 gtk_container_forall (container,
2028 gtk_container_child_position_callback,
2031 name = g_strdup_printf ("%s-%u",
2032 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2039 _gtk_container_child_composite_name (GtkContainer *container,
2042 gboolean composite_child;
2044 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2045 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2046 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2048 g_object_get (child, "composite-child", &composite_child, NULL);
2049 if (composite_child)
2051 static GQuark quark_composite_name = 0;
2054 if (!quark_composite_name)
2055 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2057 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2060 GtkContainerClass *class;
2062 class = GTK_CONTAINER_GET_CLASS (container);
2063 if (class->composite_name)
2064 name = class->composite_name (container, child);
2067 name = g_strdup (name);
2078 } ComputeExpandData;
2081 gtk_container_compute_expand_callback (GtkWidget *widget,
2082 gpointer client_data)
2084 ComputeExpandData *data = client_data;
2086 /* note that we don't get_expand on the child if we already know we
2087 * have to expand, so we only recurse into children until we find
2088 * one that expands and then we basically don't do any more
2089 * work. This means that we can leave some children in a
2090 * need_compute_expand state, which is fine, as long as GtkWidget
2091 * doesn't rely on an invariant that "if a child has
2092 * need_compute_expand, its parents also do"
2094 * gtk_widget_compute_expand() always returns FALSE if the
2095 * child is !visible so that's taken care of.
2097 data->hexpand = data->hexpand ||
2098 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2100 data->vexpand = data->vexpand ||
2101 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2105 gtk_container_compute_expand (GtkWidget *widget,
2106 gboolean *hexpand_p,
2107 gboolean *vexpand_p)
2109 ComputeExpandData data;
2111 data.hexpand = FALSE;
2112 data.vexpand = FALSE;
2114 gtk_container_forall (GTK_CONTAINER (widget),
2115 gtk_container_compute_expand_callback,
2118 *hexpand_p = data.hexpand;
2119 *vexpand_p = data.vexpand;
2123 gtk_container_real_set_focus_child (GtkContainer *container,
2126 GtkContainerPrivate *priv;
2128 g_return_if_fail (GTK_IS_CONTAINER (container));
2129 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2131 priv = container->priv;
2133 if (child != priv->focus_child)
2135 if (priv->focus_child)
2136 g_object_unref (priv->focus_child);
2137 priv->focus_child = child;
2138 if (priv->focus_child)
2139 g_object_ref (priv->focus_child);
2143 /* check for h/v adjustments
2145 if (priv->focus_child)
2147 GtkAdjustment *hadj;
2148 GtkAdjustment *vadj;
2149 GtkAllocation allocation;
2150 GtkWidget *focus_child;
2153 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2154 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2158 focus_child = priv->focus_child;
2159 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2161 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2164 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2167 gtk_widget_get_allocation (priv->focus_child, &allocation);
2171 gtk_widget_get_allocation (focus_child, &allocation);
2174 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2177 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2183 get_focus_chain (GtkContainer *container)
2185 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2188 /* same as gtk_container_get_children, except it includes internals
2191 gtk_container_get_all_children (GtkContainer *container)
2193 GList *children = NULL;
2195 gtk_container_forall (container,
2196 gtk_container_children_callback,
2203 gtk_container_focus (GtkWidget *widget,
2204 GtkDirectionType direction)
2207 GList *sorted_children;
2209 GtkContainer *container;
2210 GtkContainerPrivate *priv;
2212 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2214 container = GTK_CONTAINER (widget);
2215 priv = container->priv;
2219 if (gtk_widget_get_can_focus (widget))
2221 if (!gtk_widget_has_focus (widget))
2223 gtk_widget_grab_focus (widget);
2229 /* Get a list of the containers children, allowing focus
2230 * chain to override.
2232 if (priv->has_focus_chain)
2233 children = g_list_copy (get_focus_chain (container));
2235 children = gtk_container_get_all_children (container);
2237 if (priv->has_focus_chain &&
2238 (direction == GTK_DIR_TAB_FORWARD ||
2239 direction == GTK_DIR_TAB_BACKWARD))
2241 sorted_children = g_list_copy (children);
2243 if (direction == GTK_DIR_TAB_BACKWARD)
2244 sorted_children = g_list_reverse (sorted_children);
2247 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2249 return_val = gtk_container_focus_move (container, sorted_children, direction);
2251 g_list_free (sorted_children);
2252 g_list_free (children);
2259 tab_compare (gconstpointer a,
2263 GtkAllocation child1_allocation, child2_allocation;
2264 const GtkWidget *child1 = a;
2265 const GtkWidget *child2 = b;
2266 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2268 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2269 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2271 gint y1 = child1_allocation.y + child1_allocation.height / 2;
2272 gint y2 = child2_allocation.y + child2_allocation.height / 2;
2276 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2277 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2279 if (text_direction == GTK_TEXT_DIR_RTL)
2280 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2282 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2285 return (y1 < y2) ? -1 : 1;
2289 gtk_container_focus_sort_tab (GtkContainer *container,
2291 GtkDirectionType direction,
2292 GtkWidget *old_focus)
2294 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2295 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2297 /* if we are going backwards then reverse the order
2300 if (direction == GTK_DIR_TAB_BACKWARD)
2301 children = g_list_reverse (children);
2306 /* Get coordinates of @widget's allocation with respect to
2307 * allocation of @container.
2310 get_allocation_coords (GtkContainer *container,
2312 GdkRectangle *allocation)
2314 gtk_widget_set_allocation (widget, allocation);
2316 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2317 0, 0, &allocation->x, &allocation->y);
2320 /* Look for a child in @children that is intermediate between
2321 * the focus widget and container. This widget, if it exists,
2322 * acts as the starting widget for focus navigation.
2325 find_old_focus (GtkContainer *container,
2328 GList *tmp_list = children;
2331 GtkWidget *child = tmp_list->data;
2332 GtkWidget *widget = child;
2334 while (widget && widget != (GtkWidget *)container)
2338 parent = gtk_widget_get_parent (widget);
2340 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2349 tmp_list = tmp_list->next;
2356 old_focus_coords (GtkContainer *container,
2357 GdkRectangle *old_focus_rect)
2359 GtkWidget *widget = GTK_WIDGET (container);
2360 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2361 GtkWidget *old_focus;
2363 if (GTK_IS_WINDOW (toplevel))
2365 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2367 return get_allocation_coords (container, old_focus, old_focus_rect);
2373 typedef struct _CompareInfo CompareInfo;
2377 GtkContainer *container;
2384 up_down_compare (gconstpointer a,
2388 GdkRectangle allocation1;
2389 GdkRectangle allocation2;
2390 CompareInfo *compare = data;
2393 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2394 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2396 y1 = allocation1.y + allocation1.height / 2;
2397 y2 = allocation2.y + allocation2.height / 2;
2401 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2402 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2404 if (compare->reverse)
2405 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2407 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2410 return (y1 < y2) ? -1 : 1;
2414 gtk_container_focus_sort_up_down (GtkContainer *container,
2416 GtkDirectionType direction,
2417 GtkWidget *old_focus)
2419 CompareInfo compare;
2421 GdkRectangle old_allocation;
2423 compare.container = container;
2424 compare.reverse = (direction == GTK_DIR_UP);
2427 old_focus = find_old_focus (container, children);
2429 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2435 /* Delete widgets from list that don't match minimum criteria */
2437 compare_x1 = old_allocation.x;
2438 compare_x2 = old_allocation.x + old_allocation.width;
2440 if (direction == GTK_DIR_UP)
2441 compare_y = old_allocation.y;
2443 compare_y = old_allocation.y + old_allocation.height;
2445 tmp_list = children;
2448 GtkWidget *child = tmp_list->data;
2449 GList *next = tmp_list->next;
2450 gint child_x1, child_x2;
2451 GdkRectangle child_allocation;
2453 if (child != old_focus)
2455 if (get_allocation_coords (container, child, &child_allocation))
2457 child_x1 = child_allocation.x;
2458 child_x2 = child_allocation.x + child_allocation.width;
2460 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2461 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2462 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2464 children = g_list_delete_link (children, tmp_list);
2468 children = g_list_delete_link (children, tmp_list);
2474 compare.x = (compare_x1 + compare_x2) / 2;
2475 compare.y = old_allocation.y + old_allocation.height / 2;
2479 /* No old focus widget, need to figure out starting x,y some other way
2481 GtkAllocation allocation;
2482 GtkWidget *widget = GTK_WIDGET (container);
2483 GdkRectangle old_focus_rect;
2485 gtk_widget_get_allocation (widget, &allocation);
2487 if (old_focus_coords (container, &old_focus_rect))
2489 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2493 if (!gtk_widget_get_has_window (widget))
2494 compare.x = allocation.x + allocation.width / 2;
2496 compare.x = allocation.width / 2;
2499 if (!gtk_widget_get_has_window (widget))
2500 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2502 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2505 children = g_list_sort_with_data (children, up_down_compare, &compare);
2507 if (compare.reverse)
2508 children = g_list_reverse (children);
2514 left_right_compare (gconstpointer a,
2518 GdkRectangle allocation1;
2519 GdkRectangle allocation2;
2520 CompareInfo *compare = data;
2523 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2524 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2526 x1 = allocation1.x + allocation1.width / 2;
2527 x2 = allocation2.x + allocation2.width / 2;
2531 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2532 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2534 if (compare->reverse)
2535 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2537 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2540 return (x1 < x2) ? -1 : 1;
2544 gtk_container_focus_sort_left_right (GtkContainer *container,
2546 GtkDirectionType direction,
2547 GtkWidget *old_focus)
2549 CompareInfo compare;
2551 GdkRectangle old_allocation;
2553 compare.container = container;
2554 compare.reverse = (direction == GTK_DIR_LEFT);
2557 old_focus = find_old_focus (container, children);
2559 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2565 /* Delete widgets from list that don't match minimum criteria */
2567 compare_y1 = old_allocation.y;
2568 compare_y2 = old_allocation.y + old_allocation.height;
2570 if (direction == GTK_DIR_LEFT)
2571 compare_x = old_allocation.x;
2573 compare_x = old_allocation.x + old_allocation.width;
2575 tmp_list = children;
2578 GtkWidget *child = tmp_list->data;
2579 GList *next = tmp_list->next;
2580 gint child_y1, child_y2;
2581 GdkRectangle child_allocation;
2583 if (child != old_focus)
2585 if (get_allocation_coords (container, child, &child_allocation))
2587 child_y1 = child_allocation.y;
2588 child_y2 = child_allocation.y + child_allocation.height;
2590 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2591 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2592 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2594 children = g_list_delete_link (children, tmp_list);
2598 children = g_list_delete_link (children, tmp_list);
2604 compare.y = (compare_y1 + compare_y2) / 2;
2605 compare.x = old_allocation.x + old_allocation.width / 2;
2609 /* No old focus widget, need to figure out starting x,y some other way
2611 GtkAllocation allocation;
2612 GtkWidget *widget = GTK_WIDGET (container);
2613 GdkRectangle old_focus_rect;
2615 gtk_widget_get_allocation (widget, &allocation);
2617 if (old_focus_coords (container, &old_focus_rect))
2619 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2623 if (!gtk_widget_get_has_window (widget))
2624 compare.y = allocation.y + allocation.height / 2;
2626 compare.y = allocation.height / 2;
2629 if (!gtk_widget_get_has_window (widget))
2630 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2632 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2635 children = g_list_sort_with_data (children, left_right_compare, &compare);
2637 if (compare.reverse)
2638 children = g_list_reverse (children);
2644 * gtk_container_focus_sort:
2645 * @container: a #GtkContainer
2646 * @children: a list of descendents of @container (they don't
2647 * have to be direct children)
2648 * @direction: focus direction
2649 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2650 * to determine this automatically.
2651 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2652 * which is the only @direction we use currently,
2653 * so perhaps this argument should be removed)
2655 * Sorts @children in the correct order for focusing with
2656 * direction type @direction.
2658 * Return value: a copy of @children, sorted in correct focusing order,
2659 * with children that aren't suitable for focusing in this direction
2663 _gtk_container_focus_sort (GtkContainer *container,
2665 GtkDirectionType direction,
2666 GtkWidget *old_focus)
2668 GList *visible_children = NULL;
2672 if (gtk_widget_get_realized (children->data))
2673 visible_children = g_list_prepend (visible_children, children->data);
2674 children = children->next;
2679 case GTK_DIR_TAB_FORWARD:
2680 case GTK_DIR_TAB_BACKWARD:
2681 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2684 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2687 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2690 g_assert_not_reached ();
2696 gtk_container_focus_move (GtkContainer *container,
2698 GtkDirectionType direction)
2700 GtkContainerPrivate *priv = container->priv;
2701 GtkWidget *focus_child;
2704 focus_child = priv->focus_child;
2708 child = children->data;
2709 children = children->next;
2716 if (focus_child == child)
2720 if (gtk_widget_child_focus (child, direction))
2724 else if (gtk_widget_is_drawable (child) &&
2725 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2727 if (gtk_widget_child_focus (child, direction))
2737 gtk_container_children_callback (GtkWidget *widget,
2738 gpointer client_data)
2742 children = (GList**) client_data;
2743 *children = g_list_prepend (*children, widget);
2747 chain_widget_destroyed (GtkWidget *widget,
2750 GtkContainer *container;
2753 container = GTK_CONTAINER (user_data);
2755 chain = g_object_get_data (G_OBJECT (container),
2756 "gtk-container-focus-chain");
2758 chain = g_list_remove (chain, widget);
2760 g_signal_handlers_disconnect_by_func (widget,
2761 chain_widget_destroyed,
2764 g_object_set_data (G_OBJECT (container),
2765 I_("gtk-container-focus-chain"),
2770 * gtk_container_set_focus_chain:
2771 * @container: a #GtkContainer
2772 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2773 * the new focus chain
2775 * Sets a focus chain, overriding the one computed automatically by GTK+.
2777 * In principle each widget in the chain should be a descendant of the
2778 * container, but this is not enforced by this method, since it's allowed
2779 * to set the focus chain before you pack the widgets, or have a widget
2780 * in the chain that isn't always packed. The necessary checks are done
2781 * when the focus chain is actually traversed.
2784 gtk_container_set_focus_chain (GtkContainer *container,
2785 GList *focusable_widgets)
2789 GtkContainerPrivate *priv;
2791 g_return_if_fail (GTK_IS_CONTAINER (container));
2793 priv = container->priv;
2795 if (priv->has_focus_chain)
2796 gtk_container_unset_focus_chain (container);
2798 priv->has_focus_chain = TRUE;
2801 tmp_list = focusable_widgets;
2802 while (tmp_list != NULL)
2804 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2806 /* In principle each widget in the chain should be a descendant
2807 * of the container, but we don't want to check that here, it's
2808 * expensive and also it's allowed to set the focus chain before
2809 * you pack the widgets, or have a widget in the chain that isn't
2810 * always packed. So we check for ancestor during actual traversal.
2813 chain = g_list_prepend (chain, tmp_list->data);
2815 g_signal_connect (tmp_list->data,
2817 G_CALLBACK (chain_widget_destroyed),
2820 tmp_list = g_list_next (tmp_list);
2823 chain = g_list_reverse (chain);
2825 g_object_set_data (G_OBJECT (container),
2826 I_("gtk-container-focus-chain"),
2831 * gtk_container_get_focus_chain:
2832 * @container: a #GtkContainer
2833 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2834 * to store the focus chain of the
2835 * container, or %NULL. You should free this list
2836 * using g_list_free() when you are done with it, however
2837 * no additional reference count is added to the
2838 * individual widgets in the focus chain.
2840 * Retrieves the focus chain of the container, if one has been
2841 * set explicitly. If no focus chain has been explicitly
2842 * set, GTK+ computes the focus chain based on the positions
2843 * of the children. In that case, GTK+ stores %NULL in
2844 * @focusable_widgets and returns %FALSE.
2846 * Return value: %TRUE if the focus chain of the container
2847 * has been set explicitly.
2850 gtk_container_get_focus_chain (GtkContainer *container,
2851 GList **focus_chain)
2853 GtkContainerPrivate *priv;
2855 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2857 priv = container->priv;
2861 if (priv->has_focus_chain)
2862 *focus_chain = g_list_copy (get_focus_chain (container));
2864 *focus_chain = NULL;
2867 return priv->has_focus_chain;
2871 * gtk_container_unset_focus_chain:
2872 * @container: a #GtkContainer
2874 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2877 gtk_container_unset_focus_chain (GtkContainer *container)
2879 GtkContainerPrivate *priv;
2881 g_return_if_fail (GTK_IS_CONTAINER (container));
2883 priv = container->priv;
2885 if (priv->has_focus_chain)
2890 chain = get_focus_chain (container);
2892 priv->has_focus_chain = FALSE;
2894 g_object_set_data (G_OBJECT (container),
2895 I_("gtk-container-focus-chain"),
2899 while (tmp_list != NULL)
2901 g_signal_handlers_disconnect_by_func (tmp_list->data,
2902 chain_widget_destroyed,
2905 tmp_list = g_list_next (tmp_list);
2908 g_list_free (chain);
2913 * gtk_container_set_focus_vadjustment:
2914 * @container: a #GtkContainer
2915 * @adjustment: an adjustment which should be adjusted when the focus
2916 * is moved among the descendents of @container
2918 * Hooks up an adjustment to focus handling in a container, so when a
2919 * child of the container is focused, the adjustment is scrolled to
2920 * show that widget. This function sets the vertical alignment. See
2921 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
2922 * the adjustment and gtk_container_set_focus_hadjustment() for setting
2923 * the horizontal adjustment.
2925 * The adjustments have to be in pixel units and in the same coordinate
2926 * system as the allocation for immediate children of the container.
2929 gtk_container_set_focus_vadjustment (GtkContainer *container,
2930 GtkAdjustment *adjustment)
2932 g_return_if_fail (GTK_IS_CONTAINER (container));
2934 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2937 g_object_ref (adjustment);
2939 g_object_set_qdata_full (G_OBJECT (container),
2946 * gtk_container_get_focus_vadjustment:
2947 * @container: a #GtkContainer
2949 * Retrieves the vertical focus adjustment for the container. See
2950 * gtk_container_set_focus_vadjustment().
2952 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
2953 * none has been set.
2956 gtk_container_get_focus_vadjustment (GtkContainer *container)
2958 GtkAdjustment *vadjustment;
2960 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2962 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2968 * gtk_container_set_focus_hadjustment:
2969 * @container: a #GtkContainer
2970 * @adjustment: an adjustment which should be adjusted when the focus is
2971 * moved among the descendents of @container
2973 * Hooks up an adjustment to focus handling in a container, so when a child
2974 * of the container is focused, the adjustment is scrolled to show that
2975 * widget. This function sets the horizontal alignment.
2976 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
2977 * the adjustment and gtk_container_set_focus_vadjustment() for setting
2978 * the vertical adjustment.
2980 * The adjustments have to be in pixel units and in the same coordinate
2981 * system as the allocation for immediate children of the container.
2984 gtk_container_set_focus_hadjustment (GtkContainer *container,
2985 GtkAdjustment *adjustment)
2987 g_return_if_fail (GTK_IS_CONTAINER (container));
2989 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2992 g_object_ref (adjustment);
2994 g_object_set_qdata_full (G_OBJECT (container),
3001 * gtk_container_get_focus_hadjustment:
3002 * @container: a #GtkContainer
3004 * Retrieves the horizontal focus adjustment for the container. See
3005 * gtk_container_set_focus_hadjustment ().
3007 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3008 * none has been set.
3011 gtk_container_get_focus_hadjustment (GtkContainer *container)
3013 GtkAdjustment *hadjustment;
3015 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3017 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3024 gtk_container_show_all (GtkWidget *widget)
3026 g_return_if_fail (GTK_IS_CONTAINER (widget));
3028 gtk_container_foreach (GTK_CONTAINER (widget),
3029 (GtkCallback) gtk_widget_show_all,
3031 gtk_widget_show (widget);
3035 gtk_container_hide_all (GtkWidget *widget)
3037 g_return_if_fail (GTK_IS_CONTAINER (widget));
3039 gtk_widget_hide (widget);
3040 gtk_container_foreach (GTK_CONTAINER (widget),
3041 (GtkCallback) gtk_widget_hide_all,
3047 gtk_container_draw_child (GtkWidget *child,
3048 gpointer client_data)
3051 GtkWidget *container;
3053 } *data = client_data;
3055 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3061 gtk_container_draw (GtkWidget *widget,
3065 GtkWidget *container;
3069 data.container = widget;
3072 gtk_container_forall (GTK_CONTAINER (widget),
3073 gtk_container_draw_child,
3080 gtk_container_map_child (GtkWidget *child,
3081 gpointer client_data)
3083 if (gtk_widget_get_visible (child) &&
3084 gtk_widget_get_child_visible (child) &&
3085 !gtk_widget_get_mapped (child))
3086 gtk_widget_map (child);
3090 gtk_container_map (GtkWidget *widget)
3092 gtk_widget_set_mapped (widget, TRUE);
3094 gtk_container_forall (GTK_CONTAINER (widget),
3095 gtk_container_map_child,
3098 if (gtk_widget_get_has_window (widget))
3099 gdk_window_show (gtk_widget_get_window (widget));
3103 gtk_container_unmap (GtkWidget *widget)
3105 gtk_widget_set_mapped (widget, FALSE);
3107 if (gtk_widget_get_has_window (widget))
3108 gdk_window_hide (gtk_widget_get_window (widget));
3110 gtk_container_forall (GTK_CONTAINER (widget),
3111 (GtkCallback)gtk_widget_unmap,
3116 * gtk_container_propagate_draw:
3117 * @container: a #GtkContainer
3118 * @child: a child of @container
3119 * @cr: Cairo context as passed to the container. If you want to use @cr
3120 * in container's draw function, consider using cairo_save() and
3121 * cairo_restore() before calling this function.
3123 * When a container receives a call to the draw function, it must send
3124 * synthetic #GtkWidget::draw calls to all children that don't have their
3125 * own #GdkWindows. This function provides a convenient way of doing this.
3126 * A container, when it receives a call to its #GtkWidget::draw function,
3127 * calls gtk_container_propagate_draw() once for each child, passing in
3128 * the @cr the container received.
3130 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3131 * and deciding whether the draw needs to be sent to the child. It is a
3132 * convenient and optimized way of getting the same effect as calling
3133 * gtk_widget_draw() on the child directly.
3135 * In most cases, a container can simply either inherit the
3136 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3137 * and then chain to the ::draw implementation from #GtkContainer.
3140 gtk_container_propagate_draw (GtkContainer *container,
3144 GdkEventExpose *event;
3145 GtkAllocation allocation;
3146 GdkWindow *window, *w;
3149 g_return_if_fail (GTK_IS_CONTAINER (container));
3150 g_return_if_fail (GTK_IS_WIDGET (child));
3151 g_return_if_fail (cr != NULL);
3153 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3155 event = _gtk_cairo_get_event (cr);
3158 if (gtk_widget_get_has_window (child) ||
3159 gtk_widget_get_window (child) != event->window)
3165 /* translate coordinates. Ugly business, that. */
3166 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3168 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3178 window = gtk_widget_get_window (GTK_WIDGET (container));
3180 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3183 gdk_window_get_position (w, &wx, &wy);
3194 if (!gtk_widget_get_has_window (child))
3196 gtk_widget_get_allocation (child, &allocation);
3201 cairo_translate (cr, x, y);
3203 _gtk_widget_draw_internal (child, cr, TRUE);
3209 _gtk_container_get_need_resize (GtkContainer *container)
3211 return container->priv->need_resize;
3215 _gtk_container_set_need_resize (GtkContainer *container,
3216 gboolean need_resize)
3218 container->priv->need_resize = need_resize;
3222 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3224 return container->priv->reallocate_redraws;