]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscornervalue.c
cssvalue: First step of proper dependency tracking
[~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                               GtkCssDependencies *dependencies)
44 {
45   GtkCssValue *x, *y;
46   GtkCssDependencies x_deps, y_deps;
47
48   x = _gtk_css_value_compute (corner->x, property_id, context, &x_deps);
49   y = _gtk_css_value_compute (corner->y, property_id, context, &y_deps);
50   *dependencies = _gtk_css_dependencies_union (x_deps, y_deps);
51   if (x == corner->x && y == corner->y)
52     {
53       _gtk_css_value_unref (x);
54       _gtk_css_value_unref (y);
55       return _gtk_css_value_ref (corner);
56     }
57
58   return _gtk_css_corner_value_new (x, y);
59 }
60
61 static gboolean
62 gtk_css_value_corner_equal (const GtkCssValue *corner1,
63                             const GtkCssValue *corner2)
64 {
65   return _gtk_css_value_equal (corner1->x, corner2->x)
66       && _gtk_css_value_equal (corner1->y, corner2->y);
67 }
68
69 static GtkCssValue *
70 gtk_css_value_corner_transition (GtkCssValue *start,
71                                 GtkCssValue *end,
72                                 double       progress)
73 {
74   GtkCssValue *x, *y;
75
76   x = _gtk_css_value_transition (start->x, end->x, progress);
77   if (x == NULL)
78     return NULL;
79   y = _gtk_css_value_transition (start->y, end->y, 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
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 *
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 *
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_parser_has_number (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
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
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