Remove min() and max() macros. Use MIN() and MAX() macros from GLib.
[midnight-commander.git] / src / editor / spell_dialogs.c
blob190b60b580872a6b6ec73bde848ef76f6e98b362
1 /*
2 Editor spell checker dialogs
4 Copyright (C) 2012-2016
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 WListbox *sug_list;
76 int max_btn_len = 0;
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 replace_btn = button_new (7, 28, B_ENTER, NORMAL_BUTTON, _("&Replace"), 0);
96 replace_len = button_get_len (replace_btn);
97 skip_btn = button_new (9, 28, B_SKIP_WORD, NORMAL_BUTTON, _("&Skip"), 0);
98 skip_len = button_get_len (skip_btn);
99 cancel_button = button_new (11, 28, B_CANCEL, NORMAL_BUTTON, _("&Cancel"), 0);
100 cancel_len = button_get_len (cancel_button);
102 max_btn_len = MAX (replace_len, skip_len);
103 max_btn_len = MAX (max_btn_len, cancel_len);
105 lang_label = g_strdup_printf ("%s: %s", _("Language"), aspell_get_lang ());
106 word_label = g_strdup_printf ("%s: %s", _("Misspelled"), word);
107 word_label_len = str_term_width1 (word_label) + 5;
109 sug_dlg_w += max_btn_len;
110 sug_dlg_w = MAX (sug_dlg_w, word_label_len) + 1;
112 sug_dlg = dlg_create (TRUE, ypos, xpos, sug_dlg_h, sug_dlg_w,
113 dialog_colors, NULL, NULL, "[ASpell]", _("Check word"), DLG_COMPACT);
115 add_widget (sug_dlg, label_new (1, 2, lang_label));
116 add_widget (sug_dlg, label_new (3, 2, word_label));
118 add_widget (sug_dlg, groupbox_new (4, 2, sug_dlg_h - 5, 25, _("Suggest")));
120 sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
121 for (i = 0; i < suggest->len; i++)
122 listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i),
123 NULL, FALSE);
124 add_widget (sug_dlg, sug_list);
126 add_widget (sug_dlg, add_btn);
127 add_widget (sug_dlg, replace_btn);
128 add_widget (sug_dlg, skip_btn);
129 add_widget (sug_dlg, cancel_button);
131 res = dlg_run (sug_dlg);
132 if (res == B_ENTER)
134 char *tmp = NULL;
135 listbox_get_current (sug_list, &curr, NULL);
137 if (curr != NULL)
138 tmp = g_strdup (curr);
139 *new_word = tmp;
142 dlg_destroy (sug_dlg);
143 g_free (lang_label);
144 g_free (word_label);
146 return res;
149 /* --------------------------------------------------------------------------------------------- */
151 * Show dialog to select language for spell check.
153 * @param languages Array of available languages
154 * @return name of choosed language
157 char *
158 spell_dialog_lang_list_show (GArray * languages)
161 int lang_dlg_h = 12; /* dialog height */
162 int lang_dlg_w = 30; /* dialog width */
163 char *selected_lang = NULL;
164 unsigned int i;
165 int res;
166 Listbox *lang_list;
168 /* Create listbox */
169 lang_list = create_listbox_window_centered (-1, -1, lang_dlg_h, lang_dlg_w,
170 _("Select language"), "[ASpell]");
172 for (i = 0; i < languages->len; i++)
173 LISTBOX_APPEND_TEXT (lang_list, 0, g_array_index (languages, char *, i), NULL, FALSE);
175 res = run_listbox (lang_list);
176 if (res >= 0)
177 selected_lang = g_strdup (g_array_index (languages, char *, (unsigned int) res));
179 return selected_lang;
183 /* --------------------------------------------------------------------------------------------- */