Fix CID 1596595: Resource leak
[claws.git] / src / prefs_spelling.c
blob3fee1cef021da18e02a4ea7c60bc97a5ad0c5224
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2021 the Claws Mail team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #if USE_ENCHANT
26 #include "defs.h"
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gtk/gtk.h>
35 #include "utils.h"
36 #include "prefs_common.h"
37 #include "prefs_gtk.h"
39 #include "gtk/gtkutils.h"
40 #include "gtk/prefswindow.h"
41 #include "gtk/filesel.h"
42 #include "gtk/combobox.h"
44 typedef struct _SpellingPage
46 PrefsPage page;
48 GtkWidget *window; /* do not modify */
50 GtkWidget *automatic_frame;
51 GtkWidget *dictionary_frame;
53 GtkWidget *enable_aspell_checkbtn;
54 GtkWidget *recheck_when_changing_dict_checkbtn;
55 GtkWidget *check_while_typing_checkbtn;
56 GtkWidget *use_alternate_checkbtn;
58 GtkWidget *default_dict_label;
59 GtkWidget *default_dict_combo;
61 GtkWidget *default_alt_dict_label;
62 GtkWidget *default_alt_dict_combo;
64 GtkWidget *both_dict_check;
66 GtkWidget *misspelled_label;
67 GtkWidget *misspelled_colorbtn;
68 GtkWidget *misspelled_useblack_label;
70 GdkRGBA misspell_col;
71 } SpellingPage;
73 #define SAFE_STRING(str) \
74 (str) ? (str) : ""
76 static void prefs_spelling_create_widget(PrefsPage *_page, GtkWindow *window, gpointer data)
78 SpellingPage *prefs_spelling = (SpellingPage *) _page;
80 GtkWidget *vbox1, *vbox2;
82 GtkWidget *enable_aspell_checkbtn;
83 GtkWidget *check_while_typing_checkbtn;
84 GtkWidget *recheck_when_changing_dict_checkbtn;
85 GtkWidget *use_alternate_checkbtn;
87 GtkWidget *automatic_frame;
88 GtkWidget *dictionary_frame;
90 GtkWidget *table;
92 GtkWidget *default_dict_label;
93 GtkWidget *default_dict_combo;
95 GtkWidget *default_alt_dict_label;
96 GtkWidget *default_alt_dict_combo;
97 GtkWidget *both_dict_check;
98 #ifdef WIN32
99 GtkWidget *get_dictionaries_btn;
100 #endif
102 GtkWidget *misspelled_label;
103 GtkWidget *misspelled_hbox;
104 GtkWidget *misspelled_colorbtn;
106 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING);
107 gtk_widget_show (vbox1);
108 gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
110 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
111 gtk_widget_show (vbox2);
112 gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
114 enable_aspell_checkbtn = gtk_check_button_new_with_label(
115 _("Enable spell checker"));
116 gtk_widget_show(enable_aspell_checkbtn);
117 gtk_box_pack_start(GTK_BOX(vbox2), enable_aspell_checkbtn, TRUE, TRUE, 0);
119 use_alternate_checkbtn = gtk_check_button_new_with_label(
120 _("Enable alternate dictionary"));
121 gtk_widget_show(use_alternate_checkbtn);
122 gtk_box_pack_start(GTK_BOX(vbox2), use_alternate_checkbtn, TRUE, TRUE, 0);
124 CLAWS_SET_TIP(use_alternate_checkbtn,
125 _("Faster switching with last used dictionary"));
127 vbox2 = gtkut_get_options_frame(vbox1, &automatic_frame, _("Automatic spell checking"));
129 check_while_typing_checkbtn = gtk_check_button_new_with_label(
130 _("Check while typing"));
131 gtk_widget_show(check_while_typing_checkbtn);
132 gtk_box_pack_start(GTK_BOX(vbox2), check_while_typing_checkbtn, TRUE, TRUE, 0);
134 recheck_when_changing_dict_checkbtn = gtk_check_button_new_with_label(
135 _("Re-check message when changing dictionary"));
136 gtk_widget_show(recheck_when_changing_dict_checkbtn);
137 gtk_box_pack_start(GTK_BOX(vbox2), recheck_when_changing_dict_checkbtn, TRUE, TRUE, 0);
139 vbox2 = gtkut_get_options_frame(vbox1, &dictionary_frame, _("Dictionary"));
141 table = gtk_grid_new();
142 gtk_widget_show(table);
143 gtk_container_set_border_width(GTK_CONTAINER(table), 0);
144 gtk_grid_set_row_spacing(GTK_GRID(table), 4);
145 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
147 gtk_box_pack_start(GTK_BOX(vbox2), table, TRUE, TRUE, 0);
149 default_dict_label = gtk_label_new(_("Default dictionary"));
150 gtk_widget_show(default_dict_label);
151 gtk_label_set_justify(GTK_LABEL(default_dict_label), GTK_JUSTIFY_RIGHT);
152 gtk_label_set_xalign(GTK_LABEL(default_dict_label), 1.0);
153 gtk_grid_attach(GTK_GRID(table), default_dict_label, 0, 0, 1, 1);
155 default_dict_combo = gtkaspell_dictionary_combo_new(TRUE);
156 gtk_grid_attach(GTK_GRID(table), default_dict_combo, 1, 0, 1, 1);
158 default_alt_dict_label = gtk_label_new(_("Default alternate dictionary"));
159 gtk_widget_show(default_alt_dict_label);
160 gtk_label_set_justify(GTK_LABEL(default_alt_dict_label), GTK_JUSTIFY_RIGHT);
161 gtk_label_set_xalign(GTK_LABEL(default_alt_dict_label), 1.0);
162 gtk_grid_attach(GTK_GRID(table), default_alt_dict_label, 0, 1, 1, 1);
164 default_alt_dict_combo = gtkaspell_dictionary_combo_new(FALSE);
165 gtk_grid_attach(GTK_GRID(table), default_alt_dict_combo, 1, 1, 1, 1);
167 both_dict_check = gtk_check_button_new_with_label(
168 _("Check with both dictionaries"));
169 gtk_widget_show(both_dict_check);
170 gtk_grid_attach(GTK_GRID(table), both_dict_check, 1, 2, 1, 1);
172 #ifdef WIN32
173 get_dictionaries_btn = gtk_link_button_new_with_label(
174 DICTS_URI, _("Get more dictionaries..."));
175 gtk_widget_show(get_dictionaries_btn);
176 gtk_grid_attach(GTK_GRID(table), get_dictionaries_btn, 1, 3, 1, 1);
177 #endif
178 misspelled_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
179 gtk_widget_show(misspelled_hbox);
180 gtk_box_pack_start(GTK_BOX(vbox1), misspelled_hbox, FALSE, FALSE, 0);
182 misspelled_label = gtk_label_new(_("Misspelled word color"));
183 gtk_widget_show(misspelled_label);
184 gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_label,
185 FALSE, FALSE, 0);
186 gtk_label_set_justify(GTK_LABEL(misspelled_label), GTK_JUSTIFY_RIGHT);
187 gtk_label_set_xalign(GTK_LABEL(misspelled_label), 1.0);
189 misspelled_colorbtn = gtk_color_button_new_with_rgba(
190 &prefs_common.color[COL_MISSPELLED]);
191 gtk_color_button_set_title(GTK_COLOR_BUTTON(misspelled_colorbtn),
192 _("Pick color for misspelled word"));
193 gtk_widget_show(misspelled_colorbtn);
194 gtk_box_pack_start(GTK_BOX(misspelled_hbox), misspelled_colorbtn,
195 FALSE, FALSE, 0);
196 CLAWS_SET_TIP(misspelled_colorbtn,
197 _("Pick color for misspelled word. "
198 "Use black to underline"));
200 SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, automatic_frame);
201 SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, dictionary_frame);
202 SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, misspelled_label);
203 SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, misspelled_colorbtn);
204 SET_TOGGLE_SENSITIVITY(enable_aspell_checkbtn, use_alternate_checkbtn);
205 SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, default_alt_dict_label);
206 SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, default_alt_dict_combo);
207 SET_TOGGLE_SENSITIVITY(use_alternate_checkbtn, both_dict_check);
209 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_aspell_checkbtn),
210 prefs_common.enable_aspell);
211 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(both_dict_check),
212 prefs_common.use_both_dicts);
213 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_while_typing_checkbtn),
214 prefs_common.check_while_typing);
215 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recheck_when_changing_dict_checkbtn),
216 prefs_common.recheck_when_changing_dict);
217 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(use_alternate_checkbtn),
218 prefs_common.use_alternate);
219 if (prefs_common.dictionary &&
220 strrchr(prefs_common.dictionary, '/')) {
221 gchar *tmp = g_strdup(strrchr(prefs_common.dictionary, '/')+1);
222 g_free(prefs_common.dictionary);
223 prefs_common.dictionary = tmp;
225 if (prefs_common.alt_dictionary &&
226 strrchr(prefs_common.alt_dictionary, '/')) {
227 gchar *tmp = g_strdup(strrchr(prefs_common.alt_dictionary, '/')+1);
228 g_free(prefs_common.alt_dictionary);
229 prefs_common.alt_dictionary = tmp;
231 if (prefs_common.dictionary &&
232 strchr(prefs_common.dictionary, '-')) {
233 *(strchr(prefs_common.dictionary, '-')) = '\0';
235 if (prefs_common.alt_dictionary &&
236 strchr(prefs_common.alt_dictionary, '-')) {
237 *(strchr(prefs_common.alt_dictionary, '-')) = '\0';
239 gtkaspell_set_dictionary_menu_active_item(GTK_COMBO_BOX(default_dict_combo),
240 prefs_common.dictionary);
242 gtkaspell_set_dictionary_menu_active_item(GTK_COMBO_BOX(default_alt_dict_combo),
243 prefs_common.alt_dictionary);
245 prefs_spelling->window = GTK_WIDGET(window);
246 prefs_spelling->automatic_frame = automatic_frame;
247 prefs_spelling->dictionary_frame = dictionary_frame;
248 prefs_spelling->enable_aspell_checkbtn = enable_aspell_checkbtn;
249 prefs_spelling->check_while_typing_checkbtn
250 = check_while_typing_checkbtn;
251 prefs_spelling->recheck_when_changing_dict_checkbtn
252 = recheck_when_changing_dict_checkbtn;
253 prefs_spelling->use_alternate_checkbtn = use_alternate_checkbtn;
254 prefs_spelling->default_dict_label = default_dict_label;
255 prefs_spelling->default_dict_combo = default_dict_combo;
256 prefs_spelling->default_alt_dict_label = default_alt_dict_label;
257 prefs_spelling->default_alt_dict_combo = default_alt_dict_combo;
258 prefs_spelling->misspelled_label = misspelled_label;
259 prefs_spelling->misspelled_colorbtn = misspelled_colorbtn;
260 prefs_spelling->both_dict_check = both_dict_check;
262 prefs_spelling->page.widget = vbox1;
265 static void prefs_spelling_save(PrefsPage *_page)
267 SpellingPage *spelling = (SpellingPage *) _page;
269 prefs_common.enable_aspell =
270 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->enable_aspell_checkbtn));
271 prefs_common.check_while_typing =
272 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->check_while_typing_checkbtn));
273 prefs_common.recheck_when_changing_dict =
274 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->recheck_when_changing_dict_checkbtn));
275 prefs_common.use_alternate =
276 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->use_alternate_checkbtn));
277 prefs_common.use_both_dicts =
278 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(spelling->both_dict_check));
280 g_free(prefs_common.dictionary);
281 prefs_common.dictionary =
282 gtkaspell_get_dictionary_menu_active_item(
283 GTK_COMBO_BOX(spelling->default_dict_combo));
285 g_free(prefs_common.alt_dictionary);
286 prefs_common.alt_dictionary =
287 gtkaspell_get_dictionary_menu_active_item(
288 GTK_COMBO_BOX(spelling->default_alt_dict_combo));
290 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(spelling->misspelled_colorbtn),
291 &prefs_common.color[COL_MISSPELLED]);
294 static void prefs_spelling_destroy_widget(PrefsPage *_page)
296 /* SpellingPage *spelling = (SpellingPage *) _page; */
300 SpellingPage *prefs_spelling;
302 void prefs_spelling_init(void)
304 SpellingPage *page;
305 static gchar *path[3];
306 const gchar* language = NULL;
308 path[0] = _("Compose");
309 path[1] = _("Spell Checking");
310 path[2] = NULL;
312 page = g_new0(SpellingPage, 1);
313 page->page.path = path;
314 page->page.create_widget = prefs_spelling_create_widget;
315 page->page.destroy_widget = prefs_spelling_destroy_widget;
316 page->page.save_page = prefs_spelling_save;
317 page->page.weight = 180.0;
319 prefs_gtk_register_page((PrefsPage *) page);
320 prefs_spelling = page;
322 language = g_getenv("LANG");
323 if (language == NULL)
324 language = "en";
325 else if (!strcmp(language, "POSIX") || !strcmp(language, "C"))
326 language = "en";
328 if (!prefs_common.dictionary)
329 prefs_common.dictionary = g_strdup_printf("%s",
330 language);
331 if (!strlen(prefs_common.dictionary)
332 || !strcmp(prefs_common.dictionary,"(None"))
333 prefs_common.dictionary = g_strdup_printf("%s",
334 language);
335 if (strcasestr(prefs_common.dictionary,".utf"))
336 *(strcasestr(prefs_common.dictionary,".utf")) = '\0';
337 if (strstr(prefs_common.dictionary,"@"))
338 *(strstr(prefs_common.dictionary,"@")) = '\0';
341 void prefs_spelling_done(void)
343 prefs_gtk_unregister_page((PrefsPage *) prefs_spelling);
344 g_free(prefs_spelling);
347 #endif /* USE_ENCHANT */