]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstypes.c
css: Convert border-radius to GtkCssNumber
[~andy/gtk] / gtk / gtkcsstypes.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "gtkcsstypesprivate.h"
23 #include "gtkstylecontextprivate.h"
24
25 #define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
26 \
27 static TypeName * \
28 type_name ## _copy (const TypeName *foo) \
29 { \
30   return g_memdup (foo, sizeof (TypeName)); \
31 } \
32 \
33 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
34
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)
38
39 void
40 _gtk_css_number_init (GtkCssNumber *number,
41                       double        value,
42                       GtkCssUnit    unit)
43 {
44   number->value = value;
45   number->unit = unit;
46 }
47
48 gboolean
49 _gtk_css_number_equal (const GtkCssNumber *one,
50                        const GtkCssNumber *two)
51 {
52   return one->unit == two->unit &&
53          one->value == two->value;
54 }
55
56 double
57 _gtk_css_number_get (const GtkCssNumber *number,
58                      double              one_hundred_percent)
59 {
60   if (number->unit == GTK_CSS_PERCENT)
61     return number->value * one_hundred_percent * 0.01;
62   else
63     return number->value;
64 }
65
66 void
67 _gtk_css_number_compute (GtkCssNumber       *dest,
68                          const GtkCssNumber *src,
69                          GtkStyleContext    *context)
70 {
71   switch (src->unit)
72     {
73     default:
74       g_assert_not_reached();
75       /* fall through */
76     case GTK_CSS_PERCENT:
77     case GTK_CSS_NUMBER:
78     case GTK_CSS_PX:
79       dest->value = src->value;
80       dest->unit = src->unit;
81       break;
82     case GTK_CSS_PT:
83       dest->value = src->value * 96.0 / 72.0;
84       dest->unit = GTK_CSS_PX;
85       break;
86     case GTK_CSS_PC:
87       dest->value = src->value * 96.0 / 72.0 * 12.0;
88       dest->unit = GTK_CSS_PX;
89       break;
90     case GTK_CSS_IN:
91       dest->value = src->value * 96.0;
92       dest->unit = GTK_CSS_PX;
93       break;
94     case GTK_CSS_CM:
95       dest->value = src->value * 96.0 * 0.39370078740157477;
96       dest->unit = GTK_CSS_PX;
97       break;
98     case GTK_CSS_MM:
99       dest->value = src->value * 96.0 * 0.039370078740157477;
100       dest->unit = GTK_CSS_PX;
101       break;
102     case GTK_CSS_EM:
103       dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
104       dest->unit = GTK_CSS_PX;
105       break;
106     case GTK_CSS_EX:
107       /* for now we pretend ex is half of em */
108       dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
109       dest->unit = GTK_CSS_PX;
110       break;
111     }
112 }
113
114 void
115 _gtk_css_number_print (const GtkCssNumber *number,
116                        GString            *string)
117 {
118   char buf[G_ASCII_DTOSTR_BUF_SIZE];
119
120   g_return_if_fail (number != NULL);
121   g_return_if_fail (string != NULL);
122
123   const char *names[] = {
124     /* [GTK_CSS_NUMBER] = */ "",
125     /* [GTK_CSS_PERCENT] = */ "%",
126     /* [GTK_CSS_PX] = */ "px",
127     /* [GTK_CSS_PT] = */ "pt",
128     /* [GTK_CSS_EM] = */ "em",
129     /* [GTK_CSS_EX] = */ "ex",
130     /* [GTK_CSS_PC] = */ "pc",
131     /* [GTK_CSS_IN] = */ "in",
132     /* [GTK_CSS_CM] = */ "cm",
133     /* [GTK_CSS_MM] = */ "mm"
134   };
135
136   g_ascii_dtostr (buf, sizeof (buf), number->value);
137   g_string_append (string, buf);
138   if (number->value != 0.0)
139     g_string_append (string, names[number->unit]);
140 }