2012-03-27 22:04:29 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
|
|
|
|
2012-07-16 11:42:18 +00:00
|
|
|
#include "gtkcssenumvalueprivate.h"
|
2012-12-01 15:22:04 +00:00
|
|
|
#include "gtkcssinitialvalueprivate.h"
|
2012-03-27 22:04:29 +00:00
|
|
|
#include "gtkstylepropertyprivate.h"
|
|
|
|
|
2015-12-23 10:58:42 +00:00
|
|
|
#include "fallback-c89.c"
|
|
|
|
|
2012-03-27 22:04:29 +00:00
|
|
|
struct _GtkCssValue {
|
|
|
|
GTK_CSS_VALUE_BASE
|
|
|
|
GtkCssUnit unit;
|
|
|
|
double value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_value_number_free (GtkCssValue *value)
|
|
|
|
{
|
|
|
|
g_slice_free (GtkCssValue, value);
|
|
|
|
}
|
|
|
|
|
2012-12-01 15:22:04 +00:00
|
|
|
static double
|
|
|
|
get_base_font_size (guint property_id,
|
|
|
|
GtkStyleProviderPrivate *provider,
|
2015-01-31 10:56:15 +00:00
|
|
|
GtkCssStyle *style,
|
2015-02-14 01:27:39 +00:00
|
|
|
GtkCssStyle *parent_style)
|
2012-12-01 15:22:04 +00:00
|
|
|
{
|
|
|
|
if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
|
|
|
|
{
|
2015-01-31 10:56:15 +00:00
|
|
|
if (parent_style)
|
|
|
|
return _gtk_css_number_value_get (gtk_css_style_get_value (parent_style, GTK_CSS_PROPERTY_FONT_SIZE), 100);
|
2012-12-01 15:22:04 +00:00
|
|
|
else
|
|
|
|
return _gtk_css_font_size_get_default (provider);
|
|
|
|
}
|
|
|
|
|
2015-01-31 10:56:15 +00:00
|
|
|
return _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_FONT_SIZE), 100);
|
2012-12-01 15:22:04 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 04:56:07 +00:00
|
|
|
static GtkCssValue *
|
2012-09-28 16:02:46 +00:00
|
|
|
gtk_css_value_number_compute (GtkCssValue *number,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProviderPrivate *provider,
|
2015-01-31 10:56:15 +00:00
|
|
|
GtkCssStyle *style,
|
2015-02-14 01:27:39 +00:00
|
|
|
GtkCssStyle *parent_style)
|
2012-07-11 04:56:07 +00:00
|
|
|
{
|
2015-01-21 14:29:54 +00:00
|
|
|
GtkBorderStyle border_style;
|
|
|
|
|
|
|
|
/* special case according to http://dev.w3.org/csswg/css-backgrounds/#the-border-width */
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
|
2015-01-31 10:56:15 +00:00
|
|
|
border_style = _gtk_css_border_style_value_get(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
|
2015-01-21 14:29:54 +00:00
|
|
|
if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
|
|
|
|
return _gtk_css_number_value_new (0, GTK_CSS_NUMBER);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
|
2015-01-31 10:56:15 +00:00
|
|
|
border_style = _gtk_css_border_style_value_get(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
|
2015-01-21 14:29:54 +00:00
|
|
|
if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
|
|
|
|
return _gtk_css_number_value_new (0, GTK_CSS_NUMBER);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
|
2015-01-31 10:56:15 +00:00
|
|
|
border_style = _gtk_css_border_style_value_get(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
|
2015-01-21 14:29:54 +00:00
|
|
|
if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
|
|
|
|
return _gtk_css_number_value_new (0, GTK_CSS_NUMBER);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
|
2015-01-31 10:56:15 +00:00
|
|
|
border_style = _gtk_css_border_style_value_get(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
|
2015-01-21 14:29:54 +00:00
|
|
|
if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
|
|
|
|
return _gtk_css_number_value_new (0, GTK_CSS_NUMBER);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
|
2015-01-31 10:56:15 +00:00
|
|
|
border_style = _gtk_css_border_style_value_get(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_STYLE));
|
2015-01-21 14:29:54 +00:00
|
|
|
if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
|
|
|
|
return _gtk_css_number_value_new (0, GTK_CSS_NUMBER);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-11 04:56:07 +00:00
|
|
|
switch (number->unit)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
/* fall through */
|
|
|
|
case GTK_CSS_PERCENT:
|
2012-12-01 15:22:04 +00:00
|
|
|
/* percentages for font sizes are computed, other percentages aren't */
|
|
|
|
if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
|
|
|
|
return _gtk_css_number_value_new (number->value / 100.0 *
|
2015-02-14 01:27:39 +00:00
|
|
|
get_base_font_size (property_id, provider, style, parent_style),
|
2012-12-01 15:22:04 +00:00
|
|
|
GTK_CSS_PX);
|
2012-07-11 04:56:07 +00:00
|
|
|
case GTK_CSS_NUMBER:
|
|
|
|
case GTK_CSS_PX:
|
|
|
|
case GTK_CSS_DEG:
|
|
|
|
case GTK_CSS_S:
|
|
|
|
return _gtk_css_value_ref (number);
|
|
|
|
case GTK_CSS_PT:
|
|
|
|
return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
|
|
|
|
GTK_CSS_PX);
|
|
|
|
case GTK_CSS_PC:
|
|
|
|
return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
|
|
|
|
GTK_CSS_PX);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_IN:
|
|
|
|
return _gtk_css_number_value_new (number->value * 96.0,
|
|
|
|
GTK_CSS_PX);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_CM:
|
|
|
|
return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
|
|
|
|
GTK_CSS_PX);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_MM:
|
|
|
|
return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
|
|
|
|
GTK_CSS_PX);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_EM:
|
|
|
|
return _gtk_css_number_value_new (number->value *
|
2015-02-14 01:27:39 +00:00
|
|
|
get_base_font_size (property_id, provider, style, parent_style),
|
2012-07-11 04:56:07 +00:00
|
|
|
GTK_CSS_PX);
|
|
|
|
break;
|
|
|
|
case GTK_CSS_EX:
|
|
|
|
/* for now we pretend ex is half of em */
|
|
|
|
return _gtk_css_number_value_new (number->value * 0.5 *
|
2015-02-14 01:27:39 +00:00
|
|
|
get_base_font_size (property_id, provider, style, parent_style),
|
2012-07-11 04:56:07 +00:00
|
|
|
GTK_CSS_PX);
|
|
|
|
case GTK_CSS_RAD:
|
|
|
|
return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
|
|
|
|
GTK_CSS_DEG);
|
|
|
|
case GTK_CSS_GRAD:
|
|
|
|
return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
|
|
|
|
GTK_CSS_DEG);
|
|
|
|
case GTK_CSS_TURN:
|
|
|
|
return _gtk_css_number_value_new (number->value * 360.0,
|
|
|
|
GTK_CSS_DEG);
|
|
|
|
case GTK_CSS_MS:
|
|
|
|
return _gtk_css_number_value_new (number->value / 1000.0,
|
|
|
|
GTK_CSS_S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:04:29 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_value_number_equal (const GtkCssValue *number1,
|
|
|
|
const GtkCssValue *number2)
|
|
|
|
{
|
|
|
|
return number1->unit == number2->unit &&
|
|
|
|
number1->value == number2->value;
|
|
|
|
}
|
|
|
|
|
2012-03-30 15:47:26 +00:00
|
|
|
static GtkCssValue *
|
|
|
|
gtk_css_value_number_transition (GtkCssValue *start,
|
|
|
|
GtkCssValue *end,
|
2012-08-30 13:51:29 +00:00
|
|
|
guint property_id,
|
2012-03-30 15:47:26 +00:00
|
|
|
double progress)
|
|
|
|
{
|
|
|
|
/* FIXME: This needs to be supported at least for percentages,
|
|
|
|
* but for that we kinda need to support calc(5px + 50%) */
|
|
|
|
if (start->unit != end->unit)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
|
|
|
|
start->unit);
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:04:29 +00:00
|
|
|
static void
|
|
|
|
gtk_css_value_number_print (const GtkCssValue *number,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
char buf[G_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
|
|
|
|
const char *names[] = {
|
|
|
|
/* [GTK_CSS_NUMBER] = */ "",
|
|
|
|
/* [GTK_CSS_PERCENT] = */ "%",
|
|
|
|
/* [GTK_CSS_PX] = */ "px",
|
|
|
|
/* [GTK_CSS_PT] = */ "pt",
|
|
|
|
/* [GTK_CSS_EM] = */ "em",
|
|
|
|
/* [GTK_CSS_EX] = */ "ex",
|
|
|
|
/* [GTK_CSS_PC] = */ "pc",
|
|
|
|
/* [GTK_CSS_IN] = */ "in",
|
|
|
|
/* [GTK_CSS_CM] = */ "cm",
|
|
|
|
/* [GTK_CSS_MM] = */ "mm",
|
|
|
|
/* [GTK_CSS_RAD] = */ "rad",
|
|
|
|
/* [GTK_CSS_DEG] = */ "deg",
|
|
|
|
/* [GTK_CSS_GRAD] = */ "grad",
|
|
|
|
/* [GTK_CSS_TURN] = */ "turn",
|
2012-04-01 02:02:34 +00:00
|
|
|
/* [GTK_CSS_S] = */ "s",
|
|
|
|
/* [GTK_CSS_MS] = */ "ms",
|
2012-03-27 22:04:29 +00:00
|
|
|
};
|
|
|
|
|
2015-12-23 02:58:31 +00:00
|
|
|
if (isinf (number->value))
|
|
|
|
g_string_append (string, "infinite");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_ascii_dtostr (buf, sizeof (buf), number->value);
|
|
|
|
g_string_append (string, buf);
|
|
|
|
if (number->value != 0.0)
|
|
|
|
g_string_append (string, names[number->unit]);
|
|
|
|
}
|
2012-03-27 22:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
|
|
|
|
gtk_css_value_number_free,
|
2012-07-11 04:56:07 +00:00
|
|
|
gtk_css_value_number_compute,
|
2012-03-27 22:04:29 +00:00
|
|
|
gtk_css_value_number_equal,
|
2012-03-30 15:47:26 +00:00
|
|
|
gtk_css_value_number_transition,
|
2012-03-27 22:04:29 +00:00
|
|
|
gtk_css_value_number_print
|
|
|
|
};
|
|
|
|
|
|
|
|
GtkCssValue *
|
|
|
|
_gtk_css_number_value_new (double value,
|
|
|
|
GtkCssUnit unit)
|
|
|
|
{
|
2013-02-06 13:28:20 +00:00
|
|
|
static GtkCssValue number_singletons[] = {
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 },
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 1 },
|
|
|
|
};
|
2012-03-27 22:04:29 +00:00
|
|
|
static GtkCssValue px_singletons[] = {
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
|
|
|
|
{ >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
|
|
|
|
};
|
|
|
|
GtkCssValue *result;
|
|
|
|
|
2013-02-06 13:28:20 +00:00
|
|
|
if (unit == GTK_CSS_NUMBER && (value == 0 || value == 1))
|
|
|
|
return _gtk_css_value_ref (&number_singletons[(int) value]);
|
2012-03-27 22:04:29 +00:00
|
|
|
|
|
|
|
if (unit == GTK_CSS_PX &&
|
|
|
|
(value == 0 ||
|
|
|
|
value == 1 ||
|
|
|
|
value == 2 ||
|
|
|
|
value == 3 ||
|
|
|
|
value == 4))
|
|
|
|
{
|
|
|
|
return _gtk_css_value_ref (&px_singletons[(int) value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
|
|
|
|
result->unit = unit;
|
|
|
|
result->value = value;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-04 15:42:45 +00:00
|
|
|
GtkCssUnit
|
|
|
|
_gtk_css_number_value_get_unit (const GtkCssValue *value)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (value->class == >K_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
|
|
|
|
|
|
|
|
return value->unit;
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:04:29 +00:00
|
|
|
double
|
|
|
|
_gtk_css_number_value_get (const GtkCssValue *number,
|
|
|
|
double one_hundred_percent)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (number != NULL, 0.0);
|
|
|
|
g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
|
|
|
|
|
|
|
|
if (number->unit == GTK_CSS_PERCENT)
|
|
|
|
return number->value * one_hundred_percent / 100;
|
|
|
|
else
|
|
|
|
return number->value;
|
|
|
|
}
|
|
|
|
|