1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcontainer.h"
32 #include "gtkprivate.h"
33 #include "gtksignal.h"
35 #include "gtkwindow.h"
54 typedef struct _GtkChildArgInfo GtkChildArgInfo;
55 struct _GtkChildArgInfo
65 static void gtk_container_base_class_init (GtkContainerClass *klass);
66 static void gtk_container_class_init (GtkContainerClass *klass);
67 static void gtk_container_init (GtkContainer *container);
68 static void gtk_container_destroy (GtkObject *object);
69 static void gtk_container_set_property (GObject *object,
73 static void gtk_container_get_property (GObject *object,
77 static void gtk_container_add_unimplemented (GtkContainer *container,
79 static void gtk_container_remove_unimplemented (GtkContainer *container,
81 static void gtk_container_real_check_resize (GtkContainer *container);
82 static gboolean gtk_container_real_focus (GtkContainer *container,
83 GtkDirectionType direction);
84 static void gtk_container_real_set_focus_child (GtkContainer *container,
86 static gboolean gtk_container_focus_tab (GtkContainer *container,
88 GtkDirectionType direction);
89 static gboolean gtk_container_focus_up_down (GtkContainer *container,
91 GtkDirectionType direction);
92 static gboolean gtk_container_focus_left_right (GtkContainer *container,
94 GtkDirectionType direction);
95 static gboolean gtk_container_focus_move (GtkContainer *container,
97 GtkDirectionType direction);
98 static void gtk_container_children_callback (GtkWidget *widget,
99 gpointer client_data);
100 static void gtk_container_show_all (GtkWidget *widget);
101 static void gtk_container_hide_all (GtkWidget *widget);
102 static gint gtk_container_expose (GtkWidget *widget,
103 GdkEventExpose *event);
106 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
111 static guint container_signals[LAST_SIGNAL] = { 0 };
112 static GHashTable *container_child_arg_info_ht = NULL;
114 static GtkWidgetClass *parent_class = NULL;
116 static const gchar *vadjustment_key = "gtk-vadjustment";
117 static guint vadjustment_key_id = 0;
118 static const gchar *hadjustment_key = "gtk-hadjustment";
119 static guint hadjustment_key_id = 0;
120 static GSList *container_resize_queue = NULL;
123 gtk_container_get_type (void)
125 static GtkType container_type = 0;
129 static const GtkTypeInfo container_info =
132 sizeof (GtkContainer),
133 sizeof (GtkContainerClass),
134 (GtkClassInitFunc) gtk_container_class_init,
135 (GtkObjectInitFunc) gtk_container_init,
136 /* reserved_1 */ NULL,
137 /* reserved_2 */ NULL,
138 (GtkClassInitFunc) gtk_container_base_class_init,
141 container_type = gtk_type_unique (gtk_widget_get_type (), &container_info);
144 return container_type;
148 gtk_container_base_class_init (GtkContainerClass *class)
150 /* reset instance specifc class fields that don't get inherited */
151 class->n_child_args = 0;
152 class->set_child_arg = NULL;
153 class->get_child_arg = NULL;
157 gtk_container_class_init (GtkContainerClass *class)
159 GObjectClass *gobject_class;
160 GtkObjectClass *object_class;
161 GtkWidgetClass *widget_class;
163 gobject_class = G_OBJECT_CLASS (class);
164 object_class = (GtkObjectClass*) class;
165 widget_class = (GtkWidgetClass*) class;
167 parent_class = gtk_type_class (gtk_widget_get_type ());
169 container_child_arg_info_ht = g_hash_table_new (gtk_arg_info_hash,
172 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
173 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
175 gobject_class->set_property = gtk_container_set_property;
176 gobject_class->get_property = gtk_container_get_property;
178 g_object_class_install_property (gobject_class,
180 g_param_spec_enum ("resize_mode",
182 _("Specify how resize events are handled"),
183 GTK_TYPE_RESIZE_MODE,
186 g_object_class_install_property (gobject_class,
188 g_param_spec_uint ("border_width",
190 _("The width of the empty border outside the containers children."),
195 g_object_class_install_property (gobject_class,
197 g_param_spec_object ("child",
199 _("Can be used to add a new child to the container."),
203 object_class->destroy = gtk_container_destroy;
205 widget_class->show_all = gtk_container_show_all;
206 widget_class->hide_all = gtk_container_hide_all;
207 widget_class->expose_event = gtk_container_expose;
209 class->add = gtk_container_add_unimplemented;
210 class->remove = gtk_container_remove_unimplemented;
211 class->check_resize = gtk_container_real_check_resize;
212 class->forall = NULL;
213 class->focus = gtk_container_real_focus;
214 class->set_focus_child = gtk_container_real_set_focus_child;
215 class->child_type = NULL;
216 class->composite_name = gtk_container_child_default_composite_name;
218 container_signals[ADD] =
219 gtk_signal_new ("add",
221 GTK_CLASS_TYPE (object_class),
222 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
223 gtk_marshal_VOID__OBJECT,
226 container_signals[REMOVE] =
227 gtk_signal_new ("remove",
229 GTK_CLASS_TYPE (object_class),
230 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
231 gtk_marshal_VOID__OBJECT,
234 container_signals[CHECK_RESIZE] =
235 gtk_signal_new ("check_resize",
237 GTK_CLASS_TYPE (object_class),
238 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
239 gtk_marshal_VOID__VOID,
241 container_signals[FOCUS] =
242 gtk_signal_new ("focus",
244 GTK_CLASS_TYPE (object_class),
245 GTK_SIGNAL_OFFSET (GtkContainerClass, focus),
246 gtk_marshal_ENUM__ENUM,
247 GTK_TYPE_DIRECTION_TYPE, 1,
248 GTK_TYPE_DIRECTION_TYPE);
249 container_signals[SET_FOCUS_CHILD] =
250 gtk_signal_new ("set-focus-child",
252 GTK_CLASS_TYPE (object_class),
253 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
254 gtk_marshal_VOID__OBJECT,
260 gtk_container_child_type (GtkContainer *container)
263 GtkContainerClass *class;
265 g_return_val_if_fail (container != NULL, 0);
266 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
268 class = GTK_CONTAINER_GET_CLASS (container);
269 if (class->child_type)
270 slot = class->child_type (container);
272 slot = GTK_TYPE_NONE;
277 /****************************************************
278 * GtkContainer child argument mechanism
280 ****************************************************/
283 gtk_container_add_with_args (GtkContainer *container,
285 const gchar *first_arg_name,
288 g_return_if_fail (container != NULL);
289 g_return_if_fail (GTK_IS_CONTAINER (container));
290 g_return_if_fail (widget != NULL);
291 g_return_if_fail (GTK_IS_WIDGET (widget));
292 g_return_if_fail (widget->parent == NULL);
294 gtk_widget_ref (GTK_WIDGET (container));
295 gtk_widget_ref (widget);
297 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
302 GSList *arg_list = NULL;
303 GSList *info_list = NULL;
306 va_start (var_args, first_arg_name);
307 error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
316 g_warning ("gtk_container_add_with_args(): %s", error);
324 slist_arg = arg_list;
325 slist_info = info_list;
328 gtk_container_arg_set (container, widget, slist_arg->data, slist_info->data);
329 slist_arg = slist_arg->next;
330 slist_info = slist_info->next;
332 gtk_args_collect_cleanup (arg_list, info_list);
336 gtk_widget_unref (widget);
337 gtk_widget_unref (GTK_WIDGET (container));
341 gtk_container_addv (GtkContainer *container,
346 g_return_if_fail (container != NULL);
347 g_return_if_fail (GTK_IS_CONTAINER (container));
348 g_return_if_fail (widget != NULL);
349 g_return_if_fail (GTK_IS_WIDGET (widget));
350 g_return_if_fail (widget->parent == NULL);
352 gtk_widget_ref (GTK_WIDGET (container));
353 gtk_widget_ref (widget);
355 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
361 for (max_args = args + n_args; args < max_args; args++)
362 gtk_container_arg_set (container, widget, args, NULL);
365 gtk_widget_unref (widget);
366 gtk_widget_unref (GTK_WIDGET (container));
370 gtk_container_child_setv (GtkContainer *container,
377 g_return_if_fail (container != NULL);
378 g_return_if_fail (GTK_IS_CONTAINER (container));
379 g_return_if_fail (child != NULL);
380 g_return_if_fail (GTK_IS_WIDGET (child));
381 g_return_if_fail (child->parent != NULL);
383 g_return_if_fail (args != NULL);
385 for (max_args = args + n_args; args < max_args; args++)
386 gtk_container_arg_set (container, child, args, NULL);
390 gtk_container_child_getv (GtkContainer *container,
397 g_return_if_fail (container != NULL);
398 g_return_if_fail (GTK_IS_CONTAINER (container));
399 g_return_if_fail (child != NULL);
400 g_return_if_fail (GTK_IS_WIDGET (child));
401 g_return_if_fail (child->parent != NULL);
403 g_return_if_fail (args != NULL);
405 for (max_args = args + n_args; args < max_args; args++)
406 gtk_container_arg_get (container, child, args, NULL);
410 gtk_container_child_set (GtkContainer *container,
412 const gchar *first_arg_name,
416 GSList *arg_list = NULL;
417 GSList *info_list = NULL;
420 g_return_if_fail (container != NULL);
421 g_return_if_fail (GTK_IS_CONTAINER (container));
422 g_return_if_fail (child != NULL);
423 g_return_if_fail (GTK_IS_WIDGET (child));
424 g_return_if_fail (child->parent != NULL);
426 va_start (var_args, first_arg_name);
427 error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
436 g_warning ("gtk_container_child_set(): %s", error);
444 slist_arg = arg_list;
445 slist_info = info_list;
448 gtk_container_arg_set (container, child, slist_arg->data, slist_info->data);
449 slist_arg = slist_arg->next;
450 slist_info = slist_info->next;
452 gtk_args_collect_cleanup (arg_list, info_list);
457 gtk_container_arg_set (GtkContainer *container,
462 GtkContainerClass *class;
464 g_return_if_fail (container != NULL);
465 g_return_if_fail (GTK_IS_CONTAINER (container));
466 g_return_if_fail (child != NULL);
467 g_return_if_fail (GTK_IS_WIDGET (child));
468 g_return_if_fail (arg != NULL);
474 error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
475 container_child_arg_info_ht,
480 g_warning ("gtk_container_arg_set(): %s", error);
485 g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
487 if (! (info->arg_flags & GTK_ARG_WRITABLE))
489 g_warning ("gtk_container_arg_set(): argument \"%s\" is not writable",
493 if (info->type != arg->type)
495 g_warning ("gtk_container_arg_set(): argument \"%s\" has invalid type `%s'",
497 gtk_type_name (arg->type));
501 class = gtk_type_class (info->class_type);
502 g_assert (class->set_child_arg != NULL);
503 class->set_child_arg (container, child, arg, info->arg_id);
507 gtk_container_arg_get (GtkContainer *container,
512 GtkContainerClass *class;
514 g_return_if_fail (container != NULL);
515 g_return_if_fail (GTK_IS_CONTAINER (container));
516 g_return_if_fail (child != NULL);
517 g_return_if_fail (GTK_IS_WIDGET (child));
518 g_return_if_fail (arg != NULL);
524 error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
525 container_child_arg_info_ht,
530 g_warning ("gtk_container_arg_get(): %s", error);
532 arg->type = GTK_TYPE_INVALID;
536 g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
538 if (! (info->arg_flags & GTK_ARG_READABLE))
540 g_warning ("gtk_container_arg_get(): argument \"%s\" is not readable",
542 arg->type = GTK_TYPE_INVALID;
546 class = gtk_type_class (info->class_type);
547 g_assert (class->get_child_arg != NULL);
548 arg->type = info->type;
549 class->get_child_arg (container, child, arg, info->arg_id);
553 gtk_container_add_child_arg_type (const gchar *arg_name,
558 g_return_if_fail (arg_name != NULL);
559 g_return_if_fail (arg_type > GTK_TYPE_NONE);
560 g_return_if_fail (arg_id > 0);
561 g_return_if_fail ((arg_flags & GTK_ARG_READWRITE) == GTK_ARG_READWRITE);
562 /* g_return_if_fail ((arg_flags & GTK_ARG_CHILD_ARG) != 0); */
564 arg_flags |= GTK_ARG_CHILD_ARG;
565 arg_flags &= GTK_ARG_MASK;
567 gtk_arg_type_new_static (GTK_TYPE_CONTAINER,
569 GTK_STRUCT_OFFSET (GtkContainerClass, n_child_args),
570 container_child_arg_info_ht,
577 gtk_container_child_args_collect (GtkType object_type,
579 GSList **info_list_p,
580 const gchar *first_arg_name,
583 return gtk_args_collect (object_type,
584 container_child_arg_info_ht,
592 gtk_container_child_arg_get_info (GtkType object_type,
593 const gchar *arg_name,
596 return gtk_arg_get_info (object_type,
597 container_child_arg_info_ht,
603 gtk_container_query_child_args (GtkType class_type,
607 g_return_val_if_fail (n_args != NULL, NULL);
609 g_return_val_if_fail (gtk_type_is_a (class_type, GTK_TYPE_CONTAINER), NULL);
611 return gtk_args_query (class_type, container_child_arg_info_ht, arg_flags, n_args);
616 gtk_container_add_unimplemented (GtkContainer *container,
619 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
623 gtk_container_remove_unimplemented (GtkContainer *container,
626 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
630 gtk_container_init (GtkContainer *container)
632 container->focus_child = NULL;
633 container->border_width = 0;
634 container->need_resize = FALSE;
635 container->resize_mode = GTK_RESIZE_PARENT;
636 container->reallocate_redraws = FALSE;
637 container->resize_widgets = NULL;
641 gtk_container_destroy (GtkObject *object)
643 GtkContainer *container;
645 g_return_if_fail (object != NULL);
646 g_return_if_fail (GTK_IS_CONTAINER (object));
648 container = GTK_CONTAINER (object);
650 if (GTK_CONTAINER_RESIZE_PENDING (container))
651 gtk_container_dequeue_resize_handler (container);
652 if (container->resize_widgets)
653 gtk_container_clear_resize_widgets (container);
655 /* do this before walking child widgets, to avoid
656 * removing children from focus chain one by one.
658 if (container->has_focus_chain)
659 gtk_container_unset_focus_chain (container);
661 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
663 if (GTK_OBJECT_CLASS (parent_class)->destroy)
664 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
668 gtk_container_set_property (GObject *object,
673 GtkContainer *container;
675 container = GTK_CONTAINER (object);
679 case PROP_BORDER_WIDTH:
680 gtk_container_set_border_width (container, g_value_get_uint (value));
682 case PROP_RESIZE_MODE:
683 gtk_container_set_resize_mode (container, g_value_get_enum (value));
686 gtk_container_add (container, GTK_WIDGET (g_value_get_object (value)));
689 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
695 gtk_container_get_property (GObject *object,
700 GtkContainer *container;
702 container = GTK_CONTAINER (object);
706 case PROP_BORDER_WIDTH:
707 g_value_set_uint (value, container->border_width);
709 case PROP_RESIZE_MODE:
710 g_value_set_enum (value, container->resize_mode);
713 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
719 * gtk_container_set_border_width:
720 * @container: a #GtkContainer
721 * @border_width: amount of blank space to leave <emphasis>outside</emphasis> the container
723 * The border width of a container is the amount of space to leave
724 * around the outside of the container. The only exception to this is
725 * #GtkWindow; because toplevel windows can't leave space outside,
726 * they leave the space inside. The border is added on all sides of
727 * the container. To add space to only one side, one approach is to
728 * create a #GtkAlignment widget, call gtk_widget_set_usize() to give
729 * it a size, and place it on the side of the container as a spacer.
733 gtk_container_set_border_width (GtkContainer *container,
736 g_return_if_fail (container != NULL);
737 g_return_if_fail (GTK_IS_CONTAINER (container));
739 if (container->border_width != border_width)
741 container->border_width = border_width;
742 g_object_notify (G_OBJECT (container), "border_width");
744 if (GTK_WIDGET_REALIZED (container))
745 gtk_widget_queue_resize (GTK_WIDGET (container));
751 * @container: a #GtkContainer
752 * @widget: a widget to be placed inside @container
754 * Adds @widget to @container. Typically used for simple containers
755 * such as #GtkWindow, #GtkFrame, or #GtkButton; for more complicated
756 * layout containers such as #GtkBox or #GtkTable, this function will
757 * pick default packing parameters that may not be correct. So
758 * consider functions such as gtk_box_pack_start() and
759 * gtk_table_attach() as an alternative to gtk_container_add() in
760 * those cases. A widget may be added to only one container at a time;
761 * you can't place the same widget inside two different containers.
764 gtk_container_add (GtkContainer *container,
767 g_return_if_fail (container != NULL);
768 g_return_if_fail (GTK_IS_CONTAINER (container));
769 g_return_if_fail (widget != NULL);
770 g_return_if_fail (GTK_IS_WIDGET (widget));
772 if (widget->parent != NULL)
774 g_warning ("Attempting to add a widget with type %s to a container of "
775 "type %s, but the widget is already inside a container of type %s",
776 g_type_name (G_OBJECT_TYPE (widget)),
777 g_type_name (G_OBJECT_TYPE (container)),
778 g_type_name (G_OBJECT_TYPE (widget->parent)));
782 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
786 * gtk_container_remove:
787 * @container: a #GtkContainer
788 * @widget: a current child of @container
790 * Removes @widget from @container. @widget must be inside @container.
791 * Note that @container will own a reference to @widget, and that this
792 * may be the last reference held; so removing a widget from its
793 * container can destroy that widget. If you want to use @widget
794 * again, you need to add a reference to it while it's not inside
795 * a container, using g_object_ref().
798 gtk_container_remove (GtkContainer *container,
801 g_return_if_fail (container != NULL);
802 g_return_if_fail (GTK_IS_CONTAINER (container));
803 g_return_if_fail (widget != NULL);
804 g_return_if_fail (GTK_IS_WIDGET (widget));
805 g_return_if_fail (widget->parent == GTK_WIDGET (container));
807 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
811 gtk_container_dequeue_resize_handler (GtkContainer *container)
813 g_return_if_fail (GTK_IS_CONTAINER (container));
814 g_return_if_fail (GTK_CONTAINER_RESIZE_PENDING (container));
816 container_resize_queue = g_slist_remove (container_resize_queue, container);
817 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
821 gtk_container_clear_resize_widgets (GtkContainer *container)
825 g_return_if_fail (container != NULL);
826 g_return_if_fail (GTK_IS_CONTAINER (container));
828 node = container->resize_widgets;
832 GtkWidget *widget = node->data;
834 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
838 g_slist_free (container->resize_widgets);
839 container->resize_widgets = NULL;
843 gtk_container_set_resize_mode (GtkContainer *container,
844 GtkResizeMode resize_mode)
846 g_return_if_fail (container != NULL);
847 g_return_if_fail (GTK_IS_CONTAINER (container));
848 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
850 if (GTK_WIDGET_TOPLEVEL (container) &&
851 resize_mode == GTK_RESIZE_PARENT)
853 resize_mode = GTK_RESIZE_QUEUE;
854 g_object_notify (G_OBJECT (container), "resize_mode");
857 if (container->resize_mode != resize_mode)
859 container->resize_mode = resize_mode;
861 if (resize_mode == GTK_RESIZE_IMMEDIATE)
862 gtk_container_check_resize (container);
865 gtk_container_clear_resize_widgets (container);
866 gtk_widget_queue_resize (GTK_WIDGET (container));
868 g_object_notify (G_OBJECT (container), "resize_mode");
873 gtk_container_set_reallocate_redraws (GtkContainer *container,
874 gboolean needs_redraws)
876 g_return_if_fail (GTK_IS_CONTAINER (container));
878 container->reallocate_redraws = needs_redraws ? TRUE : FALSE;
882 gtk_container_get_resize_container (GtkContainer *container)
886 widget = GTK_WIDGET (container);
888 while (widget->parent)
890 widget = widget->parent;
891 if (GTK_IS_RESIZE_CONTAINER (widget) && !GTK_WIDGET_RESIZE_NEEDED (widget))
895 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
899 gtk_container_idle_sizer (gpointer data)
901 GDK_THREADS_ENTER ();
903 /* we may be invoked with a container_resize_queue of NULL, because
904 * queue_resize could have been adding an extra idle function while
905 * the queue still got processed. we better just ignore such case
906 * than trying to explicitely work around them with some extra flags,
907 * since it doesn't cause any actual harm.
909 while (container_resize_queue)
914 slist = container_resize_queue;
915 container_resize_queue = slist->next;
916 widget = slist->data;
917 g_slist_free_1 (slist);
919 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
920 gtk_container_check_resize (GTK_CONTAINER (widget));
923 gdk_window_process_all_updates ();
925 GDK_THREADS_LEAVE ();
931 gtk_container_queue_resize (GtkContainer *container)
933 GtkContainer *resize_container;
935 g_return_if_fail (container != NULL);
936 g_return_if_fail (GTK_IS_CONTAINER (container));
938 /* clear resize widgets for resize containers
939 * before aborting prematurely. this is especially
940 * important for toplevels which may need imemdiate
941 * processing or their resize handler to be queued.
943 if (GTK_IS_RESIZE_CONTAINER (container))
944 gtk_container_clear_resize_widgets (container);
945 if (GTK_OBJECT_DESTROYED (container) ||
946 GTK_WIDGET_RESIZE_NEEDED (container))
949 resize_container = gtk_container_get_resize_container (container);
951 if (resize_container)
953 if (GTK_WIDGET_VISIBLE (resize_container) &&
954 (GTK_WIDGET_TOPLEVEL (resize_container) || GTK_WIDGET_DRAWABLE (resize_container)))
956 switch (resize_container->resize_mode)
958 case GTK_RESIZE_QUEUE:
959 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
961 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
962 if (container_resize_queue == NULL)
963 gtk_idle_add_priority (GTK_PRIORITY_RESIZE,
964 gtk_container_idle_sizer,
966 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
969 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
970 resize_container->resize_widgets =
971 g_slist_prepend (resize_container->resize_widgets, container);
974 case GTK_RESIZE_IMMEDIATE:
975 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
976 resize_container->resize_widgets =
977 g_slist_prepend (resize_container->resize_widgets, container);
978 gtk_container_check_resize (resize_container);
981 case GTK_RESIZE_PARENT:
982 /* Ignore, should not be reached */
988 /* we need to let hidden resize containers know that something
989 * changed while they where hidden (currently only evaluated by
992 resize_container->need_resize = TRUE;
998 gtk_container_check_resize (GtkContainer *container)
1000 g_return_if_fail (container != NULL);
1001 g_return_if_fail (GTK_IS_CONTAINER (container));
1003 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
1007 gtk_container_real_check_resize (GtkContainer *container)
1010 GtkRequisition requisition;
1012 g_return_if_fail (container != NULL);
1013 g_return_if_fail (GTK_IS_CONTAINER (container));
1015 widget = GTK_WIDGET (container);
1017 gtk_widget_size_request (widget, &requisition);
1019 if (requisition.width > widget->allocation.width ||
1020 requisition.height > widget->allocation.height)
1022 if (GTK_IS_RESIZE_CONTAINER (container))
1023 gtk_widget_size_allocate (GTK_WIDGET (container),
1024 >K_WIDGET (container)->allocation);
1026 gtk_widget_queue_resize (widget);
1030 gtk_container_resize_children (container);
1034 /* The container hasn't changed size but one of its children
1035 * queued a resize request. Which means that the allocation
1036 * is not sufficient for the requisition of some child.
1037 * We've already performed a size request at this point,
1038 * so we simply need to run through the list of resize
1039 * widgets and reallocate their sizes appropriately. We
1040 * make the optimization of not performing reallocation
1041 * for a widget who also has a parent in the resize widgets
1042 * list. GTK_RESIZE_NEEDED is used for flagging those
1043 * parents inside this function.
1046 gtk_container_resize_children (GtkContainer *container)
1049 GtkWidget *resize_container;
1050 GSList *resize_widgets;
1051 GSList *resize_containers;
1054 /* resizing invariants:
1055 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
1056 * containers with resize_mode==GTK_RESIZE_PARENT have to have resize_widgets
1058 * containers that are flagged RESIZE_NEEDED must have resize_widgets set to
1059 * NULL, or are toplevels (thus have ->parent set to NULL).
1060 * widgets that are in some container->resize_widgets list must be flagged with
1062 * widgets that have RESIZE_NEEDED set must be referenced in some
1063 * GTK_IS_RESIZE_CONTAINER (container)->resize_widgets list.
1064 * containers that have an idle sizer pending must be flagged with
1068 g_return_if_fail (container != NULL);
1069 g_return_if_fail (GTK_IS_CONTAINER (container));
1071 /* we first check out if we actually need to perform a resize,
1072 * which is not the case if we got another container queued for
1073 * a resize in our ancestry. also we can skip the whole
1074 * resize_widgets checks if we are a toplevel and NEED_RESIZE.
1075 * this code assumes that our allocation is sufficient for our
1076 * requisition, since otherwise we would NEED_RESIZE.
1078 resize_container = GTK_WIDGET (container);
1079 while (resize_container)
1081 if (GTK_WIDGET_RESIZE_NEEDED (resize_container))
1083 resize_container = resize_container->parent;
1085 if (resize_container)
1087 /* queue_resize and size_allocate both clear our
1088 * resize_widgets list.
1090 if (resize_container->parent)
1091 gtk_container_queue_resize (container);
1093 gtk_widget_size_allocate (GTK_WIDGET (container),
1094 >K_WIDGET (container)->allocation);
1098 resize_container = GTK_WIDGET (container);
1100 /* we now walk the ancestry for all resize widgets as long
1101 * as they are our children and as long as their allocation
1102 * is insufficient, since we don't need to reallocate below that.
1104 resize_widgets = container->resize_widgets;
1105 container->resize_widgets = NULL;
1106 for (node = resize_widgets; node; node = node->next)
1108 widget = node->data;
1110 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1112 while (widget->parent != resize_container &&
1113 ((widget->allocation.width < widget->requisition.width) ||
1114 (widget->allocation.height < widget->requisition.height)))
1115 widget = widget->parent;
1117 GTK_PRIVATE_SET_FLAG (widget, GTK_RESIZE_NEEDED);
1118 node->data = widget;
1121 /* for the newly setup resize_widgets list, we now walk each widget's
1122 * ancestry to sort those widgets out that have RESIZE_NEEDED parents.
1123 * we can safely stop the walk if we are the parent, since we checked
1124 * our own ancestry already.
1126 resize_containers = NULL;
1127 for (node = resize_widgets; node; node = node->next)
1131 widget = node->data;
1133 if (!GTK_WIDGET_RESIZE_NEEDED (widget))
1136 parent = widget->parent;
1138 while (parent != resize_container)
1140 if (GTK_WIDGET_RESIZE_NEEDED (parent))
1142 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1145 parent = parent->parent;
1148 if (!g_slist_find (resize_containers, widget))
1150 resize_containers = g_slist_prepend (resize_containers, widget);
1151 gtk_widget_ref (widget);
1154 g_slist_free (resize_widgets);
1156 for (node = resize_containers; node; node = node->next)
1158 widget = node->data;
1160 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1162 gtk_widget_size_allocate (widget, &widget->allocation);
1164 gtk_widget_unref (widget);
1166 g_slist_free (resize_containers);
1170 * gtk_container_forall:
1171 * @container: a #GtkContainer
1172 * @callback: a callback
1173 * @callback_data: callback user data
1175 * Invokes @callback on each child of @container, including children
1176 * that are considered "internal" (implementation details of the
1177 * container). "Internal" children generally weren't added by the user
1178 * of the container, but were added by the container implementation
1179 * itself. Most applications should use gtk_container_foreach(),
1180 * rather than gtk_container_forall().
1183 gtk_container_forall (GtkContainer *container,
1184 GtkCallback callback,
1185 gpointer callback_data)
1187 GtkContainerClass *class;
1189 g_return_if_fail (container != NULL);
1190 g_return_if_fail (GTK_IS_CONTAINER (container));
1191 g_return_if_fail (callback != NULL);
1193 class = GTK_CONTAINER_GET_CLASS (container);
1196 class->forall (container, TRUE, callback, callback_data);
1200 * gtk_container_foreach:
1201 * @container: a #GtkContainer
1202 * @callback: a callback
1203 * @callback_data: callback user data
1205 * Invokes @callback on each non-internal child of @container. See
1206 * gtk_container_forall() for details on what constitutes an
1207 * "internal" child. Most applications should use
1208 * gtk_container_foreach(), rather than gtk_container_forall().
1211 gtk_container_foreach (GtkContainer *container,
1212 GtkCallback callback,
1213 gpointer callback_data)
1215 GtkContainerClass *class;
1217 g_return_if_fail (container != NULL);
1218 g_return_if_fail (GTK_IS_CONTAINER (container));
1219 g_return_if_fail (callback != NULL);
1221 class = GTK_CONTAINER_GET_CLASS (container);
1224 class->forall (container, FALSE, callback, callback_data);
1227 typedef struct _GtkForeachData GtkForeachData;
1228 struct _GtkForeachData
1230 GtkObject *container;
1231 GtkCallbackMarshal callback;
1232 gpointer callback_data;
1236 gtk_container_foreach_unmarshal (GtkWidget *child,
1239 GtkForeachData *fdata = (GtkForeachData*) data;
1242 /* first argument */
1243 args[0].name = NULL;
1244 args[0].type = GTK_OBJECT_TYPE (child);
1245 GTK_VALUE_OBJECT (args[0]) = GTK_OBJECT (child);
1247 /* location for return value */
1248 args[1].name = NULL;
1249 args[1].type = GTK_TYPE_NONE;
1251 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1255 gtk_container_foreach_full (GtkContainer *container,
1256 GtkCallback callback,
1257 GtkCallbackMarshal marshal,
1258 gpointer callback_data,
1259 GtkDestroyNotify notify)
1261 g_return_if_fail (container != NULL);
1262 g_return_if_fail (GTK_IS_CONTAINER (container));
1266 GtkForeachData fdata;
1268 fdata.container = GTK_OBJECT (container);
1269 fdata.callback = marshal;
1270 fdata.callback_data = callback_data;
1272 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1276 g_return_if_fail (callback != NULL);
1278 gtk_container_foreach (container, callback, &callback_data);
1282 notify (callback_data);
1286 gtk_container_focus (GtkContainer *container,
1287 GtkDirectionType direction)
1291 g_return_val_if_fail (container != NULL, FALSE);
1292 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1294 gtk_signal_emit (GTK_OBJECT (container),
1295 container_signals[FOCUS],
1296 direction, &return_val);
1302 gtk_container_set_focus_child (GtkContainer *container,
1305 g_return_if_fail (container != NULL);
1306 g_return_if_fail (GTK_IS_CONTAINER (container));
1308 g_return_if_fail (GTK_IS_WIDGET (widget));
1310 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1314 gtk_container_children (GtkContainer *container)
1320 gtk_container_foreach (container,
1321 gtk_container_children_callback,
1324 return g_list_reverse (children);
1328 gtk_container_child_position_callback (GtkWidget *widget,
1329 gpointer client_data)
1335 } *data = client_data;
1338 if (data->child == widget)
1339 data->index = data->i;
1343 gtk_container_child_default_composite_name (GtkContainer *container,
1353 /* fallback implementation */
1357 gtk_container_forall (container,
1358 gtk_container_child_position_callback,
1361 name = g_strdup_printf ("%s-%u",
1362 gtk_type_name (GTK_OBJECT_TYPE (child)),
1369 gtk_container_child_composite_name (GtkContainer *container,
1372 g_return_val_if_fail (container != NULL, NULL);
1373 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1374 g_return_val_if_fail (child != NULL, NULL);
1375 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1376 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1378 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1380 static GQuark quark_composite_name = 0;
1383 if (!quark_composite_name)
1384 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1386 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1389 GtkContainerClass *class;
1391 class = GTK_CONTAINER_GET_CLASS (container);
1392 if (class->composite_name)
1393 name = class->composite_name (container, child);
1396 name = g_strdup (name);
1405 gtk_container_real_set_focus_child (GtkContainer *container,
1408 g_return_if_fail (container != NULL);
1409 g_return_if_fail (GTK_IS_CONTAINER (container));
1411 g_return_if_fail (GTK_IS_WIDGET (child));
1413 if (child != container->focus_child)
1415 if (container->focus_child)
1416 gtk_widget_unref (container->focus_child);
1417 container->focus_child = child;
1418 if (container->focus_child)
1419 gtk_widget_ref (container->focus_child);
1423 /* check for h/v adjustments
1425 if (container->focus_child)
1427 GtkAdjustment *adjustment;
1429 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1431 gtk_adjustment_clamp_page (adjustment,
1432 container->focus_child->allocation.y,
1433 (container->focus_child->allocation.y +
1434 container->focus_child->allocation.height));
1436 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1438 gtk_adjustment_clamp_page (adjustment,
1439 container->focus_child->allocation.x,
1440 (container->focus_child->allocation.x +
1441 container->focus_child->allocation.width));
1446 get_focus_chain (GtkContainer *container)
1450 chain = g_object_get_data (G_OBJECT (container), "gtk-container-focus-chain");
1456 filter_unfocusable (GtkContainer *container,
1465 if (GTK_WIDGET_IS_SENSITIVE (tmp_list->data) &&
1466 GTK_WIDGET_DRAWABLE (tmp_list->data) &&
1467 (GTK_IS_CONTAINER (tmp_list->data) || GTK_WIDGET_CAN_FOCUS (tmp_list->data)))
1468 tmp_list = tmp_list->next;
1471 tmp_list2 = tmp_list;
1472 tmp_list = tmp_list->next;
1474 list = g_list_remove_link (list, tmp_list2);
1475 g_list_free_1 (tmp_list2);
1483 gtk_container_real_focus (GtkContainer *container,
1484 GtkDirectionType direction)
1489 g_return_val_if_fail (container != NULL, FALSE);
1490 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1492 /* Fail if the container is inappropriate for focus movement
1494 if (!GTK_WIDGET_DRAWABLE (container) ||
1495 !GTK_WIDGET_IS_SENSITIVE (container))
1500 if (GTK_WIDGET_CAN_FOCUS (container))
1502 if (!GTK_WIDGET_HAS_FOCUS (container))
1504 gtk_widget_grab_focus (GTK_WIDGET (container));
1510 /* Get a list of the containers children, allowing focus
1511 * chain to override.
1513 if (container->has_focus_chain)
1517 chain = get_focus_chain (container);
1519 children = g_list_copy (chain);
1524 gtk_container_forall (container,
1525 gtk_container_children_callback,
1527 children = g_list_reverse (children);
1532 /* Remove any children which are inappropriate for focus movement
1534 children = filter_unfocusable (container, children);
1538 case GTK_DIR_TAB_FORWARD:
1539 case GTK_DIR_TAB_BACKWARD:
1540 if (container->has_focus_chain)
1542 if (direction == GTK_DIR_TAB_BACKWARD)
1543 children = g_list_reverse (children);
1544 return_val = gtk_container_focus_move (container, children, direction);
1547 return_val = gtk_container_focus_tab (container, children, direction);
1551 return_val = gtk_container_focus_up_down (container, &children, direction);
1555 return_val = gtk_container_focus_left_right (container, &children, direction);
1559 g_list_free (children);
1567 gtk_container_focus_tab (GtkContainer *container,
1569 GtkDirectionType direction)
1577 length = g_list_length (children);
1579 /* sort the children in the y direction */
1580 for (i = 1; i < length; i++)
1583 tmp_list = g_list_nth (children, j);
1584 child = tmp_list->data;
1588 child2 = tmp_list->prev->data;
1589 if (child->allocation.y < child2->allocation.y)
1591 tmp_list->data = tmp_list->prev->data;
1592 tmp_list = tmp_list->prev;
1599 tmp_list->data = child;
1602 /* sort the children in the x direction while
1603 * maintaining the y direction sort.
1605 for (i = 1; i < length; i++)
1608 tmp_list = g_list_nth (children, j);
1609 child = tmp_list->data;
1613 child2 = tmp_list->prev->data;
1614 if ((child->allocation.x < child2->allocation.x) &&
1615 (child->allocation.y == child2->allocation.y))
1617 tmp_list->data = tmp_list->prev->data;
1618 tmp_list = tmp_list->prev;
1625 tmp_list->data = child;
1628 /* if we are going backwards then reverse the order
1631 if (direction == GTK_DIR_TAB_BACKWARD)
1632 children = g_list_reverse (children);
1634 return gtk_container_focus_move (container, children, direction);
1638 old_focus_coords (GtkContainer *container, GdkRectangle *old_focus_rect)
1640 GtkWidget *widget = GTK_WIDGET (container);
1641 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1644 GTK_IS_WINDOW (toplevel) && GTK_WINDOW (toplevel)->focus_widget &&
1645 GTK_WIDGET_REALIZED (container) &&
1646 GTK_WIDGET_REALIZED (GTK_WINDOW (toplevel)->focus_widget))
1648 GtkWidget *old_focus = GTK_WINDOW (toplevel)->focus_widget;
1649 GdkWindow *old_parent_window = old_focus->parent ? old_focus->parent->window : old_focus->window;
1650 GdkWindow *new_parent_window = widget->window;
1651 GdkWindow *toplevel_window = toplevel->window;
1653 *old_focus_rect = old_focus->allocation;
1655 /* Translate coordinates to the toplevel */
1657 while (old_parent_window != toplevel_window)
1661 gdk_window_get_position (old_parent_window, &dx, &dy);
1663 old_focus_rect->x += dx;
1664 old_focus_rect->y += dy;
1666 old_parent_window = gdk_window_get_parent (old_parent_window);
1669 /* Translate coordinates back to the new container */
1671 while (new_parent_window != toplevel_window)
1675 gdk_window_get_position (new_parent_window, &dx, &dy);
1677 old_focus_rect->x -= dx;
1678 old_focus_rect->y -= dy;
1680 new_parent_window = gdk_window_get_parent (new_parent_window);
1689 typedef struct _CompareInfo CompareInfo;
1698 up_down_compare (gconstpointer a,
1702 const GtkWidget *child1 = a;
1703 const GtkWidget *child2 = b;
1704 CompareInfo *compare = data;
1706 gint y1 = child1->allocation.y + child1->allocation.height / 2;
1707 gint y2 = child2->allocation.y + child2->allocation.height / 2;
1711 gint x1 = abs (child1->allocation.x + child1->allocation.width / 2 - compare->x);
1712 gint x2 = abs (child2->allocation.x + child2->allocation.width / 2 - compare->x);
1714 if (compare->y < y1)
1715 return (x1 < x2) ? -1 : ((x1 == x2) ? 0 : 1);
1717 return (x1 < x2) ? 1 : ((x1 == x2) ? 0 : -1);
1720 return (y1 < y2) ? -1 : 1;
1724 gtk_container_focus_up_down (GtkContainer *container,
1726 GtkDirectionType direction)
1728 CompareInfo compare;
1731 if (container->focus_child)
1737 /* Delete widgets from list that don't match minimum criteria */
1739 compare_x1 = container->focus_child->allocation.x;
1740 compare_x2 = container->focus_child->allocation.x + container->focus_child->allocation.width;
1742 if (direction == GTK_DIR_UP)
1743 compare_y = container->focus_child->allocation.y;
1745 compare_y = container->focus_child->allocation.y + container->focus_child->allocation.height;
1747 tmp_list = *children;
1750 GtkWidget *child = tmp_list->data;
1751 GList *next = tmp_list->next;
1752 gint child_x1, child_x2;
1754 if (child != container->focus_child)
1756 child_x1 = child->allocation.x;
1757 child_x2 = child->allocation.x + child->allocation.width;
1759 if ((child_x2 <= compare_x1 || child_x1 >= compare_x2) /* No horizontal overlap */ ||
1760 (direction == GTK_DIR_DOWN && child->allocation.y + child->allocation.height < compare_y) || /* Not below */
1761 (direction == GTK_DIR_UP && child->allocation.y > compare_y)) /* Not above */
1763 *children = g_list_delete_link (*children, tmp_list);
1770 compare.x = (compare_x1 + compare_x2) / 2;
1771 compare.y = container->focus_child->allocation.y + container->focus_child->allocation.height / 2;
1775 /* No old focus widget, need to figure out starting x,y some other way
1777 GtkWidget *widget = GTK_WIDGET (container);
1778 GdkRectangle old_focus_rect;
1780 if (old_focus_coords (container, &old_focus_rect))
1782 compare.x = old_focus_rect.x + old_focus_rect.width / 2;
1786 if (GTK_WIDGET_NO_WINDOW (widget))
1787 compare.x = widget->allocation.x + widget->allocation.width / 2;
1789 compare.x = widget->allocation.width / 2;
1792 if (GTK_WIDGET_NO_WINDOW (widget))
1793 compare.y = (direction == GTK_DIR_DOWN) ? widget->allocation.y : widget->allocation.y + widget->allocation.height;
1795 compare.y = (direction == GTK_DIR_DOWN) ? 0 : + widget->allocation.height;
1798 *children = g_list_sort_with_data (*children, up_down_compare, &compare);
1800 if (direction == GTK_DIR_UP)
1801 *children = g_list_reverse (*children);
1803 return gtk_container_focus_move (container, *children, direction);
1807 left_right_compare (gconstpointer a,
1811 const GtkWidget *child1 = a;
1812 const GtkWidget *child2 = b;
1813 CompareInfo *compare = data;
1815 gint x1 = child1->allocation.x + child1->allocation.width / 2;
1816 gint x2 = child2->allocation.x + child2->allocation.width / 2;
1820 gint y1 = abs (child1->allocation.y + child1->allocation.height / 2 - compare->y);
1821 gint y2 = abs (child2->allocation.y + child2->allocation.height / 2 - compare->y);
1823 if (compare->x < x1)
1824 return (y1 < y2) ? -1 : ((y1 == y2) ? 0 : 1);
1826 return (y1 < y2) ? 1 : ((y1 == y2) ? 0 : -1);
1829 return (x1 < x2) ? -1 : 1;
1833 gtk_container_focus_left_right (GtkContainer *container,
1835 GtkDirectionType direction)
1837 CompareInfo compare;
1840 if (container->focus_child)
1846 /* Delete widgets from list that don't match minimum criteria */
1848 compare_y1 = container->focus_child->allocation.y;
1849 compare_y2 = container->focus_child->allocation.y + container->focus_child->allocation.height;
1851 if (direction == GTK_DIR_LEFT)
1852 compare_x = container->focus_child->allocation.x;
1854 compare_x = container->focus_child->allocation.x + container->focus_child->allocation.width;
1856 tmp_list = *children;
1859 GtkWidget *child = tmp_list->data;
1860 GList *next = tmp_list->next;
1861 gint child_y1, child_y2;
1863 if (child != container->focus_child)
1865 child_y1 = child->allocation.y;
1866 child_y2 = child->allocation.y + child->allocation.height;
1868 if ((child_y2 <= compare_y1 || child_y1 >= compare_y2) /* No vertical overlap */ ||
1869 (direction == GTK_DIR_RIGHT && child->allocation.x + child->allocation.width < compare_x) || /* Not to left */
1870 (direction == GTK_DIR_LEFT && child->allocation.x > compare_x)) /* Not to right */
1872 *children = g_list_delete_link (*children, tmp_list);
1879 compare.y = (compare_y1 + compare_y2) / 2;
1880 compare.x = container->focus_child->allocation.x + container->focus_child->allocation.width / 2;
1884 /* No old focus widget, need to figure out starting x,y some other way
1886 GtkWidget *widget = GTK_WIDGET (container);
1887 GdkRectangle old_focus_rect;
1889 if (old_focus_coords (container, &old_focus_rect))
1891 compare.y = old_focus_rect.y + old_focus_rect.height / 2;
1895 if (GTK_WIDGET_NO_WINDOW (widget))
1896 compare.y = widget->allocation.y + widget->allocation.height / 2;
1898 compare.y = widget->allocation.height / 2;
1901 if (GTK_WIDGET_NO_WINDOW (widget))
1902 compare.x = (direction == GTK_DIR_RIGHT) ? widget->allocation.x : widget->allocation.x + widget->allocation.width;
1904 compare.x = (direction == GTK_DIR_RIGHT) ? 0 : widget->allocation.width;
1907 *children = g_list_sort_with_data (*children, left_right_compare, &compare);
1909 if (direction == GTK_DIR_LEFT)
1910 *children = g_list_reverse (*children);
1912 return gtk_container_focus_move (container, *children, direction);
1916 gtk_container_focus_move (GtkContainer *container,
1918 GtkDirectionType direction)
1920 GtkWidget *focus_child;
1923 focus_child = container->focus_child;
1927 child = children->data;
1928 children = children->next;
1935 if (focus_child == child)
1939 if (GTK_WIDGET_DRAWABLE (child) &&
1940 GTK_IS_CONTAINER (child))
1941 if (gtk_container_focus (GTK_CONTAINER (child), direction))
1945 else if (GTK_WIDGET_DRAWABLE (child) &&
1946 gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
1948 if (GTK_IS_CONTAINER (child))
1950 if (gtk_container_focus (GTK_CONTAINER (child), direction))
1953 else if (GTK_WIDGET_CAN_FOCUS (child))
1955 gtk_widget_grab_focus (child);
1966 gtk_container_children_callback (GtkWidget *widget,
1967 gpointer client_data)
1971 children = (GList**) client_data;
1972 *children = g_list_prepend (*children, widget);
1977 #define g_signal_handlers_disconnect_by_func(obj, func, data) g_signal_handlers_disconnect_matched (obj, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, func, data)
1980 chain_widget_destroyed (GtkWidget *widget,
1983 GtkContainer *container;
1986 container = GTK_CONTAINER (user_data);
1988 chain = g_object_get_data (G_OBJECT (container),
1989 "gtk-container-focus-chain");
1991 chain = g_list_remove (chain, widget);
1993 g_signal_handlers_disconnect_by_func (G_OBJECT (widget),
1994 chain_widget_destroyed,
1997 g_object_set_data (G_OBJECT (container),
1998 "gtk-container-focus-chain",
2003 gtk_container_set_focus_chain (GtkContainer *container,
2004 GList *focusable_widgets)
2009 g_return_if_fail (GTK_IS_CONTAINER (container));
2011 if (container->has_focus_chain)
2012 gtk_container_unset_focus_chain (container);
2014 container->has_focus_chain = TRUE;
2017 tmp_list = focusable_widgets;
2018 while (tmp_list != NULL)
2020 g_return_if_fail (GTK_IS_WIDGET (tmp_list->data));
2022 /* In principle each widget in the chain should be a descendant
2023 * of the container, but we don't want to check that here, it's
2024 * expensive and also it's allowed to set the focus chain before
2025 * you pack the widgets, or have a widget in the chain that isn't
2026 * always packed. So we check for ancestor during actual traversal.
2029 chain = g_list_prepend (chain, tmp_list->data);
2031 gtk_signal_connect (GTK_OBJECT (tmp_list->data),
2033 GTK_SIGNAL_FUNC (chain_widget_destroyed),
2036 tmp_list = g_list_next (tmp_list);
2039 chain = g_list_reverse (chain);
2041 g_object_set_data (G_OBJECT (container),
2042 "gtk-container-focus-chain",
2047 gtk_container_unset_focus_chain (GtkContainer *container)
2049 g_return_if_fail (GTK_IS_CONTAINER (container));
2051 if (container->has_focus_chain)
2056 chain = get_focus_chain (container);
2058 container->has_focus_chain = FALSE;
2060 g_object_set_data (G_OBJECT (container), "gtk-container-focus-chain",
2064 while (tmp_list != NULL)
2066 g_signal_handlers_disconnect_by_func (G_OBJECT (tmp_list->data),
2067 chain_widget_destroyed,
2070 tmp_list = g_list_next (tmp_list);
2073 g_list_free (chain);
2078 gtk_container_set_focus_vadjustment (GtkContainer *container,
2079 GtkAdjustment *adjustment)
2081 g_return_if_fail (container != NULL);
2082 g_return_if_fail (GTK_IS_CONTAINER (container));
2084 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2087 gtk_object_ref (GTK_OBJECT(adjustment));
2089 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
2092 (GtkDestroyNotify) gtk_object_unref);
2096 gtk_container_set_focus_hadjustment (GtkContainer *container,
2097 GtkAdjustment *adjustment)
2099 g_return_if_fail (container != NULL);
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);
2115 gtk_container_show_all (GtkWidget *widget)
2117 g_return_if_fail (widget != NULL);
2118 g_return_if_fail (GTK_IS_CONTAINER (widget));
2120 gtk_container_foreach (GTK_CONTAINER (widget),
2121 (GtkCallback) gtk_widget_show_all,
2123 gtk_widget_show (widget);
2127 gtk_container_hide_all (GtkWidget *widget)
2129 g_return_if_fail (widget != NULL);
2130 g_return_if_fail (GTK_IS_CONTAINER (widget));
2132 gtk_widget_hide (widget);
2133 gtk_container_foreach (GTK_CONTAINER (widget),
2134 (GtkCallback) gtk_widget_hide_all,
2140 gtk_container_expose_child (GtkWidget *child,
2141 gpointer client_data)
2144 GtkWidget *container;
2145 GdkEventExpose *event;
2146 } *data = client_data;
2148 gtk_container_propagate_expose (GTK_CONTAINER (data->container),
2154 gtk_container_expose (GtkWidget *widget,
2155 GdkEventExpose *event)
2158 GtkWidget *container;
2159 GdkEventExpose *event;
2162 g_return_val_if_fail (widget != NULL, FALSE);
2163 g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
2164 g_return_val_if_fail (event != NULL, FALSE);
2167 if (GTK_WIDGET_DRAWABLE (widget))
2169 data.container = widget;
2172 gtk_container_foreach (GTK_CONTAINER (widget),
2173 gtk_container_expose_child,
2182 * gtk_container_propagate_expose:
2183 * @container: a #GtkContainer
2184 * @child: a child of @container
2185 * @event: a expose event sent to container
2187 * When a container receives an expose event, it must send synthetic
2188 * expose events to all children that don't have their own GdkWindows.
2189 * This function provides a convenient way of doing this. A container,
2190 * when it receives an expose event, gtk_container_propagate_expose()
2191 * once for each child, passing in the event the container received.
2193 * gtk_container_propagate expose() takes care of deciding whether
2194 * an expose event needs to be sent to the child, intersecting
2195 * the event's area with the child area, and sending the event.
2197 * In most cases, a container can simply either simply inherit the
2198 * ::expose implementation from GtkContainer, or, do some drawing
2199 * and then chain to the ::expose implementation from GtkContainer.
2202 gtk_container_propagate_expose (GtkContainer *container,
2204 GdkEventExpose *event)
2206 GdkEventExpose child_event;
2208 g_return_if_fail (GTK_IS_CONTAINER (container));
2209 g_return_if_fail (GTK_IS_WIDGET (child));
2210 g_return_if_fail (event != NULL);
2212 g_assert (child->parent == GTK_WIDGET (container));
2214 if (GTK_WIDGET_DRAWABLE (child) &&
2215 GTK_WIDGET_NO_WINDOW (child) &&
2216 (child->window == event->window))
2218 child_event = *event;
2220 child_event.region = gtk_widget_region_intersect (child, event->region);
2221 if (!gdk_region_empty (child_event.region))
2223 gdk_region_get_clipbox (child_event.region, &child_event.area);
2224 gtk_widget_send_expose (child, (GdkEvent *)&child_event);
2226 gdk_region_destroy (child_event.region);