]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
Fixed gtk_distribute_natural_allocation() to place the g_newa *after* g_return_if_fai...
[~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
25 /**
26  * SECTION:gtksizerequest
27  * @Short_Description: Height-for-width geometry management
28  * @Title: GtkSizeRequest
29  *
30  * The GtkSizeRequest interface is GTK+'s height-for-width (and width-for-height)
31  * geometry management system. Height-for-width means that a widget can
32  * change how much vertical space it needs, depending on the amount
33  * of horizontal space that it is given (and similar for width-for-height).
34  * The most common example is a label that reflows to fill up the available
35  * width, wraps to fewer lines, and therefore needs less height.
36  *
37  * GTK+'s traditional two-pass <link linkend="size-allocation">size-allocation</link>
38  * algorithm does not allow this flexibility. #GtkWidget provides a default
39  * implementation of the #GtkSizeRequest interface for existing widgets,
40  * which always requests the same height, regardless of the available width.
41  *
42  * <refsect2>
43  * <title>Implementing GtkSizeRequest</title>
44  * <para>
45  * Some important things to keep in mind when implementing
46  * the GtkSizeRequest interface and when using it in container
47  * implementations.
48  *
49  * The geometry management system will query a logical hierarchy in
50  * only one orientation at a time. When widgets are initially queried
51  * for their minimum sizes it is generally done in a dual pass
52  * in the direction chosen by the toplevel.
53  *
54  * For instance when queried in the normal height-for-width mode:
55  * First the default minimum and natural width for each widget
56  * in the interface will computed and collectively returned to
57  * the toplevel by way of gtk_size_request_get_width().
58  * Next, the toplevel will use the minimum width to query for the
59  * minimum height contextual to that width using
60  * gtk_size_request_get_height_for_width(), which will also be a
61  * highly recursive operation. This minimum-for-minimum size can be
62  * used to set the minimum size constraint on the toplevel.
63  *
64  * When allocating, each container can use the minimum and natural
65  * sizes reported by their children to allocate natural sizes and
66  * expose as much content as possible with the given allocation.
67  *
68  * That means that the request operation at allocation time will
69  * usually fire again in contexts of different allocated sizes than
70  * the ones originally queried for. #GtkSizeRequest caches a
71  * small number of results to avoid re-querying for the same
72  * allocated size in one allocation cycle.
73  *
74  * A widget that does not actually do height-for-width
75  * or width-for-height size negotiations only has to implement
76  * get_width() and get_height().
77  *
78  * If a widget does move content around to smartly use up the
79  * allocated size, then it must support the request properly in
80  * both orientations; even if the request only makes sense in
81  * one orientation.
82  *
83  * For instance, a GtkLabel that does height-for-width word wrapping
84  * will not expect to have get_height() called because that
85  * call is specific to a width-for-height request, in this case the
86  * label must return the heights contextual to its minimum possible
87  * width. By following this rule any widget that handles height-for-width
88  * or width-for-height requests will always be allocated at least
89  * enough space to fit its own content.
90  *
91  * Often a widget needs to get its own request during size request or
92  * allocation, for example when computing height it may need to also
93  * compute width, or when deciding how to use an allocation the widget may
94  * need to know its natural size. In these cases, the widget should be
95  * careful to call its virtual methods directly, like this:
96  * <example>
97  *   <title>Widget calling its own size request method.</title>
98  *   <programlisting>
99  * GTK_SIZE_REQUEST_GET_IFACE(widget)-&gt;get_width(GTK_SIZE_REQUEST(widget), &min, &natural);
100  *   </programlisting>
101  * </example>
102  *
103  * It will not work to use the wrapper functions, such as
104  * gtk_size_request_get_width(), inside your own size request
105  * implementation. These return a request adjusted by #GtkSizeGroup
106  * and by the GtkWidgetClass::adjust_size_request virtual method. If a
107  * widget used the wrappers inside its virtual method implementations,
108  * then the adjustments (such as widget margins) would be applied
109  * twice. GTK+ therefore does not allow this and will warn if you try
110  * to do it.
111  *
112  * Of course if you are getting the size request for
113  * <emphasis>another</emphasis> widget, such as a child of a
114  * container, you <emphasis>must</emphasis> use the wrapper APIs;
115  * otherwise, you would not properly consider widget margins,
116  * #GtkSizeGroup, and so forth.
117  * </para>
118  * </refsect2>
119  */
120
121
122 #include <config.h>
123 #include "gtksizerequest.h"
124 #include "gtksizegroup.h"
125 #include "gtkprivate.h"
126 #include "gtkintl.h"
127
128 typedef GtkSizeRequestIface GtkSizeRequestInterface;
129 G_DEFINE_INTERFACE (GtkSizeRequest,
130                     gtk_size_request,
131                     GTK_TYPE_WIDGET);
132
133
134 static void
135 gtk_size_request_default_init (GtkSizeRequestInterface *iface)
136 {
137 }
138
139
140 /* looks for a cached size request for this for_size. If not
141  * found, returns the oldest entry so it can be overwritten
142  *
143  * Note that this caching code was directly derived from
144  * the Clutter toolkit.
145  */
146 static gboolean
147 get_cached_size (SizeRequestCache  *cache,
148                  GtkSizeGroupMode   orientation,
149                  gint               for_size,
150                  SizeRequest      **result)
151 {
152   guint i, n_sizes;
153   SizeRequest  *cached_sizes;
154
155   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
156     {
157       cached_sizes = cache->widths;
158       n_sizes = cache->cached_widths;
159     }
160   else
161     {
162       cached_sizes = cache->heights;
163       n_sizes = cache->cached_widths;
164     }
165
166   /* Search for an already cached size */
167   for (i = 0; i < n_sizes; i++)
168     {
169       if (cached_sizes[i].for_size == for_size)
170         {
171           *result = &cached_sizes[i];
172           return TRUE;
173         }
174     }
175
176   /* If not found, pull a new size from the cache, the returned size cache
177    * will immediately be used to cache the new computed size so we go ahead
178    * and increment the last_cached_width/height right away */
179   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
180     {
181       if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
182         {
183           cache->cached_widths++;
184           cache->last_cached_width = cache->cached_widths - 1;
185         }
186       else
187         {
188           if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
189             cache->last_cached_width = 0;
190         }
191
192       *result = &cache->widths[cache->last_cached_width];
193     }
194   else /* GTK_SIZE_GROUP_VERTICAL */
195     {
196       if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
197         {
198           cache->cached_heights++;
199           cache->last_cached_height = cache->cached_heights - 1;
200         }
201       else
202         {
203           if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
204             cache->last_cached_height = 0;
205         }
206
207       *result = &cache->heights[cache->last_cached_height];
208     }
209
210   return FALSE;
211 }
212
213 static void
214 do_size_request (GtkWidget      *widget,
215                  GtkRequisition *requisition)
216 {
217   /* Now we dont bother caching the deprecated "size-request" returns,
218    * just unconditionally invoke here just in case we run into legacy stuff */
219   gtk_widget_ensure_style (widget);
220   g_signal_emit_by_name (widget, "size-request", requisition);
221 }
222
223 #ifndef G_DISABLE_CHECKS
224 static GQuark recursion_check_quark = 0;
225 #endif /* G_DISABLE_CHECKS */
226
227 static void
228 push_recursion_check (GtkSizeRequest  *request,
229                       GtkSizeGroupMode orientation,
230                       gint             for_size)
231 {
232 #ifndef G_DISABLE_CHECKS
233   const char *previous_method;
234   const char *method;
235
236   if (recursion_check_quark == 0)
237     recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
238
239   previous_method = g_object_get_qdata (G_OBJECT (request), recursion_check_quark);
240
241   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
242     {
243       method = for_size < 0 ? "get_width" : "get_width_for_height";
244     }
245   else
246     {
247       method = for_size < 0 ? "get_height" : "get_height_for_width";
248     }
249
250   if (previous_method != NULL)
251     {
252       g_warning ("%s %p: widget tried to gtk_size_request_%s inside "
253                  " GtkSizeRequest::%s implementation. "
254                  "Should just invoke GTK_SIZE_REQUEST_GET_IFACE(widget)->%s "
255                  "directly rather than using gtk_size_request_%s",
256                  G_OBJECT_TYPE_NAME (request), request,
257                  method, previous_method,
258                  method, method);
259     }
260
261   g_object_set_qdata (G_OBJECT (request), recursion_check_quark, (char*) method);
262 #endif /* G_DISABLE_CHECKS */
263 }
264
265 static void
266 pop_recursion_check (GtkSizeRequest  *request,
267                      GtkSizeGroupMode orientation)
268 {
269 #ifndef G_DISABLE_CHECKS
270   g_object_set_qdata (G_OBJECT (request), recursion_check_quark, NULL);
271 #endif
272 }
273
274 static void
275 compute_size_for_orientation (GtkSizeRequest    *request,
276                               GtkSizeGroupMode   orientation,
277                               gint               for_size,
278                               gint              *minimum_size,
279                               gint              *natural_size)
280 {
281   SizeRequestCache *cache;
282   SizeRequest      *cached_size;
283   GtkWidget        *widget;
284   gboolean          found_in_cache = FALSE;
285   int adjusted_min, adjusted_natural;
286
287   g_return_if_fail (GTK_IS_SIZE_REQUEST (request));
288   g_return_if_fail (minimum_size != NULL || natural_size != NULL);
289
290   widget = GTK_WIDGET (request);
291   cache  = _gtk_widget_peek_request_cache (widget);
292
293   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
294     {
295       cached_size = &cache->widths[0];
296
297       if (!GTK_WIDGET_WIDTH_REQUEST_NEEDED (request))
298         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
299       else
300         {
301           memset (cache->widths, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
302           cache->cached_widths = 1;
303           cache->last_cached_width = 0;
304         }
305     }
306   else
307     {
308       cached_size = &cache->heights[0];
309
310       if (!GTK_WIDGET_HEIGHT_REQUEST_NEEDED (request))
311         found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
312       else
313         {
314           memset (cache->heights, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
315           cache->cached_heights = 1;
316           cache->last_cached_height = 0;
317         }
318     }
319
320   if (!found_in_cache)
321     {
322       GtkRequisition requisition = { 0, 0 };
323       gint min_size = 0, nat_size = 0;
324       gint requisition_size;
325
326       /* Unconditional size request runs but is often unhandled. */
327       do_size_request (widget, &requisition);
328
329       push_recursion_check (request, orientation, for_size);
330       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
331         {
332           requisition_size = requisition.width;
333
334           if (for_size < 0)
335             GTK_SIZE_REQUEST_GET_IFACE (request)->get_width (request, &min_size, &nat_size);
336           else
337             GTK_SIZE_REQUEST_GET_IFACE (request)->get_width_for_height (request, for_size, 
338                                                                         &min_size, &nat_size);
339         }
340       else
341         {
342           requisition_size = requisition.height;
343
344           if (for_size < 0)
345             GTK_SIZE_REQUEST_GET_IFACE (request)->get_height (request, &min_size, &nat_size);
346           else
347             GTK_SIZE_REQUEST_GET_IFACE (request)->get_height_for_width (request, for_size, 
348                                                                         &min_size, &nat_size);
349         }
350       pop_recursion_check (request, orientation);
351
352       if (min_size > nat_size)
353         {
354           g_warning ("%s %p reported min size %d and natural size %d; natural size must be >= min size",
355                      G_OBJECT_TYPE_NAME (request), request, min_size, nat_size);
356         }
357
358       /* Support for dangling "size-request" signal implementations on
359        * legacy widgets
360        */
361       min_size = MAX (min_size, requisition_size);
362       nat_size = MAX (nat_size, requisition_size);
363
364       cached_size->minimum_size = min_size;
365       cached_size->natural_size = nat_size;
366       cached_size->for_size     = for_size;
367
368       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
369         GTK_PRIVATE_UNSET_FLAG (request, GTK_WIDTH_REQUEST_NEEDED);
370       else
371         GTK_PRIVATE_UNSET_FLAG (request, GTK_HEIGHT_REQUEST_NEEDED);
372
373       adjusted_min = cached_size->minimum_size;
374       adjusted_natural = cached_size->natural_size;
375       GTK_WIDGET_GET_CLASS (request)->adjust_size_request (GTK_WIDGET (request),
376                                                            orientation == GTK_SIZE_GROUP_HORIZONTAL ?
377                                                            GTK_ORIENTATION_HORIZONTAL :
378                                                            GTK_ORIENTATION_VERTICAL,
379                                                            cached_size->for_size,
380                                                            &adjusted_min,
381                                                            &adjusted_natural);
382
383       if (adjusted_min < cached_size->minimum_size ||
384           adjusted_natural < cached_size->natural_size)
385         {
386           g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
387                      G_OBJECT_TYPE_NAME (request), request,
388                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
389                      adjusted_min, adjusted_natural,
390                      cached_size->minimum_size, cached_size->natural_size);
391           /* don't use the adjustment */
392         }
393       else if (adjusted_min > adjusted_natural)
394         {
395           g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
396                      G_OBJECT_TYPE_NAME (request), request,
397                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
398                      adjusted_min, adjusted_natural,
399                      cached_size->minimum_size, cached_size->natural_size);
400           /* don't use the adjustment */
401         }
402       else
403         {
404           /* adjustment looks good */
405           cached_size->minimum_size = adjusted_min;
406           cached_size->natural_size = adjusted_natural;
407         }
408
409       /* Update size-groups with our request and update our cached requests 
410        * with the size-group values in a single pass.
411        */
412       _gtk_size_group_bump_requisition (GTK_WIDGET (request),
413                                         orientation, 
414                                         &cached_size->minimum_size,
415                                         &cached_size->natural_size);
416     }
417
418   if (minimum_size)
419     *minimum_size = cached_size->minimum_size;
420
421   if (natural_size)
422     *natural_size = cached_size->natural_size;
423
424   g_assert (cached_size->minimum_size <= cached_size->natural_size);
425
426   GTK_NOTE (SIZE_REQUEST,
427             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
428                      request, G_OBJECT_TYPE_NAME (request),
429                      orientation == GTK_SIZE_GROUP_HORIZONTAL ?
430                      "width for height" : "height for width" ,
431                      for_size,
432                      cached_size->minimum_size,
433                      cached_size->natural_size,
434                      found_in_cache ? "yes" : "no"));
435
436 }
437
438 /**
439  * gtk_size_request_get_request_mode:
440  * @widget: a #GtkSizeRequest instance
441  *
442  * Gets whether the widget prefers a height-for-width layout
443  * or a width-for-height layout.
444  *
445  * <note><para>#GtkBin widgets generally propagate the preference of
446  * their child, container widgets need to request something either in
447  * context of their children or in context of their allocation
448  * capabilities.</para></note>
449  *
450  * Returns: The #GtkSizeRequestMode preferred by @widget.
451  *
452  * Since: 3.0
453  */
454 GtkSizeRequestMode
455 gtk_size_request_get_request_mode (GtkSizeRequest *widget)
456 {
457   GtkSizeRequestIface *iface;
458
459   g_return_val_if_fail (GTK_IS_SIZE_REQUEST (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
460
461   iface = GTK_SIZE_REQUEST_GET_IFACE (widget);
462   if (iface->get_request_mode)
463     return iface->get_request_mode (widget);
464
465   /* By default widgets are height-for-width. */
466   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
467 }
468
469 /**
470  * gtk_size_request_get_width:
471  * @widget: a #GtkSizeRequest instance
472  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
473  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
474  *
475  * Retrieves a widget's initial minimum and natural width.
476  *
477  * <note><para>This call is specific to height-for-width
478  * requests.</para></note>
479  *
480  * The returned request will be modified by the
481  * GtkWidgetClass::adjust_size_request virtual method and by any
482  * #GtkSizeGroup that have been applied. That is, the returned request
483  * is the one that should be used for layout, not necessarily the one
484  * returned by the widget itself.
485  *
486  * Since: 3.0
487  */
488 void
489 gtk_size_request_get_width (GtkSizeRequest *widget,
490                             gint           *minimum_width,
491                             gint           *natural_width)
492 {
493   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
494                                 -1, minimum_width, natural_width);
495 }
496
497
498 /**
499  * gtk_size_request_get_height:
500  * @widget: a #GtkSizeRequest instance
501  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
502  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
503  *
504  * Retrieves a widget's initial minimum and natural height.
505  *
506  * <note><para>This call is specific to width-for-height requests.</para></note>
507  *
508  * The returned request will be modified by the
509  * GtkWidgetClass::adjust_size_request virtual method and by any
510  * #GtkSizeGroup that have been applied. That is, the returned request
511  * is the one that should be used for layout, not necessarily the one
512  * returned by the widget itself.
513  *
514  * Since: 3.0
515  */
516 void
517 gtk_size_request_get_height (GtkSizeRequest *widget,
518                              gint           *minimum_height,
519                              gint           *natural_height)
520 {
521   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
522                                 -1, minimum_height, natural_height);
523 }
524
525
526
527 /**
528  * gtk_size_request_get_width_for_height:
529  * @widget: a #GtkSizeRequest instance
530  * @height: the height which is available for allocation
531  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
532  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
533  *
534  * Retrieves a widget's minimum and natural width if it would be given
535  * the specified @height.
536  *
537  * The returned request will be modified by the
538  * GtkWidgetClass::adjust_size_request virtual method and by any
539  * #GtkSizeGroup that have been applied. That is, the returned request
540  * is the one that should be used for layout, not necessarily the one
541  * returned by the widget itself.
542  *
543  * Since: 3.0
544  */
545 void
546 gtk_size_request_get_width_for_height (GtkSizeRequest *widget,
547                                        gint            height,
548                                        gint           *minimum_width,
549                                        gint           *natural_width)
550 {
551   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
552                                 height, minimum_width, natural_width);
553 }
554
555 /**
556  * gtk_size_request_get_height_for_width:
557  * @widget: a #GtkSizeRequest instance
558  * @width: the width which is available for allocation
559  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
560  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
561  *
562  * Retrieves a widget's minimum and natural height if it would be given
563  * the specified @width.
564  *
565  * The returned request will be modified by the
566  * GtkWidgetClass::adjust_size_request virtual method and by any
567  * #GtkSizeGroup that have been applied. That is, the returned request
568  * is the one that should be used for layout, not necessarily the one
569  * returned by the widget itself.
570  *
571  * Since: 3.0
572  */
573 void
574 gtk_size_request_get_height_for_width (GtkSizeRequest *widget,
575                                        gint            width,
576                                        gint           *minimum_height,
577                                        gint           *natural_height)
578 {
579   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
580                                 width, minimum_height, natural_height);
581 }
582
583 /**
584  * gtk_size_request_get_size:
585  * @widget: a #GtkSizeRequest instance
586  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
587  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
588  *
589  * Retrieves the minimum and natural size of a widget taking
590  * into account the widget's preference for height-for-width management.
591  *
592  * This is used to retrieve a suitable size by container widgets which do
593  * not impose any restrictions on the child placement.
594  *
595  * Since: 3.0
596  */
597 void
598 gtk_size_request_get_size (GtkSizeRequest    *widget,
599                            GtkRequisition    *minimum_size,
600                            GtkRequisition    *natural_size)
601 {
602   gint min_width, nat_width;
603   gint min_height, nat_height;
604
605   g_return_if_fail (GTK_IS_SIZE_REQUEST (widget));
606
607   if (gtk_size_request_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
608     {
609       gtk_size_request_get_width (widget, &min_width, &nat_width);
610
611       if (minimum_size)
612         {
613           minimum_size->width = min_width;
614           gtk_size_request_get_height_for_width (widget, min_width,
615                                                  &minimum_size->height, NULL);
616         }
617
618       if (natural_size)
619         {
620           natural_size->width = nat_width;
621           gtk_size_request_get_height_for_width (widget, nat_width,
622                                                  NULL, &natural_size->height);
623         }
624     }
625   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
626     {
627       gtk_size_request_get_height (widget, &min_height, &nat_height);
628
629       if (minimum_size)
630         {
631           minimum_size->height = min_height;
632           gtk_size_request_get_width_for_height (widget, min_height,
633                                                  &minimum_size->width, NULL);
634         }
635
636       if (natural_size)
637         {
638           natural_size->height = nat_height;
639           gtk_size_request_get_width_for_height (widget, nat_height,
640                                                  NULL, &natural_size->width);
641         }
642     }
643 }
644
645
646 static gint
647 compare_gap (gconstpointer p1,
648              gconstpointer p2,
649              gpointer      data)
650 {
651   GtkRequestedSize *sizes = data;
652   const guint *c1 = p1;
653   const guint *c2 = p2;
654
655   const gint d1 = MAX (sizes[*c1].natural_size -
656                        sizes[*c1].minimum_size,
657                        0);
658   const gint d2 = MAX (sizes[*c2].natural_size -
659                        sizes[*c2].minimum_size,
660                        0);
661
662   gint delta = (d2 - d1);
663
664   if (0 == delta)
665     delta = (*c2 - *c1);
666
667   return delta;
668 }
669
670 /**
671  * gtk_distribute_natural_allocation: 
672  * @extra_space: Extra space to redistribute among children after subtracting
673  *               minimum sizes and any child padding from the overall allocation
674  * @n_requested_sizes: Number of requests to fit into the allocation
675  * @sizes: An array of structs with a client pointer and a minimum/natural size
676  *         in the orientation of the allocation.
677  *
678  * Distributes @extra_space to child @sizes by bringing up smaller
679  * children up to natural size first.
680  *
681  * The remaining space will be added to the @minimum_size member of the
682  * GtkRequestedSize struct. If all sizes reach their natural size then
683  * the remaining space is returned.
684  *
685  * Returns: The remainder of @extra_space after redistributing space
686  * to @sizes.
687  */
688 gint 
689 gtk_distribute_natural_allocation (gint              extra_space,
690                                    guint             n_requested_sizes,
691                                    GtkRequestedSize *sizes)
692 {
693   guint *spreading;
694   gint   i;
695
696   g_return_val_if_fail (extra_space >= 0, 0);
697
698   spreading = g_newa (guint, n_requested_sizes);
699
700   for (i = 0; i < n_requested_sizes; i++)
701     spreading[i] = i;
702
703   /* Distribute the container's extra space c_gap. We want to assign
704    * this space such that the sum of extra space assigned to children
705    * (c^i_gap) is equal to c_cap. The case that there's not enough
706    * space for all children to take their natural size needs some
707    * attention. The goals we want to achieve are:
708    *
709    *   a) Maximize number of children taking their natural size.
710    *   b) The allocated size of children should be a continuous
711    *   function of c_gap.  That is, increasing the container size by
712    *   one pixel should never make drastic changes in the distribution.
713    *   c) If child i takes its natural size and child j doesn't,
714    *   child j should have received at least as much gap as child i.
715    *
716    * The following code distributes the additional space by following
717    * these rules.
718    */
719   
720   /* Sort descending by gap and position. */
721   g_qsort_with_data (spreading,
722                      n_requested_sizes, sizeof (guint),
723                      compare_gap, sizes);
724   
725   /* Distribute available space.
726    * This master piece of a loop was conceived by Behdad Esfahbod.
727    */
728   for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
729     {
730       /* Divide remaining space by number of remaining children.
731        * Sort order and reducing remaining space by assigned space
732        * ensures that space is distributed equally.
733        */
734       gint glue = (extra_space + i) / (i + 1);
735       gint gap = sizes[(spreading[i])].natural_size
736         - sizes[(spreading[i])].minimum_size;
737       
738       gint extra = MIN (glue, gap);
739       
740       sizes[spreading[i]].minimum_size += extra;
741       
742       extra_space -= extra;
743     }
744
745   return extra_space;
746 }
747