2006-02-20 [wwp] 2.0.0cvs66
[claws.git] / src / gtk / colorlabel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /* (alfons) - based on a contribution by Satoshi Nagayasu; revised for colorful 
21  * menu and more Sylpheed integration. The idea to put the code in a separate
22  * file is just that it make it easier to allow "user changeable" label colors.
23  */
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gtk/gtkwidget.h>
31 #include <gtk/gtkimage.h>
32 #include <gtk/gtkmenu.h>
33 #include <gtk/gtkcheckmenuitem.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkalignment.h>
37 #include <gtk/gtkhbox.h>
38 #include <gtk/gtkvbox.h>
39 #include <gtk/gtkwindow.h>
40 #include <gtk/gtkdrawingarea.h>
41
42 #include "colorlabel.h"
43 #include "utils.h"
44 #include "gtkutils.h"
45
46 static gchar *labels[] = {
47         N_("Orange"),
48         N_("Red") ,
49         N_("Pink"),
50         N_("Sky blue"),
51         N_("Blue"),
52         N_("Green"),
53         N_("Brown")
54 };
55
56 typedef enum LabelColorChangeFlags_ {
57         LCCF_COLOR = 1 << 0,
58         LCCF_LABEL = 1 << 1,
59         LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
60 } LabelColorChangeFlags;
61
62 /* XXX: if you add colors, make sure you also check the procmsg.h.
63  * color indices are stored as 3 bits; that explains the max. of 7 colors */
64 static struct 
65 {
66         LabelColorChangeFlags   changed; 
67         GdkColor                color;
68
69         /* XXX: note that the label member is supposed to be dynamically 
70          * allocated and fffreed */
71         gchar                   *label;
72         GtkWidget               *widget;
73 } label_colors[] = {
74         { LCCF_ALL, { 0, 0xffff, (0x99 << 8), 0x0 },            NULL, NULL },
75         { LCCF_ALL, { 0, 0xffff, 0, 0 },                        NULL, NULL },
76         { LCCF_ALL, { 0, 0xffff, (0x66 << 8), 0xffff },         NULL, NULL },
77         { LCCF_ALL, { 0, 0x0, (0xcc << 8), 0xffff },            NULL, NULL },
78         { LCCF_ALL, { 0, 0x0, 0x0, 0xffff },                    NULL, NULL },
79         { LCCF_ALL, { 0, 0x0, 0x99 << 8, 0x0 },                 NULL, NULL },
80         { LCCF_ALL, { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 },     NULL, NULL }
81 };
82
83 #define LABEL_COLOR_WIDTH       28
84 #define LABEL_COLOR_HEIGHT      16
85
86 #define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
87
88 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
89         g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
90
91 static void colorlabel_recreate        (gint);
92 static void colorlabel_recreate_label  (gint);
93
94 gint colorlabel_get_color_count(void)
95 {
96         return LABEL_COLORS_ELEMS;
97 }
98
99 GdkColor colorlabel_get_color(gint color_index)
100 {
101         GdkColor invalid = { 0 };
102
103         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
104
105         return label_colors[color_index].color;
106 }
107
108 gchar *colorlabel_get_color_text(gint color_index)
109 {
110         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
111
112         colorlabel_recreate_label(color_index);
113         return label_colors[color_index].label;
114 }
115
116 static gboolean colorlabel_drawing_area_expose_event_cb
117         (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
118 {
119         GdkDrawable *drawable = widget->window;
120         gulong c = (gulong) GPOINTER_TO_INT(data);
121         GdkColor color;
122         GdkGC *gc;
123
124         color.red   = ((c >> 16UL) & 0xFFUL) << 8UL;
125         color.green = ((c >>  8UL) & 0xFFUL) << 8UL;
126         color.blue  = ((c)         & 0xFFUL) << 8UL;
127
128         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
129
130         gc = gdk_gc_new(drawable);
131
132         gdk_gc_set_foreground(gc, &color);
133         gdk_draw_rectangle(drawable, widget->style->black_gc,
134                            FALSE, 0, 0, widget->allocation.width - 1,
135                            widget->allocation.height - 1);
136         gdk_draw_rectangle(drawable, gc,
137                            TRUE, 1, 1, widget->allocation.width - 2,
138                            widget->allocation.height - 2);
139
140         gdk_gc_unref(gc);                          
141         
142         return FALSE;
143 }
144
145 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
146 {
147         GtkWidget *widget;
148
149         widget = gtk_drawing_area_new();
150         gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2, 
151                                     LABEL_COLOR_HEIGHT - 4);
152
153 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
154 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
155                          (CL(g) << (gulong)  8) | \
156                          (CL(b)))
157
158         g_signal_connect(G_OBJECT(widget), "expose_event", 
159                          G_CALLBACK
160                                 (colorlabel_drawing_area_expose_event_cb),
161                          GINT_TO_POINTER
162                                 ((gint)CR(color.red, color.green, color.blue)));
163
164         return widget;
165 }
166
167 /* XXX: this function to check if menus with colors and labels should
168  * be recreated */
169 gboolean colorlabel_changed(void)
170 {
171         gint n;
172
173         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
174                 if (label_colors[n].changed) 
175                         return TRUE;
176         }
177
178         return FALSE;
179 }
180
181 /* XXX: colorlabel_recreate_XXX are there to make sure everything
182  * is initialized ok, without having to call a global _xxx_init_
183  * function */
184 static void colorlabel_recreate_color(gint color)
185 {
186         GtkWidget *widget;
187
188         if (!(label_colors[color].changed & LCCF_COLOR))
189                 return;
190
191         widget = colorlabel_create_color_widget(label_colors[color].color);
192         g_return_if_fail(widget);
193
194         if (label_colors[color].widget) 
195                 gtk_widget_destroy(label_colors[color].widget);
196
197         label_colors[color].widget = widget;            
198         label_colors[color].changed &= ~LCCF_COLOR;
199 }
200
201 static void colorlabel_recreate_label(gint color)
202 {
203         if (!label_colors[color].changed & LCCF_LABEL)
204                 return;
205
206         if (label_colors[color].label == NULL) 
207                 label_colors[color].label = g_strdup(gettext(labels[color]));
208
209         label_colors[color].changed &= ~LCCF_LABEL;
210 }
211
212 /* XXX: call this function everytime when you're doing important
213  * stuff with the label_colors[] array */
214 static void colorlabel_recreate(gint color)
215 {
216         colorlabel_recreate_label(color);
217         colorlabel_recreate_color(color);
218 }
219
220 static void colorlabel_recreate_all(void)
221 {
222         gint n;
223
224         for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
225                 colorlabel_recreate(n);
226 }
227
228 /* colorlabel_create_check_color_menu_item() - creates a color
229  * menu item with a check box */
230 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
231 {
232         GtkWidget *label; 
233         GtkWidget *hbox; 
234         GtkWidget *vbox; 
235         GtkWidget *item;
236
237         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
238
239         item = gtk_check_menu_item_new();
240
241         colorlabel_recreate(color_index);
242
243         /* XXX: gnome-core::panel::menu.c is a great example of
244          * how to create pixmap menus */
245         label = gtk_label_new(label_colors[color_index].label);
246
247         gtk_widget_show(label);
248         hbox = gtk_hbox_new(FALSE, 0);
249         gtk_widget_show(hbox);
250         gtk_container_add(GTK_CONTAINER(item), hbox);
251
252         vbox = gtk_vbox_new(TRUE, 0);
253         gtk_widget_show(vbox);
254         gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
255
256         gtk_container_add(GTK_CONTAINER(vbox),
257                           label_colors[color_index].widget);
258         gtk_widget_show(label_colors[color_index].widget);
259
260         gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
261         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
262
263         return item;
264 }
265
266 /* Work around a gtk bug (?): without that, the selected menu item's 
267  * colored rectangle is drawn at 0,0 in the window...
268  */
269 static void refresh_menu (GtkWidget *menushell, gpointer data)
270 {
271         GtkMenu *menu = (GtkMenu *)data;
272         GtkWidget *widget = gtk_menu_get_attach_widget(menu);
273         gtk_widget_hide_all(widget);
274         gtk_widget_unrealize(widget);
275         gtk_widget_show_all(widget);
276         gtk_widget_queue_draw(widget);
277 }
278
279 /* colorlabel_create_color_menu() - creates a color menu without 
280  * checkitems, probably for use in combo items */
281 GtkWidget *colorlabel_create_color_menu(void)
282 {
283         GtkWidget *label; 
284         GtkWidget *item;
285         GtkWidget *menu;
286         gint i;
287
288         colorlabel_recreate_all();
289
290         /* create the menu items. each item has its color code attached */
291         menu = gtk_menu_new();
292         g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
293
294         item = gtk_menu_item_new_with_label(_("None"));
295         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
296         g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
297         gtk_widget_show(item);
298
299         /* and the color items */
300         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
301                 GtkWidget *hbox; 
302                 GtkWidget *vbox;
303                 GtkWidget *widget;
304
305                 item  = gtk_menu_item_new();
306                 g_object_set_data(G_OBJECT(item), "color",
307                                   GUINT_TO_POINTER(i + 1));
308
309                 label = gtk_label_new(label_colors[i].label);
310                 
311                 gtk_widget_show(label);
312                 hbox = gtk_hbox_new(FALSE, 0);
313                 gtk_widget_show(hbox);
314                 gtk_container_add(GTK_CONTAINER(item), hbox);
315
316                 vbox = gtk_vbox_new(TRUE, 0);
317                 gtk_widget_show(vbox);
318                 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
319
320                 widget = colorlabel_create_color_widget(label_colors[i].color);
321                 gtk_widget_show(widget);
322                 gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
323
324                 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
325                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
326                 
327                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
328                 gtk_widget_show(item);
329         }
330         
331         g_signal_connect(G_OBJECT(menu), "selection-done", 
332                         G_CALLBACK(refresh_menu), menu);
333         gtk_widget_show(menu);
334
335         return menu;
336 }
337
338 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
339 {
340         GtkWidget *menuitem;
341         guint color;
342
343         menuitem = gtk_menu_get_active(GTK_MENU(menu));
344         color = GPOINTER_TO_UINT
345                 (g_object_get_data(G_OBJECT(menuitem), "color"));
346         return color;
347 }