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