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,
135 static void add_group_to_closure (GtkSizeGroup *group,
136 GtkSizeGroupMode mode,
138 static void add_widget_to_closure (GtkWidget *widget,
139 GtkSizeGroupMode mode,
143 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
144 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
147 const gchar *tagname,
148 GMarkupParser *parser,
150 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
153 const gchar *tagname,
157 mark_widget_unvisited (GtkWidget *widget)
159 _gtk_widget_set_sizegroup_visited (widget, FALSE);
163 add_group_to_closure (GtkSizeGroup *group,
164 GtkSizeGroupMode mode,
167 GtkSizeGroupPrivate *priv = group->priv;
170 for (tmp_widgets = priv->widgets; tmp_widgets; tmp_widgets = tmp_widgets->next)
172 GtkWidget *tmp_widget = tmp_widgets->data;
174 add_widget_to_closure (tmp_widget, mode, widgets);
179 add_widget_to_closure (GtkWidget *widget,
180 GtkSizeGroupMode mode,
186 if (_gtk_widget_get_sizegroup_visited (widget))
189 *widgets = g_slist_prepend (*widgets, widget);
190 _gtk_widget_set_sizegroup_visited (widget, TRUE);
191 hidden = !gtk_widget_get_mapped (widget);
193 for (tmp_groups = _gtk_widget_get_sizegroups (widget); tmp_groups; tmp_groups = tmp_groups->next)
195 GtkSizeGroup *tmp_group = tmp_groups->data;
196 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
198 if (tmp_priv->ignore_hidden && hidden)
201 if (tmp_priv->mode == GTK_SIZE_GROUP_BOTH || tmp_priv->mode == mode)
202 add_group_to_closure (tmp_group, mode, widgets);
207 real_queue_resize (GtkWidget *widget,
208 GtkQueueResizeFlags flags)
210 GtkWidget *container;
212 _gtk_widget_set_alloc_needed (widget, TRUE);
213 _gtk_widget_set_width_request_needed (widget, TRUE);
214 _gtk_widget_set_height_request_needed (widget, TRUE);
216 container = gtk_widget_get_parent (widget);
218 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
223 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
224 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
226 _gtk_container_queue_resize (GTK_CONTAINER (container));
231 queue_resize_on_widget (GtkWidget *widget,
232 gboolean check_siblings,
233 GtkQueueResizeFlags flags)
235 GtkWidget *parent = widget;
240 GSList *widget_groups;
243 if (widget == parent && !check_siblings)
245 real_queue_resize (widget, flags);
246 parent = gtk_widget_get_parent (parent);
250 widget_groups = _gtk_widget_get_sizegroups (parent);
253 if (widget == parent)
254 real_queue_resize (widget, flags);
256 parent = gtk_widget_get_parent (parent);
262 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &widgets);
263 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
265 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
267 if (tmp_list->data == parent)
269 if (widget == parent)
270 real_queue_resize (parent, flags);
272 else if (tmp_list->data == widget)
274 g_warning ("A container and its child are part of this SizeGroup");
277 queue_resize_on_widget (tmp_list->data, FALSE, flags);
280 g_slist_free (widgets);
284 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &widgets);
285 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
287 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
289 if (tmp_list->data == parent)
291 if (widget == parent)
292 real_queue_resize (parent, flags);
294 else if (tmp_list->data == widget)
296 g_warning ("A container and its child are part of this SizeGroup");
299 queue_resize_on_widget (tmp_list->data, FALSE, flags);
302 g_slist_free (widgets);
304 parent = gtk_widget_get_parent (parent);
309 queue_resize_on_group (GtkSizeGroup *size_group)
311 GtkSizeGroupPrivate *priv = size_group->priv;
314 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
318 gtk_size_group_class_init (GtkSizeGroupClass *klass)
320 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
322 gobject_class->set_property = gtk_size_group_set_property;
323 gobject_class->get_property = gtk_size_group_get_property;
325 g_object_class_install_property (gobject_class,
327 g_param_spec_enum ("mode",
329 P_("The directions in which the size group affects the requested sizes"
330 " of its component widgets"),
331 GTK_TYPE_SIZE_GROUP_MODE,
332 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
335 * GtkSizeGroup:ignore-hidden:
337 * If %TRUE, unmapped widgets are ignored when determining
338 * the size of the group.
342 g_object_class_install_property (gobject_class,
344 g_param_spec_boolean ("ignore-hidden",
346 P_("If TRUE, unmapped widgets are ignored "
347 "when determining the size of the group"),
349 GTK_PARAM_READWRITE));
351 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
355 gtk_size_group_init (GtkSizeGroup *size_group)
357 GtkSizeGroupPrivate *priv;
359 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
361 GtkSizeGroupPrivate);
362 priv = size_group->priv;
364 priv->widgets = NULL;
365 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
366 priv->ignore_hidden = FALSE;
370 gtk_size_group_buildable_init (GtkBuildableIface *iface)
372 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
373 iface->custom_finished = gtk_size_group_buildable_custom_finished;
376 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
377 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
378 gtk_size_group_buildable_init))
381 gtk_size_group_set_property (GObject *object,
386 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
391 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
393 case PROP_IGNORE_HIDDEN:
394 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403 gtk_size_group_get_property (GObject *object,
408 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
409 GtkSizeGroupPrivate *priv = size_group->priv;
414 g_value_set_enum (value, priv->mode);
416 case PROP_IGNORE_HIDDEN:
417 g_value_set_boolean (value, priv->ignore_hidden);
420 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426 * gtk_size_group_new:
427 * @mode: the mode for the new size group.
429 * Create a new #GtkSizeGroup.
431 * Return value: a newly created #GtkSizeGroup
434 gtk_size_group_new (GtkSizeGroupMode mode)
436 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
437 GtkSizeGroupPrivate *priv = size_group->priv;
445 * gtk_size_group_set_mode:
446 * @size_group: a #GtkSizeGroup
447 * @mode: the mode to set for the size group.
449 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
450 * group determines whether the widgets in the size group should
451 * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
452 * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
453 * or should all have the same requisition in both directions
454 * (%GTK_SIZE_GROUP_BOTH).
457 gtk_size_group_set_mode (GtkSizeGroup *size_group,
458 GtkSizeGroupMode mode)
460 GtkSizeGroupPrivate *priv;
462 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
464 priv = size_group->priv;
466 if (priv->mode != mode)
468 if (priv->mode != GTK_SIZE_GROUP_NONE)
469 queue_resize_on_group (size_group);
471 if (priv->mode != GTK_SIZE_GROUP_NONE)
472 queue_resize_on_group (size_group);
474 g_object_notify (G_OBJECT (size_group), "mode");
479 * gtk_size_group_get_mode:
480 * @size_group: a #GtkSizeGroup
482 * Gets the current mode of the size group. See gtk_size_group_set_mode().
484 * Return value: the current mode of the size group.
487 gtk_size_group_get_mode (GtkSizeGroup *size_group)
489 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
491 return size_group->priv->mode;
495 * gtk_size_group_set_ignore_hidden:
496 * @size_group: a #GtkSizeGroup
497 * @ignore_hidden: whether unmapped widgets should be ignored
498 * when calculating the size
500 * Sets whether unmapped widgets should be ignored when
501 * calculating the size.
506 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
507 gboolean ignore_hidden)
509 GtkSizeGroupPrivate *priv;
511 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
513 priv = size_group->priv;
515 ignore_hidden = ignore_hidden != FALSE;
517 if (priv->ignore_hidden != ignore_hidden)
519 priv->ignore_hidden = ignore_hidden;
521 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
526 * gtk_size_group_get_ignore_hidden:
527 * @size_group: a #GtkSizeGroup
529 * Returns if invisible widgets are ignored when calculating the size.
531 * Returns: %TRUE if invisible widgets are ignored.
536 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
538 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
540 return size_group->priv->ignore_hidden;
544 gtk_size_group_widget_destroyed (GtkWidget *widget,
545 GtkSizeGroup *size_group)
547 gtk_size_group_remove_widget (size_group, widget);
551 * gtk_size_group_add_widget:
552 * @size_group: a #GtkSizeGroup
553 * @widget: the #GtkWidget to add
555 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
556 * of the widget will be determined as the maximum of its requisition
557 * and the requisition of the other widgets in the size group.
558 * Whether this applies horizontally, vertically, or in both directions
559 * depends on the mode of the size group. See gtk_size_group_set_mode().
561 * When the widget is destroyed or no longer referenced elsewhere, it will
562 * be removed from the size group.
565 gtk_size_group_add_widget (GtkSizeGroup *size_group,
568 GtkSizeGroupPrivate *priv;
571 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
572 g_return_if_fail (GTK_IS_WIDGET (widget));
574 priv = size_group->priv;
576 groups = _gtk_widget_get_sizegroups (widget);
578 if (!g_slist_find (groups, size_group))
580 _gtk_widget_add_sizegroup (widget, size_group);
582 priv->widgets = g_slist_prepend (priv->widgets, widget);
584 g_signal_connect (widget, "destroy",
585 G_CALLBACK (gtk_size_group_widget_destroyed),
588 g_object_ref (size_group);
591 queue_resize_on_group (size_group);
595 * gtk_size_group_remove_widget:
596 * @size_group: a #GtkSizeGroup
597 * @widget: the #GtkWidget to remove
599 * Removes a widget from a #GtkSizeGroup.
602 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
605 GtkSizeGroupPrivate *priv;
607 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
608 g_return_if_fail (GTK_IS_WIDGET (widget));
610 priv = size_group->priv;
612 g_return_if_fail (g_slist_find (priv->widgets, widget));
614 g_signal_handlers_disconnect_by_func (widget,
615 gtk_size_group_widget_destroyed,
618 _gtk_widget_remove_sizegroup (widget, size_group);
620 priv->widgets = g_slist_remove (priv->widgets, widget);
621 queue_resize_on_group (size_group);
622 gtk_widget_queue_resize (widget);
624 g_object_unref (size_group);
628 * gtk_size_group_get_widgets:
629 * @size_group: a #GtkSizeGroup
631 * Returns the list of widgets associated with @size_group.
633 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
634 * widgets. The list is owned by GTK+ and should not be modified.
639 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
641 return size_group->priv->widgets;
645 compute_dimension (GtkWidget *widget,
646 GtkSizeGroupMode mode,
647 gint *minimum, /* in-out */
648 gint *natural) /* in-out */
650 GSList *widgets = NULL;
652 gint min_result = 0, nat_result = 0;
654 add_widget_to_closure (widget, mode, &widgets);
655 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
657 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
659 for (tmp_list = widgets; tmp_list; tmp_list = tmp_list->next)
661 GtkWidget *tmp_widget = tmp_list->data;
662 gint min_dimension, nat_dimension;
664 if (tmp_widget == widget)
666 min_dimension = *minimum;
667 nat_dimension = *natural;
671 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
672 gtk_widget_get_preferred_width (tmp_widget, &min_dimension, &nat_dimension);
674 gtk_widget_get_preferred_height (tmp_widget, &min_dimension, &nat_dimension);
677 min_result = MAX (min_result, min_dimension);
678 nat_result = MAX (nat_result, nat_dimension);
681 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
683 g_slist_free (widgets);
685 *minimum = min_result;
686 *natural = nat_result;
690 * _gtk_size_group_bump_requisition:
691 * @widget: a #GtkWidget
692 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
693 * on the dimension in which to bump the size.
694 * @minimum: a pointer to the widget's minimum size
695 * @natural: a pointer to the widget's natural size
697 * Refreshes the sizegroup while returning the groups requested
698 * value in the dimension @mode.
700 * This function is used both to update sizegroup minimum and natural size
701 * information and widget minimum and natural sizes in multiple passes from
702 * the size request apis.
705 _gtk_size_group_bump_requisition (GtkWidget *widget,
706 GtkSizeGroupMode mode,
710 if (!_gtk_widget_get_sizegroup_bumping (widget))
712 /* Avoid recursion here */
713 _gtk_widget_set_sizegroup_bumping (widget, TRUE);
715 if (_gtk_widget_get_sizegroups (widget))
716 compute_dimension (widget, mode, minimum, natural);
718 _gtk_widget_set_sizegroup_bumping (widget, FALSE);
725 * _gtk_size_group_queue_resize:
726 * @widget: a #GtkWidget
728 * Queue a resize on a widget, and on all other widgets grouped with this widget.
731 _gtk_size_group_queue_resize (GtkWidget *widget,
732 GtkQueueResizeFlags flags)
734 queue_resize_on_widget (widget, TRUE, flags);
740 } GSListSubParserData;
743 size_group_start_element (GMarkupParseContext *context,
744 const gchar *element_name,
746 const gchar **values,
751 GSListSubParserData *data = (GSListSubParserData*)user_data;
753 if (strcmp (element_name, "widget") == 0)
754 for (i = 0; names[i]; i++)
755 if (strcmp (names[i], "name") == 0)
756 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
757 else if (strcmp (element_name, "widgets") == 0)
760 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
765 static const GMarkupParser size_group_parser =
767 size_group_start_element
771 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
774 const gchar *tagname,
775 GMarkupParser *parser,
778 GSListSubParserData *parser_data;
783 if (strcmp (tagname, "widgets") == 0)
785 parser_data = g_slice_new0 (GSListSubParserData);
786 parser_data->items = NULL;
787 parser_data->object = G_OBJECT (buildable);
789 *parser = size_group_parser;
798 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
801 const gchar *tagname,
805 GSListSubParserData *data;
808 if (strcmp (tagname, "widgets"))
811 data = (GSListSubParserData*)user_data;
812 data->items = g_slist_reverse (data->items);
814 for (l = data->items; l; l = l->next)
816 object = gtk_builder_get_object (builder, l->data);
819 g_warning ("Unknown object %s specified in sizegroup %s",
820 (const gchar*)l->data,
821 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
824 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
825 GTK_WIDGET (object));
828 g_slist_free (data->items);
829 g_slice_free (GSListSubParserData, data);