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