1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2001 Red Hat Software
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gtkcontainer.h"
24 #include "gtkprivate.h"
25 #include "gtksizegroup.h"
34 static void gtk_size_group_set_property (GObject *object,
38 static void gtk_size_group_get_property (GObject *object,
43 static void add_group_to_closure (GtkSizeGroup *group,
44 GtkSizeGroupMode mode,
47 static void add_widget_to_closure (GtkWidget *widget,
48 GtkSizeGroupMode mode,
52 static GQuark size_groups_quark;
53 static const gchar size_groups_tag[] = "gtk-size-groups";
55 static GQuark visited_quark;
56 static const gchar visited_tag[] = "gtk-size-group-visited";
59 get_size_groups (GtkWidget *widget)
61 return g_object_get_qdata (G_OBJECT (widget), size_groups_quark);
65 set_size_groups (GtkWidget *widget,
68 g_object_set_qdata (G_OBJECT (widget), size_groups_quark, groups);
72 mark_visited (gpointer object)
74 g_object_set_qdata (object, visited_quark, "visited");
78 mark_unvisited (gpointer object)
80 g_object_set_qdata (object, visited_quark, NULL);
84 is_visited (gpointer object)
86 return g_object_get_qdata (object, visited_quark) != NULL;
90 add_group_to_closure (GtkSizeGroup *group,
91 GtkSizeGroupMode mode,
97 *groups = g_slist_prepend (*groups, group);
100 tmp_widgets = group->widgets;
103 GtkWidget *tmp_widget = tmp_widgets->data;
105 if (!is_visited (tmp_widget))
106 add_widget_to_closure (tmp_widget, mode, groups, widgets);
108 tmp_widgets = tmp_widgets->next;
113 add_widget_to_closure (GtkWidget *widget,
114 GtkSizeGroupMode mode,
120 *widgets = g_slist_prepend (*widgets, widget);
121 mark_visited (widget);
123 tmp_groups = get_size_groups (widget);
126 GtkSizeGroup *tmp_group = tmp_groups->data;
128 if ((tmp_group->mode == GTK_SIZE_GROUP_BOTH || tmp_group->mode == mode) &&
129 !is_visited (tmp_group))
130 add_group_to_closure (tmp_group, mode, groups, widgets);
132 tmp_groups = tmp_groups->next;
137 real_queue_resize (GtkWidget *widget)
139 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
140 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
143 _gtk_container_queue_resize (GTK_CONTAINER (widget->parent));
144 else if (GTK_WIDGET_TOPLEVEL (widget) && GTK_IS_CONTAINER (widget))
145 _gtk_container_queue_resize (GTK_CONTAINER (widget));
149 reset_group_sizes (GSList *groups)
151 GSList *tmp_list = groups;
154 GtkSizeGroup *tmp_group = tmp_list->data;
156 tmp_group->have_width = FALSE;
157 tmp_group->have_height = FALSE;
159 tmp_list = tmp_list->next;
164 queue_resize_on_widget (GtkWidget *widget,
165 gboolean check_siblings)
167 GtkWidget *parent = widget;
172 GSList *widget_groups;
176 if (widget == parent && !check_siblings)
178 real_queue_resize (widget);
179 parent = parent->parent;
183 widget_groups = get_size_groups (parent);
186 if (widget == parent)
187 real_queue_resize (widget);
189 parent = parent->parent;
196 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
197 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
198 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
200 reset_group_sizes (groups);
205 if (tmp_list->data == parent)
207 if (widget == parent)
208 real_queue_resize (parent);
211 queue_resize_on_widget (tmp_list->data, FALSE);
213 tmp_list = tmp_list->next;
216 g_slist_free (widgets);
217 g_slist_free (groups);
222 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
223 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
224 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
226 reset_group_sizes (groups);
231 if (tmp_list->data == parent)
233 if (widget == parent)
234 real_queue_resize (parent);
237 queue_resize_on_widget (tmp_list->data, FALSE);
239 tmp_list = tmp_list->next;
242 g_slist_free (widgets);
243 g_slist_free (groups);
245 parent = parent->parent;
250 queue_resize_on_group (GtkSizeGroup *size_group)
252 if (size_group->widgets)
253 queue_resize_on_widget (size_group->widgets->data, TRUE);
257 initialize_size_group_quarks (void)
259 if (!size_groups_quark)
261 size_groups_quark = g_quark_from_static_string (size_groups_tag);
262 visited_quark = g_quark_from_string (visited_tag);
267 gtk_size_group_class_init (GtkSizeGroupClass *klass)
269 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
271 gobject_class->set_property = gtk_size_group_set_property;
272 gobject_class->get_property = gtk_size_group_get_property;
274 g_object_class_install_property (gobject_class,
276 g_param_spec_enum ("mode",
278 P_("The directions in which the size group affects the requested sizes"
279 " of its component widgets"),
280 GTK_TYPE_SIZE_GROUP_MODE,
281 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
284 * GtkSizeGroup:ignore-hidden:
286 * If %TRUE, unmapped widgets are ignored when determining
287 * the size of the group.
291 g_object_class_install_property (gobject_class,
293 g_param_spec_boolean ("ignore-hidden",
295 P_("If TRUE, unmapped widgets are ignored "
296 "when determining the size of the group"),
298 GTK_PARAM_READWRITE));
300 initialize_size_group_quarks ();
304 gtk_size_group_init (GtkSizeGroup *size_group)
306 size_group->widgets = NULL;
307 size_group->mode = GTK_SIZE_GROUP_HORIZONTAL;
308 size_group->have_width = 0;
309 size_group->have_height = 0;
310 size_group->ignore_hidden = 0;
313 G_DEFINE_TYPE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT)
316 gtk_size_group_set_property (GObject *object,
321 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
326 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
328 case PROP_IGNORE_HIDDEN:
329 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 gtk_size_group_get_property (GObject *object,
343 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
348 g_value_set_enum (value, size_group->mode);
350 case PROP_IGNORE_HIDDEN:
351 g_value_set_boolean (value, size_group->ignore_hidden);
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 * gtk_size_group_new:
361 * @mode: the mode for the new size group.
363 * Create a new #GtkSizeGroup.
365 * Return value: a newly created #GtkSizeGroup
368 gtk_size_group_new (GtkSizeGroupMode mode)
370 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
372 size_group->mode = mode;
378 * gtk_size_group_set_mode:
379 * @size_group: a #GtkSizeGroup
380 * @mode: the mode to set for the size group.
382 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
383 * group determines whether the widgets in the size group should
384 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
385 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
386 * or should all have the same requisition in both directions
387 * (%GTK_SIZE_GROUP_MODE_BOTH).
390 gtk_size_group_set_mode (GtkSizeGroup *size_group,
391 GtkSizeGroupMode mode)
393 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
395 if (size_group->mode != mode)
397 if (size_group->mode != GTK_SIZE_GROUP_NONE)
398 queue_resize_on_group (size_group);
399 size_group->mode = mode;
400 if (size_group->mode != GTK_SIZE_GROUP_NONE)
401 queue_resize_on_group (size_group);
403 g_object_notify (G_OBJECT (size_group), "mode");
408 * gtk_size_group_get_mode:
409 * @size_group: a #GtkSizeGroup
411 * Gets the current mode of the size group. See gtk_size_group_set_mode().
413 * Return value: the current mode of the size group.
416 gtk_size_group_get_mode (GtkSizeGroup *size_group)
418 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
420 return size_group->mode;
424 * gtk_size_group_set_ignore_hidden:
425 * @size_group: a #GtkSizeGroup
426 * @ignore_hidden: whether unmapped widgets should be ignored
427 * when calculating the size
429 * Sets whether unmapped widgets should be ignored when
430 * calculating the size.
435 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
436 gboolean ignore_hidden)
438 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
440 ignore_hidden = ignore_hidden != FALSE;
442 if (size_group->ignore_hidden != ignore_hidden)
444 size_group->ignore_hidden = ignore_hidden;
446 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
451 * gtk_size_group_get_ignore_hidden:
452 * @size_group: a #GtkSizeGroup
454 * Returns if invisible widgets are ignored when calculating the size.
456 * Returns: %TRUE if invisible widgets are ignored.
461 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
463 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
465 return size_group->ignore_hidden;
469 gtk_size_group_widget_destroyed (GtkWidget *widget,
470 GtkSizeGroup *size_group)
472 gtk_size_group_remove_widget (size_group, widget);
476 * gtk_size_group_add_widget:
477 * @size_group: a #GtkSizeGroup
478 * @widget: the #GtkWidget to add
480 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
481 * of the widget will be determined as the maximum of its requisition
482 * and the requisition of the other widgets in the size group.
483 * Whether this applies horizontally, vertically, or in both directions
484 * depends on the mode of the size group. See gtk_size_group_set_mode().
487 gtk_size_group_add_widget (GtkSizeGroup *size_group,
492 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
493 g_return_if_fail (GTK_IS_WIDGET (widget));
495 groups = get_size_groups (widget);
497 if (!g_slist_find (groups, size_group))
499 groups = g_slist_prepend (groups, size_group);
500 set_size_groups (widget, groups);
502 size_group->widgets = g_slist_prepend (size_group->widgets, widget);
504 g_signal_connect (widget, "destroy",
505 G_CALLBACK (gtk_size_group_widget_destroyed),
508 g_object_ref (size_group);
511 queue_resize_on_group (size_group);
515 * gtk_size_group_remove_widget:
516 * @size_group: a #GtkSizeGrup
517 * @widget: the #GtkWidget to remove
519 * Removes a widget from a #GtkSizeGroup.
522 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
527 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
528 g_return_if_fail (GTK_IS_WIDGET (widget));
529 g_return_if_fail (g_slist_find (size_group->widgets, widget));
531 g_signal_handlers_disconnect_by_func (widget,
532 gtk_size_group_widget_destroyed,
535 groups = get_size_groups (widget);
536 groups = g_slist_remove (groups, size_group);
537 set_size_groups (widget, groups);
539 size_group->widgets = g_slist_remove (size_group->widgets, widget);
540 queue_resize_on_group (size_group);
541 gtk_widget_queue_resize (widget);
543 g_object_unref (size_group);
547 * gtk_size_group_get_widgets:
548 * @size_group: a #GtkSizeGrup
550 * Returns the list of widgets associated with @size_group.
552 * Return value: a #GSList of widgets. The list is owned by GTK+
553 * and should not be modified.
558 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
560 return size_group->widgets;
564 get_base_dimension (GtkWidget *widget,
565 GtkSizeGroupMode mode)
567 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
569 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
571 if (aux_info && aux_info->width > 0)
572 return aux_info->width;
574 return widget->requisition.width;
578 if (aux_info && aux_info->height > 0)
579 return aux_info->height;
581 return widget->requisition.height;
586 do_size_request (GtkWidget *widget)
588 if (GTK_WIDGET_REQUEST_NEEDED (widget))
590 gtk_widget_ensure_style (widget);
591 GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
592 g_signal_emit_by_name (widget,
594 &widget->requisition);
599 compute_base_dimension (GtkWidget *widget,
600 GtkSizeGroupMode mode)
602 do_size_request (widget);
604 return get_base_dimension (widget, mode);
608 compute_dimension (GtkWidget *widget,
609 GtkSizeGroupMode mode)
611 GSList *widgets = NULL;
612 GSList *groups = NULL;
616 add_widget_to_closure (widget, mode, &groups, &widgets);
618 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
619 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
621 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
625 result = compute_base_dimension (widget, mode);
629 GtkSizeGroup *group = groups->data;
631 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
632 result = group->requisition.width;
633 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
634 result = group->requisition.height;
640 GtkWidget *tmp_widget = tmp_list->data;
642 gint dimension = compute_base_dimension (tmp_widget, mode);
644 if (GTK_WIDGET_MAPPED (tmp_widget) || !group->ignore_hidden)
646 if (dimension > result)
650 tmp_list = tmp_list->next;
656 GtkSizeGroup *tmp_group = tmp_list->data;
658 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
660 tmp_group->have_width = TRUE;
661 tmp_group->requisition.width = result;
665 tmp_group->have_height = TRUE;
666 tmp_group->requisition.height = result;
669 tmp_list = tmp_list->next;
674 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
676 g_slist_free (widgets);
677 g_slist_free (groups);
683 get_dimension (GtkWidget *widget,
684 GtkSizeGroupMode mode)
686 GSList *widgets = NULL;
687 GSList *groups = NULL;
690 add_widget_to_closure (widget, mode, &groups, &widgets);
692 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
693 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
697 result = get_base_dimension (widget, mode);
701 GtkSizeGroup *group = groups->data;
703 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
704 result = group->requisition.width;
705 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
706 result = group->requisition.height;
709 g_slist_free (widgets);
710 g_slist_free (groups);
716 get_fast_child_requisition (GtkWidget *widget,
717 GtkRequisition *requisition)
719 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
721 *requisition = widget->requisition;
725 if (aux_info->width > 0)
726 requisition->width = aux_info->width;
727 if (aux_info && aux_info->height > 0)
728 requisition->height = aux_info->height;
733 * _gtk_size_group_get_child_requisition:
734 * @widget: a #GtkWidget
735 * @requisition: location to store computed requisition.
737 * Retrieve the "child requisition" of the widget, taking account grouping
738 * of the widget's requisition with other widgets.
741 _gtk_size_group_get_child_requisition (GtkWidget *widget,
742 GtkRequisition *requisition)
744 initialize_size_group_quarks ();
748 if (get_size_groups (widget))
750 requisition->width = get_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
751 requisition->height = get_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
753 /* Only do the full computation if we actually have size groups */
756 get_fast_child_requisition (widget, requisition);
761 * _gtk_size_group_compute_requisition:
762 * @widget: a #GtkWidget
763 * @requisition: location to store computed requisition.
765 * Compute the requisition of a widget taking into account grouping of
766 * the widget's requisition with other widgets.
769 _gtk_size_group_compute_requisition (GtkWidget *widget,
770 GtkRequisition *requisition)
775 initialize_size_group_quarks ();
777 if (get_size_groups (widget))
779 /* Only do the full computation if we actually have size groups */
781 width = compute_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
782 height = compute_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
786 requisition->width = width;
787 requisition->height = height;
792 do_size_request (widget);
795 get_fast_child_requisition (widget, requisition);
800 * _gtk_size_group_queue_resize:
801 * @widget: a #GtkWidget
803 * Queue a resize on a widget, and on all other widgets grouped with this widget.
806 _gtk_size_group_queue_resize (GtkWidget *widget)
808 initialize_size_group_quarks ();
810 queue_resize_on_widget (widget, TRUE);
813 #define __GTK_SIZE_GROUP_C__
814 #include "gtkaliasdef.c"