]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstypes.c
84db93653c185a18d5198809c5c10e46c6c0e568
[~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 (GtkCssBackgroundSize, _gtk_css_background_size)
36 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
37 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
38 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssNumber, _gtk_css_number)
39
40 void
41 _gtk_css_number_init (GtkCssNumber *number,
42                       double        value,
43                       GtkCssUnit    unit)
44 {
45   number->value = value;
46   number->unit = unit;
47 }
48
49 gboolean
50 _gtk_css_number_equal (const GtkCssNumber *one,
51                        const GtkCssNumber *two)
52 {
53   return one->unit == two->unit &&
54          one->value == two->value;
55 }
56
57 double
58 _gtk_css_number_get (const GtkCssNumber *number,
59                      double              one_hundred_percent)
60 {
61   if (number->unit == GTK_CSS_PERCENT)
62     return number->value * one_hundred_percent * 0.01;
63   else
64     return number->value;
65 }
66
67 void
68 _gtk_css_number_compute (GtkCssNumber       *dest,
69                          const GtkCssNumber *src,
70                          GtkStyleContext    *context)
71 {
72   switch (src->unit)
73     {
74     default:
75       g_assert_not_reached();
76       /* fall through */
77     case GTK_CSS_PERCENT:
78     case GTK_CSS_NUMBER:
79     case GTK_CSS_PX:
80     case GTK_CSS_DEG:
81       dest->value = src->value;
82       dest->unit = src->unit;
83       break;
84     case GTK_CSS_PT:
85       dest->value = src->value * 96.0 / 72.0;
86       dest->unit = GTK_CSS_PX;
87       break;
88     case GTK_CSS_PC:
89       dest->value = src->value * 96.0 / 72.0 * 12.0;
90       dest->unit = GTK_CSS_PX;
91       break;
92     case GTK_CSS_IN:
93       dest->value = src->value * 96.0;
94       dest->unit = GTK_CSS_PX;
95       break;
96     case GTK_CSS_CM:
97       dest->value = src->value * 96.0 * 0.39370078740157477;
98       dest->unit = GTK_CSS_PX;
99       break;
100     case GTK_CSS_MM:
101       dest->value = src->value * 96.0 * 0.039370078740157477;
102       dest->unit = GTK_CSS_PX;
103       break;
104     case GTK_CSS_EM:
105       dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
106       dest->unit = GTK_CSS_PX;
107       break;
108     case GTK_CSS_EX:
109       /* for now we pretend ex is half of em */
110       dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
111       dest->unit = GTK_CSS_PX;
112       break;
113     case GTK_CSS_RAD:
114       dest->value = 360 * src->value / (2 * G_PI);
115       dest->unit = GTK_CSS_DEG;
116       break;
117     case GTK_CSS_GRAD:
118       dest->value = 360 * src->value / 400.0;
119       dest->unit = GTK_CSS_DEG;
120       break;
121     case GTK_CSS_TURN:
122       dest->value = 360 * src->value;
123       dest->unit = GTK_CSS_DEG;
124       break;
125     }
126 }
127
128 void
129 _gtk_css_number_print (const GtkCssNumber *number,
130                        GString            *string)
131 {
132   char buf[G_ASCII_DTOSTR_BUF_SIZE];
133
134   const char *names[] = {
135     /* [GTK_CSS_NUMBER] = */ "",
136     /* [GTK_CSS_PERCENT] = */ "%",
137     /* [GTK_CSS_PX] = */ "px",
138     /* [GTK_CSS_PT] = */ "pt",
139     /* [GTK_CSS_EM] = */ "em",
140     /* [GTK_CSS_EX] = */ "ex",
141     /* [GTK_CSS_PC] = */ "pc",
142     /* [GTK_CSS_IN] = */ "in",
143     /* [GTK_CSS_CM] = */ "cm",
144     /* [GTK_CSS_MM] = */ "mm",
145     /* [GTK_CSS_RAD] = */ "rad",
146     /* [GTK_CSS_DEG] = */ "deg",
147     /* [GTK_CSS_GRAD] = */ "grad",
148     /* [GTK_CSS_TURN] = */ "turn",
149   };
150
151   g_return_if_fail (number != NULL);
152   g_return_if_fail (string != NULL);
153
154   g_ascii_dtostr (buf, sizeof (buf), number->value);
155   g_string_append (string, buf);
156   if (number->value != 0.0)
157     g_string_append (string, names[number->unit]);
158 }