mc/src/learn.c

364 lines
9.2 KiB
C
Raw Normal View History

1998-02-27 07:54:42 +03:00
/* Learn keys
Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2007, 2009 Free Software Foundation, Inc.
1998-02-27 07:54:42 +03:00
Written by: 1995 Jakub Jelinek
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1998-02-27 07:54:42 +03:00
*/
/** \file learn.c
* \brief Source: learn keys module
*/
1998-02-27 07:54:42 +03:00
#include <config.h>
2005-02-08 12:04:03 +03:00
#include <ctype.h>
1998-02-27 07:54:42 +03:00
#include <stdio.h>
2005-02-08 12:04:03 +03:00
#include <stdlib.h>
#include <string.h>
1998-02-27 07:54:42 +03:00
#include <sys/types.h>
#include <sys/stat.h>
2005-02-08 12:04:03 +03:00
#include <unistd.h>
#include "global.h"
#include "../src/tty/tty.h"
#include "../src/tty/key.h"
#include "dialog.h"
1998-02-27 07:54:42 +03:00
#include "widget.h"
#include "../src/mcconfig/mcconfig.h" /* Save profile */
1998-02-27 07:54:42 +03:00
#include "setup.h"
#include "layout.h" /* repaint_screen() */
1998-12-03 00:27:27 +03:00
#include "learn.h"
#include "wtools.h"
#include "strutil.h"
1998-12-03 00:27:27 +03:00
1998-02-27 07:54:42 +03:00
#define UX 4
#define UY 3
#define BY UY + 17
#define ROWS 13
#define COLSHIFT 23
#define BUTTONS 2
static struct {
1998-02-27 07:54:42 +03:00
int ret_cmd, flags, y, x;
const char *text;
1998-02-27 07:54:42 +03:00
} learn_but[BUTTONS] = {
{ B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel") },
{ B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save") }
1998-02-27 07:54:42 +03:00
};
static Dlg_head *learn_dlg;
typedef struct {
Widget *button;
Widget *label;
int ok;
char *sequence;
} learnkey;
static learnkey *learnkeys = NULL;
static int learn_total;
static int learnok;
static int learnchanged;
static const char* learn_title = N_("Learn keys");
1998-02-27 07:54:42 +03:00
static int learn_button (int action)
1998-02-27 07:54:42 +03:00
{
char *seq;
Dlg_head *d = create_message (D_ERROR, _(" Teach me a key "),
_("Please press the %s\n"
1998-02-27 07:54:42 +03:00
"and then wait until this message disappears.\n\n"
"Then, press it again to see if OK appears\n"
"next to its button.\n\n"
"If you want to escape, press a single Escape key\n"
"and wait as well."),
_(key_name_conv_tab [action - B_USER].longname));
mc_refresh ();
1998-02-27 07:54:42 +03:00
if (learnkeys [action - B_USER].sequence != NULL) {
g_free (learnkeys [action - B_USER].sequence);
1998-02-27 07:54:42 +03:00
learnkeys [action - B_USER].sequence = NULL;
}
seq = learn_key ();
if (seq){
/* Esc hides the dialog and do not allow definitions of
* regular characters
*/
gboolean seq_ok = FALSE;
1998-02-27 07:54:42 +03:00
if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
&& strcmp (seq, "^m" ) && strcmp (seq, "^i" )
1998-02-27 07:54:42 +03:00
&& (seq [1] || (*seq < ' ' || *seq > '~'))){
1998-02-27 07:54:42 +03:00
learnchanged = 1;
learnkeys [action - B_USER].sequence = seq;
seq = convert_controls (seq);
seq_ok = define_sequence (key_name_conv_tab [action - B_USER].code,
seq, MCKEY_NOACTION);
}
if (!seq_ok)
message (D_NORMAL, _(" Cannot accept this key "),
_(" You have entered \"%s\""), seq);
g_free (seq);
1998-02-27 07:54:42 +03:00
}
1998-02-27 07:54:42 +03:00
dlg_run_done (d);
destroy_dlg (d);
dlg_select_widget (learnkeys [action - B_USER].button);
1998-02-27 07:54:42 +03:00
return 0; /* Do not kill learn_dlg */
}
static int learn_move (int right)
{
int i, totalcols;
totalcols = (learn_total - 1) / ROWS + 1;
for (i = 0; i < learn_total; i++)
if (learnkeys [i].button == learn_dlg->current) {
1998-02-27 07:54:42 +03:00
if (right) {
if (i < learn_total - ROWS)
i += ROWS;
else
i %= ROWS;
} else {
if (i / ROWS)
i -= ROWS;
else if (i + (totalcols - 1) * ROWS >= learn_total)
i += (totalcols - 2) * ROWS;
else
i += (totalcols - 1) * ROWS;
}
dlg_select_widget (learnkeys [i].button);
1998-02-27 07:54:42 +03:00
return 1;
}
return 0;
}
static int
learn_check_key (int c)
1998-02-27 07:54:42 +03:00
{
int i;
for (i = 0; i < learn_total; i++) {
if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
continue;
dlg_select_widget (learnkeys[i].button);
/* TRANSLATORS: This label appears near learned keys. Keep it short. */
label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
learnkeys[i].ok = 1;
learnok++;
if (learnok >= learn_total) {
learn_dlg->ret_value = B_CANCEL;
if (learnchanged) {
if (query_dialog (learn_title,
_
("It seems that all your keys already\n"
"work fine. That's great."), D_ERROR, 2,
_("&Save"), _("&Discard")) == 0)
learn_dlg->ret_value = B_ENTER;
} else {
message (D_ERROR, learn_title,
_
("Great! You have a complete terminal database!\n"
"All your keys work well."));
1998-02-27 07:54:42 +03:00
}
dlg_stop (learn_dlg);
}
return 1;
1998-02-27 07:54:42 +03:00
}
switch (c) {
case KEY_LEFT:
case 'h':
return learn_move (0);
case KEY_RIGHT:
case 'l':
return learn_move (1);
case 'j':
dlg_one_down (learn_dlg);
return 1;
case 'k':
dlg_one_up (learn_dlg);
return 1;
}
/* Prevent from disappearing if a non-defined sequence is pressed
and contains a button hotkey. Only recognize hotkeys with ALT. */
if (c < 255 && g_ascii_isalnum (c))
return 1;
1998-02-27 07:54:42 +03:00
return 0;
}
static cb_ret_t
learn_callback (Dlg_head *h, dlg_msg_t msg, int parm)
1998-02-27 07:54:42 +03:00
{
switch (msg) {
1998-02-27 07:54:42 +03:00
case DLG_DRAW:
common_dialog_repaint (h);
return MSG_HANDLED;
1998-02-27 07:54:42 +03:00
case DLG_KEY:
return learn_check_key (parm);
default:
return default_dlg_callback (h, msg, parm);
1998-02-27 07:54:42 +03:00
}
}
static void
init_learn (void)
1998-02-27 07:54:42 +03:00
{
int x, y, i, j;
key_code_name_t *key;
char buffer[BUF_TINY];
1998-02-27 07:54:42 +03:00
#ifdef ENABLE_NLS
static int i18n_flag = 0;
if (!i18n_flag) {
learn_but[0].text = _(learn_but[0].text);
learn_but[0].x = 78 / 2 + 4;
learn_but[1].text = _(learn_but[1].text);
learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
learn_title = _(learn_title);
i18n_flag = 1;
}
#endif /* ENABLE_NLS */
do_refresh ();
1998-02-27 07:54:42 +03:00
learn_dlg =
create_dlg (0, 0, 23, 78, dialog_colors, learn_callback,
"[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
1998-02-27 07:54:42 +03:00
for (i = 0; i < BUTTONS; i++)
add_widget (learn_dlg,
button_new (BY + learn_but[i].y, learn_but[i].x,
learn_but[i].ret_cmd, learn_but[i].flags,
_(learn_but[i].text), 0));
1998-02-27 07:54:42 +03:00
x = UX;
y = UY;
for (key = key_name_conv_tab, j = 0;
key->name != NULL && strcmp (key->name, "kpleft"); key++, j++);
learnkeys = g_new (learnkey, j);
1998-02-27 07:54:42 +03:00
x += ((j - 1) / ROWS) * COLSHIFT;
y += (j - 1) % ROWS;
learn_total = j;
learnok = 0;
learnchanged = 0;
for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--) {
learnkeys[i].ok = 0;
learnkeys[i].sequence = NULL;
g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
add_widget (learn_dlg, learnkeys[i].button = (Widget *)
button_new (y, x, B_USER + i, NARROW_BUTTON, buffer,
learn_button));
add_widget (learn_dlg, learnkeys[i].label = (Widget *)
label_new (y, x + 19, ""));
1998-02-27 07:54:42 +03:00
if (i % 13)
y--;
else {
x -= COLSHIFT;
y = UY + ROWS - 1;
}
}
add_widget (learn_dlg,
label_new (UY + 14, 5,
_
("Press all the keys mentioned here. After you have done it, check")));
add_widget (learn_dlg,
label_new (UY + 15, 5,
_
("which keys are not marked with OK. Press space on the missing")));
add_widget (learn_dlg,
label_new (UY + 16, 5,
_
("key, or click with the mouse to define it. Move around with Tab.")));
1998-02-27 07:54:42 +03:00
}
static void learn_done (void)
{
destroy_dlg (learn_dlg);
repaint_screen ();
}
1998-12-03 00:27:27 +03:00
static void
learn_save (void)
1998-02-27 07:54:42 +03:00
{
int i;
int profile_changed = 0;
char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
1998-02-27 07:54:42 +03:00
for (i = 0; i < learn_total; i++) {
if (learnkeys [i].sequence != NULL) {
profile_changed = 1;
mc_config_set_string(mc_main_config, section,
key_name_conv_tab [i].name, learnkeys [i].sequence);
}
1998-02-27 07:54:42 +03:00
}
/* On the one hand no good idea to save the complete setup but
* without 'Auto save setup' the new key-definitions will not be
* saved unless the user does an 'Options/Save Setup'.
* On the other hand a save-button that does not save anything to
* disk is much worse.
*/
if (profile_changed)
mc_config_save_file (mc_main_config);
g_free (section);
1998-02-27 07:54:42 +03:00
}
void learn_keys (void)
{
int save_old_esc_mode = old_esc_mode;
Wed Apr 1 00:15:30 1998 Norbert Warmuth <k3190@fh-sw.de> * key.c, key.h (numeric_keypad_mode, application_keypad_mode): New functions which encapsulate two hardcoded escape sequences from main.c. * main.c (main): Use the two new functions from key.c * main.c, screen.c: Moved all file selection keys from the default keymap to the keymap for panels in listing mode. Changed *_selection_cmd to *_selection_cmd_panel in panel_keymap (functions in panel_keymap get a WPanel * as first parameter, i.e. the indirection with cpanel isn't necessary). * main.c (midnight_callback): Keys '*' and '-' were not treated when only_leading_plus_minus==0; Optimized the if-clauses a little bit (i.e. removed duplicate checks). More optimation is possible but it would make the whole stuff completly unreadable. * key.c (correct_key_code): KP_ADD, KP_SUBTRACT and KP_MULTIPLY will be translated to +, - and * only if the option alternate_plus_minus is turned off. * learn.c (learn_keys): Turn alternate_plus_minus temporarily on to avoid translation of KP_ADD, KP_SUBTRACT and KP_MULTIPLY in correct_key_code/make sure keypad is in application mode (makes it possible to learn this keys). * cmd.c (reverse_selection_cmd_panel): New function (renamed from reverse_selection_cmd, takes a WPanel * as parameter, references to cpanel changed to panel/the passed parameter). reverse_selection_cmd now simply calls this function with cpanel. This pair was missing among the *_selection_cmd* functions. * cmd.h: Added function prototypes.
1998-04-01 02:36:24 +04:00
int save_alternate_plus_minus = alternate_plus_minus;
1998-02-27 07:54:42 +03:00
old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
Wed Apr 1 00:15:30 1998 Norbert Warmuth <k3190@fh-sw.de> * key.c, key.h (numeric_keypad_mode, application_keypad_mode): New functions which encapsulate two hardcoded escape sequences from main.c. * main.c (main): Use the two new functions from key.c * main.c, screen.c: Moved all file selection keys from the default keymap to the keymap for panels in listing mode. Changed *_selection_cmd to *_selection_cmd_panel in panel_keymap (functions in panel_keymap get a WPanel * as first parameter, i.e. the indirection with cpanel isn't necessary). * main.c (midnight_callback): Keys '*' and '-' were not treated when only_leading_plus_minus==0; Optimized the if-clauses a little bit (i.e. removed duplicate checks). More optimation is possible but it would make the whole stuff completly unreadable. * key.c (correct_key_code): KP_ADD, KP_SUBTRACT and KP_MULTIPLY will be translated to +, - and * only if the option alternate_plus_minus is turned off. * learn.c (learn_keys): Turn alternate_plus_minus temporarily on to avoid translation of KP_ADD, KP_SUBTRACT and KP_MULTIPLY in correct_key_code/make sure keypad is in application mode (makes it possible to learn this keys). * cmd.c (reverse_selection_cmd_panel): New function (renamed from reverse_selection_cmd, takes a WPanel * as parameter, references to cpanel changed to panel/the passed parameter). reverse_selection_cmd now simply calls this function with cpanel. This pair was missing among the *_selection_cmd* functions. * cmd.h: Added function prototypes.
1998-04-01 02:36:24 +04:00
alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
KP_MULTIPLY to '+', '-' and '*' in
correct_key_code */
application_keypad_mode ();
1998-02-27 07:54:42 +03:00
init_learn ();
run_dlg (learn_dlg);
old_esc_mode = save_old_esc_mode;
Wed Apr 1 00:15:30 1998 Norbert Warmuth <k3190@fh-sw.de> * key.c, key.h (numeric_keypad_mode, application_keypad_mode): New functions which encapsulate two hardcoded escape sequences from main.c. * main.c (main): Use the two new functions from key.c * main.c, screen.c: Moved all file selection keys from the default keymap to the keymap for panels in listing mode. Changed *_selection_cmd to *_selection_cmd_panel in panel_keymap (functions in panel_keymap get a WPanel * as first parameter, i.e. the indirection with cpanel isn't necessary). * main.c (midnight_callback): Keys '*' and '-' were not treated when only_leading_plus_minus==0; Optimized the if-clauses a little bit (i.e. removed duplicate checks). More optimation is possible but it would make the whole stuff completly unreadable. * key.c (correct_key_code): KP_ADD, KP_SUBTRACT and KP_MULTIPLY will be translated to +, - and * only if the option alternate_plus_minus is turned off. * learn.c (learn_keys): Turn alternate_plus_minus temporarily on to avoid translation of KP_ADD, KP_SUBTRACT and KP_MULTIPLY in correct_key_code/make sure keypad is in application mode (makes it possible to learn this keys). * cmd.c (reverse_selection_cmd_panel): New function (renamed from reverse_selection_cmd, takes a WPanel * as parameter, references to cpanel changed to panel/the passed parameter). reverse_selection_cmd now simply calls this function with cpanel. This pair was missing among the *_selection_cmd* functions. * cmd.h: Added function prototypes.
1998-04-01 02:36:24 +04:00
alternate_plus_minus = save_alternate_plus_minus;
if (!alternate_plus_minus)
numeric_keypad_mode ();
1998-02-27 07:54:42 +03:00
switch (learn_dlg->ret_value) {
case B_ENTER:
learn_save ();
break;
}
learn_done ();
}