]> git.sur5r.net Git - glabels/blob - glabels2/src/color-swatch.c
2009-09-08 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / color-swatch.c
1 /*
2  *  color-swatch.c
3  *  Copyright (C) 2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "color-swatch.h"
24
25 #include "marshal.h"
26 #include "color.h"
27
28
29 /*===========================================*/
30 /* Private macros and constants.             */
31 /*===========================================*/
32
33
34 /*===========================================*/
35 /* Private types                             */
36 /*===========================================*/
37
38 struct _glColorSwatchPrivate {
39
40         guint      color;
41
42 };
43
44
45 /*===========================================*/
46 /* Private globals                           */
47 /*===========================================*/
48
49
50 /*===========================================*/
51 /* Local function prototypes                 */
52 /*===========================================*/
53
54 static void       gl_color_swatch_finalize    (GObject        *object);
55
56 static void       style_set_cb                (GtkWidget      *widget,
57                                                GtkStyle       *previous_style);
58
59 static void       redraw                      (glColorSwatch  *this);
60
61 static gboolean   expose_event_cb             (GtkWidget      *widget,
62                                                GdkEventExpose *event);
63
64 static void       draw_swatch                 (glColorSwatch  *this,
65                                                cairo_t        *cr);
66
67
68
69 /****************************************************************************/
70 /* Boilerplate Object stuff.                                                */
71 /****************************************************************************/
72 G_DEFINE_TYPE (glColorSwatch, gl_color_swatch, GTK_TYPE_DRAWING_AREA);
73
74
75 /*****************************************************************************/
76 /* Class Init Function.                                                      */
77 /*****************************************************************************/
78 static void
79 gl_color_swatch_class_init (glColorSwatchClass *class)
80 {
81         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
82         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
83
84         gl_color_swatch_parent_class = g_type_class_peek_parent (class);
85
86         gobject_class->finalize    = gl_color_swatch_finalize;
87
88         widget_class->expose_event = expose_event_cb;
89         widget_class->style_set    = style_set_cb;
90 }
91
92
93 /*****************************************************************************/
94 /* Object Instance Init Function.                                            */
95 /*****************************************************************************/
96 static void
97 gl_color_swatch_init (glColorSwatch *this)
98 {
99         GTK_WIDGET_SET_FLAGS (GTK_WIDGET (this), GTK_NO_WINDOW);
100
101         this->priv = g_new0 (glColorSwatchPrivate, 1);
102 }
103
104
105 /*****************************************************************************/
106 /* Finalize Method.                                                          */
107 /*****************************************************************************/
108 static void
109 gl_color_swatch_finalize (GObject *object)
110 {
111         glColorSwatch *this = GL_COLOR_SWATCH (object);
112
113         g_return_if_fail (object != NULL);
114         g_return_if_fail (GL_IS_COLOR_SWATCH (object));
115
116         g_free (this->priv);
117
118         G_OBJECT_CLASS (gl_color_swatch_parent_class)->finalize (object);
119 }
120
121
122 /*****************************************************************************/
123 /** New Object Generator.                                                    */
124 /*****************************************************************************/
125 GtkWidget *
126 gl_color_swatch_new (gint         w,
127                      gint         h,
128                      guint        color)
129 {
130         glColorSwatch *this;
131
132         this = g_object_new (GL_TYPE_COLOR_SWATCH, NULL);
133
134         this->priv->color = color;
135
136         gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
137
138         return GTK_WIDGET (this);
139 }
140
141
142 /*****************************************************************************/
143 /* Set color.                                                                */
144 /*****************************************************************************/
145 void
146 gl_color_swatch_set_color (glColorSwatch *this,
147                            guint          color)
148 {
149         this->priv->color = color;
150
151         redraw (this);
152 }
153
154
155 /*--------------------------------------------------------------------------*/
156 /* Style set handler (updates colors when style/theme changes).             */
157 /*--------------------------------------------------------------------------*/
158 static void
159 style_set_cb (GtkWidget        *widget,
160               GtkStyle         *previous_style)
161 {
162         redraw (GL_COLOR_SWATCH (widget));
163 }
164
165
166 /*****************************************************************************/
167 /* Request redraw.                                                           */
168 /*****************************************************************************/
169 static void
170 redraw (glColorSwatch  *this)
171 {
172         GdkRegion *region;
173
174         if (GTK_WIDGET_REALIZED (GTK_WIDGET (this)))
175         {
176                 /* redraw the cairo canvas forcing an expose event */
177                 region = gdk_drawable_get_clip_region (GTK_WIDGET (this)->window);
178                 gdk_window_invalidate_region (GTK_WIDGET (this)->window, region, TRUE);
179                 gdk_region_destroy (region);
180         }
181 }
182
183
184 /*****************************************************************************/
185 /* "Expose event" callback.                                                  */
186 /*****************************************************************************/
187 static gboolean
188 expose_event_cb (GtkWidget      *widget,
189                  GdkEventExpose *event)
190 {
191         cairo_t *cr;
192
193         cr = gdk_cairo_create (widget->window);
194
195         cairo_rectangle (cr,
196                         event->area.x, event->area.y,
197                         event->area.width, event->area.height);
198         cairo_clip (cr);
199
200         cairo_translate (cr, widget->allocation.x, widget->allocation.y);
201
202         draw_swatch (GL_COLOR_SWATCH (widget), cr);
203
204         cairo_destroy (cr);
205
206         return FALSE;
207 }
208
209
210 /*****************************************************************************/
211 /* Draw swatch.                                                              */
212 /*****************************************************************************/
213 static void
214 draw_swatch (glColorSwatch *this,
215              cairo_t       *cr)
216 {
217         GtkStyle  *style;
218         gdouble    w, h;
219         guint      fill_color, line_color;
220
221
222         cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
223
224
225         w = GTK_WIDGET (this)->allocation.width;
226         h = GTK_WIDGET (this)->allocation.height;
227
228
229         style = gtk_widget_get_style (GTK_WIDGET (this));
230         if ( GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (this)) )
231         {
232                 fill_color = this->priv->color;
233                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
234         }
235         else
236         {
237                 fill_color = GL_COLOR_NONE;
238                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
239         }
240
241         cairo_rectangle( cr, 0, 0, w-1, h-1 );
242
243         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
244         cairo_fill_preserve( cr );
245
246         cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
247         cairo_set_line_width (cr, 1.0);
248         cairo_stroke (cr);
249 }
250
251
252
253 /*
254  * Local Variables:       -- emacs
255  * mode: C                -- emacs
256  * c-basic-offset: 8      -- emacs
257  * tab-width: 8           -- emacs
258  * indent-tabs-mode: nil  -- emacs
259  * End:                   -- emacs
260  */