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);
232 g_return_if_fail (width > 0);
233 g_return_if_fail (height > 0);
237 klass = GTK_CSS_IMAGE_GET_CLASS (image);
239 klass->draw (image, cr, width, height);
245 _gtk_css_image_print (GtkCssImage *image,
248 GtkCssImageClass *klass;
250 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
251 g_return_if_fail (string != NULL);
253 klass = GTK_CSS_IMAGE_GET_CLASS (image);
255 klass->print (image, string);
258 /* Applies the algorithm outlined in
259 * http://dev.w3.org/csswg/css3-images/#default-sizing
262 _gtk_css_image_get_concrete_size (GtkCssImage *image,
263 double specified_width,
264 double specified_height,
265 double default_width,
266 double default_height,
267 double *concrete_width,
268 double *concrete_height)
270 double image_width, image_height, image_aspect;
272 g_return_if_fail (GTK_IS_CSS_IMAGE (image));
273 g_return_if_fail (specified_width >= 0);
274 g_return_if_fail (specified_height >= 0);
275 g_return_if_fail (default_width > 0);
276 g_return_if_fail (default_height > 0);
277 g_return_if_fail (concrete_width != NULL);
278 g_return_if_fail (concrete_height != NULL);
280 /* If the specified size is a definite width and height,
281 * the concrete object size is given that width and height.
283 if (specified_width && specified_height)
285 *concrete_width = specified_width;
286 *concrete_height = specified_height;
290 image_width = _gtk_css_image_get_width (image);
291 image_height = _gtk_css_image_get_height (image);
292 image_aspect = _gtk_css_image_get_aspect_ratio (image);
294 /* If the specified size has neither a definite width nor height,
295 * and has no additional contraints, the dimensions of the concrete
296 * object size are calculated as follows:
298 if (specified_width == 0.0 && specified_height == 0.0)
300 /* If the object has only an intrinsic aspect ratio,
301 * the concrete object size must have that aspect ratio,
302 * and additionally be as large as possible without either
303 * its height or width exceeding the height or width of the
304 * default object size.
306 if (image_aspect > 0 && image_width == 0 && image_height == 0)
308 if (image_aspect * default_height > default_width)
310 *concrete_width = default_height * image_aspect;
311 *concrete_height = default_height;
315 *concrete_width = default_width;
316 *concrete_height = default_width / image_aspect;
320 /* Otherwise, the width and height of the concrete object
321 * size is the same as the object's intrinsic width and
322 * intrinsic height, if they exist.
323 * If the concrete object size is still missing a width or
324 * height, and the object has an intrinsic aspect ratio,
325 * the missing dimension is calculated from the present
326 * dimension and the intrinsic aspect ratio.
327 * Otherwise, the missing dimension is taken from the default
331 *concrete_width = image_width;
332 else if (image_aspect)
333 *concrete_width = image_height * image_aspect;
335 *concrete_width = default_width;
338 *concrete_height = image_height;
339 else if (image_aspect)
340 *concrete_height = image_width / image_aspect;
342 *concrete_height = default_height;
347 /* If the specified size has only a width or height, but not both,
348 * then the concrete object size is given that specified width or height.
349 * The other dimension is calculated as follows:
350 * If the object has an intrinsic aspect ratio, the missing dimension of
351 * the concrete object size is calculated using the intrinsic aspect-ratio
352 * and the present dimension.
353 * Otherwise, if the missing dimension is present in the object's intrinsic
354 * dimensions, the missing dimension is taken from the object's intrinsic
356 * Otherwise, the missing dimension of the concrete object size is taken
357 * from the default object size.
361 *concrete_width = specified_width;
363 *concrete_height = specified_width / image_aspect;
364 else if (image_height)
365 *concrete_height = image_height;
367 *concrete_height = default_height;
371 *concrete_height = specified_height;
373 *concrete_width = specified_height * image_aspect;
374 else if (image_width)
375 *concrete_width = image_width;
377 *concrete_width = default_width;
382 _gtk_css_image_get_surface (GtkCssImage *image,
383 cairo_surface_t *target,
387 cairo_surface_t *result;
390 g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), NULL);
391 g_return_val_if_fail (surface_width > 0, NULL);
392 g_return_val_if_fail (surface_height > 0, NULL);
395 result = cairo_surface_create_similar (target,
396 CAIRO_CONTENT_COLOR_ALPHA,
400 result = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
404 cr = cairo_create (result);
405 _gtk_css_image_draw (image, cr, surface_width, surface_height);
412 gtk_css_image_get_parser_type (GtkCssParser *parser)
414 static const struct {
416 GType (* type_func) (void);
418 { "url", _gtk_css_image_url_get_type },
419 { "-gtk-gradient", _gtk_css_image_gradient_get_type },
420 { "-gtk-win32-theme-part", _gtk_css_image_win32_get_type },
421 { "linear-gradient", _gtk_css_image_linear_get_type },
422 { "repeating-linear-gradient", _gtk_css_image_linear_get_type },
423 { "cross-fade", _gtk_css_image_cross_fade_get_type }
427 for (i = 0; i < G_N_ELEMENTS (image_types); i++)
429 if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
430 return image_types[i].type_func ();
433 return G_TYPE_INVALID;
437 * _gtk_css_image_can_parse:
438 * @parser: a css parser
440 * Checks if the parser can potentially parse the given stream as an
441 * image from looking at the first token of @parser. This is useful for
442 * implementing shorthand properties. A successful parse of an image
443 * can not be guaranteed.
445 * Returns: %TURE if it looks like an image.
448 _gtk_css_image_can_parse (GtkCssParser *parser)
450 return gtk_css_image_get_parser_type (parser) != G_TYPE_INVALID;
454 _gtk_css_image_new_parse (GtkCssParser *parser)
456 GtkCssImageClass *klass;
460 g_return_val_if_fail (parser != NULL, NULL);
462 image_type = gtk_css_image_get_parser_type (parser);
463 if (image_type == G_TYPE_INVALID)
465 _gtk_css_parser_error (parser, "Not a valid image");
469 image = g_object_new (image_type, NULL);
471 klass = GTK_CSS_IMAGE_GET_CLASS (image);
472 if (!klass->parse (image, parser))
474 g_object_unref (image);