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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
20 #include "gtkcontainer.h"
21 #include "gtkprivate.h"
22 #include "gtksignal.h"
42 typedef struct _GtkChildArgInfo GtkChildArgInfo;
43 struct _GtkChildArgInfo
53 /* The global list of toplevel windows */
54 static GList *toplevel_list = NULL;
56 static void gtk_container_base_class_init (GtkContainerClass *klass);
57 static void gtk_container_class_init (GtkContainerClass *klass);
58 static void gtk_container_init (GtkContainer *container);
59 static void gtk_container_destroy (GtkObject *object);
60 static void gtk_container_get_arg (GtkObject *object,
63 static void gtk_container_set_arg (GtkObject *object,
66 static void gtk_container_add_unimplemented (GtkContainer *container,
68 static void gtk_container_remove_unimplemented (GtkContainer *container,
70 static void gtk_container_real_check_resize (GtkContainer *container);
71 static gint gtk_container_real_focus (GtkContainer *container,
72 GtkDirectionType direction);
73 static void gtk_container_real_set_focus_child (GtkContainer *container,
75 static gint gtk_container_focus_tab (GtkContainer *container,
77 GtkDirectionType direction);
78 static gint gtk_container_focus_up_down (GtkContainer *container,
80 GtkDirectionType direction);
81 static gint gtk_container_focus_left_right (GtkContainer *container,
83 GtkDirectionType direction);
84 static gint gtk_container_focus_move (GtkContainer *container,
86 GtkDirectionType direction);
87 static void gtk_container_children_callback (GtkWidget *widget,
88 gpointer client_data);
89 static void gtk_container_show_all (GtkWidget *widget);
90 static void gtk_container_hide_all (GtkWidget *widget);
92 static gchar* gtk_container_child_default_composite_name (GtkContainer *container,
97 static guint container_signals[LAST_SIGNAL] = { 0 };
98 static GHashTable *container_child_arg_info_ht = NULL;
100 static GtkWidgetClass *parent_class = NULL;
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;
109 gtk_container_get_type (void)
111 static GtkType container_type = 0;
115 static const GtkTypeInfo container_info =
118 sizeof (GtkContainer),
119 sizeof (GtkContainerClass),
120 (GtkClassInitFunc) gtk_container_class_init,
121 (GtkObjectInitFunc) gtk_container_init,
122 /* reserved_1 */ NULL,
123 /* reserved_2 */ NULL,
124 (GtkClassInitFunc) gtk_container_base_class_init,
127 container_type = gtk_type_unique (gtk_widget_get_type (), &container_info);
130 return container_type;
134 gtk_container_base_class_init (GtkContainerClass *class)
136 /* reset instance specifc class fields that don't get inherited */
137 class->n_child_args = 0;
138 class->set_child_arg = NULL;
139 class->get_child_arg = NULL;
143 gtk_container_class_init (GtkContainerClass *class)
145 GtkObjectClass *object_class;
146 GtkWidgetClass *widget_class;
148 object_class = (GtkObjectClass*) class;
149 widget_class = (GtkWidgetClass*) class;
151 parent_class = gtk_type_class (gtk_widget_get_type ());
153 container_child_arg_info_ht = g_hash_table_new (gtk_arg_info_hash,
156 vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
157 hadjustment_key_id = g_quark_from_static_string (hadjustment_key);
159 gtk_object_add_arg_type ("GtkContainer::border_width", GTK_TYPE_ULONG, GTK_ARG_READWRITE, ARG_BORDER_WIDTH);
160 gtk_object_add_arg_type ("GtkContainer::resize_mode", GTK_TYPE_RESIZE_MODE, GTK_ARG_READWRITE, ARG_RESIZE_MODE);
161 gtk_object_add_arg_type ("GtkContainer::child", GTK_TYPE_WIDGET, GTK_ARG_WRITABLE, ARG_CHILD);
163 container_signals[ADD] =
164 gtk_signal_new ("add",
167 GTK_SIGNAL_OFFSET (GtkContainerClass, add),
168 gtk_marshal_NONE__POINTER,
171 container_signals[REMOVE] =
172 gtk_signal_new ("remove",
175 GTK_SIGNAL_OFFSET (GtkContainerClass, remove),
176 gtk_marshal_NONE__POINTER,
179 container_signals[CHECK_RESIZE] =
180 gtk_signal_new ("check_resize",
183 GTK_SIGNAL_OFFSET (GtkContainerClass, check_resize),
184 gtk_marshal_NONE__NONE,
186 container_signals[FOCUS] =
187 gtk_signal_new ("focus",
190 GTK_SIGNAL_OFFSET (GtkContainerClass, focus),
191 gtk_marshal_ENUM__ENUM,
192 GTK_TYPE_DIRECTION_TYPE, 1,
193 GTK_TYPE_DIRECTION_TYPE);
194 container_signals[SET_FOCUS_CHILD] =
195 gtk_signal_new ("set-focus-child",
198 GTK_SIGNAL_OFFSET (GtkContainerClass, set_focus_child),
199 gtk_marshal_NONE__POINTER,
202 gtk_object_class_add_signals (object_class, container_signals, LAST_SIGNAL);
204 object_class->get_arg = gtk_container_get_arg;
205 object_class->set_arg = gtk_container_set_arg;
206 object_class->destroy = gtk_container_destroy;
208 widget_class->show_all = gtk_container_show_all;
209 widget_class->hide_all = gtk_container_hide_all;
211 class->add = gtk_container_add_unimplemented;
212 class->remove = gtk_container_remove_unimplemented;
213 class->check_resize = gtk_container_real_check_resize;
214 class->forall = NULL;
215 class->focus = gtk_container_real_focus;
216 class->set_focus_child = gtk_container_real_set_focus_child;
217 class->child_type = NULL;
218 class->composite_name = gtk_container_child_default_composite_name;
222 gtk_container_child_type (GtkContainer *container)
225 GtkContainerClass *class;
227 g_return_val_if_fail (container != NULL, 0);
228 g_return_val_if_fail (GTK_IS_CONTAINER (container), 0);
230 class = GTK_CONTAINER_CLASS (GTK_OBJECT (container)->klass);
231 if (class->child_type)
232 slot = class->child_type (container);
234 slot = GTK_TYPE_NONE;
239 /****************************************************
240 * GtkContainer child argument mechanism
242 ****************************************************/
245 gtk_container_add_with_args (GtkContainer *container,
247 const gchar *first_arg_name,
250 g_return_if_fail (container != NULL);
251 g_return_if_fail (GTK_IS_CONTAINER (container));
252 g_return_if_fail (widget != NULL);
253 g_return_if_fail (GTK_IS_WIDGET (widget));
254 g_return_if_fail (widget->parent == NULL);
256 gtk_widget_ref (GTK_WIDGET (container));
257 gtk_widget_ref (widget);
259 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
264 GSList *arg_list = NULL;
265 GSList *info_list = NULL;
268 va_start (var_args, first_arg_name);
269 error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
278 g_warning ("gtk_container_add_with_args(): %s", error);
286 slist_arg = arg_list;
287 slist_info = info_list;
290 gtk_container_arg_set (container, widget, slist_arg->data, slist_info->data);
291 slist_arg = slist_arg->next;
292 slist_info = slist_info->next;
294 gtk_args_collect_cleanup (arg_list, info_list);
298 gtk_widget_unref (widget);
299 gtk_widget_unref (GTK_WIDGET (container));
303 gtk_container_addv (GtkContainer *container,
308 g_return_if_fail (container != NULL);
309 g_return_if_fail (GTK_IS_CONTAINER (container));
310 g_return_if_fail (widget != NULL);
311 g_return_if_fail (GTK_IS_WIDGET (widget));
312 g_return_if_fail (widget->parent == NULL);
314 gtk_widget_ref (GTK_WIDGET (container));
315 gtk_widget_ref (widget);
317 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
323 for (max_args = args + n_args; args < max_args; args++)
324 gtk_container_arg_set (container, widget, args, NULL);
327 gtk_widget_unref (widget);
328 gtk_widget_unref (GTK_WIDGET (container));
332 gtk_container_child_setv (GtkContainer *container,
339 g_return_if_fail (container != NULL);
340 g_return_if_fail (GTK_IS_CONTAINER (container));
341 g_return_if_fail (child != NULL);
342 g_return_if_fail (GTK_IS_WIDGET (child));
343 g_return_if_fail (child->parent != NULL);
345 g_return_if_fail (args != NULL);
347 for (max_args = args + n_args; args < max_args; args++)
348 gtk_container_arg_set (container, child, args, NULL);
352 gtk_container_child_getv (GtkContainer *container,
359 g_return_if_fail (container != NULL);
360 g_return_if_fail (GTK_IS_CONTAINER (container));
361 g_return_if_fail (child != NULL);
362 g_return_if_fail (GTK_IS_WIDGET (child));
363 g_return_if_fail (child->parent != NULL);
365 g_return_if_fail (args != NULL);
367 for (max_args = args + n_args; args < max_args; args++)
368 gtk_container_arg_get (container, child, args, NULL);
372 gtk_container_child_set (GtkContainer *container,
374 const gchar *first_arg_name,
378 GSList *arg_list = NULL;
379 GSList *info_list = NULL;
382 g_return_if_fail (container != NULL);
383 g_return_if_fail (GTK_IS_CONTAINER (container));
384 g_return_if_fail (child != NULL);
385 g_return_if_fail (GTK_IS_WIDGET (child));
386 g_return_if_fail (child->parent != NULL);
388 va_start (var_args, first_arg_name);
389 error = gtk_container_child_args_collect (GTK_OBJECT_TYPE (container),
398 g_warning ("gtk_container_child_set(): %s", error);
406 slist_arg = arg_list;
407 slist_info = info_list;
410 gtk_container_arg_set (container, child, slist_arg->data, slist_info->data);
411 slist_arg = slist_arg->next;
412 slist_info = slist_info->next;
414 gtk_args_collect_cleanup (arg_list, info_list);
419 gtk_container_arg_set (GtkContainer *container,
424 GtkContainerClass *class;
426 g_return_if_fail (container != NULL);
427 g_return_if_fail (GTK_IS_CONTAINER (container));
428 g_return_if_fail (child != NULL);
429 g_return_if_fail (GTK_IS_WIDGET (child));
430 g_return_if_fail (arg != NULL);
436 error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
437 container_child_arg_info_ht,
442 g_warning ("gtk_container_arg_set(): %s", error);
447 g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
449 if (! (info->arg_flags & GTK_ARG_WRITABLE))
451 g_warning ("gtk_container_arg_set(): argument \"%s\" is not writable",
455 if (info->type != arg->type)
457 g_warning ("gtk_container_arg_set(): argument \"%s\" has invalid type `%s'",
459 gtk_type_name (arg->type));
463 class = gtk_type_class (info->class_type);
464 g_assert (class->set_child_arg != NULL);
465 class->set_child_arg (container, child, arg, info->arg_id);
469 gtk_container_arg_get (GtkContainer *container,
474 GtkContainerClass *class;
476 g_return_if_fail (container != NULL);
477 g_return_if_fail (GTK_IS_CONTAINER (container));
478 g_return_if_fail (child != NULL);
479 g_return_if_fail (GTK_IS_WIDGET (child));
480 g_return_if_fail (arg != NULL);
486 error = gtk_arg_get_info (GTK_OBJECT_TYPE (container),
487 container_child_arg_info_ht,
492 g_warning ("gtk_container_arg_get(): %s", error);
494 arg->type = GTK_TYPE_INVALID;
498 g_return_if_fail (info->arg_flags & GTK_ARG_CHILD_ARG);
500 if (! (info->arg_flags & GTK_ARG_READABLE))
502 g_warning ("gtk_container_arg_get(): argument \"%s\" is not readable",
504 arg->type = GTK_TYPE_INVALID;
508 class = gtk_type_class (info->class_type);
509 g_assert (class->get_child_arg != NULL);
510 arg->type = info->type;
511 class->get_child_arg (container, child, arg, info->arg_id);
515 gtk_container_add_child_arg_type (const gchar *arg_name,
520 g_return_if_fail (arg_name != NULL);
521 g_return_if_fail (arg_type > GTK_TYPE_NONE);
522 g_return_if_fail (arg_id > 0);
523 g_return_if_fail ((arg_flags & GTK_ARG_READWRITE) == GTK_ARG_READWRITE);
524 /* g_return_if_fail ((arg_flags & GTK_ARG_CHILD_ARG) != 0); */
526 arg_flags |= GTK_ARG_CHILD_ARG;
527 arg_flags &= GTK_ARG_MASK;
529 gtk_arg_type_new_static (GTK_TYPE_CONTAINER,
531 GTK_STRUCT_OFFSET (GtkContainerClass, n_child_args),
532 container_child_arg_info_ht,
539 gtk_container_child_args_collect (GtkType object_type,
541 GSList **info_list_p,
542 const gchar *first_arg_name,
545 return gtk_args_collect (object_type,
546 container_child_arg_info_ht,
554 gtk_container_child_arg_get_info (GtkType object_type,
555 const gchar *arg_name,
558 return gtk_arg_get_info (object_type,
559 container_child_arg_info_ht,
565 gtk_container_query_child_args (GtkType class_type,
569 g_return_val_if_fail (n_args != NULL, NULL);
571 g_return_val_if_fail (gtk_type_is_a (class_type, GTK_TYPE_CONTAINER), NULL);
573 return gtk_args_query (class_type, container_child_arg_info_ht, arg_flags, n_args);
578 gtk_container_add_unimplemented (GtkContainer *container,
581 g_warning ("GtkContainerClass::add not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
585 gtk_container_remove_unimplemented (GtkContainer *container,
588 g_warning ("GtkContainerClass::remove not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (container)));
592 gtk_container_init (GtkContainer *container)
594 container->focus_child = NULL;
595 container->border_width = 0;
596 container->need_resize = FALSE;
597 container->resize_mode = GTK_RESIZE_PARENT;
598 container->resize_widgets = NULL;
602 gtk_container_destroy (GtkObject *object)
604 GtkContainer *container;
606 g_return_if_fail (object != NULL);
607 g_return_if_fail (GTK_IS_CONTAINER (object));
609 container = GTK_CONTAINER (object);
611 if (GTK_CONTAINER_RESIZE_PENDING (container))
613 container_resize_queue = g_slist_remove (container_resize_queue, container);
614 GTK_PRIVATE_UNSET_FLAG (container, GTK_RESIZE_PENDING);
616 gtk_container_clear_resize_widgets (container);
618 gtk_container_foreach (container, (GtkCallback) gtk_widget_destroy, NULL);
620 if (GTK_OBJECT_CLASS (parent_class)->destroy)
621 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
625 gtk_container_set_arg (GtkObject *object,
629 GtkContainer *container;
631 container = GTK_CONTAINER (object);
635 case ARG_BORDER_WIDTH:
636 gtk_container_set_border_width (container, GTK_VALUE_ULONG (*arg));
638 case ARG_RESIZE_MODE:
639 gtk_container_set_resize_mode (container, GTK_VALUE_ENUM (*arg));
642 gtk_container_add (container, GTK_WIDGET (GTK_VALUE_OBJECT (*arg)));
650 gtk_container_get_arg (GtkObject *object,
654 GtkContainer *container;
656 container = GTK_CONTAINER (object);
660 case ARG_BORDER_WIDTH:
661 GTK_VALUE_ULONG (*arg) = container->border_width;
663 case ARG_RESIZE_MODE:
664 GTK_VALUE_ENUM (*arg) = container->resize_mode;
667 arg->type = GTK_TYPE_INVALID;
673 gtk_container_set_border_width (GtkContainer *container,
676 g_return_if_fail (container != NULL);
677 g_return_if_fail (GTK_IS_CONTAINER (container));
679 if (container->border_width != border_width)
681 container->border_width = border_width;
683 if (GTK_WIDGET_REALIZED (container))
684 gtk_widget_queue_resize (GTK_WIDGET (container));
689 gtk_container_add (GtkContainer *container,
692 g_return_if_fail (container != NULL);
693 g_return_if_fail (GTK_IS_CONTAINER (container));
694 g_return_if_fail (widget != NULL);
695 g_return_if_fail (GTK_IS_WIDGET (widget));
696 g_return_if_fail (widget->parent == NULL);
698 gtk_signal_emit (GTK_OBJECT (container), container_signals[ADD], widget);
702 gtk_container_remove (GtkContainer *container,
705 g_return_if_fail (container != NULL);
706 g_return_if_fail (GTK_IS_CONTAINER (container));
707 g_return_if_fail (widget != NULL);
708 g_return_if_fail (GTK_IS_WIDGET (widget));
709 g_return_if_fail (widget->parent == GTK_WIDGET (container));
711 gtk_signal_emit (GTK_OBJECT (container), container_signals[REMOVE], widget);
715 gtk_container_block_resize (GtkContainer *container)
717 g_return_if_fail (container != NULL);
718 g_return_if_fail (GTK_IS_CONTAINER (container));
720 g_message ("gtk_container_block_resize() is deprecated");
724 gtk_container_unblock_resize (GtkContainer *container)
726 g_return_if_fail (container != NULL);
727 g_return_if_fail (GTK_IS_CONTAINER (container));
729 g_message ("gtk_container_unblock_resize() is deprecated");
733 gtk_container_clear_resize_widgets (GtkContainer *container)
737 g_return_if_fail (container != NULL);
738 g_return_if_fail (GTK_IS_CONTAINER (container));
740 node = container->resize_widgets;
743 gtk_signal_disconnect_by_func (GTK_OBJECT (container),
744 GTK_SIGNAL_FUNC (gtk_container_clear_resize_widgets),
749 GtkWidget *widget = node->data;
751 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
755 g_slist_free (container->resize_widgets);
756 container->resize_widgets = NULL;
760 gtk_container_set_resize_mode (GtkContainer *container,
761 GtkResizeMode resize_mode)
763 g_return_if_fail (container != NULL);
764 g_return_if_fail (GTK_IS_CONTAINER (container));
765 g_return_if_fail (resize_mode <= GTK_RESIZE_IMMEDIATE);
767 if (GTK_WIDGET_TOPLEVEL (container) &&
768 resize_mode == GTK_RESIZE_PARENT)
769 resize_mode = GTK_RESIZE_QUEUE;
771 if (container->resize_mode != resize_mode)
773 container->resize_mode = resize_mode;
775 if (resize_mode == GTK_RESIZE_IMMEDIATE)
776 gtk_container_check_resize (container);
779 gtk_container_clear_resize_widgets (container);
780 gtk_widget_queue_resize (GTK_WIDGET (container));
786 gtk_container_need_resize (GtkContainer *container)
788 g_message ("gtk_container_need_resize() is deprecated");
790 gtk_container_check_resize (container);
795 gtk_container_get_resize_container (GtkContainer *container)
799 widget = GTK_WIDGET (container);
801 while (widget->parent)
803 widget = widget->parent;
804 if (GTK_IS_RESIZE_CONTAINER (widget) && !GTK_WIDGET_RESIZE_NEEDED (widget))
808 return GTK_IS_RESIZE_CONTAINER (widget) ? (GtkContainer*) widget : NULL;
812 gtk_container_idle_sizer (gpointer data)
814 /* we may be invoked with a container_resize_queue of NULL, because
815 * queue_resize could have been adding an extra idle function while
816 * the queue still got processed. we better just ignore such case
817 * than trying to explicitely work around them with some extra flags,
818 * sine it doesn't cause any actual harm.
820 while (container_resize_queue)
825 slist = container_resize_queue;
826 container_resize_queue = slist->next;
827 widget = slist->data;
828 g_slist_free_1 (slist);
830 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_PENDING);
831 gtk_container_check_resize (GTK_CONTAINER (widget));
838 gtk_container_queue_resize (GtkContainer *container)
840 GtkContainer *resize_container;
842 g_return_if_fail (container != NULL);
843 g_return_if_fail (GTK_IS_CONTAINER (container));
845 if (GTK_OBJECT_DESTROYED (container) ||
846 GTK_WIDGET_RESIZE_NEEDED (container))
849 if (GTK_IS_RESIZE_CONTAINER (container))
850 gtk_container_clear_resize_widgets (container);
852 resize_container = gtk_container_get_resize_container (container);
854 if (resize_container)
856 if (GTK_WIDGET_VISIBLE (resize_container))
858 switch (resize_container->resize_mode)
860 case GTK_RESIZE_QUEUE:
861 if (!GTK_CONTAINER_RESIZE_PENDING (resize_container))
863 GTK_PRIVATE_SET_FLAG (resize_container, GTK_RESIZE_PENDING);
864 if (container_resize_queue == NULL)
865 gtk_idle_add_priority (GTK_PRIORITY_INTERNAL - 1,
866 gtk_container_idle_sizer,
868 container_resize_queue = g_slist_prepend (container_resize_queue, resize_container);
871 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
872 if (!resize_container->resize_widgets)
873 gtk_signal_connect (GTK_OBJECT (resize_container),
875 GTK_SIGNAL_FUNC (gtk_container_clear_resize_widgets),
877 resize_container->resize_widgets =
878 g_slist_prepend (resize_container->resize_widgets, container);
881 case GTK_RESIZE_IMMEDIATE:
882 GTK_PRIVATE_SET_FLAG (container, GTK_RESIZE_NEEDED);
883 if (!resize_container->resize_widgets)
884 gtk_signal_connect (GTK_OBJECT (resize_container),
886 GTK_SIGNAL_FUNC (gtk_container_clear_resize_widgets),
888 resize_container->resize_widgets =
889 g_slist_prepend (resize_container->resize_widgets, container);
890 gtk_container_check_resize (resize_container);
893 case GTK_RESIZE_PARENT:
900 /* We need to let hidden toplevels know that something
901 * changed while they where hidden. For other resize containers,
902 * they will get resized when they are shown.
904 if (GTK_WIDGET_TOPLEVEL (resize_container))
905 gtk_container_check_resize (resize_container);
911 gtk_container_check_resize (GtkContainer *container)
913 g_return_if_fail (container != NULL);
914 g_return_if_fail (GTK_IS_CONTAINER (container));
916 gtk_signal_emit (GTK_OBJECT (container), container_signals[CHECK_RESIZE]);
920 gtk_container_real_check_resize (GtkContainer *container)
924 g_return_if_fail (container != NULL);
925 g_return_if_fail (GTK_IS_CONTAINER (container));
927 widget = GTK_WIDGET (container);
929 gtk_widget_size_request (widget, &widget->requisition);
931 if (widget->requisition.width > widget->allocation.width ||
932 widget->requisition.height > widget->allocation.height)
934 if (GTK_IS_RESIZE_CONTAINER (container))
936 gtk_widget_size_allocate (GTK_WIDGET (container),
937 >K_WIDGET (container)->allocation);
938 gtk_widget_queue_draw (GTK_WIDGET (container));
941 gtk_widget_queue_resize (widget);
945 gtk_container_resize_children (container);
949 /* The container hasn't changed size but one of its children
950 * queued a resize request. Which means that the allocation
951 * is not sufficient for the requisition of some child.
952 * We've already performed a size request at this point,
953 * so we simply need to run through the list of resize
954 * widgets and reallocate their sizes appropriately. We
955 * make the optimization of not performing reallocation
956 * for a widget who also has a parent in the resize widgets
957 * list. GTK_RESIZE_NEEDED is used for flagging those
958 * parents inside this function.
961 gtk_container_resize_children (GtkContainer *container)
964 GtkWidget *resize_container;
965 GSList *resize_widgets;
966 GSList *resize_containers;
969 /* resizing invariants:
970 * toplevels have *always* resize_mode != GTK_RESIZE_PARENT set.
971 * containers with resize_mode==GTK_RESIZE_PARENT have to have resize_widgets
973 * containers that are flagged RESIZE_NEEDED must have resize_widgets set to
974 * NULL, or are toplevels (thus have ->parent set to NULL).
975 * widgets that are in some container->resize_widgets list must be flagged with
977 * widgets that have RESIZE_NEEDED set must be referenced in some
978 * GTK_IS_RESIZE_CONTAINER (container)->resize_widgets list.
979 * containers that have an idle sizer pending must be flagged with
983 g_return_if_fail (container != NULL);
984 g_return_if_fail (GTK_IS_CONTAINER (container));
986 /* we first check out if we actually need to perform a resize,
987 * which is not the case if we got another container queued for
988 * a resize in our anchestry. also we can skip the whole
989 * resize_widgets checks if we are a toplevel and NEED_RESIZE.
990 * this code implies that our allocation is sufficient for our
991 * requisition, since otherwise we would NEED_RESIZE.
993 resize_container = GTK_WIDGET (container);
994 while (resize_container)
996 if (GTK_WIDGET_RESIZE_NEEDED (resize_container))
998 resize_container = resize_container->parent;
1000 if (resize_container)
1002 /* queue_resize and size_allocate both clear our
1003 * resize_widgets list.
1005 if (resize_container->parent)
1006 gtk_container_queue_resize (container);
1009 gtk_widget_size_allocate (GTK_WIDGET (container),
1010 >K_WIDGET (container)->allocation);
1011 gtk_widget_queue_draw (GTK_WIDGET (container));
1016 resize_container = GTK_WIDGET (container);
1018 /* we now walk the anchestry for all resize widgets as long
1019 * as they are our children and as long as their allocation
1020 * is insufficient, since we don't need to reallocate below that.
1022 resize_widgets = container->resize_widgets;
1024 gtk_signal_disconnect_by_func (GTK_OBJECT (container),
1025 GTK_SIGNAL_FUNC (gtk_container_clear_resize_widgets),
1027 container->resize_widgets = NULL;
1028 for (node = resize_widgets; node; node = node->next)
1030 widget = node->data;
1032 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1034 while (widget->parent != resize_container &&
1035 ((widget->allocation.width < widget->requisition.width) ||
1036 (widget->allocation.height < widget->requisition.height)))
1037 widget = widget->parent;
1039 GTK_PRIVATE_SET_FLAG (widget, GTK_RESIZE_NEEDED);
1040 node->data = widget;
1043 /* for the newly setup resize_widgets list, we now walk each widget's
1044 * anchestry to sort those widgets out that have RESIZE_NEEDED parents.
1045 * we can safely stop the walk if we are the parent, since we checked
1046 * our own anchestry already.
1048 resize_containers = NULL;
1049 for (node = resize_widgets; node; node = node->next)
1053 widget = node->data;
1055 if (!GTK_WIDGET_RESIZE_NEEDED (widget))
1058 parent = widget->parent;
1060 while (parent != resize_container)
1062 if (GTK_WIDGET_RESIZE_NEEDED (parent))
1064 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1067 parent = parent->parent;
1070 if (!g_slist_find (resize_containers, widget))
1072 resize_containers = g_slist_prepend (resize_containers, widget);
1073 gtk_widget_ref (widget);
1076 g_slist_free (resize_widgets);
1078 for (node = resize_containers; node; node = node->next)
1080 widget = node->data;
1082 GTK_PRIVATE_UNSET_FLAG (widget, GTK_RESIZE_NEEDED);
1083 gtk_widget_size_allocate (widget, &widget->allocation);
1084 gtk_widget_queue_draw (widget);
1085 gtk_widget_unref (widget);
1087 g_slist_free (resize_containers);
1091 gtk_container_forall (GtkContainer *container,
1092 GtkCallback callback,
1093 gpointer callback_data)
1095 GtkContainerClass *class;
1097 g_return_if_fail (container != NULL);
1098 g_return_if_fail (GTK_IS_CONTAINER (container));
1099 g_return_if_fail (callback != NULL);
1101 class = GTK_CONTAINER_CLASS (GTK_OBJECT (container)->klass);
1104 class->forall (container, TRUE, callback, callback_data);
1108 gtk_container_foreach (GtkContainer *container,
1109 GtkCallback callback,
1110 gpointer callback_data)
1112 GtkContainerClass *class;
1114 g_return_if_fail (container != NULL);
1115 g_return_if_fail (GTK_IS_CONTAINER (container));
1116 g_return_if_fail (callback != NULL);
1118 class = GTK_CONTAINER_CLASS (GTK_OBJECT (container)->klass);
1121 class->forall (container, FALSE, callback, callback_data);
1124 typedef struct _GtkForeachData GtkForeachData;
1125 struct _GtkForeachData
1127 GtkObject *container;
1128 GtkCallbackMarshal callback;
1129 gpointer callback_data;
1133 gtk_container_foreach_unmarshal (GtkWidget *child,
1136 GtkForeachData *fdata = (GtkForeachData*) data;
1139 /* first argument */
1140 args[0].name = NULL;
1141 args[0].type = GTK_OBJECT(child)->klass->type;
1142 GTK_VALUE_OBJECT(args[0]) = GTK_OBJECT (child);
1144 /* location for return value */
1145 args[1].name = NULL;
1146 args[1].type = GTK_TYPE_NONE;
1148 fdata->callback (fdata->container, fdata->callback_data, 1, args);
1152 gtk_container_foreach_interp (GtkContainer *container,
1153 GtkCallbackMarshal marshal,
1154 gpointer callback_data,
1155 GtkDestroyNotify notify)
1157 gtk_container_foreach_full (container, NULL, marshal,
1158 callback_data, notify);
1162 gtk_container_foreach_full (GtkContainer *container,
1163 GtkCallback callback,
1164 GtkCallbackMarshal marshal,
1165 gpointer callback_data,
1166 GtkDestroyNotify notify)
1168 g_return_if_fail (container != NULL);
1169 g_return_if_fail (GTK_IS_CONTAINER (container));
1173 GtkForeachData fdata;
1175 fdata.container = GTK_OBJECT (container);
1176 fdata.callback = marshal;
1177 fdata.callback_data = callback_data;
1179 gtk_container_foreach (container, gtk_container_foreach_unmarshal, &fdata);
1183 g_return_if_fail (callback != NULL);
1185 gtk_container_foreach (container, callback, &callback_data);
1189 notify (callback_data);
1193 gtk_container_focus (GtkContainer *container,
1194 GtkDirectionType direction)
1198 g_return_val_if_fail (container != NULL, FALSE);
1199 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1201 gtk_signal_emit (GTK_OBJECT (container),
1202 container_signals[FOCUS],
1203 direction, &return_val);
1209 gtk_container_set_focus_child (GtkContainer *container,
1212 g_return_if_fail (container != NULL);
1213 g_return_if_fail (GTK_IS_CONTAINER (container));
1215 g_return_if_fail (GTK_IS_WIDGET (widget));
1217 gtk_signal_emit (GTK_OBJECT (container), container_signals[SET_FOCUS_CHILD], widget);
1221 gtk_container_children (GtkContainer *container)
1227 gtk_container_foreach (container,
1228 gtk_container_children_callback,
1231 return g_list_reverse (children);
1235 gtk_container_register_toplevel (GtkContainer *container)
1237 g_return_if_fail (container != NULL);
1239 toplevel_list = g_list_prepend (toplevel_list, container);
1241 gtk_widget_ref (GTK_WIDGET (container));
1242 gtk_object_sink (GTK_OBJECT (container));
1246 gtk_container_unregister_toplevel (GtkContainer *container)
1250 g_return_if_fail (container != NULL);
1252 node = g_list_find (toplevel_list, container);
1253 g_return_if_fail (node != NULL);
1255 toplevel_list = g_list_remove_link (toplevel_list, node);
1256 g_list_free_1 (node);
1258 gtk_widget_unref (GTK_WIDGET (container));
1262 gtk_container_get_toplevels (void)
1264 /* XXX: fixme we should ref all these widgets and duplicate
1267 return toplevel_list;
1271 gtk_container_child_position_callback (GtkWidget *widget,
1272 gpointer client_data)
1278 } *data = client_data;
1281 if (data->child == widget)
1282 data->index = data->i;
1286 gtk_container_child_default_composite_name (GtkContainer *container,
1296 /* fallback implementation */
1300 gtk_container_forall (container,
1301 gtk_container_child_position_callback,
1304 name = g_strdup_printf ("%s-%u",
1305 gtk_type_name (GTK_OBJECT_TYPE (child)),
1312 gtk_container_child_composite_name (GtkContainer *container,
1315 g_return_val_if_fail (container != NULL, NULL);
1316 g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
1317 g_return_val_if_fail (child != NULL, NULL);
1318 g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
1319 g_return_val_if_fail (child->parent == GTK_WIDGET (container), NULL);
1321 if (GTK_WIDGET_COMPOSITE_CHILD (child))
1323 static GQuark quark_composite_name = 0;
1326 if (!quark_composite_name)
1327 quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
1329 name = gtk_object_get_data_by_id (GTK_OBJECT (child), quark_composite_name);
1332 GtkContainerClass *class;
1334 class = GTK_CONTAINER_CLASS (GTK_OBJECT (container)->klass);
1335 if (class->composite_name)
1336 name = class->composite_name (container, child);
1339 name = g_strdup (name);
1348 gtk_container_real_set_focus_child (GtkContainer *container,
1351 g_return_if_fail (container != NULL);
1352 g_return_if_fail (GTK_IS_CONTAINER (container));
1354 g_return_if_fail (GTK_IS_WIDGET (child));
1356 if (child != container->focus_child)
1358 if (container->focus_child)
1359 gtk_widget_unref (container->focus_child);
1360 container->focus_child = child;
1361 if (container->focus_child)
1362 gtk_widget_ref (container->focus_child);
1366 /* check for h/v adjustments
1368 if (container->focus_child)
1370 GtkAdjustment *adjustment;
1372 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), vadjustment_key_id);
1374 gtk_adjustment_clamp_page (adjustment,
1375 container->focus_child->allocation.y,
1376 (container->focus_child->allocation.y +
1377 container->focus_child->allocation.height));
1379 adjustment = gtk_object_get_data_by_id (GTK_OBJECT (container), hadjustment_key_id);
1381 gtk_adjustment_clamp_page (adjustment,
1382 container->focus_child->allocation.x,
1383 (container->focus_child->allocation.x +
1384 container->focus_child->allocation.width));
1389 gtk_container_real_focus (GtkContainer *container,
1390 GtkDirectionType direction)
1397 g_return_val_if_fail (container != NULL, FALSE);
1398 g_return_val_if_fail (GTK_IS_CONTAINER (container), FALSE);
1400 /* Fail if the container is inappropriate for focus movement
1402 if (!GTK_WIDGET_DRAWABLE (container) ||
1403 !GTK_WIDGET_SENSITIVE (container))
1408 if (GTK_WIDGET_CAN_FOCUS (container))
1410 gtk_widget_grab_focus (GTK_WIDGET (container));
1415 /* Get a list of the containers children
1418 gtk_container_forall (container,
1419 gtk_container_children_callback,
1421 children = g_list_reverse (children);
1422 /* children = gtk_container_children (container); */
1426 /* Remove any children which are inappropriate for focus movement
1428 tmp_list = children;
1431 if (GTK_WIDGET_SENSITIVE (tmp_list->data) &&
1432 GTK_WIDGET_DRAWABLE (tmp_list->data) &&
1433 (GTK_IS_CONTAINER (tmp_list->data) || GTK_WIDGET_CAN_FOCUS (tmp_list->data)))
1434 tmp_list = tmp_list->next;
1437 tmp_list2 = tmp_list;
1438 tmp_list = tmp_list->next;
1440 children = g_list_remove_link (children, tmp_list2);
1441 g_list_free_1 (tmp_list2);
1447 case GTK_DIR_TAB_FORWARD:
1448 case GTK_DIR_TAB_BACKWARD:
1449 return_val = gtk_container_focus_tab (container, children, direction);
1453 return_val = gtk_container_focus_up_down (container, children, direction);
1457 return_val = gtk_container_focus_left_right (container, children, direction);
1461 g_list_free (children);
1469 gtk_container_focus_tab (GtkContainer *container,
1471 GtkDirectionType direction)
1479 length = g_list_length (children);
1481 /* sort the children in the y direction */
1482 for (i = 1; i < length; i++)
1485 tmp_list = g_list_nth (children, j);
1486 child = tmp_list->data;
1490 child2 = tmp_list->prev->data;
1491 if (child->allocation.y < child2->allocation.y)
1493 tmp_list->data = tmp_list->prev->data;
1494 tmp_list = tmp_list->prev;
1501 tmp_list->data = child;
1504 /* sort the children in the x direction while
1505 * maintaining the y direction sort.
1507 for (i = 1; i < length; i++)
1510 tmp_list = g_list_nth (children, j);
1511 child = tmp_list->data;
1515 child2 = tmp_list->prev->data;
1516 if ((child->allocation.x < child2->allocation.x) &&
1517 (child->allocation.y == child2->allocation.y))
1519 tmp_list->data = tmp_list->prev->data;
1520 tmp_list = tmp_list->prev;
1527 tmp_list->data = child;
1530 /* if we are going backwards then reverse the order
1533 if (direction == GTK_DIR_TAB_BACKWARD)
1534 children = g_list_reverse (children);
1536 return gtk_container_focus_move (container, children, direction);
1540 gtk_container_focus_up_down (GtkContainer *container,
1542 GtkDirectionType direction)
1553 /* return failure if there isn't a focus child */
1554 if (container->focus_child)
1556 focus_width = container->focus_child->allocation.width / 2;
1557 focus_x = container->focus_child->allocation.x + focus_width;
1561 focus_width = GTK_WIDGET (container)->allocation.width;
1562 if (GTK_WIDGET_NO_WINDOW (container))
1563 focus_x = GTK_WIDGET (container)->allocation.x;
1568 length = g_list_length (children);
1570 /* sort the children in the y direction */
1571 for (i = 1; i < length; i++)
1574 tmp_list = g_list_nth (children, j);
1575 child = tmp_list->data;
1579 child2 = tmp_list->prev->data;
1580 if (child->allocation.y < child2->allocation.y)
1582 tmp_list->data = tmp_list->prev->data;
1583 tmp_list = tmp_list->prev;
1590 tmp_list->data = child;
1593 /* sort the children in distance in the x direction
1594 * in distance from the current focus child while maintaining the
1595 * sort in the y direction
1597 for (i = 1; i < length; i++)
1600 tmp_list = g_list_nth (children, j);
1601 child = tmp_list->data;
1602 dist1 = (child->allocation.x + child->allocation.width / 2) - focus_x;
1606 child2 = tmp_list->prev->data;
1607 dist2 = (child2->allocation.x + child2->allocation.width / 2) - focus_x;
1609 if ((dist1 < dist2) &&
1610 (child->allocation.y >= child2->allocation.y))
1612 tmp_list->data = tmp_list->prev->data;
1613 tmp_list = tmp_list->prev;
1620 tmp_list->data = child;
1623 /* go and invalidate any widget which is too
1624 * far from the focus widget.
1626 if (!container->focus_child &&
1627 (direction == GTK_DIR_UP))
1628 focus_x += focus_width;
1630 tmp_list = children;
1633 child = tmp_list->data;
1635 dist1 = (child->allocation.x + child->allocation.width / 2) - focus_x;
1636 if (((direction == GTK_DIR_DOWN) && (dist1 < 0)) ||
1637 ((direction == GTK_DIR_UP) && (dist1 > 0)))
1638 tmp_list->data = NULL;
1640 tmp_list = tmp_list->next;
1643 if (direction == GTK_DIR_UP)
1644 children = g_list_reverse (children);
1646 return gtk_container_focus_move (container, children, direction);
1650 gtk_container_focus_left_right (GtkContainer *container,
1652 GtkDirectionType direction)
1663 /* return failure if there isn't a focus child */
1664 if (container->focus_child)
1666 focus_height = container->focus_child->allocation.height / 2;
1667 focus_y = container->focus_child->allocation.y + focus_height;
1671 focus_height = GTK_WIDGET (container)->allocation.height;
1672 if (GTK_WIDGET_NO_WINDOW (container))
1673 focus_y = GTK_WIDGET (container)->allocation.y;
1678 length = g_list_length (children);
1680 /* sort the children in the x direction */
1681 for (i = 1; i < length; i++)
1684 tmp_list = g_list_nth (children, j);
1685 child = tmp_list->data;
1689 child2 = tmp_list->prev->data;
1690 if (child->allocation.x < child2->allocation.x)
1692 tmp_list->data = tmp_list->prev->data;
1693 tmp_list = tmp_list->prev;
1700 tmp_list->data = child;
1703 /* sort the children in distance in the y direction
1704 * in distance from the current focus child while maintaining the
1705 * sort in the x direction
1707 for (i = 1; i < length; i++)
1710 tmp_list = g_list_nth (children, j);
1711 child = tmp_list->data;
1712 dist1 = (child->allocation.y + child->allocation.height / 2) - focus_y;
1716 child2 = tmp_list->prev->data;
1717 dist2 = (child2->allocation.y + child2->allocation.height / 2) - focus_y;
1719 if ((dist1 < dist2) &&
1720 (child->allocation.x >= child2->allocation.x))
1722 tmp_list->data = tmp_list->prev->data;
1723 tmp_list = tmp_list->prev;
1730 tmp_list->data = child;
1733 /* go and invalidate any widget which is too
1734 * far from the focus widget.
1736 if (!container->focus_child &&
1737 (direction == GTK_DIR_LEFT))
1738 focus_y += focus_height;
1740 tmp_list = children;
1743 child = tmp_list->data;
1745 dist1 = (child->allocation.y + child->allocation.height / 2) - focus_y;
1746 if (((direction == GTK_DIR_RIGHT) && (dist1 < 0)) ||
1747 ((direction == GTK_DIR_LEFT) && (dist1 > 0)))
1748 tmp_list->data = NULL;
1750 tmp_list = tmp_list->next;
1753 if (direction == GTK_DIR_LEFT)
1754 children = g_list_reverse (children);
1756 return gtk_container_focus_move (container, children, direction);
1760 gtk_container_focus_move (GtkContainer *container,
1762 GtkDirectionType direction)
1764 GtkWidget *focus_child;
1767 focus_child = container->focus_child;
1768 gtk_container_set_focus_child (container, NULL);
1772 child = children->data;
1773 children = children->next;
1780 if (focus_child == child)
1784 if (GTK_WIDGET_DRAWABLE (child) &&
1785 GTK_IS_CONTAINER (child) &&
1786 !GTK_WIDGET_HAS_FOCUS (child))
1787 if (gtk_container_focus (GTK_CONTAINER (child), direction))
1791 else if (GTK_WIDGET_DRAWABLE (child))
1793 if (GTK_IS_CONTAINER (child))
1795 if (gtk_container_focus (GTK_CONTAINER (child), direction))
1798 else if (GTK_WIDGET_CAN_FOCUS (child))
1800 gtk_widget_grab_focus (child);
1811 gtk_container_children_callback (GtkWidget *widget,
1812 gpointer client_data)
1816 children = (GList**) client_data;
1817 *children = g_list_prepend (*children, widget);
1821 gtk_container_set_focus_vadjustment (GtkContainer *container,
1822 GtkAdjustment *adjustment)
1824 g_return_if_fail (container != NULL);
1825 g_return_if_fail (GTK_IS_CONTAINER (container));
1827 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1830 gtk_object_ref (GTK_OBJECT(adjustment));
1832 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
1835 (GtkDestroyNotify) gtk_object_unref);
1839 gtk_container_set_focus_hadjustment (GtkContainer *container,
1840 GtkAdjustment *adjustment)
1842 g_return_if_fail (container != NULL);
1843 g_return_if_fail (GTK_IS_CONTAINER (container));
1845 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1848 gtk_object_ref (GTK_OBJECT (adjustment));
1850 gtk_object_set_data_by_id_full (GTK_OBJECT (container),
1853 (GtkDestroyNotify) gtk_object_unref);
1858 gtk_container_show_all (GtkWidget *widget)
1860 g_return_if_fail (widget != NULL);
1861 g_return_if_fail (GTK_IS_CONTAINER (widget));
1863 gtk_container_foreach (GTK_CONTAINER (widget),
1864 (GtkCallback) gtk_widget_show_all,
1866 gtk_widget_show (widget);
1870 gtk_container_hide_all (GtkWidget *widget)
1872 g_return_if_fail (widget != NULL);
1873 g_return_if_fail (GTK_IS_CONTAINER (widget));
1875 gtk_widget_hide (widget);
1876 gtk_container_foreach (GTK_CONTAINER (widget),
1877 (GtkCallback) gtk_widget_hide_all,