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