]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshadowvalue.c
bdfa8d25d4010a09c72aaaa3dc292577a7e21832
[~andy/gtk] / gtk / gtkcssshadowvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * Author: Cosimo Cecchi <cosimoc@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include "gtkcssshadowvalueprivate.h"
23
24 #include "gtkcssnumbervalueprivate.h"
25 #include "gtkcssrgbavalueprivate.h"
26 #include "gtkstylecontextprivate.h"
27 #include "gtksymboliccolorprivate.h"
28 #include "gtkthemingengineprivate.h"
29 #include "gtkpango.h"
30
31 struct _GtkCssValue {
32   GTK_CSS_VALUE_BASE
33   guint inset :1;
34
35   GtkCssValue *hoffset;
36   GtkCssValue *voffset;
37   GtkCssValue *radius;
38   GtkCssValue *spread;
39
40   GtkCssValue *color;
41 };
42
43 static GtkCssValue *    gtk_css_shadow_value_new (GtkCssValue *hoffset,
44                                                   GtkCssValue *voffset,
45                                                   GtkCssValue *radius,
46                                                   GtkCssValue *spread,
47                                                   gboolean     inset,
48                                                   GtkCssValue *color);
49
50 static void
51 gtk_css_value_shadow_free (GtkCssValue *shadow)
52 {
53   _gtk_css_value_unref (shadow->hoffset);
54   _gtk_css_value_unref (shadow->voffset);
55   _gtk_css_value_unref (shadow->radius);
56   _gtk_css_value_unref (shadow->spread);
57   _gtk_css_value_unref (shadow->color);
58
59   g_slice_free (GtkCssValue, shadow);
60 }
61
62 static GtkCssValue *
63 gtk_css_value_shadow_compute (GtkCssValue        *shadow,
64                               guint               property_id,
65                               GtkStyleContext    *context,
66                               GtkCssDependencies *dependencies)
67 {
68   GtkCssValue *hoffset, *voffset, *radius, *spread, *color;
69   GtkCssDependencies child_deps;
70
71   child_deps = 0;
72   hoffset = _gtk_css_value_compute (shadow->hoffset, property_id, context, &child_deps);
73   *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
74
75   child_deps = 0;
76   voffset = _gtk_css_value_compute (shadow->voffset, property_id, context, &child_deps);
77   *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
78
79   child_deps = 0;
80   radius = _gtk_css_value_compute (shadow->radius, property_id, context, &child_deps);
81   *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
82
83   child_deps = 0;
84   spread = _gtk_css_value_compute (shadow->spread, property_id, context, &child_deps),
85   *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
86
87   child_deps = 0;
88   color = _gtk_css_value_compute (shadow->color, property_id, context, &child_deps);
89   *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
90
91   return gtk_css_shadow_value_new (hoffset, voffset, radius, spread, shadow->inset, color);
92 }
93
94 static gboolean
95 gtk_css_value_shadow_equal (const GtkCssValue *shadow1,
96                             const GtkCssValue *shadow2)
97 {
98   return shadow1->inset == shadow2->inset
99       && _gtk_css_value_equal (shadow1->hoffset, shadow2->hoffset)
100       && _gtk_css_value_equal (shadow1->voffset, shadow2->voffset)
101       && _gtk_css_value_equal (shadow1->radius, shadow2->radius)
102       && _gtk_css_value_equal (shadow1->spread, shadow2->spread)
103       && _gtk_css_value_equal (shadow1->color, shadow2->color);
104 }
105
106 static GtkCssValue *
107 gtk_css_value_shadow_transition (GtkCssValue *start,
108                                  GtkCssValue *end,
109                                  double       progress)
110 {
111   if (start->inset != end->inset)
112     return NULL;
113
114   return gtk_css_shadow_value_new (_gtk_css_value_transition (start->hoffset, end->hoffset, progress),
115                                    _gtk_css_value_transition (start->voffset, end->voffset, progress),
116                                    _gtk_css_value_transition (start->radius, end->radius, progress),
117                                    _gtk_css_value_transition (start->spread, end->spread, progress),
118                                    start->inset,
119                                    _gtk_css_value_transition (start->color, end->color, progress));
120 }
121
122 static void
123 gtk_css_value_shadow_print (const GtkCssValue *shadow,
124                             GString           *string)
125 {
126   _gtk_css_value_print (shadow->hoffset, string);
127   g_string_append_c (string, ' ');
128   _gtk_css_value_print (shadow->voffset, string);
129   g_string_append_c (string, ' ');
130   if (_gtk_css_number_value_get (shadow->radius, 100) != 0 ||
131       _gtk_css_number_value_get (shadow->spread, 100) != 0)
132     {
133       _gtk_css_value_print (shadow->radius, string);
134       g_string_append_c (string, ' ');
135     }
136
137   if (_gtk_css_number_value_get (shadow->spread, 100) != 0)
138     {
139       _gtk_css_value_print (shadow->spread, string);
140       g_string_append_c (string, ' ');
141     }
142
143   _gtk_css_value_print (shadow->color, string);
144
145   if (shadow->inset)
146     g_string_append (string, " inset");
147
148 }
149
150 static const GtkCssValueClass GTK_CSS_VALUE_SHADOW = {
151   gtk_css_value_shadow_free,
152   gtk_css_value_shadow_compute,
153   gtk_css_value_shadow_equal,
154   gtk_css_value_shadow_transition,
155   gtk_css_value_shadow_print
156 };
157
158 static GtkCssValue *
159 gtk_css_shadow_value_new (GtkCssValue *hoffset,
160                           GtkCssValue *voffset,
161                           GtkCssValue *radius,
162                           GtkCssValue *spread,
163                           gboolean     inset,
164                           GtkCssValue *color)
165 {
166   GtkCssValue *retval;
167
168   retval = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_SHADOW);
169
170   retval->hoffset = hoffset;
171   retval->voffset = voffset;
172   retval->radius = radius;
173   retval->spread = spread;
174   retval->inset = inset;
175   retval->color = color;
176
177   return retval;
178 }                  
179
180 GtkCssValue *
181 _gtk_css_shadow_value_new_for_transition (GtkCssValue *target)
182 {
183   GdkRGBA transparent = { 0, 0, 0, 0 };
184
185   g_return_val_if_fail (target->class == &GTK_CSS_VALUE_SHADOW, NULL);
186
187   return gtk_css_shadow_value_new (_gtk_css_number_value_new (0, GTK_CSS_PX),
188                                    _gtk_css_number_value_new (0, GTK_CSS_PX),
189                                    _gtk_css_number_value_new (0, GTK_CSS_PX),
190                                    _gtk_css_number_value_new (0, GTK_CSS_PX),
191                                    target->inset,
192                                    _gtk_css_rgba_value_new_from_rgba (&transparent));
193 }
194
195 static gboolean
196 value_is_done_parsing (GtkCssParser *parser)
197 {
198   return _gtk_css_parser_is_eof (parser) ||
199          _gtk_css_parser_begins_with (parser, ',') ||
200          _gtk_css_parser_begins_with (parser, ';') ||
201          _gtk_css_parser_begins_with (parser, '}');
202 }
203
204 GtkCssValue *
205 _gtk_css_shadow_value_parse (GtkCssParser *parser)
206 {
207   enum {
208     HOFFSET,
209     VOFFSET,
210     RADIUS,
211     SPREAD,
212     COLOR,
213     N_VALUES
214   };
215   GtkCssValue *values[N_VALUES] = { NULL, };
216   gboolean inset;
217   guint i;
218
219   inset = _gtk_css_parser_try (parser, "inset", TRUE);
220
221   do
222   {
223     if (values[HOFFSET] == NULL &&
224          _gtk_css_parser_has_number (parser))
225       {
226         values[HOFFSET] = _gtk_css_number_value_parse (parser,
227                                                        GTK_CSS_PARSE_LENGTH
228                                                        | GTK_CSS_NUMBER_AS_PIXELS);
229         if (values[HOFFSET] == NULL)
230           goto fail;
231
232         values[VOFFSET] = _gtk_css_number_value_parse (parser,
233                                                        GTK_CSS_PARSE_LENGTH
234                                                        | GTK_CSS_NUMBER_AS_PIXELS);
235         if (values[VOFFSET] == NULL)
236           goto fail;
237
238         if (_gtk_css_parser_has_number (parser))
239           {
240             values[RADIUS] = _gtk_css_number_value_parse (parser,
241                                                           GTK_CSS_PARSE_LENGTH
242                                                           | GTK_CSS_POSITIVE_ONLY
243                                                           | GTK_CSS_NUMBER_AS_PIXELS);
244             if (values[RADIUS] == NULL)
245               goto fail;
246           }
247         else
248           values[RADIUS] = _gtk_css_number_value_new (0.0, GTK_CSS_PX);
249                                                         
250         if (_gtk_css_parser_has_number (parser))
251           {
252             values[SPREAD] = _gtk_css_number_value_parse (parser,
253                                                           GTK_CSS_PARSE_LENGTH
254                                                           | GTK_CSS_NUMBER_AS_PIXELS);
255             if (values[SPREAD] == NULL)
256               goto fail;
257           }
258         else
259           values[SPREAD] = _gtk_css_number_value_new (0.0, GTK_CSS_PX);
260       }
261     else if (!inset && _gtk_css_parser_try (parser, "inset", TRUE))
262       {
263         if (values[HOFFSET] == NULL)
264           goto fail;
265         inset = TRUE;
266         break;
267       }
268     else if (values[COLOR] == NULL)
269       {
270         values[COLOR] = _gtk_css_symbolic_value_new (parser);
271
272         if (values[COLOR] == NULL)
273           goto fail;
274       }
275     else
276       {
277         /* We parsed everything and there's still stuff left?
278          * Pretend we didn't notice and let the normal code produce
279          * a 'junk at end of value' error */
280         goto fail;
281       }
282   }
283   while (values[HOFFSET] == NULL || !value_is_done_parsing (parser));
284
285   if (values[COLOR] == NULL)
286     values[COLOR] = _gtk_css_symbolic_value_new_take_symbolic_color (
287                       gtk_symbolic_color_ref (
288                         _gtk_symbolic_color_get_current_color ()));
289
290   return gtk_css_shadow_value_new (values[HOFFSET], values[VOFFSET],
291                                    values[RADIUS], values[SPREAD],
292                                    inset, values[COLOR]);
293
294 fail:
295   for (i = 0; i < N_VALUES; i++)
296     {
297       if (values[i])
298         _gtk_css_value_unref (values[i]);
299     }
300
301   return NULL;
302 }
303
304 void
305 _gtk_css_shadow_value_paint_layout (const GtkCssValue *shadow,
306                                     cairo_t           *cr,
307                                     PangoLayout       *layout)
308 {
309   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
310
311   if (!cairo_has_current_point (cr))
312     cairo_move_to (cr, 0, 0);
313
314   cairo_save (cr);
315
316   cairo_rel_move_to (cr, 
317                      _gtk_css_number_value_get (shadow->hoffset, 0),
318                      _gtk_css_number_value_get (shadow->voffset, 0));
319   gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
320   _gtk_pango_fill_layout (cr, layout);
321
322   cairo_rel_move_to (cr,
323                      - _gtk_css_number_value_get (shadow->hoffset, 0),
324                      - _gtk_css_number_value_get (shadow->voffset, 0));
325   cairo_restore (cr);
326 }
327
328 void
329 _gtk_css_shadow_value_paint_icon (const GtkCssValue *shadow,
330                                   cairo_t           *cr)
331 {
332   cairo_pattern_t *pattern;
333
334   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
335
336   cairo_save (cr);
337   pattern = cairo_pattern_reference (cairo_get_source (cr));
338   gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
339
340   cairo_translate (cr,
341                    _gtk_css_number_value_get (shadow->hoffset, 0),
342                    _gtk_css_number_value_get (shadow->voffset, 0));
343   cairo_mask (cr, pattern);
344
345   cairo_restore (cr);
346   cairo_pattern_destroy (pattern);
347 }
348
349 void
350 _gtk_css_shadow_value_paint_spinner (const GtkCssValue *shadow,
351                                      cairo_t           *cr,
352                                      gdouble            radius,
353                                      gdouble            progress)
354 {
355   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
356
357   cairo_save (cr);
358
359   cairo_translate (cr,
360                    _gtk_css_number_value_get (shadow->hoffset, 0),
361                    _gtk_css_number_value_get (shadow->voffset, 0));
362   _gtk_theming_engine_paint_spinner (cr,
363                                      radius, progress,
364                                      _gtk_css_rgba_value_get_rgba (shadow->color));
365
366   cairo_restore (cr);
367 }
368
369 void
370 _gtk_css_shadow_value_paint_box (const GtkCssValue   *shadow,
371                                  cairo_t             *cr,
372                                  const GtkRoundedBox *padding_box)
373 {
374   GtkRoundedBox box;
375   double spread;
376
377   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
378
379   cairo_save (cr);
380   cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
381
382   _gtk_rounded_box_path (padding_box, cr);
383   cairo_clip (cr);
384
385   box = *padding_box;
386   _gtk_rounded_box_move (&box,
387                          _gtk_css_number_value_get (shadow->hoffset, 0),
388                          _gtk_css_number_value_get (shadow->voffset, 0));
389   spread = _gtk_css_number_value_get (shadow->spread, 0);
390   _gtk_rounded_box_shrink (&box, spread, spread, spread, spread);
391
392   _gtk_rounded_box_path (&box, cr);
393   _gtk_rounded_box_clip_path (padding_box, cr);
394
395   gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (shadow->color));
396   cairo_fill (cr);
397
398   cairo_restore (cr);
399 }