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.
23 #include "gtkcontainer.h"
25 #include "gtkprivate.h"
26 #include "gtksizegroup.h"
27 #include "gtkbuildable.h"
28 #include "gtksizerequest.h"
32 struct _GtkSizeGroupPriv
34 GtkRequisition requisition;
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 GtkSizeGroupPriv *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 GtkSizeGroupPriv *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)
187 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
188 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
189 GTK_PRIVATE_SET_FLAG (widget, GTK_WIDTH_REQUEST_NEEDED);
190 GTK_PRIVATE_SET_FLAG (widget, GTK_HEIGHT_REQUEST_NEEDED);
193 _gtk_container_queue_resize (GTK_CONTAINER (widget->parent));
194 else if (gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
195 _gtk_container_queue_resize (GTK_CONTAINER (widget));
199 reset_group_sizes (GSList *groups)
201 GSList *tmp_list = groups;
204 GtkSizeGroup *tmp_group = tmp_list->data;
205 GtkSizeGroupPriv *tmp_priv = tmp_group->priv;
207 tmp_priv->have_width = FALSE;
208 tmp_priv->have_height = FALSE;
210 tmp_list = tmp_list->next;
215 queue_resize_on_widget (GtkWidget *widget,
216 gboolean check_siblings)
218 GtkWidget *parent = widget;
223 GSList *widget_groups;
227 if (widget == parent && !check_siblings)
229 real_queue_resize (widget);
230 parent = parent->parent;
234 widget_groups = get_size_groups (parent);
237 if (widget == parent)
238 real_queue_resize (widget);
240 parent = parent->parent;
247 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
248 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
249 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
251 reset_group_sizes (groups);
256 if (tmp_list->data == parent)
258 if (widget == parent)
259 real_queue_resize (parent);
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);
268 tmp_list = tmp_list->next;
271 g_slist_free (widgets);
272 g_slist_free (groups);
277 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
278 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
279 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
281 reset_group_sizes (groups);
286 if (tmp_list->data == parent)
288 if (widget == parent)
289 real_queue_resize (parent);
291 else if (tmp_list->data == widget)
293 g_warning ("A container and its child are part of this SizeGroup");
296 queue_resize_on_widget (tmp_list->data, FALSE);
298 tmp_list = tmp_list->next;
301 g_slist_free (widgets);
302 g_slist_free (groups);
304 parent = parent->parent;
309 queue_resize_on_group (GtkSizeGroup *size_group)
311 GtkSizeGroupPriv *priv = size_group->priv;
314 queue_resize_on_widget (priv->widgets->data, TRUE);
318 initialize_size_group_quarks (void)
320 if (!size_groups_quark)
322 size_groups_quark = g_quark_from_static_string (size_groups_tag);
323 visited_quark = g_quark_from_static_string (visited_tag);
324 bumping_quark = g_quark_from_static_string (bumping_tag);
329 gtk_size_group_class_init (GtkSizeGroupClass *klass)
331 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333 gobject_class->set_property = gtk_size_group_set_property;
334 gobject_class->get_property = gtk_size_group_get_property;
336 g_object_class_install_property (gobject_class,
338 g_param_spec_enum ("mode",
340 P_("The directions in which the size group affects the requested sizes"
341 " of its component widgets"),
342 GTK_TYPE_SIZE_GROUP_MODE,
343 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
346 * GtkSizeGroup:ignore-hidden:
348 * If %TRUE, unmapped widgets are ignored when determining
349 * the size of the group.
353 g_object_class_install_property (gobject_class,
355 g_param_spec_boolean ("ignore-hidden",
357 P_("If TRUE, unmapped widgets are ignored "
358 "when determining the size of the group"),
360 GTK_PARAM_READWRITE));
362 g_type_class_add_private (klass, sizeof (GtkSizeGroupPriv));
364 initialize_size_group_quarks ();
368 gtk_size_group_init (GtkSizeGroup *size_group)
370 GtkSizeGroupPriv *priv;
372 size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
375 priv = size_group->priv;
377 priv->widgets = NULL;
378 priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
379 priv->have_width = 0;
380 priv->have_height = 0;
381 priv->ignore_hidden = 0;
385 gtk_size_group_buildable_init (GtkBuildableIface *iface)
387 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
388 iface->custom_finished = gtk_size_group_buildable_custom_finished;
391 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
392 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
393 gtk_size_group_buildable_init))
396 gtk_size_group_set_property (GObject *object,
401 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
406 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
408 case PROP_IGNORE_HIDDEN:
409 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 gtk_size_group_get_property (GObject *object,
423 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
424 GtkSizeGroupPriv *priv = size_group->priv;
429 g_value_set_enum (value, priv->mode);
431 case PROP_IGNORE_HIDDEN:
432 g_value_set_boolean (value, priv->ignore_hidden);
435 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441 * gtk_size_group_new:
442 * @mode: the mode for the new size group.
444 * Create a new #GtkSizeGroup.
446 * Return value: a newly created #GtkSizeGroup
449 gtk_size_group_new (GtkSizeGroupMode mode)
451 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
452 GtkSizeGroupPriv *priv = size_group->priv;
460 * gtk_size_group_set_mode:
461 * @size_group: a #GtkSizeGroup
462 * @mode: the mode to set for the size group.
464 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
465 * group determines whether the widgets in the size group should
466 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
467 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
468 * or should all have the same requisition in both directions
469 * (%GTK_SIZE_GROUP_MODE_BOTH).
472 gtk_size_group_set_mode (GtkSizeGroup *size_group,
473 GtkSizeGroupMode mode)
475 GtkSizeGroupPriv *priv;
477 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
479 priv = size_group->priv;
481 if (priv->mode != mode)
483 if (priv->mode != GTK_SIZE_GROUP_NONE)
484 queue_resize_on_group (size_group);
486 if (priv->mode != GTK_SIZE_GROUP_NONE)
487 queue_resize_on_group (size_group);
489 g_object_notify (G_OBJECT (size_group), "mode");
494 * gtk_size_group_get_mode:
495 * @size_group: a #GtkSizeGroup
497 * Gets the current mode of the size group. See gtk_size_group_set_mode().
499 * Return value: the current mode of the size group.
502 gtk_size_group_get_mode (GtkSizeGroup *size_group)
504 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
506 return size_group->priv->mode;
510 * gtk_size_group_set_ignore_hidden:
511 * @size_group: a #GtkSizeGroup
512 * @ignore_hidden: whether unmapped widgets should be ignored
513 * when calculating the size
515 * Sets whether unmapped widgets should be ignored when
516 * calculating the size.
521 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
522 gboolean ignore_hidden)
524 GtkSizeGroupPriv *priv;
526 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
528 priv = size_group->priv;
530 ignore_hidden = ignore_hidden != FALSE;
532 if (priv->ignore_hidden != ignore_hidden)
534 priv->ignore_hidden = ignore_hidden;
536 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
541 * gtk_size_group_get_ignore_hidden:
542 * @size_group: a #GtkSizeGroup
544 * Returns if invisible widgets are ignored when calculating the size.
546 * Returns: %TRUE if invisible widgets are ignored.
551 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
553 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
555 return size_group->priv->ignore_hidden;
559 gtk_size_group_widget_destroyed (GtkWidget *widget,
560 GtkSizeGroup *size_group)
562 gtk_size_group_remove_widget (size_group, widget);
566 * gtk_size_group_add_widget:
567 * @size_group: a #GtkSizeGroup
568 * @widget: the #GtkWidget to add
570 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
571 * of the widget will be determined as the maximum of its requisition
572 * and the requisition of the other widgets in the size group.
573 * Whether this applies horizontally, vertically, or in both directions
574 * depends on the mode of the size group. See gtk_size_group_set_mode().
576 * When the widget is destroyed or no longer referenced elsewhere, it will
577 * be removed from the size group.
580 gtk_size_group_add_widget (GtkSizeGroup *size_group,
583 GtkSizeGroupPriv *priv;
586 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
587 g_return_if_fail (GTK_IS_WIDGET (widget));
589 priv = size_group->priv;
591 groups = get_size_groups (widget);
593 if (!g_slist_find (groups, size_group))
595 groups = g_slist_prepend (groups, size_group);
596 set_size_groups (widget, groups);
598 priv->widgets = g_slist_prepend (priv->widgets, widget);
600 g_signal_connect (widget, "destroy",
601 G_CALLBACK (gtk_size_group_widget_destroyed),
604 g_object_ref (size_group);
607 queue_resize_on_group (size_group);
611 * gtk_size_group_remove_widget:
612 * @size_group: a #GtkSizeGroup
613 * @widget: the #GtkWidget to remove
615 * Removes a widget from a #GtkSizeGroup.
618 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
621 GtkSizeGroupPriv *priv;
624 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
625 g_return_if_fail (GTK_IS_WIDGET (widget));
627 priv = size_group->priv;
629 g_return_if_fail (g_slist_find (priv->widgets, widget));
631 g_signal_handlers_disconnect_by_func (widget,
632 gtk_size_group_widget_destroyed,
635 groups = get_size_groups (widget);
636 groups = g_slist_remove (groups, size_group);
637 set_size_groups (widget, groups);
639 priv->widgets = g_slist_remove (priv->widgets, widget);
640 queue_resize_on_group (size_group);
641 gtk_widget_queue_resize (widget);
643 g_object_unref (size_group);
647 * gtk_size_group_get_widgets:
648 * @size_group: a #GtkSizeGroup
650 * Returns the list of widgets associated with @size_group.
652 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
653 * widgets. The list is owned by GTK+ and should not be modified.
658 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
660 return size_group->priv->widgets;
664 get_base_dimension (GtkWidget *widget,
665 GtkSizeGroupMode mode)
667 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
669 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
671 if (aux_info && aux_info->width > 0)
672 return aux_info->width;
675 /* XXX Possibly we should be using natural values and not minimums here. */
678 gtk_size_request_get_width (GTK_SIZE_REQUEST (widget), &width, NULL);
685 if (aux_info && aux_info->height > 0)
686 return aux_info->height;
689 /* XXX Possibly we should be using natural values and not minimums here. */
692 gtk_size_request_get_height (GTK_SIZE_REQUEST (widget), &height, NULL);
700 compute_dimension (GtkWidget *widget,
701 GtkSizeGroupMode mode,
702 gint widget_requisition)
704 GSList *widgets = NULL;
705 GSList *groups = NULL;
709 add_widget_to_closure (widget, mode, &groups, &widgets);
711 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
712 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
714 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
718 result = widget_requisition;
722 GtkSizeGroup *group = groups->data;
723 GtkSizeGroupPriv *priv = group->priv;
725 if (mode == GTK_SIZE_GROUP_HORIZONTAL && priv->have_width)
726 result = priv->requisition.width;
727 else if (mode == GTK_SIZE_GROUP_VERTICAL && priv->have_height)
728 result = priv->requisition.height;
734 GtkWidget *tmp_widget = tmp_list->data;
737 if (tmp_widget == widget)
738 dimension = widget_requisition;
740 dimension = get_base_dimension (tmp_widget, mode);
742 if (gtk_widget_get_mapped (tmp_widget) || !priv->ignore_hidden)
744 if (dimension > result)
748 tmp_list = tmp_list->next;
754 GtkSizeGroup *tmp_group = tmp_list->data;
755 GtkSizeGroupPriv *tmp_priv = tmp_group->priv;
757 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
759 tmp_priv->have_width = TRUE;
760 tmp_priv->requisition.width = result;
764 tmp_priv->have_height = TRUE;
765 tmp_priv->requisition.height = result;
768 tmp_list = tmp_list->next;
773 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
775 g_slist_free (widgets);
776 g_slist_free (groups);
782 * _gtk_size_group_bump_requisition:
783 * @widget: a #GtkWidget
784 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
785 * on the dimension in which to bump the size.
787 * Refreshes the sizegroup while returning the groups requested
788 * value in the dimension @mode.
790 * This function is used to update sizegroup minimum size information
791 * in multiple passes from the new #GtkSizeRequest manager.
794 _gtk_size_group_bump_requisition (GtkWidget *widget,
795 GtkSizeGroupMode mode,
796 gint widget_requisition)
798 gint result = widget_requisition;
800 if (!is_bumping (widget))
802 GtkWidgetAuxInfo *aux_info =
803 _gtk_widget_get_aux_info (widget, FALSE);
805 /* Avoid recursion here */
806 mark_bumping (widget, TRUE);
808 if (get_size_groups (widget))
812 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
813 result = compute_dimension (widget, mode, MAX (aux_info->width, widget_requisition));
815 result = compute_dimension (widget, mode, MAX (aux_info->height, widget_requisition));
818 result = compute_dimension (widget, mode, widget_requisition);
822 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
823 result = MAX (aux_info->width, widget_requisition);
825 result = MAX (aux_info->height, widget_requisition);
827 mark_bumping (widget, FALSE);
835 * _gtk_size_group_queue_resize:
836 * @widget: a #GtkWidget
838 * Queue a resize on a widget, and on all other widgets grouped with this widget.
841 _gtk_size_group_queue_resize (GtkWidget *widget)
843 initialize_size_group_quarks ();
845 queue_resize_on_widget (widget, TRUE);
851 } GSListSubParserData;
854 size_group_start_element (GMarkupParseContext *context,
855 const gchar *element_name,
857 const gchar **values,
862 GSListSubParserData *data = (GSListSubParserData*)user_data;
864 if (strcmp (element_name, "widget") == 0)
865 for (i = 0; names[i]; i++)
866 if (strcmp (names[i], "name") == 0)
867 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
868 else if (strcmp (element_name, "widgets") == 0)
871 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
876 static const GMarkupParser size_group_parser =
878 size_group_start_element
882 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
885 const gchar *tagname,
886 GMarkupParser *parser,
889 GSListSubParserData *parser_data;
894 if (strcmp (tagname, "widgets") == 0)
896 parser_data = g_slice_new0 (GSListSubParserData);
897 parser_data->items = NULL;
898 parser_data->object = G_OBJECT (buildable);
900 *parser = size_group_parser;
909 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
912 const gchar *tagname,
916 GSListSubParserData *data;
919 if (strcmp (tagname, "widgets"))
922 data = (GSListSubParserData*)user_data;
923 data->items = g_slist_reverse (data->items);
925 for (l = data->items; l; l = l->next)
927 object = gtk_builder_get_object (builder, l->data);
930 g_warning ("Unknown object %s specified in sizegroup %s",
931 (const gchar*)l->data,
932 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
935 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
936 GTK_WIDGET (object));
939 g_slist_free (data->items);
940 g_slice_free (GSListSubParserData, data);