]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimagegradient.c
symboliccolor: Deprecate
[~andy/gtk] / gtk / gtkcssimagegradient.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #define GDK_DISABLE_DEPRECATION_WARNINGS
23
24 #include "gtkcssimagegradientprivate.h"
25
26 #include "gtkcssprovider.h"
27 #include "gtkgradientprivate.h"
28 #include "gtksymboliccolorprivate.h"
29
30 G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)
31
32 static GtkCssImage *
33 gtk_css_image_gradient_compute (GtkCssImage             *image,
34                                 guint                    property_id,
35                                 GtkStyleProviderPrivate *provider,
36                                 GtkCssComputedValues    *values,
37                                 GtkCssComputedValues    *parent_values,
38                                 GtkCssDependencies      *dependencies)
39 {
40   GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
41   GtkCssImageGradient *copy;
42
43   if (gradient->pattern)
44     return g_object_ref (gradient);
45
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);
49
50   return GTK_CSS_IMAGE (copy);
51 }
52
53 static cairo_pattern_t *
54 fade_pattern (cairo_pattern_t *pattern,
55               double           opacity)
56 {
57   double x0, y0, x1, y1, r0, r1;
58   cairo_pattern_t *result;
59   int i, n;
60
61   switch (cairo_pattern_get_type (pattern))
62     {
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);
66       break;
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);
70       break;
71     default:
72       g_return_val_if_reached (NULL);
73     }
74
75   cairo_pattern_get_color_stop_count (pattern, &n);
76   for (i = 0; i < n; i++)
77     {
78       double o, r, g, b, a;
79
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);
82     }
83
84   return result;
85 }
86
87 static cairo_pattern_t *
88 transition_pattern (cairo_pattern_t *start,
89                     cairo_pattern_t *end,
90                     double           progress)
91 {
92   double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
93   cairo_pattern_t *result;
94   int i, n;
95
96   progress = CLAMP (progress, 0.0, 1.0);
97
98   if (end == NULL)
99     return fade_pattern (start, 1.0 - progress);
100
101   g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));
102
103   switch (cairo_pattern_get_type (start))
104     {
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);
112       break;
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);
122       break;
123     default:
124       g_return_val_if_reached (NULL);
125     }
126
127   cairo_pattern_get_color_stop_count (start, &n);
128   for (i = 0; i < n; i++)
129     {
130       double so, sr, sg, sb, sa, eo, er, eg, eb, ea;
131
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);
134
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);
141     }
142
143   return result;
144 }
145
146 static GtkCssImage *
147 gtk_css_image_gradient_transition (GtkCssImage *start_image,
148                                    GtkCssImage *end_image,
149                                    guint        property_id,
150                                    double       progress)
151 {
152   GtkGradient *start_gradient, *end_gradient, *gradient;
153   cairo_pattern_t *start_pattern, *end_pattern;
154   GtkCssImageGradient *result;
155
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)
159     {
160       end_gradient = NULL;
161       end_pattern = NULL;
162     }
163   else
164     {
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);
167
168       end_gradient = GTK_CSS_IMAGE_GRADIENT (end_image)->gradient;
169       end_pattern = GTK_CSS_IMAGE_GRADIENT (end_image)->pattern;
170     }
171
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);
175
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);
179
180   return GTK_CSS_IMAGE (result);
181 }
182
183 static gboolean
184 gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
185                                     cairo_t              *cr,
186                                     double               width,
187                                     double               height)
188 {
189   cairo_pattern_t *pattern = image->pattern;
190   double x0, y0, x1, y1, r0, r1;
191   GdkRGBA color0, color1;
192   double offset0, offset1;
193   int n_stops;
194
195   if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
196     return FALSE;
197   if (cairo_pattern_get_extend (pattern) != CAIRO_EXTEND_PAD)
198     return FALSE;
199
200   cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
201
202   if (x0 != x1 ||
203       y0 != y1 ||
204       r0 != 0.0)
205     return FALSE;
206
207   cairo_pattern_get_color_stop_count (pattern, &n_stops);
208   if (n_stops != 2)
209     return FALSE;
210
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)
214     return FALSE;
215
216   cairo_scale (cr, width, height);
217
218   cairo_rectangle (cr, 0, 0, 1, 1);
219   cairo_clip (cr);
220
221   gdk_cairo_set_source_rgba (cr, &color1);
222   cairo_paint (cr);
223
224   gdk_cairo_set_source_rgba (cr, &color0);
225   cairo_arc (cr, x1, y1, r1 * offset1, 0, 2 * G_PI);
226   cairo_fill (cr);
227
228   return TRUE;
229 }
230
231 static void
232 gtk_css_image_gradient_draw (GtkCssImage        *image,
233                              cairo_t            *cr,
234                              double              width,
235                              double              height)
236 {
237   GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
238
239   if (!gradient->pattern)
240     {
241       g_warning ("trying to paint unresolved gradient");
242       return;
243     }
244
245   if (gtk_css_image_gradient_draw_circle (gradient, cr, width, height))
246     return;
247
248   cairo_scale (cr, width, height);
249
250   cairo_rectangle (cr, 0, 0, 1, 1);
251   cairo_set_source (cr, gradient->pattern);
252   cairo_fill (cr);
253 }
254
255 static gboolean
256 gtk_css_image_gradient_parse (GtkCssImage  *image,
257                               GtkCssParser *parser)
258 {
259   GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
260
261   gradient->gradient = _gtk_gradient_parse (parser);
262
263   return gradient->gradient != NULL;
264 }
265
266 static void
267 gtk_css_image_gradient_print (GtkCssImage *image,
268                               GString     *string)
269 {
270   GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (image);
271   char *s;
272
273   s = gtk_gradient_to_string (gradient->gradient);
274   g_string_append (string, s);
275   g_free (s);
276 }
277
278 static void
279 gtk_css_image_gradient_dispose (GObject *object)
280 {
281   GtkCssImageGradient *gradient = GTK_CSS_IMAGE_GRADIENT (object);
282
283   if (gradient->gradient)
284     {
285       gtk_gradient_unref (gradient->gradient);
286       gradient->gradient = NULL;
287     }
288   if (gradient->pattern)
289     {
290       cairo_pattern_destroy (gradient->pattern);
291       gradient->pattern = NULL;
292     }
293
294   G_OBJECT_CLASS (_gtk_css_image_gradient_parent_class)->dispose (object);
295 }
296
297 static void
298 _gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
299 {
300   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
301   GObjectClass *object_class = G_OBJECT_CLASS (klass);
302
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;
308
309   object_class->dispose = gtk_css_image_gradient_dispose;
310 }
311
312 static void
313 _gtk_css_image_gradient_init (GtkCssImageGradient *image_gradient)
314 {
315 }
316
317 GtkGradient *
318 _gtk_gradient_parse (GtkCssParser *parser)
319 {
320   GtkGradient *gradient;
321   cairo_pattern_type_t type;
322   gdouble coords[6];
323   guint i;
324
325   g_return_val_if_fail (parser != NULL, NULL);
326
327   if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
328     {
329       _gtk_css_parser_error (parser,
330                              "Expected '-gtk-gradient'");
331       return NULL;
332     }
333
334   if (!_gtk_css_parser_try (parser, "(", TRUE))
335     {
336       _gtk_css_parser_error (parser,
337                              "Expected '(' after '-gtk-gradient'");
338       return NULL;
339     }
340
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;
346   else
347     {
348       _gtk_css_parser_error (parser,
349                              "Gradient type must be 'radial' or 'linear'");
350       return NULL;
351     }
352
353   /* Parse start/stop position parameters */
354   for (i = 0; i < 2; i++)
355     {
356       if (! _gtk_css_parser_try (parser, ",", TRUE))
357         {
358           _gtk_css_parser_error (parser,
359                                  "Expected ','");
360           return NULL;
361         }
362
363       if (_gtk_css_parser_try (parser, "left", TRUE))
364         coords[i * 3] = 0;
365       else if (_gtk_css_parser_try (parser, "right", TRUE))
366         coords[i * 3] = 1;
367       else if (_gtk_css_parser_try (parser, "center", TRUE))
368         coords[i * 3] = 0.5;
369       else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
370         {
371           _gtk_css_parser_error (parser,
372                                  "Expected a valid X coordinate");
373           return NULL;
374         }
375
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]))
383         {
384           _gtk_css_parser_error (parser,
385                                  "Expected a valid Y coordinate");
386           return NULL;
387         }
388
389       if (type == CAIRO_PATTERN_TYPE_RADIAL)
390         {
391           /* Parse radius */
392           if (! _gtk_css_parser_try (parser, ",", TRUE))
393             {
394               _gtk_css_parser_error (parser,
395                                      "Expected ','");
396               return NULL;
397             }
398
399           if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
400             {
401               _gtk_css_parser_error (parser,
402                                      "Expected a numer for the radius");
403               return NULL;
404             }
405         }
406     }
407
408   if (type == CAIRO_PATTERN_TYPE_LINEAR)
409     gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
410   else
411     gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
412                                         coords[3], coords[4], coords[5]);
413
414   while (_gtk_css_parser_try (parser, ",", TRUE))
415     {
416       GtkSymbolicColor *color;
417       gdouble position;
418
419       if (_gtk_css_parser_try (parser, "from", TRUE))
420         {
421           position = 0;
422
423           if (!_gtk_css_parser_try (parser, "(", TRUE))
424             {
425               gtk_gradient_unref (gradient);
426               _gtk_css_parser_error (parser,
427                                      "Expected '('");
428               return NULL;
429             }
430
431         }
432       else if (_gtk_css_parser_try (parser, "to", TRUE))
433         {
434           position = 1;
435
436           if (!_gtk_css_parser_try (parser, "(", TRUE))
437             {
438               gtk_gradient_unref (gradient);
439               _gtk_css_parser_error (parser,
440                                      "Expected '('");
441               return NULL;
442             }
443
444         }
445       else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
446         {
447           if (!_gtk_css_parser_try (parser, "(", TRUE))
448             {
449               gtk_gradient_unref (gradient);
450               _gtk_css_parser_error (parser,
451                                      "Expected '('");
452               return NULL;
453             }
454
455           if (!_gtk_css_parser_try_double (parser, &position))
456             {
457               gtk_gradient_unref (gradient);
458               _gtk_css_parser_error (parser,
459                                      "Expected a valid number");
460               return NULL;
461             }
462
463           if (!_gtk_css_parser_try (parser, ",", TRUE))
464             {
465               gtk_gradient_unref (gradient);
466               _gtk_css_parser_error (parser,
467                                      "Expected a comma");
468               return NULL;
469             }
470         }
471       else
472         {
473           gtk_gradient_unref (gradient);
474           _gtk_css_parser_error (parser,
475                                  "Not a valid color-stop definition");
476           return NULL;
477         }
478
479       color = _gtk_css_symbolic_value_new (parser);
480       if (color == NULL)
481         {
482           gtk_gradient_unref (gradient);
483           return NULL;
484         }
485
486       gtk_gradient_add_color_stop (gradient, position, color);
487       gtk_symbolic_color_unref (color);
488
489       if (!_gtk_css_parser_try (parser, ")", TRUE))
490         {
491           gtk_gradient_unref (gradient);
492           _gtk_css_parser_error (parser,
493                                  "Expected ')'");
494           return NULL;
495         }
496     }
497
498   if (!_gtk_css_parser_try (parser, ")", TRUE))
499     {
500       gtk_gradient_unref (gradient);
501       _gtk_css_parser_error (parser,
502                              "Expected ')'");
503       return NULL;
504     }
505
506   return gradient;
507 }