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"
36 static void gtk_size_group_set_property (GObject *object,
40 static void gtk_size_group_get_property (GObject *object,
45 static void add_group_to_closure (GtkSizeGroup *group,
46 GtkSizeGroupMode mode,
49 static void add_widget_to_closure (GtkWidget *widget,
50 GtkSizeGroupMode mode,
55 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
56 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
60 GMarkupParser *parser,
62 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
68 static GQuark size_groups_quark;
69 static const gchar size_groups_tag[] = "gtk-size-groups";
71 static GQuark visited_quark;
72 static const gchar visited_tag[] = "gtk-size-group-visited";
75 get_size_groups (GtkWidget *widget)
77 return g_object_get_qdata (G_OBJECT (widget), size_groups_quark);
81 set_size_groups (GtkWidget *widget,
84 g_object_set_qdata (G_OBJECT (widget), size_groups_quark, groups);
88 mark_visited (gpointer object)
90 g_object_set_qdata (object, visited_quark, "visited");
94 mark_unvisited (gpointer object)
96 g_object_set_qdata (object, visited_quark, NULL);
100 is_visited (gpointer object)
102 return g_object_get_qdata (object, visited_quark) != NULL;
106 add_group_to_closure (GtkSizeGroup *group,
107 GtkSizeGroupMode mode,
113 *groups = g_slist_prepend (*groups, group);
114 mark_visited (group);
116 tmp_widgets = group->widgets;
119 GtkWidget *tmp_widget = tmp_widgets->data;
121 if (!is_visited (tmp_widget))
122 add_widget_to_closure (tmp_widget, mode, groups, widgets);
124 tmp_widgets = tmp_widgets->next;
129 add_widget_to_closure (GtkWidget *widget,
130 GtkSizeGroupMode mode,
136 *widgets = g_slist_prepend (*widgets, widget);
137 mark_visited (widget);
139 tmp_groups = get_size_groups (widget);
142 GtkSizeGroup *tmp_group = tmp_groups->data;
144 if ((tmp_group->mode == GTK_SIZE_GROUP_BOTH || tmp_group->mode == mode) &&
145 !is_visited (tmp_group))
146 add_group_to_closure (tmp_group, mode, groups, widgets);
148 tmp_groups = tmp_groups->next;
153 real_queue_resize (GtkWidget *widget)
155 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
156 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
159 _gtk_container_queue_resize (GTK_CONTAINER (widget->parent));
160 else if (gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
161 _gtk_container_queue_resize (GTK_CONTAINER (widget));
165 reset_group_sizes (GSList *groups)
167 GSList *tmp_list = groups;
170 GtkSizeGroup *tmp_group = tmp_list->data;
172 tmp_group->have_width = FALSE;
173 tmp_group->have_height = FALSE;
175 tmp_list = tmp_list->next;
180 queue_resize_on_widget (GtkWidget *widget,
181 gboolean check_siblings)
183 GtkWidget *parent = widget;
188 GSList *widget_groups;
192 if (widget == parent && !check_siblings)
194 real_queue_resize (widget);
195 parent = parent->parent;
199 widget_groups = get_size_groups (parent);
202 if (widget == parent)
203 real_queue_resize (widget);
205 parent = parent->parent;
212 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
213 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
214 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
216 reset_group_sizes (groups);
221 if (tmp_list->data == parent)
223 if (widget == parent)
224 real_queue_resize (parent);
226 else if (tmp_list->data == widget)
228 g_warning ("A container and its child are part of this SizeGroup");
231 queue_resize_on_widget (tmp_list->data, FALSE);
233 tmp_list = tmp_list->next;
236 g_slist_free (widgets);
237 g_slist_free (groups);
242 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
243 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
244 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
246 reset_group_sizes (groups);
251 if (tmp_list->data == parent)
253 if (widget == parent)
254 real_queue_resize (parent);
256 else if (tmp_list->data == widget)
258 g_warning ("A container and its child are part of this SizeGroup");
261 queue_resize_on_widget (tmp_list->data, FALSE);
263 tmp_list = tmp_list->next;
266 g_slist_free (widgets);
267 g_slist_free (groups);
269 parent = parent->parent;
274 queue_resize_on_group (GtkSizeGroup *size_group)
276 if (size_group->widgets)
277 queue_resize_on_widget (size_group->widgets->data, TRUE);
281 initialize_size_group_quarks (void)
283 if (!size_groups_quark)
285 size_groups_quark = g_quark_from_static_string (size_groups_tag);
286 visited_quark = g_quark_from_static_string (visited_tag);
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 initialize_size_group_quarks ();
328 gtk_size_group_init (GtkSizeGroup *size_group)
330 size_group->widgets = NULL;
331 size_group->mode = GTK_SIZE_GROUP_HORIZONTAL;
332 size_group->have_width = 0;
333 size_group->have_height = 0;
334 size_group->ignore_hidden = 0;
338 gtk_size_group_buildable_init (GtkBuildableIface *iface)
340 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
341 iface->custom_finished = gtk_size_group_buildable_custom_finished;
344 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
345 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
346 gtk_size_group_buildable_init))
349 gtk_size_group_set_property (GObject *object,
354 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
359 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
361 case PROP_IGNORE_HIDDEN:
362 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gtk_size_group_get_property (GObject *object,
376 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
381 g_value_set_enum (value, size_group->mode);
383 case PROP_IGNORE_HIDDEN:
384 g_value_set_boolean (value, size_group->ignore_hidden);
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 * gtk_size_group_new:
394 * @mode: the mode for the new size group.
396 * Create a new #GtkSizeGroup.
398 * Return value: a newly created #GtkSizeGroup
401 gtk_size_group_new (GtkSizeGroupMode mode)
403 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
405 size_group->mode = mode;
411 * gtk_size_group_set_mode:
412 * @size_group: a #GtkSizeGroup
413 * @mode: the mode to set for the size group.
415 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
416 * group determines whether the widgets in the size group should
417 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
418 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
419 * or should all have the same requisition in both directions
420 * (%GTK_SIZE_GROUP_MODE_BOTH).
423 gtk_size_group_set_mode (GtkSizeGroup *size_group,
424 GtkSizeGroupMode mode)
426 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
428 if (size_group->mode != mode)
430 if (size_group->mode != GTK_SIZE_GROUP_NONE)
431 queue_resize_on_group (size_group);
432 size_group->mode = mode;
433 if (size_group->mode != GTK_SIZE_GROUP_NONE)
434 queue_resize_on_group (size_group);
436 g_object_notify (G_OBJECT (size_group), "mode");
441 * gtk_size_group_get_mode:
442 * @size_group: a #GtkSizeGroup
444 * Gets the current mode of the size group. See gtk_size_group_set_mode().
446 * Return value: the current mode of the size group.
449 gtk_size_group_get_mode (GtkSizeGroup *size_group)
451 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
453 return size_group->mode;
457 * gtk_size_group_set_ignore_hidden:
458 * @size_group: a #GtkSizeGroup
459 * @ignore_hidden: whether unmapped widgets should be ignored
460 * when calculating the size
462 * Sets whether unmapped widgets should be ignored when
463 * calculating the size.
468 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
469 gboolean ignore_hidden)
471 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
473 ignore_hidden = ignore_hidden != FALSE;
475 if (size_group->ignore_hidden != ignore_hidden)
477 size_group->ignore_hidden = ignore_hidden;
479 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
484 * gtk_size_group_get_ignore_hidden:
485 * @size_group: a #GtkSizeGroup
487 * Returns if invisible widgets are ignored when calculating the size.
489 * Returns: %TRUE if invisible widgets are ignored.
494 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
496 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
498 return size_group->ignore_hidden;
502 gtk_size_group_widget_destroyed (GtkWidget *widget,
503 GtkSizeGroup *size_group)
505 gtk_size_group_remove_widget (size_group, widget);
509 * gtk_size_group_add_widget:
510 * @size_group: a #GtkSizeGroup
511 * @widget: the #GtkWidget to add
513 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
514 * of the widget will be determined as the maximum of its requisition
515 * and the requisition of the other widgets in the size group.
516 * Whether this applies horizontally, vertically, or in both directions
517 * depends on the mode of the size group. See gtk_size_group_set_mode().
519 * When the widget is destroyed or no longer referenced elsewhere, it will
520 * be removed from the size group.
523 gtk_size_group_add_widget (GtkSizeGroup *size_group,
528 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
529 g_return_if_fail (GTK_IS_WIDGET (widget));
531 groups = get_size_groups (widget);
533 if (!g_slist_find (groups, size_group))
535 groups = g_slist_prepend (groups, size_group);
536 set_size_groups (widget, groups);
538 size_group->widgets = g_slist_prepend (size_group->widgets, widget);
540 g_signal_connect (widget, "destroy",
541 G_CALLBACK (gtk_size_group_widget_destroyed),
544 g_object_ref (size_group);
547 queue_resize_on_group (size_group);
551 * gtk_size_group_remove_widget:
552 * @size_group: a #GtkSizeGrup
553 * @widget: the #GtkWidget to remove
555 * Removes a widget from a #GtkSizeGroup.
558 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
563 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
564 g_return_if_fail (GTK_IS_WIDGET (widget));
565 g_return_if_fail (g_slist_find (size_group->widgets, widget));
567 g_signal_handlers_disconnect_by_func (widget,
568 gtk_size_group_widget_destroyed,
571 groups = get_size_groups (widget);
572 groups = g_slist_remove (groups, size_group);
573 set_size_groups (widget, groups);
575 size_group->widgets = g_slist_remove (size_group->widgets, widget);
576 queue_resize_on_group (size_group);
577 gtk_widget_queue_resize (widget);
579 g_object_unref (size_group);
583 * gtk_size_group_get_widgets:
584 * @size_group: a #GtkSizeGrup
586 * Returns the list of widgets associated with @size_group.
588 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
589 * widgets. The list is owned by GTK+ and should not be modified.
594 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
596 return size_group->widgets;
600 get_base_dimension (GtkWidget *widget,
601 GtkSizeGroupMode mode)
603 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
605 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
607 if (aux_info && aux_info->width > 0)
608 return aux_info->width;
610 return widget->requisition.width;
614 if (aux_info && aux_info->height > 0)
615 return aux_info->height;
617 return widget->requisition.height;
622 do_size_request (GtkWidget *widget)
624 if (GTK_WIDGET_REQUEST_NEEDED (widget))
626 gtk_widget_ensure_style (widget);
627 GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
628 g_signal_emit_by_name (widget,
630 &widget->requisition);
635 compute_base_dimension (GtkWidget *widget,
636 GtkSizeGroupMode mode)
638 do_size_request (widget);
640 return get_base_dimension (widget, mode);
644 compute_dimension (GtkWidget *widget,
645 GtkSizeGroupMode mode)
647 GSList *widgets = NULL;
648 GSList *groups = NULL;
652 add_widget_to_closure (widget, mode, &groups, &widgets);
654 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
655 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
657 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
661 result = compute_base_dimension (widget, mode);
665 GtkSizeGroup *group = groups->data;
667 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
668 result = group->requisition.width;
669 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
670 result = group->requisition.height;
676 GtkWidget *tmp_widget = tmp_list->data;
678 gint dimension = compute_base_dimension (tmp_widget, mode);
680 if (GTK_WIDGET_MAPPED (tmp_widget) || !group->ignore_hidden)
682 if (dimension > result)
686 tmp_list = tmp_list->next;
692 GtkSizeGroup *tmp_group = tmp_list->data;
694 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
696 tmp_group->have_width = TRUE;
697 tmp_group->requisition.width = result;
701 tmp_group->have_height = TRUE;
702 tmp_group->requisition.height = result;
705 tmp_list = tmp_list->next;
710 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
712 g_slist_free (widgets);
713 g_slist_free (groups);
719 get_dimension (GtkWidget *widget,
720 GtkSizeGroupMode mode)
722 GSList *widgets = NULL;
723 GSList *groups = NULL;
726 add_widget_to_closure (widget, mode, &groups, &widgets);
728 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
729 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
733 result = get_base_dimension (widget, mode);
737 GtkSizeGroup *group = groups->data;
739 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
740 result = group->requisition.width;
741 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
742 result = group->requisition.height;
745 g_slist_free (widgets);
746 g_slist_free (groups);
752 get_fast_child_requisition (GtkWidget *widget,
753 GtkRequisition *requisition)
755 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
757 *requisition = widget->requisition;
761 if (aux_info->width > 0)
762 requisition->width = aux_info->width;
763 if (aux_info && aux_info->height > 0)
764 requisition->height = aux_info->height;
769 * _gtk_size_group_get_child_requisition:
770 * @widget: a #GtkWidget
771 * @requisition: location to store computed requisition.
773 * Retrieve the "child requisition" of the widget, taking account grouping
774 * of the widget's requisition with other widgets.
777 _gtk_size_group_get_child_requisition (GtkWidget *widget,
778 GtkRequisition *requisition)
780 initialize_size_group_quarks ();
784 if (get_size_groups (widget))
786 requisition->width = get_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
787 requisition->height = get_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
789 /* Only do the full computation if we actually have size groups */
792 get_fast_child_requisition (widget, requisition);
797 * _gtk_size_group_compute_requisition:
798 * @widget: a #GtkWidget
799 * @requisition: location to store computed requisition.
801 * Compute the requisition of a widget taking into account grouping of
802 * the widget's requisition with other widgets.
805 _gtk_size_group_compute_requisition (GtkWidget *widget,
806 GtkRequisition *requisition)
811 initialize_size_group_quarks ();
813 if (get_size_groups (widget))
815 /* Only do the full computation if we actually have size groups */
817 width = compute_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
818 height = compute_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
822 requisition->width = width;
823 requisition->height = height;
828 do_size_request (widget);
831 get_fast_child_requisition (widget, requisition);
836 * _gtk_size_group_queue_resize:
837 * @widget: a #GtkWidget
839 * Queue a resize on a widget, and on all other widgets grouped with this widget.
842 _gtk_size_group_queue_resize (GtkWidget *widget)
844 initialize_size_group_quarks ();
846 queue_resize_on_widget (widget, TRUE);
852 } GSListSubParserData;
855 size_group_start_element (GMarkupParseContext *context,
856 const gchar *element_name,
858 const gchar **values,
863 GSListSubParserData *data = (GSListSubParserData*)user_data;
865 if (strcmp (element_name, "widget") == 0)
866 for (i = 0; names[i]; i++)
867 if (strcmp (names[i], "name") == 0)
868 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
869 else if (strcmp (element_name, "widgets") == 0)
872 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
877 static const GMarkupParser size_group_parser =
879 size_group_start_element
883 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
886 const gchar *tagname,
887 GMarkupParser *parser,
890 GSListSubParserData *parser_data;
895 if (strcmp (tagname, "widgets") == 0)
897 parser_data = g_slice_new0 (GSListSubParserData);
898 parser_data->items = NULL;
899 parser_data->object = G_OBJECT (buildable);
901 *parser = size_group_parser;
910 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
913 const gchar *tagname,
917 GSListSubParserData *data;
920 if (strcmp (tagname, "widgets"))
923 data = (GSListSubParserData*)user_data;
924 data->items = g_slist_reverse (data->items);
926 for (l = data->items; l; l = l->next)
928 object = gtk_builder_get_object (builder, l->data);
931 g_warning ("Unknown object %s specified in sizegroup %s",
932 (const gchar*)l->data,
933 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
936 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
937 GTK_WIDGET (object));
940 g_slist_free (data->items);
941 g_slice_free (GSListSubParserData, data);
945 #define __GTK_SIZE_GROUP_C__
946 #include "gtkaliasdef.c"