]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
Added a runtime warning if any class is implementing the ->size_request() vfunc.
[~andy/gtk] / gtk / gtksizerequest.c
1 /* gtksizerequest.c
2  * Copyright (C) 2007-2010 Openismus GmbH
3  *
4  * Authors:
5  *      Mathias Hasselmann <mathias@openismus.com>
6  *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25
26 #include "gtksizerequest.h"
27
28 #include "gtkdebug.h"
29 #include "gtkintl.h"
30 #include "gtkprivate.h"
31 #include "gtksizegroup-private.h"
32 #include "gtkwidgetprivate.h"
33
34 /* looks for a cached size request for this for_size. If not
35  * found, returns the oldest entry so it can be overwritten
36  *
37  * Note that this caching code was directly derived from
38  * the Clutter toolkit.
39  */
40 static gboolean
41 get_cached_size (SizeRequestCache  *cache,
42                  GtkSizeGroupMode   orientation,
43                  gint               for_size,
44                  SizeRequest      **result)
45 {
46   guint i, n_sizes;
47   SizeRequest  *cached_sizes;
48
49   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
50     {
51       cached_sizes = cache->widths;
52       n_sizes = cache->cached_widths;
53     }
54   else
55     {
56       cached_sizes = cache->heights;
57       n_sizes = cache->cached_widths;
58     }
59
60   /* Search for an already cached size */
61   for (i = 0; i < n_sizes; i++)
62     {
63       if (cached_sizes[i].for_size == for_size)
64         {
65           *result = &cached_sizes[i];
66           return TRUE;
67         }
68     }
69
70   /* If not found, pull a new size from the cache, the returned size cache
71    * will immediately be used to cache the new computed size so we go ahead
72    * and increment the last_cached_width/height right away */
73   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
74     {
75       if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
76         {
77           cache->cached_widths++;
78           cache->last_cached_width = cache->cached_widths - 1;
79         }
80       else
81         {
82           if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
83             cache->last_cached_width = 0;
84         }
85
86       *result = &cache->widths[cache->last_cached_width];
87     }
88   else /* GTK_SIZE_GROUP_VERTICAL */
89     {
90       if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
91         {
92           cache->cached_heights++;
93           cache->last_cached_height = cache->cached_heights - 1;
94         }
95       else
96         {
97           if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
98             cache->last_cached_height = 0;
99         }
100
101       *result = &cache->heights[cache->last_cached_height];
102     }
103
104   return FALSE;
105 }
106
107 static void
108 do_size_request (GtkWidget      *widget,
109                  GtkRequisition *requisition)
110 {
111   GtkWidgetClass *widget_class = g_type_class_peek (GTK_TYPE_WIDGET);
112
113   if (GTK_WIDGET_GET_CLASS (widget)->size_request != widget_class->size_request)
114     g_warning ("%s implements GtkWidgetClass::size_request which is deprecated and "
115                "will be removed in the next release",
116                G_OBJECT_TYPE_NAME (widget));
117
118   /* Now we dont bother caching the deprecated "size-request" returns,
119    * just unconditionally invoke here just in case we run into legacy stuff */
120   gtk_widget_ensure_style (widget);
121   g_signal_emit_by_name (widget, "size-request", requisition);
122 }
123
124 #ifndef G_DISABLE_CHECKS
125 static GQuark recursion_check_quark = 0;
126 #endif /* G_DISABLE_CHECKS */
127
128 static void
129 push_recursion_check (GtkWidget       *widget,
130                       GtkSizeGroupMode orientation,
131                       gint             for_size)
132 {
133 #ifndef G_DISABLE_CHECKS
134   const char *previous_method;
135   const char *method;
136
137   if (recursion_check_quark == 0)
138     recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
139
140   previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
141
142   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
143     {
144       method = for_size < 0 ? "get_width" : "get_width_for_height";
145     }
146   else
147     {
148       method = for_size < 0 ? "get_height" : "get_height_for_width";
149     }
150
151   if (previous_method != NULL)
152     {
153       g_warning ("%s %p: widget tried to gtk_widget_%s inside "
154                  " GtkWidget     ::%s implementation. "
155                  "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
156                  "directly rather than using gtk_widget_%s",
157                  G_OBJECT_TYPE_NAME (widget), widget,
158                  method, previous_method,
159                  method, method);
160     }
161
162   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
163 #endif /* G_DISABLE_CHECKS */
164 }
165
166 static void
167 pop_recursion_check (GtkWidget       *widget,
168                      GtkSizeGroupMode orientation)
169 {
170 #ifndef G_DISABLE_CHECKS
171   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
172 #endif
173 }
174
175 static void
176 compute_size_for_orientation (GtkWidget         *widget,
177                               GtkSizeGroupMode   orientation,
178                               gint               for_size,
179                               gint              *minimum_size,
180                               gint              *natural_size)
181 {
182   SizeRequestCache *cache;
183   SizeRequest      *cached_size;
184   gboolean          found_in_cache = FALSE;
185   int adjusted_min, adjusted_natural;
186
187   g_return_if_fail (GTK_IS_WIDGET (widget));
188   g_return_if_fail (minimum_size != NULL || natural_size != NULL);
189
190   cache  = _gtk_widget_peek_request_cache (widget);
191
192   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
193     {
194       cached_size = &cache->widths[0];
195
196       if (!_gtk_widget_get_width_request_needed (widget))
197         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
198       else
199         {
200           memset (cache->widths, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
201           cache->cached_widths = 1;
202           cache->last_cached_width = 0;
203         }
204     }
205   else
206     {
207       cached_size = &cache->heights[0];
208
209       if (!_gtk_widget_get_height_request_needed (widget))
210         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
211       else
212         {
213           memset (cache->heights, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
214           cache->cached_heights = 1;
215           cache->last_cached_height = 0;
216         }
217     }
218
219   if (!found_in_cache)
220     {
221       GtkRequisition requisition = { 0, 0 };
222       gint min_size = 0, nat_size = 0;
223       gint requisition_size;
224
225       /* Unconditional size request runs but is often unhandled. */
226       do_size_request (widget, &requisition);
227
228       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
229         {
230           requisition_size = requisition.width;
231
232           if (for_size < 0)
233             {
234               push_recursion_check (widget, orientation, for_size);
235               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
236               pop_recursion_check (widget, orientation);
237             }
238           else
239             {
240               int ignored_position = 0;
241               int natural_height;
242
243               /* Pull the base natural height from the cache as it's needed to adjust 
244                * the proposed 'for_size' */
245               gtk_widget_get_preferred_height (widget, NULL, &natural_height);
246
247               /* convert for_size to unadjusted height (for_size is a proposed allocation) */
248               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
249                                                                      GTK_ORIENTATION_VERTICAL,
250                                                                      &natural_height,
251                                                                      &ignored_position,
252                                                                      &for_size);
253
254               push_recursion_check (widget, orientation, for_size);
255               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, for_size,
256                                                                               &min_size, &nat_size);
257               pop_recursion_check (widget, orientation);
258             }
259         }
260       else
261         {
262           requisition_size = requisition.height;
263
264           if (for_size < 0)
265             {
266               push_recursion_check (widget, orientation, for_size);
267               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
268               pop_recursion_check (widget, orientation);
269             }
270           else
271             {
272               int ignored_position = 0;
273               int natural_width;
274
275               /* Pull the base natural width from the cache as it's needed to adjust 
276                * the proposed 'for_size' */
277               gtk_widget_get_preferred_width (widget, NULL, &natural_width);
278
279               /* convert for_size to unadjusted width (for_size is a proposed allocation) */
280               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
281                                                                      GTK_ORIENTATION_HORIZONTAL,
282                                                                      &natural_width,
283                                                                      &ignored_position,
284                                                                      &for_size);
285
286               push_recursion_check (widget, orientation, for_size);
287               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, for_size,
288                                                                               &min_size, &nat_size);
289               pop_recursion_check (widget, orientation);
290             }
291         }
292
293       if (min_size > nat_size)
294         {
295           g_warning ("%s %p reported min size %d and natural size %d; natural size must be >= min size",
296                      G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size);
297         }
298
299       /* Support for dangling "size-request" signal implementations on
300        * legacy widgets
301        */
302       min_size = MAX (min_size, requisition_size);
303       nat_size = MAX (nat_size, requisition_size);
304
305       cached_size->minimum_size = min_size;
306       cached_size->natural_size = nat_size;
307       cached_size->for_size     = for_size;
308
309       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
310           _gtk_widget_set_width_request_needed (widget, FALSE);
311       else
312           _gtk_widget_set_height_request_needed (widget, FALSE);
313
314       adjusted_min = cached_size->minimum_size;
315       adjusted_natural = cached_size->natural_size;
316       GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
317                                                           orientation == GTK_SIZE_GROUP_HORIZONTAL ?
318                                                           GTK_ORIENTATION_HORIZONTAL :
319                                                           GTK_ORIENTATION_VERTICAL,
320                                                           &adjusted_min,
321                                                           &adjusted_natural);
322
323       if (adjusted_min < cached_size->minimum_size ||
324           adjusted_natural < cached_size->natural_size)
325         {
326           g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
327                      G_OBJECT_TYPE_NAME (widget), widget,
328                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
329                      adjusted_min, adjusted_natural,
330                      cached_size->minimum_size, cached_size->natural_size);
331           /* don't use the adjustment */
332         }
333       else if (adjusted_min > adjusted_natural)
334         {
335           g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
336                      G_OBJECT_TYPE_NAME (widget), widget,
337                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
338                      adjusted_min, adjusted_natural,
339                      cached_size->minimum_size, cached_size->natural_size);
340           /* don't use the adjustment */
341         }
342       else
343         {
344           /* adjustment looks good */
345           cached_size->minimum_size = adjusted_min;
346           cached_size->natural_size = adjusted_natural;
347         }
348
349       /* Update size-groups with our request and update our cached requests 
350        * with the size-group values in a single pass.
351        */
352       _gtk_size_group_bump_requisition (widget,
353                                         orientation,
354                                         &cached_size->minimum_size,
355                                         &cached_size->natural_size);
356     }
357
358   if (minimum_size)
359     *minimum_size = cached_size->minimum_size;
360
361   if (natural_size)
362     *natural_size = cached_size->natural_size;
363
364   g_assert (cached_size->minimum_size <= cached_size->natural_size);
365
366   GTK_NOTE (SIZE_REQUEST,
367             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
368                      widget, G_OBJECT_TYPE_NAME (widget),
369                      orientation == GTK_SIZE_GROUP_HORIZONTAL ?
370                      "width for height" : "height for width" ,
371                      for_size,
372                      cached_size->minimum_size,
373                      cached_size->natural_size,
374                      found_in_cache ? "yes" : "no"));
375
376 }
377
378 /**
379  * gtk_widget_get_request_mode:
380  * @widget: a #GtkWidget instance
381  *
382  * Gets whether the widget prefers a height-for-width layout
383  * or a width-for-height layout.
384  *
385  * <note><para>#GtkBin widgets generally propagate the preference of
386  * their child, container widgets need to request something either in
387  * context of their children or in context of their allocation
388  * capabilities.</para></note>
389  *
390  * Returns: The #GtkSizeRequestMode preferred by @widget.
391  *
392  * Since: 3.0
393  */
394 GtkSizeRequestMode
395 gtk_widget_get_request_mode (GtkWidget *widget)
396 {
397   GtkWidgetClass *klass;
398
399   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
400
401   klass = GTK_WIDGET_GET_CLASS (widget);
402   if (klass->get_request_mode)
403     return klass->get_request_mode (widget);
404
405   /* By default widgets are height-for-width. */
406   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
407 }
408
409 /**
410  * gtk_widget_get_preferred_width:
411  * @widget: a #GtkWidget instance
412  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
413  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
414  *
415  * Retrieves a widget's initial minimum and natural width.
416  *
417  * <note><para>This call is specific to height-for-width
418  * requests.</para></note>
419  *
420  * The returned request will be modified by the
421  * GtkWidgetClass::adjust_size_request virtual method and by any
422  * #GtkSizeGroup that have been applied. That is, the returned request
423  * is the one that should be used for layout, not necessarily the one
424  * returned by the widget itself.
425  *
426  * Since: 3.0
427  */
428 void
429 gtk_widget_get_preferred_width (GtkWidget *widget,
430                                 gint      *minimum_width,
431                                 gint      *natural_width)
432 {
433   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
434                                 -1, minimum_width, natural_width);
435 }
436
437
438 /**
439  * gtk_widget_get_preferred_height:
440  * @widget: a #GtkWidget instance
441  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
442  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
443  *
444  * Retrieves a widget's initial minimum and natural height.
445  *
446  * <note><para>This call is specific to width-for-height requests.</para></note>
447  *
448  * The returned request will be modified by the
449  * GtkWidgetClass::adjust_size_request virtual method and by any
450  * #GtkSizeGroup that have been applied. That is, the returned request
451  * is the one that should be used for layout, not necessarily the one
452  * returned by the widget itself.
453  *
454  * Since: 3.0
455  */
456 void
457 gtk_widget_get_preferred_height (GtkWidget *widget,
458                                  gint      *minimum_height,
459                                  gint      *natural_height)
460 {
461   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
462                                 -1, minimum_height, natural_height);
463 }
464
465
466
467 /**
468  * gtk_widget_get_preferred_width_for_height:
469  * @widget: a #GtkWidget instance
470  * @height: the height which is available for allocation
471  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
472  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
473  *
474  * Retrieves a widget's minimum and natural width if it would be given
475  * the specified @height.
476  *
477  * The returned request will be modified by the
478  * GtkWidgetClass::adjust_size_request virtual method and by any
479  * #GtkSizeGroup that have been applied. That is, the returned request
480  * is the one that should be used for layout, not necessarily the one
481  * returned by the widget itself.
482  *
483  * Since: 3.0
484  */
485 void
486 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
487                                            gint       height,
488                                            gint      *minimum_width,
489                                            gint      *natural_width)
490 {
491   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
492                                 height, minimum_width, natural_width);
493 }
494
495 /**
496  * gtk_widget_get_preferred_height_for_width:
497  * @widget: a #GtkWidget instance
498  * @width: the width which is available for allocation
499  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
500  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
501  *
502  * Retrieves a widget's minimum and natural height if it would be given
503  * the specified @width.
504  *
505  * The returned request will be modified by the
506  * GtkWidgetClass::adjust_size_request virtual method and by any
507  * #GtkSizeGroup that have been applied. That is, the returned request
508  * is the one that should be used for layout, not necessarily the one
509  * returned by the widget itself.
510  *
511  * Since: 3.0
512  */
513 void
514 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
515                                            gint       width,
516                                            gint      *minimum_height,
517                                            gint      *natural_height)
518 {
519   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
520                                 width, minimum_height, natural_height);
521 }
522
523 /**
524  * gtk_widget_get_preferred_size:
525  * @widget: a #GtkWidget instance
526  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
527  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
528  *
529  * Retrieves the minimum and natural size of a widget taking
530  * into account the widget's preference for height-for-width management.
531  *
532  * This is used to retrieve a suitable size by container widgets which do
533  * not impose any restrictions on the child placement. It can be used
534  * to deduce toplevel window and menu sizes as well as child widgets in
535  * free form containers such as GtkLayout.
536  *
537  * <note><para>Handle with care, note that the natural height of a height-for-width
538  * widget will generally be a smaller size than the minimum height, since the required
539  * height for the natural width is generally smaller than the required height for
540  * the minimum width.</para></note>
541  *
542  * Since: 3.0
543  */
544 void
545 gtk_widget_get_preferred_size (GtkWidget      *widget,
546                                GtkRequisition *minimum_size,
547                                GtkRequisition *natural_size)
548 {
549   gint min_width, nat_width;
550   gint min_height, nat_height;
551
552   g_return_if_fail (GTK_IS_WIDGET (widget));
553
554   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
555     {
556       gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
557
558       if (minimum_size)
559         {
560           minimum_size->width = min_width;
561           gtk_widget_get_preferred_height_for_width (widget, min_width,
562                                                      &minimum_size->height, NULL);
563         }
564
565       if (natural_size)
566         {
567           natural_size->width = nat_width;
568           gtk_widget_get_preferred_height_for_width (widget, nat_width,
569                                                      NULL, &natural_size->height);
570         }
571     }
572   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
573     {
574       gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
575
576       if (minimum_size)
577         {
578           minimum_size->height = min_height;
579           gtk_widget_get_preferred_width_for_height (widget, min_height,
580                                                      &minimum_size->width, NULL);
581         }
582
583       if (natural_size)
584         {
585           natural_size->height = nat_height;
586           gtk_widget_get_preferred_width_for_height (widget, nat_height,
587                                                      NULL, &natural_size->width);
588         }
589     }
590 }
591
592
593 static gint
594 compare_gap (gconstpointer p1,
595              gconstpointer p2,
596              gpointer      data)
597 {
598   GtkRequestedSize *sizes = data;
599   const guint *c1 = p1;
600   const guint *c2 = p2;
601
602   const gint d1 = MAX (sizes[*c1].natural_size -
603                        sizes[*c1].minimum_size,
604                        0);
605   const gint d2 = MAX (sizes[*c2].natural_size -
606                        sizes[*c2].minimum_size,
607                        0);
608
609   gint delta = (d2 - d1);
610
611   if (0 == delta)
612     delta = (*c2 - *c1);
613
614   return delta;
615 }
616
617 /**
618  * gtk_distribute_natural_allocation: 
619  * @extra_space: Extra space to redistribute among children after subtracting
620  *               minimum sizes and any child padding from the overall allocation
621  * @n_requested_sizes: Number of requests to fit into the allocation
622  * @sizes: An array of structs with a client pointer and a minimum/natural size
623  *         in the orientation of the allocation.
624  *
625  * Distributes @extra_space to child @sizes by bringing up smaller
626  * children up to natural size first.
627  *
628  * The remaining space will be added to the @minimum_size member of the
629  * GtkRequestedSize struct. If all sizes reach their natural size then
630  * the remaining space is returned.
631  *
632  * Returns: The remainder of @extra_space after redistributing space
633  * to @sizes.
634  */
635 gint 
636 gtk_distribute_natural_allocation (gint              extra_space,
637                                    guint             n_requested_sizes,
638                                    GtkRequestedSize *sizes)
639 {
640   guint *spreading;
641   gint   i;
642
643   g_return_val_if_fail (extra_space >= 0, 0);
644
645   spreading = g_newa (guint, n_requested_sizes);
646
647   for (i = 0; i < n_requested_sizes; i++)
648     spreading[i] = i;
649
650   /* Distribute the container's extra space c_gap. We want to assign
651    * this space such that the sum of extra space assigned to children
652    * (c^i_gap) is equal to c_cap. The case that there's not enough
653    * space for all children to take their natural size needs some
654    * attention. The goals we want to achieve are:
655    *
656    *   a) Maximize number of children taking their natural size.
657    *   b) The allocated size of children should be a continuous
658    *   function of c_gap.  That is, increasing the container size by
659    *   one pixel should never make drastic changes in the distribution.
660    *   c) If child i takes its natural size and child j doesn't,
661    *   child j should have received at least as much gap as child i.
662    *
663    * The following code distributes the additional space by following
664    * these rules.
665    */
666   
667   /* Sort descending by gap and position. */
668   g_qsort_with_data (spreading,
669                      n_requested_sizes, sizeof (guint),
670                      compare_gap, sizes);
671   
672   /* Distribute available space.
673    * This master piece of a loop was conceived by Behdad Esfahbod.
674    */
675   for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
676     {
677       /* Divide remaining space by number of remaining children.
678        * Sort order and reducing remaining space by assigned space
679        * ensures that space is distributed equally.
680        */
681       gint glue = (extra_space + i) / (i + 1);
682       gint gap = sizes[(spreading[i])].natural_size
683         - sizes[(spreading[i])].minimum_size;
684       
685       gint extra = MIN (glue, gap);
686       
687       sizes[spreading[i]].minimum_size += extra;
688       
689       extra_space -= extra;
690     }
691
692   return extra_space;
693 }
694