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.
23 * \brief Source: learn keys module
33 #include <sys/types.h>
39 #include "../src/tty/tty.h"
40 #include "../src/tty/key.h"
44 #include "../src/mcconfig/mcconfig.h" /* Save profile */
46 #include "layout.h" /* repaint_screen() */
62 int ret_cmd
, flags
, y
, x
;
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
;
76 static learnkey
*learnkeys
= NULL
;
77 static int learn_total
;
79 static int learnchanged
;
80 static const char* learn_title
= N_("Learn keys");
83 static int learn_button (int action
)
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"
93 _(key_name_conv_tab
[action
- B_USER
].longname
));
95 if (learnkeys
[action
- B_USER
].sequence
!= NULL
) {
96 g_free (learnkeys
[action
- B_USER
].sequence
);
97 learnkeys
[action
- B_USER
].sequence
= NULL
;
102 /* Esc hides the dialog and do not allow definitions of
105 gboolean seq_ok
= FALSE
;
107 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
108 && strcmp (seq
, "^m" ) && strcmp (seq
, "^i" )
109 && (seq
[1] || (*seq
< ' ' || *seq
> '~'))){
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
);
119 message (D_NORMAL
, _(" Cannot accept this key "),
120 _(" You have entered \"%s\""), seq
);
127 dlg_select_widget (learnkeys
[action
- B_USER
].button
);
128 return 0; /* Do not kill learn_dlg */
131 static int learn_move (int right
)
135 totalcols
= (learn_total
- 1) / ROWS
+ 1;
136 for (i
= 0; i
< learn_total
; i
++)
137 if (learnkeys
[i
].button
== learn_dlg
->current
) {
139 if (i
< learn_total
- ROWS
)
146 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
147 i
+= (totalcols
- 2) * ROWS
;
149 i
+= (totalcols
- 1) * ROWS
;
151 dlg_select_widget (learnkeys
[i
].button
);
158 learn_check_key (int c
)
162 for (i
= 0; i
< learn_total
; i
++) {
163 if (key_name_conv_tab
[i
].code
!= c
|| learnkeys
[i
].ok
)
166 dlg_select_widget (learnkeys
[i
].button
);
167 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
168 label_set_text ((WLabel
*) learnkeys
[i
].label
, _("OK"));
171 if (learnok
>= learn_total
) {
172 learn_dlg
->ret_value
= B_CANCEL
;
174 if (query_dialog (learn_title
,
176 ("It seems that all your keys already\n"
177 "work fine. That's great."), D_ERROR
, 2,
178 _("&Save"), _("&Discard")) == 0)
179 learn_dlg
->ret_value
= B_ENTER
;
181 message (D_ERROR
, learn_title
,
183 ("Great! You have a complete terminal database!\n"
184 "All your keys work well."));
186 dlg_stop (learn_dlg
);
193 return learn_move (0);
196 return learn_move (1);
198 dlg_one_down (learn_dlg
);
201 dlg_one_up (learn_dlg
);
205 /* Prevent from disappearing if a non-defined sequence is pressed
206 and contains a button hotkey. Only recognize hotkeys with ALT. */
207 if (c
< 255 && g_ascii_isalnum (c
))
214 learn_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
218 common_dialog_repaint (h
);
222 return learn_check_key (parm
);
225 return default_dlg_callback (h
, msg
, parm
);
233 key_code_name_t
*key
;
234 char buffer
[BUF_TINY
];
237 static int i18n_flag
= 0;
239 learn_but
[0].text
= _(learn_but
[0].text
);
240 learn_but
[0].x
= 78 / 2 + 4;
242 learn_but
[1].text
= _(learn_but
[1].text
);
243 learn_but
[1].x
= 78 / 2 - (str_term_width1 (learn_but
[1].text
) + 9);
245 learn_title
= _(learn_title
);
248 #endif /* ENABLE_NLS */
253 create_dlg (0, 0, 23, 78, dialog_colors
, learn_callback
,
254 "[Learn keys]", learn_title
, DLG_CENTER
| DLG_REVERSE
);
256 for (i
= 0; i
< BUTTONS
; i
++)
257 add_widget (learn_dlg
,
258 button_new (BY
+ learn_but
[i
].y
, learn_but
[i
].x
,
259 learn_but
[i
].ret_cmd
, learn_but
[i
].flags
,
260 _(learn_but
[i
].text
), 0));
264 for (key
= key_name_conv_tab
, j
= 0;
265 key
->name
!= NULL
&& strcmp (key
->name
, "kpleft"); key
++, j
++);
266 learnkeys
= g_new (learnkey
, j
);
267 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
272 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--) {
274 learnkeys
[i
].sequence
= NULL
;
275 g_snprintf (buffer
, sizeof (buffer
), "%-16s", _(key
->longname
));
276 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
277 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
,
279 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*)
280 label_new (y
, x
+ 19, ""));
288 add_widget (learn_dlg
,
289 label_new (UY
+ 14, 5,
291 ("Press all the keys mentioned here. After you have done it, check")));
292 add_widget (learn_dlg
,
293 label_new (UY
+ 15, 5,
295 ("which keys are not marked with OK. Press space on the missing")));
296 add_widget (learn_dlg
,
297 label_new (UY
+ 16, 5,
299 ("key, or click with the mouse to define it. Move around with Tab.")));
302 static void learn_done (void)
304 destroy_dlg (learn_dlg
);
312 int profile_changed
= 0;
313 char *section
= g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL
);
315 for (i
= 0; i
< learn_total
; i
++) {
316 if (learnkeys
[i
].sequence
!= NULL
) {
318 mc_config_set_string(mc_main_config
, section
,
319 key_name_conv_tab
[i
].name
, learnkeys
[i
].sequence
);
323 /* On the one hand no good idea to save the complete setup but
324 * without 'Auto save setup' the new key-definitions will not be
325 * saved unless the user does an 'Options/Save Setup'.
326 * On the other hand a save-button that does not save anything to
327 * disk is much worse.
330 mc_config_save_file (mc_main_config
);
335 void learn_keys (void)
337 int save_old_esc_mode
= old_esc_mode
;
338 int save_alternate_plus_minus
= alternate_plus_minus
;
340 old_esc_mode
= 0; /* old_esc_mode cannot work in learn keys dialog */
341 alternate_plus_minus
= 1; /* don't translate KP_ADD, KP_SUBTRACT and
342 KP_MULTIPLY to '+', '-' and '*' in
344 application_keypad_mode ();
349 old_esc_mode
= save_old_esc_mode
;
350 alternate_plus_minus
= save_alternate_plus_minus
;
352 if (!alternate_plus_minus
)
353 numeric_keypad_mode ();
355 switch (learn_dlg
->ret_value
) {