9d9343b1d3b7e60ceda5d5e368d07a51bad17295
[claws.git] / src / prefs_msg_colors.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33
34 #include "prefs_common.h"
35 #include "prefs_gtk.h"
36
37 #include "gtk/gtkutils.h"
38 #include "gtk/prefswindow.h"
39
40 #include "manage_window.h"
41 #include "mainwindow.h"
42 #include "colorlabel.h"
43
44 #define SAFE_STRING(str) \
45         (str) ? (str) : ""
46
47 static struct MessageColorButtons {
48         GtkWidget *btn_quote_level1;
49         GtkWidget *btn_quote_level2;
50         GtkWidget *btn_quote_level3;
51         GtkWidget *btn_quote_level1_bg;
52         GtkWidget *btn_quote_level2_bg;
53         GtkWidget *btn_quote_level3_bg;
54         GtkWidget *btn_uri;
55         GtkWidget *btn_tgt_folder;
56         GtkWidget *btn_signature;
57         GtkWidget *btn_color_new;
58         /* custom colors */
59         GtkWidget *custom_color[COLORLABELS];
60 } color_buttons;
61
62 typedef struct _MsgColorsPage
63 {
64         PrefsPage page;
65
66         GtkWidget *window;
67         
68         GtkWidget *chkbtn_enable_colors;
69         GtkWidget *chkbtn_enable_bgcolors;
70         GtkWidget *chkbtn_recycle_colors;
71
72         /* custom colors */
73         GtkWidget *entry_custom_colorlabel[COLORLABELS];
74 } MsgColorsPage;
75
76 static GtkWidget *color_dialog;
77
78 static void quote_color_set_dialog              (GtkWidget      *widget,
79                                                  gpointer        data);
80 static void quote_colors_set_dialog_ok          (GtkWidget      *widget,
81                                                  gpointer        data);
82 static void quote_colors_set_dialog_cancel      (GtkWidget      *widget,
83                                                  gpointer        data);
84 static gboolean quote_colors_set_dialog_key_pressed     (GtkWidget      *widget,
85                                                  GdkEventKey    *event,
86                                                  gpointer        data);
87 static void set_button_bg_color                 (GtkWidget      *widget,
88                                                  gint            color);
89 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget,
90                                                  gpointer        data);
91
92 void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window, 
93                                     gpointer data)
94 {
95         MsgColorsPage *prefs_msg_colors = (MsgColorsPage *) _page;
96         
97         GtkWidget *vbox1;
98         GtkWidget *vbox2;
99         GtkWidget *chkbtn_enable_colors;
100         GtkWidget *label_quote_level1;
101         GtkWidget *label_quote_level2;
102         GtkWidget *label_quote_level3;
103         GtkWidget *label_quote_color1;
104         GtkWidget *label_quote_color2;
105         GtkWidget *label_quote_color3;
106         GtkWidget *chkbtn_enable_bgcolors;
107         GtkWidget *label_quote_bgcolor1;
108         GtkWidget *label_quote_bgcolor2;
109         GtkWidget *label_quote_bgcolor3;
110         GtkWidget *lable_uri;
111         GtkWidget *label_signature;
112         GtkWidget *label_tgt_folder;
113         GtkWidget *chkbtn_recycle_colors;
114         GtkWidget *hbox;
115         GtkWidget *label_color_new;
116         GtkWidget *frame_msg;
117         GtkWidget *frame_folder;
118         GtkWidget *frame_quote;
119         GtkWidget *vbox3;
120         GtkWidget *hbox_quote;
121         GtkWidget *vbox_quotefg;
122         GtkWidget *vbox_quotebg;
123         /* custom colors */
124         GtkWidget *vbox_color_labels;
125         GtkWidget *hbox_custom_colors;
126         GtkWidget *vbox_custom_colors;
127         GtkWidget *vbox_custom_colors1;
128         GtkWidget *vbox_custom_colors2;
129         GtkWidget *frame_custom_colors;
130         GtkWidget *hbox_reset_custom_colors;
131         GtkWidget *btn_reset_custom_colors;
132         GtkWidget *hbox_custom_color[COLORLABELS];
133         GtkWidget *entry_custom_colorlabel[COLORLABELS];
134         gint c;
135         GtkTooltips *tooltips;
136         gchar *tooltip_btn_text = NULL;
137         gchar *tooltip_entry_text = NULL;
138
139         vbox1 = gtk_vbox_new (FALSE, VSPACING_NARROW);
140         gtk_widget_show (vbox1);
141         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
142
143         PACK_FRAME(vbox1, frame_msg, _("Message view"));
144
145         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
146         gtk_widget_show (vbox2);
147         gtk_container_add (GTK_CONTAINER (frame_msg), vbox2);
148         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
149
150
151         hbox = gtk_hbox_new(FALSE, 8);
152         gtk_widget_show (hbox);
153         hbox_quote = gtk_hbox_new(FALSE, 8);
154         gtk_widget_show (hbox_quote);
155         vbox_quotefg = gtk_vbox_new(FALSE, 8);
156         gtk_widget_show (vbox_quotefg);
157         vbox_quotebg = gtk_vbox_new(FALSE, 8);
158 #if GTK_CHECK_VERSION(2, 8, 0)
159         gtk_widget_show (vbox_quotebg);
160 #endif  
161         
162         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
163         PACK_CHECK_BUTTON (hbox, chkbtn_enable_colors,
164                            _("Enable coloration of message text"));
165
166         PACK_FRAME(vbox2, frame_quote, _("Quote"));
167
168         vbox3 = gtk_vbox_new (FALSE, VSPACING_NARROW);
169         gtk_widget_show (vbox3);
170         gtk_container_add (GTK_CONTAINER (frame_quote), vbox3);
171         gtk_container_set_border_width (GTK_CONTAINER (vbox3), VBOX_BORDER);
172
173         gtk_box_pack_start (GTK_BOX (vbox3), hbox_quote, FALSE, TRUE, 0);
174         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotefg, FALSE, TRUE, 0);
175         gtk_box_pack_start (GTK_BOX (hbox_quote), vbox_quotebg, FALSE, TRUE, 0);
176
177         hbox = gtk_hbox_new(FALSE, 8);
178         gtk_widget_show (hbox);
179         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
180
181         PACK_CHECK_BUTTON (hbox, chkbtn_recycle_colors,
182                            _("Cycle quote colors"));
183         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, chkbtn_recycle_colors);
184         tooltips = gtk_tooltips_new();
185         gtk_tooltips_set_tip(tooltips, chkbtn_recycle_colors,
186                              _("If there are more than 3 quote levels, the colors will be reused"),
187                              NULL);
188
189         hbox = gtk_hbox_new(FALSE, 8);
190         gtk_widget_show (hbox);
191         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
192
193         label_quote_level1 = gtk_label_new (_("1st Level"));
194         gtk_widget_show(label_quote_level1);
195         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level1, 
196                             FALSE, FALSE, 0);
197         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_level1);
198
199         label_quote_color1 = gtk_label_new (_("Text"));
200         gtk_widget_show(label_quote_color1);
201         gtk_box_pack_end (GTK_BOX(hbox), label_quote_color1, 
202                             FALSE, FALSE, 0);
203         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_color1);
204                 
205         color_buttons.btn_quote_level1 = gtk_button_new();
206         gtk_widget_show(color_buttons.btn_quote_level1);
207         gtk_widget_set_size_request (color_buttons.btn_quote_level1, 30, 20);
208         gtk_box_pack_end (GTK_BOX(hbox), color_buttons.btn_quote_level1, 
209                             FALSE, FALSE, 0);
210         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, color_buttons.btn_quote_level1);
211         tooltips = gtk_tooltips_new();
212         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level1,
213                              _("Pick color for 1st level text"), NULL);
214
215         hbox = gtk_hbox_new(FALSE, 8);
216         gtk_widget_show (hbox);
217         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
218
219         label_quote_level2 = gtk_label_new (_("2nd Level"));
220         gtk_widget_show(label_quote_level2);
221         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level2, 
222                             FALSE, FALSE, 0);
223         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_level2);
224
225         label_quote_color2 = gtk_label_new (_("Text"));
226         gtk_widget_show(label_quote_color2);
227         gtk_box_pack_end (GTK_BOX(hbox), label_quote_color2, 
228                             FALSE, FALSE, 0);
229         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_color2);
230
231         color_buttons.btn_quote_level2 = gtk_button_new();
232         gtk_widget_show(color_buttons.btn_quote_level2);
233         gtk_widget_set_size_request (color_buttons.btn_quote_level2, 30, 20);
234         gtk_box_pack_end (GTK_BOX(hbox), color_buttons.btn_quote_level2, 
235                             FALSE, FALSE, 0);
236         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, color_buttons.btn_quote_level2);
237         tooltips = gtk_tooltips_new();
238         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level2,
239                              _("Pick color for 2nd level text"), NULL);
240
241         hbox = gtk_hbox_new(FALSE, 8);
242         gtk_widget_show (hbox);
243         gtk_box_pack_start (GTK_BOX (vbox_quotefg), hbox, FALSE, TRUE, 0);
244
245         label_quote_level3 = gtk_label_new (_("3rd Level"));
246         gtk_widget_show(label_quote_level3);
247         gtk_box_pack_start (GTK_BOX(hbox), label_quote_level3, 
248                             FALSE, FALSE, 0);
249         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_level3);
250
251         label_quote_color3 = gtk_label_new (_("Text"));
252         gtk_widget_show(label_quote_color3);
253         gtk_box_pack_end (GTK_BOX(hbox), label_quote_color3, 
254                             FALSE, FALSE, 0);
255         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_quote_color3);
256
257         color_buttons.btn_quote_level3 = gtk_button_new();
258         gtk_widget_show(color_buttons.btn_quote_level3);
259         gtk_widget_set_size_request (color_buttons.btn_quote_level3, 30, 20);
260         gtk_box_pack_end (GTK_BOX(hbox), color_buttons.btn_quote_level3, 
261                             FALSE, FALSE, 0);
262         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, color_buttons.btn_quote_level3);
263         tooltips = gtk_tooltips_new();
264         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level3,
265                              _("Pick color for 3rd level text"), NULL);
266
267         hbox = gtk_hbox_new(FALSE, 8);
268         gtk_widget_show (hbox);
269         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
270
271         PACK_CHECK_BUTTON (hbox, chkbtn_enable_bgcolors,
272                            _("Enable coloration of text background"));
273         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, chkbtn_enable_bgcolors);
274
275         hbox = gtk_hbox_new(FALSE, 8);
276         gtk_widget_show (hbox);
277         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, hbox);
278         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
279         
280         color_buttons.btn_quote_level1_bg = gtk_button_new();
281         gtk_widget_show(color_buttons.btn_quote_level1_bg);
282         gtk_widget_set_size_request (color_buttons.btn_quote_level1_bg, 30, 20);
283         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_quote_level1_bg, 
284                             FALSE, FALSE, 0);
285         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, color_buttons.btn_quote_level1_bg);
286         tooltips = gtk_tooltips_new();
287         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level1_bg,
288                              _("Pick color for 1st level text background"), NULL);
289
290         label_quote_bgcolor1 = gtk_label_new (_("Background"));
291         gtk_widget_show(label_quote_bgcolor1);
292         gtk_box_pack_start (GTK_BOX(hbox), label_quote_bgcolor1, 
293                             FALSE, FALSE, 0);
294         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, label_quote_bgcolor1);
295
296         hbox = gtk_hbox_new(FALSE, 8);
297         gtk_widget_show (hbox);
298         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, hbox);
299         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
300         
301         color_buttons.btn_quote_level2_bg = gtk_button_new();
302         gtk_widget_show(color_buttons.btn_quote_level2_bg);
303         gtk_widget_set_size_request (color_buttons.btn_quote_level2_bg, 30, 20);
304         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_quote_level2_bg, 
305                             FALSE, FALSE, 0);
306         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, color_buttons.btn_quote_level2_bg);
307         tooltips = gtk_tooltips_new();
308         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level2_bg,
309                              _("Pick color for 2nd level text background"), NULL);
310
311         label_quote_bgcolor2 = gtk_label_new (_("Background"));
312         gtk_widget_show(label_quote_bgcolor2);
313         gtk_box_pack_start (GTK_BOX(hbox), label_quote_bgcolor2, 
314                             FALSE, FALSE, 0);
315         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, label_quote_bgcolor2);
316
317         hbox = gtk_hbox_new(FALSE, 8);
318         gtk_widget_show (hbox);
319         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, hbox);
320         gtk_box_pack_start (GTK_BOX (vbox_quotebg), hbox, FALSE, TRUE, 0);
321         
322         color_buttons.btn_quote_level3_bg = gtk_button_new();
323         gtk_widget_show(color_buttons.btn_quote_level3_bg);
324         gtk_widget_set_size_request (color_buttons.btn_quote_level3_bg, 30, 20);
325         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_quote_level3_bg, 
326                             FALSE, FALSE, 0);
327         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, color_buttons.btn_quote_level3_bg);
328         tooltips = gtk_tooltips_new();
329         gtk_tooltips_set_tip(tooltips, color_buttons.btn_quote_level3_bg,
330                              _("Pick color for 3rd level text background"), NULL);
331
332         label_quote_bgcolor3 = gtk_label_new (_("Background"));
333         gtk_widget_show(label_quote_bgcolor3);
334         gtk_box_pack_start (GTK_BOX(hbox), label_quote_bgcolor3, 
335                             FALSE, FALSE, 0);
336         SET_TOGGLE_SENSITIVITY(chkbtn_enable_bgcolors, label_quote_bgcolor3);
337
338         hbox = gtk_hbox_new(FALSE, 8);
339         gtk_widget_show (hbox);
340         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
341
342         color_buttons.btn_uri = gtk_button_new();
343         gtk_widget_show(color_buttons.btn_uri);
344         gtk_widget_set_size_request (color_buttons.btn_uri, 30, 20);
345         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_uri, 
346                             FALSE, FALSE, 0);
347         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, color_buttons.btn_uri);
348         tooltips = gtk_tooltips_new();
349         gtk_tooltips_set_tip(tooltips, color_buttons.btn_uri,
350                              _("Pick color for links"), NULL);
351
352         lable_uri = gtk_label_new (_("URI link"));
353         gtk_widget_show(lable_uri);
354         gtk_box_pack_start (GTK_BOX(hbox), lable_uri, FALSE, FALSE, 0);
355         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, lable_uri);
356
357         hbox = gtk_hbox_new(FALSE, 8);
358         gtk_widget_show (hbox);
359         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
360
361         color_buttons.btn_signature = gtk_button_new();
362         gtk_widget_show(color_buttons.btn_signature);
363         gtk_widget_set_size_request (color_buttons.btn_signature, 30, 20);
364         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_signature, 
365                             FALSE, FALSE, 0);
366         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, color_buttons.btn_signature);
367         tooltips = gtk_tooltips_new();
368         gtk_tooltips_set_tip(tooltips, color_buttons.btn_signature,
369                              _("Pick color for signatures"), NULL);
370
371         label_signature = gtk_label_new (_("Signatures"));
372         gtk_widget_show(label_signature);
373         gtk_box_pack_start (GTK_BOX(hbox), label_signature, FALSE, FALSE, 0);
374         SET_TOGGLE_SENSITIVITY(chkbtn_enable_colors, label_signature);
375
376         PACK_FRAME(vbox1, frame_folder, _("Folder list"));
377
378         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
379         gtk_widget_show (vbox2);
380         gtk_container_add (GTK_CONTAINER (frame_folder), vbox2);
381         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
382
383         hbox = gtk_hbox_new(FALSE, 8);
384         gtk_widget_show (hbox);
385         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
386
387         color_buttons.btn_tgt_folder = gtk_button_new();
388         gtk_widget_show(color_buttons.btn_tgt_folder);
389         gtk_widget_set_size_request (color_buttons.btn_tgt_folder, 30, 20);
390         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_tgt_folder, 
391                             FALSE, FALSE, 0);
392         tooltips = gtk_tooltips_new();
393         gtk_tooltips_set_tip(tooltips, color_buttons.btn_tgt_folder,
394                              _("Pick color for Target folder. "
395                                "Target folder is used when the option 'Execute immediately "
396                                "when moving or deleting messages' is turned off"), NULL);
397
398         label_tgt_folder = gtk_label_new (_("Target folder"));
399         gtk_widget_show(label_tgt_folder);
400         gtk_box_pack_start (GTK_BOX(hbox), label_tgt_folder, FALSE, FALSE, 0);
401
402         hbox = gtk_hbox_new(FALSE, 8);
403         gtk_widget_show (hbox);
404         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
405
406         color_buttons.btn_color_new = gtk_button_new();
407         gtk_widget_show (color_buttons.btn_color_new);
408         gtk_widget_set_size_request (color_buttons.btn_color_new, 30, 20);
409         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.btn_color_new,
410                             FALSE, FALSE, 0);
411         tooltips = gtk_tooltips_new();
412         gtk_tooltips_set_tip(tooltips, color_buttons.btn_color_new,
413                              _("Pick color for folders containing new messages"), NULL);
414
415         label_color_new = gtk_label_new (_("Folder containing new messages"));
416         gtk_widget_show(label_color_new);
417         gtk_box_pack_start (GTK_BOX(hbox), label_color_new, FALSE, FALSE, 0);
418
419         /* custom colors */
420         PACK_FRAME (vbox1, frame_custom_colors, _("Color labels"));
421
422         vbox_color_labels = gtk_vbox_new (FALSE, VSPACING_NARROW);
423         gtk_widget_show (vbox_color_labels);
424         gtk_container_add (GTK_CONTAINER (frame_custom_colors), vbox_color_labels);
425         gtk_container_set_border_width (GTK_CONTAINER (vbox_color_labels), VBOX_BORDER);
426
427         vbox_custom_colors = gtk_vbox_new (FALSE, VSPACING_NARROW);
428         gtk_widget_show (vbox_custom_colors);
429         gtk_box_pack_start(GTK_BOX(vbox_color_labels), vbox_custom_colors,
430                 FALSE, FALSE, 0);
431
432         hbox_custom_colors = gtk_hbox_new(FALSE, 8);
433         gtk_widget_show(hbox_custom_colors);
434         gtk_box_pack_start(GTK_BOX (vbox_custom_colors), hbox_custom_colors,
435                                    FALSE, TRUE, 0);
436
437         vbox_custom_colors1 = gtk_vbox_new (FALSE, VSPACING_NARROW);
438         gtk_widget_show (vbox_custom_colors1);
439         gtk_box_pack_start (GTK_BOX (hbox_custom_colors), vbox_custom_colors1, FALSE, FALSE, 0);
440
441         vbox_custom_colors2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
442         gtk_widget_show (vbox_custom_colors2);
443         gtk_box_pack_start (GTK_BOX (hbox_custom_colors), vbox_custom_colors2, FALSE, FALSE, 0);
444
445         for (c = 0; c < 4; c++) {
446                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
447                    rule name and should not be translated */
448                 tooltip_btn_text = g_strdup_printf(_("Pick color for 'color %d'"), c+1);
449
450                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
451                    rule name and should not be translated */
452                 tooltip_entry_text = g_strdup_printf(_("Set label for 'color %d'"), c+1);
453
454                 hbox_custom_color[c] = gtk_hbox_new(FALSE, 8);
455                 gtk_widget_show(hbox_custom_color[c]);
456                 gtk_box_pack_start(GTK_BOX (vbox_custom_colors1), hbox_custom_color[c],
457                                    FALSE, TRUE, 0);
458
459                 color_buttons.custom_color[c] = gtk_button_new();
460                 gtk_widget_show(color_buttons.custom_color[c]);
461                 gtk_widget_set_size_request(color_buttons.custom_color[c], 30, 20);
462                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), color_buttons.custom_color[c],
463                                    FALSE, FALSE, 0);
464                 tooltips = gtk_tooltips_new();
465                 gtk_tooltips_set_tip(tooltips, color_buttons.custom_color[c],
466                                      tooltip_btn_text, NULL);
467
468                 entry_custom_colorlabel[c] = gtk_entry_new();
469                 gtk_widget_show (entry_custom_colorlabel[c]);
470                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), entry_custom_colorlabel[c],
471                                    FALSE, FALSE, 0);
472                 tooltips = gtk_tooltips_new();
473                 gtk_tooltips_set_tip(tooltips, entry_custom_colorlabel[c],
474                                      tooltip_entry_text, NULL);
475         }
476
477         for (c = 4; c < COLORLABELS; c++) {
478                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
479                    rule name and should not be translated */
480                 tooltip_btn_text = g_strdup_printf(_("Pick color for 'color %d'"), c+1);
481
482                 /* TRANSLATORS: 'color %d' refers to the filtering/processing 
483                    rule name and should not be translated */
484                 tooltip_entry_text = g_strdup_printf(_("Set label for 'color %d'"), c+1);
485
486                 hbox_custom_color[c] = gtk_hbox_new(FALSE, 8);
487                 gtk_widget_show(hbox_custom_color[c]);
488                 gtk_box_pack_start(GTK_BOX (vbox_custom_colors2), hbox_custom_color[c],
489                                    FALSE, TRUE, 0);
490
491                 color_buttons.custom_color[c] = gtk_button_new();
492                 gtk_widget_show(color_buttons.custom_color[c]);
493                 gtk_widget_set_size_request(color_buttons.custom_color[c], 30, 20);
494                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), color_buttons.custom_color[c],
495                                    FALSE, FALSE, 0);
496                 tooltips = gtk_tooltips_new();
497                 gtk_tooltips_set_tip(tooltips, color_buttons.custom_color[c],
498                                      tooltip_btn_text, NULL);
499
500                 entry_custom_colorlabel[c] = gtk_entry_new();
501                 gtk_widget_show (entry_custom_colorlabel[c]);
502                 gtk_box_pack_start(GTK_BOX (hbox_custom_color[c]), entry_custom_colorlabel[c],
503                                    FALSE, FALSE, 0);
504                 tooltips = gtk_tooltips_new();
505                 gtk_tooltips_set_tip(tooltips, entry_custom_colorlabel[c],
506                                      tooltip_entry_text, NULL);
507         }
508
509         g_free(tooltip_btn_text);
510         g_free(tooltip_entry_text);
511
512         hbox_reset_custom_colors = gtk_hbox_new(FALSE, 8);
513         gtk_widget_show (hbox_reset_custom_colors);
514         gtk_box_pack_start(GTK_BOX (vbox_color_labels), hbox_reset_custom_colors,
515                            FALSE, FALSE, 0);
516
517         btn_reset_custom_colors = gtk_button_new_with_label(_(" Use default "));
518         gtk_widget_show(btn_reset_custom_colors);
519         gtk_box_pack_start(GTK_BOX(hbox_reset_custom_colors), btn_reset_custom_colors,
520                 FALSE, FALSE, 0);
521
522         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level1), "clicked",
523                          G_CALLBACK(quote_color_set_dialog), "LEVEL1");
524         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level2), "clicked",
525                          G_CALLBACK(quote_color_set_dialog), "LEVEL2");
526         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level3), "clicked",
527                          G_CALLBACK(quote_color_set_dialog), "LEVEL3");
528         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level1_bg), "clicked",
529                          G_CALLBACK(quote_color_set_dialog), "LEVEL1BG");
530         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level2_bg), "clicked",
531                          G_CALLBACK(quote_color_set_dialog), "LEVEL2BG");
532         g_signal_connect(G_OBJECT(color_buttons.btn_quote_level3_bg), "clicked",
533                          G_CALLBACK(quote_color_set_dialog), "LEVEL3BG");
534         g_signal_connect(G_OBJECT(color_buttons.btn_uri), "clicked",
535                          G_CALLBACK(quote_color_set_dialog), "URI");
536         g_signal_connect(G_OBJECT(color_buttons.btn_tgt_folder), "clicked",
537                          G_CALLBACK(quote_color_set_dialog), "TGTFLD");
538         g_signal_connect(G_OBJECT(color_buttons.btn_signature), "clicked",
539                          G_CALLBACK(quote_color_set_dialog), "SIGNATURE");
540         g_signal_connect(G_OBJECT(color_buttons.btn_color_new), "clicked",
541                          G_CALLBACK(quote_color_set_dialog), "NEW");
542         /* custom colors */
543         for (c = 0; c < COLORLABELS; c++) {
544                 g_signal_connect(G_OBJECT(color_buttons.custom_color[c]), "clicked",
545                                  G_CALLBACK(quote_color_set_dialog), GINT_TO_POINTER(c));
546         }
547
548         g_signal_connect(G_OBJECT(btn_reset_custom_colors), "clicked",
549                          G_CALLBACK(prefs_msg_colors_reset_custom_colors), prefs_msg_colors);
550
551         set_button_bg_color(color_buttons.btn_quote_level1,
552                             prefs_common.quote_level1_col);
553         set_button_bg_color(color_buttons.btn_quote_level2,
554                             prefs_common.quote_level2_col);
555         set_button_bg_color(color_buttons.btn_quote_level3,
556                             prefs_common.quote_level3_col);
557         set_button_bg_color(color_buttons.btn_quote_level1_bg,
558                             prefs_common.quote_level1_bgcol);
559         set_button_bg_color(color_buttons.btn_quote_level2_bg,
560                             prefs_common.quote_level2_bgcol);
561         set_button_bg_color(color_buttons.btn_quote_level3_bg,
562                             prefs_common.quote_level3_bgcol);
563         set_button_bg_color(color_buttons.btn_uri,
564                             prefs_common.uri_col);
565         set_button_bg_color(color_buttons.btn_tgt_folder,
566                             prefs_common.tgt_folder_col);
567         set_button_bg_color(color_buttons.btn_signature,
568                             prefs_common.signature_col);
569         set_button_bg_color(color_buttons.btn_color_new,
570                             prefs_common.color_new);
571         /* custom colors */
572         for (c = 0; c < COLORLABELS; c++) {
573                 set_button_bg_color(color_buttons.custom_color[c],
574                                     prefs_common.custom_colorlabel[c].color);
575                 gtk_entry_set_text(GTK_ENTRY (entry_custom_colorlabel[c]), 
576                                    gettext(SAFE_STRING (prefs_common.custom_colorlabel[c].label)));
577         }
578
579         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enable_colors),
580                                      prefs_common.enable_color);
581         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enable_bgcolors),
582                                      prefs_common.enable_bgcolor);
583         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_recycle_colors),
584                                      prefs_common.recycle_quote_colors);
585
586         prefs_msg_colors->chkbtn_enable_colors  = chkbtn_enable_colors;
587         prefs_msg_colors->chkbtn_enable_bgcolors        = chkbtn_enable_bgcolors;
588         prefs_msg_colors->chkbtn_recycle_colors = chkbtn_recycle_colors;
589         /* custom colors */
590         for (c = 0; c < COLORLABELS; c++) {
591                 prefs_msg_colors->entry_custom_colorlabel[c] = entry_custom_colorlabel[c];
592         }
593
594         prefs_msg_colors->page.widget = vbox1;
595 }
596
597 static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
598 {
599         gchar *type = (gchar *)data;
600         gchar *title = NULL;
601         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
602         gint rgbvalue = 0;
603         GtkColorSelectionDialog *dialog;
604         gint c;
605
606         /* custom colors */
607         for (c = 0; c < COLORLABELS; c++) {
608                 if (GPOINTER_TO_INT(type) == c) {
609                         /* TRANSLATORS: 'color %d' refers to the filtering/processing 
610                            rule name and should not be translated */
611                         title = g_strdup_printf(_("Pick color for 'color %d'"), c+1);
612                         rgbvalue = prefs_common.custom_colorlabel[c].color;
613                         break;
614                 }
615         }
616         /* other colors */
617         if (c == COLORLABELS) {
618                 if(g_ascii_strcasecmp(type, "LEVEL1") == 0) {
619                         title = g_strdup(_("Pick color for quotation level 1"));
620                         rgbvalue = prefs_common.quote_level1_col;
621                 } else if(g_ascii_strcasecmp(type, "LEVEL2") == 0) {
622                         title = g_strdup(_("Pick color for quotation level 2"));
623                         rgbvalue = prefs_common.quote_level2_col;
624                 } else if(g_ascii_strcasecmp(type, "LEVEL3") == 0) {
625                         title = g_strdup(_("Pick color for quotation level 3"));
626                         rgbvalue = prefs_common.quote_level3_col;
627                 } else if(g_ascii_strcasecmp(type, "LEVEL1BG") == 0) {
628                         title = g_strdup(_("Pick color for quotation level 1 background"));
629                         rgbvalue = prefs_common.quote_level1_bgcol;
630                 } else if(g_ascii_strcasecmp(type, "LEVEL2BG") == 0) {
631                         title = g_strdup(_("Pick color for quotation level 2 background"));
632                         rgbvalue = prefs_common.quote_level2_bgcol;
633                 } else if(g_ascii_strcasecmp(type, "LEVEL3BG") == 0) {
634                         title = g_strdup(_("Pick color for quotation level 3 background"));
635                         rgbvalue = prefs_common.quote_level3_bgcol;
636                 } else if(g_ascii_strcasecmp(type, "URI") == 0) {
637                         title = g_strdup(_("Pick color for URI"));
638                         rgbvalue = prefs_common.uri_col;
639                 } else if(g_ascii_strcasecmp(type, "TGTFLD") == 0) {
640                         title = g_strdup(_("Pick color for target folder"));
641                         rgbvalue = prefs_common.tgt_folder_col;
642                 } else if(g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
643                         title = g_strdup(_("Pick color for signatures"));
644                         rgbvalue = prefs_common.signature_col;
645                 } else if(g_ascii_strcasecmp(type, "NEW") == 0) {
646                         title = g_strdup(_("Pick color for folder"));
647                         rgbvalue = prefs_common.color_new;
648                 } else {
649                         /* Should never be called */
650                         g_warning("Unrecognized datatype '%s' in quote_color_set_dialog\n", type);
651                         return;
652                 }
653         }
654
655         color_dialog = gtk_color_selection_dialog_new(title);
656         g_free(title);
657         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
658         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
659         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
660         manage_window_set_transient(GTK_WINDOW(color_dialog));
661
662         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
663                          "clicked", G_CALLBACK(quote_colors_set_dialog_cancel), data);
664         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
665                          "clicked", G_CALLBACK(quote_colors_set_dialog_ok), data);
666         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
667                          G_CALLBACK(quote_colors_set_dialog_key_pressed),data);
668
669         /* preselect the previous color in the color selection dialog */
670         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
671         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
672         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
673         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
674         gtk_color_selection_set_color
675                 (GTK_COLOR_SELECTION(dialog->colorsel), color);
676
677         gtk_widget_show(color_dialog);
678 }
679
680 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
681 {
682         GtkColorSelection *colorsel = (GtkColorSelection *)
683                                                 ((GtkColorSelectionDialog *)color_dialog)->colorsel;
684         gdouble color[4];
685         gint red, green, blue, rgbvalue;
686         gchar *type = (gchar *)data;
687         gint c;
688
689         gtk_color_selection_get_color(colorsel, color);
690
691         red      = (gint) (color[0] * 255.0);
692         green    = (gint) (color[1] * 255.0);
693         blue     = (gint) (color[2] * 255.0);
694         rgbvalue = (gint) ((red * 0x10000) | (green * 0x100) | blue);
695
696
697         /* custom colors */
698         for (c = 0; c < COLORLABELS; c++) {
699                 if (GPOINTER_TO_INT(type) == c) {
700                         prefs_common.custom_colorlabel[c].color = rgbvalue;
701                         set_button_bg_color(color_buttons.custom_color[c], rgbvalue);
702                         break;
703                 }
704         }
705         /* other colors */
706         if (c == COLORLABELS) {
707                 if (g_ascii_strcasecmp(type, "LEVEL1") == 0) {
708                         prefs_common.quote_level1_col = rgbvalue;
709                         set_button_bg_color(color_buttons.btn_quote_level1, rgbvalue);
710                 } else if (g_ascii_strcasecmp(type, "LEVEL2") == 0) {
711                         prefs_common.quote_level2_col = rgbvalue;
712                         set_button_bg_color(color_buttons.btn_quote_level2, rgbvalue);
713                 } else if (g_ascii_strcasecmp(type, "LEVEL3") == 0) {
714                         prefs_common.quote_level3_col = rgbvalue;
715                         set_button_bg_color(color_buttons.btn_quote_level3, rgbvalue);
716                 } else if (g_ascii_strcasecmp(type, "LEVEL1BG") == 0) {
717                         prefs_common.quote_level1_bgcol = rgbvalue;
718                         set_button_bg_color(color_buttons.btn_quote_level1_bg, rgbvalue);
719                 } else if (g_ascii_strcasecmp(type, "LEVEL2BG") == 0) {
720                         prefs_common.quote_level2_bgcol = rgbvalue;
721                         set_button_bg_color(color_buttons.btn_quote_level2_bg, rgbvalue);
722                 } else if (g_ascii_strcasecmp(type, "LEVEL3BG") == 0) {
723                         prefs_common.quote_level3_bgcol = rgbvalue;
724                         set_button_bg_color(color_buttons.btn_quote_level3_bg, rgbvalue);
725                 } else if (g_ascii_strcasecmp(type, "URI") == 0) {
726                         prefs_common.uri_col = rgbvalue;
727                         set_button_bg_color(color_buttons.btn_uri, rgbvalue);
728                 } else if (g_ascii_strcasecmp(type, "TGTFLD") == 0) {
729                         prefs_common.tgt_folder_col = rgbvalue;
730                         set_button_bg_color(color_buttons.btn_tgt_folder, rgbvalue);
731                         folderview_set_target_folder_color(prefs_common.tgt_folder_col);
732                 } else if (g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
733                         prefs_common.signature_col = rgbvalue;
734                         set_button_bg_color(color_buttons.btn_signature, rgbvalue);
735                 } else if (g_ascii_strcasecmp(type, "NEW") == 0) {
736                         prefs_common.color_new = rgbvalue;
737                         set_button_bg_color(color_buttons.btn_color_new, rgbvalue);
738                 } else {
739                         fprintf(stderr, "Unrecognized datatype '%s' in quote_color_set_dialog_ok\n", type);
740                 }
741         }
742
743         gtk_widget_destroy(color_dialog);
744 }
745
746 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
747 {
748         gtk_widget_destroy(color_dialog);
749 }
750
751 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
752                                                 GdkEventKey *event,
753                                                 gpointer data)
754 {
755         if (event) {
756                 switch (event->keyval) {
757                         case GDK_Escape:
758                                 gtk_button_clicked(GTK_BUTTON(GTK_COLOR_SELECTION_DIALOG
759                                                         (widget)->cancel_button));
760                                 return TRUE;
761                         case GDK_Return: 
762                         case GDK_KP_Enter:
763                                 /* NOTE: changing focus makes widget accept all currently 
764                                  * changed settings! */
765                                 gtk_widget_grab_focus
766                                         (GTK_COLOR_SELECTION_DIALOG
767                                                 (widget)->ok_button);
768                                 /* call ok handler */                                           
769                                 gtk_button_clicked(GTK_BUTTON
770                                         (GTK_COLOR_SELECTION_DIALOG
771                                                 (widget)->ok_button));
772                                 return TRUE;
773                         default:
774                                 break;
775                 }
776         }
777         return FALSE;
778 }
779
780 static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
781 {
782         GtkStyle *newstyle;
783         GdkColor color;
784
785         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
786         newstyle = gtk_style_copy(gtk_widget_get_default_style());
787         newstyle->bg[GTK_STATE_NORMAL]   = color;
788         newstyle->bg[GTK_STATE_PRELIGHT] = color;
789         newstyle->bg[GTK_STATE_ACTIVE]   = color;
790
791         gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
792 }
793
794 void prefs_msg_colors_save(PrefsPage *_page)
795 {
796         MsgColorsPage *page = (MsgColorsPage *) _page;
797         gint c;
798
799         prefs_common.enable_color = 
800                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enable_colors));
801         prefs_common.enable_bgcolor = 
802                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enable_bgcolors));
803         prefs_common.recycle_quote_colors =
804                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_recycle_colors));
805
806         /* custom colors */
807         for (c = 0; c < COLORLABELS; c++) {
808                 g_free(prefs_common.custom_colorlabel[c].label);
809                 prefs_common.custom_colorlabel[c].label =
810                         gtk_editable_get_chars(GTK_EDITABLE(page->entry_custom_colorlabel[c]), 0, -1);
811         }
812         colorlabel_update_colortable_from_prefs();
813
814         main_window_reflect_prefs_all();
815         main_window_reflect_prefs_custom_colors(mainwindow_get_mainwindow());
816 }
817
818 static void prefs_msg_colors_reset_custom_colors(GtkWidget *widget, gpointer data)
819 {
820 #define CL(x)           (((gulong) (x) >> (gulong) 8) & 0xFFUL) 
821 #define CR(r, g, b)     ((CL(r) << (gulong) 16) | \
822                          (CL(g) << (gulong)  8) | \
823                          (CL(b)))
824         MsgColorsPage *page = (MsgColorsPage *) data;
825         GdkColor color;
826         gint c;
827
828         for (c = 0; c < COLORLABELS; c++) {
829                 color = colorlabel_get_default_color(c);
830                 prefs_common.custom_colorlabel[c].color =
831                                                         (gint)CR(color.red, color.green, color.blue);
832                 set_button_bg_color(color_buttons.custom_color[c],
833                                                         prefs_common.custom_colorlabel[c].color);
834                 gtk_entry_set_text(GTK_ENTRY (page->entry_custom_colorlabel[c]),
835                                                         gettext(SAFE_STRING (colorlabel_get_color_default_text(c))));
836         }
837
838 #undef CR
839 #undef CL
840 }
841
842 static void prefs_msg_colors_destroy_widget(PrefsPage *_page)
843 {
844 }
845
846 MsgColorsPage *prefs_msg_colors;
847
848 void prefs_msg_colors_init(void)
849 {
850         MsgColorsPage *page;
851         static gchar *path[3];
852
853         path[0] = _("Display");
854         path[1] = _("Colors");
855         path[2] = NULL;
856
857         page = g_new0(MsgColorsPage, 1);
858         page->page.path = path;
859         page->page.create_widget = prefs_msg_colors_create_widget;
860         page->page.destroy_widget = prefs_msg_colors_destroy_widget;
861         page->page.save_page = prefs_msg_colors_save;
862         page->page.weight = 165.0;
863         prefs_gtk_register_page((PrefsPage *) page);
864         prefs_msg_colors = page;
865 }
866
867 void prefs_msg_colors_done(void)
868 {
869         prefs_gtk_unregister_page((PrefsPage *) prefs_msg_colors);
870         g_free(prefs_msg_colors);
871 }
872