Avoid compiler errors like: error: variable 'xxx' set but not used [-Werror=unused...
[midnight-commander.git] / src / editor / spell_dialogs.c
blobaa32e2a2f478a9a67ca135198e510a5ced12a915
1 /*
2 Editor spell checker dialogs
4 Copyright (C) 2012
5 The Free Software Foundation, Inc.
7 Written by:
8 Ilia Maslakov <il.smind@gmail.com>, 2012
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include "lib/global.h"
29 #include "lib/strutil.h" /* str_term_width1 */
30 #include "lib/widget.h"
31 #include "lib/tty/tty.h" /* COLS, LINES */
33 #include "editwidget.h"
35 #include "spell.h"
36 #include "spell_dialogs.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
48 /* --------------------------------------------------------------------------------------------- */
49 /*** public functions ****************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
51 /**
52 * Show suggests for the current word.
54 * @param edit Editor object
55 * @param word Word for spell check
56 * @param new_word Word to replace the incorrect word
57 * @param suggest Array of suggests for current word
58 * @return code of pressed button
61 int
62 spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word, GArray * suggest)
65 int sug_dlg_h = 14; /* dialog height */
66 int sug_dlg_w = 29; /* dialog width */
67 int xpos, ypos;
68 char *lang_label;
69 char *word_label;
70 unsigned int i;
71 int res;
72 char *curr = NULL;
73 WDialog *sug_dlg;
74 WListbox *sug_list;
75 int max_btn_len = 0;
76 int replace_len;
77 int skip_len;
78 int cancel_len;
79 WButton *add_btn;
80 WButton *replace_btn;
81 WButton *skip_btn;
82 WButton *cancel_button;
83 int word_label_len;
85 /* calculate the dialog metrics */
86 xpos = (COLS - sug_dlg_w) / 2;
87 ypos = (LINES - sug_dlg_h) * 2 / 3;
89 /* Sometimes menu can hide replaced text. I don't like it */
90 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + sug_dlg_h - 1))
91 ypos -= sug_dlg_h;
93 add_btn = button_new (5, 28, B_ADD_WORD, NORMAL_BUTTON, _("&Add word"), 0);
94 replace_btn = button_new (7, 28, B_ENTER, NORMAL_BUTTON, _("&Replace"), 0);
95 replace_len = button_get_len (replace_btn);
96 skip_btn = button_new (9, 28, B_SKIP_WORD, NORMAL_BUTTON, _("&Skip"), 0);
97 skip_len = button_get_len (skip_btn);
98 cancel_button = button_new (11, 28, B_CANCEL, NORMAL_BUTTON, _("&Cancel"), 0);
99 cancel_len = button_get_len (cancel_button);
101 max_btn_len = max (replace_len, skip_len);
102 max_btn_len = max (max_btn_len, cancel_len);
104 lang_label = g_strdup_printf ("%s: %s", _("Language"), aspell_get_lang ());
105 word_label = g_strdup_printf ("%s: %s", _("Misspelled"), word);
106 word_label_len = str_term_width1 (word_label) + 5;
108 sug_dlg_w += max_btn_len;
109 sug_dlg_w = max (sug_dlg_w, word_label_len) + 1;
111 sug_dlg = create_dlg (TRUE, ypos, xpos, sug_dlg_h, sug_dlg_w,
112 dialog_colors, NULL, NULL, "[ASpell]", _("Check word"), DLG_COMPACT);
114 add_widget (sug_dlg, label_new (1, 2, lang_label));
115 add_widget (sug_dlg, label_new (3, 2, word_label));
117 add_widget (sug_dlg, groupbox_new (4, 2, sug_dlg_h - 5, 25, _("Suggest")));
119 sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
120 for (i = 0; i < suggest->len; i++)
121 listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i),
122 NULL);
123 add_widget (sug_dlg, sug_list);
125 add_widget (sug_dlg, add_btn);
126 add_widget (sug_dlg, replace_btn);
127 add_widget (sug_dlg, skip_btn);
128 add_widget (sug_dlg, cancel_button);
130 res = run_dlg (sug_dlg);
131 if (res == B_ENTER)
133 char *tmp = NULL;
134 listbox_get_current (sug_list, &curr, NULL);
136 if (curr != NULL)
137 tmp = g_strdup (curr);
138 *new_word = tmp;
141 destroy_dlg (sug_dlg);
142 g_free (lang_label);
143 g_free (word_label);
145 return res;
148 /* --------------------------------------------------------------------------------------------- */
150 * Show dialog to select language for spell check.
152 * @param languages Array of available languages
153 * @return name of choosed language
156 char *
157 spell_dialog_lang_list_show (GArray * languages)
160 int lang_dlg_h = 12; /* dialog height */
161 int lang_dlg_w = 30; /* dialog width */
162 char *selected_lang = NULL;
163 unsigned int i;
164 int res;
165 Listbox *lang_list;
167 /* Create listbox */
168 lang_list = create_listbox_window_centered (-1, -1, lang_dlg_h, lang_dlg_w,
169 _("Select language"), "[ASpell]");
171 for (i = 0; i < languages->len; i++)
172 LISTBOX_APPEND_TEXT (lang_list, 0, g_array_index (languages, char *, i), NULL);
174 res = run_listbox (lang_list);
175 if (res >= 0)
176 selected_lang = g_strdup (g_array_index (languages, char *, (unsigned int) res));
178 return selected_lang;
182 /* --------------------------------------------------------------------------------------------- */