Codepage messages related translated & other stuff...
[midnight-commander.git] / src / learn.c
blobcb410b037ac75d498406e71c2b0faac28523f3b2
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/param.h>
29 #include <sys/stat.h>
30 #include <ctype.h>
31 #include "tty.h"
32 #include "global.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 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
111 && strcmp (seq, "^m" )
112 && (seq [1] || (*seq < ' ' || *seq > '~'))){
114 learnchanged = 1;
115 learnkeys [action - B_USER].sequence = seq;
116 seq = convert_controls (seq);
117 define_sequence (key_name_conv_tab [action - B_USER].code, seq,
118 MCKEY_NOACTION);
119 } else {
120 message (0, _(" Cannot accept this key "),
121 _(" You have entered \"%s\""), seq);
124 g_free (seq);
127 dlg_run_done (d);
128 destroy_dlg (d);
129 dlg_select_widget (learn_dlg, learnkeys [action - B_USER].button);
130 return 0; /* Do not kill learn_dlg */
133 static int learn_move (int right)
135 int i, totalcols;
137 totalcols = (learn_total - 1) / ROWS + 1;
138 for (i = 0; i < learn_total; i++)
139 if (learnkeys [i].button == learn_dlg->current->widget) {
140 if (right) {
141 if (i < learn_total - ROWS)
142 i += ROWS;
143 else
144 i %= ROWS;
145 } else {
146 if (i / ROWS)
147 i -= ROWS;
148 else if (i + (totalcols - 1) * ROWS >= learn_total)
149 i += (totalcols - 2) * ROWS;
150 else
151 i += (totalcols - 1) * ROWS;
153 dlg_select_widget (learn_dlg, (void *) learnkeys [i].button);
154 return 1;
156 return 0;
159 static int learn_check_key (int c)
161 int i;
163 for (i = 0; i < learn_total; i++) {
164 if (key_name_conv_tab [i].code == c) {
165 if (!learnkeys [i].ok) {
166 dlg_select_widget (learn_dlg, learnkeys [i].button);
167 label_set_text ((WLabel *) learnkeys [i].label,
168 _("OK"));
169 learnkeys [i].ok = 1;
170 learnok++;
171 if (learnok >= learn_total) {
172 learn_dlg->ret_value = B_CANCEL;
173 if (learnchanged) {
174 if (query_dialog (learn_title,
175 _("It seems that all your keys already\n"
176 "work fine. That's great."),
177 1, 2, _("&Save"), _("&Discard")) == 0)
178 learn_dlg->ret_value = B_ENTER;
179 } else {
180 message (1, learn_title,
181 _("Great! You have a complete terminal database!\n"
182 "All your keys work well."));
184 dlg_stop (learn_dlg);
186 return 1;
190 switch (c) {
191 case KEY_LEFT:
192 case 'h':
193 return learn_move (0);
194 case KEY_RIGHT:
195 case 'l':
196 return learn_move (1);
197 case 'j':
198 dlg_one_down (learn_dlg);
199 return 1;
200 case 'k':
201 dlg_one_up (learn_dlg);
202 return 1;
205 /* Prevent from disappearing if a non-defined sequence is pressed
206 and contains s or c. Use ALT('s') or ALT('c'). */
207 if (c < 255 && isalpha(c))
209 c = toupper(c);
210 for (i = 0; i < BUTTONS; i++)
211 if (c == learn_but [i].hotkey)
212 return 1;
215 return 0;
218 static int learn_callback (Dlg_head * h, int Par, int Msg)
220 switch (Msg) {
221 case DLG_DRAW:
222 learn_refresh ();
223 break;
224 case DLG_KEY:
225 return learn_check_key (Par);
227 return 0;
230 static void init_learn (void)
232 int x, y, i, j;
233 key_code_name_t *key;
234 char buffer [BUF_TINY];
235 static int i18n_flag = 0;
237 do_refresh ();
239 #ifdef ENABLE_NLS
240 if (!i18n_flag)
242 char* cp;
244 learn_but [0].text = _(learn_but [0].text);
245 learn_but [0].x = 78 / 2 + 4;
247 learn_but [1].text = _(learn_but [1].text);
248 learn_but [1].x = 78 / 2 - (strlen (learn_but [1].text) + 9);
250 for (i = 0; i < BUTTONS; i++)
252 cp = strchr(learn_but [i].text, '&');
253 if (cp != NULL && *++cp != '\0')
254 learn_but [i].hotkey = toupper(*cp);
257 learn_title = _(learn_title);
258 i18n_flag = 1;
260 #endif /* ENABLE_NLS */
262 learn_dlg = create_dlg (0, 0, 23, 78, dialog_colors,
263 learn_callback, "[Learn keys]", "Learn keys",
264 DLG_CENTER);
265 x_set_dialog_title (learn_dlg, _("Learn keys"));
267 #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 for (i = 0; i < BUTTONS; i++)
270 add_widget (learn_dlg, button_new (XTRACT (i)));
272 x = UX;
273 y = UY;
274 for (key = key_name_conv_tab, j = 0; key->name != NULL &&
275 strcmp (key->name, "kpleft"); key++, j++);
276 learnkeys = g_new (learnkey, j);
277 x += ((j - 1) / ROWS) * COLSHIFT;
278 y += (j - 1) % ROWS;
279 learn_total = j;
280 learnok = 0;
281 learnchanged = 0;
282 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
283 learnkeys [i].ok = 0;
284 learnkeys [i].sequence = NULL;
285 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
286 add_widget (learn_dlg, learnkeys [i].button = (Widget *)
287 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button, 0, NULL));
288 add_widget (learn_dlg, learnkeys [i].label = (Widget *)
289 label_new (y, x + 19, "", NULL));
290 if (i % 13)
291 y--;
292 else {
293 x -= COLSHIFT;
294 y = UY + ROWS - 1;
297 add_widget (learn_dlg,
298 label_new (UY+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL));
299 add_widget (learn_dlg,
300 label_new (UY+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL));
301 add_widget (learn_dlg,
302 label_new (UY+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL));
305 static void learn_done (void)
307 destroy_dlg (learn_dlg);
308 repaint_screen ();
311 static void
312 learn_save (void)
314 int i;
315 int profile_changed = 0;
316 char *section = g_strconcat ("terminal:", getenv ("TERM"), NULL);
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);
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.
332 if (profile_changed)
333 sync_profiles ();
336 void learn_keys (void)
338 int save_old_esc_mode = old_esc_mode;
339 int save_alternate_plus_minus = alternate_plus_minus;
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 application_keypad_mode ();
346 init_learn ();
348 run_dlg (learn_dlg);
350 old_esc_mode = save_old_esc_mode;
351 alternate_plus_minus = save_alternate_plus_minus;
353 if (!alternate_plus_minus)
354 numeric_keypad_mode ();
356 switch (learn_dlg->ret_value) {
357 case B_ENTER:
358 learn_save ();
359 break;
362 learn_done ();