]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimagecrossfade.c
cssimage: Make image transition a vfunc
[~andy/gtk] / gtk / gtkcssimagecrossfade.c
1 /*
2  * Copyright © 2012 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 #include <math.h>
23 #include <string.h>
24
25 #include "gtkcssimagecrossfadeprivate.h"
26
27 #include "gtkcssnumbervalueprivate.h"
28
29 G_DEFINE_TYPE (GtkCssImageCrossFade, _gtk_css_image_cross_fade, GTK_TYPE_CSS_IMAGE)
30
31 static int
32 gtk_css_image_cross_fade_get_width (GtkCssImage *image)
33 {
34   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
35   int start_width, end_width;
36
37   if (cross_fade->start)
38     {
39       start_width = _gtk_css_image_get_width (cross_fade->start);
40       /* no intrinsic width, what now? */
41       if (start_width == 0)
42         return 0;
43     }
44   else
45     start_width = 0;
46
47   if (cross_fade->end)
48     {
49       end_width = _gtk_css_image_get_width (cross_fade->end);
50       /* no intrinsic width, what now? */
51       if (end_width == 0)
52         return 0;
53     }
54   else
55     end_width = 0;
56
57   return start_width + (end_width - start_width) * cross_fade->progress;
58 }
59
60 static int
61 gtk_css_image_cross_fade_get_height (GtkCssImage *image)
62 {
63   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
64   int start_height, end_height;
65
66   if (cross_fade->start)
67     {
68       start_height = _gtk_css_image_get_height (cross_fade->start);
69       /* no intrinsic height, what now? */
70       if (start_height == 0)
71         return 0;
72     }
73   else
74     start_height = 0;
75
76   if (cross_fade->end)
77     {
78       end_height = _gtk_css_image_get_height (cross_fade->end);
79       /* no intrinsic height, what now? */
80       if (end_height == 0)
81         return 0;
82     }
83   else
84     end_height = 0;
85
86   return start_height + (end_height - start_height) * cross_fade->progress;
87 }
88
89 static void
90 gtk_css_image_cross_fade_draw (GtkCssImage        *image,
91                                cairo_t            *cr,
92                                double              width,
93                                double              height)
94 {
95   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
96
97   if (cross_fade->progress <= 0.0)
98     {
99       if (cross_fade->start)
100         _gtk_css_image_draw (cross_fade->start, cr, width, height);
101     }
102   else if (cross_fade->progress >= 1.0)
103     {
104       if (cross_fade->end)
105         _gtk_css_image_draw (cross_fade->end, cr, width, height);
106     }
107   else
108     {
109       if (cross_fade->start && cross_fade->end)
110         {
111           /* to reduce the group size */
112           cairo_rectangle (cr, 0, 0, ceil (width), ceil (height));
113           cairo_clip (cr);
114
115           cairo_push_group (cr);
116
117           /* performance trick */
118           cairo_reset_clip (cr);
119
120           _gtk_css_image_draw (cross_fade->start, cr, width, height);
121
122           cairo_push_group (cr);
123           _gtk_css_image_draw (cross_fade->end, cr, width, height);
124           cairo_pop_group_to_source (cr);
125
126           cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
127           cairo_paint_with_alpha (cr, cross_fade->progress);
128
129           cairo_pop_group_to_source (cr);
130           cairo_paint (cr);
131         }
132       else if (cross_fade->start || cross_fade->end)
133         {
134           cairo_push_group (cr);
135           _gtk_css_image_draw (cross_fade->start ? cross_fade->start : cross_fade->end, cr, width, height);
136           cairo_pop_group_to_source (cr);
137
138           cairo_paint_with_alpha (cr, cross_fade->start ? 1.0 - cross_fade->progress : cross_fade->progress);
139         }
140     }
141 }
142
143 static gboolean
144 gtk_css_image_cross_fade_parse (GtkCssImage  *image,
145                                 GtkCssParser *parser)
146 {
147   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
148   if (!_gtk_css_parser_try (parser, "cross-fade(", TRUE))
149     {
150       _gtk_css_parser_error (parser, "Expected 'cross-fade('");
151       return FALSE;
152     }
153
154   if (_gtk_css_parser_has_number (parser))
155     {
156       GtkCssValue *number;
157       
158       number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_PERCENT | GTK_CSS_POSITIVE_ONLY);
159       if (number == NULL)
160         return FALSE;
161       cross_fade->progress = _gtk_css_number_value_get (number, 1);
162       _gtk_css_value_unref (number);
163
164       if (cross_fade->progress > 1.0)
165         {
166           _gtk_css_parser_error (parser, "Percentages over 100%% are not allowed");
167           return FALSE;
168         }
169     }
170   else
171     cross_fade->progress = 0.5;
172
173   cross_fade->start = _gtk_css_image_new_parse (parser);
174   if (cross_fade->start == NULL)
175     return FALSE;
176
177   if (_gtk_css_parser_try (parser, ",", TRUE))
178     {
179       /* XXX: allow parsing colors here */
180       cross_fade->end = _gtk_css_image_new_parse (parser);
181       if (cross_fade->end == NULL)
182         return FALSE;
183     }
184
185   if (!_gtk_css_parser_try (parser, ")", TRUE))
186     {
187       _gtk_css_parser_error (parser, "Missing closing bracket");
188       return FALSE;
189     }
190
191   return TRUE;
192 }
193
194 static void
195 gtk_css_image_cross_fade_print (GtkCssImage *image,
196                                 GString     *string)
197 {
198   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (image);
199
200   g_string_append (string, "cross-fade(");
201   if (cross_fade->progress != 0.5)
202     {
203       g_string_append_printf (string, "%g%% ", cross_fade->progress * 100.0);
204     }
205
206   if (cross_fade->start)
207     _gtk_css_image_print (cross_fade->start, string);
208   else
209     g_string_append (string, "none");
210   if (cross_fade->end)
211     {
212       g_string_append (string, ", ");
213       _gtk_css_image_print (cross_fade->end, string);
214     }
215   g_string_append (string, ")");
216 }
217
218 static void
219 gtk_css_image_cross_fade_dispose (GObject *object)
220 {
221   GtkCssImageCrossFade *cross_fade = GTK_CSS_IMAGE_CROSS_FADE (object);
222
223   g_clear_object (&cross_fade->start);
224   g_clear_object (&cross_fade->end);
225
226   G_OBJECT_CLASS (_gtk_css_image_cross_fade_parent_class)->dispose (object);
227 }
228
229 static void
230 _gtk_css_image_cross_fade_class_init (GtkCssImageCrossFadeClass *klass)
231 {
232   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
233   GObjectClass *object_class = G_OBJECT_CLASS (klass);
234
235   image_class->get_width = gtk_css_image_cross_fade_get_width;
236   image_class->get_height = gtk_css_image_cross_fade_get_height;
237   image_class->draw = gtk_css_image_cross_fade_draw;
238   image_class->parse = gtk_css_image_cross_fade_parse;
239   image_class->print = gtk_css_image_cross_fade_print;
240
241   object_class->dispose = gtk_css_image_cross_fade_dispose;
242 }
243
244 static void
245 _gtk_css_image_cross_fade_init (GtkCssImageCrossFade *image_cross_fade)
246 {
247 }
248
249 GtkCssImage *
250 _gtk_css_image_cross_fade_new (GtkCssImage *start,
251                                GtkCssImage *end,
252                                double       progress)
253 {
254   GtkCssImageCrossFade *cross_fade;
255
256   g_return_val_if_fail (start == NULL || GTK_IS_CSS_IMAGE (start), NULL);
257   g_return_val_if_fail (end == NULL || GTK_IS_CSS_IMAGE (end), NULL);
258
259   cross_fade = g_object_new (GTK_TYPE_CSS_IMAGE_CROSS_FADE, NULL);
260   if (start)
261     cross_fade->start = g_object_ref (start);
262   if (end)
263     cross_fade->end = g_object_ref (end);
264   cross_fade->progress = progress;
265
266   return GTK_CSS_IMAGE (cross_fade);
267 }
268