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, see <http://www.gnu.org/licenses/>.
24 #include "gtksizerequest.h"
28 #include "gtkprivate.h"
29 #include "gtksizegroup-private.h"
30 #include "gtksizerequestcacheprivate.h"
31 #include "gtkwidgetprivate.h"
32 #include "deprecated/gtkstyle.h"
35 #ifndef G_DISABLE_CHECKS
36 static GQuark recursion_check_quark = 0;
37 #endif /* G_DISABLE_CHECKS */
40 push_recursion_check (GtkWidget *widget,
41 GtkSizeGroupMode orientation,
44 #ifndef G_DISABLE_CHECKS
45 const char *previous_method;
48 if (recursion_check_quark == 0)
49 recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
51 previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
53 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
55 method = for_size < 0 ? "get_width" : "get_width_for_height";
59 method = for_size < 0 ? "get_height" : "get_height_for_width";
62 if (previous_method != NULL)
64 g_warning ("%s %p: widget tried to gtk_widget_%s inside "
65 " GtkWidget ::%s implementation. "
66 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
67 "directly rather than using gtk_widget_%s",
68 G_OBJECT_TYPE_NAME (widget), widget,
69 method, previous_method,
73 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
74 #endif /* G_DISABLE_CHECKS */
78 pop_recursion_check (GtkWidget *widget,
79 GtkSizeGroupMode orientation)
81 #ifndef G_DISABLE_CHECKS
82 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
87 /* looks for a cached size request for this for_size. If not
88 * found, returns the oldest entry so it can be overwritten
90 * Note that this caching code was originally derived from
91 * the Clutter toolkit but has evolved for other GTK+ requirements.
94 get_cached_size (GtkWidget *widget,
95 GtkSizeGroupMode orientation,
99 SizeRequestCache *cache;
100 SizeRequest **cached_sizes;
103 cache = _gtk_widget_peek_request_cache (widget);
107 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
109 *result = &cache->cached_width;
110 return cache->cached_base_width;
114 *result = &cache->cached_height;
115 return cache->cached_base_height;
119 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
121 cached_sizes = cache->widths;
122 n_sizes = cache->cached_widths;
126 cached_sizes = cache->heights;
127 n_sizes = cache->cached_heights;
130 /* Search for an already cached size */
131 for (i = 0; i < n_sizes; i++)
133 if (cached_sizes[i]->lower_for_size <= for_size &&
134 cached_sizes[i]->upper_for_size >= for_size)
136 *result = &cached_sizes[i]->cached_size;
145 commit_cached_size (GtkWidget *widget,
146 GtkSizeGroupMode orientation,
151 SizeRequestCache *cache;
152 SizeRequest **cached_sizes;
155 cache = _gtk_widget_peek_request_cache (widget);
157 /* First handle caching of the base requests */
160 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
162 cache->cached_width.minimum_size = minimum_size;
163 cache->cached_width.natural_size = natural_size;
164 cache->cached_base_width = TRUE;
168 cache->cached_height.minimum_size = minimum_size;
169 cache->cached_height.natural_size = natural_size;
170 cache->cached_base_height = TRUE;
175 /* Check if the minimum_size and natural_size is already
176 * in the cache and if this result can be used to extend
179 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
181 cached_sizes = cache->widths;
182 n_sizes = cache->cached_widths;
186 cached_sizes = cache->heights;
187 n_sizes = cache->cached_heights;
190 for (i = 0; i < n_sizes; i++)
192 if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
193 cached_sizes[i]->cached_size.natural_size == natural_size)
195 cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
196 cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
201 /* If not found, pull a new size from the cache, the returned size cache
202 * will immediately be used to cache the new computed size so we go ahead
203 * and increment the last_cached_width/height right away */
204 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
206 if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
208 cache->cached_widths++;
209 cache->last_cached_width = cache->cached_widths - 1;
213 if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
214 cache->last_cached_width = 0;
218 cache->widths = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
220 if (!cache->widths[cache->last_cached_width])
221 cache->widths[cache->last_cached_width] = g_slice_new (SizeRequest);
223 cache->widths[cache->last_cached_width]->lower_for_size = for_size;
224 cache->widths[cache->last_cached_width]->upper_for_size = for_size;
225 cache->widths[cache->last_cached_width]->cached_size.minimum_size = minimum_size;
226 cache->widths[cache->last_cached_width]->cached_size.natural_size = natural_size;
228 else /* GTK_SIZE_GROUP_VERTICAL */
230 if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
232 cache->cached_heights++;
233 cache->last_cached_height = cache->cached_heights - 1;
237 if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
238 cache->last_cached_height = 0;
242 cache->heights = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
244 if (!cache->heights[cache->last_cached_height])
245 cache->heights[cache->last_cached_height] = g_slice_new (SizeRequest);
247 cache->heights[cache->last_cached_height]->lower_for_size = for_size;
248 cache->heights[cache->last_cached_height]->upper_for_size = for_size;
249 cache->heights[cache->last_cached_height]->cached_size.minimum_size = minimum_size;
250 cache->heights[cache->last_cached_height]->cached_size.natural_size = natural_size;
255 get_vfunc_name (GtkSizeGroupMode orientation,
258 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
259 return for_size < 0 ? "get_preferred_width" : "get_preferred_width_for_height";
261 return for_size < 0 ? "get_preferred_height" : "get_preferred_height_for_width";
265 gtk_widget_query_size_for_orientation (GtkWidget *widget,
266 GtkSizeGroupMode orientation,
271 CachedSize *cached_size;
272 gboolean found_in_cache = FALSE;
276 found_in_cache = get_cached_size (widget, orientation, for_size, &cached_size);
280 gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
282 gtk_widget_ensure_style (widget);
284 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
286 if (for_size < 0 || gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
288 push_recursion_check (widget, orientation, for_size);
289 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
290 pop_recursion_check (widget, orientation);
294 gint ignored_position = 0;
298 /* Pull the base natural height from the cache as it's needed to adjust
299 * the proposed 'for_size' */
300 gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
302 /* convert for_size to unadjusted height (for_size is a proposed allocation) */
303 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
304 GTK_ORIENTATION_VERTICAL,
310 push_recursion_check (widget, orientation, for_size);
311 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget,
312 MAX (adjusted_for_size, minimum_height),
313 &min_size, &nat_size);
314 pop_recursion_check (widget, orientation);
319 if (for_size < 0 || gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
321 push_recursion_check (widget, orientation, for_size);
322 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
323 pop_recursion_check (widget, orientation);
327 gint ignored_position = 0;
331 /* Pull the base natural width from the cache as it's needed to adjust
332 * the proposed 'for_size' */
333 gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
335 /* convert for_size to unadjusted width (for_size is a proposed allocation) */
336 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
337 GTK_ORIENTATION_HORIZONTAL,
343 push_recursion_check (widget, orientation, for_size);
344 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget,
345 MAX (adjusted_for_size, minimum_width),
346 &min_size, &nat_size);
347 pop_recursion_check (widget, orientation);
351 if (min_size > nat_size)
353 g_warning ("%s %p reported min size %d and natural size %d in %s(); natural size must be >= min size",
354 G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size, get_vfunc_name (orientation, for_size));
357 adjusted_min = min_size;
358 adjusted_natural = nat_size;
359 GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
360 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
361 GTK_ORIENTATION_HORIZONTAL :
362 GTK_ORIENTATION_VERTICAL,
366 if (adjusted_min < min_size ||
367 adjusted_natural < nat_size)
369 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
370 G_OBJECT_TYPE_NAME (widget), widget,
371 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
372 adjusted_min, adjusted_natural,
374 /* don't use the adjustment */
376 else if (adjusted_min > adjusted_natural)
378 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
379 G_OBJECT_TYPE_NAME (widget), widget,
380 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
381 adjusted_min, adjusted_natural,
383 /* don't use the adjustment */
387 /* adjustment looks good */
388 min_size = adjusted_min;
389 nat_size = adjusted_natural;
392 commit_cached_size (widget, orientation, for_size, min_size, nat_size);
396 min_size = cached_size->minimum_size;
397 nat_size = cached_size->natural_size;
401 *minimum_size = min_size;
404 *natural_size = nat_size;
406 g_assert (min_size <= nat_size);
408 GTK_NOTE (SIZE_REQUEST,
409 g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
410 widget, G_OBJECT_TYPE_NAME (widget),
411 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
412 "width for height" : "height for width" ,
413 for_size, min_size, nat_size,
414 found_in_cache ? "yes" : "no"));
418 /* This is the main function that checks for a cached size and
419 * possibly queries the widget class to compute the size if it's
420 * not cached. If the for_size here is -1, then get_preferred_width()
421 * or get_preferred_height() will be used.
424 _gtk_widget_compute_size_for_orientation (GtkWidget *widget,
425 GtkSizeGroupMode mode,
433 gint min_result = 0, nat_result = 0;
435 if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
437 gtk_widget_query_size_for_orientation (widget, mode, for_size, minimum, natural);
441 widgets = _gtk_size_group_get_widget_peers (widget, mode);
443 g_hash_table_foreach (widgets, (GHFunc) g_object_ref, NULL);
445 g_hash_table_iter_init (&iter, widgets);
446 while (g_hash_table_iter_next (&iter, &key, NULL))
448 GtkWidget *tmp_widget = key;
449 gint min_dimension, nat_dimension;
451 gtk_widget_query_size_for_orientation (tmp_widget, mode, for_size, &min_dimension, &nat_dimension);
453 min_result = MAX (min_result, min_dimension);
454 nat_result = MAX (nat_result, nat_dimension);
457 g_hash_table_foreach (widgets, (GHFunc) g_object_unref, NULL);
459 g_hash_table_destroy (widgets);
462 *minimum = min_result;
465 *natural = nat_result;
469 * gtk_widget_get_request_mode:
470 * @widget: a #GtkWidget instance
472 * Gets whether the widget prefers a height-for-width layout
473 * or a width-for-height layout.
475 * <note><para>#GtkBin widgets generally propagate the preference of
476 * their child, container widgets need to request something either in
477 * context of their children or in context of their allocation
478 * capabilities.</para></note>
480 * Returns: The #GtkSizeRequestMode preferred by @widget.
485 gtk_widget_get_request_mode (GtkWidget *widget)
487 SizeRequestCache *cache;
489 g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
491 cache = _gtk_widget_peek_request_cache (widget);
493 if (!cache->request_mode_valid)
495 cache->request_mode = GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
496 cache->request_mode_valid = TRUE;
499 return cache->request_mode;
503 * gtk_widget_get_preferred_width:
504 * @widget: a #GtkWidget instance
505 * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
506 * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
508 * Retrieves a widget's initial minimum and natural width.
510 * <note><para>This call is specific to height-for-width
511 * requests.</para></note>
513 * The returned request will be modified by the
514 * GtkWidgetClass::adjust_size_request virtual method and by any
515 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
516 * is the one that should be used for layout, not necessarily the one
517 * returned by the widget itself.
522 gtk_widget_get_preferred_width (GtkWidget *widget,
526 g_return_if_fail (GTK_IS_WIDGET (widget));
527 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
529 _gtk_widget_compute_size_for_orientation (widget,
530 GTK_SIZE_GROUP_HORIZONTAL,
538 * gtk_widget_get_preferred_height:
539 * @widget: a #GtkWidget instance
540 * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
541 * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
543 * Retrieves a widget's initial minimum and natural height.
545 * <note><para>This call is specific to width-for-height requests.</para></note>
547 * The returned request will be modified by the
548 * GtkWidgetClass::adjust_size_request virtual method and by any
549 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
550 * is the one that should be used for layout, not necessarily the one
551 * returned by the widget itself.
556 gtk_widget_get_preferred_height (GtkWidget *widget,
557 gint *minimum_height,
558 gint *natural_height)
560 g_return_if_fail (GTK_IS_WIDGET (widget));
561 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
563 _gtk_widget_compute_size_for_orientation (widget,
564 GTK_SIZE_GROUP_VERTICAL,
573 * gtk_widget_get_preferred_width_for_height:
574 * @widget: a #GtkWidget instance
575 * @height: the height which is available for allocation
576 * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
577 * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
579 * Retrieves a widget's minimum and natural width if it would be given
580 * the specified @height.
582 * The returned request will be modified by the
583 * GtkWidgetClass::adjust_size_request virtual method and by any
584 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
585 * is the one that should be used for layout, not necessarily the one
586 * returned by the widget itself.
591 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
596 g_return_if_fail (GTK_IS_WIDGET (widget));
597 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
598 g_return_if_fail (height >= 0);
600 _gtk_widget_compute_size_for_orientation (widget,
601 GTK_SIZE_GROUP_HORIZONTAL,
608 * gtk_widget_get_preferred_height_for_width:
609 * @widget: a #GtkWidget instance
610 * @width: the width which is available for allocation
611 * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
612 * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
614 * Retrieves a widget's minimum and natural height if it would be given
615 * the specified @width.
617 * The returned request will be modified by the
618 * GtkWidgetClass::adjust_size_request virtual method and by any
619 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
620 * is the one that should be used for layout, not necessarily the one
621 * returned by the widget itself.
626 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
628 gint *minimum_height,
629 gint *natural_height)
631 g_return_if_fail (GTK_IS_WIDGET (widget));
632 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
633 g_return_if_fail (width >= 0);
635 _gtk_widget_compute_size_for_orientation (widget,
636 GTK_SIZE_GROUP_VERTICAL,
643 * gtk_widget_get_preferred_size:
644 * @widget: a #GtkWidget instance
645 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
646 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
648 * Retrieves the minimum and natural size of a widget, taking
649 * into account the widget's preference for height-for-width management.
651 * This is used to retrieve a suitable size by container widgets which do
652 * not impose any restrictions on the child placement. It can be used
653 * to deduce toplevel window and menu sizes as well as child widgets in
654 * free-form containers such as GtkLayout.
656 * <note><para>Handle with care. Note that the natural height of a height-for-width
657 * widget will generally be a smaller size than the minimum height, since the required
658 * height for the natural width is generally smaller than the required height for
659 * the minimum width.</para></note>
664 gtk_widget_get_preferred_size (GtkWidget *widget,
665 GtkRequisition *minimum_size,
666 GtkRequisition *natural_size)
668 gint min_width, nat_width;
669 gint min_height, nat_height;
671 g_return_if_fail (GTK_IS_WIDGET (widget));
673 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
675 gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
679 minimum_size->width = min_width;
680 gtk_widget_get_preferred_height_for_width (widget, min_width,
681 &minimum_size->height, NULL);
686 natural_size->width = nat_width;
687 gtk_widget_get_preferred_height_for_width (widget, nat_width,
688 NULL, &natural_size->height);
691 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
693 gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
697 minimum_size->height = min_height;
698 gtk_widget_get_preferred_width_for_height (widget, min_height,
699 &minimum_size->width, NULL);
704 natural_size->height = nat_height;
705 gtk_widget_get_preferred_width_for_height (widget, nat_height,
706 NULL, &natural_size->width);
713 compare_gap (gconstpointer p1,
717 GtkRequestedSize *sizes = data;
718 const guint *c1 = p1;
719 const guint *c2 = p2;
721 const gint d1 = MAX (sizes[*c1].natural_size -
722 sizes[*c1].minimum_size,
724 const gint d2 = MAX (sizes[*c2].natural_size -
725 sizes[*c2].minimum_size,
728 gint delta = (d2 - d1);
737 * gtk_distribute_natural_allocation:
738 * @extra_space: Extra space to redistribute among children after subtracting
739 * minimum sizes and any child padding from the overall allocation
740 * @n_requested_sizes: Number of requests to fit into the allocation
741 * @sizes: An array of structs with a client pointer and a minimum/natural size
742 * in the orientation of the allocation.
744 * Distributes @extra_space to child @sizes by bringing smaller
745 * children up to natural size first.
747 * The remaining space will be added to the @minimum_size member of the
748 * GtkRequestedSize struct. If all sizes reach their natural size then
749 * the remaining space is returned.
751 * Returns: The remainder of @extra_space after redistributing space
755 gtk_distribute_natural_allocation (gint extra_space,
756 guint n_requested_sizes,
757 GtkRequestedSize *sizes)
762 g_return_val_if_fail (extra_space >= 0, 0);
764 spreading = g_newa (guint, n_requested_sizes);
766 for (i = 0; i < n_requested_sizes; i++)
769 /* Distribute the container's extra space c_gap. We want to assign
770 * this space such that the sum of extra space assigned to children
771 * (c^i_gap) is equal to c_cap. The case that there's not enough
772 * space for all children to take their natural size needs some
773 * attention. The goals we want to achieve are:
775 * a) Maximize number of children taking their natural size.
776 * b) The allocated size of children should be a continuous
777 * function of c_gap. That is, increasing the container size by
778 * one pixel should never make drastic changes in the distribution.
779 * c) If child i takes its natural size and child j doesn't,
780 * child j should have received at least as much gap as child i.
782 * The following code distributes the additional space by following
786 /* Sort descending by gap and position. */
787 g_qsort_with_data (spreading,
788 n_requested_sizes, sizeof (guint),
791 /* Distribute available space.
792 * This master piece of a loop was conceived by Behdad Esfahbod.
794 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
796 /* Divide remaining space by number of remaining children.
797 * Sort order and reducing remaining space by assigned space
798 * ensures that space is distributed equally.
800 gint glue = (extra_space + i) / (i + 1);
801 gint gap = sizes[(spreading[i])].natural_size
802 - sizes[(spreading[i])].minimum_size;
804 gint extra = MIN (glue, gap);
806 sizes[spreading[i]].minimum_size += extra;
808 extra_space -= extra;