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