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;
1017 g_return_if_fail (GTK_IS_CONTAINER (container));
1019 widget = GTK_WIDGET (container);
1020 resize_container = gtk_container_get_resize_container (container);
1022 while (!GTK_WIDGET_ALLOC_NEEDED (widget) || !GTK_WIDGET_REQUEST_NEEDED (widget))
1024 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1025 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1026 if ((resize_container && widget == GTK_WIDGET (resize_container)) ||
1030 widget = widget->parent;
1033 if (resize_container)
1035 if (GTK_WIDGET_VISIBLE (resize_container) &&
1036 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_REALIZED (resize_container)))
1038 switch (resize_container->resize_mode)
1040 case GTK_RESIZE_QUEUE:
1041 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
1043 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
1044 if (container_resize_queue == NULL)
1045 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
1046 gtk_container_idle_sizer,
1048 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
1052 case GTK_RESIZE_IMMEDIATE:
1053 gtk_container_check_resize (resize_container);
1056 case GTK_RESIZE_PARENT:
1057 g_assert_not_reached ();
1063 /* we need to let hidden resize containers know that something
1064 * changed while they where hidden (currently only evaluated by
1067 resize_container->need_resize = TRUE;
1073 gtk_container_check_resize (GtkContainer *container)
1075 g_return_if_fail (GTK_IS_CONTAINER (container));
1077 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1081 gtk_container_real_check_resize (GtkContainer *container)
1083 GtkWidget *widget = GTK_WIDGET (container);
1084 GtkRequisition requisition;
1086 gtk_widget_size_request (widget, &requisition);
1088 if (requisition.width > widget->allocation.width ||
1089 requisition.height > widget->allocation.height)
1091 if (GTK_IS_RESIZE_CONTAINER (container))
1092 gtk_widget_size_allocate (GTK_WIDGET (container),
1093 >K_WIDGET (container)->allocation);
1095 gtk_widget_queue_resize (widget);
1099 gtk_container_resize_children (container);
1103 /* The container hasn't changed size but one of its children
1104 * queued a resize request. Which means that the allocation
1105 * is not sufficient for the requisition of some child.
1106 * We've already performed a size request at this point,
1107 * so we simply need to reallocate and let the allocation
1108 * trickle down via GTK_WIDGET_ALLOC_NEEDED flags.
1111 gtk_container_resize_children (GtkContainer *container)
1115 /* resizing invariants:
1116 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1117 * containers that have an idle sizer pending must be flagged with
1120 g_return_if_fail (GTK_IS_CONTAINER (container));
1122 widget = GTK_WIDGET (container);
1123 gtk_widget_size_allocate (widget, &widget->allocation);
1127 * gtk_container_forall:
1128 * @container: a #GtkContainer
1129 * @callback: a callback
1130 * @callback_data: callback user data
1132 * Invokes @callback on each child of @container, including children
1133 * that are considered "internal" (implementation details of the
1134 * container). "Internal" children generally weren't added by the user
1135 * of the container, but were added by the container implementation
1136 * itself. Most applications should use gtk_container_foreach(),
1137 * rather than gtk_container_forall().
1140 gtk_container_forall (GtkContainer *container,
1141 GtkCallback callback,
1142 gpointer callback_data)
1144 GtkContainerClass *class;
1146 g_return_if_fail (GTK_IS_CONTAINER (container));
1147 g_return_if_fail (callback != NULL);
1149 class = GTK_CONTAINER_GET_CLASS (container);
1152 class->forall (container, TRUE, callback, callback_data);
1156 * gtk_container_foreach:
1157 * @container: a #GtkContainer
1158 * @callback: a callback
1159 * @callback_data: callback user data
1161 * Invokes @callback on each non-internal child of @container. See
1162 * gtk_container_forall() for details on what constitutes an
1163 * "internal" child. Most applications should use
1164 * gtk_container_foreach(), rather than gtk_container_forall().
1167 gtk_container_foreach (GtkContainer *container,
1168 GtkCallback callback,
1169 gpointer callback_data)
1171 GtkContainerClass *class;
1173 g_return_if_fail (GTK_IS_CONTAINER (container));
1174 g_return_if_fail (callback != NULL);
1176 class = GTK_CONTAINER_GET_CLASS (container);
1179 class->forall (container, FALSE, callback, callback_data);
1182 typedef struct _GtkForeachData GtkForeachData;
1183 struct _GtkForeachData
1185 GtkObject *container;
1186 GtkCallbackMarshal callback;
1187 gpointer callback_data;
1191 gtk_container_foreach_unmarshal (GtkWidget *child,
1194 GtkForeachData *fdata = (GtkForeachData*) data;
1197 /* first argument */
1198 args[0].name = NULL;
1199 args[0].type = GTK_OBJECT_TYPE (child);
1200 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1202 /* location for return value */
1203 args[1].name = NULL;
1204 args[1].type = GTK_TYPE_NONE;
1206 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1210 gtk_container_foreach_full (GtkContainer *container,
1211 GtkCallback callback,
1212 GtkCallbackMarshal marshal,
1213 gpointer callback_data,
1214 GtkDestroyNotify notify)
1216 g_return_if_fail (GTK_IS_CONTAINER (container));
1220 GtkForeachData fdata;
1222 fdata.container = GTK_OBJECT (container);
1223 fdata.callback = marshal;
1224 fdata.callback_data = callback_data;
1226 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1230 g_return_if_fail (callback != NULL);
1232 gtk_container_foreach (container, callback, &callback_data);
1236 notify (callback_data);
1240 gtk_container_set_focus_child (GtkContainer *container,
1243 g_return_if_fail (GTK_IS_CONTAINER (container));
1245 g_return_if_fail (GTK_IS_WIDGET (widget));
1247 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1251 * gtk_container_get_children:
1252 * @container: a #GtkContainer.
1254 * Returns the the container's non-internal children. See
1255 * gtk_container_forall() for details on what constitutes an "internal" child.
1257 * Return value: a newly-allocated list of the container's non-internal children.
1260 gtk_container_get_children (GtkContainer *container)
1262 GList *children = NULL;
1264 gtk_container_foreach (container,
1265 gtk_container_children_callback,
1268 return g_list_reverse (children);
1272 gtk_container_child_position_callback (GtkWidget *widget,
1273 gpointer client_data)
1279 } *data = client_data;
1282 if (data->child == widget)
1283 data->index = data->i;
1287 gtk_container_child_default_composite_name (GtkContainer *container,
1297 /* fallback implementation */
1301 gtk_container_forall (container,
1302 gtk_container_child_position_callback,
1305 name = g_strdup_printf ("%s-%u",
1306 gtk_type_name (GTK_OBJECT_TYPE (child)),
1313 _gtk_container_child_composite_name (GtkContainer *container,
1316 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1317 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1318 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1320 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1322 static GQuark quark_composite_name = 0;
1325 if (!quark_composite_name)
1326 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1328 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1331 GtkContainerClass *class;
1333 class = GTK_CONTAINER_GET_CLASS (container);
1334 if (class->composite_name)
1335 name = class->composite_name (container, child);
1338 name = g_strdup (name);
1347 gtk_container_real_set_focus_child (GtkContainer *container,
1350 g_return_if_fail (GTK_IS_CONTAINER (container));
1352 g_return_if_fail (GTK_IS_WIDGET (child));
1354 if (child != container->focus_child)
1356 if (container->focus_child)
1357 gtk_widget_unref (container->focus_child);
1358 container->focus_child = child;
1359 if (container->focus_child)
1360 gtk_widget_ref (container->focus_child);
1364 /* check for h/v adjustments
1366 if (container->focus_child)
1368 GtkAdjustment *adjustment;
1370 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1372 gtk_adjustment_clamp_page (adjustment,
1373 container->focus_child->allocation.y,
1374 (container->focus_child->allocation.y +
1375 container->focus_child->allocation.height));
1377 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1379 gtk_adjustment_clamp_page (adjustment,
1380 container->focus_child->allocation.x,
1381 (container->focus_child->allocation.x +
1382 container->focus_child->allocation.width));
1387 get_focus_chain (GtkContainer *container)
1389 return g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1393 gtk_container_focus (GtkWidget *widget,
1394 GtkDirectionType direction)
1397 GList *sorted_children;
1399 GtkContainer *container;
1401 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
1403 container = GTK_CONTAINER (widget);
1407 if (GTK_WIDGET_CAN_FOCUS (container))
1409 if (!GTK_WIDGET_HAS_FOCUS (container))
1411 gtk_widget_grab_focus (GTK_WIDGET (container));
1417 /* Get a list of the containers children, allowing focus
1418 * chain to override.
1420 if (container->has_focus_chain)
1421 children = g_list_copy (get_focus_chain (container));
1423 children = gtk_container_get_children (container);
1425 if (container->has_focus_chain &&
1426 (direction == GTK_DIR_TAB_FORWARD ||
1427 direction == GTK_DIR_TAB_BACKWARD))
1429 sorted_children = g_list_copy (children);
1431 if (direction == GTK_DIR_TAB_BACKWARD)
1432 sorted_children = g_list_reverse (sorted_children);
1435 sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
1437 return_val = gtk_container_focus_move (container, sorted_children, direction);
1439 g_list_free (sorted_children);
1440 g_list_free (children);
1447 tab_compare (gconstpointer a,
1450 const GtkWidget *child1 = a;
1451 const GtkWidget *child2 = b;
1453 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1454 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1458 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1459 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1461 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1464 return (y1 < y2) ? -1 : 1;
1468 gtk_container_focus_sort_tab (GtkContainer *container,
1470 GtkDirectionType direction,
1471 GtkWidget *old_focus)
1473 children = g_list_sort (children, tab_compare);
1475 /* if we are going backwards then reverse the order
1478 if (direction == GTK_DIR_TAB_BACKWARD)
1479 children = g_list_reverse (children);
1484 /* Get coordinates of @widget's allocation with respect to
1485 * allocation of @container.
1488 get_allocation_coords (GtkContainer *container,
1490 GdkRectangle *allocation)
1492 *allocation = widget->allocation;
1494 return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
1495 0, 0, &allocation->x, &allocation->y);
1498 /* Look for a child in @children that is intermediate between
1499 * the focus widget and container. This widget, if it exists,
1500 * acts as the starting widget for focus navigation.
1503 find_old_focus (GtkContainer *container,
1506 GList *tmp_list = children;
1509 GtkWidget *child = tmp_list->data;
1510 GtkWidget *widget = child;
1512 while (widget && widget != (GtkWidget *)container)
1514 GtkWidget *parent = widget->parent;
1515 if (parent && ((GtkContainer *)parent)->focus_child != widget)
1524 tmp_list = tmp_list->next;
1531 old_focus_coords (GtkContainer *container,
1532 GdkRectangle *old_focus_rect)
1534 GtkWidget *widget = GTK_WIDGET (container);
1535 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1537 if (toplevel && GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
1539 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1541 return get_allocation_coords (container, old_focus, old_focus_rect);
1547 typedef struct _CompareInfo CompareInfo;
1551 GtkContainer *container;
1558 up_down_compare (gconstpointer a,
1562 GdkRectangle allocation1;
1563 GdkRectangle allocation2;
1564 CompareInfo *compare = data;
1567 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1568 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1570 y1 = allocation1.y + allocation1.height / 2;
1571 y2 = allocation2.y + allocation2.height / 2;
1575 gint x1 = abs (allocation1.x + allocation1.width / 2 - compare->x);
1576 gint x2 = abs (allocation2.x + allocation2.width / 2 - compare->x);
1578 if (compare->reverse)
1579 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1581 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1584 return (y1 < y2) ? -1 : 1;
1588 gtk_container_focus_sort_up_down (GtkContainer *container,
1590 GtkDirectionType direction,
1591 GtkWidget *old_focus)
1593 CompareInfo compare;
1596 compare.container = container;
1597 compare.reverse = (direction == GTK_DIR_UP);
1600 old_focus = find_old_focus (container, children);
1604 GdkRectangle old_allocation;
1609 /* Delete widgets from list that don't match minimum criteria */
1611 get_allocation_coords (container, old_focus, &old_allocation);
1613 compare_x1 = old_allocation.x;
1614 compare_x2 = old_allocation.x + old_allocation.width;
1616 if (direction == GTK_DIR_UP)
1617 compare_y = old_allocation.y;
1619 compare_y = old_allocation.y + old_allocation.height;
1621 tmp_list = children;
1624 GtkWidget *child = tmp_list->data;
1625 GList *next = tmp_list->next;
1626 gint child_x1, child_x2;
1627 GdkRectangle child_allocation;
1629 if (child != old_focus)
1631 get_allocation_coords (container, child, &child_allocation);
1633 child_x1 = child_allocation.x;
1634 child_x2 = child_allocation.x + child_allocation.width;
1636 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1637 (direction == GTK_DIR_DOWN && child_allocation.y + child_allocation.height < compare_y) || /* Not below */
1638 (direction == GTK_DIR_UP && child_allocation.y > compare_y)) /* Not above */
1640 children = g_list_delete_link (children, tmp_list);
1647 compare.x = (compare_x1 + compare_x2) / 2;
1648 compare.y = old_allocation.y + old_allocation.height / 2;
1652 /* No old focus widget, need to figure out starting x,y some other way
1654 GtkWidget *widget = GTK_WIDGET (container);
1655 GdkRectangle old_focus_rect;
1657 if (old_focus_coords (container, &old_focus_rect))
1659 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1663 if (GTK_WIDGET_NO_WINDOW (widget))
1664 compare.x = widget->allocation.x + widget->allocation.width / 2;
1666 compare.x = widget->allocation.width / 2;
1669 if (GTK_WIDGET_NO_WINDOW (widget))
1670 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1672 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1675 children = g_list_sort_with_data (children, up_down_compare, &compare);
1677 if (compare.reverse)
1678 children = g_list_reverse (children);
1684 left_right_compare (gconstpointer a,
1688 GdkRectangle allocation1;
1689 GdkRectangle allocation2;
1690 CompareInfo *compare = data;
1693 get_allocation_coords (compare->container, (GtkWidget *)a, &allocation1);
1694 get_allocation_coords (compare->container, (GtkWidget *)b, &allocation2);
1696 x1 = allocation1.x + allocation1.width / 2;
1697 x2 = allocation2.x + allocation2.width / 2;
1701 gint y1 = abs (allocation1.y + allocation1.height / 2 - compare->y);
1702 gint y2 = abs (allocation2.y + allocation2.height / 2 - compare->y);
1704 if (compare->reverse)
1705 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1707 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1710 return (x1 < x2) ? -1 : 1;
1714 gtk_container_focus_sort_left_right (GtkContainer *container,
1716 GtkDirectionType direction,
1717 GtkWidget *old_focus)
1719 CompareInfo compare;
1722 compare.container = container;
1723 compare.reverse = (direction == GTK_DIR_LEFT);
1726 old_focus = find_old_focus (container, children);
1730 GdkRectangle old_allocation;
1736 /* Delete widgets from list that don't match minimum criteria */
1738 get_allocation_coords (container, old_focus, &old_allocation);
1740 compare_y1 = old_allocation.y;
1741 compare_y2 = old_allocation.y + old_allocation.height;
1743 if (direction == GTK_DIR_LEFT)
1744 compare_x = old_allocation.x;
1746 compare_x = old_allocation.x + old_allocation.width;
1748 tmp_list = children;
1751 GtkWidget *child = tmp_list->data;
1752 GList *next = tmp_list->next;
1753 gint child_y1, child_y2;
1754 GdkRectangle child_allocation;
1756 if (child != old_focus)
1758 get_allocation_coords (container, child, &child_allocation);
1760 child_y1 = child_allocation.y;
1761 child_y2 = child_allocation.y + child_allocation.height;
1763 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1764 (direction == GTK_DIR_RIGHT && child_allocation.x + child_allocation.width < compare_x) || /* Not to left */
1765 (direction == GTK_DIR_LEFT && child_allocation.x > compare_x)) /* Not to right */
1767 children = g_list_delete_link (children, tmp_list);
1774 compare.y = (compare_y1 + compare_y2) / 2;
1775 compare.x = old_allocation.x + old_allocation.width / 2;
1779 /* No old focus widget, need to figure out starting x,y some other way
1781 GtkWidget *widget = GTK_WIDGET (container);
1782 GdkRectangle old_focus_rect;
1784 if (old_focus_coords (container, &old_focus_rect))
1786 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1790 if (GTK_WIDGET_NO_WINDOW (widget))
1791 compare.y = widget->allocation.y + widget->allocation.height / 2;
1793 compare.y = widget->allocation.height / 2;
1796 if (GTK_WIDGET_NO_WINDOW (widget))
1797 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1799 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1802 children = g_list_sort_with_data (children, left_right_compare, &compare);
1804 if (compare.reverse)
1805 children = g_list_reverse (children);
1811 * gtk_container_focus_sort:
1812 * @container: a #GtkContainer
1813 * @children: a list of descendents of @container (they don't
1814 * have to be direct children.
1815 * @direction: focus direction
1816 * @old_focus: widget to use for the starting position, or %NULL
1817 * to determine this automatically.
1818 * [ Note, this argument isn't used for GTK_DIR_TAB_*,
1819 * which is the only @direction we use currently,
1820 * so perhaps this argument should be removed ]
1822 * Sorts @children in the correct order for focusing with
1823 * direction type @direction.
1825 * Return value: a copy of @children, sorted in correct focusing order,
1826 * with children that aren't suitable for focusing in this direction
1830 _gtk_container_focus_sort (GtkContainer *container,
1832 GtkDirectionType direction,
1833 GtkWidget *old_focus)
1835 children = g_list_copy (children);
1839 case GTK_DIR_TAB_FORWARD:
1840 case GTK_DIR_TAB_BACKWARD:
1841 return gtk_container_focus_sort_tab (container, children, direction, old_focus);
1844 return gtk_container_focus_sort_up_down (container, children, direction, old_focus);
1847 return gtk_container_focus_sort_left_right (container, children, direction, old_focus);
1850 g_assert_not_reached ();
1856 gtk_container_focus_move (GtkContainer *container,
1858 GtkDirectionType direction)
1860 GtkWidget *focus_child;
1863 focus_child = container->focus_child;
1867 child = children->data;
1868 children = children->next;
1875 if (focus_child == child)
1879 if (gtk_widget_child_focus (child, direction))
1883 else if (GTK_WIDGET_DRAWABLE (child) &&
1884 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1886 if (gtk_widget_child_focus (child, direction))
1896 gtk_container_children_callback (GtkWidget *widget,
1897 gpointer client_data)
1901 children = (GList**) client_data;
1902 *children = g_list_prepend (*children, widget);
1906 chain_widget_destroyed (GtkWidget *widget,
1909 GtkContainer *container;
1912 container = GTK_CONTAINER (user_data);
1914 chain = g_object_get_data (G_OBJECT (container),
1915 "gtk-container-focus-chain");
1917 chain = g_list_remove (chain, widget);
1919 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1920 (gpointer) chain_widget_destroyed,
1923 g_object_set_data (G_OBJECT (container),
1924 "gtk-container-focus-chain",
1929 * gtk_container_set_focus_chain:
1930 * @container: a #GtkContainer.
1931 * @focusable_widgets: the new focus chain.
1933 * Sets a focus chain, overriding the one computed automatically by GTK+.
1935 * In principle each widget in the chain should be a descendant of the
1936 * container, but this is not enforced by this method, since it's allowed
1937 * to set the focus chain before you pack the widgets, or have a widget
1938 * in the chain that isn't always packed. The necessary checks are done
1939 * when the focus chain is actually traversed.
1942 gtk_container_set_focus_chain (GtkContainer *container,
1943 GList *focusable_widgets)
1948 g_return_if_fail (GTK_IS_CONTAINER (container));
1950 if (container->has_focus_chain)
1951 gtk_container_unset_focus_chain (container);
1953 container->has_focus_chain = TRUE;
1956 tmp_list = focusable_widgets;
1957 while (tmp_list != NULL)
1959 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
1961 /* In principle each widget in the chain should be a descendant
1962 * of the container, but we don't want to check that here, it's
1963 * expensive and also it's allowed to set the focus chain before
1964 * you pack the widgets, or have a widget in the chain that isn't
1965 * always packed. So we check for ancestor during actual traversal.
1968 chain = g_list_prepend (chain, tmp_list->data);
1970 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
1972 GTK_SIGNAL_FUNC (chain_widget_destroyed),
1975 tmp_list = g_list_next (tmp_list);
1978 chain = g_list_reverse (chain);
1980 g_object_set_data (G_OBJECT (container),
1981 "gtk-container-focus-chain",
1986 * gtk_container_get_focus_chain:
1987 * @container: a #GtkContainer
1988 * @focusable_widgets: location to store the focus chain of the
1989 * container, or %NULL. You should free this list
1990 * using g_list_free() when you are done with it, however
1991 * no additional reference count is added to the
1992 * individual widgets in the focus chain.
1994 * Retrieves the focus chain of the container, if one has been
1995 * set explicitly. If no focus chain has been explicitly
1996 * set, GTK+ computes the focus chain based on the positions
1997 * of the children. In that case, GTK+ stores %NULL in
1998 * @focusable_widgets and returns %FALSE.
2000 * Return value: %TRUE if the focus chain of the container
2001 * has been set explicitly.
2004 gtk_container_get_focus_chain (GtkContainer *container,
2005 GList **focus_chain)
2007 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
2011 if (container->has_focus_chain)
2012 *focus_chain = g_list_copy (get_focus_chain (container));
2014 *focus_chain = NULL;
2017 return container->has_focus_chain;
2021 * gtk_container_unset_focus_chain:
2022 * @container: a #GtkContainer.
2024 * Removes a focus chain explicitly set with gtk_container_set_focus_chain().
2027 gtk_container_unset_focus_chain (GtkContainer *container)
2029 g_return_if_fail (GTK_IS_CONTAINER (container));
2031 if (container->has_focus_chain)
2036 chain = get_focus_chain (container);
2038 container->has_focus_chain = FALSE;
2040 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2044 while (tmp_list != NULL)
2046 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2047 (gpointer) chain_widget_destroyed,
2050 tmp_list = g_list_next (tmp_list);
2053 g_list_free (chain);
2058 gtk_container_set_focus_vadjustment (GtkContainer *container,
2059 GtkAdjustment *adjustment)
2061 g_return_if_fail (GTK_IS_CONTAINER (container));
2063 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2066 gtk_object_ref (GTK_OBJECT(adjustment));
2068 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2071 (GtkDestroyNotify) gtk_object_unref);
2075 * gtk_container_get_focus_vadjustment:
2076 * @container: a #GtkContainer
2078 * Retrieves the vertical focus adjustment for the container. See
2079 * gtk_container_set_focus_vadjustment ().
2081 * Return value: the vertical focus adjustment, or %NULL if
2082 * none has been set.
2085 gtk_container_get_focus_vadjustment (GtkContainer *container)
2087 GtkAdjustment *vadjustment;
2089 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2091 vadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2092 vadjustment_key_id);
2098 gtk_container_set_focus_hadjustment (GtkContainer *container,
2099 GtkAdjustment *adjustment)
2101 g_return_if_fail (GTK_IS_CONTAINER (container));
2103 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2106 gtk_object_ref (GTK_OBJECT (adjustment));
2108 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2111 (GtkDestroyNotify) gtk_object_unref);
2115 * gtk_container_get_focus_hadjustment:
2116 * @container: a #GtkContainer
2118 * Retrieves the horizontal focus adjustment for the container. See
2119 * gtk_container_set_focus_hadjustment ().
2121 * Return value: the horizontal focus adjustment, or %NULL if
2122 * none has been set.
2125 gtk_container_get_focus_hadjustment (GtkContainer *container)
2127 GtkAdjustment *hadjustment;
2129 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
2131 hadjustment = gtk_object_get_data_by_id (GTK_OBJECT (container),
2132 hadjustment_key_id);
2139 gtk_container_show_all (GtkWidget *widget)
2141 g_return_if_fail (GTK_IS_CONTAINER (widget));
2143 gtk_container_foreach (GTK_CONTAINER (widget),
2144 (GtkCallback) gtk_widget_show_all,
2146 gtk_widget_show (widget);
2150 gtk_container_hide_all (GtkWidget *widget)
2152 g_return_if_fail (GTK_IS_CONTAINER (widget));
2154 gtk_widget_hide (widget);
2155 gtk_container_foreach (GTK_CONTAINER (widget),
2156 (GtkCallback) gtk_widget_hide_all,
2162 gtk_container_expose_child (GtkWidget *child,
2163 gpointer client_data)
2166 GtkWidget *container;
2167 GdkEventExpose *event;
2168 } *data = client_data;
2170 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2176 gtk_container_expose (GtkWidget *widget,
2177 GdkEventExpose *event)
2180 GtkWidget *container;
2181 GdkEventExpose *event;
2184 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2185 g_return_val_if_fail (event != NULL, FALSE);
2188 if (GTK_WIDGET_DRAWABLE (widget))
2190 data.container = widget;
2193 gtk_container_forall (GTK_CONTAINER (widget),
2194 gtk_container_expose_child,
2202 gtk_container_map_child (GtkWidget *child,
2203 gpointer client_data)
2205 if (GTK_WIDGET_VISIBLE (child) &&
2206 GTK_WIDGET_CHILD_VISIBLE (child) &&
2207 !GTK_WIDGET_MAPPED (child))
2208 gtk_widget_map (child);
2212 gtk_container_map (GtkWidget *widget)
2214 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
2216 gtk_container_forall (GTK_CONTAINER (widget),
2217 gtk_container_map_child,
2220 if (!GTK_WIDGET_NO_WINDOW (widget))
2221 gdk_window_show (widget->window);
2225 gtk_container_unmap (GtkWidget *widget)
2227 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
2229 if (!GTK_WIDGET_NO_WINDOW (widget))
2230 gdk_window_hide (widget->window);
2232 gtk_container_forall (GTK_CONTAINER (widget),
2233 (GtkCallback)gtk_widget_unmap,
2238 * gtk_container_propagate_expose:
2239 * @container: a #GtkContainer
2240 * @child: a child of @container
2241 * @event: a expose event sent to container
2243 * When a container receives an expose event, it must send synthetic
2244 * expose events to all children that don't have their own #GdkWindows.
2245 * This function provides a convenient way of doing this. A container,
2246 * when it receives an expose event, calls gtk_container_propagate_expose()
2247 * once for each child, passing in the event the container received.
2249 * gtk_container_propagate_expose() takes care of deciding whether
2250 * an expose event needs to be sent to the child, intersecting
2251 * the event's area with the child area, and sending the event.
2253 * In most cases, a container can simply either simply inherit the
2254 * ::expose implementation from #GtkContainer, or, do some drawing
2255 * and then chain to the ::expose implementation from #GtkContainer.
2258 gtk_container_propagate_expose (GtkContainer *container,
2260 GdkEventExpose *event)
2262 GdkEventExpose child_event;
2264 g_return_if_fail (GTK_IS_CONTAINER (container));
2265 g_return_if_fail (GTK_IS_WIDGET (child));
2266 g_return_if_fail (event != NULL);
2268 g_assert (child->parent == GTK_WIDGET (container));
2270 if (GTK_WIDGET_DRAWABLE (child) &&
2271 GTK_WIDGET_NO_WINDOW (child) &&
2272 (child->window == event->window))
2274 child_event = *event;
2276 child_event.region = gtk_widget_region_intersect (child, event->region);
2277 if (!gdk_region_empty (child_event.region))
2279 gdk_region_get_clipbox (child_event.region, &child_event.area);
2280 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2282 gdk_region_destroy (child_event.region);