2010-06-18 04:11:26 +00:00
|
|
|
|
/* gtksizerequest.c
|
2010-04-20 22:53:54 +00:00
|
|
|
|
* Copyright (C) 2007-2010 Openismus GmbH
|
2009-12-02 08:48:42 +00:00
|
|
|
|
*
|
2010-04-20 22:53:54 +00:00
|
|
|
|
* Authors:
|
2009-12-02 08:48:42 +00:00
|
|
|
|
* Mathias Hasselmann <mathias@openismus.com>
|
2010-04-20 22:53:54 +00:00
|
|
|
|
* Tristan Van Berkom <tristan.van.berkom@gmail.com>
|
2009-12-02 08:48:42 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2009-12-02 08:48:42 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2010-10-19 17:14:46 +00:00
|
|
|
|
|
2010-06-18 04:11:26 +00:00
|
|
|
|
#include "gtksizerequest.h"
|
2010-10-19 17:14:46 +00:00
|
|
|
|
|
2010-09-18 23:57:32 +00:00
|
|
|
|
#include "gtkdebug.h"
|
2009-12-02 08:48:42 +00:00
|
|
|
|
#include "gtkintl.h"
|
2010-10-19 17:14:46 +00:00
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
|
#include "gtksizegroup-private.h"
|
2012-11-13 12:41:05 +00:00
|
|
|
|
#include "gtksizerequestcacheprivate.h"
|
2010-10-19 17:14:46 +00:00
|
|
|
|
#include "gtkwidgetprivate.h"
|
2017-05-03 17:23:35 +00:00
|
|
|
|
#include "gtkcssnodeprivate.h"
|
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2018-12-12 17:20:28 +00:00
|
|
|
|
#include "gtklayoutmanagerprivate.h"
|
2009-12-02 08:48:42 +00:00
|
|
|
|
|
2011-03-05 10:29:10 +00:00
|
|
|
|
|
2015-09-09 01:06:38 +00:00
|
|
|
|
#ifdef G_ENABLE_CONSISTENCY_CHECKS
|
2011-03-06 06:13:56 +00:00
|
|
|
|
static GQuark recursion_check_quark = 0;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
push_recursion_check (GtkWidget *widget,
|
2017-05-18 07:43:32 +00:00
|
|
|
|
GtkOrientation orientation)
|
2011-03-06 06:13:56 +00:00
|
|
|
|
{
|
2017-05-18 07:43:32 +00:00
|
|
|
|
gboolean in_measure = FALSE;
|
2011-03-06 06:13:56 +00:00
|
|
|
|
|
|
|
|
|
if (recursion_check_quark == 0)
|
|
|
|
|
recursion_check_quark = g_quark_from_static_string ("gtk-size-request-in-progress");
|
|
|
|
|
|
2017-05-18 07:43:32 +00:00
|
|
|
|
in_measure = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), recursion_check_quark));
|
2011-03-06 06:13:56 +00:00
|
|
|
|
|
2017-05-18 07:43:32 +00:00
|
|
|
|
if (in_measure)
|
2011-03-06 06:13:56 +00:00
|
|
|
|
{
|
2017-05-18 07:43:32 +00:00
|
|
|
|
g_warning ("%s %p: widget tried to gtk_widget_measure inside "
|
|
|
|
|
" GtkWidget::measure implementation. "
|
|
|
|
|
"Should just invoke GTK_WIDGET_GET_CLASS(widget)->measure "
|
|
|
|
|
"directly rather than using gtk_widget_measure",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget);
|
2011-03-06 06:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 07:43:32 +00:00
|
|
|
|
g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, GINT_TO_POINTER(TRUE));
|
2011-03-06 06:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
pop_recursion_check (GtkWidget *widget,
|
2012-11-14 00:25:21 +00:00
|
|
|
|
GtkOrientation orientation)
|
2011-03-06 06:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
g_object_set_qdata (G_OBJECT (widget), recursion_check_quark, NULL);
|
|
|
|
|
}
|
2015-09-09 01:06:38 +00:00
|
|
|
|
#else
|
2017-05-18 07:43:32 +00:00
|
|
|
|
#define push_recursion_check(widget, orientation)
|
2015-09-09 01:06:38 +00:00
|
|
|
|
#define pop_recursion_check(widget, orientation)
|
|
|
|
|
#endif /* G_ENABLE_CONSISTENCY_CHECKS */
|
2011-03-06 06:13:56 +00:00
|
|
|
|
|
2019-05-25 07:03:37 +00:00
|
|
|
|
static GtkSizeRequestMode
|
|
|
|
|
fetch_request_mode (GtkWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);
|
|
|
|
|
|
|
|
|
|
if (layout_manager != NULL)
|
|
|
|
|
return gtk_layout_manager_get_request_mode (layout_manager);
|
|
|
|
|
else
|
|
|
|
|
return GTK_WIDGET_GET_CLASS (widget)->get_request_mode (widget);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
|
static int
|
2020-01-28 07:38:25 +00:00
|
|
|
|
get_number (GtkCssValue *value)
|
2017-05-03 17:23:35 +00:00
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
|
double d = _gtk_css_number_value_get (value, 100);
|
2017-05-03 17:23:35 +00:00
|
|
|
|
|
|
|
|
|
if (d < 1)
|
|
|
|
|
return ceil (d);
|
|
|
|
|
else
|
|
|
|
|
return floor (d);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 20:45:27 +00:00
|
|
|
|
/* Special-case min-width|height to round upwards, to avoid underalloc by 1px */
|
|
|
|
|
static int
|
2020-01-28 07:38:25 +00:00
|
|
|
|
get_number_ceil (GtkCssValue *value)
|
2018-05-21 20:45:27 +00:00
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
|
return ceil (_gtk_css_number_value_get (value, 100));
|
2018-05-21 20:45:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 17:23:35 +00:00
|
|
|
|
static void
|
|
|
|
|
get_box_margin (GtkCssStyle *style,
|
|
|
|
|
GtkBorder *margin)
|
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
|
margin->top = get_number (style->size->margin_top);
|
|
|
|
|
margin->left = get_number (style->size->margin_left);
|
|
|
|
|
margin->bottom = get_number (style->size->margin_bottom);
|
|
|
|
|
margin->right = get_number (style->size->margin_right);
|
2017-05-03 17:23:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_box_border (GtkCssStyle *style,
|
|
|
|
|
GtkBorder *border)
|
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
|
border->top = get_number (style->border->border_top_width);
|
|
|
|
|
border->left = get_number (style->border->border_left_width);
|
|
|
|
|
border->bottom = get_number (style->border->border_bottom_width);
|
|
|
|
|
border->right = get_number (style->border->border_right_width);
|
2017-05-03 17:23:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_box_padding (GtkCssStyle *style,
|
|
|
|
|
GtkBorder *border)
|
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
|
border->top = get_number (style->size->padding_top);
|
|
|
|
|
border->left = get_number (style->size->padding_left);
|
|
|
|
|
border->bottom = get_number (style->size->padding_bottom);
|
|
|
|
|
border->right = get_number (style->size->padding_right);
|
2017-05-03 17:23:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 15:02:14 +00:00
|
|
|
|
static void
|
2012-11-04 15:10:20 +00:00
|
|
|
|
gtk_widget_query_size_for_orientation (GtkWidget *widget,
|
2012-11-14 00:25:21 +00:00
|
|
|
|
GtkOrientation orientation,
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int for_size,
|
|
|
|
|
int *minimum,
|
|
|
|
|
int *natural,
|
|
|
|
|
int *minimum_baseline,
|
|
|
|
|
int *natural_baseline)
|
2012-11-04 15:02:14 +00:00
|
|
|
|
{
|
2018-07-15 15:56:11 +00:00
|
|
|
|
const gboolean baselines_requested = (minimum_baseline != NULL || natural_baseline != NULL);
|
2012-11-14 00:10:21 +00:00
|
|
|
|
SizeRequestCache *cache;
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int min_size = 0;
|
|
|
|
|
int nat_size = 0;
|
|
|
|
|
int min_baseline = -1;
|
|
|
|
|
int nat_baseline = -1;
|
2012-11-14 00:10:21 +00:00
|
|
|
|
gboolean found_in_cache;
|
2010-04-28 14:04:21 +00:00
|
|
|
|
|
2015-09-28 00:19:25 +00:00
|
|
|
|
gtk_widget_ensure_resize (widget);
|
|
|
|
|
|
2018-12-02 08:54:25 +00:00
|
|
|
|
/* We check the request mode first, to determine whether the widget even does
|
|
|
|
|
* any wfh/hfw handling. If it doesn't, we reset for_size to -1 and ensure
|
|
|
|
|
* that we only cache one size for the widget (i.e. a lot more cache hits). */
|
|
|
|
|
cache = _gtk_widget_peek_request_cache (widget);
|
|
|
|
|
if (G_UNLIKELY (!cache->request_mode_valid))
|
|
|
|
|
{
|
2019-05-25 07:03:37 +00:00
|
|
|
|
cache->request_mode = fetch_request_mode (widget);
|
2018-12-02 08:54:25 +00:00
|
|
|
|
cache->request_mode_valid = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cache->request_mode == GTK_SIZE_REQUEST_CONSTANT_SIZE)
|
2012-11-14 13:03:47 +00:00
|
|
|
|
for_size = -1;
|
|
|
|
|
|
2012-11-14 00:10:21 +00:00
|
|
|
|
found_in_cache = _gtk_size_request_cache_lookup (cache,
|
|
|
|
|
orientation,
|
|
|
|
|
for_size,
|
|
|
|
|
&min_size,
|
2013-03-05 13:54:03 +00:00
|
|
|
|
&nat_size,
|
2017-05-06 08:02:03 +00:00
|
|
|
|
&min_baseline,
|
|
|
|
|
&nat_baseline);
|
2013-03-05 13:54:03 +00:00
|
|
|
|
|
2010-04-17 05:51:10 +00:00
|
|
|
|
if (!found_in_cache)
|
|
|
|
|
{
|
2018-04-21 07:14:48 +00:00
|
|
|
|
GtkWidgetClass *widget_class;
|
|
|
|
|
GtkCssStyle *style;
|
|
|
|
|
GtkBorder margin, border, padding;
|
2017-05-18 10:16:35 +00:00
|
|
|
|
int adjusted_min, adjusted_natural;
|
2017-06-27 11:27:58 +00:00
|
|
|
|
int reported_min_size = 0;
|
|
|
|
|
int reported_nat_size = 0;
|
2018-04-21 07:14:48 +00:00
|
|
|
|
int css_min_size;
|
|
|
|
|
int css_min_for_size;
|
|
|
|
|
int css_extra_for_size;
|
|
|
|
|
int css_extra_size;
|
2020-02-22 04:23:41 +00:00
|
|
|
|
int widget_margins_for_size;
|
2017-05-18 10:16:35 +00:00
|
|
|
|
|
|
|
|
|
style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
|
|
|
|
|
get_box_margin (style, &margin);
|
|
|
|
|
get_box_border (style, &border);
|
|
|
|
|
get_box_padding (style, &padding);
|
|
|
|
|
|
2018-04-21 07:14:48 +00:00
|
|
|
|
widget_class = GTK_WIDGET_GET_CLASS (widget);
|
|
|
|
|
|
2017-05-18 10:16:35 +00:00
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
|
{
|
|
|
|
|
css_extra_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
|
|
|
|
|
css_extra_for_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
|
2020-01-28 07:38:25 +00:00
|
|
|
|
css_min_size = get_number_ceil (style->size->min_width);
|
|
|
|
|
css_min_for_size = get_number_ceil (style->size->min_height);
|
2020-02-22 04:23:41 +00:00
|
|
|
|
widget_margins_for_size = widget->priv->margin.top + widget->priv->margin.bottom;
|
2017-05-18 10:16:35 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
css_extra_size = margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
|
|
|
|
|
css_extra_for_size = margin.left + margin.right + border.left + border.right + padding.left + padding.right;
|
2020-01-28 07:38:25 +00:00
|
|
|
|
css_min_size = get_number_ceil (style->size->min_height);
|
|
|
|
|
css_min_for_size = get_number_ceil (style->size->min_width);
|
2020-02-22 04:23:41 +00:00
|
|
|
|
widget_margins_for_size = widget->priv->margin.left + widget->priv->margin.right;
|
2017-05-18 10:16:35 +00:00
|
|
|
|
}
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2018-12-12 17:20:28 +00:00
|
|
|
|
GtkLayoutManager *layout_manager = gtk_widget_get_layout_manager (widget);
|
|
|
|
|
|
|
|
|
|
if (layout_manager != NULL)
|
2010-04-24 01:52:55 +00:00
|
|
|
|
{
|
2018-12-12 17:20:28 +00:00
|
|
|
|
if (for_size < 0)
|
|
|
|
|
{
|
|
|
|
|
push_recursion_check (widget, orientation);
|
|
|
|
|
gtk_layout_manager_measure (layout_manager, widget,
|
|
|
|
|
orientation, -1,
|
|
|
|
|
&reported_min_size, &reported_nat_size,
|
|
|
|
|
&min_baseline, &nat_baseline);
|
|
|
|
|
pop_recursion_check (widget, orientation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int adjusted_for_size;
|
|
|
|
|
int minimum_for_size = 0;
|
|
|
|
|
int natural_for_size = 0;
|
|
|
|
|
|
|
|
|
|
/* Pull the minimum for_size from the cache as it's needed to adjust
|
|
|
|
|
* the proposed 'for_size' */
|
|
|
|
|
gtk_layout_manager_measure (layout_manager, widget,
|
|
|
|
|
OPPOSITE_ORIENTATION (orientation), -1,
|
|
|
|
|
&minimum_for_size, &natural_for_size,
|
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
|
|
if (for_size < MAX (minimum_for_size, css_min_for_size))
|
|
|
|
|
for_size = MAX (minimum_for_size, css_min_for_size);
|
|
|
|
|
|
2020-02-22 04:23:41 +00:00
|
|
|
|
adjusted_for_size = for_size - widget_margins_for_size;
|
2018-12-12 17:20:28 +00:00
|
|
|
|
adjusted_for_size -= css_extra_for_size;
|
|
|
|
|
if (adjusted_for_size < 0)
|
|
|
|
|
adjusted_for_size = MAX (minimum_for_size, css_min_for_size);
|
|
|
|
|
|
|
|
|
|
push_recursion_check (widget, orientation);
|
|
|
|
|
gtk_layout_manager_measure (layout_manager, widget,
|
|
|
|
|
orientation,
|
|
|
|
|
adjusted_for_size,
|
|
|
|
|
&reported_min_size, &reported_nat_size,
|
|
|
|
|
&min_baseline, &nat_baseline);
|
|
|
|
|
pop_recursion_check (widget, orientation);
|
|
|
|
|
}
|
2010-04-24 01:52:55 +00:00
|
|
|
|
}
|
2010-04-17 05:51:10 +00:00
|
|
|
|
else
|
2010-04-24 01:52:55 +00:00
|
|
|
|
{
|
2018-12-12 17:20:28 +00:00
|
|
|
|
if (for_size < 0)
|
|
|
|
|
{
|
|
|
|
|
push_recursion_check (widget, orientation);
|
|
|
|
|
widget_class->measure (widget, orientation, -1,
|
|
|
|
|
&reported_min_size, &reported_nat_size,
|
|
|
|
|
&min_baseline, &nat_baseline);
|
|
|
|
|
pop_recursion_check (widget, orientation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int adjusted_for_size;
|
|
|
|
|
int minimum_for_size = 0;
|
|
|
|
|
int natural_for_size = 0;
|
|
|
|
|
|
|
|
|
|
/* Pull the minimum for_size from the cache as it's needed to adjust
|
|
|
|
|
* the proposed 'for_size' */
|
|
|
|
|
gtk_widget_measure (widget, OPPOSITE_ORIENTATION (orientation), -1,
|
|
|
|
|
&minimum_for_size, &natural_for_size, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
/* TODO: Warn if the given for_size is too small? */
|
|
|
|
|
if (for_size < MAX (minimum_for_size, css_min_for_size))
|
|
|
|
|
for_size = MAX (minimum_for_size, css_min_for_size);
|
|
|
|
|
|
2020-02-22 04:23:41 +00:00
|
|
|
|
adjusted_for_size = for_size - widget_margins_for_size;
|
2018-12-12 17:20:28 +00:00
|
|
|
|
|
|
|
|
|
adjusted_for_size -= css_extra_for_size;
|
|
|
|
|
|
|
|
|
|
push_recursion_check (widget, orientation);
|
|
|
|
|
widget_class->measure (widget,
|
|
|
|
|
orientation,
|
|
|
|
|
adjusted_for_size,
|
|
|
|
|
&reported_min_size, &reported_nat_size,
|
|
|
|
|
&min_baseline, &nat_baseline);
|
|
|
|
|
pop_recursion_check (widget, orientation);
|
|
|
|
|
}
|
2010-04-24 01:52:55 +00:00
|
|
|
|
}
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2017-06-27 11:27:58 +00:00
|
|
|
|
min_size = MAX (0, MAX (reported_min_size, css_min_size)) + css_extra_size;
|
|
|
|
|
nat_size = MAX (0, MAX (reported_nat_size, css_min_size)) + css_extra_size;
|
2017-05-18 10:16:35 +00:00
|
|
|
|
|
2017-03-05 13:59:19 +00:00
|
|
|
|
if (G_UNLIKELY (min_size > nat_size))
|
2010-09-06 01:46:22 +00:00
|
|
|
|
{
|
2017-03-05 13:59:19 +00:00
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
|
{
|
2018-01-09 22:48:51 +00:00
|
|
|
|
g_warning ("%s %p (%s) reported min width %d and natural width %d in measure() with for_size=%d; natural size must be >= min size",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
2020-01-23 23:43:26 +00:00
|
|
|
|
g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
|
2018-01-09 22:48:51 +00:00
|
|
|
|
min_size, nat_size, for_size);
|
2017-03-05 13:59:19 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-01-09 22:48:51 +00:00
|
|
|
|
g_warning ("%s %p (%s) reported min height %d and natural height %d in measure() with for_size=%d; natural size must be >= min size",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
2020-01-23 23:43:26 +00:00
|
|
|
|
g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
|
2018-01-09 22:48:51 +00:00
|
|
|
|
min_size, nat_size, for_size);
|
2017-03-05 13:59:19 +00:00
|
|
|
|
|
|
|
|
|
}
|
2017-06-04 18:13:26 +00:00
|
|
|
|
|
|
|
|
|
nat_size = min_size;
|
|
|
|
|
}
|
|
|
|
|
else if (G_UNLIKELY (min_size < 0))
|
|
|
|
|
{
|
2018-01-09 22:48:51 +00:00
|
|
|
|
g_warning ("%s %p (%s) reported min %s %d, but sizes must be >= 0",
|
2017-06-04 18:13:26 +00:00
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
2020-01-23 23:43:26 +00:00
|
|
|
|
g_quark_to_string (gtk_css_node_get_name (gtk_widget_get_css_node (widget))),
|
2017-06-04 18:13:26 +00:00
|
|
|
|
orientation == GTK_ORIENTATION_HORIZONTAL ? "width" : "height",
|
|
|
|
|
min_size);
|
|
|
|
|
min_size = 0;
|
|
|
|
|
nat_size = MAX (0, min_size);
|
2010-09-06 01:46:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 06:13:56 +00:00
|
|
|
|
adjusted_min = min_size;
|
|
|
|
|
adjusted_natural = nat_size;
|
2016-11-23 16:46:44 +00:00
|
|
|
|
gtk_widget_adjust_size_request (widget, orientation,
|
|
|
|
|
&adjusted_min, &adjusted_natural);
|
2010-09-05 16:19:14 +00:00
|
|
|
|
|
2011-03-06 06:13:56 +00:00
|
|
|
|
if (adjusted_min < min_size ||
|
|
|
|
|
adjusted_natural < nat_size)
|
2010-09-05 16:19:14 +00:00
|
|
|
|
{
|
|
|
|
|
g_warning ("%s %p adjusted size %s min %d natural %d must not decrease below min %d natural %d",
|
2010-10-26 14:53:46 +00:00
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
2012-11-14 00:25:21 +00:00
|
|
|
|
orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
|
2010-09-05 16:19:14 +00:00
|
|
|
|
adjusted_min, adjusted_natural,
|
2011-03-06 06:13:56 +00:00
|
|
|
|
min_size, nat_size);
|
2010-09-05 16:19:14 +00:00
|
|
|
|
/* don't use the adjustment */
|
|
|
|
|
}
|
|
|
|
|
else if (adjusted_min > adjusted_natural)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("%s %p adjusted size %s min %d natural %d original min %d natural %d has min greater than natural",
|
2010-10-26 14:53:46 +00:00
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
2012-11-14 00:25:21 +00:00
|
|
|
|
orientation == GTK_ORIENTATION_VERTICAL ? "vertical" : "horizontal",
|
2010-09-05 16:19:14 +00:00
|
|
|
|
adjusted_min, adjusted_natural,
|
2011-03-06 06:13:56 +00:00
|
|
|
|
min_size, nat_size);
|
2010-09-05 16:19:14 +00:00
|
|
|
|
/* don't use the adjustment */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* adjustment looks good */
|
2011-03-06 06:13:56 +00:00
|
|
|
|
min_size = adjusted_min;
|
|
|
|
|
nat_size = adjusted_natural;
|
2010-09-05 16:19:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-15 15:56:11 +00:00
|
|
|
|
if (baselines_requested && (min_baseline != -1 || nat_baseline != -1))
|
2013-03-05 13:54:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("%s %p reported a horizontal baseline",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget);
|
|
|
|
|
min_baseline = -1;
|
|
|
|
|
nat_baseline = -1;
|
|
|
|
|
}
|
|
|
|
|
else if (min_baseline == -1 || nat_baseline == -1)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("%s %p reported baseline for only one of min/natural (min: %d, natural: %d)",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget,
|
|
|
|
|
min_baseline, nat_baseline);
|
|
|
|
|
min_baseline = -1;
|
|
|
|
|
nat_baseline = -1;
|
|
|
|
|
}
|
2017-07-04 09:35:06 +00:00
|
|
|
|
else if (min_baseline > reported_min_size ||
|
|
|
|
|
nat_baseline > reported_nat_size ||
|
|
|
|
|
min_baseline < 0 ||
|
|
|
|
|
nat_baseline < 0)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("%s %p reported baselines of minimum %d and natural %d, but sizes of "
|
|
|
|
|
"minimum %d and natural %d. Baselines must be inside the widget size.",
|
|
|
|
|
G_OBJECT_TYPE_NAME (widget), widget, min_baseline, nat_baseline,
|
|
|
|
|
reported_min_size, reported_nat_size);
|
|
|
|
|
min_baseline = -1;
|
|
|
|
|
nat_baseline = -1;
|
|
|
|
|
}
|
2013-03-05 13:54:03 +00:00
|
|
|
|
else
|
2017-06-27 11:27:58 +00:00
|
|
|
|
{
|
|
|
|
|
if (css_min_size > reported_min_size)
|
|
|
|
|
{
|
|
|
|
|
min_baseline += (css_min_size - reported_min_size) / 2;
|
|
|
|
|
nat_baseline += (css_min_size - reported_min_size) / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
min_baseline += margin.top + border.top + padding.top;
|
|
|
|
|
nat_baseline += margin.top + border.top + padding.top;
|
|
|
|
|
|
|
|
|
|
gtk_widget_adjust_baseline_request (widget, &min_baseline, &nat_baseline);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-05 13:54:03 +00:00
|
|
|
|
|
2012-11-14 00:10:21 +00:00
|
|
|
|
_gtk_size_request_cache_commit (cache,
|
2012-11-13 23:55:50 +00:00
|
|
|
|
orientation,
|
|
|
|
|
for_size,
|
|
|
|
|
min_size,
|
2013-03-05 13:54:03 +00:00
|
|
|
|
nat_size,
|
|
|
|
|
min_baseline,
|
|
|
|
|
nat_baseline);
|
2011-03-06 06:13:56 +00:00
|
|
|
|
}
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2017-05-18 08:11:54 +00:00
|
|
|
|
if (minimum)
|
|
|
|
|
*minimum = min_size;
|
2010-04-28 14:04:21 +00:00
|
|
|
|
|
2017-05-18 08:11:54 +00:00
|
|
|
|
if (natural)
|
|
|
|
|
*natural = nat_size;
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2013-03-05 13:54:03 +00:00
|
|
|
|
if (minimum_baseline)
|
|
|
|
|
*minimum_baseline = min_baseline;
|
|
|
|
|
|
|
|
|
|
if (natural_baseline)
|
|
|
|
|
*natural_baseline = nat_baseline;
|
|
|
|
|
|
2011-03-06 06:13:56 +00:00
|
|
|
|
g_assert (min_size <= nat_size);
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2018-04-21 15:04:41 +00:00
|
|
|
|
GTK_DISPLAY_NOTE (_gtk_widget_get_display (widget), SIZE_REQUEST, {
|
2016-02-28 20:33:18 +00:00
|
|
|
|
GString *s;
|
|
|
|
|
|
|
|
|
|
s = g_string_new ("");
|
|
|
|
|
g_string_append_printf (s, "[%p] %s\t%s: %d is minimum %d and natural: %d",
|
|
|
|
|
widget, G_OBJECT_TYPE_NAME (widget),
|
|
|
|
|
orientation == GTK_ORIENTATION_HORIZONTAL
|
|
|
|
|
? "width for height"
|
|
|
|
|
: "height for width",
|
|
|
|
|
for_size, min_size, nat_size);
|
2013-03-05 13:54:03 +00:00
|
|
|
|
if (min_baseline != -1 || nat_baseline != -1)
|
2016-08-25 21:01:28 +00:00
|
|
|
|
{
|
|
|
|
|
g_string_append_printf (s, ", baseline %d/%d",
|
|
|
|
|
min_baseline, nat_baseline);
|
|
|
|
|
}
|
2016-02-28 20:33:18 +00:00
|
|
|
|
g_string_append_printf (s, " (hit cache: %s)\n",
|
|
|
|
|
found_in_cache ? "yes" : "no");
|
|
|
|
|
g_message ("%s", s->str);
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
});
|
2012-11-04 15:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-12 21:15:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_widget_measure:
|
|
|
|
|
* @widget: A #GtkWidget instance
|
|
|
|
|
* @orientation: the orientation to measure
|
|
|
|
|
* @for_size: Size for the opposite of @orientation, i.e.
|
|
|
|
|
* if @orientation is %GTK_ORIENTATION_HORIZONTAL, this is
|
|
|
|
|
* the height the widget should be measured with. The %GTK_ORIENTATION_VERTICAL
|
|
|
|
|
* case is analogous. This way, both height-for-width and width-for-height
|
|
|
|
|
* requests can be implemented. If no size is known, -1 can be passed.
|
|
|
|
|
* @minimum: (out) (optional): location to store the minimum size, or %NULL
|
|
|
|
|
* @natural: (out) (optional): location to store the natural size, or %NULL
|
|
|
|
|
* @minimum_baseline: (out) (optional): location to store the baseline
|
|
|
|
|
* position for the minimum size, or %NULL
|
|
|
|
|
* @natural_baseline: (out) (optional): location to store the baseline
|
|
|
|
|
* position for the natural size, or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Measures @widget in the orientation @orientation and for the given @for_size.
|
|
|
|
|
*
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* As an example, if @orientation is %GTK_ORIENTATION_HORIZONTAL and @for_size
|
|
|
|
|
* is 300, this functions will compute the minimum and natural width of @widget
|
|
|
|
|
* if it is allocated at a height of 300 pixels.
|
|
|
|
|
*
|
|
|
|
|
* See [GtkWidget’s geometry management section](class.Widget.html#height-for-width-geometry-management) for
|
2018-01-03 15:14:47 +00:00
|
|
|
|
* a more details on implementing #GtkWidgetClass.measure().
|
2012-11-04 15:10:20 +00:00
|
|
|
|
*/
|
2016-11-12 21:15:48 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_widget_measure (GtkWidget *widget,
|
|
|
|
|
GtkOrientation orientation,
|
|
|
|
|
int for_size,
|
|
|
|
|
int *minimum,
|
|
|
|
|
int *natural,
|
|
|
|
|
int *minimum_baseline,
|
|
|
|
|
int *natural_baseline)
|
2012-11-04 15:10:20 +00:00
|
|
|
|
{
|
2016-11-12 21:15:48 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_WIDGET (widget));
|
|
|
|
|
g_return_if_fail (for_size >= -1);
|
|
|
|
|
g_return_if_fail (orientation == GTK_ORIENTATION_HORIZONTAL ||
|
|
|
|
|
orientation == GTK_ORIENTATION_VERTICAL);
|
|
|
|
|
|
|
|
|
|
/* This is the main function that checks for a cached size and
|
|
|
|
|
* possibly queries the widget class to compute the size if it's
|
|
|
|
|
* not cached.
|
|
|
|
|
*/
|
2019-05-20 03:31:03 +00:00
|
|
|
|
if (!_gtk_widget_get_visible (widget) && !GTK_IS_ROOT (widget))
|
2013-01-08 11:26:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (minimum)
|
|
|
|
|
*minimum = 0;
|
|
|
|
|
if (natural)
|
|
|
|
|
*natural = 0;
|
2013-03-05 13:54:03 +00:00
|
|
|
|
if (minimum_baseline)
|
|
|
|
|
*minimum_baseline = -1;
|
|
|
|
|
if (natural_baseline)
|
|
|
|
|
*natural_baseline = -1;
|
2013-01-08 11:26:04 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 15:10:20 +00:00
|
|
|
|
if (G_LIKELY (!_gtk_widget_get_sizegroups (widget)))
|
|
|
|
|
{
|
2013-03-05 13:54:03 +00:00
|
|
|
|
gtk_widget_query_size_for_orientation (widget, orientation, for_size, minimum, natural,
|
2017-05-06 08:02:03 +00:00
|
|
|
|
minimum_baseline, natural_baseline);
|
2012-11-04 15:10:20 +00:00
|
|
|
|
}
|
2018-07-15 16:08:30 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GHashTable *widgets;
|
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer key;
|
|
|
|
|
int min_result = 0, nat_result = 0;
|
2010-04-17 05:51:10 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
widgets = _gtk_size_group_get_widget_peers (widget, orientation);
|
2012-11-04 15:10:20 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
g_hash_table_iter_init (&iter, widgets);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, NULL))
|
|
|
|
|
{
|
|
|
|
|
GtkWidget *tmp_widget = key;
|
|
|
|
|
int min_dimension, nat_dimension;
|
2012-11-04 15:10:20 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
gtk_widget_query_size_for_orientation (tmp_widget, orientation, for_size,
|
|
|
|
|
&min_dimension, &nat_dimension, NULL, NULL);
|
2012-11-04 15:10:20 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
min_result = MAX (min_result, min_dimension);
|
|
|
|
|
nat_result = MAX (nat_result, nat_dimension);
|
|
|
|
|
}
|
2012-11-04 15:10:20 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
g_hash_table_destroy (widgets);
|
2012-11-04 15:10:20 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
/* Baselines make no sense with sizegroups really */
|
|
|
|
|
if (minimum_baseline)
|
|
|
|
|
*minimum_baseline = -1;
|
2013-03-05 13:54:03 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
if (natural_baseline)
|
|
|
|
|
*natural_baseline = -1;
|
2013-03-05 13:54:03 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
if (minimum)
|
|
|
|
|
*minimum = min_result;
|
2012-11-04 16:42:55 +00:00
|
|
|
|
|
2018-07-15 16:08:30 +00:00
|
|
|
|
if (natural)
|
|
|
|
|
*natural = nat_result;
|
|
|
|
|
}
|
2010-04-17 05:51:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-11 02:32:55 +00:00
|
|
|
|
/**
|
2010-09-27 06:11:27 +00:00
|
|
|
|
* gtk_widget_get_request_mode:
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* @widget: a `GtkWidget` instance
|
2010-04-11 02:32:55 +00:00
|
|
|
|
*
|
|
|
|
|
* Gets whether the widget prefers a height-for-width layout
|
2010-04-24 01:52:55 +00:00
|
|
|
|
* or a width-for-height layout.
|
|
|
|
|
*
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* Single-child widgets generally propagate the preference of
|
|
|
|
|
* their child, more complex widgets need to request something
|
|
|
|
|
* either in context of their children or in context of their
|
|
|
|
|
* allocation capabilities.
|
2010-04-11 02:32:55 +00:00
|
|
|
|
*
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* Returns: The `GtkSizeRequestMode` preferred by @widget.
|
2010-04-11 02:32:55 +00:00
|
|
|
|
*/
|
2010-06-18 04:11:26 +00:00
|
|
|
|
GtkSizeRequestMode
|
2010-09-21 14:35:17 +00:00
|
|
|
|
gtk_widget_get_request_mode (GtkWidget *widget)
|
2010-04-11 02:32:55 +00:00
|
|
|
|
{
|
2012-11-13 21:02:53 +00:00
|
|
|
|
SizeRequestCache *cache;
|
|
|
|
|
|
|
|
|
|
cache = _gtk_widget_peek_request_cache (widget);
|
|
|
|
|
|
2015-09-11 13:08:36 +00:00
|
|
|
|
if (G_UNLIKELY (!cache->request_mode_valid))
|
2012-11-13 21:02:53 +00:00
|
|
|
|
{
|
2019-05-25 07:03:37 +00:00
|
|
|
|
cache->request_mode = fetch_request_mode (widget);
|
2012-11-13 21:02:53 +00:00
|
|
|
|
cache->request_mode_valid = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cache->request_mode;
|
2010-04-11 02:32:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 09:22:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_widget_get_preferred_size:
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* @widget: a `GtkWidget` instance
|
2010-05-12 22:53:43 +00:00
|
|
|
|
* @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
|
|
|
|
|
* @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
|
2010-04-13 02:21:46 +00:00
|
|
|
|
*
|
2020-01-06 09:22:54 +00:00
|
|
|
|
* Retrieves the minimum and natural size of a widget, taking
|
|
|
|
|
* into account the widget’s preference for height-for-width management.
|
2010-04-13 02:21:46 +00:00
|
|
|
|
*
|
2010-04-24 01:52:55 +00:00
|
|
|
|
* This is used to retrieve a suitable size by container widgets which do
|
2010-10-08 14:10:42 +00:00
|
|
|
|
* not impose any restrictions on the child placement. It can be used
|
|
|
|
|
* to deduce toplevel window and menu sizes as well as child widgets in
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* free-form containers such as `GtkFixed`.
|
2010-10-08 14:10:42 +00:00
|
|
|
|
*
|
2014-02-02 06:22:14 +00:00
|
|
|
|
* Handle with care. Note that the natural height of a height-for-width
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* widget will generally be a smaller size than the minimum height, since
|
|
|
|
|
* the required height for the natural width is generally smaller than the
|
|
|
|
|
* required height for the minimum width.
|
2020-01-06 09:22:54 +00:00
|
|
|
|
*
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* Use [id@gtk_widget_measure] if you want to support baseline alignment.
|
2010-04-13 02:21:46 +00:00
|
|
|
|
*/
|
|
|
|
|
void
|
2020-01-06 09:22:54 +00:00
|
|
|
|
gtk_widget_get_preferred_size (GtkWidget *widget,
|
|
|
|
|
GtkRequisition *minimum_size,
|
|
|
|
|
GtkRequisition *natural_size)
|
2010-04-13 02:21:46 +00:00
|
|
|
|
{
|
2020-01-06 09:22:54 +00:00
|
|
|
|
int min_width, nat_width;
|
|
|
|
|
int min_height, nat_height;
|
2010-04-13 02:21:46 +00:00
|
|
|
|
|
2010-09-21 14:35:17 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_WIDGET (widget));
|
2010-04-13 02:21:46 +00:00
|
|
|
|
|
2010-09-21 14:35:17 +00:00
|
|
|
|
if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
|
2010-04-13 02:21:46 +00:00
|
|
|
|
{
|
2017-05-02 19:50:50 +00:00
|
|
|
|
gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
|
|
|
|
|
&min_width, &nat_width, NULL, NULL);
|
2010-06-18 04:11:26 +00:00
|
|
|
|
|
|
|
|
|
if (minimum_size)
|
2020-01-06 09:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
minimum_size->width = min_width;
|
2016-11-23 17:05:09 +00:00
|
|
|
|
gtk_widget_measure (widget,
|
|
|
|
|
GTK_ORIENTATION_VERTICAL, min_width,
|
2020-01-06 09:22:54 +00:00
|
|
|
|
&minimum_size->height, NULL, NULL, NULL);
|
|
|
|
|
}
|
2010-06-18 04:11:26 +00:00
|
|
|
|
|
|
|
|
|
if (natural_size)
|
2020-01-06 09:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
natural_size->width = nat_width;
|
2016-11-23 17:05:09 +00:00
|
|
|
|
gtk_widget_measure (widget,
|
|
|
|
|
GTK_ORIENTATION_VERTICAL, nat_width,
|
2020-01-06 09:22:54 +00:00
|
|
|
|
NULL, &natural_size->height, NULL, NULL);
|
|
|
|
|
}
|
2010-04-13 02:21:46 +00:00
|
|
|
|
}
|
2011-03-05 08:49:49 +00:00
|
|
|
|
else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT or CONSTANT_SIZE */
|
2010-04-13 02:21:46 +00:00
|
|
|
|
{
|
2016-11-23 17:05:09 +00:00
|
|
|
|
gtk_widget_measure (widget, GTK_ORIENTATION_VERTICAL,
|
2020-01-06 09:22:54 +00:00
|
|
|
|
-1, &min_height, &nat_height, NULL, NULL);
|
2010-06-18 04:11:26 +00:00
|
|
|
|
|
|
|
|
|
if (minimum_size)
|
2020-01-06 09:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
minimum_size->height = min_height;
|
2017-05-02 19:50:50 +00:00
|
|
|
|
gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, min_height,
|
|
|
|
|
&minimum_size->width, NULL, NULL, NULL);
|
2020-01-06 09:22:54 +00:00
|
|
|
|
}
|
2010-06-18 04:11:26 +00:00
|
|
|
|
|
|
|
|
|
if (natural_size)
|
2020-01-06 09:22:54 +00:00
|
|
|
|
{
|
|
|
|
|
natural_size->height = nat_height;
|
2017-05-02 19:50:50 +00:00
|
|
|
|
gtk_widget_measure (widget, GTK_ORIENTATION_HORIZONTAL, nat_height,
|
|
|
|
|
NULL, &natural_size->width, NULL, NULL);
|
2020-01-06 09:22:54 +00:00
|
|
|
|
}
|
2010-04-13 02:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-05 16:45:48 +00:00
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
|
static int
|
2010-08-05 16:45:48 +00:00
|
|
|
|
compare_gap (gconstpointer p1,
|
|
|
|
|
gconstpointer p2,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GtkRequestedSize *sizes = data;
|
|
|
|
|
const guint *c1 = p1;
|
|
|
|
|
const guint *c2 = p2;
|
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
|
const int d1 = MAX (sizes[*c1].natural_size -
|
2010-08-05 16:45:48 +00:00
|
|
|
|
sizes[*c1].minimum_size,
|
|
|
|
|
0);
|
2020-07-24 13:54:49 +00:00
|
|
|
|
const int d2 = MAX (sizes[*c2].natural_size -
|
2010-08-05 16:45:48 +00:00
|
|
|
|
sizes[*c2].minimum_size,
|
|
|
|
|
0);
|
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int delta = (d2 - d1);
|
2010-08-05 16:45:48 +00:00
|
|
|
|
|
|
|
|
|
if (0 == delta)
|
|
|
|
|
delta = (*c2 - *c1);
|
|
|
|
|
|
|
|
|
|
return delta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-04 15:30:48 +00:00
|
|
|
|
* gtk_distribute_natural_allocation:
|
2010-08-05 16:45:48 +00:00
|
|
|
|
* @extra_space: Extra space to redistribute among children after subtracting
|
|
|
|
|
* minimum sizes and any child padding from the overall allocation
|
|
|
|
|
* @n_requested_sizes: Number of requests to fit into the allocation
|
2021-03-07 19:59:01 +00:00
|
|
|
|
* @sizes: (array length=n_requested_sizes): An array of structs with a client pointer and a minimum/natural size
|
2010-08-05 16:45:48 +00:00
|
|
|
|
* in the orientation of the allocation.
|
|
|
|
|
*
|
2010-11-04 15:30:48 +00:00
|
|
|
|
* Distributes @extra_space to child @sizes by bringing smaller
|
2010-08-05 16:45:48 +00:00
|
|
|
|
* children up to natural size first.
|
|
|
|
|
*
|
|
|
|
|
* The remaining space will be added to the @minimum_size member of the
|
2021-03-02 04:41:04 +00:00
|
|
|
|
* `GtkRequestedSize` struct. If all sizes reach their natural size then
|
2010-08-05 16:45:48 +00:00
|
|
|
|
* the remaining space is returned.
|
|
|
|
|
*
|
|
|
|
|
* Returns: The remainder of @extra_space after redistributing space
|
|
|
|
|
* to @sizes.
|
|
|
|
|
*/
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int
|
|
|
|
|
gtk_distribute_natural_allocation (int extra_space,
|
2010-08-05 16:45:48 +00:00
|
|
|
|
guint n_requested_sizes,
|
|
|
|
|
GtkRequestedSize *sizes)
|
|
|
|
|
{
|
2010-09-22 04:57:35 +00:00
|
|
|
|
guint *spreading;
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int i;
|
2010-08-05 16:45:48 +00:00
|
|
|
|
|
2010-08-18 23:31:53 +00:00
|
|
|
|
g_return_val_if_fail (extra_space >= 0, 0);
|
2021-03-27 12:59:21 +00:00
|
|
|
|
|
|
|
|
|
if (n_requested_sizes == 0)
|
|
|
|
|
return extra_space;
|
2010-08-18 23:31:53 +00:00
|
|
|
|
|
2010-09-22 04:57:35 +00:00
|
|
|
|
spreading = g_newa (guint, n_requested_sizes);
|
|
|
|
|
|
2010-08-05 16:45:48 +00:00
|
|
|
|
for (i = 0; i < n_requested_sizes; i++)
|
|
|
|
|
spreading[i] = i;
|
|
|
|
|
|
|
|
|
|
/* Distribute the container's extra space c_gap. We want to assign
|
|
|
|
|
* this space such that the sum of extra space assigned to children
|
|
|
|
|
* (c^i_gap) is equal to c_cap. The case that there's not enough
|
|
|
|
|
* space for all children to take their natural size needs some
|
|
|
|
|
* attention. The goals we want to achieve are:
|
|
|
|
|
*
|
|
|
|
|
* a) Maximize number of children taking their natural size.
|
|
|
|
|
* b) The allocated size of children should be a continuous
|
|
|
|
|
* function of c_gap. That is, increasing the container size by
|
|
|
|
|
* one pixel should never make drastic changes in the distribution.
|
|
|
|
|
* c) If child i takes its natural size and child j doesn't,
|
|
|
|
|
* child j should have received at least as much gap as child i.
|
|
|
|
|
*
|
|
|
|
|
* The following code distributes the additional space by following
|
|
|
|
|
* these rules.
|
|
|
|
|
*/
|
2010-11-04 15:30:48 +00:00
|
|
|
|
|
2010-08-05 16:45:48 +00:00
|
|
|
|
/* Sort descending by gap and position. */
|
|
|
|
|
g_qsort_with_data (spreading,
|
|
|
|
|
n_requested_sizes, sizeof (guint),
|
|
|
|
|
compare_gap, sizes);
|
2010-11-04 15:30:48 +00:00
|
|
|
|
|
2010-08-05 16:45:48 +00:00
|
|
|
|
/* Distribute available space.
|
2020-06-18 16:20:56 +00:00
|
|
|
|
* This masterpiece of a loop was conceived by Behdad Esfahbod.
|
2010-08-05 16:45:48 +00:00
|
|
|
|
*/
|
|
|
|
|
for (i = n_requested_sizes - 1; extra_space > 0 && i >= 0; --i)
|
|
|
|
|
{
|
|
|
|
|
/* Divide remaining space by number of remaining children.
|
|
|
|
|
* Sort order and reducing remaining space by assigned space
|
|
|
|
|
* ensures that space is distributed equally.
|
|
|
|
|
*/
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int glue = (extra_space + i) / (i + 1);
|
|
|
|
|
int gap = sizes[(spreading[i])].natural_size
|
2010-08-05 16:45:48 +00:00
|
|
|
|
- sizes[(spreading[i])].minimum_size;
|
2010-11-04 15:30:48 +00:00
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int extra = MIN (glue, gap);
|
2010-11-04 15:30:48 +00:00
|
|
|
|
|
2010-08-05 16:45:48 +00:00
|
|
|
|
sizes[spreading[i]].minimum_size += extra;
|
2010-11-04 15:30:48 +00:00
|
|
|
|
|
2010-08-05 16:45:48 +00:00
|
|
|
|
extra_space -= extra;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return extra_space;
|
|
|
|
|
}
|