2 * color-combo-color-menu-item.c
3 * Copyright (C) 2008 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-combo-color-menu-item.h"
26 #include <glib/gi18n.h>
27 #include <gtk/gtkmenuitem.h>
28 #include <gtk/gtkseparatormenuitem.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkimage.h>
34 /*===========================================*/
35 /* Private macros and constants. */
36 /*===========================================*/
39 /*===========================================*/
41 /*===========================================*/
43 struct _glColorComboColorMenuItemPrivate {
49 /*===========================================*/
51 /*===========================================*/
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static void gl_color_combo_color_menu_item_finalize (GObject *object);
61 create_color_pixbuf (gdouble w,
65 /****************************************************************************/
66 /* Boilerplate Object stuff. */
67 /****************************************************************************/
68 G_DEFINE_TYPE (glColorComboColorMenuItem, gl_color_combo_color_menu_item, GTK_TYPE_MENU_ITEM);
71 /*****************************************************************************/
72 /* Class Init Function. */
73 /*****************************************************************************/
75 gl_color_combo_color_menu_item_class_init (glColorComboColorMenuItemClass *class)
77 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
78 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
80 gl_color_combo_color_menu_item_parent_class = g_type_class_peek_parent (class);
82 gobject_class->finalize = gl_color_combo_color_menu_item_finalize;
86 /*****************************************************************************/
87 /* Object Instance Init Function. */
88 /*****************************************************************************/
90 gl_color_combo_color_menu_item_init (glColorComboColorMenuItem *this)
92 this->priv = g_new0 (glColorComboColorMenuItemPrivate, 1);
96 /*****************************************************************************/
97 /* Finalize Method. */
98 /*****************************************************************************/
100 gl_color_combo_color_menu_item_finalize (GObject *object)
102 glColorComboColorMenuItem *this = GL_COLOR_COMBO_COLOR_MENU_ITEM (object);
104 g_return_if_fail (object != NULL);
105 g_return_if_fail (GL_IS_COLOR_COMBO_COLOR_MENU_ITEM (object));
109 G_OBJECT_CLASS (gl_color_combo_color_menu_item_parent_class)->finalize (object);
113 /*****************************************************************************/
114 /** New Object Generator. */
115 /*****************************************************************************/
117 gl_color_combo_color_menu_item_new (gint id,
121 glColorComboColorMenuItem *this;
124 this = g_object_new (GL_TYPE_COLOR_COMBO_COLOR_MENU_ITEM, NULL);
128 pixbuf = create_color_pixbuf (16, 16, color);
129 gtk_container_add (GTK_CONTAINER (this),
130 gtk_image_new_from_pixbuf (pixbuf));
132 gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip);
134 return GTK_WIDGET (this);
138 /*****************************************************************************/
140 /*****************************************************************************/
142 gl_color_combo_color_menu_item_set_color(glColorComboColorMenuItem *this,
151 pixbuf = create_color_pixbuf (16, 16, color);
152 gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (this))),
155 gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip);
159 /*****************************************************************************/
160 /* Create new pixbuf with color preview. */
161 /*****************************************************************************/
163 create_color_pixbuf (gdouble w,
167 cairo_surface_t *surface;
171 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
173 cr = cairo_create (surface);
175 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
178 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
182 cairo_rectangle( cr, 2, 2, w-3, h-3 );
184 cairo_set_source_rgba (cr,
185 GL_COLOR_F_RED (color),
186 GL_COLOR_F_GREEN (color),
187 GL_COLOR_F_BLUE (color),
188 GL_COLOR_F_ALPHA (color));
189 cairo_fill_preserve( cr );
191 cairo_set_line_width (cr, 1.0);
192 cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
197 pixbuf = gl_util_cairo_convert_to_pixbuf (surface);
198 cairo_surface_destroy (surface);
204 /*****************************************************************************/
206 /*****************************************************************************/
208 gl_color_combo_color_menu_item_get_id (glColorComboColorMenuItem *this)
210 return this->priv->id;
216 * Local Variables: -- emacs
218 * c-basic-offset: 8 -- emacs
219 * tab-width: 8 -- emacs
220 * indent-tabs-mode: nil -- emacs