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.
21 #include "gtkcontainer.h"
23 #include "gtkprivate.h"
24 #include "gtksignal.h"
25 #include "gtksizegroup.h"
32 static void gtk_size_group_set_property (GObject *object,
36 static void gtk_size_group_get_property (GObject *object,
41 static void add_group_to_closure (GtkSizeGroup *group,
42 GtkSizeGroupMode mode,
45 static void add_widget_to_closure (GtkWidget *widget,
46 GtkSizeGroupMode mode,
50 static GQuark size_groups_quark;
51 static const gchar *size_groups_tag = "gtk-size-groups";
54 get_size_groups (GtkWidget *widget)
56 if (!size_groups_quark)
57 size_groups_quark = g_quark_from_string (size_groups_tag);
59 return g_object_get_qdata (G_OBJECT (widget), size_groups_quark);
63 set_size_groups (GtkWidget *widget,
66 if (!size_groups_quark)
67 size_groups_quark = g_quark_from_string (size_groups_tag);
69 g_object_set_qdata (G_OBJECT (widget), size_groups_quark, groups);
73 add_group_to_closure (GtkSizeGroup *group,
74 GtkSizeGroupMode mode,
80 *groups = g_slist_prepend (*groups, group);
82 tmp_widgets = group->widgets;
85 GtkWidget *tmp_widget = tmp_widgets->data;
87 if (!g_slist_find (*widgets, tmp_widget))
88 add_widget_to_closure (tmp_widget, mode, groups, widgets);
90 tmp_widgets = tmp_widgets->next;
95 add_widget_to_closure (GtkWidget *widget,
96 GtkSizeGroupMode mode,
102 *widgets = g_slist_prepend (*widgets, widget);
104 tmp_groups = get_size_groups (widget);
107 GtkSizeGroup *tmp_group = tmp_groups->data;
109 if ((tmp_group->mode == GTK_SIZE_GROUP_BOTH || tmp_group->mode == mode) &&
110 !g_slist_find (*groups, tmp_group))
111 add_group_to_closure (tmp_group, mode, groups, widgets);
113 tmp_groups = tmp_groups->next;
118 real_queue_resize (GtkWidget *widget)
120 GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
121 GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
124 _gtk_container_queue_resize (GTK_CONTAINER (widget->parent));
125 else if (GTK_WIDGET_TOPLEVEL (widget) && GTK_IS_CONTAINER (widget))
126 _gtk_container_queue_resize (GTK_CONTAINER (widget));
130 reset_group_sizes (GSList *groups)
132 GSList *tmp_list = groups;
135 GtkSizeGroup *tmp_group = tmp_list->data;
137 tmp_group->have_width = FALSE;
138 tmp_group->have_height = FALSE;
140 tmp_list = tmp_list->next;
145 queue_resize_on_widget (GtkWidget *widget,
146 gboolean check_siblings)
148 GtkWidget *parent = widget;
153 GSList *widget_groups;
157 if (widget == parent && !check_siblings)
159 real_queue_resize (widget);
160 parent = parent->parent;
164 widget_groups = get_size_groups (parent);
167 if (widget == parent)
168 real_queue_resize (widget);
170 parent = parent->parent;
177 add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
178 reset_group_sizes (groups);
183 if (tmp_list->data == parent)
185 if (widget == parent)
186 real_queue_resize (parent);
189 queue_resize_on_widget (tmp_list->data, FALSE);
191 tmp_list = tmp_list->next;
194 g_slist_free (widgets);
195 g_slist_free (groups);
200 add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
201 reset_group_sizes (groups);
206 if (tmp_list->data == parent)
208 if (widget == parent)
209 real_queue_resize (parent);
212 queue_resize_on_widget (tmp_list->data, FALSE);
214 tmp_list = tmp_list->next;
217 g_slist_free (widgets);
218 g_slist_free (groups);
220 parent = parent->parent;
225 queue_resize_on_group (GtkSizeGroup *size_group)
227 if (size_group->widgets)
228 queue_resize_on_widget (size_group->widgets->data, TRUE);
232 gtk_size_group_class_init (GtkSizeGroupClass *klass)
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 gobject_class->set_property = gtk_size_group_set_property;
237 gobject_class->get_property = gtk_size_group_get_property;
239 g_object_class_install_property (gobject_class,
241 g_param_spec_enum ("mode",
243 _("The the directions in which the size group effects the requested sizes"
244 " of its component widgets."),
245 GTK_TYPE_SIZE_GROUP_MODE,
246 GTK_SIZE_GROUP_HORIZONTAL,
251 gtk_size_group_init (GtkSizeGroup *size_group)
253 size_group->widgets = NULL;
254 size_group->mode = GTK_SIZE_GROUP_HORIZONTAL;
255 size_group->have_width = 0;
256 size_group->have_height = 0;
260 gtk_size_group_get_type (void)
262 static GtkType size_group_type = 0;
264 if (!size_group_type)
266 static const GTypeInfo size_group_info =
268 sizeof (GtkSizeGroupClass),
269 NULL, /* base_init */
270 NULL, /* base_finalize */
271 (GClassInitFunc) gtk_size_group_class_init,
272 NULL, /* class_finalize */
273 NULL, /* class_data */
274 sizeof (GtkSizeGroup),
275 16, /* n_preallocs */
276 (GInstanceInitFunc) gtk_size_group_init,
279 size_group_type = g_type_register_static (G_TYPE_OBJECT, "GtkSizeGroup", &size_group_info, 0);
282 return size_group_type;
286 gtk_size_group_set_property (GObject *object,
291 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
296 gtk_size_group_set_mode (size_group, g_value_get_enum (value));
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 gtk_size_group_get_property (GObject *object,
310 GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
315 g_value_set_enum (value, size_group->mode);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 * gtk_size_group_new:
325 * @mode: the mode for the new size group.
327 * Create a new #GtkSizeGroup.
329 * Return value: a newly created #GtkSizeGroup
332 gtk_size_group_new (GtkSizeGroupMode mode)
334 GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
336 size_group->mode = mode;
342 * gtk_size_group_set_mode:
343 * @size_group: a #GtkSizeGroup
344 * @mode: the mode to set for the size group.
346 * Sets the #GtkSizeGroupMode of the size group. The mode of the size
347 * group determines whether the widgets in the size group should
348 * all have the same horizontal requisition (%GTK_SIZE_GROUP_MODE_HORIZONTAL)
349 * all have the same vertical requisition (%GTK_SIZE_GROUP_MODE_VERTICAL),
350 * or should all have the same requisition in both directions
351 * (%GTK_SIZE_GROUP_MODE_BOTH).
354 gtk_size_group_set_mode (GtkSizeGroup *size_group,
355 GtkSizeGroupMode mode)
357 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
359 if (size_group->mode != mode)
361 size_group->mode = mode;
362 queue_resize_on_group (size_group);
367 * gtk_size_group_get_mode:
368 * @size_group: a #GtkSizeGroup
370 * Gets the current mode of the size group. See gtk_size_group_set_mode().
372 * Return value: the current mode of the size group.
375 gtk_size_group_get_mode (GtkSizeGroup *size_group)
377 g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
379 return size_group->mode;
383 gtk_size_group_widget_destroyed (GtkWidget *widget,
384 GtkSizeGroup *size_group)
386 gtk_size_group_remove_widget (size_group, widget);
390 * gtk_size_group_add_widget:
391 * @size_group: a #GtkSizeGroup
392 * @widget: the #GtkWidget to add
394 * Adds a widget to a #GtkSizeGroup. In the future, the requisition
395 * of the widget will be determined as the maximum of its requisition
396 * and the requisition of the other widgets in the size group.
397 * Whether this applies horizontally, vertically, or in both directions
398 * depends on the mode of the size group. See gtk_size_group_set_mode().
401 gtk_size_group_add_widget (GtkSizeGroup *size_group,
406 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
407 g_return_if_fail (GTK_IS_WIDGET (widget));
409 groups = get_size_groups (widget);
411 if (!g_slist_find (groups, widget))
413 groups = g_slist_prepend (groups, size_group);
414 set_size_groups (widget, groups);
416 size_group->widgets = g_slist_prepend (size_group->widgets, widget);
418 gtk_signal_connect (GTK_OBJECT (widget), "destroy",
419 GTK_SIGNAL_FUNC (gtk_size_group_widget_destroyed), size_group);
421 g_object_ref (G_OBJECT (size_group));
424 queue_resize_on_group (size_group);
428 * gtk_size_group_remove_widget:
429 * @size_group: a #GtkSizeGrup
430 * @widget: the #GtkWidget to remove
432 * Removes a widget from a #GtkSizeGroup.
435 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
440 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
441 g_return_if_fail (GTK_IS_WIDGET (widget));
442 g_return_if_fail (g_slist_find (size_group->widgets, widget));
444 gtk_signal_disconnect_by_func (GTK_OBJECT (widget),
445 GTK_SIGNAL_FUNC (gtk_size_group_widget_destroyed), size_group);
447 groups = get_size_groups (widget);
448 groups = g_slist_remove (groups, size_group);
449 set_size_groups (widget, groups);
451 size_group->widgets = g_slist_remove (size_group->widgets, widget);
452 queue_resize_on_group (size_group);
453 gtk_widget_queue_resize (widget);
455 g_object_unref (G_OBJECT (size_group));
459 get_base_dimension (GtkWidget *widget,
460 GtkSizeGroupMode mode)
462 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
464 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
466 if (aux_info && aux_info->width > 0)
467 return aux_info->width;
469 return widget->requisition.width;
473 if (aux_info && aux_info->height > 0)
474 return aux_info->height;
476 return widget->requisition.height;
481 do_size_request (GtkWidget *widget)
483 if (GTK_WIDGET_REQUEST_NEEDED (widget))
485 gtk_widget_ensure_style (widget);
486 gtk_signal_emit_by_name (GTK_OBJECT (widget), "size_request", &widget->requisition);
488 GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
493 compute_base_dimension (GtkWidget *widget,
494 GtkSizeGroupMode mode)
496 do_size_request (widget);
498 return get_base_dimension (widget, mode);
502 compute_dimension (GtkWidget *widget,
503 GtkSizeGroupMode mode)
505 GSList *widgets = NULL;
506 GSList *groups = NULL;
510 add_widget_to_closure (widget, mode, &groups, &widgets);
512 g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
516 result = compute_base_dimension (widget, mode);
520 GtkSizeGroup *group = groups->data;
522 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
523 result = group->requisition.width;
524 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
525 result = group->requisition.height;
531 GtkWidget *tmp_widget = tmp_list->data;
533 gint dimension = compute_base_dimension (tmp_widget, mode);
535 if (dimension > result)
538 tmp_list = tmp_list->next;
544 GtkSizeGroup *tmp_group = tmp_list->data;
546 if (mode == GTK_SIZE_GROUP_HORIZONTAL)
548 tmp_group->have_width = TRUE;
549 tmp_group->requisition.width = result;
553 tmp_group->have_height = TRUE;
554 tmp_group->requisition.height = result;
557 tmp_list = tmp_list->next;
562 g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
564 g_slist_free (widgets);
565 g_slist_free (groups);
571 get_dimension (GtkWidget *widget,
572 GtkSizeGroupMode mode)
574 GSList *widgets = NULL;
575 GSList *groups = NULL;
578 add_widget_to_closure (widget, mode, &groups, &widgets);
582 result = get_base_dimension (widget, mode);
586 GtkSizeGroup *group = groups->data;
588 if (mode == GTK_SIZE_GROUP_HORIZONTAL && group->have_width)
589 result = group->requisition.width;
590 else if (mode == GTK_SIZE_GROUP_VERTICAL && group->have_height)
591 result = group->requisition.height;
594 g_slist_free (widgets);
595 g_slist_free (groups);
601 get_fast_child_requisition (GtkWidget *widget,
602 GtkRequisition *requisition)
604 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
606 *requisition = widget->requisition;
610 if (aux_info->width > 0)
611 requisition->width = aux_info->width;
612 if (aux_info && aux_info->height > 0)
613 requisition->height = aux_info->height;
618 * _gtk_size_group_get_child_requisition:
619 * @widget: a #GtkWidget
620 * @requisition: location to store computed requisition.
622 * Retrieve the "child requisition" of the widget, taking account grouping
623 * of the widget's requisition with other widgets.
626 _gtk_size_group_get_child_requisition (GtkWidget *widget,
627 GtkRequisition *requisition)
631 if (get_size_groups (widget))
633 requisition->width = get_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
634 requisition->height = get_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
636 /* Only do the full computation if we actually have size groups */
639 get_fast_child_requisition (widget, requisition);
644 * _gtk_size_group_compute_requisition:
645 * @widget: a #GtkWidget
646 * @requisition: location to store computed requisition.
648 * Compute the requisition of a widget taking into account grouping of
649 * the widget's requisition with other widgets.
652 _gtk_size_group_compute_requisition (GtkWidget *widget,
653 GtkRequisition *requisition)
658 if (get_size_groups (widget))
660 /* Only do the full computation if we actually have size groups */
662 width = compute_dimension (widget, GTK_SIZE_GROUP_HORIZONTAL);
663 height = compute_dimension (widget, GTK_SIZE_GROUP_VERTICAL);
667 requisition->width = width;
668 requisition->height = height;
673 do_size_request (widget);
676 get_fast_child_requisition (widget, requisition);
681 * _gtk_size_group_queue_resize:
682 * @widget: a #GtkWidget
684 * Queue a resize on a widget, and on all other widgets grouped with this widget.
687 _gtk_size_group_queue_resize (GtkWidget *widget)
689 queue_resize_on_widget (widget, TRUE);