2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2006 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 <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>
42 #include "colorlabel.h"
45 #include "prefs_common.h"
47 static gchar *labels[COLORLABELS] = {
57 static GdkColor default_colors[COLORLABELS] = {
58 { 0, 0xffff, (0x99 << 8), 0x0 },
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 }
67 typedef enum LabelColorChangeFlags_ {
70 LCCF_ALL = LCCF_COLOR | LCCF_LABEL
71 } LabelColorChangeFlags;
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 */
77 LabelColorChangeFlags changed;
78 /* color here is initialized from default_colors[] at startup */
81 /* XXX: note that the label member is supposed to be dynamically
82 * allocated and fffreed */
85 } label_colors[NUM_MENUS][COLORLABELS] = {
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 }},
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 }}
104 #define LABEL_COLOR_WIDTH 28
105 #define LABEL_COLOR_HEIGHT 16
107 #define LABEL_COLORS_ELEMS (sizeof label_colors[0] / sizeof label_colors[0][0])
109 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
110 g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
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;
117 static void colorlabel_recreate (gint);
118 static void colorlabel_recreate_label (gint);
120 void colorlabel_update_colortable_from_prefs(void)
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);
136 gint colorlabel_get_color_count(void)
138 return LABEL_COLORS_ELEMS;
141 GdkColor colorlabel_get_color(gint color_index)
143 GdkColor invalid = { 0 };
145 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
147 return label_colors[0][color_index].color;
150 GdkColor colorlabel_get_default_color(gint color_index)
152 GdkColor invalid = { 0 };
154 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
156 return default_colors[color_index];
159 gchar *colorlabel_get_color_text(gint color_index)
161 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
163 colorlabel_recreate_label(color_index);
164 return label_colors[0][color_index].label;
167 gchar *colorlabel_get_color_default_text(gint color_index)
169 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
171 return labels[color_index];
174 static gboolean colorlabel_drawing_area_expose_event_cb
175 (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
177 GdkDrawable *drawable = widget->window;
178 gulong c = (gulong) GPOINTER_TO_INT(data);
182 INTCOLOR_TO_GDKCOLOR(c, color)
184 gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
186 gc = gdk_gc_new(drawable);
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);
201 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
205 widget = gtk_drawing_area_new();
206 gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2,
207 LABEL_COLOR_HEIGHT - 4);
209 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
210 #define CR(r, g, b) ((CL(r) << (gulong) 16) | \
211 (CL(g) << (gulong) 8) | \
214 g_signal_connect(G_OBJECT(widget), "expose_event",
216 (colorlabel_drawing_area_expose_event_cb),
218 ((gint)CR(color.red, color.green, color.blue)));
223 /* XXX: this function to check if menus with colors and labels should
225 gboolean colorlabel_changed(void)
229 for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
230 if (label_colors[0][n].changed)
237 /* XXX: colorlabel_recreate_XXX are there to make sure everything
238 * is initialized ok, without having to call a global _xxx_init_
240 static void colorlabel_recreate_color(gint color)
245 for (i = 0; i < NUM_MENUS; i++) {
246 if (!(label_colors[i][color].changed & LCCF_COLOR))
249 widget = colorlabel_create_color_widget(label_colors[i][color].color);
250 g_return_if_fail(widget);
252 if (label_colors[i][color].widget)
253 gtk_widget_destroy(label_colors[i][color].widget);
255 label_colors[i][color].widget = widget;
256 label_colors[i][color].changed &= ~LCCF_COLOR;
260 static void colorlabel_recreate_label(gint color)
264 for (i = 0; i < NUM_MENUS; i++) {
265 if (!label_colors[i][color].changed & LCCF_LABEL)
268 if (label_colors[i][color].label == NULL)
269 label_colors[i][color].label = g_strdup(gettext(labels[color]));
271 label_colors[i][color].changed &= ~LCCF_LABEL;
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)
279 colorlabel_recreate_label(color);
280 colorlabel_recreate_color(color);
283 static void colorlabel_recreate_all(void)
287 for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
288 colorlabel_recreate(n);
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)
301 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
303 item = gtk_check_menu_item_new();
306 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
307 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
309 colorlabel_recreate(color_index);
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);
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);
320 vbox = gtk_vbox_new(TRUE, 0);
321 gtk_widget_show(vbox);
322 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
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);
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);
335 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
339 /* Work around a gtk bug (?): without that, the selected menu item's
340 * colored rectangle is drawn at 0,0 in the window...
342 static void refresh_menu (GtkWidget *menushell, gpointer data)
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);
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)
361 colorlabel_recreate_all();
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);
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);
372 /* and the color items */
373 for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
378 item = gtk_menu_item_new();
379 g_object_set_data(G_OBJECT(item), "color",
380 GUINT_TO_POINTER(i + 1));
382 label = gtk_label_new(label_colors[0][i].label);
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);
389 vbox = gtk_vbox_new(TRUE, 0);
390 gtk_widget_show(vbox);
391 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
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);
397 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
398 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
400 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
401 gtk_widget_show(item);
404 g_signal_connect(G_OBJECT(menu), "selection-done",
405 G_CALLBACK(refresh_menu), menu);
406 gtk_widget_show(menu);
411 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
416 menuitem = gtk_menu_get_active(GTK_MENU(menu));
417 color = GPOINTER_TO_UINT
418 (g_object_get_data(G_OBJECT(menuitem), "color"));