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"
30 #include "gtkcontainerprivate.h"
36 #include <gobject/gobjectnotifyqueue.c>
37 #include <gobject/gvaluecollector.h>
39 #include "gtkbuildable.h"
40 #include "gtkbuilderprivate.h"
41 #include "gtktypebuiltins.h"
42 #include "gtkprivate.h"
44 #include "gtkmarshalers.h"
45 #include "gtksizerequest.h"
46 #include "gtkwidgetprivate.h"
47 #include "gtkwindow.h"
49 #include "a11y/gtkcontaineraccessible.h"
52 * SECTION:gtkcontainer
53 * @Short_description: Base class for widgets which contain other widgets
54 * @Title: GtkContainer
56 * A GTK+ user interface is constructed by nesting widgets inside widgets.
57 * Container widgets are the inner nodes in the resulting tree of widgets:
58 * they contain other widgets. So, for example, you might have a #GtkWindow
59 * containing a #GtkFrame containing a #GtkLabel. If you wanted an image instead
60 * of a textual label inside the frame, you might replace the #GtkLabel widget
61 * with a #GtkImage widget.
63 * There are two major kinds of container widgets in GTK+. Both are subclasses
64 * of the abstract GtkContainer base class.
66 * The first type of container widget has a single child widget and derives
67 * from #GtkBin. These containers are <emphasis>decorators</emphasis>, which
68 * add some kind of functionality to the child. For example, a #GtkButton makes
69 * its child into a clickable button; a #GtkFrame draws a frame around its child
70 * and a #GtkWindow places its child widget inside a top-level window.
72 * The second type of container can have more than one child; its purpose is to
73 * manage <emphasis>layout</emphasis>. This means that these containers assign
74 * sizes and positions to their children. For example, a #GtkHBox arranges its
75 * children in a horizontal row, and a #GtkTable arranges the widgets it contains
76 * in a two-dimensional grid.
78 * <refsect2 id="container-geometry-management">
79 * <title>Height for width geometry management</title>
81 * GTK+ uses a height-for-width (and width-for-height) geometry management system.
82 * Height-for-width means that a widget can change how much vertical space it needs,
83 * depending on the amount of horizontal space that it is given (and similar for
86 * There are some things to keep in mind when implementing container widgets
87 * that make use of GTK+'s height for width geometry management system. First,
88 * it's important to note that a container must prioritize one of its
89 * dimensions, that is to say that a widget or container can only have a
90 * #GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or
91 * %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container
92 * must be able to respond to the APIs for both dimensions, i.e. even if a
93 * widget has a request mode that is height-for-width, it is possible that
94 * its parent will request its sizes using the width-for-height APIs.
96 * To ensure that everything works properly, here are some guidelines to follow
97 * when implementing height-for-width (or width-for-height) containers.
99 * Each request mode involves 2 virtual methods. Height-for-width apis run
100 * through gtk_widget_get_preferred_width() and then through gtk_widget_get_preferred_height_for_width().
101 * When handling requests in the opposite #GtkSizeRequestMode it is important that
102 * every widget request at least enough space to display all of its content at all times.
104 * When gtk_widget_get_preferred_height() is called on a container that is height-for-width,
105 * the container must return the height for its minimum width. This is easily achieved by
106 * simply calling the reverse apis implemented for itself as follows:
108 * <programlisting><![CDATA[
110 * foo_container_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
112 * if (i_am_in_height_for_width_mode)
116 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
117 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
118 * min_height, nat_height);
122 * ... many containers support both request modes, execute the real width-for-height
123 * request here by returning the collective heights of all widgets that are
124 * stacked vertically (or whatever is appropriate for this container) ...
127 * ]]></programlisting>
129 * Similarly, when gtk_widget_get_preferred_width_for_height() is called for a container or widget
130 * that is height-for-width, it then only needs to return the base minimum width like so:
132 * <programlisting><![CDATA[
134 * foo_container_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
135 * gint *min_width, gint *nat_width)
137 * if (i_am_in_height_for_width_mode)
139 * GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
143 * ... execute the real width-for-height request here based on the required width
144 * of the children collectively if the container were to be allocated the said height ...
147 * ]]></programlisting>
149 * Height for width requests are generally implemented in terms of a virtual allocation
150 * of widgets in the input orientation. Assuming an height-for-width request mode, a container
151 * would implement the <function>get_preferred_height_for_width()</function> virtual function by first calling
152 * gtk_widget_get_preferred_width() for each of its children.
154 * For each potential group of children that are lined up horizontally, the values returned by
155 * gtk_widget_get_preferred_width() should be collected in an array of #GtkRequestedSize structures.
156 * Any child spacing should be removed from the input @for_width and then the collective size should be
157 * allocated using the gtk_distribute_natural_allocation() convenience function.
159 * The container will then move on to request the preferred height for each child by using
160 * gtk_widget_get_preferred_height_for_width() and using the sizes stored in the #GtkRequestedSize array.
162 * To allocate a height-for-width container, it's again important
163 * to consider that a container must prioritize one dimension over the other. So if
164 * a container is a height-for-width container it must first allocate all widgets horizontally
165 * using a #GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any
166 * extra space (if and where appropriate) for the widget to expand.
168 * After adding all the expand space, the container assumes it was allocated sufficient
169 * height to fit all of its content. At this time, the container must use the total horizontal sizes
170 * of each widget to request the height-for-width of each of its children and store the requests in a
171 * #GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can
172 * be generalized into the heights and widths of rows and columns).
173 * The vertical space must then again be distributed using gtk_distribute_natural_allocation()
174 * while this time considering the allocated height of the widget minus any vertical spacing
175 * that the container adds. Then vertical expand space should be added where appropriate and available
176 * and the container should go on to actually allocating the child widgets.
178 * See <link linkend="geometry-management">GtkWidget's geometry management section</link>
179 * to learn more about implementing height-for-width geometry management for widgets.
182 * <refsect2 id="child-properties">
183 * <title>Child properties</title>
185 * GtkContainer introduces <emphasis>child properties</emphasis>.
186 * These are object properties that are not specific
187 * to either the container or the contained widget, but rather to their relation.
188 * Typical examples of child properties are the position or pack-type of a widget
189 * which is contained in a #GtkBox.
191 * Use gtk_container_class_install_child_property() to install child properties
192 * for a container class and gtk_container_class_find_child_property() or
193 * gtk_container_class_list_child_properties() to get information about existing
196 * To set the value of a child property, use gtk_container_child_set_property(),
197 * gtk_container_child_set() or gtk_container_child_set_valist().
198 * To obtain the value of a child property, use
199 * gtk_container_child_get_property(), gtk_container_child_get() or
200 * gtk_container_child_get_valist(). To emit notification about child property
201 * changes, use gtk_widget_child_notify().
204 * <refsect2 id="GtkContainer-BUILDER-UI">
205 * <title>GtkContainer as GtkBuildable</title>
207 * The GtkContainer implementation of the GtkBuildable interface
208 * supports a <packing> element for children, which can
209 * contain multiple <property> elements that specify
210 * child properties for the child.
212 * <title>Child properties in UI definitions</title>
213 * <programlisting><![CDATA[
214 * <object class="GtkVBox">
216 * <object class="GtkLabel"/>
218 * <property name="pack-type">start</property>
222 * ]]></programlisting>
224 * Since 2.16, child properties can also be marked as translatable using
225 * the same "translatable", "comments" and "context" attributes that are used
226 * for regular properties.
232 struct _GtkContainerPrivate
234 GtkWidget *focus_child;
236 guint border_width : 16;
238 guint has_focus_chain : 1;
239 guint need_resize : 1;
240 guint reallocate_redraws : 1;
241 guint resize_mode : 2;
242 guint request_mode : 2;
260 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
261 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
264 /* --- prototypes --- */
265 static void gtk_container_base_class_init (GtkContainerClass *klass);
266 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
267 static void gtk_container_class_init (GtkContainerClass *klass);
268 static void gtk_container_init (GtkContainer *container);
269 static void gtk_container_destroy (GtkWidget *widget);
270 static void gtk_container_set_property (GObject *object,
274 static void gtk_container_get_property (GObject *object,
278 static void gtk_container_add_unimplemented (GtkContainer *container,
280 static void gtk_container_remove_unimplemented (GtkContainer *container,
282 static void gtk_container_real_check_resize (GtkContainer *container);
283 static void gtk_container_compute_expand (GtkWidget *widget,
285 gboolean *vexpand_p);
286 static gboolean gtk_container_focus (GtkWidget *widget,
287 GtkDirectionType direction);
288 static void gtk_container_real_set_focus_child (GtkContainer *container,
291 static gboolean gtk_container_focus_move (GtkContainer *container,
293 GtkDirectionType direction);
294 static void gtk_container_children_callback (GtkWidget *widget,
295 gpointer client_data);
296 static void gtk_container_show_all (GtkWidget *widget);
297 static gint gtk_container_draw (GtkWidget *widget,
299 static void gtk_container_map (GtkWidget *widget);
300 static void gtk_container_unmap (GtkWidget *widget);
301 static void gtk_container_adjust_size_request (GtkWidget *widget,
302 GtkOrientation orientation,
305 static void gtk_container_adjust_size_allocation (GtkWidget *widget,
306 GtkOrientation orientation,
310 gint *allocated_size);
311 static GtkSizeRequestMode gtk_container_get_request_mode (GtkWidget *widget);
313 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
316 static GtkWidgetPath * gtk_container_real_get_path_for_child (GtkContainer *container,
320 static void gtk_container_buildable_init (GtkBuildableIface *iface);
321 static void gtk_container_buildable_add_child (GtkBuildable *buildable,
325 static gboolean gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
328 const gchar *tagname,
329 GMarkupParser *parser,
331 static void gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
334 const gchar *tagname,
338 /* --- variables --- */
339 static const gchar vadjustment_key[] = "gtk-vadjustment";
340 static guint vadjustment_key_id = 0;
341 static const gchar hadjustment_key[] = "gtk-hadjustment";
342 static guint hadjustment_key_id = 0;
343 static GSList *container_resize_queue = NULL;
344 static guint container_signals[LAST_SIGNAL] = { 0 };
345 static GtkWidgetClass *parent_class = NULL;
346 extern GParamSpecPool *_gtk_widget_child_property_pool;
347 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
348 static GtkBuildableIface *parent_buildable_iface;
351 /* --- functions --- */
353 gtk_container_get_type (void)
355 static GType container_type = 0;
359 const GTypeInfo container_info =
361 sizeof (GtkContainerClass),
362 (GBaseInitFunc) gtk_container_base_class_init,
363 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
364 (GClassInitFunc) gtk_container_class_init,
365 NULL /* class_finalize */,
366 NULL /* class_data */,
367 sizeof (GtkContainer),
369 (GInstanceInitFunc) gtk_container_init,
370 NULL, /* value_table */
373 const GInterfaceInfo buildable_info =
375 (GInterfaceInitFunc) gtk_container_buildable_init,
381 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
382 &container_info, G_TYPE_FLAG_ABSTRACT);
384 g_type_add_interface_static (container_type,
390 return container_type;
394 gtk_container_base_class_init (GtkContainerClass *class)
396 /* reset instance specifc class fields that don't get inherited */
397 class->set_child_property = NULL;
398 class->get_child_property = NULL;
402 gtk_container_base_class_finalize (GtkContainerClass *class)
406 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
407 for (node = list; node; node = node->next)
409 GParamSpec *pspec = node->data;
411 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
412 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
413 g_param_spec_unref (pspec);
419 gtk_container_class_init (GtkContainerClass *class)
421 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
422 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
424 parent_class = g_type_class_peek_parent (class);
426 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
427 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
429 gobject_class->set_property = gtk_container_set_property;
430 gobject_class->get_property = gtk_container_get_property;
432 widget_class->destroy = gtk_container_destroy;
433 widget_class->compute_expand = gtk_container_compute_expand;
434 widget_class->show_all = gtk_container_show_all;
435 widget_class->draw = gtk_container_draw;
436 widget_class->map = gtk_container_map;
437 widget_class->unmap = gtk_container_unmap;
438 widget_class->focus = gtk_container_focus;
440 widget_class->adjust_size_request = gtk_container_adjust_size_request;
441 widget_class->adjust_size_allocation = gtk_container_adjust_size_allocation;
442 widget_class->get_request_mode = gtk_container_get_request_mode;
444 class->add = gtk_container_add_unimplemented;
445 class->remove = gtk_container_remove_unimplemented;
446 class->check_resize = gtk_container_real_check_resize;
447 class->forall = NULL;
448 class->set_focus_child = gtk_container_real_set_focus_child;
449 class->child_type = NULL;
450 class->composite_name = gtk_container_child_default_composite_name;
451 class->get_path_for_child = gtk_container_real_get_path_for_child;
453 g_object_class_install_property (gobject_class,
455 g_param_spec_enum ("resize-mode",
457 P_("Specify how resize events are handled"),
458 GTK_TYPE_RESIZE_MODE,
460 GTK_PARAM_READWRITE));
461 g_object_class_install_property (gobject_class,
463 g_param_spec_uint ("border-width",
465 P_("The width of the empty border outside the containers children"),
469 GTK_PARAM_READWRITE));
470 g_object_class_install_property (gobject_class,
472 g_param_spec_object ("child",
474 P_("Can be used to add a new child to the container"),
476 GTK_PARAM_WRITABLE));
477 container_signals[ADD] =
478 g_signal_new (I_("add"),
479 G_OBJECT_CLASS_TYPE (gobject_class),
481 G_STRUCT_OFFSET (GtkContainerClass, add),
483 _gtk_marshal_VOID__OBJECT,
486 container_signals[REMOVE] =
487 g_signal_new (I_("remove"),
488 G_OBJECT_CLASS_TYPE (gobject_class),
490 G_STRUCT_OFFSET (GtkContainerClass, remove),
492 _gtk_marshal_VOID__OBJECT,
495 container_signals[CHECK_RESIZE] =
496 g_signal_new (I_("check-resize"),
497 G_OBJECT_CLASS_TYPE (gobject_class),
499 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
501 _gtk_marshal_VOID__VOID,
503 container_signals[SET_FOCUS_CHILD] =
504 g_signal_new (I_("set-focus-child"),
505 G_OBJECT_CLASS_TYPE (gobject_class),
507 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
509 _gtk_marshal_VOID__OBJECT,
513 g_type_class_add_private (class, sizeof (GtkContainerPrivate));
515 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CONTAINER_ACCESSIBLE);
519 gtk_container_buildable_init (GtkBuildableIface *iface)
521 parent_buildable_iface = g_type_interface_peek_parent (iface);
522 iface->add_child = gtk_container_buildable_add_child;
523 iface->custom_tag_start = gtk_container_buildable_custom_tag_start;
524 iface->custom_tag_end = gtk_container_buildable_custom_tag_end;
528 gtk_container_buildable_add_child (GtkBuildable *buildable,
535 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
537 else if (GTK_IS_WIDGET (child) &&
538 gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
540 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
543 g_warning ("Cannot add an object of type %s to a container of type %s",
544 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
548 gtk_container_buildable_set_child_property (GtkContainer *container,
555 GValue gvalue = { 0, };
556 GError *error = NULL;
558 pspec = gtk_container_class_find_child_property
559 (G_OBJECT_GET_CLASS (container), name);
562 g_warning ("%s does not have a property called %s",
563 g_type_name (G_OBJECT_TYPE (container)), name);
567 if (!gtk_builder_value_from_string (builder, pspec, value, &gvalue, &error))
569 g_warning ("Could not read property %s:%s with value %s of type %s: %s",
570 g_type_name (G_OBJECT_TYPE (container)),
573 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
575 g_error_free (error);
579 gtk_container_child_set_property (container, child, name, &gvalue);
580 g_value_unset (&gvalue);
585 GtkContainer *container;
588 gchar *child_prop_name;
590 gboolean translatable;
591 } PackingPropertiesData;
594 attributes_start_element (GMarkupParseContext *context,
595 const gchar *element_name,
597 const gchar **values,
601 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
604 if (strcmp (element_name, "property") == 0)
606 for (i = 0; names[i]; i++)
607 if (strcmp (names[i], "name") == 0)
608 parser_data->child_prop_name = g_strdup (values[i]);
609 else if (strcmp (names[i], "translatable") == 0)
611 if (!_gtk_builder_boolean_from_string (values[1],
612 &parser_data->translatable,
616 else if (strcmp (names[i], "comments") == 0)
617 ; /* for translators */
618 else if (strcmp (names[i], "context") == 0)
619 parser_data->context = g_strdup (values[1]);
621 g_warning ("Unsupported attribute for GtkContainer Child "
622 "property: %s\n", names[i]);
624 else if (strcmp (element_name, "packing") == 0)
627 g_warning ("Unsupported tag for GtkContainer: %s\n", element_name);
631 attributes_text_element (GMarkupParseContext *context,
637 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
639 if (parser_data->child_prop_name)
640 g_string_append_len (parser_data->string, text, text_len);
644 attributes_end_element (GMarkupParseContext *context,
645 const gchar *element_name,
649 PackingPropertiesData *parser_data = (PackingPropertiesData*)user_data;
651 /* translate the string */
652 if (parser_data->string->len && parser_data->translatable)
657 domain = gtk_builder_get_translation_domain (parser_data->builder);
659 translated = _gtk_builder_parser_translate (domain,
660 parser_data->context,
661 parser_data->string->str);
662 g_string_set_size (parser_data->string, 0);
663 g_string_append (parser_data->string, translated);
666 if (parser_data->child_prop_name)
667 gtk_container_buildable_set_child_property (parser_data->container,
668 parser_data->builder,
670 parser_data->child_prop_name,
671 parser_data->string->str);
673 g_string_set_size (parser_data->string, 0);
674 g_free (parser_data->child_prop_name);
675 g_free (parser_data->context);
676 parser_data->child_prop_name = NULL;
677 parser_data->context = NULL;
678 parser_data->translatable = FALSE;
681 static const GMarkupParser attributes_parser =
683 attributes_start_element,
684 attributes_end_element,
685 attributes_text_element,
689 gtk_container_buildable_custom_tag_start (GtkBuildable *buildable,
692 const gchar *tagname,
693 GMarkupParser *parser,
696 PackingPropertiesData *parser_data;
698 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
699 tagname, parser, data))
702 if (child && strcmp (tagname, "packing") == 0)
704 parser_data = g_slice_new0 (PackingPropertiesData);
705 parser_data->string = g_string_new ("");
706 parser_data->builder = builder;
707 parser_data->container = GTK_CONTAINER (buildable);
708 parser_data->child = GTK_WIDGET (child);
709 parser_data->child_prop_name = NULL;
711 *parser = attributes_parser;
720 gtk_container_buildable_custom_tag_end (GtkBuildable *buildable,
723 const gchar *tagname,
726 if (strcmp (tagname, "packing") == 0)
728 PackingPropertiesData *parser_data = (PackingPropertiesData*)data;
729 g_string_free (parser_data->string, TRUE);
730 g_slice_free (PackingPropertiesData, parser_data);
734 if (parent_buildable_iface->custom_tag_end)
735 parent_buildable_iface->custom_tag_end (buildable, builder,
736 child, tagname, data);
740 * gtk_container_child_type:
741 * @container: a #GtkContainer
743 * Returns the type of the children supported by the container.
745 * Note that this may return %G_TYPE_NONE to indicate that no more
746 * children can be added, e.g. for a #GtkPaned which already has two
749 * Return value: a #GType.
752 gtk_container_child_type (GtkContainer *container)
755 GtkContainerClass *class;
757 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
759 class = GTK_CONTAINER_GET_CLASS (container);
760 if (class->child_type)
761 slot = class->child_type (container);
768 /* --- GtkContainer child property mechanism --- */
771 * gtk_container_child_notify:
772 * @container: the #GtkContainer
773 * @child: the child widget
774 * @child_property: the name of a child property installed on
775 * the class of @container
777 * Emits a #GtkWidget::child-notify signal for the
778 * <link linkend="child-properties">child property</link>
779 * @child_property on widget.
781 * This is an analogue of g_object_notify() for child properties.
783 * Also see gtk_widget_child_notify().
788 gtk_container_child_notify (GtkContainer *container,
790 const gchar *child_property)
795 g_return_if_fail (GTK_IS_CONTAINER (container));
796 g_return_if_fail (GTK_IS_WIDGET (child));
797 g_return_if_fail (child_property != NULL);
799 obj = G_OBJECT (child);
801 if (obj->ref_count == 0)
806 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
808 G_OBJECT_TYPE (container),
813 g_warning ("%s: container class `%s' has no child property named `%s'",
815 G_OBJECT_TYPE_NAME (container),
820 GObjectNotifyQueue *nqueue;
822 nqueue = g_object_notify_queue_freeze (obj, _gtk_widget_child_property_notify_context);
824 g_object_notify_queue_add (obj, nqueue, pspec);
825 g_object_notify_queue_thaw (obj, nqueue);
828 g_object_unref (obj);
832 container_get_child_property (GtkContainer *container,
837 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
839 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
843 container_set_child_property (GtkContainer *container,
847 GObjectNotifyQueue *nqueue)
849 GValue tmp_value = { 0, };
850 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
852 /* provide a copy to work from, convert (if necessary) and validate */
853 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
854 if (!g_value_transform (value, &tmp_value))
855 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
857 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
858 G_VALUE_TYPE_NAME (value));
859 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
861 gchar *contents = g_strdup_value_contents (value);
863 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
865 G_VALUE_TYPE_NAME (value),
867 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
872 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
873 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
875 g_value_unset (&tmp_value);
879 * gtk_container_child_get_valist:
880 * @container: a #GtkContainer
881 * @child: a widget which is a child of @container
882 * @first_property_name: the name of the first property to get
883 * @var_args: return location for the first property, followed
884 * optionally by more name/return location pairs, followed by %NULL
886 * Gets the values of one or more child properties for @child and @container.
889 gtk_container_child_get_valist (GtkContainer *container,
891 const gchar *first_property_name,
896 g_return_if_fail (GTK_IS_CONTAINER (container));
897 g_return_if_fail (GTK_IS_WIDGET (child));
899 g_object_ref (container);
900 g_object_ref (child);
902 name = first_property_name;
905 GValue value = { 0, };
909 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
911 G_OBJECT_TYPE (container),
915 g_warning ("%s: container class `%s' has no child property named `%s'",
917 G_OBJECT_TYPE_NAME (container),
921 if (!(pspec->flags & G_PARAM_READABLE))
923 g_warning ("%s: child property `%s' of container class `%s' is not readable",
926 G_OBJECT_TYPE_NAME (container));
929 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
930 container_get_child_property (container, child, pspec, &value);
931 G_VALUE_LCOPY (&value, var_args, 0, &error);
934 g_warning ("%s: %s", G_STRLOC, error);
936 g_value_unset (&value);
939 g_value_unset (&value);
940 name = va_arg (var_args, gchar*);
943 g_object_unref (child);
944 g_object_unref (container);
948 * gtk_container_child_get_property:
949 * @container: a #GtkContainer
950 * @child: a widget which is a child of @container
951 * @property_name: the name of the property to get
952 * @value: a location to return the value
954 * Gets the value of a child property for @child and @container.
957 gtk_container_child_get_property (GtkContainer *container,
959 const gchar *property_name,
964 g_return_if_fail (GTK_IS_CONTAINER (container));
965 g_return_if_fail (GTK_IS_WIDGET (child));
966 g_return_if_fail (property_name != NULL);
967 g_return_if_fail (G_IS_VALUE (value));
969 g_object_ref (container);
970 g_object_ref (child);
971 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
972 G_OBJECT_TYPE (container), TRUE);
974 g_warning ("%s: container class `%s' has no child property named `%s'",
976 G_OBJECT_TYPE_NAME (container),
978 else if (!(pspec->flags & G_PARAM_READABLE))
979 g_warning ("%s: child property `%s' of container class `%s' is not readable",
982 G_OBJECT_TYPE_NAME (container));
985 GValue *prop_value, tmp_value = { 0, };
987 /* auto-conversion of the callers value type
989 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
991 g_value_reset (value);
994 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
996 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
998 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
999 G_VALUE_TYPE_NAME (value));
1000 g_object_unref (child);
1001 g_object_unref (container);
1006 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1007 prop_value = &tmp_value;
1009 container_get_child_property (container, child, pspec, prop_value);
1010 if (prop_value != value)
1012 g_value_transform (prop_value, value);
1013 g_value_unset (&tmp_value);
1016 g_object_unref (child);
1017 g_object_unref (container);
1021 * gtk_container_child_set_valist:
1022 * @container: a #GtkContainer
1023 * @child: a widget which is a child of @container
1024 * @first_property_name: the name of the first property to set
1025 * @var_args: a %NULL-terminated list of property names and values, starting
1026 * with @first_prop_name
1028 * Sets one or more child properties for @child and @container.
1031 gtk_container_child_set_valist (GtkContainer *container,
1033 const gchar *first_property_name,
1036 GObjectNotifyQueue *nqueue;
1039 g_return_if_fail (GTK_IS_CONTAINER (container));
1040 g_return_if_fail (GTK_IS_WIDGET (child));
1042 g_object_ref (container);
1043 g_object_ref (child);
1045 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1046 name = first_property_name;
1049 GValue value = { 0, };
1050 gchar *error = NULL;
1051 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1053 G_OBJECT_TYPE (container),
1057 g_warning ("%s: container class `%s' has no child property named `%s'",
1059 G_OBJECT_TYPE_NAME (container),
1063 if (!(pspec->flags & G_PARAM_WRITABLE))
1065 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1068 G_OBJECT_TYPE_NAME (container));
1072 G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
1073 var_args, 0, &error);
1076 g_warning ("%s: %s", G_STRLOC, error);
1079 /* we purposely leak the value here, it might not be
1080 * in a sane state if an error condition occoured
1084 container_set_child_property (container, child, pspec, &value, nqueue);
1085 g_value_unset (&value);
1086 name = va_arg (var_args, gchar*);
1088 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1090 g_object_unref (container);
1091 g_object_unref (child);
1095 * gtk_container_child_set_property:
1096 * @container: a #GtkContainer
1097 * @child: a widget which is a child of @container
1098 * @property_name: the name of the property to set
1099 * @value: the value to set the property to
1101 * Sets a child property for @child and @container.
1104 gtk_container_child_set_property (GtkContainer *container,
1106 const gchar *property_name,
1107 const GValue *value)
1109 GObjectNotifyQueue *nqueue;
1112 g_return_if_fail (GTK_IS_CONTAINER (container));
1113 g_return_if_fail (GTK_IS_WIDGET (child));
1114 g_return_if_fail (property_name != NULL);
1115 g_return_if_fail (G_IS_VALUE (value));
1117 g_object_ref (container);
1118 g_object_ref (child);
1120 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
1121 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
1122 G_OBJECT_TYPE (container), TRUE);
1124 g_warning ("%s: container class `%s' has no child property named `%s'",
1126 G_OBJECT_TYPE_NAME (container),
1128 else if (!(pspec->flags & G_PARAM_WRITABLE))
1129 g_warning ("%s: child property `%s' of container class `%s' is not writable",
1132 G_OBJECT_TYPE_NAME (container));
1135 container_set_child_property (container, child, pspec, value, nqueue);
1137 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
1138 g_object_unref (container);
1139 g_object_unref (child);
1143 * gtk_container_add_with_properties:
1144 * @container: a #GtkContainer
1145 * @widget: a widget to be placed inside @container
1146 * @first_prop_name: the name of the first child property to set
1147 * @Varargs: a %NULL-terminated list of property names and values, starting
1148 * with @first_prop_name
1150 * Adds @widget to @container, setting child properties at the same time.
1151 * See gtk_container_add() and gtk_container_child_set() for more details.
1154 gtk_container_add_with_properties (GtkContainer *container,
1156 const gchar *first_prop_name,
1159 g_return_if_fail (GTK_IS_CONTAINER (container));
1160 g_return_if_fail (GTK_IS_WIDGET (widget));
1161 g_return_if_fail (gtk_widget_get_parent (widget) == NULL);
1163 g_object_ref (container);
1164 g_object_ref (widget);
1165 gtk_widget_freeze_child_notify (widget);
1167 g_signal_emit (container, container_signals[ADD], 0, widget);
1168 if (gtk_widget_get_parent (widget))
1172 va_start (var_args, first_prop_name);
1173 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
1177 gtk_widget_thaw_child_notify (widget);
1178 g_object_unref (widget);
1179 g_object_unref (container);
1183 * gtk_container_child_set:
1184 * @container: a #GtkContainer
1185 * @child: a widget which is a child of @container
1186 * @first_prop_name: the name of the first property to set
1187 * @Varargs: a %NULL-terminated list of property names and values, starting
1188 * with @first_prop_name
1190 * Sets one or more child properties for @child and @container.
1193 gtk_container_child_set (GtkContainer *container,
1195 const gchar *first_prop_name,
1200 va_start (var_args, first_prop_name);
1201 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
1206 * gtk_container_child_get:
1207 * @container: a #GtkContainer
1208 * @child: a widget which is a child of @container
1209 * @first_prop_name: the name of the first property to get
1210 * @Varargs: return location for the first property, followed
1211 * optionally by more name/return location pairs, followed by %NULL
1213 * Gets the values of one or more child properties for @child and @container.
1216 gtk_container_child_get (GtkContainer *container,
1218 const gchar *first_prop_name,
1223 va_start (var_args, first_prop_name);
1224 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
1229 * gtk_container_class_install_child_property:
1230 * @cclass: a #GtkContainerClass
1231 * @property_id: the id for the property
1232 * @pspec: the #GParamSpec for the property
1234 * Installs a child property on a container class.
1237 gtk_container_class_install_child_property (GtkContainerClass *cclass,
1241 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
1242 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1243 if (pspec->flags & G_PARAM_WRITABLE)
1244 g_return_if_fail (cclass->set_child_property != NULL);
1245 if (pspec->flags & G_PARAM_READABLE)
1246 g_return_if_fail (cclass->get_child_property != NULL);
1247 g_return_if_fail (property_id > 0);
1248 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
1249 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1250 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
1252 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
1254 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
1255 G_OBJECT_CLASS_NAME (cclass),
1259 g_param_spec_ref (pspec);
1260 g_param_spec_sink (pspec);
1261 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
1262 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
1266 * gtk_container_class_find_child_property:
1267 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1268 * @property_name: the name of the child property to find
1269 * @returns: (transfer none): the #GParamSpec of the child property or
1270 * %NULL if @class has no child property with that name.
1272 * Finds a child property of a container class by name.
1275 gtk_container_class_find_child_property (GObjectClass *cclass,
1276 const gchar *property_name)
1278 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1279 g_return_val_if_fail (property_name != NULL, NULL);
1281 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1283 G_OBJECT_CLASS_TYPE (cclass),
1288 * gtk_container_class_list_child_properties:
1289 * @cclass: (type GtkContainerClass): a #GtkContainerClass
1290 * @n_properties: location to return the number of child properties found
1291 * @returns: (array length=n_properties) (transfer container): a newly
1292 * allocated %NULL-terminated array of #GParamSpec*. The
1293 * array must be freed with g_free().
1295 * Returns all child properties of a container class.
1298 gtk_container_class_list_child_properties (GObjectClass *cclass,
1299 guint *n_properties)
1301 GParamSpec **pspecs;
1304 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
1306 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
1307 G_OBJECT_CLASS_TYPE (cclass),
1316 gtk_container_add_unimplemented (GtkContainer *container,
1319 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1323 gtk_container_remove_unimplemented (GtkContainer *container,
1326 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
1330 gtk_container_init (GtkContainer *container)
1332 GtkContainerPrivate *priv;
1334 container->priv = G_TYPE_INSTANCE_GET_PRIVATE (container,
1336 GtkContainerPrivate);
1337 priv = container->priv;
1339 priv->focus_child = NULL;
1340 priv->border_width = 0;
1341 priv->need_resize = FALSE;
1342 priv->resize_mode = GTK_RESIZE_PARENT;
1343 priv->reallocate_redraws = FALSE;
1347 gtk_container_destroy (GtkWidget *widget)
1349 GtkContainer *container = GTK_CONTAINER (widget);
1350 GtkContainerPrivate *priv = container->priv;
1352 if (_gtk_widget_get_resize_pending (GTK_WIDGET (container)))
1353 _gtk_container_dequeue_resize_handler (container);
1355 if (priv->focus_child)
1357 g_object_unref (priv->focus_child);
1358 priv->focus_child = NULL;
1361 /* do this before walking child widgets, to avoid
1362 * removing children from focus chain one by one.
1364 if (priv->has_focus_chain)
1365 gtk_container_unset_focus_chain (container);
1367 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
1369 GTK_WIDGET_CLASS (parent_class)->destroy (widget);
1373 gtk_container_set_property (GObject *object,
1375 const GValue *value,
1378 GtkContainer *container = GTK_CONTAINER (object);
1382 case PROP_BORDER_WIDTH:
1383 gtk_container_set_border_width (container, g_value_get_uint (value));
1385 case PROP_RESIZE_MODE:
1386 gtk_container_set_resize_mode (container, g_value_get_enum (value));
1389 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
1392 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1398 gtk_container_get_property (GObject *object,
1403 GtkContainer *container = GTK_CONTAINER (object);
1404 GtkContainerPrivate *priv = container->priv;
1408 case PROP_BORDER_WIDTH:
1409 g_value_set_uint (value, priv->border_width);
1411 case PROP_RESIZE_MODE:
1412 g_value_set_enum (value, priv->resize_mode);
1415 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1421 * gtk_container_set_border_width:
1422 * @container: a #GtkContainer
1423 * @border_width: amount of blank space to leave <emphasis>outside</emphasis>
1424 * the container. Valid values are in the range 0-65535 pixels.
1426 * Sets the border width of the container.
1428 * The border width of a container is the amount of space to leave
1429 * around the outside of the container. The only exception to this is
1430 * #GtkWindow; because toplevel windows can't leave space outside,
1431 * they leave the space inside. The border is added on all sides of
1432 * the container. To add space to only one side, one approach is to
1433 * create a #GtkAlignment widget, call gtk_widget_set_size_request()
1434 * to give it a size, and place it on the side of the container as
1438 gtk_container_set_border_width (GtkContainer *container,
1441 GtkContainerPrivate *priv;
1443 g_return_if_fail (GTK_IS_CONTAINER (container));
1445 priv = container->priv;
1447 if (priv->border_width != border_width)
1449 priv->border_width = border_width;
1450 g_object_notify (G_OBJECT (container), "border-width");
1452 if (gtk_widget_get_realized (GTK_WIDGET (container)))
1453 gtk_widget_queue_resize (GTK_WIDGET (container));
1458 * gtk_container_get_border_width:
1459 * @container: a #GtkContainer
1461 * Retrieves the border width of the container. See
1462 * gtk_container_set_border_width().
1464 * Return value: the current border width
1467 gtk_container_get_border_width (GtkContainer *container)
1469 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
1471 return container->priv->border_width;
1475 * gtk_container_add:
1476 * @container: a #GtkContainer
1477 * @widget: a widget to be placed inside @container
1479 * Adds @widget to @container. Typically used for simple containers
1480 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
1481 * layout containers such as #GtkBox or #GtkTable, this function will
1482 * pick default packing parameters that may not be correct. So
1483 * consider functions such as gtk_box_pack_start() and
1484 * gtk_table_attach() as an alternative to gtk_container_add() in
1485 * those cases. A widget may be added to only one container at a time;
1486 * you can't place the same widget inside two different containers.
1489 gtk_container_add (GtkContainer *container,
1494 g_return_if_fail (GTK_IS_CONTAINER (container));
1495 g_return_if_fail (GTK_IS_WIDGET (widget));
1497 parent = gtk_widget_get_parent (widget);
1501 g_warning ("Attempting to add a widget with type %s to a container of "
1502 "type %s, but the widget is already inside a container of type %s, "
1503 "please use gtk_widget_reparent()" ,
1504 g_type_name (G_OBJECT_TYPE (widget)),
1505 g_type_name (G_OBJECT_TYPE (container)),
1506 g_type_name (G_OBJECT_TYPE (parent)));
1510 g_signal_emit (container, container_signals[ADD], 0, widget);
1514 * gtk_container_remove:
1515 * @container: a #GtkContainer
1516 * @widget: a current child of @container
1518 * Removes @widget from @container. @widget must be inside @container.
1519 * Note that @container will own a reference to @widget, and that this
1520 * may be the last reference held; so removing a widget from its
1521 * container can destroy that widget. If you want to use @widget
1522 * again, you need to add a reference to it while it's not inside
1523 * a container, using g_object_ref(). If you don't want to use @widget
1524 * again it's usually more efficient to simply destroy it directly
1525 * using gtk_widget_destroy() since this will remove it from the
1526 * container and help break any circular reference count cycles.
1529 gtk_container_remove (GtkContainer *container,
1532 g_return_if_fail (GTK_IS_CONTAINER (container));
1533 g_return_if_fail (GTK_IS_WIDGET (widget));
1534 g_return_if_fail (gtk_widget_get_parent (widget) == GTK_WIDGET (container));
1536 g_signal_emit (container, container_signals[REMOVE], 0, widget);
1540 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1542 g_return_if_fail (GTK_IS_CONTAINER (container));
1543 g_return_if_fail (_gtk_widget_get_resize_pending (GTK_WIDGET (container)));
1545 container_resize_queue = g_slist_remove (container_resize_queue, container);
1546 _gtk_widget_set_resize_pending (GTK_WIDGET (container), FALSE);
1550 * gtk_container_set_resize_mode:
1551 * @container: a #GtkContainer
1552 * @resize_mode: the new resize mode
1554 * Sets the resize mode for the container.
1556 * The resize mode of a container determines whether a resize request
1557 * will be passed to the container's parent, queued for later execution
1558 * or executed immediately.
1561 gtk_container_set_resize_mode (GtkContainer *container,
1562 GtkResizeMode resize_mode)
1564 GtkContainerPrivate *priv;
1566 g_return_if_fail (GTK_IS_CONTAINER (container));
1567 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1569 priv = container->priv;
1571 if (gtk_widget_is_toplevel (GTK_WIDGET (container)) &&
1572 resize_mode == GTK_RESIZE_PARENT)
1574 resize_mode = GTK_RESIZE_QUEUE;
1577 if (priv->resize_mode != resize_mode)
1579 priv->resize_mode = resize_mode;
1581 gtk_widget_queue_resize (GTK_WIDGET (container));
1582 g_object_notify (G_OBJECT (container), "resize-mode");
1587 * gtk_container_get_resize_mode:
1588 * @container: a #GtkContainer
1590 * Returns the resize mode for the container. See
1591 * gtk_container_set_resize_mode ().
1593 * Return value: the current resize mode
1596 gtk_container_get_resize_mode (GtkContainer *container)
1598 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1600 return container->priv->resize_mode;
1604 * gtk_container_set_reallocate_redraws:
1605 * @container: a #GtkContainer
1606 * @needs_redraws: the new value for the container's @reallocate_redraws flag
1608 * Sets the @reallocate_redraws flag of the container to the given value.
1610 * Containers requesting reallocation redraws get automatically
1611 * redrawn if any of their children changed allocation.
1614 gtk_container_set_reallocate_redraws (GtkContainer *container,
1615 gboolean needs_redraws)
1617 g_return_if_fail (GTK_IS_CONTAINER (container));
1619 container->priv->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1622 static GtkContainer*
1623 gtk_container_get_resize_container (GtkContainer *container)
1626 GtkWidget *widget = GTK_WIDGET (container);
1628 while ((parent = gtk_widget_get_parent (widget)))
1631 if (GTK_IS_RESIZE_CONTAINER (widget))
1635 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1639 gtk_container_idle_sizer (gpointer data)
1641 /* we may be invoked with a container_resize_queue of NULL, because
1642 * queue_resize could have been adding an extra idle function while
1643 * the queue still got processed. we better just ignore such case
1644 * than trying to explicitely work around them with some extra flags,
1645 * since it doesn't cause any actual harm.
1647 while (container_resize_queue)
1652 slist = container_resize_queue;
1653 container_resize_queue = slist->next;
1654 widget = slist->data;
1655 g_slist_free_1 (slist);
1657 _gtk_widget_set_resize_pending (widget, FALSE);
1658 gtk_container_check_resize (GTK_CONTAINER (widget));
1661 gdk_window_process_all_updates ();
1667 _gtk_container_queue_resize_internal (GtkContainer *container,
1668 gboolean invalidate_only)
1670 GtkContainer *resize_container;
1674 g_return_if_fail (GTK_IS_CONTAINER (container));
1676 widget = GTK_WIDGET (container);
1678 resize_container = gtk_container_get_resize_container (container);
1682 _gtk_widget_set_alloc_needed (widget, TRUE);
1683 _gtk_widget_set_width_request_needed (widget, TRUE);
1684 _gtk_widget_set_height_request_needed (widget, TRUE);
1686 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1687 !(parent = gtk_widget_get_parent (widget)))
1693 if (resize_container && !invalidate_only)
1695 if (gtk_widget_get_visible (GTK_WIDGET (resize_container)) &&
1696 (gtk_widget_is_toplevel (GTK_WIDGET (resize_container)) ||
1697 gtk_widget_get_realized (GTK_WIDGET (resize_container))))
1699 switch (resize_container->priv->resize_mode)
1701 case GTK_RESIZE_QUEUE:
1702 if (!_gtk_widget_get_resize_pending (GTK_WIDGET (resize_container)))
1704 _gtk_widget_set_resize_pending (GTK_WIDGET (resize_container), TRUE);
1705 if (container_resize_queue == NULL)
1706 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1707 gtk_container_idle_sizer,
1709 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1713 case GTK_RESIZE_IMMEDIATE:
1714 gtk_container_check_resize (resize_container);
1717 case GTK_RESIZE_PARENT:
1718 g_assert_not_reached ();
1724 /* we need to let hidden resize containers know that something
1725 * changed while they where hidden (currently only evaluated by
1728 resize_container->priv->need_resize = TRUE;
1734 * _gtk_container_queue_resize:
1735 * @container: a #GtkContainer
1737 * Determines the "resize container" in the hierarchy above this container
1738 * (typically the toplevel, but other containers can be set as resize
1739 * containers with gtk_container_set_resize_mode()), marks the container
1740 * and all parents up to and including the resize container as needing
1741 * to have sizes recompted, and if necessary adds the resize container
1742 * to the queue of containers that will be resized out at idle.
1745 _gtk_container_queue_resize (GtkContainer *container)
1747 _gtk_container_queue_resize_internal (container, FALSE);
1751 * _gtk_container_resize_invalidate:
1752 * @container: a #GtkContainer
1754 * Invalidates cached sizes like _gtk_container_queue_resize() but doesn't
1755 * actually queue the resize container for resize.
1758 _gtk_container_resize_invalidate (GtkContainer *container)
1760 _gtk_container_queue_resize_internal (container, TRUE);
1764 gtk_container_check_resize (GtkContainer *container)
1766 g_return_if_fail (GTK_IS_CONTAINER (container));
1768 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1772 gtk_container_real_check_resize (GtkContainer *container)
1774 GtkWidget *widget = GTK_WIDGET (container);
1775 GtkAllocation allocation;
1776 GtkRequisition requisition;
1778 gtk_widget_get_preferred_size (widget,
1779 &requisition, NULL);
1780 gtk_widget_get_allocation (widget, &allocation);
1782 if (requisition.width > allocation.width ||
1783 requisition.height > allocation.height)
1785 if (GTK_IS_RESIZE_CONTAINER (container))
1787 gtk_widget_size_allocate (widget, &allocation);
1788 gtk_widget_set_allocation (widget, &allocation);
1791 gtk_widget_queue_resize (widget);
1795 gtk_container_resize_children (container);
1799 /* The container hasn't changed size but one of its children
1800 * queued a resize request. Which means that the allocation
1801 * is not sufficient for the requisition of some child.
1802 * We've already performed a size request at this point,
1803 * so we simply need to reallocate and let the allocation
1804 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1807 gtk_container_resize_children (GtkContainer *container)
1809 GtkAllocation allocation;
1812 /* resizing invariants:
1813 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1814 * containers that have an idle sizer pending must be flagged with
1817 g_return_if_fail (GTK_IS_CONTAINER (container));
1819 widget = GTK_WIDGET (container);
1820 gtk_widget_get_allocation (widget, &allocation);
1822 gtk_widget_size_allocate (widget, &allocation);
1823 gtk_widget_set_allocation (widget, &allocation);
1827 gtk_container_adjust_size_request (GtkWidget *widget,
1828 GtkOrientation orientation,
1832 GtkContainer *container;
1834 container = GTK_CONTAINER (widget);
1836 if (GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1840 border_width = container->priv->border_width;
1842 *minimum_size += border_width * 2;
1843 *natural_size += border_width * 2;
1846 /* chain up last so gtk_widget_set_size_request() values
1847 * will have a chance to overwrite our border width.
1849 parent_class->adjust_size_request (widget, orientation,
1850 minimum_size, natural_size);
1854 gtk_container_adjust_size_allocation (GtkWidget *widget,
1855 GtkOrientation orientation,
1858 gint *allocated_pos,
1859 gint *allocated_size)
1861 GtkContainer *container;
1864 container = GTK_CONTAINER (widget);
1866 if (!GTK_CONTAINER_GET_CLASS (widget)->_handle_border_width)
1868 parent_class->adjust_size_allocation (widget, orientation,
1869 minimum_size, natural_size, allocated_pos,
1874 border_width = container->priv->border_width;
1876 *allocated_size -= border_width * 2;
1878 /* If we get a pathological too-small allocation to hold
1879 * even the border width, leave all allocation to the actual
1880 * widget, and leave x,y unchanged. (GtkWidget's min size is
1881 * 1x1 if you're wondering why <1 and not <0)
1883 * As long as we have space, set x,y properly.
1886 if (*allocated_size < 1)
1888 *allocated_size += border_width * 2;
1892 *allocated_pos += border_width;
1893 *minimum_size -= border_width * 2;
1894 *natural_size -= border_width * 2;
1897 /* Chain up to GtkWidgetClass *after* removing our border width from
1898 * the proposed allocation size. This is because it's possible that the
1899 * widget was allocated more space than it needs in a said orientation,
1900 * if GtkWidgetClass does any alignments and thus limits the size to the
1901 * natural size... then we need that to be done *after* removing any margins
1902 * and padding values.
1904 parent_class->adjust_size_allocation (widget, orientation,
1905 minimum_size, natural_size, allocated_pos,
1915 count_request_modes (GtkWidget *widget,
1916 RequestModeCount *count)
1918 GtkSizeRequestMode mode = gtk_widget_get_request_mode (widget);
1922 case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
1925 case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
1928 case GTK_SIZE_REQUEST_CONSTANT_SIZE:
1934 static GtkSizeRequestMode
1935 gtk_container_get_request_mode (GtkWidget *widget)
1937 GtkContainer *container = GTK_CONTAINER (widget);
1938 GtkContainerPrivate *priv = container->priv;
1940 /* Recalculate the request mode of the children by majority
1941 * vote whenever the internal content changes */
1942 if (_gtk_widget_get_width_request_needed (widget) ||
1943 _gtk_widget_get_height_request_needed (widget))
1945 RequestModeCount count = { 0, 0 };
1947 gtk_container_forall (container, (GtkCallback)count_request_modes, &count);
1949 if (!count.hfw && !count.wfh)
1950 priv->request_mode = GTK_SIZE_REQUEST_CONSTANT_SIZE;
1952 priv->request_mode = count.wfh > count.hfw ?
1953 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
1954 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1957 return priv->request_mode;
1961 * gtk_container_class_handle_border_width:
1962 * @klass: the class struct of a #GtkContainer subclass
1964 * Modifies a subclass of #GtkContainerClass to automatically add and
1965 * remove the border-width setting on GtkContainer. This allows the
1966 * subclass to ignore the border width in its size request and
1967 * allocate methods. The intent is for a subclass to invoke this
1968 * in its class_init function.
1970 * gtk_container_class_handle_border_width() is necessary because it
1971 * would break API too badly to make this behavior the default. So
1972 * subclasses must "opt in" to the parent class handling border_width
1976 gtk_container_class_handle_border_width (GtkContainerClass *klass)
1978 g_return_if_fail (GTK_IS_CONTAINER_CLASS (klass));
1980 klass->_handle_border_width = TRUE;
1984 * gtk_container_forall:
1985 * @container: a #GtkContainer
1986 * @callback: (scope call) (closure callback_data): a callback
1987 * @callback_data: callback user data
1989 * Invokes @callback on each child of @container, including children
1990 * that are considered "internal" (implementation details of the
1991 * container). "Internal" children generally weren't added by the user
1992 * of the container, but were added by the container implementation
1993 * itself. Most applications should use gtk_container_foreach(),
1994 * rather than gtk_container_forall().
1999 gtk_container_forall (GtkContainer *container,
2000 GtkCallback callback,
2001 gpointer callback_data)
2003 GtkContainerClass *class;
2005 g_return_if_fail (GTK_IS_CONTAINER (container));
2006 g_return_if_fail (callback != NULL);
2008 class = GTK_CONTAINER_GET_CLASS (container);
2011 class->forall (container, TRUE, callback, callback_data);
2015 * gtk_container_foreach:
2016 * @container: a #GtkContainer
2017 * @callback: (scope call): a callback
2018 * @callback_data: callback user data
2020 * Invokes @callback on each non-internal child of @container. See
2021 * gtk_container_forall() for details on what constitutes an
2022 * "internal" child. Most applications should use
2023 * gtk_container_foreach(), rather than gtk_container_forall().
2026 gtk_container_foreach (GtkContainer *container,
2027 GtkCallback callback,
2028 gpointer callback_data)
2030 GtkContainerClass *class;
2032 g_return_if_fail (GTK_IS_CONTAINER (container));
2033 g_return_if_fail (callback != NULL);
2035 class = GTK_CONTAINER_GET_CLASS (container);
2038 class->forall (container, FALSE, callback, callback_data);
2042 * gtk_container_set_focus_child:
2043 * @container: a #GtkContainer
2044 * @child: (allow-none): a #GtkWidget, or %NULL
2046 * Sets, or unsets if @child is %NULL, the focused child of @container.
2048 * This function emits the GtkContainer::set_focus_child signal of
2049 * @container. Implementations of #GtkContainer can override the
2050 * default behaviour by overriding the class closure of this signal.
2052 * This is function is mostly meant to be used by widgets. Applications can use
2053 * gtk_widget_grab_focus() to manualy set the focus to a specific widget.
2056 gtk_container_set_focus_child (GtkContainer *container,
2059 g_return_if_fail (GTK_IS_CONTAINER (container));
2061 g_return_if_fail (GTK_IS_WIDGET (child));
2063 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, child);
2067 * gtk_container_get_focus_child:
2068 * @container: a #GtkContainer
2070 * Returns the current focus child widget inside @container. This is not the
2071 * currently focused widget. That can be obtained by calling
2072 * gtk_window_get_focus().
2074 * Returns: (transfer none): The child widget which will receive the
2075 * focus inside @container when the @conatiner is focussed,
2076 * or %NULL if none is set.
2081 gtk_container_get_focus_child (GtkContainer *container)
2083 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2085 return container->priv->focus_child;
2089 * gtk_container_get_children:
2090 * @container: a #GtkContainer
2092 * Returns the container's non-internal children. See
2093 * gtk_container_forall() for details on what constitutes an "internal" child.
2095 * Return value: (element-type GtkWidget) (transfer container): a newly-allocated list of the container's non-internal children.
2098 gtk_container_get_children (GtkContainer *container)
2100 GList *children = NULL;
2102 gtk_container_foreach (container,
2103 gtk_container_children_callback,
2106 return g_list_reverse (children);
2110 gtk_container_child_position_callback (GtkWidget *widget,
2111 gpointer client_data)
2117 } *data = client_data;
2120 if (data->child == widget)
2121 data->index = data->i;
2125 gtk_container_child_default_composite_name (GtkContainer *container,
2135 /* fallback implementation */
2139 gtk_container_forall (container,
2140 gtk_container_child_position_callback,
2143 name = g_strdup_printf ("%s-%u",
2144 g_type_name (G_TYPE_FROM_INSTANCE (child)),
2151 _gtk_container_child_composite_name (GtkContainer *container,
2154 gboolean composite_child;
2156 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2157 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
2158 g_return_val_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (container), NULL);
2160 g_object_get (child, "composite-child", &composite_child, NULL);
2161 if (composite_child)
2163 static GQuark quark_composite_name = 0;
2166 if (!quark_composite_name)
2167 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
2169 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
2172 GtkContainerClass *class;
2174 class = GTK_CONTAINER_GET_CLASS (container);
2175 if (class->composite_name)
2176 name = class->composite_name (container, child);
2179 name = g_strdup (name);
2190 } ComputeExpandData;
2193 gtk_container_compute_expand_callback (GtkWidget *widget,
2194 gpointer client_data)
2196 ComputeExpandData *data = client_data;
2198 /* note that we don't get_expand on the child if we already know we
2199 * have to expand, so we only recurse into children until we find
2200 * one that expands and then we basically don't do any more
2201 * work. This means that we can leave some children in a
2202 * need_compute_expand state, which is fine, as long as GtkWidget
2203 * doesn't rely on an invariant that "if a child has
2204 * need_compute_expand, its parents also do"
2206 * gtk_widget_compute_expand() always returns FALSE if the
2207 * child is !visible so that's taken care of.
2209 data->hexpand = data->hexpand ||
2210 gtk_widget_compute_expand (widget, GTK_ORIENTATION_HORIZONTAL);
2212 data->vexpand = data->vexpand ||
2213 gtk_widget_compute_expand (widget, GTK_ORIENTATION_VERTICAL);
2217 gtk_container_compute_expand (GtkWidget *widget,
2218 gboolean *hexpand_p,
2219 gboolean *vexpand_p)
2221 ComputeExpandData data;
2223 data.hexpand = FALSE;
2224 data.vexpand = FALSE;
2226 gtk_container_forall (GTK_CONTAINER (widget),
2227 gtk_container_compute_expand_callback,
2230 *hexpand_p = data.hexpand;
2231 *vexpand_p = data.vexpand;
2235 gtk_container_real_set_focus_child (GtkContainer *container,
2238 GtkContainerPrivate *priv;
2240 g_return_if_fail (GTK_IS_CONTAINER (container));
2241 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
2243 priv = container->priv;
2245 if (child != priv->focus_child)
2247 if (priv->focus_child)
2248 g_object_unref (priv->focus_child);
2249 priv->focus_child = child;
2250 if (priv->focus_child)
2251 g_object_ref (priv->focus_child);
2255 /* check for h/v adjustments
2257 if (priv->focus_child)
2259 GtkAdjustment *hadj;
2260 GtkAdjustment *vadj;
2261 GtkAllocation allocation;
2262 GtkWidget *focus_child;
2265 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2266 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2270 focus_child = priv->focus_child;
2271 while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
2273 focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
2276 gtk_widget_translate_coordinates (focus_child, priv->focus_child,
2279 gtk_widget_get_allocation (priv->focus_child, &allocation);
2283 gtk_widget_get_allocation (focus_child, &allocation);
2286 gtk_adjustment_clamp_page (vadj, y, y + allocation.height);
2289 gtk_adjustment_clamp_page (hadj, x, x + allocation.width);
2295 get_focus_chain (GtkContainer *container)
2297 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
2300 /* same as gtk_container_get_children, except it includes internals
2303 gtk_container_get_all_children (GtkContainer *container)
2305 GList *children = NULL;
2307 gtk_container_forall (container,
2308 gtk_container_children_callback,
2314 static GtkWidgetPath *
2315 gtk_container_real_get_path_for_child (GtkContainer *container,
2318 GtkStyleContext *context;
2319 GtkWidgetPath *path;
2322 context = gtk_widget_get_style_context (GTK_WIDGET (container));
2323 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
2325 /* Copy any permanent classes to the path */
2326 classes = gtk_style_context_list_classes (context);
2333 classes = classes->next;
2335 gtk_widget_path_iter_add_class (path, -1, cur->data);
2336 g_list_free_1 (cur);
2339 gtk_widget_path_append_for_widget (path, child);
2345 gtk_container_focus (GtkWidget *widget,
2346 GtkDirectionType direction)
2349 GList *sorted_children;
2351 GtkContainer *container;
2352 GtkContainerPrivate *priv;
2354 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2356 container = GTK_CONTAINER (widget);
2357 priv = container->priv;
2361 if (gtk_widget_get_can_focus (widget))
2363 if (!gtk_widget_has_focus (widget))
2365 gtk_widget_grab_focus (widget);
2371 /* Get a list of the containers children, allowing focus
2372 * chain to override.
2374 if (priv->has_focus_chain)
2375 children = g_list_copy (get_focus_chain (container));
2377 children = gtk_container_get_all_children (container);
2379 if (priv->has_focus_chain &&
2380 (direction == GTK_DIR_TAB_FORWARD ||
2381 direction == GTK_DIR_TAB_BACKWARD))
2383 sorted_children = g_list_copy (children);
2385 if (direction == GTK_DIR_TAB_BACKWARD)
2386 sorted_children = g_list_reverse (sorted_children);
2389 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
2391 return_val = gtk_container_focus_move (container, sorted_children, direction);
2393 g_list_free (sorted_children);
2394 g_list_free (children);
2401 tab_compare (gconstpointer a,
2405 GtkAllocation child1_allocation, child2_allocation;
2406 const GtkWidget *child1 = a;
2407 const GtkWidget *child2 = b;
2408 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
2411 gtk_widget_get_allocation ((GtkWidget *) child1, &child1_allocation);
2412 gtk_widget_get_allocation ((GtkWidget *) child2, &child2_allocation);
2414 y1 = child1_allocation.y + child1_allocation.height / 2;
2415 y2 = child2_allocation.y + child2_allocation.height / 2;
2419 gint x1 = child1_allocation.x + child1_allocation.width / 2;
2420 gint x2 = child2_allocation.x + child2_allocation.width / 2;
2422 if (text_direction == GTK_TEXT_DIR_RTL)
2423 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2425 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2428 return (y1 < y2) ? -1 : 1;
2432 gtk_container_focus_sort_tab (GtkContainer *container,
2434 GtkDirectionType direction,
2435 GtkWidget *old_focus)
2437 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
2438 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
2440 /* if we are going backwards then reverse the order
2443 if (direction == GTK_DIR_TAB_BACKWARD)
2444 children = g_list_reverse (children);
2449 /* Get coordinates of @widget's allocation with respect to
2450 * allocation of @container.
2453 get_allocation_coords (GtkContainer *container,
2455 GdkRectangle *allocation)
2457 gtk_widget_get_allocation (widget, allocation);
2459 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
2460 0, 0, &allocation->x, &allocation->y);
2463 /* Look for a child in @children that is intermediate between
2464 * the focus widget and container. This widget, if it exists,
2465 * acts as the starting widget for focus navigation.
2468 find_old_focus (GtkContainer *container,
2471 GList *tmp_list = children;
2474 GtkWidget *child = tmp_list->data;
2475 GtkWidget *widget = child;
2477 while (widget && widget != (GtkWidget *)container)
2481 parent = gtk_widget_get_parent (widget);
2483 if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
2492 tmp_list = tmp_list->next;
2499 old_focus_coords (GtkContainer *container,
2500 GdkRectangle *old_focus_rect)
2502 GtkWidget *widget = GTK_WIDGET (container);
2503 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2504 GtkWidget *old_focus;
2506 if (GTK_IS_WINDOW (toplevel))
2508 old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
2510 return get_allocation_coords (container, old_focus, old_focus_rect);
2516 typedef struct _CompareInfo CompareInfo;
2520 GtkContainer *container;
2527 up_down_compare (gconstpointer a,
2531 GdkRectangle allocation1;
2532 GdkRectangle allocation2;
2533 CompareInfo *compare = data;
2536 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2537 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2539 y1 = allocation1.y + allocation1.height / 2;
2540 y2 = allocation2.y + allocation2.height / 2;
2544 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
2545 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
2547 if (compare->reverse)
2548 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
2550 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
2553 return (y1 < y2) ? -1 : 1;
2557 gtk_container_focus_sort_up_down (GtkContainer *container,
2559 GtkDirectionType direction,
2560 GtkWidget *old_focus)
2562 CompareInfo compare;
2564 GdkRectangle old_allocation;
2566 compare.container = container;
2567 compare.reverse = (direction == GTK_DIR_UP);
2570 old_focus = find_old_focus (container, children);
2572 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2578 /* Delete widgets from list that don't match minimum criteria */
2580 compare_x1 = old_allocation.x;
2581 compare_x2 = old_allocation.x + old_allocation.width;
2583 if (direction == GTK_DIR_UP)
2584 compare_y = old_allocation.y;
2586 compare_y = old_allocation.y + old_allocation.height;
2588 tmp_list = children;
2591 GtkWidget *child = tmp_list->data;
2592 GList *next = tmp_list->next;
2593 gint child_x1, child_x2;
2594 GdkRectangle child_allocation;
2596 if (child != old_focus)
2598 if (get_allocation_coords (container, child, &child_allocation))
2600 child_x1 = child_allocation.x;
2601 child_x2 = child_allocation.x + child_allocation.width;
2603 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
2604 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
2605 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
2607 children = g_list_delete_link (children, tmp_list);
2611 children = g_list_delete_link (children, tmp_list);
2617 compare.x = (compare_x1 + compare_x2) / 2;
2618 compare.y = old_allocation.y + old_allocation.height / 2;
2622 /* No old focus widget, need to figure out starting x,y some other way
2624 GtkAllocation allocation;
2625 GtkWidget *widget = GTK_WIDGET (container);
2626 GdkRectangle old_focus_rect;
2628 gtk_widget_get_allocation (widget, &allocation);
2630 if (old_focus_coords (container, &old_focus_rect))
2632 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
2636 if (!gtk_widget_get_has_window (widget))
2637 compare.x = allocation.x + allocation.width / 2;
2639 compare.x = allocation.width / 2;
2642 if (!gtk_widget_get_has_window (widget))
2643 compare.y = (direction == GTK_DIR_DOWN) ? allocation.y : allocation.y + allocation.height;
2645 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + allocation.height;
2648 children = g_list_sort_with_data (children, up_down_compare, &compare);
2650 if (compare.reverse)
2651 children = g_list_reverse (children);
2657 left_right_compare (gconstpointer a,
2661 GdkRectangle allocation1;
2662 GdkRectangle allocation2;
2663 CompareInfo *compare = data;
2666 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
2667 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
2669 x1 = allocation1.x + allocation1.width / 2;
2670 x2 = allocation2.x + allocation2.width / 2;
2674 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
2675 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
2677 if (compare->reverse)
2678 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
2680 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
2683 return (x1 < x2) ? -1 : 1;
2687 gtk_container_focus_sort_left_right (GtkContainer *container,
2689 GtkDirectionType direction,
2690 GtkWidget *old_focus)
2692 CompareInfo compare;
2694 GdkRectangle old_allocation;
2696 compare.container = container;
2697 compare.reverse = (direction == GTK_DIR_LEFT);
2700 old_focus = find_old_focus (container, children);
2702 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
2708 /* Delete widgets from list that don't match minimum criteria */
2710 compare_y1 = old_allocation.y;
2711 compare_y2 = old_allocation.y + old_allocation.height;
2713 if (direction == GTK_DIR_LEFT)
2714 compare_x = old_allocation.x;
2716 compare_x = old_allocation.x + old_allocation.width;
2718 tmp_list = children;
2721 GtkWidget *child = tmp_list->data;
2722 GList *next = tmp_list->next;
2723 gint child_y1, child_y2;
2724 GdkRectangle child_allocation;
2726 if (child != old_focus)
2728 if (get_allocation_coords (container, child, &child_allocation))
2730 child_y1 = child_allocation.y;
2731 child_y2 = child_allocation.y + child_allocation.height;
2733 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
2734 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
2735 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
2737 children = g_list_delete_link (children, tmp_list);
2741 children = g_list_delete_link (children, tmp_list);
2747 compare.y = (compare_y1 + compare_y2) / 2;
2748 compare.x = old_allocation.x + old_allocation.width / 2;
2752 /* No old focus widget, need to figure out starting x,y some other way
2754 GtkAllocation allocation;
2755 GtkWidget *widget = GTK_WIDGET (container);
2756 GdkRectangle old_focus_rect;
2758 gtk_widget_get_allocation (widget, &allocation);
2760 if (old_focus_coords (container, &old_focus_rect))
2762 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
2766 if (!gtk_widget_get_has_window (widget))
2767 compare.y = allocation.y + allocation.height / 2;
2769 compare.y = allocation.height / 2;
2772 if (!gtk_widget_get_has_window (widget))
2773 compare.x = (direction == GTK_DIR_RIGHT) ? allocation.x : allocation.x + allocation.width;
2775 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : allocation.width;
2778 children = g_list_sort_with_data (children, left_right_compare, &compare);
2780 if (compare.reverse)
2781 children = g_list_reverse (children);
2787 * gtk_container_focus_sort:
2788 * @container: a #GtkContainer
2789 * @children: a list of descendents of @container (they don't
2790 * have to be direct children)
2791 * @direction: focus direction
2792 * @old_focus: (allow-none): widget to use for the starting position, or %NULL
2793 * to determine this automatically.
2794 * (Note, this argument isn't used for GTK_DIR_TAB_*,
2795 * which is the only @direction we use currently,
2796 * so perhaps this argument should be removed)
2798 * Sorts @children in the correct order for focusing with
2799 * direction type @direction.
2801 * Return value: a copy of @children, sorted in correct focusing order,
2802 * with children that aren't suitable for focusing in this direction
2806 _gtk_container_focus_sort (GtkContainer *container,
2808 GtkDirectionType direction,
2809 GtkWidget *old_focus)
2811 GList *visible_children = NULL;
2815 if (gtk_widget_get_realized (children->data))
2816 visible_children = g_list_prepend (visible_children, children->data);
2817 children = children->next;
2822 case GTK_DIR_TAB_FORWARD:
2823 case GTK_DIR_TAB_BACKWARD:
2824 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
2827 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
2830 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
2833 g_assert_not_reached ();
2839 gtk_container_focus_move (GtkContainer *container,
2841 GtkDirectionType direction)
2843 GtkContainerPrivate *priv = container->priv;
2844 GtkWidget *focus_child;
2847 focus_child = priv->focus_child;
2851 child = children->data;
2852 children = children->next;
2859 if (focus_child == child)
2863 if (gtk_widget_child_focus (child, direction))
2867 else if (gtk_widget_is_drawable (child) &&
2868 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2870 if (gtk_widget_child_focus (child, direction))
2880 gtk_container_children_callback (GtkWidget *widget,
2881 gpointer client_data)
2885 children = (GList**) client_data;
2886 *children = g_list_prepend (*children, widget);
2890 chain_widget_destroyed (GtkWidget *widget,
2893 GtkContainer *container;
2896 container = GTK_CONTAINER (user_data);
2898 chain = g_object_get_data (G_OBJECT (container),
2899 "gtk-container-focus-chain");
2901 chain = g_list_remove (chain, widget);
2903 g_signal_handlers_disconnect_by_func (widget,
2904 chain_widget_destroyed,
2907 g_object_set_data (G_OBJECT (container),
2908 I_("gtk-container-focus-chain"),
2913 * gtk_container_set_focus_chain:
2914 * @container: a #GtkContainer
2915 * @focusable_widgets: (transfer none) (element-type GtkWidget):
2916 * the new focus chain
2918 * Sets a focus chain, overriding the one computed automatically by GTK+.
2920 * In principle each widget in the chain should be a descendant of the
2921 * container, but this is not enforced by this method, since it's allowed
2922 * to set the focus chain before you pack the widgets, or have a widget
2923 * in the chain that isn't always packed. The necessary checks are done
2924 * when the focus chain is actually traversed.
2927 gtk_container_set_focus_chain (GtkContainer *container,
2928 GList *focusable_widgets)
2932 GtkContainerPrivate *priv;
2934 g_return_if_fail (GTK_IS_CONTAINER (container));
2936 priv = container->priv;
2938 if (priv->has_focus_chain)
2939 gtk_container_unset_focus_chain (container);
2941 priv->has_focus_chain = TRUE;
2944 tmp_list = focusable_widgets;
2945 while (tmp_list != NULL)
2947 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2949 /* In principle each widget in the chain should be a descendant
2950 * of the container, but we don't want to check that here. It's
2951 * expensive and also it's allowed to set the focus chain before
2952 * you pack the widgets, or have a widget in the chain that isn't
2953 * always packed. So we check for ancestor during actual traversal.
2956 chain = g_list_prepend (chain, tmp_list->data);
2958 g_signal_connect (tmp_list->data,
2960 G_CALLBACK (chain_widget_destroyed),
2963 tmp_list = g_list_next (tmp_list);
2966 chain = g_list_reverse (chain);
2968 g_object_set_data (G_OBJECT (container),
2969 I_("gtk-container-focus-chain"),
2974 * gtk_container_get_focus_chain:
2975 * @container: a #GtkContainer
2976 * @focusable_widgets: (element-type GtkWidget) (out) (transfer container): location
2977 * to store the focus chain of the
2978 * container, or %NULL. You should free this list
2979 * using g_list_free() when you are done with it, however
2980 * no additional reference count is added to the
2981 * individual widgets in the focus chain.
2983 * Retrieves the focus chain of the container, if one has been
2984 * set explicitly. If no focus chain has been explicitly
2985 * set, GTK+ computes the focus chain based on the positions
2986 * of the children. In that case, GTK+ stores %NULL in
2987 * @focusable_widgets and returns %FALSE.
2989 * Return value: %TRUE if the focus chain of the container
2990 * has been set explicitly.
2993 gtk_container_get_focus_chain (GtkContainer *container,
2994 GList **focus_chain)
2996 GtkContainerPrivate *priv;
2998 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
3000 priv = container->priv;
3004 if (priv->has_focus_chain)
3005 *focus_chain = g_list_copy (get_focus_chain (container));
3007 *focus_chain = NULL;
3010 return priv->has_focus_chain;
3014 * gtk_container_unset_focus_chain:
3015 * @container: a #GtkContainer
3017 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
3020 gtk_container_unset_focus_chain (GtkContainer *container)
3022 GtkContainerPrivate *priv;
3024 g_return_if_fail (GTK_IS_CONTAINER (container));
3026 priv = container->priv;
3028 if (priv->has_focus_chain)
3033 chain = get_focus_chain (container);
3035 priv->has_focus_chain = FALSE;
3037 g_object_set_data (G_OBJECT (container),
3038 I_("gtk-container-focus-chain"),
3042 while (tmp_list != NULL)
3044 g_signal_handlers_disconnect_by_func (tmp_list->data,
3045 chain_widget_destroyed,
3048 tmp_list = g_list_next (tmp_list);
3051 g_list_free (chain);
3056 * gtk_container_set_focus_vadjustment:
3057 * @container: a #GtkContainer
3058 * @adjustment: an adjustment which should be adjusted when the focus
3059 * is moved among the descendents of @container
3061 * Hooks up an adjustment to focus handling in a container, so when a
3062 * child of the container is focused, the adjustment is scrolled to
3063 * show that widget. This function sets the vertical alignment. See
3064 * gtk_scrolled_window_get_vadjustment() for a typical way of obtaining
3065 * the adjustment and gtk_container_set_focus_hadjustment() for setting
3066 * the horizontal adjustment.
3068 * The adjustments have to be in pixel units and in the same coordinate
3069 * system as the allocation for immediate children of the container.
3072 gtk_container_set_focus_vadjustment (GtkContainer *container,
3073 GtkAdjustment *adjustment)
3075 g_return_if_fail (GTK_IS_CONTAINER (container));
3077 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3080 g_object_ref (adjustment);
3082 g_object_set_qdata_full (G_OBJECT (container),
3089 * gtk_container_get_focus_vadjustment:
3090 * @container: a #GtkContainer
3092 * Retrieves the vertical focus adjustment for the container. See
3093 * gtk_container_set_focus_vadjustment().
3095 * Return value: (transfer none): the vertical focus adjustment, or %NULL if
3096 * none has been set.
3099 gtk_container_get_focus_vadjustment (GtkContainer *container)
3101 GtkAdjustment *vadjustment;
3103 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3105 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
3111 * gtk_container_set_focus_hadjustment:
3112 * @container: a #GtkContainer
3113 * @adjustment: an adjustment which should be adjusted when the focus is
3114 * moved among the descendents of @container
3116 * Hooks up an adjustment to focus handling in a container, so when a child
3117 * of the container is focused, the adjustment is scrolled to show that
3118 * widget. This function sets the horizontal alignment.
3119 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
3120 * the adjustment and gtk_container_set_focus_vadjustment() for setting
3121 * the vertical adjustment.
3123 * The adjustments have to be in pixel units and in the same coordinate
3124 * system as the allocation for immediate children of the container.
3127 gtk_container_set_focus_hadjustment (GtkContainer *container,
3128 GtkAdjustment *adjustment)
3130 g_return_if_fail (GTK_IS_CONTAINER (container));
3132 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
3135 g_object_ref (adjustment);
3137 g_object_set_qdata_full (G_OBJECT (container),
3144 * gtk_container_get_focus_hadjustment:
3145 * @container: a #GtkContainer
3147 * Retrieves the horizontal focus adjustment for the container. See
3148 * gtk_container_set_focus_hadjustment ().
3150 * Return value: (transfer none): the horizontal focus adjustment, or %NULL if
3151 * none has been set.
3154 gtk_container_get_focus_hadjustment (GtkContainer *container)
3156 GtkAdjustment *hadjustment;
3158 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3160 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
3167 gtk_container_show_all (GtkWidget *widget)
3169 g_return_if_fail (GTK_IS_CONTAINER (widget));
3171 gtk_container_foreach (GTK_CONTAINER (widget),
3172 (GtkCallback) gtk_widget_show_all,
3174 gtk_widget_show (widget);
3178 gtk_container_draw_child (GtkWidget *child,
3179 gpointer client_data)
3182 GtkWidget *container;
3184 } *data = client_data;
3186 gtk_container_propagate_draw (GTK_CONTAINER (data->container),
3192 gtk_container_draw (GtkWidget *widget,
3196 GtkWidget *container;
3200 data.container = widget;
3203 gtk_container_forall (GTK_CONTAINER (widget),
3204 gtk_container_draw_child,
3211 gtk_container_map_child (GtkWidget *child,
3212 gpointer client_data)
3214 if (gtk_widget_get_visible (child) &&
3215 gtk_widget_get_child_visible (child) &&
3216 !gtk_widget_get_mapped (child))
3217 gtk_widget_map (child);
3221 gtk_container_map (GtkWidget *widget)
3223 gtk_widget_set_mapped (widget, TRUE);
3225 gtk_container_forall (GTK_CONTAINER (widget),
3226 gtk_container_map_child,
3229 if (gtk_widget_get_has_window (widget))
3230 gdk_window_show (gtk_widget_get_window (widget));
3234 gtk_container_unmap (GtkWidget *widget)
3236 gtk_widget_set_mapped (widget, FALSE);
3238 /* hide our window first so user doesn't see all the child windows
3239 * vanishing one by one. (only matters these days if one of the
3240 * children has an actual native window instead of client-side
3241 * window, e.g. a GtkSocket would)
3243 if (gtk_widget_get_has_window (widget))
3244 gdk_window_hide (gtk_widget_get_window (widget));
3246 gtk_container_forall (GTK_CONTAINER (widget),
3247 (GtkCallback)gtk_widget_unmap,
3252 * gtk_container_propagate_draw:
3253 * @container: a #GtkContainer
3254 * @child: a child of @container
3255 * @cr: Cairo context as passed to the container. If you want to use @cr
3256 * in container's draw function, consider using cairo_save() and
3257 * cairo_restore() before calling this function.
3259 * When a container receives a call to the draw function, it must send
3260 * synthetic #GtkWidget::draw calls to all children that don't have their
3261 * own #GdkWindows. This function provides a convenient way of doing this.
3262 * A container, when it receives a call to its #GtkWidget::draw function,
3263 * calls gtk_container_propagate_draw() once for each child, passing in
3264 * the @cr the container received.
3266 * gtk_container_propagate_draw() takes care of translating the origin of @cr,
3267 * and deciding whether the draw needs to be sent to the child. It is a
3268 * convenient and optimized way of getting the same effect as calling
3269 * gtk_widget_draw() on the child directly.
3271 * In most cases, a container can simply either inherit the
3272 * #GtkWidget::draw implementation from #GtkContainer, or do some drawing
3273 * and then chain to the ::draw implementation from #GtkContainer.
3276 gtk_container_propagate_draw (GtkContainer *container,
3280 GdkEventExpose *event;
3281 GtkAllocation allocation;
3282 GdkWindow *window, *w;
3285 g_return_if_fail (GTK_IS_CONTAINER (container));
3286 g_return_if_fail (GTK_IS_WIDGET (child));
3287 g_return_if_fail (cr != NULL);
3289 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (container));
3291 event = _gtk_cairo_get_event (cr);
3294 if (gtk_widget_get_has_window (child) ||
3295 gtk_widget_get_window (child) != event->window)
3301 /* translate coordinates. Ugly business, that. */
3302 if (!gtk_widget_get_has_window (GTK_WIDGET (container)))
3304 gtk_widget_get_allocation (GTK_WIDGET (container), &allocation);
3314 window = gtk_widget_get_window (GTK_WIDGET (container));
3316 for (w = gtk_widget_get_window (child); w && w != window; w = gdk_window_get_parent (w))
3319 gdk_window_get_position (w, &wx, &wy);
3330 if (!gtk_widget_get_has_window (child))
3332 gtk_widget_get_allocation (child, &allocation);
3337 cairo_translate (cr, x, y);
3339 _gtk_widget_draw_internal (child, cr, TRUE);
3345 _gtk_container_get_need_resize (GtkContainer *container)
3347 return container->priv->need_resize;
3351 _gtk_container_set_need_resize (GtkContainer *container,
3352 gboolean need_resize)
3354 container->priv->need_resize = need_resize;
3358 _gtk_container_get_reallocate_redraws (GtkContainer *container)
3360 return container->priv->reallocate_redraws;
3364 * gtk_container_get_path_for_child:
3365 * @container: a #GtkContainer
3366 * @child: a child of @container
3368 * Returns a newly created widget path representing all the widget hierarchy
3369 * from the toplevel down to and including @child.
3371 * Returns: A newly created #GtkWidgetPath
3374 gtk_container_get_path_for_child (GtkContainer *container,
3377 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
3378 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
3379 g_return_val_if_fail (container == (GtkContainer *) gtk_widget_get_parent (child), NULL);
3381 return GTK_CONTAINER_GET_CLASS (container)->get_path_for_child (container, child);