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.
24 #include "gtkbuildable.h"
25 #include "gtkcontainer.h"
27 #include "gtktypebuiltins.h"
28 #include "gtkprivate.h"
29 #include "gtksizegroup-private.h"
30 #include "gtkwidgetprivate.h"
32 struct _GtkSizeGroupPrivate
34 GtkRequisition minimum_req;
35 GtkRequisition natural_req;
42 guint have_height : 1;
43 guint ignore_hidden : 1;
53 static void gtk_size_group_set_property (GObject *object,
57 static void gtk_size_group_get_property (GObject *object,
62 static void add_group_to_closure (GtkSizeGroup *group,
63 GtkSizeGroupMode mode,
66 static void add_widget_to_closure (GtkWidget *widget,
67 GtkSizeGroupMode mode,
72 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
73 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
77 GMarkupParser *parser,
79 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
86 mark_group_unvisited (GtkSizeGroup *group)
88 group->priv->visited = FALSE;
92 mark_widget_unvisited (GtkWidget *widget)
94 _gtk_widget_set_sizegroup_visited (widget, FALSE);
98 add_group_to_closure (GtkSizeGroup *group,
99 GtkSizeGroupMode mode,
103 GtkSizeGroupPrivate *priv = group->priv;
106 *groups = g_slist_prepend (*groups, group);
107 priv->visited = TRUE;
109 tmp_widgets = priv->widgets;
112 GtkWidget *tmp_widget = tmp_widgets->data;
114 if (!_gtk_widget_get_sizegroup_visited (tmp_widget))
115 add_widget_to_closure (tmp_widget, mode, groups, widgets);
117 tmp_widgets = tmp_widgets->next;
122 add_widget_to_closure (GtkWidget *widget,
123 GtkSizeGroupMode mode,
129 *widgets = g_slist_prepend (*widgets, widget);
130 _gtk_widget_set_sizegroup_visited (widget, TRUE);
132 tmp_groups = _gtk_widget_get_sizegroups (widget);
135 GtkSizeGroup *tmp_group = tmp_groups->data;
136 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
138 if ((tmp_priv->mode == GTK_SIZE_GROUP_BOTH || tmp_priv->mode == mode) &&
139 !tmp_group->priv->visited)
140 add_group_to_closure (tmp_group, mode, groups, widgets);
142 tmp_groups = tmp_groups->next;
147 real_queue_resize (GtkWidget *widget,
148 GtkQueueResizeFlags flags)
150 GtkWidget *container;
152 _gtk_widget_set_alloc_needed (widget, TRUE);
153 _gtk_widget_set_width_request_needed (widget, TRUE);
154 _gtk_widget_set_height_request_needed (widget, TRUE);
156 container = gtk_widget_get_parent (widget);
158 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
163 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
164 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
166 _gtk_container_queue_resize (GTK_CONTAINER (container));
171 reset_group_sizes (GSList *groups)
173 GSList *tmp_list = groups;
176 GtkSizeGroup *tmp_group = tmp_list->data;
177 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
179 tmp_priv->have_width = FALSE;
180 tmp_priv->have_height = FALSE;
182 tmp_list = tmp_list->next;
187 queue_resize_on_widget (GtkWidget *widget,
188 gboolean check_siblings,
189 GtkQueueResizeFlags flags)
191 GtkWidget *parent = widget;
196 GSList *widget_groups;
200 if (widget == parent && !check_siblings)
202 real_queue_resize (widget, flags);
203 parent = gtk_widget_get_parent (parent);
207 widget_groups = _gtk_widget_get_sizegroups (parent);
210 if (widget == parent)
211 real_queue_resize (widget, flags);
213 parent = gtk_widget_get_parent (parent);
220 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
221 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
222 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
224 reset_group_sizes (groups);
229 if (tmp_list->data == parent)
231 if (widget == parent)
232 real_queue_resize (parent, flags);
234 else if (tmp_list->data == widget)
236 g_warning ("A container and its child are part of this SizeGroup");
239 queue_resize_on_widget (tmp_list->data, FALSE, flags);
241 tmp_list = tmp_list->next;
244 g_slist_free (widgets);
245 g_slist_free (groups);
250 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
251 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
252 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
254 reset_group_sizes (groups);
259 if (tmp_list->data == parent)
261 if (widget == parent)
262 real_queue_resize (parent, flags);
264 else if (tmp_list->data == widget)
266 g_warning ("A container and its child are part of this SizeGroup");
269 queue_resize_on_widget (tmp_list->data, FALSE, flags);
271 tmp_list = tmp_list->next;
274 g_slist_free (widgets);
275 g_slist_free (groups);
277 parent = gtk_widget_get_parent (parent);
282 queue_resize_on_group (GtkSizeGroup *size_group)
284 GtkSizeGroupPrivate *priv = size_group->priv;
287 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
291 gtk_size_group_class_init (GtkSizeGroupClass *klass)
293 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
295 gobject_class->set_property = gtk_size_group_set_property;
296 gobject_class->get_property = gtk_size_group_get_property;
298 g_object_class_install_property (gobject_class,
300 g_param_spec_enum ("mode",
302 P_("The directions in which the size group affects the requested sizes"
303 " of its component widgets"),
304 GTK_TYPE_SIZE_GROUP_MODE,
305 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
308 * GtkSizeGroup:ignore-hidden:
310 * If %TRUE, unmapped widgets are ignored when determining
311 * the size of the group.
315 g_object_class_install_property (gobject_class,
317 g_param_spec_boolean ("ignore-hidden",
319 P_("If TRUE, unmapped widgets are ignored "
320 "when determining the size of the group"),
322 GTK_PARAM_READWRITE));
324 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
328 gtk_size_group_init (GtkSizeGroup *size_group)
330 GtkSizeGroupPrivate *priv;
332 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
334 GtkSizeGroupPrivate);
335 priv = size_group->priv;
337 priv->widgets = NULL;
338 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
339 priv->have_width = 0;
340 priv->have_height = 0;
341 priv->ignore_hidden = 0;
342 priv->visited = FALSE;
346 gtk_size_group_buildable_init (GtkBuildableIface *iface)
348 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
349 iface->custom_finished = gtk_size_group_buildable_custom_finished;
352 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
353 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
354 gtk_size_group_buildable_init))
357 gtk_size_group_set_property (GObject *object,
362 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
367 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
369 case PROP_IGNORE_HIDDEN:
370 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 gtk_size_group_get_property (GObject *object,
384 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
385 GtkSizeGroupPrivate *priv = size_group->priv;
390 g_value_set_enum (value, priv->mode);
392 case PROP_IGNORE_HIDDEN:
393 g_value_set_boolean (value, priv->ignore_hidden);
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
402 * gtk_size_group_new:
403 * @mode: the mode for the new size group.
405 * Create a new #GtkSizeGroup.
407 * Return value: a newly created #GtkSizeGroup
410 gtk_size_group_new (GtkSizeGroupMode mode)
412 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
413 GtkSizeGroupPrivate *priv = size_group->priv;
421 * gtk_size_group_set_mode:
422 * @size_group: a #GtkSizeGroup
423 * @mode: the mode to set for the size group.
425 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
426 * group determines whether the widgets in the size group should
427 * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
428 * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
429 * or should all have the same requisition in both directions
430 * (%GTK_SIZE_GROUP_BOTH).
433 gtk_size_group_set_mode (GtkSizeGroup *size_group,
434 GtkSizeGroupMode mode)
436 GtkSizeGroupPrivate *priv;
438 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
440 priv = size_group->priv;
442 if (priv->mode != mode)
444 if (priv->mode != GTK_SIZE_GROUP_NONE)
445 queue_resize_on_group (size_group);
447 if (priv->mode != GTK_SIZE_GROUP_NONE)
448 queue_resize_on_group (size_group);
450 g_object_notify (G_OBJECT (size_group), "mode");
455 * gtk_size_group_get_mode:
456 * @size_group: a #GtkSizeGroup
458 * Gets the current mode of the size group. See gtk_size_group_set_mode().
460 * Return value: the current mode of the size group.
463 gtk_size_group_get_mode (GtkSizeGroup *size_group)
465 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
467 return size_group->priv->mode;
471 * gtk_size_group_set_ignore_hidden:
472 * @size_group: a #GtkSizeGroup
473 * @ignore_hidden: whether unmapped widgets should be ignored
474 * when calculating the size
476 * Sets whether unmapped widgets should be ignored when
477 * calculating the size.
482 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
483 gboolean ignore_hidden)
485 GtkSizeGroupPrivate *priv;
487 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
489 priv = size_group->priv;
491 ignore_hidden = ignore_hidden != FALSE;
493 if (priv->ignore_hidden != ignore_hidden)
495 priv->ignore_hidden = ignore_hidden;
497 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
502 * gtk_size_group_get_ignore_hidden:
503 * @size_group: a #GtkSizeGroup
505 * Returns if invisible widgets are ignored when calculating the size.
507 * Returns: %TRUE if invisible widgets are ignored.
512 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
514 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
516 return size_group->priv->ignore_hidden;
520 gtk_size_group_widget_destroyed (GtkWidget *widget,
521 GtkSizeGroup *size_group)
523 gtk_size_group_remove_widget (size_group, widget);
527 * gtk_size_group_add_widget:
528 * @size_group: a #GtkSizeGroup
529 * @widget: the #GtkWidget to add
531 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
532 * of the widget will be determined as the maximum of its requisition
533 * and the requisition of the other widgets in the size group.
534 * Whether this applies horizontally, vertically, or in both directions
535 * depends on the mode of the size group. See gtk_size_group_set_mode().
537 * When the widget is destroyed or no longer referenced elsewhere, it will
538 * be removed from the size group.
541 gtk_size_group_add_widget (GtkSizeGroup *size_group,
544 GtkSizeGroupPrivate *priv;
547 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
548 g_return_if_fail (GTK_IS_WIDGET (widget));
550 priv = size_group->priv;
552 groups = _gtk_widget_get_sizegroups (widget);
554 if (!g_slist_find (groups, size_group))
556 _gtk_widget_add_sizegroup (widget, size_group);
558 priv->widgets = g_slist_prepend (priv->widgets, widget);
560 g_signal_connect (widget, "destroy",
561 G_CALLBACK (gtk_size_group_widget_destroyed),
564 g_object_ref (size_group);
567 queue_resize_on_group (size_group);
571 * gtk_size_group_remove_widget:
572 * @size_group: a #GtkSizeGroup
573 * @widget: the #GtkWidget to remove
575 * Removes a widget from a #GtkSizeGroup.
578 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
581 GtkSizeGroupPrivate *priv;
583 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
584 g_return_if_fail (GTK_IS_WIDGET (widget));
586 priv = size_group->priv;
588 g_return_if_fail (g_slist_find (priv->widgets, widget));
590 g_signal_handlers_disconnect_by_func (widget,
591 gtk_size_group_widget_destroyed,
594 _gtk_widget_remove_sizegroup (widget, size_group);
596 priv->widgets = g_slist_remove (priv->widgets, widget);
597 queue_resize_on_group (size_group);
598 gtk_widget_queue_resize (widget);
600 g_object_unref (size_group);
604 * gtk_size_group_get_widgets:
605 * @size_group: a #GtkSizeGroup
607 * Returns the list of widgets associated with @size_group.
609 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
610 * widgets. The list is owned by GTK+ and should not be modified.
615 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
617 return size_group->priv->widgets;
621 compute_dimension (GtkWidget *widget,
622 GtkSizeGroupMode mode,
623 gint *minimum, /* in-out */
624 gint *natural) /* in-out */
626 GSList *widgets = NULL;
627 GSList *groups = NULL;
629 gint min_result = 0, nat_result = 0;
631 add_widget_to_closure (widget, mode, &groups, &widgets);
632 g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
633 g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
635 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
639 min_result = *minimum;
640 nat_result = *natural;
644 GtkSizeGroup *group = groups->data;
645 GtkSizeGroupPrivate *priv = group->priv;
647 if (mode == GTK_SIZE_GROUP_HORIZONTAL && priv->have_width)
649 min_result = priv->minimum_req.width;
650 nat_result = priv->natural_req.width;
652 else if (mode == GTK_SIZE_GROUP_VERTICAL && priv->have_height)
654 min_result = priv->minimum_req.height;
655 nat_result = priv->natural_req.height;
662 GtkWidget *tmp_widget = tmp_list->data;
663 gint min_dimension, nat_dimension;
665 if (tmp_widget == widget)
667 min_dimension = *minimum;
668 nat_dimension = *natural;
672 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
673 gtk_widget_get_preferred_width (tmp_widget, &min_dimension, &nat_dimension);
675 gtk_widget_get_preferred_height (tmp_widget, &min_dimension, &nat_dimension);
678 if (gtk_widget_get_mapped (tmp_widget) || !priv->ignore_hidden)
680 min_result = MAX (min_result, min_dimension);
681 nat_result = MAX (nat_result, nat_dimension);
684 tmp_list = tmp_list->next;
690 GtkSizeGroup *tmp_group = tmp_list->data;
691 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
693 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
695 tmp_priv->have_width = TRUE;
696 tmp_priv->minimum_req.width = min_result;
697 tmp_priv->natural_req.width = nat_result;
701 tmp_priv->have_height = TRUE;
702 tmp_priv->minimum_req.height = min_result;
703 tmp_priv->natural_req.height = nat_result;
706 tmp_list = tmp_list->next;
711 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
713 g_slist_free (widgets);
714 g_slist_free (groups);
716 *minimum = min_result;
717 *natural = nat_result;
721 * _gtk_size_group_bump_requisition:
722 * @widget: a #GtkWidget
723 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
724 * on the dimension in which to bump the size.
725 * @minimum: a pointer to the widget's minimum size
726 * @natural: a pointer to the widget's natural size
728 * Refreshes the sizegroup while returning the groups requested
729 * value in the dimension @mode.
731 * This function is used both to update sizegroup minimum and natural size
732 * information and widget minimum and natural sizes in multiple passes from
733 * the size request apis.
736 _gtk_size_group_bump_requisition (GtkWidget *widget,
737 GtkSizeGroupMode mode,
741 if (!_gtk_widget_get_sizegroup_bumping (widget))
743 /* Avoid recursion here */
744 _gtk_widget_set_sizegroup_bumping (widget, TRUE);
746 if (_gtk_widget_get_sizegroups (widget))
747 compute_dimension (widget, mode, minimum, natural);
749 _gtk_widget_set_sizegroup_bumping (widget, FALSE);
756 * _gtk_size_group_queue_resize:
757 * @widget: a #GtkWidget
759 * Queue a resize on a widget, and on all other widgets grouped with this widget.
762 _gtk_size_group_queue_resize (GtkWidget *widget,
763 GtkQueueResizeFlags flags)
765 queue_resize_on_widget (widget, TRUE, flags);
771 } GSListSubParserData;
774 size_group_start_element (GMarkupParseContext *context,
775 const gchar *element_name,
777 const gchar **values,
782 GSListSubParserData *data = (GSListSubParserData*)user_data;
784 if (strcmp (element_name, "widget") == 0)
785 for (i = 0; names[i]; i++)
786 if (strcmp (names[i], "name") == 0)
787 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
788 else if (strcmp (element_name, "widgets") == 0)
791 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
796 static const GMarkupParser size_group_parser =
798 size_group_start_element
802 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
805 const gchar *tagname,
806 GMarkupParser *parser,
809 GSListSubParserData *parser_data;
814 if (strcmp (tagname, "widgets") == 0)
816 parser_data = g_slice_new0 (GSListSubParserData);
817 parser_data->items = NULL;
818 parser_data->object = G_OBJECT (buildable);
820 *parser = size_group_parser;
829 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
832 const gchar *tagname,
836 GSListSubParserData *data;
839 if (strcmp (tagname, "widgets"))
842 data = (GSListSubParserData*)user_data;
843 data->items = g_slist_reverse (data->items);
845 for (l = data->items; l; l = l->next)
847 object = gtk_builder_get_object (builder, l->data);
850 g_warning ("Unknown object %s specified in sizegroup %s",
851 (const gchar*)l->data,
852 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
855 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
856 GTK_WIDGET (object));
859 g_slist_free (data->items);
860 g_slice_free (GSListSubParserData, data);