2007-01-13 [colin] 2.7.0cvs21
[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         vbox2 = gtkut_get_options_frame(vbox1, &frame, _("Forwarding"));
139
140         PACK_CHECK_BUTTON (vbox2, checkbtn_forward_as_attachment,
141                            _("Forward as attachment"));
142
143         PACK_CHECK_BUTTON (vbox2, checkbtn_redirect_keep_from,
144                            _("Keep the original 'From' header when redirecting"));
145
146         vbox2 = gtkut_get_options_frame(vbox1, &frame, _("Editing"));
147
148         PACK_CHECK_BUTTON (vbox2, checkbtn_autoextedit,
149                            _("Automatically launch the external editor"));
150
151         hbox_autosave = gtk_hbox_new (FALSE, 8);
152         gtk_widget_show (hbox_autosave);
153         gtk_box_pack_start (GTK_BOX (vbox2), hbox_autosave, FALSE, FALSE, 0);
154
155         PACK_CHECK_BUTTON (hbox_autosave, checkbtn_autosave,
156                            _("Autosave message text to Drafts folder every"));
157
158         spinbtn_autosave_adj = gtk_adjustment_new (50, 0, 1000, 1, 10, 10);
159         spinbtn_autosave_length = gtk_spin_button_new
160                 (GTK_ADJUSTMENT (spinbtn_autosave_adj), 1, 0);
161         gtk_widget_set_size_request (spinbtn_autosave_length, 64, -1);  
162         gtk_widget_show (spinbtn_autosave_length);
163         gtk_box_pack_start (GTK_BOX (hbox_autosave), spinbtn_autosave_length, FALSE, FALSE, 0);
164         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_autosave_length), TRUE);
165         
166         label_autosave_length = gtk_label_new(_("characters"));
167         gtk_widget_show (label_autosave_length);
168         gtk_box_pack_start (GTK_BOX (hbox_autosave), label_autosave_length, FALSE, FALSE, 0);
169         
170         hbox_undolevel = gtk_hbox_new (FALSE, 8);
171         gtk_widget_show (hbox_undolevel);
172         gtk_box_pack_start (GTK_BOX (vbox2), hbox_undolevel, FALSE, FALSE, 0);
173
174         label_undolevel = gtk_label_new (_("Undo level"));
175         gtk_widget_show (label_undolevel);
176         gtk_box_pack_start (GTK_BOX (hbox_undolevel), label_undolevel, FALSE, FALSE, 0);
177
178         spinbtn_undolevel_adj = gtk_adjustment_new (50, 0, 100, 1, 10, 10);
179         spinbtn_undolevel = gtk_spin_button_new
180                 (GTK_ADJUSTMENT (spinbtn_undolevel_adj), 1, 0);
181         gtk_widget_show (spinbtn_undolevel);
182         gtk_box_pack_start (GTK_BOX (hbox_undolevel), spinbtn_undolevel, FALSE, FALSE, 0);
183         gtk_widget_set_size_request (spinbtn_undolevel, 64, -1);
184         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_undolevel), TRUE);
185         
186         PACK_CHECK_BUTTON (vbox1, checkbtn_default_reply_list,
187                            _("Reply button invokes mailing list reply"));
188
189         /* dnd insert or attach */
190         label_dnd_insert_or_attach = gtk_label_new (_("When dropping files into the Compose window"));
191         gtk_misc_set_alignment(GTK_MISC(label_dnd_insert_or_attach), 0, 0.5);
192         gtk_widget_show (label_dnd_insert_or_attach);
193
194         optmenu_dnd_insert_or_attach = gtk_option_menu_new ();
195         gtk_widget_show (optmenu_dnd_insert_or_attach);
196
197         menu = gtk_menu_new ();
198         MENUITEM_ADD (menu, menuitem, _("Ask"), COMPOSE_DND_ASK);
199         MENUITEM_ADD (menu, menuitem, _("Insert"), COMPOSE_DND_INSERT);
200         MENUITEM_ADD (menu, menuitem, _("Attach"), COMPOSE_DND_ATTACH);
201
202         gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu_dnd_insert_or_attach), menu);
203
204         hbox_dnd_insert_or_attach = gtk_hbox_new(FALSE, 20);
205         gtk_widget_show(hbox_dnd_insert_or_attach);
206         gtk_box_pack_start(GTK_BOX(vbox1), hbox_dnd_insert_or_attach, FALSE, FALSE, 0);
207         gtk_box_pack_start(GTK_BOX(hbox_dnd_insert_or_attach),
208                         label_dnd_insert_or_attach, FALSE, FALSE, 0);
209         gtk_box_pack_start(GTK_BOX(hbox_dnd_insert_or_attach),
210                         optmenu_dnd_insert_or_attach, FALSE, FALSE, 0);
211         
212         SET_TOGGLE_SENSITIVITY (checkbtn_autosave, spinbtn_autosave_length);
213         SET_TOGGLE_SENSITIVITY (checkbtn_autosave, label_autosave_length);
214
215         PACK_CHECK_BUTTON (vbox1, checkbtn_compose_with_format, 
216                            _("Use format when composing new messages"));
217
218         vbox_format = gtkut_get_options_frame(vbox1, &frame_format, _("New message format"));
219
220         hbox_format = gtk_hbox_new (FALSE, 8);
221         gtk_widget_show (hbox_format);
222         gtk_box_pack_start (GTK_BOX (vbox_format), hbox_format, FALSE, FALSE, 0);
223
224         label_subject = gtk_label_new (_("Subject"));
225         gtk_widget_show (label_subject);
226         gtk_box_pack_start (GTK_BOX (hbox_format), label_subject, FALSE, FALSE, 0);
227
228         entry_subject = gtk_entry_new ();
229         gtk_widget_show (entry_subject);
230         gtk_box_pack_start (GTK_BOX (hbox_format), entry_subject, TRUE, TRUE, 0);
231         gtk_widget_set_size_request (entry_subject, 100, -1);
232
233         scrolledwin_format = gtk_scrolled_window_new (NULL, NULL);
234         gtk_widget_show (scrolledwin_format);
235         gtk_box_pack_start (GTK_BOX (vbox_format), scrolledwin_format,
236                             TRUE, TRUE, 0);
237         gtk_scrolled_window_set_policy
238                 (GTK_SCROLLED_WINDOW (scrolledwin_format),
239                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
240         gtk_scrolled_window_set_shadow_type
241                 (GTK_SCROLLED_WINDOW (scrolledwin_format), GTK_SHADOW_IN);
242
243         text_format = gtk_text_view_new ();
244         if (prefs_common.textfont) {
245                 PangoFontDescription *font_desc;
246
247                 font_desc = pango_font_description_from_string
248                                                 (prefs_common.textfont);
249                 if (font_desc) {
250                         gtk_widget_modify_font(text_format, font_desc);
251                         pango_font_description_free(font_desc);
252                 }
253         }
254         gtk_widget_show (text_format);
255         gtk_container_add(GTK_CONTAINER(scrolledwin_format), text_format);
256         gtk_text_view_set_editable (GTK_TEXT_VIEW (text_format), TRUE);
257         gtk_widget_set_size_request(text_format, -1, 100);
258
259         hbox_formatdesc = gtk_hbox_new (FALSE, 32);
260         gtk_widget_show (hbox_formatdesc);
261         gtk_box_pack_start (GTK_BOX (vbox_format), hbox_formatdesc, FALSE, FALSE, 0);
262
263 #if GTK_CHECK_VERSION(2, 8, 0)
264         btn_formatdesc = gtk_button_new_from_stock(GTK_STOCK_INFO);
265 #else
266         btn_formatdesc =
267                 gtk_button_new_with_label (_(" Description of symbols... "));
268 #endif
269         gtk_widget_show (btn_formatdesc);
270         gtk_box_pack_start (GTK_BOX (hbox_formatdesc), btn_formatdesc, FALSE, FALSE, 0);
271         g_signal_connect(G_OBJECT(btn_formatdesc), "clicked",
272                          G_CALLBACK(quote_fmt_quote_description), GTK_WIDGET(window));
273
274         SET_TOGGLE_SENSITIVITY(checkbtn_compose_with_format, frame_format);
275
276         prefs_writing->checkbtn_autoextedit = checkbtn_autoextedit;
277
278         prefs_writing->checkbtn_reply_account_autosel   = checkbtn_reply_account_autosel;
279         prefs_writing->checkbtn_forward_account_autosel = checkbtn_forward_account_autosel;
280         prefs_writing->checkbtn_reedit_account_autosel  = checkbtn_reedit_account_autosel;
281
282         prefs_writing->spinbtn_undolevel     = spinbtn_undolevel;
283
284         prefs_writing->checkbtn_autosave     = checkbtn_autosave;
285         prefs_writing->spinbtn_autosave_length = spinbtn_autosave_length;
286         
287         prefs_writing->checkbtn_forward_as_attachment =
288                 checkbtn_forward_as_attachment;
289         prefs_writing->checkbtn_redirect_keep_from =
290                 checkbtn_redirect_keep_from;
291         prefs_writing->checkbtn_default_reply_list = checkbtn_default_reply_list;
292
293         prefs_writing->optmenu_dnd_insert_or_attach = optmenu_dnd_insert_or_attach;
294
295         prefs_writing->checkbtn_compose_with_format
296                                                                  = checkbtn_compose_with_format;
297         prefs_writing->entry_subject = entry_subject;
298         prefs_writing->text_format       = text_format;
299
300         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autoextedit),
301                 prefs_common.auto_exteditor);
302         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_as_attachment),
303                 prefs_common.forward_as_attachment);
304         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_redirect_keep_from),
305                 prefs_common.redirect_keep_from);
306         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_autosave),
307                 prefs_common.autosave);
308         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_autosave_length),
309                 prefs_common.autosave_length);
310         gtk_spin_button_set_value(GTK_SPIN_BUTTON(prefs_writing->spinbtn_undolevel),
311                 prefs_common.undolevels);
312         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reply_account_autosel),
313                 prefs_common.reply_account_autosel);
314         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_forward_account_autosel),
315                 prefs_common.forward_account_autosel);
316         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_reedit_account_autosel),
317                 prefs_common.reedit_account_autosel);
318         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_default_reply_list),
319                 prefs_common.default_reply_list);
320         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_dnd_insert_or_attach),
321                 prefs_common.compose_dnd_mode);
322
323         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prefs_writing->checkbtn_compose_with_format),
324                         prefs_common.compose_with_format);
325         pref_set_entry_from_pref(GTK_ENTRY(prefs_writing->entry_subject), prefs_common.compose_subject_format);
326         pref_set_textview_from_pref(GTK_TEXT_VIEW(prefs_writing->text_format), prefs_common.compose_body_format);
327
328         prefs_writing->page.widget = vbox1;
329 }
330
331 void prefs_compose_writing_save(PrefsPage *_page)
332 {
333         GtkWidget *menu;
334         GtkWidget *menuitem;
335
336         WritingPage *page = (WritingPage *) _page;
337         prefs_common.auto_exteditor = 
338                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autoextedit));
339         prefs_common.forward_as_attachment =
340                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_as_attachment));
341         prefs_common.redirect_keep_from =
342                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_redirect_keep_from));
343         prefs_common.autosave = 
344                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autosave));
345         prefs_common.autosave_length =
346                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_autosave_length));
347         prefs_common.undolevels = 
348                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_undolevel));
349                 
350         prefs_common.reply_account_autosel =
351                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reply_account_autosel));
352         prefs_common.forward_account_autosel =
353                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_forward_account_autosel));
354         prefs_common.reedit_account_autosel =
355                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_reedit_account_autosel));
356         prefs_common.default_reply_list =
357                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_default_reply_list));
358         
359         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_dnd_insert_or_attach));
360         menuitem = gtk_menu_get_active(GTK_MENU(menu));
361         prefs_common.compose_dnd_mode = GPOINTER_TO_INT
362                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
363
364         prefs_common.compose_with_format =
365                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_compose_with_format));
366         prefs_common.compose_subject_format = pref_get_pref_from_entry(
367                         GTK_ENTRY(page->entry_subject));
368         if (!prefs_template_string_is_valid(prefs_common.compose_subject_format))
369                 alertpanel_error(_("New message subject format error."));
370         prefs_common.compose_body_format = pref_get_pref_from_textview(
371                         GTK_TEXT_VIEW(page->text_format));
372         if (!prefs_template_string_is_valid(prefs_common.compose_body_format))
373                 alertpanel_error(_("New message body format error."));
374 }
375
376 static void prefs_compose_writing_destroy_widget(PrefsPage *_page)
377 {
378 }
379
380 WritingPage *prefs_writing;
381
382 void prefs_compose_writing_init(void)
383 {
384         WritingPage *page;
385         static gchar *path[3];
386
387         path[0] = _("Compose");
388         path[1] = _("Writing");
389         path[2] = NULL;
390
391         page = g_new0(WritingPage, 1);
392         page->page.path = path;
393         page->page.create_widget = prefs_compose_writing_create_widget;
394         page->page.destroy_widget = prefs_compose_writing_destroy_widget;
395         page->page.save_page = prefs_compose_writing_save;
396         page->page.weight = 190.0;
397         prefs_gtk_register_page((PrefsPage *) page);
398         prefs_writing = page;
399 }
400
401 void prefs_compose_writing_done(void)
402 {
403         prefs_gtk_unregister_page((PrefsPage *) prefs_writing);
404         g_free(prefs_writing);
405 }