2006-11-18 [paul] 2.6.0cvs55
[claws.git] / src / prefs_compose_writing.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2006 Colin Leroy <colin@colino.net> & 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 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 #include "gtk/menu.h"
40
41 #include "manage_window.h"
42
43 #include "quote_fmt.h"
44 #include "prefs_template.h"
45 #include "alertpanel.h"
46
47 typedef struct _WritingPage
48 {
49         PrefsPage page;
50
51         GtkWidget *window;
52
53         GtkWidget *checkbtn_autoextedit;
54         GtkWidget *checkbtn_reply_account_autosel;
55         GtkWidget *checkbtn_forward_account_autosel;
56         GtkWidget *checkbtn_reedit_account_autosel;
57         GtkWidget *spinbtn_undolevel;
58         GtkWidget *checkbtn_default_reply_list;
59         GtkWidget *checkbtn_forward_as_attachment;
60         GtkWidget *checkbtn_redirect_keep_from;
61         GtkWidget *hbox_autosave;
62         GtkWidget *checkbtn_autosave;
63         GtkWidget *spinbtn_autosave_length;
64         GtkWidget *optmenu_dnd_insert_or_attach;
65         GtkWidget *checkbtn_compose_with_format;
66         GtkWidget *entry_subject;
67         GtkWidget *text_format;
68 } WritingPage;
69
70 void prefs_compose_writing_create_widget(PrefsPage *_page, GtkWindow *window, 
71                                   gpointer data)
72 {
73         WritingPage *prefs_writing = (WritingPage *) _page;
74         
75         GtkWidget *vbox1;
76         GtkWidget *vbox2;
77
78         GtkWidget *checkbtn_autoextedit;
79
80         GtkWidget *frame;
81         GtkWidget *hbox_autosel;
82         GtkWidget *checkbtn_reply_account_autosel;
83         GtkWidget *checkbtn_forward_account_autosel;
84         GtkWidget *checkbtn_reedit_account_autosel;
85
86         GtkWidget *hbox_undolevel;
87         GtkWidget *label_undolevel;
88         GtkObject *spinbtn_undolevel_adj;
89         GtkWidget *spinbtn_undolevel;
90
91         GtkWidget *checkbtn_default_reply_list;
92
93         GtkWidget *checkbtn_forward_as_attachment;
94         GtkWidget *checkbtn_redirect_keep_from;
95
96         GtkWidget *hbox_autosave;
97         GtkWidget *checkbtn_autosave;
98         GtkObject *spinbtn_autosave_adj;
99         GtkWidget *spinbtn_autosave_length;
100         GtkWidget *label_autosave_length;
101         
102         GtkWidget *hbox_dnd_insert_or_attach;
103         GtkWidget *label_dnd_insert_or_attach;
104         GtkWidget *optmenu_dnd_insert_or_attach;
105         GtkWidget *menu;
106         GtkWidget *menuitem;
107
108         GtkWidget *frame_format;
109         GtkWidget *checkbtn_compose_with_format;
110         GtkWidget *vbox_format;
111         GtkWidget *hbox_format;
112         GtkWidget *label_subject;
113         GtkWidget *entry_subject;
114         GtkWidget *scrolledwin_format;
115         GtkWidget *text_format;
116         GtkWidget *hbox_formatdesc;
117         GtkWidget *btn_formatdesc;
118
119         vbox1 = gtk_vbox_new (FALSE, VSPACING);
120         gtk_widget_show (vbox1);
121         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
122
123         /* Account autoselection */
124         PACK_FRAME(vbox1, frame, _("Automatic account selection"));
125
126         hbox_autosel = gtk_hbox_new (TRUE, VSPACING_NARROW);
127         gtk_widget_show (hbox_autosel);
128         gtk_container_add (GTK_CONTAINER (frame), hbox_autosel);
129         gtk_container_set_border_width (GTK_CONTAINER (hbox_autosel), 8);
130
131         PACK_CHECK_BUTTON (hbox_autosel, checkbtn_reply_account_autosel,
132                            _("when replying"));
133         PACK_CHECK_BUTTON (hbox_autosel, checkbtn_forward_account_autosel,
134                            _("when forwarding"));
135         PACK_CHECK_BUTTON (hbox_autosel, checkbtn_reedit_account_autosel,
136                            _("when re-editing"));
137
138         PACK_FRAME(vbox1, frame, _("Forwarding"));
139         
140         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
141         gtk_widget_show (vbox2);
142         gtk_container_add (GTK_CONTAINER (frame), vbox2);
143         gtk_container_set_border_width (GTK_CONTAINER (vbox2), 8);      
144
145         PACK_CHECK_BUTTON (vbox2, checkbtn_forward_as_attachment,
146                            _("Forward as attachment"));
147
148         PACK_CHECK_BUTTON (vbox2, checkbtn_redirect_keep_from,
149                            _("Keep the original 'From' header when redirecting"));
150
151         PACK_FRAME(vbox1, frame, _("Editing"));
152
153         vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
154         gtk_widget_show (vbox2);
155         gtk_container_add (GTK_CONTAINER (frame), vbox2);
156         gtk_container_set_border_width (GTK_CONTAINER (vbox2), 8);
157
158         PACK_CHECK_BUTTON (vbox2, checkbtn_autoextedit,
159                            _("Automatically launch the external editor"));
160
161         hbox_autosave = gtk_hbox_new (FALSE, 8);
162         gtk_widget_show (hbox_autosave);
163         gtk_box_pack_start (GTK_BOX (vbox2), hbox_autosave, FALSE, FALSE, 0);
164
165         PACK_CHECK_BUTTON (hbox_autosave, checkbtn_autosave,
166                            _("Autosave message text to Drafts folder every"));
167
168         spinbtn_autosave_adj = gtk_adjustment_new (50, 0, 1000, 1, 10, 10);
169         spinbtn_autosave_length = gtk_spin_button_new
170                 (GTK_ADJUSTMENT (spinbtn_autosave_adj), 1, 0);
171         gtk_widget_set_size_request (spinbtn_autosave_length, 64, -1);  
172         gtk_widget_show (spinbtn_autosave_length);
173         gtk_box_pack_start (GTK_BOX (hbox_autosave), spinbtn_autosave_length, FALSE, FALSE, 0);
174         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_autosave_length), TRUE);
175         
176         label_autosave_length = gtk_label_new(_("characters"));
177         gtk_widget_show (label_autosave_length);
178         gtk_box_pack_start (GTK_BOX (hbox_autosave), label_autosave_length, FALSE, FALSE, 0);
179         
180         hbox_undolevel = gtk_hbox_new (FALSE, 8);
181         gtk_widget_show (hbox_undolevel);
182         gtk_box_pack_start (GTK_BOX (vbox2), hbox_undolevel, FALSE, FALSE, 0);
183
184         label_undolevel = gtk_label_new (_("Undo level"));
185         gtk_widget_show (label_undolevel);
186         gtk_box_pack_start (GTK_BOX (hbox_undolevel), label_undolevel, FALSE, FALSE, 0);
187
188         spinbtn_undolevel_adj = gtk_adjustment_new (50, 0, 100, 1, 10, 10);
189         spinbtn_undolevel = gtk_spin_button_new
190                 (GTK_ADJUSTMENT (spinbtn_undolevel_adj), 1, 0);
191         gtk_widget_show (spinbtn_undolevel);
192         gtk_box_pack_start (GTK_BOX (hbox_undolevel), spinbtn_undolevel, FALSE, FALSE, 0);
193         gtk_widget_set_size_request (spinbtn_undolevel, 64, -1);
194         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_undolevel), TRUE);
195         
196         PACK_CHECK_BUTTON (vbox1, checkbtn_default_reply_list,
197                            _("Reply button invokes mailing list reply"));
198
199         /* dnd insert or attach */
200         label_dnd_insert_or_attach = gtk_label_new (_("When dropping files into the Compose window"));
201         gtk_misc_set_alignment(GTK_MISC(label_dnd_insert_or_attach), 0, 0.5);
202         gtk_widget_show (label_dnd_insert_or_attach);
203
204         optmenu_dnd_insert_or_attach = gtk_option_menu_new ();
205         gtk_widget_show (optmenu_dnd_insert_or_attach);
206
207         menu = gtk_menu_new ();
208         MENUITEM_ADD (menu, menuitem, _("Ask"), COMPOSE_DND_ASK);
209         MENUITEM_ADD (menu, menuitem, _("Insert"), COMPOSE_DND_INSERT);
210         MENUITEM_ADD (menu, menuitem, _("Attach"), COMPOSE_DND_ATTACH);
211
212         gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu_dnd_insert_or_attach), menu);
213
214         hbox_dnd_insert_or_attach = gtk_hbox_new(FALSE, 20);
215         gtk_widget_show(hbox_dnd_insert_or_attach);
216         gtk_box_pack_start(GTK_BOX(vbox1), hbox_dnd_insert_or_attach, FALSE, FALSE, 0);
217         gtk_box_pack_start(GTK_BOX(hbox_dnd_insert_or_attach),
218                         label_dnd_insert_or_attach, FALSE, FALSE, 0);
219         gtk_box_pack_start(GTK_BOX(hbox_dnd_insert_or_attach),
220                         optmenu_dnd_insert_or_attach, FALSE, FALSE, 0);
221         
222         SET_TOGGLE_SENSITIVITY (checkbtn_autosave, spinbtn_autosave_length);
223         SET_TOGGLE_SENSITIVITY (checkbtn_autosave, label_autosave_length);
224
225         PACK_CHECK_BUTTON (vbox1, checkbtn_compose_with_format, 
226                            _("Use format when composing new messages"));
227
228         PACK_FRAME (vbox1, frame_format, _("New message format"));
229
230         vbox_format = gtk_vbox_new (FALSE, VSPACING_NARROW);
231         gtk_widget_show (vbox_format);
232         gtk_container_add (GTK_CONTAINER (frame_format), vbox_format);
233         gtk_container_set_border_width (GTK_CONTAINER (vbox_format), 8);
234
235         hbox_format = gtk_hbox_new (FALSE, 8);
236         gtk_widget_show (hbox_format);
237         gtk_box_pack_start (GTK_BOX (vbox_format), hbox_format, FALSE, FALSE, 0);
238
239         label_subject = gtk_label_new (_("Subject"));
240         gtk_widget_show (label_subject);
241         gtk_box_pack_start (GTK_BOX (hbox_format), label_subject, FALSE, FALSE, 0);
242
243         entry_subject = gtk_entry_new ();
244         gtk_widget_show (entry_subject);
245         gtk_box_pack_start (GTK_BOX (hbox_format), entry_subject, TRUE, TRUE, 0);
246         gtk_widget_set_size_request (entry_subject, 100, -1);
247
248         scrolledwin_format = gtk_scrolled_window_new (NULL, NULL);
249         gtk_widget_show (scrolledwin_format);
250         gtk_box_pack_start (GTK_BOX (vbox_format), scrolledwin_format,
251                             TRUE, TRUE, 0);
252         gtk_scrolled_window_set_policy
253                 (GTK_SCROLLED_WINDOW (scrolledwin_format),
254                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
255         gtk_scrolled_window_set_shadow_type
256                 (GTK_SCROLLED_WINDOW (scrolledwin_format), GTK_SHADOW_IN);
257
258         text_format = gtk_text_view_new ();
259         gtk_widget_show (text_format);
260         gtk_container_add(GTK_CONTAINER(scrolledwin_format), text_format);
261         gtk_text_view_set_editable (GTK_TEXT_VIEW (text_format), TRUE);
262         gtk_widget_set_size_request(text_format, -1, 100);
263
264         hbox_formatdesc = gtk_hbox_new (FALSE, 32);
265         gtk_widget_show (hbox_formatdesc);
266         gtk_box_pack_start (GTK_BOX (vbox_format), hbox_formatdesc, FALSE, FALSE, 0);
267
268 #if GTK_CHECK_VERSION(2, 8, 0)
269         btn_formatdesc = gtk_button_new_from_stock(GTK_STOCK_INFO);
270 #else
271         btn_formatdesc =
272                 gtk_button_new_with_label (_(" Description of symbols... "));
273 #endif
274         gtk_widget_show (btn_formatdesc);
275         gtk_box_pack_start (GTK_BOX (hbox_formatdesc), btn_formatdesc, FALSE, FALSE, 0);
276         g_signal_connect(G_OBJECT(btn_formatdesc), "clicked",
277                          G_CALLBACK(quote_fmt_quote_description), GTK_WIDGET(window));
278
279         SET_TOGGLE_SENSITIVITY(checkbtn_compose_with_format, frame_format);
280
281         prefs_writing->checkbtn_autoextedit = checkbtn_autoextedit;
282
283         prefs_writing->checkbtn_reply_account_autosel   = checkbtn_reply_account_autosel;
284         prefs_writing->checkbtn_forward_account_autosel = checkbtn_forward_account_autosel;
285         prefs_writing->checkbtn_reedit_account_autosel  = checkbtn_reedit_account_autosel;
286
287         prefs_writing->spinbtn_undolevel     = spinbtn_undolevel;
288
289         prefs_writing->checkbtn_autosave     = checkbtn_autosave;
290         prefs_writing->spinbtn_autosave_length = spinbtn_autosave_length;
291         
292         prefs_writing->checkbtn_forward_as_attachment =
293                 checkbtn_forward_as_attachment;
294         prefs_writing->checkbtn_redirect_keep_from =
295                 checkbtn_redirect_keep_from;
296         prefs_writing->checkbtn_default_reply_list = checkbtn_default_reply_list;
297
298         prefs_writing->optmenu_dnd_insert_or_attach = optmenu_dnd_insert_or_attach;
299
300         prefs_writing->checkbtn_compose_with_format
301                                                                  = checkbtn_compose_with_format;
302         prefs_writing->entry_subject = entry_subject;
303         prefs_writing->text_format       = text_format;
304
305         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autoextedit),
306                 prefs_common.auto_exteditor);
307         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_as_attachment),
308                 prefs_common.forward_as_attachment);
309         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_redirect_keep_from),
310                 prefs_common.redirect_keep_from);
311         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autosave),
312                 prefs_common.autosave);
313         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_autosave_length),
314                 prefs_common.autosave_length);
315         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_undolevel),
316                 prefs_common.undolevels);
317         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reply_account_autosel),
318                 prefs_common.reply_account_autosel);
319         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_account_autosel),
320                 prefs_common.forward_account_autosel);
321         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reedit_account_autosel),
322                 prefs_common.reedit_account_autosel);
323         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_default_reply_list),
324                 prefs_common.default_reply_list);
325         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_dnd_insert_or_attach),
326                 prefs_common.compose_dnd_mode);
327
328         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_compose_with_format),
329                         prefs_common.compose_with_format);
330         pref_set_entry_from_pref(GTK_ENTRY(prefs_writing->entry_subject), prefs_common.compose_subject_format);
331         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_writing->text_format), prefs_common.compose_body_format);
332
333         prefs_writing->page.widget = vbox1;
334 }
335
336 void prefs_compose_writing_save(PrefsPage *_page)
337 {
338         GtkWidget *menu;
339         GtkWidget *menuitem;
340
341         WritingPage *page = (WritingPage *) _page;
342         prefs_common.auto_exteditor = 
343                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autoextedit));
344         prefs_common.forward_as_attachment =
345                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_as_attachment));
346         prefs_common.redirect_keep_from =
347                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_redirect_keep_from));
348         prefs_common.autosave = 
349                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autosave));
350         prefs_common.autosave_length =
351                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_autosave_length));
352         prefs_common.undolevels = 
353                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_undolevel));
354                 
355         prefs_common.reply_account_autosel =
356                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reply_account_autosel));
357         prefs_common.forward_account_autosel =
358                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_account_autosel));
359         prefs_common.reedit_account_autosel =
360                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reedit_account_autosel));
361         prefs_common.default_reply_list =
362                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_default_reply_list));
363         
364         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_dnd_insert_or_attach));
365         menuitem = gtk_menu_get_active(GTK_MENU(menu));
366         prefs_common.compose_dnd_mode = GPOINTER_TO_INT
367                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
368
369         prefs_common.compose_with_format =
370                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_compose_with_format));
371         prefs_common.compose_subject_format = pref_get_pref_from_entry(
372                         GTK_ENTRY(page->entry_subject));
373         if (!prefs_template_string_is_valid(prefs_common.compose_subject_format))
374                 alertpanel_error(_("New message subject format error."));
375         prefs_common.compose_body_format = pref_get_pref_from_textview(
376                         GTK_TEXT_VIEW(page->text_format));
377         if (!prefs_template_string_is_valid(prefs_common.compose_body_format))
378                 alertpanel_error(_("New message body format error."));
379 }
380
381 static void prefs_compose_writing_destroy_widget(PrefsPage *_page)
382 {
383 }
384
385 WritingPage *prefs_writing;
386
387 void prefs_compose_writing_init(void)
388 {
389         WritingPage *page;
390         static gchar *path[3];
391
392         path[0] = _("Compose");
393         path[1] = _("Writing");
394         path[2] = NULL;
395
396         page = g_new0(WritingPage, 1);
397         page->page.path = path;
398         page->page.create_widget = prefs_compose_writing_create_widget;
399         page->page.destroy_widget = prefs_compose_writing_destroy_widget;
400         page->page.save_page = prefs_compose_writing_save;
401         page->page.weight = 190.0;
402         prefs_gtk_register_page((PrefsPage *) page);
403         prefs_writing = page;
404 }
405
406 void prefs_compose_writing_done(void)
407 {
408         prefs_gtk_unregister_page((PrefsPage *) prefs_writing);
409         g_free(prefs_writing);
410 }