]> git.0d.be Git - empathy.git/blob - src/empathy-rounded-rectangle.c
e06889d58d016600803e683242d663a7beb07b6c
[empathy.git] / src / empathy-rounded-rectangle.c
1 /*
2  * empathy-rounded-rectangle.c - Source for EmpathyRoundedRectangle
3  * Copyright (C) 2011 Collabora Ltd.
4  * @author Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <math.h>
22
23 #include <clutter/clutter.h>
24
25 #include "empathy-rounded-rectangle.h"
26
27 G_DEFINE_TYPE (EmpathyRoundedRectangle,
28     empathy_rounded_rectangle,
29     CLUTTER_TYPE_CAIRO_TEXTURE)
30
31 struct _EmpathyRoundedRectanglePriv
32 {
33   ClutterColor border_color;
34   guint border_width;
35 };
36
37 static void
38 empathy_rounded_rectangle_paint (EmpathyRoundedRectangle *self)
39 {
40   guint width, height;
41   guint tmp_alpha;
42   cairo_t *cr;
43
44 #define RADIUS (height / 8.)
45
46   clutter_cairo_texture_get_surface_size (CLUTTER_CAIRO_TEXTURE (self),
47       &width, &height);
48
49   /* compute the composited opacity of the actor taking into
50    * account the opacity of the color set by the user */
51   tmp_alpha = clutter_actor_get_paint_opacity (CLUTTER_ACTOR (self))
52             * self->priv->border_color.alpha
53             / 255;
54
55   cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (self));
56
57   cairo_set_source_rgba (cr,
58       self->priv->border_color.red,
59       self->priv->border_color.green,
60       self->priv->border_color.blue,
61       tmp_alpha);
62
63   cairo_set_line_width (cr, self->priv->border_width);
64
65   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
66   cairo_paint (cr);
67   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
68
69   cairo_new_sub_path (cr);
70   cairo_arc (cr, width - RADIUS, RADIUS, RADIUS,
71       -M_PI/2.0, 0);
72   cairo_arc (cr, width - RADIUS, height - RADIUS, RADIUS,
73       0, M_PI/2.0);
74   cairo_arc (cr, RADIUS, height - RADIUS, RADIUS,
75       M_PI/2.0, M_PI);
76   cairo_arc (cr, RADIUS, RADIUS, RADIUS,
77       M_PI, -M_PI/2.0);
78   cairo_close_path (cr);
79
80   cairo_stroke (cr);
81   cairo_destroy (cr);
82
83 #undef RADIUS
84 }
85
86 static void
87 empathy_rounded_rectangle_init (EmpathyRoundedRectangle *self)
88 {
89   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
90       EMPATHY_TYPE_ROUNDED_RECTANGLE, EmpathyRoundedRectanglePriv);
91
92   self->priv->border_width = 1;
93 }
94
95 static void
96 empathy_rounded_rectangle_class_init (EmpathyRoundedRectangleClass *klass)
97 {
98   g_type_class_add_private (klass, sizeof (EmpathyRoundedRectanglePriv));
99 }
100
101 ClutterActor *
102 empathy_rounded_rectangle_new (guint width,
103     guint height)
104 {
105   ClutterActor *self;
106
107   self = CLUTTER_ACTOR (g_object_new (EMPATHY_TYPE_ROUNDED_RECTANGLE, NULL));
108
109   clutter_cairo_texture_set_surface_size (CLUTTER_CAIRO_TEXTURE (self),
110       width, height);
111
112   empathy_rounded_rectangle_paint (EMPATHY_ROUNDED_RECTANGLE (self));
113
114   return self;
115 }
116
117 void
118 empathy_rounded_rectangle_set_border_width (EmpathyRoundedRectangle *self,
119     guint border_width)
120 {
121   self->priv->border_width = border_width;
122
123   empathy_rounded_rectangle_paint (self);
124 }
125
126 void
127 empathy_rounded_rectangle_set_border_color (EmpathyRoundedRectangle *self,
128     const ClutterColor *color)
129 {
130   self->priv->border_color = *color;
131
132   empathy_rounded_rectangle_paint (self);
133 }