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