*** empty log message ***
[midnight-commander.git] / src / learn.c
blob102a7b46e9d0ec4a57a869ff72d1212b7b5f5e1f
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <sys/types.h>
28 #include <sys/stat.h>
29 #include <ctype.h>
31 #include "global.h"
32 #include "tty.h"
33 #include "win.h"
34 #include "color.h"
35 #include "dlg.h"
36 #include "widget.h"
37 #include "dialog.h" /* For do_refresh() */
38 #include "profile.h" /* Save profile */
39 #include "key.h"
40 #include "setup.h"
41 #include "main.h"
42 #include "learn.h"
44 #define UX 4
45 #define UY 3
47 #define BY UY + 17
49 #define ROWS 13
50 #define COLSHIFT 23
52 #define BUTTONS 2
54 static struct {
55 int ret_cmd, flags, y, x;
56 unsigned int hotkey;
57 char *text;
58 } learn_but[BUTTONS] = {
59 { B_CANCEL, NORMAL_BUTTON, 0, 39, 'C', N_("&Cancel") },
60 { B_ENTER, DEFPUSH_BUTTON, 0, 25, 'S', 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 char* learn_title = N_(" Learn keys ");
76 static void learn_refresh (void)
78 attrset (COLOR_NORMAL);
79 dlg_erase (learn_dlg);
81 draw_box (learn_dlg, 1, 2, learn_dlg->lines - 2, learn_dlg->cols - 4);
83 attrset (COLOR_HOT_NORMAL);
84 dlg_move (learn_dlg, 1, (learn_dlg->cols - strlen (learn_title)) / 2);
85 addstr (learn_title);
88 static int learn_button (int action, void *param)
90 unsigned char *seq;
91 Dlg_head *d = message (D_INSERT | 1, _(" Teach me a key "),
92 _("Please press the %s\n"
93 "and then wait until this message disappears.\n\n"
94 "Then, press it again to see if OK appears\n"
95 "next to its button.\n\n"
96 "If you want to escape, press a single Escape key\n"
97 "and wait as well."),
98 _(key_name_conv_tab [action - B_USER].longname));
99 mc_refresh ();
100 if (learnkeys [action - B_USER].sequence != NULL) {
101 g_free (learnkeys [action - B_USER].sequence);
102 learnkeys [action - B_USER].sequence = NULL;
104 seq = learn_key ();
106 if (seq){
107 /* Esc hides the dialog and do not allow definitions of
108 * regular characters
110 int seq_ok;
112 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
113 && strcmp (seq, "^m" )
114 && (seq [1] || (*seq < ' ' || *seq > '~'))){
116 learnchanged = 1;
117 learnkeys [action - B_USER].sequence = seq;
118 seq = convert_controls (seq);
119 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
120 seq, MCKEY_NOACTION);
121 } else {
122 seq_ok = 0;
125 if (!seq_ok) {
126 message (0, _(" Cannot accept this key "),
127 _(" You have entered \"%s\""), seq);
130 g_free (seq);
133 dlg_run_done (d);
134 destroy_dlg (d);
135 dlg_select_widget (learn_dlg, learnkeys [action - B_USER].button);
136 return 0; /* Do not kill learn_dlg */
139 static int learn_move (int right)
141 int i, totalcols;
143 totalcols = (learn_total - 1) / ROWS + 1;
144 for (i = 0; i < learn_total; i++)
145 if (learnkeys [i].button == learn_dlg->current->widget) {
146 if (right) {
147 if (i < learn_total - ROWS)
148 i += ROWS;
149 else
150 i %= ROWS;
151 } else {
152 if (i / ROWS)
153 i -= ROWS;
154 else if (i + (totalcols - 1) * ROWS >= learn_total)
155 i += (totalcols - 2) * ROWS;
156 else
157 i += (totalcols - 1) * ROWS;
159 dlg_select_widget (learn_dlg, (void *) learnkeys [i].button);
160 return 1;
162 return 0;
165 static int learn_check_key (int c)
167 int i;
169 for (i = 0; i < learn_total; i++) {
170 if (key_name_conv_tab [i].code == c) {
171 if (!learnkeys [i].ok) {
172 dlg_select_widget (learn_dlg, learnkeys [i].button);
173 label_set_text ((WLabel *) learnkeys [i].label,
174 _("OK"));
175 learnkeys [i].ok = 1;
176 learnok++;
177 if (learnok >= learn_total) {
178 learn_dlg->ret_value = B_CANCEL;
179 if (learnchanged) {
180 if (query_dialog (learn_title,
181 _("It seems that all your keys already\n"
182 "work fine. That's great."),
183 1, 2, _("&Save"), _("&Discard")) == 0)
184 learn_dlg->ret_value = B_ENTER;
185 } else {
186 message (1, learn_title,
187 _("Great! You have a complete terminal database!\n"
188 "All your keys work well."));
190 dlg_stop (learn_dlg);
192 return 1;
196 switch (c) {
197 case KEY_LEFT:
198 case 'h':
199 return learn_move (0);
200 case KEY_RIGHT:
201 case 'l':
202 return learn_move (1);
203 case 'j':
204 dlg_one_down (learn_dlg);
205 return 1;
206 case 'k':
207 dlg_one_up (learn_dlg);
208 return 1;
211 /* Prevent from disappearing if a non-defined sequence is pressed
212 and contains s or c. Use ALT('s') or ALT('c'). */
213 if (c < 255 && isalpha(c))
215 c = toupper(c);
216 for (i = 0; i < BUTTONS; i++)
217 if (c == learn_but [i].hotkey)
218 return 1;
221 return 0;
224 static int learn_callback (Dlg_head * h, int Par, int Msg)
226 switch (Msg) {
227 case DLG_DRAW:
228 learn_refresh ();
229 break;
230 case DLG_KEY:
231 return learn_check_key (Par);
233 return 0;
236 static void init_learn (void)
238 int x, y, i, j;
239 key_code_name_t *key;
240 char buffer [BUF_TINY];
242 #ifdef ENABLE_NLS
243 static int i18n_flag = 0;
244 if (!i18n_flag)
246 char* cp;
248 learn_but [0].text = _(learn_but [0].text);
249 learn_but [0].x = 78 / 2 + 4;
251 learn_but [1].text = _(learn_but [1].text);
252 learn_but [1].x = 78 / 2 - (strlen (learn_but [1].text) + 9);
254 for (i = 0; i < BUTTONS; i++)
256 cp = strchr(learn_but [i].text, '&');
257 if (cp != NULL && *++cp != '\0')
258 learn_but [i].hotkey = toupper(*cp);
261 learn_title = _(learn_title);
262 i18n_flag = 1;
264 #endif /* ENABLE_NLS */
266 do_refresh ();
268 learn_dlg = create_dlg (0, 0, 23, 78, dialog_colors,
269 learn_callback, "[Learn keys]", "Learn keys",
270 DLG_CENTER);
271 x_set_dialog_title (learn_dlg, _("Learn keys"));
273 #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
275 for (i = 0; i < BUTTONS; i++)
276 add_widget (learn_dlg, button_new (XTRACT (i)));
278 x = UX;
279 y = UY;
280 for (key = key_name_conv_tab, j = 0; key->name != NULL &&
281 strcmp (key->name, "kpleft"); key++, j++);
282 learnkeys = g_new (learnkey, j);
283 x += ((j - 1) / ROWS) * COLSHIFT;
284 y += (j - 1) % ROWS;
285 learn_total = j;
286 learnok = 0;
287 learnchanged = 0;
288 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
289 learnkeys [i].ok = 0;
290 learnkeys [i].sequence = NULL;
291 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
292 add_widget (learn_dlg, learnkeys [i].button = (Widget *)
293 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button, 0, NULL));
294 add_widget (learn_dlg, learnkeys [i].label = (Widget *)
295 label_new (y, x + 19, "", NULL));
296 if (i % 13)
297 y--;
298 else {
299 x -= COLSHIFT;
300 y = UY + ROWS - 1;
303 add_widget (learn_dlg,
304 label_new (UY+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL));
305 add_widget (learn_dlg,
306 label_new (UY+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL));
307 add_widget (learn_dlg,
308 label_new (UY+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL));
311 static void learn_done (void)
313 destroy_dlg (learn_dlg);
314 repaint_screen ();
317 static void
318 learn_save (void)
320 int i;
321 int profile_changed = 0;
322 char *section = g_strconcat ("terminal:", getenv ("TERM"), NULL);
324 for (i = 0; i < learn_total; i++) {
325 if (learnkeys [i].sequence != NULL) {
326 profile_changed = 1;
327 WritePrivateProfileString (section, key_name_conv_tab [i].name,
328 learnkeys [i].sequence, profile_name);
332 /* On the one hand no good idea to save the complete setup but
333 * without 'Auto save setup' the new key-definitions will not be
334 * saved unless the user does an 'Options/Save Setup'.
335 * On the other hand a save-button that does not save anything to
336 * disk is much worse.
338 if (profile_changed)
339 sync_profiles ();
341 g_free (section);
344 void learn_keys (void)
346 int save_old_esc_mode = old_esc_mode;
347 int save_alternate_plus_minus = alternate_plus_minus;
349 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
350 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
351 KP_MULTIPLY to '+', '-' and '*' in
352 correct_key_code */
353 application_keypad_mode ();
354 init_learn ();
356 run_dlg (learn_dlg);
358 old_esc_mode = save_old_esc_mode;
359 alternate_plus_minus = save_alternate_plus_minus;
361 if (!alternate_plus_minus)
362 numeric_keypad_mode ();
364 switch (learn_dlg->ret_value) {
365 case B_ENTER:
366 learn_save ();
367 break;
370 learn_done ();