Update base version to 4.6.1.
[midnight-commander.git] / src / learn.c
blob608acf28d243edc945ec3c539d3160766638aaba
1 /* Learn keys
2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <config.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include "global.h"
33 #include "tty.h"
34 #include "win.h"
35 #include "color.h"
36 #include "dialog.h"
37 #include "widget.h"
38 #include "profile.h" /* Save profile */
39 #include "key.h"
40 #include "setup.h"
41 #include "main.h"
42 #include "learn.h"
43 #include "wtools.h"
45 #define UX 4
46 #define UY 3
48 #define BY UY + 17
50 #define ROWS 13
51 #define COLSHIFT 23
53 #define BUTTONS 2
55 static struct {
56 int ret_cmd, flags, y, x;
57 const char *text;
58 } learn_but[BUTTONS] = {
59 { B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
60 { B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
63 static Dlg_head *learn_dlg;
64 typedef struct {
65 Widget *button;
66 Widget *label;
67 int ok;
68 char *sequence;
69 } learnkey;
70 static learnkey *learnkeys = NULL;
71 static int learn_total;
72 static int learnok;
73 static int learnchanged;
74 static const char* learn_title = N_("Learn keys");
77 static int learn_button (int action)
79 char *seq;
80 Dlg_head *d = create_message (D_ERROR, _(" Teach me a key "),
81 _("Please press the %s\n"
82 "and then wait until this message disappears.\n\n"
83 "Then, press it again to see if OK appears\n"
84 "next to its button.\n\n"
85 "If you want to escape, press a single Escape key\n"
86 "and wait as well."),
87 _(key_name_conv_tab [action - B_USER].longname));
88 mc_refresh ();
89 if (learnkeys [action - B_USER].sequence != NULL) {
90 g_free (learnkeys [action - B_USER].sequence);
91 learnkeys [action - B_USER].sequence = NULL;
93 seq = learn_key ();
95 if (seq){
96 /* Esc hides the dialog and do not allow definitions of
97 * regular characters
99 int seq_ok;
101 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
102 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
103 && (seq [1] || (*seq < ' ' || *seq > '~'))){
105 learnchanged = 1;
106 learnkeys [action - B_USER].sequence = seq;
107 seq = convert_controls (seq);
108 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
109 seq, MCKEY_NOACTION);
110 } else {
111 seq_ok = 0;
114 if (!seq_ok) {
115 message (0, _(" Cannot accept this key "),
116 _(" You have entered \"%s\""), seq);
119 g_free (seq);
122 dlg_run_done (d);
123 destroy_dlg (d);
124 dlg_select_widget (learnkeys [action - B_USER].button);
125 return 0; /* Do not kill learn_dlg */
128 static int learn_move (int right)
130 int i, totalcols;
132 totalcols = (learn_total - 1) / ROWS + 1;
133 for (i = 0; i < learn_total; i++)
134 if (learnkeys [i].button == learn_dlg->current) {
135 if (right) {
136 if (i < learn_total - ROWS)
137 i += ROWS;
138 else
139 i %= ROWS;
140 } else {
141 if (i / ROWS)
142 i -= ROWS;
143 else if (i + (totalcols - 1) * ROWS >= learn_total)
144 i += (totalcols - 2) * ROWS;
145 else
146 i += (totalcols - 1) * ROWS;
148 dlg_select_widget (learnkeys [i].button);
149 return 1;
151 return 0;
154 static int
155 learn_check_key (int c)
157 int i;
159 for (i = 0; i < learn_total; i++) {
160 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
161 continue;
163 dlg_select_widget (learnkeys[i].button);
164 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
165 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
166 learnkeys[i].ok = 1;
167 learnok++;
168 if (learnok >= learn_total) {
169 learn_dlg->ret_value = B_CANCEL;
170 if (learnchanged) {
171 if (query_dialog (learn_title,
173 ("It seems that all your keys already\n"
174 "work fine. That's great."), 1, 2,
175 _("&Save"), _("&Discard")) == 0)
176 learn_dlg->ret_value = B_ENTER;
177 } else {
178 message (1, learn_title,
180 ("Great! You have a complete terminal database!\n"
181 "All your keys work well."));
183 dlg_stop (learn_dlg);
185 return 1;
187 switch (c) {
188 case KEY_LEFT:
189 case 'h':
190 return learn_move (0);
191 case KEY_RIGHT:
192 case 'l':
193 return learn_move (1);
194 case 'j':
195 dlg_one_down (learn_dlg);
196 return 1;
197 case 'k':
198 dlg_one_up (learn_dlg);
199 return 1;
202 /* Prevent from disappearing if a non-defined sequence is pressed
203 and contains a button hotkey. Only recognize hotkeys with ALT. */
204 if (c < 255 && isalnum (c))
205 return 1;
207 return 0;
210 static cb_ret_t
211 learn_callback (Dlg_head *h, dlg_msg_t msg, int parm)
213 switch (msg) {
214 case DLG_DRAW:
215 common_dialog_repaint (h);
216 return MSG_HANDLED;
218 case DLG_KEY:
219 return learn_check_key (parm);
221 default:
222 return default_dlg_callback (h, msg, parm);
226 static void
227 init_learn (void)
229 int x, y, i, j;
230 key_code_name_t *key;
231 char buffer[BUF_TINY];
233 #ifdef ENABLE_NLS
234 static int i18n_flag = 0;
235 if (!i18n_flag) {
236 learn_but[0].text = _(learn_but[0].text);
237 learn_but[0].x = 78 / 2 + 4;
239 learn_but[1].text = _(learn_but[1].text);
240 learn_but[1].x = 78 / 2 - (strlen (learn_but[1].text) + 9);
242 learn_title = _(learn_title);
243 i18n_flag = 1;
245 #endif /* ENABLE_NLS */
247 do_refresh ();
249 learn_dlg =
250 create_dlg (0, 0, 23, 78, dialog_colors, learn_callback,
251 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
253 for (i = 0; i < BUTTONS; i++)
254 add_widget (learn_dlg,
255 button_new (BY + learn_but[i].y, learn_but[i].x,
256 learn_but[i].ret_cmd, learn_but[i].flags,
257 _(learn_but[i].text), 0));
259 x = UX;
260 y = UY;
261 for (key = key_name_conv_tab, j = 0;
262 key->name != NULL && strcmp (key->name, "kpleft"); key++, j++);
263 learnkeys = g_new (learnkey, j);
264 x += ((j - 1) / ROWS) * COLSHIFT;
265 y += (j - 1) % ROWS;
266 learn_total = j;
267 learnok = 0;
268 learnchanged = 0;
269 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
270 learnkeys[i].ok = 0;
271 learnkeys[i].sequence = NULL;
272 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
273 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
274 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
275 learn_button));
276 add_widget (learn_dlg, learnkeys[i].label = (Widget *)
277 label_new (y, x + 19, ""));
278 if (i % 13)
279 y--;
280 else {
281 x -= COLSHIFT;
282 y = UY + ROWS - 1;
285 add_widget (learn_dlg,
286 label_new (UY + 14, 5,
288 ("Press all the keys mentioned here. After you have done it, check")));
289 add_widget (learn_dlg,
290 label_new (UY + 15, 5,
292 ("which keys are not marked with OK. Press space on the missing")));
293 add_widget (learn_dlg,
294 label_new (UY + 16, 5,
296 ("key, or click with the mouse to define it. Move around with Tab.")));
299 static void learn_done (void)
301 destroy_dlg (learn_dlg);
302 repaint_screen ();
305 static void
306 learn_save (void)
308 int i;
309 int profile_changed = 0;
310 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
312 for (i = 0; i < learn_total; i++) {
313 if (learnkeys [i].sequence != NULL) {
314 profile_changed = 1;
315 WritePrivateProfileString (section, key_name_conv_tab [i].name,
316 learnkeys [i].sequence, profile_name);
320 /* On the one hand no good idea to save the complete setup but
321 * without 'Auto save setup' the new key-definitions will not be
322 * saved unless the user does an 'Options/Save Setup'.
323 * On the other hand a save-button that does not save anything to
324 * disk is much worse.
326 if (profile_changed)
327 sync_profiles ();
329 g_free (section);
332 void learn_keys (void)
334 int save_old_esc_mode = old_esc_mode;
335 int save_alternate_plus_minus = alternate_plus_minus;
337 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
338 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
339 KP_MULTIPLY to '+', '-' and '*' in
340 correct_key_code */
341 application_keypad_mode ();
342 init_learn ();
344 run_dlg (learn_dlg);
346 old_esc_mode = save_old_esc_mode;
347 alternate_plus_minus = save_alternate_plus_minus;
349 if (!alternate_plus_minus)
350 numeric_keypad_mode ();
352 switch (learn_dlg->ret_value) {
353 case B_ENTER:
354 learn_save ();
355 break;
358 learn_done ();