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, see <http://www.gnu.org/licenses/>.
22 #include "gtkbuildable.h"
23 #include "gtkcontainer.h"
25 #include "gtktypebuiltins.h"
26 #include "gtkprivate.h"
27 #include "gtksizegroup-private.h"
28 #include "gtkwidgetprivate.h"
29 #include "gtkcontainerprivate.h"
33 * SECTION:gtksizegroup
34 * @Short_description: Grouping widgets so they request the same size
35 * @Title: GtkSizeGroup
37 * #GtkSizeGroup provides a mechanism for grouping a number of widgets
38 * together so they all request the same amount of space. This is
39 * typically useful when you want a column of widgets to have the same
40 * size, but you can't use a #GtkGrid widget.
42 * In detail, the size requested for each widget in a #GtkSizeGroup is
43 * the maximum of the sizes that would have been requested for each
44 * widget in the size group if they were not in the size group. The mode
45 * of the size group (see gtk_size_group_set_mode()) determines whether
46 * this applies to the horizontal size, the vertical size, or both sizes.
48 * Note that size groups only affect the amount of space requested, not
49 * the size that the widgets finally receive. If you want the widgets in
50 * a #GtkSizeGroup to actually be the same size, you need to pack them in
51 * such a way that they get the size they request and not more. For
52 * example, if you are packing your widgets into a table, you would not
53 * include the %GTK_FILL flag.
55 * #GtkSizeGroup objects are referenced by each widget in the size group,
56 * so once you have added all widgets to a #GtkSizeGroup, you can drop
57 * the initial reference to the size group with g_object_unref(). If the
58 * widgets in the size group are subsequently destroyed, then they will
59 * be removed from the size group and drop their references on the size
60 * group; when all widgets have been removed, the size group will be
63 * Widgets can be part of multiple size groups; GTK+ will compute the
64 * horizontal size of a widget from the horizontal requisition of all
65 * widgets that can be reached from the widget by a chain of size groups
66 * of type %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_BOTH, and the
67 * vertical size from the vertical requisition of all widgets that can be
68 * reached from the widget by a chain of size groups of type
69 * %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH.
71 * Note that only non-contextual sizes of every widget are ever consulted
72 * by size groups (since size groups have no knowledge of what size a widget
73 * will be allocated in one dimension, it cannot derive how much height
74 * a widget will receive for a given width). When grouping widgets that
75 * trade height for width in mode %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH:
76 * the height for the minimum width will be the requested height for all
77 * widgets in the group. The same is of course true when horizontally grouping
78 * width for height widgets.
80 * Widgets that trade height-for-width should set a reasonably large minimum width
81 * by way of #GtkLabel:width-chars for instance. Widgets with static sizes as well
82 * as widgets that grow (such as ellipsizing text) need no such considerations.
84 * <refsect2 id="GtkSizeGroup-BUILDER-UI">
85 * <title>GtkSizeGroup as GtkBuildable</title>
87 * Size groups can be specified in a UI definition by placing an
88 * <object> element with <literal>class="GtkSizeGroup"</literal>
89 * somewhere in the UI definition. The widgets that belong to the
90 * size group are specified by a <widgets> element that may
91 * contain multiple <widget> elements, one for each member
92 * of the size group. The name attribute gives the id of the widget.
95 * <title>A UI definition fragment with GtkSizeGroup</title>
96 * <programlisting><![CDATA[
97 * <object class="GtkSizeGroup">
98 * <property name="mode">GTK_SIZE_GROUP_HORIZONTAL</property>
100 * <widget name="radio1"/>
101 * <widget name="radio2"/>
104 * ]]></programlisting>
111 struct _GtkSizeGroupPrivate
117 guint ignore_hidden : 1;
127 static void gtk_size_group_set_property (GObject *object,
131 static void gtk_size_group_get_property (GObject *object,
136 static void add_group_to_closure (GtkSizeGroup *group,
137 GtkSizeGroupMode mode,
140 static void add_widget_to_closure (GtkWidget *widget,
141 GtkSizeGroupMode mode,
146 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
147 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
150 const gchar *tagname,
151 GMarkupParser *parser,
153 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
156 const gchar *tagname,
160 mark_group_unvisited (GtkSizeGroup *group)
162 group->priv->visited = FALSE;
166 mark_widget_unvisited (GtkWidget *widget)
168 _gtk_widget_set_sizegroup_visited (widget, FALSE);
172 add_group_to_closure (GtkSizeGroup *group,
173 GtkSizeGroupMode mode,
177 GtkSizeGroupPrivate *priv = group->priv;
180 *groups = g_slist_prepend (*groups, group);
181 priv->visited = TRUE;
183 tmp_widgets = priv->widgets;
186 GtkWidget *tmp_widget = tmp_widgets->data;
188 if (!_gtk_widget_get_sizegroup_visited (tmp_widget))
189 add_widget_to_closure (tmp_widget, mode, groups, widgets);
191 tmp_widgets = tmp_widgets->next;
196 add_widget_to_closure (GtkWidget *widget,
197 GtkSizeGroupMode mode,
203 *widgets = g_slist_prepend (*widgets, widget);
204 _gtk_widget_set_sizegroup_visited (widget, TRUE);
206 tmp_groups = _gtk_widget_get_sizegroups (widget);
209 GtkSizeGroup *tmp_group = tmp_groups->data;
210 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
212 if ((tmp_priv->mode == GTK_SIZE_GROUP_BOTH || tmp_priv->mode == mode) &&
213 !tmp_group->priv->visited)
214 add_group_to_closure (tmp_group, mode, groups, widgets);
216 tmp_groups = tmp_groups->next;
221 real_queue_resize (GtkWidget *widget,
222 GtkQueueResizeFlags flags)
224 GtkWidget *container;
226 _gtk_widget_set_alloc_needed (widget, TRUE);
227 _gtk_widget_set_width_request_needed (widget, TRUE);
228 _gtk_widget_set_height_request_needed (widget, TRUE);
230 container = gtk_widget_get_parent (widget);
232 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
237 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
238 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
240 _gtk_container_queue_resize (GTK_CONTAINER (container));
245 queue_resize_on_widget (GtkWidget *widget,
246 gboolean check_siblings,
247 GtkQueueResizeFlags flags)
249 GtkWidget *parent = widget;
254 GSList *widget_groups;
258 if (widget == parent && !check_siblings)
260 real_queue_resize (widget, flags);
261 parent = gtk_widget_get_parent (parent);
265 widget_groups = _gtk_widget_get_sizegroups (parent);
268 if (widget == parent)
269 real_queue_resize (widget, flags);
271 parent = gtk_widget_get_parent (parent);
278 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
279 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
280 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
285 if (tmp_list->data == parent)
287 if (widget == parent)
288 real_queue_resize (parent, flags);
290 else if (tmp_list->data == widget)
292 g_warning ("A container and its child are part of this SizeGroup");
295 queue_resize_on_widget (tmp_list->data, FALSE, flags);
297 tmp_list = tmp_list->next;
300 g_slist_free (widgets);
301 g_slist_free (groups);
306 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
307 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
308 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
313 if (tmp_list->data == parent)
315 if (widget == parent)
316 real_queue_resize (parent, flags);
318 else if (tmp_list->data == widget)
320 g_warning ("A container and its child are part of this SizeGroup");
323 queue_resize_on_widget (tmp_list->data, FALSE, flags);
325 tmp_list = tmp_list->next;
328 g_slist_free (widgets);
329 g_slist_free (groups);
331 parent = gtk_widget_get_parent (parent);
336 queue_resize_on_group (GtkSizeGroup *size_group)
338 GtkSizeGroupPrivate *priv = size_group->priv;
341 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
345 gtk_size_group_class_init (GtkSizeGroupClass *klass)
347 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
349 gobject_class->set_property = gtk_size_group_set_property;
350 gobject_class->get_property = gtk_size_group_get_property;
352 g_object_class_install_property (gobject_class,
354 g_param_spec_enum ("mode",
356 P_("The directions in which the size group affects the requested sizes"
357 " of its component widgets"),
358 GTK_TYPE_SIZE_GROUP_MODE,
359 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
362 * GtkSizeGroup:ignore-hidden:
364 * If %TRUE, unmapped widgets are ignored when determining
365 * the size of the group.
369 g_object_class_install_property (gobject_class,
371 g_param_spec_boolean ("ignore-hidden",
373 P_("If TRUE, unmapped widgets are ignored "
374 "when determining the size of the group"),
376 GTK_PARAM_READWRITE));
378 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
382 gtk_size_group_init (GtkSizeGroup *size_group)
384 GtkSizeGroupPrivate *priv;
386 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
388 GtkSizeGroupPrivate);
389 priv = size_group->priv;
391 priv->widgets = NULL;
392 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
393 priv->ignore_hidden = FALSE;
394 priv->visited = FALSE;
398 gtk_size_group_buildable_init (GtkBuildableIface *iface)
400 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
401 iface->custom_finished = gtk_size_group_buildable_custom_finished;
404 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
405 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
406 gtk_size_group_buildable_init))
409 gtk_size_group_set_property (GObject *object,
414 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
419 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
421 case PROP_IGNORE_HIDDEN:
422 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 gtk_size_group_get_property (GObject *object,
436 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
437 GtkSizeGroupPrivate *priv = size_group->priv;
442 g_value_set_enum (value, priv->mode);
444 case PROP_IGNORE_HIDDEN:
445 g_value_set_boolean (value, priv->ignore_hidden);
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454 * gtk_size_group_new:
455 * @mode: the mode for the new size group.
457 * Create a new #GtkSizeGroup.
459 * Return value: a newly created #GtkSizeGroup
462 gtk_size_group_new (GtkSizeGroupMode mode)
464 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
465 GtkSizeGroupPrivate *priv = size_group->priv;
473 * gtk_size_group_set_mode:
474 * @size_group: a #GtkSizeGroup
475 * @mode: the mode to set for the size group.
477 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
478 * group determines whether the widgets in the size group should
479 * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
480 * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
481 * or should all have the same requisition in both directions
482 * (%GTK_SIZE_GROUP_BOTH).
485 gtk_size_group_set_mode (GtkSizeGroup *size_group,
486 GtkSizeGroupMode mode)
488 GtkSizeGroupPrivate *priv;
490 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
492 priv = size_group->priv;
494 if (priv->mode != mode)
496 if (priv->mode != GTK_SIZE_GROUP_NONE)
497 queue_resize_on_group (size_group);
499 if (priv->mode != GTK_SIZE_GROUP_NONE)
500 queue_resize_on_group (size_group);
502 g_object_notify (G_OBJECT (size_group), "mode");
507 * gtk_size_group_get_mode:
508 * @size_group: a #GtkSizeGroup
510 * Gets the current mode of the size group. See gtk_size_group_set_mode().
512 * Return value: the current mode of the size group.
515 gtk_size_group_get_mode (GtkSizeGroup *size_group)
517 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
519 return size_group->priv->mode;
523 * gtk_size_group_set_ignore_hidden:
524 * @size_group: a #GtkSizeGroup
525 * @ignore_hidden: whether unmapped widgets should be ignored
526 * when calculating the size
528 * Sets whether unmapped widgets should be ignored when
529 * calculating the size.
534 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
535 gboolean ignore_hidden)
537 GtkSizeGroupPrivate *priv;
539 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
541 priv = size_group->priv;
543 ignore_hidden = ignore_hidden != FALSE;
545 if (priv->ignore_hidden != ignore_hidden)
547 priv->ignore_hidden = ignore_hidden;
549 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
554 * gtk_size_group_get_ignore_hidden:
555 * @size_group: a #GtkSizeGroup
557 * Returns if invisible widgets are ignored when calculating the size.
559 * Returns: %TRUE if invisible widgets are ignored.
564 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
566 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
568 return size_group->priv->ignore_hidden;
572 gtk_size_group_widget_destroyed (GtkWidget *widget,
573 GtkSizeGroup *size_group)
575 gtk_size_group_remove_widget (size_group, widget);
579 * gtk_size_group_add_widget:
580 * @size_group: a #GtkSizeGroup
581 * @widget: the #GtkWidget to add
583 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
584 * of the widget will be determined as the maximum of its requisition
585 * and the requisition of the other widgets in the size group.
586 * Whether this applies horizontally, vertically, or in both directions
587 * depends on the mode of the size group. See gtk_size_group_set_mode().
589 * When the widget is destroyed or no longer referenced elsewhere, it will
590 * be removed from the size group.
593 gtk_size_group_add_widget (GtkSizeGroup *size_group,
596 GtkSizeGroupPrivate *priv;
599 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
600 g_return_if_fail (GTK_IS_WIDGET (widget));
602 priv = size_group->priv;
604 groups = _gtk_widget_get_sizegroups (widget);
606 if (!g_slist_find (groups, size_group))
608 _gtk_widget_add_sizegroup (widget, size_group);
610 priv->widgets = g_slist_prepend (priv->widgets, widget);
612 g_signal_connect (widget, "destroy",
613 G_CALLBACK (gtk_size_group_widget_destroyed),
616 g_object_ref (size_group);
619 queue_resize_on_group (size_group);
623 * gtk_size_group_remove_widget:
624 * @size_group: a #GtkSizeGroup
625 * @widget: the #GtkWidget to remove
627 * Removes a widget from a #GtkSizeGroup.
630 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
633 GtkSizeGroupPrivate *priv;
635 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
636 g_return_if_fail (GTK_IS_WIDGET (widget));
638 priv = size_group->priv;
640 g_return_if_fail (g_slist_find (priv->widgets, widget));
642 g_signal_handlers_disconnect_by_func (widget,
643 gtk_size_group_widget_destroyed,
646 _gtk_widget_remove_sizegroup (widget, size_group);
648 priv->widgets = g_slist_remove (priv->widgets, widget);
649 queue_resize_on_group (size_group);
650 gtk_widget_queue_resize (widget);
652 g_object_unref (size_group);
656 * gtk_size_group_get_widgets:
657 * @size_group: a #GtkSizeGroup
659 * Returns the list of widgets associated with @size_group.
661 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
662 * widgets. The list is owned by GTK+ and should not be modified.
667 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
669 return size_group->priv->widgets;
673 compute_dimension (GtkWidget *widget,
674 GtkSizeGroupMode mode,
675 gint *minimum, /* in-out */
676 gint *natural) /* in-out */
678 GSList *widgets = NULL;
679 GSList *groups = NULL;
681 gint min_result = 0, nat_result = 0;
683 add_widget_to_closure (widget, mode, &groups, &widgets);
684 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
685 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
687 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
691 min_result = *minimum;
692 nat_result = *natural;
696 GtkSizeGroup *group = groups->data;
697 GtkSizeGroupPrivate *priv = group->priv;
702 GtkWidget *tmp_widget = tmp_list->data;
703 gint min_dimension, nat_dimension;
705 if (tmp_widget == widget)
707 min_dimension = *minimum;
708 nat_dimension = *natural;
710 else if (!gtk_widget_get_mapped (tmp_widget) && priv->ignore_hidden)
717 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
718 gtk_widget_get_preferred_width (tmp_widget, &min_dimension, &nat_dimension);
720 gtk_widget_get_preferred_height (tmp_widget, &min_dimension, &nat_dimension);
723 min_result = MAX (min_result, min_dimension);
724 nat_result = MAX (nat_result, nat_dimension);
726 tmp_list = tmp_list->next;
730 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
732 g_slist_free (widgets);
733 g_slist_free (groups);
735 *minimum = min_result;
736 *natural = nat_result;
740 * _gtk_size_group_bump_requisition:
741 * @widget: a #GtkWidget
742 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
743 * on the dimension in which to bump the size.
744 * @minimum: a pointer to the widget's minimum size
745 * @natural: a pointer to the widget's natural size
747 * Refreshes the sizegroup while returning the groups requested
748 * value in the dimension @mode.
750 * This function is used both to update sizegroup minimum and natural size
751 * information and widget minimum and natural sizes in multiple passes from
752 * the size request apis.
755 _gtk_size_group_bump_requisition (GtkWidget *widget,
756 GtkSizeGroupMode mode,
760 if (!_gtk_widget_get_sizegroup_bumping (widget))
762 /* Avoid recursion here */
763 _gtk_widget_set_sizegroup_bumping (widget, TRUE);
765 if (_gtk_widget_get_sizegroups (widget))
766 compute_dimension (widget, mode, minimum, natural);
768 _gtk_widget_set_sizegroup_bumping (widget, FALSE);
775 * _gtk_size_group_queue_resize:
776 * @widget: a #GtkWidget
778 * Queue a resize on a widget, and on all other widgets grouped with this widget.
781 _gtk_size_group_queue_resize (GtkWidget *widget,
782 GtkQueueResizeFlags flags)
784 queue_resize_on_widget (widget, TRUE, flags);
790 } GSListSubParserData;
793 size_group_start_element (GMarkupParseContext *context,
794 const gchar *element_name,
796 const gchar **values,
801 GSListSubParserData *data = (GSListSubParserData*)user_data;
803 if (strcmp (element_name, "widget") == 0)
804 for (i = 0; names[i]; i++)
805 if (strcmp (names[i], "name") == 0)
806 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
807 else if (strcmp (element_name, "widgets") == 0)
810 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
815 static const GMarkupParser size_group_parser =
817 size_group_start_element
821 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
824 const gchar *tagname,
825 GMarkupParser *parser,
828 GSListSubParserData *parser_data;
833 if (strcmp (tagname, "widgets") == 0)
835 parser_data = g_slice_new0 (GSListSubParserData);
836 parser_data->items = NULL;
837 parser_data->object = G_OBJECT (buildable);
839 *parser = size_group_parser;
848 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
851 const gchar *tagname,
855 GSListSubParserData *data;
858 if (strcmp (tagname, "widgets"))
861 data = (GSListSubParserData*)user_data;
862 data->items = g_slist_reverse (data->items);
864 for (l = data->items; l; l = l->next)
866 object = gtk_builder_get_object (builder, l->data);
869 g_warning ("Unknown object %s specified in sizegroup %s",
870 (const gchar*)l->data,
871 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
874 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
875 GTK_WIDGET (object));
878 g_slist_free (data->items);
879 g_slice_free (GSListSubParserData, data);