80c222d646f47825fede8c24580a986405384b3b
[claws.git] / src / gtk / colorlabel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2004 Hiroyuki Yamamoto & The Sylpheed Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
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.
23  */
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #ifdef GDK_WINDOWING_X11
29 #       include <gdk/gdkx.h>
30 #endif /* GDK_WINDOWING_X11 */
31 #include <gtk/gtk.h>
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>
42
43 #include "intl.h"
44 #include "colorlabel.h"
45 #include "utils.h"
46
47 static gchar *labels[] = {
48         N_("Orange"),
49         N_("Red") ,
50         N_("Pink"),
51         N_("Sky blue"),
52         N_("Blue"),
53         N_("Green"),
54         N_("Brown")
55 };
56
57 typedef enum LabelColorChangeFlags_ {
58         LCCF_COLOR = 1 << 0,
59         LCCF_LABEL = 1 << 1,
60         LCCF_ALL   = LCCF_COLOR | LCCF_LABEL
61 } LabelColorChangeFlags;
62
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 */
65 static struct 
66 {
67         LabelColorChangeFlags   changed; 
68         GdkColor                color;
69
70         /* XXX: note that the label member is supposed to be dynamically 
71          * allocated and fffreed */
72         gchar                   *label;
73         GtkWidget               *widget;
74 } label_colors[] = {
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 }
82 };
83
84 #define LABEL_COLOR_WIDTH       28
85 #define LABEL_COLOR_HEIGHT      16
86
87 #define LABEL_COLORS_ELEMS (sizeof label_colors / sizeof label_colors[0])
88
89 #define G_RETURN_VAL_IF_INVALID_COLOR(color, val) \
90         g_return_val_if_fail((color) >= 0 && (color) < LABEL_COLORS_ELEMS, (val))
91
92 static void colorlabel_recreate        (gint);
93 static void colorlabel_recreate_label  (gint);
94
95 gint colorlabel_get_color_count(void)
96 {
97         return LABEL_COLORS_ELEMS;
98 }
99
100 GdkColor colorlabel_get_color(gint color_index)
101 {
102         GdkColor invalid = { 0 };
103
104         G_RETURN_VAL_IF_INVALID_COLOR(color_index, invalid);
105
106         return label_colors[color_index].color;
107 }
108
109 gchar *colorlabel_get_color_text(gint color_index)
110 {
111         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
112
113         colorlabel_recreate_label(color_index);
114         return label_colors[color_index].label;
115 }
116
117 static gboolean colorlabel_drawing_area_expose_event_cb
118         (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
119 {
120         GdkDrawable *drawable = widget->window;
121         gulong c = (gulong) GPOINTER_TO_INT(data);
122         GdkColor color;
123         GdkGC *gc;
124
125         color.red   = ((c >> 16UL) & 0xFFUL) << 8UL;
126         color.green = ((c >>  8UL) & 0xFFUL) << 8UL;
127         color.blue  = ((c)         & 0xFFUL) << 8UL;
128
129         gdk_colormap_alloc_color(gtk_widget_get_colormap(widget), &color, FALSE, TRUE);
130
131         gc = gdk_gc_new(drawable);
132
133         gdk_gc_set_foreground(gc, &color);
134         gdk_draw_rectangle(drawable, gc,
135                            TRUE, 0, 0, widget->allocation.width - 1,
136                            widget->allocation.height - 1);
137         gdk_draw_rectangle(drawable, widget->style->black_gc,
138                            FALSE, 0, 0, widget->allocation.width - 1,
139                            widget->allocation.height - 1);
140
141         gdk_gc_unref(gc);                          
142
143         return FALSE;
144 }
145
146 static GtkWidget *colorlabel_create_color_widget(GdkColor color)
147 {
148         GtkWidget *widget;
149
150         widget = gtk_drawing_area_new();
151         gtk_widget_set_size_request(widget, LABEL_COLOR_WIDTH - 2, 
152                                     LABEL_COLOR_HEIGHT - 4);
153
154 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
155 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
156                          (CL(g) << (gulong)  8) | \
157                          (CL(b)))
158
159         g_signal_connect(G_OBJECT(widget), "expose_event", 
160                          G_CALLBACK
161                                 (colorlabel_drawing_area_expose_event_cb),
162                          GINT_TO_POINTER
163                                 ((gint)CR(color.red, color.green, color.blue)));
164
165         return widget;
166 }
167
168 /* XXX: this function to check if menus with colors and labels should
169  * be recreated */
170 gboolean colorlabel_changed(void)
171 {
172         gint n;
173
174         for (n = 0; n < LABEL_COLORS_ELEMS; n++) {
175                 if (label_colors[n].changed) 
176                         return TRUE;
177         }
178
179         return FALSE;
180 }
181
182 /* XXX: colorlabel_recreate_XXX are there to make sure everything
183  * is initialized ok, without having to call a global _xxx_init_
184  * function */
185 static void colorlabel_recreate_color(gint color)
186 {
187         GtkWidget *widget;
188
189         if (!(label_colors[color].changed & LCCF_COLOR))
190                 return;
191
192         widget = colorlabel_create_color_widget(label_colors[color].color);
193         g_return_if_fail(widget);
194
195         if (label_colors[color].widget) 
196                 gtk_widget_destroy(label_colors[color].widget);
197
198         label_colors[color].widget = widget;            
199         label_colors[color].changed &= ~LCCF_COLOR;
200 }
201
202 static void colorlabel_recreate_label(gint color)
203 {
204         if (!label_colors[color].changed & LCCF_LABEL)
205                 return;
206
207         if (label_colors[color].label == NULL) 
208                 label_colors[color].label = g_strdup(gettext(labels[color]));
209
210         label_colors[color].changed &= ~LCCF_LABEL;
211 }
212
213 /* XXX: call this function everytime when you're doing important
214  * stuff with the label_colors[] array */
215 static void colorlabel_recreate(gint color)
216 {
217         colorlabel_recreate_label(color);
218         colorlabel_recreate_color(color);
219 }
220
221 static void colorlabel_recreate_all(void)
222 {
223         gint n;
224
225         for ( n = 0; n < LABEL_COLORS_ELEMS; n++) 
226                 colorlabel_recreate(n);
227 }
228
229 /* colorlabel_create_check_color_menu_item() - creates a color
230  * menu item with a check box */
231 GtkWidget *colorlabel_create_check_color_menu_item(gint color_index)
232 {
233         GtkWidget *label; 
234         GtkWidget *hbox; 
235         GtkWidget *align; 
236         GtkWidget *item;
237
238         G_RETURN_VAL_IF_INVALID_COLOR(color_index, NULL);
239
240         item = gtk_check_menu_item_new();
241
242         colorlabel_recreate(color_index);
243
244         /* XXX: gnome-core::panel::menu.c is a great example of
245          * how to create pixmap menus */
246         label = gtk_label_new(label_colors[color_index].label);
247
248         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
249         gtk_widget_show(label);
250         hbox = gtk_hbox_new(FALSE, 0);
251         gtk_widget_show(hbox);
252         gtk_container_add(GTK_CONTAINER(item), hbox);
253
254         align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
255         gtk_widget_show(align);
256         gtk_container_set_border_width(GTK_CONTAINER(align), 1);
257
258         gtk_container_add(GTK_CONTAINER(align), label_colors[color_index].widget);
259         gtk_widget_show(label_colors[color_index].widget);
260         gtk_widget_set_size_request(align, LABEL_COLOR_WIDTH, 
261                                     LABEL_COLOR_HEIGHT);
262
263         gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
264         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
265
266         return item;
267 }
268
269 /* colorlabel_create_color_menu() - creates a color menu without 
270  * checkitems, probably for use in combo items */
271 GtkWidget *colorlabel_create_color_menu(void)
272 {
273         GtkWidget *label; 
274         GtkWidget *hbox; 
275         GtkWidget *align; 
276         GtkWidget *item;
277         GtkWidget *menu;
278         gint i;
279
280         colorlabel_recreate_all();
281
282         /* create the menu items. each item has its color code attached */
283         menu = gtk_menu_new();
284         g_object_set_data(G_OBJECT(menu), "label_color_menu", menu);
285
286         item = gtk_menu_item_new_with_label(_("None"));
287         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
288         g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(0));
289         gtk_widget_show(item);
290
291         /* and the color items */
292         for (i = 0; i < LABEL_COLORS_ELEMS; i++) {
293                 GtkWidget *widget = colorlabel_create_color_widget(label_colors[i].color);
294
295                 item  = gtk_menu_item_new();
296                 g_object_set_data(G_OBJECT(item), "color", GUINT_TO_POINTER(i + 1));
297
298                 label = gtk_label_new(label_colors[i].label);
299                 
300                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
301                 gtk_widget_show(label);
302                 hbox = gtk_hbox_new(FALSE, 0);
303                 gtk_widget_show(hbox);
304                 gtk_container_add(GTK_CONTAINER(item), hbox);
305
306                 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
307                 gtk_widget_show(align);
308                 gtk_container_set_border_width(GTK_CONTAINER(align), 1);
309
310                 gtk_container_add(GTK_CONTAINER(align), widget);
311                 gtk_widget_show(widget);
312                 gtk_widget_set_size_request(align, LABEL_COLOR_WIDTH, 
313                                             LABEL_COLOR_HEIGHT);
314
315                 gtk_box_pack_start(GTK_BOX(hbox), align, FALSE, FALSE, 0);
316                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 4);
317                 
318                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
319                 gtk_widget_show(item);
320         }
321
322         gtk_widget_show(menu);
323
324         return menu;
325 }
326
327 guint colorlabel_get_color_menu_active_item(GtkWidget *menu)
328 {
329         GtkWidget *menuitem;
330         guint color;
331
332         menuitem = gtk_menu_get_active(GTK_MENU(menu));
333         color = GPOINTER_TO_UINT
334                 (g_object_get_data(G_OBJECT(menuitem), "color"));
335         return color;
336 }