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 draw_cb (GtkWidget *widget,
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->style_set = style_set_cb;
89 widget_class->draw = draw_cb;
93 /*****************************************************************************/
94 /* Object Instance Init Function. */
95 /*****************************************************************************/
97 gl_color_swatch_init (glColorSwatch *this)
99 gtk_widget_set_has_window (GTK_WIDGET (this), FALSE);
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 if ( color != this->priv->color )
151 this->priv->color = color;
158 /*--------------------------------------------------------------------------*/
159 /* Style set handler (updates colors when style/theme changes). */
160 /*--------------------------------------------------------------------------*/
162 style_set_cb (GtkWidget *widget,
163 GtkStyle *previous_style)
165 redraw (GL_COLOR_SWATCH (widget));
169 /*****************************************************************************/
170 /* Request redraw. */
171 /*****************************************************************************/
173 redraw (glColorSwatch *this)
176 GtkAllocation allocation;
178 window = gtk_widget_get_window (GTK_WIDGET (this));
182 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
183 gdk_window_invalidate_rect (window, &allocation, FALSE);
188 /*****************************************************************************/
189 /* "Expose event" callback. */
190 /*****************************************************************************/
192 draw_cb (GtkWidget *widget,
195 draw_swatch (GL_COLOR_SWATCH (widget), cr);
201 /*****************************************************************************/
203 /*****************************************************************************/
205 draw_swatch (glColorSwatch *this,
208 GtkAllocation allocation;
211 guint fill_color, line_color;
214 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
217 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
218 w = allocation.width;
219 h = allocation.height;
222 style = gtk_widget_get_style (GTK_WIDGET (this));
223 if ( gtk_widget_is_sensitive (GTK_WIDGET (this)) )
225 fill_color = this->priv->color;
226 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_NORMAL]);
230 fill_color = GL_COLOR_NONE;
231 line_color = gl_color_from_gdk_color (&style->fg[GTK_STATE_INSENSITIVE]);
234 cairo_rectangle( cr, 1, 1, w-2, h-2 );
236 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
237 cairo_fill_preserve( cr );
239 cairo_set_source_rgb (cr, GL_COLOR_RGB_ARGS (line_color));
240 cairo_set_line_width (cr, 1.0);
247 * Local Variables: -- emacs
249 * c-basic-offset: 8 -- emacs
250 * tab-width: 8 -- emacs
251 * indent-tabs-mode: nil -- emacs