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