2 * Copyright © 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.1 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/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcssimageprivate.h"
24 #include "gtkcsscomputedvaluesprivate.h"
26 /* for the types only */
27 #include "gtk/gtkcssimagecrossfadeprivate.h"
28 #include "gtk/gtkcssimagegradientprivate.h"
29 #include "gtk/gtkcssimagelinearprivate.h"
30 #include "gtk/gtkcssimageurlprivate.h"
31 #include "gtk/gtkcssimagewin32private.h"
33 G_DEFINE_ABSTRACT_TYPE (GtkCssImage, _gtk_css_image, G_TYPE_OBJECT)
36 gtk_css_image_real_get_width (GtkCssImage *image)
42 gtk_css_image_real_get_height (GtkCssImage *image)
48 gtk_css_image_real_get_aspect_ratio (GtkCssImage *image)
52 width = _gtk_css_image_get_width (image);
53 height = _gtk_css_image_get_height (image);
56 return (double) width / height;
62 gtk_css_image_real_compute (GtkCssImage *image,
64 GtkStyleProviderPrivate *provider,
65 GtkCssComputedValues *values,
66 GtkCssComputedValues *parent_values,
67 GtkCssDependencies *dependencies)
69 return g_object_ref (image);
73 gtk_css_image_real_equal (GtkCssImage *image1,
80 gtk_css_image_real_transition (GtkCssImage *start,
86 return g_object_ref (start);
87 else if (progress >= 1.0)
88 return end ? g_object_ref (end) : NULL;
90 return _gtk_css_image_cross_fade_new (start, end, progress);
94 _gtk_css_image_class_init (GtkCssImageClass *klass)
96 klass->get_width = gtk_css_image_real_get_width;
97 klass->get_height = gtk_css_image_real_get_height;
98 klass->get_aspect_ratio = gtk_css_image_real_get_aspect_ratio;
99 klass->compute = gtk_css_image_real_compute;
100 klass->equal = gtk_css_image_real_equal;
101 klass->transition = gtk_css_image_real_transition;
105 _gtk_css_image_init (GtkCssImage *image)
110 _gtk_css_image_get_width (GtkCssImage *image)
112 GtkCssImageClass *klass;
114 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
116 klass = GTK_CSS_IMAGE_GET_CLASS (image);
118 return klass->get_width (image);
122 _gtk_css_image_get_height (GtkCssImage *image)
124 GtkCssImageClass *klass;
126 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
128 klass = GTK_CSS_IMAGE_GET_CLASS (image);
130 return klass->get_height (image);
134 _gtk_css_image_get_aspect_ratio (GtkCssImage *image)
136 GtkCssImageClass *klass;
138 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), 0);
140 klass = GTK_CSS_IMAGE_GET_CLASS (image);
142 return klass->get_aspect_ratio (image);
146 _gtk_css_image_compute (GtkCssImage *image,
148 GtkStyleProviderPrivate *provider,
149 GtkCssComputedValues *values,
150 GtkCssComputedValues *parent_values,
151 GtkCssDependencies *dependencies)
153 GtkCssDependencies unused;
154 GtkCssImageClass *klass;
156 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
157 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
158 g_return_val_if_fail (parent_values == NULL || GTK_IS_CSS_COMPUTED_VALUES (parent_values), NULL);
160 if (dependencies == NULL)
161 dependencies = &unused;
164 klass = GTK_CSS_IMAGE_GET_CLASS (image);
166 return klass->compute (image, property_id, provider, values, parent_values, dependencies);
170 _gtk_css_image_transition (GtkCssImage *start,
175 GtkCssImageClass *klass;
177 g_return_val_if_fail (start == NULL || GTK_IS_CSS_IMAGE (start), NULL);
178 g_return_val_if_fail (end == NULL || GTK_IS_CSS_IMAGE (end), NULL);
180 progress = CLAMP (progress, 0.0, 1.0);
190 progress = 1.0 - progress;
194 klass = GTK_CSS_IMAGE_GET_CLASS (start);
196 return klass->transition (start, end, property_id, progress);
200 _gtk_css_image_equal (GtkCssImage *image1,
203 GtkCssImageClass *klass;
205 g_return_val_if_fail (image1 == NULL || GTK_IS_CSS_IMAGE (image1), FALSE);
206 g_return_val_if_fail (image2 == NULL || GTK_IS_CSS_IMAGE (image2), FALSE);
208 if (image1 == image2)
211 if (image1 == NULL || image2 == NULL)
214 if (G_OBJECT_TYPE (image1) != G_OBJECT_TYPE (image2))
217 klass = GTK_CSS_IMAGE_GET_CLASS (image1);
219 return klass->equal (image1, image2);
223 _gtk_css_image_draw (GtkCssImage *image,
228 GtkCssImageClass *klass;
230 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
231 g_return_if_fail (cr != NULL);
235 klass = GTK_CSS_IMAGE_GET_CLASS (image);
237 klass->draw (image, cr, width, height);
243 _gtk_css_image_print (GtkCssImage *image,
246 GtkCssImageClass *klass;
248 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
249 g_return_if_fail (string != NULL);
251 klass = GTK_CSS_IMAGE_GET_CLASS (image);
253 klass->print (image, string);
256 /* Applies the algorithm outlined in
257 * http://dev.w3.org/csswg/css3-images/#default-sizing
260 _gtk_css_image_get_concrete_size (GtkCssImage *image,
261 double specified_width,
262 double specified_height,
263 double default_width,
264 double default_height,
265 double *concrete_width,
266 double *concrete_height)
268 double image_width, image_height, image_aspect;
270 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
271 g_return_if_fail (specified_width >= 0);
272 g_return_if_fail (specified_height >= 0);
273 g_return_if_fail (default_width > 0);
274 g_return_if_fail (default_height > 0);
275 g_return_if_fail (concrete_width != NULL);
276 g_return_if_fail (concrete_height != NULL);
278 /* If the specified size is a definite width and height,
279 * the concrete object size is given that width and height.
281 if (specified_width && specified_height)
283 *concrete_width = specified_width;
284 *concrete_height = specified_height;
288 image_width = _gtk_css_image_get_width (image);
289 image_height = _gtk_css_image_get_height (image);
290 image_aspect = _gtk_css_image_get_aspect_ratio (image);
292 /* If the specified size has neither a definite width nor height,
293 * and has no additional contraints, the dimensions of the concrete
294 * object size are calculated as follows:
296 if (specified_width == 0.0 && specified_height == 0.0)
298 /* If the object has only an intrinsic aspect ratio,
299 * the concrete object size must have that aspect ratio,
300 * and additionally be as large as possible without either
301 * its height or width exceeding the height or width of the
302 * default object size.
304 if (image_aspect > 0 && image_width == 0 && image_height == 0)
306 if (image_aspect * default_height > default_width)
308 *concrete_width = default_height * image_aspect;
309 *concrete_height = default_height;
313 *concrete_width = default_width;
314 *concrete_height = default_width / image_aspect;
318 /* Otherwise, the width and height of the concrete object
319 * size is the same as the object's intrinsic width and
320 * intrinsic height, if they exist.
321 * If the concrete object size is still missing a width or
322 * height, and the object has an intrinsic aspect ratio,
323 * the missing dimension is calculated from the present
324 * dimension and the intrinsic aspect ratio.
325 * Otherwise, the missing dimension is taken from the default
329 *concrete_width = image_width;
330 else if (image_aspect)
331 *concrete_width = image_height * image_aspect;
333 *concrete_width = default_width;
336 *concrete_height = image_height;
337 else if (image_aspect)
338 *concrete_height = image_width / image_aspect;
340 *concrete_height = default_height;
345 /* If the specified size has only a width or height, but not both,
346 * then the concrete object size is given that specified width or height.
347 * The other dimension is calculated as follows:
348 * If the object has an intrinsic aspect ratio, the missing dimension of
349 * the concrete object size is calculated using the intrinsic aspect-ratio
350 * and the present dimension.
351 * Otherwise, if the missing dimension is present in the object's intrinsic
352 * dimensions, the missing dimension is taken from the object's intrinsic
354 * Otherwise, the missing dimension of the concrete object size is taken
355 * from the default object size.
359 *concrete_width = specified_width;
361 *concrete_height = specified_width / image_aspect;
362 else if (image_height)
363 *concrete_height = image_height;
365 *concrete_height = default_height;
369 *concrete_height = specified_height;
371 *concrete_width = specified_height * image_aspect;
372 else if (image_width)
373 *concrete_width = image_width;
375 *concrete_width = default_width;
380 _gtk_css_image_get_surface (GtkCssImage *image,
381 cairo_surface_t *target,
385 cairo_surface_t *result;
388 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
389 g_return_val_if_fail (surface_width > 0, NULL);
390 g_return_val_if_fail (surface_height > 0, NULL);
393 result = cairo_surface_create_similar (target,
394 CAIRO_CONTENT_COLOR_ALPHA,
398 result = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
402 cr = cairo_create (result);
403 _gtk_css_image_draw (image, cr, surface_width, surface_height);
410 gtk_css_image_get_parser_type (GtkCssParser *parser)
412 static const struct {
414 GType (* type_func) (void);
416 { "url", _gtk_css_image_url_get_type },
417 { "-gtk-gradient", _gtk_css_image_gradient_get_type },
418 { "-gtk-win32-theme-part", _gtk_css_image_win32_get_type },
419 { "linear-gradient", _gtk_css_image_linear_get_type },
420 { "repeating-linear-gradient", _gtk_css_image_linear_get_type },
421 { "cross-fade", _gtk_css_image_cross_fade_get_type }
425 for (i = 0; i < G_N_ELEMENTS (image_types); i++)
427 if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
428 return image_types[i].type_func ();
431 return G_TYPE_INVALID;
435 * _gtk_css_image_can_parse:
436 * @parser: a css parser
438 * Checks if the parser can potentially parse the given stream as an
439 * image from looking at the first token of @parser. This is useful for
440 * implementing shorthand properties. A successful parse of an image
441 * can not be guaranteed.
443 * Returns: %TURE if it looks like an image.
446 _gtk_css_image_can_parse (GtkCssParser *parser)
448 return gtk_css_image_get_parser_type (parser) != G_TYPE_INVALID;
452 _gtk_css_image_new_parse (GtkCssParser *parser)
454 GtkCssImageClass *klass;
458 g_return_val_if_fail (parser != NULL, NULL);
460 image_type = gtk_css_image_get_parser_type (parser);
461 if (image_type == G_TYPE_INVALID)
463 _gtk_css_parser_error (parser, "Not a valid image");
467 image = g_object_new (image_type, NULL);
469 klass = GTK_CSS_IMAGE_GET_CLASS (image);
470 if (!klass->parse (image, parser))
472 g_object_unref (image);