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/.
32 #include "gtkcontainer.h"
33 #include "gtkprivate.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
38 #include "gtktoolbar.h"
39 #include <gobject/gobjectnotifyqueue.c>
40 #include <gobject/gvaluecollector.h>
59 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
60 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
63 /* --- prototypes --- */
64 static void gtk_container_base_class_init (GtkContainerClass *klass);
65 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
66 static void gtk_container_class_init (GtkContainerClass *klass);
67 static void gtk_container_init (GtkContainer *container);
68 static void gtk_container_destroy (GtkObject *object);
69 static void gtk_container_set_property (GObject *object,
73 static void gtk_container_get_property (GObject *object,
77 static void gtk_container_add_unimplemented (GtkContainer *container,
79 static void gtk_container_remove_unimplemented (GtkContainer *container,
81 static void gtk_container_real_check_resize (GtkContainer *container);
82 static gboolean gtk_container_focus (GtkWidget *widget,
83 GtkDirectionType direction);
84 static void gtk_container_real_set_focus_child (GtkContainer *container,
87 static gboolean gtk_container_focus_move (GtkContainer *container,
89 GtkDirectionType direction);
90 static void gtk_container_children_callback (GtkWidget *widget,
91 gpointer client_data);
92 static void gtk_container_show_all (GtkWidget *widget);
93 static void gtk_container_hide_all (GtkWidget *widget);
94 static gint gtk_container_expose (GtkWidget *widget,
95 GdkEventExpose *event);
96 static void gtk_container_map (GtkWidget *widget);
97 static void gtk_container_unmap (GtkWidget *widget);
99 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
103 /* --- variables --- */
104 static const gchar vadjustment_key[] = "gtk-vadjustment";
105 static guint vadjustment_key_id = 0;
106 static const gchar hadjustment_key[] = "gtk-hadjustment";
107 static guint hadjustment_key_id = 0;
108 static GSList *container_resize_queue = NULL;
109 static guint container_signals[LAST_SIGNAL] = { 0 };
110 static GtkWidgetClass *parent_class = NULL;
111 extern GParamSpecPool *_gtk_widget_child_property_pool;
112 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
115 /* --- functions --- */
117 gtk_container_get_type (void)
119 static GType container_type = 0;
123 const GTypeInfo container_info =
125 sizeof (GtkContainerClass),
126 (GBaseInitFunc) gtk_container_base_class_init,
127 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
128 (GClassInitFunc) gtk_container_class_init,
129 NULL /* class_finalize */,
130 NULL /* class_data */,
131 sizeof (GtkContainer),
133 (GInstanceInitFunc) gtk_container_init,
134 NULL, /* value_table */
138 g_type_register_static (GTK_TYPE_WIDGET, I_("GtkContainer"),
139 &container_info, G_TYPE_FLAG_ABSTRACT);
142 return container_type;
146 gtk_container_base_class_init (GtkContainerClass *class)
148 /* reset instance specifc class fields that don't get inherited */
149 class->set_child_property = NULL;
150 class->get_child_property = NULL;
154 gtk_container_base_class_finalize (GtkContainerClass *class)
158 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
159 for (node = list; node; node = node->next)
161 GParamSpec *pspec = node->data;
163 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
164 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
165 g_param_spec_unref (pspec);
171 gtk_container_class_init (GtkContainerClass *class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
174 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
175 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 parent_class = g_type_class_peek_parent (class);
179 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
180 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
182 gobject_class->set_property = gtk_container_set_property;
183 gobject_class->get_property = gtk_container_get_property;
185 object_class->destroy = gtk_container_destroy;
187 widget_class->show_all = gtk_container_show_all;
188 widget_class->hide_all = gtk_container_hide_all;
189 widget_class->expose_event = gtk_container_expose;
190 widget_class->map = gtk_container_map;
191 widget_class->unmap = gtk_container_unmap;
192 widget_class->focus = gtk_container_focus;
194 class->add = gtk_container_add_unimplemented;
195 class->remove = gtk_container_remove_unimplemented;
196 class->check_resize = gtk_container_real_check_resize;
197 class->forall = NULL;
198 class->set_focus_child = gtk_container_real_set_focus_child;
199 class->child_type = NULL;
200 class->composite_name = gtk_container_child_default_composite_name;
202 g_object_class_install_property (gobject_class,
204 g_param_spec_enum ("resize-mode",
206 P_("Specify how resize events are handled"),
207 GTK_TYPE_RESIZE_MODE,
209 GTK_PARAM_READWRITE));
210 g_object_class_install_property (gobject_class,
212 g_param_spec_uint ("border-width",
214 P_("The width of the empty border outside the containers children"),
218 GTK_PARAM_READWRITE));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_object ("child",
223 P_("Can be used to add a new child to the container"),
225 GTK_PARAM_WRITABLE));
226 container_signals[ADD] =
227 g_signal_new (I_("add"),
228 G_OBJECT_CLASS_TYPE (object_class),
230 G_STRUCT_OFFSET (GtkContainerClass, add),
232 _gtk_marshal_VOID__OBJECT,
235 container_signals[REMOVE] =
236 g_signal_new (I_("remove"),
237 G_OBJECT_CLASS_TYPE (object_class),
239 G_STRUCT_OFFSET (GtkContainerClass, remove),
241 _gtk_marshal_VOID__OBJECT,
244 container_signals[CHECK_RESIZE] =
245 g_signal_new (I_("check_resize"),
246 G_OBJECT_CLASS_TYPE (object_class),
248 G_STRUCT_OFFSET (GtkContainerClass, check_resize),
250 _gtk_marshal_VOID__VOID,
252 container_signals[SET_FOCUS_CHILD] =
253 g_signal_new (I_("set-focus-child"),
254 G_OBJECT_CLASS_TYPE (object_class),
256 G_STRUCT_OFFSET (GtkContainerClass, set_focus_child),
258 _gtk_marshal_VOID__OBJECT,
264 * gtk_container_child_type:
265 * @container: a #GtkContainer.
267 * Returns the type of the children supported by the container.
269 * Note that this may return %G_TYPE_NONE to indicate that no more
270 * children can be added, e.g. for a #GtkPaned which already has two
273 * Return value: a #GType.
276 gtk_container_child_type (GtkContainer *container)
279 GtkContainerClass *class;
281 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
283 class = GTK_CONTAINER_GET_CLASS (container);
284 if (class->child_type)
285 slot = class->child_type (container);
292 /* --- GtkContainer child property mechanism --- */
294 container_get_child_property (GtkContainer *container,
299 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
301 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
305 container_set_child_property (GtkContainer *container,
309 GObjectNotifyQueue *nqueue)
311 GValue tmp_value = { 0, };
312 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
314 /* provide a copy to work from, convert (if necessary) and validate */
315 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
316 if (!g_value_transform (value, &tmp_value))
317 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
319 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
320 G_VALUE_TYPE_NAME (value));
321 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
323 gchar *contents = g_strdup_value_contents (value);
325 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
327 G_VALUE_TYPE_NAME (value),
329 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
334 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
335 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
337 g_value_unset (&tmp_value);
341 * gtk_container_child_get_valist:
342 * @container: a #GtkContainer
343 * @child: a widget which is a child of @container
344 * @first_property_name: the name of the first property to get
345 * @var_args: a %NULL-terminated list of property names and #GValue*,
346 * starting with @first_prop_name.
348 * Gets the values of one or more child properties for @child and @container.
351 gtk_container_child_get_valist (GtkContainer *container,
353 const gchar *first_property_name,
358 g_return_if_fail (GTK_IS_CONTAINER (container));
359 g_return_if_fail (GTK_IS_WIDGET (child));
360 g_return_if_fail (child->parent == GTK_WIDGET (container));
362 g_object_ref (container);
363 g_object_ref (child);
365 name = first_property_name;
368 GValue value = { 0, };
372 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
374 G_OBJECT_TYPE (container),
378 g_warning ("%s: container class `%s' has no child property named `%s'",
380 G_OBJECT_TYPE_NAME (container),
384 if (!(pspec->flags & G_PARAM_READABLE))
386 g_warning ("%s: child property `%s' of container class `%s' is not readable",
389 G_OBJECT_TYPE_NAME (container));
392 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
393 container_get_child_property (container, child, pspec, &value);
394 G_VALUE_LCOPY (&value, var_args, 0, &error);
397 g_warning ("%s: %s", G_STRLOC, error);
399 g_value_unset (&value);
402 g_value_unset (&value);
403 name = va_arg (var_args, gchar*);
406 g_object_unref (child);
407 g_object_unref (container);
411 * gtk_container_child_get_property:
412 * @container: a #GtkContainer
413 * @child: a widget which is a child of @container
414 * @property_name: the name of the property to get
415 * @value: a location to return the value
417 * Gets the value of a child property for @child and @container.
420 gtk_container_child_get_property (GtkContainer *container,
422 const gchar *property_name,
427 g_return_if_fail (GTK_IS_CONTAINER (container));
428 g_return_if_fail (GTK_IS_WIDGET (child));
429 g_return_if_fail (child->parent == GTK_WIDGET (container));
430 g_return_if_fail (property_name != NULL);
431 g_return_if_fail (G_IS_VALUE (value));
433 g_object_ref (container);
434 g_object_ref (child);
435 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
436 G_OBJECT_TYPE (container), TRUE);
438 g_warning ("%s: container class `%s' has no child property named `%s'",
440 G_OBJECT_TYPE_NAME (container),
442 else if (!(pspec->flags & G_PARAM_READABLE))
443 g_warning ("%s: child property `%s' of container class `%s' is not readable",
446 G_OBJECT_TYPE_NAME (container));
449 GValue *prop_value, tmp_value = { 0, };
451 /* auto-conversion of the callers value type
453 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
455 g_value_reset (value);
458 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
460 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
462 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
463 G_VALUE_TYPE_NAME (value));
464 g_object_unref (child);
465 g_object_unref (container);
470 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
471 prop_value = &tmp_value;
473 container_get_child_property (container, child, pspec, prop_value);
474 if (prop_value != value)
476 g_value_transform (prop_value, value);
477 g_value_unset (&tmp_value);
480 g_object_unref (child);
481 g_object_unref (container);
485 * gtk_container_child_set_valist:
486 * @container: a #GtkContainer
487 * @child: a widget which is a child of @container
488 * @first_property_name: the name of the first property to set
489 * @var_args: a %NULL-terminated list of property names and values, starting
490 * with @first_prop_name.
492 * Sets one or more child properties for @child and @container.
495 gtk_container_child_set_valist (GtkContainer *container,
497 const gchar *first_property_name,
500 GObjectNotifyQueue *nqueue;
503 g_return_if_fail (GTK_IS_CONTAINER (container));
504 g_return_if_fail (GTK_IS_WIDGET (child));
505 g_return_if_fail (child->parent == GTK_WIDGET (container));
507 g_object_ref (container);
508 g_object_ref (child);
510 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
511 name = first_property_name;
514 GValue value = { 0, };
516 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
518 G_OBJECT_TYPE (container),
522 g_warning ("%s: container class `%s' has no child property named `%s'",
524 G_OBJECT_TYPE_NAME (container),
528 if (!(pspec->flags & G_PARAM_WRITABLE))
530 g_warning ("%s: child property `%s' of container class `%s' is not writable",
533 G_OBJECT_TYPE_NAME (container));
536 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
537 G_VALUE_COLLECT (&value, var_args, 0, &error);
540 g_warning ("%s: %s", G_STRLOC, error);
543 /* we purposely leak the value here, it might not be
544 * in a sane state if an error condition occoured
548 container_set_child_property (container, child, pspec, &value, nqueue);
549 g_value_unset (&value);
550 name = va_arg (var_args, gchar*);
552 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
554 g_object_unref (container);
555 g_object_unref (child);
559 * gtk_container_child_set_property:
560 * @container: a #GtkContainer
561 * @child: a widget which is a child of @container
562 * @property_name: the name of the property to set
563 * @value: the value to set the property to
565 * Sets a child property for @child and @container.
568 gtk_container_child_set_property (GtkContainer *container,
570 const gchar *property_name,
573 GObjectNotifyQueue *nqueue;
576 g_return_if_fail (GTK_IS_CONTAINER (container));
577 g_return_if_fail (GTK_IS_WIDGET (child));
578 g_return_if_fail (child->parent == GTK_WIDGET (container));
579 g_return_if_fail (property_name != NULL);
580 g_return_if_fail (G_IS_VALUE (value));
582 g_object_ref (container);
583 g_object_ref (child);
585 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
586 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
587 G_OBJECT_TYPE (container), TRUE);
589 g_warning ("%s: container class `%s' has no child property named `%s'",
591 G_OBJECT_TYPE_NAME (container),
593 else if (!(pspec->flags & G_PARAM_WRITABLE))
594 g_warning ("%s: child property `%s' of container class `%s' is not writable",
597 G_OBJECT_TYPE_NAME (container));
600 container_set_child_property (container, child, pspec, value, nqueue);
602 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
603 g_object_unref (container);
604 g_object_unref (child);
608 * gtk_container_add_with_properties:
609 * @container: a #GtkContainer
610 * @widget: a widget to be placed inside @container
611 * @first_prop_name: the name of the first child property to set
612 * @Varargs: a %NULL-terminated list of property names and values, starting
613 * with @first_prop_name.
615 * Adds @widget to @container, setting child properties at the same time.
616 * See gtk_container_add() and gtk_container_child_set() for more details.
619 gtk_container_add_with_properties (GtkContainer *container,
621 const gchar *first_prop_name,
624 g_return_if_fail (GTK_IS_CONTAINER (container));
625 g_return_if_fail (GTK_IS_WIDGET (widget));
626 g_return_if_fail (widget->parent == NULL);
628 g_object_ref (container);
629 g_object_ref (widget);
630 gtk_widget_freeze_child_notify (widget);
632 g_signal_emit (container, container_signals[ADD], 0, widget);
637 va_start (var_args, first_prop_name);
638 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
642 gtk_widget_thaw_child_notify (widget);
643 g_object_unref (widget);
644 g_object_unref (container);
648 * gtk_container_child_set:
649 * @container: a #GtkContainer
650 * @child: a widget which is a child of @container
651 * @first_prop_name: the name of the first property to set
652 * @Varargs: a %NULL-terminated list of property names and values, starting
653 * with @first_prop_name.
655 * Sets one or more child properties for @child and @container.
658 gtk_container_child_set (GtkContainer *container,
660 const gchar *first_prop_name,
665 g_return_if_fail (GTK_IS_CONTAINER (container));
666 g_return_if_fail (GTK_IS_WIDGET (child));
667 g_return_if_fail (child->parent == GTK_WIDGET (container));
669 va_start (var_args, first_prop_name);
670 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
675 * gtk_container_child_get:
676 * @container: a #GtkContainer
677 * @child: a widget which is a child of @container
678 * @first_prop_name: the name of the first property to get
679 * @Varargs: a %NULL-terminated list of property names and #GValue*,
680 * starting with @first_prop_name.
682 * Gets the values of one or more child properties for @child and @container.
685 gtk_container_child_get (GtkContainer *container,
687 const gchar *first_prop_name,
692 g_return_if_fail (GTK_IS_CONTAINER (container));
693 g_return_if_fail (GTK_IS_WIDGET (child));
694 g_return_if_fail (child->parent == GTK_WIDGET (container));
696 va_start (var_args, first_prop_name);
697 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
702 * gtk_container_class_install_child_property:
703 * @cclass: a #GtkContainerClass
704 * @property_id: the id for the property
705 * @pspec: the #GParamSpec for the property
707 * Installs a child property on a container class.
710 gtk_container_class_install_child_property (GtkContainerClass *cclass,
714 g_return_if_fail (GTK_IS_CONTAINER_CLASS (cclass));
715 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
716 if (pspec->flags & G_PARAM_WRITABLE)
717 g_return_if_fail (cclass->set_child_property != NULL);
718 if (pspec->flags & G_PARAM_READABLE)
719 g_return_if_fail (cclass->get_child_property != NULL);
720 g_return_if_fail (property_id > 0);
721 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
722 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
723 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
725 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (cclass), FALSE))
727 g_warning (G_STRLOC ": class `%s' already contains a child property named `%s'",
728 G_OBJECT_CLASS_NAME (cclass),
732 g_param_spec_ref (pspec);
733 g_param_spec_sink (pspec);
734 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
735 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (cclass));
739 * gtk_container_class_find_child_property:
740 * @cclass: a #GtkContainerClass
741 * @property_name: the name of the child property to find
742 * @returns: the #GParamSpec of the child property or %NULL if @class has no
743 * child property with that name.
745 * Finds a child property of a container class by name.
748 gtk_container_class_find_child_property (GObjectClass *cclass,
749 const gchar *property_name)
751 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
752 g_return_val_if_fail (property_name != NULL, NULL);
754 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
756 G_OBJECT_CLASS_TYPE (cclass),
761 * gtk_container_class_list_child_properties:
762 * @cclass: a #GtkContainerClass
763 * @n_properties: location to return the number of child properties found
764 * @returns: a newly allocated %NULL-terminated array of #GParamSpec*.
765 * The array must be freed with g_free().
767 * Returns all child properties of a container class.
770 gtk_container_class_list_child_properties (GObjectClass *cclass,
776 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (cclass), NULL);
778 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
779 G_OBJECT_CLASS_TYPE (cclass),
788 gtk_container_add_unimplemented (GtkContainer *container,
791 g_warning ("GtkContainerClass::add not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
795 gtk_container_remove_unimplemented (GtkContainer *container,
798 g_warning ("GtkContainerClass::remove not implemented for `%s'", g_type_name (G_TYPE_FROM_INSTANCE (container)));
802 gtk_container_init (GtkContainer *container)
804 container->focus_child = NULL;
805 container->border_width = 0;
806 container->need_resize = FALSE;
807 container->resize_mode = GTK_RESIZE_PARENT;
808 container->reallocate_redraws = FALSE;
812 gtk_container_destroy (GtkObject *object)
814 GtkContainer *container = GTK_CONTAINER (object);
816 if (GTK_CONTAINER_RESIZE_PENDING (container))
817 _gtk_container_dequeue_resize_handler (container);
819 /* do this before walking child widgets, to avoid
820 * removing children from focus chain one by one.
822 if (container->has_focus_chain)
823 gtk_container_unset_focus_chain (container);
825 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
827 if (GTK_OBJECT_CLASS (parent_class)->destroy)
828 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
832 gtk_container_set_property (GObject *object,
837 GtkContainer *container = GTK_CONTAINER (object);
841 case PROP_BORDER_WIDTH:
842 gtk_container_set_border_width (container, g_value_get_uint (value));
844 case PROP_RESIZE_MODE:
845 gtk_container_set_resize_mode (container, g_value_get_enum (value));
848 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
851 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
857 gtk_container_get_property (GObject *object,
862 GtkContainer *container = GTK_CONTAINER (object);
866 case PROP_BORDER_WIDTH:
867 g_value_set_uint (value, container->border_width);
869 case PROP_RESIZE_MODE:
870 g_value_set_enum (value, container->resize_mode);
873 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
879 * gtk_container_set_border_width:
880 * @container: a #GtkContainer
881 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
882 * Valid values are in the range 0-65535 pixels.
884 * Sets the border width of the container.
886 * The border width of a container is the amount of space to leave
887 * around the outside of the container. The only exception to this is
888 * #GtkWindow; because toplevel windows can't leave space outside,
889 * they leave the space inside. The border is added on all sides of
890 * the container. To add space to only one side, one approach is to
891 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
892 * it a size, and place it on the side of the container as a spacer.
895 gtk_container_set_border_width (GtkContainer *container,
898 g_return_if_fail (GTK_IS_CONTAINER (container));
900 if (container->border_width != border_width)
902 container->border_width = border_width;
903 g_object_notify (G_OBJECT (container), "border-width");
905 if (GTK_WIDGET_REALIZED (container))
906 gtk_widget_queue_resize (GTK_WIDGET (container));
911 * gtk_container_get_border_width:
912 * @container: a #GtkContainer
914 * Retrieves the border width of the container. See
915 * gtk_container_set_border_width().
917 * Return value: the current border width
920 gtk_container_get_border_width (GtkContainer *container)
922 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
924 return container->border_width;
929 * @container: a #GtkContainer
930 * @widget: a widget to be placed inside @container
932 * Adds @widget to @container. Typically used for simple containers
933 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
934 * layout containers such as #GtkBox or #GtkTable, this function will
935 * pick default packing parameters that may not be correct. So
936 * consider functions such as gtk_box_pack_start() and
937 * gtk_table_attach() as an alternative to gtk_container_add() in
938 * those cases. A widget may be added to only one container at a time;
939 * you can't place the same widget inside two different containers.
942 gtk_container_add (GtkContainer *container,
945 g_return_if_fail (GTK_IS_CONTAINER (container));
946 g_return_if_fail (GTK_IS_WIDGET (widget));
948 if (widget->parent != NULL)
950 g_warning ("Attempting to add a widget with type %s to a container of "
951 "type %s, but the widget is already inside a container of type %s, "
952 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
953 g_type_name (G_OBJECT_TYPE (widget)),
954 g_type_name (G_OBJECT_TYPE (container)),
955 g_type_name (G_OBJECT_TYPE (widget->parent)));
959 g_signal_emit (container, container_signals[ADD], 0, widget);
963 * gtk_container_remove:
964 * @container: a #GtkContainer
965 * @widget: a current child of @container
967 * Removes @widget from @container. @widget must be inside @container.
968 * Note that @container will own a reference to @widget, and that this
969 * may be the last reference held; so removing a widget from its
970 * container can destroy that widget. If you want to use @widget
971 * again, you need to add a reference to it while it's not inside
972 * a container, using g_object_ref(). If you don't want to use @widget
973 * again it's usually more efficient to simply destroy it directly
974 * using gtk_widget_destroy() since this will remove it from the
975 * container and help break any circular reference count cycles.
978 gtk_container_remove (GtkContainer *container,
981 g_return_if_fail (GTK_IS_CONTAINER (container));
982 g_return_if_fail (GTK_IS_WIDGET (widget));
984 /* When using the deprecated API of the toolbar, it is possible
985 * to legitimately call this function with a widget that is not
986 * a direct child of the container.
988 g_return_if_fail (GTK_IS_TOOLBAR (container) ||
989 widget->parent == GTK_WIDGET (container));
991 g_signal_emit (container, container_signals[REMOVE], 0, widget);
995 _gtk_container_dequeue_resize_handler (GtkContainer *container)
997 g_return_if_fail (GTK_IS_CONTAINER (container));
998 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
1000 container_resize_queue = g_slist_remove (container_resize_queue, container);
1001 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
1005 * gtk_container_set_resize_mode:
1006 * @container: a #GtkContainer.
1007 * @resize_mode: the new resize mode.
1009 * Sets the resize mode for the container.
1011 * The resize mode of a container determines whether a resize request
1012 * will be passed to the container's parent, queued for later execution
1013 * or executed immediately.
1016 gtk_container_set_resize_mode (GtkContainer *container,
1017 GtkResizeMode resize_mode)
1019 g_return_if_fail (GTK_IS_CONTAINER (container));
1020 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
1022 if (GTK_WIDGET_TOPLEVEL (container) &&
1023 resize_mode == GTK_RESIZE_PARENT)
1025 resize_mode = GTK_RESIZE_QUEUE;
1028 if (container->resize_mode != resize_mode)
1030 container->resize_mode = resize_mode;
1032 gtk_widget_queue_resize (GTK_WIDGET (container));
1033 g_object_notify (G_OBJECT (container), "resize-mode");
1038 * gtk_container_get_resize_mode:
1039 * @container: a #GtkContainer
1041 * Returns the resize mode for the container. See
1042 * gtk_container_set_resize_mode ().
1044 * Return value: the current resize mode
1047 gtk_container_get_resize_mode (GtkContainer *container)
1049 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
1051 return container->resize_mode;
1055 * gtk_container_set_reallocate_redraws:
1056 * @container: a #GtkContainer.
1057 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
1059 * Sets the @reallocate_redraws flag of the container to the given value.
1061 * Containers requesting reallocation redraws get automatically
1062 * redrawn if any of their children changed allocation.
1065 gtk_container_set_reallocate_redraws (GtkContainer *container,
1066 gboolean needs_redraws)
1068 g_return_if_fail (GTK_IS_CONTAINER (container));
1070 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
1073 static GtkContainer*
1074 gtk_container_get_resize_container (GtkContainer *container)
1076 GtkWidget *widget = GTK_WIDGET (container);
1078 while (widget->parent)
1080 widget = widget->parent;
1081 if (GTK_IS_RESIZE_CONTAINER (widget))
1085 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
1089 gtk_container_idle_sizer (gpointer data)
1091 /* we may be invoked with a container_resize_queue of NULL, because
1092 * queue_resize could have been adding an extra idle function while
1093 * the queue still got processed. we better just ignore such case
1094 * than trying to explicitely work around them with some extra flags,
1095 * since it doesn't cause any actual harm.
1097 while (container_resize_queue)
1102 slist = container_resize_queue;
1103 container_resize_queue = slist->next;
1104 widget = slist->data;
1105 g_slist_free_1 (slist);
1107 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1108 gtk_container_check_resize (GTK_CONTAINER (widget));
1111 gdk_window_process_all_updates ();
1117 _gtk_container_queue_resize (GtkContainer *container)
1119 GtkContainer *resize_container;
1122 g_return_if_fail (GTK_IS_CONTAINER (container));
1124 widget = GTK_WIDGET (container);
1125 resize_container = gtk_container_get_resize_container (container);
1129 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1130 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1131 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1135 widget = widget->parent;
1138 if (resize_container)
1140 if (GTK_WIDGET_VISIBLE (resize_container) &&
1141 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1143 switch (resize_container->resize_mode)
1145 case GTK_RESIZE_QUEUE:
1146 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1148 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1149 if (container_resize_queue == NULL)
1150 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE,
1151 gtk_container_idle_sizer,
1153 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1157 case GTK_RESIZE_IMMEDIATE:
1158 gtk_container_check_resize (resize_container);
1161 case GTK_RESIZE_PARENT:
1162 g_assert_not_reached ();
1168 /* we need to let hidden resize containers know that something
1169 * changed while they where hidden (currently only evaluated by
1172 resize_container->need_resize = TRUE;
1178 gtk_container_check_resize (GtkContainer *container)
1180 g_return_if_fail (GTK_IS_CONTAINER (container));
1182 g_signal_emit (container, container_signals[CHECK_RESIZE], 0);
1186 gtk_container_real_check_resize (GtkContainer *container)
1188 GtkWidget *widget = GTK_WIDGET (container);
1189 GtkRequisition requisition;
1191 gtk_widget_size_request (widget, &requisition);
1193 if (requisition.width > widget->allocation.width ||
1194 requisition.height > widget->allocation.height)
1196 if (GTK_IS_RESIZE_CONTAINER (container))
1197 gtk_widget_size_allocate (GTK_WIDGET (container),
1198 >K_WIDGET (container)->allocation);
1200 gtk_widget_queue_resize (widget);
1204 gtk_container_resize_children (container);
1208 /* The container hasn't changed size but one of its children
1209 * queued a resize request. Which means that the allocation
1210 * is not sufficient for the requisition of some child.
1211 * We've already performed a size request at this point,
1212 * so we simply need to reallocate and let the allocation
1213 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1216 gtk_container_resize_children (GtkContainer *container)
1220 /* resizing invariants:
1221 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1222 * containers that have an idle sizer pending must be flagged with
1225 g_return_if_fail (GTK_IS_CONTAINER (container));
1227 widget = GTK_WIDGET (container);
1228 gtk_widget_size_allocate (widget, &widget->allocation);
1232 * gtk_container_forall:
1233 * @container: a #GtkContainer
1234 * @callback: a callback
1235 * @callback_data: callback user data
1237 * Invokes @callback on each child of @container, including children
1238 * that are considered "internal" (implementation details of the
1239 * container). "Internal" children generally weren't added by the user
1240 * of the container, but were added by the container implementation
1241 * itself. Most applications should use gtk_container_foreach(),
1242 * rather than gtk_container_forall().
1245 gtk_container_forall (GtkContainer *container,
1246 GtkCallback callback,
1247 gpointer callback_data)
1249 GtkContainerClass *class;
1251 g_return_if_fail (GTK_IS_CONTAINER (container));
1252 g_return_if_fail (callback != NULL);
1254 class = GTK_CONTAINER_GET_CLASS (container);
1257 class->forall (container, TRUE, callback, callback_data);
1261 * gtk_container_foreach:
1262 * @container: a #GtkContainer
1263 * @callback: a callback
1264 * @callback_data: callback user data
1266 * Invokes @callback on each non-internal child of @container. See
1267 * gtk_container_forall() for details on what constitutes an
1268 * "internal" child. Most applications should use
1269 * gtk_container_foreach(), rather than gtk_container_forall().
1272 gtk_container_foreach (GtkContainer *container,
1273 GtkCallback callback,
1274 gpointer callback_data)
1276 GtkContainerClass *class;
1278 g_return_if_fail (GTK_IS_CONTAINER (container));
1279 g_return_if_fail (callback != NULL);
1281 class = GTK_CONTAINER_GET_CLASS (container);
1284 class->forall (container, FALSE, callback, callback_data);
1287 typedef struct _GtkForeachData GtkForeachData;
1288 struct _GtkForeachData
1290 GtkObject *container;
1291 GtkCallbackMarshal callback;
1292 gpointer callback_data;
1296 gtk_container_foreach_unmarshal (GtkWidget *child,
1299 GtkForeachData *fdata = (GtkForeachData*) data;
1302 /* first argument */
1303 args[0].name = NULL;
1304 args[0].type = G_TYPE_FROM_INSTANCE (child);
1305 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1307 /* location for return value */
1308 args[1].name = NULL;
1309 args[1].type = G_TYPE_NONE;
1311 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1315 gtk_container_foreach_full (GtkContainer *container,
1316 GtkCallback callback,
1317 GtkCallbackMarshal marshal,
1318 gpointer callback_data,
1319 GtkDestroyNotify notify)
1321 g_return_if_fail (GTK_IS_CONTAINER (container));
1325 GtkForeachData fdata;
1327 fdata.container = GTK_OBJECT (container);
1328 fdata.callback = marshal;
1329 fdata.callback_data = callback_data;
1331 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1335 g_return_if_fail (callback != NULL);
1337 gtk_container_foreach (container, callback, &callback_data);
1341 notify (callback_data);
1345 gtk_container_set_focus_child (GtkContainer *container,
1348 g_return_if_fail (GTK_IS_CONTAINER (container));
1350 g_return_if_fail (GTK_IS_WIDGET (widget));
1352 g_signal_emit (container, container_signals[SET_FOCUS_CHILD], 0, widget);
1356 * gtk_container_get_children:
1357 * @container: a #GtkContainer.
1359 * Returns the container's non-internal children. See
1360 * gtk_container_forall() for details on what constitutes an "internal" child.
1362 * Return value: a newly-allocated list of the container's non-internal children.
1365 gtk_container_get_children (GtkContainer *container)
1367 GList *children = NULL;
1369 gtk_container_foreach (container,
1370 gtk_container_children_callback,
1373 return g_list_reverse (children);
1377 gtk_container_child_position_callback (GtkWidget *widget,
1378 gpointer client_data)
1384 } *data = client_data;
1387 if (data->child == widget)
1388 data->index = data->i;
1392 gtk_container_child_default_composite_name (GtkContainer *container,
1402 /* fallback implementation */
1406 gtk_container_forall (container,
1407 gtk_container_child_position_callback,
1410 name = g_strdup_printf ("%s-%u",
1411 g_type_name (G_TYPE_FROM_INSTANCE (child)),
1418 _gtk_container_child_composite_name (GtkContainer *container,
1421 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1422 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1423 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1425 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1427 static GQuark quark_composite_name = 0;
1430 if (!quark_composite_name)
1431 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1433 name = g_object_get_qdata (G_OBJECT (child), quark_composite_name);
1436 GtkContainerClass *class;
1438 class = GTK_CONTAINER_GET_CLASS (container);
1439 if (class->composite_name)
1440 name = class->composite_name (container, child);
1443 name = g_strdup (name);
1452 gtk_container_real_set_focus_child (GtkContainer *container,
1455 g_return_if_fail (GTK_IS_CONTAINER (container));
1456 g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
1458 if (child != container->focus_child)
1460 if (container->focus_child)
1461 g_object_unref (container->focus_child);
1462 container->focus_child = child;
1463 if (container->focus_child)
1464 g_object_ref (container->focus_child);
1468 /* check for h/v adjustments
1470 if (container->focus_child)
1472 GtkAdjustment *hadj;
1473 GtkAdjustment *vadj;
1474 GtkWidget *focus_child;
1477 hadj = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
1478 vadj = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
1482 focus_child = container->focus_child;
1483 while (GTK_IS_CONTAINER (focus_child) &&
1484 GTK_CONTAINER (focus_child)->focus_child)
1486 focus_child = GTK_CONTAINER (focus_child)->focus_child;
1489 gtk_widget_translate_coordinates (focus_child, container->focus_child,
1492 x += container->focus_child->allocation.x;
1493 y += container->focus_child->allocation.y;
1496 gtk_adjustment_clamp_page (vadj, y, y + focus_child->allocation.height);
1499 gtk_adjustment_clamp_page (hadj, x, x + focus_child->allocation.width);
1505 get_focus_chain (GtkContainer *container)
1507 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1510 /* same as gtk_container_get_children, except it includes internals
1513 gtk_container_get_all_children (GtkContainer *container)
1515 GList *children = NULL;
1517 gtk_container_forall (container,
1518 gtk_container_children_callback,
1525 gtk_container_focus (GtkWidget *widget,
1526 GtkDirectionType direction)
1529 GList *sorted_children;
1531 GtkContainer *container;
1533 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1535 container = GTK_CONTAINER (widget);
1539 if (GTK_WIDGET_CAN_FOCUS (container))
1541 if (!GTK_WIDGET_HAS_FOCUS (container))
1543 gtk_widget_grab_focus (GTK_WIDGET (container));
1549 /* Get a list of the containers children, allowing focus
1550 * chain to override.
1552 if (container->has_focus_chain)
1553 children = g_list_copy (get_focus_chain (container));
1555 children = gtk_container_get_all_children (container);
1557 if (container->has_focus_chain &&
1558 (direction == GTK_DIR_TAB_FORWARD ||
1559 direction == GTK_DIR_TAB_BACKWARD))
1561 sorted_children = g_list_copy (children);
1563 if (direction == GTK_DIR_TAB_BACKWARD)
1564 sorted_children = g_list_reverse (sorted_children);
1567 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1569 return_val = gtk_container_focus_move (container, sorted_children, direction);
1571 g_list_free (sorted_children);
1572 g_list_free (children);
1579 tab_compare (gconstpointer a,
1583 const GtkWidget *child1 = a;
1584 const GtkWidget *child2 = b;
1585 GtkTextDirection text_direction = GPOINTER_TO_INT (data);
1587 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1588 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1592 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1593 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1595 if (text_direction == GTK_TEXT_DIR_RTL)
1596 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1598 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1601 return (y1 < y2) ? -1 : 1;
1605 gtk_container_focus_sort_tab (GtkContainer *container,
1607 GtkDirectionType direction,
1608 GtkWidget *old_focus)
1610 GtkTextDirection text_direction = gtk_widget_get_direction (GTK_WIDGET (container));
1611 children = g_list_sort_with_data (children, tab_compare, GINT_TO_POINTER (text_direction));
1613 /* if we are going backwards then reverse the order
1616 if (direction == GTK_DIR_TAB_BACKWARD)
1617 children = g_list_reverse (children);
1622 /* Get coordinates of @widget's allocation with respect to
1623 * allocation of @container.
1626 get_allocation_coords (GtkContainer *container,
1628 GdkRectangle *allocation)
1630 *allocation = widget->allocation;
1632 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1633 0, 0, &allocation->x, &allocation->y);
1636 /* Look for a child in @children that is intermediate between
1637 * the focus widget and container. This widget, if it exists,
1638 * acts as the starting widget for focus navigation.
1641 find_old_focus (GtkContainer *container,
1644 GList *tmp_list = children;
1647 GtkWidget *child = tmp_list->data;
1648 GtkWidget *widget = child;
1650 while (widget && widget != (GtkWidget *)container)
1652 GtkWidget *parent = widget->parent;
1653 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1662 tmp_list = tmp_list->next;
1669 old_focus_coords (GtkContainer *container,
1670 GdkRectangle *old_focus_rect)
1672 GtkWidget *widget = GTK_WIDGET (container);
1673 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1675 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1677 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1679 return get_allocation_coords (container, old_focus, old_focus_rect);
1685 typedef struct _CompareInfo CompareInfo;
1689 GtkContainer *container;
1696 up_down_compare (gconstpointer a,
1700 GdkRectangle allocation1;
1701 GdkRectangle allocation2;
1702 CompareInfo *compare = data;
1705 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1706 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1708 y1 = allocation1.y + allocation1.height / 2;
1709 y2 = allocation2.y + allocation2.height / 2;
1713 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1714 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1716 if (compare->reverse)
1717 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1719 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1722 return (y1 < y2) ? -1 : 1;
1726 gtk_container_focus_sort_up_down (GtkContainer *container,
1728 GtkDirectionType direction,
1729 GtkWidget *old_focus)
1731 CompareInfo compare;
1733 GdkRectangle old_allocation;
1735 compare.container = container;
1736 compare.reverse = (direction == GTK_DIR_UP);
1739 old_focus = find_old_focus (container, children);
1741 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1747 /* Delete widgets from list that don't match minimum criteria */
1749 compare_x1 = old_allocation.x;
1750 compare_x2 = old_allocation.x + old_allocation.width;
1752 if (direction == GTK_DIR_UP)
1753 compare_y = old_allocation.y;
1755 compare_y = old_allocation.y + old_allocation.height;
1757 tmp_list = children;
1760 GtkWidget *child = tmp_list->data;
1761 GList *next = tmp_list->next;
1762 gint child_x1, child_x2;
1763 GdkRectangle child_allocation;
1765 if (child != old_focus)
1767 if (get_allocation_coords (container, child, &child_allocation))
1769 child_x1 = child_allocation.x;
1770 child_x2 = child_allocation.x + child_allocation.width;
1772 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1773 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1774 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1776 children = g_list_delete_link (children, tmp_list);
1780 children = g_list_delete_link (children, tmp_list);
1786 compare.x = (compare_x1 + compare_x2) / 2;
1787 compare.y = old_allocation.y + old_allocation.height / 2;
1791 /* No old focus widget, need to figure out starting x,y some other way
1793 GtkWidget *widget = GTK_WIDGET (container);
1794 GdkRectangle old_focus_rect;
1796 if (old_focus_coords (container, &old_focus_rect))
1798 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1802 if (GTK_WIDGET_NO_WINDOW (widget))
1803 compare.x = widget->allocation.x + widget->allocation.width / 2;
1805 compare.x = widget->allocation.width / 2;
1808 if (GTK_WIDGET_NO_WINDOW (widget))
1809 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1811 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1814 children = g_list_sort_with_data (children, up_down_compare, &compare);
1816 if (compare.reverse)
1817 children = g_list_reverse (children);
1823 left_right_compare (gconstpointer a,
1827 GdkRectangle allocation1;
1828 GdkRectangle allocation2;
1829 CompareInfo *compare = data;
1832 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1833 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1835 x1 = allocation1.x + allocation1.width / 2;
1836 x2 = allocation2.x + allocation2.width / 2;
1840 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1841 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1843 if (compare->reverse)
1844 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1846 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1849 return (x1 < x2) ? -1 : 1;
1853 gtk_container_focus_sort_left_right (GtkContainer *container,
1855 GtkDirectionType direction,
1856 GtkWidget *old_focus)
1858 CompareInfo compare;
1860 GdkRectangle old_allocation;
1862 compare.container = container;
1863 compare.reverse = (direction == GTK_DIR_LEFT);
1866 old_focus = find_old_focus (container, children);
1868 if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
1874 /* Delete widgets from list that don't match minimum criteria */
1876 compare_y1 = old_allocation.y;
1877 compare_y2 = old_allocation.y + old_allocation.height;
1879 if (direction == GTK_DIR_LEFT)
1880 compare_x = old_allocation.x;
1882 compare_x = old_allocation.x + old_allocation.width;
1884 tmp_list = children;
1887 GtkWidget *child = tmp_list->data;
1888 GList *next = tmp_list->next;
1889 gint child_y1, child_y2;
1890 GdkRectangle child_allocation;
1892 if (child != old_focus)
1894 if (get_allocation_coords (container, child, &child_allocation))
1896 child_y1 = child_allocation.y;
1897 child_y2 = child_allocation.y + child_allocation.height;
1899 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1900 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1901 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1903 children = g_list_delete_link (children, tmp_list);
1907 children = g_list_delete_link (children, tmp_list);
1913 compare.y = (compare_y1 + compare_y2) / 2;
1914 compare.x = old_allocation.x + old_allocation.width / 2;
1918 /* No old focus widget, need to figure out starting x,y some other way
1920 GtkWidget *widget = GTK_WIDGET (container);
1921 GdkRectangle old_focus_rect;
1923 if (old_focus_coords (container, &old_focus_rect))
1925 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1929 if (GTK_WIDGET_NO_WINDOW (widget))
1930 compare.y = widget->allocation.y + widget->allocation.height / 2;
1932 compare.y = widget->allocation.height / 2;
1935 if (GTK_WIDGET_NO_WINDOW (widget))
1936 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1938 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1941 children = g_list_sort_with_data (children, left_right_compare, &compare);
1943 if (compare.reverse)
1944 children = g_list_reverse (children);
1950 * gtk_container_focus_sort:
1951 * @container: a #GtkContainer
1952 * @children: a list of descendents of @container (they don't
1953 * have to be direct children.
1954 * @direction: focus direction
1955 * @old_focus: widget to use for the starting position, or %NULL
1956 * to determine this automatically.
1957 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1958 * which is the only @direction we use currently,
1959 * so perhaps this argument should be removed ]
1961 * Sorts @children in the correct order for focusing with
1962 * direction type @direction.
1964 * Return value: a copy of @children, sorted in correct focusing order,
1965 * with children that aren't suitable for focusing in this direction
1969 _gtk_container_focus_sort (GtkContainer *container,
1971 GtkDirectionType direction,
1972 GtkWidget *old_focus)
1974 GList *visible_children = NULL;
1978 if (GTK_WIDGET_REALIZED (children->data))
1979 visible_children = g_list_prepend (visible_children, children->data);
1980 children = children->next;
1985 case GTK_DIR_TAB_FORWARD:
1986 case GTK_DIR_TAB_BACKWARD:
1987 return gtk_container_focus_sort_tab (container, visible_children, direction, old_focus);
1990 return gtk_container_focus_sort_up_down (container, visible_children, direction, old_focus);
1993 return gtk_container_focus_sort_left_right (container, visible_children, direction, old_focus);
1996 g_assert_not_reached ();
2002 gtk_container_focus_move (GtkContainer *container,
2004 GtkDirectionType direction)
2006 GtkWidget *focus_child;
2009 focus_child = container->focus_child;
2013 child = children->data;
2014 children = children->next;
2021 if (focus_child == child)
2025 if (gtk_widget_child_focus (child, direction))
2029 else if (GTK_WIDGET_DRAWABLE (child) &&
2030 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
2032 if (gtk_widget_child_focus (child, direction))
2042 gtk_container_children_callback (GtkWidget *widget,
2043 gpointer client_data)
2047 children = (GList**) client_data;
2048 *children = g_list_prepend (*children, widget);
2052 chain_widget_destroyed (GtkWidget *widget,
2055 GtkContainer *container;
2058 container = GTK_CONTAINER (user_data);
2060 chain = g_object_get_data (G_OBJECT (container),
2061 "gtk-container-focus-chain");
2063 chain = g_list_remove (chain, widget);
2065 g_signal_handlers_disconnect_by_func (widget,
2066 chain_widget_destroyed,
2069 g_object_set_data (G_OBJECT (container),
2070 I_("gtk-container-focus-chain"),
2075 * gtk_container_set_focus_chain:
2076 * @container: a #GtkContainer.
2077 * @focusable_widgets: the new focus chain.
2079 * Sets a focus chain, overriding the one computed automatically by GTK+.
2081 * In principle each widget in the chain should be a descendant of the
2082 * container, but this is not enforced by this method, since it's allowed
2083 * to set the focus chain before you pack the widgets, or have a widget
2084 * in the chain that isn't always packed. The necessary checks are done
2085 * when the focus chain is actually traversed.
2088 gtk_container_set_focus_chain (GtkContainer *container,
2089 GList *focusable_widgets)
2094 g_return_if_fail (GTK_IS_CONTAINER (container));
2096 if (container->has_focus_chain)
2097 gtk_container_unset_focus_chain (container);
2099 container->has_focus_chain = TRUE;
2102 tmp_list = focusable_widgets;
2103 while (tmp_list != NULL)
2105 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2107 /* In principle each widget in the chain should be a descendant
2108 * of the container, but we don't want to check that here, it's
2109 * expensive and also it's allowed to set the focus chain before
2110 * you pack the widgets, or have a widget in the chain that isn't
2111 * always packed. So we check for ancestor during actual traversal.
2114 chain = g_list_prepend (chain, tmp_list->data);
2116 g_signal_connect (tmp_list->data,
2118 G_CALLBACK (chain_widget_destroyed),
2121 tmp_list = g_list_next (tmp_list);
2124 chain = g_list_reverse (chain);
2126 g_object_set_data (G_OBJECT (container),
2127 I_("gtk-container-focus-chain"),
2132 * gtk_container_get_focus_chain:
2133 * @container: a #GtkContainer
2134 * @focusable_widgets: location to store the focus chain of the
2135 * container, or %NULL. You should free this list
2136 * using g_list_free() when you are done with it, however
2137 * no additional reference count is added to the
2138 * individual widgets in the focus chain.
2140 * Retrieves the focus chain of the container, if one has been
2141 * set explicitly. If no focus chain has been explicitly
2142 * set, GTK+ computes the focus chain based on the positions
2143 * of the children. In that case, GTK+ stores %NULL in
2144 * @focusable_widgets and returns %FALSE.
2146 * Return value: %TRUE if the focus chain of the container
2147 * has been set explicitly.
2150 gtk_container_get_focus_chain (GtkContainer *container,
2151 GList **focus_chain)
2153 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2157 if (container->has_focus_chain)
2158 *focus_chain = g_list_copy (get_focus_chain (container));
2160 *focus_chain = NULL;
2163 return container->has_focus_chain;
2167 * gtk_container_unset_focus_chain:
2168 * @container: a #GtkContainer.
2170 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2173 gtk_container_unset_focus_chain (GtkContainer *container)
2175 g_return_if_fail (GTK_IS_CONTAINER (container));
2177 if (container->has_focus_chain)
2182 chain = get_focus_chain (container);
2184 container->has_focus_chain = FALSE;
2186 g_object_set_data (G_OBJECT (container),
2187 I_("gtk-container-focus-chain"),
2191 while (tmp_list != NULL)
2193 g_signal_handlers_disconnect_by_func (tmp_list->data,
2194 chain_widget_destroyed,
2197 tmp_list = g_list_next (tmp_list);
2200 g_list_free (chain);
2205 * gtk_container_set_focus_vadjustment:
2206 * @container: a #GtkContainer
2207 * @adjustment: an adjustment which should be adjusted when the focus is moved among the
2208 * descendents of @container
2210 * Hooks up an adjustment to focus handling in a container, so when a child of the
2211 * container is focused, the adjustment is scrolled to show that widget. This function
2212 * sets the vertical alignment. See gtk_scrolled_window_get_vadjustment() for a typical
2213 * way of obtaining the adjustment and gtk_container_set_focus_hadjustment() for setting
2214 * the horizontal adjustment.
2216 * The adjustments have to be in pixel units and in the same coordinate system as the
2217 * allocation for immediate children of the container.
2220 gtk_container_set_focus_vadjustment (GtkContainer *container,
2221 GtkAdjustment *adjustment)
2223 g_return_if_fail (GTK_IS_CONTAINER (container));
2225 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2228 g_object_ref (adjustment);
2230 g_object_set_qdata_full (G_OBJECT (container),
2237 * gtk_container_get_focus_vadjustment:
2238 * @container: a #GtkContainer
2240 * Retrieves the vertical focus adjustment for the container. See
2241 * gtk_container_set_focus_vadjustment ().
2243 * Return value: the vertical focus adjustment, or %NULL if
2244 * none has been set.
2247 gtk_container_get_focus_vadjustment (GtkContainer *container)
2249 GtkAdjustment *vadjustment;
2251 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2253 vadjustment = g_object_get_qdata (G_OBJECT (container), vadjustment_key_id);
2259 * gtk_container_set_focus_hadjustment:
2260 * @container: a #GtkContainer
2261 * @adjustment: an adjustment which should be adjusted when the focus is moved among the
2262 * descendents of @container
2264 * Hooks up an adjustment to focus handling in a container, so when a child of the
2265 * container is focused, the adjustment is scrolled to show that widget. This function
2266 * sets the horizontal alignment. See gtk_scrolled_window_get_hadjustment() for a typical
2267 * way of obtaining the adjustment and gtk_container_set_focus_vadjustment() for setting
2268 * the vertical adjustment.
2270 * The adjustments have to be in pixel units and in the same coordinate system as the
2271 * allocation for immediate children of the container.
2274 gtk_container_set_focus_hadjustment (GtkContainer *container,
2275 GtkAdjustment *adjustment)
2277 g_return_if_fail (GTK_IS_CONTAINER (container));
2279 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2282 g_object_ref (adjustment);
2284 g_object_set_qdata_full (G_OBJECT (container),
2291 * gtk_container_get_focus_hadjustment:
2292 * @container: a #GtkContainer
2294 * Retrieves the horizontal focus adjustment for the container. See
2295 * gtk_container_set_focus_hadjustment ().
2297 * Return value: the horizontal focus adjustment, or %NULL if
2298 * none has been set.
2301 gtk_container_get_focus_hadjustment (GtkContainer *container)
2303 GtkAdjustment *hadjustment;
2305 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2307 hadjustment = g_object_get_qdata (G_OBJECT (container), hadjustment_key_id);
2314 gtk_container_show_all (GtkWidget *widget)
2316 g_return_if_fail (GTK_IS_CONTAINER (widget));
2318 gtk_container_foreach (GTK_CONTAINER (widget),
2319 (GtkCallback) gtk_widget_show_all,
2321 gtk_widget_show (widget);
2325 gtk_container_hide_all (GtkWidget *widget)
2327 g_return_if_fail (GTK_IS_CONTAINER (widget));
2329 gtk_widget_hide (widget);
2330 gtk_container_foreach (GTK_CONTAINER (widget),
2331 (GtkCallback) gtk_widget_hide_all,
2337 gtk_container_expose_child (GtkWidget *child,
2338 gpointer client_data)
2341 GtkWidget *container;
2342 GdkEventExpose *event;
2343 } *data = client_data;
2345 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2351 gtk_container_expose (GtkWidget *widget,
2352 GdkEventExpose *event)
2355 GtkWidget *container;
2356 GdkEventExpose *event;
2359 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2360 g_return_val_if_fail (event != NULL, FALSE);
2363 if (GTK_WIDGET_DRAWABLE (widget))
2365 data.container = widget;
2368 gtk_container_forall (GTK_CONTAINER (widget),
2369 gtk_container_expose_child,
2377 gtk_container_map_child (GtkWidget *child,
2378 gpointer client_data)
2380 if (GTK_WIDGET_VISIBLE (child) &&
2381 GTK_WIDGET_CHILD_VISIBLE (child) &&
2382 !GTK_WIDGET_MAPPED (child))
2383 gtk_widget_map (child);
2387 gtk_container_map (GtkWidget *widget)
2389 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2391 gtk_container_forall (GTK_CONTAINER (widget),
2392 gtk_container_map_child,
2395 if (!GTK_WIDGET_NO_WINDOW (widget))
2396 gdk_window_show (widget->window);
2400 gtk_container_unmap (GtkWidget *widget)
2402 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2404 if (!GTK_WIDGET_NO_WINDOW (widget))
2405 gdk_window_hide (widget->window);
2407 gtk_container_forall (GTK_CONTAINER (widget),
2408 (GtkCallback)gtk_widget_unmap,
2413 * gtk_container_propagate_expose:
2414 * @container: a #GtkContainer
2415 * @child: a child of @container
2416 * @event: a expose event sent to container
2418 * When a container receives an expose event, it must send synthetic
2419 * expose events to all children that don't have their own #GdkWindows.
2420 * This function provides a convenient way of doing this. A container,
2421 * when it receives an expose event, calls gtk_container_propagate_expose()
2422 * once for each child, passing in the event the container received.
2424 * gtk_container_propagate_expose() takes care of deciding whether
2425 * an expose event needs to be sent to the child, intersecting
2426 * the event's area with the child area, and sending the event.
2428 * In most cases, a container can simply either simply inherit the
2429 * ::expose implementation from #GtkContainer, or, do some drawing
2430 * and then chain to the ::expose implementation from #GtkContainer.
2433 gtk_container_propagate_expose (GtkContainer *container,
2435 GdkEventExpose *event)
2437 GdkEvent *child_event;
2439 g_return_if_fail (GTK_IS_CONTAINER (container));
2440 g_return_if_fail (GTK_IS_WIDGET (child));
2441 g_return_if_fail (event != NULL);
2443 g_assert (child->parent == GTK_WIDGET (container));
2445 if (GTK_WIDGET_DRAWABLE (child) &&
2446 GTK_WIDGET_NO_WINDOW (child) &&
2447 (child->window == event->window))
2449 child_event = gdk_event_new (GDK_EXPOSE);
2450 child_event->expose = *event;
2451 g_object_ref (child_event->expose.window);
2453 child_event->expose.region = gtk_widget_region_intersect (child, event->region);
2454 if (!gdk_region_empty (child_event->expose.region))
2456 gdk_region_get_clipbox (child_event->expose.region, &child_event->expose.area);
2457 gtk_widget_send_expose (child, child_event);
2459 gdk_event_free (child_event);
2463 #define __GTK_CONTAINER_C__
2464 #include "gtkaliasdef.c"