]> git.0d.be Git - empathy.git/blob - src/empathy-rounded-rectangle.c
add missing config.h
[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 "config.h"
22
23 #include <math.h>
24
25 #include <clutter/clutter.h>
26
27 #include "empathy-rounded-rectangle.h"
28
29 G_DEFINE_TYPE (EmpathyRoundedRectangle,
30     empathy_rounded_rectangle,
31     CLUTTER_TYPE_CAIRO_TEXTURE)
32
33 struct _EmpathyRoundedRectanglePriv
34 {
35   ClutterColor border_color;
36   guint border_width;
37 };
38
39 static void
40 empathy_rounded_rectangle_paint (EmpathyRoundedRectangle *self)
41 {
42   guint width, height;
43   guint tmp_alpha;
44   cairo_t *cr;
45
46 #define RADIUS (height / 8.)
47
48   clutter_cairo_texture_get_surface_size (CLUTTER_CAIRO_TEXTURE (self),
49       &width, &height);
50
51   /* compute the composited opacity of the actor taking into
52    * account the opacity of the color set by the user */
53   tmp_alpha = clutter_actor_get_paint_opacity (CLUTTER_ACTOR (self))
54             * self->priv->border_color.alpha
55             / 255;
56
57   cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (self));
58
59   cairo_set_source_rgba (cr,
60       self->priv->border_color.red,
61       self->priv->border_color.green,
62       self->priv->border_color.blue,
63       tmp_alpha);
64
65   cairo_set_line_width (cr, self->priv->border_width);
66
67   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
68   cairo_paint (cr);
69   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
70
71   cairo_new_sub_path (cr);
72   cairo_arc (cr, width - RADIUS, RADIUS, RADIUS,
73       -M_PI/2.0, 0);
74   cairo_arc (cr, width - RADIUS, height - RADIUS, RADIUS,
75       0, M_PI/2.0);
76   cairo_arc (cr, RADIUS, height - RADIUS, RADIUS,
77       M_PI/2.0, M_PI);
78   cairo_arc (cr, RADIUS, RADIUS, RADIUS,
79       M_PI, -M_PI/2.0);
80   cairo_close_path (cr);
81
82   cairo_stroke (cr);
83   cairo_destroy (cr);
84
85 #undef RADIUS
86 }
87
88 static void
89 empathy_rounded_rectangle_init (EmpathyRoundedRectangle *self)
90 {
91   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
92       EMPATHY_TYPE_ROUNDED_RECTANGLE, EmpathyRoundedRectanglePriv);
93
94   self->priv->border_width = 1;
95 }
96
97 static void
98 empathy_rounded_rectangle_class_init (EmpathyRoundedRectangleClass *klass)
99 {
100   g_type_class_add_private (klass, sizeof (EmpathyRoundedRectanglePriv));
101 }
102
103 ClutterActor *
104 empathy_rounded_rectangle_new (guint width,
105     guint height)
106 {
107   ClutterActor *self;
108
109   self = CLUTTER_ACTOR (g_object_new (EMPATHY_TYPE_ROUNDED_RECTANGLE, NULL));
110
111   clutter_cairo_texture_set_surface_size (CLUTTER_CAIRO_TEXTURE (self),
112       width, height);
113
114   empathy_rounded_rectangle_paint (EMPATHY_ROUNDED_RECTANGLE (self));
115
116   return self;
117 }
118
119 void
120 empathy_rounded_rectangle_set_border_width (EmpathyRoundedRectangle *self,
121     guint border_width)
122 {
123   self->priv->border_width = border_width;
124
125   empathy_rounded_rectangle_paint (self);
126 }
127
128 void
129 empathy_rounded_rectangle_set_border_color (EmpathyRoundedRectangle *self,
130     const ClutterColor *color)
131 {
132   self->priv->border_color = *color;
133
134   empathy_rounded_rectangle_paint (self);
135 }