2 * Copyright (C) 2007-2010 Openismus GmbH
5 * Mathias Hasselmann <mathias@openismus.com>
6 * Tristan Van Berkom <tristan.van.berkom@gmail.com>
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.
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.
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.
26 * SECTION:gtksizerequest
27 * @Short_Description: Height-for-width geometry management
28 * @Title: GtkSizeRequest
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.
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.
43 * <title>Implementing GtkSizeRequest</title>
45 * Some important things to keep in mind when implementing
46 * the GtkSizeRequest interface and when using it in container
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.
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.
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.
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.
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().
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
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.
96 #include "gtksizerequest.h"
97 #include "gtksizegroup.h"
98 #include "gtkprivate.h"
101 /* With GtkSizeRequest, a widget may be requested
102 * its width for 2 or maximum 3 heights in one resize
104 #define N_CACHED_SIZES 3
115 SizeRequest widths[N_CACHED_SIZES];
116 SizeRequest heights[N_CACHED_SIZES];
117 guint8 cached_width_age;
118 guint8 cached_height_age;
121 static GQuark quark_cache = 0;
125 gtk_size_request_get_type (void)
127 static GType size_request_type = 0;
129 if (G_UNLIKELY(!size_request_type))
132 g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkSizeRequest"),
133 sizeof (GtkSizeRequestIface),
136 g_type_interface_add_prerequisite (size_request_type, GTK_TYPE_WIDGET);
138 quark_cache = g_quark_from_static_string ("gtk-size-request-cache");
141 return size_request_type;
144 /* looks for a cached size request for this for_size. If not
145 * found, returns the oldest entry so it can be overwritten
147 * Note that this caching code was directly derived from
148 * the Clutter toolkit.
151 get_cached_size (gint for_size,
152 SizeRequest *cached_sizes,
153 SizeRequest **result)
157 *result = &cached_sizes[0];
159 for (i = 0; i < N_CACHED_SIZES; i++)
163 cs = &cached_sizes[i];
165 if (cs->age > 0 && cs->for_size == for_size)
170 else if (cs->age < (*result)->age)
180 destroy_cache (SizeRequestCache *cache)
182 g_slice_free (SizeRequestCache, cache);
185 static SizeRequestCache *
186 get_cache (GtkSizeRequest *widget,
189 SizeRequestCache *cache;
191 cache = g_object_get_qdata (G_OBJECT (widget), quark_cache);
192 if (!cache && create)
194 cache = g_slice_new0 (SizeRequestCache);
196 cache->cached_width_age = 1;
197 cache->cached_height_age = 1;
199 g_object_set_qdata_full (G_OBJECT (widget), quark_cache, cache,
200 (GDestroyNotify)destroy_cache);
208 do_size_request (GtkWidget *widget)
210 if (GTK_WIDGET_REQUEST_NEEDED (widget))
212 gtk_widget_ensure_style (widget);
213 GTK_PRIVATE_UNSET_FLAG (widget, GTK_REQUEST_NEEDED);
214 g_signal_emit_by_name (widget,
216 &widget->requisition);
221 compute_size_for_orientation (GtkSizeRequest *request,
222 GtkSizeGroupMode orientation,
227 SizeRequestCache *cache;
228 SizeRequest *cached_size;
230 gboolean found_in_cache = FALSE;
232 g_return_if_fail (GTK_IS_SIZE_REQUEST (request));
233 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
235 widget = GTK_WIDGET (request);
236 cache = get_cache (request, TRUE);
238 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
240 cached_size = &cache->widths[0];
242 if (!GTK_WIDGET_WIDTH_REQUEST_NEEDED (request))
243 found_in_cache = get_cached_size (for_size, cache->widths, &cached_size);
246 memset (cache->widths, 0, N_CACHED_SIZES * sizeof (SizeRequest));
247 cache->cached_width_age = 1;
252 cached_size = &cache->heights[0];
254 if (!GTK_WIDGET_HEIGHT_REQUEST_NEEDED (request))
255 found_in_cache = get_cached_size (for_size, cache->heights, &cached_size);
258 memset (cache->heights, 0, N_CACHED_SIZES * sizeof (SizeRequest));
259 cache->cached_height_age = 1;
265 gint min_size = 0, nat_size = 0;
266 gint group_size, requisition_size;
268 /* Unconditional size request runs but is often unhandled. */
269 do_size_request (widget);
271 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
273 requisition_size = widget->requisition.width;
276 GTK_SIZE_REQUEST_GET_IFACE (request)->get_width (request, &min_size, &nat_size);
278 GTK_SIZE_REQUEST_GET_IFACE (request)->get_width_for_height (request, for_size,
279 &min_size, &nat_size);
283 requisition_size = widget->requisition.height;
286 GTK_SIZE_REQUEST_GET_IFACE (request)->get_height (request, &min_size, &nat_size);
288 GTK_SIZE_REQUEST_GET_IFACE (request)->get_height_for_width (request, for_size,
289 &min_size, &nat_size);
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.
296 min_size = MAX (min_size, requisition_size);
297 nat_size = MAX (nat_size, requisition_size);
299 cached_size->minimum_size = min_size;
300 cached_size->natural_size = nat_size;
301 cached_size->for_size = for_size;
303 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
305 cached_size->age = cache->cached_width_age;
306 cache->cached_width_age++;
308 GTK_PRIVATE_UNSET_FLAG (request, GTK_WIDTH_REQUEST_NEEDED);
312 cached_size->age = cache->cached_height_age;
313 cache->cached_height_age++;
315 GTK_PRIVATE_UNSET_FLAG (request, GTK_HEIGHT_REQUEST_NEEDED);
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.
322 * Note this is also where values from gtk_widget_set_size_request()
326 _gtk_size_group_bump_requisition (GTK_WIDGET (request),
327 orientation, cached_size->minimum_size);
329 cached_size->minimum_size = MAX (cached_size->minimum_size, group_size);
330 cached_size->natural_size = MAX (cached_size->natural_size, group_size);
334 *minimum_size = cached_size->minimum_size;
337 *natural_size = cached_size->natural_size;
339 g_assert (cached_size->minimum_size <= cached_size->natural_size);
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" ,
347 cached_size->minimum_size,
348 cached_size->natural_size,
349 found_in_cache ? "yes" : "no"));
354 * gtk_size_request_is_height_for_width:
355 * @widget: a #GtkSizeRequest instance
357 * Gets whether the widget prefers a height-for-width layout
358 * or a width-for-height layout.
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>
365 * Returns: %TRUE if the widget prefers height-for-width, %FALSE if
366 * the widget should be treated with a width-for-height preference.
371 gtk_size_request_get_request_mode (GtkSizeRequest *widget)
373 GtkSizeRequestIface *iface;
375 g_return_val_if_fail (GTK_IS_SIZE_REQUEST (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
377 iface = GTK_SIZE_REQUEST_GET_IFACE (widget);
378 if (iface->get_request_mode)
379 return iface->get_request_mode (widget);
381 /* By default widgets are height-for-width. */
382 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
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
391 * Retrieves a widget's initial minimum and natural width.
393 * <note><para>This call is specific to height-for-width
394 * requests.</para></note>
399 gtk_size_request_get_width (GtkSizeRequest *widget,
403 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
404 -1, minimum_width, natural_width);
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
414 * Retrieves a widget's initial minimum and natural height.
416 * <note><para>This call is specific to width-for-height requests.</para></note>
421 gtk_size_request_get_height (GtkSizeRequest *widget,
422 gint *minimum_height,
423 gint *natural_height)
425 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
426 -1, minimum_height, natural_height);
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
438 * Retrieves a widget's desired width if it would be given
439 * the specified @height.
444 gtk_size_request_get_width_for_height (GtkSizeRequest *widget,
449 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
450 height, minimum_width, natural_width);
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
460 * Retrieves a widget's desired height if it would be given
461 * the specified @width.
466 gtk_size_request_get_height_for_width (GtkSizeRequest *widget,
468 gint *minimum_height,
469 gint *natural_height)
471 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
472 width, minimum_height, natural_height);
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
481 * Retrieves the minimum and natural size of a widget taking
482 * into account the widget's preference for height-for-width management.
484 * This is used to retrieve a suitable size by container widgets which do
485 * not impose any restrictions on the child placement.
490 gtk_size_request_get_size (GtkSizeRequest *widget,
491 GtkRequisition *minimum_size,
492 GtkRequisition *natural_size)
494 gint min_width, nat_width;
495 gint min_height, nat_height;
497 g_return_if_fail (GTK_IS_SIZE_REQUEST (widget));
499 if (gtk_size_request_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
501 gtk_size_request_get_width (widget, &min_width, &nat_width);
505 minimum_size->width = min_width;
506 gtk_size_request_get_height_for_width (widget, min_width,
507 &minimum_size->height, NULL);
512 natural_size->width = nat_width;
513 gtk_size_request_get_height_for_width (widget, nat_width,
514 NULL, &natural_size->height);
519 gtk_size_request_get_height (widget, &min_height, &nat_height);
523 minimum_size->height = min_height;
524 gtk_size_request_get_width_for_height (widget, min_height,
525 &minimum_size->width, NULL);
530 natural_size->height = nat_height;
531 gtk_size_request_get_width_for_height (widget, nat_height,
532 NULL, &natural_size->width);