cf93ef68cb6aa8c1d1653d03d6f19b222fa5b3e9
[claws.git] / src / prefs_msg_colors.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 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
42 static struct MessageColorButtons {
43         GtkWidget *quote_level1_btn;
44         GtkWidget *quote_level2_btn;
45         GtkWidget *quote_level3_btn;
46         GtkWidget *uri_btn;
47         GtkWidget *tgt_folder_btn;
48         GtkWidget *signature_btn;
49 } color_buttons;
50
51 typedef struct _MsgColorsPage
52 {
53         PrefsPage page;
54
55         GtkWidget *window;
56         
57         GtkWidget *chkbtn_enablecol;
58         GtkWidget *chkbtn_recycle_colors;
59 } MsgColorsPage;
60
61 static GtkWidget *color_dialog;
62
63 static void quote_color_set_dialog              (GtkWidget      *widget,
64                                                  gpointer        data);
65 static void quote_colors_set_dialog_ok          (GtkWidget      *widget,
66                                                  gpointer        data);
67 static void quote_colors_set_dialog_cancel      (GtkWidget      *widget,
68                                                  gpointer        data);
69 static gboolean quote_colors_set_dialog_key_pressed     (GtkWidget      *widget,
70                                                  GdkEventKey    *event,
71                                                  gpointer        data);
72 static void set_button_bg_color                 (GtkWidget      *widget,
73                                                  gint            color);
74
75 void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window, 
76                                     gpointer data)
77 {
78         MsgColorsPage *prefs_msg_colors = (MsgColorsPage *) _page;
79         
80         GtkWidget *table;
81         GtkWidget *chkbtn_enablecol;
82         GtkWidget *quotelevel1_label;
83         GtkWidget *quotelevel2_label;
84         GtkWidget *quotelevel3_label;
85         GtkWidget *uri_label;
86         GtkWidget *signature_label;
87         GtkWidget *tgt_folder_label;
88         GtkWidget *chkbtn_recycle_colors;
89         GtkWidget *hbox;
90
91         table = gtk_table_new(8, 3, FALSE);
92         gtk_widget_show(table);
93         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
94         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
95         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
96
97         chkbtn_enablecol = 
98                 gtk_check_button_new_with_label(_("Enable coloration of message"));
99         gtk_table_attach (GTK_TABLE (table), chkbtn_enablecol, 0, 1, 1, 2,
100                           (GtkAttachOptions) (GTK_FILL),
101                           (GtkAttachOptions) (0), 0, 0);
102
103         hbox = gtk_hbox_new(FALSE, 8);
104         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 2, 3,
105                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
106
107         color_buttons.quote_level1_btn = gtk_button_new();
108         gtk_widget_set_size_request (color_buttons.quote_level1_btn, 30, 20);
109         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level1_btn, 
110                             FALSE, FALSE, 0);
111
112         quotelevel1_label = gtk_label_new (_("Quoted Text - First Level"));
113         gtk_box_pack_start (GTK_BOX(hbox), quotelevel1_label, 
114                             FALSE, FALSE, 0);
115                 
116         hbox = gtk_hbox_new(FALSE, 8);
117         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 3, 4,
118                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
119
120         color_buttons.quote_level2_btn = gtk_button_new();
121         gtk_widget_set_size_request (color_buttons.quote_level2_btn, 30, 20);
122         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level2_btn, 
123                             FALSE, FALSE, 0);
124
125         quotelevel2_label = gtk_label_new (_("Quoted Text - Second Level"));
126         gtk_box_pack_start (GTK_BOX(hbox), quotelevel2_label, 
127                             FALSE, FALSE, 0);
128
129         hbox = gtk_hbox_new(FALSE, 8);
130         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 4, 5,
131                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
132
133         color_buttons.quote_level3_btn = gtk_button_new_with_label ("");
134         gtk_widget_set_size_request (color_buttons.quote_level3_btn, 30, 20);
135         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level3_btn, 
136                             FALSE, FALSE, 0);
137
138         quotelevel3_label = gtk_label_new (_("Quoted Text - Third Level"));
139         gtk_box_pack_start (GTK_BOX(hbox), quotelevel3_label, 
140                             FALSE, FALSE, 0);
141
142         hbox = gtk_hbox_new(FALSE, 8);
143         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 5, 6,
144                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
145
146         color_buttons.uri_btn = gtk_button_new_with_label ("");
147         gtk_widget_set_size_request (color_buttons.uri_btn, 30, 20);
148         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.uri_btn, 
149                             FALSE, FALSE, 0);
150
151         uri_label = gtk_label_new (_("URI link"));
152         gtk_box_pack_start (GTK_BOX(hbox), uri_label, FALSE, FALSE, 0);
153
154         hbox = gtk_hbox_new(FALSE, 8);
155         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 6, 7,
156                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
157
158         color_buttons.tgt_folder_btn = gtk_button_new_with_label ("");
159         gtk_widget_set_size_request (color_buttons.tgt_folder_btn, 30, 20);
160         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.tgt_folder_btn, 
161                             FALSE, FALSE, 0);
162
163         tgt_folder_label = gtk_label_new (_("Target folder"));
164         gtk_box_pack_start (GTK_BOX(hbox), tgt_folder_label, FALSE, FALSE, 0);
165
166         hbox = gtk_hbox_new(FALSE, 8);
167         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 7, 8,
168                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
169
170         color_buttons.signature_btn = gtk_button_new_with_label ("");
171         gtk_widget_set_size_request (color_buttons.signature_btn, 30, 20);
172         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.signature_btn, 
173                             FALSE, FALSE, 0);
174
175         signature_label = gtk_label_new (_("Signatures"));
176         gtk_box_pack_start (GTK_BOX(hbox), signature_label, FALSE, FALSE, 0);
177
178         chkbtn_recycle_colors = 
179                 gtk_check_button_new_with_label(_("Recycle quote colors"));
180         gtk_table_attach (GTK_TABLE (table), chkbtn_recycle_colors, 0, 1, 8, 9,
181                           (GtkAttachOptions) (GTK_FILL),
182                           (GtkAttachOptions) (0), 0, 0);
183                           
184         gtk_widget_show_all(table);
185
186         g_signal_connect(G_OBJECT(color_buttons.quote_level1_btn), "clicked",
187                          G_CALLBACK(quote_color_set_dialog), "LEVEL1");
188         g_signal_connect(G_OBJECT(color_buttons.quote_level2_btn), "clicked",
189                          G_CALLBACK(quote_color_set_dialog), "LEVEL2");
190         g_signal_connect(G_OBJECT(color_buttons.quote_level3_btn), "clicked",
191                          G_CALLBACK(quote_color_set_dialog), "LEVEL3");
192         g_signal_connect(G_OBJECT(color_buttons.uri_btn), "clicked",
193                          G_CALLBACK(quote_color_set_dialog), "URI");
194         g_signal_connect(G_OBJECT(color_buttons.tgt_folder_btn), "clicked",
195                          G_CALLBACK(quote_color_set_dialog), "TGTFLD");
196         g_signal_connect(G_OBJECT(color_buttons.signature_btn), "clicked",
197                          G_CALLBACK(quote_color_set_dialog), "SIGNATURE");
198
199         set_button_bg_color(color_buttons.quote_level1_btn,
200                             prefs_common.quote_level1_col);
201         set_button_bg_color(color_buttons.quote_level2_btn,
202                             prefs_common.quote_level2_col);
203         set_button_bg_color(color_buttons.quote_level3_btn,
204                             prefs_common.quote_level3_col);
205         set_button_bg_color(color_buttons.uri_btn,
206                             prefs_common.uri_col);
207         set_button_bg_color(color_buttons.tgt_folder_btn,
208                             prefs_common.tgt_folder_col);
209         set_button_bg_color(color_buttons.signature_btn,
210                             prefs_common.signature_col);
211
212         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enablecol),
213                                      prefs_common.enable_color);
214         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_recycle_colors),
215                                      prefs_common.recycle_quote_colors);
216
217         prefs_msg_colors->window                = GTK_WIDGET(window);
218         prefs_msg_colors->chkbtn_enablecol      = chkbtn_enablecol;
219         prefs_msg_colors->chkbtn_recycle_colors = chkbtn_recycle_colors;
220
221         prefs_msg_colors->page.widget = table;
222 }
223
224 static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
225 {
226         gchar *type = (gchar *)data;
227         gchar *title = NULL;
228         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
229         gint rgbvalue = 0;
230         GtkColorSelectionDialog *dialog;
231
232         if(g_ascii_strcasecmp(type, "LEVEL1") == 0) {
233                 title = _("Pick color for quotation level 1");
234                 rgbvalue = prefs_common.quote_level1_col;
235         } else if(g_ascii_strcasecmp(type, "LEVEL2") == 0) {
236                 title = _("Pick color for quotation level 2");
237                 rgbvalue = prefs_common.quote_level2_col;
238         } else if(g_ascii_strcasecmp(type, "LEVEL3") == 0) {
239                 title = _("Pick color for quotation level 3");
240                 rgbvalue = prefs_common.quote_level3_col;
241         } else if(g_ascii_strcasecmp(type, "URI") == 0) {
242                 title = _("Pick color for URI");
243                 rgbvalue = prefs_common.uri_col;
244         } else if(g_ascii_strcasecmp(type, "TGTFLD") == 0) {
245                 title = _("Pick color for target folder");
246                 rgbvalue = prefs_common.tgt_folder_col;
247         } else if(g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
248                 title = _("Pick color for signatures");
249                 rgbvalue = prefs_common.signature_col;
250         } else {   /* Should never be called */
251                 g_warning("Unrecognized datatype '%s' in quote_color_set_dialog\n", type);
252                 return;
253         }
254
255         color_dialog = gtk_color_selection_dialog_new(title);
256         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
257         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
258         gtk_window_set_resizable(GTK_WINDOW(color_dialog), FALSE);
259         manage_window_set_transient(GTK_WINDOW(color_dialog));
260
261         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
262                          "clicked", G_CALLBACK(quote_colors_set_dialog_ok), data);
263         g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
264                          "clicked", G_CALLBACK(quote_colors_set_dialog_cancel), data);
265         g_signal_connect(G_OBJECT(color_dialog), "key_press_event",
266                          G_CALLBACK(quote_colors_set_dialog_key_pressed),data);
267
268         /* preselect the previous color in the color selection dialog */
269         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
270         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
271         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
272         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
273         gtk_color_selection_set_color
274                 (GTK_COLOR_SELECTION(dialog->colorsel), color);
275
276         gtk_widget_show(color_dialog);
277 }
278
279 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
280 {
281         GtkColorSelection *colorsel = (GtkColorSelection *)
282                                                 ((GtkColorSelectionDialog *)color_dialog)->colorsel;
283         gdouble color[4];
284         gint red, green, blue, rgbvalue;
285         gchar *type = (gchar *)data;
286
287         gtk_color_selection_get_color(colorsel, color);
288
289         red      = (gint) (color[0] * 255.0);
290         green    = (gint) (color[1] * 255.0);
291         blue     = (gint) (color[2] * 255.0);
292         rgbvalue = (gint) ((red * 0x10000) | (green * 0x100) | blue);
293
294 #if 0
295         fprintf(stderr, "redc = %f, greenc = %f, bluec = %f\n", color[0], color[1], color[2]);
296         fprintf(stderr, "red = %d, green = %d, blue = %d\n", red, green, blue);
297         fprintf(stderr, "Color is %x\n", rgbvalue);
298 #endif
299
300         if (g_ascii_strcasecmp(type, "LEVEL1") == 0) {
301                 prefs_common.quote_level1_col = rgbvalue;
302                 set_button_bg_color(color_buttons.quote_level1_btn, rgbvalue);
303         } else if (g_ascii_strcasecmp(type, "LEVEL2") == 0) {
304                 prefs_common.quote_level2_col = rgbvalue;
305                 set_button_bg_color(color_buttons.quote_level2_btn, rgbvalue);
306         } else if (g_ascii_strcasecmp(type, "LEVEL3") == 0) {
307                 prefs_common.quote_level3_col = rgbvalue;
308                 set_button_bg_color(color_buttons.quote_level3_btn, rgbvalue);
309         } else if (g_ascii_strcasecmp(type, "URI") == 0) {
310                 prefs_common.uri_col = rgbvalue;
311                 set_button_bg_color(color_buttons.uri_btn, rgbvalue);
312         } else if (g_ascii_strcasecmp(type, "TGTFLD") == 0) {
313                 prefs_common.tgt_folder_col = rgbvalue;
314                 set_button_bg_color(color_buttons.tgt_folder_btn, rgbvalue);
315                 folderview_set_target_folder_color(prefs_common.tgt_folder_col);
316         } else if (g_ascii_strcasecmp(type, "SIGNATURE") == 0) {
317                 prefs_common.signature_col = rgbvalue;
318                 set_button_bg_color(color_buttons.signature_btn, rgbvalue);
319         } else
320                 fprintf( stderr, "Unrecognized datatype '%s' in quote_color_set_dialog_ok\n", type );
321
322         gtk_widget_destroy(color_dialog);
323 }
324
325 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
326 {
327         gtk_widget_destroy(color_dialog);
328 }
329
330 static gboolean quote_colors_set_dialog_key_pressed(GtkWidget *widget,
331                                                 GdkEventKey *event,
332                                                 gpointer data)
333 {
334         if (event) {
335                 switch (event->keyval) {
336                         case GDK_Escape:
337                                 gtk_button_clicked(GTK_BUTTON(GTK_COLOR_SELECTION_DIALOG
338                                                         (widget)->cancel_button));
339                                 return TRUE;
340                         case GDK_Return: 
341                         case GDK_KP_Enter:
342                                 /* NOTE: changing focus makes widget accept all currently 
343                                  * changed settings! */
344                                 gtk_widget_grab_focus
345                                         (GTK_COLOR_SELECTION_DIALOG
346                                                 (widget)->ok_button);
347                                 /* call ok handler */                                           
348                                 gtk_button_clicked(GTK_BUTTON
349                                         (GTK_COLOR_SELECTION_DIALOG
350                                                 (widget)->ok_button));
351                                 return TRUE;
352                         default:
353                                 break;
354                 }
355         }
356         return FALSE;
357 }
358
359 static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
360 {
361         GtkStyle *newstyle;
362         GdkColor color;
363
364         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
365         newstyle = gtk_style_copy(gtk_widget_get_default_style());
366         newstyle->bg[GTK_STATE_NORMAL]   = color;
367         newstyle->bg[GTK_STATE_PRELIGHT] = color;
368         newstyle->bg[GTK_STATE_ACTIVE]   = color;
369
370         gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
371 }
372
373 void prefs_msg_colors_save(PrefsPage *_page)
374 {
375         MsgColorsPage *page = (MsgColorsPage *) _page;
376
377         prefs_common.enable_color = 
378                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enablecol));
379         prefs_common.recycle_quote_colors =
380                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_recycle_colors));
381 }
382
383 static void prefs_msg_colors_destroy_widget(PrefsPage *_page)
384 {
385 }
386
387 MsgColorsPage *prefs_msg_colors;
388
389 void prefs_msg_colors_init(void)
390 {
391         MsgColorsPage *page;
392         static gchar *path[3];
393
394         path[0] = _("Message View");
395         path[1] = _("Colors");
396         path[2] = NULL;
397
398         page = g_new0(MsgColorsPage, 1);
399         page->page.path = path;
400         page->page.create_widget = prefs_msg_colors_create_widget;
401         page->page.destroy_widget = prefs_msg_colors_destroy_widget;
402         page->page.save_page = prefs_msg_colors_save;
403         page->page.weight = 165.0;
404         prefs_gtk_register_page((PrefsPage *) page);
405         prefs_msg_colors = page;
406 }
407
408 void prefs_msg_colors_done(void)
409 {
410         prefs_gtk_unregister_page((PrefsPage *) prefs_msg_colors);
411         g_free(prefs_msg_colors);
412 }
413