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
gtk_css_value_corner_free(GtkCssValue * value)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 GtkCssValue *
gtk_css_value_corner_compute(GtkCssValue * corner,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)40 gtk_css_value_corner_compute (GtkCssValue             *corner,
41                               guint                    property_id,
42                               GtkStyleProviderPrivate *provider,
43                               GtkCssStyle             *style,
44                               GtkCssStyle             *parent_style)
45 {
46   GtkCssValue *x, *y;
47 
48   x = _gtk_css_value_compute (corner->x, property_id, provider, style, parent_style);
49   y = _gtk_css_value_compute (corner->y, property_id, provider, style, parent_style);
50   if (x == corner->x && y == corner->y)
51     {
52       _gtk_css_value_unref (x);
53       _gtk_css_value_unref (y);
54       return _gtk_css_value_ref (corner);
55     }
56 
57   return _gtk_css_corner_value_new (x, y);
58 }
59 
60 static gboolean
gtk_css_value_corner_equal(const GtkCssValue * corner1,const GtkCssValue * corner2)61 gtk_css_value_corner_equal (const GtkCssValue *corner1,
62                             const GtkCssValue *corner2)
63 {
64   return _gtk_css_value_equal (corner1->x, corner2->x)
65       && _gtk_css_value_equal (corner1->y, corner2->y);
66 }
67 
68 static GtkCssValue *
gtk_css_value_corner_transition(GtkCssValue * start,GtkCssValue * end,guint property_id,double progress)69 gtk_css_value_corner_transition (GtkCssValue *start,
70                                  GtkCssValue *end,
71                                  guint        property_id,
72                                  double       progress)
73 {
74   GtkCssValue *x, *y;
75 
76   x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
77   if (x == NULL)
78     return NULL;
79   y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
80   if (y == NULL)
81     {
82       _gtk_css_value_unref (x);
83       return NULL;
84     }
85 
86   return _gtk_css_corner_value_new (x, y);
87 }
88 
89 static void
gtk_css_value_corner_print(const GtkCssValue * corner,GString * string)90 gtk_css_value_corner_print (const GtkCssValue *corner,
91                            GString           *string)
92 {
93   _gtk_css_value_print (corner->x, string);
94   if (!_gtk_css_value_equal (corner->x, corner->y))
95     {
96       g_string_append_c (string, ' ');
97       _gtk_css_value_print (corner->y, string);
98     }
99 }
100 
101 static const GtkCssValueClass GTK_CSS_VALUE_CORNER = {
102   gtk_css_value_corner_free,
103   gtk_css_value_corner_compute,
104   gtk_css_value_corner_equal,
105   gtk_css_value_corner_transition,
106   gtk_css_value_corner_print
107 };
108 
109 GtkCssValue *
_gtk_css_corner_value_new(GtkCssValue * x,GtkCssValue * y)110 _gtk_css_corner_value_new (GtkCssValue *x,
111                            GtkCssValue *y)
112 {
113   GtkCssValue *result;
114 
115   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_CORNER);
116   result->x = x;
117   result->y = y;
118 
119   return result;
120 }
121 
122 GtkCssValue *
_gtk_css_corner_value_parse(GtkCssParser * parser)123 _gtk_css_corner_value_parse (GtkCssParser *parser)
124 {
125   GtkCssValue *x, *y;
126 
127   x = _gtk_css_number_value_parse (parser,
128                                    GTK_CSS_POSITIVE_ONLY
129                                    | GTK_CSS_PARSE_PERCENT
130                                    | GTK_CSS_NUMBER_AS_PIXELS
131                                    | GTK_CSS_PARSE_LENGTH);
132   if (x == NULL)
133     return NULL;
134 
135   if (!gtk_css_number_value_can_parse (parser))
136     y = _gtk_css_value_ref (x);
137   else
138     {
139       y = _gtk_css_number_value_parse (parser,
140                                        GTK_CSS_POSITIVE_ONLY
141                                        | GTK_CSS_PARSE_PERCENT
142                                        | GTK_CSS_NUMBER_AS_PIXELS
143                                        | GTK_CSS_PARSE_LENGTH);
144       if (y == NULL)
145         {
146           _gtk_css_value_unref (x);
147           return NULL;
148         }
149     }
150 
151   return _gtk_css_corner_value_new (x, y);
152 }
153 
154 double
_gtk_css_corner_value_get_x(const GtkCssValue * corner,double one_hundred_percent)155 _gtk_css_corner_value_get_x (const GtkCssValue *corner,
156                              double             one_hundred_percent)
157 {
158   g_return_val_if_fail (corner != NULL, 0.0);
159   g_return_val_if_fail (corner->class == &GTK_CSS_VALUE_CORNER, 0.0);
160 
161   return _gtk_css_number_value_get (corner->x, one_hundred_percent);
162 }
163 
164 double
_gtk_css_corner_value_get_y(const GtkCssValue * corner,double one_hundred_percent)165 _gtk_css_corner_value_get_y (const GtkCssValue *corner,
166                              double             one_hundred_percent)
167 {
168   g_return_val_if_fail (corner != NULL, 0.0);
169   g_return_val_if_fail (corner->class == &GTK_CSS_VALUE_CORNER, 0.0);
170 
171   return _gtk_css_number_value_get (corner->y, one_hundred_percent);
172 }
173 
174