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