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 "gtkwidgetprivate.h"
31 #include "deprecated/gtkstyle.h"
34 #ifndef G_DISABLE_CHECKS
35 static GQuark recursion_check_quark = 0;
36 #endif /* G_DISABLE_CHECKS */
39 push_recursion_check (GtkWidget *widget,
40 GtkSizeGroupMode orientation,
43 #ifndef G_DISABLE_CHECKS
44 const char *previous_method;
47 if (recursion_check_quark == 0)
48 recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
50 previous_method = g_object_get_qdata (G_OBJECT (widget), recursion_check_quark);
52 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
54 method = for_size < 0 ? "get_width" : "get_width_for_height";
58 method = for_size < 0 ? "get_height" : "get_height_for_width";
61 if (previous_method != NULL)
63 g_warning ("%s %p: widget tried to gtk_widget_%s inside "
64 " GtkWidget ::%s implementation. "
65 "Should just invoke GTK_WIDGET_GET_CLASS(widget)->%s "
66 "directly rather than using gtk_widget_%s",
67 G_OBJECT_TYPE_NAME (widget), widget,
68 method, previous_method,
72 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, (char*) method);
73 #endif /* G_DISABLE_CHECKS */
77 pop_recursion_check (GtkWidget *widget,
78 GtkSizeGroupMode orientation)
80 #ifndef G_DISABLE_CHECKS
81 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
87 clear_cache (SizeRequestCache *cache,
88 GtkSizeGroupMode orientation)
93 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
95 sizes = cache->widths;
98 cache->cached_widths = 0;
99 cache->last_cached_width = 0;
100 cache->cached_base_width = FALSE;
104 sizes = cache->heights;
106 cache->heights = NULL;
107 cache->cached_heights = 0;
108 cache->last_cached_height = 0;
109 cache->cached_base_height = FALSE;
114 for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES && sizes[i] != NULL; i++)
115 g_slice_free (SizeRequest, sizes[i]);
117 g_slice_free1 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES, sizes);
122 _gtk_widget_free_cached_sizes (GtkWidget *widget)
124 SizeRequestCache *cache;
126 cache = _gtk_widget_peek_request_cache (widget);
128 clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
129 clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);
132 /* This function checks if 'request_needed' flag is present
133 * and resets the cache state if a request is needed for
134 * a given orientation.
136 static SizeRequestCache *
137 init_cache (GtkWidget *widget)
139 SizeRequestCache *cache;
141 cache = _gtk_widget_peek_request_cache (widget);
143 if (_gtk_widget_get_width_request_needed (widget))
144 clear_cache (cache, GTK_SIZE_GROUP_HORIZONTAL);
146 if (_gtk_widget_get_height_request_needed (widget))
147 clear_cache (cache, GTK_SIZE_GROUP_VERTICAL);
152 /* looks for a cached size request for this for_size. If not
153 * found, returns the oldest entry so it can be overwritten
155 * Note that this caching code was originally derived from
156 * the Clutter toolkit but has evolved for other GTK+ requirements.
159 get_cached_size (GtkWidget *widget,
160 GtkSizeGroupMode orientation,
164 SizeRequestCache *cache;
165 SizeRequest **cached_sizes;
168 cache = init_cache (widget);
172 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
174 *result = &cache->cached_width;
175 return cache->cached_base_width;
179 *result = &cache->cached_height;
180 return cache->cached_base_height;
184 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
186 cached_sizes = cache->widths;
187 n_sizes = cache->cached_widths;
191 cached_sizes = cache->heights;
192 n_sizes = cache->cached_heights;
195 /* Search for an already cached size */
196 for (i = 0; i < n_sizes; i++)
198 if (cached_sizes[i]->lower_for_size <= for_size &&
199 cached_sizes[i]->upper_for_size >= for_size)
201 *result = &cached_sizes[i]->cached_size;
210 commit_cached_size (GtkWidget *widget,
211 GtkSizeGroupMode orientation,
216 SizeRequestCache *cache;
217 SizeRequest **cached_sizes;
220 cache = _gtk_widget_peek_request_cache (widget);
222 /* First handle caching of the base requests */
225 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
227 cache->cached_width.minimum_size = minimum_size;
228 cache->cached_width.natural_size = natural_size;
229 cache->cached_base_width = TRUE;
233 cache->cached_height.minimum_size = minimum_size;
234 cache->cached_height.natural_size = natural_size;
235 cache->cached_base_height = TRUE;
240 /* Check if the minimum_size and natural_size is already
241 * in the cache and if this result can be used to extend
244 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
246 cached_sizes = cache->widths;
247 n_sizes = cache->cached_widths;
251 cached_sizes = cache->heights;
252 n_sizes = cache->cached_heights;
255 for (i = 0; i < n_sizes; i++)
257 if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
258 cached_sizes[i]->cached_size.natural_size == natural_size)
260 cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
261 cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
266 /* If not found, pull a new size from the cache, the returned size cache
267 * will immediately be used to cache the new computed size so we go ahead
268 * and increment the last_cached_width/height right away */
269 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
271 if (cache->cached_widths < GTK_SIZE_REQUEST_CACHED_SIZES)
273 cache->cached_widths++;
274 cache->last_cached_width = cache->cached_widths - 1;
278 if (++cache->last_cached_width == GTK_SIZE_REQUEST_CACHED_SIZES)
279 cache->last_cached_width = 0;
283 cache->widths = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
285 if (!cache->widths[cache->last_cached_width])
286 cache->widths[cache->last_cached_width] = g_slice_new (SizeRequest);
288 cache->widths[cache->last_cached_width]->lower_for_size = for_size;
289 cache->widths[cache->last_cached_width]->upper_for_size = for_size;
290 cache->widths[cache->last_cached_width]->cached_size.minimum_size = minimum_size;
291 cache->widths[cache->last_cached_width]->cached_size.natural_size = natural_size;
293 else /* GTK_SIZE_GROUP_VERTICAL */
295 if (cache->cached_heights < GTK_SIZE_REQUEST_CACHED_SIZES)
297 cache->cached_heights++;
298 cache->last_cached_height = cache->cached_heights - 1;
302 if (++cache->last_cached_height == GTK_SIZE_REQUEST_CACHED_SIZES)
303 cache->last_cached_height = 0;
307 cache->heights = g_slice_alloc0 (sizeof (SizeRequest *) * GTK_SIZE_REQUEST_CACHED_SIZES);
309 if (!cache->heights[cache->last_cached_height])
310 cache->heights[cache->last_cached_height] = g_slice_new (SizeRequest);
312 cache->heights[cache->last_cached_height]->lower_for_size = for_size;
313 cache->heights[cache->last_cached_height]->upper_for_size = for_size;
314 cache->heights[cache->last_cached_height]->cached_size.minimum_size = minimum_size;
315 cache->heights[cache->last_cached_height]->cached_size.natural_size = natural_size;
320 get_vfunc_name (GtkSizeGroupMode orientation,
323 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
324 return for_size < 0 ? "get_preferred_width" : "get_preferred_width_for_height";
326 return for_size < 0 ? "get_preferred_height" : "get_preferred_height_for_width";
329 /* This is the main function that checks for a cached size and
330 * possibly queries the widget class to compute the size if it's
331 * not cached. If the for_size here is -1, then get_preferred_width()
332 * or get_preferred_height() will be used.
335 compute_size_for_orientation (GtkWidget *widget,
336 GtkSizeGroupMode orientation,
341 CachedSize *cached_size;
342 gboolean found_in_cache = FALSE;
346 found_in_cache = get_cached_size (widget, orientation, for_size, &cached_size);
350 gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
352 gtk_widget_ensure_style (widget);
354 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
358 push_recursion_check (widget, orientation, for_size);
359 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
360 pop_recursion_check (widget, orientation);
364 gint ignored_position = 0;
368 /* Pull the base natural height from the cache as it's needed to adjust
369 * the proposed 'for_size' */
370 gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
372 /* convert for_size to unadjusted height (for_size is a proposed allocation) */
373 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
374 GTK_ORIENTATION_VERTICAL,
380 push_recursion_check (widget, orientation, for_size);
381 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget,
382 MAX (adjusted_for_size, minimum_height),
383 &min_size, &nat_size);
384 pop_recursion_check (widget, orientation);
391 push_recursion_check (widget, orientation, for_size);
392 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
393 pop_recursion_check (widget, orientation);
397 gint ignored_position = 0;
401 /* Pull the base natural width from the cache as it's needed to adjust
402 * the proposed 'for_size' */
403 gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
405 /* convert for_size to unadjusted width (for_size is a proposed allocation) */
406 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
407 GTK_ORIENTATION_HORIZONTAL,
413 push_recursion_check (widget, orientation, for_size);
414 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget,
415 MAX (adjusted_for_size, minimum_width),
416 &min_size, &nat_size);
417 pop_recursion_check (widget, orientation);
421 if (min_size > nat_size)
423 g_warning ("%s %p reported min size %d and natural size %d in %s(); natural size must be >= min size",
424 G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size, get_vfunc_name (orientation, for_size));
427 if (orientation == GTK_SIZE_GROUP_HORIZONTAL)
428 _gtk_widget_set_width_request_needed (widget, FALSE);
430 _gtk_widget_set_height_request_needed (widget, FALSE);
432 adjusted_min = min_size;
433 adjusted_natural = nat_size;
434 GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
435 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
436 GTK_ORIENTATION_HORIZONTAL :
437 GTK_ORIENTATION_VERTICAL,
441 if (adjusted_min < min_size ||
442 adjusted_natural < nat_size)
444 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
445 G_OBJECT_TYPE_NAME (widget), widget,
446 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
447 adjusted_min, adjusted_natural,
449 /* don't use the adjustment */
451 else if (adjusted_min > adjusted_natural)
453 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
454 G_OBJECT_TYPE_NAME (widget), widget,
455 orientation == GTK_SIZE_GROUP_VERTICAL ? "vertical" : "horizontal",
456 adjusted_min, adjusted_natural,
458 /* don't use the adjustment */
462 /* adjustment looks good */
463 min_size = adjusted_min;
464 nat_size = adjusted_natural;
467 /* Update size-groups with our request and update our cached requests
468 * with the size-group values in a single pass.
470 _gtk_size_group_bump_requisition (widget,
475 commit_cached_size (widget, orientation, for_size, min_size, nat_size);
479 min_size = cached_size->minimum_size;
480 nat_size = cached_size->natural_size;
484 *minimum_size = min_size;
487 *natural_size = nat_size;
489 g_assert (min_size <= nat_size);
491 GTK_NOTE (SIZE_REQUEST,
492 g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
493 widget, G_OBJECT_TYPE_NAME (widget),
494 orientation == GTK_SIZE_GROUP_HORIZONTAL ?
495 "width for height" : "height for width" ,
496 for_size, min_size, nat_size,
497 found_in_cache ? "yes" : "no"));
502 * gtk_widget_get_request_mode:
503 * @widget: a #GtkWidget instance
505 * Gets whether the widget prefers a height-for-width layout
506 * or a width-for-height layout.
508 * <note><para>#GtkBin widgets generally propagate the preference of
509 * their child, container widgets need to request something either in
510 * context of their children or in context of their allocation
511 * capabilities.</para></note>
513 * Returns: The #GtkSizeRequestMode preferred by @widget.
518 gtk_widget_get_request_mode (GtkWidget *widget)
520 g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
522 return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
526 * gtk_widget_get_preferred_width:
527 * @widget: a #GtkWidget instance
528 * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
529 * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
531 * Retrieves a widget's initial minimum and natural width.
533 * <note><para>This call is specific to height-for-width
534 * requests.</para></note>
536 * The returned request will be modified by the
537 * GtkWidgetClass::adjust_size_request virtual method and by any
538 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
539 * is the one that should be used for layout, not necessarily the one
540 * returned by the widget itself.
545 gtk_widget_get_preferred_width (GtkWidget *widget,
549 g_return_if_fail (GTK_IS_WIDGET (widget));
550 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
552 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
553 -1, minimum_width, natural_width);
558 * gtk_widget_get_preferred_height:
559 * @widget: a #GtkWidget instance
560 * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
561 * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
563 * Retrieves a widget's initial minimum and natural height.
565 * <note><para>This call is specific to width-for-height requests.</para></note>
567 * The returned request will be modified by the
568 * GtkWidgetClass::adjust_size_request virtual method and by any
569 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
570 * is the one that should be used for layout, not necessarily the one
571 * returned by the widget itself.
576 gtk_widget_get_preferred_height (GtkWidget *widget,
577 gint *minimum_height,
578 gint *natural_height)
580 g_return_if_fail (GTK_IS_WIDGET (widget));
581 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
583 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
584 -1, minimum_height, natural_height);
590 * gtk_widget_get_preferred_width_for_height:
591 * @widget: a #GtkWidget instance
592 * @height: the height which is available for allocation
593 * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
594 * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
596 * Retrieves a widget's minimum and natural width if it would be given
597 * the specified @height.
599 * The returned request will be modified by the
600 * GtkWidgetClass::adjust_size_request virtual method and by any
601 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
602 * is the one that should be used for layout, not necessarily the one
603 * returned by the widget itself.
608 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
613 g_return_if_fail (GTK_IS_WIDGET (widget));
614 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
615 g_return_if_fail (height >= 0);
617 if (GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
618 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
619 -1, minimum_width, natural_width);
621 compute_size_for_orientation (widget, GTK_SIZE_GROUP_HORIZONTAL,
622 height, minimum_width, natural_width);
626 * gtk_widget_get_preferred_height_for_width:
627 * @widget: a #GtkWidget instance
628 * @width: the width which is available for allocation
629 * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
630 * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
632 * Retrieves a widget's minimum and natural height if it would be given
633 * the specified @width.
635 * The returned request will be modified by the
636 * GtkWidgetClass::adjust_size_request virtual method and by any
637 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
638 * is the one that should be used for layout, not necessarily the one
639 * returned by the widget itself.
644 gtk_widget_get_preferred_height_for_width (GtkWidget *widget,
646 gint *minimum_height,
647 gint *natural_height)
649 g_return_if_fail (GTK_IS_WIDGET (widget));
650 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
651 g_return_if_fail (width >= 0);
653 if (GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
654 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
655 -1, minimum_height, natural_height);
657 compute_size_for_orientation (widget, GTK_SIZE_GROUP_VERTICAL,
658 width, minimum_height, natural_height);
662 * gtk_widget_get_preferred_size:
663 * @widget: a #GtkWidget instance
664 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
665 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
667 * Retrieves the minimum and natural size of a widget, taking
668 * into account the widget's preference for height-for-width management.
670 * This is used to retrieve a suitable size by container widgets which do
671 * not impose any restrictions on the child placement. It can be used
672 * to deduce toplevel window and menu sizes as well as child widgets in
673 * free-form containers such as GtkLayout.
675 * <note><para>Handle with care. Note that the natural height of a height-for-width
676 * widget will generally be a smaller size than the minimum height, since the required
677 * height for the natural width is generally smaller than the required height for
678 * the minimum width.</para></note>
683 gtk_widget_get_preferred_size (GtkWidget *widget,
684 GtkRequisition *minimum_size,
685 GtkRequisition *natural_size)
687 gint min_width, nat_width;
688 gint min_height, nat_height;
690 g_return_if_fail (GTK_IS_WIDGET (widget));
692 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
694 gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
698 minimum_size->width = min_width;
699 gtk_widget_get_preferred_height_for_width (widget, min_width,
700 &minimum_size->height, NULL);
705 natural_size->width = nat_width;
706 gtk_widget_get_preferred_height_for_width (widget, nat_width,
707 NULL, &natural_size->height);
710 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
712 gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
716 minimum_size->height = min_height;
717 gtk_widget_get_preferred_width_for_height (widget, min_height,
718 &minimum_size->width, NULL);
723 natural_size->height = nat_height;
724 gtk_widget_get_preferred_width_for_height (widget, nat_height,
725 NULL, &natural_size->width);
732 compare_gap (gconstpointer p1,
736 GtkRequestedSize *sizes = data;
737 const guint *c1 = p1;
738 const guint *c2 = p2;
740 const gint d1 = MAX (sizes[*c1].natural_size -
741 sizes[*c1].minimum_size,
743 const gint d2 = MAX (sizes[*c2].natural_size -
744 sizes[*c2].minimum_size,
747 gint delta = (d2 - d1);
756 * gtk_distribute_natural_allocation:
757 * @extra_space: Extra space to redistribute among children after subtracting
758 * minimum sizes and any child padding from the overall allocation
759 * @n_requested_sizes: Number of requests to fit into the allocation
760 * @sizes: An array of structs with a client pointer and a minimum/natural size
761 * in the orientation of the allocation.
763 * Distributes @extra_space to child @sizes by bringing smaller
764 * children up to natural size first.
766 * The remaining space will be added to the @minimum_size member of the
767 * GtkRequestedSize struct. If all sizes reach their natural size then
768 * the remaining space is returned.
770 * Returns: The remainder of @extra_space after redistributing space
774 gtk_distribute_natural_allocation (gint extra_space,
775 guint n_requested_sizes,
776 GtkRequestedSize *sizes)
781 g_return_val_if_fail (extra_space >= 0, 0);
783 spreading = g_newa (guint, n_requested_sizes);
785 for (i = 0; i < n_requested_sizes; i++)
788 /* Distribute the container's extra space c_gap. We want to assign
789 * this space such that the sum of extra space assigned to children
790 * (c^i_gap) is equal to c_cap. The case that there's not enough
791 * space for all children to take their natural size needs some
792 * attention. The goals we want to achieve are:
794 * a) Maximize number of children taking their natural size.
795 * b) The allocated size of children should be a continuous
796 * function of c_gap. That is, increasing the container size by
797 * one pixel should never make drastic changes in the distribution.
798 * c) If child i takes its natural size and child j doesn't,
799 * child j should have received at least as much gap as child i.
801 * The following code distributes the additional space by following
805 /* Sort descending by gap and position. */
806 g_qsort_with_data (spreading,
807 n_requested_sizes, sizeof (guint),
810 /* Distribute available space.
811 * This master piece of a loop was conceived by Behdad Esfahbod.
813 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
815 /* Divide remaining space by number of remaining children.
816 * Sort order and reducing remaining space by assigned space
817 * ensures that space is distributed equally.
819 gint glue = (extra_space + i) / (i + 1);
820 gint gap = sizes[(spreading[i])].natural_size
821 - sizes[(spreading[i])].minimum_size;
823 gint extra = MIN (glue, gap);
825 sizes[spreading[i]].minimum_size += extra;
827 extra_space -= extra;