2006-11-18 [paul] 2.6.0cvs55
[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 Claws Mail 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 #include "prefs_common.h"
46
47 static gchar *labels[COLORLABELS] = {
48         N_("Orange"),
49         N_("Red") ,
50         N_("Pink"),
51         N_("Sky blue"),
52         N_("Blue"),
53         N_("Green"),
54         N_("Brown")
55 };
56
57 static GdkColor default_colors[COLORLABELS] = {
58         { 0, 0xffff, (0x99 << 8), 0x0 },
59         { 0, 0xffff, 0, 0 },
60         { 0, 0xffff, (0x66 << 8), 0xffff },
61         { 0, 0x0, (0xcc << 8), 0xffff },
62         { 0, 0x0, 0x0, 0xffff },
63         { 0, 0x0, (0x99 << 8), 0x0 },
64         { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 }
65 };
66         
67 typedef enum LabelColorChangeFlags_ {
68         LCCF_COLOR = 1 << 0,
69         LCCF_LABEL = 1 << 1,
70         LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
71 } LabelColorChangeFlags;
72
73 /* XXX: if you add colors, make sure you also check the procmsg.h.
74  * color indices are stored as 3 bits; that explains the max. of 7 colors */
75 static struct 
76 {
77         LabelColorChangeFlags   changed; 
78         /* color here is initialized from default_colors[] at startup */
79         GdkColor                color;
80
81         /* XXX: note that the label member is supposed to be dynamically 
82          * allocated and fffreed */
83         gchar                   *label;
84         GtkWidget               *widget;
85 } label_colors[NUM_MENUS][COLORLABELS] = {
86     {
87         { LCCF_ALL, { 0 }, NULL, NULL },
88         { LCCF_ALL, { 0 }, NULL, NULL },
89         { LCCF_ALL, { 0 }, NULL, NULL },
90         { LCCF_ALL, { 0 }, NULL, NULL },
91         { LCCF_ALL, { 0 }, NULL, NULL },
92         { LCCF_ALL, { 0 }, NULL, NULL },
93         { LCCF_ALL, { 0 }, NULL, NULL }},
94     {
95         { LCCF_ALL, { 0 }, NULL, NULL },
96         { LCCF_ALL, { 0 }, NULL, NULL },
97         { LCCF_ALL, { 0 }, NULL, NULL },
98         { LCCF_ALL, { 0 }, NULL, NULL },
99         { LCCF_ALL, { 0 }, NULL, NULL },
100         { LCCF_ALL, { 0 }, NULL, NULL },
101         { LCCF_ALL, { 0 }, NULL, NULL }}
102 };
103
104 #define LABEL_COLOR_WIDTH       28
105 #define LABEL_COLOR_HEIGHT      16
106
107 #define LABEL_COLORS_ELEMS (sizeof label_colors[0] / sizeof label_colors[0][0])
108
109 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
110         g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
111
112 #define INTCOLOR_TO_GDKCOLOR(intcolor, gdkcolor) \
113         gdkcolor.red   = ((intcolor >> 16UL) & 0xFFUL) << 8UL; \
114         gdkcolor.green = ((intcolor >>  8UL) & 0xFFUL) << 8UL; \
115         gdkcolor.blue  = ((intcolor)         & 0xFFUL) << 8UL;
116
117 static void colorlabel_recreate        (gint);
118 static void colorlabel_recreate_label  (gint);
119
120 void colorlabel_update_colortable_from_prefs(void)
121 {
122         gint i, c;
123
124         for (i = 0; i < NUM_MENUS; i++) {
125                 for (c = 0; c < COLORLABELS; c++) {
126                         INTCOLOR_TO_GDKCOLOR(prefs_common.custom_colorlabel[c].color,
127                                         label_colors[i][c].color);
128                         g_free(label_colors[i][c].label);
129                         label_colors[i][c].label =
130                                         g_strdup(prefs_common.custom_colorlabel[c].label);
131                 }
132         }
133 }
134
135
136 gint colorlabel_get_color_count(void)
137 {
138         return LABEL_COLORS_ELEMS;
139 }
140
141 GdkColor colorlabel_get_color(gint color_index)
142 {
143         GdkColor invalid = { 0 };
144
145         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
146
147         return label_colors[0][color_index].color;
148 }
149
150 GdkColor colorlabel_get_default_color(gint color_index)
151 {
152         GdkColor invalid = { 0 };
153
154         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
155
156         return default_colors[color_index];
157 }
158                 
159 gchar *colorlabel_get_color_text(gint color_index)
160 {
161         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
162
163         colorlabel_recreate_label(color_index);
164         return label_colors[0][color_index].label;
165 }
166
167 gchar *colorlabel_get_color_default_text(gint color_index)
168 {
169         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
170
171         return labels[color_index];
172 }
173
174 static gboolean colorlabel_drawing_area_expose_event_cb
175         (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
176 {
177         GdkDrawable *drawable = widget->window;
178         gulong c = (gulong) GPOINTER_TO_INT(data);
179         GdkColor color;
180         GdkGC *gc;
181
182         INTCOLOR_TO_GDKCOLOR(c, color)
183
184         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
185
186         gc = gdk_gc_new(drawable);
187
188         gdk_gc_set_foreground(gc, &color);
189         gdk_draw_rectangle(drawable, widget->style->black_gc,
190                            FALSE, 0, 0, widget->allocation.width - 1,
191                            widget->allocation.height - 1);
192         gdk_draw_rectangle(drawable, gc,
193                            TRUE, 1, 1, widget->allocation.width - 2,
194                            widget->allocation.height - 2);
195
196         gdk_gc_unref(gc);                          
197         
198         return FALSE;
199 }
200
201 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
202 {
203         GtkWidget *widget;
204
205         widget = gtk_drawing_area_new();
206         gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2, 
207                                     LABEL_COLOR_HEIGHT - 4);
208
209 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
210 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
211                          (CL(g) << (gulong)  8) | \
212                          (CL(b)))
213
214         g_signal_connect(G_OBJECT(widget), "expose_event", 
215                          G_CALLBACK
216                                 (colorlabel_drawing_area_expose_event_cb),
217                          GINT_TO_POINTER
218                                 ((gint)CR(color.red, color.green, color.blue)));
219
220         return widget;
221 }
222
223 /* XXX: this function to check if menus with colors and labels should
224  * be recreated */
225 gboolean colorlabel_changed(void)
226 {
227         gint n;
228
229         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
230                 if (label_colors[0][n].changed) 
231                         return TRUE;
232         }
233
234         return FALSE;
235 }
236
237 /* XXX: colorlabel_recreate_XXX are there to make sure everything
238  * is initialized ok, without having to call a global _xxx_init_
239  * function */
240 static void colorlabel_recreate_color(gint color)
241 {
242         GtkWidget *widget;
243         int i;
244         
245         for (i = 0; i < NUM_MENUS; i++) {
246                 if (!(label_colors[i][color].changed & LCCF_COLOR))
247                         continue;
248
249                 widget = colorlabel_create_color_widget(label_colors[i][color].color);
250                 g_return_if_fail(widget);
251
252                 if (label_colors[i][color].widget) 
253                         gtk_widget_destroy(label_colors[i][color].widget);
254
255                 label_colors[i][color].widget = widget;         
256                 label_colors[i][color].changed &= ~LCCF_COLOR;
257         }
258 }
259
260 static void colorlabel_recreate_label(gint color)
261 {
262         int i;
263         
264         for (i = 0; i < NUM_MENUS; i++) {
265                 if (!label_colors[i][color].changed & LCCF_LABEL)
266                         continue;
267
268                 if (label_colors[i][color].label == NULL) 
269                         label_colors[i][color].label = g_strdup(gettext(labels[color]));
270
271                 label_colors[i][color].changed &= ~LCCF_LABEL;
272         }
273 }
274
275 /* XXX: call this function everytime when you're doing important
276  * stuff with the label_colors[] array */
277 static void colorlabel_recreate(gint color)
278 {
279         colorlabel_recreate_label(color);
280         colorlabel_recreate_color(color);
281 }
282
283 static void colorlabel_recreate_all(void)
284 {
285         gint n;
286
287         for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
288                 colorlabel_recreate(n);
289 }
290
291 /* colorlabel_create_check_color_menu_item() - creates a color
292  * menu item with a check box */
293 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index, gboolean force, gint menu_index)
294 {
295         GtkWidget *label; 
296         GtkWidget *hbox; 
297         GtkWidget *vbox; 
298         GtkWidget *item;
299         gchar *accel;
300         
301         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
302
303         item = gtk_check_menu_item_new();
304
305         if (force) {
306                 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
307                 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
308         }
309         colorlabel_recreate(color_index);
310
311         /* XXX: gnome-core::panel::menu.c is a great example of
312          * how to create pixmap menus */
313         label = gtk_label_new(label_colors[menu_index][color_index].label);
314
315         gtk_widget_show(label);
316         hbox = gtk_hbox_new(FALSE, 0);
317         gtk_widget_show(hbox);
318         gtk_container_add(GTK_CONTAINER(item), hbox);
319
320         vbox = gtk_vbox_new(TRUE, 0);
321         gtk_widget_show(vbox);
322         gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
323
324         gtk_container_add(GTK_CONTAINER(vbox),
325                           label_colors[menu_index][color_index].widget);
326         gtk_widget_show(label_colors[menu_index][color_index].widget);
327
328         gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
329         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
330         accel = g_strdup_printf("Ctrl+%c", '1'+color_index);
331         label = gtk_label_new(accel);
332         gtk_widget_show(label);
333         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
334         g_free(accel);
335         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
336         return item;
337 }
338
339 /* Work around a gtk bug (?): without that, the selected menu item's 
340  * colored rectangle is drawn at 0,0 in the window...
341  */
342 static void refresh_menu (GtkWidget *menushell, gpointer data)
343 {
344         GtkMenu *menu = (GtkMenu *)data;
345         GtkWidget *widget = gtk_menu_get_attach_widget(menu);
346         gtk_widget_hide_all(widget);
347         gtk_widget_unrealize(widget);
348         gtk_widget_show_all(widget);
349         gtk_widget_queue_draw(widget);
350 }
351
352 /* colorlabel_create_color_menu() - creates a color menu without 
353  * checkitems, probably for use in combo items */
354 GtkWidget *colorlabel_create_color_menu(void)
355 {
356         GtkWidget *label; 
357         GtkWidget *item;
358         GtkWidget *menu;
359         gint i;
360
361         colorlabel_recreate_all();
362
363         /* create the menu items. each item has its color code attached */
364         menu = gtk_menu_new();
365         g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
366
367         item = gtk_menu_item_new_with_label(_("None"));
368         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
369         g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
370         gtk_widget_show(item);
371
372         /* and the color items */
373         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
374                 GtkWidget *hbox; 
375                 GtkWidget *vbox;
376                 GtkWidget *widget;
377
378                 item  = gtk_menu_item_new();
379                 g_object_set_data(G_OBJECT(item), "color",
380                                   GUINT_TO_POINTER(i + 1));
381
382                 label = gtk_label_new(label_colors[0][i].label);
383                 
384                 gtk_widget_show(label);
385                 hbox = gtk_hbox_new(FALSE, 0);
386                 gtk_widget_show(hbox);
387                 gtk_container_add(GTK_CONTAINER(item), hbox);
388
389                 vbox = gtk_vbox_new(TRUE, 0);
390                 gtk_widget_show(vbox);
391                 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
392
393                 widget = colorlabel_create_color_widget(label_colors[0][i].color);
394                 gtk_widget_show(widget);
395                 gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
396
397                 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
398                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
399                 
400                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
401                 gtk_widget_show(item);
402         }
403         
404         g_signal_connect(G_OBJECT(menu), "selection-done", 
405                         G_CALLBACK(refresh_menu), menu);
406         gtk_widget_show(menu);
407
408         return menu;
409 }
410
411 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
412 {
413         GtkWidget *menuitem;
414         guint color;
415
416         menuitem = gtk_menu_get_active(GTK_MENU(menu));
417         color = GPOINTER_TO_UINT
418                 (g_object_get_data(G_OBJECT(menuitem), "color"));
419         return color;
420 }