]> git.sur5r.net Git - glabels/blob - glabels2/src/color-swatch.c
ae72e3dcb990dbf51374da0a733c9094be9dad9a
[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       redraw                      (glColorSwatch  *this);
57
58 static gboolean   expose_event_cb             (GtkWidget      *widget,
59                                                GdkEventExpose *event);
60
61 static void       draw_swatch                 (glColorSwatch  *this,
62                                                cairo_t        *cr);
63
64
65
66 /****************************************************************************/
67 /* Boilerplate Object stuff.                                                */
68 /****************************************************************************/
69 G_DEFINE_TYPE (glColorSwatch, gl_color_swatch, GTK_TYPE_DRAWING_AREA);
70
71
72 /*****************************************************************************/
73 /* Class Init Function.                                                      */
74 /*****************************************************************************/
75 static void
76 gl_color_swatch_class_init (glColorSwatchClass *class)
77 {
78         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
79         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
80
81         gl_color_swatch_parent_class = g_type_class_peek_parent (class);
82
83         gobject_class->finalize    = gl_color_swatch_finalize;
84
85         widget_class->expose_event = expose_event_cb;
86 }
87
88
89 /*****************************************************************************/
90 /* Object Instance Init Function.                                            */
91 /*****************************************************************************/
92 static void
93 gl_color_swatch_init (glColorSwatch *this)
94 {
95         GTK_WIDGET_SET_FLAGS (GTK_WIDGET (this), GTK_NO_WINDOW);
96
97         this->priv = g_new0 (glColorSwatchPrivate, 1);
98 }
99
100
101 /*****************************************************************************/
102 /* Finalize Method.                                                          */
103 /*****************************************************************************/
104 static void
105 gl_color_swatch_finalize (GObject *object)
106 {
107         glColorSwatch *this = GL_COLOR_SWATCH (object);
108
109         g_return_if_fail (object != NULL);
110         g_return_if_fail (GL_IS_COLOR_SWATCH (object));
111
112         g_free (this->priv);
113
114         G_OBJECT_CLASS (gl_color_swatch_parent_class)->finalize (object);
115 }
116
117
118 /*****************************************************************************/
119 /** New Object Generator.                                                    */
120 /*****************************************************************************/
121 GtkWidget *
122 gl_color_swatch_new (gint         w,
123                      gint         h,
124                      guint        color)
125 {
126         glColorSwatch *this;
127
128         this = g_object_new (GL_TYPE_COLOR_SWATCH, NULL);
129
130         this->priv->color = color;
131
132         gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
133
134         return GTK_WIDGET (this);
135 }
136
137
138 /*****************************************************************************/
139 /* Set color.                                                                */
140 /*****************************************************************************/
141 void
142 gl_color_swatch_set_color (glColorSwatch *this,
143                            guint          color)
144 {
145         this->priv->color = color;
146
147         redraw (this);
148 }
149
150
151 /*****************************************************************************/
152 /* Request redraw.                                                           */
153 /*****************************************************************************/
154 static void
155 redraw (glColorSwatch  *this)
156 {
157         GdkRegion *region;
158
159         if (GTK_WIDGET_REALIZED (GTK_WIDGET (this)))
160         {
161                 /* redraw the cairo canvas forcing an expose event */
162                 region = gdk_drawable_get_clip_region (GTK_WIDGET (this)->window);
163                 gdk_window_invalidate_region (GTK_WIDGET (this)->window, region, TRUE);
164                 gdk_region_destroy (region);
165         }
166 }
167
168
169 /*****************************************************************************/
170 /* "Expose event" callback.                                                  */
171 /*****************************************************************************/
172 static gboolean
173 expose_event_cb (GtkWidget      *widget,
174                  GdkEventExpose *event)
175 {
176         cairo_t *cr;
177
178         cr = gdk_cairo_create (widget->window);
179
180         cairo_rectangle (cr,
181                         event->area.x, event->area.y,
182                         event->area.width, event->area.height);
183         cairo_clip (cr);
184
185         cairo_translate (cr, widget->allocation.x, widget->allocation.y);
186
187         draw_swatch (GL_COLOR_SWATCH (widget), cr);
188
189         cairo_destroy (cr);
190
191         return FALSE;
192 }
193
194
195 /*****************************************************************************/
196 /* Draw swatch.                                                              */
197 /*****************************************************************************/
198 static void
199 draw_swatch (glColorSwatch *this,
200              cairo_t       *cr)
201 {
202         GdkPixbuf *pixbuf;
203         GtkStyle  *style;
204         gdouble    w, h;
205         guint      fill_color, line_color;
206
207
208         cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
209
210
211         w = GTK_WIDGET (this)->allocation.width;
212         h = GTK_WIDGET (this)->allocation.height;
213
214
215         style = gtk_widget_get_style (GTK_WIDGET (this));
216         if ( GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (this)) )
217         {
218                 fill_color = this->priv->color;
219                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
220         }
221         else
222         {
223                 fill_color = GL_COLOR_NONE;
224                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
225         }
226
227         cairo_rectangle( cr, 0, 0, w-1, h-1 );
228
229         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
230         cairo_fill_preserve( cr );
231
232         cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
233         cairo_set_line_width (cr, 1.0);
234         cairo_stroke (cr);
235 }
236
237
238
239 /*
240  * Local Variables:       -- emacs
241  * mode: C                -- emacs
242  * c-basic-offset: 8      -- emacs
243  * tab-width: 8           -- emacs
244  * indent-tabs-mode: nil  -- emacs
245  * End:                   -- emacs
246  */