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 #define GDK_DISABLE_DEPRECATION_WARNINGS
24 #include "gtkcssimagegradientprivate.h"
26 #include "gtkcssprovider.h"
27 #include "gtkgradientprivate.h"
28 #include "gtksymboliccolorprivate.h"
30 G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)
33 gtk_css_image_gradient_compute (GtkCssImage *image,
35 GtkStyleProviderPrivate *provider,
36 GtkCssComputedValues *values,
37 GtkCssComputedValues *parent_values,
38 GtkCssDependencies *dependencies)
40 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
41 GtkCssImageGradient *copy;
43 if (gradient->pattern)
44 return g_object_ref (gradient);
46 copy = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
47 copy->gradient = gtk_gradient_ref (gradient->gradient);
48 copy->pattern = _gtk_gradient_resolve_full (copy->gradient, provider, values, parent_values, dependencies);
50 return GTK_CSS_IMAGE (copy);
53 static cairo_pattern_t *
54 fade_pattern (cairo_pattern_t *pattern,
57 double x0, y0, x1, y1, r0, r1;
58 cairo_pattern_t *result;
61 switch (cairo_pattern_get_type (pattern))
63 case CAIRO_PATTERN_TYPE_LINEAR:
64 cairo_pattern_get_linear_points (pattern, &x0, &y0, &x1, &y1);
65 result = cairo_pattern_create_linear (x0, y0, x1, y1);
67 case CAIRO_PATTERN_TYPE_RADIAL:
68 cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
69 result = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
72 g_return_val_if_reached (NULL);
75 cairo_pattern_get_color_stop_count (pattern, &n);
76 for (i = 0; i < n; i++)
80 cairo_pattern_get_color_stop_rgba (pattern, i, &o, &r, &g, &b, &a);
81 cairo_pattern_add_color_stop_rgba (result, o, r, g, b, a * opacity);
87 static cairo_pattern_t *
88 transition_pattern (cairo_pattern_t *start,
92 double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
93 cairo_pattern_t *result;
96 progress = CLAMP (progress, 0.0, 1.0);
99 return fade_pattern (start, 1.0 - progress);
101 g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));
103 switch (cairo_pattern_get_type (start))
105 case CAIRO_PATTERN_TYPE_LINEAR:
106 cairo_pattern_get_linear_points (start, &sx0, &sy0, &sx1, &sy1);
107 cairo_pattern_get_linear_points (end, &ex0, &ey0, &ex1, &ey1);
108 result = cairo_pattern_create_linear ((1 - progress) * sx0 + progress * ex0,
109 (1 - progress) * sx1 + progress * ex1,
110 (1 - progress) * sy0 + progress * ey0,
111 (1 - progress) * sy1 + progress * ey1);
113 case CAIRO_PATTERN_TYPE_RADIAL:
114 cairo_pattern_get_radial_circles (start, &sx0, &sy0, &sr0, &sx1, &sy1, &sr1);
115 cairo_pattern_get_radial_circles (end, &ex0, &ey0, &er0, &ex1, &ey1, &er1);
116 result = cairo_pattern_create_radial ((1 - progress) * sx0 + progress * ex0,
117 (1 - progress) * sy0 + progress * ey0,
118 (1 - progress) * sr0 + progress * er0,
119 (1 - progress) * sx1 + progress * ex1,
120 (1 - progress) * sy1 + progress * ey1,
121 (1 - progress) * sr1 + progress * er1);
124 g_return_val_if_reached (NULL);
127 cairo_pattern_get_color_stop_count (start, &n);
128 for (i = 0; i < n; i++)
130 double so, sr, sg, sb, sa, eo, er, eg, eb, ea;
132 cairo_pattern_get_color_stop_rgba (start, i, &so, &sr, &sg, &sb, &sa);
133 cairo_pattern_get_color_stop_rgba (end, i, &eo, &er, &eg, &eb, &ea);
135 cairo_pattern_add_color_stop_rgba (result,
136 (1 - progress) * so + progress * eo,
137 (1 - progress) * sr + progress * er,
138 (1 - progress) * sg + progress * eg,
139 (1 - progress) * sb + progress * eb,
140 (1 - progress) * sa + progress * ea);
147 gtk_css_image_gradient_transition (GtkCssImage *start_image,
148 GtkCssImage *end_image,
152 GtkGradient *start_gradient, *end_gradient, *gradient;
153 cairo_pattern_t *start_pattern, *end_pattern;
154 GtkCssImageGradient *result;
156 start_gradient = GTK_CSS_IMAGE_GRADIENT (start_image)->gradient;
157 start_pattern = GTK_CSS_IMAGE_GRADIENT (start_image)->pattern;
158 if (end_image == NULL)
165 if (!GTK_IS_CSS_IMAGE_GRADIENT (end_image))
166 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
168 end_gradient = GTK_CSS_IMAGE_GRADIENT (end_image)->gradient;
169 end_pattern = GTK_CSS_IMAGE_GRADIENT (end_image)->pattern;
172 gradient = _gtk_gradient_transition (start_gradient, end_gradient, property_id, progress);
173 if (gradient == NULL)
174 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
176 result = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
177 result->gradient = gradient;
178 result->pattern = transition_pattern (start_pattern, end_pattern, progress);
180 return GTK_CSS_IMAGE (result);
184 gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
189 cairo_pattern_t *pattern = image->pattern;
190 double x0, y0, x1, y1, r0, r1;
191 GdkRGBA color0, color1;
192 double offset0, offset1;
195 if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
197 if (cairo_pattern_get_extend (pattern) != CAIRO_EXTEND_PAD)
200 cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
207 cairo_pattern_get_color_stop_count (pattern, &n_stops);
211 cairo_pattern_get_color_stop_rgba (pattern, 0, &offset0, &color0.red, &color0.green, &color0.blue, &color0.alpha);
212 cairo_pattern_get_color_stop_rgba (pattern, 1, &offset1, &color1.red, &color1.green, &color1.blue, &color1.alpha);
213 if (offset0 != offset1)
216 cairo_scale (cr, width, height);
218 cairo_rectangle (cr, 0, 0, 1, 1);
221 gdk_cairo_set_source_rgba (cr, &color1);
224 gdk_cairo_set_source_rgba (cr, &color0);
225 cairo_arc (cr, x1, y1, r1 * offset1, 0, 2 * G_PI);
232 gtk_css_image_gradient_draw (GtkCssImage *image,
237 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
239 if (!gradient->pattern)
241 g_warning ("trying to paint unresolved gradient");
245 if (gtk_css_image_gradient_draw_circle (gradient, cr, width, height))
248 cairo_scale (cr, width, height);
250 cairo_rectangle (cr, 0, 0, 1, 1);
251 cairo_set_source (cr, gradient->pattern);
256 gtk_css_image_gradient_parse (GtkCssImage *image,
257 GtkCssParser *parser)
259 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
261 gradient->gradient = _gtk_gradient_parse (parser);
263 return gradient->gradient != NULL;
267 gtk_css_image_gradient_print (GtkCssImage *image,
270 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
273 s = gtk_gradient_to_string (gradient->gradient);
274 g_string_append (string, s);
279 gtk_css_image_gradient_dispose (GObject *object)
281 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (object);
283 if (gradient->gradient)
285 gtk_gradient_unref (gradient->gradient);
286 gradient->gradient = NULL;
288 if (gradient->pattern)
290 cairo_pattern_destroy (gradient->pattern);
291 gradient->pattern = NULL;
294 G_OBJECT_CLASS (_gtk_css_image_gradient_parent_class)->dispose (object);
298 _gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
300 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
301 GObjectClass *object_class = G_OBJECT_CLASS (klass);
303 image_class->compute = gtk_css_image_gradient_compute;
304 image_class->transition = gtk_css_image_gradient_transition;
305 image_class->draw = gtk_css_image_gradient_draw;
306 image_class->parse = gtk_css_image_gradient_parse;
307 image_class->print = gtk_css_image_gradient_print;
309 object_class->dispose = gtk_css_image_gradient_dispose;
313 _gtk_css_image_gradient_init (GtkCssImageGradient *image_gradient)
318 _gtk_gradient_parse (GtkCssParser *parser)
320 GtkGradient *gradient;
321 cairo_pattern_type_t type;
325 g_return_val_if_fail (parser != NULL, NULL);
327 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
329 _gtk_css_parser_error (parser,
330 "Expected '-gtk-gradient'");
334 if (!_gtk_css_parser_try (parser, "(", TRUE))
336 _gtk_css_parser_error (parser,
337 "Expected '(' after '-gtk-gradient'");
341 /* Parse gradient type */
342 if (_gtk_css_parser_try (parser, "linear", TRUE))
343 type = CAIRO_PATTERN_TYPE_LINEAR;
344 else if (_gtk_css_parser_try (parser, "radial", TRUE))
345 type = CAIRO_PATTERN_TYPE_RADIAL;
348 _gtk_css_parser_error (parser,
349 "Gradient type must be 'radial' or 'linear'");
353 /* Parse start/stop position parameters */
354 for (i = 0; i < 2; i++)
356 if (! _gtk_css_parser_try (parser, ",", TRUE))
358 _gtk_css_parser_error (parser,
363 if (_gtk_css_parser_try (parser, "left", TRUE))
365 else if (_gtk_css_parser_try (parser, "right", TRUE))
367 else if (_gtk_css_parser_try (parser, "center", TRUE))
369 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
371 _gtk_css_parser_error (parser,
372 "Expected a valid X coordinate");
376 if (_gtk_css_parser_try (parser, "top", TRUE))
377 coords[i * 3 + 1] = 0;
378 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
379 coords[i * 3 + 1] = 1;
380 else if (_gtk_css_parser_try (parser, "center", TRUE))
381 coords[i * 3 + 1] = 0.5;
382 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
384 _gtk_css_parser_error (parser,
385 "Expected a valid Y coordinate");
389 if (type == CAIRO_PATTERN_TYPE_RADIAL)
392 if (! _gtk_css_parser_try (parser, ",", TRUE))
394 _gtk_css_parser_error (parser,
399 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
401 _gtk_css_parser_error (parser,
402 "Expected a numer for the radius");
408 if (type == CAIRO_PATTERN_TYPE_LINEAR)
409 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
411 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
412 coords[3], coords[4], coords[5]);
414 while (_gtk_css_parser_try (parser, ",", TRUE))
416 GtkSymbolicColor *color;
419 if (_gtk_css_parser_try (parser, "from", TRUE))
423 if (!_gtk_css_parser_try (parser, "(", TRUE))
425 gtk_gradient_unref (gradient);
426 _gtk_css_parser_error (parser,
432 else if (_gtk_css_parser_try (parser, "to", TRUE))
436 if (!_gtk_css_parser_try (parser, "(", TRUE))
438 gtk_gradient_unref (gradient);
439 _gtk_css_parser_error (parser,
445 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
447 if (!_gtk_css_parser_try (parser, "(", TRUE))
449 gtk_gradient_unref (gradient);
450 _gtk_css_parser_error (parser,
455 if (!_gtk_css_parser_try_double (parser, &position))
457 gtk_gradient_unref (gradient);
458 _gtk_css_parser_error (parser,
459 "Expected a valid number");
463 if (!_gtk_css_parser_try (parser, ",", TRUE))
465 gtk_gradient_unref (gradient);
466 _gtk_css_parser_error (parser,
473 gtk_gradient_unref (gradient);
474 _gtk_css_parser_error (parser,
475 "Not a valid color-stop definition");
479 color = _gtk_css_symbolic_value_new (parser);
482 gtk_gradient_unref (gradient);
486 gtk_gradient_add_color_stop (gradient, position, color);
487 gtk_symbolic_color_unref (color);
489 if (!_gtk_css_parser_try (parser, ")", TRUE))
491 gtk_gradient_unref (gradient);
492 _gtk_css_parser_error (parser,
498 if (!_gtk_css_parser_try (parser, ")", TRUE))
500 gtk_gradient_unref (gradient);
501 _gtk_css_parser_error (parser,