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 style_set_cb (GtkWidget *widget,
57 GtkStyle *previous_style);
59 static void redraw (glColorSwatch *this);
61 static gboolean expose_event_cb (GtkWidget *widget,
62 GdkEventExpose *event);
64 static void draw_swatch (glColorSwatch *this,
69 /****************************************************************************/
70 /* Boilerplate Object stuff. */
71 /****************************************************************************/
72 G_DEFINE_TYPE (glColorSwatch, gl_color_swatch, GTK_TYPE_DRAWING_AREA);
75 /*****************************************************************************/
76 /* Class Init Function. */
77 /*****************************************************************************/
79 gl_color_swatch_class_init (glColorSwatchClass *class)
81 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
82 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
84 gl_color_swatch_parent_class = g_type_class_peek_parent (class);
86 gobject_class->finalize = gl_color_swatch_finalize;
88 widget_class->expose_event = expose_event_cb;
89 widget_class->style_set = style_set_cb;
93 /*****************************************************************************/
94 /* Object Instance Init Function. */
95 /*****************************************************************************/
97 gl_color_swatch_init (glColorSwatch *this)
99 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (this), GTK_NO_WINDOW);
101 this->priv = g_new0 (glColorSwatchPrivate, 1);
105 /*****************************************************************************/
106 /* Finalize Method. */
107 /*****************************************************************************/
109 gl_color_swatch_finalize (GObject *object)
111 glColorSwatch *this = GL_COLOR_SWATCH (object);
113 g_return_if_fail (object != NULL);
114 g_return_if_fail (GL_IS_COLOR_SWATCH (object));
118 G_OBJECT_CLASS (gl_color_swatch_parent_class)->finalize (object);
122 /*****************************************************************************/
123 /** New Object Generator. */
124 /*****************************************************************************/
126 gl_color_swatch_new (gint w,
132 this = g_object_new (GL_TYPE_COLOR_SWATCH, NULL);
134 this->priv->color = color;
136 gtk_widget_set_size_request (GTK_WIDGET (this), w, h);
138 return GTK_WIDGET (this);
142 /*****************************************************************************/
144 /*****************************************************************************/
146 gl_color_swatch_set_color (glColorSwatch *this,
149 this->priv->color = color;
155 /*--------------------------------------------------------------------------*/
156 /* Style set handler (updates colors when style/theme changes). */
157 /*--------------------------------------------------------------------------*/
159 style_set_cb (GtkWidget *widget,
160 GtkStyle *previous_style)
162 redraw (GL_COLOR_SWATCH (widget));
166 /*****************************************************************************/
167 /* Request redraw. */
168 /*****************************************************************************/
170 redraw (glColorSwatch *this)
174 if (GTK_WIDGET_REALIZED (GTK_WIDGET (this)))
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);
184 /*****************************************************************************/
185 /* "Expose event" callback. */
186 /*****************************************************************************/
188 expose_event_cb (GtkWidget *widget,
189 GdkEventExpose *event)
193 cr = gdk_cairo_create (widget->window);
196 event->area.x, event->area.y,
197 event->area.width, event->area.height);
200 cairo_translate (cr, widget->allocation.x, widget->allocation.y);
202 draw_swatch (GL_COLOR_SWATCH (widget), cr);
210 /*****************************************************************************/
212 /*****************************************************************************/
214 draw_swatch (glColorSwatch *this,
219 guint fill_color, line_color;
222 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
225 w = GTK_WIDGET (this)->allocation.width;
226 h = GTK_WIDGET (this)->allocation.height;
229 style = gtk_widget_get_style (GTK_WIDGET (this));
230 if ( GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (this)) )
232 fill_color = this->priv->color;
233 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
237 fill_color = GL_COLOR_NONE;
238 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
241 cairo_rectangle( cr, 0, 0, w-1, h-1 );
243 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
244 cairo_fill_preserve( cr );
246 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
247 cairo_set_line_width (cr, 1.0);
254 * Local Variables: -- emacs
256 * c-basic-offset: 8 -- emacs
257 * tab-width: 8 -- emacs
258 * indent-tabs-mode: nil -- emacs