This commit was manufactured by cvs2svn to create branch 'gtk2'.
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32
33 #include "intl.h"
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 prefs_quote_colors_key_pressed      (GtkWidget      *widget,
64                                                  GdkEventKey    *event,
65                                                  gpointer        data);
66 static void quote_color_set_dialog              (GtkWidget      *widget,
67                                                  gpointer        data);
68 static void quote_colors_set_dialog_ok          (GtkWidget      *widget,
69                                                  gpointer        data);
70 static void quote_colors_set_dialog_cancel      (GtkWidget      *widget,
71                                                  gpointer        data);
72 static void quote_colors_set_dialog_key_pressed (GtkWidget      *widget,
73                                                  GdkEventKey    *event,
74                                                  gpointer        data);
75 static void set_button_bg_color                 (GtkWidget      *widget,
76                                                  gint            color);
77
78 void prefs_msg_colors_create_widget(PrefsPage *_page, GtkWindow *window, 
79                                     gpointer data)
80 {
81         MsgColorsPage *prefs_msg_colors = (MsgColorsPage *) _page;
82         
83         GtkWidget *table;
84         GtkWidget *chkbtn_enablecol;
85         GtkWidget *quotelevel1_label;
86         GtkWidget *quotelevel2_label;
87         GtkWidget *quotelevel3_label;
88         GtkWidget *uri_label;
89         GtkWidget *signature_label;
90         GtkWidget *tgt_folder_label;
91         GtkWidget *chkbtn_recycle_colors;
92         GtkWidget *hbox;
93
94         table = gtk_table_new(8, 3, FALSE);
95         gtk_widget_show(table);
96         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
97         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
98         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
99
100         chkbtn_enablecol = 
101                 gtk_check_button_new_with_label(_("Enable coloration of message"));
102         gtk_table_attach (GTK_TABLE (table), chkbtn_enablecol, 0, 1, 1, 2,
103                           (GtkAttachOptions) (GTK_FILL),
104                           (GtkAttachOptions) (0), 0, 0);
105
106         hbox = gtk_hbox_new(FALSE, 8);
107         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 2, 3,
108                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
109
110         color_buttons.quote_level1_btn = gtk_button_new();
111         gtk_widget_set_usize (color_buttons.quote_level1_btn, 30, 20);
112         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level1_btn, 
113                             FALSE, FALSE, 0);
114
115         quotelevel1_label = gtk_label_new (_("Quoted Text - First Level"));
116         gtk_box_pack_start (GTK_BOX(hbox), quotelevel1_label, 
117                             FALSE, FALSE, 0);
118                 
119         hbox = gtk_hbox_new(FALSE, 8);
120         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 3, 4,
121                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
122
123         color_buttons.quote_level2_btn = gtk_button_new();
124         gtk_widget_set_usize (color_buttons.quote_level2_btn, 30, 20);
125         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level2_btn, 
126                             FALSE, FALSE, 0);
127
128         quotelevel2_label = gtk_label_new (_("Quoted Text - Second Level"));
129         gtk_box_pack_start (GTK_BOX(hbox), quotelevel2_label, 
130                             FALSE, FALSE, 0);
131
132         hbox = gtk_hbox_new(FALSE, 8);
133         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 4, 5,
134                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
135
136         color_buttons.quote_level3_btn = gtk_button_new_with_label ("");
137         gtk_widget_set_usize (color_buttons.quote_level3_btn, 30, 20);
138         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.quote_level3_btn, 
139                             FALSE, FALSE, 0);
140
141         quotelevel3_label = gtk_label_new (_("Quoted Text - Third Level"));
142         gtk_box_pack_start (GTK_BOX(hbox), quotelevel3_label, 
143                             FALSE, FALSE, 0);
144
145         hbox = gtk_hbox_new(FALSE, 8);
146         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 5, 6,
147                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
148
149         color_buttons.uri_btn = gtk_button_new_with_label ("");
150         gtk_widget_set_usize (color_buttons.uri_btn, 30, 20);
151         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.uri_btn, 
152                             FALSE, FALSE, 0);
153
154         uri_label = gtk_label_new (_("URI link"));
155         gtk_box_pack_start (GTK_BOX(hbox), uri_label, FALSE, FALSE, 0);
156
157         hbox = gtk_hbox_new(FALSE, 8);
158         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 6, 7,
159                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
160
161         color_buttons.tgt_folder_btn = gtk_button_new_with_label ("");
162         gtk_widget_set_usize (color_buttons.tgt_folder_btn, 30, 20);
163         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.tgt_folder_btn, 
164                             FALSE, FALSE, 0);
165
166         tgt_folder_label = gtk_label_new (_("Target folder"));
167         gtk_box_pack_start (GTK_BOX(hbox), tgt_folder_label, FALSE, FALSE, 0);
168
169         hbox = gtk_hbox_new(FALSE, 8);
170         gtk_table_attach (GTK_TABLE (table), hbox, 0, 1, 7, 8,
171                           (GTK_EXPAND | GTK_FILL), 0, 0, 0);
172
173         color_buttons.signature_btn = gtk_button_new_with_label ("");
174         gtk_widget_set_usize (color_buttons.signature_btn, 30, 20);
175         gtk_box_pack_start (GTK_BOX(hbox), color_buttons.signature_btn, 
176                             FALSE, FALSE, 0);
177
178         signature_label = gtk_label_new (_("Signatures"));
179         gtk_box_pack_start (GTK_BOX(hbox), signature_label, FALSE, FALSE, 0);
180
181         chkbtn_recycle_colors = 
182                 gtk_check_button_new_with_label(_("Recycle quote colors"));
183         gtk_table_attach (GTK_TABLE (table), chkbtn_recycle_colors, 0, 1, 8, 9,
184                           (GtkAttachOptions) (GTK_FILL),
185                           (GtkAttachOptions) (0), 0, 0);
186                           
187         gtk_widget_show_all(table);
188
189         gtk_signal_connect(GTK_OBJECT(color_buttons.quote_level1_btn), "clicked",
190                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "LEVEL1");
191         gtk_signal_connect(GTK_OBJECT(color_buttons.quote_level2_btn), "clicked",
192                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "LEVEL2");
193         gtk_signal_connect(GTK_OBJECT(color_buttons.quote_level3_btn), "clicked",
194                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "LEVEL3");
195         gtk_signal_connect(GTK_OBJECT(color_buttons.uri_btn), "clicked",
196                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "URI");
197         gtk_signal_connect(GTK_OBJECT(color_buttons.tgt_folder_btn), "clicked",
198                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "TGTFLD");
199         gtk_signal_connect(GTK_OBJECT(color_buttons.signature_btn), "clicked",
200                            GTK_SIGNAL_FUNC(quote_color_set_dialog), "SIGNATURE");
201
202         set_button_bg_color(color_buttons.quote_level1_btn,
203                             prefs_common.quote_level1_col);
204         set_button_bg_color(color_buttons.quote_level2_btn,
205                             prefs_common.quote_level2_col);
206         set_button_bg_color(color_buttons.quote_level3_btn,
207                             prefs_common.quote_level3_col);
208         set_button_bg_color(color_buttons.uri_btn,
209                             prefs_common.uri_col);
210         set_button_bg_color(color_buttons.tgt_folder_btn,
211                             prefs_common.tgt_folder_col);
212         set_button_bg_color(color_buttons.signature_btn,
213                             prefs_common.signature_col);
214
215         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_enablecol),
216                                      prefs_common.enable_color);
217         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chkbtn_recycle_colors),
218                                      prefs_common.recycle_quote_colors);
219
220         prefs_msg_colors->window                = GTK_WIDGET(window);
221         prefs_msg_colors->chkbtn_enablecol      = chkbtn_enablecol;
222         prefs_msg_colors->chkbtn_recycle_colors = chkbtn_recycle_colors;
223
224         prefs_msg_colors->page.widget = table;
225 }
226
227 static void prefs_quote_colors_key_pressed(GtkWidget *widget,
228                                            GdkEventKey *event, gpointer data)
229 {
230         if (event && event->keyval == GDK_Escape)
231                 gtk_main_quit();
232 }
233
234 static void quote_color_set_dialog(GtkWidget *widget, gpointer data)
235 {
236         gchar *type = (gchar *)data;
237         gchar *title = NULL;
238         gdouble color[4] = {0.0, 0.0, 0.0, 0.0};
239         gint rgbvalue = 0;
240         GtkColorSelectionDialog *dialog;
241
242         if(g_strcasecmp(type, "LEVEL1") == 0) {
243                 title = _("Pick color for quotation level 1");
244                 rgbvalue = prefs_common.quote_level1_col;
245         } else if(g_strcasecmp(type, "LEVEL2") == 0) {
246                 title = _("Pick color for quotation level 2");
247                 rgbvalue = prefs_common.quote_level2_col;
248         } else if(g_strcasecmp(type, "LEVEL3") == 0) {
249                 title = _("Pick color for quotation level 3");
250                 rgbvalue = prefs_common.quote_level3_col;
251         } else if(g_strcasecmp(type, "URI") == 0) {
252                 title = _("Pick color for URI");
253                 rgbvalue = prefs_common.uri_col;
254         } else if(g_strcasecmp(type, "TGTFLD") == 0) {
255                 title = _("Pick color for target folder");
256                 rgbvalue = prefs_common.tgt_folder_col;
257         } else if(g_strcasecmp(type, "SIGNATURE") == 0) {
258                 title = _("Pick color for signatures");
259                 rgbvalue = prefs_common.signature_col;
260         } else {   /* Should never be called */
261                 g_warning("Unrecognized datatype '%s' in quote_color_set_dialog\n", type);
262                 return;
263         }
264
265         color_dialog = gtk_color_selection_dialog_new(title);
266         gtk_window_set_position(GTK_WINDOW(color_dialog), GTK_WIN_POS_CENTER);
267         gtk_window_set_modal(GTK_WINDOW(color_dialog), TRUE);
268         gtk_window_set_policy(GTK_WINDOW(color_dialog), FALSE, FALSE, FALSE);
269         manage_window_set_transient(GTK_WINDOW(color_dialog));
270
271         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->ok_button),
272                            "clicked", GTK_SIGNAL_FUNC(quote_colors_set_dialog_ok), data);
273         gtk_signal_connect(GTK_OBJECT(GTK_COLOR_SELECTION_DIALOG(color_dialog)->cancel_button),
274                            "clicked", GTK_SIGNAL_FUNC(quote_colors_set_dialog_cancel), data);
275         gtk_signal_connect(GTK_OBJECT(color_dialog), "key_press_event",
276                            GTK_SIGNAL_FUNC(quote_colors_set_dialog_key_pressed),
277                            data);
278
279         /* preselect the previous color in the color selection dialog */
280         color[0] = (gdouble) ((rgbvalue & 0xff0000) >> 16) / 255.0;
281         color[1] = (gdouble) ((rgbvalue & 0x00ff00) >>  8) / 255.0;
282         color[2] = (gdouble)  (rgbvalue & 0x0000ff)        / 255.0;
283         dialog = GTK_COLOR_SELECTION_DIALOG(color_dialog);
284         gtk_color_selection_set_color
285                 (GTK_COLOR_SELECTION(dialog->colorsel), color);
286
287         gtk_widget_show(color_dialog);
288 }
289
290 static void quote_colors_set_dialog_ok(GtkWidget *widget, gpointer data)
291 {
292         GtkColorSelection *colorsel = (GtkColorSelection *)
293                                                 ((GtkColorSelectionDialog *)color_dialog)->colorsel;
294         gdouble color[4];
295         gint red, green, blue, rgbvalue;
296         gchar *type = (gchar *)data;
297
298         gtk_color_selection_get_color(colorsel, color);
299
300         red      = (gint) (color[0] * 255.0);
301         green    = (gint) (color[1] * 255.0);
302         blue     = (gint) (color[2] * 255.0);
303         rgbvalue = (gint) ((red * 0x10000) | (green * 0x100) | blue);
304
305 #if 0
306         fprintf(stderr, "redc = %f, greenc = %f, bluec = %f\n", color[0], color[1], color[2]);
307         fprintf(stderr, "red = %d, green = %d, blue = %d\n", red, green, blue);
308         fprintf(stderr, "Color is %x\n", rgbvalue);
309 #endif
310
311         if (g_strcasecmp(type, "LEVEL1") == 0) {
312                 prefs_common.quote_level1_col = rgbvalue;
313                 set_button_bg_color(color_buttons.quote_level1_btn, rgbvalue);
314         } else if (g_strcasecmp(type, "LEVEL2") == 0) {
315                 prefs_common.quote_level2_col = rgbvalue;
316                 set_button_bg_color(color_buttons.quote_level2_btn, rgbvalue);
317         } else if (g_strcasecmp(type, "LEVEL3") == 0) {
318                 prefs_common.quote_level3_col = rgbvalue;
319                 set_button_bg_color(color_buttons.quote_level3_btn, rgbvalue);
320         } else if (g_strcasecmp(type, "URI") == 0) {
321                 prefs_common.uri_col = rgbvalue;
322                 set_button_bg_color(color_buttons.uri_btn, rgbvalue);
323         } else if (g_strcasecmp(type, "TGTFLD") == 0) {
324                 prefs_common.tgt_folder_col = rgbvalue;
325                 set_button_bg_color(color_buttons.tgt_folder_btn, rgbvalue);
326                 folderview_set_target_folder_color(prefs_common.tgt_folder_col);
327         } else if (g_strcasecmp(type, "SIGNATURE") == 0) {
328                 prefs_common.signature_col = rgbvalue;
329                 set_button_bg_color(color_buttons.signature_btn, rgbvalue);
330         } else
331                 fprintf( stderr, "Unrecognized datatype '%s' in quote_color_set_dialog_ok\n", type );
332
333         gtk_widget_destroy(color_dialog);
334 }
335
336 static void quote_colors_set_dialog_cancel(GtkWidget *widget, gpointer data)
337 {
338         gtk_widget_destroy(color_dialog);
339 }
340
341 static void quote_colors_set_dialog_key_pressed(GtkWidget *widget,
342                                                 GdkEventKey *event,
343                                                 gpointer data)
344 {
345         gtk_widget_destroy(color_dialog);
346 }
347
348 static void set_button_bg_color(GtkWidget *widget, gint rgbvalue)
349 {
350         GtkStyle *newstyle;
351         GdkColor color;
352
353         gtkut_convert_int_to_gdk_color(rgbvalue, &color);
354         newstyle = gtk_style_copy(gtk_widget_get_default_style());
355         newstyle->bg[GTK_STATE_NORMAL]   = color;
356         newstyle->bg[GTK_STATE_PRELIGHT] = color;
357         newstyle->bg[GTK_STATE_ACTIVE]   = color;
358
359         gtk_widget_set_style(GTK_WIDGET(widget), newstyle);
360 }
361
362 void prefs_msg_colors_save(PrefsPage *_page)
363 {
364         MsgColorsPage *page = (MsgColorsPage *) _page;
365
366         prefs_common.enable_color = 
367                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_enablecol));
368         prefs_common.recycle_quote_colors =
369                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->chkbtn_recycle_colors));
370 }
371
372 static void prefs_msg_colors_destroy_widget(PrefsPage *_page)
373 {
374 }
375
376 MsgColorsPage *prefs_msg_colors;
377
378 void prefs_msg_colors_init(void)
379 {
380         MsgColorsPage *page;
381
382         page = g_new0(MsgColorsPage, 1);
383         page->page.path = _("Message View/Colors");
384         page->page.create_widget = prefs_msg_colors_create_widget;
385         page->page.destroy_widget = prefs_msg_colors_destroy_widget;
386         page->page.save_page = prefs_msg_colors_save;
387         page->page.weight = 60.0;
388         prefs_gtk_register_page((PrefsPage *) page);
389         prefs_msg_colors = page;
390 }
391
392 void prefs_msg_colors_done(void)
393 {
394         prefs_gtk_unregister_page((PrefsPage *) prefs_msg_colors);
395         g_free(prefs_msg_colors);
396 }
397