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 "gtksymboliccolorprivate.h"
26 #include "gtkstylepropertiesprivate.h"
28 G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)
31 gtk_css_image_gradient_compute (GtkCssImage *image,
33 GtkStyleContext *context,
34 GtkCssDependencies *dependencies)
36 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
37 GtkCssImageGradient *copy;
39 if (gradient->pattern)
40 return g_object_ref (gradient);
42 copy = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
43 copy->gradient = gtk_gradient_ref (gradient->gradient);
44 copy->pattern = _gtk_gradient_resolve_full (copy->gradient, context, dependencies);
46 return GTK_CSS_IMAGE (copy);
50 gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
55 cairo_pattern_t *pattern = image->pattern;
56 double x0, y0, x1, y1, r0, r1;
57 GdkRGBA color0, color1;
58 double offset0, offset1;
61 if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
63 if (cairo_pattern_get_extend (pattern) != CAIRO_EXTEND_PAD)
66 cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
73 cairo_pattern_get_color_stop_count (pattern, &n_stops);
77 cairo_pattern_get_color_stop_rgba (pattern, 0, &offset0, &color0.red, &color0.green, &color0.blue, &color0.alpha);
78 cairo_pattern_get_color_stop_rgba (pattern, 1, &offset1, &color1.red, &color1.green, &color1.blue, &color1.alpha);
79 if (offset0 != offset1)
82 cairo_scale (cr, width, height);
84 cairo_rectangle (cr, 0, 0, 1, 1);
87 gdk_cairo_set_source_rgba (cr, &color1);
90 gdk_cairo_set_source_rgba (cr, &color0);
91 cairo_arc (cr, x1, y1, r1 * offset1, 0, 2 * G_PI);
98 gtk_css_image_gradient_draw (GtkCssImage *image,
103 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
105 if (!gradient->pattern)
107 g_warning ("trying to paint unresolved gradient");
111 if (gtk_css_image_gradient_draw_circle (gradient, cr, width, height))
114 cairo_scale (cr, width, height);
116 cairo_rectangle (cr, 0, 0, 1, 1);
117 cairo_set_source (cr, gradient->pattern);
122 gtk_css_image_gradient_parse (GtkCssImage *image,
123 GtkCssParser *parser)
125 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
127 gradient->gradient = _gtk_gradient_parse (parser);
129 return gradient->gradient != NULL;
133 gtk_css_image_gradient_print (GtkCssImage *image,
136 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
139 s = gtk_gradient_to_string (gradient->gradient);
140 g_string_append (string, s);
145 gtk_css_image_gradient_dispose (GObject *object)
147 GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (object);
149 if (gradient->gradient)
151 gtk_gradient_unref (gradient->gradient);
152 gradient->gradient = NULL;
154 if (gradient->pattern)
156 cairo_pattern_destroy (gradient->pattern);
157 gradient->pattern = NULL;
160 G_OBJECT_CLASS (_gtk_css_image_gradient_parent_class)->dispose (object);
164 _gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
166 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
167 GObjectClass *object_class = G_OBJECT_CLASS (klass);
169 image_class->compute = gtk_css_image_gradient_compute;
170 image_class->draw = gtk_css_image_gradient_draw;
171 image_class->parse = gtk_css_image_gradient_parse;
172 image_class->print = gtk_css_image_gradient_print;
174 object_class->dispose = gtk_css_image_gradient_dispose;
178 _gtk_css_image_gradient_init (GtkCssImageGradient *image_gradient)
183 _gtk_gradient_parse (GtkCssParser *parser)
185 GtkGradient *gradient;
186 cairo_pattern_type_t type;
190 g_return_val_if_fail (parser != NULL, NULL);
192 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
194 _gtk_css_parser_error (parser,
195 "Expected '-gtk-gradient'");
199 if (!_gtk_css_parser_try (parser, "(", TRUE))
201 _gtk_css_parser_error (parser,
202 "Expected '(' after '-gtk-gradient'");
206 /* Parse gradient type */
207 if (_gtk_css_parser_try (parser, "linear", TRUE))
208 type = CAIRO_PATTERN_TYPE_LINEAR;
209 else if (_gtk_css_parser_try (parser, "radial", TRUE))
210 type = CAIRO_PATTERN_TYPE_RADIAL;
213 _gtk_css_parser_error (parser,
214 "Gradient type must be 'radial' or 'linear'");
218 /* Parse start/stop position parameters */
219 for (i = 0; i < 2; i++)
221 if (! _gtk_css_parser_try (parser, ",", TRUE))
223 _gtk_css_parser_error (parser,
228 if (_gtk_css_parser_try (parser, "left", TRUE))
230 else if (_gtk_css_parser_try (parser, "right", TRUE))
232 else if (_gtk_css_parser_try (parser, "center", TRUE))
234 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
236 _gtk_css_parser_error (parser,
237 "Expected a valid X coordinate");
241 if (_gtk_css_parser_try (parser, "top", TRUE))
242 coords[i * 3 + 1] = 0;
243 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
244 coords[i * 3 + 1] = 1;
245 else if (_gtk_css_parser_try (parser, "center", TRUE))
246 coords[i * 3 + 1] = 0.5;
247 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
249 _gtk_css_parser_error (parser,
250 "Expected a valid Y coordinate");
254 if (type == CAIRO_PATTERN_TYPE_RADIAL)
257 if (! _gtk_css_parser_try (parser, ",", TRUE))
259 _gtk_css_parser_error (parser,
264 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
266 _gtk_css_parser_error (parser,
267 "Expected a numer for the radius");
273 if (type == CAIRO_PATTERN_TYPE_LINEAR)
274 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
276 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
277 coords[3], coords[4], coords[5]);
279 while (_gtk_css_parser_try (parser, ",", TRUE))
281 GtkSymbolicColor *color;
284 if (_gtk_css_parser_try (parser, "from", TRUE))
288 if (!_gtk_css_parser_try (parser, "(", TRUE))
290 gtk_gradient_unref (gradient);
291 _gtk_css_parser_error (parser,
297 else if (_gtk_css_parser_try (parser, "to", TRUE))
301 if (!_gtk_css_parser_try (parser, "(", TRUE))
303 gtk_gradient_unref (gradient);
304 _gtk_css_parser_error (parser,
310 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
312 if (!_gtk_css_parser_try (parser, "(", TRUE))
314 gtk_gradient_unref (gradient);
315 _gtk_css_parser_error (parser,
320 if (!_gtk_css_parser_try_double (parser, &position))
322 gtk_gradient_unref (gradient);
323 _gtk_css_parser_error (parser,
324 "Expected a valid number");
328 if (!_gtk_css_parser_try (parser, ",", TRUE))
330 gtk_gradient_unref (gradient);
331 _gtk_css_parser_error (parser,
338 gtk_gradient_unref (gradient);
339 _gtk_css_parser_error (parser,
340 "Not a valid color-stop definition");
344 color = _gtk_symbolic_color_new_take_value (_gtk_css_symbolic_value_new (parser));
347 gtk_gradient_unref (gradient);
351 gtk_gradient_add_color_stop (gradient, position, color);
352 gtk_symbolic_color_unref (color);
354 if (!_gtk_css_parser_try (parser, ")", TRUE))
356 gtk_gradient_unref (gradient);
357 _gtk_css_parser_error (parser,
363 if (!_gtk_css_parser_try (parser, ")", TRUE))
365 gtk_gradient_unref (gradient);
366 _gtk_css_parser_error (parser,