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,
84 static GList * gtk_container_focus_sort (GtkContainer *container,
86 GtkDirectionType direction);
88 static gboolean gtk_container_focus_move (GtkContainer *container,
90 GtkDirectionType direction);
91 static void gtk_container_children_callback (GtkWidget *widget,
92 gpointer client_data);
93 static void gtk_container_show_all (GtkWidget *widget);
94 static void gtk_container_hide_all (GtkWidget *widget);
95 static gint gtk_container_expose (GtkWidget *widget,
96 GdkEventExpose *event);
97 static void gtk_container_map (GtkWidget *widget);
98 static void gtk_container_unmap (GtkWidget *widget);
100 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
104 /* --- variables --- */
105 static const gchar *vadjustment_key = "gtk-vadjustment";
106 static guint vadjustment_key_id = 0;
107 static const gchar *hadjustment_key = "gtk-hadjustment";
108 static guint hadjustment_key_id = 0;
109 static GSList *container_resize_queue = NULL;
110 static guint container_signals[LAST_SIGNAL] = { 0 };
111 static GtkWidgetClass *parent_class = NULL;
112 extern GParamSpecPool *_gtk_widget_child_property_pool;
113 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
116 /* --- functions --- */
118 gtk_container_get_type (void)
120 static GtkType container_type = 0;
124 static 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_destroy */,
130 NULL /* class_data */,
131 sizeof (GtkContainer),
133 (GInstanceInitFunc) gtk_container_init,
134 NULL, /* value_table */
137 container_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer", &container_info, 0);
140 return container_type;
144 gtk_container_base_class_init (GtkContainerClass *class)
146 /* reset instance specifc class fields that don't get inherited */
147 class->set_child_property = NULL;
148 class->get_child_property = NULL;
152 gtk_container_base_class_finalize (GtkContainerClass *class)
156 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
157 for (node = list; node; node = node->next)
159 GParamSpec *pspec = node->data;
161 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
162 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
163 g_param_spec_unref (pspec);
169 gtk_container_class_init (GtkContainerClass *class)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
172 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
173 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
175 parent_class = g_type_class_peek_parent (class);
177 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
178 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
180 gobject_class->set_property = gtk_container_set_property;
181 gobject_class->get_property = gtk_container_get_property;
183 object_class->destroy = gtk_container_destroy;
185 widget_class->show_all = gtk_container_show_all;
186 widget_class->hide_all = gtk_container_hide_all;
187 widget_class->expose_event = gtk_container_expose;
188 widget_class->map = gtk_container_map;
189 widget_class->unmap = gtk_container_unmap;
190 widget_class->focus = gtk_container_focus;
192 class->add = gtk_container_add_unimplemented;
193 class->remove = gtk_container_remove_unimplemented;
194 class->check_resize = gtk_container_real_check_resize;
195 class->forall = NULL;
196 class->set_focus_child = gtk_container_real_set_focus_child;
197 class->child_type = NULL;
198 class->composite_name = gtk_container_child_default_composite_name;
200 g_object_class_install_property (gobject_class,
202 g_param_spec_enum ("resize_mode",
204 _("Specify how resize events are handled"),
205 GTK_TYPE_RESIZE_MODE,
208 g_object_class_install_property (gobject_class,
210 g_param_spec_uint ("border_width",
212 _("The width of the empty border outside the containers children."),
217 g_object_class_install_property (gobject_class,
219 g_param_spec_object ("child",
221 _("Can be used to add a new child to the container."),
224 container_signals[ADD] =
225 gtk_signal_new ("add",
227 GTK_CLASS_TYPE (object_class),
228 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
229 _gtk_marshal_VOID__OBJECT,
232 container_signals[REMOVE] =
233 gtk_signal_new ("remove",
235 GTK_CLASS_TYPE (object_class),
236 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
237 _gtk_marshal_VOID__OBJECT,
240 container_signals[CHECK_RESIZE] =
241 gtk_signal_new ("check_resize",
243 GTK_CLASS_TYPE (object_class),
244 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
245 _gtk_marshal_VOID__VOID,
247 container_signals[SET_FOCUS_CHILD] =
248 gtk_signal_new ("set-focus-child",
250 GTK_CLASS_TYPE (object_class),
251 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
252 _gtk_marshal_VOID__OBJECT,
258 gtk_container_child_type (GtkContainer *container)
261 GtkContainerClass *class;
263 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
265 class = GTK_CONTAINER_GET_CLASS (container);
266 if (class->child_type)
267 slot = class->child_type (container);
269 slot = GTK_TYPE_NONE;
274 /* --- GtkContainer child property mechanism --- */
276 container_get_child_property (GtkContainer *container,
281 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
283 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
287 container_set_child_property (GtkContainer *container,
291 GObjectNotifyQueue *nqueue)
293 GValue tmp_value = { 0, };
294 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
296 /* provide a copy to work from, convert (if necessary) and validate */
297 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
298 if (!g_value_transform (value, &tmp_value))
299 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
301 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
302 G_VALUE_TYPE_NAME (value));
303 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
305 gchar *contents = g_strdup_value_contents (value);
307 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
309 G_VALUE_TYPE_NAME (value),
311 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
316 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
317 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
319 g_value_unset (&tmp_value);
323 gtk_container_child_get_valist (GtkContainer *container,
325 const gchar *first_property_name,
330 g_return_if_fail (GTK_IS_CONTAINER (container));
331 g_return_if_fail (GTK_IS_WIDGET (child));
332 g_return_if_fail (child->parent == GTK_WIDGET (container));
334 g_object_ref (container);
335 g_object_ref (child);
337 name = first_property_name;
340 GValue value = { 0, };
344 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
346 G_OBJECT_TYPE (container),
350 g_warning ("%s: container class `%s' has no child property named `%s'",
352 G_OBJECT_TYPE_NAME (container),
356 if (!(pspec->flags & G_PARAM_READABLE))
358 g_warning ("%s: child property `%s' of container class `%s' is not readable",
361 G_OBJECT_TYPE_NAME (container));
364 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
365 container_get_child_property (container, child, pspec, &value);
366 G_VALUE_LCOPY (&value, var_args, 0, &error);
369 g_warning ("%s: %s", G_STRLOC, error);
371 g_value_unset (&value);
374 g_value_unset (&value);
375 name = va_arg (var_args, gchar*);
378 g_object_unref (child);
379 g_object_unref (container);
383 gtk_container_child_get_property (GtkContainer *container,
385 const gchar *property_name,
390 g_return_if_fail (GTK_IS_CONTAINER (container));
391 g_return_if_fail (GTK_IS_WIDGET (child));
392 g_return_if_fail (child->parent == GTK_WIDGET (container));
393 g_return_if_fail (property_name != NULL);
394 g_return_if_fail (G_IS_VALUE (value));
396 g_object_ref (container);
397 g_object_ref (child);
398 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
399 G_OBJECT_TYPE (container), TRUE);
401 g_warning ("%s: container class `%s' has no child property named `%s'",
403 G_OBJECT_TYPE_NAME (container),
405 else if (!(pspec->flags & G_PARAM_READABLE))
406 g_warning ("%s: child property `%s' of container class `%s' is not readable",
409 G_OBJECT_TYPE_NAME (container));
412 GValue *prop_value, tmp_value = { 0, };
414 /* auto-conversion of the callers value type
416 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
418 g_value_reset (value);
421 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
423 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
425 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
426 G_VALUE_TYPE_NAME (value));
427 g_object_unref (child);
428 g_object_unref (container);
433 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
434 prop_value = &tmp_value;
436 container_get_child_property (container, child, pspec, prop_value);
437 if (prop_value != value)
439 g_value_transform (prop_value, value);
440 g_value_unset (&tmp_value);
443 g_object_unref (child);
444 g_object_unref (container);
448 gtk_container_child_set_valist (GtkContainer *container,
450 const gchar *first_property_name,
454 GObjectNotifyQueue *nqueue;
457 g_return_if_fail (GTK_IS_CONTAINER (container));
458 g_return_if_fail (GTK_IS_WIDGET (child));
459 g_return_if_fail (child->parent == GTK_WIDGET (container));
461 g_object_ref (container);
462 g_object_ref (child);
464 object = G_OBJECT (container);
465 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
466 name = first_property_name;
469 GValue value = { 0, };
471 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
473 G_OBJECT_TYPE (container),
477 g_warning ("%s: container class `%s' has no child property named `%s'",
479 G_OBJECT_TYPE_NAME (container),
483 if (!(pspec->flags & G_PARAM_WRITABLE))
485 g_warning ("%s: child property `%s' of container class `%s' is not writable",
488 G_OBJECT_TYPE_NAME (container));
491 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
492 G_VALUE_COLLECT (&value, var_args, 0, &error);
495 g_warning ("%s: %s", G_STRLOC, error);
498 /* we purposely leak the value here, it might not be
499 * in a sane state if an error condition occoured
503 container_set_child_property (container, child, pspec, &value, nqueue);
504 g_value_unset (&value);
505 name = va_arg (var_args, gchar*);
507 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
509 g_object_unref (container);
510 g_object_unref (child);
514 gtk_container_child_set_property (GtkContainer *container,
516 const gchar *property_name,
520 GObjectNotifyQueue *nqueue;
523 g_return_if_fail (GTK_IS_CONTAINER (container));
524 g_return_if_fail (GTK_IS_WIDGET (child));
525 g_return_if_fail (child->parent == GTK_WIDGET (container));
526 g_return_if_fail (property_name != NULL);
527 g_return_if_fail (G_IS_VALUE (value));
529 g_object_ref (container);
530 g_object_ref (child);
532 object = G_OBJECT (container);
533 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
534 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
535 G_OBJECT_TYPE (container), TRUE);
537 g_warning ("%s: container class `%s' has no child property named `%s'",
539 G_OBJECT_TYPE_NAME (container),
541 else if (!(pspec->flags & G_PARAM_WRITABLE))
542 g_warning ("%s: child property `%s' of container class `%s' is not writable",
545 G_OBJECT_TYPE_NAME (container));
548 container_set_child_property (container, child, pspec, value, nqueue);
550 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
551 g_object_unref (container);
552 g_object_unref (child);
556 gtk_container_add_with_properties (GtkContainer *container,
558 const gchar *first_prop_name,
561 g_return_if_fail (GTK_IS_CONTAINER (container));
562 g_return_if_fail (GTK_IS_WIDGET (widget));
563 g_return_if_fail (widget->parent == NULL);
565 gtk_widget_ref (GTK_WIDGET (container));
566 gtk_widget_ref (widget);
567 gtk_widget_freeze_child_notify (widget);
569 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
574 va_start (var_args, first_prop_name);
575 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
579 gtk_widget_thaw_child_notify (widget);
580 gtk_widget_unref (widget);
581 gtk_widget_unref (GTK_WIDGET (container));
585 gtk_container_child_set (GtkContainer *container,
587 const gchar *first_prop_name,
592 g_return_if_fail (GTK_IS_CONTAINER (container));
593 g_return_if_fail (GTK_IS_WIDGET (child));
594 g_return_if_fail (child->parent == GTK_WIDGET (container));
596 va_start (var_args, first_prop_name);
597 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
602 gtk_container_child_get (GtkContainer *container,
604 const gchar *first_prop_name,
609 g_return_if_fail (container != NULL);
610 g_return_if_fail (GTK_IS_CONTAINER (container));
611 g_return_if_fail (child != NULL);
612 g_return_if_fail (GTK_IS_WIDGET (child));
613 g_return_if_fail (child->parent == GTK_WIDGET (container));
615 va_start (var_args, first_prop_name);
616 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
621 gtk_container_class_install_child_property (GtkContainerClass *class,
625 g_return_if_fail (GTK_IS_CONTAINER_CLASS (class));
626 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
627 if (pspec->flags & G_PARAM_WRITABLE)
628 g_return_if_fail (class->set_child_property != NULL);
629 if (pspec->flags & G_PARAM_READABLE)
630 g_return_if_fail (class->get_child_property != NULL);
631 g_return_if_fail (property_id > 0);
632 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
633 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
634 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
636 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
638 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
639 G_OBJECT_CLASS_NAME (class),
643 g_param_spec_ref (pspec);
644 g_param_spec_sink (pspec);
645 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
646 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (class));
650 gtk_container_class_find_child_property (GObjectClass *class,
651 const gchar *property_name)
653 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
654 g_return_val_if_fail (property_name != NULL, NULL);
656 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
658 G_OBJECT_CLASS_TYPE (class),
662 GParamSpec** /* free result */
663 gtk_container_class_list_child_properties (GObjectClass *class,
669 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
671 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
672 G_OBJECT_CLASS_TYPE (class),
681 gtk_container_add_unimplemented (GtkContainer *container,
684 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
688 gtk_container_remove_unimplemented (GtkContainer *container,
691 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
695 gtk_container_init (GtkContainer *container)
697 container->focus_child = NULL;
698 container->border_width = 0;
699 container->need_resize = FALSE;
700 container->resize_mode = GTK_RESIZE_PARENT;
701 container->reallocate_redraws = FALSE;
705 gtk_container_destroy (GtkObject *object)
707 GtkContainer *container;
709 g_return_if_fail (object != NULL);
710 g_return_if_fail (GTK_IS_CONTAINER (object));
712 container = GTK_CONTAINER (object);
714 if (GTK_CONTAINER_RESIZE_PENDING (container))
715 _gtk_container_dequeue_resize_handler (container);
717 /* do this before walking child widgets, to avoid
718 * removing children from focus chain one by one.
720 if (container->has_focus_chain)
721 gtk_container_unset_focus_chain (container);
723 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
725 if (GTK_OBJECT_CLASS (parent_class)->destroy)
726 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
730 gtk_container_set_property (GObject *object,
735 GtkContainer *container;
737 container = GTK_CONTAINER (object);
741 case PROP_BORDER_WIDTH:
742 gtk_container_set_border_width (container, g_value_get_uint (value));
744 case PROP_RESIZE_MODE:
745 gtk_container_set_resize_mode (container, g_value_get_enum (value));
748 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
751 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757 gtk_container_get_property (GObject *object,
762 GtkContainer *container;
764 container = GTK_CONTAINER (object);
768 case PROP_BORDER_WIDTH:
769 g_value_set_uint (value, container->border_width);
771 case PROP_RESIZE_MODE:
772 g_value_set_enum (value, container->resize_mode);
775 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
781 * gtk_container_set_border_width:
782 * @container: a #GtkContainer
783 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
784 * Valid values are in the range 0-65535 pixels.
786 * The border width of a container is the amount of space to leave
787 * around the outside of the container. The only exception to this is
788 * #GtkWindow; because toplevel windows can't leave space outside,
789 * they leave the space inside. The border is added on all sides of
790 * the container. To add space to only one side, one approach is to
791 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
792 * it a size, and place it on the side of the container as a spacer.
795 gtk_container_set_border_width (GtkContainer *container,
798 g_return_if_fail (container != NULL);
799 g_return_if_fail (GTK_IS_CONTAINER (container));
801 if (container->border_width != border_width)
803 container->border_width = border_width;
804 g_object_notify (G_OBJECT (container), "border_width");
806 if (GTK_WIDGET_REALIZED (container))
807 gtk_widget_queue_resize (GTK_WIDGET (container));
812 * gtk_container_get_border_width:
813 * @container: a #GtkContainer
815 * Retrieves the border width of the container. See
816 * gtk_container_set_border_width().
818 * Return value: the current border width
821 gtk_container_get_border_width (GtkContainer *container)
823 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
825 return container->border_width;
830 * @container: a #GtkContainer
831 * @widget: a widget to be placed inside @container
833 * Adds @widget to @container. Typically used for simple containers
834 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
835 * layout containers such as #GtkBox or #GtkTable, this function will
836 * pick default packing parameters that may not be correct. So
837 * consider functions such as gtk_box_pack_start() and
838 * gtk_table_attach() as an alternative to gtk_container_add() in
839 * those cases. A widget may be added to only one container at a time;
840 * you can't place the same widget inside two different containers.
843 gtk_container_add (GtkContainer *container,
846 g_return_if_fail (container != NULL);
847 g_return_if_fail (GTK_IS_CONTAINER (container));
848 g_return_if_fail (widget != NULL);
849 g_return_if_fail (GTK_IS_WIDGET (widget));
851 if (widget->parent != NULL)
853 g_warning ("Attempting to add a widget with type %s to a container of "
854 "type %s, but the widget is already inside a container of type %s, "
855 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
856 g_type_name (G_OBJECT_TYPE (widget)),
857 g_type_name (G_OBJECT_TYPE (container)),
858 g_type_name (G_OBJECT_TYPE (widget->parent)));
862 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
866 * gtk_container_remove:
867 * @container: a #GtkContainer
868 * @widget: a current child of @container
870 * Removes @widget from @container. @widget must be inside @container.
871 * Note that @container will own a reference to @widget, and that this
872 * may be the last reference held; so removing a widget from its
873 * container can destroy that widget. If you want to use @widget
874 * again, you need to add a reference to it while it's not inside
875 * a container, using g_object_ref().
878 gtk_container_remove (GtkContainer *container,
881 g_return_if_fail (container != NULL);
882 g_return_if_fail (GTK_IS_CONTAINER (container));
883 g_return_if_fail (widget != NULL);
884 g_return_if_fail (GTK_IS_WIDGET (widget));
885 g_return_if_fail (widget->parent == GTK_WIDGET (container));
887 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
891 _gtk_container_dequeue_resize_handler (GtkContainer *container)
893 g_return_if_fail (GTK_IS_CONTAINER (container));
894 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
896 container_resize_queue = g_slist_remove (container_resize_queue, container);
897 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
901 gtk_container_set_resize_mode (GtkContainer *container,
902 GtkResizeMode resize_mode)
904 g_return_if_fail (container != NULL);
905 g_return_if_fail (GTK_IS_CONTAINER (container));
906 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
908 if (GTK_WIDGET_TOPLEVEL (container) &&
909 resize_mode == GTK_RESIZE_PARENT)
911 resize_mode = GTK_RESIZE_QUEUE;
912 g_object_notify (G_OBJECT (container), "resize_mode");
915 if (container->resize_mode != resize_mode)
917 container->resize_mode = resize_mode;
919 gtk_widget_queue_resize (GTK_WIDGET (container));
920 g_object_notify (G_OBJECT (container), "resize_mode");
925 * gtk_container_get_resize_mode:
926 * @container: a #GtkContainer
928 * Returns the resize mode for the container. See
929 * gtk_container_set_resize_mode ().
931 * Return value: the current resize mode
934 gtk_container_get_resize_mode (GtkContainer *container)
936 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
938 return container->resize_mode;
942 gtk_container_set_reallocate_redraws (GtkContainer *container,
943 gboolean needs_redraws)
945 g_return_if_fail (GTK_IS_CONTAINER (container));
947 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
951 gtk_container_get_resize_container (GtkContainer *container)
955 widget = GTK_WIDGET (container);
957 while (widget->parent)
959 widget = widget->parent;
960 if (GTK_IS_RESIZE_CONTAINER (widget))
964 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
968 gtk_container_idle_sizer (gpointer data)
970 GDK_THREADS_ENTER ();
972 /* we may be invoked with a container_resize_queue of NULL, because
973 * queue_resize could have been adding an extra idle function while
974 * the queue still got processed. we better just ignore such case
975 * than trying to explicitely work around them with some extra flags,
976 * since it doesn't cause any actual harm.
978 while (container_resize_queue)
983 slist = container_resize_queue;
984 container_resize_queue = slist->next;
985 widget = slist->data;
986 g_slist_free_1 (slist);
988 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
989 gtk_container_check_resize (GTK_CONTAINER (widget));
992 gdk_window_process_all_updates ();
994 GDK_THREADS_LEAVE ();
1000 _gtk_container_queue_resize (GtkContainer *container)
1002 GtkContainer *resize_container;
1004 g_return_if_fail (container != NULL);
1005 g_return_if_fail (GTK_IS_CONTAINER (container));
1007 resize_container = gtk_container_get_resize_container (container);
1009 if (resize_container)
1011 GtkWidget *widget = GTK_WIDGET (container);
1013 while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1015 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1016 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1017 if (widget == GTK_WIDGET (resize_container))
1020 widget = widget->parent;
1023 if (GTK_WIDGET_VISIBLE (resize_container) &&
1024 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_DRAWABLE (resize_container)))
1026 switch (resize_container->resize_mode)
1028 case GTK_RESIZE_QUEUE:
1029 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1031 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1032 if (container_resize_queue == NULL)
1033 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1034 gtk_container_idle_sizer,
1036 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1040 case GTK_RESIZE_IMMEDIATE:
1041 gtk_container_check_resize (resize_container);
1044 case GTK_RESIZE_PARENT:
1045 /* Ignore, should not be reached */
1051 /* we need to let hidden resize containers know that something
1052 * changed while they where hidden (currently only evaluated by
1055 resize_container->need_resize = TRUE;
1061 gtk_container_check_resize (GtkContainer *container)
1063 g_return_if_fail (container != NULL);
1064 g_return_if_fail (GTK_IS_CONTAINER (container));
1066 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1070 gtk_container_real_check_resize (GtkContainer *container)
1073 GtkRequisition requisition;
1075 g_return_if_fail (container != NULL);
1076 g_return_if_fail (GTK_IS_CONTAINER (container));
1078 widget = GTK_WIDGET (container);
1080 gtk_widget_size_request (widget, &requisition);
1082 if (requisition.width > widget->allocation.width ||
1083 requisition.height > widget->allocation.height)
1085 if (GTK_IS_RESIZE_CONTAINER (container))
1086 gtk_widget_size_allocate (GTK_WIDGET (container),
1087 >K_WIDGET (container)->allocation);
1089 gtk_widget_queue_resize (widget);
1093 gtk_container_resize_children (container);
1097 /* The container hasn't changed size but one of its children
1098 * queued a resize request. Which means that the allocation
1099 * is not sufficient for the requisition of some child.
1100 * We've already performed a size request at this point,
1101 * so we simply need to reallocate and let the allocation
1102 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1105 gtk_container_resize_children (GtkContainer *container)
1109 /* resizing invariants:
1110 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1111 * containers that have an idle sizer pending must be flagged with
1114 g_return_if_fail (container != NULL);
1115 g_return_if_fail (GTK_IS_CONTAINER (container));
1117 widget = GTK_WIDGET (container);
1118 gtk_widget_size_allocate (widget, &widget->allocation);
1122 * gtk_container_forall:
1123 * @container: a #GtkContainer
1124 * @callback: a callback
1125 * @callback_data: callback user data
1127 * Invokes @callback on each child of @container, including children
1128 * that are considered "internal" (implementation details of the
1129 * container). "Internal" children generally weren't added by the user
1130 * of the container, but were added by the container implementation
1131 * itself. Most applications should use gtk_container_foreach(),
1132 * rather than gtk_container_forall().
1135 gtk_container_forall (GtkContainer *container,
1136 GtkCallback callback,
1137 gpointer callback_data)
1139 GtkContainerClass *class;
1141 g_return_if_fail (container != NULL);
1142 g_return_if_fail (GTK_IS_CONTAINER (container));
1143 g_return_if_fail (callback != NULL);
1145 class = GTK_CONTAINER_GET_CLASS (container);
1148 class->forall (container, TRUE, callback, callback_data);
1152 * gtk_container_foreach:
1153 * @container: a #GtkContainer
1154 * @callback: a callback
1155 * @callback_data: callback user data
1157 * Invokes @callback on each non-internal child of @container. See
1158 * gtk_container_forall() for details on what constitutes an
1159 * "internal" child. Most applications should use
1160 * gtk_container_foreach(), rather than gtk_container_forall().
1163 gtk_container_foreach (GtkContainer *container,
1164 GtkCallback callback,
1165 gpointer callback_data)
1167 GtkContainerClass *class;
1169 g_return_if_fail (container != NULL);
1170 g_return_if_fail (GTK_IS_CONTAINER (container));
1171 g_return_if_fail (callback != NULL);
1173 class = GTK_CONTAINER_GET_CLASS (container);
1176 class->forall (container, FALSE, callback, callback_data);
1179 typedef struct _GtkForeachData GtkForeachData;
1180 struct _GtkForeachData
1182 GtkObject *container;
1183 GtkCallbackMarshal callback;
1184 gpointer callback_data;
1188 gtk_container_foreach_unmarshal (GtkWidget *child,
1191 GtkForeachData *fdata = (GtkForeachData*) data;
1194 /* first argument */
1195 args[0].name = NULL;
1196 args[0].type = GTK_OBJECT_TYPE (child);
1197 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1199 /* location for return value */
1200 args[1].name = NULL;
1201 args[1].type = GTK_TYPE_NONE;
1203 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1207 gtk_container_foreach_full (GtkContainer *container,
1208 GtkCallback callback,
1209 GtkCallbackMarshal marshal,
1210 gpointer callback_data,
1211 GtkDestroyNotify notify)
1213 g_return_if_fail (container != NULL);
1214 g_return_if_fail (GTK_IS_CONTAINER (container));
1218 GtkForeachData fdata;
1220 fdata.container = GTK_OBJECT (container);
1221 fdata.callback = marshal;
1222 fdata.callback_data = callback_data;
1224 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1228 g_return_if_fail (callback != NULL);
1230 gtk_container_foreach (container, callback, &callback_data);
1234 notify (callback_data);
1238 gtk_container_set_focus_child (GtkContainer *container,
1241 g_return_if_fail (container != NULL);
1242 g_return_if_fail (GTK_IS_CONTAINER (container));
1244 g_return_if_fail (GTK_IS_WIDGET (widget));
1246 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1250 gtk_container_get_children (GtkContainer *container)
1256 gtk_container_foreach (container,
1257 gtk_container_children_callback,
1260 return g_list_reverse (children);
1264 gtk_container_child_position_callback (GtkWidget *widget,
1265 gpointer client_data)
1271 } *data = client_data;
1274 if (data->child == widget)
1275 data->index = data->i;
1279 gtk_container_child_default_composite_name (GtkContainer *container,
1289 /* fallback implementation */
1293 gtk_container_forall (container,
1294 gtk_container_child_position_callback,
1297 name = g_strdup_printf ("%s-%u",
1298 gtk_type_name (GTK_OBJECT_TYPE (child)),
1305 _gtk_container_child_composite_name (GtkContainer *container,
1308 g_return_val_if_fail (container != NULL, NULL);
1309 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1310 g_return_val_if_fail (child != NULL, NULL);
1311 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1312 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1314 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1316 static GQuark quark_composite_name = 0;
1319 if (!quark_composite_name)
1320 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1322 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1325 GtkContainerClass *class;
1327 class = GTK_CONTAINER_GET_CLASS (container);
1328 if (class->composite_name)
1329 name = class->composite_name (container, child);
1332 name = g_strdup (name);
1341 gtk_container_real_set_focus_child (GtkContainer *container,
1344 g_return_if_fail (container != NULL);
1345 g_return_if_fail (GTK_IS_CONTAINER (container));
1347 g_return_if_fail (GTK_IS_WIDGET (child));
1349 if (child != container->focus_child)
1351 if (container->focus_child)
1352 gtk_widget_unref (container->focus_child);
1353 container->focus_child = child;
1354 if (container->focus_child)
1355 gtk_widget_ref (container->focus_child);
1359 /* check for h/v adjustments
1361 if (container->focus_child)
1363 GtkAdjustment *adjustment;
1365 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1367 gtk_adjustment_clamp_page (adjustment,
1368 container->focus_child->allocation.y,
1369 (container->focus_child->allocation.y +
1370 container->focus_child->allocation.height));
1372 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1374 gtk_adjustment_clamp_page (adjustment,
1375 container->focus_child->allocation.x,
1376 (container->focus_child->allocation.x +
1377 container->focus_child->allocation.width));
1382 get_focus_chain (GtkContainer *container)
1384 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1388 gtk_container_focus (GtkWidget *widget,
1389 GtkDirectionType direction)
1392 GList *sorted_children;
1394 GtkContainer *container;
1396 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1398 container = GTK_CONTAINER (widget);
1402 if (GTK_WIDGET_CAN_FOCUS (container))
1404 if (!GTK_WIDGET_HAS_FOCUS (container))
1406 gtk_widget_grab_focus (GTK_WIDGET (container));
1412 /* Get a list of the containers children, allowing focus
1413 * chain to override.
1415 if (container->has_focus_chain)
1416 children = g_list_copy (get_focus_chain (container));
1418 children = gtk_container_get_children (container);
1420 if (container->has_focus_chain &&
1421 (direction == GTK_DIR_TAB_FORWARD ||
1422 direction == GTK_DIR_TAB_BACKWARD))
1424 sorted_children = g_list_copy (children);
1426 if (direction == GTK_DIR_TAB_BACKWARD)
1427 sorted_children = g_list_reverse (sorted_children);
1430 sorted_children = gtk_container_focus_sort (container, children, direction);
1432 return_val = gtk_container_focus_move (container, sorted_children, direction);
1434 g_list_free (sorted_children);
1435 g_list_free (children);
1442 tab_compare (gconstpointer a,
1445 const GtkWidget *child1 = a;
1446 const GtkWidget *child2 = b;
1448 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1449 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1453 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1454 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1456 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1459 return (y1 < y2) ? -1 : 1;
1463 gtk_container_focus_sort_tab (GtkContainer *container,
1465 GtkDirectionType direction)
1467 children = g_list_sort (children, tab_compare);
1469 /* if we are going backwards then reverse the order
1472 if (direction == GTK_DIR_TAB_BACKWARD)
1473 children = g_list_reverse (children);
1478 /* Get coordinates of @widget's allocation with respect to
1479 * allocation of @container.
1482 get_allocation_coords (GtkContainer *container,
1484 GdkRectangle *allocation)
1486 *allocation = widget->allocation;
1488 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1489 0, 0, &allocation->x, &allocation->y);
1492 /* Look for a child in @children that is intermediate between
1493 * the focus widget and container. This widget, if it exists,
1494 * acts as the starting widget for focus navigation.
1497 find_old_focus (GtkContainer *container,
1500 GList *tmp_list = children;
1503 GtkWidget *child = tmp_list->data;
1504 GtkWidget *widget = child;
1506 while (widget && widget != (GtkWidget *)container)
1508 GtkWidget *parent = widget->parent;
1509 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1518 tmp_list = tmp_list->next;
1525 old_focus_coords (GtkContainer *container,
1526 GdkRectangle *old_focus_rect)
1528 GtkWidget *widget = GTK_WIDGET (container);
1529 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1531 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1533 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1535 return get_allocation_coords (container, old_focus, old_focus_rect);
1541 typedef struct _CompareInfo CompareInfo;
1545 GtkContainer *container;
1552 up_down_compare (gconstpointer a,
1556 GdkRectangle allocation1;
1557 GdkRectangle allocation2;
1558 CompareInfo *compare = data;
1561 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1562 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1564 y1 = allocation1.y + allocation1.height / 2;
1565 y2 = allocation2.y + allocation2.height / 2;
1569 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1570 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1572 if (compare->reverse)
1573 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1575 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1578 return (y1 < y2) ? -1 : 1;
1582 gtk_container_focus_sort_up_down (GtkContainer *container,
1584 GtkDirectionType direction)
1586 CompareInfo compare;
1588 GtkWidget *old_focus;
1590 compare.container = container;
1591 compare.reverse = (direction == GTK_DIR_UP);
1593 old_focus = find_old_focus (container, children);
1596 GdkRectangle old_allocation;
1601 /* Delete widgets from list that don't match minimum criteria */
1603 get_allocation_coords (container, old_focus, &old_allocation);
1605 compare_x1 = old_allocation.x;
1606 compare_x2 = old_allocation.x + old_allocation.width;
1608 if (direction == GTK_DIR_UP)
1609 compare_y = old_allocation.y;
1611 compare_y = old_allocation.y + old_allocation.height;
1613 tmp_list = children;
1616 GtkWidget *child = tmp_list->data;
1617 GList *next = tmp_list->next;
1618 gint child_x1, child_x2;
1619 GdkRectangle child_allocation;
1621 if (child != old_focus)
1623 get_allocation_coords (container, child, &child_allocation);
1625 child_x1 = child_allocation.x;
1626 child_x2 = child_allocation.x + child_allocation.width;
1628 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1629 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1630 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1632 children = g_list_delete_link (children, tmp_list);
1639 compare.x = (compare_x1 + compare_x2) / 2;
1640 compare.y = old_allocation.y + old_allocation.height / 2;
1644 /* No old focus widget, need to figure out starting x,y some other way
1646 GtkWidget *widget = GTK_WIDGET (container);
1647 GdkRectangle old_focus_rect;
1649 if (old_focus_coords (container, &old_focus_rect))
1651 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1655 if (GTK_WIDGET_NO_WINDOW (widget))
1656 compare.x = widget->allocation.x + widget->allocation.width / 2;
1658 compare.x = widget->allocation.width / 2;
1661 if (GTK_WIDGET_NO_WINDOW (widget))
1662 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1664 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1667 children = g_list_sort_with_data (children, up_down_compare, &compare);
1669 if (compare.reverse)
1670 children = g_list_reverse (children);
1676 left_right_compare (gconstpointer a,
1680 GdkRectangle allocation1;
1681 GdkRectangle allocation2;
1682 CompareInfo *compare = data;
1685 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1686 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1688 x1 = allocation1.x + allocation1.width / 2;
1689 x2 = allocation2.x + allocation2.width / 2;
1693 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1694 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1696 if (compare->reverse)
1697 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1699 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1702 return (x1 < x2) ? -1 : 1;
1706 gtk_container_focus_sort_left_right (GtkContainer *container,
1708 GtkDirectionType direction)
1710 CompareInfo compare;
1712 GtkWidget *old_focus;
1714 compare.container = container;
1715 compare.reverse = (direction == GTK_DIR_LEFT);
1717 old_focus = find_old_focus (container, children);
1720 GdkRectangle old_allocation;
1726 /* Delete widgets from list that don't match minimum criteria */
1728 get_allocation_coords (container, old_focus, &old_allocation);
1730 compare_y1 = old_allocation.y;
1731 compare_y2 = old_allocation.y + old_allocation.height;
1733 if (direction == GTK_DIR_LEFT)
1734 compare_x = old_allocation.x;
1736 compare_x = old_allocation.x + old_allocation.width;
1738 tmp_list = children;
1741 GtkWidget *child = tmp_list->data;
1742 GList *next = tmp_list->next;
1743 gint child_y1, child_y2;
1744 GdkRectangle child_allocation;
1746 if (child != old_focus)
1748 get_allocation_coords (container, child, &child_allocation);
1750 child_y1 = child_allocation.y;
1751 child_y2 = child_allocation.y + child_allocation.height;
1753 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1754 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1755 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1757 children = g_list_delete_link (children, tmp_list);
1764 compare.y = (compare_y1 + compare_y2) / 2;
1765 compare.x = old_allocation.x + old_allocation.width / 2;
1769 /* No old focus widget, need to figure out starting x,y some other way
1771 GtkWidget *widget = GTK_WIDGET (container);
1772 GdkRectangle old_focus_rect;
1774 if (old_focus_coords (container, &old_focus_rect))
1776 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1780 if (GTK_WIDGET_NO_WINDOW (widget))
1781 compare.y = widget->allocation.y + widget->allocation.height / 2;
1783 compare.y = widget->allocation.height / 2;
1786 if (GTK_WIDGET_NO_WINDOW (widget))
1787 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1789 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1792 children = g_list_sort_with_data (children, left_right_compare, &compare);
1794 if (compare.reverse)
1795 children = g_list_reverse (children);
1801 * gtk_container_focus_sort:
1802 * @container: a #GtkContainer
1803 * @children: a list of descendents of @container (they don't
1804 * have to be direct children.
1805 * @direction: focus direction
1807 * Sorts @children in the correct order for focusing with
1808 * direction type @direction.
1810 * Return value: a copy of @children, sorted in correct focusing order,
1811 * with children that aren't suitable for focusing in this direction
1815 gtk_container_focus_sort (GtkContainer *container,
1817 GtkDirectionType direction)
1819 children = g_list_copy (children);
1823 case GTK_DIR_TAB_FORWARD:
1824 case GTK_DIR_TAB_BACKWARD:
1825 return gtk_container_focus_sort_tab (container, children, direction);
1828 return gtk_container_focus_sort_up_down (container, children, direction);
1831 return gtk_container_focus_sort_left_right (container, children, direction);
1834 g_assert_not_reached ();
1840 gtk_container_focus_move (GtkContainer *container,
1842 GtkDirectionType direction)
1844 GtkWidget *focus_child;
1847 focus_child = container->focus_child;
1851 child = children->data;
1852 children = children->next;
1859 if (focus_child == child)
1863 if (gtk_widget_child_focus (child, direction))
1867 else if (GTK_WIDGET_DRAWABLE (child) &&
1868 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1870 if (gtk_widget_child_focus (child, direction))
1880 gtk_container_children_callback (GtkWidget *widget,
1881 gpointer client_data)
1885 children = (GList**) client_data;
1886 *children = g_list_prepend (*children, widget);
1890 chain_widget_destroyed (GtkWidget *widget,
1893 GtkContainer *container;
1896 container = GTK_CONTAINER (user_data);
1898 chain = g_object_get_data (G_OBJECT (container),
1899 "gtk-container-focus-chain");
1901 chain = g_list_remove (chain, widget);
1903 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1904 chain_widget_destroyed,
1907 g_object_set_data (G_OBJECT (container),
1908 "gtk-container-focus-chain",
1913 gtk_container_set_focus_chain (GtkContainer *container,
1914 GList *focusable_widgets)
1919 g_return_if_fail (GTK_IS_CONTAINER (container));
1921 if (container->has_focus_chain)
1922 gtk_container_unset_focus_chain (container);
1924 container->has_focus_chain = TRUE;
1927 tmp_list = focusable_widgets;
1928 while (tmp_list != NULL)
1930 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
1932 /* In principle each widget in the chain should be a descendant
1933 * of the container, but we don't want to check that here, it's
1934 * expensive and also it's allowed to set the focus chain before
1935 * you pack the widgets, or have a widget in the chain that isn't
1936 * always packed. So we check for ancestor during actual traversal.
1939 chain = g_list_prepend (chain, tmp_list->data);
1941 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
1943 GTK_SIGNAL_FUNC (chain_widget_destroyed),
1946 tmp_list = g_list_next (tmp_list);
1949 chain = g_list_reverse (chain);
1951 g_object_set_data (G_OBJECT (container),
1952 "gtk-container-focus-chain",
1957 * gtk_container_get_focus_chain:
1958 * @container: a #GtkContainer
1959 * @focusable_widgets: location to store the focus chain of the
1960 * container, or %NULL. You should free this list
1961 * using g_list_free() when you are done with it, however
1962 * no additional reference count is added to the
1963 * individual widgets in the focus chain.
1965 * Retrieve the focus chain of the container, if one has been
1966 * set explicitely. If no focus chain has been explicitely
1967 * set, GTK+ computes the focus chain based on the positions
1968 * of the children. In that case, GTK+ stores %NULL in
1969 * @focusable_widgets and returns %FALSE.
1971 * Return value: %TRUE if the focus chain of the container,
1972 * has been set explicitely.
1975 gtk_container_get_focus_chain (GtkContainer *container,
1976 GList **focus_chain)
1978 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1982 if (container->has_focus_chain)
1983 *focus_chain = g_list_copy (get_focus_chain (container));
1985 *focus_chain = NULL;
1988 return container->has_focus_chain;
1992 gtk_container_unset_focus_chain (GtkContainer *container)
1994 g_return_if_fail (GTK_IS_CONTAINER (container));
1996 if (container->has_focus_chain)
2001 chain = get_focus_chain (container);
2003 container->has_focus_chain = FALSE;
2005 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2009 while (tmp_list != NULL)
2011 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2012 chain_widget_destroyed,
2015 tmp_list = g_list_next (tmp_list);
2018 g_list_free (chain);
2023 gtk_container_set_focus_vadjustment (GtkContainer *container,
2024 GtkAdjustment *adjustment)
2026 g_return_if_fail (container != NULL);
2027 g_return_if_fail (GTK_IS_CONTAINER (container));
2029 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2032 gtk_object_ref (GTK_OBJECT(adjustment));
2034 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2037 (GtkDestroyNotify) gtk_object_unref);
2041 * gtk_container_get_focus_vadjustment:
2042 * @container: a #GtkContainer
2044 * Retrieves the vertical focus adjustment for the container. See
2045 * gtk_container_set_focus_vadjustment ().
2047 * Return value: the vertical focus adjustment, or %NULL if
2048 * none has been set.
2051 gtk_container_get_focus_vadjustment (GtkContainer *container)
2053 GtkAdjustment *vadjustment;
2055 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2057 vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2058 vadjustment_key_id);
2064 gtk_container_set_focus_hadjustment (GtkContainer *container,
2065 GtkAdjustment *adjustment)
2067 g_return_if_fail (container != NULL);
2068 g_return_if_fail (GTK_IS_CONTAINER (container));
2070 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2073 gtk_object_ref (GTK_OBJECT (adjustment));
2075 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2078 (GtkDestroyNotify) gtk_object_unref);
2082 * gtk_container_get_focus_hadjustment:
2083 * @container: a #GtkContainer
2085 * Retrieves the horizontal focus adjustment for the container. See
2086 * gtk_container_set_focus_hadjustment ().
2088 * Return value: the horizontal focus adjustment, or %NULL if none
2089 * none has been set.
2092 gtk_container_get_focus_hadjustment (GtkContainer *container)
2094 GtkAdjustment *hadjustment;
2096 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2098 hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2099 hadjustment_key_id);
2106 gtk_container_show_all (GtkWidget *widget)
2108 g_return_if_fail (widget != NULL);
2109 g_return_if_fail (GTK_IS_CONTAINER (widget));
2111 gtk_container_foreach (GTK_CONTAINER (widget),
2112 (GtkCallback) gtk_widget_show_all,
2114 gtk_widget_show (widget);
2118 gtk_container_hide_all (GtkWidget *widget)
2120 g_return_if_fail (widget != NULL);
2121 g_return_if_fail (GTK_IS_CONTAINER (widget));
2123 gtk_widget_hide (widget);
2124 gtk_container_foreach (GTK_CONTAINER (widget),
2125 (GtkCallback) gtk_widget_hide_all,
2131 gtk_container_expose_child (GtkWidget *child,
2132 gpointer client_data)
2135 GtkWidget *container;
2136 GdkEventExpose *event;
2137 } *data = client_data;
2139 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2145 gtk_container_expose (GtkWidget *widget,
2146 GdkEventExpose *event)
2149 GtkWidget *container;
2150 GdkEventExpose *event;
2153 g_return_val_if_fail (widget != NULL, FALSE);
2154 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2155 g_return_val_if_fail (event != NULL, FALSE);
2158 if (GTK_WIDGET_DRAWABLE (widget))
2160 data.container = widget;
2163 gtk_container_forall (GTK_CONTAINER (widget),
2164 gtk_container_expose_child,
2172 gtk_container_map_child (GtkWidget *child,
2173 gpointer client_data)
2175 if (GTK_WIDGET_VISIBLE (child) &&
2176 GTK_WIDGET_CHILD_VISIBLE (child) &&
2177 !GTK_WIDGET_MAPPED (child))
2178 gtk_widget_map (child);
2182 gtk_container_map (GtkWidget *widget)
2184 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2186 gtk_container_forall (GTK_CONTAINER (widget),
2187 gtk_container_map_child,
2190 if (!GTK_WIDGET_NO_WINDOW (widget))
2191 gdk_window_show (widget->window);
2195 gtk_container_unmap (GtkWidget *widget)
2197 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2199 if (!GTK_WIDGET_NO_WINDOW (widget))
2200 gdk_window_hide (widget->window);
2202 gtk_container_forall (GTK_CONTAINER (widget),
2203 (GtkCallback)gtk_widget_unmap,
2208 * gtk_container_propagate_expose:
2209 * @container: a #GtkContainer
2210 * @child: a child of @container
2211 * @event: a expose event sent to container
2213 * When a container receives an expose event, it must send synthetic
2214 * expose events to all children that don't have their own GdkWindows.
2215 * This function provides a convenient way of doing this. A container,
2216 * when it receives an expose event, gtk_container_propagate_expose()
2217 * once for each child, passing in the event the container received.
2219 * gtk_container_propagate expose() takes care of deciding whether
2220 * an expose event needs to be sent to the child, intersecting
2221 * the event's area with the child area, and sending the event.
2223 * In most cases, a container can simply either simply inherit the
2224 * ::expose implementation from GtkContainer, or, do some drawing
2225 * and then chain to the ::expose implementation from GtkContainer.
2228 gtk_container_propagate_expose (GtkContainer *container,
2230 GdkEventExpose *event)
2232 GdkEventExpose child_event;
2234 g_return_if_fail (GTK_IS_CONTAINER (container));
2235 g_return_if_fail (GTK_IS_WIDGET (child));
2236 g_return_if_fail (event != NULL);
2238 g_assert (child->parent == GTK_WIDGET (container));
2240 if (GTK_WIDGET_DRAWABLE (child) &&
2241 GTK_WIDGET_NO_WINDOW (child) &&
2242 (child->window == event->window))
2244 child_event = *event;
2246 child_event.region = gtk_widget_region_intersect (child, event->region);
2247 if (!gdk_region_empty (child_event.region))
2249 gdk_region_get_clipbox (child_event.region, &child_event.area);
2250 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2252 gdk_region_destroy (child_event.region);