Removed draw_double_box() function.
[midnight-commander.git] / src / learn.c
blob84cfab463498b2827adab87164b0560c24250452
1 /* Learn keys
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2009 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 /** \file learn.c
23 * \brief Source: learn keys module
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
37 #include "global.h"
39 #include "../src/tty/tty.h"
40 #include "../src/tty/key.h"
42 #include "dialog.h"
43 #include "widget.h"
44 #include "../src/mcconfig/mcconfig.h" /* Save profile */
45 #include "setup.h"
46 #include "main.h"
47 #include "learn.h"
48 #include "wtools.h"
49 #include "strutil.h"
51 #define UX 4
52 #define UY 3
54 #define BY UY + 17
56 #define ROWS 13
57 #define COLSHIFT 23
59 #define BUTTONS 2
61 static struct {
62 int ret_cmd, flags, y, x;
63 const char *text;
64 } learn_but[BUTTONS] = {
65 { B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
66 { B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
69 static Dlg_head *learn_dlg;
70 typedef struct {
71 Widget *button;
72 Widget *label;
73 int ok;
74 char *sequence;
75 } learnkey;
76 static learnkey *learnkeys = NULL;
77 static int learn_total;
78 static int learnok;
79 static int learnchanged;
80 static const char* learn_title = N_("Learn keys");
83 static int learn_button (int action)
85 char *seq;
86 Dlg_head *d = create_message (D_ERROR, _(" Teach me a key "),
87 _("Please press the %s\n"
88 "and then wait until this message disappears.\n\n"
89 "Then, press it again to see if OK appears\n"
90 "next to its button.\n\n"
91 "If you want to escape, press a single Escape key\n"
92 "and wait as well."),
93 _(key_name_conv_tab [action - B_USER].longname));
94 tty_refresh ();
95 if (learnkeys [action - B_USER].sequence != NULL) {
96 g_free (learnkeys [action - B_USER].sequence);
97 learnkeys [action - B_USER].sequence = NULL;
99 seq = learn_key ();
101 if (seq){
102 /* Esc hides the dialog and do not allow definitions of
103 * regular characters
105 int seq_ok;
107 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
108 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
109 && (seq [1] || (*seq < ' ' || *seq > '~'))){
111 learnchanged = 1;
112 learnkeys [action - B_USER].sequence = seq;
113 seq = convert_controls (seq);
114 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
115 seq, MCKEY_NOACTION);
116 } else {
117 seq_ok = 0;
120 if (!seq_ok) {
121 message (D_NORMAL, _(" Cannot accept this key "),
122 _(" You have entered \"%s\""), seq);
125 g_free (seq);
128 dlg_run_done (d);
129 destroy_dlg (d);
130 dlg_select_widget (learnkeys [action - B_USER].button);
131 return 0; /* Do not kill learn_dlg */
134 static int learn_move (int right)
136 int i, totalcols;
138 totalcols = (learn_total - 1) / ROWS + 1;
139 for (i = 0; i < learn_total; i++)
140 if (learnkeys [i].button == learn_dlg->current) {
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;
154 dlg_select_widget (learnkeys [i].button);
155 return 1;
157 return 0;
160 static int
161 learn_check_key (int c)
163 int i;
165 for (i = 0; i < learn_total; i++) {
166 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
167 continue;
169 dlg_select_widget (learnkeys[i].button);
170 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
171 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
172 learnkeys[i].ok = 1;
173 learnok++;
174 if (learnok >= learn_total) {
175 learn_dlg->ret_value = B_CANCEL;
176 if (learnchanged) {
177 if (query_dialog (learn_title,
179 ("It seems that all your keys already\n"
180 "work fine. That's great."), D_ERROR, 2,
181 _("&Save"), _("&Discard")) == 0)
182 learn_dlg->ret_value = B_ENTER;
183 } else {
184 message (D_ERROR, learn_title,
186 ("Great! You have a complete terminal database!\n"
187 "All your keys work well."));
189 dlg_stop (learn_dlg);
191 return 1;
193 switch (c) {
194 case KEY_LEFT:
195 case 'h':
196 return learn_move (0);
197 case KEY_RIGHT:
198 case 'l':
199 return learn_move (1);
200 case 'j':
201 dlg_one_down (learn_dlg);
202 return 1;
203 case 'k':
204 dlg_one_up (learn_dlg);
205 return 1;
208 /* Prevent from disappearing if a non-defined sequence is pressed
209 and contains a button hotkey. Only recognize hotkeys with ALT. */
210 if (c < 255 && g_ascii_isalnum (c))
211 return 1;
213 return 0;
216 static cb_ret_t
217 learn_callback (Dlg_head *h, dlg_msg_t msg, int parm)
219 switch (msg) {
220 case DLG_DRAW:
221 common_dialog_repaint (h);
222 return MSG_HANDLED;
224 case DLG_KEY:
225 return learn_check_key (parm);
227 default:
228 return default_dlg_callback (h, msg, parm);
232 static void
233 init_learn (void)
235 int x, y, i, j;
236 key_code_name_t *key;
237 char buffer[BUF_TINY];
239 #ifdef ENABLE_NLS
240 static int i18n_flag = 0;
241 if (!i18n_flag) {
242 learn_but[0].text = _(learn_but[0].text);
243 learn_but[0].x = 78 / 2 + 4;
245 learn_but[1].text = _(learn_but[1].text);
246 learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
248 learn_title = _(learn_title);
249 i18n_flag = 1;
251 #endif /* ENABLE_NLS */
253 do_refresh ();
255 learn_dlg =
256 create_dlg (0, 0, 23, 78, dialog_colors, learn_callback,
257 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
259 for (i = 0; i < BUTTONS; i++)
260 add_widget (learn_dlg,
261 button_new (BY + learn_but[i].y, learn_but[i].x,
262 learn_but[i].ret_cmd, learn_but[i].flags,
263 _(learn_but[i].text), 0));
265 x = UX;
266 y = UY;
267 for (key = key_name_conv_tab, j = 0;
268 key->name != NULL && strcmp (key->name, "kpleft"); key++, j++);
269 learnkeys = g_new (learnkey, j);
270 x += ((j - 1) / ROWS) * COLSHIFT;
271 y += (j - 1) % ROWS;
272 learn_total = j;
273 learnok = 0;
274 learnchanged = 0;
275 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
276 learnkeys[i].ok = 0;
277 learnkeys[i].sequence = NULL;
278 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
279 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
280 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
281 learn_button));
282 add_widget (learn_dlg, learnkeys[i].label = (Widget *)
283 label_new (y, x + 19, ""));
284 if (i % 13)
285 y--;
286 else {
287 x -= COLSHIFT;
288 y = UY + ROWS - 1;
291 add_widget (learn_dlg,
292 label_new (UY + 14, 5,
294 ("Press all the keys mentioned here. After you have done it, check")));
295 add_widget (learn_dlg,
296 label_new (UY + 15, 5,
298 ("which keys are not marked with OK. Press space on the missing")));
299 add_widget (learn_dlg,
300 label_new (UY + 16, 5,
302 ("key, or click with the mouse to define it. Move around with Tab.")));
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"), (char *) NULL);
318 for (i = 0; i < learn_total; i++) {
319 if (learnkeys [i].sequence != NULL) {
320 profile_changed = 1;
321 mc_config_set_string(mc_main_config, section,
322 key_name_conv_tab [i].name, learnkeys [i].sequence);
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 mc_config_save_file (mc_main_config);
335 g_free (section);
338 void learn_keys (void)
340 int save_old_esc_mode = old_esc_mode;
341 int save_alternate_plus_minus = alternate_plus_minus;
343 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
344 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
345 KP_MULTIPLY to '+', '-' and '*' in
346 correct_key_code */
347 application_keypad_mode ();
348 init_learn ();
350 run_dlg (learn_dlg);
352 old_esc_mode = save_old_esc_mode;
353 alternate_plus_minus = save_alternate_plus_minus;
355 if (!alternate_plus_minus)
356 numeric_keypad_mode ();
358 switch (learn_dlg->ret_value) {
359 case B_ENTER:
360 learn_save ();
361 break;
364 learn_done ();