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_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);
227 queue_resize_on_widget (tmp_list->data, FALSE);
229 tmp_list = tmp_list->next;
232 g_slist_free (widgets);
233 g_slist_free (groups);
238 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
239 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
240 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
242 reset_group_sizes (groups);
247 if (tmp_list->data == parent)
249 if (widget == parent)
250 real_queue_resize (parent);
253 queue_resize_on_widget (tmp_list->data, FALSE);
255 tmp_list = tmp_list->next;
258 g_slist_free (widgets);
259 g_slist_free (groups);
261 parent = parent->parent;
266 queue_resize_on_group (GtkSizeGroup *size_group)
268 if (size_group->widgets)
269 queue_resize_on_widget (size_group->widgets->data, TRUE);
273 initialize_size_group_quarks (void)
275 if (!size_groups_quark)
277 size_groups_quark = g_quark_from_static_string (size_groups_tag);
278 visited_quark = g_quark_from_static_string (visited_tag);
283 gtk_size_group_class_init (GtkSizeGroupClass *klass)
285 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
287 gobject_class->set_property = gtk_size_group_set_property;
288 gobject_class->get_property = gtk_size_group_get_property;
290 g_object_class_install_property (gobject_class,
292 g_param_spec_enum ("mode",
294 P_("The directions in which the size group affects the requested sizes"
295 " of its component widgets"),
296 GTK_TYPE_SIZE_GROUP_MODE,
297 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
300 * GtkSizeGroup:ignore-hidden:
302 * If %TRUE, unmapped widgets are ignored when determining
303 * the size of the group.
307 g_object_class_install_property (gobject_class,
309 g_param_spec_boolean ("ignore-hidden",
311 P_("If TRUE, unmapped widgets are ignored "
312 "when determining the size of the group"),
314 GTK_PARAM_READWRITE));
316 initialize_size_group_quarks ();
320 gtk_size_group_init (GtkSizeGroup *size_group)
322 size_group->widgets = NULL;
323 size_group->mode = GTK_SIZE_GROUP_HORIZONTAL;
324 size_group->have_width = 0;
325 size_group->have_height = 0;
326 size_group->ignore_hidden = 0;
330 gtk_size_group_buildable_init (GtkBuildableIface *iface)
332 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
333 iface->custom_finished = gtk_size_group_buildable_custom_finished;
336 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
337 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
338 gtk_size_group_buildable_init))
341 gtk_size_group_set_property (GObject *object,
346 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
351 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
353 case PROP_IGNORE_HIDDEN:
354 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gtk_size_group_get_property (GObject *object,
368 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
373 g_value_set_enum (value, size_group->mode);
375 case PROP_IGNORE_HIDDEN:
376 g_value_set_boolean (value, size_group->ignore_hidden);
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 * gtk_size_group_new:
386 * @mode: the mode for the new size group.
388 * Create a new #GtkSizeGroup.
390 * Return value: a newly created #GtkSizeGroup
393 gtk_size_group_new (GtkSizeGroupMode mode)
395 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
397 size_group->mode = mode;
403 * gtk_size_group_set_mode:
404 * @size_group: a #GtkSizeGroup
405 * @mode: the mode to set for the size group.
407 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
408 * group determines whether the widgets in the size group should
409 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
410 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
411 * or should all have the same requisition in both directions
412 * (%GTK_SIZE_GROUP_MODE_BOTH).
415 gtk_size_group_set_mode (GtkSizeGroup *size_group,
416 GtkSizeGroupMode mode)
418 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
420 if (size_group->mode != mode)
422 if (size_group->mode != GTK_SIZE_GROUP_NONE)
423 queue_resize_on_group (size_group);
424 size_group->mode = mode;
425 if (size_group->mode != GTK_SIZE_GROUP_NONE)
426 queue_resize_on_group (size_group);
428 g_object_notify (G_OBJECT (size_group), "mode");
433 * gtk_size_group_get_mode:
434 * @size_group: a #GtkSizeGroup
436 * Gets the current mode of the size group. See gtk_size_group_set_mode().
438 * Return value: the current mode of the size group.
441 gtk_size_group_get_mode (GtkSizeGroup *size_group)
443 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
445 return size_group->mode;
449 * gtk_size_group_set_ignore_hidden:
450 * @size_group: a #GtkSizeGroup
451 * @ignore_hidden: whether unmapped widgets should be ignored
452 * when calculating the size
454 * Sets whether unmapped widgets should be ignored when
455 * calculating the size.
460 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
461 gboolean ignore_hidden)
463 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
465 ignore_hidden = ignore_hidden != FALSE;
467 if (size_group->ignore_hidden != ignore_hidden)
469 size_group->ignore_hidden = ignore_hidden;
471 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
476 * gtk_size_group_get_ignore_hidden:
477 * @size_group: a #GtkSizeGroup
479 * Returns if invisible widgets are ignored when calculating the size.
481 * Returns: %TRUE if invisible widgets are ignored.
486 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
488 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
490 return size_group->ignore_hidden;
494 gtk_size_group_widget_destroyed (GtkWidget *widget,
495 GtkSizeGroup *size_group)
497 gtk_size_group_remove_widget (size_group, widget);
501 * gtk_size_group_add_widget:
502 * @size_group: a #GtkSizeGroup
503 * @widget: the #GtkWidget to add
505 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
506 * of the widget will be determined as the maximum of its requisition
507 * and the requisition of the other widgets in the size group.
508 * Whether this applies horizontally, vertically, or in both directions
509 * depends on the mode of the size group. See gtk_size_group_set_mode().
511 * When the widget is destroyed or no longer referenced elsewhere, it will
512 * be removed from the size group.
515 gtk_size_group_add_widget (GtkSizeGroup *size_group,
520 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
521 g_return_if_fail (GTK_IS_WIDGET (widget));
523 groups = get_size_groups (widget);
525 if (!g_slist_find (groups, size_group))
527 groups = g_slist_prepend (groups, size_group);
528 set_size_groups (widget, groups);
530 size_group->widgets = g_slist_prepend (size_group->widgets, widget);
532 g_signal_connect (widget, "destroy",
533 G_CALLBACK (gtk_size_group_widget_destroyed),
536 g_object_ref (size_group);
539 queue_resize_on_group (size_group);
543 * gtk_size_group_remove_widget:
544 * @size_group: a #GtkSizeGrup
545 * @widget: the #GtkWidget to remove
547 * Removes a widget from a #GtkSizeGroup.
550 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
555 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
556 g_return_if_fail (GTK_IS_WIDGET (widget));
557 g_return_if_fail (g_slist_find (size_group->widgets, widget));
559 g_signal_handlers_disconnect_by_func (widget,
560 gtk_size_group_widget_destroyed,
563 groups = get_size_groups (widget);
564 groups = g_slist_remove (groups, size_group);
565 set_size_groups (widget, groups);
567 size_group->widgets = g_slist_remove (size_group->widgets, widget);
568 queue_resize_on_group (size_group);
569 gtk_widget_queue_resize (widget);
571 g_object_unref (size_group);
575 * gtk_size_group_get_widgets:
576 * @size_group: a #GtkSizeGrup
578 * Returns the list of widgets associated with @size_group.
580 * Return value: a #GSList of widgets. The list is owned by GTK+
581 * and should not be modified.
586 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
588 return size_group->widgets;
592 get_base_dimension (GtkWidget *widget,
593 GtkSizeGroupMode mode)
595 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
597 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
599 if (aux_info && aux_info->width > 0)
600 return aux_info->width;
602 return widget->requisition.width;
606 if (aux_info && aux_info->height > 0)
607 return aux_info->height;
609 return widget->requisition.height;
614 do_size_request (GtkWidget *widget)
616 if (GTK_WIDGET_REQUEST_NEEDED (widget))
618 gtk_widget_ensure_style (widget);
619 GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
620 g_signal_emit_by_name (widget,
622 &widget->requisition);
627 compute_base_dimension (GtkWidget *widget,
628 GtkSizeGroupMode mode)
630 do_size_request (widget);
632 return get_base_dimension (widget, mode);
636 compute_dimension (GtkWidget *widget,
637 GtkSizeGroupMode mode)
639 GSList *widgets = NULL;
640 GSList *groups = NULL;
644 add_widget_to_closure (widget, mode, &groups, &widgets);
646 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
647 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
649 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
653 result = compute_base_dimension (widget, mode);
657 GtkSizeGroup *group = groups->data;
659 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
660 result = group->requisition.width;
661 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
662 result = group->requisition.height;
668 GtkWidget *tmp_widget = tmp_list->data;
670 gint dimension = compute_base_dimension (tmp_widget, mode);
672 if (GTK_WIDGET_MAPPED (tmp_widget) || !group->ignore_hidden)
674 if (dimension > result)
678 tmp_list = tmp_list->next;
684 GtkSizeGroup *tmp_group = tmp_list->data;
686 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
688 tmp_group->have_width = TRUE;
689 tmp_group->requisition.width = result;
693 tmp_group->have_height = TRUE;
694 tmp_group->requisition.height = result;
697 tmp_list = tmp_list->next;
702 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
704 g_slist_free (widgets);
705 g_slist_free (groups);
711 get_dimension (GtkWidget *widget,
712 GtkSizeGroupMode mode)
714 GSList *widgets = NULL;
715 GSList *groups = NULL;
718 add_widget_to_closure (widget, mode, &groups, &widgets);
720 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
721 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
725 result = get_base_dimension (widget, mode);
729 GtkSizeGroup *group = groups->data;
731 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
732 result = group->requisition.width;
733 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
734 result = group->requisition.height;
737 g_slist_free (widgets);
738 g_slist_free (groups);
744 get_fast_child_requisition (GtkWidget *widget,
745 GtkRequisition *requisition)
747 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
749 *requisition = widget->requisition;
753 if (aux_info->width > 0)
754 requisition->width = aux_info->width;
755 if (aux_info && aux_info->height > 0)
756 requisition->height = aux_info->height;
761 * _gtk_size_group_get_child_requisition:
762 * @widget: a #GtkWidget
763 * @requisition: location to store computed requisition.
765 * Retrieve the "child requisition" of the widget, taking account grouping
766 * of the widget's requisition with other widgets.
769 _gtk_size_group_get_child_requisition (GtkWidget *widget,
770 GtkRequisition *requisition)
772 initialize_size_group_quarks ();
776 if (get_size_groups (widget))
778 requisition->width = get_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
779 requisition->height = get_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
781 /* Only do the full computation if we actually have size groups */
784 get_fast_child_requisition (widget, requisition);
789 * _gtk_size_group_compute_requisition:
790 * @widget: a #GtkWidget
791 * @requisition: location to store computed requisition.
793 * Compute the requisition of a widget taking into account grouping of
794 * the widget's requisition with other widgets.
797 _gtk_size_group_compute_requisition (GtkWidget *widget,
798 GtkRequisition *requisition)
803 initialize_size_group_quarks ();
805 if (get_size_groups (widget))
807 /* Only do the full computation if we actually have size groups */
809 width = compute_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
810 height = compute_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
814 requisition->width = width;
815 requisition->height = height;
820 do_size_request (widget);
823 get_fast_child_requisition (widget, requisition);
828 * _gtk_size_group_queue_resize:
829 * @widget: a #GtkWidget
831 * Queue a resize on a widget, and on all other widgets grouped with this widget.
834 _gtk_size_group_queue_resize (GtkWidget *widget)
836 initialize_size_group_quarks ();
838 queue_resize_on_widget (widget, TRUE);
844 } GSListSubParserData;
847 size_group_start_element (GMarkupParseContext *context,
848 const gchar *element_name,
850 const gchar **values,
855 GSListSubParserData *data = (GSListSubParserData*)user_data;
857 if (strcmp (element_name, "widget") == 0)
858 for (i = 0; names[i]; i++)
859 if (strcmp (names[i], "name") == 0)
860 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
861 else if (strcmp (element_name, "widgets") == 0)
864 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
869 static const GMarkupParser size_group_parser =
871 size_group_start_element
875 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
878 const gchar *tagname,
879 GMarkupParser *parser,
882 GSListSubParserData *parser_data;
887 if (strcmp (tagname, "widgets") == 0)
889 parser_data = g_slice_new0 (GSListSubParserData);
890 parser_data->items = NULL;
891 parser_data->object = G_OBJECT (buildable);
893 *parser = size_group_parser;
902 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
905 const gchar *tagname,
909 GSListSubParserData *data;
912 if (strcmp (tagname, "widgets"))
915 data = (GSListSubParserData*)user_data;
916 data->items = g_slist_reverse (data->items);
918 for (l = data->items; l; l = l->next)
920 object = gtk_builder_get_object (builder, l->data);
923 g_warning ("Unknown object %s specified in sizegroup %s",
924 (const gchar*)l->data,
925 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
928 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
929 GTK_WIDGET (object));
932 g_slist_free (data->items);
933 g_slice_free (GSListSubParserData, data);
937 #define __GTK_SIZE_GROUP_C__
938 #include "gtkaliasdef.c"