]> Pileus Git - ~andy/gtk/blob - gtk/gtksizerequest.c
Merge branch 'windows_list'
[~andy/gtk] / gtk / gtksizerequest.c
1 /* gtksizerequest.c
2  * Copyright (C) 2007-2010 Openismus GmbH
3  *
4  * Authors:
5  *      Mathias Hasselmann <mathias@openismus.com>
6  *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24
25 /**
26  * SECTION:gtksizerequest
27  * @Short_Description: Height-for-width geometry management
28  * @Title: GtkSizeRequest
29  *
30  * The GtkSizeRequest interface is GTK+'s height-for-width (and width-for-height)
31  * geometry management system. Height-for-width means that a widget can
32  * change how much vertical space it needs, depending on the amount
33  * of horizontal space that it is given (and similar for width-for-height).
34  * The most common example is a label that reflows to fill up the available
35  * width, wraps to fewer lines, and therefore needs less height.
36  *
37  * GTK+'s traditional two-pass <link linkend="size-allocation">size-allocation</link>
38  * algorithm does not allow this flexibility. #GtkWidget provides a default
39  * implementation of the #GtkSizeRequest interface for existing widgets,
40  * which always requests the same height, regardless of the available width.
41  *
42  * <refsect2>
43  * <title>Implementing GtkSizeRequest</title>
44  * <para>
45  * Some important things to keep in mind when implementing
46  * the GtkSizeRequest interface and when using it in container
47  * implementations.
48  *
49  * The geometry management system will query a logical hierarchy in
50  * only one orientation at a time. When widgets are initially queried
51  * for their minimum sizes it is generally done in a dual pass
52  * in the direction chosen by the toplevel.
53  *
54  * For instance when queried in the normal height-for-width mode:
55  * First the default minimum and natural width for each widget
56  * in the interface will computed and collectively returned to
57  * the toplevel by way of gtk_size_request_get_desired_width().
58  * Next, the toplevel will use the minimum width to query for the
59  * minimum height contextual to that width using
60  * gtk_size_request_get_height_for_width(), which will also be a
61  * highly recursive operation. This minimum-for-minimum size can be
62  * used to set the minimum size constraint on the toplevel.
63  *
64  * When allocating, each container can use the minimum and natural
65  * sizes reported by their children to allocate natural sizes and
66  * expose as much content as possible with the given allocation.
67  *
68  * That means that the request operation at allocation time will
69  * usually fire again in contexts of different allocated sizes than
70  * the ones originally queried for. #GtkSizeRequest caches a
71  * small number of results to avoid re-querying for the same
72  * allocated size in one allocation cycle.
73  *
74  * A widget that does not actually do height-for-width
75  * or width-for-height size negotiations only has to implement
76  * get_width() and get_height().
77  *
78  * If a widget does move content around to smartly use up the
79  * allocated size, then it must support the request properly in
80  * both orientations; even if the request only makes sense in
81  * one orientation.
82  *
83  * For instance, a GtkLabel that does height-for-width word wrapping
84  * will not expect to have get_height() called because that
85  * call is specific to a width-for-height request, in this case the
86  * label must return the heights contextual to its minimum possible
87  * width. By following this rule any widget that handles height-for-width
88  * or width-for-height requests will always be allocated at least
89  * enough space to fit its own content.
90  * </para>
91  * </refsect2>
92  */
93
94
95 #include <config.h>
96 #include "gtksizerequest.h"
97 #include "gtksizegroup.h"
98 #include "gtkprivate.h"
99 #include "gtkintl.h"
100
101 /* With GtkSizeRequest, a widget may be requested
102  * its width for 2 or maximum 3 heights in one resize
103  */
104 #define N_CACHED_SIZES 3
105
106 typedef struct
107 {
108   guint  age;
109   gint   for_size;
110   gint   minimum_size;
111   gint   natural_size;
112 } SizeRequest;
113
114 typedef struct {
115   SizeRequest widths[N_CACHED_SIZES];
116   SizeRequest heights[N_CACHED_SIZES];
117   guint8      cached_width_age;
118   guint8      cached_height_age;
119 } SizeRequestCache;
120
121 static GQuark quark_cache = 0;
122
123
124 GType
125 gtk_size_request_get_type (void)
126 {
127   static GType size_request_type = 0;
128
129   if (G_UNLIKELY(!size_request_type))
130     {
131       size_request_type =
132         g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkSizeRequest"),
133                                        sizeof (GtkSizeRequestIface),
134                                        NULL, 0, NULL, 0);
135
136       g_type_interface_add_prerequisite (size_request_type, GTK_TYPE_WIDGET);
137
138       quark_cache = g_quark_from_static_string ("gtk-size-request-cache");
139     }
140
141   return size_request_type;
142 }
143
144 /* looks for a cached size request for this for_size. If not
145  * found, returns the oldest entry so it can be overwritten
146  *
147  * Note that this caching code was directly derived from
148  * the Clutter toolkit.
149  */
150 static gboolean
151 get_cached_size (gint           for_size,
152                  SizeRequest   *cached_sizes,
153                  SizeRequest  **result)
154 {
155   guint i;
156
157   *result = &cached_sizes[0];
158
159   for (i = 0; i < N_CACHED_SIZES; i++)
160     {
161       SizeRequest *cs;
162
163       cs = &cached_sizes[i];
164
165       if (cs->age > 0 && cs->for_size == for_size)
166         {
167           *result = cs;
168           return TRUE;
169         }
170       else if (cs->age < (*result)->age)
171         {
172           *result = cs;
173         }
174     }
175
176   return FALSE;
177 }
178
179 static void
180 destroy_cache (SizeRequestCache *cache)
181 {
182   g_slice_free (SizeRequestCache, cache);
183 }
184
185 static SizeRequestCache *
186 get_cache (GtkSizeRequest *widget,
187            gboolean        create)
188 {
189   SizeRequestCache *cache;
190
191   cache = g_object_get_qdata (G_OBJECT (widget), quark_cache);
192   if (!cache && create)
193     {
194       cache = g_slice_new0 (SizeRequestCache);
195
196       cache->cached_width_age  = 1;
197       cache->cached_height_age = 1;
198
199       g_object_set_qdata_full (G_OBJECT (widget), quark_cache, cache,
200                                (GDestroyNotify)destroy_cache);
201     }
202
203   return cache;
204 }
205
206
207 static void
208 do_size_request (GtkWidget *widget)
209 {
210   if (GTK_WIDGET_REQUEST_NEEDED (widget))
211     {
212       gtk_widget_ensure_style (widget);
213       GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
214       g_signal_emit_by_name (widget,
215                              "size-request",
216                              &widget->requisition);
217     }
218 }
219
220 static void
221 compute_size_for_orientation (GtkSizeRequest    *request,
222                               GtkSizeGroupMode   orientation,
223                               gint               for_size,
224                               gint              *minimum_size,
225                               gint              *natural_size)
226 {
227   SizeRequestCache *cache;
228   SizeRequest      *cached_size;
229   GtkWidget        *widget;
230   gboolean          found_in_cache = FALSE;
231
232   g_return_if_fail (GTK_IS_SIZE_REQUEST (request));
233   g_return_if_fail (minimum_size != NULL || natural_size != NULL);
234
235   widget = GTK_WIDGET (request);
236   cache  = get_cache (request, TRUE);
237
238   if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
239     {
240       cached_size = &cache->widths[0];
241
242       if (!GTK_WIDGET_WIDTH_REQUEST_NEEDED (request))
243         found_in_cache = get_cached_size (for_size, cache->widths, &cached_size);
244       else
245         {
246           memset (cache->widths, 0, N_CACHED_SIZES * sizeof (SizeRequest));
247           cache->cached_width_age = 1;
248         }
249     }
250   else
251     {
252       cached_size = &cache->heights[0];
253
254       if (!GTK_WIDGET_HEIGHT_REQUEST_NEEDED (request))
255         found_in_cache = get_cached_size (for_size, cache->heights, &cached_size);
256       else
257         {
258           memset (cache->heights, 0, N_CACHED_SIZES * sizeof (SizeRequest));
259           cache->cached_height_age = 1;
260         }
261     }
262
263   if (!found_in_cache)
264     {
265       gint min_size = 0, nat_size = 0;
266       gint group_size, requisition_size;
267
268       /* Unconditional size request runs but is often unhandled. */
269       do_size_request (widget);
270
271       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
272         {
273           requisition_size = widget->requisition.width;
274
275           if (for_size < 0)
276             GTK_SIZE_REQUEST_GET_IFACE (request)->get_width (request, &min_size, &nat_size);
277           else
278             GTK_SIZE_REQUEST_GET_IFACE (request)->get_width_for_height (request, for_size, 
279                                                                         &min_size, &nat_size);
280         }
281       else
282         {
283           requisition_size = widget->requisition.height;
284
285           if (for_size < 0)
286             GTK_SIZE_REQUEST_GET_IFACE (request)->get_height (request, &min_size, &nat_size);
287           else
288             GTK_SIZE_REQUEST_GET_IFACE (request)->get_height_for_width (request, for_size, 
289                                                                         &min_size, &nat_size);
290         }
291
292       /* Support for dangling "size-request" signals and forward derived
293        * classes that will not default to a ->get_width() that
294        * returns the values in the ->requisition cache.
295        */
296       min_size = MAX (min_size, requisition_size);
297       nat_size = MAX (nat_size, requisition_size);
298
299       cached_size->minimum_size = min_size;
300       cached_size->natural_size = nat_size;
301       cached_size->for_size     = for_size;
302
303       if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
304         {
305           cached_size->age = cache->cached_width_age;
306           cache->cached_width_age++;
307
308           GTK_PRIVATE_UNSET_FLAG (request, GTK_WIDTH_REQUEST_NEEDED);
309         }
310       else
311         {
312           cached_size->age = cache->cached_height_age;
313           cache->cached_height_age++;
314
315           GTK_PRIVATE_UNSET_FLAG (request, GTK_HEIGHT_REQUEST_NEEDED);
316         }
317
318       /* Get size groups to compute the base requisition once one
319        * of the values have been cached, then go ahead and update
320        * the cache with the sizegroup computed value.
321        *
322        * Note this is also where values from gtk_widget_set_size_request()
323        * are considered.
324        */
325       group_size =
326         _gtk_size_group_bump_requisition (GTK_WIDGET (request),
327                                           orientation, cached_size->minimum_size);
328
329       cached_size->minimum_size = MAX (cached_size->minimum_size, group_size);
330       cached_size->natural_size = MAX (cached_size->natural_size, group_size);
331     }
332
333   if (minimum_size)
334     *minimum_size = cached_size->minimum_size;
335
336   if (natural_size)
337     *natural_size = cached_size->natural_size;
338
339   g_assert (cached_size->minimum_size <= cached_size->natural_size);
340
341   GTK_NOTE (SIZE_REQUEST,
342             g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
343                      request, G_OBJECT_TYPE_NAME (request),
344                      orientation == GTK_SIZE_GROUP_HORIZONTAL ?
345                      "width for height" : "height for width" ,
346                      for_size,
347                      cached_size->minimum_size,
348                      cached_size->natural_size,
349                      found_in_cache ? "yes" : "no"));
350
351 }
352
353 /**
354  * gtk_size_request_is_height_for_width:
355  * @widget: a #GtkSizeRequest instance
356  *
357  * Gets whether the widget prefers a height-for-width layout
358  * or a width-for-height layout.
359  *
360  * <note><para>#GtkBin widgets generally propagate the preference of
361  * their child, container widgets need to request something either in
362  * context of their children or in context of their allocation
363  * capabilities.</para></note>
364  *
365  * Returns: %TRUE if the widget prefers height-for-width, %FALSE if
366  * the widget should be treated with a width-for-height preference.
367  *
368  * Since: 3.0
369  */
370 GtkSizeRequestMode
371 gtk_size_request_get_request_mode (GtkSizeRequest *widget)
372 {
373   GtkSizeRequestIface *iface;
374
375   g_return_val_if_fail (GTK_IS_SIZE_REQUEST (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
376
377   iface = GTK_SIZE_REQUEST_GET_IFACE (widget);
378   if (iface->get_request_mode)
379     return iface->get_request_mode (widget);
380
381   /* By default widgets are height-for-width. */
382   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
383 }
384
385 /**
386  * gtk_size_request_get_width:
387  * @widget: a #GtkSizeRequest instance
388  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
389  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
390  *
391  * Retrieves a widget's initial minimum and natural width.
392  *
393  * <note><para>This call is specific to height-for-width
394  * requests.</para></note>
395  *
396  * Since: 3.0
397  */
398 void
399 gtk_size_request_get_width (GtkSizeRequest *widget,
400                                        gint              *minimum_width,
401                                        gint              *natural_width)
402 {
403   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
404                                 -1, minimum_width, natural_width);
405 }
406
407
408 /**
409  * gtk_size_request_get_height:
410  * @widget: a #GtkSizeRequest instance
411  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
412  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
413  *
414  * Retrieves a widget's initial minimum and natural height.
415  *
416  * <note><para>This call is specific to width-for-height requests.</para></note>
417  *
418  * Since: 3.0
419  */
420 void
421 gtk_size_request_get_height (GtkSizeRequest *widget,
422                              gint           *minimum_height,
423                              gint           *natural_height)
424 {
425   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
426                                 -1, minimum_height, natural_height);
427 }
428
429
430
431 /**
432  * gtk_size_request_get_width_for_height:
433  * @widget: a #GtkSizeRequest instance
434  * @height: the height which is available for allocation
435  * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
436  * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
437  *
438  * Retrieves a widget's desired width if it would be given
439  * the specified @height.
440  *
441  * Since: 3.0
442  */
443 void
444 gtk_size_request_get_width_for_height (GtkSizeRequest *widget,
445                                        gint            height,
446                                        gint           *minimum_width,
447                                        gint           *natural_width)
448 {
449   compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
450                                 height, minimum_width, natural_width);
451 }
452
453 /**
454  * gtk_size_request_get_height_for_width:
455  * @widget: a #GtkSizeRequest instance
456  * @width: the width which is available for allocation
457  * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
458  * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
459  *
460  * Retrieves a widget's desired height if it would be given
461  * the specified @width.
462  *
463  * Since: 3.0
464  */
465 void
466 gtk_size_request_get_height_for_width (GtkSizeRequest *widget,
467                                        gint            width,
468                                        gint           *minimum_height,
469                                        gint           *natural_height)
470 {
471   compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
472                                 width, minimum_height, natural_height);
473 }
474
475 /**
476  * gtk_size_request_get_size:
477  * @widget: a #GtkSizeRequest instance
478  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
479  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
480  *
481  * Retrieves the minimum and natural size of a widget taking
482  * into account the widget's preference for height-for-width management.
483  *
484  * This is used to retrieve a suitable size by container widgets which do
485  * not impose any restrictions on the child placement.
486  *
487  * Since: 3.0
488  */
489 void
490 gtk_size_request_get_size (GtkSizeRequest    *widget,
491                            GtkRequisition    *minimum_size,
492                            GtkRequisition    *natural_size)
493 {
494   gint min_width, nat_width;
495   gint min_height, nat_height;
496
497   g_return_if_fail (GTK_IS_SIZE_REQUEST (widget));
498
499   if (gtk_size_request_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
500     {
501       gtk_size_request_get_width (widget, &min_width, &nat_width);
502
503       if (minimum_size)
504         {
505           minimum_size->width = min_width;
506           gtk_size_request_get_height_for_width (widget, min_width,
507                                                  &minimum_size->height, NULL);
508         }
509
510       if (natural_size)
511         {
512           natural_size->width = nat_width;
513           gtk_size_request_get_height_for_width (widget, nat_width,
514                                                  NULL, &natural_size->height);
515         }
516     }
517   else
518     {
519       gtk_size_request_get_height (widget, &min_height, &nat_height);
520
521       if (minimum_size)
522         {
523           minimum_size->height = min_height;
524           gtk_size_request_get_width_for_height (widget, min_height,
525                                                  &minimum_size->width, NULL);
526         }
527
528       if (natural_size)
529         {
530           natural_size->height = nat_height;
531           gtk_size_request_get_width_for_height (widget, nat_height,
532                                                  NULL, &natural_size->width);
533         }
534     }
535 }