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