First bunch of mhl_mem_free removal patches
[midnight-commander.git] / src / learn.c
blob93208834f02580155227160209be702fcc240635
1 /* Learn keys
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
5 Written by: 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
33 #include "global.h"
34 #include "tty.h"
35 #include "win.h"
36 #include "color.h"
37 #include "dialog.h"
38 #include "widget.h"
39 #include "profile.h" /* Save profile */
40 #include "key.h"
41 #include "setup.h"
42 #include "main.h"
43 #include "learn.h"
44 #include "wtools.h"
46 #define UX 4
47 #define UY 3
49 #define BY UY + 17
51 #define ROWS 13
52 #define COLSHIFT 23
54 #define BUTTONS 2
56 static struct {
57 int ret_cmd, flags, y, x;
58 const char *text;
59 } learn_but[BUTTONS] = {
60 { B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
61 { B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
64 static Dlg_head *learn_dlg;
65 typedef struct {
66 Widget *button;
67 Widget *label;
68 int ok;
69 char *sequence;
70 } learnkey;
71 static learnkey *learnkeys = NULL;
72 static int learn_total;
73 static int learnok;
74 static int learnchanged;
75 static const char* learn_title = N_("Learn keys");
78 static int learn_button (int action)
80 char *seq;
81 Dlg_head *d = create_message (D_ERROR, _(" Teach me a key "),
82 _("Please press the %s\n"
83 "and then wait until this message disappears.\n\n"
84 "Then, press it again to see if OK appears\n"
85 "next to its button.\n\n"
86 "If you want to escape, press a single Escape key\n"
87 "and wait as well."),
88 _(key_name_conv_tab [action - B_USER].longname));
89 mc_refresh ();
90 if (learnkeys [action - B_USER].sequence != NULL) {
91 g_free (learnkeys [action - B_USER].sequence);
92 learnkeys [action - B_USER].sequence = NULL;
94 seq = learn_key ();
96 if (seq){
97 /* Esc hides the dialog and do not allow definitions of
98 * regular characters
100 int seq_ok;
102 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
103 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
104 && (seq [1] || (*seq < ' ' || *seq > '~'))){
106 learnchanged = 1;
107 learnkeys [action - B_USER].sequence = seq;
108 seq = convert_controls (seq);
109 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
110 seq, MCKEY_NOACTION);
111 } else {
112 seq_ok = 0;
115 if (!seq_ok) {
116 message (D_NORMAL, _(" Cannot accept this key "),
117 _(" You have entered \"%s\""), seq);
120 g_free (seq);
123 dlg_run_done (d);
124 destroy_dlg (d);
125 dlg_select_widget (learnkeys [action - B_USER].button);
126 return 0; /* Do not kill learn_dlg */
129 static int learn_move (int right)
131 int i, totalcols;
133 totalcols = (learn_total - 1) / ROWS + 1;
134 for (i = 0; i < learn_total; i++)
135 if (learnkeys [i].button == learn_dlg->current) {
136 if (right) {
137 if (i < learn_total - ROWS)
138 i += ROWS;
139 else
140 i %= ROWS;
141 } else {
142 if (i / ROWS)
143 i -= ROWS;
144 else if (i + (totalcols - 1) * ROWS >= learn_total)
145 i += (totalcols - 2) * ROWS;
146 else
147 i += (totalcols - 1) * ROWS;
149 dlg_select_widget (learnkeys [i].button);
150 return 1;
152 return 0;
155 static int
156 learn_check_key (int c)
158 int i;
160 for (i = 0; i < learn_total; i++) {
161 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
162 continue;
164 dlg_select_widget (learnkeys[i].button);
165 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
166 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
167 learnkeys[i].ok = 1;
168 learnok++;
169 if (learnok >= learn_total) {
170 learn_dlg->ret_value = B_CANCEL;
171 if (learnchanged) {
172 if (query_dialog (learn_title,
174 ("It seems that all your keys already\n"
175 "work fine. That's great."), D_ERROR, 2,
176 _("&Save"), _("&Discard")) == 0)
177 learn_dlg->ret_value = B_ENTER;
178 } else {
179 message (D_ERROR, 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;
188 switch (c) {
189 case KEY_LEFT:
190 case 'h':
191 return learn_move (0);
192 case KEY_RIGHT:
193 case 'l':
194 return learn_move (1);
195 case 'j':
196 dlg_one_down (learn_dlg);
197 return 1;
198 case 'k':
199 dlg_one_up (learn_dlg);
200 return 1;
203 /* Prevent from disappearing if a non-defined sequence is pressed
204 and contains a button hotkey. Only recognize hotkeys with ALT. */
205 if (c < 255 && isalnum (c))
206 return 1;
208 return 0;
211 static cb_ret_t
212 learn_callback (Dlg_head *h, dlg_msg_t msg, int parm)
214 switch (msg) {
215 case DLG_DRAW:
216 common_dialog_repaint (h);
217 return MSG_HANDLED;
219 case DLG_KEY:
220 return learn_check_key (parm);
222 default:
223 return default_dlg_callback (h, msg, parm);
227 static void
228 init_learn (void)
230 int x, y, i, j;
231 key_code_name_t *key;
232 char buffer[BUF_TINY];
234 #ifdef ENABLE_NLS
235 static int i18n_flag = 0;
236 if (!i18n_flag) {
237 learn_but[0].text = _(learn_but[0].text);
238 learn_but[0].x = 78 / 2 + 4;
240 learn_but[1].text = _(learn_but[1].text);
241 learn_but[1].x = 78 / 2 - (strlen (learn_but[1].text) + 9);
243 learn_title = _(learn_title);
244 i18n_flag = 1;
246 #endif /* ENABLE_NLS */
248 do_refresh ();
250 learn_dlg =
251 create_dlg (0, 0, 23, 78, dialog_colors, learn_callback,
252 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
254 for (i = 0; i < BUTTONS; i++)
255 add_widget (learn_dlg,
256 button_new (BY + learn_but[i].y, learn_but[i].x,
257 learn_but[i].ret_cmd, learn_but[i].flags,
258 _(learn_but[i].text), 0));
260 x = UX;
261 y = UY;
262 for (key = key_name_conv_tab, j = 0;
263 key->name != NULL && strcmp (key->name, "kpleft"); key++, j++);
264 learnkeys = g_new (learnkey, j);
265 x += ((j - 1) / ROWS) * COLSHIFT;
266 y += (j - 1) % ROWS;
267 learn_total = j;
268 learnok = 0;
269 learnchanged = 0;
270 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
271 learnkeys[i].ok = 0;
272 learnkeys[i].sequence = NULL;
273 snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
274 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
275 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
276 learn_button));
277 add_widget (learn_dlg, learnkeys[i].label = (Widget *)
278 label_new (y, x + 19, ""));
279 if (i % 13)
280 y--;
281 else {
282 x -= COLSHIFT;
283 y = UY + ROWS - 1;
286 add_widget (learn_dlg,
287 label_new (UY + 14, 5,
289 ("Press all the keys mentioned here. After you have done it, check")));
290 add_widget (learn_dlg,
291 label_new (UY + 15, 5,
293 ("which keys are not marked with OK. Press space on the missing")));
294 add_widget (learn_dlg,
295 label_new (UY + 16, 5,
297 ("key, or click with the mouse to define it. Move around with Tab.")));
300 static void learn_done (void)
302 destroy_dlg (learn_dlg);
303 repaint_screen ();
306 static void
307 learn_save (void)
309 int i;
310 int profile_changed = 0;
311 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
313 for (i = 0; i < learn_total; i++) {
314 if (learnkeys [i].sequence != NULL) {
315 profile_changed = 1;
316 WritePrivateProfileString (section, key_name_conv_tab [i].name,
317 learnkeys [i].sequence, profile_name);
321 /* On the one hand no good idea to save the complete setup but
322 * without 'Auto save setup' the new key-definitions will not be
323 * saved unless the user does an 'Options/Save Setup'.
324 * On the other hand a save-button that does not save anything to
325 * disk is much worse.
327 if (profile_changed)
328 sync_profiles ();
330 g_free (section);
333 void learn_keys (void)
335 int save_old_esc_mode = old_esc_mode;
336 int save_alternate_plus_minus = alternate_plus_minus;
338 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
339 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
340 KP_MULTIPLY to '+', '-' and '*' in
341 correct_key_code */
342 application_keypad_mode ();
343 init_learn ();
345 run_dlg (learn_dlg);
347 old_esc_mode = save_old_esc_mode;
348 alternate_plus_minus = save_alternate_plus_minus;
350 if (!alternate_plus_minus)
351 numeric_keypad_mode ();
353 switch (learn_dlg->ret_value) {
354 case B_ENTER:
355 learn_save ();
356 break;
359 learn_done ();