]> Pileus Git - ~andy/gtk/blob - gtk/gtksizegroup.c
4ab1850e3498a4deea2c48a3bac07d0f9e9aa8c4
[~andy/gtk] / gtk / gtksizegroup.c
1 /* GTK - The GIMP Toolkit
2  * gtksizegroup.c: 
3  * Copyright (C) 2001 Red Hat Software
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20 #include <string.h>
21
22 #include "gtkbuildable.h"
23 #include "gtkcontainer.h"
24 #include "gtkintl.h"
25 #include "gtktypebuiltins.h"
26 #include "gtkprivate.h"
27 #include "gtksizegroup-private.h"
28 #include "gtkwidgetprivate.h"
29 #include "gtkcontainerprivate.h"
30
31
32 /**
33  * SECTION:gtksizegroup
34  * @Short_description: Grouping widgets so they request the same size
35  * @Title: GtkSizeGroup
36  *
37  * #GtkSizeGroup provides a mechanism for grouping a number of widgets
38  * together so they all request the same amount of space.  This is
39  * typically useful when you want a column of widgets to have the same
40  * size, but you can't use a #GtkGrid widget.
41  *
42  * In detail, the size requested for each widget in a #GtkSizeGroup is
43  * the maximum of the sizes that would have been requested for each
44  * widget in the size group if they were not in the size group. The mode
45  * of the size group (see gtk_size_group_set_mode()) determines whether
46  * this applies to the horizontal size, the vertical size, or both sizes.
47  *
48  * Note that size groups only affect the amount of space requested, not
49  * the size that the widgets finally receive. If you want the widgets in
50  * a #GtkSizeGroup to actually be the same size, you need to pack them in
51  * such a way that they get the size they request and not more. For
52  * example, if you are packing your widgets into a table, you would not
53  * include the %GTK_FILL flag.
54  *
55  * #GtkSizeGroup objects are referenced by each widget in the size group,
56  * so once you have added all widgets to a #GtkSizeGroup, you can drop
57  * the initial reference to the size group with g_object_unref(). If the
58  * widgets in the size group are subsequently destroyed, then they will
59  * be removed from the size group and drop their references on the size
60  * group; when all widgets have been removed, the size group will be
61  * freed.
62  *
63  * Widgets can be part of multiple size groups; GTK+ will compute the
64  * horizontal size of a widget from the horizontal requisition of all
65  * widgets that can be reached from the widget by a chain of size groups
66  * of type %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_BOTH, and the
67  * vertical size from the vertical requisition of all widgets that can be
68  * reached from the widget by a chain of size groups of type
69  * %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH.
70  *
71  * Note that only non-contextual sizes of every widget are ever consulted
72  * by size groups (since size groups have no knowledge of what size a widget
73  * will be allocated in one dimension, it cannot derive how much height
74  * a widget will receive for a given width). When grouping widgets that
75  * trade height for width in mode %GTK_SIZE_GROUP_VERTICAL or %GTK_SIZE_GROUP_BOTH:
76  * the height for the minimum width will be the requested height for all
77  * widgets in the group. The same is of course true when horizontally grouping
78  * width for height widgets.
79  *
80  * Widgets that trade height-for-width should set a reasonably large minimum width
81  * by way of #GtkLabel:width-chars for instance. Widgets with static sizes as well
82  * as widgets that grow (such as ellipsizing text) need no such considerations.
83  *
84  * <refsect2 id="GtkSizeGroup-BUILDER-UI">
85  * <title>GtkSizeGroup as GtkBuildable</title>
86  * <para>
87  * Size groups can be specified in a UI definition by placing an
88  * &lt;object&gt; element with <literal>class="GtkSizeGroup"</literal>
89  * somewhere in the UI definition. The widgets that belong to the
90  * size group are specified by a &lt;widgets&gt; element that may
91  * contain multiple &lt;widget&gt; elements, one for each member
92  * of the size group. The name attribute gives the id of the widget.
93  *
94  * <example>
95  * <title>A UI definition fragment with GtkSizeGroup</title>
96  * <programlisting><![CDATA[
97  * <object class="GtkSizeGroup">
98  *   <property name="mode">GTK_SIZE_GROUP_HORIZONTAL</property>
99  *   <widgets>
100  *     <widget name="radio1"/>
101  *     <widget name="radio2"/>
102  *   </widgets>
103  * </object>
104  * ]]></programlisting>
105  * </example>
106  * </para>
107  * </refsect2>
108  */
109
110
111 struct _GtkSizeGroupPrivate
112 {
113   GSList         *widgets;
114
115   guint8          mode;
116
117   guint           ignore_hidden : 1;
118   guint           visited       : 1;
119 };
120
121 enum {
122   PROP_0,
123   PROP_MODE,
124   PROP_IGNORE_HIDDEN
125 };
126
127 static void gtk_size_group_set_property (GObject      *object,
128                                          guint         prop_id,
129                                          const GValue *value,
130                                          GParamSpec   *pspec);
131 static void gtk_size_group_get_property (GObject      *object,
132                                          guint         prop_id,
133                                          GValue       *value,
134                                          GParamSpec   *pspec);
135
136 static void add_group_to_closure  (GtkSizeGroup      *group,
137                                    GtkSizeGroupMode   mode,
138                                    GSList           **groups,
139                                    GSList           **widgets);
140 static void add_widget_to_closure (GtkWidget         *widget,
141                                    GtkSizeGroupMode   mode,
142                                    GSList           **groups,
143                                    GSList           **widgets);
144
145 /* GtkBuildable */
146 static void gtk_size_group_buildable_init (GtkBuildableIface *iface);
147 static gboolean gtk_size_group_buildable_custom_tag_start (GtkBuildable  *buildable,
148                                                            GtkBuilder    *builder,
149                                                            GObject       *child,
150                                                            const gchar   *tagname,
151                                                            GMarkupParser *parser,
152                                                            gpointer      *data);
153 static void gtk_size_group_buildable_custom_finished (GtkBuildable  *buildable,
154                                                       GtkBuilder    *builder,
155                                                       GObject       *child,
156                                                       const gchar   *tagname,
157                                                       gpointer       user_data);
158
159 static void
160 mark_group_unvisited (GtkSizeGroup *group)
161 {
162   group->priv->visited = FALSE;
163 }
164
165 static void
166 mark_widget_unvisited (GtkWidget *widget)
167 {
168   _gtk_widget_set_sizegroup_visited (widget, FALSE);
169 }
170
171 static void
172 add_group_to_closure (GtkSizeGroup    *group,
173                       GtkSizeGroupMode mode,
174                       GSList         **groups,
175                       GSList         **widgets)
176 {
177   GtkSizeGroupPrivate *priv = group->priv;
178   GSList *tmp_widgets;
179   
180   *groups = g_slist_prepend (*groups, group);
181   priv->visited = TRUE;
182
183   tmp_widgets = priv->widgets;
184   while (tmp_widgets)
185     {
186       GtkWidget *tmp_widget = tmp_widgets->data;
187       
188       if (!_gtk_widget_get_sizegroup_visited (tmp_widget))
189         add_widget_to_closure (tmp_widget, mode, groups, widgets);
190       
191       tmp_widgets = tmp_widgets->next;
192     }
193 }
194
195 static void
196 add_widget_to_closure (GtkWidget       *widget,
197                        GtkSizeGroupMode mode,
198                        GSList         **groups,
199                        GSList         **widgets)
200 {
201   GSList *tmp_groups;
202
203   *widgets = g_slist_prepend (*widgets, widget);
204   _gtk_widget_set_sizegroup_visited (widget, TRUE);
205
206   tmp_groups = _gtk_widget_get_sizegroups (widget);
207   while (tmp_groups)
208     {
209       GtkSizeGroup        *tmp_group = tmp_groups->data;
210       GtkSizeGroupPrivate *tmp_priv  = tmp_group->priv;
211
212       if ((tmp_priv->mode == GTK_SIZE_GROUP_BOTH || tmp_priv->mode == mode) &&
213           !tmp_group->priv->visited)
214         add_group_to_closure (tmp_group, mode, groups, widgets);
215
216       tmp_groups = tmp_groups->next;
217     }
218 }
219
220 static void
221 real_queue_resize (GtkWidget          *widget,
222                    GtkQueueResizeFlags flags)
223 {
224   GtkWidget *container;
225
226   _gtk_widget_set_alloc_needed (widget, TRUE);
227   _gtk_widget_set_width_request_needed (widget, TRUE);
228   _gtk_widget_set_height_request_needed (widget, TRUE);
229
230   container = gtk_widget_get_parent (widget);
231   if (!container &&
232       gtk_widget_is_toplevel (widget) && GTK_IS_CONTAINER (widget))
233     container = widget;
234
235   if (container)
236     {
237       if (flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY)
238         _gtk_container_resize_invalidate (GTK_CONTAINER (container));
239       else
240         _gtk_container_queue_resize (GTK_CONTAINER (container));
241     }
242 }
243
244 static void
245 queue_resize_on_widget (GtkWidget          *widget,
246                         gboolean            check_siblings,
247                         GtkQueueResizeFlags flags)
248 {
249   GtkWidget *parent = widget;
250   GSList *tmp_list;
251
252   while (parent)
253     {
254       GSList *widget_groups;
255       GSList *groups;
256       GSList *widgets;
257       
258       if (widget == parent && !check_siblings)
259         {
260           real_queue_resize (widget, flags);
261           parent = gtk_widget_get_parent (parent);
262           continue;
263         }
264       
265       widget_groups = _gtk_widget_get_sizegroups (parent);
266       if (!widget_groups)
267         {
268           if (widget == parent)
269             real_queue_resize (widget, flags);
270
271           parent = gtk_widget_get_parent (parent);
272           continue;
273         }
274
275       groups = NULL;
276       widgets = NULL;
277           
278       add_widget_to_closure (parent, GTK_SIZE_GROUP_HORIZONTAL, &groups, &widgets);
279       g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
280       g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
281
282       tmp_list = widgets;
283       while (tmp_list)
284         {
285           if (tmp_list->data == parent)
286             {
287               if (widget == parent)
288                 real_queue_resize (parent, flags);
289             }
290           else if (tmp_list->data == widget)
291             {
292               g_warning ("A container and its child are part of this SizeGroup");
293             }
294           else
295             queue_resize_on_widget (tmp_list->data, FALSE, flags);
296
297           tmp_list = tmp_list->next;
298         }
299       
300       g_slist_free (widgets);
301       g_slist_free (groups);
302               
303       groups = NULL;
304       widgets = NULL;
305               
306       add_widget_to_closure (parent, GTK_SIZE_GROUP_VERTICAL, &groups, &widgets);
307       g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
308       g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
309
310       tmp_list = widgets;
311       while (tmp_list)
312         {
313           if (tmp_list->data == parent)
314             {
315               if (widget == parent)
316                 real_queue_resize (parent, flags);
317             }
318           else if (tmp_list->data == widget)
319             {
320               g_warning ("A container and its child are part of this SizeGroup");
321             }
322           else
323             queue_resize_on_widget (tmp_list->data, FALSE, flags);
324
325           tmp_list = tmp_list->next;
326         }
327       
328       g_slist_free (widgets);
329       g_slist_free (groups);
330
331       parent = gtk_widget_get_parent (parent);
332     }
333 }
334
335 static void
336 queue_resize_on_group (GtkSizeGroup       *size_group)
337 {
338   GtkSizeGroupPrivate *priv = size_group->priv;
339
340   if (priv->widgets)
341     queue_resize_on_widget (priv->widgets->data, TRUE, 0);
342 }
343
344 static void
345 gtk_size_group_class_init (GtkSizeGroupClass *klass)
346 {
347   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
348
349   gobject_class->set_property = gtk_size_group_set_property;
350   gobject_class->get_property = gtk_size_group_get_property;
351   
352   g_object_class_install_property (gobject_class,
353                                    PROP_MODE,
354                                    g_param_spec_enum ("mode",
355                                                       P_("Mode"),
356                                                       P_("The directions in which the size group affects the requested sizes"
357                                                         " of its component widgets"),
358                                                       GTK_TYPE_SIZE_GROUP_MODE,
359                                                       GTK_SIZE_GROUP_HORIZONTAL,                                                      GTK_PARAM_READWRITE));
360
361   /**
362    * GtkSizeGroup:ignore-hidden:
363    *
364    * If %TRUE, unmapped widgets are ignored when determining 
365    * the size of the group.
366    *
367    * Since: 2.8
368    */
369   g_object_class_install_property (gobject_class,
370                                    PROP_IGNORE_HIDDEN,
371                                    g_param_spec_boolean ("ignore-hidden",
372                                                          P_("Ignore hidden"),
373                                                          P_("If TRUE, unmapped widgets are ignored "
374                                                             "when determining the size of the group"),
375                                                          FALSE,
376                                                          GTK_PARAM_READWRITE));
377
378   g_type_class_add_private (klass, sizeof (GtkSizeGroupPrivate));
379 }
380
381 static void
382 gtk_size_group_init (GtkSizeGroup *size_group)
383 {
384   GtkSizeGroupPrivate *priv;
385
386   size_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (size_group,
387                                                   GTK_TYPE_SIZE_GROUP,
388                                                   GtkSizeGroupPrivate);
389   priv = size_group->priv;
390
391   priv->widgets = NULL;
392   priv->mode = GTK_SIZE_GROUP_HORIZONTAL;
393   priv->ignore_hidden = FALSE;
394   priv->visited  = FALSE;
395 }
396
397 static void
398 gtk_size_group_buildable_init (GtkBuildableIface *iface)
399 {
400   iface->custom_tag_start = gtk_size_group_buildable_custom_tag_start;
401   iface->custom_finished = gtk_size_group_buildable_custom_finished;
402 }
403
404 G_DEFINE_TYPE_WITH_CODE (GtkSizeGroup, gtk_size_group, G_TYPE_OBJECT,
405                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
406                                                 gtk_size_group_buildable_init))
407
408 static void
409 gtk_size_group_set_property (GObject      *object,
410                              guint         prop_id,
411                              const GValue *value,
412                              GParamSpec   *pspec)
413 {
414   GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
415
416   switch (prop_id)
417     {
418     case PROP_MODE:
419       gtk_size_group_set_mode (size_group, g_value_get_enum (value));
420       break;
421     case PROP_IGNORE_HIDDEN:
422       gtk_size_group_set_ignore_hidden (size_group, g_value_get_boolean (value));
423       break;
424     default:
425       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426       break;
427     }
428 }
429
430 static void
431 gtk_size_group_get_property (GObject      *object,
432                              guint         prop_id,
433                              GValue       *value,
434                              GParamSpec   *pspec)
435 {
436   GtkSizeGroup *size_group = GTK_SIZE_GROUP (object);
437   GtkSizeGroupPrivate *priv = size_group->priv;
438
439   switch (prop_id)
440     {
441     case PROP_MODE:
442       g_value_set_enum (value, priv->mode);
443       break;
444     case PROP_IGNORE_HIDDEN:
445       g_value_set_boolean (value, priv->ignore_hidden);
446       break;
447     default:
448       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449       break;
450     }
451 }
452
453 /**
454  * gtk_size_group_new:
455  * @mode: the mode for the new size group.
456  * 
457  * Create a new #GtkSizeGroup.
458  
459  * Return value: a newly created #GtkSizeGroup
460  **/
461 GtkSizeGroup *
462 gtk_size_group_new (GtkSizeGroupMode mode)
463 {
464   GtkSizeGroup *size_group = g_object_new (GTK_TYPE_SIZE_GROUP, NULL);
465   GtkSizeGroupPrivate *priv = size_group->priv;
466
467   priv->mode = mode;
468
469   return size_group;
470 }
471
472 /**
473  * gtk_size_group_set_mode:
474  * @size_group: a #GtkSizeGroup
475  * @mode: the mode to set for the size group.
476  * 
477  * Sets the #GtkSizeGroupMode of the size group. The mode of the size
478  * group determines whether the widgets in the size group should
479  * all have the same horizontal requisition (%GTK_SIZE_GROUP_HORIZONTAL)
480  * all have the same vertical requisition (%GTK_SIZE_GROUP_VERTICAL),
481  * or should all have the same requisition in both directions
482  * (%GTK_SIZE_GROUP_BOTH).
483  **/
484 void
485 gtk_size_group_set_mode (GtkSizeGroup     *size_group,
486                          GtkSizeGroupMode  mode)
487 {
488   GtkSizeGroupPrivate *priv;
489
490   g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
491
492   priv = size_group->priv;
493
494   if (priv->mode != mode)
495     {
496       if (priv->mode != GTK_SIZE_GROUP_NONE)
497         queue_resize_on_group (size_group);
498       priv->mode = mode;
499       if (priv->mode != GTK_SIZE_GROUP_NONE)
500         queue_resize_on_group (size_group);
501
502       g_object_notify (G_OBJECT (size_group), "mode");
503     }
504 }
505
506 /**
507  * gtk_size_group_get_mode:
508  * @size_group: a #GtkSizeGroup
509  * 
510  * Gets the current mode of the size group. See gtk_size_group_set_mode().
511  * 
512  * Return value: the current mode of the size group.
513  **/
514 GtkSizeGroupMode
515 gtk_size_group_get_mode (GtkSizeGroup *size_group)
516 {
517   g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), GTK_SIZE_GROUP_BOTH);
518
519   return size_group->priv->mode;
520 }
521
522 /**
523  * gtk_size_group_set_ignore_hidden:
524  * @size_group: a #GtkSizeGroup
525  * @ignore_hidden: whether unmapped widgets should be ignored
526  *   when calculating the size
527  * 
528  * Sets whether unmapped widgets should be ignored when
529  * calculating the size.
530  *
531  * Since: 2.8 
532  */
533 void
534 gtk_size_group_set_ignore_hidden (GtkSizeGroup *size_group,
535                                   gboolean      ignore_hidden)
536 {
537   GtkSizeGroupPrivate *priv;
538
539   g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
540
541   priv = size_group->priv;
542
543   ignore_hidden = ignore_hidden != FALSE;
544
545   if (priv->ignore_hidden != ignore_hidden)
546     {
547       priv->ignore_hidden = ignore_hidden;
548
549       g_object_notify (G_OBJECT (size_group), "ignore-hidden");
550     }
551 }
552
553 /**
554  * gtk_size_group_get_ignore_hidden:
555  * @size_group: a #GtkSizeGroup
556  *
557  * Returns if invisible widgets are ignored when calculating the size.
558  *
559  * Returns: %TRUE if invisible widgets are ignored.
560  *
561  * Since: 2.8
562  */
563 gboolean
564 gtk_size_group_get_ignore_hidden (GtkSizeGroup *size_group)
565 {
566   g_return_val_if_fail (GTK_IS_SIZE_GROUP (size_group), FALSE);
567
568   return size_group->priv->ignore_hidden;
569 }
570
571 static void
572 gtk_size_group_widget_destroyed (GtkWidget    *widget,
573                                  GtkSizeGroup *size_group)
574 {
575   gtk_size_group_remove_widget (size_group, widget);
576 }
577
578 /**
579  * gtk_size_group_add_widget:
580  * @size_group: a #GtkSizeGroup
581  * @widget: the #GtkWidget to add
582  * 
583  * Adds a widget to a #GtkSizeGroup. In the future, the requisition
584  * of the widget will be determined as the maximum of its requisition
585  * and the requisition of the other widgets in the size group.
586  * Whether this applies horizontally, vertically, or in both directions
587  * depends on the mode of the size group. See gtk_size_group_set_mode().
588  *
589  * When the widget is destroyed or no longer referenced elsewhere, it will 
590  * be removed from the size group.
591  */
592 void
593 gtk_size_group_add_widget (GtkSizeGroup     *size_group,
594                            GtkWidget        *widget)
595 {
596   GtkSizeGroupPrivate *priv;
597   GSList *groups;
598   
599   g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
600   g_return_if_fail (GTK_IS_WIDGET (widget));
601
602   priv = size_group->priv;
603
604   groups = _gtk_widget_get_sizegroups (widget);
605
606   if (!g_slist_find (groups, size_group))
607     {
608       _gtk_widget_add_sizegroup (widget, size_group);
609
610       priv->widgets = g_slist_prepend (priv->widgets, widget);
611
612       g_signal_connect (widget, "destroy",
613                         G_CALLBACK (gtk_size_group_widget_destroyed),
614                         size_group);
615
616       g_object_ref (size_group);
617     }
618   
619   queue_resize_on_group (size_group);
620 }
621
622 /**
623  * gtk_size_group_remove_widget:
624  * @size_group: a #GtkSizeGroup
625  * @widget: the #GtkWidget to remove
626  * 
627  * Removes a widget from a #GtkSizeGroup.
628  **/
629 void
630 gtk_size_group_remove_widget (GtkSizeGroup *size_group,
631                               GtkWidget    *widget)
632 {
633   GtkSizeGroupPrivate *priv;
634   
635   g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
636   g_return_if_fail (GTK_IS_WIDGET (widget));
637
638   priv = size_group->priv;
639
640   g_return_if_fail (g_slist_find (priv->widgets, widget));
641
642   g_signal_handlers_disconnect_by_func (widget,
643                                         gtk_size_group_widget_destroyed,
644                                         size_group);
645   
646   _gtk_widget_remove_sizegroup (widget, size_group);
647
648   priv->widgets = g_slist_remove (priv->widgets, widget);
649   queue_resize_on_group (size_group);
650   gtk_widget_queue_resize (widget);
651
652   g_object_unref (size_group);
653 }
654
655 /**
656  * gtk_size_group_get_widgets:
657  * @size_group: a #GtkSizeGroup
658  * 
659  * Returns the list of widgets associated with @size_group.
660  *
661  * Return value:  (element-type GtkWidget) (transfer none): a #GSList of
662  *   widgets. The list is owned by GTK+ and should not be modified.
663  *
664  * Since: 2.10
665  **/
666 GSList *
667 gtk_size_group_get_widgets (GtkSizeGroup *size_group)
668 {
669   return size_group->priv->widgets;
670 }
671
672 static void
673 compute_dimension (GtkWidget        *widget,
674                    GtkSizeGroupMode  mode,
675                    gint             *minimum, /* in-out */
676                    gint             *natural) /* in-out */
677 {
678   GSList *widgets = NULL;
679   GSList *groups = NULL;
680   GSList *tmp_list;
681   gint    min_result = 0, nat_result = 0;
682
683   add_widget_to_closure (widget, mode, &groups, &widgets);
684   g_slist_foreach (widgets, (GFunc)mark_widget_unvisited, NULL);
685   g_slist_foreach (groups, (GFunc)mark_group_unvisited, NULL);
686
687   g_slist_foreach (widgets, (GFunc)g_object_ref, NULL);
688   
689   if (!groups)
690     {
691       min_result = *minimum;
692       nat_result = *natural;
693     }
694   else
695     {
696       GtkSizeGroup *group = groups->data;
697       GtkSizeGroupPrivate *priv = group->priv;
698
699       tmp_list = widgets;
700       while (tmp_list)
701         {
702           GtkWidget *tmp_widget = tmp_list->data;
703           gint min_dimension, nat_dimension;
704
705           if (tmp_widget == widget)
706             {
707               min_dimension = *minimum;
708               nat_dimension = *natural;
709             }
710           else if (!gtk_widget_get_mapped (tmp_widget) && priv->ignore_hidden)
711             {
712               min_dimension = 0;
713               nat_dimension = 0;
714             }
715           else
716             {
717               if (mode == GTK_SIZE_GROUP_HORIZONTAL)
718                 gtk_widget_get_preferred_width (tmp_widget, &min_dimension, &nat_dimension);
719               else
720                 gtk_widget_get_preferred_height (tmp_widget, &min_dimension, &nat_dimension);
721             }
722
723           min_result = MAX (min_result, min_dimension);
724           nat_result = MAX (nat_result, nat_dimension);
725
726           tmp_list = tmp_list->next;
727         }
728     }
729
730   g_slist_foreach (widgets, (GFunc)g_object_unref, NULL);
731
732   g_slist_free (widgets);
733   g_slist_free (groups);
734
735   *minimum = min_result;
736   *natural = nat_result;
737 }
738
739 /**
740  * _gtk_size_group_bump_requisition:
741  * @widget: a #GtkWidget
742  * @mode: either %GTK_SIZE_GROUP_HORIZONTAL or %GTK_SIZE_GROUP_VERTICAL, depending
743  *        on the dimension in which to bump the size.
744  * @minimum: a pointer to the widget's minimum size
745  * @natural: a pointer to the widget's natural size
746  *
747  * Refreshes the sizegroup while returning the groups requested
748  * value in the dimension @mode.
749  *
750  * This function is used both to update sizegroup minimum and natural size 
751  * information and widget minimum and natural sizes in multiple passes from 
752  * the size request apis.
753  */
754 void
755 _gtk_size_group_bump_requisition (GtkWidget        *widget,
756                                   GtkSizeGroupMode  mode,
757                                   gint             *minimum,
758                                   gint             *natural)
759 {
760   if (!_gtk_widget_get_sizegroup_bumping (widget))
761     {
762       /* Avoid recursion here */
763       _gtk_widget_set_sizegroup_bumping (widget, TRUE);
764
765       if (_gtk_widget_get_sizegroups (widget))
766         compute_dimension (widget, mode, minimum, natural);
767
768       _gtk_widget_set_sizegroup_bumping (widget, FALSE);
769     }
770 }
771
772
773
774 /**
775  * _gtk_size_group_queue_resize:
776  * @widget: a #GtkWidget
777  * 
778  * Queue a resize on a widget, and on all other widgets grouped with this widget.
779  **/
780 void
781 _gtk_size_group_queue_resize (GtkWidget           *widget,
782                               GtkQueueResizeFlags  flags)
783 {
784   queue_resize_on_widget (widget, TRUE, flags);
785 }
786
787 typedef struct {
788   GObject *object;
789   GSList *items;
790 } GSListSubParserData;
791
792 static void
793 size_group_start_element (GMarkupParseContext *context,
794                           const gchar         *element_name,
795                           const gchar        **names,
796                           const gchar        **values,
797                           gpointer            user_data,
798                           GError            **error)
799 {
800   guint i;
801   GSListSubParserData *data = (GSListSubParserData*)user_data;
802
803   if (strcmp (element_name, "widget") == 0)
804     for (i = 0; names[i]; i++)
805       if (strcmp (names[i], "name") == 0)
806         data->items = g_slist_prepend (data->items, g_strdup (values[i]));
807   else if (strcmp (element_name, "widgets") == 0)
808     return;
809   else
810     g_warning ("Unsupported type tag for GtkSizeGroup: %s\n",
811                element_name);
812
813 }
814
815 static const GMarkupParser size_group_parser =
816   {
817     size_group_start_element
818   };
819
820 static gboolean
821 gtk_size_group_buildable_custom_tag_start (GtkBuildable  *buildable,
822                                            GtkBuilder    *builder,
823                                            GObject       *child,
824                                            const gchar   *tagname,
825                                            GMarkupParser *parser,
826                                            gpointer      *data)
827 {
828   GSListSubParserData *parser_data;
829
830   if (child)
831     return FALSE;
832
833   if (strcmp (tagname, "widgets") == 0)
834     {
835       parser_data = g_slice_new0 (GSListSubParserData);
836       parser_data->items = NULL;
837       parser_data->object = G_OBJECT (buildable);
838
839       *parser = size_group_parser;
840       *data = parser_data;
841       return TRUE;
842     }
843
844   return FALSE;
845 }
846
847 static void
848 gtk_size_group_buildable_custom_finished (GtkBuildable  *buildable,
849                                           GtkBuilder    *builder,
850                                           GObject       *child,
851                                           const gchar   *tagname,
852                                           gpointer       user_data)
853 {
854   GSList *l;
855   GSListSubParserData *data;
856   GObject *object;
857
858   if (strcmp (tagname, "widgets"))
859     return;
860   
861   data = (GSListSubParserData*)user_data;
862   data->items = g_slist_reverse (data->items);
863
864   for (l = data->items; l; l = l->next)
865     {
866       object = gtk_builder_get_object (builder, l->data);
867       if (!object)
868         {
869           g_warning ("Unknown object %s specified in sizegroup %s",
870                      (const gchar*)l->data,
871                      gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
872           continue;
873         }
874       gtk_size_group_add_widget (GTK_SIZE_GROUP (data->object),
875                                  GTK_WIDGET (object));
876       g_free (l->data);
877     }
878   g_slist_free (data->items);
879   g_slice_free (GSListSubParserData, data);
880 }