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 "color.h"
26 #include "marshal.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   draw_cb                     (GtkWidget      *widget,
62                                                cairo_t        *cr);
63 
64 static void       draw_swatch                 (glColorSwatch  *this,
65                                                cairo_t        *cr);
66 
67 
68 
69 /****************************************************************************/
70 /* Boilerplate Object stuff.                                                */
71 /****************************************************************************/
G_DEFINE_TYPE(glColorSwatch,gl_color_swatch,GTK_TYPE_DRAWING_AREA)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->style_set    = style_set_cb;
89         widget_class->draw         = draw_cb;
90 }
91 
92 
93 /*****************************************************************************/
94 /* Object Instance Init Function.                                            */
95 /*****************************************************************************/
96 static void
gl_color_swatch_init(glColorSwatch * this)97 gl_color_swatch_init (glColorSwatch *this)
98 {
99         gtk_widget_set_has_window (GTK_WIDGET (this), FALSE);
100 
101 	this->priv = g_new0 (glColorSwatchPrivate, 1);
102 }
103 
104 
105 /*****************************************************************************/
106 /* Finalize Method.                                                          */
107 /*****************************************************************************/
108 static void
gl_color_swatch_finalize(GObject * object)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 *
gl_color_swatch_new(gint w,gint h,guint color)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
gl_color_swatch_set_color(glColorSwatch * this,guint color)146 gl_color_swatch_set_color (glColorSwatch *this,
147                            guint          color)
148 {
149         if ( color != this->priv->color )
150         {
151                 this->priv->color = color;
152 
153                 redraw (this);
154         }
155 }
156 
157 
158 /*--------------------------------------------------------------------------*/
159 /* Style set handler (updates colors when style/theme changes).             */
160 /*--------------------------------------------------------------------------*/
161 static void
style_set_cb(GtkWidget * widget,GtkStyle * previous_style)162 style_set_cb (GtkWidget        *widget,
163               GtkStyle         *previous_style)
164 {
165         redraw (GL_COLOR_SWATCH (widget));
166 }
167 
168 
169 /*****************************************************************************/
170 /* Request redraw.                                                           */
171 /*****************************************************************************/
172 static void
redraw(glColorSwatch * this)173 redraw (glColorSwatch  *this)
174 {
175         GdkWindow     *window;
176         GtkAllocation  allocation;
177 
178         window = gtk_widget_get_window (GTK_WIDGET (this));
179 
180         if (window)
181         {
182                 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
183                 gdk_window_invalidate_rect (window, &allocation, FALSE);
184         }
185 }
186 
187 
188 /*****************************************************************************/
189 /* "Expose event" callback.                                                  */
190 /*****************************************************************************/
191 static gboolean
draw_cb(GtkWidget * widget,cairo_t * cr)192 draw_cb (GtkWidget      *widget,
193          cairo_t        *cr)
194 {
195 	draw_swatch (GL_COLOR_SWATCH (widget), cr);
196 
197 	return FALSE;
198 }
199 
200 
201 /*****************************************************************************/
202 /* Draw swatch.                                                              */
203 /*****************************************************************************/
204 static void
draw_swatch(glColorSwatch * this,cairo_t * cr)205 draw_swatch (glColorSwatch *this,
206              cairo_t       *cr)
207 {
208         GtkAllocation  allocation;
209         GtkStyle      *style;
210         gdouble        w, h;
211         guint          fill_color, line_color;
212 
213 
214         cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
215 
216 
217         gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
218         w = allocation.width;
219         h = allocation.height;
220 
221 
222         style = gtk_widget_get_style (GTK_WIDGET (this));
223         if ( gtk_widget_is_sensitive (GTK_WIDGET (this)) )
224         {
225                 fill_color = this->priv->color;
226                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
227         }
228         else
229         {
230                 fill_color = GL_COLOR_NONE;
231                 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
232         }
233 
234         cairo_rectangle( cr, 1, 1, w-2, h-2 );
235 
236         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
237         cairo_fill_preserve( cr );
238 
239         cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
240         cairo_set_line_width (cr, 1.0);
241         cairo_stroke (cr);
242 }
243 
244 
245 
246 /*
247  * Local Variables:       -- emacs
248  * mode: C                -- emacs
249  * c-basic-offset: 8      -- emacs
250  * tab-width: 8           -- emacs
251  * indent-tabs-mode: nil  -- emacs
252  * End:                   -- emacs
253  */
254