5d43478b677288ed6b7dd2cb14dc919af6458985
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #include "defs.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <glib.h>
34 #include <gtk/gtk.h>
35
36 #include "intl.h"
37 #include "utils.h"
38 #include "prefs_common.h"
39 #include "prefs_gtk.h"
40
41 #include "gtk/gtkutils.h"
42 #include "gtk/prefswindow.h"
43 #include "gtk/filesel.h"
44 #include "gtk/colorsel.h"
45
46 typedef struct _SpellingPage
47 {
48         PrefsPage page;
49
50         GtkWidget *window;              /* do not modify */
51
52         GtkWidget *checkbtn_enable_aspell;
53         GtkWidget *entry_aspell_path;
54         GtkWidget *btn_aspell_path;
55         GtkWidget *optmenu_dictionary;
56         GtkWidget *optmenu_sugmode;
57         GtkWidget *misspelled_btn;
58         GtkWidget *checkbtn_use_alternate;
59         GtkWidget *checkbtn_check_while_typing;
60
61         gint       misspell_col;
62 } SpellingPage;
63
64 static void prefs_spelling_enable(SpellingPage *spelling, gboolean enable)
65 {
66         gtk_widget_set_sensitive(spelling->entry_aspell_path,           enable);
67         gtk_widget_set_sensitive(spelling->optmenu_dictionary,          enable);
68         gtk_widget_set_sensitive(spelling->optmenu_sugmode,             enable);
69         gtk_widget_set_sensitive(spelling->btn_aspell_path,             enable);
70         gtk_widget_set_sensitive(spelling->misspelled_btn,              enable);
71         gtk_widget_set_sensitive(spelling->checkbtn_use_alternate,      enable);
72         gtk_widget_set_sensitive(spelling->checkbtn_check_while_typing, enable);
73 }
74
75 static void prefs_spelling_checkbtn_enable_aspell_toggle_cb
76         (GtkWidget *widget,
77          gpointer data)
78 {
79         SpellingPage *spelling = (SpellingPage *) data;
80         gboolean toggled;
81
82         toggled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
83         prefs_spelling_enable(spelling, toggled);
84 }
85
86 static void prefs_spelling_btn_aspell_path_clicked_cb(GtkWidget *widget,
87                                                      gpointer data)
88 {
89         SpellingPage *spelling = (SpellingPage *) data;
90         gchar *file_path;
91         GtkWidget *new_menu;
92
93         file_path = filesel_select_file(_("Select dictionaries location"),
94                                         prefs_common.aspell_path);
95         if (file_path != NULL) {
96                 gchar *tmp_path, *tmp;
97
98                 tmp_path = g_dirname(file_path);
99                 tmp = g_strdup_printf("%s%s", tmp_path, G_DIR_SEPARATOR_S);
100                 g_free(tmp_path);
101
102                 new_menu = gtkaspell_dictionary_option_menu_new(tmp);
103                 gtk_option_menu_set_menu(GTK_OPTION_MENU(spelling->optmenu_dictionary),
104                                          new_menu);
105
106                 gtk_entry_set_text(GTK_ENTRY(spelling->entry_aspell_path), tmp);
107                 /* select first one */
108                 gtk_option_menu_set_history(GTK_OPTION_MENU(
109                                         spelling->optmenu_dictionary), 0);
110         
111                 g_free(tmp);
112
113         }
114 }
115
116 static void prefs_spelling_colorsel(GtkWidget *widget,
117                                     gpointer data)
118 {
119         SpellingPage *spelling = (SpellingPage *) data;
120         gint rgbcolor;
121
122         rgbcolor = colorsel_select_color_rgb(_("Pick color for misspelled word"), 
123                                              spelling->misspell_col);
124         gtkut_set_widget_bgcolor_rgb(spelling->misspelled_btn, rgbcolor);
125         spelling->misspell_col = rgbcolor;
126 }
127
128 #define SAFE_STRING(str) \
129         (str) ? (str) : ""
130
131 void prefs_spelling_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
132 {
133         SpellingPage *prefs_spelling = (SpellingPage *) _page;
134         GList *cur;
135         gint n;
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 *label1;
143         GtkWidget *label2;
144         GtkWidget *entry_aspell_path;
145         GtkWidget *label3;
146         GtkWidget *optmenu_dictionary;
147         GtkWidget *optmenu_dictionary_menu;
148         GtkWidget *label4;
149         GtkWidget *optmenu_sugmode;
150         GtkWidget *optmenu_sugmode_menu;
151         GtkWidget *label5;
152         GtkWidget *btn_aspell_path;
153         GtkWidget *hbox1;
154         GtkWidget *misspelled_btn;
155
156         table = gtk_table_new(8, 3, FALSE);
157         gtk_widget_show(table);
158         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
159         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
160         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
161
162         checkbtn_enable_aspell =
163             gtk_check_button_new_with_label(_("Enable spell checker"));
164         gtk_widget_show(checkbtn_enable_aspell);
165         gtk_table_attach(GTK_TABLE(table), checkbtn_enable_aspell, 0, 3, 0,
166                          1, (GtkAttachOptions) (GTK_FILL),
167                          (GtkAttachOptions) (0), 0, 0);
168
169         checkbtn_check_while_typing =
170             gtk_check_button_new_with_label(_("Check while typing"));
171         gtk_widget_show(checkbtn_check_while_typing);
172         gtk_table_attach(GTK_TABLE(table), checkbtn_check_while_typing, 0,
173                          3, 1, 2, (GtkAttachOptions) (GTK_FILL),
174                          (GtkAttachOptions) (0), 0, 0);
175
176         checkbtn_use_alternate =
177             gtk_check_button_new_with_label(_
178                                             ("Enable alternate dictionary"));
179         gtk_widget_show(checkbtn_use_alternate);
180         gtk_table_attach(GTK_TABLE(table), checkbtn_use_alternate, 0, 3, 2,
181                          3, (GtkAttachOptions) (GTK_FILL),
182                          (GtkAttachOptions) (0), 0, 0);
183
184         label1 =
185             gtk_label_new(_
186                           ("Enabling an alternate dictionary makes switching\nwith the last used dictionary faster"));
187         gtk_widget_show(label1);
188         gtk_table_attach(GTK_TABLE(table), label1, 0, 3, 3, 4,
189                          (GtkAttachOptions) (GTK_FILL),
190                          (GtkAttachOptions) (0), 0, 0);
191         gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
192
193         label2 = gtk_label_new(_("Dictionaries path:"));
194         gtk_widget_show(label2);
195         gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 4, 5,
196                          (GtkAttachOptions) (GTK_FILL),
197                          (GtkAttachOptions) (0), 0, 0);
198         gtk_label_set_justify(GTK_LABEL(label2), GTK_JUSTIFY_RIGHT);
199         gtk_misc_set_alignment(GTK_MISC(label2), 1, 0.5);
200
201         entry_aspell_path = gtk_entry_new();
202         gtk_widget_show(entry_aspell_path);
203         gtk_table_attach(GTK_TABLE(table), entry_aspell_path, 1, 2, 4, 5,
204                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
205                          (GtkAttachOptions) (0), 0, 0);
206
207         label3 = gtk_label_new(_("Default dictionary:"));
208         gtk_widget_show(label3);
209         gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 5, 6,
210                          (GtkAttachOptions) (GTK_FILL),
211                          (GtkAttachOptions) (0), 0, 0);
212         gtk_label_set_justify(GTK_LABEL(label3), GTK_JUSTIFY_RIGHT);
213         gtk_misc_set_alignment(GTK_MISC(label3), 1, 0.5);
214
215         optmenu_dictionary = gtk_option_menu_new();
216         gtk_widget_show(optmenu_dictionary);
217         gtk_table_attach(GTK_TABLE(table), optmenu_dictionary, 1, 3, 5, 6,
218                          (GtkAttachOptions) (GTK_FILL),
219                          (GtkAttachOptions) (0), 0, 0);
220         optmenu_dictionary_menu = gtk_menu_new();
221         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_dictionary),
222                                  optmenu_dictionary_menu);
223
224         label4 = gtk_label_new(_("Default suggestion mode:"));
225         gtk_widget_show(label4);
226         gtk_table_attach(GTK_TABLE(table), label4, 0, 1, 6, 7,
227                          (GtkAttachOptions) (GTK_FILL),
228                          (GtkAttachOptions) (0), 0, 0);
229         gtk_label_set_justify(GTK_LABEL(label4), GTK_JUSTIFY_RIGHT);
230         gtk_misc_set_alignment(GTK_MISC(label4), 1, 0.5);
231
232         optmenu_sugmode = gtk_option_menu_new();
233         gtk_widget_show(optmenu_sugmode);
234         gtk_table_attach(GTK_TABLE(table), optmenu_sugmode, 1, 3, 6, 7,
235                          (GtkAttachOptions) (GTK_FILL),
236                          (GtkAttachOptions) (0), 0, 0);
237         optmenu_sugmode_menu = gtk_menu_new();
238         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_sugmode),
239                                  optmenu_sugmode_menu);
240
241         label5 = gtk_label_new(_("Misspelled word color:"));
242         gtk_widget_show(label5);
243         gtk_table_attach(GTK_TABLE(table), label5, 0, 1, 7, 8,
244                          (GtkAttachOptions) (GTK_FILL),
245                          (GtkAttachOptions) (0), 0, 0);
246         gtk_label_set_justify(GTK_LABEL(label5), GTK_JUSTIFY_RIGHT);
247         gtk_misc_set_alignment(GTK_MISC(label5), 1, 0.5);
248
249         btn_aspell_path = gtk_button_new_with_label(_(" ... "));
250         gtk_widget_show(btn_aspell_path);
251         gtk_table_attach(GTK_TABLE(table), btn_aspell_path, 2, 3, 4, 5,
252                          (GtkAttachOptions) (GTK_FILL),
253                          (GtkAttachOptions) (0), 0, 0);
254
255         hbox1 = gtk_hbox_new(FALSE, 0);
256         gtk_widget_show(hbox1);
257         gtk_table_attach(GTK_TABLE(table), hbox1, 1, 2, 7, 8,
258                          (GtkAttachOptions) (GTK_FILL),
259                          (GtkAttachOptions) (GTK_FILL), 0, 0);
260
261         misspelled_btn = gtk_button_new_with_label("");
262         gtk_widget_show(misspelled_btn);
263         gtk_box_pack_start(GTK_BOX(hbox1), misspelled_btn, FALSE, FALSE,
264                            0);
265         gtk_widget_set_usize(misspelled_btn, 30, 20);
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         gtk_signal_connect(GTK_OBJECT(checkbtn_enable_aspell), "toggled",
278                            GTK_SIGNAL_FUNC(prefs_spelling_checkbtn_enable_aspell_toggle_cb),
279                            prefs_spelling);
280         gtk_signal_connect(GTK_OBJECT(btn_aspell_path), "clicked", 
281                            GTK_SIGNAL_FUNC(prefs_spelling_btn_aspell_path_clicked_cb),
282                            prefs_spelling);
283         gtk_signal_connect(GTK_OBJECT(misspelled_btn), "clicked",
284                            GTK_SIGNAL_FUNC(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         n = 0;
289         for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu_dictionary)))->children;
290              cur != NULL; cur = cur->next) {
291                 GtkWidget *menuitem;
292                 gchar *dict_name;
293
294                 menuitem = GTK_WIDGET(cur->data);
295                 dict_name = gtk_object_get_data(GTK_OBJECT(menuitem), 
296                                                 "dict_name");
297                 if (!strcmp2(dict_name, prefs_common.dictionary)) {
298                         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_dictionary), n);
299                 }
300                 n++;
301         }
302
303         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu_sugmode),
304                                  gtkaspell_sugmode_option_menu_new(prefs_common.aspell_sugmode));
305         gtkaspell_sugmode_option_menu_set(GTK_OPTION_MENU(optmenu_sugmode),
306                                           prefs_common.aspell_sugmode);
307
308         prefs_spelling->misspell_col = prefs_common.misspelled_col;
309         gtkut_set_widget_bgcolor_rgb(misspelled_btn, prefs_spelling->misspell_col);
310
311         prefs_spelling->window
312                 = GTK_WIDGET(window);
313         prefs_spelling->checkbtn_enable_aspell 
314                 = checkbtn_enable_aspell;
315         prefs_spelling->entry_aspell_path
316                 = entry_aspell_path;
317         prefs_spelling->btn_aspell_path
318                 = btn_aspell_path;
319         prefs_spelling->optmenu_dictionary
320                 = optmenu_dictionary;
321         prefs_spelling->optmenu_sugmode
322                 = optmenu_sugmode;
323         prefs_spelling->checkbtn_use_alternate
324                 = checkbtn_use_alternate;
325         prefs_spelling->checkbtn_check_while_typing
326                 = checkbtn_check_while_typing;
327         prefs_spelling->misspelled_btn
328                 = misspelled_btn;
329
330         prefs_spelling->page.widget = table;
331
332         prefs_spelling_enable(prefs_spelling, prefs_common.enable_aspell);
333 }
334
335 void prefs_spelling_save(PrefsPage *_page)
336 {
337         SpellingPage *spelling = (SpellingPage *) _page;
338
339         prefs_common.enable_aspell =
340                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_enable_aspell));
341         prefs_common.check_while_typing =
342                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_check_while_typing));
343         prefs_common.use_alternate =
344                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->checkbtn_use_alternate));
345
346         if (prefs_common.aspell_path)
347                 g_free(prefs_common.aspell_path);
348         prefs_common.aspell_path =
349                 gtk_editable_get_chars(GTK_EDITABLE(spelling->entry_aspell_path), 0, -1);
350
351         if (prefs_common.dictionary != NULL)
352                 g_free(prefs_common.dictionary);
353         prefs_common.dictionary = 
354                 gtkaspell_get_dictionary_menu_active_item(
355                         gtk_option_menu_get_menu(
356                                 GTK_OPTION_MENU(
357                                         spelling->optmenu_dictionary)));
358
359         prefs_common.aspell_sugmode =
360                 gtkaspell_get_sugmode_from_option_menu(
361                         GTK_OPTION_MENU(spelling->optmenu_sugmode));
362
363         prefs_common.misspelled_col = spelling->misspell_col;
364 }
365
366 static void prefs_spelling_destroy_widget(PrefsPage *_page)
367 {
368         /* SpellingPage *spelling = (SpellingPage *) _page; */
369
370 }
371
372 SpellingPage *prefs_spelling;
373
374 void prefs_spelling_init(void)
375 {
376         SpellingPage *page;
377
378         page = g_new0(SpellingPage, 1);
379         page->page.path = _("Compose/Spell Checker");
380         page->page.create_widget = prefs_spelling_create_widget;
381         page->page.destroy_widget = prefs_spelling_destroy_widget;
382         page->page.save_page = prefs_spelling_save;
383         page->page.weight = 50.0;
384         prefs_gtk_register_page((PrefsPage *) page);
385         prefs_spelling = page;
386 }
387
388 void prefs_spelling_done(void)
389 {
390         prefs_gtk_unregister_page((PrefsPage *) prefs_spelling);
391         g_free(prefs_spelling);
392 }