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