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 #include "gtksizerequest.h"
30 #include "gtkprivate.h"
31 #include "gtksizegroup-private.h"
32 #include "gtkwidgetprivate.h"
34 /* looks for a cached size request for this for_size. If not
35 * found, returns the oldest entry so it can be overwritten
37 * Note that this caching code was directly derived from
38 * the Clutter toolkit.
41 get_cached_size (SizeRequestCache *cache,
42 GtkSizeGroupMode orientation,
47 SizeRequest *cached_sizes;
49 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
51 cached_sizes = cache->widths;
52 n_sizes = cache->cached_widths;
56 cached_sizes = cache->heights;
57 n_sizes = cache->cached_widths;
60 /* Search for an already cached size */
61 for (i = 0; i < n_sizes; i++)
63 if (cached_sizes[i].for_size == for_size)
65 *result = &cached_sizes[i];
70 /* If not found, pull a new size from the cache, the returned size cache
71 * will immediately be used to cache the new computed size so we go ahead
72 * and increment the last_cached_width/height right away */
73 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
75 if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
77 cache->cached_widths++;
78 cache->last_cached_width = cache->cached_widths - 1;
82 if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
83 cache->last_cached_width = 0;
86 *result = &cache->widths[cache->last_cached_width];
88 else /* GTK_SIZE_GROUP_VERTICAL */
90 if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
92 cache->cached_heights++;
93 cache->last_cached_height = cache->cached_heights - 1;
97 if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
98 cache->last_cached_height = 0;
101 *result = &cache->heights[cache->last_cached_height];
108 #ifndef G_DISABLE_CHECKS
109 static GQuark recursion_check_quark = 0;
110 #endif /* G_DISABLE_CHECKS */
113 push_recursion_check (GtkWidget *widget,
114 GtkSizeGroupMode orientation,
117 #ifndef G_DISABLE_CHECKS
118 const char *previous_method;
121 if (recursion_check_quark == 0)
122 recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
124 previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
126 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
128 method = for_size < 0 ? "get_width" : "get_width_for_height";
132 method = for_size < 0 ? "get_height" : "get_height_for_width";
135 if (previous_method != NULL)
137 g_warning ("%s %p: widget tried to gtk_widget_%s inside "
138 " GtkWidget ::%s implementation. "
139 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
140 "directly rather than using gtk_widget_%s",
141 G_OBJECT_TYPE_NAME (widget), widget,
142 method, previous_method,
146 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
147 #endif /* G_DISABLE_CHECKS */
151 pop_recursion_check (GtkWidget *widget,
152 GtkSizeGroupMode orientation)
154 #ifndef G_DISABLE_CHECKS
155 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
160 compute_size_for_orientation (GtkWidget *widget,
161 GtkSizeGroupMode orientation,
166 SizeRequestCache *cache;
167 SizeRequest *cached_size;
168 gboolean found_in_cache = FALSE;
169 int adjusted_min, adjusted_natural;
171 g_return_if_fail (GTK_IS_WIDGET (widget));
172 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
174 cache = _gtk_widget_peek_request_cache (widget);
176 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
178 cached_size = &cache->widths[0];
180 if (!_gtk_widget_get_width_request_needed (widget))
181 found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
184 memset (cache->widths, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
185 cache->cached_widths = 1;
186 cache->last_cached_width = 0;
191 cached_size = &cache->heights[0];
193 if (!_gtk_widget_get_height_request_needed (widget))
194 found_in_cache = get_cached_size (cache, orientation, for_size, &cached_size);
197 memset (cache->heights, 0, GTK_SIZE_REQUEST_CACHED_SIZES * sizeof (SizeRequest));
198 cache->cached_heights = 1;
199 cache->last_cached_height = 0;
208 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
212 push_recursion_check (widget, orientation, for_size);
213 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
214 pop_recursion_check (widget, orientation);
218 gint ignored_position = 0;
222 /* Pull the base natural height from the cache as it's needed to adjust
223 * the proposed 'for_size' */
224 gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
226 /* convert for_size to unadjusted height (for_size is a proposed allocation) */
227 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
228 GTK_ORIENTATION_VERTICAL,
234 push_recursion_check (widget, orientation, for_size);
235 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget,
236 MAX (for_size, minimum_height),
237 &min_size, &nat_size);
238 pop_recursion_check (widget, orientation);
245 push_recursion_check (widget, orientation, for_size);
246 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
247 pop_recursion_check (widget, orientation);
251 int ignored_position = 0;
255 /* Pull the base natural width from the cache as it's needed to adjust
256 * the proposed 'for_size' */
257 gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
259 /* convert for_size to unadjusted width (for_size is a proposed allocation) */
260 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
261 GTK_ORIENTATION_HORIZONTAL,
267 push_recursion_check (widget, orientation, for_size);
268 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget,
269 MAX (for_size, minimum_width),
270 &min_size, &nat_size);
271 pop_recursion_check (widget, orientation);
275 if (min_size > nat_size)
277 g_warning ("%s %p reported min size %d and natural size %d; natural size must be >= min size",
278 G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size);
281 cached_size->minimum_size = min_size;
282 cached_size->natural_size = nat_size;
283 cached_size->for_size = for_size;
285 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
286 _gtk_widget_set_width_request_needed (widget, FALSE);
288 _gtk_widget_set_height_request_needed (widget, FALSE);
290 adjusted_min = cached_size->minimum_size;
291 adjusted_natural = cached_size->natural_size;
292 GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
293 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
294 GTK_ORIENTATION_HORIZONTAL :
295 GTK_ORIENTATION_VERTICAL,
299 if (adjusted_min < cached_size->minimum_size ||
300 adjusted_natural < cached_size->natural_size)
302 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
303 G_OBJECT_TYPE_NAME (widget), widget,
304 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
305 adjusted_min, adjusted_natural,
306 cached_size->minimum_size, cached_size->natural_size);
307 /* don't use the adjustment */
309 else if (adjusted_min > adjusted_natural)
311 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
312 G_OBJECT_TYPE_NAME (widget), widget,
313 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
314 adjusted_min, adjusted_natural,
315 cached_size->minimum_size, cached_size->natural_size);
316 /* don't use the adjustment */
320 /* adjustment looks good */
321 cached_size->minimum_size = adjusted_min;
322 cached_size->natural_size = adjusted_natural;
325 /* Update size-groups with our request and update our cached requests
326 * with the size-group values in a single pass.
328 _gtk_size_group_bump_requisition (widget,
330 &cached_size->minimum_size,
331 &cached_size->natural_size);
335 *minimum_size = cached_size->minimum_size;
338 *natural_size = cached_size->natural_size;
340 g_assert (cached_size->minimum_size <= cached_size->natural_size);
342 GTK_NOTE (SIZE_REQUEST,
343 g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
344 widget, G_OBJECT_TYPE_NAME (widget),
345 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
346 "width for height" : "height for width" ,
348 cached_size->minimum_size,
349 cached_size->natural_size,
350 found_in_cache ? "yes" : "no"));
355 * gtk_widget_get_request_mode:
356 * @widget: a #GtkWidget instance
358 * Gets whether the widget prefers a height-for-width layout
359 * or a width-for-height layout.
361 * <note><para>#GtkBin widgets generally propagate the preference of
362 * their child, container widgets need to request something either in
363 * context of their children or in context of their allocation
364 * capabilities.</para></note>
366 * Returns: The #GtkSizeRequestMode preferred by @widget.
371 gtk_widget_get_request_mode (GtkWidget *widget)
373 GtkWidgetClass *klass;
375 g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
377 klass = GTK_WIDGET_GET_CLASS (widget);
378 if (klass->get_request_mode)
379 return klass->get_request_mode (widget);
381 /* By default widgets are height-for-width. */
382 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
386 * gtk_widget_get_preferred_width:
387 * @widget: a #GtkWidget 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>
396 * The returned request will be modified by the
397 * GtkWidgetClass::adjust_size_request virtual method and by any
398 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
399 * is the one that should be used for layout, not necessarily the one
400 * returned by the widget itself.
405 gtk_widget_get_preferred_width (GtkWidget *widget,
409 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
410 -1, minimum_width, natural_width);
415 * gtk_widget_get_preferred_height:
416 * @widget: a #GtkWidget instance
417 * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
418 * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
420 * Retrieves a widget's initial minimum and natural height.
422 * <note><para>This call is specific to width-for-height requests.</para></note>
424 * The returned request will be modified by the
425 * GtkWidgetClass::adjust_size_request virtual method and by any
426 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
427 * is the one that should be used for layout, not necessarily the one
428 * returned by the widget itself.
433 gtk_widget_get_preferred_height (GtkWidget *widget,
434 gint *minimum_height,
435 gint *natural_height)
437 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
438 -1, minimum_height, natural_height);
444 * gtk_widget_get_preferred_width_for_height:
445 * @widget: a #GtkWidget instance
446 * @height: the height which is available for allocation
447 * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
448 * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
450 * Retrieves a widget's minimum and natural width if it would be given
451 * the specified @height.
453 * The returned request will be modified by the
454 * GtkWidgetClass::adjust_size_request virtual method and by any
455 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
456 * is the one that should be used for layout, not necessarily the one
457 * returned by the widget itself.
462 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
467 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
468 height, minimum_width, natural_width);
472 * gtk_widget_get_preferred_height_for_width:
473 * @widget: a #GtkWidget instance
474 * @width: the width which is available for allocation
475 * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
476 * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
478 * Retrieves a widget's minimum and natural height if it would be given
479 * the specified @width.
481 * The returned request will be modified by the
482 * GtkWidgetClass::adjust_size_request virtual method and by any
483 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
484 * is the one that should be used for layout, not necessarily the one
485 * returned by the widget itself.
490 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
492 gint *minimum_height,
493 gint *natural_height)
495 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
496 width, minimum_height, natural_height);
500 * gtk_widget_get_preferred_size:
501 * @widget: a #GtkWidget instance
502 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
503 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
505 * Retrieves the minimum and natural size of a widget, taking
506 * into account the widget's preference for height-for-width management.
508 * This is used to retrieve a suitable size by container widgets which do
509 * not impose any restrictions on the child placement. It can be used
510 * to deduce toplevel window and menu sizes as well as child widgets in
511 * free-form containers such as GtkLayout.
513 * <note><para>Handle with care. Note that the natural height of a height-for-width
514 * widget will generally be a smaller size than the minimum height, since the required
515 * height for the natural width is generally smaller than the required height for
516 * the minimum width.</para></note>
521 gtk_widget_get_preferred_size (GtkWidget *widget,
522 GtkRequisition *minimum_size,
523 GtkRequisition *natural_size)
525 gint min_width, nat_width;
526 gint min_height, nat_height;
528 g_return_if_fail (GTK_IS_WIDGET (widget));
530 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
532 gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
536 minimum_size->width = min_width;
537 gtk_widget_get_preferred_height_for_width (widget, min_width,
538 &minimum_size->height, NULL);
543 natural_size->width = nat_width;
544 gtk_widget_get_preferred_height_for_width (widget, nat_width,
545 NULL, &natural_size->height);
548 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
550 gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
554 minimum_size->height = min_height;
555 gtk_widget_get_preferred_width_for_height (widget, min_height,
556 &minimum_size->width, NULL);
561 natural_size->height = nat_height;
562 gtk_widget_get_preferred_width_for_height (widget, nat_height,
563 NULL, &natural_size->width);
570 compare_gap (gconstpointer p1,
574 GtkRequestedSize *sizes = data;
575 const guint *c1 = p1;
576 const guint *c2 = p2;
578 const gint d1 = MAX (sizes[*c1].natural_size -
579 sizes[*c1].minimum_size,
581 const gint d2 = MAX (sizes[*c2].natural_size -
582 sizes[*c2].minimum_size,
585 gint delta = (d2 - d1);
594 * gtk_distribute_natural_allocation:
595 * @extra_space: Extra space to redistribute among children after subtracting
596 * minimum sizes and any child padding from the overall allocation
597 * @n_requested_sizes: Number of requests to fit into the allocation
598 * @sizes: An array of structs with a client pointer and a minimum/natural size
599 * in the orientation of the allocation.
601 * Distributes @extra_space to child @sizes by bringing smaller
602 * children up to natural size first.
604 * The remaining space will be added to the @minimum_size member of the
605 * GtkRequestedSize struct. If all sizes reach their natural size then
606 * the remaining space is returned.
608 * Returns: The remainder of @extra_space after redistributing space
612 gtk_distribute_natural_allocation (gint extra_space,
613 guint n_requested_sizes,
614 GtkRequestedSize *sizes)
619 g_return_val_if_fail (extra_space >= 0, 0);
621 spreading = g_newa (guint, n_requested_sizes);
623 for (i = 0; i < n_requested_sizes; i++)
626 /* Distribute the container's extra space c_gap. We want to assign
627 * this space such that the sum of extra space assigned to children
628 * (c^i_gap) is equal to c_cap. The case that there's not enough
629 * space for all children to take their natural size needs some
630 * attention. The goals we want to achieve are:
632 * a) Maximize number of children taking their natural size.
633 * b) The allocated size of children should be a continuous
634 * function of c_gap. That is, increasing the container size by
635 * one pixel should never make drastic changes in the distribution.
636 * c) If child i takes its natural size and child j doesn't,
637 * child j should have received at least as much gap as child i.
639 * The following code distributes the additional space by following
643 /* Sort descending by gap and position. */
644 g_qsort_with_data (spreading,
645 n_requested_sizes, sizeof (guint),
648 /* Distribute available space.
649 * This master piece of a loop was conceived by Behdad Esfahbod.
651 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
653 /* Divide remaining space by number of remaining children.
654 * Sort order and reducing remaining space by assigned space
655 * ensures that space is distributed equally.
657 gint glue = (extra_space + i) / (i + 1);
658 gint gap = sizes[(spreading[i])].natural_size
659 - sizes[(spreading[i])].minimum_size;
661 gint extra = MIN (glue, gap);
663 sizes[spreading[i]].minimum_size += extra;
665 extra_space -= extra;