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