]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
style: Remove GtkStylePropertyContext again
[~andy/gtk] / gtk / gtkthemingbackground.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2011 Red Hat, Inc.
4  * 
5  * Authors: Carlos Garnacho <carlosg@gnome.org>
6  *          Cosimo Cecchi <cosimoc@gnome.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "gtkcsstypesprivate.h"
25 #include "gtkthemingbackgroundprivate.h"
26 #include "gtkthemingengineprivate.h"
27
28 #include <gdk/gdk.h>
29
30 static void
31 _gtk_theming_background_apply_window_background (GtkThemingBackground *bg,
32                                                  cairo_t              *cr)
33 {
34   if (gtk_theming_engine_has_class (bg->engine, "background"))
35     {
36       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
37       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
38       cairo_paint (cr);
39     }
40 }
41
42 static void
43 _gtk_theming_background_apply_running_transformation (GtkThemingBackground *bg,
44                                                       cairo_t              *cr)
45 {
46   gboolean running;
47   gdouble progress;
48   cairo_pattern_t *other_pattern;
49   GtkStateFlags other_flags;
50   GdkRGBA other_bg;
51   cairo_pattern_t *new_pattern = NULL;
52
53   running = gtk_theming_engine_state_is_running (bg->engine, GTK_STATE_PRELIGHT, &progress);
54
55   if (!running)
56     return;
57
58   if (bg->flags & GTK_STATE_FLAG_PRELIGHT)
59     {
60       other_flags = bg->flags & ~(GTK_STATE_FLAG_PRELIGHT);
61       progress = 1 - progress;
62     }
63   else
64     other_flags = bg->flags | GTK_STATE_FLAG_PRELIGHT;
65
66   gtk_theming_engine_get_background_color (bg->engine, other_flags, &other_bg);
67   gtk_theming_engine_get (bg->engine, other_flags,
68                           "background-image", &other_pattern,
69                           NULL);
70
71   if (bg->pattern && other_pattern)
72     {
73       cairo_pattern_type_t type, other_type;
74       gint n0, n1;
75
76       cairo_pattern_get_color_stop_count (bg->pattern, &n0);
77       cairo_pattern_get_color_stop_count (other_pattern, &n1);
78       type = cairo_pattern_get_type (bg->pattern);
79       other_type = cairo_pattern_get_type (other_pattern);
80
81       if (type == other_type && n0 == n1)
82         {
83           gdouble offset0, red0, green0, blue0, alpha0;
84           gdouble offset1, red1, green1, blue1, alpha1;
85           gdouble x00, x01, y00, y01, x10, x11, y10, y11;
86           gdouble r00, r01, r10, r11;
87           guint i;
88
89           if (type == CAIRO_PATTERN_TYPE_LINEAR)
90             {
91               cairo_pattern_get_linear_points (bg->pattern, &x00, &y00, &x01, &y01);
92               cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
93
94               new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
95                                                          y00 + (y10 - y00) * progress,
96                                                          x01 + (x11 - x01) * progress,
97                                                          y01 + (y11 - y01) * progress);
98             }
99           else
100             {
101               cairo_pattern_get_radial_circles (bg->pattern, &x00, &y00, &r00, &x01, &y01, &r01);
102               cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
103
104               new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
105                                                          y00 + (y10 - y00) * progress,
106                                                          r00 + (r10 - r00) * progress,
107                                                          x01 + (x11 - x01) * progress,
108                                                          y01 + (y11 - y01) * progress,
109                                                          r01 + (r11 - r01) * progress);
110             }
111
112           cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
113           i = 0;
114
115           /* Blend both gradients into one */
116           while (i < n0 && i < n1)
117             {
118               cairo_pattern_get_color_stop_rgba (bg->pattern, i,
119                                                  &offset0,
120                                                  &red0, &green0, &blue0,
121                                                  &alpha0);
122               cairo_pattern_get_color_stop_rgba (other_pattern, i,
123                                                  &offset1,
124                                                  &red1, &green1, &blue1,
125                                                  &alpha1);
126
127               cairo_pattern_add_color_stop_rgba (new_pattern,
128                                                  offset0 + ((offset1 - offset0) * progress),
129                                                  red0 + ((red1 - red0) * progress),
130                                                  green0 + ((green1 - green0) * progress),
131                                                  blue0 + ((blue1 - blue0) * progress),
132                                                  alpha0 + ((alpha1 - alpha0) * progress));
133               i++;
134             }
135         }
136       else
137         {
138           cairo_save (cr);
139
140           cairo_rectangle (cr, 0, 0, bg->paint_area.width, bg->paint_area.height);
141           cairo_clip (cr);
142
143           cairo_push_group (cr);
144
145           cairo_scale (cr, bg->paint_area.width, bg->paint_area.height);
146           cairo_set_source (cr, other_pattern);
147           cairo_paint_with_alpha (cr, progress);
148           cairo_set_source (cr, bg->pattern);
149           cairo_paint_with_alpha (cr, 1.0 - progress);
150
151           new_pattern = cairo_pop_group (cr);
152
153           cairo_restore (cr);
154         }
155     }
156   else if (bg->pattern || other_pattern)
157     {
158       cairo_pattern_t *p;
159       const GdkRGBA *c;
160       gdouble x0, y0, x1, y1, r0, r1;
161       gint n, i;
162
163       /* Blend a pattern with a color */
164       if (bg->pattern)
165         {
166           p = bg->pattern;
167           c = &other_bg;
168           progress = 1 - progress;
169         }
170       else
171         {
172           p = other_pattern;
173           c = &bg->bg_color;
174         }
175
176       if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
177         {
178           cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
179           new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
180         }
181       else
182         {
183           cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
184           new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
185         }
186
187       cairo_pattern_get_color_stop_count (p, &n);
188
189       for (i = 0; i < n; i++)
190         {
191           gdouble red1, green1, blue1, alpha1;
192           gdouble offset;
193
194           cairo_pattern_get_color_stop_rgba (p, i,
195                                              &offset,
196                                              &red1, &green1, &blue1,
197                                              &alpha1);
198           cairo_pattern_add_color_stop_rgba (new_pattern, offset,
199                                              c->red + ((red1 - c->red) * progress),
200                                              c->green + ((green1 - c->green) * progress),
201                                              c->blue + ((blue1 - c->blue) * progress),
202                                              c->alpha + ((alpha1 - c->alpha) * progress));
203         }
204     }
205   else
206     {
207       /* Merge just colors */
208       new_pattern = cairo_pattern_create_rgba (CLAMP (bg->bg_color.red + ((other_bg.red - bg->bg_color.red) * progress), 0, 1),
209                                                CLAMP (bg->bg_color.green + ((other_bg.green - bg->bg_color.green) * progress), 0, 1),
210                                                CLAMP (bg->bg_color.blue + ((other_bg.blue - bg->bg_color.blue) * progress), 0, 1),
211                                                CLAMP (bg->bg_color.alpha + ((other_bg.alpha - bg->bg_color.alpha) * progress), 0, 1));
212     }
213
214   if (new_pattern)
215     {
216       /* Replace pattern to use */
217       cairo_pattern_destroy (bg->pattern);
218       bg->pattern = new_pattern;
219     }
220
221   if (other_pattern)
222     cairo_pattern_destroy (other_pattern);
223 }
224
225 static void
226 _gtk_theming_background_apply_origin (GtkThemingBackground *bg)
227 {
228   GtkCssArea origin;
229   cairo_rectangle_t image_rect;
230
231   gtk_theming_engine_get (bg->engine, bg->flags,
232                           "background-origin", &origin,
233                           NULL);
234
235   /* The default size of the background image depends on the
236      background-origin value as this affects the top left
237      and the bottom right corners. */
238   switch (origin) {
239   case GTK_CSS_AREA_BORDER_BOX:
240     image_rect.x = 0;
241     image_rect.y = 0;
242     image_rect.width = bg->paint_area.width;
243     image_rect.height = bg->paint_area.height;
244     break;
245   case GTK_CSS_AREA_CONTENT_BOX:
246     image_rect.x = bg->border.left + bg->padding.left;
247     image_rect.y = bg->border.top + bg->padding.top;
248     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
249     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
250     break;
251   case GTK_CSS_AREA_PADDING_BOX:
252   default:
253     image_rect.x = bg->border.left;
254     image_rect.y = bg->border.top;
255     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
256     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
257     break;
258   }
259
260   bg->image_rect = image_rect;
261 }
262
263 static void
264 _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
265 {
266   GtkCssArea clip;
267
268   gtk_theming_engine_get (bg->engine, bg->flags,
269                           "background-clip", &clip,
270                           NULL);
271
272   if (clip == GTK_CSS_AREA_PADDING_BOX)
273     {
274       _gtk_rounded_box_shrink (&bg->clip_box,
275                                bg->border.top, bg->border.right,
276                                bg->border.bottom, bg->border.left);
277     }
278   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
279     {
280       _gtk_rounded_box_shrink (&bg->clip_box,
281                                bg->border.top + bg->padding.top,
282                                bg->border.right + bg->padding.right,
283                                bg->border.bottom + bg->padding.bottom,
284                                bg->border.left + bg->padding.left);
285     }
286 }
287
288 static void
289 _gtk_theming_background_paint (GtkThemingBackground *bg,
290                                cairo_t              *cr)
291 {
292   _gtk_rounded_box_path (&bg->clip_box, cr);
293   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
294
295   if (bg->pattern)
296     {
297       GtkCssBackgroundRepeat *repeat;
298       cairo_surface_t *surface;
299       int scale_width, scale_height;
300
301       gtk_theming_engine_get (bg->engine, bg->flags,
302                               "background-repeat", &repeat,
303                               NULL);
304
305       if (cairo_pattern_get_surface (bg->pattern, &surface) != CAIRO_STATUS_SUCCESS)
306         surface = NULL;
307
308       if (surface && repeat)
309         {
310           scale_width = cairo_image_surface_get_width (surface);
311           scale_height = cairo_image_surface_get_height (surface);
312           if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT)
313             cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_REPEAT);
314           else if (repeat->repeat == GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT)
315             cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_NONE);
316         }
317       else
318         {
319           cairo_pattern_set_extend (bg->pattern, CAIRO_EXTEND_PAD);
320           scale_width = bg->image_rect.width;
321           scale_height = bg->image_rect.height;
322         }
323
324       if (scale_width && scale_height)
325         {
326           /* Fill background color first */
327           cairo_fill_preserve (cr);
328
329           cairo_translate (cr, bg->image_rect.x, bg->image_rect.y);
330           cairo_scale (cr, scale_width, scale_height);
331           cairo_set_source (cr, bg->pattern);
332           cairo_scale (cr, 1.0 / scale_width, 1.0 / scale_height);
333           cairo_translate (cr, -bg->image_rect.x, -bg->image_rect.y);
334
335           g_free (repeat);
336
337           cairo_pattern_destroy (bg->pattern);
338           bg->pattern = NULL;
339         }
340     }
341
342   cairo_fill (cr);
343 }
344
345 static void
346 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
347                                       cairo_t              *cr)
348 {
349   GtkShadow *box_shadow;
350
351   gtk_theming_engine_get (bg->engine, bg->flags,
352                           "box-shadow", &box_shadow,
353                           NULL);
354
355   if (box_shadow != NULL)
356     {
357       _gtk_box_shadow_render (box_shadow, cr, &bg->padding_box);
358       _gtk_shadow_unref (box_shadow);
359     }
360 }
361
362 static void
363 _gtk_theming_background_init_engine (GtkThemingBackground *bg)
364 {
365   bg->flags = gtk_theming_engine_get_state (bg->engine);
366
367   gtk_theming_engine_get_border (bg->engine, bg->flags, &bg->border);
368   gtk_theming_engine_get_padding (bg->engine, bg->flags, &bg->padding);
369   gtk_theming_engine_get_background_color (bg->engine, bg->flags, &bg->bg_color);
370
371   /* In the CSS box model, by default the background positioning area is
372    * the padding-box, i.e. all the border-box minus the borders themselves,
373    * which determines also its default size, see
374    * http://dev.w3.org/csswg/css3-background/#background-origin
375    *
376    * In the future we might want to support different origins or clips, but
377    * right now we just shrink to the default.
378    */
379   _gtk_rounded_box_init_rect (&bg->padding_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
380   _gtk_rounded_box_apply_border_radius (&bg->padding_box, bg->engine, bg->flags, bg->junction);
381
382   bg->clip_box = bg->padding_box;
383   _gtk_rounded_box_shrink (&bg->padding_box,
384                            bg->border.top, bg->border.right,
385                            bg->border.bottom, bg->border.left);
386
387   _gtk_theming_background_apply_clip (bg);
388   _gtk_theming_background_apply_origin (bg);
389
390   gtk_theming_engine_get (bg->engine, bg->flags,
391                           "background-image", &bg->pattern,
392                           NULL);
393 }
394
395 void
396 _gtk_theming_background_init (GtkThemingBackground *bg,
397                               GtkThemingEngine     *engine,
398                               gdouble               x,
399                               gdouble               y,
400                               gdouble               width,
401                               gdouble               height,
402                               GtkJunctionSides      junction)
403 {
404   g_assert (bg != NULL);
405
406   bg->engine = engine;
407
408   bg->paint_area.x = x;
409   bg->paint_area.y = y;
410   bg->paint_area.width = width;
411   bg->paint_area.height = height;
412
413   bg->pattern = NULL;
414   bg->junction = junction;
415
416   _gtk_theming_background_init_engine (bg);
417 }
418
419 void
420 _gtk_theming_background_render (GtkThemingBackground *bg,
421                                 cairo_t              *cr)
422 {
423   cairo_save (cr);
424   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
425
426   _gtk_theming_background_apply_window_background (bg, cr);
427   _gtk_theming_background_apply_running_transformation (bg, cr);
428   _gtk_theming_background_paint (bg, cr);
429   _gtk_theming_background_apply_shadow (bg, cr);
430
431   cairo_restore (cr);
432 }
433
434 gboolean
435 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
436 {
437   return (bg->pattern != NULL) ? TRUE : FALSE;
438 }