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 "gtksizerequestcacheprivate.h"
29 #include "gtkwidgetprivate.h"
30 #include "gtkcontainerprivate.h"
34 * SECTION:gtksizegroup
35 * @Short_description: Grouping widgets so they request the same size
36 * @Title: GtkSizeGroup
38 * #GtkSizeGroup provides a mechanism for grouping a number of widgets
39 * together so they all request the same amount of space. This is
40 * typically useful when you want a column of widgets to have the same
41 * size, but you can't use a #GtkGrid widget.
43 * In detail, the size requested for each widget in a #GtkSizeGroup is
44 * the maximum of the sizes that would have been requested for each
45 * widget in the size group if they were not in the size group. The mode
46 * of the size group (see gtk_size_group_set_mode()) determines whether
47 * this applies to the horizontal size, the vertical size, or both sizes.
49 * Note that size groups only affect the amount of space requested, not
50 * the size that the widgets finally receive. If you want the widgets in
51 * a #GtkSizeGroup to actually be the same size, you need to pack them in
52 * such a way that they get the size they request and not more. For
53 * example, if you are packing your widgets into a table, you would not
54 * include the %GTK_FILL flag.
56 * #GtkSizeGroup objects are referenced by each widget in the size group,
57 * so once you have added all widgets to a #GtkSizeGroup, you can drop
58 * the initial reference to the size group with g_object_unref(). If the
59 * widgets in the size group are subsequently destroyed, then they will
60 * be removed from the size group and drop their references on the size
61 * group; when all widgets have been removed, the size group will be
64 * Widgets can be part of multiple size groups; GTK+ will compute the
65 * horizontal size of a widget from the horizontal requisition of all
66 * widgets that can be reached from the widget by a chain of size groups
67 * of type %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_BOTH, and the
68 * vertical size from the vertical requisition of all widgets that can be
69 * reached from the widget by a chain of size groups of type
70 * %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH.
72 * Note that only non-contextual sizes of every widget are ever consulted
73 * by size groups (since size groups have no knowledge of what size a widget
74 * will be allocated in one dimension, it cannot derive how much height
75 * a widget will receive for a given width). When grouping widgets that
76 * trade height for width in mode %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH:
77 * the height for the minimum width will be the requested height for all
78 * widgets in the group. The same is of course true when horizontally grouping
79 * width for height widgets.
81 * Widgets that trade height-for-width should set a reasonably large minimum width
82 * by way of #GtkLabel:width-chars for instance. Widgets with static sizes as well
83 * as widgets that grow (such as ellipsizing text) need no such considerations.
85 * <refsect2 id="GtkSizeGroup-BUILDER-UI">
86 * <title>GtkSizeGroup as GtkBuildable</title>
88 * Size groups can be specified in a UI definition by placing an
89 * <object> element with <literal>class="GtkSizeGroup"</literal>
90 * somewhere in the UI definition. The widgets that belong to the
91 * size group are specified by a <widgets> element that may
92 * contain multiple <widget> elements, one for each member
93 * of the size group. The name attribute gives the id of the widget.
96 * <title>A UI definition fragment with GtkSizeGroup</title>
97 * <programlisting><![CDATA[
98 * <object class="GtkSizeGroup">
99 * <property name="mode">GTK_SIZE_GROUP_HORIZONTAL</property>
101 * <widget name="radio1"/>
102 * <widget name="radio2"/>
105 * ]]></programlisting>
112 struct _GtkSizeGroupPrivate
118 guint ignore_hidden : 1;
127 static void gtk_size_group_set_property (GObject *object,
131 static void gtk_size_group_get_property (GObject *object,
137 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
138 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
141 const gchar *tagname,
142 GMarkupParser *parser,
144 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
147 const gchar *tagname,
150 G_STATIC_ASSERT (GTK_SIZE_GROUP_HORIZONTAL == (1 << GTK_ORIENTATION_HORIZONTAL));
151 G_STATIC_ASSERT (GTK_SIZE_GROUP_VERTICAL == (1 << GTK_ORIENTATION_VERTICAL));
152 G_STATIC_ASSERT (GTK_SIZE_GROUP_BOTH == (GTK_SIZE_GROUP_HORIZONTAL | GTK_SIZE_GROUP_VERTICAL));
155 add_widget_to_closure (GHashTable *widgets,
158 GtkOrientation orientation)
160 GSList *tmp_groups, *tmp_widgets;
163 if (g_hash_table_lookup (widgets, widget))
166 g_hash_table_add (widgets, widget);
167 hidden = !gtk_widget_is_visible (widget);
169 for (tmp_groups = _gtk_widget_get_sizegroups (widget); tmp_groups; tmp_groups = tmp_groups->next)
171 GtkSizeGroup *tmp_group = tmp_groups->data;
172 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
174 if (g_hash_table_lookup (groups, tmp_group))
177 if (tmp_priv->ignore_hidden && hidden)
180 if (!(tmp_priv->mode & (1 << orientation)))
183 g_hash_table_add (groups, tmp_group);
185 for (tmp_widgets = tmp_priv->widgets; tmp_widgets; tmp_widgets = tmp_widgets->next)
186 add_widget_to_closure (widgets, groups, tmp_widgets->data, orientation);
191 _gtk_size_group_get_widget_peers (GtkWidget *for_widget,
192 GtkOrientation orientation)
194 GHashTable *widgets, *groups;
196 widgets = g_hash_table_new (g_direct_hash, g_direct_equal);
197 groups = g_hash_table_new (g_direct_hash, g_direct_equal);
199 add_widget_to_closure (widgets, groups, for_widget, orientation);
201 g_hash_table_unref (groups);
207 real_queue_resize (GtkWidget *widget,
208 GtkQueueResizeFlags flags)
210 GtkWidget *container;
212 _gtk_widget_set_alloc_needed (widget, TRUE);
213 _gtk_size_request_cache_clear (_gtk_widget_peek_request_cache (widget));
215 container = gtk_widget_get_parent (widget);
217 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
222 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
223 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
225 _gtk_container_queue_resize (GTK_CONTAINER (container));
230 queue_resize_on_widget (GtkWidget *widget,
231 gboolean check_siblings,
232 GtkQueueResizeFlags flags)
234 GtkWidget *parent = widget;
238 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);
260 widgets = _gtk_size_group_get_widget_peers (parent, GTK_ORIENTATION_HORIZONTAL);
262 g_hash_table_iter_init (&iter, widgets);
263 while (g_hash_table_iter_next (&iter, ¤t, NULL))
265 if (current == parent)
267 if (widget == parent)
268 real_queue_resize (parent, flags);
270 else if (current == widget)
272 g_warning ("A container and its child are part of this SizeGroup");
275 queue_resize_on_widget (current, FALSE, flags);
278 g_hash_table_destroy (widgets);
280 widgets = _gtk_size_group_get_widget_peers (parent, GTK_ORIENTATION_VERTICAL);
282 g_hash_table_iter_init (&iter, widgets);
283 while (g_hash_table_iter_next (&iter, ¤t, NULL))
285 if (current == parent)
287 if (widget == parent)
288 real_queue_resize (parent, flags);
290 else if (current == widget)
292 g_warning ("A container and its child are part of this SizeGroup");
295 queue_resize_on_widget (current, FALSE, flags);
298 g_hash_table_destroy (widgets);
300 parent = gtk_widget_get_parent (parent);
305 queue_resize_on_group (GtkSizeGroup *size_group)
307 GtkSizeGroupPrivate *priv = size_group->priv;
310 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
314 gtk_size_group_class_init (GtkSizeGroupClass *klass)
316 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
318 gobject_class->set_property = gtk_size_group_set_property;
319 gobject_class->get_property = gtk_size_group_get_property;
321 g_object_class_install_property (gobject_class,
323 g_param_spec_enum ("mode",
325 P_("The directions in which the size group affects the requested sizes"
326 " of its component widgets"),
327 GTK_TYPE_SIZE_GROUP_MODE,
328 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
331 * GtkSizeGroup:ignore-hidden:
333 * If %TRUE, unmapped widgets are ignored when determining
334 * the size of the group.
338 g_object_class_install_property (gobject_class,
340 g_param_spec_boolean ("ignore-hidden",
342 P_("If TRUE, unmapped widgets are ignored "
343 "when determining the size of the group"),
345 GTK_PARAM_READWRITE));
347 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
351 gtk_size_group_init (GtkSizeGroup *size_group)
353 GtkSizeGroupPrivate *priv;
355 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
357 GtkSizeGroupPrivate);
358 priv = size_group->priv;
360 priv->widgets = NULL;
361 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
362 priv->ignore_hidden = FALSE;
366 gtk_size_group_buildable_init (GtkBuildableIface *iface)
368 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
369 iface->custom_finished = gtk_size_group_buildable_custom_finished;
372 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
373 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
374 gtk_size_group_buildable_init))
377 gtk_size_group_set_property (GObject *object,
382 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
387 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
389 case PROP_IGNORE_HIDDEN:
390 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
399 gtk_size_group_get_property (GObject *object,
404 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
405 GtkSizeGroupPrivate *priv = size_group->priv;
410 g_value_set_enum (value, priv->mode);
412 case PROP_IGNORE_HIDDEN:
413 g_value_set_boolean (value, priv->ignore_hidden);
416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
422 * gtk_size_group_new:
423 * @mode: the mode for the new size group.
425 * Create a new #GtkSizeGroup.
427 * Return value: a newly created #GtkSizeGroup
430 gtk_size_group_new (GtkSizeGroupMode mode)
432 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
433 GtkSizeGroupPrivate *priv = size_group->priv;
441 * gtk_size_group_set_mode:
442 * @size_group: a #GtkSizeGroup
443 * @mode: the mode to set for the size group.
445 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
446 * group determines whether the widgets in the size group should
447 * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
448 * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
449 * or should all have the same requisition in both directions
450 * (%GTK_SIZE_GROUP_BOTH).
453 gtk_size_group_set_mode (GtkSizeGroup *size_group,
454 GtkSizeGroupMode mode)
456 GtkSizeGroupPrivate *priv;
458 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
460 priv = size_group->priv;
462 if (priv->mode != mode)
464 if (priv->mode != GTK_SIZE_GROUP_NONE)
465 queue_resize_on_group (size_group);
467 if (priv->mode != GTK_SIZE_GROUP_NONE)
468 queue_resize_on_group (size_group);
470 g_object_notify (G_OBJECT (size_group), "mode");
475 * gtk_size_group_get_mode:
476 * @size_group: a #GtkSizeGroup
478 * Gets the current mode of the size group. See gtk_size_group_set_mode().
480 * Return value: the current mode of the size group.
483 gtk_size_group_get_mode (GtkSizeGroup *size_group)
485 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
487 return size_group->priv->mode;
491 * gtk_size_group_set_ignore_hidden:
492 * @size_group: a #GtkSizeGroup
493 * @ignore_hidden: whether unmapped widgets should be ignored
494 * when calculating the size
496 * Sets whether unmapped widgets should be ignored when
497 * calculating the size.
502 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
503 gboolean ignore_hidden)
505 GtkSizeGroupPrivate *priv;
507 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
509 priv = size_group->priv;
511 ignore_hidden = ignore_hidden != FALSE;
513 if (priv->ignore_hidden != ignore_hidden)
515 priv->ignore_hidden = ignore_hidden;
517 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
522 * gtk_size_group_get_ignore_hidden:
523 * @size_group: a #GtkSizeGroup
525 * Returns if invisible widgets are ignored when calculating the size.
527 * Returns: %TRUE if invisible widgets are ignored.
532 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
534 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
536 return size_group->priv->ignore_hidden;
540 gtk_size_group_widget_destroyed (GtkWidget *widget,
541 GtkSizeGroup *size_group)
543 gtk_size_group_remove_widget (size_group, widget);
547 * gtk_size_group_add_widget:
548 * @size_group: a #GtkSizeGroup
549 * @widget: the #GtkWidget to add
551 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
552 * of the widget will be determined as the maximum of its requisition
553 * and the requisition of the other widgets in the size group.
554 * Whether this applies horizontally, vertically, or in both directions
555 * depends on the mode of the size group. See gtk_size_group_set_mode().
557 * When the widget is destroyed or no longer referenced elsewhere, it will
558 * be removed from the size group.
561 gtk_size_group_add_widget (GtkSizeGroup *size_group,
564 GtkSizeGroupPrivate *priv;
567 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
568 g_return_if_fail (GTK_IS_WIDGET (widget));
570 priv = size_group->priv;
572 groups = _gtk_widget_get_sizegroups (widget);
574 if (!g_slist_find (groups, size_group))
576 _gtk_widget_add_sizegroup (widget, size_group);
578 priv->widgets = g_slist_prepend (priv->widgets, widget);
580 g_signal_connect (widget, "destroy",
581 G_CALLBACK (gtk_size_group_widget_destroyed),
584 g_object_ref (size_group);
587 queue_resize_on_group (size_group);
591 * gtk_size_group_remove_widget:
592 * @size_group: a #GtkSizeGroup
593 * @widget: the #GtkWidget to remove
595 * Removes a widget from a #GtkSizeGroup.
598 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
601 GtkSizeGroupPrivate *priv;
603 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
604 g_return_if_fail (GTK_IS_WIDGET (widget));
606 priv = size_group->priv;
608 g_return_if_fail (g_slist_find (priv->widgets, widget));
610 g_signal_handlers_disconnect_by_func (widget,
611 gtk_size_group_widget_destroyed,
614 _gtk_widget_remove_sizegroup (widget, size_group);
616 priv->widgets = g_slist_remove (priv->widgets, widget);
617 queue_resize_on_group (size_group);
618 gtk_widget_queue_resize (widget);
620 g_object_unref (size_group);
624 * gtk_size_group_get_widgets:
625 * @size_group: a #GtkSizeGroup
627 * Returns the list of widgets associated with @size_group.
629 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
630 * widgets. The list is owned by GTK+ and should not be modified.
635 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
637 return size_group->priv->widgets;
641 * _gtk_size_group_queue_resize:
642 * @widget: a #GtkWidget
644 * Queue a resize on a widget, and on all other widgets grouped with this widget.
647 _gtk_size_group_queue_resize (GtkWidget *widget,
648 GtkQueueResizeFlags flags)
650 queue_resize_on_widget (widget, TRUE, flags);
656 } GSListSubParserData;
659 size_group_start_element (GMarkupParseContext *context,
660 const gchar *element_name,
662 const gchar **values,
667 GSListSubParserData *data = (GSListSubParserData*)user_data;
669 if (strcmp (element_name, "widget") == 0)
670 for (i = 0; names[i]; i++)
671 if (strcmp (names[i], "name") == 0)
672 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
673 else if (strcmp (element_name, "widgets") == 0)
676 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
681 static const GMarkupParser size_group_parser =
683 size_group_start_element
687 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
690 const gchar *tagname,
691 GMarkupParser *parser,
694 GSListSubParserData *parser_data;
699 if (strcmp (tagname, "widgets") == 0)
701 parser_data = g_slice_new0 (GSListSubParserData);
702 parser_data->items = NULL;
703 parser_data->object = G_OBJECT (buildable);
705 *parser = size_group_parser;
714 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
717 const gchar *tagname,
721 GSListSubParserData *data;
724 if (strcmp (tagname, "widgets"))
727 data = (GSListSubParserData*)user_data;
728 data->items = g_slist_reverse (data->items);
730 for (l = data->items; l; l = l->next)
732 object = gtk_builder_get_object (builder, l->data);
735 g_warning ("Unknown object %s specified in sizegroup %s",
736 (const gchar*)l->data,
737 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
740 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
741 GTK_WIDGET (object));
744 g_slist_free (data->items);
745 g_slice_free (GSListSubParserData, data);