1 /* ide-cairo.c
2  *
3  * Copyright (C) 2014 Christian Hergert <christian@hergert.me>
4  *
5  * This file is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This file is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "ide-cairo.h"
20 
21 cairo_region_t *
ide_cairo_region_create_from_clip_extents(cairo_t * cr)22 ide_cairo_region_create_from_clip_extents (cairo_t *cr)
23 {
24   cairo_rectangle_int_t crect;
25   GdkRectangle rect;
26 
27   g_return_val_if_fail (cr, NULL);
28 
29   gdk_cairo_get_clip_rectangle (cr, &rect);
30   crect.x = rect.x;
31   crect.y = rect.y;
32   crect.width = rect.width;
33   crect.height = rect.height;
34 
35   return cairo_region_create_rectangle (&crect);
36 }
37 
38 void
ide_cairo_rounded_rectangle(cairo_t * cr,const GdkRectangle * rect,gint x_radius,gint y_radius)39 ide_cairo_rounded_rectangle (cairo_t            *cr,
40                             const GdkRectangle *rect,
41                             gint                x_radius,
42                             gint                y_radius)
43 {
44   gint x;
45   gint y;
46   gint width;
47   gint height;
48   gint x1, x2;
49   gint y1, y2;
50   gint xr1, xr2;
51   gint yr1, yr2;
52 
53   g_return_if_fail (cr);
54   g_return_if_fail (rect);
55 
56   x = rect->x;
57   y = rect->y;
58   width = rect->width;
59   height = rect->height;
60 
61   x1 = x;
62   x2 = x1 + width;
63   y1 = y;
64   y2 = y1 + height;
65 
66   x_radius = MIN (x_radius, width / 2.0);
67   y_radius = MIN (y_radius, width / 2.0);
68 
69   xr1 = x_radius;
70   xr2 = x_radius / 2.0;
71   yr1 = y_radius;
72   yr2 = y_radius / 2.0;
73 
74   cairo_move_to (cr, x1 + xr1, y1);
75   cairo_line_to (cr, x2 - xr1, y1);
76   cairo_curve_to (cr, x2 - xr2, y1, x2, y1 + yr2, x2, y1 + yr1);
77   cairo_line_to (cr, x2, y2 - yr1);
78   cairo_curve_to (cr, x2, y2 - yr2, x2 - xr2, y2, x2 - xr1, y2);
79   cairo_line_to (cr, x1 + xr1, y2);
80   cairo_curve_to (cr, x1 + xr2, y2, x1, y2 - yr2, x1, y2 - yr1);
81   cairo_line_to (cr, x1, y1 + yr1);
82   cairo_curve_to (cr, x1, y1 + yr2, x1 + xr2, y1, x1 + xr1, y1);
83   cairo_close_path (cr);
84 }
85