1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3 * Copyright (C) 2011 Red Hat, Inc.
5 * Authors: Carlos Garnacho <carlosg@gnome.org>
6 * Cosimo Cecchi <cosimoc@gnome.org>
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.
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.
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.
25 #include <cairo-gobject.h>
29 #include "gtkborderimageprivate.h"
31 G_DEFINE_BOXED_TYPE (GtkBorderImage, _gtk_border_image,
32 _gtk_border_image_ref, _gtk_border_image_unref)
39 BORDER_TOP = BORDER_LEFT,
40 BORDER_BOTTOM = BORDER_RIGHT
50 struct _GtkBorderImage {
51 cairo_pattern_t *source;
52 GtkGradient *source_gradient;
55 GtkCssBorderImageRepeat repeat;
61 _gtk_border_image_new (cairo_pattern_t *pattern,
63 GtkCssBorderImageRepeat *repeat)
65 GtkBorderImage *image;
67 image = g_slice_new0 (GtkBorderImage);
71 image->source = cairo_pattern_reference (pattern);
74 image->slice = *slice;
77 image->repeat = *repeat;
83 _gtk_border_image_new_for_gradient (GtkGradient *gradient,
85 GtkCssBorderImageRepeat *repeat)
87 GtkBorderImage *image;
89 image = g_slice_new0 (GtkBorderImage);
93 image->source_gradient = gtk_gradient_ref (gradient);
96 image->slice = *slice;
99 image->repeat = *repeat;
105 _gtk_border_image_ref (GtkBorderImage *image)
107 g_return_val_if_fail (image != NULL, NULL);
115 _gtk_border_image_unref (GtkBorderImage *image)
117 g_return_if_fail (image != NULL);
121 if (image->ref_count == 0)
123 if (image->source != NULL)
124 cairo_pattern_destroy (image->source);
126 if (image->source_gradient != NULL)
127 gtk_gradient_unref (image->source_gradient);
129 g_slice_free (GtkBorderImage, image);
134 _gtk_border_image_unpack (const GValue *value,
137 GParameter *parameter = g_new0 (GParameter, 3);
138 GtkBorderImage *image = g_value_get_boxed (value);
140 parameter[0].name = "border-image-source";
141 g_value_init (¶meter[0].value, CAIRO_GOBJECT_TYPE_PATTERN);
142 g_value_set_boxed (¶meter[0].value, image->source);
144 parameter[1].name = "border-image-slice";
145 g_value_init (¶meter[1].value, GTK_TYPE_BORDER);
146 g_value_set_boxed (¶meter[1].value, &image->slice);
148 parameter[2].name = "border-image-repeat";
149 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
150 g_value_set_boxed (¶meter[2].value, &image->repeat);
157 _gtk_border_image_pack (GValue *value,
158 GtkStyleProperties *props,
161 GtkBorderImage *image;
162 cairo_pattern_t *source;
164 GtkCssBorderImageRepeat *repeat;
166 gtk_style_properties_get (props, state,
167 "border-image-source", &source,
168 "border-image-slice", &slice,
169 "border-image-repeat", &repeat,
174 g_value_take_boxed (value, NULL);
178 image = _gtk_border_image_new (source, slice, repeat);
179 g_value_take_boxed (value, image);
181 cairo_pattern_destroy (source);
185 gtk_border_free (slice);
192 render_corner (cairo_t *cr,
195 gdouble corner_width,
196 gdouble corner_height,
197 cairo_surface_t *surface,
199 gdouble image_height)
201 if (corner_width == 0 || corner_height == 0)
206 cairo_translate (cr, corner_x, corner_y);
208 corner_width / image_width,
209 corner_height / image_height);
210 cairo_set_source_surface (cr, surface, 0, 0);
212 /* use the nearest filter for scaling, to avoid color blending */
213 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
220 static cairo_surface_t *
221 create_spaced_surface (cairo_surface_t *tile,
226 GtkOrientation orientation)
229 gdouble avail_space, step;
230 cairo_surface_t *retval;
233 n_repeats = (orientation == GTK_ORIENTATION_HORIZONTAL) ?
234 (gint) floor (width / tile_width) :
235 (gint) floor (height / tile_height);
237 avail_space = (orientation == GTK_ORIENTATION_HORIZONTAL) ?
238 width - (n_repeats * tile_width) :
239 height - (n_repeats * tile_height);
240 step = avail_space / (n_repeats + 2);
242 retval = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
244 cr = cairo_create (retval);
247 while (idx < n_repeats)
251 if (orientation == GTK_ORIENTATION_HORIZONTAL)
252 cairo_set_source_surface (cr, tile,
253 ((idx + 1) * step) + (idx * tile_width), 0);
255 cairo_set_source_surface (cr, tile,
256 0, ((idx + 1 ) * step) + (idx * tile_height));
270 render_border (cairo_t *cr,
272 gdouble total_height,
273 cairo_surface_t *surface,
274 gdouble surface_width,
275 gdouble surface_height,
277 GtkBorder *border_area,
278 GtkCssBorderImageRepeat *repeat)
280 gdouble target_x, target_y;
281 gdouble target_width, target_height;
282 GdkRectangle image_area;
283 cairo_pattern_t *pattern;
284 gboolean repeat_pattern;
289 cairo_surface_reference (surface);
290 repeat_pattern = FALSE;
292 if (side == SIDE_TOP || side == SIDE_BOTTOM)
294 target_height = (side == SIDE_TOP) ? (border_area->top) : (border_area->bottom);
295 target_width = surface_width * (target_height / surface_height);
299 target_width = (side == SIDE_LEFT) ? (border_area->left) : (border_area->right);
300 target_height = surface_height * (target_width / surface_width);
303 if (side == SIDE_TOP || side == SIDE_BOTTOM)
305 image_area.x = border_area->left;
306 image_area.y = (side == SIDE_TOP) ? 0 : (total_height - border_area->bottom);
307 image_area.width = total_width - border_area->left - border_area->right;
308 image_area.height = (side == SIDE_TOP) ? border_area->top : border_area->bottom;
310 target_x = border_area->left;
311 target_y = (side == SIDE_TOP) ? 0 : (total_height - border_area->bottom);
313 if (repeat->vrepeat == GTK_CSS_REPEAT_STYLE_NONE)
315 target_width = image_area.width;
317 else if (repeat->vrepeat == GTK_CSS_REPEAT_STYLE_REPEAT)
319 repeat_pattern = TRUE;
321 target_x = border_area->left + (total_width - border_area->left - border_area->right) / 2;
322 target_y = ((side == SIDE_TOP) ? 0 : (total_height - border_area->bottom)) / 2;
324 else if (repeat->vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
328 repeat_pattern = TRUE;
330 n_repeats = (gint) floor (image_area.width / surface_width);
331 target_width = image_area.width / n_repeats;
333 else if (repeat->vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
335 cairo_surface_t *spaced_surface;
337 spaced_surface = create_spaced_surface (surface,
338 surface_width, surface_height,
339 image_area.width, surface_height,
340 GTK_ORIENTATION_HORIZONTAL);
341 cairo_surface_destroy (surface);
342 surface = spaced_surface;
344 /* short-circuit hscaling */
345 target_width = surface_width = cairo_image_surface_get_width (spaced_surface);
350 image_area.x = (side == SIDE_LEFT) ? 0 : (total_width - border_area->right);
351 image_area.y = border_area->top;
352 image_area.width = (side == SIDE_LEFT) ? border_area->left : border_area->right;
353 image_area.height = total_height - border_area->top - border_area->bottom;
355 target_x = (side == SIDE_LEFT) ? 0 : (total_width - border_area->right);
356 target_y = border_area->top;
358 if (repeat->hrepeat == GTK_CSS_REPEAT_STYLE_NONE)
360 target_height = total_height - border_area->top - border_area->bottom;
362 else if (repeat->hrepeat == GTK_CSS_REPEAT_STYLE_REPEAT)
364 repeat_pattern = TRUE;
366 target_height = total_height - border_area->top - border_area->bottom;
367 target_x = (side == SIDE_LEFT) ? 0 : (total_width - border_area->right) / 2;
368 target_y = border_area->top + (total_height - border_area->top - border_area->bottom) / 2;
370 else if (repeat->hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
374 repeat_pattern = TRUE;
376 n_repeats = (gint) floor (image_area.height / surface_height);
377 target_height = image_area.height / n_repeats;
379 else if (repeat->hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
381 cairo_surface_t *spaced_surface;
383 spaced_surface = create_spaced_surface (surface,
384 surface_width, surface_height,
385 surface_width, image_area.height,
386 GTK_ORIENTATION_VERTICAL);
387 cairo_surface_destroy (surface);
388 surface = spaced_surface;
390 /* short-circuit vscaling */
391 target_height = surface_height = cairo_image_surface_get_height (spaced_surface);
395 if (target_width == 0 || target_height == 0)
400 pattern = cairo_pattern_create_for_surface (surface);
402 gdk_cairo_rectangle (cr, &image_area);
408 /* use the nearest filter for scaling, to avoid color blending */
409 cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
412 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
415 target_width / surface_width,
416 target_height / surface_height);
418 cairo_set_source (cr, pattern);
423 cairo_pattern_destroy (pattern);
424 cairo_surface_destroy (surface);
428 _gtk_border_image_render (GtkBorderImage *image,
429 GtkBorder *border_width,
436 cairo_surface_t *surface, *slice;
437 gdouble slice_width, slice_height, surface_width, surface_height;
439 if (cairo_pattern_get_type (image->source) != CAIRO_PATTERN_TYPE_SURFACE)
441 cairo_matrix_t matrix;
444 surface_width = width;
445 surface_height = height;
447 cairo_matrix_init_scale (&matrix, 1 / width, 1 / height);
448 cairo_pattern_set_matrix (image->source, &matrix);
450 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
451 surface_cr = cairo_create (surface);
452 cairo_set_source (surface_cr, image->source);
453 cairo_paint (surface_cr);
455 cairo_destroy (surface_cr);
459 cairo_pattern_get_surface (image->source, &surface);
460 cairo_surface_reference (surface);
462 surface_width = cairo_image_surface_get_width (surface);
463 surface_height = cairo_image_surface_get_height (surface);
467 cairo_translate (cr, x, y);
469 if ((image->slice.left + image->slice.right) < surface_width)
472 slice_width = surface_width - image->slice.left - image->slice.right;
473 slice_height = image->slice.top;
474 slice = cairo_surface_create_for_rectangle
476 image->slice.left, 0,
477 slice_width, slice_height);
482 slice_width, slice_height,
487 cairo_surface_destroy (slice);
490 slice_height = image->slice.bottom;
491 slice = cairo_surface_create_for_rectangle
493 image->slice.left, surface_height - image->slice.bottom,
494 slice_width, slice_height);
499 slice_width, slice_height,
504 cairo_surface_destroy (slice);
507 if ((image->slice.top + image->slice.bottom) < surface_height)
510 slice_width = image->slice.left;
511 slice_height = surface_height - image->slice.top - image->slice.bottom;
512 slice = cairo_surface_create_for_rectangle
515 slice_width, slice_height);
520 slice_width, slice_height,
525 cairo_surface_destroy (slice);
528 slice_width = image->slice.right;
529 slice = cairo_surface_create_for_rectangle
531 surface_width - image->slice.right, image->slice.top,
532 slice_width, slice_height);
537 slice_width, slice_height,
542 cairo_surface_destroy (slice);
545 /* Top/left corner */
546 slice_width = image->slice.left;
547 slice_height = image->slice.top;
548 slice = cairo_surface_create_for_rectangle
551 slice_width, slice_height);
555 border_width->left, border_width->top,
557 slice_width, slice_height);
559 cairo_surface_destroy (slice);
561 /* Top/right corner */
562 slice_width = image->slice.right;
563 slice = cairo_surface_create_for_rectangle
565 surface_width - image->slice.right, 0,
566 slice_width, slice_height);
569 width - border_width->right, 0,
570 border_width->right, border_width->top,
572 slice_width, slice_height);
574 cairo_surface_destroy (slice);
576 /* Bottom/left corner */
577 slice_width = image->slice.left;
578 slice_height = image->slice.bottom;
579 slice = cairo_surface_create_for_rectangle
581 0, surface_height - image->slice.bottom,
582 slice_width, slice_height);
585 0, height - border_width->bottom,
586 border_width->left, border_width->bottom,
588 slice_width, slice_height);
590 cairo_surface_destroy (slice);
592 /* Bottom/right corner */
593 slice_width = image->slice.right;
594 slice = cairo_surface_create_for_rectangle
596 surface_width - image->slice.right,
597 surface_height - image->slice.bottom,
598 slice_width, slice_height);
601 width - border_width->right, height - border_width->bottom,
602 border_width->right, border_width->bottom,
604 slice_width, slice_height);
606 cairo_surface_destroy (slice);