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 "gtkcssimagegradientprivate.h"
24 #include "gtkcssprovider.h"
25 #include "gtkgradientprivate.h"
26 #include "gtksymboliccolorprivate.h"
28 G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)
31 gtk_css_image_gradient_compute (GtkCssImage *image,
33 GtkStyleProviderPrivate *provider,
34 GtkCssComputedValues *values,
35 GtkCssComputedValues *parent_values,
36 GtkCssDependencies *dependencies)
38 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
39 GtkCssImageGradient *copy;
41 if (gradient->pattern)
42 return g_object_ref (gradient);
44 copy = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
45 copy->gradient = gtk_gradient_ref (gradient->gradient);
46 copy->pattern = _gtk_gradient_resolve_full (copy->gradient, provider, values, parent_values, dependencies);
48 return GTK_CSS_IMAGE (copy);
52 fade_pattern (cairo_pattern_t *pattern,
55 double x0, y0, x1, y1, r0, r1;
56 cairo_pattern_t *result;
59 switch (cairo_pattern_get_type (pattern))
61 case CAIRO_PATTERN_TYPE_LINEAR:
62 cairo_pattern_get_linear_points (pattern, &x0, &y0, &x1, &y1);
63 result = cairo_pattern_create_linear (x0, y0, x1, y1);
65 case CAIRO_PATTERN_TYPE_RADIAL:
66 cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
67 result = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
70 g_return_val_if_reached (NULL);
73 cairo_pattern_get_color_stop_count (pattern, &n);
74 for (i = 0; i < n; i++)
78 cairo_pattern_get_color_stop_rgba (pattern, i, &o, &r, &g, &b, &a);
79 cairo_pattern_add_color_stop_rgba (result, o, r, g, b, a * opacity);
86 transition_pattern (cairo_pattern_t *start,
90 double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
91 cairo_pattern_t *result;
94 progress = CLAMP (progress, 0.0, 1.0);
97 return fade_pattern (start, 1.0 - progress);
99 g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));
101 switch (cairo_pattern_get_type (start))
103 case CAIRO_PATTERN_TYPE_LINEAR:
104 cairo_pattern_get_linear_points (start, &sx0, &sy0, &sx1, &sy1);
105 cairo_pattern_get_linear_points (start, &ex0, &ey0, &ex1, &ey1);
106 result = cairo_pattern_create_linear ((1 - progress) * sx0 + progress * ex0,
107 (1 - progress) * sx1 + progress * ex1,
108 (1 - progress) * sy0 + progress * ey0,
109 (1 - progress) * sy1 + progress * ey1);
111 case CAIRO_PATTERN_TYPE_RADIAL:
112 cairo_pattern_get_radial_circles (start, &sx0, &sy0, &sr0, &sx1, &sy1, &sr1);
113 cairo_pattern_get_radial_circles (start, &ex0, &ey0, &er0, &ex1, &ey1, &er1);
114 result = cairo_pattern_create_radial ((1 - progress) * sx0 + progress * ex0,
115 (1 - progress) * sy0 + progress * ey0,
116 (1 - progress) * sr0 + progress * er0,
117 (1 - progress) * sx1 + progress * ex1,
118 (1 - progress) * sy1 + progress * ey1,
119 (1 - progress) * sr1 + progress * er1);
122 g_return_val_if_reached (NULL);
125 cairo_pattern_get_color_stop_count (start, &n);
126 for (i = 0; i < n; i++)
128 double so, sr, sg, sb, sa, eo, er, eg, eb, ea;
130 cairo_pattern_get_color_stop_rgba (start, i, &so, &sr, &sg, &sb, &sa);
131 cairo_pattern_get_color_stop_rgba (start, i, &eo, &er, &eg, &eb, &ea);
133 cairo_pattern_add_color_stop_rgba (result,
134 (1 - progress) * so + progress * eo,
135 (1 - progress) * sr + progress * er,
136 (1 - progress) * sg + progress * eg,
137 (1 - progress) * sb + progress * eb,
138 (1 - progress) * sa + progress * ea);
145 gtk_css_image_gradient_transition (GtkCssImage *start_image,
146 GtkCssImage *end_image,
150 GtkGradient *start_gradient, *end_gradient, *gradient;
151 cairo_pattern_t *start_pattern, *end_pattern;
152 GtkCssImageGradient *result;
154 start_gradient = GTK_CSS_IMAGE_GRADIENT (start_image)->gradient;
155 start_pattern = GTK_CSS_IMAGE_GRADIENT (start_image)->pattern;
156 if (end_image == NULL)
163 if (!GTK_IS_CSS_IMAGE_GRADIENT (end_image))
164 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
166 end_gradient = GTK_CSS_IMAGE_GRADIENT (end_image)->gradient;
167 end_pattern = GTK_CSS_IMAGE_GRADIENT (end_image)->pattern;
170 gradient = _gtk_gradient_transition (start_gradient, end_gradient, property_id, progress);
171 if (gradient == NULL)
172 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
174 result = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
175 result->gradient = gradient;
176 result->pattern = transition_pattern (start_pattern, end_pattern, progress);
178 return GTK_CSS_IMAGE (result);
182 gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
187 cairo_pattern_t *pattern = image->pattern;
188 double x0, y0, x1, y1, r0, r1;
189 GdkRGBA color0, color1;
190 double offset0, offset1;
193 if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
195 if (cairo_pattern_get_extend (pattern) != CAIRO_EXTEND_PAD)
198 cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
205 cairo_pattern_get_color_stop_count (pattern, &n_stops);
209 cairo_pattern_get_color_stop_rgba (pattern, 0, &offset0, &color0.red, &color0.green, &color0.blue, &color0.alpha);
210 cairo_pattern_get_color_stop_rgba (pattern, 1, &offset1, &color1.red, &color1.green, &color1.blue, &color1.alpha);
211 if (offset0 != offset1)
214 cairo_scale (cr, width, height);
216 cairo_rectangle (cr, 0, 0, 1, 1);
219 gdk_cairo_set_source_rgba (cr, &color1);
222 gdk_cairo_set_source_rgba (cr, &color0);
223 cairo_arc (cr, x1, y1, r1 * offset1, 0, 2 * G_PI);
230 gtk_css_image_gradient_draw (GtkCssImage *image,
235 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
237 if (!gradient->pattern)
239 g_warning ("trying to paint unresolved gradient");
243 if (gtk_css_image_gradient_draw_circle (gradient, cr, width, height))
246 cairo_scale (cr, width, height);
248 cairo_rectangle (cr, 0, 0, 1, 1);
249 cairo_set_source (cr, gradient->pattern);
254 gtk_css_image_gradient_parse (GtkCssImage *image,
255 GtkCssParser *parser)
257 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
259 gradient->gradient = _gtk_gradient_parse (parser);
261 return gradient->gradient != NULL;
265 gtk_css_image_gradient_print (GtkCssImage *image,
268 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
271 s = gtk_gradient_to_string (gradient->gradient);
272 g_string_append (string, s);
277 gtk_css_image_gradient_dispose (GObject *object)
279 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (object);
281 if (gradient->gradient)
283 gtk_gradient_unref (gradient->gradient);
284 gradient->gradient = NULL;
286 if (gradient->pattern)
288 cairo_pattern_destroy (gradient->pattern);
289 gradient->pattern = NULL;
292 G_OBJECT_CLASS (_gtk_css_image_gradient_parent_class)->dispose (object);
296 _gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
298 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
299 GObjectClass *object_class = G_OBJECT_CLASS (klass);
301 image_class->compute = gtk_css_image_gradient_compute;
302 image_class->transition = gtk_css_image_gradient_transition;
303 image_class->draw = gtk_css_image_gradient_draw;
304 image_class->parse = gtk_css_image_gradient_parse;
305 image_class->print = gtk_css_image_gradient_print;
307 object_class->dispose = gtk_css_image_gradient_dispose;
311 _gtk_css_image_gradient_init (GtkCssImageGradient *image_gradient)
316 _gtk_gradient_parse (GtkCssParser *parser)
318 GtkGradient *gradient;
319 cairo_pattern_type_t type;
323 g_return_val_if_fail (parser != NULL, NULL);
325 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
327 _gtk_css_parser_error (parser,
328 "Expected '-gtk-gradient'");
332 if (!_gtk_css_parser_try (parser, "(", TRUE))
334 _gtk_css_parser_error (parser,
335 "Expected '(' after '-gtk-gradient'");
339 /* Parse gradient type */
340 if (_gtk_css_parser_try (parser, "linear", TRUE))
341 type = CAIRO_PATTERN_TYPE_LINEAR;
342 else if (_gtk_css_parser_try (parser, "radial", TRUE))
343 type = CAIRO_PATTERN_TYPE_RADIAL;
346 _gtk_css_parser_error (parser,
347 "Gradient type must be 'radial' or 'linear'");
351 /* Parse start/stop position parameters */
352 for (i = 0; i < 2; i++)
354 if (! _gtk_css_parser_try (parser, ",", TRUE))
356 _gtk_css_parser_error (parser,
361 if (_gtk_css_parser_try (parser, "left", TRUE))
363 else if (_gtk_css_parser_try (parser, "right", TRUE))
365 else if (_gtk_css_parser_try (parser, "center", TRUE))
367 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
369 _gtk_css_parser_error (parser,
370 "Expected a valid X coordinate");
374 if (_gtk_css_parser_try (parser, "top", TRUE))
375 coords[i * 3 + 1] = 0;
376 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
377 coords[i * 3 + 1] = 1;
378 else if (_gtk_css_parser_try (parser, "center", TRUE))
379 coords[i * 3 + 1] = 0.5;
380 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
382 _gtk_css_parser_error (parser,
383 "Expected a valid Y coordinate");
387 if (type == CAIRO_PATTERN_TYPE_RADIAL)
390 if (! _gtk_css_parser_try (parser, ",", TRUE))
392 _gtk_css_parser_error (parser,
397 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
399 _gtk_css_parser_error (parser,
400 "Expected a numer for the radius");
406 if (type == CAIRO_PATTERN_TYPE_LINEAR)
407 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
409 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
410 coords[3], coords[4], coords[5]);
412 while (_gtk_css_parser_try (parser, ",", TRUE))
414 GtkSymbolicColor *color;
417 if (_gtk_css_parser_try (parser, "from", TRUE))
421 if (!_gtk_css_parser_try (parser, "(", TRUE))
423 gtk_gradient_unref (gradient);
424 _gtk_css_parser_error (parser,
430 else if (_gtk_css_parser_try (parser, "to", TRUE))
434 if (!_gtk_css_parser_try (parser, "(", TRUE))
436 gtk_gradient_unref (gradient);
437 _gtk_css_parser_error (parser,
443 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
445 if (!_gtk_css_parser_try (parser, "(", TRUE))
447 gtk_gradient_unref (gradient);
448 _gtk_css_parser_error (parser,
453 if (!_gtk_css_parser_try_double (parser, &position))
455 gtk_gradient_unref (gradient);
456 _gtk_css_parser_error (parser,
457 "Expected a valid number");
461 if (!_gtk_css_parser_try (parser, ",", TRUE))
463 gtk_gradient_unref (gradient);
464 _gtk_css_parser_error (parser,
471 gtk_gradient_unref (gradient);
472 _gtk_css_parser_error (parser,
473 "Not a valid color-stop definition");
477 color = _gtk_symbolic_color_new_take_value (_gtk_css_symbolic_value_new (parser));
480 gtk_gradient_unref (gradient);
484 gtk_gradient_add_color_stop (gradient, position, color);
485 gtk_symbolic_color_unref (color);
487 if (!_gtk_css_parser_try (parser, ")", TRUE))
489 gtk_gradient_unref (gradient);
490 _gtk_css_parser_error (parser,
496 if (!_gtk_css_parser_try (parser, ")", TRUE))
498 gtk_gradient_unref (gradient);
499 _gtk_css_parser_error (parser,