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 "gtkwindow.h"
37 #include <gobject/gobjectnotifyqueue.c>
38 #include <gobject/gvaluecollector.h>
56 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
57 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
60 /* --- prototypes --- */
61 static void gtk_container_base_class_init (GtkContainerClass *klass);
62 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
63 static void gtk_container_class_init (GtkContainerClass *klass);
64 static void gtk_container_init (GtkContainer *container);
65 static void gtk_container_destroy (GtkObject *object);
66 static void gtk_container_set_property (GObject *object,
70 static void gtk_container_get_property (GObject *object,
74 static void gtk_container_add_unimplemented (GtkContainer *container,
76 static void gtk_container_remove_unimplemented (GtkContainer *container,
78 static void gtk_container_real_check_resize (GtkContainer *container);
79 static gboolean gtk_container_focus (GtkWidget *widget,
80 GtkDirectionType direction);
81 static void gtk_container_real_set_focus_child (GtkContainer *container,
83 static gboolean gtk_container_focus_tab (GtkContainer *container,
85 GtkDirectionType direction);
86 static gboolean gtk_container_focus_up_down (GtkContainer *container,
88 GtkDirectionType direction);
89 static gboolean gtk_container_focus_left_right (GtkContainer *container,
91 GtkDirectionType direction);
92 static gboolean gtk_container_focus_move (GtkContainer *container,
94 GtkDirectionType direction);
95 static void gtk_container_children_callback (GtkWidget *widget,
96 gpointer client_data);
97 static void gtk_container_show_all (GtkWidget *widget);
98 static void gtk_container_hide_all (GtkWidget *widget);
99 static gint gtk_container_expose (GtkWidget *widget,
100 GdkEventExpose *event);
101 static void gtk_container_map (GtkWidget *widget);
102 static void gtk_container_unmap (GtkWidget *widget);
104 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
108 /* --- variables --- */
109 static const gchar *vadjustment_key = "gtk-vadjustment";
110 static guint vadjustment_key_id = 0;
111 static const gchar *hadjustment_key = "gtk-hadjustment";
112 static guint hadjustment_key_id = 0;
113 static GSList *container_resize_queue = NULL;
114 static guint container_signals[LAST_SIGNAL] = { 0 };
115 static GtkWidgetClass *parent_class = NULL;
116 extern GParamSpecPool *_gtk_widget_child_property_pool;
117 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
120 /* --- functions --- */
122 gtk_container_get_type (void)
124 static GtkType container_type = 0;
128 static GTypeInfo container_info = {
129 sizeof (GtkContainerClass),
130 (GBaseInitFunc) gtk_container_base_class_init,
131 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
132 (GClassInitFunc) gtk_container_class_init,
133 NULL /* class_destroy */,
134 NULL /* class_data */,
135 sizeof (GtkContainer),
137 (GInstanceInitFunc) gtk_container_init,
138 NULL, /* value_table */
141 container_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer", &container_info, 0);
144 return container_type;
148 gtk_container_base_class_init (GtkContainerClass *class)
150 /* reset instance specifc class fields that don't get inherited */
151 class->set_child_property = NULL;
152 class->get_child_property = NULL;
156 gtk_container_base_class_finalize (GtkContainerClass *class)
160 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
161 for (node = list; node; node = node->next)
163 GParamSpec *pspec = node->data;
165 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
166 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
167 g_param_spec_unref (pspec);
173 gtk_container_class_init (GtkContainerClass *class)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
176 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
177 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
179 parent_class = g_type_class_peek_parent (class);
181 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
182 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
184 gobject_class->set_property = gtk_container_set_property;
185 gobject_class->get_property = gtk_container_get_property;
187 object_class->destroy = gtk_container_destroy;
189 widget_class->show_all = gtk_container_show_all;
190 widget_class->hide_all = gtk_container_hide_all;
191 widget_class->expose_event = gtk_container_expose;
192 widget_class->map = gtk_container_map;
193 widget_class->unmap = gtk_container_unmap;
194 widget_class->focus = gtk_container_focus;
196 class->add = gtk_container_add_unimplemented;
197 class->remove = gtk_container_remove_unimplemented;
198 class->check_resize = gtk_container_real_check_resize;
199 class->forall = NULL;
200 class->set_focus_child = gtk_container_real_set_focus_child;
201 class->child_type = NULL;
202 class->composite_name = gtk_container_child_default_composite_name;
204 g_object_class_install_property (gobject_class,
206 g_param_spec_enum ("resize_mode",
208 _("Specify how resize events are handled"),
209 GTK_TYPE_RESIZE_MODE,
212 g_object_class_install_property (gobject_class,
214 g_param_spec_uint ("border_width",
216 _("The width of the empty border outside the containers children."),
221 g_object_class_install_property (gobject_class,
223 g_param_spec_object ("child",
225 _("Can be used to add a new child to the container."),
228 container_signals[ADD] =
229 gtk_signal_new ("add",
231 GTK_CLASS_TYPE (object_class),
232 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
233 gtk_marshal_VOID__OBJECT,
236 container_signals[REMOVE] =
237 gtk_signal_new ("remove",
239 GTK_CLASS_TYPE (object_class),
240 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
241 gtk_marshal_VOID__OBJECT,
244 container_signals[CHECK_RESIZE] =
245 gtk_signal_new ("check_resize",
247 GTK_CLASS_TYPE (object_class),
248 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
249 gtk_marshal_VOID__VOID,
251 container_signals[SET_FOCUS_CHILD] =
252 gtk_signal_new ("set-focus-child",
254 GTK_CLASS_TYPE (object_class),
255 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
256 gtk_marshal_VOID__OBJECT,
262 gtk_container_child_type (GtkContainer *container)
265 GtkContainerClass *class;
267 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
269 class = GTK_CONTAINER_GET_CLASS (container);
270 if (class->child_type)
271 slot = class->child_type (container);
273 slot = GTK_TYPE_NONE;
278 /* --- GtkContainer child property mechanism --- */
280 container_get_child_property (GtkContainer *container,
285 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
287 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
291 container_set_child_property (GtkContainer *container,
295 GObjectNotifyQueue *nqueue)
297 GValue tmp_value = { 0, };
298 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
300 /* provide a copy to work from, convert (if necessary) and validate */
301 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
302 if (!g_value_transform (value, &tmp_value))
303 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
305 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
306 G_VALUE_TYPE_NAME (value));
307 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
309 gchar *contents = g_strdup_value_contents (value);
311 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
313 G_VALUE_TYPE_NAME (value),
315 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
320 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
321 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
323 g_value_unset (&tmp_value);
327 gtk_container_child_get_valist (GtkContainer *container,
329 const gchar *first_property_name,
334 g_return_if_fail (GTK_IS_CONTAINER (container));
335 g_return_if_fail (GTK_IS_WIDGET (child));
336 g_return_if_fail (child->parent == GTK_WIDGET (container));
338 g_object_ref (container);
339 g_object_ref (child);
341 name = first_property_name;
344 GValue value = { 0, };
348 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
350 G_OBJECT_TYPE (container),
354 g_warning ("%s: container class `%s' has no child property named `%s'",
356 G_OBJECT_TYPE_NAME (container),
360 if (!(pspec->flags & G_PARAM_READABLE))
362 g_warning ("%s: child property `%s' of container class `%s' is not readable",
365 G_OBJECT_TYPE_NAME (container));
368 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
369 container_get_child_property (container, child, pspec, &value);
370 G_VALUE_LCOPY (&value, var_args, 0, &error);
373 g_warning ("%s: %s", G_STRLOC, error);
375 g_value_unset (&value);
378 g_value_unset (&value);
379 name = va_arg (var_args, gchar*);
382 g_object_unref (child);
383 g_object_unref (container);
387 gtk_container_child_get_property (GtkContainer *container,
389 const gchar *property_name,
394 g_return_if_fail (GTK_IS_CONTAINER (container));
395 g_return_if_fail (GTK_IS_WIDGET (child));
396 g_return_if_fail (child->parent == GTK_WIDGET (container));
397 g_return_if_fail (property_name != NULL);
398 g_return_if_fail (G_IS_VALUE (value));
400 g_object_ref (container);
401 g_object_ref (child);
402 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
403 G_OBJECT_TYPE (container), TRUE);
405 g_warning ("%s: container class `%s' has no child property named `%s'",
407 G_OBJECT_TYPE_NAME (container),
409 else if (!(pspec->flags & G_PARAM_READABLE))
410 g_warning ("%s: child property `%s' of container class `%s' is not readable",
413 G_OBJECT_TYPE_NAME (container));
416 GValue *prop_value, tmp_value = { 0, };
418 /* auto-conversion of the callers value type
420 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
422 g_value_reset (value);
425 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
427 g_warning ("can't retrive child property `%s' of type `%s' as value of type `%s'",
429 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
430 G_VALUE_TYPE_NAME (value));
431 g_object_unref (child);
432 g_object_unref (container);
437 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
438 prop_value = &tmp_value;
440 container_get_child_property (container, child, pspec, prop_value);
441 if (prop_value != value)
443 g_value_transform (prop_value, value);
444 g_value_unset (&tmp_value);
447 g_object_unref (child);
448 g_object_unref (container);
452 gtk_container_child_set_valist (GtkContainer *container,
454 const gchar *first_property_name,
458 GObjectNotifyQueue *nqueue;
461 g_return_if_fail (GTK_IS_CONTAINER (container));
462 g_return_if_fail (GTK_IS_WIDGET (child));
463 g_return_if_fail (child->parent == GTK_WIDGET (container));
465 g_object_ref (container);
466 g_object_ref (child);
468 object = G_OBJECT (container);
469 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
470 name = first_property_name;
473 GValue value = { 0, };
475 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
477 G_OBJECT_TYPE (container),
481 g_warning ("%s: container class `%s' has no child property named `%s'",
483 G_OBJECT_TYPE_NAME (container),
487 if (!(pspec->flags & G_PARAM_WRITABLE))
489 g_warning ("%s: child property `%s' of container class `%s' is not writable",
492 G_OBJECT_TYPE_NAME (container));
495 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
496 G_VALUE_COLLECT (&value, var_args, 0, &error);
499 g_warning ("%s: %s", G_STRLOC, error);
502 /* we purposely leak the value here, it might not be
503 * in a sane state if an error condition occoured
507 container_set_child_property (container, child, pspec, &value, nqueue);
508 g_value_unset (&value);
509 name = va_arg (var_args, gchar*);
511 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
513 g_object_unref (container);
514 g_object_unref (child);
518 gtk_container_child_set_property (GtkContainer *container,
520 const gchar *property_name,
524 GObjectNotifyQueue *nqueue;
527 g_return_if_fail (GTK_IS_CONTAINER (container));
528 g_return_if_fail (GTK_IS_WIDGET (child));
529 g_return_if_fail (child->parent == GTK_WIDGET (container));
530 g_return_if_fail (property_name != NULL);
531 g_return_if_fail (G_IS_VALUE (value));
533 g_object_ref (container);
534 g_object_ref (child);
536 object = G_OBJECT (container);
537 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
538 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
539 G_OBJECT_TYPE (container), TRUE);
541 g_warning ("%s: container class `%s' has no child property named `%s'",
543 G_OBJECT_TYPE_NAME (container),
545 else if (!(pspec->flags & G_PARAM_WRITABLE))
546 g_warning ("%s: child property `%s' of container class `%s' is not writable",
549 G_OBJECT_TYPE_NAME (container));
552 container_set_child_property (container, child, pspec, value, nqueue);
554 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
555 g_object_unref (container);
556 g_object_unref (child);
560 gtk_container_add_with_properties (GtkContainer *container,
562 const gchar *first_prop_name,
565 g_return_if_fail (GTK_IS_CONTAINER (container));
566 g_return_if_fail (GTK_IS_WIDGET (widget));
567 g_return_if_fail (widget->parent == NULL);
569 gtk_widget_ref (GTK_WIDGET (container));
570 gtk_widget_ref (widget);
571 gtk_widget_freeze_child_notify (widget);
573 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
578 va_start (var_args, first_prop_name);
579 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
583 gtk_widget_thaw_child_notify (widget);
584 gtk_widget_unref (widget);
585 gtk_widget_unref (GTK_WIDGET (container));
589 gtk_container_child_set (GtkContainer *container,
591 const gchar *first_prop_name,
596 g_return_if_fail (GTK_IS_CONTAINER (container));
597 g_return_if_fail (GTK_IS_WIDGET (child));
598 g_return_if_fail (child->parent == GTK_WIDGET (container));
600 va_start (var_args, first_prop_name);
601 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
606 gtk_container_child_get (GtkContainer *container,
608 const gchar *first_prop_name,
613 g_return_if_fail (container != NULL);
614 g_return_if_fail (GTK_IS_CONTAINER (container));
615 g_return_if_fail (child != NULL);
616 g_return_if_fail (GTK_IS_WIDGET (child));
617 g_return_if_fail (child->parent == GTK_WIDGET (container));
619 va_start (var_args, first_prop_name);
620 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
625 gtk_container_class_install_child_property (GtkContainerClass *class,
629 g_return_if_fail (GTK_IS_CONTAINER_CLASS (class));
630 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
631 if (pspec->flags & G_PARAM_WRITABLE)
632 g_return_if_fail (class->set_child_property != NULL);
633 if (pspec->flags & G_PARAM_READABLE)
634 g_return_if_fail (class->get_child_property != NULL);
635 g_return_if_fail (property_id > 0);
636 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
637 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
638 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
640 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
642 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
643 G_OBJECT_CLASS_NAME (class),
647 g_param_spec_ref (pspec);
648 g_param_spec_sink (pspec);
649 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
650 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (class));
654 gtk_container_class_find_child_property (GObjectClass *class,
655 const gchar *property_name)
657 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
658 g_return_val_if_fail (property_name != NULL, NULL);
660 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
662 G_OBJECT_CLASS_TYPE (class),
666 GParamSpec** /* free result */
667 gtk_container_class_list_child_properties (GObjectClass *class,
673 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
675 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
676 G_OBJECT_CLASS_TYPE (class),
685 gtk_container_add_unimplemented (GtkContainer *container,
688 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
692 gtk_container_remove_unimplemented (GtkContainer *container,
695 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
699 gtk_container_init (GtkContainer *container)
701 container->focus_child = NULL;
702 container->border_width = 0;
703 container->need_resize = FALSE;
704 container->resize_mode = GTK_RESIZE_PARENT;
705 container->reallocate_redraws = FALSE;
706 container->resize_widgets = NULL;
710 gtk_container_destroy (GtkObject *object)
712 GtkContainer *container;
714 g_return_if_fail (object != NULL);
715 g_return_if_fail (GTK_IS_CONTAINER (object));
717 container = GTK_CONTAINER (object);
719 if (GTK_CONTAINER_RESIZE_PENDING (container))
720 _gtk_container_dequeue_resize_handler (container);
721 if (container->resize_widgets)
722 gtk_container_clear_resize_widgets (container);
724 /* do this before walking child widgets, to avoid
725 * removing children from focus chain one by one.
727 if (container->has_focus_chain)
728 gtk_container_unset_focus_chain (container);
730 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
732 if (GTK_OBJECT_CLASS (parent_class)->destroy)
733 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
737 gtk_container_set_property (GObject *object,
742 GtkContainer *container;
744 container = GTK_CONTAINER (object);
748 case PROP_BORDER_WIDTH:
749 gtk_container_set_border_width (container, g_value_get_uint (value));
751 case PROP_RESIZE_MODE:
752 gtk_container_set_resize_mode (container, g_value_get_enum (value));
755 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
758 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
764 gtk_container_get_property (GObject *object,
769 GtkContainer *container;
771 container = GTK_CONTAINER (object);
775 case PROP_BORDER_WIDTH:
776 g_value_set_uint (value, container->border_width);
778 case PROP_RESIZE_MODE:
779 g_value_set_enum (value, container->resize_mode);
782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
788 * gtk_container_set_border_width:
789 * @container: a #GtkContainer
790 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container
792 * The border width of a container is the amount of space to leave
793 * around the outside of the container. The only exception to this is
794 * #GtkWindow; because toplevel windows can't leave space outside,
795 * they leave the space inside. The border is added on all sides of
796 * the container. To add space to only one side, one approach is to
797 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
798 * it a size, and place it on the side of the container as a spacer.
802 gtk_container_set_border_width (GtkContainer *container,
805 g_return_if_fail (container != NULL);
806 g_return_if_fail (GTK_IS_CONTAINER (container));
808 if (container->border_width != border_width)
810 container->border_width = border_width;
811 g_object_notify (G_OBJECT (container), "border_width");
813 if (GTK_WIDGET_REALIZED (container))
814 gtk_widget_queue_resize (GTK_WIDGET (container));
819 * gtk_container_get_border_width:
820 * @container: a #GtkContainer
822 * Retrieves the border width of the container. See
823 * gtk_container_set_border_width().
825 * Return value: the current border width
828 gtk_container_get_border_width (GtkContainer *container)
830 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
832 return container->border_width;
837 * @container: a #GtkContainer
838 * @widget: a widget to be placed inside @container
840 * Adds @widget to @container. Typically used for simple containers
841 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
842 * layout containers such as #GtkBox or #GtkTable, this function will
843 * pick default packing parameters that may not be correct. So
844 * consider functions such as gtk_box_pack_start() and
845 * gtk_table_attach() as an alternative to gtk_container_add() in
846 * those cases. A widget may be added to only one container at a time;
847 * you can't place the same widget inside two different containers.
850 gtk_container_add (GtkContainer *container,
853 g_return_if_fail (container != NULL);
854 g_return_if_fail (GTK_IS_CONTAINER (container));
855 g_return_if_fail (widget != NULL);
856 g_return_if_fail (GTK_IS_WIDGET (widget));
858 if (widget->parent != NULL)
860 g_warning ("Attempting to add a widget with type %s to a container of "
861 "type %s, but the widget is already inside a container of type %s",
862 g_type_name (G_OBJECT_TYPE (widget)),
863 g_type_name (G_OBJECT_TYPE (container)),
864 g_type_name (G_OBJECT_TYPE (widget->parent)));
868 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
872 * gtk_container_remove:
873 * @container: a #GtkContainer
874 * @widget: a current child of @container
876 * Removes @widget from @container. @widget must be inside @container.
877 * Note that @container will own a reference to @widget, and that this
878 * may be the last reference held; so removing a widget from its
879 * container can destroy that widget. If you want to use @widget
880 * again, you need to add a reference to it while it's not inside
881 * a container, using g_object_ref().
884 gtk_container_remove (GtkContainer *container,
887 g_return_if_fail (container != NULL);
888 g_return_if_fail (GTK_IS_CONTAINER (container));
889 g_return_if_fail (widget != NULL);
890 g_return_if_fail (GTK_IS_WIDGET (widget));
891 g_return_if_fail (widget->parent == GTK_WIDGET (container));
893 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
897 _gtk_container_dequeue_resize_handler (GtkContainer *container)
899 g_return_if_fail (GTK_IS_CONTAINER (container));
900 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
902 container_resize_queue = g_slist_remove (container_resize_queue, container);
903 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
907 gtk_container_clear_resize_widgets (GtkContainer *container)
911 g_return_if_fail (container != NULL);
912 g_return_if_fail (GTK_IS_CONTAINER (container));
914 node = container->resize_widgets;
918 GtkWidget *widget = node->data;
920 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
924 g_slist_free (container->resize_widgets);
925 container->resize_widgets = NULL;
929 gtk_container_set_resize_mode (GtkContainer *container,
930 GtkResizeMode resize_mode)
932 g_return_if_fail (container != NULL);
933 g_return_if_fail (GTK_IS_CONTAINER (container));
934 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
936 if (GTK_WIDGET_TOPLEVEL (container) &&
937 resize_mode == GTK_RESIZE_PARENT)
939 resize_mode = GTK_RESIZE_QUEUE;
940 g_object_notify (G_OBJECT (container), "resize_mode");
943 if (container->resize_mode != resize_mode)
945 container->resize_mode = resize_mode;
947 if (resize_mode == GTK_RESIZE_IMMEDIATE)
948 gtk_container_check_resize (container);
951 gtk_container_clear_resize_widgets (container);
952 gtk_widget_queue_resize (GTK_WIDGET (container));
954 g_object_notify (G_OBJECT (container), "resize_mode");
959 * gtk_container_get_resize_mode:
960 * @container: a #GtkContainer
962 * Returns the resize mode for the container. See
963 * gtk_container_set_resize_mode ().
965 * Return value: the current resize mode
968 gtk_container_get_resize_mode (GtkContainer *container)
970 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
972 return container->resize_mode;
976 gtk_container_set_reallocate_redraws (GtkContainer *container,
977 gboolean needs_redraws)
979 g_return_if_fail (GTK_IS_CONTAINER (container));
981 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
985 gtk_container_get_resize_container (GtkContainer *container)
989 widget = GTK_WIDGET (container);
991 while (widget->parent)
993 widget = widget->parent;
994 if (GTK_IS_RESIZE_CONTAINER (widget) && !GTK_WIDGET_RESIZE_NEEDED (widget))
998 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1002 gtk_container_idle_sizer (gpointer data)
1004 GDK_THREADS_ENTER ();
1006 /* we may be invoked with a container_resize_queue of NULL, because
1007 * queue_resize could have been adding an extra idle function while
1008 * the queue still got processed. we better just ignore such case
1009 * than trying to explicitely work around them with some extra flags,
1010 * since it doesn't cause any actual harm.
1012 while (container_resize_queue)
1017 slist = container_resize_queue;
1018 container_resize_queue = slist->next;
1019 widget = slist->data;
1020 g_slist_free_1 (slist);
1022 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1023 gtk_container_check_resize (GTK_CONTAINER (widget));
1026 gdk_window_process_all_updates ();
1028 GDK_THREADS_LEAVE ();
1034 gtk_container_queue_resize (GtkContainer *container)
1036 GtkContainer *resize_container;
1038 g_return_if_fail (container != NULL);
1039 g_return_if_fail (GTK_IS_CONTAINER (container));
1041 /* clear resize widgets for resize containers
1042 * before aborting prematurely. this is especially
1043 * important for toplevels which may need imemdiate
1044 * processing or their resize handler to be queued.
1046 if (GTK_IS_RESIZE_CONTAINER (container))
1047 gtk_container_clear_resize_widgets (container);
1048 if (GTK_OBJECT_DESTROYED (container) ||
1049 GTK_WIDGET_RESIZE_NEEDED (container))
1052 resize_container = gtk_container_get_resize_container (container);
1054 if (resize_container)
1056 if (GTK_WIDGET_VISIBLE (resize_container) &&
1057 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_DRAWABLE (resize_container)))
1059 switch (resize_container->resize_mode)
1061 case GTK_RESIZE_QUEUE:
1062 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1064 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1065 if (container_resize_queue == NULL)
1066 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1067 gtk_container_idle_sizer,
1069 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1072 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
1073 resize_container->resize_widgets =
1074 g_slist_prepend (resize_container->resize_widgets, container);
1077 case GTK_RESIZE_IMMEDIATE:
1078 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
1079 resize_container->resize_widgets =
1080 g_slist_prepend (resize_container->resize_widgets, container);
1081 gtk_container_check_resize (resize_container);
1084 case GTK_RESIZE_PARENT:
1085 /* Ignore, should not be reached */
1091 /* we need to let hidden resize containers know that something
1092 * changed while they where hidden (currently only evaluated by
1095 resize_container->need_resize = TRUE;
1101 gtk_container_check_resize (GtkContainer *container)
1103 g_return_if_fail (container != NULL);
1104 g_return_if_fail (GTK_IS_CONTAINER (container));
1106 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1110 gtk_container_real_check_resize (GtkContainer *container)
1113 GtkRequisition requisition;
1115 g_return_if_fail (container != NULL);
1116 g_return_if_fail (GTK_IS_CONTAINER (container));
1118 widget = GTK_WIDGET (container);
1120 gtk_widget_size_request (widget, &requisition);
1122 if (requisition.width > widget->allocation.width ||
1123 requisition.height > widget->allocation.height)
1125 if (GTK_IS_RESIZE_CONTAINER (container))
1126 gtk_widget_size_allocate (GTK_WIDGET (container),
1127 >K_WIDGET (container)->allocation);
1129 gtk_widget_queue_resize (widget);
1133 gtk_container_resize_children (container);
1137 /* The container hasn't changed size but one of its children
1138 * queued a resize request. Which means that the allocation
1139 * is not sufficient for the requisition of some child.
1140 * We've already performed a size request at this point,
1141 * so we simply need to run through the list of resize
1142 * widgets and reallocate their sizes appropriately. We
1143 * make the optimization of not performing reallocation
1144 * for a widget who also has a parent in the resize widgets
1145 * list. GTK_RESIZE_NEEDED is used for flagging those
1146 * parents inside this function.
1149 gtk_container_resize_children (GtkContainer *container)
1152 GtkWidget *resize_container;
1153 GSList *resize_widgets;
1154 GSList *resize_containers;
1157 /* resizing invariants:
1158 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1159 * containers with resize_mode==GTK_RESIZE_PARENT have to have resize_widgets
1161 * containers that are flagged RESIZE_NEEDED must have resize_widgets set to
1162 * NULL, or are toplevels (thus have ->parent set to NULL).
1163 * widgets that are in some container->resize_widgets list must be flagged with
1165 * widgets that have RESIZE_NEEDED set must be referenced in some
1166 * GTK_IS_RESIZE_CONTAINER (container)->resize_widgets list.
1167 * containers that have an idle sizer pending must be flagged with
1171 g_return_if_fail (container != NULL);
1172 g_return_if_fail (GTK_IS_CONTAINER (container));
1174 /* we first check out if we actually need to perform a resize,
1175 * which is not the case if we got another container queued for
1176 * a resize in our ancestry. also we can skip the whole
1177 * resize_widgets checks if we are a toplevel and NEED_RESIZE.
1178 * this code assumes that our allocation is sufficient for our
1179 * requisition, since otherwise we would NEED_RESIZE.
1181 resize_container = GTK_WIDGET (container);
1182 while (resize_container)
1184 if (GTK_WIDGET_RESIZE_NEEDED (resize_container))
1186 resize_container = resize_container->parent;
1188 if (resize_container)
1190 /* queue_resize and size_allocate both clear our
1191 * resize_widgets list.
1193 if (resize_container->parent)
1194 gtk_container_queue_resize (container);
1196 gtk_widget_size_allocate (GTK_WIDGET (container),
1197 >K_WIDGET (container)->allocation);
1201 resize_container = GTK_WIDGET (container);
1203 /* we now walk the ancestry for all resize widgets as long
1204 * as they are our children and as long as their allocation
1205 * is insufficient, since we don't need to reallocate below that.
1207 resize_widgets = container->resize_widgets;
1208 container->resize_widgets = NULL;
1209 for (node = resize_widgets; node; node = node->next)
1211 widget = node->data;
1213 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1215 while (widget->parent != resize_container &&
1216 ((widget->allocation.width < widget->requisition.width) ||
1217 (widget->allocation.height < widget->requisition.height)))
1218 widget = widget->parent;
1220 GTK_PRIVATE_SET_FLAG (widget, GTK_RESIZE_NEEDED);
1221 node->data = widget;
1224 /* for the newly setup resize_widgets list, we now walk each widget's
1225 * ancestry to sort those widgets out that have RESIZE_NEEDED parents.
1226 * we can safely stop the walk if we are the parent, since we checked
1227 * our own ancestry already.
1229 resize_containers = NULL;
1230 for (node = resize_widgets; node; node = node->next)
1234 widget = node->data;
1236 if (!GTK_WIDGET_RESIZE_NEEDED (widget))
1239 parent = widget->parent;
1241 while (parent != resize_container)
1243 if (GTK_WIDGET_RESIZE_NEEDED (parent))
1245 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1248 parent = parent->parent;
1251 if (!g_slist_find (resize_containers, widget))
1253 resize_containers = g_slist_prepend (resize_containers, widget);
1254 gtk_widget_ref (widget);
1257 g_slist_free (resize_widgets);
1259 for (node = resize_containers; node; node = node->next)
1261 widget = node->data;
1263 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1265 gtk_widget_size_allocate (widget, &widget->allocation);
1267 gtk_widget_unref (widget);
1269 g_slist_free (resize_containers);
1273 * gtk_container_forall:
1274 * @container: a #GtkContainer
1275 * @callback: a callback
1276 * @callback_data: callback user data
1278 * Invokes @callback on each child of @container, including children
1279 * that are considered "internal" (implementation details of the
1280 * container). "Internal" children generally weren't added by the user
1281 * of the container, but were added by the container implementation
1282 * itself. Most applications should use gtk_container_foreach(),
1283 * rather than gtk_container_forall().
1286 gtk_container_forall (GtkContainer *container,
1287 GtkCallback callback,
1288 gpointer callback_data)
1290 GtkContainerClass *class;
1292 g_return_if_fail (container != NULL);
1293 g_return_if_fail (GTK_IS_CONTAINER (container));
1294 g_return_if_fail (callback != NULL);
1296 class = GTK_CONTAINER_GET_CLASS (container);
1299 class->forall (container, TRUE, callback, callback_data);
1303 * gtk_container_foreach:
1304 * @container: a #GtkContainer
1305 * @callback: a callback
1306 * @callback_data: callback user data
1308 * Invokes @callback on each non-internal child of @container. See
1309 * gtk_container_forall() for details on what constitutes an
1310 * "internal" child. Most applications should use
1311 * gtk_container_foreach(), rather than gtk_container_forall().
1314 gtk_container_foreach (GtkContainer *container,
1315 GtkCallback callback,
1316 gpointer callback_data)
1318 GtkContainerClass *class;
1320 g_return_if_fail (container != NULL);
1321 g_return_if_fail (GTK_IS_CONTAINER (container));
1322 g_return_if_fail (callback != NULL);
1324 class = GTK_CONTAINER_GET_CLASS (container);
1327 class->forall (container, FALSE, callback, callback_data);
1330 typedef struct _GtkForeachData GtkForeachData;
1331 struct _GtkForeachData
1333 GtkObject *container;
1334 GtkCallbackMarshal callback;
1335 gpointer callback_data;
1339 gtk_container_foreach_unmarshal (GtkWidget *child,
1342 GtkForeachData *fdata = (GtkForeachData*) data;
1345 /* first argument */
1346 args[0].name = NULL;
1347 args[0].type = GTK_OBJECT_TYPE (child);
1348 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1350 /* location for return value */
1351 args[1].name = NULL;
1352 args[1].type = GTK_TYPE_NONE;
1354 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1358 gtk_container_foreach_full (GtkContainer *container,
1359 GtkCallback callback,
1360 GtkCallbackMarshal marshal,
1361 gpointer callback_data,
1362 GtkDestroyNotify notify)
1364 g_return_if_fail (container != NULL);
1365 g_return_if_fail (GTK_IS_CONTAINER (container));
1369 GtkForeachData fdata;
1371 fdata.container = GTK_OBJECT (container);
1372 fdata.callback = marshal;
1373 fdata.callback_data = callback_data;
1375 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1379 g_return_if_fail (callback != NULL);
1381 gtk_container_foreach (container, callback, &callback_data);
1385 notify (callback_data);
1389 gtk_container_set_focus_child (GtkContainer *container,
1392 g_return_if_fail (container != NULL);
1393 g_return_if_fail (GTK_IS_CONTAINER (container));
1395 g_return_if_fail (GTK_IS_WIDGET (widget));
1397 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1401 gtk_container_children (GtkContainer *container)
1407 gtk_container_foreach (container,
1408 gtk_container_children_callback,
1411 return g_list_reverse (children);
1415 gtk_container_child_position_callback (GtkWidget *widget,
1416 gpointer client_data)
1422 } *data = client_data;
1425 if (data->child == widget)
1426 data->index = data->i;
1430 gtk_container_child_default_composite_name (GtkContainer *container,
1440 /* fallback implementation */
1444 gtk_container_forall (container,
1445 gtk_container_child_position_callback,
1448 name = g_strdup_printf ("%s-%u",
1449 gtk_type_name (GTK_OBJECT_TYPE (child)),
1456 gtk_container_child_composite_name (GtkContainer *container,
1459 g_return_val_if_fail (container != NULL, NULL);
1460 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1461 g_return_val_if_fail (child != NULL, NULL);
1462 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1463 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1465 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1467 static GQuark quark_composite_name = 0;
1470 if (!quark_composite_name)
1471 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1473 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1476 GtkContainerClass *class;
1478 class = GTK_CONTAINER_GET_CLASS (container);
1479 if (class->composite_name)
1480 name = class->composite_name (container, child);
1483 name = g_strdup (name);
1492 gtk_container_real_set_focus_child (GtkContainer *container,
1495 g_return_if_fail (container != NULL);
1496 g_return_if_fail (GTK_IS_CONTAINER (container));
1498 g_return_if_fail (GTK_IS_WIDGET (child));
1500 if (child != container->focus_child)
1502 if (container->focus_child)
1503 gtk_widget_unref (container->focus_child);
1504 container->focus_child = child;
1505 if (container->focus_child)
1506 gtk_widget_ref (container->focus_child);
1510 /* check for h/v adjustments
1512 if (container->focus_child)
1514 GtkAdjustment *adjustment;
1516 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1518 gtk_adjustment_clamp_page (adjustment,
1519 container->focus_child->allocation.y,
1520 (container->focus_child->allocation.y +
1521 container->focus_child->allocation.height));
1523 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1525 gtk_adjustment_clamp_page (adjustment,
1526 container->focus_child->allocation.x,
1527 (container->focus_child->allocation.x +
1528 container->focus_child->allocation.width));
1533 get_focus_chain (GtkContainer *container)
1535 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1539 filter_unfocusable (GtkContainer *container,
1548 if (GTK_WIDGET_IS_SENSITIVE (tmp_list->data) &&
1549 GTK_WIDGET_DRAWABLE (tmp_list->data) &&
1550 (GTK_IS_CONTAINER (tmp_list->data) || GTK_WIDGET_CAN_FOCUS (tmp_list->data)))
1551 tmp_list = tmp_list->next;
1554 tmp_list2 = tmp_list;
1555 tmp_list = tmp_list->next;
1557 list = g_list_remove_link (list, tmp_list2);
1558 g_list_free_1 (tmp_list2);
1566 gtk_container_focus (GtkWidget *widget,
1567 GtkDirectionType direction)
1571 GtkContainer *container;
1573 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1575 container = GTK_CONTAINER (widget);
1579 if (GTK_WIDGET_CAN_FOCUS (container))
1581 if (!GTK_WIDGET_HAS_FOCUS (container))
1583 gtk_widget_grab_focus (GTK_WIDGET (container));
1589 /* Get a list of the containers children, allowing focus
1590 * chain to override.
1592 if (container->has_focus_chain)
1594 children = g_list_copy (get_focus_chain (container));
1599 gtk_container_forall (container,
1600 gtk_container_children_callback,
1602 children = g_list_reverse (children);
1607 /* Remove any children which are inappropriate for focus movement
1609 children = filter_unfocusable (container, children);
1613 case GTK_DIR_TAB_FORWARD:
1614 case GTK_DIR_TAB_BACKWARD:
1615 if (container->has_focus_chain)
1617 if (direction == GTK_DIR_TAB_BACKWARD)
1618 children = g_list_reverse (children);
1619 return_val = gtk_container_focus_move (container, children, direction);
1622 return_val = gtk_container_focus_tab (container, children, direction);
1626 return_val = gtk_container_focus_up_down (container, &children, direction);
1630 return_val = gtk_container_focus_left_right (container, &children, direction);
1634 g_list_free (children);
1642 gtk_container_focus_tab (GtkContainer *container,
1644 GtkDirectionType direction)
1652 length = g_list_length (children);
1654 /* sort the children in the y direction */
1655 for (i = 1; i < length; i++)
1658 tmp_list = g_list_nth (children, j);
1659 child = tmp_list->data;
1663 child2 = tmp_list->prev->data;
1664 if (child->allocation.y < child2->allocation.y)
1666 tmp_list->data = tmp_list->prev->data;
1667 tmp_list = tmp_list->prev;
1674 tmp_list->data = child;
1677 /* sort the children in the x direction while
1678 * maintaining the y direction sort.
1680 for (i = 1; i < length; i++)
1683 tmp_list = g_list_nth (children, j);
1684 child = tmp_list->data;
1688 child2 = tmp_list->prev->data;
1689 if ((child->allocation.x < child2->allocation.x) &&
1690 (child->allocation.y == child2->allocation.y))
1692 tmp_list->data = tmp_list->prev->data;
1693 tmp_list = tmp_list->prev;
1700 tmp_list->data = child;
1703 /* if we are going backwards then reverse the order
1706 if (direction == GTK_DIR_TAB_BACKWARD)
1707 children = g_list_reverse (children);
1709 return gtk_container_focus_move (container, children, direction);
1713 old_focus_coords (GtkContainer *container, GdkRectangle *old_focus_rect)
1715 GtkWidget *widget = GTK_WIDGET (container);
1716 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1719 GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget &&
1720 GTK_WIDGET_REALIZED (container) &&
1721 GTK_WIDGET_REALIZED (GTK_WINDOW (toplevel)->focus_widget))
1723 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1724 GdkWindow *old_parent_window = old_focus->parent ? old_focus->parent->window : old_focus->window;
1725 GdkWindow *new_parent_window = widget->window;
1726 GdkWindow *toplevel_window = toplevel->window;
1728 *old_focus_rect = old_focus->allocation;
1730 /* Translate coordinates to the toplevel */
1732 while (old_parent_window != toplevel_window)
1736 gdk_window_get_position (old_parent_window, &dx, &dy);
1738 old_focus_rect->x += dx;
1739 old_focus_rect->y += dy;
1741 old_parent_window = gdk_window_get_parent (old_parent_window);
1744 /* Translate coordinates back to the new container */
1746 while (new_parent_window != toplevel_window)
1750 gdk_window_get_position (new_parent_window, &dx, &dy);
1752 old_focus_rect->x -= dx;
1753 old_focus_rect->y -= dy;
1755 new_parent_window = gdk_window_get_parent (new_parent_window);
1764 typedef struct _CompareInfo CompareInfo;
1773 up_down_compare (gconstpointer a,
1777 const GtkWidget *child1 = a;
1778 const GtkWidget *child2 = b;
1779 CompareInfo *compare = data;
1781 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1782 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1786 gint x1 = abs (child1->allocation.x + child1->allocation.width / 2 - compare->x);
1787 gint x2 = abs (child2->allocation.x + child2->allocation.width / 2 - compare->x);
1789 if (compare->y < y1)
1790 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1792 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1795 return (y1 < y2) ? -1 : 1;
1799 gtk_container_focus_up_down (GtkContainer *container,
1801 GtkDirectionType direction)
1803 CompareInfo compare;
1806 if (container->focus_child)
1812 /* Delete widgets from list that don't match minimum criteria */
1814 compare_x1 = container->focus_child->allocation.x;
1815 compare_x2 = container->focus_child->allocation.x + container->focus_child->allocation.width;
1817 if (direction == GTK_DIR_UP)
1818 compare_y = container->focus_child->allocation.y;
1820 compare_y = container->focus_child->allocation.y + container->focus_child->allocation.height;
1822 tmp_list = *children;
1825 GtkWidget *child = tmp_list->data;
1826 GList *next = tmp_list->next;
1827 gint child_x1, child_x2;
1829 if (child != container->focus_child)
1831 child_x1 = child->allocation.x;
1832 child_x2 = child->allocation.x + child->allocation.width;
1834 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1835 (direction == GTK_DIR_DOWN && child->allocation.y + child->allocation.height < compare_y) || /* Not below */
1836 (direction == GTK_DIR_UP && child->allocation.y > compare_y)) /* Not above */
1838 *children = g_list_delete_link (*children, tmp_list);
1845 compare.x = (compare_x1 + compare_x2) / 2;
1846 compare.y = container->focus_child->allocation.y + container->focus_child->allocation.height / 2;
1850 /* No old focus widget, need to figure out starting x,y some other way
1852 GtkWidget *widget = GTK_WIDGET (container);
1853 GdkRectangle old_focus_rect;
1855 if (old_focus_coords (container, &old_focus_rect))
1857 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1861 if (GTK_WIDGET_NO_WINDOW (widget))
1862 compare.x = widget->allocation.x + widget->allocation.width / 2;
1864 compare.x = widget->allocation.width / 2;
1867 if (GTK_WIDGET_NO_WINDOW (widget))
1868 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1870 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1873 *children = g_list_sort_with_data (*children, up_down_compare, &compare);
1875 if (direction == GTK_DIR_UP)
1876 *children = g_list_reverse (*children);
1878 return gtk_container_focus_move (container, *children, direction);
1882 left_right_compare (gconstpointer a,
1886 const GtkWidget *child1 = a;
1887 const GtkWidget *child2 = b;
1888 CompareInfo *compare = data;
1890 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1891 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1895 gint y1 = abs (child1->allocation.y + child1->allocation.height / 2 - compare->y);
1896 gint y2 = abs (child2->allocation.y + child2->allocation.height / 2 - compare->y);
1898 if (compare->x < x1)
1899 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1901 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1904 return (x1 < x2) ? -1 : 1;
1908 gtk_container_focus_left_right (GtkContainer *container,
1910 GtkDirectionType direction)
1912 CompareInfo compare;
1915 if (container->focus_child)
1921 /* Delete widgets from list that don't match minimum criteria */
1923 compare_y1 = container->focus_child->allocation.y;
1924 compare_y2 = container->focus_child->allocation.y + container->focus_child->allocation.height;
1926 if (direction == GTK_DIR_LEFT)
1927 compare_x = container->focus_child->allocation.x;
1929 compare_x = container->focus_child->allocation.x + container->focus_child->allocation.width;
1931 tmp_list = *children;
1934 GtkWidget *child = tmp_list->data;
1935 GList *next = tmp_list->next;
1936 gint child_y1, child_y2;
1938 if (child != container->focus_child)
1940 child_y1 = child->allocation.y;
1941 child_y2 = child->allocation.y + child->allocation.height;
1943 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1944 (direction == GTK_DIR_RIGHT && child->allocation.x + child->allocation.width < compare_x) || /* Not to left */
1945 (direction == GTK_DIR_LEFT && child->allocation.x > compare_x)) /* Not to right */
1947 *children = g_list_delete_link (*children, tmp_list);
1954 compare.y = (compare_y1 + compare_y2) / 2;
1955 compare.x = container->focus_child->allocation.x + container->focus_child->allocation.width / 2;
1959 /* No old focus widget, need to figure out starting x,y some other way
1961 GtkWidget *widget = GTK_WIDGET (container);
1962 GdkRectangle old_focus_rect;
1964 if (old_focus_coords (container, &old_focus_rect))
1966 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1970 if (GTK_WIDGET_NO_WINDOW (widget))
1971 compare.y = widget->allocation.y + widget->allocation.height / 2;
1973 compare.y = widget->allocation.height / 2;
1976 if (GTK_WIDGET_NO_WINDOW (widget))
1977 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1979 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1982 *children = g_list_sort_with_data (*children, left_right_compare, &compare);
1984 if (direction == GTK_DIR_LEFT)
1985 *children = g_list_reverse (*children);
1987 return gtk_container_focus_move (container, *children, direction);
1991 gtk_container_focus_move (GtkContainer *container,
1993 GtkDirectionType direction)
1995 GtkWidget *focus_child;
1998 focus_child = container->focus_child;
2002 child = children->data;
2003 children = children->next;
2010 if (focus_child == child)
2014 if (gtk_widget_child_focus (child, direction))
2018 else if (GTK_WIDGET_DRAWABLE (child) &&
2019 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2021 if (gtk_widget_child_focus (child, direction))
2031 gtk_container_children_callback (GtkWidget *widget,
2032 gpointer client_data)
2036 children = (GList**) client_data;
2037 *children = g_list_prepend (*children, widget);
2041 chain_widget_destroyed (GtkWidget *widget,
2044 GtkContainer *container;
2047 container = GTK_CONTAINER (user_data);
2049 chain = g_object_get_data (G_OBJECT (container),
2050 "gtk-container-focus-chain");
2052 chain = g_list_remove (chain, widget);
2054 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
2055 chain_widget_destroyed,
2058 g_object_set_data (G_OBJECT (container),
2059 "gtk-container-focus-chain",
2064 gtk_container_set_focus_chain (GtkContainer *container,
2065 GList *focusable_widgets)
2070 g_return_if_fail (GTK_IS_CONTAINER (container));
2072 if (container->has_focus_chain)
2073 gtk_container_unset_focus_chain (container);
2075 container->has_focus_chain = TRUE;
2078 tmp_list = focusable_widgets;
2079 while (tmp_list != NULL)
2081 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2083 /* In principle each widget in the chain should be a descendant
2084 * of the container, but we don't want to check that here, it's
2085 * expensive and also it's allowed to set the focus chain before
2086 * you pack the widgets, or have a widget in the chain that isn't
2087 * always packed. So we check for ancestor during actual traversal.
2090 chain = g_list_prepend (chain, tmp_list->data);
2092 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
2094 GTK_SIGNAL_FUNC (chain_widget_destroyed),
2097 tmp_list = g_list_next (tmp_list);
2100 chain = g_list_reverse (chain);
2102 g_object_set_data (G_OBJECT (container),
2103 "gtk-container-focus-chain",
2108 * gtk_container_get_focus_chain:
2109 * @container: a #GtkContainer
2110 * @focusable_widgets: location to store the focus chain of the
2111 * container, or %NULL. You should free this list
2112 * using g_list_free() when you are done with it, however
2113 * no additional reference count is added to the
2114 * individual widgets in the focus chain.
2116 * Retrieve the focus chain of the container, if one has been
2117 * set explicitely. If no focus chain has been explicitely
2118 * set, GTK+ computes the focus chain based on the positions
2119 * of the children. In that case, GTK+ stores %NULL in
2120 * @focusable_widgets and returns %FALSE.
2122 * Return value: %TRUE if the focus chain of the container,
2123 * has been set explicitely.
2126 gtk_container_get_focus_chain (GtkContainer *container,
2127 GList **focus_chain)
2129 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2133 if (container->has_focus_chain)
2134 *focus_chain = g_list_copy (get_focus_chain (container));
2136 *focus_chain = NULL;
2139 return container->has_focus_chain;
2143 gtk_container_unset_focus_chain (GtkContainer *container)
2145 g_return_if_fail (GTK_IS_CONTAINER (container));
2147 if (container->has_focus_chain)
2152 chain = get_focus_chain (container);
2154 container->has_focus_chain = FALSE;
2156 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2160 while (tmp_list != NULL)
2162 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2163 chain_widget_destroyed,
2166 tmp_list = g_list_next (tmp_list);
2169 g_list_free (chain);
2174 gtk_container_set_focus_vadjustment (GtkContainer *container,
2175 GtkAdjustment *adjustment)
2177 g_return_if_fail (container != NULL);
2178 g_return_if_fail (GTK_IS_CONTAINER (container));
2180 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2183 gtk_object_ref (GTK_OBJECT(adjustment));
2185 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2188 (GtkDestroyNotify) gtk_object_unref);
2192 * gtk_container_get_focus_vadjustment:
2193 * @container: a #GtkContainer
2195 * Retrieves the vertical focus adjustment for the container. See
2196 * gtk_container_set_focus_vadjustment ().
2198 * Return value: the vertical focus adjustment, or %NULL if
2199 * none has been set.
2202 gtk_container_get_focus_vadjustment (GtkContainer *container)
2204 GtkAdjustment *vadjustment;
2206 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2208 vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2209 vadjustment_key_id);
2215 gtk_container_set_focus_hadjustment (GtkContainer *container,
2216 GtkAdjustment *adjustment)
2218 g_return_if_fail (container != NULL);
2219 g_return_if_fail (GTK_IS_CONTAINER (container));
2221 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2224 gtk_object_ref (GTK_OBJECT (adjustment));
2226 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2229 (GtkDestroyNotify) gtk_object_unref);
2233 * gtk_container_get_focus_hadjustment:
2234 * @container: a #GtkContainer
2236 * Retrieves the horizontal focus adjustment for the container. See
2237 * gtk_container_set_focus_hadjustment ().
2239 * Return value: the horizontal focus adjustment, or %NULL if none
2240 * none has been set.
2243 gtk_container_get_focus_hadjustment (GtkContainer *container)
2245 GtkAdjustment *hadjustment;
2247 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2249 hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2250 hadjustment_key_id);
2257 gtk_container_show_all (GtkWidget *widget)
2259 g_return_if_fail (widget != NULL);
2260 g_return_if_fail (GTK_IS_CONTAINER (widget));
2262 gtk_container_foreach (GTK_CONTAINER (widget),
2263 (GtkCallback) gtk_widget_show_all,
2265 gtk_widget_show (widget);
2269 gtk_container_hide_all (GtkWidget *widget)
2271 g_return_if_fail (widget != NULL);
2272 g_return_if_fail (GTK_IS_CONTAINER (widget));
2274 gtk_widget_hide (widget);
2275 gtk_container_foreach (GTK_CONTAINER (widget),
2276 (GtkCallback) gtk_widget_hide_all,
2282 gtk_container_expose_child (GtkWidget *child,
2283 gpointer client_data)
2286 GtkWidget *container;
2287 GdkEventExpose *event;
2288 } *data = client_data;
2290 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2296 gtk_container_expose (GtkWidget *widget,
2297 GdkEventExpose *event)
2300 GtkWidget *container;
2301 GdkEventExpose *event;
2304 g_return_val_if_fail (widget != NULL, FALSE);
2305 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2306 g_return_val_if_fail (event != NULL, FALSE);
2309 if (GTK_WIDGET_DRAWABLE (widget))
2311 data.container = widget;
2314 gtk_container_foreach (GTK_CONTAINER (widget),
2315 gtk_container_expose_child,
2323 gtk_container_map_child (GtkWidget *child,
2324 gpointer client_data)
2326 if (GTK_WIDGET_VISIBLE (child) &&
2327 GTK_WIDGET_CHILD_VISIBLE (child) &&
2328 !GTK_WIDGET_MAPPED (child))
2329 gtk_widget_map (child);
2333 gtk_container_map (GtkWidget *widget)
2335 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2337 gtk_container_forall (GTK_CONTAINER (widget),
2338 gtk_container_map_child,
2341 if (!GTK_WIDGET_NO_WINDOW (widget))
2342 gdk_window_show (widget->window);
2346 gtk_container_unmap (GtkWidget *widget)
2348 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2350 if (!GTK_WIDGET_NO_WINDOW (widget))
2351 gdk_window_hide (widget->window);
2353 gtk_container_forall (GTK_CONTAINER (widget),
2354 (GtkCallback)gtk_widget_unmap,
2359 * gtk_container_propagate_expose:
2360 * @container: a #GtkContainer
2361 * @child: a child of @container
2362 * @event: a expose event sent to container
2364 * When a container receives an expose event, it must send synthetic
2365 * expose events to all children that don't have their own GdkWindows.
2366 * This function provides a convenient way of doing this. A container,
2367 * when it receives an expose event, gtk_container_propagate_expose()
2368 * once for each child, passing in the event the container received.
2370 * gtk_container_propagate expose() takes care of deciding whether
2371 * an expose event needs to be sent to the child, intersecting
2372 * the event's area with the child area, and sending the event.
2374 * In most cases, a container can simply either simply inherit the
2375 * ::expose implementation from GtkContainer, or, do some drawing
2376 * and then chain to the ::expose implementation from GtkContainer.
2379 gtk_container_propagate_expose (GtkContainer *container,
2381 GdkEventExpose *event)
2383 GdkEventExpose child_event;
2385 g_return_if_fail (GTK_IS_CONTAINER (container));
2386 g_return_if_fail (GTK_IS_WIDGET (child));
2387 g_return_if_fail (event != NULL);
2389 g_assert (child->parent == GTK_WIDGET (container));
2391 if (GTK_WIDGET_DRAWABLE (child) &&
2392 GTK_WIDGET_NO_WINDOW (child) &&
2393 (child->window == event->window))
2395 child_event = *event;
2397 child_event.region = gtk_widget_region_intersect (child, event->region);
2398 if (!gdk_region_empty (child_event.region))
2400 gdk_region_get_clipbox (child_event.region, &child_event.area);
2401 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2403 gdk_region_destroy (child_event.region);