2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2006 Hiroyuki Yamamoto & The Sylpheed Claws 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 if (label_colors[i][c].label != NULL) {
129 g_free(label_colors[i][c].label);
131 label_colors[i][c].label =
132 g_strdup(prefs_common.custom_colorlabel[c].label);
138 gint colorlabel_get_color_count(void)
140 return LABEL_COLORS_ELEMS;
143 GdkColor colorlabel_get_color(gint color_index)
145 GdkColor invalid = { 0 };
147 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
149 return label_colors[0][color_index].color;
152 GdkColor colorlabel_get_default_color(gint color_index)
154 GdkColor invalid = { 0 };
156 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
158 return default_colors[color_index];
161 gchar *colorlabel_get_color_text(gint color_index)
163 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
165 colorlabel_recreate_label(color_index);
166 return label_colors[0][color_index].label;
169 gchar *colorlabel_get_color_default_text(gint color_index)
171 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
173 return labels[color_index];
176 static gboolean colorlabel_drawing_area_expose_event_cb
177 (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
179 GdkDrawable *drawable = widget->window;
180 gulong c = (gulong) GPOINTER_TO_INT(data);
184 INTCOLOR_TO_GDKCOLOR(c, color)
186 gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
188 gc = gdk_gc_new(drawable);
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);
203 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
207 widget = gtk_drawing_area_new();
208 gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2,
209 LABEL_COLOR_HEIGHT - 4);
211 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
212 #define CR(r, g, b) ((CL(r) << (gulong) 16) | \
213 (CL(g) << (gulong) 8) | \
216 g_signal_connect(G_OBJECT(widget), "expose_event",
218 (colorlabel_drawing_area_expose_event_cb),
220 ((gint)CR(color.red, color.green, color.blue)));
225 /* XXX: this function to check if menus with colors and labels should
227 gboolean colorlabel_changed(void)
231 for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
232 if (label_colors[0][n].changed)
239 /* XXX: colorlabel_recreate_XXX are there to make sure everything
240 * is initialized ok, without having to call a global _xxx_init_
242 static void colorlabel_recreate_color(gint color)
247 for (i = 0; i < NUM_MENUS; i++) {
248 if (!(label_colors[i][color].changed & LCCF_COLOR))
251 widget = colorlabel_create_color_widget(label_colors[i][color].color);
252 g_return_if_fail(widget);
254 if (label_colors[i][color].widget)
255 gtk_widget_destroy(label_colors[i][color].widget);
257 label_colors[i][color].widget = widget;
258 label_colors[i][color].changed &= ~LCCF_COLOR;
262 static void colorlabel_recreate_label(gint color)
266 for (i = 0; i < NUM_MENUS; i++) {
267 if (!label_colors[i][color].changed & LCCF_LABEL)
270 if (label_colors[i][color].label == NULL)
271 label_colors[i][color].label = g_strdup(gettext(labels[color]));
273 label_colors[i][color].changed &= ~LCCF_LABEL;
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)
281 colorlabel_recreate_label(color);
282 colorlabel_recreate_color(color);
285 static void colorlabel_recreate_all(void)
289 for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
290 colorlabel_recreate(n);
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)
303 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
305 item = gtk_check_menu_item_new();
308 label_colors[menu_index][color_index].changed |= LCCF_COLOR;
309 label_colors[menu_index][color_index].changed |= LCCF_LABEL;
311 colorlabel_recreate(color_index);
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);
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);
322 vbox = gtk_vbox_new(TRUE, 0);
323 gtk_widget_show(vbox);
324 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
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);
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);
337 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
341 /* Work around a gtk bug (?): without that, the selected menu item's
342 * colored rectangle is drawn at 0,0 in the window...
344 static void refresh_menu (GtkWidget *menushell, gpointer data)
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);
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)
363 colorlabel_recreate_all();
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);
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);
374 /* and the color items */
375 for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
380 item = gtk_menu_item_new();
381 g_object_set_data(G_OBJECT(item), "color",
382 GUINT_TO_POINTER(i + 1));
384 label = gtk_label_new(label_colors[0][i].label);
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);
391 vbox = gtk_vbox_new(TRUE, 0);
392 gtk_widget_show(vbox);
393 gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
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);
399 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
400 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
402 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
403 gtk_widget_show(item);
406 g_signal_connect(G_OBJECT(menu), "selection-done",
407 G_CALLBACK(refresh_menu), menu);
408 gtk_widget_show(menu);
413 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
418 menuitem = gtk_menu_get_active(GTK_MENU(menu));
419 color = GPOINTER_TO_UINT
420 (g_object_get_data(G_OBJECT(menuitem), "color"));