3 * Copyright (C) 2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
23 #include "color-swatch.h"
29 /*===========================================*/
30 /* Private macros and constants. */
31 /*===========================================*/
34 /*===========================================*/
36 /*===========================================*/
38 struct _glColorSwatchPrivate {
45 /*===========================================*/
47 /*===========================================*/
50 /*===========================================*/
51 /* Local function prototypes */
52 /*===========================================*/
54 static void gl_color_swatch_finalize (GObject *object);
56 static void redraw (glColorSwatch *this);
58 static gboolean expose_event_cb (GtkWidget *widget,
59 GdkEventExpose *event);
61 static void draw_swatch (glColorSwatch *this,
66 /****************************************************************************/
67 /* Boilerplate Object stuff. */
68 /****************************************************************************/
69 G_DEFINE_TYPE (glColorSwatch, gl_color_swatch, GTK_TYPE_DRAWING_AREA);
72 /*****************************************************************************/
73 /* Class Init Function. */
74 /*****************************************************************************/
76 gl_color_swatch_class_init (glColorSwatchClass *class)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
79 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
81 gl_color_swatch_parent_class = g_type_class_peek_parent (class);
83 gobject_class->finalize = gl_color_swatch_finalize;
85 widget_class->expose_event = expose_event_cb;
89 /*****************************************************************************/
90 /* Object Instance Init Function. */
91 /*****************************************************************************/
93 gl_color_swatch_init (glColorSwatch *this)
95 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (this), GTK_NO_WINDOW);
97 this->priv = g_new0 (glColorSwatchPrivate, 1);
101 /*****************************************************************************/
102 /* Finalize Method. */
103 /*****************************************************************************/
105 gl_color_swatch_finalize (GObject *object)
107 glColorSwatch *this = GL_COLOR_SWATCH (object);
109 g_return_if_fail (object != NULL);
110 g_return_if_fail (GL_IS_COLOR_SWATCH (object));
114 G_OBJECT_CLASS (gl_color_swatch_parent_class)->finalize (object);
118 /*****************************************************************************/
119 /** New Object Generator. */
120 /*****************************************************************************/
122 gl_color_swatch_new (gint w,
128 this = g_object_new (GL_TYPE_COLOR_SWATCH, NULL);
130 this->priv->color = color;
132 gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
134 return GTK_WIDGET (this);
138 /*****************************************************************************/
140 /*****************************************************************************/
142 gl_color_swatch_set_color (glColorSwatch *this,
145 this->priv->color = color;
151 /*****************************************************************************/
152 /* Request redraw. */
153 /*****************************************************************************/
155 redraw (glColorSwatch *this)
159 if (GTK_WIDGET_REALIZED (GTK_WIDGET (this)))
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);
169 /*****************************************************************************/
170 /* "Expose event" callback. */
171 /*****************************************************************************/
173 expose_event_cb (GtkWidget *widget,
174 GdkEventExpose *event)
178 cr = gdk_cairo_create (widget->window);
181 event->area.x, event->area.y,
182 event->area.width, event->area.height);
185 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
187 draw_swatch (GL_COLOR_SWATCH (widget), cr);
195 /*****************************************************************************/
197 /*****************************************************************************/
199 draw_swatch (glColorSwatch *this,
205 guint fill_color, line_color;
208 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
211 w = GTK_WIDGET (this)->allocation.width;
212 h = GTK_WIDGET (this)->allocation.height;
215 style = gtk_widget_get_style (GTK_WIDGET (this));
216 if ( GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (this)) )
218 fill_color = this->priv->color;
219 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
223 fill_color = GL_COLOR_NONE;
224 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
227 cairo_rectangle( cr, 0, 0, w-1, h-1 );
229 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
230 cairo_fill_preserve( cr );
232 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
233 cairo_set_line_width (cr, 1.0);
240 * Local Variables: -- emacs
242 * c-basic-offset: 8 -- emacs
243 * tab-width: 8 -- emacs
244 * indent-tabs-mode: nil -- emacs