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