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 GtkOrientation 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_ORIENTATION_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 GtkOrientation orientation)
81 #ifndef G_DISABLE_CHECKS
82 g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
87 get_vfunc_name (GtkOrientation orientation,
90 if (orientation == GTK_ORIENTATION_HORIZONTAL)
91 return for_size < 0 ? "get_preferred_width" : "get_preferred_width_for_height";
93 return for_size < 0 ? "get_preferred_height" : "get_preferred_height_for_width";
97 gtk_widget_query_size_for_orientation (GtkWidget *widget,
98 GtkOrientation orientation,
103 SizeRequestCache *cache;
106 gboolean found_in_cache;
108 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_CONSTANT_SIZE)
111 cache = _gtk_widget_peek_request_cache (widget);
112 found_in_cache = _gtk_size_request_cache_lookup (cache,
120 gint adjusted_min, adjusted_natural, adjusted_for_size = for_size;
122 gtk_widget_ensure_style (widget);
124 if (orientation == GTK_ORIENTATION_HORIZONTAL)
128 push_recursion_check (widget, orientation, for_size);
129 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_size, &nat_size);
130 pop_recursion_check (widget, orientation);
134 gint ignored_position = 0;
138 /* Pull the base natural height from the cache as it's needed to adjust
139 * the proposed 'for_size' */
140 gtk_widget_get_preferred_height (widget, &minimum_height, &natural_height);
142 /* convert for_size to unadjusted height (for_size is a proposed allocation) */
143 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
144 GTK_ORIENTATION_VERTICAL,
150 push_recursion_check (widget, orientation, for_size);
151 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget,
152 MAX (adjusted_for_size, minimum_height),
153 &min_size, &nat_size);
154 pop_recursion_check (widget, orientation);
161 push_recursion_check (widget, orientation, for_size);
162 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_size, &nat_size);
163 pop_recursion_check (widget, orientation);
167 gint ignored_position = 0;
171 /* Pull the base natural width from the cache as it's needed to adjust
172 * the proposed 'for_size' */
173 gtk_widget_get_preferred_width (widget, &minimum_width, &natural_width);
175 /* convert for_size to unadjusted width (for_size is a proposed allocation) */
176 GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
177 GTK_ORIENTATION_HORIZONTAL,
183 push_recursion_check (widget, orientation, for_size);
184 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget,
185 MAX (adjusted_for_size, minimum_width),
186 &min_size, &nat_size);
187 pop_recursion_check (widget, orientation);
191 if (min_size > nat_size)
193 g_warning ("%s %p reported min size %d and natural size %d in %s(); natural size must be >= min size",
194 G_OBJECT_TYPE_NAME (widget), widget, min_size, nat_size, get_vfunc_name (orientation, for_size));
197 adjusted_min = min_size;
198 adjusted_natural = nat_size;
199 GTK_WIDGET_GET_CLASS (widget)->adjust_size_request (widget,
204 if (adjusted_min < min_size ||
205 adjusted_natural < nat_size)
207 g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
208 G_OBJECT_TYPE_NAME (widget), widget,
209 orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
210 adjusted_min, adjusted_natural,
212 /* don't use the adjustment */
214 else if (adjusted_min > adjusted_natural)
216 g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
217 G_OBJECT_TYPE_NAME (widget), widget,
218 orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
219 adjusted_min, adjusted_natural,
221 /* don't use the adjustment */
225 /* adjustment looks good */
226 min_size = adjusted_min;
227 nat_size = adjusted_natural;
230 _gtk_size_request_cache_commit (cache,
238 *minimum_size = min_size;
241 *natural_size = nat_size;
243 g_assert (min_size <= nat_size);
245 GTK_NOTE (SIZE_REQUEST,
246 g_print ("[%p] %s\t%s: %d is minimum %d and natural: %d (hit cache: %s)\n",
247 widget, G_OBJECT_TYPE_NAME (widget),
248 orientation == GTK_ORIENTATION_HORIZONTAL ?
249 "width for height" : "height for width" ,
250 for_size, min_size, nat_size,
251 found_in_cache ? "yes" : "no"));
254 /* This is the main function that checks for a cached size and
255 * possibly queries the widget class to compute the size if it's
256 * not cached. If the for_size here is -1, then get_preferred_width()
257 * or get_preferred_height() will be used.
260 _gtk_widget_compute_size_for_orientation (GtkWidget *widget,
261 GtkOrientation orientation,
269 gint min_result = 0, nat_result = 0;
271 if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
273 gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural);
277 widgets = _gtk_size_group_get_widget_peers (widget, orientation);
279 g_hash_table_foreach (widgets, (GHFunc) g_object_ref, NULL);
281 g_hash_table_iter_init (&iter, widgets);
282 while (g_hash_table_iter_next (&iter, &key, NULL))
284 GtkWidget *tmp_widget = key;
285 gint min_dimension, nat_dimension;
287 gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size, &min_dimension, &nat_dimension);
289 min_result = MAX (min_result, min_dimension);
290 nat_result = MAX (nat_result, nat_dimension);
293 g_hash_table_foreach (widgets, (GHFunc) g_object_unref, NULL);
295 g_hash_table_destroy (widgets);
298 *minimum = min_result;
301 *natural = nat_result;
305 * gtk_widget_get_request_mode:
306 * @widget: a #GtkWidget instance
308 * Gets whether the widget prefers a height-for-width layout
309 * or a width-for-height layout.
311 * <note><para>#GtkBin widgets generally propagate the preference of
312 * their child, container widgets need to request something either in
313 * context of their children or in context of their allocation
314 * capabilities.</para></note>
316 * Returns: The #GtkSizeRequestMode preferred by @widget.
321 gtk_widget_get_request_mode (GtkWidget *widget)
323 SizeRequestCache *cache;
325 g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
327 cache = _gtk_widget_peek_request_cache (widget);
329 if (!cache->request_mode_valid)
331 cache->request_mode = GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
332 cache->request_mode_valid = TRUE;
335 return cache->request_mode;
339 * gtk_widget_get_preferred_width:
340 * @widget: a #GtkWidget instance
341 * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
342 * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
344 * Retrieves a widget's initial minimum and natural width.
346 * <note><para>This call is specific to height-for-width
347 * requests.</para></note>
349 * The returned request will be modified by the
350 * GtkWidgetClass::adjust_size_request virtual method and by any
351 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
352 * is the one that should be used for layout, not necessarily the one
353 * returned by the widget itself.
358 gtk_widget_get_preferred_width (GtkWidget *widget,
362 g_return_if_fail (GTK_IS_WIDGET (widget));
363 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
365 _gtk_widget_compute_size_for_orientation (widget,
366 GTK_ORIENTATION_HORIZONTAL,
374 * gtk_widget_get_preferred_height:
375 * @widget: a #GtkWidget instance
376 * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
377 * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
379 * Retrieves a widget's initial minimum and natural height.
381 * <note><para>This call is specific to width-for-height requests.</para></note>
383 * The returned request will be modified by the
384 * GtkWidgetClass::adjust_size_request virtual method and by any
385 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
386 * is the one that should be used for layout, not necessarily the one
387 * returned by the widget itself.
392 gtk_widget_get_preferred_height (GtkWidget *widget,
393 gint *minimum_height,
394 gint *natural_height)
396 g_return_if_fail (GTK_IS_WIDGET (widget));
397 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
399 _gtk_widget_compute_size_for_orientation (widget,
400 GTK_ORIENTATION_VERTICAL,
409 * gtk_widget_get_preferred_width_for_height:
410 * @widget: a #GtkWidget instance
411 * @height: the height which is available for allocation
412 * @minimum_width: (out) (allow-none): location for storing the minimum width, or %NULL
413 * @natural_width: (out) (allow-none): location for storing the natural width, or %NULL
415 * Retrieves a widget's minimum and natural width if it would be given
416 * the specified @height.
418 * The returned request will be modified by the
419 * GtkWidgetClass::adjust_size_request virtual method and by any
420 * #GtkSizeGroup<!-- -->s that have been applied. That is, the returned request
421 * is the one that should be used for layout, not necessarily the one
422 * returned by the widget itself.
427 gtk_widget_get_preferred_width_for_height (GtkWidget *widget,
432 g_return_if_fail (GTK_IS_WIDGET (widget));
433 g_return_if_fail (minimum_width != NULL || natural_width != NULL);
434 g_return_if_fail (height >= 0);
436 _gtk_widget_compute_size_for_orientation (widget,
437 GTK_ORIENTATION_HORIZONTAL,
444 * gtk_widget_get_preferred_height_for_width:
445 * @widget: a #GtkWidget instance
446 * @width: the width which is available for allocation
447 * @minimum_height: (out) (allow-none): location for storing the minimum height, or %NULL
448 * @natural_height: (out) (allow-none): location for storing the natural height, or %NULL
450 * Retrieves a widget's minimum and natural height if it would be given
451 * the specified @width.
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_height_for_width (GtkWidget *widget,
464 gint *minimum_height,
465 gint *natural_height)
467 g_return_if_fail (GTK_IS_WIDGET (widget));
468 g_return_if_fail (minimum_height != NULL || natural_height != NULL);
469 g_return_if_fail (width >= 0);
471 _gtk_widget_compute_size_for_orientation (widget,
472 GTK_ORIENTATION_VERTICAL,
479 * gtk_widget_get_preferred_size:
480 * @widget: a #GtkWidget instance
481 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
482 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
484 * Retrieves the minimum and natural size of a widget, taking
485 * into account the widget's preference for height-for-width management.
487 * This is used to retrieve a suitable size by container widgets which do
488 * not impose any restrictions on the child placement. It can be used
489 * to deduce toplevel window and menu sizes as well as child widgets in
490 * free-form containers such as GtkLayout.
492 * <note><para>Handle with care. Note that the natural height of a height-for-width
493 * widget will generally be a smaller size than the minimum height, since the required
494 * height for the natural width is generally smaller than the required height for
495 * the minimum width.</para></note>
500 gtk_widget_get_preferred_size (GtkWidget *widget,
501 GtkRequisition *minimum_size,
502 GtkRequisition *natural_size)
504 gint min_width, nat_width;
505 gint min_height, nat_height;
507 g_return_if_fail (GTK_IS_WIDGET (widget));
509 if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
511 gtk_widget_get_preferred_width (widget, &min_width, &nat_width);
515 minimum_size->width = min_width;
516 gtk_widget_get_preferred_height_for_width (widget, min_width,
517 &minimum_size->height, NULL);
522 natural_size->width = nat_width;
523 gtk_widget_get_preferred_height_for_width (widget, nat_width,
524 NULL, &natural_size->height);
527 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
529 gtk_widget_get_preferred_height (widget, &min_height, &nat_height);
533 minimum_size->height = min_height;
534 gtk_widget_get_preferred_width_for_height (widget, min_height,
535 &minimum_size->width, NULL);
540 natural_size->height = nat_height;
541 gtk_widget_get_preferred_width_for_height (widget, nat_height,
542 NULL, &natural_size->width);
549 compare_gap (gconstpointer p1,
553 GtkRequestedSize *sizes = data;
554 const guint *c1 = p1;
555 const guint *c2 = p2;
557 const gint d1 = MAX (sizes[*c1].natural_size -
558 sizes[*c1].minimum_size,
560 const gint d2 = MAX (sizes[*c2].natural_size -
561 sizes[*c2].minimum_size,
564 gint delta = (d2 - d1);
573 * gtk_distribute_natural_allocation:
574 * @extra_space: Extra space to redistribute among children after subtracting
575 * minimum sizes and any child padding from the overall allocation
576 * @n_requested_sizes: Number of requests to fit into the allocation
577 * @sizes: An array of structs with a client pointer and a minimum/natural size
578 * in the orientation of the allocation.
580 * Distributes @extra_space to child @sizes by bringing smaller
581 * children up to natural size first.
583 * The remaining space will be added to the @minimum_size member of the
584 * GtkRequestedSize struct. If all sizes reach their natural size then
585 * the remaining space is returned.
587 * Returns: The remainder of @extra_space after redistributing space
591 gtk_distribute_natural_allocation (gint extra_space,
592 guint n_requested_sizes,
593 GtkRequestedSize *sizes)
598 g_return_val_if_fail (extra_space >= 0, 0);
600 spreading = g_newa (guint, n_requested_sizes);
602 for (i = 0; i < n_requested_sizes; i++)
605 /* Distribute the container's extra space c_gap. We want to assign
606 * this space such that the sum of extra space assigned to children
607 * (c^i_gap) is equal to c_cap. The case that there's not enough
608 * space for all children to take their natural size needs some
609 * attention. The goals we want to achieve are:
611 * a) Maximize number of children taking their natural size.
612 * b) The allocated size of children should be a continuous
613 * function of c_gap. That is, increasing the container size by
614 * one pixel should never make drastic changes in the distribution.
615 * c) If child i takes its natural size and child j doesn't,
616 * child j should have received at least as much gap as child i.
618 * The following code distributes the additional space by following
622 /* Sort descending by gap and position. */
623 g_qsort_with_data (spreading,
624 n_requested_sizes, sizeof (guint),
627 /* Distribute available space.
628 * This master piece of a loop was conceived by Behdad Esfahbod.
630 for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
632 /* Divide remaining space by number of remaining children.
633 * Sort order and reducing remaining space by assigned space
634 * ensures that space is distributed equally.
636 gint glue = (extra_space + i) / (i + 1);
637 gint gap = sizes[(spreading[i])].natural_size
638 - sizes[(spreading[i])].minimum_size;
640 gint extra = MIN (glue, gap);
642 sizes[spreading[i]].minimum_size += extra;
644 extra_space -= extra;