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_equal (const GtkCssValue *value1,
45 const GtkCssValue *value2)
47 return value1->cover == value2->cover &&
48 value2->contain == value2->contain &&
49 (value1->x == value2->x ||
50 (value1->x != NULL && value2->x != NULL &&
51 _gtk_css_value_equal (value1->x, value2->x))) &&
52 (value1->y == value2->y ||
53 (value1->y != NULL && value2->y != NULL &&
54 _gtk_css_value_equal (value1->y, value2->y)));
58 gtk_css_value_bg_size_transition (GtkCssValue *start,
65 return end->cover ? _gtk_css_value_ref (end) : NULL;
67 return end->contain ? _gtk_css_value_ref (end) : NULL;
69 if ((start->x != NULL) ^ (end->x != NULL) ||
70 (start->y != NULL) ^ (end->y != NULL))
75 x = _gtk_css_value_transition (start->x, end->x, progress);
84 y = _gtk_css_value_transition (start->y, end->y, progress);
87 _gtk_css_value_unref (x);
94 return _gtk_css_bg_size_value_new (x, y);
98 gtk_css_value_bg_size_print (const GtkCssValue *value,
102 g_string_append (string, "cover");
103 else if (value->contain)
104 g_string_append (string, "contain");
107 if (value->x == NULL)
108 g_string_append (string, "auto");
110 _gtk_css_value_print (value->x, string);
114 g_string_append_c (string, ' ');
115 _gtk_css_value_print (value->y, string);
120 static const GtkCssValueClass GTK_CSS_VALUE_BG_SIZE = {
121 gtk_css_value_bg_size_free,
122 gtk_css_value_bg_size_equal,
123 gtk_css_value_bg_size_transition,
124 gtk_css_value_bg_size_print
127 static GtkCssValue auto_singleton = { >K_CSS_VALUE_BG_SIZE, 1, FALSE, FALSE, NULL, NULL };
128 static GtkCssValue cover_singleton = { >K_CSS_VALUE_BG_SIZE, 1, TRUE, FALSE, NULL, NULL };
129 static GtkCssValue contain_singleton = { >K_CSS_VALUE_BG_SIZE, 1, FALSE, TRUE, NULL, NULL };
132 _gtk_css_bg_size_value_new (GtkCssValue *x,
137 if (x == NULL && y == NULL)
138 return _gtk_css_value_ref (&auto_singleton);
140 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_BG_SIZE);
148 _gtk_css_bg_size_value_parse (GtkCssParser *parser)
152 if (_gtk_css_parser_try (parser, "cover", TRUE))
153 return _gtk_css_value_ref (&cover_singleton);
154 else if (_gtk_css_parser_try (parser, "contain", TRUE))
155 return _gtk_css_value_ref (&contain_singleton);
157 if (_gtk_css_parser_try (parser, "auto", TRUE))
161 x = _gtk_css_number_value_parse (parser,
162 GTK_CSS_POSITIVE_ONLY
163 | GTK_CSS_PARSE_PERCENT
164 | GTK_CSS_PARSE_LENGTH);
169 if (_gtk_css_parser_try (parser, "auto", TRUE))
171 else if (!_gtk_css_parser_has_number (parser))
175 y = _gtk_css_number_value_parse (parser,
176 GTK_CSS_POSITIVE_ONLY
177 | GTK_CSS_PARSE_PERCENT
178 | GTK_CSS_PARSE_LENGTH);
181 _gtk_css_value_unref (x);
186 return _gtk_css_bg_size_value_new (x, y);
190 gtk_css_bg_size_compute_size_for_cover_contain (gboolean cover,
194 double *concrete_width,
195 double *concrete_height)
197 double aspect, image_aspect;
199 image_aspect = _gtk_css_image_get_aspect_ratio (image);
200 if (image_aspect == 0.0)
202 *concrete_width = width;
203 *concrete_height = height;
207 aspect = width / height;
209 if ((aspect >= image_aspect && cover) ||
210 (aspect < image_aspect && !cover))
212 *concrete_width = width;
213 *concrete_height = width / image_aspect;
217 *concrete_height = height;
218 *concrete_width = height * image_aspect;
223 _gtk_css_bg_size_value_compute_size (const GtkCssValue *value,
230 g_return_if_fail (value->class == >K_CSS_VALUE_BG_SIZE);
232 if (value->contain || value->cover)
233 gtk_css_bg_size_compute_size_for_cover_contain (value->cover,
235 area_width, area_height,
236 out_width, out_height);
238 _gtk_css_image_get_concrete_size (image,
239 /* note: 0 does the right thing here for 'auto' */
240 value->x ? _gtk_css_number_value_get (value->x, area_width) : 0,
241 value->y ? _gtk_css_number_value_get (value->y, area_height) : 0,
242 area_width, area_height,
243 out_width, out_height);
247 _gtk_css_bg_size_value_compute (GtkCssValue *value,
248 GtkStyleContext *context)
250 g_return_val_if_fail (value->class == >K_CSS_VALUE_BG_SIZE, NULL);
252 if (value->x == NULL && value->y == NULL)
253 return _gtk_css_value_ref (value);
255 return _gtk_css_bg_size_value_new (value->x ? _gtk_css_number_value_compute (value->x, context) : NULL,
256 value->y ? _gtk_css_number_value_compute (value->y, context) : NULL);