]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
Fixed bug in GtkSizeRequest code where the cache is not reset properly
[~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
35 #ifndef G_DISABLE_CHECKS
36 static GQuark recursion_check_quark = 0;
37 #endif /* G_DISABLE_CHECKS */
38
39 static void
40 push_recursion_check (GtkWidget       *widget,
41                       GtkSizeGroupMode orientation,
42                       gint             for_size)
43 {
44 #ifndef G_DISABLE_CHECKS
45   const char *previous_method;
46   const char *method;
47
48   if (recursion_check_quark == 0)
49     recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
50
51   previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
52
53   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
54     {
55       method = for_size < 0 ? "get_width" : "get_width_for_height";
56     }
57   else
58     {
59       method = for_size < 0 ? "get_height" : "get_height_for_width";
60     }
61
62   if (previous_method != NULL)
63     {
64       g_warning ("%s %p: widget tried to gtk_widget_%s inside "
65                  " GtkWidget     ::%s implementation. "
66                  "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
67                  "directly rather than using gtk_widget_%s",
68                  G_OBJECT_TYPE_NAME (widget), widget,
69                  method, previous_method,
70                  method, method);
71     }
72
73   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
74 #endif /* G_DISABLE_CHECKS */
75 }
76
77 static void
78 pop_recursion_check (GtkWidget       *widget,
79                      GtkSizeGroupMode orientation)
80 {
81 #ifndef G_DISABLE_CHECKS
82   g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
83 #endif
84 }
85
86
87 static void
88 clear_cache (SizeRequestCache   *cache,
89              GtkSizeGroupMode    orientation)
90 {
91   SizeRequest **sizes;
92   gint          i;
93
94   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
95     {
96       sizes = cache->widths;
97
98       cache->widths            = NULL;
99       cache->cached_widths     = 0;
100       cache->last_cached_width = 0;
101       cache->cached_base_width = FALSE;
102     }
103   else
104     {
105       sizes = cache->heights;
106
107       cache->heights            = NULL;
108       cache->cached_heights     = 0;
109       cache->last_cached_height = 0; 
110       cache->cached_base_height = FALSE;
111    }
112
113   if (sizes)
114     {
115       for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES && sizes[i] != NULL; i++)
116         g_slice_free (SizeRequest, sizes[i]);
117       
118       g_slice_free1 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES, sizes);
119     }
120 }
121
122 void
123 _gtk_widget_free_cached_sizes (GtkWidget *widget)
124 {
125   SizeRequestCache   *cache;
126
127   cache = _gtk_widget_peek_request_cache (widget);
128
129   clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
130   clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);
131 }
132
133 /* This function checks if 'request_needed' flag is present
134  * and resets the cache state if a request is needed for
135  * a given orientation.
136  */
137 static SizeRequestCache *
138 init_cache (GtkWidget        *widget)
139 {
140   SizeRequestCache *cache;
141
142   cache = _gtk_widget_peek_request_cache (widget);
143
144   if (_gtk_widget_get_width_request_needed (widget))
145     clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
146   
147   if (_gtk_widget_get_height_request_needed (widget))
148     clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);
149
150   return cache;
151 }
152
153 /* looks for a cached size request for this for_size. If not
154  * found, returns the oldest entry so it can be overwritten
155  *
156  * Note that this caching code was originally derived from
157  * the Clutter toolkit but has evolved for other GTK+ requirements.
158  */
159 static gboolean
160 get_cached_size (GtkWidget         *widget,
161                  GtkSizeGroupMode   orientation,
162                  gint               for_size,
163                  CachedSize       **result)
164 {
165   SizeRequestCache  *cache;
166   SizeRequest      **cached_sizes;
167   guint              i, n_sizes;
168
169   cache = init_cache (widget);
170
171   if (for_size < 0)
172     {
173       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
174         {
175           *result = &cache->cached_width;
176           return cache->cached_base_width;
177         }
178       else
179         {
180           *result = &cache->cached_height;
181           return cache->cached_base_height;
182         }
183     }
184
185   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
186     {
187       cached_sizes = cache->widths;
188       n_sizes      = cache->cached_widths;
189     }
190   else
191     {
192       cached_sizes = cache->heights;
193       n_sizes      = cache->cached_heights;
194     }
195
196   /* Search for an already cached size */
197   for (i = 0; i < n_sizes; i++)
198     {
199       if (cached_sizes[i]->lower_for_size <= for_size &&
200           cached_sizes[i]->upper_for_size >= for_size)
201         {
202           *result = &cached_sizes[i]->cached_size;
203           return TRUE;
204         }
205     }
206
207   return FALSE;
208 }
209
210 static void
211 commit_cached_size (GtkWidget         *widget,
212                     GtkSizeGroupMode   orientation,
213                     gint               for_size,
214                     gint               minimum_size,
215                     gint               natural_size)
216 {
217   SizeRequestCache  *cache;
218   SizeRequest      **cached_sizes;
219   guint              i, n_sizes;
220
221   cache = _gtk_widget_peek_request_cache (widget);
222
223   /* First handle caching of the base requests */
224   if (for_size < 0)
225     {
226       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
227         {
228           cache->cached_width.minimum_size = minimum_size;
229           cache->cached_width.natural_size = natural_size;
230           cache->cached_base_width = TRUE;
231         }
232       else
233         {
234           cache->cached_height.minimum_size = minimum_size;
235           cache->cached_height.natural_size = natural_size;
236           cache->cached_base_height = TRUE;
237         }
238       return;
239     }
240
241   /* Check if the minimum_size and natural_size is already
242    * in the cache and if this result can be used to extend
243    * that cache entry 
244    */
245   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
246     {
247       cached_sizes = cache->widths;
248       n_sizes = cache->cached_widths;
249     }
250   else
251     {
252       cached_sizes = cache->heights;
253       n_sizes = cache->cached_heights;
254     }
255
256   for (i = 0; i < n_sizes; i++)
257     {
258       if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
259           cached_sizes[i]->cached_size.natural_size == natural_size)
260         {
261           cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
262           cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
263           return;
264         }
265     }
266
267   /* If not found, pull a new size from the cache, the returned size cache
268    * will immediately be used to cache the new computed size so we go ahead
269    * and increment the last_cached_width/height right away */
270   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
271     {
272       if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
273         {
274           cache->cached_widths++;
275           cache->last_cached_width = cache->cached_widths - 1;
276         }
277       else
278         {
279           if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
280             cache->last_cached_width = 0;
281         }
282
283       if (!cache->widths)
284         cache->widths = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
285
286       if (!cache->widths[cache->last_cached_width])
287         cache->widths[cache->last_cached_width] = g_slice_new (SizeRequest);
288
289       cache->widths[cache->last_cached_width]->lower_for_size = for_size;
290       cache->widths[cache->last_cached_width]->upper_for_size = for_size;
291       cache->widths[cache->last_cached_width]->cached_size.minimum_size = minimum_size;
292       cache->widths[cache->last_cached_width]->cached_size.natural_size = natural_size;
293     }
294   else /* GTK_SIZE_GROUP_VERTICAL */
295     {
296       if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
297         {
298           cache->cached_heights++;
299           cache->last_cached_height = cache->cached_heights - 1;
300         }
301       else
302         {
303           if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
304             cache->last_cached_height = 0;
305         }
306
307       if (!cache->heights)
308         cache->heights = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
309
310       if (!cache->heights[cache->last_cached_height])
311         cache->heights[cache->last_cached_height] = g_slice_new (SizeRequest);
312
313       cache->heights[cache->last_cached_height]->lower_for_size = for_size;
314       cache->heights[cache->last_cached_height]->upper_for_size = for_size;
315       cache->heights[cache->last_cached_height]->cached_size.minimum_size = minimum_size;
316       cache->heights[cache->last_cached_height]->cached_size.natural_size = natural_size;
317     }
318 }
319
320 /* This is the main function that checks for a cached size and
321  * possibly queries the widget class to compute the size if it's
322  * not cached. If the for_size here is -1, then get_preferred_width()
323  * or get_preferred_height() will be used.
324  */
325 static void
326 compute_size_for_orientation (GtkWidget         *widget,
327                               GtkSizeGroupMode   orientation,
328                               gint               for_size,
329                               gint              *minimum_size,
330                               gint              *natural_size)
331 {
332   CachedSize       *cached_size;
333   gboolean          found_in_cache = FALSE;
334   gint              min_size = 0;
335   gint              nat_size = 0;
336
337   found_in_cache = get_cached_size (widget, orientation, for_size, &cached_size);
338
339   if (!found_in_cache)
340     {
341       gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
342
343       gtk_widget_ensure_style (widget);
344
345       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
346         {
347           if (for_size < 0)
348             {
349               push_recursion_check (widget, orientation, for_size);
350               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
351               pop_recursion_check (widget, orientation);
352             }
353           else
354             {
355               gint ignored_position = 0;
356               gint minimum_height;
357               gint natural_height;
358
359               /* Pull the base natural height from the cache as it's needed to adjust
360                * the proposed 'for_size' */
361               gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
362
363               /* convert for_size to unadjusted height (for_size is a proposed allocation) */
364               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
365                                                                      GTK_ORIENTATION_VERTICAL,
366                                                                      &minimum_height,
367                                                                      &natural_height,
368                                                                      &ignored_position,
369                                                                      &adjusted_for_size);
370
371               push_recursion_check (widget, orientation, for_size);
372               GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget, 
373                                                                              MAX (adjusted_for_size, minimum_height),
374                                                                              &min_size, &nat_size);
375               pop_recursion_check (widget, orientation);
376             }
377         }
378       else
379         {
380           if (for_size < 0)
381             {
382               push_recursion_check (widget, orientation, for_size);
383               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
384               pop_recursion_check (widget, orientation);
385             }
386           else
387             {
388               gint ignored_position = 0;
389               gint minimum_width;
390               gint natural_width;
391
392               /* Pull the base natural width from the cache as it's needed to adjust
393                * the proposed 'for_size' */
394               gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
395
396               /* convert for_size to unadjusted width (for_size is a proposed allocation) */
397               GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
398                                                                      GTK_ORIENTATION_HORIZONTAL,
399                                                                      &minimum_width,
400                                                                      &natural_width,
401                                                                      &ignored_position,
402                                                                      &adjusted_for_size);
403
404               push_recursion_check (widget, orientation, for_size);
405               GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, 
406                                                                              MAX (adjusted_for_size, minimum_width),
407                                                                              &min_size, &nat_size);
408               pop_recursion_check (widget, orientation);
409             }
410         }
411
412       if (min_size > nat_size)
413         {
414           g_warning ("%s %p reported min size %d and natural size %d; natural size must be >= min size",
415                      G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size);
416         }
417
418       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
419           _gtk_widget_set_width_request_needed (widget, FALSE);
420       else
421           _gtk_widget_set_height_request_needed (widget, FALSE);
422
423       adjusted_min     = min_size;
424       adjusted_natural = nat_size;
425       GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
426                                                           orientation == GTK_SIZE_GROUP_HORIZONTAL ?
427                                                           GTK_ORIENTATION_HORIZONTAL :
428                                                           GTK_ORIENTATION_VERTICAL,
429                                                           &adjusted_min,
430                                                           &adjusted_natural);
431
432       if (adjusted_min < min_size ||
433           adjusted_natural < nat_size)
434         {
435           g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
436                      G_OBJECT_TYPE_NAME (widget), widget,
437                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
438                      adjusted_min, adjusted_natural,
439                      min_size, nat_size);
440           /* don't use the adjustment */
441         }
442       else if (adjusted_min > adjusted_natural)
443         {
444           g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
445                      G_OBJECT_TYPE_NAME (widget), widget,
446                      orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
447                      adjusted_min, adjusted_natural,
448                      min_size, nat_size);
449           /* don't use the adjustment */
450         }
451       else
452         {
453           /* adjustment looks good */
454           min_size = adjusted_min;
455           nat_size = adjusted_natural;
456         }
457
458       /* Update size-groups with our request and update our cached requests
459        * with the size-group values in a single pass.
460        */
461       _gtk_size_group_bump_requisition (widget,
462                                         orientation,
463                                         &min_size,
464                                         &nat_size);
465
466       commit_cached_size (widget, orientation, for_size, min_size, nat_size);
467     }
468   else
469     {
470       min_size = cached_size->minimum_size;
471       nat_size = cached_size->natural_size;
472     }
473
474   if (minimum_size)
475     *minimum_size = min_size;
476
477   if (natural_size)
478     *natural_size = nat_size;
479
480   g_assert (min_size <= nat_size);
481
482   GTK_NOTE (SIZE_REQUEST,
483             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
484                      widget, G_OBJECT_TYPE_NAME (widget),
485                      orientation == GTK_SIZE_GROUP_HORIZONTAL ?
486                      "width for height" : "height for width" ,
487                      for_size, min_size, nat_size,
488                      found_in_cache ? "yes" : "no"));
489
490 }
491
492 /**
493  * gtk_widget_get_request_mode:
494  * @widget: a #GtkWidget instance
495  *
496  * Gets whether the widget prefers a height-for-width layout
497  * or a width-for-height layout.
498  *
499  * <note><para>#GtkBin widgets generally propagate the preference of
500  * their child, container widgets need to request something either in
501  * context of their children or in context of their allocation
502  * capabilities.</para></note>
503  *
504  * Returns: The #GtkSizeRequestMode preferred by @widget.
505  *
506  * Since: 3.0
507  */
508 GtkSizeRequestMode
509 gtk_widget_get_request_mode (GtkWidget *widget)
510 {
511   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
512
513   return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
514 }
515
516 /**
517  * gtk_widget_get_preferred_width:
518  * @widget: a #GtkWidget instance
519  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
520  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
521  *
522  * Retrieves a widget's initial minimum and natural width.
523  *
524  * <note><para>This call is specific to height-for-width
525  * requests.</para></note>
526  *
527  * The returned request will be modified by the
528  * GtkWidgetClass::adjust_size_request virtual method and by any
529  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
530  * is the one that should be used for layout, not necessarily the one
531  * returned by the widget itself.
532  *
533  * Since: 3.0
534  */
535 void
536 gtk_widget_get_preferred_width (GtkWidget *widget,
537                                 gint      *minimum_width,
538                                 gint      *natural_width)
539 {
540   g_return_if_fail (GTK_IS_WIDGET (widget));
541   g_return_if_fail (minimum_width != NULL || natural_width != NULL);
542
543   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
544                                 -1, minimum_width, natural_width);
545 }
546
547
548 /**
549  * gtk_widget_get_preferred_height:
550  * @widget: a #GtkWidget instance
551  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
552  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
553  *
554  * Retrieves a widget's initial minimum and natural height.
555  *
556  * <note><para>This call is specific to width-for-height requests.</para></note>
557  *
558  * The returned request will be modified by the
559  * GtkWidgetClass::adjust_size_request virtual method and by any
560  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
561  * is the one that should be used for layout, not necessarily the one
562  * returned by the widget itself.
563  *
564  * Since: 3.0
565  */
566 void
567 gtk_widget_get_preferred_height (GtkWidget *widget,
568                                  gint      *minimum_height,
569                                  gint      *natural_height)
570 {
571   g_return_if_fail (GTK_IS_WIDGET (widget));
572   g_return_if_fail (minimum_height != NULL || natural_height != NULL);
573
574   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
575                                 -1, minimum_height, natural_height);
576 }
577
578
579
580 /**
581  * gtk_widget_get_preferred_width_for_height:
582  * @widget: a #GtkWidget instance
583  * @height: the height which is available for allocation
584  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
585  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
586  *
587  * Retrieves a widget's minimum and natural width if it would be given
588  * the specified @height.
589  *
590  * The returned request will be modified by the
591  * GtkWidgetClass::adjust_size_request virtual method and by any
592  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
593  * is the one that should be used for layout, not necessarily the one
594  * returned by the widget itself.
595  *
596  * Since: 3.0
597  */
598 void
599 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
600                                            gint       height,
601                                            gint      *minimum_width,
602                                            gint      *natural_width)
603 {
604   g_return_if_fail (GTK_IS_WIDGET (widget));
605   g_return_if_fail (minimum_width != NULL || natural_width != NULL);
606   g_return_if_fail (height >= 0);
607
608   if (GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
609     compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
610                                   -1, minimum_width, natural_width);
611   else
612     compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
613                                   height, minimum_width, natural_width);
614 }
615
616 /**
617  * gtk_widget_get_preferred_height_for_width:
618  * @widget: a #GtkWidget instance
619  * @width: the width which is available for allocation
620  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
621  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
622  *
623  * Retrieves a widget's minimum and natural height if it would be given
624  * the specified @width.
625  *
626  * The returned request will be modified by the
627  * GtkWidgetClass::adjust_size_request virtual method and by any
628  * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
629  * is the one that should be used for layout, not necessarily the one
630  * returned by the widget itself.
631  *
632  * Since: 3.0
633  */
634 void
635 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
636                                            gint       width,
637                                            gint      *minimum_height,
638                                            gint      *natural_height)
639 {
640   g_return_if_fail (GTK_IS_WIDGET (widget));
641   g_return_if_fail (minimum_height != NULL || natural_height != NULL);
642   g_return_if_fail (width >= 0);
643
644   if (GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
645     compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
646                                   -1, minimum_height, natural_height);
647   else
648     compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
649                                   width, minimum_height, natural_height);
650 }
651
652 /**
653  * gtk_widget_get_preferred_size:
654  * @widget: a #GtkWidget instance
655  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
656  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
657  *
658  * Retrieves the minimum and natural size of a widget, taking
659  * into account the widget's preference for height-for-width management.
660  *
661  * This is used to retrieve a suitable size by container widgets which do
662  * not impose any restrictions on the child placement. It can be used
663  * to deduce toplevel window and menu sizes as well as child widgets in
664  * free-form containers such as GtkLayout.
665  *
666  * <note><para>Handle with care. Note that the natural height of a height-for-width
667  * widget will generally be a smaller size than the minimum height, since the required
668  * height for the natural width is generally smaller than the required height for
669  * the minimum width.</para></note>
670  *
671  * Since: 3.0
672  */
673 void
674 gtk_widget_get_preferred_size (GtkWidget      *widget,
675                                GtkRequisition *minimum_size,
676                                GtkRequisition *natural_size)
677 {
678   gint min_width, nat_width;
679   gint min_height, nat_height;
680
681   g_return_if_fail (GTK_IS_WIDGET (widget));
682
683   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
684     {
685       gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
686
687       if (minimum_size)
688         {
689           minimum_size->width = min_width;
690           gtk_widget_get_preferred_height_for_width (widget, min_width,
691                                                      &minimum_size->height, NULL);
692         }
693
694       if (natural_size)
695         {
696           natural_size->width = nat_width;
697           gtk_widget_get_preferred_height_for_width (widget, nat_width,
698                                                      NULL, &natural_size->height);
699         }
700     }
701   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
702     {
703       gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
704
705       if (minimum_size)
706         {
707           minimum_size->height = min_height;
708           gtk_widget_get_preferred_width_for_height (widget, min_height,
709                                                      &minimum_size->width, NULL);
710         }
711
712       if (natural_size)
713         {
714           natural_size->height = nat_height;
715           gtk_widget_get_preferred_width_for_height (widget, nat_height,
716                                                      NULL, &natural_size->width);
717         }
718     }
719 }
720
721
722 static gint
723 compare_gap (gconstpointer p1,
724              gconstpointer p2,
725              gpointer      data)
726 {
727   GtkRequestedSize *sizes = data;
728   const guint *c1 = p1;
729   const guint *c2 = p2;
730
731   const gint d1 = MAX (sizes[*c1].natural_size -
732                        sizes[*c1].minimum_size,
733                        0);
734   const gint d2 = MAX (sizes[*c2].natural_size -
735                        sizes[*c2].minimum_size,
736                        0);
737
738   gint delta = (d2 - d1);
739
740   if (0 == delta)
741     delta = (*c2 - *c1);
742
743   return delta;
744 }
745
746 /**
747  * gtk_distribute_natural_allocation:
748  * @extra_space: Extra space to redistribute among children after subtracting
749  *               minimum sizes and any child padding from the overall allocation
750  * @n_requested_sizes: Number of requests to fit into the allocation
751  * @sizes: An array of structs with a client pointer and a minimum/natural size
752  *         in the orientation of the allocation.
753  *
754  * Distributes @extra_space to child @sizes by bringing smaller
755  * children up to natural size first.
756  *
757  * The remaining space will be added to the @minimum_size member of the
758  * GtkRequestedSize struct. If all sizes reach their natural size then
759  * the remaining space is returned.
760  *
761  * Returns: The remainder of @extra_space after redistributing space
762  * to @sizes.
763  */
764 gint
765 gtk_distribute_natural_allocation (gint              extra_space,
766                                    guint             n_requested_sizes,
767                                    GtkRequestedSize *sizes)
768 {
769   guint *spreading;
770   gint   i;
771
772   g_return_val_if_fail (extra_space >= 0, 0);
773
774   spreading = g_newa (guint, n_requested_sizes);
775
776   for (i = 0; i < n_requested_sizes; i++)
777     spreading[i] = i;
778
779   /* Distribute the container's extra space c_gap. We want to assign
780    * this space such that the sum of extra space assigned to children
781    * (c^i_gap) is equal to c_cap. The case that there's not enough
782    * space for all children to take their natural size needs some
783    * attention. The goals we want to achieve are:
784    *
785    *   a) Maximize number of children taking their natural size.
786    *   b) The allocated size of children should be a continuous
787    *   function of c_gap.  That is, increasing the container size by
788    *   one pixel should never make drastic changes in the distribution.
789    *   c) If child i takes its natural size and child j doesn't,
790    *   child j should have received at least as much gap as child i.
791    *
792    * The following code distributes the additional space by following
793    * these rules.
794    */
795
796   /* Sort descending by gap and position. */
797   g_qsort_with_data (spreading,
798                      n_requested_sizes, sizeof (guint),
799                      compare_gap, sizes);
800
801   /* Distribute available space.
802    * This master piece of a loop was conceived by Behdad Esfahbod.
803    */
804   for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
805     {
806       /* Divide remaining space by number of remaining children.
807        * Sort order and reducing remaining space by assigned space
808        * ensures that space is distributed equally.
809        */
810       gint glue = (extra_space + i) / (i + 1);
811       gint gap = sizes[(spreading[i])].natural_size
812         - sizes[(spreading[i])].minimum_size;
813
814       gint extra = MIN (glue, gap);
815
816       sizes[spreading[i]].minimum_size += extra;
817
818       extra_space -= extra;
819     }
820
821   return extra_space;
822 }