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