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