]> git.0d.be Git - empathy.git/blob - src/empathy-rounded-rectangle.c
Merge remote-tracking branch 'em/trivia'
[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   guint width, height;
36   ClutterColor border_color;
37   guint border_width;
38   guint round_factor;
39 };
40
41 static gboolean
42 draw_cb (ClutterCairoTexture *canvas,
43     cairo_t *cr)
44 {
45   EmpathyRoundedRectangle *self = EMPATHY_ROUNDED_RECTANGLE (canvas);
46   guint width, height;
47   guint border_width;
48   guint tmp_alpha;
49   gdouble radius;
50
51   width = self->priv->width;
52   height = self->priv->height;
53   radius = self->priv->height / self->priv->round_factor;
54   border_width = self->priv->border_width;
55
56   /* compute the composited opacity of the actor taking into
57    * account the opacity of the color set by the user */
58   tmp_alpha = clutter_actor_get_paint_opacity (CLUTTER_ACTOR (self))
59             * self->priv->border_color.alpha
60             / 255;
61
62   cairo_set_source_rgba (cr,
63       self->priv->border_color.red,
64       self->priv->border_color.green,
65       self->priv->border_color.blue,
66       tmp_alpha);
67
68   cairo_set_line_width (cr, border_width);
69
70   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
71   cairo_paint (cr);
72   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
73
74   /* make room for the portion of the border drawn on the outside */
75   cairo_translate (cr, border_width/2.0, border_width/2.0);
76
77   cairo_new_sub_path (cr);
78   cairo_arc (cr, width - radius, radius, radius,
79       -M_PI/2.0, 0);
80   cairo_arc (cr, width - radius, height - radius, radius,
81       0, M_PI/2.0);
82   cairo_arc (cr, radius, height - radius, radius,
83       M_PI/2.0, M_PI);
84   cairo_arc (cr, radius, radius, radius,
85       M_PI, -M_PI/2.0);
86   cairo_close_path (cr);
87
88   cairo_stroke (cr);
89
90   return TRUE;
91 }
92
93 static void
94 empathy_rounded_rectangle_init (EmpathyRoundedRectangle *self)
95 {
96   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
97       EMPATHY_TYPE_ROUNDED_RECTANGLE, EmpathyRoundedRectanglePriv);
98
99   self->priv->border_width = 1;
100   self->priv->round_factor = 2;
101 }
102
103 static void
104 empathy_rounded_rectangle_finalize (GObject *object)
105 {
106   G_OBJECT_CLASS (empathy_rounded_rectangle_parent_class)->finalize (object);
107 }
108
109 static void
110 empathy_rounded_rectangle_class_init (EmpathyRoundedRectangleClass *klass)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113
114   gobject_class->finalize = empathy_rounded_rectangle_finalize;
115
116   g_type_class_add_private (klass, sizeof (EmpathyRoundedRectanglePriv));
117 }
118
119 static void
120 empathy_rounded_rectangle_update_surface_size (EmpathyRoundedRectangle *self)
121 {
122   clutter_cairo_texture_set_surface_size (CLUTTER_CAIRO_TEXTURE (self),
123       self->priv->width + self->priv->border_width,
124       self->priv->height + self->priv->border_width);
125 }
126
127 EmpathyRoundedRectangle *
128 empathy_rounded_rectangle_new (guint width,
129     guint height,
130     guint round_factor)
131 {
132   EmpathyRoundedRectangle *self;
133
134   self = EMPATHY_ROUNDED_RECTANGLE (g_object_new (EMPATHY_TYPE_ROUNDED_RECTANGLE, NULL));
135
136   self->priv->width = width;
137   self->priv->height = height;
138   self->priv->round_factor = round_factor;
139
140   g_signal_connect (self, "draw", G_CALLBACK (draw_cb), NULL);
141
142   empathy_rounded_rectangle_update_surface_size (self);
143   clutter_cairo_texture_invalidate (CLUTTER_CAIRO_TEXTURE (self));
144
145   return self;
146 }
147
148 void
149 empathy_rounded_rectangle_set_border_width (EmpathyRoundedRectangle *self,
150     guint border_width)
151 {
152   self->priv->border_width = border_width;
153
154   empathy_rounded_rectangle_update_surface_size (self);
155   clutter_cairo_texture_invalidate (CLUTTER_CAIRO_TEXTURE (self));
156 }
157
158 void
159 empathy_rounded_rectangle_set_border_color (EmpathyRoundedRectangle *self,
160     const ClutterColor *color)
161 {
162   self->priv->border_color = *color;
163
164   clutter_cairo_texture_invalidate (CLUTTER_CAIRO_TEXTURE (self));
165 }