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;
126 static void gtk_size_group_set_property (GObject *object,
130 static void gtk_size_group_get_property (GObject *object,
136 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
137 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
140 const gchar *tagname,
141 GMarkupParser *parser,
143 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
146 const gchar *tagname,
150 mark_widget_unvisited (GtkWidget *widget)
152 _gtk_widget_set_sizegroup_visited (widget, FALSE);
156 add_widget_to_closure (GtkWidget *widget,
157 GtkSizeGroupMode mode,
160 GSList *tmp_groups, *tmp_widgets;
163 if (_gtk_widget_get_sizegroup_visited (widget))
166 *widgets = g_slist_prepend (*widgets, widget);
167 _gtk_widget_set_sizegroup_visited (widget, TRUE);
168 hidden = !gtk_widget_is_visible (widget);
170 for (tmp_groups = _gtk_widget_get_sizegroups (widget); tmp_groups; tmp_groups = tmp_groups->next)
172 GtkSizeGroup *tmp_group = tmp_groups->data;
173 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
175 if (tmp_priv->ignore_hidden && hidden)
178 if (tmp_priv->mode != GTK_SIZE_GROUP_BOTH && tmp_priv->mode != mode)
181 for (tmp_widgets = tmp_priv->widgets; tmp_widgets; tmp_widgets = tmp_widgets->next)
182 add_widget_to_closure (tmp_widgets->data, mode, widgets);
187 widget_get_size_group_peers (GtkWidget *widget,
188 GtkSizeGroupMode mode)
190 GSList *result = NULL;
192 add_widget_to_closure (widget, mode, &result);
193 g_slist_foreach (result, (GFunc) mark_widget_unvisited, NULL);
199 real_queue_resize (GtkWidget *widget,
200 GtkQueueResizeFlags flags)
202 GtkWidget *container;
204 _gtk_widget_set_alloc_needed (widget, TRUE);
205 _gtk_widget_set_width_request_needed (widget, TRUE);
206 _gtk_widget_set_height_request_needed (widget, TRUE);
208 container = gtk_widget_get_parent (widget);
210 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
215 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
216 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
218 _gtk_container_queue_resize (GTK_CONTAINER (container));
223 queue_resize_on_widget (GtkWidget *widget,
224 gboolean check_siblings,
225 GtkQueueResizeFlags flags)
227 GtkWidget *parent = widget;
232 GSList *widget_groups;
235 if (widget == parent && !check_siblings)
237 real_queue_resize (widget, flags);
238 parent = gtk_widget_get_parent (parent);
242 widget_groups = _gtk_widget_get_sizegroups (parent);
245 if (widget == parent)
246 real_queue_resize (widget, flags);
248 parent = gtk_widget_get_parent (parent);
252 widgets = widget_get_size_group_peers (parent, GTK_SIZE_GROUP_HORIZONTAL);
254 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
256 if (tmp_list->data == parent)
258 if (widget == parent)
259 real_queue_resize (parent, flags);
261 else if (tmp_list->data == widget)
263 g_warning ("A container and its child are part of this SizeGroup");
266 queue_resize_on_widget (tmp_list->data, FALSE, flags);
269 g_slist_free (widgets);
271 widgets = widget_get_size_group_peers (parent, GTK_SIZE_GROUP_VERTICAL);
273 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
275 if (tmp_list->data == parent)
277 if (widget == parent)
278 real_queue_resize (parent, flags);
280 else if (tmp_list->data == widget)
282 g_warning ("A container and its child are part of this SizeGroup");
285 queue_resize_on_widget (tmp_list->data, FALSE, flags);
288 g_slist_free (widgets);
290 parent = gtk_widget_get_parent (parent);
295 queue_resize_on_group (GtkSizeGroup *size_group)
297 GtkSizeGroupPrivate *priv = size_group->priv;
300 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
304 gtk_size_group_class_init (GtkSizeGroupClass *klass)
306 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
308 gobject_class->set_property = gtk_size_group_set_property;
309 gobject_class->get_property = gtk_size_group_get_property;
311 g_object_class_install_property (gobject_class,
313 g_param_spec_enum ("mode",
315 P_("The directions in which the size group affects the requested sizes"
316 " of its component widgets"),
317 GTK_TYPE_SIZE_GROUP_MODE,
318 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
321 * GtkSizeGroup:ignore-hidden:
323 * If %TRUE, unmapped widgets are ignored when determining
324 * the size of the group.
328 g_object_class_install_property (gobject_class,
330 g_param_spec_boolean ("ignore-hidden",
332 P_("If TRUE, unmapped widgets are ignored "
333 "when determining the size of the group"),
335 GTK_PARAM_READWRITE));
337 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
341 gtk_size_group_init (GtkSizeGroup *size_group)
343 GtkSizeGroupPrivate *priv;
345 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
347 GtkSizeGroupPrivate);
348 priv = size_group->priv;
350 priv->widgets = NULL;
351 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
352 priv->ignore_hidden = FALSE;
356 gtk_size_group_buildable_init (GtkBuildableIface *iface)
358 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
359 iface->custom_finished = gtk_size_group_buildable_custom_finished;
362 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
363 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
364 gtk_size_group_buildable_init))
367 gtk_size_group_set_property (GObject *object,
372 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
377 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
379 case PROP_IGNORE_HIDDEN:
380 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
383 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389 gtk_size_group_get_property (GObject *object,
394 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
395 GtkSizeGroupPrivate *priv = size_group->priv;
400 g_value_set_enum (value, priv->mode);
402 case PROP_IGNORE_HIDDEN:
403 g_value_set_boolean (value, priv->ignore_hidden);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 * gtk_size_group_new:
413 * @mode: the mode for the new size group.
415 * Create a new #GtkSizeGroup.
417 * Return value: a newly created #GtkSizeGroup
420 gtk_size_group_new (GtkSizeGroupMode mode)
422 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
423 GtkSizeGroupPrivate *priv = size_group->priv;
431 * gtk_size_group_set_mode:
432 * @size_group: a #GtkSizeGroup
433 * @mode: the mode to set for the size group.
435 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
436 * group determines whether the widgets in the size group should
437 * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
438 * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
439 * or should all have the same requisition in both directions
440 * (%GTK_SIZE_GROUP_BOTH).
443 gtk_size_group_set_mode (GtkSizeGroup *size_group,
444 GtkSizeGroupMode mode)
446 GtkSizeGroupPrivate *priv;
448 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
450 priv = size_group->priv;
452 if (priv->mode != mode)
454 if (priv->mode != GTK_SIZE_GROUP_NONE)
455 queue_resize_on_group (size_group);
457 if (priv->mode != GTK_SIZE_GROUP_NONE)
458 queue_resize_on_group (size_group);
460 g_object_notify (G_OBJECT (size_group), "mode");
465 * gtk_size_group_get_mode:
466 * @size_group: a #GtkSizeGroup
468 * Gets the current mode of the size group. See gtk_size_group_set_mode().
470 * Return value: the current mode of the size group.
473 gtk_size_group_get_mode (GtkSizeGroup *size_group)
475 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
477 return size_group->priv->mode;
481 * gtk_size_group_set_ignore_hidden:
482 * @size_group: a #GtkSizeGroup
483 * @ignore_hidden: whether unmapped widgets should be ignored
484 * when calculating the size
486 * Sets whether unmapped widgets should be ignored when
487 * calculating the size.
492 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
493 gboolean ignore_hidden)
495 GtkSizeGroupPrivate *priv;
497 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
499 priv = size_group->priv;
501 ignore_hidden = ignore_hidden != FALSE;
503 if (priv->ignore_hidden != ignore_hidden)
505 priv->ignore_hidden = ignore_hidden;
507 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
512 * gtk_size_group_get_ignore_hidden:
513 * @size_group: a #GtkSizeGroup
515 * Returns if invisible widgets are ignored when calculating the size.
517 * Returns: %TRUE if invisible widgets are ignored.
522 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
524 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
526 return size_group->priv->ignore_hidden;
530 gtk_size_group_widget_destroyed (GtkWidget *widget,
531 GtkSizeGroup *size_group)
533 gtk_size_group_remove_widget (size_group, widget);
537 * gtk_size_group_add_widget:
538 * @size_group: a #GtkSizeGroup
539 * @widget: the #GtkWidget to add
541 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
542 * of the widget will be determined as the maximum of its requisition
543 * and the requisition of the other widgets in the size group.
544 * Whether this applies horizontally, vertically, or in both directions
545 * depends on the mode of the size group. See gtk_size_group_set_mode().
547 * When the widget is destroyed or no longer referenced elsewhere, it will
548 * be removed from the size group.
551 gtk_size_group_add_widget (GtkSizeGroup *size_group,
554 GtkSizeGroupPrivate *priv;
557 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
558 g_return_if_fail (GTK_IS_WIDGET (widget));
560 priv = size_group->priv;
562 groups = _gtk_widget_get_sizegroups (widget);
564 if (!g_slist_find (groups, size_group))
566 _gtk_widget_add_sizegroup (widget, size_group);
568 priv->widgets = g_slist_prepend (priv->widgets, widget);
570 g_signal_connect (widget, "destroy",
571 G_CALLBACK (gtk_size_group_widget_destroyed),
574 g_object_ref (size_group);
577 queue_resize_on_group (size_group);
581 * gtk_size_group_remove_widget:
582 * @size_group: a #GtkSizeGroup
583 * @widget: the #GtkWidget to remove
585 * Removes a widget from a #GtkSizeGroup.
588 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
591 GtkSizeGroupPrivate *priv;
593 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
594 g_return_if_fail (GTK_IS_WIDGET (widget));
596 priv = size_group->priv;
598 g_return_if_fail (g_slist_find (priv->widgets, widget));
600 g_signal_handlers_disconnect_by_func (widget,
601 gtk_size_group_widget_destroyed,
604 _gtk_widget_remove_sizegroup (widget, size_group);
606 priv->widgets = g_slist_remove (priv->widgets, widget);
607 queue_resize_on_group (size_group);
608 gtk_widget_queue_resize (widget);
610 g_object_unref (size_group);
614 * gtk_size_group_get_widgets:
615 * @size_group: a #GtkSizeGroup
617 * Returns the list of widgets associated with @size_group.
619 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
620 * widgets. The list is owned by GTK+ and should not be modified.
625 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
627 return size_group->priv->widgets;
631 * _gtk_size_group_bump_requisition:
632 * @widget: a #GtkWidget
633 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
634 * on the dimension in which to bump the size.
635 * @for_size: Size to request minimum and natural size for
636 * @minimum: a pointer to the widget's minimum size
637 * @natural: a pointer to the widget's natural size
639 * Refreshes the sizegroup while returning the groups requested
640 * value in the dimension @mode.
642 * This function is used both to update sizegroup minimum and natural size
643 * information and widget minimum and natural sizes in multiple passes from
644 * the size request apis.
647 _gtk_size_group_bump_requisition (GtkWidget *widget,
648 GtkSizeGroupMode mode,
655 gint min_result = 0, nat_result = 0;
657 if (!_gtk_widget_get_sizegroups (widget))
660 widgets = widget_get_size_group_peers (widget, mode);
662 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
664 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
666 GtkWidget *tmp_widget = tmp_list->data;
667 gint min_dimension, nat_dimension;
669 if (tmp_widget == widget)
671 min_dimension = *minimum;
672 nat_dimension = *natural;
676 _gtk_widget_compute_size_for_orientation (tmp_widget, mode, TRUE, for_size, &min_dimension, &nat_dimension);
679 min_result = MAX (min_result, min_dimension);
680 nat_result = MAX (nat_result, nat_dimension);
683 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
685 g_slist_free (widgets);
687 *minimum = min_result;
688 *natural = nat_result;
692 * _gtk_size_group_queue_resize:
693 * @widget: a #GtkWidget
695 * Queue a resize on a widget, and on all other widgets grouped with this widget.
698 _gtk_size_group_queue_resize (GtkWidget *widget,
699 GtkQueueResizeFlags flags)
701 queue_resize_on_widget (widget, TRUE, flags);
707 } GSListSubParserData;
710 size_group_start_element (GMarkupParseContext *context,
711 const gchar *element_name,
713 const gchar **values,
718 GSListSubParserData *data = (GSListSubParserData*)user_data;
720 if (strcmp (element_name, "widget") == 0)
721 for (i = 0; names[i]; i++)
722 if (strcmp (names[i], "name") == 0)
723 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
724 else if (strcmp (element_name, "widgets") == 0)
727 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
732 static const GMarkupParser size_group_parser =
734 size_group_start_element
738 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
741 const gchar *tagname,
742 GMarkupParser *parser,
745 GSListSubParserData *parser_data;
750 if (strcmp (tagname, "widgets") == 0)
752 parser_data = g_slice_new0 (GSListSubParserData);
753 parser_data->items = NULL;
754 parser_data->object = G_OBJECT (buildable);
756 *parser = size_group_parser;
765 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
768 const gchar *tagname,
772 GSListSubParserData *data;
775 if (strcmp (tagname, "widgets"))
778 data = (GSListSubParserData*)user_data;
779 data->items = g_slist_reverse (data->items);
781 for (l = data->items; l; l = l->next)
783 object = gtk_builder_get_object (builder, l->data);
786 g_warning ("Unknown object %s specified in sizegroup %s",
787 (const gchar*)l->data,
788 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
791 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
792 GTK_WIDGET (object));
795 g_slist_free (data->items);
796 g_slice_free (GSListSubParserData, data);