1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcsstypesprivate.h"
23 #include "gtkstylecontextprivate.h"
25 #define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
28 type_name ## _copy (const TypeName *foo) \
30 return g_memdup (foo, sizeof (TypeName)); \
33 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
35 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
36 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
37 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssNumber, _gtk_css_number)
40 _gtk_css_number_init (GtkCssNumber *number,
44 number->value = value;
49 _gtk_css_number_equal (const GtkCssNumber *one,
50 const GtkCssNumber *two)
52 return one->unit == two->unit &&
53 one->value == two->value;
57 _gtk_css_number_get (const GtkCssNumber *number,
58 double one_hundred_percent)
60 if (number->unit == GTK_CSS_PERCENT)
61 return number->value * one_hundred_percent * 0.01;
67 _gtk_css_number_compute (GtkCssNumber *dest,
68 const GtkCssNumber *src,
69 GtkStyleContext *context)
74 g_assert_not_reached();
80 dest->value = src->value;
81 dest->unit = src->unit;
84 dest->value = src->value * 96.0 / 72.0;
85 dest->unit = GTK_CSS_PX;
88 dest->value = src->value * 96.0 / 72.0 * 12.0;
89 dest->unit = GTK_CSS_PX;
92 dest->value = src->value * 96.0;
93 dest->unit = GTK_CSS_PX;
96 dest->value = src->value * 96.0 * 0.39370078740157477;
97 dest->unit = GTK_CSS_PX;
100 dest->value = src->value * 96.0 * 0.039370078740157477;
101 dest->unit = GTK_CSS_PX;
104 dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
105 dest->unit = GTK_CSS_PX;
108 /* for now we pretend ex is half of em */
109 dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
110 dest->unit = GTK_CSS_PX;
113 dest->value = 360 * src->value / (2 * G_PI);
114 dest->unit = GTK_CSS_DEG;
117 dest->value = 360 * src->value / 400.0;
118 dest->unit = GTK_CSS_DEG;
121 dest->value = 360 * src->value;
122 dest->unit = GTK_CSS_DEG;
128 _gtk_css_number_print (const GtkCssNumber *number,
131 char buf[G_ASCII_DTOSTR_BUF_SIZE];
133 g_return_if_fail (number != NULL);
134 g_return_if_fail (string != NULL);
136 const char *names[] = {
137 /* [GTK_CSS_NUMBER] = */ "",
138 /* [GTK_CSS_PERCENT] = */ "%",
139 /* [GTK_CSS_PX] = */ "px",
140 /* [GTK_CSS_PT] = */ "pt",
141 /* [GTK_CSS_EM] = */ "em",
142 /* [GTK_CSS_EX] = */ "ex",
143 /* [GTK_CSS_PC] = */ "pc",
144 /* [GTK_CSS_IN] = */ "in",
145 /* [GTK_CSS_CM] = */ "cm",
146 /* [GTK_CSS_MM] = */ "mm",
147 /* [GTK_CSS_RAD] = */ "rad",
148 /* [GTK_CSS_DEG] = */ "deg",
149 /* [GTK_CSS_GRAD] = */ "grad",
150 /* [GTK_CSS_TURN] = */ "turn",
153 g_ascii_dtostr (buf, sizeof (buf), number->value);
154 g_string_append (string, buf);
155 if (number->value != 0.0)
156 g_string_append (string, names[number->unit]);