]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingbackground.c
themingbackground: Get rid of flags variable
[~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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "config.h"
23
24 #include "gtkthemingbackgroundprivate.h"
25
26 #include "gtkcssarrayvalueprivate.h"
27 #include "gtkcssbgsizevalueprivate.h"
28 #include "gtkcssenumvalueprivate.h"
29 #include "gtkcssimagevalueprivate.h"
30 #include "gtkcssshadowsvalueprivate.h"
31 #include "gtkcsspositionvalueprivate.h"
32 #include "gtkcssrepeatvalueprivate.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkthemingengineprivate.h"
35
36 #include <math.h>
37
38 #include <gdk/gdk.h>
39
40 /* this is in case round() is not provided by the compiler, 
41  * such as in the case of C89 compilers, like MSVC
42  */
43 #include "fallback-c89.c"
44
45 typedef struct {
46   cairo_rectangle_t image_rect;
47   GtkRoundedBox clip_box;
48
49   gint idx;
50 } GtkThemingBackgroundLayer;
51
52 static void
53 _gtk_theming_background_layer_apply_origin (GtkThemingBackground *bg,
54                                             GtkThemingBackgroundLayer *layer)
55 {
56   cairo_rectangle_t image_rect;
57   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN);
58   GtkCssArea origin = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
59
60   /* The default size of the background image depends on the
61      background-origin value as this affects the top left
62      and the bottom right corners. */
63   switch (origin) {
64   case GTK_CSS_AREA_BORDER_BOX:
65     image_rect.x = 0;
66     image_rect.y = 0;
67     image_rect.width = bg->paint_area.width;
68     image_rect.height = bg->paint_area.height;
69     break;
70   case GTK_CSS_AREA_CONTENT_BOX:
71     image_rect.x = bg->border.left + bg->padding.left;
72     image_rect.y = bg->border.top + bg->padding.top;
73     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right - bg->padding.left - bg->padding.right;
74     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom - bg->padding.top - bg->padding.bottom;
75     break;
76   case GTK_CSS_AREA_PADDING_BOX:
77   default:
78     image_rect.x = bg->border.left;
79     image_rect.y = bg->border.top;
80     image_rect.width = bg->paint_area.width - bg->border.left - bg->border.right;
81     image_rect.height = bg->paint_area.height - bg->border.top - bg->border.bottom;
82     break;
83   }
84
85   /* XXX: image_rect might have negative width/height here.
86    * Do we need to do something about it? */
87   layer->image_rect = image_rect;
88 }
89
90 static void
91 _gtk_theming_background_apply_clip (GtkThemingBackground *bg,
92                                     GtkRoundedBox *box,
93                                     GtkCssArea clip)
94 {
95   if (clip == GTK_CSS_AREA_PADDING_BOX)
96     {
97       _gtk_rounded_box_shrink (box,
98                                bg->border.top, bg->border.right,
99                                bg->border.bottom, bg->border.left);
100     }
101   else if (clip == GTK_CSS_AREA_CONTENT_BOX)
102     {
103       _gtk_rounded_box_shrink (box,
104                                bg->border.top + bg->padding.top,
105                                bg->border.right + bg->padding.right,
106                                bg->border.bottom + bg->padding.bottom,
107                                bg->border.left + bg->padding.left);
108     }
109 }
110
111 static void
112 _gtk_theming_background_layer_apply_clip (GtkThemingBackground *bg,
113                                           GtkThemingBackgroundLayer *layer)
114 {
115   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP);
116   GtkCssArea clip = _gtk_css_area_value_get (_gtk_css_array_value_get_nth (value, layer->idx));
117
118   _gtk_theming_background_apply_clip (bg, &layer->clip_box, clip);
119 }
120
121 static void
122 _gtk_theming_background_paint_color (GtkThemingBackground *bg,
123                                      cairo_t              *cr,
124                                      GtkCssValue          *background_image)
125 {
126   GtkRoundedBox clip_box;
127   gint n_values = _gtk_css_array_value_get_n_values (background_image);
128   GtkCssArea clip = _gtk_css_area_value_get 
129     (_gtk_css_array_value_get_nth 
130      (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP), 
131       n_values - 1));
132
133   clip_box = bg->border_box;
134   _gtk_theming_background_apply_clip (bg, &clip_box, clip);
135
136   cairo_save (cr);
137   _gtk_rounded_box_path (&clip_box, cr);
138   cairo_clip (cr);
139
140   gdk_cairo_set_source_rgba (cr, &bg->bg_color);
141   cairo_paint (cr);
142
143   cairo_restore (cr);
144 }
145
146 static void
147 _gtk_theming_background_paint_layer (GtkThemingBackground *bg,
148                                      GtkThemingBackgroundLayer *layer,
149                                      cairo_t              *cr)
150 {
151   GtkCssRepeatStyle hrepeat, vrepeat;
152   const GtkCssValue *pos, *repeat;
153   GtkCssImage *image;
154   double image_width, image_height;
155   double width, height;
156
157   pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), layer->idx);
158   repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), layer->idx);
159   hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
160   vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
161   image = _gtk_css_image_value_get_image (
162               _gtk_css_array_value_get_nth (
163                   _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
164                   layer->idx));
165   width = layer->image_rect.width;
166   height = layer->image_rect.height;
167
168   if (image == NULL || width <= 0 || height <= 0)
169     return;
170
171   _gtk_css_bg_size_value_compute_size (_gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE), layer->idx),
172                                        image,
173                                        width,
174                                        height,
175                                        &image_width,
176                                        &image_height);
177
178   if (image_width <= 0 || image_height <= 0)
179     return;
180
181   /* optimization */
182   if (image_width == width)
183     hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
184   if (image_height == height)
185     vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
186
187
188   cairo_save (cr);
189
190   _gtk_rounded_box_path (&layer->clip_box, cr);
191   cairo_clip (cr);
192
193
194   cairo_translate (cr, layer->image_rect.x, layer->image_rect.y);
195
196   if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
197     {
198       cairo_translate (cr,
199                        _gtk_css_position_value_get_x (pos, width - image_width),
200                        _gtk_css_position_value_get_y (pos, height - image_height));
201       /* shortcut for normal case */
202       _gtk_css_image_draw (image, cr, image_width, image_height);
203     }
204   else
205     {
206       int surface_width, surface_height;
207       cairo_rectangle_t fill_rect;
208       cairo_surface_t *surface;
209       cairo_t *cr2;
210
211       /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
212        * there is a second step. The UA must scale the image in that
213        * dimension (or both dimensions) so that it fits a whole number of
214        * times in the background positioning area. In the case of the width
215        * (height is analogous):
216        *
217        * If X ≠ 0 is the width of the image after step one and W is the width
218        * of the background positioning area, then the rounded width
219        * X' = W / round(W / X) where round() is a function that returns the
220        * nearest natural number (integer greater than zero). 
221        *
222        * If ‘background-repeat’ is ‘round’ for one dimension only and if
223        * ‘background-size’ is ‘auto’ for the other dimension, then there is
224        * a third step: that other dimension is scaled so that the original
225        * aspect ratio is restored. 
226        */
227       if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
228         {
229           double n = round (width / image_width);
230
231           n = MAX (1, n);
232
233           if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
234               /* && vsize == auto (it is by default) */)
235             image_height *= width / (image_width * n);
236           image_width = width / n;
237         }
238       if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
239         {
240           double n = round (height / image_height);
241
242           n = MAX (1, n);
243
244           if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
245               /* && hsize == auto (it is by default) */)
246             image_width *= height / (image_height * n);
247           image_height = height / n;
248         }
249
250       /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
251        * to store the extra space. */
252       if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
253         {
254           double n = floor (width / image_width);
255           surface_width = n ? round (width / n) : 0;
256         }
257       else
258         surface_width = round (image_width);
259
260       if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
261         {
262           double n = floor (height / image_height);
263           surface_height = n ? round (height / n) : 0;
264         }
265       else
266         surface_height = round (image_height);
267
268       surface = cairo_surface_create_similar (cairo_get_target (cr),
269                                               CAIRO_CONTENT_COLOR_ALPHA,
270                                               surface_width, surface_height);
271       cr2 = cairo_create (surface);
272       cairo_translate (cr2,
273                        0.5 * (surface_width - image_width),
274                        0.5 * (surface_height - image_height));
275       _gtk_css_image_draw (image, cr2, image_width, image_height);
276       cairo_destroy (cr2);
277
278       cairo_set_source_surface (cr, surface,
279                                 _gtk_css_position_value_get_x (pos, width - image_width),
280                                 _gtk_css_position_value_get_y (pos, height - image_height));
281       cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
282       cairo_surface_destroy (surface);
283
284       if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
285         {
286           fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
287           fill_rect.width = image_width;
288         }
289       else
290         {
291           fill_rect.x = 0;
292           fill_rect.width = width;
293         }
294
295       if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
296         {
297           fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
298           fill_rect.height = image_height;
299         }
300       else
301         {
302           fill_rect.y = 0;
303           fill_rect.height = height;
304         }
305
306       cairo_rectangle (cr, fill_rect.x, fill_rect.y,
307                        fill_rect.width, fill_rect.height);
308       cairo_fill (cr);
309     }
310
311
312   cairo_restore (cr);
313 }
314
315 static void
316 _gtk_theming_background_apply_shadow (GtkThemingBackground *bg,
317                                       cairo_t              *cr)
318 {
319   _gtk_css_shadows_value_paint_box (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BOX_SHADOW),
320                                     cr,
321                                     &bg->padding_box);
322 }
323
324 static void
325 _gtk_theming_background_init_layer (GtkThemingBackground *bg,
326                                     GtkThemingBackgroundLayer *layer,
327                                     gint idx)
328 {
329   layer->idx = idx;
330   layer->clip_box = bg->border_box;
331
332   _gtk_theming_background_layer_apply_clip (bg, layer);
333   _gtk_theming_background_layer_apply_origin (bg, layer);
334 }
335
336 static void
337 _gtk_theming_background_init_context (GtkThemingBackground *bg)
338 {
339   GtkStateFlags flags = gtk_style_context_get_state (bg->context);
340
341   gtk_style_context_get_border (bg->context, flags, &bg->border);
342   gtk_style_context_get_padding (bg->context, flags, &bg->padding);
343   gtk_style_context_get_background_color (bg->context, flags, &bg->bg_color);
344
345   /* In the CSS box model, by default the background positioning area is
346    * the padding-box, i.e. all the border-box minus the borders themselves,
347    * which determines also its default size, see
348    * http://dev.w3.org/csswg/css3-background/#background-origin
349    *
350    * In the future we might want to support different origins or clips, but
351    * right now we just shrink to the default.
352    */
353   _gtk_rounded_box_init_rect (&bg->border_box, 0, 0, bg->paint_area.width, bg->paint_area.height);
354   _gtk_rounded_box_apply_border_radius_for_context (&bg->border_box, bg->context, bg->junction);
355
356   bg->padding_box = bg->border_box;
357   _gtk_rounded_box_shrink (&bg->padding_box,
358                            bg->border.top, bg->border.right,
359                            bg->border.bottom, bg->border.left);
360 }
361
362 void
363 _gtk_theming_background_init (GtkThemingBackground *bg,
364                               GtkThemingEngine     *engine,
365                               gdouble               x,
366                               gdouble               y,
367                               gdouble               width,
368                               gdouble               height,
369                               GtkJunctionSides      junction)
370 {
371   GtkStyleContext *context;
372
373   g_assert (bg != NULL);
374
375   context = _gtk_theming_engine_get_context (engine);
376   _gtk_theming_background_init_from_context (bg, context,
377                                              x, y, width, height,
378                                              junction);
379 }
380
381 void
382 _gtk_theming_background_init_from_context (GtkThemingBackground *bg,
383                                            GtkStyleContext      *context,
384                                            gdouble               x,
385                                            gdouble               y,
386                                            gdouble               width,
387                                            gdouble               height,
388                                            GtkJunctionSides      junction)
389 {
390   g_assert (bg != NULL);
391
392   bg->context = context;
393
394   bg->paint_area.x = x;
395   bg->paint_area.y = y;
396   bg->paint_area.width = width;
397   bg->paint_area.height = height;
398
399   bg->junction = junction;
400
401   _gtk_theming_background_init_context (bg);
402 }
403
404 void
405 _gtk_theming_background_render (GtkThemingBackground *bg,
406                                 cairo_t              *cr)
407 {
408   gint idx;
409   GtkThemingBackgroundLayer layer;
410   GtkCssValue *background_image;
411
412   background_image = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
413
414   cairo_save (cr);
415   cairo_translate (cr, bg->paint_area.x, bg->paint_area.y);
416
417   _gtk_theming_background_paint_color (bg, cr, background_image);
418
419   for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
420     {
421       _gtk_theming_background_init_layer (bg, &layer, idx);
422       _gtk_theming_background_paint_layer (bg, &layer, cr);
423     }
424
425   _gtk_theming_background_apply_shadow (bg, cr);
426
427   cairo_restore (cr);
428 }
429
430 gboolean
431 _gtk_theming_background_has_background_image (GtkThemingBackground *bg)
432 {
433   GtkCssImage *image;
434   GtkCssValue *value = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
435
436   if (_gtk_css_array_value_get_n_values (value) == 0)
437     return FALSE;
438
439   image = _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (value, 0));
440   return (image != NULL);
441 }