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_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.
91 * Often a widget needs to get its own request during size request or
92 * allocation, for example when computing height it may need to also
93 * compute width, or when deciding how to use an allocation the widget may
94 * need to know its natural size. In these cases, the widget should be
95 * careful to call its virtual methods directly, like this:
97 * <title>Widget calling its own size request method.</title>
99 * GTK_SIZE_REQUEST_GET_IFACE(widget)->get_width(GTK_SIZE_REQUEST(widget), &min, &natural);
103 * It will not work to use the wrapper functions, such as
104 * gtk_size_request_get_width(), inside your own size request
105 * implementation. These return a request adjusted by #GtkSizeGroup
106 * and by the GtkWidgetClass::adjust_size_request virtual method. If a
107 * widget used the wrappers inside its virtual method implementations,
108 * then the adjustments (such as widget margins) would be applied
109 * twice. GTK+ therefore does not allow this and will warn if you try
112 * Of course if you are getting the size request for
113 * <emphasis>another</emphasis> widget, such as a child of a
114 * container, you <emphasis>must</emphasis> use the wrapper APIs;
115 * otherwise, you would not properly consider widget margins,
116 * #GtkSizeGroup, and so forth.
123 #include "gtksizerequest.h"
124 #include "gtksizegroup.h"
125 #include "gtkprivate.h"
128 typedef GtkSizeRequestIface GtkSizeRequestInterface;
129 G_DEFINE_INTERFACE (GtkSizeRequest,
135 gtk_size_request_default_init (GtkSizeRequestInterface *iface)
140 /* looks for a cached size request for this for_size. If not
141 * found, returns the oldest entry so it can be overwritten
143 * Note that this caching code was directly derived from
144 * the Clutter toolkit.
147 get_cached_size (gint for_size,
148 SizeRequest *cached_sizes,
149 SizeRequest **result)
153 *result = &cached_sizes[0];
155 for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES; i++)
159 cs = &cached_sizes[i];
161 if (cs->age > 0 && cs->for_size == for_size)
166 else if (cs->age < (*result)->age)
176 do_size_request (GtkWidget *widget,
177 GtkRequisition *requisition)
179 /* Now we dont bother caching the deprecated "size-request" returns,
180 * just unconditionally invoke here just in case we run into legacy stuff */
181 gtk_widget_ensure_style (widget);
182 g_signal_emit_by_name (widget, "size-request", requisition);
185 #ifndef G_DISABLE_CHECKS
186 static GQuark recursion_check_quark = 0;
187 #endif /* G_DISABLE_CHECKS */
190 push_recursion_check (GtkSizeRequest *request,
191 GtkSizeGroupMode orientation,
194 #ifndef G_DISABLE_CHECKS
195 const char *previous_method;
198 if (recursion_check_quark == 0)
199 recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
201 previous_method = g_object_get_qdata (G_OBJECT (request), recursion_check_quark);
203 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
205 method = for_size < 0 ? "get_width" : "get_width_for_height";
209 method = for_size < 0 ? "get_height" : "get_height_for_width";
212 if (previous_method != NULL)
214 g_warning ("%s %p: widget tried to gtk_size_request_%s inside "
215 " GtkSizeRequest::%s implementation. "
216 "Should just invoke GTK_SIZE_REQUEST_GET_IFACE(widget)->%s "
217 "directly rather than using gtk_size_request_%s",
218 G_OBJECT_TYPE_NAME (request), request,
219 method, previous_method,
223 g_object_set_qdata (G_OBJECT (request), recursion_check_quark, (char*) method);
224 #endif /* G_DISABLE_CHECKS */
228 pop_recursion_check (GtkSizeRequest *request,
229 GtkSizeGroupMode orientation)
231 #ifndef G_DISABLE_CHECKS
232 g_object_set_qdata (G_OBJECT (request), recursion_check_quark, NULL);
237 compute_size_for_orientation (GtkSizeRequest *request,
238 GtkSizeGroupMode orientation,
243 SizeRequestCache *cache;
244 SizeRequest *cached_size;
246 gboolean found_in_cache = FALSE;
247 int adjusted_min, adjusted_natural;
249 g_return_if_fail (GTK_IS_SIZE_REQUEST (request));
250 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
252 widget = GTK_WIDGET (request);
253 cache = _gtk_widget_peek_request_cache (widget);
255 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
257 cached_size = &cache->widths[0];
259 if (!GTK_WIDGET_WIDTH_REQUEST_NEEDED (request))
260 found_in_cache = get_cached_size (for_size, cache->widths, &cached_size);
263 memset (cache->widths, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
264 cache->cached_width_age = 1;
269 cached_size = &cache->heights[0];
271 if (!GTK_WIDGET_HEIGHT_REQUEST_NEEDED (request))
272 found_in_cache = get_cached_size (for_size, cache->heights, &cached_size);
275 memset (cache->heights, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
276 cache->cached_height_age = 1;
282 GtkRequisition requisition = { 0, 0 };
283 gint min_size = 0, nat_size = 0;
284 gint requisition_size;
286 /* Unconditional size request runs but is often unhandled. */
287 do_size_request (widget, &requisition);
289 push_recursion_check (request, orientation, for_size);
290 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
292 requisition_size = requisition.width;
295 GTK_SIZE_REQUEST_GET_IFACE (request)->get_width (request, &min_size, &nat_size);
297 GTK_SIZE_REQUEST_GET_IFACE (request)->get_width_for_height (request, for_size,
298 &min_size, &nat_size);
302 requisition_size = requisition.height;
305 GTK_SIZE_REQUEST_GET_IFACE (request)->get_height (request, &min_size, &nat_size);
307 GTK_SIZE_REQUEST_GET_IFACE (request)->get_height_for_width (request, for_size,
308 &min_size, &nat_size);
310 pop_recursion_check (request, orientation);
312 if (min_size > nat_size)
314 g_warning ("%s %p reported min size %d and natural size %d; natural size must be >= min size",
315 G_OBJECT_TYPE_NAME (request), request, min_size, nat_size);
318 /* Support for dangling "size-request" signal implementations on
321 min_size = MAX (min_size, requisition_size);
322 nat_size = MAX (nat_size, requisition_size);
324 cached_size->minimum_size = min_size;
325 cached_size->natural_size = nat_size;
326 cached_size->for_size = for_size;
328 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
330 cached_size->age = cache->cached_width_age;
331 cache->cached_width_age++;
333 GTK_PRIVATE_UNSET_FLAG (request, GTK_WIDTH_REQUEST_NEEDED);
337 cached_size->age = cache->cached_height_age;
338 cache->cached_height_age++;
340 GTK_PRIVATE_UNSET_FLAG (request, GTK_HEIGHT_REQUEST_NEEDED);
343 adjusted_min = cached_size->minimum_size;
344 adjusted_natural = cached_size->natural_size;
345 GTK_WIDGET_GET_CLASS (request)->adjust_size_request (GTK_WIDGET (request),
346 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
347 GTK_ORIENTATION_HORIZONTAL :
348 GTK_ORIENTATION_VERTICAL,
349 cached_size->for_size,
353 if (adjusted_min < cached_size->minimum_size ||
354 adjusted_natural < cached_size->natural_size)
356 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
357 G_OBJECT_TYPE_NAME (request), request,
358 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
359 adjusted_min, adjusted_natural,
360 cached_size->minimum_size, cached_size->natural_size);
361 /* don't use the adjustment */
363 else if (adjusted_min > adjusted_natural)
365 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
366 G_OBJECT_TYPE_NAME (request), request,
367 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
368 adjusted_min, adjusted_natural,
369 cached_size->minimum_size, cached_size->natural_size);
370 /* don't use the adjustment */
374 /* adjustment looks good */
375 cached_size->minimum_size = adjusted_min;
376 cached_size->natural_size = adjusted_natural;
379 /* Update size-groups with our request and update our cached requests
380 * with the size-group values in a single pass.
382 _gtk_size_group_bump_requisition (GTK_WIDGET (request),
384 &cached_size->minimum_size,
385 &cached_size->natural_size);
389 *minimum_size = cached_size->minimum_size;
392 *natural_size = cached_size->natural_size;
394 g_assert (cached_size->minimum_size <= cached_size->natural_size);
396 GTK_NOTE (SIZE_REQUEST,
397 g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
398 request, G_OBJECT_TYPE_NAME (request),
399 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
400 "width for height" : "height for width" ,
402 cached_size->minimum_size,
403 cached_size->natural_size,
404 found_in_cache ? "yes" : "no"));
409 * gtk_size_request_get_request_mode:
410 * @widget: a #GtkSizeRequest instance
412 * Gets whether the widget prefers a height-for-width layout
413 * or a width-for-height layout.
415 * <note><para>#GtkBin widgets generally propagate the preference of
416 * their child, container widgets need to request something either in
417 * context of their children or in context of their allocation
418 * capabilities.</para></note>
420 * Returns: The #GtkSizeRequestMode preferred by @widget.
425 gtk_size_request_get_request_mode (GtkSizeRequest *widget)
427 GtkSizeRequestIface *iface;
429 g_return_val_if_fail (GTK_IS_SIZE_REQUEST (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
431 iface = GTK_SIZE_REQUEST_GET_IFACE (widget);
432 if (iface->get_request_mode)
433 return iface->get_request_mode (widget);
435 /* By default widgets are height-for-width. */
436 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
440 * gtk_size_request_get_width:
441 * @widget: a #GtkSizeRequest instance
442 * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
443 * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
445 * Retrieves a widget's initial minimum and natural width.
447 * <note><para>This call is specific to height-for-width
448 * requests.</para></note>
450 * The returned request will be modified by the
451 * GtkWidgetClass::adjust_size_request virtual method and by any
452 * #GtkSizeGroup that have been applied. That is, the returned request
453 * is the one that should be used for layout, not necessarily the one
454 * returned by the widget itself.
459 gtk_size_request_get_width (GtkSizeRequest *widget,
463 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
464 -1, minimum_width, natural_width);
469 * gtk_size_request_get_height:
470 * @widget: a #GtkSizeRequest instance
471 * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
472 * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
474 * Retrieves a widget's initial minimum and natural height.
476 * <note><para>This call is specific to width-for-height requests.</para></note>
478 * The returned request will be modified by the
479 * GtkWidgetClass::adjust_size_request virtual method and by any
480 * #GtkSizeGroup that have been applied. That is, the returned request
481 * is the one that should be used for layout, not necessarily the one
482 * returned by the widget itself.
487 gtk_size_request_get_height (GtkSizeRequest *widget,
488 gint *minimum_height,
489 gint *natural_height)
491 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
492 -1, minimum_height, natural_height);
498 * gtk_size_request_get_width_for_height:
499 * @widget: a #GtkSizeRequest instance
500 * @height: the height which is available for allocation
501 * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
502 * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
504 * Retrieves a widget's minimum and natural width if it would be given
505 * the specified @height.
507 * The returned request will be modified by the
508 * GtkWidgetClass::adjust_size_request virtual method and by any
509 * #GtkSizeGroup that have been applied. That is, the returned request
510 * is the one that should be used for layout, not necessarily the one
511 * returned by the widget itself.
516 gtk_size_request_get_width_for_height (GtkSizeRequest *widget,
521 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
522 height, minimum_width, natural_width);
526 * gtk_size_request_get_height_for_width:
527 * @widget: a #GtkSizeRequest instance
528 * @width: the width which is available for allocation
529 * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
530 * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
532 * Retrieves a widget's minimum and natural height if it would be given
533 * the specified @width.
535 * The returned request will be modified by the
536 * GtkWidgetClass::adjust_size_request virtual method and by any
537 * #GtkSizeGroup that have been applied. That is, the returned request
538 * is the one that should be used for layout, not necessarily the one
539 * returned by the widget itself.
544 gtk_size_request_get_height_for_width (GtkSizeRequest *widget,
546 gint *minimum_height,
547 gint *natural_height)
549 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
550 width, minimum_height, natural_height);
554 * gtk_size_request_get_size:
555 * @widget: a #GtkSizeRequest instance
556 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
557 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
559 * Retrieves the minimum and natural size of a widget taking
560 * into account the widget's preference for height-for-width management.
562 * This is used to retrieve a suitable size by container widgets which do
563 * not impose any restrictions on the child placement.
568 gtk_size_request_get_size (GtkSizeRequest *widget,
569 GtkRequisition *minimum_size,
570 GtkRequisition *natural_size)
572 gint min_width, nat_width;
573 gint min_height, nat_height;
575 g_return_if_fail (GTK_IS_SIZE_REQUEST (widget));
577 if (gtk_size_request_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
579 gtk_size_request_get_width (widget, &min_width, &nat_width);
583 minimum_size->width = min_width;
584 gtk_size_request_get_height_for_width (widget, min_width,
585 &minimum_size->height, NULL);
590 natural_size->width = nat_width;
591 gtk_size_request_get_height_for_width (widget, nat_width,
592 NULL, &natural_size->height);
595 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
597 gtk_size_request_get_height (widget, &min_height, &nat_height);
601 minimum_size->height = min_height;
602 gtk_size_request_get_width_for_height (widget, min_height,
603 &minimum_size->width, NULL);
608 natural_size->height = nat_height;
609 gtk_size_request_get_width_for_height (widget, nat_height,
610 NULL, &natural_size->width);
617 compare_gap (gconstpointer p1,
621 GtkRequestedSize *sizes = data;
622 const guint *c1 = p1;
623 const guint *c2 = p2;
625 const gint d1 = MAX (sizes[*c1].natural_size -
626 sizes[*c1].minimum_size,
628 const gint d2 = MAX (sizes[*c2].natural_size -
629 sizes[*c2].minimum_size,
632 gint delta = (d2 - d1);
641 * gtk_distribute_natural_allocation:
642 * @extra_space: Extra space to redistribute among children after subtracting
643 * minimum sizes and any child padding from the overall allocation
644 * @n_requested_sizes: Number of requests to fit into the allocation
645 * @sizes: An array of structs with a client pointer and a minimum/natural size
646 * in the orientation of the allocation.
648 * Distributes @extra_space to child @sizes by bringing up smaller
649 * children up to natural size first.
651 * The remaining space will be added to the @minimum_size member of the
652 * GtkRequestedSize struct. If all sizes reach their natural size then
653 * the remaining space is returned.
655 * Returns: The remainder of @extra_space after redistributing space
659 gtk_distribute_natural_allocation (gint extra_space,
660 guint n_requested_sizes,
661 GtkRequestedSize *sizes)
663 guint *spreading = g_newa (guint, n_requested_sizes);
666 g_return_val_if_fail (extra_space >= 0, 0);
668 for (i = 0; i < n_requested_sizes; i++)
671 /* Distribute the container's extra space c_gap. We want to assign
672 * this space such that the sum of extra space assigned to children
673 * (c^i_gap) is equal to c_cap. The case that there's not enough
674 * space for all children to take their natural size needs some
675 * attention. The goals we want to achieve are:
677 * a) Maximize number of children taking their natural size.
678 * b) The allocated size of children should be a continuous
679 * function of c_gap. That is, increasing the container size by
680 * one pixel should never make drastic changes in the distribution.
681 * c) If child i takes its natural size and child j doesn't,
682 * child j should have received at least as much gap as child i.
684 * The following code distributes the additional space by following
688 /* Sort descending by gap and position. */
689 g_qsort_with_data (spreading,
690 n_requested_sizes, sizeof (guint),
693 /* Distribute available space.
694 * This master piece of a loop was conceived by Behdad Esfahbod.
696 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
698 /* Divide remaining space by number of remaining children.
699 * Sort order and reducing remaining space by assigned space
700 * ensures that space is distributed equally.
702 gint glue = (extra_space + i) / (i + 1);
703 gint gap = sizes[(spreading[i])].natural_size
704 - sizes[(spreading[i])].minimum_size;
706 gint extra = MIN (glue, gap);
708 sizes[spreading[i]].minimum_size += extra;
710 extra_space -= extra;