2006-03-17 [colin] 2.0.0cvs149
[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 #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                         if (label_colors[i][c].label != NULL) {
129                                 g_free(label_colors[i][c].label);
130                         }
131                         label_colors[i][c].label =
132                                         g_strdup(prefs_common.custom_colorlabel[c].label);
133                 }
134         }
135 }
136
137
138 gint colorlabel_get_color_count(void)
139 {
140         return LABEL_COLORS_ELEMS;
141 }
142
143 GdkColor colorlabel_get_color(gint color_index)
144 {
145         GdkColor invalid = { 0 };
146
147         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
148
149         return label_colors[0][color_index].color;
150 }
151
152 GdkColor colorlabel_get_default_color(gint color_index)
153 {
154         GdkColor invalid = { 0 };
155
156         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
157
158         return default_colors[color_index];
159 }
160                 
161 gchar *colorlabel_get_color_text(gint color_index)
162 {
163         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
164
165         colorlabel_recreate_label(color_index);
166         return label_colors[0][color_index].label;
167 }
168
169 gchar *colorlabel_get_color_default_text(gint color_index)
170 {
171         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
172
173         return labels[color_index];
174 }
175
176 static gboolean colorlabel_drawing_area_expose_event_cb
177         (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
178 {
179         GdkDrawable *drawable = widget->window;
180         gulong c = (gulong) GPOINTER_TO_INT(data);
181         GdkColor color;
182         GdkGC *gc;
183
184         INTCOLOR_TO_GDKCOLOR(c, color)
185
186         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
187
188         gc = gdk_gc_new(drawable);
189
190         gdk_gc_set_foreground(gc, &color);
191         gdk_draw_rectangle(drawable, widget->style->black_gc,
192                            FALSE, 0, 0, widget->allocation.width - 1,
193                            widget->allocation.height - 1);
194         gdk_draw_rectangle(drawable, gc,
195                            TRUE, 1, 1, widget->allocation.width - 2,
196                            widget->allocation.height - 2);
197
198         gdk_gc_unref(gc);                          
199         
200         return FALSE;
201 }
202
203 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
204 {
205         GtkWidget *widget;
206
207         widget = gtk_drawing_area_new();
208         gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2, 
209                                     LABEL_COLOR_HEIGHT - 4);
210
211 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
212 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
213                          (CL(g) << (gulong)  8) | \
214                          (CL(b)))
215
216         g_signal_connect(G_OBJECT(widget), "expose_event", 
217                          G_CALLBACK
218                                 (colorlabel_drawing_area_expose_event_cb),
219                          GINT_TO_POINTER
220                                 ((gint)CR(color.red, color.green, color.blue)));
221
222         return widget;
223 }
224
225 /* XXX: this function to check if menus with colors and labels should
226  * be recreated */
227 gboolean colorlabel_changed(void)
228 {
229         gint n;
230
231         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
232                 if (label_colors[0][n].changed) 
233                         return TRUE;
234         }
235
236         return FALSE;
237 }
238
239 /* XXX: colorlabel_recreate_XXX are there to make sure everything
240  * is initialized ok, without having to call a global _xxx_init_
241  * function */
242 static void colorlabel_recreate_color(gint color)
243 {
244         GtkWidget *widget;
245         int i;
246         
247         for (i = 0; i < NUM_MENUS; i++) {
248                 if (!(label_colors[i][color].changed & LCCF_COLOR))
249                         continue;
250
251                 widget = colorlabel_create_color_widget(label_colors[i][color].color);
252                 g_return_if_fail(widget);
253
254                 if (label_colors[i][color].widget) 
255                         gtk_widget_destroy(label_colors[i][color].widget);
256
257                 label_colors[i][color].widget = widget;         
258                 label_colors[i][color].changed &= ~LCCF_COLOR;
259         }
260 }
261
262 static void colorlabel_recreate_label(gint color)
263 {
264         int i;
265         
266         for (i = 0; i < NUM_MENUS; i++) {
267                 if (!label_colors[i][color].changed & LCCF_LABEL)
268                         continue;
269
270                 if (label_colors[i][color].label == NULL) 
271                         label_colors[i][color].label = g_strdup(gettext(labels[color]));
272
273                 label_colors[i][color].changed &= ~LCCF_LABEL;
274         }
275 }
276
277 /* XXX: call this function everytime when you're doing important
278  * stuff with the label_colors[] array */
279 static void colorlabel_recreate(gint color)
280 {
281         colorlabel_recreate_label(color);
282         colorlabel_recreate_color(color);
283 }
284
285 static void colorlabel_recreate_all(void)
286 {
287         gint n;
288
289         for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
290                 colorlabel_recreate(n);
291 }
292
293 /* colorlabel_create_check_color_menu_item() - creates a color
294  * menu item with a check box */
295 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index, gboolean force, gint menu_index)
296 {
297         GtkWidget *label; 
298         GtkWidget *hbox; 
299         GtkWidget *vbox; 
300         GtkWidget *item;
301         gchar *accel;
302         
303         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
304
305         item = gtk_check_menu_item_new();
306
307         if (force) {
308                 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
309                 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
310         }
311         colorlabel_recreate(color_index);
312
313         /* XXX: gnome-core::panel::menu.c is a great example of
314          * how to create pixmap menus */
315         label = gtk_label_new(label_colors[menu_index][color_index].label);
316
317         gtk_widget_show(label);
318         hbox = gtk_hbox_new(FALSE, 0);
319         gtk_widget_show(hbox);
320         gtk_container_add(GTK_CONTAINER(item), hbox);
321
322         vbox = gtk_vbox_new(TRUE, 0);
323         gtk_widget_show(vbox);
324         gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
325
326         gtk_container_add(GTK_CONTAINER(vbox),
327                           label_colors[menu_index][color_index].widget);
328         gtk_widget_show(label_colors[menu_index][color_index].widget);
329
330         gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
331         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
332         accel = g_strdup_printf("Ctrl+%c", '1'+color_index);
333         label = gtk_label_new(accel);
334         gtk_widget_show(label);
335         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
336         g_free(accel);
337         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
338         return item;
339 }
340
341 /* Work around a gtk bug (?): without that, the selected menu item's 
342  * colored rectangle is drawn at 0,0 in the window...
343  */
344 static void refresh_menu (GtkWidget *menushell, gpointer data)
345 {
346         GtkMenu *menu = (GtkMenu *)data;
347         GtkWidget *widget = gtk_menu_get_attach_widget(menu);
348         gtk_widget_hide_all(widget);
349         gtk_widget_unrealize(widget);
350         gtk_widget_show_all(widget);
351         gtk_widget_queue_draw(widget);
352 }
353
354 /* colorlabel_create_color_menu() - creates a color menu without 
355  * checkitems, probably for use in combo items */
356 GtkWidget *colorlabel_create_color_menu(void)
357 {
358         GtkWidget *label; 
359         GtkWidget *item;
360         GtkWidget *menu;
361         gint i;
362
363         colorlabel_recreate_all();
364
365         /* create the menu items. each item has its color code attached */
366         menu = gtk_menu_new();
367         g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
368
369         item = gtk_menu_item_new_with_label(_("None"));
370         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
371         g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
372         gtk_widget_show(item);
373
374         /* and the color items */
375         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
376                 GtkWidget *hbox; 
377                 GtkWidget *vbox;
378                 GtkWidget *widget;
379
380                 item  = gtk_menu_item_new();
381                 g_object_set_data(G_OBJECT(item), "color",
382                                   GUINT_TO_POINTER(i + 1));
383
384                 label = gtk_label_new(label_colors[0][i].label);
385                 
386                 gtk_widget_show(label);
387                 hbox = gtk_hbox_new(FALSE, 0);
388                 gtk_widget_show(hbox);
389                 gtk_container_add(GTK_CONTAINER(item), hbox);
390
391                 vbox = gtk_vbox_new(TRUE, 0);
392                 gtk_widget_show(vbox);
393                 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
394
395                 widget = colorlabel_create_color_widget(label_colors[0][i].color);
396                 gtk_widget_show(widget);
397                 gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
398
399                 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
400                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
401                 
402                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
403                 gtk_widget_show(item);
404         }
405         
406         g_signal_connect(G_OBJECT(menu), "selection-done", 
407                         G_CALLBACK(refresh_menu), menu);
408         gtk_widget_show(menu);
409
410         return menu;
411 }
412
413 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
414 {
415         GtkWidget *menuitem;
416         guint color;
417
418         menuitem = gtk_menu_get_active(GTK_MENU(menu));
419         color = GPOINTER_TO_UINT
420                 (g_object_get_data(G_OBJECT(menuitem), "color"));
421         return color;
422 }