1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Red Hat, Inc.
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.
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.
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/>.
20 #include "gtkcssbgsizevalueprivate.h"
22 #include "gtkcssnumbervalueprivate.h"
33 gtk_css_value_bg_size_free (GtkCssValue *value)
36 _gtk_css_value_unref (value->x);
38 _gtk_css_value_unref (value->y);
40 g_slice_free (GtkCssValue, value);
44 gtk_css_value_bg_size_compute (GtkCssValue *value,
46 GtkStyleProviderPrivate *provider,
47 GtkCssComputedValues *values,
48 GtkCssComputedValues *parent_values,
49 GtkCssDependencies *dependencies)
52 GtkCssDependencies x_deps, y_deps;
54 if (value->x == NULL && value->y == NULL)
55 return _gtk_css_value_ref (value);
61 x = _gtk_css_value_compute (value->x, property_id, provider, values, parent_values, &x_deps);
64 y = _gtk_css_value_compute (value->y, property_id, provider, values, parent_values, &y_deps);
66 *dependencies = _gtk_css_dependencies_union (x_deps, y_deps);
68 return _gtk_css_bg_size_value_new (value->x ? x : NULL,
73 gtk_css_value_bg_size_equal (const GtkCssValue *value1,
74 const GtkCssValue *value2)
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)));
87 gtk_css_value_bg_size_transition (GtkCssValue *start,
95 return end->cover ? _gtk_css_value_ref (end) : NULL;
97 return end->contain ? _gtk_css_value_ref (end) : NULL;
99 if ((start->x != NULL) ^ (end->x != NULL) ||
100 (start->y != NULL) ^ (end->y != NULL))
105 x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
114 y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
117 _gtk_css_value_unref (x);
124 return _gtk_css_bg_size_value_new (x, y);
128 gtk_css_value_bg_size_print (const GtkCssValue *value,
132 g_string_append (string, "cover");
133 else if (value->contain)
134 g_string_append (string, "contain");
137 if (value->x == NULL)
138 g_string_append (string, "auto");
140 _gtk_css_value_print (value->x, string);
144 g_string_append_c (string, ' ');
145 _gtk_css_value_print (value->y, string);
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
158 static GtkCssValue auto_singleton = { >K_CSS_VALUE_BG_SIZE, 1, FALSE, FALSE, NULL, NULL };
159 static GtkCssValue cover_singleton = { >K_CSS_VALUE_BG_SIZE, 1, TRUE, FALSE, NULL, NULL };
160 static GtkCssValue contain_singleton = { >K_CSS_VALUE_BG_SIZE, 1, FALSE, TRUE, NULL, NULL };
163 _gtk_css_bg_size_value_new (GtkCssValue *x,
168 if (x == NULL && y == NULL)
169 return _gtk_css_value_ref (&auto_singleton);
171 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_BG_SIZE);
179 _gtk_css_bg_size_value_parse (GtkCssParser *parser)
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);
188 if (_gtk_css_parser_try (parser, "auto", TRUE))
192 x = _gtk_css_number_value_parse (parser,
193 GTK_CSS_POSITIVE_ONLY
194 | GTK_CSS_PARSE_PERCENT
195 | GTK_CSS_PARSE_LENGTH);
200 if (_gtk_css_parser_try (parser, "auto", TRUE))
202 else if (!_gtk_css_parser_has_number (parser))
206 y = _gtk_css_number_value_parse (parser,
207 GTK_CSS_POSITIVE_ONLY
208 | GTK_CSS_PARSE_PERCENT
209 | GTK_CSS_PARSE_LENGTH);
212 _gtk_css_value_unref (x);
217 return _gtk_css_bg_size_value_new (x, y);
221 gtk_css_bg_size_compute_size_for_cover_contain (gboolean cover,
225 double *concrete_width,
226 double *concrete_height)
228 double aspect, image_aspect;
230 image_aspect = _gtk_css_image_get_aspect_ratio (image);
231 if (image_aspect == 0.0)
233 *concrete_width = width;
234 *concrete_height = height;
238 aspect = width / height;
240 if ((aspect >= image_aspect && cover) ||
241 (aspect < image_aspect && !cover))
243 *concrete_width = width;
244 *concrete_height = width / image_aspect;
248 *concrete_height = height;
249 *concrete_width = height * image_aspect;
254 _gtk_css_bg_size_value_compute_size (const GtkCssValue *value,
261 g_return_if_fail (value->class == >K_CSS_VALUE_BG_SIZE);
263 if (value->contain || value->cover)
265 gtk_css_bg_size_compute_size_for_cover_contain (value->cover,
267 area_width, area_height,
268 out_width, out_height);
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;
278 if ((x <= 0 && value->x) ||
279 (y <= 0 && value->y))
286 _gtk_css_image_get_concrete_size (image,
288 area_width, area_height,
289 out_width, out_height);