Code indentation.
[midnight-commander.git] / src / editor / spell_dialogs.c
blob2ebf0206e6628964f117a36f21823b7e221c6c11
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 * @returns 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;
112 sug_dlg = create_dlg (TRUE, ypos, xpos, sug_dlg_h, sug_dlg_w,
113 dialog_colors, NULL, NULL, "[ASpell]", _("Check word"), DLG_COMPACT);
115 sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
116 for (i = 0; i < suggest->len; i++)
117 listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i),
118 NULL);
119 add_widget (sug_dlg, sug_list);
121 add_widget (sug_dlg, add_btn);
122 add_widget (sug_dlg, replace_btn);
123 add_widget (sug_dlg, skip_btn);
124 add_widget (sug_dlg, cancel_button);
126 add_widget (sug_dlg, label_new (1, 2, lang_label));
127 add_widget (sug_dlg, label_new (3, 2, word_label));
128 add_widget (sug_dlg, groupbox_new (4, 2, sug_dlg_h - 5, 25, _("Suggest")));
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 * @returns 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 /* --------------------------------------------------------------------------------------------- */