]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
size-request: Clamp size requests to screen size
[~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
108 #ifndef G_DISABLE_CHECKS
109 static GQuark recursion_check_quark = 0;
110 #endif /* G_DISABLE_CHECKS */
111
112 static void
113 push_recursion_check (GtkWidget       *widget,
114                       GtkSizeGroupMode orientation,
115                       gint             for_size)
116 {
117 #ifndef G_DISABLE_CHECKS
118   const char *previous_method;
119   const char *method;
120
121   if (recursion_check_quark == 0)
122     recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
123
124   previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
125
126   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
127     {
128       method = for_size < 0 ? "get_width" : "get_width_for_height";
129     }
130   else
131     {
132       method = for_size < 0 ? "get_height" : "get_height_for_width";
133     }
134
135   if (previous_method != NULL)
136     {
137       g_warning ("%s %p: widget tried to gtk_widget_%s inside "
138                  " GtkWidget     ::%s implementation. "
139                  "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
140                  "directly rather than using gtk_widget_%s",
141                  G_OBJECT_TYPE_NAME (widget), widget,
142                  method, previous_method,
143                  method, method);
144     }
145
146   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
147 #endif /* G_DISABLE_CHECKS */
148 }
149
150 static void
151 pop_recursion_check (GtkWidget       *widget,
152                      GtkSizeGroupMode orientation)
153 {
154 #ifndef G_DISABLE_CHECKS
155   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
156 #endif
157 }
158
159 static void
160 compute_size_for_orientation (GtkWidget         *widget,
161                               GtkSizeGroupMode   orientation,
162                               gint               for_size,
163                               gint              *minimum_size,
164                               gint              *natural_size)
165 {
166   SizeRequestCache *cache;
167   SizeRequest      *cached_size;
168   gboolean          found_in_cache = FALSE;
169   int adjusted_min, adjusted_natural;
170
171   g_return_if_fail (GTK_IS_WIDGET (widget));
172   g_return_if_fail (minimum_size != NULL || natural_size != NULL);
173
174   cache  = _gtk_widget_peek_request_cache (widget);
175
176   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
177     {
178       cached_size = &cache->widths[0];
179
180       if (!_gtk_widget_get_width_request_needed (widget))
181         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
182       else
183         {
184           memset (cache->widths, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
185           cache->cached_widths = 1;
186           cache->last_cached_width = 0;
187         }
188     }
189   else
190     {
191       cached_size = &cache->heights[0];
192
193       if (!_gtk_widget_get_height_request_needed (widget))
194         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
195       else
196         {
197           memset (cache->heights, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
198           cache->cached_heights = 1;
199           cache->last_cached_height = 0;
200         }
201     }
202
203   if (!found_in_cache)
204     {
205       gint min_size = 0;
206       gint nat_size = 0;
207
208       gtk_widget_ensure_style (widget);
209
210       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
211         {
212           if (for_size < 0)
213             {
214               push_recursion_check (widget, orientation, for_size);
215               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
216               pop_recursion_check (widget, orientation);
217             }
218           else
219             {
220               gint ignored_position = 0;
221               gint minimum_height;
222               gint natural_height;
223
224               /* Pull the base natural height from the cache as it's needed to adjust
225                * the proposed 'for_size' */
226               gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
227
228               /* convert for_size to unadjusted height (for_size is a proposed allocation) */
229               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
230                                                                      GTK_ORIENTATION_VERTICAL,
231                                                                      &minimum_height,
232                                                                      &natural_height,
233                                                                      &ignored_position,
234                                                                      &for_size);
235
236               push_recursion_check (widget, orientation, for_size);
237               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, 
238                                                                              MAX (for_size, minimum_height),
239                                                                              &min_size, &nat_size);
240               pop_recursion_check (widget, orientation);
241             }
242 #ifndef G_DISABLE_CHECKS
243           if (gtk_widget_get_screen (widget))
244             {
245               guint screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
246               min_size = MIN (min_size, screen_width);
247               nat_size = MIN (nat_size, screen_width);
248             }
249 #endif
250         }
251       else
252         {
253           if (for_size < 0)
254             {
255               push_recursion_check (widget, orientation, for_size);
256               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
257               pop_recursion_check (widget, orientation);
258             }
259           else
260             {
261               int ignored_position = 0;
262               int minimum_width;
263               int natural_width;
264
265               /* Pull the base natural width from the cache as it's needed to adjust
266                * the proposed 'for_size' */
267               gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
268
269               /* convert for_size to unadjusted width (for_size is a proposed allocation) */
270               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
271                                                                      GTK_ORIENTATION_HORIZONTAL,
272                                                                      &minimum_width,
273                                                                      &natural_width,
274                                                                      &ignored_position,
275                                                                      &for_size);
276
277               push_recursion_check (widget, orientation, for_size);
278               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, 
279                                                                              MAX (for_size, minimum_width),
280                                                                              &min_size, &nat_size);
281               pop_recursion_check (widget, orientation);
282             }
283 #ifndef G_DISABLE_CHECKS
284           if (gtk_widget_get_screen (widget))
285             {
286               guint screen_height = gdk_screen_get_height (gtk_widget_get_screen (widget));
287               min_size = MIN (min_size, screen_height);
288               nat_size = MIN (nat_size, screen_height);
289             }
290 #endif
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       cached_size->minimum_size = min_size;
300       cached_size->natural_size = nat_size;
301       cached_size->for_size     = for_size;
302
303       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
304           _gtk_widget_set_width_request_needed (widget, FALSE);
305       else
306           _gtk_widget_set_height_request_needed (widget, FALSE);
307
308       adjusted_min = cached_size->minimum_size;
309       adjusted_natural = cached_size->natural_size;
310       GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
311                                                           orientation == GTK_SIZE_GROUP_HORIZONTAL ?
312                                                           GTK_ORIENTATION_HORIZONTAL :
313                                                           GTK_ORIENTATION_VERTICAL,
314                                                           &adjusted_min,
315                                                           &adjusted_natural);
316
317       if (adjusted_min < cached_size->minimum_size ||
318           adjusted_natural < cached_size->natural_size)
319         {
320           g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
321                      G_OBJECT_TYPE_NAME (widget), widget,
322                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
323                      adjusted_min, adjusted_natural,
324                      cached_size->minimum_size, cached_size->natural_size);
325           /* don't use the adjustment */
326         }
327       else if (adjusted_min > adjusted_natural)
328         {
329           g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
330                      G_OBJECT_TYPE_NAME (widget), widget,
331                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
332                      adjusted_min, adjusted_natural,
333                      cached_size->minimum_size, cached_size->natural_size);
334           /* don't use the adjustment */
335         }
336       else
337         {
338           /* adjustment looks good */
339           cached_size->minimum_size = adjusted_min;
340           cached_size->natural_size = adjusted_natural;
341         }
342
343       /* Update size-groups with our request and update our cached requests
344        * with the size-group values in a single pass.
345        */
346       _gtk_size_group_bump_requisition (widget,
347                                         orientation,
348                                         &cached_size->minimum_size,
349                                         &cached_size->natural_size);
350     }
351
352   if (minimum_size)
353     *minimum_size = cached_size->minimum_size;
354
355   if (natural_size)
356     *natural_size = cached_size->natural_size;
357
358   g_assert (cached_size->minimum_size <= cached_size->natural_size);
359
360   GTK_NOTE (SIZE_REQUEST,
361             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
362                      widget, G_OBJECT_TYPE_NAME (widget),
363                      orientation == GTK_SIZE_GROUP_HORIZONTAL ?
364                      "width for height" : "height for width" ,
365                      for_size,
366                      cached_size->minimum_size,
367                      cached_size->natural_size,
368                      found_in_cache ? "yes" : "no"));
369
370 }
371
372 /**
373  * gtk_widget_get_request_mode:
374  * @widget: a #GtkWidget instance
375  *
376  * Gets whether the widget prefers a height-for-width layout
377  * or a width-for-height layout.
378  *
379  * <note><para>#GtkBin widgets generally propagate the preference of
380  * their child, container widgets need to request something either in
381  * context of their children or in context of their allocation
382  * capabilities.</para></note>
383  *
384  * Returns: The #GtkSizeRequestMode preferred by @widget.
385  *
386  * Since: 3.0
387  */
388 GtkSizeRequestMode
389 gtk_widget_get_request_mode (GtkWidget *widget)
390 {
391   GtkWidgetClass *klass;
392
393   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
394
395   klass = GTK_WIDGET_GET_CLASS (widget);
396   if (klass->get_request_mode)
397     return klass->get_request_mode (widget);
398
399   /* By default widgets are height-for-width. */
400   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
401 }
402
403 /**
404  * gtk_widget_get_preferred_width:
405  * @widget: a #GtkWidget instance
406  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
407  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
408  *
409  * Retrieves a widget's initial minimum and natural width.
410  *
411  * <note><para>This call is specific to height-for-width
412  * requests.</para></note>
413  *
414  * The returned request will be modified by the
415  * GtkWidgetClass::adjust_size_request virtual method and by any
416  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
417  * is the one that should be used for layout, not necessarily the one
418  * returned by the widget itself.
419  *
420  * Since: 3.0
421  */
422 void
423 gtk_widget_get_preferred_width (GtkWidget *widget,
424                                 gint      *minimum_width,
425                                 gint      *natural_width)
426 {
427   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
428                                 -1, minimum_width, natural_width);
429 }
430
431
432 /**
433  * gtk_widget_get_preferred_height:
434  * @widget: a #GtkWidget instance
435  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
436  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
437  *
438  * Retrieves a widget's initial minimum and natural height.
439  *
440  * <note><para>This call is specific to width-for-height requests.</para></note>
441  *
442  * The returned request will be modified by the
443  * GtkWidgetClass::adjust_size_request virtual method and by any
444  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
445  * is the one that should be used for layout, not necessarily the one
446  * returned by the widget itself.
447  *
448  * Since: 3.0
449  */
450 void
451 gtk_widget_get_preferred_height (GtkWidget *widget,
452                                  gint      *minimum_height,
453                                  gint      *natural_height)
454 {
455   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
456                                 -1, minimum_height, natural_height);
457 }
458
459
460
461 /**
462  * gtk_widget_get_preferred_width_for_height:
463  * @widget: a #GtkWidget instance
464  * @height: the height which is available for allocation
465  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
466  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
467  *
468  * Retrieves a widget's minimum and natural width if it would be given
469  * the specified @height.
470  *
471  * The returned request will be modified by the
472  * GtkWidgetClass::adjust_size_request virtual method and by any
473  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
474  * is the one that should be used for layout, not necessarily the one
475  * returned by the widget itself.
476  *
477  * Since: 3.0
478  */
479 void
480 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
481                                            gint       height,
482                                            gint      *minimum_width,
483                                            gint      *natural_width)
484 {
485   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
486                                 height, minimum_width, natural_width);
487 }
488
489 /**
490  * gtk_widget_get_preferred_height_for_width:
491  * @widget: a #GtkWidget instance
492  * @width: the width which is available for allocation
493  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
494  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
495  *
496  * Retrieves a widget's minimum and natural height if it would be given
497  * the specified @width.
498  *
499  * The returned request will be modified by the
500  * GtkWidgetClass::adjust_size_request virtual method and by any
501  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
502  * is the one that should be used for layout, not necessarily the one
503  * returned by the widget itself.
504  *
505  * Since: 3.0
506  */
507 void
508 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
509                                            gint       width,
510                                            gint      *minimum_height,
511                                            gint      *natural_height)
512 {
513   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
514                                 width, minimum_height, natural_height);
515 }
516
517 /**
518  * gtk_widget_get_preferred_size:
519  * @widget: a #GtkWidget instance
520  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
521  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
522  *
523  * Retrieves the minimum and natural size of a widget, taking
524  * into account the widget's preference for height-for-width management.
525  *
526  * This is used to retrieve a suitable size by container widgets which do
527  * not impose any restrictions on the child placement. It can be used
528  * to deduce toplevel window and menu sizes as well as child widgets in
529  * free-form containers such as GtkLayout.
530  *
531  * <note><para>Handle with care. Note that the natural height of a height-for-width
532  * widget will generally be a smaller size than the minimum height, since the required
533  * height for the natural width is generally smaller than the required height for
534  * the minimum width.</para></note>
535  *
536  * Since: 3.0
537  */
538 void
539 gtk_widget_get_preferred_size (GtkWidget      *widget,
540                                GtkRequisition *minimum_size,
541                                GtkRequisition *natural_size)
542 {
543   gint min_width, nat_width;
544   gint min_height, nat_height;
545
546   g_return_if_fail (GTK_IS_WIDGET (widget));
547
548   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
549     {
550       gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
551
552       if (minimum_size)
553         {
554           minimum_size->width = min_width;
555           gtk_widget_get_preferred_height_for_width (widget, min_width,
556                                                      &minimum_size->height, NULL);
557         }
558
559       if (natural_size)
560         {
561           natural_size->width = nat_width;
562           gtk_widget_get_preferred_height_for_width (widget, nat_width,
563                                                      NULL, &natural_size->height);
564         }
565     }
566   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
567     {
568       gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
569
570       if (minimum_size)
571         {
572           minimum_size->height = min_height;
573           gtk_widget_get_preferred_width_for_height (widget, min_height,
574                                                      &minimum_size->width, NULL);
575         }
576
577       if (natural_size)
578         {
579           natural_size->height = nat_height;
580           gtk_widget_get_preferred_width_for_height (widget, nat_height,
581                                                      NULL, &natural_size->width);
582         }
583     }
584 }
585
586
587 static gint
588 compare_gap (gconstpointer p1,
589              gconstpointer p2,
590              gpointer      data)
591 {
592   GtkRequestedSize *sizes = data;
593   const guint *c1 = p1;
594   const guint *c2 = p2;
595
596   const gint d1 = MAX (sizes[*c1].natural_size -
597                        sizes[*c1].minimum_size,
598                        0);
599   const gint d2 = MAX (sizes[*c2].natural_size -
600                        sizes[*c2].minimum_size,
601                        0);
602
603   gint delta = (d2 - d1);
604
605   if (0 == delta)
606     delta = (*c2 - *c1);
607
608   return delta;
609 }
610
611 /**
612  * gtk_distribute_natural_allocation:
613  * @extra_space: Extra space to redistribute among children after subtracting
614  *               minimum sizes and any child padding from the overall allocation
615  * @n_requested_sizes: Number of requests to fit into the allocation
616  * @sizes: An array of structs with a client pointer and a minimum/natural size
617  *         in the orientation of the allocation.
618  *
619  * Distributes @extra_space to child @sizes by bringing smaller
620  * children up to natural size first.
621  *
622  * The remaining space will be added to the @minimum_size member of the
623  * GtkRequestedSize struct. If all sizes reach their natural size then
624  * the remaining space is returned.
625  *
626  * Returns: The remainder of @extra_space after redistributing space
627  * to @sizes.
628  */
629 gint
630 gtk_distribute_natural_allocation (gint              extra_space,
631                                    guint             n_requested_sizes,
632                                    GtkRequestedSize *sizes)
633 {
634   guint *spreading;
635   gint   i;
636
637   g_return_val_if_fail (extra_space >= 0, 0);
638
639   spreading = g_newa (guint, n_requested_sizes);
640
641   for (i = 0; i < n_requested_sizes; i++)
642     spreading[i] = i;
643
644   /* Distribute the container's extra space c_gap. We want to assign
645    * this space such that the sum of extra space assigned to children
646    * (c^i_gap) is equal to c_cap. The case that there's not enough
647    * space for all children to take their natural size needs some
648    * attention. The goals we want to achieve are:
649    *
650    *   a) Maximize number of children taking their natural size.
651    *   b) The allocated size of children should be a continuous
652    *   function of c_gap.  That is, increasing the container size by
653    *   one pixel should never make drastic changes in the distribution.
654    *   c) If child i takes its natural size and child j doesn't,
655    *   child j should have received at least as much gap as child i.
656    *
657    * The following code distributes the additional space by following
658    * these rules.
659    */
660
661   /* Sort descending by gap and position. */
662   g_qsort_with_data (spreading,
663                      n_requested_sizes, sizeof (guint),
664                      compare_gap, sizes);
665
666   /* Distribute available space.
667    * This master piece of a loop was conceived by Behdad Esfahbod.
668    */
669   for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
670     {
671       /* Divide remaining space by number of remaining children.
672        * Sort order and reducing remaining space by assigned space
673        * ensures that space is distributed equally.
674        */
675       gint glue = (extra_space + i) / (i + 1);
676       gint gap = sizes[(spreading[i])].natural_size
677         - sizes[(spreading[i])].minimum_size;
678
679       gint extra = MIN (glue, gap);
680
681       sizes[spreading[i]].minimum_size += extra;
682
683       extra_space -= extra;
684     }
685
686   return extra_space;
687 }