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 "gtkextendedlayout.h"
37 static void gtk_size_group_set_property (GObject *object,
41 static void gtk_size_group_get_property (GObject *object,
46 static void add_group_to_closure (GtkSizeGroup *group,
47 GtkSizeGroupMode mode,
50 static void add_widget_to_closure (GtkWidget *widget,
51 GtkSizeGroupMode mode,
56 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
57 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
61 GMarkupParser *parser,
63 static void gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
69 static GQuark size_groups_quark;
70 static const gchar size_groups_tag[] = "gtk-size-groups";
72 static GQuark visited_quark;
73 static const gchar visited_tag[] = "gtk-size-group-visited";
75 static GQuark bumping_quark;
76 static const gchar bumping_tag[] = "gtk-size-group-bumping";
79 get_size_groups (GtkWidget *widget)
81 return g_object_get_qdata (G_OBJECT (widget), size_groups_quark);
85 set_size_groups (GtkWidget *widget,
88 g_object_set_qdata (G_OBJECT (widget), size_groups_quark, groups);
92 mark_visited (gpointer object)
94 g_object_set_qdata (object, visited_quark, "visited");
98 mark_unvisited (gpointer object)
100 g_object_set_qdata (object, visited_quark, NULL);
104 is_visited (gpointer object)
106 return g_object_get_qdata (object, visited_quark) != NULL;
110 mark_bumping (gpointer object, gboolean bumping)
112 g_object_set_qdata (object, bumping_quark, bumping ? "bumping" : NULL);
116 is_bumping (gpointer object)
118 return g_object_get_qdata (object, bumping_quark) != NULL;
122 add_group_to_closure (GtkSizeGroup *group,
123 GtkSizeGroupMode mode,
129 *groups = g_slist_prepend (*groups, group);
130 mark_visited (group);
132 tmp_widgets = group->widgets;
135 GtkWidget *tmp_widget = tmp_widgets->data;
137 if (!is_visited (tmp_widget))
138 add_widget_to_closure (tmp_widget, mode, groups, widgets);
140 tmp_widgets = tmp_widgets->next;
145 add_widget_to_closure (GtkWidget *widget,
146 GtkSizeGroupMode mode,
152 *widgets = g_slist_prepend (*widgets, widget);
153 mark_visited (widget);
155 tmp_groups = get_size_groups (widget);
158 GtkSizeGroup *tmp_group = tmp_groups->data;
160 if ((tmp_group->mode == GTK_SIZE_GROUP_BOTH || tmp_group->mode == mode) &&
161 !is_visited (tmp_group))
162 add_group_to_closure (tmp_group, mode, groups, widgets);
164 tmp_groups = tmp_groups->next;
169 real_queue_resize (GtkWidget *widget)
171 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
172 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
173 GTK_PRIVATE_SET_FLAG (widget, GTK_WIDTH_REQUEST_NEEDED);
174 GTK_PRIVATE_SET_FLAG (widget, GTK_HEIGHT_REQUEST_NEEDED);
177 _gtk_container_queue_resize (GTK_CONTAINER (widget->parent));
178 else if (gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
179 _gtk_container_queue_resize (GTK_CONTAINER (widget));
183 reset_group_sizes (GSList *groups)
185 GSList *tmp_list = groups;
188 GtkSizeGroup *tmp_group = tmp_list->data;
190 tmp_group->have_width = FALSE;
191 tmp_group->have_height = FALSE;
193 tmp_list = tmp_list->next;
198 queue_resize_on_widget (GtkWidget *widget,
199 gboolean check_siblings)
201 GtkWidget *parent = widget;
206 GSList *widget_groups;
210 if (widget == parent && !check_siblings)
212 real_queue_resize (widget);
213 parent = parent->parent;
217 widget_groups = get_size_groups (parent);
220 if (widget == parent)
221 real_queue_resize (widget);
223 parent = parent->parent;
230 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
231 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
232 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
234 reset_group_sizes (groups);
239 if (tmp_list->data == parent)
241 if (widget == parent)
242 real_queue_resize (parent);
244 else if (tmp_list->data == widget)
246 g_warning ("A container and its child are part of this SizeGroup");
249 queue_resize_on_widget (tmp_list->data, FALSE);
251 tmp_list = tmp_list->next;
254 g_slist_free (widgets);
255 g_slist_free (groups);
260 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
261 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
262 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
264 reset_group_sizes (groups);
269 if (tmp_list->data == parent)
271 if (widget == parent)
272 real_queue_resize (parent);
274 else if (tmp_list->data == widget)
276 g_warning ("A container and its child are part of this SizeGroup");
279 queue_resize_on_widget (tmp_list->data, FALSE);
281 tmp_list = tmp_list->next;
284 g_slist_free (widgets);
285 g_slist_free (groups);
287 parent = parent->parent;
292 queue_resize_on_group (GtkSizeGroup *size_group)
294 if (size_group->widgets)
295 queue_resize_on_widget (size_group->widgets->data, TRUE);
299 initialize_size_group_quarks (void)
301 if (!size_groups_quark)
303 size_groups_quark = g_quark_from_static_string (size_groups_tag);
304 visited_quark = g_quark_from_static_string (visited_tag);
305 bumping_quark = g_quark_from_static_string (bumping_tag);
310 gtk_size_group_class_init (GtkSizeGroupClass *klass)
312 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
314 gobject_class->set_property = gtk_size_group_set_property;
315 gobject_class->get_property = gtk_size_group_get_property;
317 g_object_class_install_property (gobject_class,
319 g_param_spec_enum ("mode",
321 P_("The directions in which the size group affects the requested sizes"
322 " of its component widgets"),
323 GTK_TYPE_SIZE_GROUP_MODE,
324 GTK_SIZE_GROUP_HORIZONTAL, GTK_PARAM_READWRITE));
327 * GtkSizeGroup:ignore-hidden:
329 * If %TRUE, unmapped widgets are ignored when determining
330 * the size of the group.
334 g_object_class_install_property (gobject_class,
336 g_param_spec_boolean ("ignore-hidden",
338 P_("If TRUE, unmapped widgets are ignored "
339 "when determining the size of the group"),
341 GTK_PARAM_READWRITE));
343 initialize_size_group_quarks ();
347 gtk_size_group_init (GtkSizeGroup *size_group)
349 size_group->widgets = NULL;
350 size_group->mode = GTK_SIZE_GROUP_HORIZONTAL;
351 size_group->have_width = 0;
352 size_group->have_height = 0;
353 size_group->ignore_hidden = 0;
357 gtk_size_group_buildable_init (GtkBuildableIface *iface)
359 iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
360 iface->custom_finished = gtk_size_group_buildable_custom_finished;
363 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
364 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
365 gtk_size_group_buildable_init))
368 gtk_size_group_set_property (GObject *object,
373 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
378 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
380 case PROP_IGNORE_HIDDEN:
381 gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390 gtk_size_group_get_property (GObject *object,
395 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
400 g_value_set_enum (value, size_group->mode);
402 case PROP_IGNORE_HIDDEN:
403 g_value_set_boolean (value, size_group->ignore_hidden);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412 * gtk_size_group_new:
413 * @mode: the mode for the new size group.
415 * Create a new #GtkSizeGroup.
417 * Return value: a newly created #GtkSizeGroup
420 gtk_size_group_new (GtkSizeGroupMode mode)
422 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
424 size_group->mode = mode;
430 * gtk_size_group_set_mode:
431 * @size_group: a #GtkSizeGroup
432 * @mode: the mode to set for the size group.
434 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
435 * group determines whether the widgets in the size group should
436 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
437 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
438 * or should all have the same requisition in both directions
439 * (%GTK_SIZE_GROUP_MODE_BOTH).
442 gtk_size_group_set_mode (GtkSizeGroup *size_group,
443 GtkSizeGroupMode mode)
445 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
447 if (size_group->mode != mode)
449 if (size_group->mode != GTK_SIZE_GROUP_NONE)
450 queue_resize_on_group (size_group);
451 size_group->mode = mode;
452 if (size_group->mode != GTK_SIZE_GROUP_NONE)
453 queue_resize_on_group (size_group);
455 g_object_notify (G_OBJECT (size_group), "mode");
460 * gtk_size_group_get_mode:
461 * @size_group: a #GtkSizeGroup
463 * Gets the current mode of the size group. See gtk_size_group_set_mode().
465 * Return value: the current mode of the size group.
468 gtk_size_group_get_mode (GtkSizeGroup *size_group)
470 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
472 return size_group->mode;
476 * gtk_size_group_set_ignore_hidden:
477 * @size_group: a #GtkSizeGroup
478 * @ignore_hidden: whether unmapped widgets should be ignored
479 * when calculating the size
481 * Sets whether unmapped widgets should be ignored when
482 * calculating the size.
487 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
488 gboolean ignore_hidden)
490 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
492 ignore_hidden = ignore_hidden != FALSE;
494 if (size_group->ignore_hidden != ignore_hidden)
496 size_group->ignore_hidden = ignore_hidden;
498 g_object_notify (G_OBJECT (size_group), "ignore-hidden");
503 * gtk_size_group_get_ignore_hidden:
504 * @size_group: a #GtkSizeGroup
506 * Returns if invisible widgets are ignored when calculating the size.
508 * Returns: %TRUE if invisible widgets are ignored.
513 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
515 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
517 return size_group->ignore_hidden;
521 gtk_size_group_widget_destroyed (GtkWidget *widget,
522 GtkSizeGroup *size_group)
524 gtk_size_group_remove_widget (size_group, widget);
528 * gtk_size_group_add_widget:
529 * @size_group: a #GtkSizeGroup
530 * @widget: the #GtkWidget to add
532 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
533 * of the widget will be determined as the maximum of its requisition
534 * and the requisition of the other widgets in the size group.
535 * Whether this applies horizontally, vertically, or in both directions
536 * depends on the mode of the size group. See gtk_size_group_set_mode().
538 * When the widget is destroyed or no longer referenced elsewhere, it will
539 * be removed from the size group.
542 gtk_size_group_add_widget (GtkSizeGroup *size_group,
547 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
548 g_return_if_fail (GTK_IS_WIDGET (widget));
550 groups = get_size_groups (widget);
552 if (!g_slist_find (groups, size_group))
554 groups = g_slist_prepend (groups, size_group);
555 set_size_groups (widget, groups);
557 size_group->widgets = g_slist_prepend (size_group->widgets, widget);
559 g_signal_connect (widget, "destroy",
560 G_CALLBACK (gtk_size_group_widget_destroyed),
563 g_object_ref (size_group);
566 queue_resize_on_group (size_group);
570 * gtk_size_group_remove_widget:
571 * @size_group: a #GtkSizeGroup
572 * @widget: the #GtkWidget to remove
574 * Removes a widget from a #GtkSizeGroup.
577 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
582 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
583 g_return_if_fail (GTK_IS_WIDGET (widget));
584 g_return_if_fail (g_slist_find (size_group->widgets, widget));
586 g_signal_handlers_disconnect_by_func (widget,
587 gtk_size_group_widget_destroyed,
590 groups = get_size_groups (widget);
591 groups = g_slist_remove (groups, size_group);
592 set_size_groups (widget, groups);
594 size_group->widgets = g_slist_remove (size_group->widgets, widget);
595 queue_resize_on_group (size_group);
596 gtk_widget_queue_resize (widget);
598 g_object_unref (size_group);
602 * gtk_size_group_get_widgets:
603 * @size_group: a #GtkSizeGroup
605 * Returns the list of widgets associated with @size_group.
607 * Return value: (element-type GtkWidget) (transfer none): a #GSList of
608 * widgets. The list is owned by GTK+ and should not be modified.
613 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
615 return size_group->widgets;
619 get_base_dimension (GtkWidget *widget,
620 GtkSizeGroupMode mode)
622 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
624 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
626 if (aux_info && aux_info->width > 0)
627 return aux_info->width;
630 /* XXX Possibly we should be using natural values and not minimums here. */
633 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (widget), &width, NULL);
640 if (aux_info && aux_info->height > 0)
641 return aux_info->height;
644 /* XXX Possibly we should be using natural values and not minimums here. */
647 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (widget), &height, NULL);
655 compute_dimension (GtkWidget *widget,
656 GtkSizeGroupMode mode,
657 gint widget_requisition)
659 GSList *widgets = NULL;
660 GSList *groups = NULL;
664 add_widget_to_closure (widget, mode, &groups, &widgets);
666 g_slist_foreach (widgets, (GFunc)mark_unvisited, NULL);
667 g_slist_foreach (groups, (GFunc)mark_unvisited, NULL);
669 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
673 result = widget_requisition;
677 GtkSizeGroup *group = groups->data;
679 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
680 result = group->requisition.width;
681 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
682 result = group->requisition.height;
688 GtkWidget *tmp_widget = tmp_list->data;
691 if (tmp_widget == widget)
692 dimension = widget_requisition;
694 dimension = get_base_dimension (tmp_widget, mode);
696 if (gtk_widget_get_mapped (tmp_widget) || !group->ignore_hidden)
698 if (dimension > result)
702 tmp_list = tmp_list->next;
708 GtkSizeGroup *tmp_group = tmp_list->data;
710 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
712 tmp_group->have_width = TRUE;
713 tmp_group->requisition.width = result;
717 tmp_group->have_height = TRUE;
718 tmp_group->requisition.height = result;
721 tmp_list = tmp_list->next;
726 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
728 g_slist_free (widgets);
729 g_slist_free (groups);
735 * _gtk_size_group_bump_requisition:
736 * @widget: a #GtkWidget
737 * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
738 * on the dimension in which to bump the size.
740 * Refreshes the sizegroup while returning the groups requested
741 * value in the dimension @mode.
743 * This function is used to update sizegroup minimum size information
744 * in multiple passes from the new #GtkExtendedLayout manager.
747 _gtk_size_group_bump_requisition (GtkWidget *widget,
748 GtkSizeGroupMode mode,
749 gint widget_requisition)
751 gint result = widget_requisition;
753 if (!is_bumping (widget))
755 GtkWidgetAuxInfo *aux_info =
756 _gtk_widget_get_aux_info (widget, FALSE);
758 /* Avoid recursion here */
759 mark_bumping (widget, TRUE);
761 if (get_size_groups (widget))
765 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
766 result = compute_dimension (widget, mode, MAX (aux_info->width, widget_requisition));
768 result = compute_dimension (widget, mode, MAX (aux_info->height, widget_requisition));
771 result = compute_dimension (widget, mode, widget_requisition);
775 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
776 result = MAX (aux_info->width, widget_requisition);
778 result = MAX (aux_info->height, widget_requisition);
780 mark_bumping (widget, FALSE);
788 * _gtk_size_group_queue_resize:
789 * @widget: a #GtkWidget
791 * Queue a resize on a widget, and on all other widgets grouped with this widget.
794 _gtk_size_group_queue_resize (GtkWidget *widget)
796 initialize_size_group_quarks ();
798 queue_resize_on_widget (widget, TRUE);
804 } GSListSubParserData;
807 size_group_start_element (GMarkupParseContext *context,
808 const gchar *element_name,
810 const gchar **values,
815 GSListSubParserData *data = (GSListSubParserData*)user_data;
817 if (strcmp (element_name, "widget") == 0)
818 for (i = 0; names[i]; i++)
819 if (strcmp (names[i], "name") == 0)
820 data->items = g_slist_prepend (data->items, g_strdup (values[i]));
821 else if (strcmp (element_name, "widgets") == 0)
824 g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
829 static const GMarkupParser size_group_parser =
831 size_group_start_element
835 gtk_size_group_buildable_custom_tag_start (GtkBuildable *buildable,
838 const gchar *tagname,
839 GMarkupParser *parser,
842 GSListSubParserData *parser_data;
847 if (strcmp (tagname, "widgets") == 0)
849 parser_data = g_slice_new0 (GSListSubParserData);
850 parser_data->items = NULL;
851 parser_data->object = G_OBJECT (buildable);
853 *parser = size_group_parser;
862 gtk_size_group_buildable_custom_finished (GtkBuildable *buildable,
865 const gchar *tagname,
869 GSListSubParserData *data;
872 if (strcmp (tagname, "widgets"))
875 data = (GSListSubParserData*)user_data;
876 data->items = g_slist_reverse (data->items);
878 for (l = data->items; l; l = l->next)
880 object = gtk_builder_get_object (builder, l->data);
883 g_warning ("Unknown object %s specified in sizegroup %s",
884 (const gchar*)l->data,
885 gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
888 gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
889 GTK_WIDGET (object));
892 g_slist_free (data->items);
893 g_slice_free (GSListSubParserData, data);
897 #define __GTK_SIZE_GROUP_C__
898 #include "gtkaliasdef.c"