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/.
33 #include "gtkcontainer.h"
34 #include "gtkprivate.h"
36 #include "gtkmarshalers.h"
37 #include "gtkwindow.h"
39 #include "gtktoolbar.h"
40 #include <gobject/gobjectnotifyqueue.c>
41 #include <gobject/gvaluecollector.h>
59 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
60 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
63 /* --- prototypes --- */
64 static void gtk_container_base_class_init (GtkContainerClass *klass);
65 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
66 static void gtk_container_class_init (GtkContainerClass *klass);
67 static void gtk_container_init (GtkContainer *container);
68 static void gtk_container_destroy (GtkObject *object);
69 static void gtk_container_set_property (GObject *object,
73 static void gtk_container_get_property (GObject *object,
77 static void gtk_container_add_unimplemented (GtkContainer *container,
79 static void gtk_container_remove_unimplemented (GtkContainer *container,
81 static void gtk_container_real_check_resize (GtkContainer *container);
82 static gboolean gtk_container_focus (GtkWidget *widget,
83 GtkDirectionType direction);
84 static void gtk_container_real_set_focus_child (GtkContainer *container,
87 static gboolean gtk_container_focus_move (GtkContainer *container,
89 GtkDirectionType direction);
90 static void gtk_container_children_callback (GtkWidget *widget,
91 gpointer client_data);
92 static void gtk_container_show_all (GtkWidget *widget);
93 static void gtk_container_hide_all (GtkWidget *widget);
94 static gint gtk_container_expose (GtkWidget *widget,
95 GdkEventExpose *event);
96 static void gtk_container_map (GtkWidget *widget);
97 static void gtk_container_unmap (GtkWidget *widget);
99 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
103 /* --- variables --- */
104 static const gchar *vadjustment_key = "gtk-vadjustment";
105 static guint vadjustment_key_id = 0;
106 static const gchar *hadjustment_key = "gtk-hadjustment";
107 static guint hadjustment_key_id = 0;
108 static GSList *container_resize_queue = NULL;
109 static guint container_signals[LAST_SIGNAL] = { 0 };
110 static GtkWidgetClass *parent_class = NULL;
111 extern GParamSpecPool *_gtk_widget_child_property_pool;
112 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
115 /* --- functions --- */
117 gtk_container_get_type (void)
119 static GType container_type = 0;
123 static const GTypeInfo container_info =
125 sizeof (GtkContainerClass),
126 (GBaseInitFunc) gtk_container_base_class_init,
127 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
128 (GClassInitFunc) gtk_container_class_init,
129 NULL /* class_finalize */,
130 NULL /* class_data */,
131 sizeof (GtkContainer),
133 (GInstanceInitFunc) gtk_container_init,
134 NULL, /* value_table */
138 g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer",
139 &container_info, G_TYPE_FLAG_ABSTRACT);
142 return container_type;
146 gtk_container_base_class_init (GtkContainerClass *class)
148 /* reset instance specifc class fields that don't get inherited */
149 class->set_child_property = NULL;
150 class->get_child_property = NULL;
154 gtk_container_base_class_finalize (GtkContainerClass *class)
158 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
159 for (node = list; node; node = node->next)
161 GParamSpec *pspec = node->data;
163 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
164 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
165 g_param_spec_unref (pspec);
171 gtk_container_class_init (GtkContainerClass *class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
174 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
175 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 parent_class = g_type_class_peek_parent (class);
179 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
180 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
182 gobject_class->set_property = gtk_container_set_property;
183 gobject_class->get_property = gtk_container_get_property;
185 object_class->destroy = gtk_container_destroy;
187 widget_class->show_all = gtk_container_show_all;
188 widget_class->hide_all = gtk_container_hide_all;
189 widget_class->expose_event = gtk_container_expose;
190 widget_class->map = gtk_container_map;
191 widget_class->unmap = gtk_container_unmap;
192 widget_class->focus = gtk_container_focus;
194 class->add = gtk_container_add_unimplemented;
195 class->remove = gtk_container_remove_unimplemented;
196 class->check_resize = gtk_container_real_check_resize;
197 class->forall = NULL;
198 class->set_focus_child = gtk_container_real_set_focus_child;
199 class->child_type = NULL;
200 class->composite_name = gtk_container_child_default_composite_name;
202 g_object_class_install_property (gobject_class,
204 g_param_spec_enum ("resize_mode",
206 P_("Specify how resize events are handled"),
207 GTK_TYPE_RESIZE_MODE,
210 g_object_class_install_property (gobject_class,
212 g_param_spec_uint ("border_width",
214 P_("The width of the empty border outside the containers children"),
219 g_object_class_install_property (gobject_class,
221 g_param_spec_object ("child",
223 P_("Can be used to add a new child to the container"),
226 container_signals[ADD] =
228 G_OBJECT_CLASS_TYPE (object_class),
230 G_STRUCT_OFFSET (GtkContainerClass, add),
232 _gtk_marshal_VOID__OBJECT,
235 container_signals[REMOVE] =
236 g_signal_new ("remove",
237 G_OBJECT_CLASS_TYPE (object_class),
239 G_STRUCT_OFFSET (GtkContainerClass, remove),
241 _gtk_marshal_VOID__OBJECT,
244 container_signals[CHECK_RESIZE] =
245 g_signal_new ("check_resize",
246 G_OBJECT_CLASS_TYPE (object_class),
248 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
250 _gtk_marshal_VOID__VOID,
252 container_signals[SET_FOCUS_CHILD] =
253 g_signal_new ("set-focus-child",
254 G_OBJECT_CLASS_TYPE (object_class),
256 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
258 _gtk_marshal_VOID__OBJECT,
264 * gtk_container_child_type:
265 * @container: a #GtkContainer.
267 * Returns the type of the children supported by the container.
269 * Note that this may return %G_TYPE_NONE to indicate that no more
270 * children can be added, e.g. for a #GtkPaned which already has two
273 * Return value: a #GType.
276 gtk_container_child_type (GtkContainer *container)
279 GtkContainerClass *class;
281 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
283 class = GTK_CONTAINER_GET_CLASS (container);
284 if (class->child_type)
285 slot = class->child_type (container);
292 /* --- GtkContainer child property mechanism --- */
294 container_get_child_property (GtkContainer *container,
299 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
301 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
305 container_set_child_property (GtkContainer *container,
309 GObjectNotifyQueue *nqueue)
311 GValue tmp_value = { 0, };
312 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
314 /* provide a copy to work from, convert (if necessary) and validate */
315 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
316 if (!g_value_transform (value, &tmp_value))
317 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
319 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
320 G_VALUE_TYPE_NAME (value));
321 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
323 gchar *contents = g_strdup_value_contents (value);
325 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
327 G_VALUE_TYPE_NAME (value),
329 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
334 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
335 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
337 g_value_unset (&tmp_value);
341 * gtk_container_child_get_valist:
342 * @container: a #GtkContainer
343 * @child: a widget which is a child of @container
344 * @first_property_name: the name of the first property to get
345 * @var_args: a %NULL-terminated list of property names and #GValue*,
346 * starting with @first_prop_name.
348 * Gets the values of one or more child properties for @child and @container.
351 gtk_container_child_get_valist (GtkContainer *container,
353 const gchar *first_property_name,
358 g_return_if_fail (GTK_IS_CONTAINER (container));
359 g_return_if_fail (GTK_IS_WIDGET (child));
360 g_return_if_fail (child->parent == GTK_WIDGET (container));
362 g_object_ref (container);
363 g_object_ref (child);
365 name = first_property_name;
368 GValue value = { 0, };
372 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
374 G_OBJECT_TYPE (container),
378 g_warning ("%s: container class `%s' has no child property named `%s'",
380 G_OBJECT_TYPE_NAME (container),
384 if (!(pspec->flags & G_PARAM_READABLE))
386 g_warning ("%s: child property `%s' of container class `%s' is not readable",
389 G_OBJECT_TYPE_NAME (container));
392 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
393 container_get_child_property (container, child, pspec, &value);
394 G_VALUE_LCOPY (&value, var_args, 0, &error);
397 g_warning ("%s: %s", G_STRLOC, error);
399 g_value_unset (&value);
402 g_value_unset (&value);
403 name = va_arg (var_args, gchar*);
406 g_object_unref (child);
407 g_object_unref (container);
411 * gtk_container_child_get_property:
412 * @container: a #GtkContainer
413 * @child: a widget which is a child of @container
414 * @property_name: the name of the property to get
415 * @value: a location to return the value
417 * Gets the value of a child property for @child and @container.
420 gtk_container_child_get_property (GtkContainer *container,
422 const gchar *property_name,
427 g_return_if_fail (GTK_IS_CONTAINER (container));
428 g_return_if_fail (GTK_IS_WIDGET (child));
429 g_return_if_fail (child->parent == GTK_WIDGET (container));
430 g_return_if_fail (property_name != NULL);
431 g_return_if_fail (G_IS_VALUE (value));
433 g_object_ref (container);
434 g_object_ref (child);
435 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
436 G_OBJECT_TYPE (container), TRUE);
438 g_warning ("%s: container class `%s' has no child property named `%s'",
440 G_OBJECT_TYPE_NAME (container),
442 else if (!(pspec->flags & G_PARAM_READABLE))
443 g_warning ("%s: child property `%s' of container class `%s' is not readable",
446 G_OBJECT_TYPE_NAME (container));
449 GValue *prop_value, tmp_value = { 0, };
451 /* auto-conversion of the callers value type
453 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
455 g_value_reset (value);
458 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
460 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
462 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
463 G_VALUE_TYPE_NAME (value));
464 g_object_unref (child);
465 g_object_unref (container);
470 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
471 prop_value = &tmp_value;
473 container_get_child_property (container, child, pspec, prop_value);
474 if (prop_value != value)
476 g_value_transform (prop_value, value);
477 g_value_unset (&tmp_value);
480 g_object_unref (child);
481 g_object_unref (container);
485 * gtk_container_child_set_valist:
486 * @container: a #GtkContainer
487 * @child: a widget which is a child of @container
488 * @first_property_name: the name of the first property to set
489 * @var_args: a %NULL-terminated list of property names and values, starting
490 * with @first_prop_name.
492 * Sets one or more child properties for @child and @container.
495 gtk_container_child_set_valist (GtkContainer *container,
497 const gchar *first_property_name,
501 GObjectNotifyQueue *nqueue;
504 g_return_if_fail (GTK_IS_CONTAINER (container));
505 g_return_if_fail (GTK_IS_WIDGET (child));
506 g_return_if_fail (child->parent == GTK_WIDGET (container));
508 g_object_ref (container);
509 g_object_ref (child);
511 object = G_OBJECT (container);
512 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
513 name = first_property_name;
516 GValue value = { 0, };
518 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
520 G_OBJECT_TYPE (container),
524 g_warning ("%s: container class `%s' has no child property named `%s'",
526 G_OBJECT_TYPE_NAME (container),
530 if (!(pspec->flags & G_PARAM_WRITABLE))
532 g_warning ("%s: child property `%s' of container class `%s' is not writable",
535 G_OBJECT_TYPE_NAME (container));
538 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
539 G_VALUE_COLLECT (&value, var_args, 0, &error);
542 g_warning ("%s: %s", G_STRLOC, error);
545 /* we purposely leak the value here, it might not be
546 * in a sane state if an error condition occoured
550 container_set_child_property (container, child, pspec, &value, nqueue);
551 g_value_unset (&value);
552 name = va_arg (var_args, gchar*);
554 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
556 g_object_unref (container);
557 g_object_unref (child);
561 * gtk_container_child_set_property:
562 * @container: a #GtkContainer
563 * @child: a widget which is a child of @container
564 * @property_name: the name of the property to set
565 * @value: the value to set the property to
567 * Sets a child property for @child and @container.
570 gtk_container_child_set_property (GtkContainer *container,
572 const gchar *property_name,
576 GObjectNotifyQueue *nqueue;
579 g_return_if_fail (GTK_IS_CONTAINER (container));
580 g_return_if_fail (GTK_IS_WIDGET (child));
581 g_return_if_fail (child->parent == GTK_WIDGET (container));
582 g_return_if_fail (property_name != NULL);
583 g_return_if_fail (G_IS_VALUE (value));
585 g_object_ref (container);
586 g_object_ref (child);
588 object = G_OBJECT (container);
589 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
590 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
591 G_OBJECT_TYPE (container), TRUE);
593 g_warning ("%s: container class `%s' has no child property named `%s'",
595 G_OBJECT_TYPE_NAME (container),
597 else if (!(pspec->flags & G_PARAM_WRITABLE))
598 g_warning ("%s: child property `%s' of container class `%s' is not writable",
601 G_OBJECT_TYPE_NAME (container));
604 container_set_child_property (container, child, pspec, value, nqueue);
606 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
607 g_object_unref (container);
608 g_object_unref (child);
612 * gtk_container_add_with_properties:
613 * @container: a #GtkContainer
614 * @widget: a widget to be placed inside @container
615 * @first_prop_name: the name of the first child property to set
616 * @Varargs: a %NULL-terminated list of property names and values, starting
617 * with @first_prop_name.
619 * Adds @widget to @container, setting child properties at the same time.
620 * See gtk_container_add() and gtk_container_child_set() for more details.
623 gtk_container_add_with_properties (GtkContainer *container,
625 const gchar *first_prop_name,
628 g_return_if_fail (GTK_IS_CONTAINER (container));
629 g_return_if_fail (GTK_IS_WIDGET (widget));
630 g_return_if_fail (widget->parent == NULL);
632 g_object_ref (container);
633 g_object_ref (widget);
634 gtk_widget_freeze_child_notify (widget);
636 g_signal_emit (container, container_signals[ADD], 0, widget);
641 va_start (var_args, first_prop_name);
642 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
646 gtk_widget_thaw_child_notify (widget);
647 g_object_unref (widget);
648 g_object_unref (container);
652 * gtk_container_child_set:
653 * @container: a #GtkContainer
654 * @child: a widget which is a child of @container
655 * @first_prop_name: the name of the first property to set
656 * @Varargs: a %NULL-terminated list of property names and values, starting
657 * with @first_prop_name.
659 * Sets one or more child properties for @child and @container.
662 gtk_container_child_set (GtkContainer *container,
664 const gchar *first_prop_name,
669 g_return_if_fail (GTK_IS_CONTAINER (container));
670 g_return_if_fail (GTK_IS_WIDGET (child));
671 g_return_if_fail (child->parent == GTK_WIDGET (container));
673 va_start (var_args, first_prop_name);
674 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
679 * gtk_container_child_get:
680 * @container: a #GtkContainer
681 * @child: a widget which is a child of @container
682 * @first_prop_name: the name of the first property to get
683 * @Varargs: a %NULL-terminated list of property names and #GValue*,
684 * starting with @first_prop_name.
686 * Gets the values of one or more child properties for @child and @container.
689 gtk_container_child_get (GtkContainer *container,
691 const gchar *first_prop_name,
696 g_return_if_fail (GTK_IS_CONTAINER (container));
697 g_return_if_fail (GTK_IS_WIDGET (child));
698 g_return_if_fail (child->parent == GTK_WIDGET (container));
700 va_start (var_args, first_prop_name);
701 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
706 * gtk_container_class_install_child_property:
707 * @cclass: a #GtkContainerClass
708 * @property_id: the id for the property
709 * @pspec: the #GParamSpec for the property
711 * Installs a child property on a container class.
714 gtk_container_class_install_child_property (GtkContainerClass *cclass,
718 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
719 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
720 if (pspec->flags & G_PARAM_WRITABLE)
721 g_return_if_fail (cclass->set_child_property != NULL);
722 if (pspec->flags & G_PARAM_READABLE)
723 g_return_if_fail (cclass->get_child_property != NULL);
724 g_return_if_fail (property_id > 0);
725 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
726 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
727 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
729 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
731 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
732 G_OBJECT_CLASS_NAME (cclass),
736 g_param_spec_ref (pspec);
737 g_param_spec_sink (pspec);
738 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
739 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
743 * gtk_container_class_find_child_property:
744 * @cclass: a #GtkContainerClass
745 * @property_name: the name of the child property to find
746 * @returns: the #GParamSpec of the child property or %NULL if @class has no
747 * child property with that name.
749 * Finds a child property of a container class by name.
752 gtk_container_class_find_child_property (GObjectClass *cclass,
753 const gchar *property_name)
755 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
756 g_return_val_if_fail (property_name != NULL, NULL);
758 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
760 G_OBJECT_CLASS_TYPE (cclass),
765 * gtk_container_class_list_child_properties:
766 * @cclass: a #GtkContainerClass
767 * @n_properties: location to return the number of child properties found
768 * @returns: a newly allocated array of #GParamSpec*. The array must be
769 * freed with g_free().
771 * Returns all child properties of a container class.
774 gtk_container_class_list_child_properties (GObjectClass *cclass,
780 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
782 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
783 G_OBJECT_CLASS_TYPE (cclass),
792 gtk_container_add_unimplemented (GtkContainer *container,
795 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
799 gtk_container_remove_unimplemented (GtkContainer *container,
802 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
806 gtk_container_init (GtkContainer *container)
808 container->focus_child = NULL;
809 container->border_width = 0;
810 container->need_resize = FALSE;
811 container->resize_mode = GTK_RESIZE_PARENT;
812 container->reallocate_redraws = FALSE;
816 gtk_container_destroy (GtkObject *object)
818 GtkContainer *container = GTK_CONTAINER (object);
820 if (GTK_CONTAINER_RESIZE_PENDING (container))
821 _gtk_container_dequeue_resize_handler (container);
823 /* do this before walking child widgets, to avoid
824 * removing children from focus chain one by one.
826 if (container->has_focus_chain)
827 gtk_container_unset_focus_chain (container);
829 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
831 if (GTK_OBJECT_CLASS (parent_class)->destroy)
832 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
836 gtk_container_set_property (GObject *object,
841 GtkContainer *container = GTK_CONTAINER (object);
845 case PROP_BORDER_WIDTH:
846 gtk_container_set_border_width (container, g_value_get_uint (value));
848 case PROP_RESIZE_MODE:
849 gtk_container_set_resize_mode (container, g_value_get_enum (value));
852 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
855 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
861 gtk_container_get_property (GObject *object,
866 GtkContainer *container = GTK_CONTAINER (object);
870 case PROP_BORDER_WIDTH:
871 g_value_set_uint (value, container->border_width);
873 case PROP_RESIZE_MODE:
874 g_value_set_enum (value, container->resize_mode);
877 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
883 * gtk_container_set_border_width:
884 * @container: a #GtkContainer
885 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
886 * Valid values are in the range 0-65535 pixels.
888 * Sets the border width of the container.
890 * The border width of a container is the amount of space to leave
891 * around the outside of the container. The only exception to this is
892 * #GtkWindow; because toplevel windows can't leave space outside,
893 * they leave the space inside. The border is added on all sides of
894 * the container. To add space to only one side, one approach is to
895 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
896 * it a size, and place it on the side of the container as a spacer.
899 gtk_container_set_border_width (GtkContainer *container,
902 g_return_if_fail (GTK_IS_CONTAINER (container));
904 if (container->border_width != border_width)
906 container->border_width = border_width;
907 g_object_notify (G_OBJECT (container), "border_width");
909 if (GTK_WIDGET_REALIZED (container))
910 gtk_widget_queue_resize (GTK_WIDGET (container));
915 * gtk_container_get_border_width:
916 * @container: a #GtkContainer
918 * Retrieves the border width of the container. See
919 * gtk_container_set_border_width().
921 * Return value: the current border width
924 gtk_container_get_border_width (GtkContainer *container)
926 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
928 return container->border_width;
933 * @container: a #GtkContainer
934 * @widget: a widget to be placed inside @container
936 * Adds @widget to @container. Typically used for simple containers
937 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
938 * layout containers such as #GtkBox or #GtkTable, this function will
939 * pick default packing parameters that may not be correct. So
940 * consider functions such as gtk_box_pack_start() and
941 * gtk_table_attach() as an alternative to gtk_container_add() in
942 * those cases. A widget may be added to only one container at a time;
943 * you can't place the same widget inside two different containers.
946 gtk_container_add (GtkContainer *container,
949 g_return_if_fail (GTK_IS_CONTAINER (container));
950 g_return_if_fail (GTK_IS_WIDGET (widget));
952 if (widget->parent != NULL)
954 g_warning ("Attempting to add a widget with type %s to a container of "
955 "type %s, but the widget is already inside a container of type %s, "
956 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
957 g_type_name (G_OBJECT_TYPE (widget)),
958 g_type_name (G_OBJECT_TYPE (container)),
959 g_type_name (G_OBJECT_TYPE (widget->parent)));
963 g_signal_emit (container, container_signals[ADD], 0, widget);
967 * gtk_container_remove:
968 * @container: a #GtkContainer
969 * @widget: a current child of @container
971 * Removes @widget from @container. @widget must be inside @container.
972 * Note that @container will own a reference to @widget, and that this
973 * may be the last reference held; so removing a widget from its
974 * container can destroy that widget. If you want to use @widget
975 * again, you need to add a reference to it while it's not inside
976 * a container, using g_object_ref(). If you don't want to use @widget
977 * again it's usually more efficient to simply destroy it directly
978 * using gtk_widget_destroy() since this will remove it from the
979 * container and help break any circular reference count cycles.
982 gtk_container_remove (GtkContainer *container,
985 g_return_if_fail (GTK_IS_CONTAINER (container));
986 g_return_if_fail (GTK_IS_WIDGET (widget));
988 /* When using the deprecated API of the toolbar, it is possible
989 * to legitimately call this function with a widget that is not
990 * a direct child of the container.
992 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
993 widget->parent == GTK_WIDGET (container));
995 g_signal_emit (container, container_signals[REMOVE], 0, widget);
999 _gtk_container_dequeue_resize_handler (GtkContainer *container)
1001 g_return_if_fail (GTK_IS_CONTAINER (container));
1002 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1004 container_resize_queue = g_slist_remove (container_resize_queue, container);
1005 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1009 * gtk_container_set_resize_mode:
1010 * @container: a #GtkContainer.
1011 * @resize_mode: the new resize mode.
1013 * Sets the resize mode for the container.
1015 * The resize mode of a container determines whether a resize request
1016 * will be passed to the container's parent, queued for later execution
1017 * or executed immediately.
1020 gtk_container_set_resize_mode (GtkContainer *container,
1021 GtkResizeMode resize_mode)
1023 g_return_if_fail (GTK_IS_CONTAINER (container));
1024 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1026 if (GTK_WIDGET_TOPLEVEL (container) &&
1027 resize_mode == GTK_RESIZE_PARENT)
1029 resize_mode = GTK_RESIZE_QUEUE;
1032 if (container->resize_mode != resize_mode)
1034 container->resize_mode = resize_mode;
1036 gtk_widget_queue_resize (GTK_WIDGET (container));
1037 g_object_notify (G_OBJECT (container), "resize_mode");
1042 * gtk_container_get_resize_mode:
1043 * @container: a #GtkContainer
1045 * Returns the resize mode for the container. See
1046 * gtk_container_set_resize_mode ().
1048 * Return value: the current resize mode
1051 gtk_container_get_resize_mode (GtkContainer *container)
1053 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1055 return container->resize_mode;
1059 * gtk_container_set_reallocate_redraws:
1060 * @container: a #GtkContainer.
1061 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
1063 * Sets the @reallocate_redraws flag of the container to the given value.
1065 * Containers requesting reallocation redraws get automatically
1066 * redrawn if any of their children changed allocation.
1069 gtk_container_set_reallocate_redraws (GtkContainer *container,
1070 gboolean needs_redraws)
1072 g_return_if_fail (GTK_IS_CONTAINER (container));
1074 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1077 static GtkContainer*
1078 gtk_container_get_resize_container (GtkContainer *container)
1080 GtkWidget *widget = GTK_WIDGET (container);
1082 while (widget->parent)
1084 widget = widget->parent;
1085 if (GTK_IS_RESIZE_CONTAINER (widget))
1089 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1093 gtk_container_idle_sizer (gpointer data)
1095 GDK_THREADS_ENTER ();
1097 /* we may be invoked with a container_resize_queue of NULL, because
1098 * queue_resize could have been adding an extra idle function while
1099 * the queue still got processed. we better just ignore such case
1100 * than trying to explicitely work around them with some extra flags,
1101 * since it doesn't cause any actual harm.
1103 while (container_resize_queue)
1108 slist = container_resize_queue;
1109 container_resize_queue = slist->next;
1110 widget = slist->data;
1111 g_slist_free_1 (slist);
1113 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1114 gtk_container_check_resize (GTK_CONTAINER (widget));
1117 gdk_window_process_all_updates ();
1119 GDK_THREADS_LEAVE ();
1125 _gtk_container_queue_resize (GtkContainer *container)
1127 GtkContainer *resize_container;
1130 g_return_if_fail (GTK_IS_CONTAINER (container));
1132 widget = GTK_WIDGET (container);
1133 resize_container = gtk_container_get_resize_container (container);
1137 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1138 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1139 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1143 widget = widget->parent;
1146 if (resize_container)
1148 if (GTK_WIDGET_VISIBLE (resize_container) &&
1149 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1151 switch (resize_container->resize_mode)
1153 case GTK_RESIZE_QUEUE:
1154 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1156 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1157 if (container_resize_queue == NULL)
1158 g_idle_add_full (GTK_PRIORITY_RESIZE,
1159 gtk_container_idle_sizer,
1161 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1165 case GTK_RESIZE_IMMEDIATE:
1166 gtk_container_check_resize (resize_container);
1169 case GTK_RESIZE_PARENT:
1170 g_assert_not_reached ();
1176 /* we need to let hidden resize containers know that something
1177 * changed while they where hidden (currently only evaluated by
1180 resize_container->need_resize = TRUE;
1186 gtk_container_check_resize (GtkContainer *container)
1188 g_return_if_fail (GTK_IS_CONTAINER (container));
1190 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1194 gtk_container_real_check_resize (GtkContainer *container)
1196 GtkWidget *widget = GTK_WIDGET (container);
1197 GtkRequisition requisition;
1199 gtk_widget_size_request (widget, &requisition);
1201 if (requisition.width > widget->allocation.width ||
1202 requisition.height > widget->allocation.height)
1204 if (GTK_IS_RESIZE_CONTAINER (container))
1205 gtk_widget_size_allocate (GTK_WIDGET (container),
1206 >K_WIDGET (container)->allocation);
1208 gtk_widget_queue_resize (widget);
1212 gtk_container_resize_children (container);
1216 /* The container hasn't changed size but one of its children
1217 * queued a resize request. Which means that the allocation
1218 * is not sufficient for the requisition of some child.
1219 * We've already performed a size request at this point,
1220 * so we simply need to reallocate and let the allocation
1221 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1224 gtk_container_resize_children (GtkContainer *container)
1228 /* resizing invariants:
1229 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1230 * containers that have an idle sizer pending must be flagged with
1233 g_return_if_fail (GTK_IS_CONTAINER (container));
1235 widget = GTK_WIDGET (container);
1236 gtk_widget_size_allocate (widget, &widget->allocation);
1240 * gtk_container_forall:
1241 * @container: a #GtkContainer
1242 * @callback: a callback
1243 * @callback_data: callback user data
1245 * Invokes @callback on each child of @container, including children
1246 * that are considered "internal" (implementation details of the
1247 * container). "Internal" children generally weren't added by the user
1248 * of the container, but were added by the container implementation
1249 * itself. Most applications should use gtk_container_foreach(),
1250 * rather than gtk_container_forall().
1253 gtk_container_forall (GtkContainer *container,
1254 GtkCallback callback,
1255 gpointer callback_data)
1257 GtkContainerClass *class;
1259 g_return_if_fail (GTK_IS_CONTAINER (container));
1260 g_return_if_fail (callback != NULL);
1262 class = GTK_CONTAINER_GET_CLASS (container);
1265 class->forall (container, TRUE, callback, callback_data);
1269 * gtk_container_foreach:
1270 * @container: a #GtkContainer
1271 * @callback: a callback
1272 * @callback_data: callback user data
1274 * Invokes @callback on each non-internal child of @container. See
1275 * gtk_container_forall() for details on what constitutes an
1276 * "internal" child. Most applications should use
1277 * gtk_container_foreach(), rather than gtk_container_forall().
1280 gtk_container_foreach (GtkContainer *container,
1281 GtkCallback callback,
1282 gpointer callback_data)
1284 GtkContainerClass *class;
1286 g_return_if_fail (GTK_IS_CONTAINER (container));
1287 g_return_if_fail (callback != NULL);
1289 class = GTK_CONTAINER_GET_CLASS (container);
1292 class->forall (container, FALSE, callback, callback_data);
1295 typedef struct _GtkForeachData GtkForeachData;
1296 struct _GtkForeachData
1298 GtkObject *container;
1299 GtkCallbackMarshal callback;
1300 gpointer callback_data;
1304 gtk_container_foreach_unmarshal (GtkWidget *child,
1307 GtkForeachData *fdata = (GtkForeachData*) data;
1310 /* first argument */
1311 args[0].name = NULL;
1312 args[0].type = G_TYPE_FROM_INSTANCE (child);
1313 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1315 /* location for return value */
1316 args[1].name = NULL;
1317 args[1].type = G_TYPE_NONE;
1319 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1323 gtk_container_foreach_full (GtkContainer *container,
1324 GtkCallback callback,
1325 GtkCallbackMarshal marshal,
1326 gpointer callback_data,
1327 GtkDestroyNotify notify)
1329 g_return_if_fail (GTK_IS_CONTAINER (container));
1333 GtkForeachData fdata;
1335 fdata.container = GTK_OBJECT (container);
1336 fdata.callback = marshal;
1337 fdata.callback_data = callback_data;
1339 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1343 g_return_if_fail (callback != NULL);
1345 gtk_container_foreach (container, callback, &callback_data);
1349 notify (callback_data);
1353 gtk_container_set_focus_child (GtkContainer *container,
1356 g_return_if_fail (GTK_IS_CONTAINER (container));
1358 g_return_if_fail (GTK_IS_WIDGET (widget));
1360 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1364 * gtk_container_get_children:
1365 * @container: a #GtkContainer.
1367 * Returns the container's non-internal children. See
1368 * gtk_container_forall() for details on what constitutes an "internal" child.
1370 * Return value: a newly-allocated list of the container's non-internal children.
1373 gtk_container_get_children (GtkContainer *container)
1375 GList *children = NULL;
1377 gtk_container_foreach (container,
1378 gtk_container_children_callback,
1381 return g_list_reverse (children);
1385 gtk_container_child_position_callback (GtkWidget *widget,
1386 gpointer client_data)
1392 } *data = client_data;
1395 if (data->child == widget)
1396 data->index = data->i;
1400 gtk_container_child_default_composite_name (GtkContainer *container,
1410 /* fallback implementation */
1414 gtk_container_forall (container,
1415 gtk_container_child_position_callback,
1418 name = g_strdup_printf ("%s-%u",
1419 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1426 _gtk_container_child_composite_name (GtkContainer *container,
1429 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1430 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1431 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1433 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1435 static GQuark quark_composite_name = 0;
1438 if (!quark_composite_name)
1439 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1441 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1444 GtkContainerClass *class;
1446 class = GTK_CONTAINER_GET_CLASS (container);
1447 if (class->composite_name)
1448 name = class->composite_name (container, child);
1451 name = g_strdup (name);
1460 gtk_container_real_set_focus_child (GtkContainer *container,
1463 g_return_if_fail (GTK_IS_CONTAINER (container));
1464 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1466 if (child != container->focus_child)
1468 if (container->focus_child)
1469 g_object_unref (container->focus_child);
1470 container->focus_child = child;
1471 if (container->focus_child)
1472 g_object_ref (container->focus_child);
1476 /* check for h/v adjustments
1478 if (container->focus_child)
1480 GtkAdjustment *hadj;
1481 GtkAdjustment *vadj;
1482 GtkWidget *focus_child;
1485 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1486 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1490 focus_child = container->focus_child;
1491 while (GTK_IS_CONTAINER (focus_child) &&
1492 GTK_CONTAINER (focus_child)->focus_child)
1494 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1497 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1500 x += container->focus_child->allocation.x;
1501 y += container->focus_child->allocation.y;
1504 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1507 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1513 get_focus_chain (GtkContainer *container)
1515 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1518 /* same as gtk_container_get_children, except it includes internals
1521 gtk_container_get_all_children (GtkContainer *container)
1523 GList *children = NULL;
1525 gtk_container_forall (container,
1526 gtk_container_children_callback,
1533 gtk_container_focus (GtkWidget *widget,
1534 GtkDirectionType direction)
1537 GList *sorted_children;
1539 GtkContainer *container;
1541 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1543 container = GTK_CONTAINER (widget);
1547 if (GTK_WIDGET_CAN_FOCUS (container))
1549 if (!GTK_WIDGET_HAS_FOCUS (container))
1551 gtk_widget_grab_focus (GTK_WIDGET (container));
1557 /* Get a list of the containers children, allowing focus
1558 * chain to override.
1560 if (container->has_focus_chain)
1561 children = g_list_copy (get_focus_chain (container));
1563 children = gtk_container_get_all_children (container);
1565 if (container->has_focus_chain &&
1566 (direction == GTK_DIR_TAB_FORWARD ||
1567 direction == GTK_DIR_TAB_BACKWARD))
1569 sorted_children = g_list_copy (children);
1571 if (direction == GTK_DIR_TAB_BACKWARD)
1572 sorted_children = g_list_reverse (sorted_children);
1575 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1577 return_val = gtk_container_focus_move (container, sorted_children, direction);
1579 g_list_free (sorted_children);
1580 g_list_free (children);
1587 tab_compare (gconstpointer a,
1591 const GtkWidget *child1 = a;
1592 const GtkWidget *child2 = b;
1593 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1595 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1596 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1600 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1601 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1603 if (text_direction == GTK_TEXT_DIR_RTL)
1604 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1606 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1609 return (y1 < y2) ? -1 : 1;
1613 gtk_container_focus_sort_tab (GtkContainer *container,
1615 GtkDirectionType direction,
1616 GtkWidget *old_focus)
1618 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1619 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1621 /* if we are going backwards then reverse the order
1624 if (direction == GTK_DIR_TAB_BACKWARD)
1625 children = g_list_reverse (children);
1630 /* Get coordinates of @widget's allocation with respect to
1631 * allocation of @container.
1634 get_allocation_coords (GtkContainer *container,
1636 GdkRectangle *allocation)
1638 *allocation = widget->allocation;
1640 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1641 0, 0, &allocation->x, &allocation->y);
1644 /* Look for a child in @children that is intermediate between
1645 * the focus widget and container. This widget, if it exists,
1646 * acts as the starting widget for focus navigation.
1649 find_old_focus (GtkContainer *container,
1652 GList *tmp_list = children;
1655 GtkWidget *child = tmp_list->data;
1656 GtkWidget *widget = child;
1658 while (widget && widget != (GtkWidget *)container)
1660 GtkWidget *parent = widget->parent;
1661 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1670 tmp_list = tmp_list->next;
1677 old_focus_coords (GtkContainer *container,
1678 GdkRectangle *old_focus_rect)
1680 GtkWidget *widget = GTK_WIDGET (container);
1681 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1683 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1685 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1687 return get_allocation_coords (container, old_focus, old_focus_rect);
1693 typedef struct _CompareInfo CompareInfo;
1697 GtkContainer *container;
1704 up_down_compare (gconstpointer a,
1708 GdkRectangle allocation1;
1709 GdkRectangle allocation2;
1710 CompareInfo *compare = data;
1713 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1715 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1718 y1 = allocation1.y + allocation1.height / 2;
1719 y2 = allocation2.y + allocation2.height / 2;
1723 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1724 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1726 if (compare->reverse)
1727 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1729 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1732 return (y1 < y2) ? -1 : 1;
1736 gtk_container_focus_sort_up_down (GtkContainer *container,
1738 GtkDirectionType direction,
1739 GtkWidget *old_focus)
1741 CompareInfo compare;
1743 GdkRectangle old_allocation;
1745 compare.container = container;
1746 compare.reverse = (direction == GTK_DIR_UP);
1749 old_focus = find_old_focus (container, children);
1751 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1757 /* Delete widgets from list that don't match minimum criteria */
1759 compare_x1 = old_allocation.x;
1760 compare_x2 = old_allocation.x + old_allocation.width;
1762 if (direction == GTK_DIR_UP)
1763 compare_y = old_allocation.y;
1765 compare_y = old_allocation.y + old_allocation.height;
1767 tmp_list = children;
1770 GtkWidget *child = tmp_list->data;
1771 GList *next = tmp_list->next;
1772 gint child_x1, child_x2;
1773 GdkRectangle child_allocation;
1775 if (child != old_focus)
1777 if (get_allocation_coords (container, child, &child_allocation))
1779 child_x1 = child_allocation.x;
1780 child_x2 = child_allocation.x + child_allocation.width;
1782 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1783 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1784 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1786 children = g_list_delete_link (children, tmp_list);
1790 children = g_list_delete_link (children, tmp_list);
1796 compare.x = (compare_x1 + compare_x2) / 2;
1797 compare.y = old_allocation.y + old_allocation.height / 2;
1801 /* No old focus widget, need to figure out starting x,y some other way
1803 GtkWidget *widget = GTK_WIDGET (container);
1804 GdkRectangle old_focus_rect;
1806 if (old_focus_coords (container, &old_focus_rect))
1808 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1812 if (GTK_WIDGET_NO_WINDOW (widget))
1813 compare.x = widget->allocation.x + widget->allocation.width / 2;
1815 compare.x = widget->allocation.width / 2;
1818 if (GTK_WIDGET_NO_WINDOW (widget))
1819 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1821 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1824 children = g_list_sort_with_data (children, up_down_compare, &compare);
1826 if (compare.reverse)
1827 children = g_list_reverse (children);
1833 left_right_compare (gconstpointer a,
1837 GdkRectangle allocation1;
1838 GdkRectangle allocation2;
1839 CompareInfo *compare = data;
1842 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1844 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1847 x1 = allocation1.x + allocation1.width / 2;
1848 x2 = allocation2.x + allocation2.width / 2;
1852 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1853 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1855 if (compare->reverse)
1856 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1858 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1861 return (x1 < x2) ? -1 : 1;
1865 gtk_container_focus_sort_left_right (GtkContainer *container,
1867 GtkDirectionType direction,
1868 GtkWidget *old_focus)
1870 CompareInfo compare;
1872 GdkRectangle old_allocation;
1874 compare.container = container;
1875 compare.reverse = (direction == GTK_DIR_LEFT);
1878 old_focus = find_old_focus (container, children);
1880 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1886 /* Delete widgets from list that don't match minimum criteria */
1888 compare_y1 = old_allocation.y;
1889 compare_y2 = old_allocation.y + old_allocation.height;
1891 if (direction == GTK_DIR_LEFT)
1892 compare_x = old_allocation.x;
1894 compare_x = old_allocation.x + old_allocation.width;
1896 tmp_list = children;
1899 GtkWidget *child = tmp_list->data;
1900 GList *next = tmp_list->next;
1901 gint child_y1, child_y2;
1902 GdkRectangle child_allocation;
1904 if (child != old_focus)
1906 if (get_allocation_coords (container, child, &child_allocation))
1908 child_y1 = child_allocation.y;
1909 child_y2 = child_allocation.y + child_allocation.height;
1911 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1912 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1913 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1915 children = g_list_delete_link (children, tmp_list);
1919 children = g_list_delete_link (children, tmp_list);
1925 compare.y = (compare_y1 + compare_y2) / 2;
1926 compare.x = old_allocation.x + old_allocation.width / 2;
1930 /* No old focus widget, need to figure out starting x,y some other way
1932 GtkWidget *widget = GTK_WIDGET (container);
1933 GdkRectangle old_focus_rect;
1935 if (old_focus_coords (container, &old_focus_rect))
1937 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1941 if (GTK_WIDGET_NO_WINDOW (widget))
1942 compare.y = widget->allocation.y + widget->allocation.height / 2;
1944 compare.y = widget->allocation.height / 2;
1947 if (GTK_WIDGET_NO_WINDOW (widget))
1948 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1950 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1953 children = g_list_sort_with_data (children, left_right_compare, &compare);
1955 if (compare.reverse)
1956 children = g_list_reverse (children);
1962 * gtk_container_focus_sort:
1963 * @container: a #GtkContainer
1964 * @children: a list of descendents of @container (they don't
1965 * have to be direct children.
1966 * @direction: focus direction
1967 * @old_focus: widget to use for the starting position, or %NULL
1968 * to determine this automatically.
1969 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1970 * which is the only @direction we use currently,
1971 * so perhaps this argument should be removed ]
1973 * Sorts @children in the correct order for focusing with
1974 * direction type @direction.
1976 * Return value: a copy of @children, sorted in correct focusing order,
1977 * with children that aren't suitable for focusing in this direction
1981 _gtk_container_focus_sort (GtkContainer *container,
1983 GtkDirectionType direction,
1984 GtkWidget *old_focus)
1986 children = g_list_copy (children);
1990 case GTK_DIR_TAB_FORWARD:
1991 case GTK_DIR_TAB_BACKWARD:
1992 return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1995 return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1998 return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
2001 g_assert_not_reached ();
2007 gtk_container_focus_move (GtkContainer *container,
2009 GtkDirectionType direction)
2011 GtkWidget *focus_child;
2014 focus_child = container->focus_child;
2018 child = children->data;
2019 children = children->next;
2026 if (focus_child == child)
2030 if (gtk_widget_child_focus (child, direction))
2034 else if (GTK_WIDGET_DRAWABLE (child) &&
2035 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2037 if (gtk_widget_child_focus (child, direction))
2047 gtk_container_children_callback (GtkWidget *widget,
2048 gpointer client_data)
2052 children = (GList**) client_data;
2053 *children = g_list_prepend (*children, widget);
2057 chain_widget_destroyed (GtkWidget *widget,
2060 GtkContainer *container;
2063 container = GTK_CONTAINER (user_data);
2065 chain = g_object_get_data (G_OBJECT (container),
2066 "gtk-container-focus-chain");
2068 chain = g_list_remove (chain, widget);
2070 g_signal_handlers_disconnect_by_func (widget,
2071 chain_widget_destroyed,
2074 g_object_set_data (G_OBJECT (container),
2075 "gtk-container-focus-chain",
2080 * gtk_container_set_focus_chain:
2081 * @container: a #GtkContainer.
2082 * @focusable_widgets: the new focus chain.
2084 * Sets a focus chain, overriding the one computed automatically by GTK+.
2086 * In principle each widget in the chain should be a descendant of the
2087 * container, but this is not enforced by this method, since it's allowed
2088 * to set the focus chain before you pack the widgets, or have a widget
2089 * in the chain that isn't always packed. The necessary checks are done
2090 * when the focus chain is actually traversed.
2093 gtk_container_set_focus_chain (GtkContainer *container,
2094 GList *focusable_widgets)
2099 g_return_if_fail (GTK_IS_CONTAINER (container));
2101 if (container->has_focus_chain)
2102 gtk_container_unset_focus_chain (container);
2104 container->has_focus_chain = TRUE;
2107 tmp_list = focusable_widgets;
2108 while (tmp_list != NULL)
2110 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2112 /* In principle each widget in the chain should be a descendant
2113 * of the container, but we don't want to check that here, it's
2114 * expensive and also it's allowed to set the focus chain before
2115 * you pack the widgets, or have a widget in the chain that isn't
2116 * always packed. So we check for ancestor during actual traversal.
2119 chain = g_list_prepend (chain, tmp_list->data);
2121 g_signal_connect (tmp_list->data,
2123 G_CALLBACK (chain_widget_destroyed),
2126 tmp_list = g_list_next (tmp_list);
2129 chain = g_list_reverse (chain);
2131 g_object_set_data (G_OBJECT (container),
2132 "gtk-container-focus-chain",
2137 * gtk_container_get_focus_chain:
2138 * @container: a #GtkContainer
2139 * @focusable_widgets: location to store the focus chain of the
2140 * container, or %NULL. You should free this list
2141 * using g_list_free() when you are done with it, however
2142 * no additional reference count is added to the
2143 * individual widgets in the focus chain.
2145 * Retrieves the focus chain of the container, if one has been
2146 * set explicitly. If no focus chain has been explicitly
2147 * set, GTK+ computes the focus chain based on the positions
2148 * of the children. In that case, GTK+ stores %NULL in
2149 * @focusable_widgets and returns %FALSE.
2151 * Return value: %TRUE if the focus chain of the container
2152 * has been set explicitly.
2155 gtk_container_get_focus_chain (GtkContainer *container,
2156 GList **focus_chain)
2158 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2162 if (container->has_focus_chain)
2163 *focus_chain = g_list_copy (get_focus_chain (container));
2165 *focus_chain = NULL;
2168 return container->has_focus_chain;
2172 * gtk_container_unset_focus_chain:
2173 * @container: a #GtkContainer.
2175 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2178 gtk_container_unset_focus_chain (GtkContainer *container)
2180 g_return_if_fail (GTK_IS_CONTAINER (container));
2182 if (container->has_focus_chain)
2187 chain = get_focus_chain (container);
2189 container->has_focus_chain = FALSE;
2191 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2195 while (tmp_list != NULL)
2197 g_signal_handlers_disconnect_by_func (tmp_list->data,
2198 chain_widget_destroyed,
2201 tmp_list = g_list_next (tmp_list);
2204 g_list_free (chain);
2209 * gtk_container_set_focus_vadjustment:
2210 * @container: a #GtkContainer
2211 * @adjustment: an adjustment which should be adjusted when the focus is moved among the
2212 * descendents of @container
2214 * Hooks up an adjustment to focus handling in a container, so when a child of the
2215 * container is focused, the adjustment is scrolled to show that widget. This function
2216 * sets the vertical alignment. See gtk_scrolled_window_get_vadjustment() for a typical
2217 * way of obtaining the adjustment and gtk_container_set_focus_hadjustment() for setting
2218 * the horizontal adjustment.
2220 * The adjustments have to be in pixel units and in the same coordinate system as the
2221 * allocation for immediate children of the container.
2224 gtk_container_set_focus_vadjustment (GtkContainer *container,
2225 GtkAdjustment *adjustment)
2227 g_return_if_fail (GTK_IS_CONTAINER (container));
2229 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2232 g_object_ref (adjustment);
2234 g_object_set_qdata_full (G_OBJECT (container),
2241 * gtk_container_get_focus_vadjustment:
2242 * @container: a #GtkContainer
2244 * Retrieves the vertical focus adjustment for the container. See
2245 * gtk_container_set_focus_vadjustment ().
2247 * Return value: the vertical focus adjustment, or %NULL if
2248 * none has been set.
2251 gtk_container_get_focus_vadjustment (GtkContainer *container)
2253 GtkAdjustment *vadjustment;
2255 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2257 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2263 * gtk_container_set_focus_hadjustment:
2264 * @container: a #GtkContainer
2265 * @adjustment: an adjustment which should be adjusted when the focus is moved among the
2266 * descendents of @container
2268 * Hooks up an adjustment to focus handling in a container, so when a child of the
2269 * container is focused, the adjustment is scrolled to show that widget. This function
2270 * sets the horizontal alignment. See gtk_scrolled_window_get_hadjustment() for a typical
2271 * way of obtaining the adjustment and gtk_container_set_focus_vadjustment() for setting
2272 * the vertical adjustment.
2274 * The adjustments have to be in pixel units and in the same coordinate system as the
2275 * allocation for immediate children of the container.
2278 gtk_container_set_focus_hadjustment (GtkContainer *container,
2279 GtkAdjustment *adjustment)
2281 g_return_if_fail (GTK_IS_CONTAINER (container));
2283 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2286 g_object_ref (adjustment);
2288 g_object_set_qdata_full (G_OBJECT (container),
2295 * gtk_container_get_focus_hadjustment:
2296 * @container: a #GtkContainer
2298 * Retrieves the horizontal focus adjustment for the container. See
2299 * gtk_container_set_focus_hadjustment ().
2301 * Return value: the horizontal focus adjustment, or %NULL if
2302 * none has been set.
2305 gtk_container_get_focus_hadjustment (GtkContainer *container)
2307 GtkAdjustment *hadjustment;
2309 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2311 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2318 gtk_container_show_all (GtkWidget *widget)
2320 g_return_if_fail (GTK_IS_CONTAINER (widget));
2322 gtk_container_foreach (GTK_CONTAINER (widget),
2323 (GtkCallback) gtk_widget_show_all,
2325 gtk_widget_show (widget);
2329 gtk_container_hide_all (GtkWidget *widget)
2331 g_return_if_fail (GTK_IS_CONTAINER (widget));
2333 gtk_widget_hide (widget);
2334 gtk_container_foreach (GTK_CONTAINER (widget),
2335 (GtkCallback) gtk_widget_hide_all,
2341 gtk_container_expose_child (GtkWidget *child,
2342 gpointer client_data)
2345 GtkWidget *container;
2346 GdkEventExpose *event;
2347 } *data = client_data;
2349 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2355 gtk_container_expose (GtkWidget *widget,
2356 GdkEventExpose *event)
2359 GtkWidget *container;
2360 GdkEventExpose *event;
2363 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2364 g_return_val_if_fail (event != NULL, FALSE);
2367 if (GTK_WIDGET_DRAWABLE (widget))
2369 data.container = widget;
2372 gtk_container_forall (GTK_CONTAINER (widget),
2373 gtk_container_expose_child,
2381 gtk_container_map_child (GtkWidget *child,
2382 gpointer client_data)
2384 if (GTK_WIDGET_VISIBLE (child) &&
2385 GTK_WIDGET_CHILD_VISIBLE (child) &&
2386 !GTK_WIDGET_MAPPED (child))
2387 gtk_widget_map (child);
2391 gtk_container_map (GtkWidget *widget)
2393 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2395 gtk_container_forall (GTK_CONTAINER (widget),
2396 gtk_container_map_child,
2399 if (!GTK_WIDGET_NO_WINDOW (widget))
2400 gdk_window_show (widget->window);
2404 gtk_container_unmap (GtkWidget *widget)
2406 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2408 if (!GTK_WIDGET_NO_WINDOW (widget))
2409 gdk_window_hide (widget->window);
2411 gtk_container_forall (GTK_CONTAINER (widget),
2412 (GtkCallback)gtk_widget_unmap,
2417 * gtk_container_propagate_expose:
2418 * @container: a #GtkContainer
2419 * @child: a child of @container
2420 * @event: a expose event sent to container
2422 * When a container receives an expose event, it must send synthetic
2423 * expose events to all children that don't have their own #GdkWindows.
2424 * This function provides a convenient way of doing this. A container,
2425 * when it receives an expose event, calls gtk_container_propagate_expose()
2426 * once for each child, passing in the event the container received.
2428 * gtk_container_propagate_expose() takes care of deciding whether
2429 * an expose event needs to be sent to the child, intersecting
2430 * the event's area with the child area, and sending the event.
2432 * In most cases, a container can simply either simply inherit the
2433 * ::expose implementation from #GtkContainer, or, do some drawing
2434 * and then chain to the ::expose implementation from #GtkContainer.
2437 gtk_container_propagate_expose (GtkContainer *container,
2439 GdkEventExpose *event)
2441 GdkEvent *child_event;
2443 g_return_if_fail (GTK_IS_CONTAINER (container));
2444 g_return_if_fail (GTK_IS_WIDGET (child));
2445 g_return_if_fail (event != NULL);
2447 g_assert (child->parent == GTK_WIDGET (container));
2449 if (GTK_WIDGET_DRAWABLE (child) &&
2450 GTK_WIDGET_NO_WINDOW (child) &&
2451 (child->window == event->window))
2453 child_event = gdk_event_new (GDK_EXPOSE);
2454 child_event->expose = *event;
2455 g_object_ref (child_event->expose.window);
2457 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2458 if (!gdk_region_empty (child_event->expose.region))
2460 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2461 gtk_widget_send_expose (child, child_event);
2463 gdk_event_free (child_event);