]> git.sur5r.net Git - glabels/blob - glabels2/src/color-combo-color-menu-item.c
2008-10-12 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / color-combo-color-menu-item.c
1 /*
2  *  color-combo-color-menu-item.c
3  *  Copyright (C) 2008  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-combo-color-menu-item.h"
24
25 #include "marshal.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>
31 #include "color.h"
32
33
34 /*===========================================*/
35 /* Private macros and constants.             */
36 /*===========================================*/
37
38
39 /*===========================================*/
40 /* Private types                             */
41 /*===========================================*/
42
43 struct _glColorComboColorMenuItemPrivate {
44
45         gint id;
46 };
47
48
49 /*===========================================*/
50 /* Private globals                           */
51 /*===========================================*/
52
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
58 static void gl_color_combo_color_menu_item_finalize    (GObject                *object);
59
60 static GdkPixbuf *
61 create_color_pixbuf (gdouble         w,
62                      gdouble         h,
63                      guint           color);
64
65 /****************************************************************************/
66 /* Boilerplate Object stuff.                                                */
67 /****************************************************************************/
68 G_DEFINE_TYPE (glColorComboColorMenuItem, gl_color_combo_color_menu_item, GTK_TYPE_MENU_ITEM);
69
70
71 /*****************************************************************************/
72 /* Class Init Function.                                                      */
73 /*****************************************************************************/
74 static void
75 gl_color_combo_color_menu_item_class_init (glColorComboColorMenuItemClass *class)
76 {
77         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
78         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
79
80         gl_color_combo_color_menu_item_parent_class = g_type_class_peek_parent (class);
81
82         gobject_class->finalize = gl_color_combo_color_menu_item_finalize;
83 }
84
85
86 /*****************************************************************************/
87 /* Object Instance Init Function.                                            */
88 /*****************************************************************************/
89 static void
90 gl_color_combo_color_menu_item_init (glColorComboColorMenuItem *this)
91 {
92         this->priv = g_new0 (glColorComboColorMenuItemPrivate, 1);
93 }
94
95
96 /*****************************************************************************/
97 /* Finalize Method.                                                          */
98 /*****************************************************************************/
99 static void
100 gl_color_combo_color_menu_item_finalize (GObject *object)
101 {
102         glColorComboColorMenuItem *this = GL_COLOR_COMBO_COLOR_MENU_ITEM (object);
103
104         g_return_if_fail (object != NULL);
105         g_return_if_fail (GL_IS_COLOR_COMBO_COLOR_MENU_ITEM (object));
106
107         g_free (this->priv);
108
109         G_OBJECT_CLASS (gl_color_combo_color_menu_item_parent_class)->finalize (object);
110 }
111
112
113 /*****************************************************************************/
114 /** New Object Generator.                                                    */
115 /*****************************************************************************/
116 GtkWidget *
117 gl_color_combo_color_menu_item_new (gint         id,
118                                     guint        color,
119                                     const gchar *tip)
120 {
121         glColorComboColorMenuItem *this;
122         GdkPixbuf                 *pixbuf;
123
124         this = g_object_new (GL_TYPE_COLOR_COMBO_COLOR_MENU_ITEM, NULL);
125
126         this->priv->id = id;
127
128         pixbuf = create_color_pixbuf (16, 16, color);
129         gtk_container_add (GTK_CONTAINER (this),
130                            gtk_image_new_from_pixbuf (pixbuf));
131
132         gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip);
133
134         return GTK_WIDGET (this);
135 }
136
137
138 /*****************************************************************************/
139 /* Set color.                                                                */
140 /*****************************************************************************/
141 void
142 gl_color_combo_color_menu_item_set_color(glColorComboColorMenuItem *this,
143                                          gint                       id,
144                                          guint                      color,
145                                          const gchar               *tip)
146 {
147         GdkPixbuf                 *pixbuf;
148
149         this->priv->id = id;
150
151         pixbuf = create_color_pixbuf (16, 16, color);
152         gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (this))),
153                                    pixbuf);
154
155         gtk_widget_set_tooltip_text (GTK_WIDGET (this), tip);
156  }
157
158
159 /*****************************************************************************/
160 /* Create new pixbuf with color preview.                                     */
161 /*****************************************************************************/
162 static GdkPixbuf *
163 create_color_pixbuf (gdouble         w,
164                      gdouble         h,
165                      guint           color)
166 {
167         cairo_surface_t   *surface;
168         cairo_t           *cr;
169         GdkPixbuf         *pixbuf;
170
171         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
172
173         cr = cairo_create (surface);
174
175         cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
176
177         cairo_save (cr);
178         cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
179         cairo_paint (cr);
180         cairo_restore (cr);
181
182         cairo_rectangle( cr, 2, 2, w-3, h-3 );
183
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 );
190
191         cairo_set_line_width (cr, 1.0);
192         cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
193         cairo_stroke (cr);
194
195         cairo_destroy( cr );
196
197         pixbuf = gl_util_cairo_convert_to_pixbuf (surface);
198         cairo_surface_destroy (surface);
199
200         return pixbuf;
201 }
202
203
204 /*****************************************************************************/
205 /* Get id.                                                                   */
206 /*****************************************************************************/
207 gint
208 gl_color_combo_color_menu_item_get_id (glColorComboColorMenuItem *this)
209 {
210         return this->priv->id;
211 }
212
213
214
215 /*
216  * Local Variables:       -- emacs
217  * mode: C                -- emacs
218  * c-basic-offset: 8      -- emacs
219  * tab-width: 8           -- emacs
220  * indent-tabs-mode: nil  -- emacs
221  * End:                   -- emacs
222  */