1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcontainer.h"
32 #include "gtkprivate.h"
33 #include "gtksignal.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
38 #include <gobject/gobjectnotifyqueue.c>
39 #include <gobject/gvaluecollector.h>
57 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
58 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
61 /* --- prototypes --- */
62 static void gtk_container_base_class_init (GtkContainerClass *klass);
63 static void gtk_container_base_class_finalize (GtkContainerClass *klass);
64 static void gtk_container_class_init (GtkContainerClass *klass);
65 static void gtk_container_init (GtkContainer *container);
66 static void gtk_container_destroy (GtkObject *object);
67 static void gtk_container_set_property (GObject *object,
71 static void gtk_container_get_property (GObject *object,
75 static void gtk_container_add_unimplemented (GtkContainer *container,
77 static void gtk_container_remove_unimplemented (GtkContainer *container,
79 static void gtk_container_real_check_resize (GtkContainer *container);
80 static gboolean gtk_container_focus (GtkWidget *widget,
81 GtkDirectionType direction);
82 static void gtk_container_real_set_focus_child (GtkContainer *container,
85 static gboolean gtk_container_focus_move (GtkContainer *container,
87 GtkDirectionType direction);
88 static void gtk_container_children_callback (GtkWidget *widget,
89 gpointer client_data);
90 static void gtk_container_show_all (GtkWidget *widget);
91 static void gtk_container_hide_all (GtkWidget *widget);
92 static gint gtk_container_expose (GtkWidget *widget,
93 GdkEventExpose *event);
94 static void gtk_container_map (GtkWidget *widget);
95 static void gtk_container_unmap (GtkWidget *widget);
97 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
101 /* --- variables --- */
102 static const gchar *vadjustment_key = "gtk-vadjustment";
103 static guint vadjustment_key_id = 0;
104 static const gchar *hadjustment_key = "gtk-hadjustment";
105 static guint hadjustment_key_id = 0;
106 static GSList *container_resize_queue = NULL;
107 static guint container_signals[LAST_SIGNAL] = { 0 };
108 static GtkWidgetClass *parent_class = NULL;
109 extern GParamSpecPool *_gtk_widget_child_property_pool;
110 extern GObjectNotifyContext *_gtk_widget_child_property_notify_context;
113 /* --- functions --- */
115 gtk_container_get_type (void)
117 static GtkType container_type = 0;
121 static GTypeInfo container_info = {
122 sizeof (GtkContainerClass),
123 (GBaseInitFunc) gtk_container_base_class_init,
124 (GBaseFinalizeFunc) gtk_container_base_class_finalize,
125 (GClassInitFunc) gtk_container_class_init,
126 NULL /* class_destroy */,
127 NULL /* class_data */,
128 sizeof (GtkContainer),
130 (GInstanceInitFunc) gtk_container_init,
131 NULL, /* value_table */
134 container_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkContainer", &container_info, 0);
137 return container_type;
141 gtk_container_base_class_init (GtkContainerClass *class)
143 /* reset instance specifc class fields that don't get inherited */
144 class->set_child_property = NULL;
145 class->get_child_property = NULL;
149 gtk_container_base_class_finalize (GtkContainerClass *class)
153 list = g_param_spec_pool_list_owned (_gtk_widget_child_property_pool, G_OBJECT_CLASS_TYPE (class));
154 for (node = list; node; node = node->next)
156 GParamSpec *pspec = node->data;
158 g_param_spec_pool_remove (_gtk_widget_child_property_pool, pspec);
159 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
160 g_param_spec_unref (pspec);
166 gtk_container_class_init (GtkContainerClass *class)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
169 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
170 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
172 parent_class = g_type_class_peek_parent (class);
174 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
175 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
177 gobject_class->set_property = gtk_container_set_property;
178 gobject_class->get_property = gtk_container_get_property;
180 object_class->destroy = gtk_container_destroy;
182 widget_class->show_all = gtk_container_show_all;
183 widget_class->hide_all = gtk_container_hide_all;
184 widget_class->expose_event = gtk_container_expose;
185 widget_class->map = gtk_container_map;
186 widget_class->unmap = gtk_container_unmap;
187 widget_class->focus = gtk_container_focus;
189 class->add = gtk_container_add_unimplemented;
190 class->remove = gtk_container_remove_unimplemented;
191 class->check_resize = gtk_container_real_check_resize;
192 class->forall = NULL;
193 class->set_focus_child = gtk_container_real_set_focus_child;
194 class->child_type = NULL;
195 class->composite_name = gtk_container_child_default_composite_name;
197 g_object_class_install_property (gobject_class,
199 g_param_spec_enum ("resize_mode",
201 _("Specify how resize events are handled"),
202 GTK_TYPE_RESIZE_MODE,
205 g_object_class_install_property (gobject_class,
207 g_param_spec_uint ("border_width",
209 _("The width of the empty border outside the containers children."),
214 g_object_class_install_property (gobject_class,
216 g_param_spec_object ("child",
218 _("Can be used to add a new child to the container."),
221 container_signals[ADD] =
222 gtk_signal_new ("add",
224 GTK_CLASS_TYPE (object_class),
225 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
226 _gtk_marshal_VOID__OBJECT,
229 container_signals[REMOVE] =
230 gtk_signal_new ("remove",
232 GTK_CLASS_TYPE (object_class),
233 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
234 _gtk_marshal_VOID__OBJECT,
237 container_signals[CHECK_RESIZE] =
238 gtk_signal_new ("check_resize",
240 GTK_CLASS_TYPE (object_class),
241 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
242 _gtk_marshal_VOID__VOID,
244 container_signals[SET_FOCUS_CHILD] =
245 gtk_signal_new ("set-focus-child",
247 GTK_CLASS_TYPE (object_class),
248 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
249 _gtk_marshal_VOID__OBJECT,
255 * gtk_container_child_type:
256 * @container: a #GtkContainer.
258 * Returns the type of the children supported by the container.
260 * Note that this may return %GTK_TYPE_NONE to indicate that no more
261 * children can be added, e.g. for a #GtkPaned which already has two
264 * Return value: a #GtkType.
267 gtk_container_child_type (GtkContainer *container)
270 GtkContainerClass *class;
272 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
274 class = GTK_CONTAINER_GET_CLASS (container);
275 if (class->child_type)
276 slot = class->child_type (container);
278 slot = GTK_TYPE_NONE;
283 /* --- GtkContainer child property mechanism --- */
285 container_get_child_property (GtkContainer *container,
290 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
292 class->get_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
296 container_set_child_property (GtkContainer *container,
300 GObjectNotifyQueue *nqueue)
302 GValue tmp_value = { 0, };
303 GtkContainerClass *class = g_type_class_peek (pspec->owner_type);
305 /* provide a copy to work from, convert (if necessary) and validate */
306 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
307 if (!g_value_transform (value, &tmp_value))
308 g_warning ("unable to set child property `%s' of type `%s' from value of type `%s'",
310 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
311 G_VALUE_TYPE_NAME (value));
312 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
314 gchar *contents = g_strdup_value_contents (value);
316 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
318 G_VALUE_TYPE_NAME (value),
320 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
325 class->set_child_property (container, child, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
326 g_object_notify_queue_add (G_OBJECT (child), nqueue, pspec);
328 g_value_unset (&tmp_value);
332 gtk_container_child_get_valist (GtkContainer *container,
334 const gchar *first_property_name,
339 g_return_if_fail (GTK_IS_CONTAINER (container));
340 g_return_if_fail (GTK_IS_WIDGET (child));
341 g_return_if_fail (child->parent == GTK_WIDGET (container));
343 g_object_ref (container);
344 g_object_ref (child);
346 name = first_property_name;
349 GValue value = { 0, };
353 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
355 G_OBJECT_TYPE (container),
359 g_warning ("%s: container class `%s' has no child property named `%s'",
361 G_OBJECT_TYPE_NAME (container),
365 if (!(pspec->flags & G_PARAM_READABLE))
367 g_warning ("%s: child property `%s' of container class `%s' is not readable",
370 G_OBJECT_TYPE_NAME (container));
373 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
374 container_get_child_property (container, child, pspec, &value);
375 G_VALUE_LCOPY (&value, var_args, 0, &error);
378 g_warning ("%s: %s", G_STRLOC, error);
380 g_value_unset (&value);
383 g_value_unset (&value);
384 name = va_arg (var_args, gchar*);
387 g_object_unref (child);
388 g_object_unref (container);
392 gtk_container_child_get_property (GtkContainer *container,
394 const gchar *property_name,
399 g_return_if_fail (GTK_IS_CONTAINER (container));
400 g_return_if_fail (GTK_IS_WIDGET (child));
401 g_return_if_fail (child->parent == GTK_WIDGET (container));
402 g_return_if_fail (property_name != NULL);
403 g_return_if_fail (G_IS_VALUE (value));
405 g_object_ref (container);
406 g_object_ref (child);
407 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
408 G_OBJECT_TYPE (container), TRUE);
410 g_warning ("%s: container class `%s' has no child property named `%s'",
412 G_OBJECT_TYPE_NAME (container),
414 else if (!(pspec->flags & G_PARAM_READABLE))
415 g_warning ("%s: child property `%s' of container class `%s' is not readable",
418 G_OBJECT_TYPE_NAME (container));
421 GValue *prop_value, tmp_value = { 0, };
423 /* auto-conversion of the callers value type
425 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
427 g_value_reset (value);
430 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
432 g_warning ("can't retrieve child property `%s' of type `%s' as value of type `%s'",
434 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
435 G_VALUE_TYPE_NAME (value));
436 g_object_unref (child);
437 g_object_unref (container);
442 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
443 prop_value = &tmp_value;
445 container_get_child_property (container, child, pspec, prop_value);
446 if (prop_value != value)
448 g_value_transform (prop_value, value);
449 g_value_unset (&tmp_value);
452 g_object_unref (child);
453 g_object_unref (container);
457 gtk_container_child_set_valist (GtkContainer *container,
459 const gchar *first_property_name,
463 GObjectNotifyQueue *nqueue;
466 g_return_if_fail (GTK_IS_CONTAINER (container));
467 g_return_if_fail (GTK_IS_WIDGET (child));
468 g_return_if_fail (child->parent == GTK_WIDGET (container));
470 g_object_ref (container);
471 g_object_ref (child);
473 object = G_OBJECT (container);
474 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
475 name = first_property_name;
478 GValue value = { 0, };
480 GParamSpec *pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
482 G_OBJECT_TYPE (container),
486 g_warning ("%s: container class `%s' has no child property named `%s'",
488 G_OBJECT_TYPE_NAME (container),
492 if (!(pspec->flags & G_PARAM_WRITABLE))
494 g_warning ("%s: child property `%s' of container class `%s' is not writable",
497 G_OBJECT_TYPE_NAME (container));
500 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
501 G_VALUE_COLLECT (&value, var_args, 0, &error);
504 g_warning ("%s: %s", G_STRLOC, error);
507 /* we purposely leak the value here, it might not be
508 * in a sane state if an error condition occoured
512 container_set_child_property (container, child, pspec, &value, nqueue);
513 g_value_unset (&value);
514 name = va_arg (var_args, gchar*);
516 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
518 g_object_unref (container);
519 g_object_unref (child);
523 gtk_container_child_set_property (GtkContainer *container,
525 const gchar *property_name,
529 GObjectNotifyQueue *nqueue;
532 g_return_if_fail (GTK_IS_CONTAINER (container));
533 g_return_if_fail (GTK_IS_WIDGET (child));
534 g_return_if_fail (child->parent == GTK_WIDGET (container));
535 g_return_if_fail (property_name != NULL);
536 g_return_if_fail (G_IS_VALUE (value));
538 g_object_ref (container);
539 g_object_ref (child);
541 object = G_OBJECT (container);
542 nqueue = g_object_notify_queue_freeze (G_OBJECT (child), _gtk_widget_child_property_notify_context);
543 pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool, property_name,
544 G_OBJECT_TYPE (container), TRUE);
546 g_warning ("%s: container class `%s' has no child property named `%s'",
548 G_OBJECT_TYPE_NAME (container),
550 else if (!(pspec->flags & G_PARAM_WRITABLE))
551 g_warning ("%s: child property `%s' of container class `%s' is not writable",
554 G_OBJECT_TYPE_NAME (container));
557 container_set_child_property (container, child, pspec, value, nqueue);
559 g_object_notify_queue_thaw (G_OBJECT (child), nqueue);
560 g_object_unref (container);
561 g_object_unref (child);
565 gtk_container_add_with_properties (GtkContainer *container,
567 const gchar *first_prop_name,
570 g_return_if_fail (GTK_IS_CONTAINER (container));
571 g_return_if_fail (GTK_IS_WIDGET (widget));
572 g_return_if_fail (widget->parent == NULL);
574 gtk_widget_ref (GTK_WIDGET (container));
575 gtk_widget_ref (widget);
576 gtk_widget_freeze_child_notify (widget);
578 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
583 va_start (var_args, first_prop_name);
584 gtk_container_child_set_valist (container, widget, first_prop_name, var_args);
588 gtk_widget_thaw_child_notify (widget);
589 gtk_widget_unref (widget);
590 gtk_widget_unref (GTK_WIDGET (container));
594 gtk_container_child_set (GtkContainer *container,
596 const gchar *first_prop_name,
601 g_return_if_fail (GTK_IS_CONTAINER (container));
602 g_return_if_fail (GTK_IS_WIDGET (child));
603 g_return_if_fail (child->parent == GTK_WIDGET (container));
605 va_start (var_args, first_prop_name);
606 gtk_container_child_set_valist (container, child, first_prop_name, var_args);
611 gtk_container_child_get (GtkContainer *container,
613 const gchar *first_prop_name,
618 g_return_if_fail (GTK_IS_CONTAINER (container));
619 g_return_if_fail (GTK_IS_WIDGET (child));
620 g_return_if_fail (child->parent == GTK_WIDGET (container));
622 va_start (var_args, first_prop_name);
623 gtk_container_child_get_valist (container, child, first_prop_name, var_args);
628 gtk_container_class_install_child_property (GtkContainerClass *class,
632 g_return_if_fail (GTK_IS_CONTAINER_CLASS (class));
633 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
634 if (pspec->flags & G_PARAM_WRITABLE)
635 g_return_if_fail (class->set_child_property != NULL);
636 if (pspec->flags & G_PARAM_READABLE)
637 g_return_if_fail (class->get_child_property != NULL);
638 g_return_if_fail (property_id > 0);
639 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
640 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
641 g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
643 if (g_param_spec_pool_lookup (_gtk_widget_child_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
645 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
646 G_OBJECT_CLASS_NAME (class),
650 g_param_spec_ref (pspec);
651 g_param_spec_sink (pspec);
652 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
653 g_param_spec_pool_insert (_gtk_widget_child_property_pool, pspec, G_OBJECT_CLASS_TYPE (class));
657 gtk_container_class_find_child_property (GObjectClass *class,
658 const gchar *property_name)
660 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
661 g_return_val_if_fail (property_name != NULL, NULL);
663 return g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
665 G_OBJECT_CLASS_TYPE (class),
669 GParamSpec** /* free result */
670 gtk_container_class_list_child_properties (GObjectClass *class,
676 g_return_val_if_fail (GTK_IS_CONTAINER_CLASS (class), NULL);
678 pspecs = g_param_spec_pool_list (_gtk_widget_child_property_pool,
679 G_OBJECT_CLASS_TYPE (class),
688 gtk_container_add_unimplemented (GtkContainer *container,
691 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
695 gtk_container_remove_unimplemented (GtkContainer *container,
698 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
702 gtk_container_init (GtkContainer *container)
704 container->focus_child = NULL;
705 container->border_width = 0;
706 container->need_resize = FALSE;
707 container->resize_mode = GTK_RESIZE_PARENT;
708 container->reallocate_redraws = FALSE;
712 gtk_container_destroy (GtkObject *object)
714 GtkContainer *container = GTK_CONTAINER (object);
716 if (GTK_CONTAINER_RESIZE_PENDING (container))
717 _gtk_container_dequeue_resize_handler (container);
719 /* do this before walking child widgets, to avoid
720 * removing children from focus chain one by one.
722 if (container->has_focus_chain)
723 gtk_container_unset_focus_chain (container);
725 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
727 if (GTK_OBJECT_CLASS (parent_class)->destroy)
728 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
732 gtk_container_set_property (GObject *object,
737 GtkContainer *container = GTK_CONTAINER (object);
741 case PROP_BORDER_WIDTH:
742 gtk_container_set_border_width (container, g_value_get_uint (value));
744 case PROP_RESIZE_MODE:
745 gtk_container_set_resize_mode (container, g_value_get_enum (value));
748 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
751 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757 gtk_container_get_property (GObject *object,
762 GtkContainer *container = GTK_CONTAINER (object);
766 case PROP_BORDER_WIDTH:
767 g_value_set_uint (value, container->border_width);
769 case PROP_RESIZE_MODE:
770 g_value_set_enum (value, container->resize_mode);
773 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
779 * gtk_container_set_border_width:
780 * @container: a #GtkContainer
781 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container.
782 * Valid values are in the range 0-65535 pixels.
784 * Sets the border width of the container.
786 * The border width of a container is the amount of space to leave
787 * around the outside of the container. The only exception to this is
788 * #GtkWindow; because toplevel windows can't leave space outside,
789 * they leave the space inside. The border is added on all sides of
790 * the container. To add space to only one side, one approach is to
791 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
792 * it a size, and place it on the side of the container as a spacer.
795 gtk_container_set_border_width (GtkContainer *container,
798 g_return_if_fail (GTK_IS_CONTAINER (container));
800 if (container->border_width != border_width)
802 container->border_width = border_width;
803 g_object_notify (G_OBJECT (container), "border_width");
805 if (GTK_WIDGET_REALIZED (container))
806 gtk_widget_queue_resize (GTK_WIDGET (container));
811 * gtk_container_get_border_width:
812 * @container: a #GtkContainer
814 * Retrieves the border width of the container. See
815 * gtk_container_set_border_width().
817 * Return value: the current border width
820 gtk_container_get_border_width (GtkContainer *container)
822 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
824 return container->border_width;
829 * @container: a #GtkContainer
830 * @widget: a widget to be placed inside @container
832 * Adds @widget to @container. Typically used for simple containers
833 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
834 * layout containers such as #GtkBox or #GtkTable, this function will
835 * pick default packing parameters that may not be correct. So
836 * consider functions such as gtk_box_pack_start() and
837 * gtk_table_attach() as an alternative to gtk_container_add() in
838 * those cases. A widget may be added to only one container at a time;
839 * you can't place the same widget inside two different containers.
842 gtk_container_add (GtkContainer *container,
845 g_return_if_fail (GTK_IS_CONTAINER (container));
846 g_return_if_fail (GTK_IS_WIDGET (widget));
848 if (widget->parent != NULL)
850 g_warning ("Attempting to add a widget with type %s to a container of "
851 "type %s, but the widget is already inside a container of type %s, "
852 "the GTK+ FAQ at http://www.gtk.org/faq/ explains how to reparent a widget.",
853 g_type_name (G_OBJECT_TYPE (widget)),
854 g_type_name (G_OBJECT_TYPE (container)),
855 g_type_name (G_OBJECT_TYPE (widget->parent)));
859 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
863 * gtk_container_remove:
864 * @container: a #GtkContainer
865 * @widget: a current child of @container
867 * Removes @widget from @container. @widget must be inside @container.
868 * Note that @container will own a reference to @widget, and that this
869 * may be the last reference held; so removing a widget from its
870 * container can destroy that widget. If you want to use @widget
871 * again, you need to add a reference to it while it's not inside
872 * a container, using g_object_ref().
875 gtk_container_remove (GtkContainer *container,
878 g_return_if_fail (GTK_IS_CONTAINER (container));
879 g_return_if_fail (GTK_IS_WIDGET (widget));
880 g_return_if_fail (widget->parent == GTK_WIDGET (container));
882 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
886 _gtk_container_dequeue_resize_handler (GtkContainer *container)
888 g_return_if_fail (GTK_IS_CONTAINER (container));
889 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
891 container_resize_queue = g_slist_remove (container_resize_queue, container);
892 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
896 * gtk_container_set_resize_mode:
897 * @container: a #GtkContainer.
898 * @resize_mode: the new resize mode.
900 * Sets the resize mode for the container.
902 * The resize mode of a container determines whether a resize request
903 * will be passed to the container's parent, queued for later execution
904 * or executed immediately.
907 gtk_container_set_resize_mode (GtkContainer *container,
908 GtkResizeMode resize_mode)
910 g_return_if_fail (GTK_IS_CONTAINER (container));
911 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
913 if (GTK_WIDGET_TOPLEVEL (container) &&
914 resize_mode == GTK_RESIZE_PARENT)
916 resize_mode = GTK_RESIZE_QUEUE;
919 if (container->resize_mode != resize_mode)
921 container->resize_mode = resize_mode;
923 gtk_widget_queue_resize (GTK_WIDGET (container));
924 g_object_notify (G_OBJECT (container), "resize_mode");
929 * gtk_container_get_resize_mode:
930 * @container: a #GtkContainer
932 * Returns the resize mode for the container. See
933 * gtk_container_set_resize_mode ().
935 * Return value: the current resize mode
938 gtk_container_get_resize_mode (GtkContainer *container)
940 g_return_val_if_fail (GTK_IS_CONTAINER (container), GTK_RESIZE_PARENT);
942 return container->resize_mode;
946 * gtk_container_set_reallocate_redraws:
947 * @container: a #GtkContainer.
948 * @needs_redraws: the new value for the container's @reallocate_redraws flag.
950 * Sets the @reallocate_redraws flag of the container to the given value.
952 * Containers requesting reallocation redraws get automatically
953 * redrawn if any of their children changed allocation.
956 gtk_container_set_reallocate_redraws (GtkContainer *container,
957 gboolean needs_redraws)
959 g_return_if_fail (GTK_IS_CONTAINER (container));
961 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
965 gtk_container_get_resize_container (GtkContainer *container)
967 GtkWidget *widget = GTK_WIDGET (container);
969 while (widget->parent)
971 widget = widget->parent;
972 if (GTK_IS_RESIZE_CONTAINER (widget))
976 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
980 gtk_container_idle_sizer (gpointer data)
982 GDK_THREADS_ENTER ();
984 /* we may be invoked with a container_resize_queue of NULL, because
985 * queue_resize could have been adding an extra idle function while
986 * the queue still got processed. we better just ignore such case
987 * than trying to explicitely work around them with some extra flags,
988 * since it doesn't cause any actual harm.
990 while (container_resize_queue)
995 slist = container_resize_queue;
996 container_resize_queue = slist->next;
997 widget = slist->data;
998 g_slist_free_1 (slist);
1000 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
1001 gtk_container_check_resize (GTK_CONTAINER (widget));
1004 gdk_window_process_all_updates ();
1006 GDK_THREADS_LEAVE ();
1012 _gtk_container_queue_resize (GtkContainer *container)
1014 GtkContainer *resize_container;
1016 g_return_if_fail (GTK_IS_CONTAINER (container));
1018 resize_container = gtk_container_get_resize_container (container);
1020 if (resize_container)
1022 GtkWidget *widget = GTK_WIDGET (container);
1024 while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1026 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1027 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1028 if (widget == GTK_WIDGET (resize_container))
1031 widget = widget->parent;
1034 if (GTK_WIDGET_VISIBLE (resize_container) &&
1035 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1037 switch (resize_container->resize_mode)
1039 case GTK_RESIZE_QUEUE:
1040 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1042 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1043 if (container_resize_queue == NULL)
1044 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1045 gtk_container_idle_sizer,
1047 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1051 case GTK_RESIZE_IMMEDIATE:
1052 gtk_container_check_resize (resize_container);
1055 case GTK_RESIZE_PARENT:
1056 g_assert_not_reached ();
1062 /* we need to let hidden resize containers know that something
1063 * changed while they where hidden (currently only evaluated by
1066 resize_container->need_resize = TRUE;
1072 gtk_container_check_resize (GtkContainer *container)
1074 g_return_if_fail (GTK_IS_CONTAINER (container));
1076 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1080 gtk_container_real_check_resize (GtkContainer *container)
1082 GtkWidget *widget = GTK_WIDGET (container);
1083 GtkRequisition requisition;
1085 gtk_widget_size_request (widget, &requisition);
1087 if (requisition.width > widget->allocation.width ||
1088 requisition.height > widget->allocation.height)
1090 if (GTK_IS_RESIZE_CONTAINER (container))
1091 gtk_widget_size_allocate (GTK_WIDGET (container),
1092 >K_WIDGET (container)->allocation);
1094 gtk_widget_queue_resize (widget);
1098 gtk_container_resize_children (container);
1102 /* The container hasn't changed size but one of its children
1103 * queued a resize request. Which means that the allocation
1104 * is not sufficient for the requisition of some child.
1105 * We've already performed a size request at this point,
1106 * so we simply need to reallocate and let the allocation
1107 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1110 gtk_container_resize_children (GtkContainer *container)
1114 /* resizing invariants:
1115 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1116 * containers that have an idle sizer pending must be flagged with
1119 g_return_if_fail (GTK_IS_CONTAINER (container));
1121 widget = GTK_WIDGET (container);
1122 gtk_widget_size_allocate (widget, &widget->allocation);
1126 * gtk_container_forall:
1127 * @container: a #GtkContainer
1128 * @callback: a callback
1129 * @callback_data: callback user data
1131 * Invokes @callback on each child of @container, including children
1132 * that are considered "internal" (implementation details of the
1133 * container). "Internal" children generally weren't added by the user
1134 * of the container, but were added by the container implementation
1135 * itself. Most applications should use gtk_container_foreach(),
1136 * rather than gtk_container_forall().
1139 gtk_container_forall (GtkContainer *container,
1140 GtkCallback callback,
1141 gpointer callback_data)
1143 GtkContainerClass *class;
1145 g_return_if_fail (GTK_IS_CONTAINER (container));
1146 g_return_if_fail (callback != NULL);
1148 class = GTK_CONTAINER_GET_CLASS (container);
1151 class->forall (container, TRUE, callback, callback_data);
1155 * gtk_container_foreach:
1156 * @container: a #GtkContainer
1157 * @callback: a callback
1158 * @callback_data: callback user data
1160 * Invokes @callback on each non-internal child of @container. See
1161 * gtk_container_forall() for details on what constitutes an
1162 * "internal" child. Most applications should use
1163 * gtk_container_foreach(), rather than gtk_container_forall().
1166 gtk_container_foreach (GtkContainer *container,
1167 GtkCallback callback,
1168 gpointer callback_data)
1170 GtkContainerClass *class;
1172 g_return_if_fail (GTK_IS_CONTAINER (container));
1173 g_return_if_fail (callback != NULL);
1175 class = GTK_CONTAINER_GET_CLASS (container);
1178 class->forall (container, FALSE, callback, callback_data);
1181 typedef struct _GtkForeachData GtkForeachData;
1182 struct _GtkForeachData
1184 GtkObject *container;
1185 GtkCallbackMarshal callback;
1186 gpointer callback_data;
1190 gtk_container_foreach_unmarshal (GtkWidget *child,
1193 GtkForeachData *fdata = (GtkForeachData*) data;
1196 /* first argument */
1197 args[0].name = NULL;
1198 args[0].type = GTK_OBJECT_TYPE (child);
1199 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1201 /* location for return value */
1202 args[1].name = NULL;
1203 args[1].type = GTK_TYPE_NONE;
1205 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1209 gtk_container_foreach_full (GtkContainer *container,
1210 GtkCallback callback,
1211 GtkCallbackMarshal marshal,
1212 gpointer callback_data,
1213 GtkDestroyNotify notify)
1215 g_return_if_fail (GTK_IS_CONTAINER (container));
1219 GtkForeachData fdata;
1221 fdata.container = GTK_OBJECT (container);
1222 fdata.callback = marshal;
1223 fdata.callback_data = callback_data;
1225 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1229 g_return_if_fail (callback != NULL);
1231 gtk_container_foreach (container, callback, &callback_data);
1235 notify (callback_data);
1239 gtk_container_set_focus_child (GtkContainer *container,
1242 g_return_if_fail (GTK_IS_CONTAINER (container));
1244 g_return_if_fail (GTK_IS_WIDGET (widget));
1246 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1250 * gtk_container_get_children:
1251 * @container: a #GtkContainer.
1253 * Returns the the container's non-internal children. See
1254 * gtk_container_forall() for details on what constitutes an "internal" child.
1256 * Return value: a newly-allocated list of the container's non-internal children.
1259 gtk_container_get_children (GtkContainer *container)
1261 GList *children = NULL;
1263 gtk_container_foreach (container,
1264 gtk_container_children_callback,
1267 return g_list_reverse (children);
1271 gtk_container_child_position_callback (GtkWidget *widget,
1272 gpointer client_data)
1278 } *data = client_data;
1281 if (data->child == widget)
1282 data->index = data->i;
1286 gtk_container_child_default_composite_name (GtkContainer *container,
1296 /* fallback implementation */
1300 gtk_container_forall (container,
1301 gtk_container_child_position_callback,
1304 name = g_strdup_printf ("%s-%u",
1305 gtk_type_name (GTK_OBJECT_TYPE (child)),
1312 _gtk_container_child_composite_name (GtkContainer *container,
1315 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1316 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1317 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1319 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1321 static GQuark quark_composite_name = 0;
1324 if (!quark_composite_name)
1325 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1327 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1330 GtkContainerClass *class;
1332 class = GTK_CONTAINER_GET_CLASS (container);
1333 if (class->composite_name)
1334 name = class->composite_name (container, child);
1337 name = g_strdup (name);
1346 gtk_container_real_set_focus_child (GtkContainer *container,
1349 g_return_if_fail (GTK_IS_CONTAINER (container));
1351 g_return_if_fail (GTK_IS_WIDGET (child));
1353 if (child != container->focus_child)
1355 if (container->focus_child)
1356 gtk_widget_unref (container->focus_child);
1357 container->focus_child = child;
1358 if (container->focus_child)
1359 gtk_widget_ref (container->focus_child);
1363 /* check for h/v adjustments
1365 if (container->focus_child)
1367 GtkAdjustment *adjustment;
1369 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1371 gtk_adjustment_clamp_page (adjustment,
1372 container->focus_child->allocation.y,
1373 (container->focus_child->allocation.y +
1374 container->focus_child->allocation.height));
1376 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1378 gtk_adjustment_clamp_page (adjustment,
1379 container->focus_child->allocation.x,
1380 (container->focus_child->allocation.x +
1381 container->focus_child->allocation.width));
1386 get_focus_chain (GtkContainer *container)
1388 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1392 gtk_container_focus (GtkWidget *widget,
1393 GtkDirectionType direction)
1396 GList *sorted_children;
1398 GtkContainer *container;
1400 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1402 container = GTK_CONTAINER (widget);
1406 if (GTK_WIDGET_CAN_FOCUS (container))
1408 if (!GTK_WIDGET_HAS_FOCUS (container))
1410 gtk_widget_grab_focus (GTK_WIDGET (container));
1416 /* Get a list of the containers children, allowing focus
1417 * chain to override.
1419 if (container->has_focus_chain)
1420 children = g_list_copy (get_focus_chain (container));
1422 children = gtk_container_get_children (container);
1424 if (container->has_focus_chain &&
1425 (direction == GTK_DIR_TAB_FORWARD ||
1426 direction == GTK_DIR_TAB_BACKWARD))
1428 sorted_children = g_list_copy (children);
1430 if (direction == GTK_DIR_TAB_BACKWARD)
1431 sorted_children = g_list_reverse (sorted_children);
1434 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1436 return_val = gtk_container_focus_move (container, sorted_children, direction);
1438 g_list_free (sorted_children);
1439 g_list_free (children);
1446 tab_compare (gconstpointer a,
1449 const GtkWidget *child1 = a;
1450 const GtkWidget *child2 = b;
1452 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1453 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1457 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1458 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1460 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1463 return (y1 < y2) ? -1 : 1;
1467 gtk_container_focus_sort_tab (GtkContainer *container,
1469 GtkDirectionType direction,
1470 GtkWidget *old_focus)
1472 children = g_list_sort (children, tab_compare);
1474 /* if we are going backwards then reverse the order
1477 if (direction == GTK_DIR_TAB_BACKWARD)
1478 children = g_list_reverse (children);
1483 /* Get coordinates of @widget's allocation with respect to
1484 * allocation of @container.
1487 get_allocation_coords (GtkContainer *container,
1489 GdkRectangle *allocation)
1491 *allocation = widget->allocation;
1493 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1494 0, 0, &allocation->x, &allocation->y);
1497 /* Look for a child in @children that is intermediate between
1498 * the focus widget and container. This widget, if it exists,
1499 * acts as the starting widget for focus navigation.
1502 find_old_focus (GtkContainer *container,
1505 GList *tmp_list = children;
1508 GtkWidget *child = tmp_list->data;
1509 GtkWidget *widget = child;
1511 while (widget && widget != (GtkWidget *)container)
1513 GtkWidget *parent = widget->parent;
1514 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1523 tmp_list = tmp_list->next;
1530 old_focus_coords (GtkContainer *container,
1531 GdkRectangle *old_focus_rect)
1533 GtkWidget *widget = GTK_WIDGET (container);
1534 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1536 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1538 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1540 return get_allocation_coords (container, old_focus, old_focus_rect);
1546 typedef struct _CompareInfo CompareInfo;
1550 GtkContainer *container;
1557 up_down_compare (gconstpointer a,
1561 GdkRectangle allocation1;
1562 GdkRectangle allocation2;
1563 CompareInfo *compare = data;
1566 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1567 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1569 y1 = allocation1.y + allocation1.height / 2;
1570 y2 = allocation2.y + allocation2.height / 2;
1574 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1575 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1577 if (compare->reverse)
1578 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1580 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1583 return (y1 < y2) ? -1 : 1;
1587 gtk_container_focus_sort_up_down (GtkContainer *container,
1589 GtkDirectionType direction,
1590 GtkWidget *old_focus)
1592 CompareInfo compare;
1595 compare.container = container;
1596 compare.reverse = (direction == GTK_DIR_UP);
1599 old_focus = find_old_focus (container, children);
1603 GdkRectangle old_allocation;
1608 /* Delete widgets from list that don't match minimum criteria */
1610 get_allocation_coords (container, old_focus, &old_allocation);
1612 compare_x1 = old_allocation.x;
1613 compare_x2 = old_allocation.x + old_allocation.width;
1615 if (direction == GTK_DIR_UP)
1616 compare_y = old_allocation.y;
1618 compare_y = old_allocation.y + old_allocation.height;
1620 tmp_list = children;
1623 GtkWidget *child = tmp_list->data;
1624 GList *next = tmp_list->next;
1625 gint child_x1, child_x2;
1626 GdkRectangle child_allocation;
1628 if (child != old_focus)
1630 get_allocation_coords (container, child, &child_allocation);
1632 child_x1 = child_allocation.x;
1633 child_x2 = child_allocation.x + child_allocation.width;
1635 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1636 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1637 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1639 children = g_list_delete_link (children, tmp_list);
1646 compare.x = (compare_x1 + compare_x2) / 2;
1647 compare.y = old_allocation.y + old_allocation.height / 2;
1651 /* No old focus widget, need to figure out starting x,y some other way
1653 GtkWidget *widget = GTK_WIDGET (container);
1654 GdkRectangle old_focus_rect;
1656 if (old_focus_coords (container, &old_focus_rect))
1658 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1662 if (GTK_WIDGET_NO_WINDOW (widget))
1663 compare.x = widget->allocation.x + widget->allocation.width / 2;
1665 compare.x = widget->allocation.width / 2;
1668 if (GTK_WIDGET_NO_WINDOW (widget))
1669 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1671 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1674 children = g_list_sort_with_data (children, up_down_compare, &compare);
1676 if (compare.reverse)
1677 children = g_list_reverse (children);
1683 left_right_compare (gconstpointer a,
1687 GdkRectangle allocation1;
1688 GdkRectangle allocation2;
1689 CompareInfo *compare = data;
1692 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1693 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1695 x1 = allocation1.x + allocation1.width / 2;
1696 x2 = allocation2.x + allocation2.width / 2;
1700 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1701 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1703 if (compare->reverse)
1704 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1706 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1709 return (x1 < x2) ? -1 : 1;
1713 gtk_container_focus_sort_left_right (GtkContainer *container,
1715 GtkDirectionType direction,
1716 GtkWidget *old_focus)
1718 CompareInfo compare;
1721 compare.container = container;
1722 compare.reverse = (direction == GTK_DIR_LEFT);
1725 old_focus = find_old_focus (container, children);
1729 GdkRectangle old_allocation;
1735 /* Delete widgets from list that don't match minimum criteria */
1737 get_allocation_coords (container, old_focus, &old_allocation);
1739 compare_y1 = old_allocation.y;
1740 compare_y2 = old_allocation.y + old_allocation.height;
1742 if (direction == GTK_DIR_LEFT)
1743 compare_x = old_allocation.x;
1745 compare_x = old_allocation.x + old_allocation.width;
1747 tmp_list = children;
1750 GtkWidget *child = tmp_list->data;
1751 GList *next = tmp_list->next;
1752 gint child_y1, child_y2;
1753 GdkRectangle child_allocation;
1755 if (child != old_focus)
1757 get_allocation_coords (container, child, &child_allocation);
1759 child_y1 = child_allocation.y;
1760 child_y2 = child_allocation.y + child_allocation.height;
1762 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1763 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1764 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1766 children = g_list_delete_link (children, tmp_list);
1773 compare.y = (compare_y1 + compare_y2) / 2;
1774 compare.x = old_allocation.x + old_allocation.width / 2;
1778 /* No old focus widget, need to figure out starting x,y some other way
1780 GtkWidget *widget = GTK_WIDGET (container);
1781 GdkRectangle old_focus_rect;
1783 if (old_focus_coords (container, &old_focus_rect))
1785 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1789 if (GTK_WIDGET_NO_WINDOW (widget))
1790 compare.y = widget->allocation.y + widget->allocation.height / 2;
1792 compare.y = widget->allocation.height / 2;
1795 if (GTK_WIDGET_NO_WINDOW (widget))
1796 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1798 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1801 children = g_list_sort_with_data (children, left_right_compare, &compare);
1803 if (compare.reverse)
1804 children = g_list_reverse (children);
1810 * gtk_container_focus_sort:
1811 * @container: a #GtkContainer
1812 * @children: a list of descendents of @container (they don't
1813 * have to be direct children.
1814 * @direction: focus direction
1815 * @old_focus: widget to use for the starting position, or %NULL
1816 * to determine this automatically.
1817 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1818 * which is the only @direction we use currently,
1819 * so perhaps this argument should be removed ]
1821 * Sorts @children in the correct order for focusing with
1822 * direction type @direction.
1824 * Return value: a copy of @children, sorted in correct focusing order,
1825 * with children that aren't suitable for focusing in this direction
1829 _gtk_container_focus_sort (GtkContainer *container,
1831 GtkDirectionType direction,
1832 GtkWidget *old_focus)
1834 children = g_list_copy (children);
1838 case GTK_DIR_TAB_FORWARD:
1839 case GTK_DIR_TAB_BACKWARD:
1840 return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1843 return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1846 return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
1849 g_assert_not_reached ();
1855 gtk_container_focus_move (GtkContainer *container,
1857 GtkDirectionType direction)
1859 GtkWidget *focus_child;
1862 focus_child = container->focus_child;
1866 child = children->data;
1867 children = children->next;
1874 if (focus_child == child)
1878 if (gtk_widget_child_focus (child, direction))
1882 else if (GTK_WIDGET_DRAWABLE (child) &&
1883 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1885 if (gtk_widget_child_focus (child, direction))
1895 gtk_container_children_callback (GtkWidget *widget,
1896 gpointer client_data)
1900 children = (GList**) client_data;
1901 *children = g_list_prepend (*children, widget);
1905 chain_widget_destroyed (GtkWidget *widget,
1908 GtkContainer *container;
1911 container = GTK_CONTAINER (user_data);
1913 chain = g_object_get_data (G_OBJECT (container),
1914 "gtk-container-focus-chain");
1916 chain = g_list_remove (chain, widget);
1918 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1919 chain_widget_destroyed,
1922 g_object_set_data (G_OBJECT (container),
1923 "gtk-container-focus-chain",
1928 * gtk_container_set_focus_chain:
1929 * @container: a #GtkContainer.
1930 * @focusable_widgets: the new focus chain.
1932 * Sets a focus chain, overriding the one computed automatically by GTK+.
1934 * In principle each widget in the chain should be a descendant of the
1935 * container, but this is not enforced by this method, since it's allowed
1936 * to set the focus chain before you pack the widgets, or have a widget
1937 * in the chain that isn't always packed. The necessary checks are done
1938 * when the focus chain is actually traversed.
1941 gtk_container_set_focus_chain (GtkContainer *container,
1942 GList *focusable_widgets)
1947 g_return_if_fail (GTK_IS_CONTAINER (container));
1949 if (container->has_focus_chain)
1950 gtk_container_unset_focus_chain (container);
1952 container->has_focus_chain = TRUE;
1955 tmp_list = focusable_widgets;
1956 while (tmp_list != NULL)
1958 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
1960 /* In principle each widget in the chain should be a descendant
1961 * of the container, but we don't want to check that here, it's
1962 * expensive and also it's allowed to set the focus chain before
1963 * you pack the widgets, or have a widget in the chain that isn't
1964 * always packed. So we check for ancestor during actual traversal.
1967 chain = g_list_prepend (chain, tmp_list->data);
1969 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
1971 GTK_SIGNAL_FUNC (chain_widget_destroyed),
1974 tmp_list = g_list_next (tmp_list);
1977 chain = g_list_reverse (chain);
1979 g_object_set_data (G_OBJECT (container),
1980 "gtk-container-focus-chain",
1985 * gtk_container_get_focus_chain:
1986 * @container: a #GtkContainer
1987 * @focusable_widgets: location to store the focus chain of the
1988 * container, or %NULL. You should free this list
1989 * using g_list_free() when you are done with it, however
1990 * no additional reference count is added to the
1991 * individual widgets in the focus chain.
1993 * Retrieves the focus chain of the container, if one has been
1994 * set explicitly. If no focus chain has been explicitly
1995 * set, GTK+ computes the focus chain based on the positions
1996 * of the children. In that case, GTK+ stores %NULL in
1997 * @focusable_widgets and returns %FALSE.
1999 * Return value: %TRUE if the focus chain of the container
2000 * has been set explicitly.
2003 gtk_container_get_focus_chain (GtkContainer *container,
2004 GList **focus_chain)
2006 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2010 if (container->has_focus_chain)
2011 *focus_chain = g_list_copy (get_focus_chain (container));
2013 *focus_chain = NULL;
2016 return container->has_focus_chain;
2020 * gtk_container_unset_focus_chain:
2021 * @container: a #GtkContainer.
2023 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2026 gtk_container_unset_focus_chain (GtkContainer *container)
2028 g_return_if_fail (GTK_IS_CONTAINER (container));
2030 if (container->has_focus_chain)
2035 chain = get_focus_chain (container);
2037 container->has_focus_chain = FALSE;
2039 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2043 while (tmp_list != NULL)
2045 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2046 chain_widget_destroyed,
2049 tmp_list = g_list_next (tmp_list);
2052 g_list_free (chain);
2057 gtk_container_set_focus_vadjustment (GtkContainer *container,
2058 GtkAdjustment *adjustment)
2060 g_return_if_fail (GTK_IS_CONTAINER (container));
2062 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2065 gtk_object_ref (GTK_OBJECT(adjustment));
2067 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2070 (GtkDestroyNotify) gtk_object_unref);
2074 * gtk_container_get_focus_vadjustment:
2075 * @container: a #GtkContainer
2077 * Retrieves the vertical focus adjustment for the container. See
2078 * gtk_container_set_focus_vadjustment ().
2080 * Return value: the vertical focus adjustment, or %NULL if
2081 * none has been set.
2084 gtk_container_get_focus_vadjustment (GtkContainer *container)
2086 GtkAdjustment *vadjustment;
2088 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2090 vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2091 vadjustment_key_id);
2097 gtk_container_set_focus_hadjustment (GtkContainer *container,
2098 GtkAdjustment *adjustment)
2100 g_return_if_fail (GTK_IS_CONTAINER (container));
2102 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2105 gtk_object_ref (GTK_OBJECT (adjustment));
2107 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2110 (GtkDestroyNotify) gtk_object_unref);
2114 * gtk_container_get_focus_hadjustment:
2115 * @container: a #GtkContainer
2117 * Retrieves the horizontal focus adjustment for the container. See
2118 * gtk_container_set_focus_hadjustment ().
2120 * Return value: the horizontal focus adjustment, or %NULL if
2121 * none has been set.
2124 gtk_container_get_focus_hadjustment (GtkContainer *container)
2126 GtkAdjustment *hadjustment;
2128 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2130 hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2131 hadjustment_key_id);
2138 gtk_container_show_all (GtkWidget *widget)
2140 g_return_if_fail (GTK_IS_CONTAINER (widget));
2142 gtk_container_foreach (GTK_CONTAINER (widget),
2143 (GtkCallback) gtk_widget_show_all,
2145 gtk_widget_show (widget);
2149 gtk_container_hide_all (GtkWidget *widget)
2151 g_return_if_fail (GTK_IS_CONTAINER (widget));
2153 gtk_widget_hide (widget);
2154 gtk_container_foreach (GTK_CONTAINER (widget),
2155 (GtkCallback) gtk_widget_hide_all,
2161 gtk_container_expose_child (GtkWidget *child,
2162 gpointer client_data)
2165 GtkWidget *container;
2166 GdkEventExpose *event;
2167 } *data = client_data;
2169 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2175 gtk_container_expose (GtkWidget *widget,
2176 GdkEventExpose *event)
2179 GtkWidget *container;
2180 GdkEventExpose *event;
2183 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2184 g_return_val_if_fail (event != NULL, FALSE);
2187 if (GTK_WIDGET_DRAWABLE (widget))
2189 data.container = widget;
2192 gtk_container_forall (GTK_CONTAINER (widget),
2193 gtk_container_expose_child,
2201 gtk_container_map_child (GtkWidget *child,
2202 gpointer client_data)
2204 if (GTK_WIDGET_VISIBLE (child) &&
2205 GTK_WIDGET_CHILD_VISIBLE (child) &&
2206 !GTK_WIDGET_MAPPED (child))
2207 gtk_widget_map (child);
2211 gtk_container_map (GtkWidget *widget)
2213 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2215 gtk_container_forall (GTK_CONTAINER (widget),
2216 gtk_container_map_child,
2219 if (!GTK_WIDGET_NO_WINDOW (widget))
2220 gdk_window_show (widget->window);
2224 gtk_container_unmap (GtkWidget *widget)
2226 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2228 if (!GTK_WIDGET_NO_WINDOW (widget))
2229 gdk_window_hide (widget->window);
2231 gtk_container_forall (GTK_CONTAINER (widget),
2232 (GtkCallback)gtk_widget_unmap,
2237 * gtk_container_propagate_expose:
2238 * @container: a #GtkContainer
2239 * @child: a child of @container
2240 * @event: a expose event sent to container
2242 * When a container receives an expose event, it must send synthetic
2243 * expose events to all children that don't have their own #GdkWindows.
2244 * This function provides a convenient way of doing this. A container,
2245 * when it receives an expose event, calls gtk_container_propagate_expose()
2246 * once for each child, passing in the event the container received.
2248 * gtk_container_propagate_expose() takes care of deciding whether
2249 * an expose event needs to be sent to the child, intersecting
2250 * the event's area with the child area, and sending the event.
2252 * In most cases, a container can simply either simply inherit the
2253 * ::expose implementation from #GtkContainer, or, do some drawing
2254 * and then chain to the ::expose implementation from #GtkContainer.
2257 gtk_container_propagate_expose (GtkContainer *container,
2259 GdkEventExpose *event)
2261 GdkEventExpose child_event;
2263 g_return_if_fail (GTK_IS_CONTAINER (container));
2264 g_return_if_fail (GTK_IS_WIDGET (child));
2265 g_return_if_fail (event != NULL);
2267 g_assert (child->parent == GTK_WIDGET (container));
2269 if (GTK_WIDGET_DRAWABLE (child) &&
2270 GTK_WIDGET_NO_WINDOW (child) &&
2271 (child->window == event->window))
2273 child_event = *event;
2275 child_event.region = gtk_widget_region_intersect (child, event->region);
2276 if (!gdk_region_empty (child_event.region))
2278 gdk_region_get_clipbox (child_event.region, &child_event.area);
2279 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2281 gdk_region_destroy (child_event.region);