(spell_dialog_spell_suggest_show): fixed widget order in the dialog.
[midnight-commander.git] / src / editor / spell_dialogs.c
blob298127636f10699cc231ec3560baaa3cf5c9c355
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 Dlg_head *sug_dlg;
74 WListbox *sug_list;
75 int max_btn_len = 0;
76 int add_len;
77 int replace_len;
78 int skip_len;
79 int cancel_len;
80 WButton *add_btn;
81 WButton *replace_btn;
82 WButton *skip_btn;
83 WButton *cancel_button;
84 int word_label_len;
86 /* calculate the dialog metrics */
87 xpos = (COLS - sug_dlg_w) / 2;
88 ypos = (LINES - sug_dlg_h) * 2 / 3;
90 /* Sometimes menu can hide replaced text. I don't like it */
91 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + sug_dlg_h - 1))
92 ypos -= sug_dlg_h;
94 add_btn = button_new (5, 28, B_ADD_WORD, NORMAL_BUTTON, _("&Add word"), 0);
95 add_len = button_get_len (add_btn);
96 replace_btn = button_new (7, 28, B_ENTER, NORMAL_BUTTON, _("&Replace"), 0);
97 replace_len = button_get_len (replace_btn);
98 skip_btn = button_new (9, 28, B_SKIP_WORD, NORMAL_BUTTON, _("&Skip"), 0);
99 skip_len = button_get_len (skip_btn);
100 cancel_button = button_new (11, 28, B_CANCEL, NORMAL_BUTTON, _("&Cancel"), 0);
101 cancel_len = button_get_len (cancel_button);
103 max_btn_len = max (replace_len, skip_len);
104 max_btn_len = max (max_btn_len, cancel_len);
106 lang_label = g_strdup_printf ("%s: %s", _("Language"), aspell_get_lang ());
107 word_label = g_strdup_printf ("%s: %s", _("Misspelled"), word);
108 word_label_len = str_term_width1 (word_label) + 5;
110 sug_dlg_w += max_btn_len;
111 sug_dlg_w = max (sug_dlg_w, word_label_len) + 1;
113 sug_dlg = create_dlg (TRUE, ypos, xpos, sug_dlg_h, sug_dlg_w,
114 dialog_colors, NULL, NULL, "[ASpell]", _("Check word"), DLG_COMPACT);
116 add_widget (sug_dlg, label_new (1, 2, lang_label));
117 add_widget (sug_dlg, label_new (3, 2, word_label));
119 add_widget (sug_dlg, groupbox_new (4, 2, sug_dlg_h - 5, 25, _("Suggest")));
121 sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
122 for (i = 0; i < suggest->len; i++)
123 listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i),
124 NULL);
125 add_widget (sug_dlg, sug_list);
127 add_widget (sug_dlg, add_btn);
128 add_widget (sug_dlg, replace_btn);
129 add_widget (sug_dlg, skip_btn);
130 add_widget (sug_dlg, cancel_button);
132 res = run_dlg (sug_dlg);
133 if (res == B_ENTER)
135 char *tmp = NULL;
136 listbox_get_current (sug_list, &curr, NULL);
138 if (curr != NULL)
139 tmp = g_strdup (curr);
140 *new_word = tmp;
143 destroy_dlg (sug_dlg);
144 g_free (lang_label);
145 g_free (word_label);
147 return res;
150 /* --------------------------------------------------------------------------------------------- */
152 * Show dialog to select language for spell check.
154 * @param languages Array of available languages
155 * @return name of choosed language
158 char *
159 spell_dialog_lang_list_show (GArray * languages)
162 int lang_dlg_h = 12; /* dialog height */
163 int lang_dlg_w = 30; /* dialog width */
164 char *selected_lang = NULL;
165 unsigned int i;
166 int res;
167 Listbox *lang_list;
169 /* Create listbox */
170 lang_list = create_listbox_window_centered (-1, -1, lang_dlg_h, lang_dlg_w,
171 _("Select language"), "[ASpell]");
173 for (i = 0; i < languages->len; i++)
174 LISTBOX_APPEND_TEXT (lang_list, 0, g_array_index (languages, char *, i), NULL);
176 res = run_listbox (lang_list);
177 if (res >= 0)
178 selected_lang = g_strdup (g_array_index (languages, char *, (unsigned int) res));
180 return selected_lang;
184 /* --------------------------------------------------------------------------------------------- */