9bcdeac1bbb281f180343230816bca2cc9731175
[claws.git] / src / prefs_compose_writing.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2007 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         if (prefs_common.textfont) {
260                 PangoFontDescription *font_desc;
261
262                 font_desc = pango_font_description_from_string
263                                                 (prefs_common.textfont);
264                 if (font_desc) {
265                         gtk_widget_modify_font(text_format, font_desc);
266                         pango_font_description_free(font_desc);
267                 }
268         }
269         gtk_widget_show (text_format);
270         gtk_container_add(GTK_CONTAINER(scrolledwin_format), text_format);
271         gtk_text_view_set_editable (GTK_TEXT_VIEW (text_format), TRUE);
272         gtk_widget_set_size_request(text_format, -1, 100);
273
274         hbox_formatdesc = gtk_hbox_new (FALSE, 32);
275         gtk_widget_show (hbox_formatdesc);
276         gtk_box_pack_start (GTK_BOX (vbox_format), hbox_formatdesc, FALSE, FALSE, 0);
277
278 #if GTK_CHECK_VERSION(2, 8, 0)
279         btn_formatdesc = gtk_button_new_from_stock(GTK_STOCK_INFO);
280 #else
281         btn_formatdesc =
282                 gtk_button_new_with_label (_(" Description of symbols... "));
283 #endif
284         gtk_widget_show (btn_formatdesc);
285         gtk_box_pack_start (GTK_BOX (hbox_formatdesc), btn_formatdesc, FALSE, FALSE, 0);
286         g_signal_connect(G_OBJECT(btn_formatdesc), "clicked",
287                          G_CALLBACK(quote_fmt_quote_description), GTK_WIDGET(window));
288
289         SET_TOGGLE_SENSITIVITY(checkbtn_compose_with_format, frame_format);
290
291         prefs_writing->checkbtn_autoextedit = checkbtn_autoextedit;
292
293         prefs_writing->checkbtn_reply_account_autosel   = checkbtn_reply_account_autosel;
294         prefs_writing->checkbtn_forward_account_autosel = checkbtn_forward_account_autosel;
295         prefs_writing->checkbtn_reedit_account_autosel  = checkbtn_reedit_account_autosel;
296
297         prefs_writing->spinbtn_undolevel     = spinbtn_undolevel;
298
299         prefs_writing->checkbtn_autosave     = checkbtn_autosave;
300         prefs_writing->spinbtn_autosave_length = spinbtn_autosave_length;
301         
302         prefs_writing->checkbtn_forward_as_attachment =
303                 checkbtn_forward_as_attachment;
304         prefs_writing->checkbtn_redirect_keep_from =
305                 checkbtn_redirect_keep_from;
306         prefs_writing->checkbtn_default_reply_list = checkbtn_default_reply_list;
307
308         prefs_writing->optmenu_dnd_insert_or_attach = optmenu_dnd_insert_or_attach;
309
310         prefs_writing->checkbtn_compose_with_format
311                                                                  = checkbtn_compose_with_format;
312         prefs_writing->entry_subject = entry_subject;
313         prefs_writing->text_format       = text_format;
314
315         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autoextedit),
316                 prefs_common.auto_exteditor);
317         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_as_attachment),
318                 prefs_common.forward_as_attachment);
319         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_redirect_keep_from),
320                 prefs_common.redirect_keep_from);
321         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autosave),
322                 prefs_common.autosave);
323         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_autosave_length),
324                 prefs_common.autosave_length);
325         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_undolevel),
326                 prefs_common.undolevels);
327         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reply_account_autosel),
328                 prefs_common.reply_account_autosel);
329         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_account_autosel),
330                 prefs_common.forward_account_autosel);
331         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reedit_account_autosel),
332                 prefs_common.reedit_account_autosel);
333         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_default_reply_list),
334                 prefs_common.default_reply_list);
335         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_dnd_insert_or_attach),
336                 prefs_common.compose_dnd_mode);
337
338         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_compose_with_format),
339                         prefs_common.compose_with_format);
340         pref_set_entry_from_pref(GTK_ENTRY(prefs_writing->entry_subject), prefs_common.compose_subject_format);
341         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_writing->text_format), prefs_common.compose_body_format);
342
343         prefs_writing->page.widget = vbox1;
344 }
345
346 void prefs_compose_writing_save(PrefsPage *_page)
347 {
348         GtkWidget *menu;
349         GtkWidget *menuitem;
350
351         WritingPage *page = (WritingPage *) _page;
352         prefs_common.auto_exteditor = 
353                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autoextedit));
354         prefs_common.forward_as_attachment =
355                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_as_attachment));
356         prefs_common.redirect_keep_from =
357                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_redirect_keep_from));
358         prefs_common.autosave = 
359                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autosave));
360         prefs_common.autosave_length =
361                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_autosave_length));
362         prefs_common.undolevels = 
363                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_undolevel));
364                 
365         prefs_common.reply_account_autosel =
366                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reply_account_autosel));
367         prefs_common.forward_account_autosel =
368                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_account_autosel));
369         prefs_common.reedit_account_autosel =
370                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reedit_account_autosel));
371         prefs_common.default_reply_list =
372                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_default_reply_list));
373         
374         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_dnd_insert_or_attach));
375         menuitem = gtk_menu_get_active(GTK_MENU(menu));
376         prefs_common.compose_dnd_mode = GPOINTER_TO_INT
377                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
378
379         prefs_common.compose_with_format =
380                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_compose_with_format));
381         prefs_common.compose_subject_format = pref_get_pref_from_entry(
382                         GTK_ENTRY(page->entry_subject));
383         if (!prefs_template_string_is_valid(prefs_common.compose_subject_format))
384                 alertpanel_error(_("New message subject format error."));
385         prefs_common.compose_body_format = pref_get_pref_from_textview(
386                         GTK_TEXT_VIEW(page->text_format));
387         if (!prefs_template_string_is_valid(prefs_common.compose_body_format))
388                 alertpanel_error(_("New message body format error."));
389 }
390
391 static void prefs_compose_writing_destroy_widget(PrefsPage *_page)
392 {
393 }
394
395 WritingPage *prefs_writing;
396
397 void prefs_compose_writing_init(void)
398 {
399         WritingPage *page;
400         static gchar *path[3];
401
402         path[0] = _("Compose");
403         path[1] = _("Writing");
404         path[2] = NULL;
405
406         page = g_new0(WritingPage, 1);
407         page->page.path = path;
408         page->page.create_widget = prefs_compose_writing_create_widget;
409         page->page.destroy_widget = prefs_compose_writing_destroy_widget;
410         page->page.save_page = prefs_compose_writing_save;
411         page->page.weight = 190.0;
412         prefs_gtk_register_page((PrefsPage *) page);
413         prefs_writing = page;
414 }
415
416 void prefs_compose_writing_done(void)
417 {
418         prefs_gtk_unregister_page((PrefsPage *) prefs_writing);
419         g_free(prefs_writing);
420 }