2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 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 do if ((color) < 0 || (color) >= LABEL_COLORS_ELEMS) { \
137 #define INTCOLOR_TO_GDKCOLOR(intcolor, gdkcolor) \
138 gdkcolor.red = ((intcolor >> 16UL) & 0xFFUL) << 8UL; \
139 gdkcolor.green = ((intcolor >> 8UL) & 0xFFUL) << 8UL; \
140 gdkcolor.blue = ((intcolor) & 0xFFUL) << 8UL;
142 static void colorlabel_recreate (gint);
143 static void colorlabel_recreate_label (gint);
145 void colorlabel_update_colortable_from_prefs(void)
149 for (i = 0; i < NUM_MENUS; i++) {
150 for (c = 0; c < COLORLABELS; c++) {
151 INTCOLOR_TO_GDKCOLOR(prefs_common.custom_colorlabel[c].color,
152 label_colors[i][c].color);
153 g_free(label_colors[i][c].label);
154 label_colors[i][c].label =
155 g_strdup(prefs_common.custom_colorlabel[c].label);
161 gint colorlabel_get_color_count(void)
163 return LABEL_COLORS_ELEMS;
166 GdkColor colorlabel_get_color(gint color_index)
168 GdkColor invalid = { 0 };
170 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
172 return label_colors[0][color_index].color;
175 GdkColor colorlabel_get_default_color(gint color_index)
177 GdkColor invalid = { 0 };
179 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
181 return default_colors[color_index];
184 gchar *colorlabel_get_color_default_text(gint color_index)
186 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
188 return labels[color_index];
191 static gboolean colorlabel_drawing_area_expose_event_cb
192 #if !GTK_CHECK_VERSION(3, 0, 0)
193 (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
195 (GtkWidget *widget, cairo_t *cr, gpointer data)
198 #if !GTK_CHECK_VERSION(3, 0, 0)
200 GdkWindow *drawable = gtk_widget_get_window(widget);
202 GtkAllocation allocation;
203 gulong c = (gulong) GPOINTER_TO_INT(data);
206 INTCOLOR_TO_GDKCOLOR(c, color)
208 #if !GTK_CHECK_VERSION(3, 0, 0)
209 gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
210 cr = gdk_cairo_create(drawable);
212 gtk_widget_get_allocation(widget, &allocation);
214 cairo_set_source_rgb(cr, 0., 0., 0.);
215 cairo_rectangle(cr, 0, 0,
216 allocation.width - 1,
217 allocation.height - 1);
219 gdk_cairo_set_source_color(cr, &color);
220 cairo_rectangle(cr, 1, 1,
221 allocation.width - 2,
222 allocation.height - 2);
224 #if !GTK_CHECK_VERSION(3, 0, 0)
231 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
235 widget = gtk_drawing_area_new();
236 gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2,
237 LABEL_COLOR_HEIGHT - 4);
239 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
240 #define CR(r, g, b) ((CL(r) << (gulong) 16) | \
241 (CL(g) << (gulong) 8) | \
244 #if !GTK_CHECK_VERSION(3, 0, 0)
245 g_signal_connect(G_OBJECT(widget), "expose_event",
247 (colorlabel_drawing_area_expose_event_cb),
249 ((gint)CR(color.red, color.green, color.blue)));
251 g_signal_connect(G_OBJECT(widget), "draw",
253 (colorlabel_drawing_area_expose_event_cb),
255 ((gint)CR(color.red, color.green, color.blue)));
261 /* XXX: colorlabel_recreate_XXX are there to make sure everything
262 * is initialized ok, without having to call a global _xxx_init_
264 static void colorlabel_recreate_color(gint color)
269 for (i = 0; i < NUM_MENUS; i++) {
270 if (!(label_colors[i][color].changed & LCCF_COLOR))
273 widget = colorlabel_create_color_widget(label_colors[i][color].color);
274 cm_return_if_fail(widget);
276 if (label_colors[i][color].widget)
277 gtk_widget_destroy(label_colors[i][color].widget);
279 label_colors[i][color].widget = widget;
280 label_colors[i][color].changed &= ~LCCF_COLOR;
284 static void colorlabel_recreate_label(gint color)
288 for (i = 0; i < NUM_MENUS; i++) {
289 if (!(label_colors[i][color].changed & LCCF_LABEL))
292 if (label_colors[i][color].label == NULL)
293 label_colors[i][color].label = g_strdup(gettext(labels[color]));
295 label_colors[i][color].changed &= ~LCCF_LABEL;
299 /* XXX: call this function everytime when you're doing important
300 * stuff with the label_colors[] array */
301 static void colorlabel_recreate(gint color)
303 colorlabel_recreate_label(color);
304 colorlabel_recreate_color(color);
307 static void colorlabel_recreate_all(void)
311 for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
312 colorlabel_recreate(n);
315 /* colorlabel_create_check_color_menu_item() - creates a color
316 * menu item with a check box */
317 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index, gboolean force, gint menu_index)
325 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
327 item = gtk_check_menu_item_new();
330 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
331 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
333 colorlabel_recreate(color_index);
335 /* XXX: gnome-core::panel::menu.c is a great example of
336 * how to create pixmap menus */
337 label = gtk_label_new(label_colors[menu_index][color_index].label);
339 gtk_widget_show(label);
340 hbox = gtk_hbox_new(FALSE, 0);
341 gtk_widget_show(hbox);
342 gtk_container_add(GTK_CONTAINER(item), hbox);
344 vbox = gtk_vbox_new(TRUE, 0);
345 gtk_widget_show(vbox);
346 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
348 gtk_container_add(GTK_CONTAINER(vbox),
349 label_colors[menu_index][color_index].widget);
350 gtk_widget_show(label_colors[menu_index][color_index].widget);
352 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
353 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
354 if (color_index < 9) {
355 accel = gtk_accelerator_get_label(GDK_KEY_1+color_index, GDK_CONTROL_MASK);
356 label = gtk_label_new(accel);
357 gtk_widget_show(label);
358 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
360 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
361 g_object_set_data(G_OBJECT(item), "accel_label", label);
363 label = gtk_label_new("");
364 gtk_widget_show(label);
365 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
366 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
367 g_object_set_data(G_OBJECT(item), "accel_label", label);
372 /* colorlabel_create_color_menu() - creates a color menu without
373 * checkitems, probably for use in combo items */
374 GtkWidget *colorlabel_create_color_menu(void)
381 colorlabel_recreate_all();
383 /* create the menu items. each item has its color code attached */
384 menu = gtk_menu_new();
385 g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
387 item = gtk_menu_item_new_with_label(_("None"));
388 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
389 g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
390 gtk_widget_show(item);
392 item = gtk_separator_menu_item_new();
393 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
394 gtk_widget_show(item);
396 /* and the color items */
397 for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
402 item = gtk_menu_item_new();
403 g_object_set_data(G_OBJECT(item), "color",
404 GUINT_TO_POINTER(i + 1));
406 label = gtk_label_new(label_colors[0][i].label);
408 gtk_widget_show(label);
409 hbox = gtk_hbox_new(FALSE, 0);
410 gtk_widget_show(hbox);
411 gtk_container_add(GTK_CONTAINER(item), hbox);
413 vbox = gtk_vbox_new(TRUE, 0);
414 gtk_widget_show(vbox);
415 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
417 widget = colorlabel_create_color_widget(label_colors[0][i].color);
418 gtk_widget_show(widget);
419 gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 0);
421 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
422 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
424 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
425 gtk_widget_show(item);
428 gtk_widget_show(menu);
433 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
438 menuitem = gtk_menu_get_active(GTK_MENU(menu));
439 color = GPOINTER_TO_UINT
440 (g_object_get_data(G_OBJECT(menuitem), "color"));