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 "gtkprivate.h"
28 #include "gtksizegroup-private.h"
29 #include "gtkwidgetprivate.h"
31 struct _GtkSizeGroupPrivate
33 GtkRequisition minimum_req;
34 GtkRequisition natural_req;
41 guint have_height : 1;
42 guint ignore_hidden : 1;
51 static void gtk_size_group_set_property (GObject *object,
55 static void gtk_size_group_get_property (GObject *object,
60 static void add_group_to_closure (GtkSizeGroup *group,
61 GtkSizeGroupMode mode,
64 static void add_widget_to_closure (GtkWidget *widget,
65 GtkSizeGroupMode mode,
70 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
71 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
75 GMarkupParser *parser,
77 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
83 static GQuark size_groups_quark;
84 static const gchar size_groups_tag[] = "gtk-size-groups";
86 static GQuark visited_quark;
87 static const gchar visited_tag[] = "gtk-size-group-visited";
89 static GQuark bumping_quark;
90 static const gchar bumping_tag[] = "gtk-size-group-bumping";
93 get_size_groups (GtkWidget *widget)
95 return g_object_get_qdata (G_OBJECT (widget), size_groups_quark);
99 set_size_groups (GtkWidget *widget,
102 g_object_set_qdata (G_OBJECT (widget), size_groups_quark, groups);
106 mark_visited (gpointer object)
108 g_object_set_qdata (object, visited_quark, "visited");
112 mark_unvisited (gpointer object)
114 g_object_set_qdata (object, visited_quark, NULL);
118 is_visited (gpointer object)
120 return g_object_get_qdata (object, visited_quark) != NULL;
124 mark_bumping (gpointer object, gboolean bumping)
126 g_object_set_qdata (object, bumping_quark, bumping ? "bumping" : NULL);
130 is_bumping (gpointer object)
132 return g_object_get_qdata (object, bumping_quark) != NULL;
136 add_group_to_closure (GtkSizeGroup *group,
137 GtkSizeGroupMode mode,
141 GtkSizeGroupPrivate *priv = group->priv;
144 *groups = g_slist_prepend (*groups, group);
145 mark_visited (group);
147 tmp_widgets = priv->widgets;
150 GtkWidget *tmp_widget = tmp_widgets->data;
152 if (!is_visited (tmp_widget))
153 add_widget_to_closure (tmp_widget, mode, groups, widgets);
155 tmp_widgets = tmp_widgets->next;
160 add_widget_to_closure (GtkWidget *widget,
161 GtkSizeGroupMode mode,
167 *widgets = g_slist_prepend (*widgets, widget);
168 mark_visited (widget);
170 tmp_groups = get_size_groups (widget);
173 GtkSizeGroup *tmp_group = tmp_groups->data;
174 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
176 if ((tmp_priv->mode == GTK_SIZE_GROUP_BOTH || tmp_priv->mode == mode) &&
177 !is_visited (tmp_group))
178 add_group_to_closure (tmp_group, mode, groups, widgets);
180 tmp_groups = tmp_groups->next;
185 real_queue_resize (GtkWidget *widget,
186 GtkQueueResizeFlags flags)
188 GtkWidget *container;
190 _gtk_widget_set_alloc_needed (widget, TRUE);
191 _gtk_widget_set_width_request_needed (widget, TRUE);
192 _gtk_widget_set_height_request_needed (widget, TRUE);
194 container = gtk_widget_get_parent (widget);
196 gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
201 if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
202 _gtk_container_resize_invalidate (GTK_CONTAINER (container));
204 _gtk_container_queue_resize (GTK_CONTAINER (container));
209 reset_group_sizes (GSList *groups)
211 GSList *tmp_list = groups;
214 GtkSizeGroup *tmp_group = tmp_list->data;
215 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
217 tmp_priv->have_width = FALSE;
218 tmp_priv->have_height = FALSE;
220 tmp_list = tmp_list->next;
225 queue_resize_on_widget (GtkWidget *widget,
226 gboolean check_siblings,
227 GtkQueueResizeFlags flags)
229 GtkWidget *parent = widget;
234 GSList *widget_groups;
238 if (widget == parent && !check_siblings)
240 real_queue_resize (widget, flags);
241 parent = gtk_widget_get_parent (parent);
245 widget_groups = get_size_groups (parent);
248 if (widget == parent)
249 real_queue_resize (widget, flags);
251 parent = gtk_widget_get_parent (parent);
258 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
259 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
260 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
262 reset_group_sizes (groups);
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);
279 tmp_list = tmp_list->next;
282 g_slist_free (widgets);
283 g_slist_free (groups);
288 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
289 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
290 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
292 reset_group_sizes (groups);
297 if (tmp_list->data == parent)
299 if (widget == parent)
300 real_queue_resize (parent, flags);
302 else if (tmp_list->data == widget)
304 g_warning ("A container and its child are part of this SizeGroup");
307 queue_resize_on_widget (tmp_list->data, FALSE, flags);
309 tmp_list = tmp_list->next;
312 g_slist_free (widgets);
313 g_slist_free (groups);
315 parent = gtk_widget_get_parent (parent);
320 queue_resize_on_group (GtkSizeGroup *size_group)
322 GtkSizeGroupPrivate *priv = size_group->priv;
325 queue_resize_on_widget (priv->widgets->data, TRUE, 0);
329 initialize_size_group_quarks (void)
331 if (!size_groups_quark)
333 size_groups_quark = g_quark_from_static_string (size_groups_tag);
334 visited_quark = g_quark_from_static_string (visited_tag);
335 bumping_quark = g_quark_from_static_string (bumping_tag);
340 gtk_size_group_class_init (GtkSizeGroupClass *klass)
342 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
344 gobject_class->set_property = gtk_size_group_set_property;
345 gobject_class->get_property = gtk_size_group_get_property;
347 g_object_class_install_property (gobject_class,
349 g_param_spec_enum ("mode",
351 P_("The directions in which the size group affects the requested sizes"
352 " of its component widgets"),
353 GTK_TYPE_SIZE_GROUP_MODE,
354 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
357 * GtkSizeGroup:ignore-hidden:
359 * If %TRUE, unmapped widgets are ignored when determining
360 * the size of the group.
364 g_object_class_install_property (gobject_class,
366 g_param_spec_boolean ("ignore-hidden",
368 P_("If TRUE, unmapped widgets are ignored "
369 "when determining the size of the group"),
371 GTK_PARAM_READWRITE));
373 g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
375 initialize_size_group_quarks ();
379 gtk_size_group_init (GtkSizeGroup *size_group)
381 GtkSizeGroupPrivate *priv;
383 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
385 GtkSizeGroupPrivate);
386 priv = size_group->priv;
388 priv->widgets = NULL;
389 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
390 priv->have_width = 0;
391 priv->have_height = 0;
392 priv->ignore_hidden = 0;
396 gtk_size_group_buildable_init (GtkBuildableIface *iface)
398 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
399 iface->custom_finished = gtk_size_group_buildable_custom_finished;
402 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
403 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
404 gtk_size_group_buildable_init))
407 gtk_size_group_set_property (GObject *object,
412 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
417 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
419 case PROP_IGNORE_HIDDEN:
420 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429 gtk_size_group_get_property (GObject *object,
434 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
435 GtkSizeGroupPrivate *priv = size_group->priv;
440 g_value_set_enum (value, priv->mode);
442 case PROP_IGNORE_HIDDEN:
443 g_value_set_boolean (value, priv->ignore_hidden);
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 * gtk_size_group_new:
453 * @mode: the mode for the new size group.
455 * Create a new #GtkSizeGroup.
457 * Return value: a newly created #GtkSizeGroup
460 gtk_size_group_new (GtkSizeGroupMode mode)
462 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
463 GtkSizeGroupPrivate *priv = size_group->priv;
471 * gtk_size_group_set_mode:
472 * @size_group: a #GtkSizeGroup
473 * @mode: the mode to set for the size group.
475 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
476 * group determines whether the widgets in the size group should
477 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
478 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
479 * or should all have the same requisition in both directions
480 * (%GTK_SIZE_GROUP_MODE_BOTH).
483 gtk_size_group_set_mode (GtkSizeGroup *size_group,
484 GtkSizeGroupMode mode)
486 GtkSizeGroupPrivate *priv;
488 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
490 priv = size_group->priv;
492 if (priv->mode != mode)
494 if (priv->mode != GTK_SIZE_GROUP_NONE)
495 queue_resize_on_group (size_group);
497 if (priv->mode != GTK_SIZE_GROUP_NONE)
498 queue_resize_on_group (size_group);
500 g_object_notify (G_OBJECT (size_group), "mode");
505 * gtk_size_group_get_mode:
506 * @size_group: a #GtkSizeGroup
508 * Gets the current mode of the size group. See gtk_size_group_set_mode().
510 * Return value: the current mode of the size group.
513 gtk_size_group_get_mode (GtkSizeGroup *size_group)
515 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
517 return size_group->priv->mode;
521 * gtk_size_group_set_ignore_hidden:
522 * @size_group: a #GtkSizeGroup
523 * @ignore_hidden: whether unmapped widgets should be ignored
524 * when calculating the size
526 * Sets whether unmapped widgets should be ignored when
527 * calculating the size.
532 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
533 gboolean ignore_hidden)
535 GtkSizeGroupPrivate *priv;
537 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
539 priv = size_group->priv;
541 ignore_hidden = ignore_hidden != FALSE;
543 if (priv->ignore_hidden != ignore_hidden)
545 priv->ignore_hidden = ignore_hidden;
547 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
552 * gtk_size_group_get_ignore_hidden:
553 * @size_group: a #GtkSizeGroup
555 * Returns if invisible widgets are ignored when calculating the size.
557 * Returns: %TRUE if invisible widgets are ignored.
562 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
564 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
566 return size_group->priv->ignore_hidden;
570 gtk_size_group_widget_destroyed (GtkWidget *widget,
571 GtkSizeGroup *size_group)
573 gtk_size_group_remove_widget (size_group, widget);
577 * gtk_size_group_add_widget:
578 * @size_group: a #GtkSizeGroup
579 * @widget: the #GtkWidget to add
581 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
582 * of the widget will be determined as the maximum of its requisition
583 * and the requisition of the other widgets in the size group.
584 * Whether this applies horizontally, vertically, or in both directions
585 * depends on the mode of the size group. See gtk_size_group_set_mode().
587 * When the widget is destroyed or no longer referenced elsewhere, it will
588 * be removed from the size group.
591 gtk_size_group_add_widget (GtkSizeGroup *size_group,
594 GtkSizeGroupPrivate *priv;
597 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
598 g_return_if_fail (GTK_IS_WIDGET (widget));
600 priv = size_group->priv;
602 groups = get_size_groups (widget);
604 if (!g_slist_find (groups, size_group))
606 groups = g_slist_prepend (groups, size_group);
607 set_size_groups (widget, groups);
609 priv->widgets = g_slist_prepend (priv->widgets, widget);
611 g_signal_connect (widget, "destroy",
612 G_CALLBACK (gtk_size_group_widget_destroyed),
615 g_object_ref (size_group);
618 queue_resize_on_group (size_group);
622 * gtk_size_group_remove_widget:
623 * @size_group: a #GtkSizeGroup
624 * @widget: the #GtkWidget to remove
626 * Removes a widget from a #GtkSizeGroup.
629 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
632 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 groups = get_size_groups (widget);
647 groups = g_slist_remove (groups, size_group);
648 set_size_groups (widget, groups);
650 priv->widgets = g_slist_remove (priv->widgets, widget);
651 queue_resize_on_group (size_group);
652 gtk_widget_queue_resize (widget);
654 g_object_unref (size_group);
658 * gtk_size_group_get_widgets:
659 * @size_group: a #GtkSizeGroup
661 * Returns the list of widgets associated with @size_group.
663 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
664 * widgets. The list is owned by GTK+ and should not be modified.
669 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
671 return size_group->priv->widgets;
675 compute_dimension (GtkWidget *widget,
676 GtkSizeGroupMode mode,
677 gint *minimum, /* in-out */
678 gint *natural) /* in-out */
680 GSList *widgets = NULL;
681 GSList *groups = NULL;
683 gint min_result = 0, nat_result = 0;
685 add_widget_to_closure (widget, mode, &groups, &widgets);
687 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
688 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
690 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
694 min_result = *minimum;
695 nat_result = *natural;
699 GtkSizeGroup *group = groups->data;
700 GtkSizeGroupPrivate *priv = group->priv;
702 if (mode == GTK_SIZE_GROUP_HORIZONTAL && priv->have_width)
704 min_result = priv->minimum_req.width;
705 nat_result = priv->natural_req.width;
707 else if (mode == GTK_SIZE_GROUP_VERTICAL && priv->have_height)
709 min_result = priv->minimum_req.height;
710 nat_result = priv->natural_req.height;
717 GtkWidget *tmp_widget = tmp_list->data;
718 gint min_dimension, nat_dimension;
720 if (tmp_widget == widget)
722 min_dimension = *minimum;
723 nat_dimension = *natural;
727 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
728 gtk_widget_get_preferred_width (tmp_widget, &min_dimension, &nat_dimension);
730 gtk_widget_get_preferred_height (tmp_widget, &min_dimension, &nat_dimension);
733 if (gtk_widget_get_mapped (tmp_widget) || !priv->ignore_hidden)
735 min_result = MAX (min_result, min_dimension);
736 nat_result = MAX (nat_result, nat_dimension);
739 tmp_list = tmp_list->next;
745 GtkSizeGroup *tmp_group = tmp_list->data;
746 GtkSizeGroupPrivate *tmp_priv = tmp_group->priv;
748 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
750 tmp_priv->have_width = TRUE;
751 tmp_priv->minimum_req.width = min_result;
752 tmp_priv->natural_req.width = nat_result;
756 tmp_priv->have_height = TRUE;
758 tmp_priv->minimum_req.height = min_result;
759 tmp_priv->natural_req.height = nat_result;
762 tmp_list = tmp_list->next;
767 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
769 g_slist_free (widgets);
770 g_slist_free (groups);
772 *minimum = min_result;
773 *natural = nat_result;
777 * _gtk_size_group_bump_requisition:
778 * @widget: a #GtkWidget
779 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
780 * on the dimension in which to bump the size.
781 * @minimum: a pointer to the widget's minimum size
782 * @natural: a pointer to the widget's natural size
784 * Refreshes the sizegroup while returning the groups requested
785 * value in the dimension @mode.
787 * This function is used both to update sizegroup minimum and natural size
788 * information and widget minimum and natural sizes in multiple passes from
789 * the size request apis.
792 _gtk_size_group_bump_requisition (GtkWidget *widget,
793 GtkSizeGroupMode mode,
797 if (!is_bumping (widget))
799 /* Avoid recursion here */
800 mark_bumping (widget, TRUE);
802 if (get_size_groups (widget))
803 compute_dimension (widget, mode, minimum, natural);
805 mark_bumping (widget, FALSE);
812 * _gtk_size_group_queue_resize:
813 * @widget: a #GtkWidget
815 * Queue a resize on a widget, and on all other widgets grouped with this widget.
818 _gtk_size_group_queue_resize (GtkWidget *widget,
819 GtkQueueResizeFlags flags)
821 initialize_size_group_quarks ();
823 queue_resize_on_widget (widget, TRUE, flags);
829 } GSListSubParserData;
832 size_group_start_element (GMarkupParseContext *context,
833 const gchar *element_name,
835 const gchar **values,
840 GSListSubParserData *data = (GSListSubParserData*)user_data;
842 if (strcmp (element_name, "widget") == 0)
843 for (i = 0; names[i]; i++)
844 if (strcmp (names[i], "name") == 0)
845 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
846 else if (strcmp (element_name, "widgets") == 0)
849 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
854 static const GMarkupParser size_group_parser =
856 size_group_start_element
860 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
863 const gchar *tagname,
864 GMarkupParser *parser,
867 GSListSubParserData *parser_data;
872 if (strcmp (tagname, "widgets") == 0)
874 parser_data = g_slice_new0 (GSListSubParserData);
875 parser_data->items = NULL;
876 parser_data->object = G_OBJECT (buildable);
878 *parser = size_group_parser;
887 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
890 const gchar *tagname,
894 GSListSubParserData *data;
897 if (strcmp (tagname, "widgets"))
900 data = (GSListSubParserData*)user_data;
901 data->items = g_slist_reverse (data->items);
903 for (l = data->items; l; l = l->next)
905 object = gtk_builder_get_object (builder, l->data);
908 g_warning ("Unknown object %s specified in sizegroup %s",
909 (const gchar*)l->data,
910 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
913 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
914 GTK_WIDGET (object));
917 g_slist_free (data->items);
918 g_slice_free (GSListSubParserData, data);