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