]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscornervalue.c
cssvalue: Add a value for border-radius corner properties
[~andy/gtk] / gtk / gtkcsscornervalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcsscornervalueprivate.h"
21
22 #include "gtkcssnumbervalueprivate.h"
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   GtkCssValue *x;
27   GtkCssValue *y;
28 };
29
30 static void
31 gtk_css_value_corner_free (GtkCssValue *value)
32 {
33   _gtk_css_value_unref (value->x);
34   _gtk_css_value_unref (value->y);
35
36   g_slice_free (GtkCssValue, value);
37 }
38
39 static gboolean
40 gtk_css_value_corner_equal (const GtkCssValue *corner1,
41                             const GtkCssValue *corner2)
42 {
43   return _gtk_css_value_equal (corner1->x, corner2->x)
44       && _gtk_css_value_equal (corner1->y, corner2->y);
45 }
46
47 static GtkCssValue *
48 gtk_css_value_corner_transition (GtkCssValue *start,
49                                 GtkCssValue *end,
50                                 double       progress)
51 {
52   GtkCssValue *x, *y;
53
54   x = _gtk_css_value_transition (start->x, end->x, progress);
55   if (x == NULL)
56     return NULL;
57   y = _gtk_css_value_transition (start->y, end->y, progress);
58   if (y == NULL)
59     {
60       _gtk_css_value_unref (x);
61       return NULL;
62     }
63
64   return _gtk_css_corner_value_new (x, y);
65 }
66
67 static void
68 gtk_css_value_corner_print (const GtkCssValue *corner,
69                            GString           *string)
70 {
71   _gtk_css_value_print (corner->x, string);
72   if (!_gtk_css_value_equal (corner->x, corner->y))
73     {
74       g_string_append_c (string, ' ');
75       _gtk_css_value_print (corner->y, string);
76     }
77 }
78
79 static const GtkCssValueClass GTK_CSS_VALUE_CORNER = {
80   gtk_css_value_corner_free,
81   gtk_css_value_corner_equal,
82   gtk_css_value_corner_transition,
83   gtk_css_value_corner_print
84 };
85
86 GtkCssValue *
87 _gtk_css_corner_value_new (GtkCssValue *x,
88                            GtkCssValue *y)
89 {
90   GtkCssValue *result;
91
92   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_CORNER);
93   result->x = x;
94   result->y = y;
95
96   return result;
97 }
98
99 GtkCssValue *
100 _gtk_css_corner_value_parse (GtkCssParser *parser)
101 {
102   GtkCssValue *x, *y;
103
104   x = _gtk_css_number_value_parse (parser,
105                                    GTK_CSS_POSITIVE_ONLY
106                                    | GTK_CSS_PARSE_PERCENT
107                                    | GTK_CSS_NUMBER_AS_PIXELS
108                                    | GTK_CSS_PARSE_LENGTH);
109   if (x == NULL)
110     return NULL;
111
112   if (!_gtk_css_parser_has_number (parser))
113     y = _gtk_css_value_ref (x);
114   else
115     {
116       y = _gtk_css_number_value_parse (parser,
117                                        GTK_CSS_POSITIVE_ONLY
118                                        | GTK_CSS_PARSE_PERCENT
119                                        | GTK_CSS_NUMBER_AS_PIXELS
120                                        | GTK_CSS_PARSE_LENGTH);
121       if (y == NULL)
122         {
123           _gtk_css_value_unref (x);
124           return NULL;
125         }
126     }
127
128   return _gtk_css_corner_value_new (x, y);
129 }
130
131 double
132 _gtk_css_corner_value_get_x (const GtkCssValue *corner,
133                              double             one_hundred_percent)
134 {
135   g_return_val_if_fail (corner != NULL, 0.0);
136   g_return_val_if_fail (corner->class == &GTK_CSS_VALUE_CORNER, 0.0);
137
138   return _gtk_css_number_value_get (corner->x, one_hundred_percent);
139 }
140
141 double
142 _gtk_css_corner_value_get_y (const GtkCssValue *corner,
143                              double             one_hundred_percent)
144 {
145   g_return_val_if_fail (corner != NULL, 0.0);
146   g_return_val_if_fail (corner->class == &GTK_CSS_VALUE_CORNER, 0.0);
147
148   return _gtk_css_number_value_get (corner->y, one_hundred_percent);
149 }
150
151 GtkCssValue *
152 _gtk_css_corner_value_compute (GtkCssValue     *corner,
153                                GtkStyleContext *context)
154 {
155   GtkCssValue *x, *y;
156
157   g_return_val_if_fail (corner->class == &GTK_CSS_VALUE_CORNER, NULL);
158
159   x = _gtk_css_number_value_compute (corner->x, context);
160   y = _gtk_css_number_value_compute (corner->y, context);
161   if (x == corner->x && y == corner->y)
162     {
163       _gtk_css_value_unref (x);
164       _gtk_css_value_unref (y);
165       return _gtk_css_value_ref (corner);
166     }
167
168   return _gtk_css_corner_value_new (x, y);
169 }
170