2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #ifdef GDK_WINDOWING_X11
29 # include <gdk/gdkx.h>
30 #endif /* GDK_WINDOWING_X11 */
32 #include <gtk/gtkwidget.h>
33 #include <gtk/gtkimage.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkcheckmenuitem.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkmenuitem.h>
38 #include <gtk/gtkalignment.h>
39 #include <gtk/gtkhbox.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtkdrawingarea.h>
44 #include "colorlabel.h"
47 static gchar *labels[] = {
57 typedef enum LabelColorChangeFlags_ {
60 LCCF_ALL = LCCF_COLOR | LCCF_LABEL
61 } LabelColorChangeFlags;
63 /* XXX: if you add colors, make sure you also check the procmsg.h.
64 * color indices are stored as 3 bits; that explains the max. of 7 colors */
67 LabelColorChangeFlags changed;
70 /* XXX: note that the label member is supposed to be dynamically
71 * allocated and fffreed */
75 { LCCF_ALL, { 0, 0xffff, (0x99 << 8), 0x0 }, NULL, NULL },
76 { LCCF_ALL, { 0, 0xffff, 0, 0 }, NULL, NULL },
77 { LCCF_ALL, { 0, 0xffff, (0x66 << 8), 0xffff }, NULL, NULL },
78 { LCCF_ALL, { 0, 0x0, (0xcc << 8), 0xffff }, NULL, NULL },
79 { LCCF_ALL, { 0, 0x0, 0x0, 0xffff }, NULL, NULL },
80 { LCCF_ALL, { 0, 0x0, 0x99 << 8, 0x0 }, NULL, NULL },
81 { LCCF_ALL, { 0, 0x66 << 8, 0x33 << 8, 0x33 << 8 }, NULL, NULL }
84 #define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
86 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
87 g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
89 static void colorlabel_recreate (gint);
90 static void colorlabel_recreate_label (gint);
92 gint colorlabel_get_color_count(void)
94 return LABEL_COLORS_ELEMS;
97 GdkColor colorlabel_get_color(gint color_index)
99 GdkColor invalid = { 0 };
101 G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
103 return label_colors[color_index].color;
106 gchar *colorlabel_get_color_text(gint color_index)
108 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
110 colorlabel_recreate_label(color_index);
111 return label_colors[color_index].label;
114 static gboolean colorlabel_drawing_area_expose_event_cb
115 (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
117 GdkDrawable *drawable = widget->window;
118 gulong c = (gulong) GPOINTER_TO_INT(data);
122 color.red = ((c >> 16UL) & 0xFFUL) << 8UL;
123 color.green = ((c >> 8UL) & 0xFFUL) << 8UL;
124 color.blue = ((c) & 0xFFUL) << 8UL;
126 gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
128 gc = gdk_gc_new(drawable);
130 gdk_gc_set_foreground(gc, &color);
131 gdk_draw_rectangle(drawable, gc,
132 TRUE, 0, 0, widget->allocation.width,
133 widget->allocation.height);
134 gdk_draw_rectangle(drawable, widget->style->black_gc,
135 FALSE, 0, 0, widget->allocation.width,
136 widget->allocation.height);
143 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
147 widget = gtk_drawing_area_new();
148 gtk_widget_set_size_request(widget, 16, 16);
150 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
151 #define CR(r, g, b) ((CL(r) << (gulong) 16) | \
152 (CL(g) << (gulong) 8) | \
155 g_signal_connect(G_OBJECT(widget), "expose_event",
157 (colorlabel_drawing_area_expose_event_cb),
159 ((gint)CR(color.red, color.green, color.blue)));
164 /* XXX: this function to check if menus with colors and labels should
166 gboolean colorlabel_changed(void)
170 for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
171 if (label_colors[n].changed)
178 /* XXX: colorlabel_recreate_XXX are there to make sure everything
179 * is initialized ok, without having to call a global _xxx_init_
181 static void colorlabel_recreate_color(gint color)
185 if (!(label_colors[color].changed & LCCF_COLOR))
188 widget = colorlabel_create_color_widget(label_colors[color].color);
189 g_return_if_fail(widget);
191 if (label_colors[color].widget)
192 gtk_widget_destroy(label_colors[color].widget);
194 label_colors[color].widget = widget;
195 label_colors[color].changed &= ~LCCF_COLOR;
198 static void colorlabel_recreate_label(gint color)
200 if (!label_colors[color].changed & LCCF_LABEL)
203 if (label_colors[color].label == NULL)
204 label_colors[color].label = g_strdup(gettext(labels[color]));
206 label_colors[color].changed &= ~LCCF_LABEL;
209 /* XXX: call this function everytime when you're doing important
210 * stuff with the label_colors[] array */
211 static void colorlabel_recreate(gint color)
213 colorlabel_recreate_label(color);
214 colorlabel_recreate_color(color);
217 static void colorlabel_recreate_all(void)
221 for ( n = 0; n < LABEL_COLORS_ELEMS; n++)
222 colorlabel_recreate(n);
225 /* colorlabel_create_check_color_menu_item() - creates a color
226 * menu item with a check box */
227 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
234 G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
236 item = gtk_check_menu_item_new();
238 colorlabel_recreate(color_index);
240 /* XXX: gnome-core::panel::menu.c is a great example of
241 * how to create pixmap menus */
242 label = gtk_label_new(label_colors[color_index].label);
244 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
245 gtk_widget_show(label);
246 hbox = gtk_hbox_new(FALSE, 0);
247 gtk_widget_show(hbox);
248 gtk_container_add(GTK_CONTAINER(item), hbox);
250 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
251 gtk_widget_show(align);
252 gtk_container_set_border_width(GTK_CONTAINER(align), 1);
254 gtk_container_add(GTK_CONTAINER(align), label_colors[color_index].widget);
255 gtk_widget_show(label_colors[color_index].widget);
256 gtk_widget_set_size_request(align, 16, 16);
258 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
259 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
264 /* colorlabel_create_color_menu() - creates a color menu without
265 * checkitems, probably for use in combo items */
266 GtkWidget *colorlabel_create_color_menu(void)
275 colorlabel_recreate_all();
277 /* create the menu items. each item has its color code attached */
278 menu = gtk_menu_new();
279 g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
281 item = gtk_menu_item_new_with_label(_("None"));
282 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
283 g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
284 gtk_widget_show(item);
286 /* and the color items */
287 for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
288 GtkWidget *widget = colorlabel_create_color_widget(label_colors[i].color);
290 item = gtk_menu_item_new();
291 g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(i + 1));
293 label = gtk_label_new(label_colors[i].label);
295 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
296 gtk_widget_show(label);
297 hbox = gtk_hbox_new(FALSE, 0);
298 gtk_widget_show(hbox);
299 gtk_container_add(GTK_CONTAINER(item), hbox);
301 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
302 gtk_widget_show(align);
303 gtk_container_set_border_width(GTK_CONTAINER(align), 1);
305 gtk_container_add(GTK_CONTAINER(align), widget);
306 gtk_widget_show(widget);
307 gtk_widget_set_size_request(align, 16, 16);
309 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
310 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
312 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
313 gtk_widget_show(item);
316 gtk_widget_show(menu);
321 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
327 (g_object_get_data(G_OBJECT(menu), "label_color_menu"), 0);
328 menuitem = gtk_menu_get_active(GTK_MENU(menu));
329 color = GPOINTER_TO_UINT
330 (g_object_get_data(G_OBJECT(menuitem), "color"));