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