Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / editor / spell_dialogs.c
blob392f8e4ecab11562e471380957057cf541e9c593
1 /*
2 Editor spell checker dialogs
4 Copyright (C) 2012-2020
5 Free Software Foundation, Inc.
7 Written by:
8 Ilia Maslakov <il.smind@gmail.com>, 2012
9 Andrew Borodin <aborodin@vmail.ru>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include "lib/global.h"
30 #include "lib/strutil.h" /* str_term_width1 */
31 #include "lib/widget.h"
32 #include "lib/tty/tty.h" /* COLS, LINES */
34 #include "editwidget.h"
36 #include "spell.h"
37 #include "spell_dialogs.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
50 /*** public functions ****************************************************************************/
51 /* --------------------------------------------------------------------------------------------- */
52 /**
53 * Show suggests for the current word.
55 * @param edit Editor object
56 * @param word Word for spell check
57 * @param new_word Word to replace the incorrect word
58 * @param suggest Array of suggests for current word
59 * @return code of pressed button
62 int
63 spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word, GArray * suggest)
66 int sug_dlg_h = 14; /* dialog height */
67 int sug_dlg_w = 29; /* dialog width */
68 int xpos, ypos;
69 char *lang_label;
70 char *word_label;
71 unsigned int i;
72 int res;
73 char *curr = NULL;
74 WDialog *sug_dlg;
75 WGroup *g;
76 WListbox *sug_list;
77 int max_btn_len = 0;
78 int replace_len;
79 int skip_len;
80 int cancel_len;
81 WButton *add_btn;
82 WButton *replace_btn;
83 WButton *skip_btn;
84 WButton *cancel_button;
85 int word_label_len;
87 /* calculate the dialog metrics */
88 xpos = (COLS - sug_dlg_w) / 2;
89 ypos = (LINES - sug_dlg_h) * 2 / 3;
91 /* Sometimes menu can hide replaced text. I don't like it */
92 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + sug_dlg_h - 1))
93 ypos -= sug_dlg_h;
95 add_btn = button_new (5, 28, B_ADD_WORD, NORMAL_BUTTON, _("&Add word"), 0);
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 = dlg_create (TRUE, ypos, xpos, sug_dlg_h, sug_dlg_w, WPOS_KEEP_DEFAULT, TRUE,
114 dialog_colors, NULL, NULL, "[ASpell]", _("Check word"));
115 g = GROUP (sug_dlg);
117 group_add_widget (g, label_new (1, 2, lang_label));
118 group_add_widget (g, label_new (3, 2, word_label));
120 group_add_widget (g, groupbox_new (4, 2, sug_dlg_h - 5, 25, _("Suggest")));
122 sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
123 for (i = 0; i < suggest->len; i++)
124 listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i),
125 NULL, FALSE);
126 group_add_widget (g, sug_list);
128 group_add_widget (g, add_btn);
129 group_add_widget (g, replace_btn);
130 group_add_widget (g, skip_btn);
131 group_add_widget (g, cancel_button);
133 res = dlg_run (sug_dlg);
134 if (res == B_ENTER)
136 char *tmp = NULL;
137 listbox_get_current (sug_list, &curr, NULL);
139 if (curr != NULL)
140 tmp = g_strdup (curr);
141 *new_word = tmp;
144 dlg_destroy (sug_dlg);
145 g_free (lang_label);
146 g_free (word_label);
148 return res;
151 /* --------------------------------------------------------------------------------------------- */
153 * Show dialog to select language for spell check.
155 * @param languages Array of available languages
156 * @return name of choosed language
159 char *
160 spell_dialog_lang_list_show (GArray * languages)
163 int lang_dlg_h = 12; /* dialog height */
164 int lang_dlg_w = 30; /* dialog width */
165 char *selected_lang = NULL;
166 unsigned int i;
167 int res;
168 Listbox *lang_list;
170 /* Create listbox */
171 lang_list = create_listbox_window_centered (-1, -1, lang_dlg_h, lang_dlg_w,
172 _("Select language"), "[ASpell]");
174 for (i = 0; i < languages->len; i++)
175 LISTBOX_APPEND_TEXT (lang_list, 0, g_array_index (languages, char *, i), NULL, FALSE);
177 res = run_listbox (lang_list);
178 if (res >= 0)
179 selected_lang = g_strdup (g_array_index (languages, char *, (unsigned int) res));
181 return selected_lang;
185 /* --------------------------------------------------------------------------------------------- */