2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2007 Hiroyuki Yamamoto & The Claws Mail Team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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.
28 #include <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
32 #include "colorlabel.h"
35 #include "prefs_common.h"
37 static gchar *labels[COLORLABELS] = {
55 static GdkColor default_colors[COLORLABELS] = {
56 { 0, 0xffff, (0x99 << 8), 0x0 },
57 { 0, 0xffff, 0x0, 0x0 },
58 { 0, 0xffff, (0x66 << 8), 0xffff },
59 { 0, 0x0, (0xcc << 8), 0xffff },
60 { 0, 0x0, 0x0, 0xffff },
61 { 0, 0x0, (0x99 << 8), 0x0 },
62 { 0, (0x66 << 8), (0x33 << 8), (0x33 << 8) },
63 { 0, (0xaa << 8), (0xaa << 8), (0xaa << 8) },
64 { 0, (0xc0 << 8), (0x72 << 8), (0x54 << 8) },
65 { 0, (0xc0 << 8), 0x0, 0x0 },
66 { 0, (0xcc << 8), (0x10 << 8), (0x74 << 8) },
67 { 0, (0x50 << 8), (0x94 << 8), (0xcd << 8) },
68 { 0, 0xffff, (0xd5 << 8), 0x0 },
69 { 0, 0x0, (0xd8 << 8), 0x0 },
70 { 0, (0xc0 << 8), (0x60 << 8), (0xc0 << 8) }
74 typedef enum LabelColorChangeFlags_ {
77 LCCF_ALL = LCCF_COLOR | LCCF_LABEL
78 } LabelColorChangeFlags;
80 /* XXX: if you add colors, make sure you also check the procmsg.h.
81 * color indices are stored as 3 bits; that explains the max. of 7 colors */
84 LabelColorChangeFlags changed;
85 /* color here is initialized from default_colors[] at startup */
88 /* XXX: note that the label member is supposed to be dynamically
89 * allocated and freed */
92 } label_colors[NUM_MENUS][COLORLABELS] = {
94 { LCCF_ALL, { 0 }, NULL, NULL },
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 { LCCF_ALL, { 0 }, NULL, NULL },
103 { LCCF_ALL, { 0 }, NULL, NULL },
104 { LCCF_ALL, { 0 }, NULL, NULL },
105 { LCCF_ALL, { 0 }, NULL, NULL },
106 { LCCF_ALL, { 0 }, NULL, NULL },
107 { LCCF_ALL, { 0 }, NULL, NULL },
108 { LCCF_ALL, { 0 }, NULL, NULL }},
110 { LCCF_ALL, { 0 }, NULL, NULL },
111 { LCCF_ALL, { 0 }, NULL, NULL },
112 { LCCF_ALL, { 0 }, NULL, NULL },
113 { LCCF_ALL, { 0 }, NULL, NULL },
114 { LCCF_ALL, { 0 }, NULL, NULL },
115 { LCCF_ALL, { 0 }, NULL, NULL },
116 { LCCF_ALL, { 0 }, NULL, NULL },
117 { LCCF_ALL, { 0 }, NULL, NULL },
118 { LCCF_ALL, { 0 }, NULL, NULL },
119 { LCCF_ALL, { 0 }, NULL, NULL },
120 { LCCF_ALL, { 0 }, NULL, NULL },
121 { LCCF_ALL, { 0 }, NULL, NULL },
122 { LCCF_ALL, { 0 }, NULL, NULL },
123 { LCCF_ALL, { 0 }, NULL, NULL },
124 { LCCF_ALL, { 0 }, NULL, NULL }}
127 #define LABEL_COLOR_WIDTH 28
128 #define LABEL_COLOR_HEIGHT 16
130 #define LABEL_COLORS_ELEMS (sizeof label_colors[0] / sizeof label_colors[0][0])
132 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
133 g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
135 #define INTCOLOR_TO_GDKCOLOR(intcolor, gdkcolor) \
136 gdkcolor.red = ((intcolor >> 16UL) & 0xFFUL) << 8UL; \
137 gdkcolor.green = ((intcolor >> 8UL) & 0xFFUL) << 8UL; \
138 gdkcolor.blue = ((intcolor) & 0xFFUL) << 8UL;
140 static void colorlabel_recreate (gint);
141 static void colorlabel_recreate_label (gint);
143 void colorlabel_update_colortable_from_prefs(void)
147 for (i = 0; i < NUM_MENUS; i++) {
148 for (c = 0; c < COLORLABELS; c++) {
149 INTCOLOR_TO_GDKCOLOR(prefs_common.custom_colorlabel[c].color,
150 label_colors[i][c].color);
151 g_free(label_colors[i][c].label);
152 label_colors[i][c].label =
153 g_strdup(prefs_common.custom_colorlabel[c].label);
159 gint colorlabel_get_color_count(void)
161 return LABEL_COLORS_ELEMS;
164 GdkColor colorlabel_get_color(gint color_index)
166 GdkColor invalid = { 0 };
168 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
170 return label_colors[0][color_index].color;
173 GdkColor colorlabel_get_default_color(gint color_index)
175 GdkColor invalid = { 0 };
177 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
179 return default_colors[color_index];
182 gchar *colorlabel_get_color_default_text(gint color_index)
184 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
186 return labels[color_index];
189 static gboolean colorlabel_drawing_area_expose_event_cb
190 (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
192 GdkDrawable *drawable = widget->window;
193 gulong c = (gulong) GPOINTER_TO_INT(data);
197 INTCOLOR_TO_GDKCOLOR(c, color)
199 gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
201 gc = gdk_gc_new(drawable);
203 gdk_gc_set_foreground(gc, &color);
204 gdk_draw_rectangle(drawable, widget->style->black_gc,
205 FALSE, 0, 0, widget->allocation.width - 1,
206 widget->allocation.height - 1);
207 gdk_draw_rectangle(drawable, gc,
208 TRUE, 1, 1, widget->allocation.width - 2,
209 widget->allocation.height - 2);
216 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
220 widget = gtk_drawing_area_new();
221 gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2,
222 LABEL_COLOR_HEIGHT - 4);
224 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
225 #define CR(r, g, b) ((CL(r) << (gulong) 16) | \
226 (CL(g) << (gulong) 8) | \
229 g_signal_connect(G_OBJECT(widget), "expose_event",
231 (colorlabel_drawing_area_expose_event_cb),
233 ((gint)CR(color.red, color.green, color.blue)));
238 /* XXX: colorlabel_recreate_XXX are there to make sure everything
239 * is initialized ok, without having to call a global _xxx_init_
241 static void colorlabel_recreate_color(gint color)
246 for (i = 0; i < NUM_MENUS; i++) {
247 if (!(label_colors[i][color].changed & LCCF_COLOR))
250 widget = colorlabel_create_color_widget(label_colors[i][color].color);
251 g_return_if_fail(widget);
253 if (label_colors[i][color].widget)
254 gtk_widget_destroy(label_colors[i][color].widget);
256 label_colors[i][color].widget = widget;
257 label_colors[i][color].changed &= ~LCCF_COLOR;
261 static void colorlabel_recreate_label(gint color)
265 for (i = 0; i < NUM_MENUS; i++) {
266 if (!label_colors[i][color].changed & LCCF_LABEL)
269 if (label_colors[i][color].label == NULL)
270 label_colors[i][color].label = g_strdup(gettext(labels[color]));
272 label_colors[i][color].changed &= ~LCCF_LABEL;
276 /* XXX: call this function everytime when you're doing important
277 * stuff with the label_colors[] array */
278 static void colorlabel_recreate(gint color)
280 colorlabel_recreate_label(color);
281 colorlabel_recreate_color(color);
284 static void colorlabel_recreate_all(void)
288 for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
289 colorlabel_recreate(n);
292 /* colorlabel_create_check_color_menu_item() - creates a color
293 * menu item with a check box */
294 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index, gboolean force, gint menu_index)
302 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
304 item = gtk_check_menu_item_new();
307 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
308 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
310 colorlabel_recreate(color_index);
312 /* XXX: gnome-core::panel::menu.c is a great example of
313 * how to create pixmap menus */
314 label = gtk_label_new(label_colors[menu_index][color_index].label);
316 gtk_widget_show(label);
317 hbox = gtk_hbox_new(FALSE, 0);
318 gtk_widget_show(hbox);
319 gtk_container_add(GTK_CONTAINER(item), hbox);
321 vbox = gtk_vbox_new(TRUE, 0);
322 gtk_widget_show(vbox);
323 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
325 gtk_container_add(GTK_CONTAINER(vbox),
326 label_colors[menu_index][color_index].widget);
327 gtk_widget_show(label_colors[menu_index][color_index].widget);
329 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
330 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
331 if (color_index < 9) {
332 accel = gtk_accelerator_get_label(GDK_1+color_index, GDK_CONTROL_MASK);
333 label = gtk_label_new(accel);
334 gtk_widget_show(label);
335 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
337 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
338 g_object_set_data(G_OBJECT(item), "accel_label", label);
340 label = gtk_label_new("");
341 gtk_widget_show(label);
342 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
343 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
344 g_object_set_data(G_OBJECT(item), "accel_label", label);
349 /* Work around a gtk bug (?): without that, the selected menu item's
350 * colored rectangle is drawn at 0,0 in the window...
352 static void refresh_menu (GtkWidget *menushell, gpointer data)
354 GtkMenu *menu = (GtkMenu *)data;
355 GtkWidget *widget = gtk_menu_get_attach_widget(menu);
356 gtk_widget_hide_all(widget);
357 gtk_widget_unrealize(widget);
358 gtk_widget_show_all(widget);
359 gtk_widget_queue_draw(widget);
362 /* colorlabel_create_color_menu() - creates a color menu without
363 * checkitems, probably for use in combo items */
364 GtkWidget *colorlabel_create_color_menu(void)
371 colorlabel_recreate_all();
373 /* create the menu items. each item has its color code attached */
374 menu = gtk_menu_new();
375 g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
377 item = gtk_menu_item_new_with_label(_("None"));
378 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
379 g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
380 gtk_widget_show(item);
382 /* and the color items */
383 for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
388 item = gtk_menu_item_new();
389 g_object_set_data(G_OBJECT(item), "color",
390 GUINT_TO_POINTER(i + 1));
392 label = gtk_label_new(label_colors[0][i].label);
394 gtk_widget_show(label);
395 hbox = gtk_hbox_new(FALSE, 0);
396 gtk_widget_show(hbox);
397 gtk_container_add(GTK_CONTAINER(item), hbox);
399 vbox = gtk_vbox_new(TRUE, 0);
400 gtk_widget_show(vbox);
401 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
403 widget = colorlabel_create_color_widget(label_colors[0][i].color);
404 gtk_widget_show(widget);
405 gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
407 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
408 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
410 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
411 gtk_widget_show(item);
414 g_signal_connect(G_OBJECT(menu), "selection-done",
415 G_CALLBACK(refresh_menu), menu);
416 gtk_widget_show(menu);
421 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
426 menuitem = gtk_menu_get_active(GTK_MENU(menu));
427 color = GPOINTER_TO_UINT
428 (g_object_get_data(G_OBJECT(menuitem), "color"));