Python 2 EOL: migrate vcard2xml.py to Python 3
[claws.git] / src / prefs_spelling.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2017 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 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #if USE_ENCHANT
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         
54         GtkWidget *enable_aspell_checkbtn;
55         GtkWidget *recheck_when_changing_dict_checkbtn;
56         GtkWidget *check_while_typing_checkbtn;
57         GtkWidget *use_alternate_checkbtn;
58
59         GtkWidget *default_dict_label;
60         GtkWidget *default_dict_combo;
61
62         GtkWidget *default_alt_dict_label;
63         GtkWidget *default_alt_dict_combo;
64
65         GtkWidget *both_dict_check;
66
67         GtkWidget *misspelled_label;
68         GtkWidget *misspelled_colorbtn;
69         GtkWidget *misspelled_useblack_label;
70
71         gint       misspell_col;
72 } SpellingPage;
73
74 static void prefs_spelling_colorsel(GtkWidget *widget,
75                                     gpointer data)
76 {
77         SpellingPage *spelling = (SpellingPage *) data;
78         gint rgbcolor;
79
80         rgbcolor = colorsel_select_color_rgb(_("Pick color for misspelled word"), 
81                                              spelling->misspell_col);
82         gtkut_set_widget_bgcolor_rgb(spelling->misspelled_colorbtn, rgbcolor);
83         spelling->misspell_col = rgbcolor;
84 }
85
86 #define SAFE_STRING(str) \
87         (str) ? (str) : ""
88
89 static void prefs_spelling_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
90 {
91         SpellingPage *prefs_spelling = (SpellingPage *) _page;
92
93         GtkWidget *vbox1, *vbox2;
94
95         GtkWidget *enable_aspell_checkbtn;
96         GtkWidget *check_while_typing_checkbtn;
97         GtkWidget *recheck_when_changing_dict_checkbtn;
98         GtkWidget *use_alternate_checkbtn;
99
100         GtkWidget *automatic_frame;
101         GtkWidget *dictionary_frame;
102
103         GtkWidget *table;
104
105         GtkWidget *default_dict_label;
106         GtkWidget *default_dict_combo;
107
108         GtkWidget *default_alt_dict_label;
109         GtkWidget *default_alt_dict_combo;
110         GtkWidget *both_dict_check;
111 #ifdef WIN32
112         GtkWidget *get_dictionaries_btn;
113 #endif
114
115         GtkWidget *misspelled_label;
116         GtkWidget *misspelled_hbox;
117         GtkWidget *misspelled_colorbtn;
118
119         vbox1 = gtk_vbox_new (FALSE, VSPACING);
120         gtk_widget_show (vbox1);
121         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
122
123         vbox2 = gtk_vbox_new (FALSE, 0);
124         gtk_widget_show (vbox2);
125         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
126
127         enable_aspell_checkbtn = gtk_check_button_new_with_label(
128                         _("Enable spell checker"));
129         gtk_widget_show(enable_aspell_checkbtn);
130         gtk_box_pack_start(GTK_BOX(vbox2), enable_aspell_checkbtn, TRUE, TRUE, 0);
131
132         use_alternate_checkbtn = gtk_check_button_new_with_label(
133                         _("Enable alternate dictionary"));
134         gtk_widget_show(use_alternate_checkbtn);
135         gtk_box_pack_start(GTK_BOX(vbox2), use_alternate_checkbtn, TRUE, TRUE, 0);
136
137         CLAWS_SET_TIP(use_alternate_checkbtn, 
138                         _("Faster switching with last used dictionary"));
139
140         vbox2 = gtkut_get_options_frame(vbox1, &automatic_frame, _("Automatic spell checking"));
141         
142         check_while_typing_checkbtn = gtk_check_button_new_with_label(
143                         _("Check while typing"));
144         gtk_widget_show(check_while_typing_checkbtn);
145         gtk_box_pack_start(GTK_BOX(vbox2), check_while_typing_checkbtn, TRUE, TRUE, 0);
146
147         recheck_when_changing_dict_checkbtn = gtk_check_button_new_with_label(
148                         _("Re-check message when changing dictionary"));
149         gtk_widget_show(recheck_when_changing_dict_checkbtn);
150         gtk_box_pack_start(GTK_BOX(vbox2), recheck_when_changing_dict_checkbtn, TRUE, TRUE, 0);
151         
152         vbox2 = gtkut_get_options_frame(vbox1, &dictionary_frame, _("Dictionary"));
153         
154         table = gtk_table_new(6, 4, FALSE);
155         gtk_widget_show(table);
156         gtk_container_set_border_width(GTK_CONTAINER(table), 0);
157         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
158         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
159
160         gtk_box_pack_start(GTK_BOX(vbox2), table, TRUE, TRUE, 0);
161
162         default_dict_label = gtk_label_new(_("Default dictionary"));
163         gtk_widget_show(default_dict_label);
164         gtk_table_attach(GTK_TABLE (table), default_dict_label, 0, 1, 0, 1,
165                          (GtkAttachOptions) (GTK_FILL),
166                          (GtkAttachOptions) (0), 0, 2);
167         gtk_label_set_justify(GTK_LABEL(default_dict_label), GTK_JUSTIFY_RIGHT);
168         gtk_misc_set_alignment(GTK_MISC(default_dict_label), 1, 0.5);
169         
170         default_dict_combo = gtkaspell_dictionary_combo_new(TRUE);
171         gtk_table_attach (GTK_TABLE (table), default_dict_combo, 1, 2, 0, 1,
172                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
173
174         default_alt_dict_label = gtk_label_new(_("Default alternate dictionary"));
175         gtk_widget_show(default_alt_dict_label);
176         gtk_table_attach(GTK_TABLE (table), default_alt_dict_label, 0, 1, 1, 2,
177                          (GtkAttachOptions) (GTK_FILL),
178                          (GtkAttachOptions) (0), 0, 2);
179         gtk_label_set_justify(GTK_LABEL(default_alt_dict_label), GTK_JUSTIFY_RIGHT);
180         gtk_misc_set_alignment(GTK_MISC(default_alt_dict_label), 1, 0.5);
181         
182         default_alt_dict_combo = gtkaspell_dictionary_combo_new(FALSE);
183         gtk_table_attach (GTK_TABLE (table), default_alt_dict_combo, 1, 2, 1, 2,
184                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
185
186         both_dict_check = gtk_check_button_new_with_label(
187                                 _("Check with both dictionaries"));
188         gtk_widget_show(both_dict_check);
189         gtk_table_attach (GTK_TABLE (table), both_dict_check, 1, 2, 2, 3,
190                           GTK_EXPAND | GTK_FILL, 0, 0, 0);
191
192 #ifdef WIN32
193         get_dictionaries_btn = gtkut_get_link_btn(GTK_WIDGET(window), 
194                                 DICTS_URI, _("Get more dictionaries..."));
195
196         gtk_widget_show(get_dictionaries_btn);
197         gtk_table_attach (GTK_TABLE (table), get_dictionaries_btn, 1, 2, 3, 4,
198                           GTK_SHRINK, 0, 0, 0);
199 #endif
200         misspelled_hbox = gtk_hbox_new(FALSE, 10);
201         gtk_widget_show(misspelled_hbox);
202         gtk_box_pack_start(GTK_BOX(vbox1), misspelled_hbox, FALSE, FALSE, 0);
203                 
204         misspelled_label = gtk_label_new(_("Misspelled word color"));
205         gtk_widget_show(misspelled_label);
206         gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_label,
207                 FALSE, FALSE, 0);
208         gtk_label_set_justify(GTK_LABEL(misspelled_label), GTK_JUSTIFY_RIGHT);
209         gtk_misc_set_alignment(GTK_MISC(misspelled_label), 1, 0.5);
210
211         misspelled_colorbtn = GTKUT_COLOR_BUTTON();
212         gtk_widget_show(misspelled_colorbtn);
213         gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_colorbtn,
214                 FALSE, FALSE, 0);
215         CLAWS_SET_TIP(misspelled_colorbtn,
216                              _("Pick color for misspelled word. "
217                                "Use black to underline"));
218
219         SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, automatic_frame);
220         SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, dictionary_frame);
221         SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, misspelled_label);
222         SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, misspelled_colorbtn);
223         SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, use_alternate_checkbtn);
224         SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, default_alt_dict_label);
225         SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, default_alt_dict_combo);
226         SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, both_dict_check);
227
228         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_aspell_checkbtn),
229                         prefs_common.enable_aspell);
230         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(both_dict_check),
231                         prefs_common.use_both_dicts);
232         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_while_typing_checkbtn),
233                         prefs_common.check_while_typing);
234         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recheck_when_changing_dict_checkbtn),
235                         prefs_common.recheck_when_changing_dict);
236         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(use_alternate_checkbtn),
237                         prefs_common.use_alternate);
238         if (prefs_common.dictionary && 
239             strrchr(prefs_common.dictionary, '/')) {
240                 gchar *tmp = g_strdup(strrchr(prefs_common.dictionary, '/')+1);
241                 g_free(prefs_common.dictionary);
242                 prefs_common.dictionary = tmp;
243         }
244         if (prefs_common.alt_dictionary &&
245             strrchr(prefs_common.alt_dictionary, '/')) {
246                 gchar *tmp = g_strdup(strrchr(prefs_common.alt_dictionary, '/')+1);
247                 g_free(prefs_common.alt_dictionary);
248                 prefs_common.alt_dictionary = tmp;
249         }
250         if (prefs_common.dictionary &&
251             strchr(prefs_common.dictionary, '-')) {
252                 *(strchr(prefs_common.dictionary, '-')) = '\0';
253         }
254         if (prefs_common.alt_dictionary &&
255             strchr(prefs_common.alt_dictionary, '-')) {
256                 *(strchr(prefs_common.alt_dictionary, '-')) = '\0';
257         }
258         gtkaspell_set_dictionary_menu_active_item(GTK_COMBO_BOX(default_dict_combo),
259                                                 prefs_common.dictionary);
260
261         gtkaspell_set_dictionary_menu_active_item(GTK_COMBO_BOX(default_alt_dict_combo),
262                                                 prefs_common.alt_dictionary);
263
264         g_signal_connect(G_OBJECT(misspelled_colorbtn), "clicked",
265                          G_CALLBACK(prefs_spelling_colorsel), prefs_spelling);
266
267         prefs_spelling->misspell_col = prefs_common.color[COL_MISSPELLED];
268         gtkut_set_widget_bgcolor_rgb(misspelled_colorbtn, prefs_spelling->misspell_col);
269
270         prefs_spelling->window                  = GTK_WIDGET(window);
271         prefs_spelling->automatic_frame =       automatic_frame;
272         prefs_spelling->dictionary_frame =      dictionary_frame;
273         prefs_spelling->enable_aspell_checkbtn  = enable_aspell_checkbtn;
274         prefs_spelling->check_while_typing_checkbtn
275                 = check_while_typing_checkbtn;
276         prefs_spelling->recheck_when_changing_dict_checkbtn
277                 = recheck_when_changing_dict_checkbtn;
278         prefs_spelling->use_alternate_checkbtn  = use_alternate_checkbtn;
279         prefs_spelling->default_dict_label      = default_dict_label;
280         prefs_spelling->default_dict_combo      = default_dict_combo;
281         prefs_spelling->default_alt_dict_label  = default_alt_dict_label;
282         prefs_spelling->default_alt_dict_combo  = default_alt_dict_combo;
283         prefs_spelling->misspelled_label        = misspelled_label;
284         prefs_spelling->misspelled_colorbtn     = misspelled_colorbtn;
285         prefs_spelling->both_dict_check = both_dict_check;
286
287         prefs_spelling->page.widget = vbox1;
288 }
289
290 static void prefs_spelling_save(PrefsPage *_page)
291 {
292         SpellingPage *spelling = (SpellingPage *) _page;
293
294         prefs_common.enable_aspell =
295                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->enable_aspell_checkbtn));
296         prefs_common.check_while_typing =
297                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->check_while_typing_checkbtn));
298         prefs_common.recheck_when_changing_dict =
299                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->recheck_when_changing_dict_checkbtn));
300         prefs_common.use_alternate =
301                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->use_alternate_checkbtn));
302         prefs_common.use_both_dicts =
303                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->both_dict_check));
304
305         g_free(prefs_common.dictionary);
306         prefs_common.dictionary = 
307                 gtkaspell_get_dictionary_menu_active_item(
308                                 GTK_COMBO_BOX(spelling->default_dict_combo));
309
310         g_free(prefs_common.alt_dictionary);
311         prefs_common.alt_dictionary = 
312                 gtkaspell_get_dictionary_menu_active_item(
313                                 GTK_COMBO_BOX(spelling->default_alt_dict_combo));
314
315         prefs_common.color[COL_MISSPELLED] = spelling->misspell_col;
316 }
317
318 static void prefs_spelling_destroy_widget(PrefsPage *_page)
319 {
320         /* SpellingPage *spelling = (SpellingPage *) _page; */
321
322 }
323
324 SpellingPage *prefs_spelling;
325
326 void prefs_spelling_init(void)
327 {
328         SpellingPage *page;
329         static gchar *path[3];
330         const gchar* language = NULL;
331         
332         path[0] = _("Compose");
333         path[1] = _("Spell Checking");
334         path[2] = NULL;
335
336         page = g_new0(SpellingPage, 1);
337         page->page.path = path;
338         page->page.create_widget = prefs_spelling_create_widget;
339         page->page.destroy_widget = prefs_spelling_destroy_widget;
340         page->page.save_page = prefs_spelling_save;
341         page->page.weight = 180.0;
342
343         prefs_gtk_register_page((PrefsPage *) page);
344         prefs_spelling = page;
345         
346         language = g_getenv("LANG");
347         if (language == NULL)
348                 language = "en";
349         else if (!strcmp(language, "POSIX") || !strcmp(language, "C"))
350                 language = "en";
351         
352         if (!prefs_common.dictionary)
353                 prefs_common.dictionary = g_strdup_printf("%s",
354                                                 language);
355         if (!strlen(prefs_common.dictionary)
356         ||  !strcmp(prefs_common.dictionary,"(None"))
357                 prefs_common.dictionary = g_strdup_printf("%s",
358                                                 language);
359         if (strcasestr(prefs_common.dictionary,".utf"))
360                 *(strcasestr(prefs_common.dictionary,".utf")) = '\0';
361         if (strstr(prefs_common.dictionary,"@"))
362                 *(strstr(prefs_common.dictionary,"@")) = '\0';
363 }
364
365 void prefs_spelling_done(void)
366 {
367         prefs_gtk_unregister_page((PrefsPage *) prefs_spelling);
368         g_free(prefs_spelling);
369 }
370
371 #endif /* USE_ENCHANT */