Fix umpnpmgr build
[reactos.git] / rosapps / mc / src / learn.c
1 /* Learn keys
2 Copyright (C) 1995 The Free Software Foundation
3
4 Written by: 1995 Jakub Jelinek
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <config.h>
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h> /* For malloc() */
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include "tty.h"
32 #include "mad.h"
33 #include "util.h" /* Needed for the externs and convert_controls */
34 #include "win.h"
35 #include "color.h"
36 #include "dlg.h"
37 #include "widget.h"
38 #include "dialog.h" /* For do_refresh() */
39 #include "profile.h" /* Save profile */
40 #include "key.h"
41 #include "setup.h"
42 #include "main.h"
43 #define UX 4
44 #define UY 3
45
46 #define BY UY + 17
47
48 #define ROWS 13
49 #define COLSHIFT 23
50
51 #define BUTTONS 2
52
53 struct {
54 int ret_cmd, flags, y, x;
55 unsigned int hotkey;
56 char *text;
57 } learn_but[BUTTONS] = {
58 { B_CANCEL, NORMAL_BUTTON, 0, 39, 'C', N_("&Cancel") },
59 { B_ENTER, DEFPUSH_BUTTON, 0, 25, 'S', N_("&Save") }
60 };
61
62 static Dlg_head *learn_dlg;
63 typedef struct {
64 Widget *button;
65 Widget *label;
66 int ok;
67 char *sequence;
68 } learnkey;
69 static learnkey *learnkeys = NULL;
70 static int learn_total;
71 static int learnok;
72 static int learnchanged;
73 static char* learn_title = N_(" Learn keys ");
74
75 #ifndef HAVE_X
76 static void learn_refresh (void)
77 {
78 attrset (COLOR_NORMAL);
79 dlg_erase (learn_dlg);
80
81 draw_box (learn_dlg, 1, 2, learn_dlg->lines - 2, learn_dlg->cols - 4);
82
83 attrset (COLOR_HOT_NORMAL);
84 dlg_move (learn_dlg, 1, (learn_dlg->cols - strlen (learn_title)) / 2);
85 addstr (learn_title);
86 }
87 #endif
88
89 static int learn_button (int action, void *param)
90 {
91 unsigned char *seq;
92 Dlg_head *d = message (D_INSERT | 1, _(" Teach me a key "),
93 _("Please press the %s\n"
94 "and then wait until this message disappears.\n\n"
95 "Then, press it again to see if OK appears\n"
96 "next to its button.\n\n"
97 "If you want to escape, press a single Escape key\n"
98 "and wait as well."),
99 _(key_name_conv_tab [action - B_USER].longname));
100 mc_refresh ();
101 if (learnkeys [action - B_USER].sequence != NULL) {
102 free (learnkeys [action - B_USER].sequence);
103 learnkeys [action - B_USER].sequence = NULL;
104 }
105 seq = learn_key ();
106
107 if (seq){
108 /* Esc hides the dialog and do not allow definitions of
109 * regular characters
110 */
111 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
112 && strcmp (seq, "^m" )
113 && (seq [1] || (*seq < ' ' || *seq > '~'))){
114
115 learnchanged = 1;
116 learnkeys [action - B_USER].sequence = seq;
117 seq = convert_controls (seq);
118 define_sequence (key_name_conv_tab [action - B_USER].code, seq,
119 MCKEY_NOACTION);
120 } else {
121 message (0, _(" Cannot accept this key "),
122 _(" You have entered \"%s\""), seq);
123 }
124
125 free (seq);
126 }
127
128 dlg_run_done (d);
129 destroy_dlg (d);
130 dlg_select_widget (learn_dlg, learnkeys [action - B_USER].button);
131 return 0; /* Do not kill learn_dlg */
132 }
133
134 static int learn_move (int right)
135 {
136 int i, totalcols;
137
138 totalcols = (learn_total - 1) / ROWS + 1;
139 for (i = 0; i < learn_total; i++)
140 if (learnkeys [i].button == learn_dlg->current->widget) {
141 if (right) {
142 if (i < learn_total - ROWS)
143 i += ROWS;
144 else
145 i %= ROWS;
146 } else {
147 if (i / ROWS)
148 i -= ROWS;
149 else if (i + (totalcols - 1) * ROWS >= learn_total)
150 i += (totalcols - 2) * ROWS;
151 else
152 i += (totalcols - 1) * ROWS;
153 }
154 dlg_select_widget (learn_dlg, (void *) learnkeys [i].button);
155 return 1;
156 }
157 return 0;
158 }
159
160 static int learn_check_key (int c)
161 {
162 int i;
163
164 for (i = 0; i < learn_total; i++) {
165 if (key_name_conv_tab [i].code == c) {
166 if (!learnkeys [i].ok) {
167 dlg_select_widget (learn_dlg, learnkeys [i].button);
168 label_set_text ((WLabel *) learnkeys [i].label,
169 _("OK"));
170 learnkeys [i].ok = 1;
171 learnok++;
172 if (learnok >= learn_total) {
173 learn_dlg->ret_value = B_CANCEL;
174 if (learnchanged) {
175 if (query_dialog (learn_title,
176 _("It seems that all your keys already\n"
177 "work fine. That's great."),
178 1, 2, _("&Save"), _("&Discard")) == 0)
179 learn_dlg->ret_value = B_ENTER;
180 } else {
181 message (1, learn_title,
182 _("Great! You have a complete terminal database!\n"
183 "All your keys work well."));
184 }
185 dlg_stop (learn_dlg);
186 }
187 return 1;
188 }
189 }
190 }
191 switch (c) {
192 case KEY_LEFT:
193 case 'h':
194 return learn_move (0);
195 case KEY_RIGHT:
196 case 'l':
197 return learn_move (1);
198 case 'j':
199 dlg_one_down (learn_dlg);
200 return 1;
201 case 'k':
202 dlg_one_up (learn_dlg);
203 return 1;
204 }
205
206 /* Prevent from disappearing if a non-defined sequence is pressed
207 and contains s or c. Use ALT('s') or ALT('c'). */
208 if (c < 255 && isalpha(c))
209 {
210 c = toupper(c);
211 for (i = 0; i < BUTTONS; i++)
212 if (c == learn_but [i].hotkey)
213 return 1;
214 }
215
216 return 0;
217 }
218
219 static int learn_callback (Dlg_head * h, int Par, int Msg)
220 {
221 switch (Msg) {
222 case DLG_DRAW:
223 learn_refresh ();
224 break;
225 case DLG_KEY:
226 return learn_check_key (Par);
227 }
228 return 0;
229 }
230
231 static void init_learn (void)
232 {
233 int x, y, i, j;
234 key_code_name_t *key;
235 char buffer [22];
236 static int i18n_flag = 0;
237
238 do_refresh ();
239
240 #ifdef ENABLE_NLS
241 if (!i18n_flag)
242 {
243 char* cp;
244
245 learn_but [0].text = _(learn_but [0].text);
246 learn_but [0].x = 78 / 2 + 4;
247
248 learn_but [1].text = _(learn_but [1].text);
249 learn_but [1].x = 78 / 2 - (strlen (learn_but [1].text) + 9);
250
251 for (i = 0; i < BUTTONS; i++)
252 {
253 cp = strchr(learn_but [i].text, '&');
254 if (cp != NULL && *++cp != '\0')
255 learn_but [i].hotkey = toupper(*cp);
256 }
257
258 learn_title = _(learn_title);
259 i18n_flag = 1;
260 }
261 #endif /* ENABLE_NLS */
262
263 learn_dlg = create_dlg (0, 0, 23, 78, dialog_colors,
264 learn_callback, "[Learn keys]", "Learn keys",
265 DLG_CENTER);
266 x_set_dialog_title (learn_dlg, _("Learn keys"));
267
268 #define XTRACT(i) BY+learn_but[i].y, learn_but[i].x, learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0, 0, NULL
269
270 for (i = 0; i < BUTTONS; i++)
271 add_widget (learn_dlg, button_new (XTRACT (i)));
272
273 x = UX;
274 y = UY;
275 for (key = key_name_conv_tab, j = 0; key->name != NULL &&
276 strcmp (key->name, "kpleft"); key++, j++);
277 learnkeys = (learnkey *) xmalloc (sizeof (learnkey) * j, "Learn keys");
278 x += ((j - 1) / ROWS) * COLSHIFT;
279 y += (j - 1) % ROWS;
280 learn_total = j;
281 learnok = 0;
282 learnchanged = 0;
283 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
284 learnkeys [i].ok = 0;
285 learnkeys [i].sequence = NULL;
286 sprintf (buffer, "%-16s", _(key->longname));
287 add_widget (learn_dlg, learnkeys [i].button = (Widget *)
288 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button, 0, NULL));
289 add_widget (learn_dlg, learnkeys [i].label = (Widget *)
290 label_new (y, x + 19, "", NULL));
291 if (i % 13)
292 y--;
293 else {
294 x -= COLSHIFT;
295 y = UY + ROWS - 1;
296 }
297 }
298 add_widget (learn_dlg,
299 label_new (UY+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL));
300 add_widget (learn_dlg,
301 label_new (UY+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL));
302 add_widget (learn_dlg,
303 label_new (UY+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL));
304 }
305
306 static void learn_done (void)
307 {
308 destroy_dlg (learn_dlg);
309 repaint_screen ();
310 }
311
312 void learn_save (void)
313 {
314 int i;
315 int profile_changed = 0;
316 char *section = copy_strings ("terminal:", getenv ("TERM"), NULL);
317
318 for (i = 0; i < learn_total; i++) {
319 if (learnkeys [i].sequence != NULL) {
320 profile_changed = 1;
321 WritePrivateProfileString (section, key_name_conv_tab [i].name,
322 learnkeys [i].sequence, profile_name);
323 }
324 }
325
326 /* On the one hand no good idea to save the complete setup but
327 * without 'Auto save setup' the new key-definitions will not be
328 * saved unless the user does an 'Options/Save Setup'.
329 * On the other hand a save-button that does not save anything to
330 * disk is much worse.
331 */
332 if (profile_changed)
333 sync_profiles ();
334 }
335
336 void learn_keys (void)
337 {
338 int save_old_esc_mode = old_esc_mode;
339 int save_alternate_plus_minus = alternate_plus_minus;
340
341 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
342 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
343 KP_MULTIPLY to '+', '-' and '*' in
344 correct_key_code */
345 #ifndef HAVE_X
346 application_keypad_mode ();
347 #endif
348 init_learn ();
349
350 run_dlg (learn_dlg);
351
352 old_esc_mode = save_old_esc_mode;
353 alternate_plus_minus = save_alternate_plus_minus;
354
355 #ifndef HAVE_X
356 if (!alternate_plus_minus)
357 numeric_keypad_mode ();
358
359 #endif
360
361 switch (learn_dlg->ret_value) {
362 case B_ENTER:
363 learn_save ();
364 break;
365 }
366
367 learn_done ();
368 }
369