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/.
31 #include "gtkcontainer.h"
32 #include "gtkprivate.h"
33 #include "gtksignal.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
38 #include <gobject/gobjectnotifyqueue.c>
39 #include <gobject/gvaluecollector.h>
57 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
58 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
61 /* --- prototypes --- */
62 static void gtk_container_base_class_init (GtkContainerClass *klass);
63 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
64 static void gtk_container_class_init (GtkContainerClass *klass);
65 static void gtk_container_init (GtkContainer *container);
66 static void gtk_container_destroy (GtkObject *object);
67 static void gtk_container_set_property (GObject *object,
71 static void gtk_container_get_property (GObject *object,
75 static void gtk_container_add_unimplemented (GtkContainer *container,
77 static void gtk_container_remove_unimplemented (GtkContainer *container,
79 static void gtk_container_real_check_resize (GtkContainer *container);
80 static gboolean gtk_container_focus (GtkWidget *widget,
81 GtkDirectionType direction);
82 static void gtk_container_real_set_focus_child (GtkContainer *container,
85 static gboolean gtk_container_focus_move (GtkContainer *container,
87 GtkDirectionType direction);
88 static void gtk_container_children_callback (GtkWidget *widget,
89 gpointer client_data);
90 static void gtk_container_show_all (GtkWidget *widget);
91 static void gtk_container_hide_all (GtkWidget *widget);
92 static gint gtk_container_expose (GtkWidget *widget,
93 GdkEventExpose *event);
94 static void gtk_container_map (GtkWidget *widget);
95 static void gtk_container_unmap (GtkWidget *widget);
97 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
101 /* --- variables --- */
102 static const gchar *vadjustment_key = "gtk-vadjustment";
103 static guint vadjustment_key_id = 0;
104 static const gchar *hadjustment_key = "gtk-hadjustment";
105 static guint hadjustment_key_id = 0;
106 static GSList *container_resize_queue = NULL;
107 static guint container_signals[LAST_SIGNAL] = { 0 };
108 static GtkWidgetClass *parent_class = NULL;
109 extern GParamSpecPool *_gtk_widget_child_property_pool;
110 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
113 /* --- functions --- */
115 gtk_container_get_type (void)
117 static GtkType container_type = 0;
121 static GTypeInfo container_info = {
122 sizeof (GtkContainerClass),
123 (GBaseInitFunc) gtk_container_base_class_init,
124 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
125 (GClassInitFunc) gtk_container_class_init,
126 NULL /* class_destroy */,
127 NULL /* class_data */,
128 sizeof (GtkContainer),
130 (GInstanceInitFunc) gtk_container_init,
131 NULL, /* value_table */
134 container_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer",
135 &container_info, G_TYPE_FLAG_ABSTRACT);
138 return container_type;
142 gtk_container_base_class_init (GtkContainerClass *class)
144 /* reset instance specifc class fields that don't get inherited */
145 class->set_child_property = NULL;
146 class->get_child_property = NULL;
150 gtk_container_base_class_finalize (GtkContainerClass *class)
154 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
155 for (node = list; node; node = node->next)
157 GParamSpec *pspec = node->data;
159 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
160 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
161 g_param_spec_unref (pspec);
167 gtk_container_class_init (GtkContainerClass *class)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
170 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
171 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
173 parent_class = g_type_class_peek_parent (class);
175 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
176 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
178 gobject_class->set_property = gtk_container_set_property;
179 gobject_class->get_property = gtk_container_get_property;
181 object_class->destroy = gtk_container_destroy;
183 widget_class->show_all = gtk_container_show_all;
184 widget_class->hide_all = gtk_container_hide_all;
185 widget_class->expose_event = gtk_container_expose;
186 widget_class->map = gtk_container_map;
187 widget_class->unmap = gtk_container_unmap;
188 widget_class->focus = gtk_container_focus;
190 class->add = gtk_container_add_unimplemented;
191 class->remove = gtk_container_remove_unimplemented;
192 class->check_resize = gtk_container_real_check_resize;
193 class->forall = NULL;
194 class->set_focus_child = gtk_container_real_set_focus_child;
195 class->child_type = NULL;
196 class->composite_name = gtk_container_child_default_composite_name;
198 g_object_class_install_property (gobject_class,
200 g_param_spec_enum ("resize_mode",
202 _("Specify how resize events are handled"),
203 GTK_TYPE_RESIZE_MODE,
206 g_object_class_install_property (gobject_class,
208 g_param_spec_uint ("border_width",
210 _("The width of the empty border outside the containers children"),
215 g_object_class_install_property (gobject_class,
217 g_param_spec_object ("child",
219 _("Can be used to add a new child to the container"),
222 container_signals[ADD] =
223 gtk_signal_new ("add",
225 GTK_CLASS_TYPE (object_class),
226 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
227 _gtk_marshal_VOID__OBJECT,
230 container_signals[REMOVE] =
231 gtk_signal_new ("remove",
233 GTK_CLASS_TYPE (object_class),
234 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
235 _gtk_marshal_VOID__OBJECT,
238 container_signals[CHECK_RESIZE] =
239 gtk_signal_new ("check_resize",
241 GTK_CLASS_TYPE (object_class),
242 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
243 _gtk_marshal_VOID__VOID,
245 container_signals[SET_FOCUS_CHILD] =
246 gtk_signal_new ("set-focus-child",
248 GTK_CLASS_TYPE (object_class),
249 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
250 _gtk_marshal_VOID__OBJECT,
256 * gtk_container_child_type:
257 * @container: a #GtkContainer.
259 * Returns the type of the children supported by the container.
261 * Note that this may return %GTK_TYPE_NONE to indicate that no more
262 * children can be added, e.g. for a #GtkPaned which already has two
265 * Return value: a #GtkType.
268 gtk_container_child_type (GtkContainer *container)
271 GtkContainerClass *class;
273 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
275 class = GTK_CONTAINER_GET_CLASS (container);
276 if (class->child_type)
277 slot = class->child_type (container);
279 slot = GTK_TYPE_NONE;
284 /* --- GtkContainer child property mechanism --- */
286 container_get_child_property (GtkContainer *container,
291 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
293 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
297 container_set_child_property (GtkContainer *container,
301 GObjectNotifyQueue *nqueue)
303 GValue tmp_value = { 0, };
304 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
306 /* provide a copy to work from, convert (if necessary) and validate */
307 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
308 if (!g_value_transform (value, &tmp_value))
309 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
311 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
312 G_VALUE_TYPE_NAME (value));
313 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
315 gchar *contents = g_strdup_value_contents (value);
317 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
319 G_VALUE_TYPE_NAME (value),
321 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
326 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
327 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
329 g_value_unset (&tmp_value);
333 gtk_container_child_get_valist (GtkContainer *container,
335 const gchar *first_property_name,
340 g_return_if_fail (GTK_IS_CONTAINER (container));
341 g_return_if_fail (GTK_IS_WIDGET (child));
342 g_return_if_fail (child->parent == GTK_WIDGET (container));
344 g_object_ref (container);
345 g_object_ref (child);
347 name = first_property_name;
350 GValue value = { 0, };
354 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
356 G_OBJECT_TYPE (container),
360 g_warning ("%s: container class `%s' has no child property named `%s'",
362 G_OBJECT_TYPE_NAME (container),
366 if (!(pspec->flags & G_PARAM_READABLE))
368 g_warning ("%s: child property `%s' of container class `%s' is not readable",
371 G_OBJECT_TYPE_NAME (container));
374 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
375 container_get_child_property (container, child, pspec, &value);
376 G_VALUE_LCOPY (&value, var_args, 0, &error);
379 g_warning ("%s: %s", G_STRLOC, error);
381 g_value_unset (&value);
384 g_value_unset (&value);
385 name = va_arg (var_args, gchar*);
388 g_object_unref (child);
389 g_object_unref (container);
393 gtk_container_child_get_property (GtkContainer *container,
395 const gchar *property_name,
400 g_return_if_fail (GTK_IS_CONTAINER (container));
401 g_return_if_fail (GTK_IS_WIDGET (child));
402 g_return_if_fail (child->parent == GTK_WIDGET (container));
403 g_return_if_fail (property_name != NULL);
404 g_return_if_fail (G_IS_VALUE (value));
406 g_object_ref (container);
407 g_object_ref (child);
408 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
409 G_OBJECT_TYPE (container), TRUE);
411 g_warning ("%s: container class `%s' has no child property named `%s'",
413 G_OBJECT_TYPE_NAME (container),
415 else if (!(pspec->flags & G_PARAM_READABLE))
416 g_warning ("%s: child property `%s' of container class `%s' is not readable",
419 G_OBJECT_TYPE_NAME (container));
422 GValue *prop_value, tmp_value = { 0, };
424 /* auto-conversion of the callers value type
426 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
428 g_value_reset (value);
431 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
433 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
435 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
436 G_VALUE_TYPE_NAME (value));
437 g_object_unref (child);
438 g_object_unref (container);
443 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
444 prop_value = &tmp_value;
446 container_get_child_property (container, child, pspec, prop_value);
447 if (prop_value != value)
449 g_value_transform (prop_value, value);
450 g_value_unset (&tmp_value);
453 g_object_unref (child);
454 g_object_unref (container);
458 gtk_container_child_set_valist (GtkContainer *container,
460 const gchar *first_property_name,
464 GObjectNotifyQueue *nqueue;
467 g_return_if_fail (GTK_IS_CONTAINER (container));
468 g_return_if_fail (GTK_IS_WIDGET (child));
469 g_return_if_fail (child->parent == GTK_WIDGET (container));
471 g_object_ref (container);
472 g_object_ref (child);
474 object = G_OBJECT (container);
475 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
476 name = first_property_name;
479 GValue value = { 0, };
481 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
483 G_OBJECT_TYPE (container),
487 g_warning ("%s: container class `%s' has no child property named `%s'",
489 G_OBJECT_TYPE_NAME (container),
493 if (!(pspec->flags & G_PARAM_WRITABLE))
495 g_warning ("%s: child property `%s' of container class `%s' is not writable",
498 G_OBJECT_TYPE_NAME (container));
501 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
502 G_VALUE_COLLECT (&value, var_args, 0, &error);
505 g_warning ("%s: %s", G_STRLOC, error);
508 /* we purposely leak the value here, it might not be
509 * in a sane state if an error condition occoured
513 container_set_child_property (container, child, pspec, &value, nqueue);
514 g_value_unset (&value);
515 name = va_arg (var_args, gchar*);
517 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
519 g_object_unref (container);
520 g_object_unref (child);
524 gtk_container_child_set_property (GtkContainer *container,
526 const gchar *property_name,
530 GObjectNotifyQueue *nqueue;
533 g_return_if_fail (GTK_IS_CONTAINER (container));
534 g_return_if_fail (GTK_IS_WIDGET (child));
535 g_return_if_fail (child->parent == GTK_WIDGET (container));
536 g_return_if_fail (property_name != NULL);
537 g_return_if_fail (G_IS_VALUE (value));
539 g_object_ref (container);
540 g_object_ref (child);
542 object = G_OBJECT (container);
543 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
544 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
545 G_OBJECT_TYPE (container), TRUE);
547 g_warning ("%s: container class `%s' has no child property named `%s'",
549 G_OBJECT_TYPE_NAME (container),
551 else if (!(pspec->flags & G_PARAM_WRITABLE))
552 g_warning ("%s: child property `%s' of container class `%s' is not writable",
555 G_OBJECT_TYPE_NAME (container));
558 container_set_child_property (container, child, pspec, value, nqueue);
560 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
561 g_object_unref (container);
562 g_object_unref (child);
566 gtk_container_add_with_properties (GtkContainer *container,
568 const gchar *first_prop_name,
571 g_return_if_fail (GTK_IS_CONTAINER (container));
572 g_return_if_fail (GTK_IS_WIDGET (widget));
573 g_return_if_fail (widget->parent == NULL);
575 gtk_widget_ref (GTK_WIDGET (container));
576 gtk_widget_ref (widget);
577 gtk_widget_freeze_child_notify (widget);
579 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
584 va_start (var_args, first_prop_name);
585 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
589 gtk_widget_thaw_child_notify (widget);
590 gtk_widget_unref (widget);
591 gtk_widget_unref (GTK_WIDGET (container));
595 gtk_container_child_set (GtkContainer *container,
597 const gchar *first_prop_name,
602 g_return_if_fail (GTK_IS_CONTAINER (container));
603 g_return_if_fail (GTK_IS_WIDGET (child));
604 g_return_if_fail (child->parent == GTK_WIDGET (container));
606 va_start (var_args, first_prop_name);
607 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
612 gtk_container_child_get (GtkContainer *container,
614 const gchar *first_prop_name,
619 g_return_if_fail (GTK_IS_CONTAINER (container));
620 g_return_if_fail (GTK_IS_WIDGET (child));
621 g_return_if_fail (child->parent == GTK_WIDGET (container));
623 va_start (var_args, first_prop_name);
624 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
629 gtk_container_class_install_child_property (GtkContainerClass *cclass,
633 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
634 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
635 if (pspec->flags & G_PARAM_WRITABLE)
636 g_return_if_fail (cclass->set_child_property != NULL);
637 if (pspec->flags & G_PARAM_READABLE)
638 g_return_if_fail (cclass->get_child_property != NULL);
639 g_return_if_fail (property_id > 0);
640 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
641 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
642 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
644 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
646 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
647 G_OBJECT_CLASS_NAME (cclass),
651 g_param_spec_ref (pspec);
652 g_param_spec_sink (pspec);
653 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
654 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
658 * gtk_container_class_find_child_property:
659 * @cclass: a #GtkContainerClass
660 * @property_name: the name of the child property to find
661 * @returns: the #GParamSpec of the child property or %NULL if @class has ho
662 * child property with that name.
664 * Finds a child property of a container class by name.
667 gtk_container_class_find_child_property (GObjectClass *cclass,
668 const gchar *property_name)
670 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
671 g_return_val_if_fail (property_name != NULL, NULL);
673 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
675 G_OBJECT_CLASS_TYPE (cclass),
680 * gtk_container_class_list_child_properties:
681 * @cclass: a #GtkContainerClass
682 * @n_properties: location to return the number of child properties found
683 * @returns: an newly allocated array of #GParamSpec*. The array must be freed with g_free().
685 * Returns all child properties of a container class.
688 gtk_container_class_list_child_properties (GObjectClass *cclass,
694 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
696 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
697 G_OBJECT_CLASS_TYPE (cclass),
706 gtk_container_add_unimplemented (GtkContainer *container,
709 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
713 gtk_container_remove_unimplemented (GtkContainer *container,
716 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
720 gtk_container_init (GtkContainer *container)
722 container->focus_child = NULL;
723 container->border_width = 0;
724 container->need_resize = FALSE;
725 container->resize_mode = GTK_RESIZE_PARENT;
726 container->reallocate_redraws = FALSE;
730 gtk_container_destroy (GtkObject *object)
732 GtkContainer *container = GTK_CONTAINER (object);
734 if (GTK_CONTAINER_RESIZE_PENDING (container))
735 _gtk_container_dequeue_resize_handler (container);
737 /* do this before walking child widgets, to avoid
738 * removing children from focus chain one by one.
740 if (container->has_focus_chain)
741 gtk_container_unset_focus_chain (container);
743 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
745 if (GTK_OBJECT_CLASS (parent_class)->destroy)
746 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
750 gtk_container_set_property (GObject *object,
755 GtkContainer *container = GTK_CONTAINER (object);
759 case PROP_BORDER_WIDTH:
760 gtk_container_set_border_width (container, g_value_get_uint (value));
762 case PROP_RESIZE_MODE:
763 gtk_container_set_resize_mode (container, g_value_get_enum (value));
766 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
769 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
775 gtk_container_get_property (GObject *object,
780 GtkContainer *container = GTK_CONTAINER (object);
784 case PROP_BORDER_WIDTH:
785 g_value_set_uint (value, container->border_width);
787 case PROP_RESIZE_MODE:
788 g_value_set_enum (value, container->resize_mode);
791 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
797 * gtk_container_set_border_width:
798 * @container: a #GtkContainer
799 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
800 * Valid values are in the range 0-65535 pixels.
802 * Sets the border width of the container.
804 * The border width of a container is the amount of space to leave
805 * around the outside of the container. The only exception to this is
806 * #GtkWindow; because toplevel windows can't leave space outside,
807 * they leave the space inside. The border is added on all sides of
808 * the container. To add space to only one side, one approach is to
809 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
810 * it a size, and place it on the side of the container as a spacer.
813 gtk_container_set_border_width (GtkContainer *container,
816 g_return_if_fail (GTK_IS_CONTAINER (container));
818 if (container->border_width != border_width)
820 container->border_width = border_width;
821 g_object_notify (G_OBJECT (container), "border_width");
823 if (GTK_WIDGET_REALIZED (container))
824 gtk_widget_queue_resize (GTK_WIDGET (container));
829 * gtk_container_get_border_width:
830 * @container: a #GtkContainer
832 * Retrieves the border width of the container. See
833 * gtk_container_set_border_width().
835 * Return value: the current border width
838 gtk_container_get_border_width (GtkContainer *container)
840 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
842 return container->border_width;
847 * @container: a #GtkContainer
848 * @widget: a widget to be placed inside @container
850 * Adds @widget to @container. Typically used for simple containers
851 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
852 * layout containers such as #GtkBox or #GtkTable, this function will
853 * pick default packing parameters that may not be correct. So
854 * consider functions such as gtk_box_pack_start() and
855 * gtk_table_attach() as an alternative to gtk_container_add() in
856 * those cases. A widget may be added to only one container at a time;
857 * you can't place the same widget inside two different containers.
860 gtk_container_add (GtkContainer *container,
863 g_return_if_fail (GTK_IS_CONTAINER (container));
864 g_return_if_fail (GTK_IS_WIDGET (widget));
866 if (widget->parent != NULL)
868 g_warning ("Attempting to add a widget with type %s to a container of "
869 "type %s, but the widget is already inside a container of type %s, "
870 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
871 g_type_name (G_OBJECT_TYPE (widget)),
872 g_type_name (G_OBJECT_TYPE (container)),
873 g_type_name (G_OBJECT_TYPE (widget->parent)));
877 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
881 * gtk_container_remove:
882 * @container: a #GtkContainer
883 * @widget: a current child of @container
885 * Removes @widget from @container. @widget must be inside @container.
886 * Note that @container will own a reference to @widget, and that this
887 * may be the last reference held; so removing a widget from its
888 * container can destroy that widget. If you want to use @widget
889 * again, you need to add a reference to it while it's not inside
890 * a container, using g_object_ref().
893 gtk_container_remove (GtkContainer *container,
896 g_return_if_fail (GTK_IS_CONTAINER (container));
897 g_return_if_fail (GTK_IS_WIDGET (widget));
898 g_return_if_fail (widget->parent == GTK_WIDGET (container));
900 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
904 _gtk_container_dequeue_resize_handler (GtkContainer *container)
906 g_return_if_fail (GTK_IS_CONTAINER (container));
907 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
909 container_resize_queue = g_slist_remove (container_resize_queue, container);
910 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
914 * gtk_container_set_resize_mode:
915 * @container: a #GtkContainer.
916 * @resize_mode: the new resize mode.
918 * Sets the resize mode for the container.
920 * The resize mode of a container determines whether a resize request
921 * will be passed to the container's parent, queued for later execution
922 * or executed immediately.
925 gtk_container_set_resize_mode (GtkContainer *container,
926 GtkResizeMode resize_mode)
928 g_return_if_fail (GTK_IS_CONTAINER (container));
929 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
931 if (GTK_WIDGET_TOPLEVEL (container) &&
932 resize_mode == GTK_RESIZE_PARENT)
934 resize_mode = GTK_RESIZE_QUEUE;
937 if (container->resize_mode != resize_mode)
939 container->resize_mode = resize_mode;
941 gtk_widget_queue_resize (GTK_WIDGET (container));
942 g_object_notify (G_OBJECT (container), "resize_mode");
947 * gtk_container_get_resize_mode:
948 * @container: a #GtkContainer
950 * Returns the resize mode for the container. See
951 * gtk_container_set_resize_mode ().
953 * Return value: the current resize mode
956 gtk_container_get_resize_mode (GtkContainer *container)
958 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
960 return container->resize_mode;
964 * gtk_container_set_reallocate_redraws:
965 * @container: a #GtkContainer.
966 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
968 * Sets the @reallocate_redraws flag of the container to the given value.
970 * Containers requesting reallocation redraws get automatically
971 * redrawn if any of their children changed allocation.
974 gtk_container_set_reallocate_redraws (GtkContainer *container,
975 gboolean needs_redraws)
977 g_return_if_fail (GTK_IS_CONTAINER (container));
979 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
983 gtk_container_get_resize_container (GtkContainer *container)
985 GtkWidget *widget = GTK_WIDGET (container);
987 while (widget->parent)
989 widget = widget->parent;
990 if (GTK_IS_RESIZE_CONTAINER (widget))
994 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
998 gtk_container_idle_sizer (gpointer data)
1000 GDK_THREADS_ENTER ();
1002 /* we may be invoked with a container_resize_queue of NULL, because
1003 * queue_resize could have been adding an extra idle function while
1004 * the queue still got processed. we better just ignore such case
1005 * than trying to explicitely work around them with some extra flags,
1006 * since it doesn't cause any actual harm.
1008 while (container_resize_queue)
1013 slist = container_resize_queue;
1014 container_resize_queue = slist->next;
1015 widget = slist->data;
1016 g_slist_free_1 (slist);
1018 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1019 gtk_container_check_resize (GTK_CONTAINER (widget));
1022 gdk_window_process_all_updates ();
1024 GDK_THREADS_LEAVE ();
1030 _gtk_container_queue_resize (GtkContainer *container)
1032 GtkContainer *resize_container;
1035 g_return_if_fail (GTK_IS_CONTAINER (container));
1037 widget = GTK_WIDGET (container);
1038 resize_container = gtk_container_get_resize_container (container);
1040 while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1042 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1043 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1044 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1048 widget = widget->parent;
1051 if (resize_container)
1053 if (GTK_WIDGET_VISIBLE (resize_container) &&
1054 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1056 switch (resize_container->resize_mode)
1058 case GTK_RESIZE_QUEUE:
1059 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1061 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1062 if (container_resize_queue == NULL)
1063 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1064 gtk_container_idle_sizer,
1066 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1070 case GTK_RESIZE_IMMEDIATE:
1071 gtk_container_check_resize (resize_container);
1074 case GTK_RESIZE_PARENT:
1075 g_assert_not_reached ();
1081 /* we need to let hidden resize containers know that something
1082 * changed while they where hidden (currently only evaluated by
1085 resize_container->need_resize = TRUE;
1091 gtk_container_check_resize (GtkContainer *container)
1093 g_return_if_fail (GTK_IS_CONTAINER (container));
1095 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1099 gtk_container_real_check_resize (GtkContainer *container)
1101 GtkWidget *widget = GTK_WIDGET (container);
1102 GtkRequisition requisition;
1104 gtk_widget_size_request (widget, &requisition);
1106 if (requisition.width > widget->allocation.width ||
1107 requisition.height > widget->allocation.height)
1109 if (GTK_IS_RESIZE_CONTAINER (container))
1110 gtk_widget_size_allocate (GTK_WIDGET (container),
1111 >K_WIDGET (container)->allocation);
1113 gtk_widget_queue_resize (widget);
1117 gtk_container_resize_children (container);
1121 /* The container hasn't changed size but one of its children
1122 * queued a resize request. Which means that the allocation
1123 * is not sufficient for the requisition of some child.
1124 * We've already performed a size request at this point,
1125 * so we simply need to reallocate and let the allocation
1126 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1129 gtk_container_resize_children (GtkContainer *container)
1133 /* resizing invariants:
1134 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1135 * containers that have an idle sizer pending must be flagged with
1138 g_return_if_fail (GTK_IS_CONTAINER (container));
1140 widget = GTK_WIDGET (container);
1141 gtk_widget_size_allocate (widget, &widget->allocation);
1145 * gtk_container_forall:
1146 * @container: a #GtkContainer
1147 * @callback: a callback
1148 * @callback_data: callback user data
1150 * Invokes @callback on each child of @container, including children
1151 * that are considered "internal" (implementation details of the
1152 * container). "Internal" children generally weren't added by the user
1153 * of the container, but were added by the container implementation
1154 * itself. Most applications should use gtk_container_foreach(),
1155 * rather than gtk_container_forall().
1158 gtk_container_forall (GtkContainer *container,
1159 GtkCallback callback,
1160 gpointer callback_data)
1162 GtkContainerClass *class;
1164 g_return_if_fail (GTK_IS_CONTAINER (container));
1165 g_return_if_fail (callback != NULL);
1167 class = GTK_CONTAINER_GET_CLASS (container);
1170 class->forall (container, TRUE, callback, callback_data);
1174 * gtk_container_foreach:
1175 * @container: a #GtkContainer
1176 * @callback: a callback
1177 * @callback_data: callback user data
1179 * Invokes @callback on each non-internal child of @container. See
1180 * gtk_container_forall() for details on what constitutes an
1181 * "internal" child. Most applications should use
1182 * gtk_container_foreach(), rather than gtk_container_forall().
1185 gtk_container_foreach (GtkContainer *container,
1186 GtkCallback callback,
1187 gpointer callback_data)
1189 GtkContainerClass *class;
1191 g_return_if_fail (GTK_IS_CONTAINER (container));
1192 g_return_if_fail (callback != NULL);
1194 class = GTK_CONTAINER_GET_CLASS (container);
1197 class->forall (container, FALSE, callback, callback_data);
1200 typedef struct _GtkForeachData GtkForeachData;
1201 struct _GtkForeachData
1203 GtkObject *container;
1204 GtkCallbackMarshal callback;
1205 gpointer callback_data;
1209 gtk_container_foreach_unmarshal (GtkWidget *child,
1212 GtkForeachData *fdata = (GtkForeachData*) data;
1215 /* first argument */
1216 args[0].name = NULL;
1217 args[0].type = GTK_OBJECT_TYPE (child);
1218 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1220 /* location for return value */
1221 args[1].name = NULL;
1222 args[1].type = GTK_TYPE_NONE;
1224 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1228 gtk_container_foreach_full (GtkContainer *container,
1229 GtkCallback callback,
1230 GtkCallbackMarshal marshal,
1231 gpointer callback_data,
1232 GtkDestroyNotify notify)
1234 g_return_if_fail (GTK_IS_CONTAINER (container));
1238 GtkForeachData fdata;
1240 fdata.container = GTK_OBJECT (container);
1241 fdata.callback = marshal;
1242 fdata.callback_data = callback_data;
1244 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1248 g_return_if_fail (callback != NULL);
1250 gtk_container_foreach (container, callback, &callback_data);
1254 notify (callback_data);
1258 gtk_container_set_focus_child (GtkContainer *container,
1261 g_return_if_fail (GTK_IS_CONTAINER (container));
1263 g_return_if_fail (GTK_IS_WIDGET (widget));
1265 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1269 * gtk_container_get_children:
1270 * @container: a #GtkContainer.
1272 * Returns the the container's non-internal children. See
1273 * gtk_container_forall() for details on what constitutes an "internal" child.
1275 * Return value: a newly-allocated list of the container's non-internal children.
1278 gtk_container_get_children (GtkContainer *container)
1280 GList *children = NULL;
1282 gtk_container_foreach (container,
1283 gtk_container_children_callback,
1286 return g_list_reverse (children);
1290 gtk_container_child_position_callback (GtkWidget *widget,
1291 gpointer client_data)
1297 } *data = client_data;
1300 if (data->child == widget)
1301 data->index = data->i;
1305 gtk_container_child_default_composite_name (GtkContainer *container,
1315 /* fallback implementation */
1319 gtk_container_forall (container,
1320 gtk_container_child_position_callback,
1323 name = g_strdup_printf ("%s-%u",
1324 gtk_type_name (GTK_OBJECT_TYPE (child)),
1331 _gtk_container_child_composite_name (GtkContainer *container,
1334 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1335 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1336 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1338 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1340 static GQuark quark_composite_name = 0;
1343 if (!quark_composite_name)
1344 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1346 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1349 GtkContainerClass *class;
1351 class = GTK_CONTAINER_GET_CLASS (container);
1352 if (class->composite_name)
1353 name = class->composite_name (container, child);
1356 name = g_strdup (name);
1365 gtk_container_real_set_focus_child (GtkContainer *container,
1368 g_return_if_fail (GTK_IS_CONTAINER (container));
1370 g_return_if_fail (GTK_IS_WIDGET (child));
1372 if (child != container->focus_child)
1374 if (container->focus_child)
1375 gtk_widget_unref (container->focus_child);
1376 container->focus_child = child;
1377 if (container->focus_child)
1378 gtk_widget_ref (container->focus_child);
1382 /* check for h/v adjustments
1384 if (container->focus_child)
1386 GtkAdjustment *adjustment;
1388 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1390 gtk_adjustment_clamp_page (adjustment,
1391 container->focus_child->allocation.y,
1392 (container->focus_child->allocation.y +
1393 container->focus_child->allocation.height));
1395 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1397 gtk_adjustment_clamp_page (adjustment,
1398 container->focus_child->allocation.x,
1399 (container->focus_child->allocation.x +
1400 container->focus_child->allocation.width));
1405 get_focus_chain (GtkContainer *container)
1407 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1410 /* same as gtk_container_get_children, except it includes internals
1413 gtk_container_get_all_children (GtkContainer *container)
1415 GList *children = NULL;
1417 gtk_container_forall (container,
1418 gtk_container_children_callback,
1425 gtk_container_focus (GtkWidget *widget,
1426 GtkDirectionType direction)
1429 GList *sorted_children;
1431 GtkContainer *container;
1433 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1435 container = GTK_CONTAINER (widget);
1439 if (GTK_WIDGET_CAN_FOCUS (container))
1441 if (!GTK_WIDGET_HAS_FOCUS (container))
1443 gtk_widget_grab_focus (GTK_WIDGET (container));
1449 /* Get a list of the containers children, allowing focus
1450 * chain to override.
1452 if (container->has_focus_chain)
1453 children = g_list_copy (get_focus_chain (container));
1455 children = gtk_container_get_all_children (container);
1457 if (container->has_focus_chain &&
1458 (direction == GTK_DIR_TAB_FORWARD ||
1459 direction == GTK_DIR_TAB_BACKWARD))
1461 sorted_children = g_list_copy (children);
1463 if (direction == GTK_DIR_TAB_BACKWARD)
1464 sorted_children = g_list_reverse (sorted_children);
1467 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1469 return_val = gtk_container_focus_move (container, sorted_children, direction);
1471 g_list_free (sorted_children);
1472 g_list_free (children);
1479 tab_compare (gconstpointer a,
1482 const GtkWidget *child1 = a;
1483 const GtkWidget *child2 = b;
1485 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1486 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1490 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1491 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1493 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1496 return (y1 < y2) ? -1 : 1;
1500 gtk_container_focus_sort_tab (GtkContainer *container,
1502 GtkDirectionType direction,
1503 GtkWidget *old_focus)
1505 children = g_list_sort (children, tab_compare);
1507 /* if we are going backwards then reverse the order
1510 if (direction == GTK_DIR_TAB_BACKWARD)
1511 children = g_list_reverse (children);
1516 /* Get coordinates of @widget's allocation with respect to
1517 * allocation of @container.
1520 get_allocation_coords (GtkContainer *container,
1522 GdkRectangle *allocation)
1524 *allocation = widget->allocation;
1526 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1527 0, 0, &allocation->x, &allocation->y);
1530 /* Look for a child in @children that is intermediate between
1531 * the focus widget and container. This widget, if it exists,
1532 * acts as the starting widget for focus navigation.
1535 find_old_focus (GtkContainer *container,
1538 GList *tmp_list = children;
1541 GtkWidget *child = tmp_list->data;
1542 GtkWidget *widget = child;
1544 while (widget && widget != (GtkWidget *)container)
1546 GtkWidget *parent = widget->parent;
1547 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1556 tmp_list = tmp_list->next;
1563 old_focus_coords (GtkContainer *container,
1564 GdkRectangle *old_focus_rect)
1566 GtkWidget *widget = GTK_WIDGET (container);
1567 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1569 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1571 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1573 return get_allocation_coords (container, old_focus, old_focus_rect);
1579 typedef struct _CompareInfo CompareInfo;
1583 GtkContainer *container;
1590 up_down_compare (gconstpointer a,
1594 GdkRectangle allocation1;
1595 GdkRectangle allocation2;
1596 CompareInfo *compare = data;
1599 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1601 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1604 y1 = allocation1.y + allocation1.height / 2;
1605 y2 = allocation2.y + allocation2.height / 2;
1609 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1610 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1612 if (compare->reverse)
1613 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1615 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1618 return (y1 < y2) ? -1 : 1;
1622 gtk_container_focus_sort_up_down (GtkContainer *container,
1624 GtkDirectionType direction,
1625 GtkWidget *old_focus)
1627 CompareInfo compare;
1629 GdkRectangle old_allocation;
1631 compare.container = container;
1632 compare.reverse = (direction == GTK_DIR_UP);
1635 old_focus = find_old_focus (container, children);
1637 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1643 /* Delete widgets from list that don't match minimum criteria */
1645 compare_x1 = old_allocation.x;
1646 compare_x2 = old_allocation.x + old_allocation.width;
1648 if (direction == GTK_DIR_UP)
1649 compare_y = old_allocation.y;
1651 compare_y = old_allocation.y + old_allocation.height;
1653 tmp_list = children;
1656 GtkWidget *child = tmp_list->data;
1657 GList *next = tmp_list->next;
1658 gint child_x1, child_x2;
1659 GdkRectangle child_allocation;
1661 if (child != old_focus)
1663 if (get_allocation_coords (container, child, &child_allocation))
1665 child_x1 = child_allocation.x;
1666 child_x2 = child_allocation.x + child_allocation.width;
1668 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1669 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1670 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1672 children = g_list_delete_link (children, tmp_list);
1676 children = g_list_delete_link (children, tmp_list);
1682 compare.x = (compare_x1 + compare_x2) / 2;
1683 compare.y = old_allocation.y + old_allocation.height / 2;
1687 /* No old focus widget, need to figure out starting x,y some other way
1689 GtkWidget *widget = GTK_WIDGET (container);
1690 GdkRectangle old_focus_rect;
1692 if (old_focus_coords (container, &old_focus_rect))
1694 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1698 if (GTK_WIDGET_NO_WINDOW (widget))
1699 compare.x = widget->allocation.x + widget->allocation.width / 2;
1701 compare.x = widget->allocation.width / 2;
1704 if (GTK_WIDGET_NO_WINDOW (widget))
1705 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1707 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1710 children = g_list_sort_with_data (children, up_down_compare, &compare);
1712 if (compare.reverse)
1713 children = g_list_reverse (children);
1719 left_right_compare (gconstpointer a,
1723 GdkRectangle allocation1;
1724 GdkRectangle allocation2;
1725 CompareInfo *compare = data;
1728 if (!get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1))
1730 if (!get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2))
1733 x1 = allocation1.x + allocation1.width / 2;
1734 x2 = allocation2.x + allocation2.width / 2;
1738 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1739 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1741 if (compare->reverse)
1742 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1744 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1747 return (x1 < x2) ? -1 : 1;
1751 gtk_container_focus_sort_left_right (GtkContainer *container,
1753 GtkDirectionType direction,
1754 GtkWidget *old_focus)
1756 CompareInfo compare;
1758 GdkRectangle old_allocation;
1760 compare.container = container;
1761 compare.reverse = (direction == GTK_DIR_LEFT);
1764 old_focus = find_old_focus (container, children);
1766 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1772 /* Delete widgets from list that don't match minimum criteria */
1774 compare_y1 = old_allocation.y;
1775 compare_y2 = old_allocation.y + old_allocation.height;
1777 if (direction == GTK_DIR_LEFT)
1778 compare_x = old_allocation.x;
1780 compare_x = old_allocation.x + old_allocation.width;
1782 tmp_list = children;
1785 GtkWidget *child = tmp_list->data;
1786 GList *next = tmp_list->next;
1787 gint child_y1, child_y2;
1788 GdkRectangle child_allocation;
1790 if (child != old_focus)
1792 if (get_allocation_coords (container, child, &child_allocation))
1794 child_y1 = child_allocation.y;
1795 child_y2 = child_allocation.y + child_allocation.height;
1797 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1798 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1799 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1801 children = g_list_delete_link (children, tmp_list);
1805 children = g_list_delete_link (children, tmp_list);
1811 compare.y = (compare_y1 + compare_y2) / 2;
1812 compare.x = old_allocation.x + old_allocation.width / 2;
1816 /* No old focus widget, need to figure out starting x,y some other way
1818 GtkWidget *widget = GTK_WIDGET (container);
1819 GdkRectangle old_focus_rect;
1821 if (old_focus_coords (container, &old_focus_rect))
1823 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1827 if (GTK_WIDGET_NO_WINDOW (widget))
1828 compare.y = widget->allocation.y + widget->allocation.height / 2;
1830 compare.y = widget->allocation.height / 2;
1833 if (GTK_WIDGET_NO_WINDOW (widget))
1834 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1836 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1839 children = g_list_sort_with_data (children, left_right_compare, &compare);
1841 if (compare.reverse)
1842 children = g_list_reverse (children);
1848 * gtk_container_focus_sort:
1849 * @container: a #GtkContainer
1850 * @children: a list of descendents of @container (they don't
1851 * have to be direct children.
1852 * @direction: focus direction
1853 * @old_focus: widget to use for the starting position, or %NULL
1854 * to determine this automatically.
1855 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1856 * which is the only @direction we use currently,
1857 * so perhaps this argument should be removed ]
1859 * Sorts @children in the correct order for focusing with
1860 * direction type @direction.
1862 * Return value: a copy of @children, sorted in correct focusing order,
1863 * with children that aren't suitable for focusing in this direction
1867 _gtk_container_focus_sort (GtkContainer *container,
1869 GtkDirectionType direction,
1870 GtkWidget *old_focus)
1872 children = g_list_copy (children);
1876 case GTK_DIR_TAB_FORWARD:
1877 case GTK_DIR_TAB_BACKWARD:
1878 return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1881 return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1884 return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
1887 g_assert_not_reached ();
1893 gtk_container_focus_move (GtkContainer *container,
1895 GtkDirectionType direction)
1897 GtkWidget *focus_child;
1900 focus_child = container->focus_child;
1904 child = children->data;
1905 children = children->next;
1912 if (focus_child == child)
1916 if (gtk_widget_child_focus (child, direction))
1920 else if (GTK_WIDGET_DRAWABLE (child) &&
1921 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1923 if (gtk_widget_child_focus (child, direction))
1933 gtk_container_children_callback (GtkWidget *widget,
1934 gpointer client_data)
1938 children = (GList**) client_data;
1939 *children = g_list_prepend (*children, widget);
1943 chain_widget_destroyed (GtkWidget *widget,
1946 GtkContainer *container;
1949 container = GTK_CONTAINER (user_data);
1951 chain = g_object_get_data (G_OBJECT (container),
1952 "gtk-container-focus-chain");
1954 chain = g_list_remove (chain, widget);
1956 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1957 (gpointer) chain_widget_destroyed,
1960 g_object_set_data (G_OBJECT (container),
1961 "gtk-container-focus-chain",
1966 * gtk_container_set_focus_chain:
1967 * @container: a #GtkContainer.
1968 * @focusable_widgets: the new focus chain.
1970 * Sets a focus chain, overriding the one computed automatically by GTK+.
1972 * In principle each widget in the chain should be a descendant of the
1973 * container, but this is not enforced by this method, since it's allowed
1974 * to set the focus chain before you pack the widgets, or have a widget
1975 * in the chain that isn't always packed. The necessary checks are done
1976 * when the focus chain is actually traversed.
1979 gtk_container_set_focus_chain (GtkContainer *container,
1980 GList *focusable_widgets)
1985 g_return_if_fail (GTK_IS_CONTAINER (container));
1987 if (container->has_focus_chain)
1988 gtk_container_unset_focus_chain (container);
1990 container->has_focus_chain = TRUE;
1993 tmp_list = focusable_widgets;
1994 while (tmp_list != NULL)
1996 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
1998 /* In principle each widget in the chain should be a descendant
1999 * of the container, but we don't want to check that here, it's
2000 * expensive and also it's allowed to set the focus chain before
2001 * you pack the widgets, or have a widget in the chain that isn't
2002 * always packed. So we check for ancestor during actual traversal.
2005 chain = g_list_prepend (chain, tmp_list->data);
2007 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
2009 GTK_SIGNAL_FUNC (chain_widget_destroyed),
2012 tmp_list = g_list_next (tmp_list);
2015 chain = g_list_reverse (chain);
2017 g_object_set_data (G_OBJECT (container),
2018 "gtk-container-focus-chain",
2023 * gtk_container_get_focus_chain:
2024 * @container: a #GtkContainer
2025 * @focusable_widgets: location to store the focus chain of the
2026 * container, or %NULL. You should free this list
2027 * using g_list_free() when you are done with it, however
2028 * no additional reference count is added to the
2029 * individual widgets in the focus chain.
2031 * Retrieves the focus chain of the container, if one has been
2032 * set explicitly. If no focus chain has been explicitly
2033 * set, GTK+ computes the focus chain based on the positions
2034 * of the children. In that case, GTK+ stores %NULL in
2035 * @focusable_widgets and returns %FALSE.
2037 * Return value: %TRUE if the focus chain of the container
2038 * has been set explicitly.
2041 gtk_container_get_focus_chain (GtkContainer *container,
2042 GList **focus_chain)
2044 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2048 if (container->has_focus_chain)
2049 *focus_chain = g_list_copy (get_focus_chain (container));
2051 *focus_chain = NULL;
2054 return container->has_focus_chain;
2058 * gtk_container_unset_focus_chain:
2059 * @container: a #GtkContainer.
2061 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2064 gtk_container_unset_focus_chain (GtkContainer *container)
2066 g_return_if_fail (GTK_IS_CONTAINER (container));
2068 if (container->has_focus_chain)
2073 chain = get_focus_chain (container);
2075 container->has_focus_chain = FALSE;
2077 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2081 while (tmp_list != NULL)
2083 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2084 (gpointer) chain_widget_destroyed,
2087 tmp_list = g_list_next (tmp_list);
2090 g_list_free (chain);
2095 gtk_container_set_focus_vadjustment (GtkContainer *container,
2096 GtkAdjustment *adjustment)
2098 g_return_if_fail (GTK_IS_CONTAINER (container));
2100 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2103 gtk_object_ref (GTK_OBJECT(adjustment));
2105 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2108 (GtkDestroyNotify) gtk_object_unref);
2112 * gtk_container_get_focus_vadjustment:
2113 * @container: a #GtkContainer
2115 * Retrieves the vertical focus adjustment for the container. See
2116 * gtk_container_set_focus_vadjustment ().
2118 * Return value: the vertical focus adjustment, or %NULL if
2119 * none has been set.
2122 gtk_container_get_focus_vadjustment (GtkContainer *container)
2124 GtkAdjustment *vadjustment;
2126 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2128 vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2129 vadjustment_key_id);
2135 gtk_container_set_focus_hadjustment (GtkContainer *container,
2136 GtkAdjustment *adjustment)
2138 g_return_if_fail (GTK_IS_CONTAINER (container));
2140 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2143 gtk_object_ref (GTK_OBJECT (adjustment));
2145 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2148 (GtkDestroyNotify) gtk_object_unref);
2152 * gtk_container_get_focus_hadjustment:
2153 * @container: a #GtkContainer
2155 * Retrieves the horizontal focus adjustment for the container. See
2156 * gtk_container_set_focus_hadjustment ().
2158 * Return value: the horizontal focus adjustment, or %NULL if
2159 * none has been set.
2162 gtk_container_get_focus_hadjustment (GtkContainer *container)
2164 GtkAdjustment *hadjustment;
2166 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2168 hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2169 hadjustment_key_id);
2176 gtk_container_show_all (GtkWidget *widget)
2178 g_return_if_fail (GTK_IS_CONTAINER (widget));
2180 gtk_container_foreach (GTK_CONTAINER (widget),
2181 (GtkCallback) gtk_widget_show_all,
2183 gtk_widget_show (widget);
2187 gtk_container_hide_all (GtkWidget *widget)
2189 g_return_if_fail (GTK_IS_CONTAINER (widget));
2191 gtk_widget_hide (widget);
2192 gtk_container_foreach (GTK_CONTAINER (widget),
2193 (GtkCallback) gtk_widget_hide_all,
2199 gtk_container_expose_child (GtkWidget *child,
2200 gpointer client_data)
2203 GtkWidget *container;
2204 GdkEventExpose *event;
2205 } *data = client_data;
2207 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2213 gtk_container_expose (GtkWidget *widget,
2214 GdkEventExpose *event)
2217 GtkWidget *container;
2218 GdkEventExpose *event;
2221 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2222 g_return_val_if_fail (event != NULL, FALSE);
2225 if (GTK_WIDGET_DRAWABLE (widget))
2227 data.container = widget;
2230 gtk_container_forall (GTK_CONTAINER (widget),
2231 gtk_container_expose_child,
2239 gtk_container_map_child (GtkWidget *child,
2240 gpointer client_data)
2242 if (GTK_WIDGET_VISIBLE (child) &&
2243 GTK_WIDGET_CHILD_VISIBLE (child) &&
2244 !GTK_WIDGET_MAPPED (child))
2245 gtk_widget_map (child);
2249 gtk_container_map (GtkWidget *widget)
2251 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2253 gtk_container_forall (GTK_CONTAINER (widget),
2254 gtk_container_map_child,
2257 if (!GTK_WIDGET_NO_WINDOW (widget))
2258 gdk_window_show (widget->window);
2262 gtk_container_unmap (GtkWidget *widget)
2264 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2266 if (!GTK_WIDGET_NO_WINDOW (widget))
2267 gdk_window_hide (widget->window);
2269 gtk_container_forall (GTK_CONTAINER (widget),
2270 (GtkCallback)gtk_widget_unmap,
2275 * gtk_container_propagate_expose:
2276 * @container: a #GtkContainer
2277 * @child: a child of @container
2278 * @event: a expose event sent to container
2280 * When a container receives an expose event, it must send synthetic
2281 * expose events to all children that don't have their own #GdkWindows.
2282 * This function provides a convenient way of doing this. A container,
2283 * when it receives an expose event, calls gtk_container_propagate_expose()
2284 * once for each child, passing in the event the container received.
2286 * gtk_container_propagate_expose() takes care of deciding whether
2287 * an expose event needs to be sent to the child, intersecting
2288 * the event's area with the child area, and sending the event.
2290 * In most cases, a container can simply either simply inherit the
2291 * ::expose implementation from #GtkContainer, or, do some drawing
2292 * and then chain to the ::expose implementation from #GtkContainer.
2295 gtk_container_propagate_expose (GtkContainer *container,
2297 GdkEventExpose *event)
2299 GdkEventExpose child_event;
2301 g_return_if_fail (GTK_IS_CONTAINER (container));
2302 g_return_if_fail (GTK_IS_WIDGET (child));
2303 g_return_if_fail (event != NULL);
2305 g_assert (child->parent == GTK_WIDGET (container));
2307 if (GTK_WIDGET_DRAWABLE (child) &&
2308 GTK_WIDGET_NO_WINDOW (child) &&
2309 (child->window == event->window))
2311 child_event = *event;
2313 child_event.region = gtk_widget_region_intersect (child, event->region);
2314 if (!gdk_region_empty (child_event.region))
2316 gdk_region_get_clipbox (child_event.region, &child_event.area);
2317 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2319 gdk_region_destroy (child_event.region);