4052b5105d0e0866d3157a9af94ab7d404d19bda
[claws.git] / src / prefs_spelling.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2006 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 #if USE_ASPELL
25
26 #include "defs.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gtk/gtk.h>
34
35 #include "utils.h"
36 #include "prefs_common.h"
37 #include "prefs_gtk.h"
38
39 #include "gtk/gtkutils.h"
40 #include "gtk/prefswindow.h"
41 #include "gtk/filesel.h"
42 #include "gtk/colorsel.h"
43
44 typedef struct _SpellingPage
45 {
46         PrefsPage page;
47
48         GtkWidget *window;              /* do not modify */
49
50         GtkWidget *automatic_frame;
51         GtkWidget *dictionary_frame;
52         GtkWidget *path_frame;
53         
54         GtkWidget *enable_aspell_checkbtn;
55         GtkWidget *recheck_when_changing_dict_checkbtn;
56         GtkWidget *check_while_typing_checkbtn;
57         GtkWidget *use_alternate_checkbtn;
58
59         GtkWidget *aspell_path_entry;
60         GtkWidget *aspell_path_select;
61
62         GtkWidget *default_dict_label;
63         GtkWidget *default_dict_optmenu;
64         GtkWidget *default_dict_optmenu_menu;
65
66         GtkWidget *sugmode_label;
67         GtkWidget *sugmode_optmenu;
68         GtkWidget *sugmode_optmenu_menu;
69
70         GtkWidget *misspelled_label;
71         GtkWidget *misspelled_colorbtn;
72         GtkWidget *misspelled_useblack_label;
73
74         gint       misspell_col;
75 } SpellingPage;
76
77 static void prefs_spelling_enable(SpellingPage *spelling, gboolean enable)
78 {
79         gtk_widget_set_sensitive(spelling->automatic_frame, enable);
80         gtk_widget_set_sensitive(spelling->dictionary_frame, enable);
81         gtk_widget_set_sensitive(spelling->path_frame, enable);
82         gtk_widget_set_sensitive(spelling->misspelled_label, enable);
83         gtk_widget_set_sensitive(spelling->misspelled_colorbtn, enable);
84         gtk_widget_set_sensitive(spelling->use_alternate_checkbtn, enable);
85 }
86
87 static void prefs_spelling_checkbtn_enable_aspell_toggle_cb
88         (GtkWidget *widget,
89          gpointer data)
90 {
91         SpellingPage *spelling = (SpellingPage *) data;
92         gboolean toggled;
93
94         toggled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
95         prefs_spelling_enable(spelling, toggled);
96 }
97
98 static void prefs_spelling_btn_aspell_path_clicked_cb(GtkWidget *widget,
99                                                      gpointer data)
100 {
101         SpellingPage *spelling = (SpellingPage *) data;
102         gchar *file_path;
103         GtkWidget *new_menu;
104
105         file_path = filesel_select_file_open(_("Select dictionaries location"),
106                                         prefs_common.aspell_path);
107         if (file_path != NULL) {
108                 gchar *tmp_path, *tmp;
109
110                 tmp_path = g_path_get_dirname(file_path);
111                 tmp = g_strdup_printf("%s%s", tmp_path, G_DIR_SEPARATOR_S);
112                 g_free(tmp_path);
113
114                 new_menu = gtkaspell_dictionary_option_menu_new(tmp);
115                 gtk_option_menu_set_menu(GTK_OPTION_MENU(spelling->default_dict_optmenu),
116                                          new_menu);
117
118                 gtk_entry_set_text(GTK_ENTRY(spelling->aspell_path_entry), tmp);
119                 /* select first one */
120                 gtk_option_menu_set_history(GTK_OPTION_MENU(
121                                         spelling->default_dict_optmenu), 0);
122         
123                 g_free(tmp);
124
125         }
126 }
127
128 static void prefs_spelling_colorsel(GtkWidget *widget,
129                                     gpointer data)
130 {
131         SpellingPage *spelling = (SpellingPage *) data;
132         gint rgbcolor;
133
134         rgbcolor = colorsel_select_color_rgb(_("Pick color for misspelled word"), 
135                                              spelling->misspell_col);
136         gtkut_set_widget_bgcolor_rgb(spelling->misspelled_colorbtn, rgbcolor);
137         spelling->misspell_col = rgbcolor;
138 }
139
140 #define SAFE_STRING(str) \
141         (str) ? (str) : ""
142
143 void prefs_spelling_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
144 {
145         SpellingPage *prefs_spelling = (SpellingPage *) _page;
146
147         GtkWidget *vbox1, *vbox2, *hbox;
148
149         GtkWidget *enable_aspell_checkbtn;
150         GtkWidget *check_while_typing_checkbtn;
151         GtkWidget *recheck_when_changing_dict_checkbtn;
152         GtkWidget *use_alternate_checkbtn;
153
154         GtkWidget *label;
155
156         GtkWidget *automatic_frame;
157         GtkWidget *dictionary_frame;
158         GtkWidget *path_frame;
159
160         GtkWidget *aspell_path_hbox;
161         GtkWidget *aspell_path_entry;
162         GtkWidget *aspell_path_select;
163
164         GtkWidget *default_dict_label;
165         GtkWidget *default_dict_optmenu;
166         GtkWidget *default_dict_optmenu_menu;
167
168         GtkWidget *sugmode_label;
169         GtkWidget *sugmode_optmenu;
170         GtkWidget *sugmode_optmenu_menu;
171
172         GtkWidget *misspelled_label;
173         GtkWidget *misspelled_hbox;
174         GtkWidget *misspelled_colorbtn;
175         GtkTooltips *tooltips;
176
177         vbox1 = gtk_vbox_new (FALSE, VSPACING);
178         gtk_widget_show (vbox1);
179         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
180
181         vbox2 = gtk_vbox_new (FALSE, 0);
182         gtk_widget_show (vbox2);
183         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
184
185         enable_aspell_checkbtn = gtk_check_button_new_with_label(
186                         _("Enable spell checker"));
187         gtk_widget_show(enable_aspell_checkbtn);
188         gtk_box_pack_start(GTK_BOX(vbox2), enable_aspell_checkbtn, TRUE, TRUE, 0);
189
190         use_alternate_checkbtn = gtk_check_button_new_with_label(
191                         _("Enable alternate dictionary"));
192         gtk_widget_show(use_alternate_checkbtn);
193         gtk_box_pack_start(GTK_BOX(vbox2), use_alternate_checkbtn, TRUE, TRUE, 0);
194
195         tooltips = gtk_tooltips_new();
196         gtk_tooltips_set_tip(tooltips, use_alternate_checkbtn, 
197                         _("Faster switching with last used dictionary"), NULL);
198
199         PACK_FRAME(vbox1, path_frame, _("Dictionary path"));
200         aspell_path_hbox = gtk_hbox_new(FALSE, 8);
201         gtk_widget_show(aspell_path_hbox);
202         gtk_container_add(GTK_CONTAINER(path_frame), aspell_path_hbox);
203         gtk_container_set_border_width(GTK_CONTAINER(aspell_path_hbox), 8);     
204
205         aspell_path_entry = gtk_entry_new();
206         gtk_widget_show(aspell_path_entry);
207         gtk_box_pack_start(GTK_BOX(aspell_path_hbox), aspell_path_entry, TRUE, TRUE, 0);
208         gtk_widget_set_size_request(aspell_path_entry, 30, 20);
209
210         aspell_path_select = gtkut_get_browse_directory_btn(_("_Browse"));
211         gtk_widget_show(aspell_path_select);
212         gtk_box_pack_start(GTK_BOX(aspell_path_hbox), aspell_path_select, FALSE, FALSE, 0);
213
214         PACK_FRAME(vbox1, automatic_frame, _("Automatic spelling"));
215         vbox2 = gtk_vbox_new(FALSE, VSPACING_NARROW);
216         gtk_widget_show(vbox2);
217         gtk_container_add(GTK_CONTAINER(automatic_frame), vbox2);
218         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
219         
220         check_while_typing_checkbtn = gtk_check_button_new_with_label(
221                         _("Check while typing"));
222         gtk_widget_show(check_while_typing_checkbtn);
223         gtk_box_pack_start(GTK_BOX(vbox2), check_while_typing_checkbtn, TRUE, TRUE, 0);
224
225         recheck_when_changing_dict_checkbtn = gtk_check_button_new_with_label(
226                         _("Re-check message when changing dictionary"));
227         gtk_widget_show(recheck_when_changing_dict_checkbtn);
228         gtk_box_pack_start(GTK_BOX(vbox2), recheck_when_changing_dict_checkbtn, TRUE, TRUE, 0);
229         
230         PACK_FRAME(vbox1, dictionary_frame, _("Dictionary"));
231         vbox2 = gtk_vbox_new(FALSE, VSPACING_NARROW);
232         gtk_widget_show(vbox2);
233         gtk_container_add(GTK_CONTAINER(dictionary_frame), vbox2);
234         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
235         
236         hbox = gtk_hbox_new(FALSE, 10);
237         gtk_widget_show(hbox);
238         gtk_box_pack_start(GTK_BOX(vbox2), hbox, TRUE, TRUE, 0);
239         
240         default_dict_label = gtk_label_new(_("Default dictionary"));
241         gtk_widget_show(default_dict_label);
242         gtk_label_set_justify(GTK_LABEL(default_dict_label), GTK_JUSTIFY_LEFT);
243         gtk_misc_set_alignment(GTK_MISC(default_dict_label), 1, 0.5);
244         gtk_box_pack_start(GTK_BOX(hbox), default_dict_label, FALSE, FALSE, 0);
245         
246         label = gtk_label_new("");
247         gtk_widget_show(label);
248         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
249         
250         default_dict_optmenu = gtk_option_menu_new();
251         gtk_widget_show(default_dict_optmenu);
252         gtk_widget_set_size_request(default_dict_optmenu, 180, -1);
253
254         default_dict_optmenu_menu = gtk_menu_new();
255         gtk_option_menu_set_menu(GTK_OPTION_MENU(default_dict_optmenu),
256                         default_dict_optmenu_menu);
257         gtk_box_pack_start(GTK_BOX(hbox), default_dict_optmenu, FALSE, FALSE, 0);
258
259         hbox = gtk_hbox_new(FALSE, 10);
260         gtk_widget_show(hbox);
261         gtk_box_pack_start(GTK_BOX(vbox2), hbox, TRUE, TRUE, 0);
262         
263         sugmode_label = gtk_label_new(_("Default suggestion mode"));
264         gtk_widget_show(sugmode_label);
265         gtk_label_set_justify(GTK_LABEL(sugmode_label), GTK_JUSTIFY_LEFT);
266         gtk_misc_set_alignment(GTK_MISC(sugmode_label), 1, 0.5);
267         gtk_box_pack_start(GTK_BOX(hbox), sugmode_label, FALSE, FALSE, 0);
268
269         label = gtk_label_new("");
270         gtk_widget_show(label);
271         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
272
273         sugmode_optmenu = gtk_option_menu_new();
274         gtk_widget_show(sugmode_optmenu);
275         gtk_widget_set_size_request(sugmode_optmenu, 180, -1); 
276         
277         sugmode_optmenu_menu = gtk_menu_new();
278         gtk_option_menu_set_menu(GTK_OPTION_MENU(sugmode_optmenu),
279                         sugmode_optmenu_menu);
280         gtk_box_pack_start(GTK_BOX(hbox), sugmode_optmenu, FALSE, FALSE, 0);
281         
282         misspelled_hbox = gtk_hbox_new(FALSE, 10);
283         gtk_widget_show(misspelled_hbox);
284         gtk_box_pack_start(GTK_BOX(vbox1), misspelled_hbox, FALSE, FALSE, 0);
285                 
286         misspelled_label = gtk_label_new(_("Misspelled word color:"));
287         gtk_widget_show(misspelled_label);
288         gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_label,
289                 FALSE, FALSE, 0);
290         gtk_label_set_justify(GTK_LABEL(misspelled_label), GTK_JUSTIFY_RIGHT);
291         gtk_misc_set_alignment(GTK_MISC(misspelled_label), 1, 0.5);
292
293         misspelled_colorbtn = gtk_button_new_with_label("");
294         gtk_widget_show(misspelled_colorbtn);
295         gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_colorbtn,
296                 FALSE, FALSE, 0);
297         gtk_widget_set_size_request(misspelled_colorbtn, 30, 20);
298         tooltips = gtk_tooltips_new();
299         gtk_tooltips_set_tip(tooltips, misspelled_colorbtn,
300                              _("Pick color for misspelled word. "
301                                "Use black to underline"), NULL);
302
303         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_aspell_checkbtn),
304                         prefs_common.enable_aspell);
305         g_signal_connect(G_OBJECT(enable_aspell_checkbtn), "toggled",
306                          G_CALLBACK(prefs_spelling_checkbtn_enable_aspell_toggle_cb),
307                          prefs_spelling);
308         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_while_typing_checkbtn),
309                         prefs_common.check_while_typing);
310         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recheck_when_changing_dict_checkbtn),
311                         prefs_common.recheck_when_changing_dict);
312         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(use_alternate_checkbtn),
313                         prefs_common.use_alternate);
314         gtk_entry_set_text(GTK_ENTRY(aspell_path_entry), 
315                         SAFE_STRING(prefs_common.aspell_path));
316         g_signal_connect(G_OBJECT(aspell_path_select), "clicked", 
317                          G_CALLBACK(prefs_spelling_btn_aspell_path_clicked_cb),
318                          prefs_spelling);
319         gtk_option_menu_set_menu(GTK_OPTION_MENU(default_dict_optmenu),
320                                  gtkaspell_dictionary_option_menu_new(prefs_common.aspell_path));
321         gtkaspell_set_dictionary_menu_active_item(default_dict_optmenu, prefs_common.dictionary);
322         gtk_option_menu_set_menu(GTK_OPTION_MENU(sugmode_optmenu),
323                                  gtkaspell_sugmode_option_menu_new(prefs_common.aspell_sugmode));
324         gtkaspell_sugmode_option_menu_set(GTK_OPTION_MENU(sugmode_optmenu),
325                                           prefs_common.aspell_sugmode);
326         g_signal_connect(G_OBJECT(misspelled_colorbtn), "clicked",
327                          G_CALLBACK(prefs_spelling_colorsel), prefs_spelling);
328
329         prefs_spelling->misspell_col = prefs_common.misspelled_col;
330         gtkut_set_widget_bgcolor_rgb(misspelled_colorbtn, prefs_spelling->misspell_col);
331
332         prefs_spelling->window                  = GTK_WIDGET(window);
333         prefs_spelling->automatic_frame =       automatic_frame;
334         prefs_spelling->dictionary_frame =      dictionary_frame;
335         prefs_spelling->path_frame =    path_frame;
336         prefs_spelling->enable_aspell_checkbtn  = enable_aspell_checkbtn;
337         prefs_spelling->check_while_typing_checkbtn
338                 = check_while_typing_checkbtn;
339         prefs_spelling->recheck_when_changing_dict_checkbtn
340                 = recheck_when_changing_dict_checkbtn;
341         prefs_spelling->use_alternate_checkbtn  = use_alternate_checkbtn;
342         prefs_spelling->aspell_path_entry       = aspell_path_entry;
343         prefs_spelling->aspell_path_select      = aspell_path_select;
344         prefs_spelling->default_dict_label      = default_dict_label;
345         prefs_spelling->default_dict_optmenu    = default_dict_optmenu;
346         prefs_spelling->default_dict_optmenu_menu
347                 = default_dict_optmenu_menu;
348         prefs_spelling->sugmode_label           = sugmode_label;
349         prefs_spelling->sugmode_optmenu         = sugmode_optmenu;
350         prefs_spelling->sugmode_optmenu_menu    = sugmode_optmenu_menu;
351         prefs_spelling->misspelled_label        = misspelled_label;
352         prefs_spelling->misspelled_colorbtn     = misspelled_colorbtn;
353
354         prefs_spelling->page.widget = vbox1;
355
356         prefs_spelling_enable(prefs_spelling, prefs_common.enable_aspell);
357 }
358
359 void prefs_spelling_save(PrefsPage *_page)
360 {
361         SpellingPage *spelling = (SpellingPage *) _page;
362
363         prefs_common.enable_aspell =
364                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->enable_aspell_checkbtn));
365         prefs_common.check_while_typing =
366                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->check_while_typing_checkbtn));
367         prefs_common.recheck_when_changing_dict =
368                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->recheck_when_changing_dict_checkbtn));
369         prefs_common.use_alternate =
370                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->use_alternate_checkbtn));
371
372         g_free(prefs_common.aspell_path);
373         prefs_common.aspell_path =
374                 gtk_editable_get_chars(GTK_EDITABLE(spelling->aspell_path_entry), 0, -1);
375
376         g_free(prefs_common.dictionary);
377         prefs_common.dictionary = 
378                 gtkaspell_get_dictionary_menu_active_item(
379                         gtk_option_menu_get_menu(
380                                 GTK_OPTION_MENU(
381                                         spelling->default_dict_optmenu)));
382
383         prefs_common.aspell_sugmode =
384                 gtkaspell_get_sugmode_from_option_menu(
385                         GTK_OPTION_MENU(spelling->sugmode_optmenu));
386
387         prefs_common.misspelled_col = spelling->misspell_col;
388 }
389
390 static void prefs_spelling_destroy_widget(PrefsPage *_page)
391 {
392         /* SpellingPage *spelling = (SpellingPage *) _page; */
393
394 }
395
396 SpellingPage *prefs_spelling;
397
398 void prefs_spelling_init(void)
399 {
400         SpellingPage *page;
401         static gchar *path[3];
402         const gchar* language = NULL;
403         
404         path[0] = _("Compose");
405         path[1] = _("Spell Checking");
406         path[2] = NULL;
407
408         page = g_new0(SpellingPage, 1);
409         page->page.path = path;
410         page->page.create_widget = prefs_spelling_create_widget;
411         page->page.destroy_widget = prefs_spelling_destroy_widget;
412         page->page.save_page = prefs_spelling_save;
413         page->page.weight = 180.0;
414
415         prefs_gtk_register_page((PrefsPage *) page);
416         prefs_spelling = page;
417         
418         language = g_getenv("LANG");
419         if (language == NULL)
420                 language = "en";
421         else if (!strcmp(language, "POSIX") || !strcmp(language, "C"))
422                 language = "en";
423         
424         if (!prefs_common.dictionary)
425                 prefs_common.dictionary = g_strdup_printf("%s%s",
426                                                 prefs_common.aspell_path,
427                                                 language);
428         if (!strlen(prefs_common.dictionary)
429         ||  !strcmp(prefs_common.dictionary,"(None"))
430                 prefs_common.dictionary = g_strdup_printf("%s%s",
431                                                 prefs_common.aspell_path,
432                                                 language);
433         if (strcasestr(prefs_common.dictionary,".utf"))
434                 *(strcasestr(prefs_common.dictionary,".utf")) = '\0';
435         if (strstr(prefs_common.dictionary,"@"))
436                 *(strstr(prefs_common.dictionary,"@")) = '\0';
437 }
438
439 void prefs_spelling_done(void)
440 {
441         prefs_gtk_unregister_page((PrefsPage *) prefs_spelling);
442         g_free(prefs_spelling);
443 }
444
445 #endif /* USE_ASPELL */