2005-11-18 [cleroy] 1.9.100cvs18
[claws.git] / src / prefs_spelling.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 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         PangoFontDescription *font_desc;
156         gint size;
157
158         tooltips = gtk_tooltips_new ();
159
160         table = gtk_table_new(8, 3, FALSE);
161         gtk_widget_show(table);
162         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
163         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
164         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
165
166         checkbtn_enable_aspell =
167             gtk_check_button_new_with_label(_("Enable spell checker"));
168         gtk_widget_show(checkbtn_enable_aspell);
169         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_aspell, 0, 3, 0,
170                          1, (GtkAttachOptions) (GTK_FILL),
171                          (GtkAttachOptions) (0), 0, 0);
172
173         checkbtn_check_while_typing =
174             gtk_check_button_new_with_label(_("Check while typing"));
175         gtk_widget_show(checkbtn_check_while_typing);
176         gtk_table_attach(GTK_TABLE(table), checkbtn_check_while_typing, 0,
177                          3, 1, 2, (GtkAttachOptions) (GTK_FILL),
178                          (GtkAttachOptions) (0), 0, 0);
179
180         checkbtn_use_alternate =
181             gtk_check_button_new_with_label(_
182                                             ("Enable alternate dictionary"));
183         gtk_widget_show(checkbtn_use_alternate);
184         gtk_table_attach(GTK_TABLE(table), checkbtn_use_alternate, 0, 3, 2,
185                          3, (GtkAttachOptions) (GTK_FILL),
186                          (GtkAttachOptions) (0), 0, 0);
187         gtk_tooltips_set_tip (tooltips, checkbtn_use_alternate, 
188                         _("Faster switching with last used dictionary"), NULL);
189
190         label2 = gtk_label_new(_("Dictionaries path:"));
191         gtk_widget_show(label2);
192         gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 4, 5,
193                          (GtkAttachOptions) (GTK_FILL),
194                          (GtkAttachOptions) (0), 0, 0);
195         gtk_label_set_justify(GTK_LABEL(label2), GTK_JUSTIFY_RIGHT);
196         gtk_misc_set_alignment(GTK_MISC(label2), 1, 0.5);
197
198         entry_aspell_path = gtk_entry_new();
199         gtk_widget_show(entry_aspell_path);
200         gtk_table_attach(GTK_TABLE(table), entry_aspell_path, 1, 2, 4, 5,
201                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
202                          (GtkAttachOptions) (0), 0, 0);
203
204         label3 = gtk_label_new(_("Default dictionary:"));
205         gtk_widget_show(label3);
206         gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 5, 6,
207                          (GtkAttachOptions) (GTK_FILL),
208                          (GtkAttachOptions) (0), 0, 0);
209         gtk_label_set_justify(GTK_LABEL(label3), GTK_JUSTIFY_RIGHT);
210         gtk_misc_set_alignment(GTK_MISC(label3), 1, 0.5);
211
212         optmenu_dictionary = gtk_option_menu_new();
213         gtk_widget_show(optmenu_dictionary);
214         gtk_table_attach(GTK_TABLE(table), optmenu_dictionary, 1, 3, 5, 6,
215                          (GtkAttachOptions) (GTK_FILL),
216                          (GtkAttachOptions) (0), 0, 0);
217         optmenu_dictionary_menu = gtk_menu_new();
218         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_dictionary),
219                                  optmenu_dictionary_menu);
220
221         label4 = gtk_label_new(_("Default suggestion mode:"));
222         gtk_widget_show(label4);
223         gtk_table_attach(GTK_TABLE(table), label4, 0, 1, 6, 7,
224                          (GtkAttachOptions) (GTK_FILL),
225                          (GtkAttachOptions) (0), 0, 0);
226         gtk_label_set_justify(GTK_LABEL(label4), GTK_JUSTIFY_RIGHT);
227         gtk_misc_set_alignment(GTK_MISC(label4), 1, 0.5);
228
229         optmenu_sugmode = gtk_option_menu_new();
230         gtk_widget_show(optmenu_sugmode);
231         gtk_table_attach(GTK_TABLE(table), optmenu_sugmode, 1, 3, 6, 7,
232                          (GtkAttachOptions) (GTK_FILL),
233                          (GtkAttachOptions) (0), 0, 0);
234         optmenu_sugmode_menu = gtk_menu_new();
235         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_sugmode),
236                                  optmenu_sugmode_menu);
237
238         label5 = gtk_label_new(_("Misspelled word color:"));
239         gtk_widget_show(label5);
240         gtk_table_attach(GTK_TABLE(table), label5, 0, 1, 7, 8,
241                          (GtkAttachOptions) (GTK_FILL),
242                          (GtkAttachOptions) (0), 0, 0);
243         gtk_label_set_justify(GTK_LABEL(label5), GTK_JUSTIFY_RIGHT);
244         gtk_misc_set_alignment(GTK_MISC(label5), 1, 0.5);
245
246         btn_aspell_path = gtkut_get_browse_directory_btn(_("_Browse"));
247         gtk_widget_show(btn_aspell_path);
248         gtk_table_attach(GTK_TABLE(table), btn_aspell_path, 2, 3, 4, 5,
249                          (GtkAttachOptions) (GTK_FILL),
250                          (GtkAttachOptions) (0), 0, 0);
251
252         hbox1 = gtk_hbox_new(FALSE, 0);
253         gtk_widget_show(hbox1);
254         gtk_table_attach(GTK_TABLE(table), hbox1, 1, 2, 7, 8,
255                          (GtkAttachOptions) (GTK_FILL),
256                          (GtkAttachOptions) (GTK_FILL), 0, 0);
257
258         misspelled_btn = gtk_button_new_with_label("");
259         gtk_widget_show(misspelled_btn);
260         gtk_box_pack_start(GTK_BOX(hbox1), misspelled_btn, FALSE, FALSE,
261                            0);
262         gtk_widget_set_size_request(misspelled_btn, 30, 20);
263         label5 = gtk_label_new(_("(Black to use underline)"));
264         gtk_misc_set_alignment(GTK_MISC(label5), 0, 0.5);
265         gtk_label_set_justify(GTK_LABEL(label4), GTK_JUSTIFY_LEFT);
266         gtk_widget_show(label5);
267         font_desc = pango_font_description_new();
268         size = pango_font_description_get_size
269                 (label5->style->font_desc);
270         pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
271         gtk_widget_modify_font(label5, font_desc);
272         pango_font_description_free(font_desc);
273         gtk_box_pack_start(GTK_BOX(hbox1), label5, FALSE, FALSE,
274                            4);
275         /* END GLADE CODE */
276
277         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_enable_aspell),
278                                      prefs_common.enable_aspell);
279         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_check_while_typing),
280                                      prefs_common.check_while_typing);
281         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_alternate),
282                                      prefs_common.use_alternate);
283         gtk_entry_set_text(GTK_ENTRY(entry_aspell_path), 
284                            SAFE_STRING(prefs_common.aspell_path));
285
286         g_signal_connect(G_OBJECT(checkbtn_enable_aspell), "toggled",
287                          G_CALLBACK(prefs_spelling_checkbtn_enable_aspell_toggle_cb),
288                          prefs_spelling);
289         g_signal_connect(G_OBJECT(btn_aspell_path), "clicked", 
290                          G_CALLBACK(prefs_spelling_btn_aspell_path_clicked_cb),
291                          prefs_spelling);
292         g_signal_connect(G_OBJECT(misspelled_btn), "clicked",
293                          G_CALLBACK(prefs_spelling_colorsel), prefs_spelling);
294
295         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_dictionary),
296                                  gtkaspell_dictionary_option_menu_new(prefs_common.aspell_path));
297         gtkaspell_set_dictionary_menu_active_item(optmenu_dictionary, prefs_common.dictionary);
298
299         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_sugmode),
300                                  gtkaspell_sugmode_option_menu_new(prefs_common.aspell_sugmode));
301         gtkaspell_sugmode_option_menu_set(GTK_OPTION_MENU(optmenu_sugmode),
302                                           prefs_common.aspell_sugmode);
303
304         prefs_spelling->misspell_col = prefs_common.misspelled_col;
305         gtkut_set_widget_bgcolor_rgb(misspelled_btn, prefs_spelling->misspell_col);
306
307         prefs_spelling->window
308                 = GTK_WIDGET(window);
309         prefs_spelling->checkbtn_enable_aspell 
310                 = checkbtn_enable_aspell;
311         prefs_spelling->entry_aspell_path
312                 = entry_aspell_path;
313         prefs_spelling->btn_aspell_path
314                 = btn_aspell_path;
315         prefs_spelling->optmenu_dictionary
316                 = optmenu_dictionary;
317         prefs_spelling->optmenu_sugmode
318                 = optmenu_sugmode;
319         prefs_spelling->checkbtn_use_alternate
320                 = checkbtn_use_alternate;
321         prefs_spelling->checkbtn_check_while_typing
322                 = checkbtn_check_while_typing;
323         prefs_spelling->misspelled_btn
324                 = misspelled_btn;
325
326         prefs_spelling->page.widget = table;
327
328         prefs_spelling_enable(prefs_spelling, prefs_common.enable_aspell);
329 }
330
331 void prefs_spelling_save(PrefsPage *_page)
332 {
333         SpellingPage *spelling = (SpellingPage *) _page;
334
335         prefs_common.enable_aspell =
336                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_enable_aspell));
337         prefs_common.check_while_typing =
338                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_check_while_typing));
339         prefs_common.use_alternate =
340                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_use_alternate));
341
342         if (prefs_common.aspell_path)
343                 g_free(prefs_common.aspell_path);
344         prefs_common.aspell_path =
345                 gtk_editable_get_chars(GTK_EDITABLE(spelling->entry_aspell_path), 0, -1);
346
347         if (prefs_common.dictionary != NULL)
348                 g_free(prefs_common.dictionary);
349         prefs_common.dictionary = 
350                 gtkaspell_get_dictionary_menu_active_item(
351                         gtk_option_menu_get_menu(
352                                 GTK_OPTION_MENU(
353                                         spelling->optmenu_dictionary)));
354
355         prefs_common.aspell_sugmode =
356                 gtkaspell_get_sugmode_from_option_menu(
357                         GTK_OPTION_MENU(spelling->optmenu_sugmode));
358
359         prefs_common.misspelled_col = spelling->misspell_col;
360 }
361
362 static void prefs_spelling_destroy_widget(PrefsPage *_page)
363 {
364         /* SpellingPage *spelling = (SpellingPage *) _page; */
365
366 }
367
368 SpellingPage *prefs_spelling;
369
370 void prefs_spelling_init(void)
371 {
372         SpellingPage *page;
373         static gchar *path[3];
374         const gchar* language = NULL;
375         
376         path[0] = _("Compose");
377         path[1] = _("Spell Checking");
378         path[2] = NULL;
379
380         page = g_new0(SpellingPage, 1);
381         page->page.path = path;
382         page->page.create_widget = prefs_spelling_create_widget;
383         page->page.destroy_widget = prefs_spelling_destroy_widget;
384         page->page.save_page = prefs_spelling_save;
385         page->page.weight = 180.0;
386
387         prefs_gtk_register_page((PrefsPage *) page);
388         prefs_spelling = page;
389         
390         language = g_getenv("LANG");
391         if (language == NULL)
392                 language = "en";
393         else if (!strcmp(language, "POSIX") || !strcmp(language, "C"))
394                 language = "en";
395         
396         if (!prefs_common.dictionary)
397                 prefs_common.dictionary = g_strdup_printf("%s%s",
398                                                 prefs_common.aspell_path,
399                                                 language);
400         if (!strlen(prefs_common.dictionary)
401         ||  !strcmp(prefs_common.dictionary,"(None"))
402                 prefs_common.dictionary = g_strdup_printf("%s%s",
403                                                 prefs_common.aspell_path,
404                                                 language);
405         if (strcasestr(prefs_common.dictionary,".utf"))
406                 *(strcasestr(prefs_common.dictionary,".utf")) = '\0';
407         if (strstr(prefs_common.dictionary,"@"))
408                 *(strstr(prefs_common.dictionary,"@")) = '\0';
409 }
410
411 void prefs_spelling_done(void)
412 {
413         prefs_gtk_unregister_page((PrefsPage *) prefs_spelling);
414         g_free(prefs_spelling);
415 }
416
417 #endif /* USE_ASPELL */