]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssbgsizevalue.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcssbgsizevalue.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 "gtkcssbgsizevalueprivate.h"
21
22 #include "gtkcssnumbervalueprivate.h"
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   guint cover :1;
27   guint contain :1;
28   GtkCssValue *x;
29   GtkCssValue *y;
30 };
31
32 static void
33 gtk_css_value_bg_size_free (GtkCssValue *value)
34 {
35   if (value->x)
36     _gtk_css_value_unref (value->x);
37   if (value->y)
38     _gtk_css_value_unref (value->y);
39
40   g_slice_free (GtkCssValue, value);
41 }
42
43 static GtkCssValue *
44 gtk_css_value_bg_size_compute (GtkCssValue             *value,
45                                guint                    property_id,
46                                GtkStyleProviderPrivate *provider,
47                                GtkCssComputedValues    *values,
48                                GtkCssComputedValues    *parent_values,
49                                GtkCssDependencies      *dependencies)
50 {
51   GtkCssValue *x, *y;
52   GtkCssDependencies x_deps, y_deps;
53
54   if (value->x == NULL && value->y == NULL)
55     return _gtk_css_value_ref (value);
56
57   x_deps = y_deps = 0;
58   x = y = NULL;
59
60   if (value->x)
61     x = _gtk_css_value_compute (value->x, property_id, provider, values, parent_values, &x_deps);
62
63   if (value->y)
64     y = _gtk_css_value_compute (value->y, property_id, provider, values, parent_values, &y_deps);
65
66   *dependencies = _gtk_css_dependencies_union (x_deps, y_deps);
67
68   return _gtk_css_bg_size_value_new (value->x ? x : NULL,
69                                      value->y ? y : NULL);
70 }
71
72 static gboolean
73 gtk_css_value_bg_size_equal (const GtkCssValue *value1,
74                              const GtkCssValue *value2)
75 {
76   return value1->cover == value2->cover &&
77          value1->contain == value2->contain &&
78          (value1->x == value2->x ||
79           (value1->x != NULL && value2->x != NULL &&
80            _gtk_css_value_equal (value1->x, value2->x))) &&
81          (value1->y == value2->y ||
82           (value1->y != NULL && value2->y != NULL &&
83            _gtk_css_value_equal (value1->y, value2->y)));
84 }
85
86 static GtkCssValue *
87 gtk_css_value_bg_size_transition (GtkCssValue *start,
88                                   GtkCssValue *end,
89                                   guint        property_id,
90                                   double       progress)
91 {
92   GtkCssValue *x, *y;
93
94   if (start->cover)
95     return end->cover ? _gtk_css_value_ref (end) : NULL;
96   if (start->contain)
97     return end->contain ? _gtk_css_value_ref (end) : NULL;
98
99   if ((start->x != NULL) ^ (end->x != NULL) ||
100       (start->y != NULL) ^ (end->y != NULL))
101     return NULL;
102
103   if (start->x)
104     {
105       x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
106       if (x == NULL)
107         return NULL;
108     }
109   else
110     x = NULL;
111
112   if (start->y)
113     {
114       y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
115       if (y == NULL)
116         {
117           _gtk_css_value_unref (x);
118           return NULL;
119         }
120     }
121   else
122     y = NULL;
123
124   return _gtk_css_bg_size_value_new (x, y);
125 }
126
127 static void
128 gtk_css_value_bg_size_print (const GtkCssValue *value,
129                              GString           *string)
130 {
131   if (value->cover)
132     g_string_append (string, "cover");
133   else if (value->contain)
134     g_string_append (string, "contain");
135   else
136     {
137       if (value->x == NULL)
138         g_string_append (string, "auto");
139       else
140         _gtk_css_value_print (value->x, string);
141
142       if (value->y)
143         {
144           g_string_append_c (string, ' ');
145           _gtk_css_value_print (value->y, string);
146         }
147     }
148 }
149
150 static const GtkCssValueClass GTK_CSS_VALUE_BG_SIZE = {
151   gtk_css_value_bg_size_free,
152   gtk_css_value_bg_size_compute,
153   gtk_css_value_bg_size_equal,
154   gtk_css_value_bg_size_transition,
155   gtk_css_value_bg_size_print
156 };
157
158 static GtkCssValue auto_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, FALSE, NULL, NULL };
159 static GtkCssValue cover_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, TRUE, FALSE, NULL, NULL };
160 static GtkCssValue contain_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, TRUE, NULL, NULL };
161
162 GtkCssValue *
163 _gtk_css_bg_size_value_new (GtkCssValue *x,
164                             GtkCssValue *y)
165 {
166   GtkCssValue *result;
167
168   if (x == NULL && y == NULL)
169     return _gtk_css_value_ref (&auto_singleton);
170
171   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_BG_SIZE);
172   result->x = x;
173   result->y = y;
174
175   return result;
176 }
177
178 GtkCssValue *
179 _gtk_css_bg_size_value_parse (GtkCssParser *parser)
180 {
181   GtkCssValue *x, *y;
182
183   if (_gtk_css_parser_try (parser, "cover", TRUE))
184     return _gtk_css_value_ref (&cover_singleton);
185   else if (_gtk_css_parser_try (parser, "contain", TRUE))
186     return _gtk_css_value_ref (&contain_singleton);
187
188   if (_gtk_css_parser_try (parser, "auto", TRUE))
189     x = NULL;
190   else
191     {
192       x = _gtk_css_number_value_parse (parser,
193                                        GTK_CSS_POSITIVE_ONLY
194                                        | GTK_CSS_PARSE_PERCENT
195                                        | GTK_CSS_PARSE_LENGTH);
196       if (x == NULL)
197         return NULL;
198     }
199
200   if (_gtk_css_parser_try (parser, "auto", TRUE))
201     y = NULL;
202   else if (!_gtk_css_parser_has_number (parser))
203     y = NULL;
204   else
205     {
206       y = _gtk_css_number_value_parse (parser,
207                                        GTK_CSS_POSITIVE_ONLY
208                                        | GTK_CSS_PARSE_PERCENT
209                                        | GTK_CSS_PARSE_LENGTH);
210       if (y == NULL)
211         {
212           _gtk_css_value_unref (x);
213           return NULL;
214         }
215     }
216
217   return _gtk_css_bg_size_value_new (x, y);
218 }
219
220 static void
221 gtk_css_bg_size_compute_size_for_cover_contain (gboolean     cover,
222                                                 GtkCssImage *image,
223                                                 double       width,
224                                                 double       height,
225                                                 double      *concrete_width,
226                                                 double      *concrete_height)
227 {
228   double aspect, image_aspect;
229   
230   image_aspect = _gtk_css_image_get_aspect_ratio (image);
231   if (image_aspect == 0.0)
232     {
233       *concrete_width = width;
234       *concrete_height = height;
235       return;
236     }
237
238   aspect = width / height;
239
240   if ((aspect >= image_aspect && cover) ||
241       (aspect < image_aspect && !cover))
242     {
243       *concrete_width = width;
244       *concrete_height = width / image_aspect;
245     }
246   else
247     {
248       *concrete_height = height;
249       *concrete_width = height * image_aspect;
250     }
251 }
252
253 void
254 _gtk_css_bg_size_value_compute_size (const GtkCssValue *value,
255                                      GtkCssImage       *image,
256                                      double             area_width,
257                                      double             area_height,
258                                      double            *out_width,
259                                      double            *out_height)
260 {
261   g_return_if_fail (value->class == &GTK_CSS_VALUE_BG_SIZE);
262
263   if (value->contain || value->cover)
264     {
265       gtk_css_bg_size_compute_size_for_cover_contain (value->cover,
266                                                       image,
267                                                       area_width, area_height,
268                                                       out_width, out_height);
269     }
270   else
271     {
272       double x, y;
273
274       /* note: 0 does the right thing later for 'auto' */
275       x = value->x ? _gtk_css_number_value_get (value->x, area_width) : 0;
276       y = value->y ? _gtk_css_number_value_get (value->y, area_height) : 0;
277
278       if ((x <= 0 && value->x) ||
279           (y <= 0 && value->y))
280         {
281           *out_width = 0;
282           *out_height = 0;
283         }
284       else
285         {
286           _gtk_css_image_get_concrete_size (image,
287                                             x, y,
288                                             area_width, area_height,
289                                             out_width, out_height);
290         }
291     }
292 }
293