1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtk9slice.h"
28 BORDER_TOP = BORDER_LEFT,
29 BORDER_BOTTOM = BORDER_RIGHT
41 cairo_surface_t *surfaces[BORDER_LAST][BORDER_LAST];
42 GtkSliceSideModifier modifiers[4];
47 G_DEFINE_BOXED_TYPE (Gtk9Slice, _gtk_9slice,
48 _gtk_9slice_ref, _gtk_9slice_unref)
52 _gtk_9slice_new (GdkPixbuf *pixbuf,
54 gdouble distance_bottom,
55 gdouble distance_left,
56 gdouble distance_right,
57 GtkSliceSideModifier horizontal_modifier,
58 GtkSliceSideModifier vertical_modifier)
61 cairo_surface_t *surface;
65 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
67 slice = g_slice_new0 (Gtk9Slice);
70 slice->distances[SIDE_TOP] = distance_top;
71 slice->distances[SIDE_BOTTOM] = distance_bottom;
72 slice->distances[SIDE_LEFT] = distance_left;
73 slice->distances[SIDE_RIGHT] = distance_right;
75 slice->modifiers[SIDE_TOP] = slice->modifiers[SIDE_BOTTOM] = horizontal_modifier;
76 slice->modifiers[SIDE_LEFT] = slice->modifiers[SIDE_RIGHT] = vertical_modifier;
78 width = gdk_pixbuf_get_width (pixbuf);
79 height = gdk_pixbuf_get_height (pixbuf);
81 /* Get an image surface from the pixbuf */
82 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
83 cr = cairo_create (surface);
85 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
90 /* Top /left corner */
91 slice->surfaces[BORDER_LEFT][BORDER_TOP] = cairo_surface_create_similar (surface, CAIRO_CONTENT_COLOR_ALPHA,
92 distance_left, distance_top);
93 cr = cairo_create (slice->surfaces[BORDER_LEFT][BORDER_TOP]);
95 cairo_set_source_surface (cr, surface, 0, 0);
100 /* Top/right corner */
101 slice->surfaces[BORDER_RIGHT][BORDER_TOP] = cairo_surface_create_similar (surface, CAIRO_CONTENT_COLOR_ALPHA,
102 distance_right, distance_top);
103 cr = cairo_create (slice->surfaces[BORDER_RIGHT][BORDER_TOP]);
105 cairo_set_source_surface (cr, surface, - width + distance_right, 0);
110 /* Bottom/left corner */
111 slice->surfaces[BORDER_LEFT][BORDER_BOTTOM] = cairo_surface_create_similar (surface, CAIRO_CONTENT_COLOR_ALPHA,
112 distance_left, distance_bottom);
113 cr = cairo_create (slice->surfaces[BORDER_LEFT][BORDER_BOTTOM]);
115 cairo_set_source_surface (cr, surface, 0, - height + distance_bottom);
120 /* Bottom/right corner */
121 slice->surfaces[BORDER_RIGHT][BORDER_BOTTOM] = cairo_surface_create_similar (surface, CAIRO_CONTENT_COLOR_ALPHA,
122 distance_right, distance_bottom);
123 cr = cairo_create (slice->surfaces[BORDER_RIGHT][BORDER_BOTTOM]);
125 cairo_set_source_surface (cr, surface, - width + distance_right, - height + distance_bottom);
131 slice->surfaces[BORDER_MIDDLE][BORDER_TOP] = cairo_surface_create_similar (surface,
132 CAIRO_CONTENT_COLOR_ALPHA,
133 width - distance_left - distance_right,
135 cr = cairo_create (slice->surfaces[BORDER_MIDDLE][BORDER_TOP]);
136 cairo_set_source_surface (cr, surface, - distance_left, 0);
141 slice->surfaces[BORDER_MIDDLE][BORDER_BOTTOM] = cairo_surface_create_similar (surface,
142 CAIRO_CONTENT_COLOR_ALPHA,
143 width - distance_left - distance_right,
145 cr = cairo_create (slice->surfaces[BORDER_MIDDLE][BORDER_BOTTOM]);
146 cairo_set_source_surface (cr, surface, - distance_left, - height + distance_bottom);
151 slice->surfaces[BORDER_LEFT][BORDER_MIDDLE] = cairo_surface_create_similar (surface,
152 CAIRO_CONTENT_COLOR_ALPHA,
154 height - distance_top - distance_bottom);
155 cr = cairo_create (slice->surfaces[BORDER_LEFT][BORDER_MIDDLE]);
156 cairo_set_source_surface (cr, surface, 0, - distance_top);
161 slice->surfaces[BORDER_RIGHT][BORDER_MIDDLE] = cairo_surface_create_similar (surface,
162 CAIRO_CONTENT_COLOR_ALPHA,
164 height - distance_top - distance_bottom);
165 cr = cairo_create (slice->surfaces[BORDER_RIGHT][BORDER_MIDDLE]);
166 cairo_set_source_surface (cr, surface, - width + distance_right, - distance_top);
171 slice->surfaces[BORDER_MIDDLE][BORDER_MIDDLE] = cairo_surface_create_similar (surface,
172 CAIRO_CONTENT_COLOR_ALPHA,
173 width - distance_left - distance_right,
174 height - distance_top - distance_bottom);
175 cr = cairo_create (slice->surfaces[BORDER_MIDDLE][BORDER_MIDDLE]);
176 cairo_set_source_surface (cr, surface, - distance_left, - distance_top);
180 cairo_surface_destroy (surface);
186 render_border (cairo_t *cr,
187 cairo_surface_t *surface,
193 GtkSliceSideModifier modifier)
195 cairo_pattern_t *pattern;
196 cairo_matrix_t matrix;
200 cairo_rectangle (cr, x, y, width, height);
203 pattern = cairo_pattern_create_for_surface (surface);
205 if (modifier == GTK_SLICE_REPEAT)
207 cairo_matrix_init_translate (&matrix, - x, - y);
208 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
210 cairo_pattern_set_matrix (pattern, &matrix);
211 cairo_set_source (cr, pattern);
213 cairo_rectangle (cr, x, y, width, height);
218 /* Use nearest filter so borders aren't blurred */
219 cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
221 if (side == SIDE_TOP || side == SIDE_BOTTOM)
223 gint w = cairo_image_surface_get_width (surface);
225 cairo_translate (cr, x, y);
226 cairo_scale (cr, width / w, 1.0);
230 gint h = cairo_image_surface_get_height (surface);
232 cairo_translate (cr, x, y);
233 cairo_scale (cr, 1.0, height / h);
236 cairo_set_source (cr, pattern);
237 cairo_rectangle (cr, x, y, width, height);
243 cairo_pattern_destroy (pattern);
247 render_corner (cairo_t *cr,
248 cairo_surface_t *surface,
256 cairo_rectangle (cr, x, y, width, height);
259 cairo_set_source_surface (cr, surface, x, y);
260 cairo_rectangle (cr, x, y, width, height);
267 _gtk_9slice_render (Gtk9Slice *slice,
274 int img_width, img_height;
275 cairo_surface_t *surface;
280 surface = slice->surfaces[BORDER_MIDDLE][BORDER_TOP];
281 img_height = cairo_image_surface_get_height (surface);
283 render_border (cr, surface, SIDE_TOP,
284 x + slice->distances[SIDE_LEFT], y,
285 width - slice->distances[SIDE_LEFT] - slice->distances[SIDE_RIGHT],
286 (gdouble) img_height,
287 slice->modifiers[SIDE_TOP]);
290 surface = slice->surfaces[BORDER_MIDDLE][BORDER_BOTTOM];
291 img_height = cairo_image_surface_get_height (surface);
293 render_border (cr, surface, SIDE_BOTTOM,
294 x + slice->distances[SIDE_LEFT], y + height - img_height,
295 width - slice->distances[SIDE_LEFT] - slice->distances[SIDE_RIGHT],
296 (gdouble) img_height,
297 slice->modifiers[SIDE_BOTTOM]);
300 surface = slice->surfaces[BORDER_LEFT][BORDER_MIDDLE];
301 img_width = cairo_image_surface_get_width (surface);
303 render_border (cr, surface, SIDE_LEFT,
304 x, y + slice->distances[SIDE_TOP],
306 height - slice->distances[SIDE_TOP] - slice->distances[SIDE_BOTTOM],
307 slice->modifiers[SIDE_LEFT]);
310 surface = slice->surfaces[BORDER_RIGHT][BORDER_MIDDLE];
311 img_width = cairo_image_surface_get_width (surface);
313 render_border (cr, surface, SIDE_RIGHT,
314 x + width - img_width, y + slice->distances[SIDE_TOP],
316 height - slice->distances[SIDE_TOP] - slice->distances[SIDE_BOTTOM],
317 slice->modifiers[SIDE_RIGHT]);
319 /* Top/Left corner */
320 surface = slice->surfaces[BORDER_LEFT][BORDER_TOP];
321 img_width = cairo_image_surface_get_width (surface);
322 img_height = cairo_image_surface_get_height (surface);
323 render_corner (cr, surface, x, y, (gdouble) img_width, (gdouble) img_height);
325 /* Top/right corner */
326 surface = slice->surfaces[BORDER_RIGHT][BORDER_TOP];
327 img_width = cairo_image_surface_get_width (surface);
328 img_height = cairo_image_surface_get_height (surface);
329 render_corner (cr, surface, x + width - img_width, y,
330 (gdouble) img_width, (gdouble) img_height);
332 /* Bottom/left corner */
333 surface = slice->surfaces[BORDER_LEFT][BORDER_BOTTOM];
334 img_width = cairo_image_surface_get_width (surface);
335 img_height = cairo_image_surface_get_height (surface);
336 render_corner (cr, surface, x, y + height - img_height,
337 (gdouble) img_width, (gdouble) img_height);
339 /* Bottom/right corner */
340 surface = slice->surfaces[BORDER_RIGHT][BORDER_BOTTOM];
341 img_width = cairo_image_surface_get_width (surface);
342 img_height = cairo_image_surface_get_height (surface);
343 render_corner (cr, surface, x + width - img_width, y + height - img_height,
344 (gdouble) img_width, (gdouble) img_height);
350 _gtk_9slice_ref (Gtk9Slice *slice)
352 g_return_val_if_fail (slice != NULL, NULL);
359 _gtk_9slice_unref (Gtk9Slice *slice)
361 g_return_if_fail (slice != NULL);
365 if (slice->ref_count == 0)
369 for (i = 0; i < BORDER_LAST; i++)
370 for (j = 0; j < BORDER_LAST; j++)
371 cairo_surface_destroy (slice->surfaces[i][j]);
373 g_slice_free (Gtk9Slice, slice);