]> Pileus Git - ~andy/gtk/blob - gtk/gtkborderimage.c
399e869c4a92d667ac6c3fa980519211a1db2f1d
[~andy/gtk] / gtk / gtkborderimage.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 #include <cairo-gobject.h>
24
25 #include <math.h>
26
27 #include "gtkborderimageprivate.h"
28 #include "gtkcssimagevalueprivate.h"
29 #include "gtkstylepropertiesprivate.h"
30 #include "gtkthemingengineprivate.h"
31
32 /* this is in case round() is not provided by the compiler, 
33  * such as in the case of C89 compilers, like MSVC
34  */
35 #include "fallback-c89.c"
36
37 gboolean
38 _gtk_border_image_init (GtkBorderImage   *image,
39                         GtkThemingEngine *engine)
40 {
41   GtkBorder *width;
42
43   image->source = _gtk_css_image_value_get_image (_gtk_theming_engine_peek_property (engine, "border-image-source"));
44   if (image->source == NULL)
45     return FALSE;
46
47   image->slice = *(GtkBorder *) _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-slice"));
48   width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, "border-image-width"));
49   if (width)
50     {
51       image->width = *width;
52       image->has_width = TRUE;
53     }
54   else
55     image->has_width = FALSE;
56
57   image->repeat = *_gtk_css_value_get_border_image_repeat (_gtk_theming_engine_peek_property (engine, "border-image-repeat"));
58
59   return TRUE;
60 }
61
62 typedef struct _GtkBorderImageSliceSize GtkBorderImageSliceSize;
63 struct _GtkBorderImageSliceSize {
64   double offset;
65   double size;
66 };
67
68 static void
69 gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
70                                       double                  offset,
71                                       double                  area_size,
72                                       int                     start_border,
73                                       int                     end_border)
74 {
75   /* This code assumes area_size >= start_border + end_border */
76
77   sizes[0].offset = offset;
78   sizes[0].size = start_border;
79   sizes[1].offset = offset + start_border;
80   sizes[1].size = area_size - start_border - end_border;
81   sizes[2].offset = offset + area_size - end_border;
82   sizes[2].size = end_border;
83 }
84
85 static void
86 gtk_border_image_render_slice (cairo_t           *cr,
87                                cairo_surface_t   *slice,
88                                double             slice_width,
89                                double             slice_height,
90                                double             x,
91                                double             y,
92                                double             width,
93                                double             height,
94                                GtkCssBorderRepeatStyle  hrepeat,
95                                GtkCssBorderRepeatStyle  vrepeat)
96 {
97   double hscale, vscale;
98   double xstep, ystep;
99   cairo_extend_t extend = CAIRO_EXTEND_PAD;
100   cairo_matrix_t matrix;
101   cairo_pattern_t *pattern;
102
103   /* We can't draw center tiles yet */
104   g_assert (hrepeat == GTK_CSS_REPEAT_STYLE_STRETCH || vrepeat == GTK_CSS_REPEAT_STYLE_STRETCH);
105
106   hscale = width / slice_width;
107   vscale = height / slice_height;
108   xstep = width;
109   ystep = height;
110
111   switch (hrepeat)
112     {
113     case GTK_CSS_REPEAT_STYLE_REPEAT:
114       extend = CAIRO_EXTEND_REPEAT;
115       hscale = vscale;
116       break;
117     case GTK_CSS_REPEAT_STYLE_SPACE:
118       {
119         double space, n;
120
121         extend = CAIRO_EXTEND_NONE;
122         hscale = vscale;
123
124         xstep = hscale * slice_width;
125         n = floor (width / xstep);
126         space = (width - n * xstep) / (n + 1);
127         xstep += space;
128         x += space;
129         width -= 2 * space;
130       }
131       break;
132     case GTK_CSS_REPEAT_STYLE_STRETCH:
133       break;
134     case GTK_CSS_REPEAT_STYLE_ROUND:
135       extend = CAIRO_EXTEND_REPEAT;
136       hscale = width / (slice_width * MAX (round (width / (slice_width * vscale)), 1));
137       break;
138     default:
139       g_assert_not_reached ();
140       break;
141     }
142
143   switch (vrepeat)
144     {
145     case GTK_CSS_REPEAT_STYLE_REPEAT:
146       extend = CAIRO_EXTEND_REPEAT;
147       vscale = hscale;
148       break;
149     case GTK_CSS_REPEAT_STYLE_SPACE:
150       {
151         double space, n;
152
153         extend = CAIRO_EXTEND_NONE;
154         vscale = hscale;
155
156         ystep = vscale * slice_height;
157         n = floor (height / ystep);
158         space = (height - n * ystep) / (n + 1);
159         ystep += space;
160         y += space;
161         height -= 2 * space;
162       }
163       break;
164     case GTK_CSS_REPEAT_STYLE_STRETCH:
165       break;
166     case GTK_CSS_REPEAT_STYLE_ROUND:
167       extend = CAIRO_EXTEND_REPEAT;
168       vscale = height / (slice_height * MAX (round (height / (slice_height * hscale)), 1));
169       break;
170     default:
171       g_assert_not_reached ();
172       break;
173     }
174
175   pattern = cairo_pattern_create_for_surface (slice);
176
177   cairo_matrix_init_translate (&matrix,
178                                hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_width / 2 : 0,
179                                vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? slice_height / 2 : 0);
180   cairo_matrix_scale (&matrix, 1 / hscale, 1 / vscale);
181   cairo_matrix_translate (&matrix,
182                           hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - width / 2 : 0,
183                           vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT ? - height / 2 : 0);
184
185   cairo_pattern_set_matrix (pattern, &matrix);
186   cairo_pattern_set_extend (pattern, extend);
187
188   cairo_save (cr);
189   cairo_translate (cr, x, y);
190
191   for (y = 0; y < height; y += ystep)
192     {
193       for (x = 0; x < width; x += xstep)
194         {
195           cairo_save (cr);
196           cairo_translate (cr, x, y);
197           cairo_set_source (cr, pattern);
198           cairo_rectangle (cr, 0, 0, xstep, ystep);
199           cairo_fill (cr);
200           cairo_restore (cr);
201         }
202     }
203
204   cairo_restore (cr);
205
206   cairo_pattern_destroy (pattern);
207 }
208
209 static void
210 gtk_border_image_compute_slice_size (GtkBorderImageSliceSize sizes[3],
211                                      int                     surface_size,
212                                      int                     start_size,
213                                      int                     end_size)
214 {
215   sizes[0].size = MIN (start_size, surface_size);
216   sizes[0].offset = 0;
217
218   sizes[2].size = MIN (end_size, surface_size);
219   sizes[2].offset = surface_size - sizes[2].size;
220
221   sizes[1].size = MAX (0, surface_size - sizes[0].size - sizes[2].size);
222   sizes[1].offset = sizes[0].size;
223 }
224
225 void
226 _gtk_border_image_render (GtkBorderImage   *image,
227                           GtkBorder        *border_width,
228                           cairo_t          *cr,
229                           gdouble           x,
230                           gdouble           y,
231                           gdouble           width,
232                           gdouble           height)
233 {
234   cairo_surface_t *surface, *slice;
235   GtkBorderImageSliceSize vertical_slice[3], horizontal_slice[3];
236   GtkBorderImageSliceSize vertical_border[3], horizontal_border[3];
237   double source_width, source_height;
238   int h, v;
239
240   if (image->has_width)
241     border_width = &image->width;
242
243   _gtk_css_image_get_concrete_size (image->source,
244                                     0, 0,
245                                     width, height,
246                                     &source_width, &source_height);
247
248   /* XXX: Optimize for (source_width == width && source_height == height) */
249
250   surface = _gtk_css_image_get_surface (image->source,
251                                         cairo_get_target (cr),
252                                         source_width, source_height);
253
254   gtk_border_image_compute_slice_size (horizontal_slice,
255                                        source_width, 
256                                        image->slice.left,
257                                        image->slice.right);
258   gtk_border_image_compute_slice_size (vertical_slice,
259                                        source_height, 
260                                        image->slice.top,
261                                        image->slice.bottom);
262   gtk_border_image_compute_border_size (horizontal_border,
263                                         x,
264                                         width,
265                                         border_width->left,
266                                         border_width->right);
267   gtk_border_image_compute_border_size (vertical_border,
268                                         y,
269                                         height,
270                                         border_width->top,
271                                         border_width->bottom);
272   
273   for (v = 0; v < 3; v++)
274     {
275       if (vertical_slice[v].size == 0 ||
276           vertical_border[v].size == 0)
277         continue;
278
279       for (h = 0; h < 3; h++)
280         {
281           if (horizontal_slice[h].size == 0 ||
282               horizontal_border[h].size == 0)
283             continue;
284
285           if (h == 1 && v == 1)
286             continue;
287
288           slice = cairo_surface_create_for_rectangle (surface,
289                                                       horizontal_slice[h].offset,
290                                                       vertical_slice[v].offset,
291                                                       horizontal_slice[h].size,
292                                                       vertical_slice[v].size);
293
294           gtk_border_image_render_slice (cr,
295                                          slice,
296                                          horizontal_slice[h].size,
297                                          vertical_slice[v].size,
298                                          horizontal_border[h].offset,
299                                          vertical_border[v].offset,
300                                          horizontal_border[h].size,
301                                          vertical_border[v].size,
302                                          h == 1 ? image->repeat.hrepeat : GTK_CSS_REPEAT_STYLE_STRETCH,
303                                          v == 1 ? image->repeat.vrepeat : GTK_CSS_REPEAT_STYLE_STRETCH);
304
305           cairo_surface_destroy (slice);
306         }
307     }
308
309   cairo_surface_destroy (surface);
310 }