Merge branch '2199_button_bar_mouse'
[midnight-commander.git] / src / learn.c
blob49c5892c276e50b817a10ebc4dfae25d6863294d
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 "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/key.h"
41 #include "lib/mcconfig.h" /* Save profile */
42 #include "lib/strescape.h"
43 #include "lib/strutil.h"
45 #include "dialog.h"
46 #include "widget.h"
47 #include "setup.h"
48 #include "layout.h" /* repaint_screen() */
49 #include "learn.h"
50 #include "wtools.h"
52 #define UX 4
53 #define UY 3
55 #define BY UY + 17
57 #define ROWS 13
58 #define COLSHIFT 23
60 #define BUTTONS 2
62 static struct {
63 int ret_cmd, flags, y, x;
64 const char *text;
65 } learn_but[BUTTONS] = {
66 { B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
67 { B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
70 static Dlg_head *learn_dlg;
71 typedef struct {
72 Widget *button;
73 Widget *label;
74 int ok;
75 char *sequence;
76 } learnkey;
77 static learnkey *learnkeys = NULL;
78 static int learn_total;
79 static int learnok;
80 static int learnchanged;
81 static const char* learn_title = N_("Learn keys");
84 static int learn_button (int action)
86 char *seq;
87 Dlg_head *d = create_message (D_ERROR, _("Teach me a key"),
88 _("Please press the %s\n"
89 "and then wait until this message disappears.\n\n"
90 "Then, press it again to see if OK appears\n"
91 "next to its button.\n\n"
92 "If you want to escape, press a single Escape key\n"
93 "and wait as well."),
94 _(key_name_conv_tab [action - B_USER].longname));
95 mc_refresh ();
96 if (learnkeys [action - B_USER].sequence != NULL) {
97 g_free (learnkeys [action - B_USER].sequence);
98 learnkeys [action - B_USER].sequence = NULL;
100 seq = learn_key ();
102 if (seq){
103 /* Esc hides the dialog and do not allow definitions of
104 * regular characters
106 gboolean seq_ok = FALSE;
108 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
109 && strcmp (seq, "^m" ) && strcmp (seq, "^i" )
110 && (seq [1] || (*seq < ' ' || *seq > '~'))){
112 learnchanged = 1;
113 learnkeys [action - B_USER].sequence = seq;
114 seq = convert_controls (seq);
115 seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
116 seq, MCKEY_NOACTION);
119 if (!seq_ok)
120 message (D_NORMAL, _("Cannot accept this key"),
121 _("You have entered \"%s\""), seq);
123 g_free (seq);
126 dlg_run_done (d);
127 destroy_dlg (d);
128 dlg_select_widget (learnkeys [action - B_USER].button);
129 return 0; /* Do not kill learn_dlg */
132 static int learn_move (int right)
134 int i, totalcols;
136 totalcols = (learn_total - 1) / ROWS + 1;
137 for (i = 0; i < learn_total; i++)
138 if (learnkeys [i].button == learn_dlg->current) {
139 if (right) {
140 if (i < learn_total - ROWS)
141 i += ROWS;
142 else
143 i %= ROWS;
144 } else {
145 if (i / ROWS)
146 i -= ROWS;
147 else if (i + (totalcols - 1) * ROWS >= learn_total)
148 i += (totalcols - 2) * ROWS;
149 else
150 i += (totalcols - 1) * ROWS;
152 dlg_select_widget (learnkeys [i].button);
153 return 1;
155 return 0;
158 static int
159 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 || learnkeys[i].ok)
165 continue;
167 dlg_select_widget (learnkeys[i].button);
168 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
169 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
170 learnkeys[i].ok = 1;
171 learnok++;
172 if (learnok >= learn_total) {
173 learn_dlg->ret_value = B_CANCEL;
174 if (learnchanged) {
175 if (query_dialog (learn_title,
177 ("It seems that all your keys already\n"
178 "work fine. That's great."), D_ERROR, 2,
179 _("&Save"), _("&Discard")) == 0)
180 learn_dlg->ret_value = B_ENTER;
181 } else {
182 message (D_ERROR, learn_title,
184 ("Great! You have a complete terminal database!\n"
185 "All your keys work well."));
187 dlg_stop (learn_dlg);
189 return 1;
191 switch (c) {
192 case KEY_LEFT:
193 case 'h':
194 return learn_move (0);
195 case KEY_RIGHT:
196 case 'l':
197 return learn_move (1);
198 case 'j':
199 dlg_one_down (learn_dlg);
200 return 1;
201 case 'k':
202 dlg_one_up (learn_dlg);
203 return 1;
206 /* Prevent from disappearing if a non-defined sequence is pressed
207 and contains a button hotkey. Only recognize hotkeys with ALT. */
208 if (c < 255 && g_ascii_isalnum (c))
209 return 1;
211 return 0;
214 static cb_ret_t
215 learn_callback (Dlg_head *h, Widget *sender,
216 dlg_msg_t msg, int parm, void *data)
218 switch (msg) {
219 case DLG_DRAW:
220 common_dialog_repaint (h);
221 return MSG_HANDLED;
223 case DLG_KEY:
224 return learn_check_key (parm);
226 default:
227 return default_dlg_callback (h, sender, msg, parm, data);
231 static void
232 init_learn (void)
234 int x, y, i, j;
235 const key_code_name_t *key;
236 char buffer[BUF_TINY];
238 #ifdef ENABLE_NLS
239 static int i18n_flag = 0;
240 if (!i18n_flag) {
241 learn_but[0].text = _(learn_but[0].text);
242 learn_but[0].x = 78 / 2 + 4;
244 learn_but[1].text = _(learn_but[1].text);
245 learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
247 learn_title = _(learn_title);
248 i18n_flag = 1;
250 #endif /* ENABLE_NLS */
252 do_refresh ();
254 learn_dlg =
255 create_dlg (0, 0, 23, 78, dialog_colors, learn_callback,
256 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
258 for (i = 0; i < BUTTONS; i++)
259 add_widget (learn_dlg,
260 button_new (BY + learn_but[i].y, learn_but[i].x,
261 learn_but[i].ret_cmd, learn_but[i].flags,
262 _(learn_but[i].text), 0));
264 x = UX;
265 y = UY;
266 for (key = key_name_conv_tab, j = 0;
267 key->name != NULL && strcmp (key->name, "kpleft");
268 key++, j++)
270 learnkeys = g_new (learnkey, j);
271 x += ((j - 1) / ROWS) * COLSHIFT;
272 y += (j - 1) % ROWS;
273 learn_total = j;
274 learnok = 0;
275 learnchanged = 0;
276 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
277 learnkeys[i].ok = 0;
278 learnkeys[i].sequence = NULL;
279 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
280 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
281 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
282 learn_button));
283 add_widget (learn_dlg, learnkeys[i].label = (Widget *)
284 label_new (y, x + 19, ""));
285 if (i % 13)
286 y--;
287 else {
288 x -= COLSHIFT;
289 y = UY + ROWS - 1;
292 add_widget (learn_dlg,
293 label_new (UY + 14, 5,
295 ("Press all the keys mentioned here. After you have done it, check")));
296 add_widget (learn_dlg,
297 label_new (UY + 15, 5,
299 ("which keys are not marked with OK. Press space on the missing")));
300 add_widget (learn_dlg,
301 label_new (UY + 16, 5,
303 ("key, or click with the mouse to define it. Move around with Tab.")));
306 static void learn_done (void)
308 destroy_dlg (learn_dlg);
309 repaint_screen ();
312 static void
313 learn_save (void)
315 int i;
316 int profile_changed = 0;
317 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
318 char *esc_str;
320 for (i = 0; i < learn_total; i++) {
321 if (learnkeys [i].sequence != NULL) {
322 profile_changed = 1;
324 esc_str = strutils_escape (learnkeys [i].sequence, -1, ";\\", TRUE);
326 mc_config_direct_set_string(mc_main_config, section,
327 key_name_conv_tab [i].name, esc_str);
329 g_free(esc_str);
333 /* On the one hand no good idea to save the complete setup but
334 * without 'Auto save setup' the new key-definitions will not be
335 * saved unless the user does an 'Options/Save Setup'.
336 * On the other hand a save-button that does not save anything to
337 * disk is much worse.
339 if (profile_changed)
340 mc_config_save_file (mc_main_config, NULL);
342 g_free (section);
345 void learn_keys (void)
347 int save_old_esc_mode = old_esc_mode;
348 int save_alternate_plus_minus = alternate_plus_minus;
350 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
351 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
352 KP_MULTIPLY to '+', '-' and '*' in
353 correct_key_code */
354 application_keypad_mode ();
355 init_learn ();
357 run_dlg (learn_dlg);
359 old_esc_mode = save_old_esc_mode;
360 alternate_plus_minus = save_alternate_plus_minus;
362 if (!alternate_plus_minus)
363 numeric_keypad_mode ();
365 switch (learn_dlg->ret_value) {
366 case B_ENTER:
367 learn_save ();
368 break;
371 learn_done ();