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>
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"
44 #include "lib/util.h" /* convert_controls() */
45 #include "lib/widget.h"
47 #include "filemanager/layout.h" /* repaint_screen() */
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
66 /*** file scope type declarations ****************************************************************/
76 /*** file scope variables ************************************************************************/
80 int ret_cmd
, flags
, y
, x
;
82 } learn_but
[BUTTONS
] =
86 B_CANCEL
, NORMAL_BUTTON
, 0, 39, N_("&Cancel")},
88 B_ENTER
, DEFPUSH_BUTTON
, 0, 25, N_("&Save")}
92 static Dlg_head
*learn_dlg
;
94 static learnkey
*learnkeys
= NULL
;
95 static int learn_total
;
97 static int learnchanged
;
98 static const char *learn_title
= N_("Learn keys");
100 /*** file scope functions ************************************************************************/
101 /* --------------------------------------------------------------------------------------------- */
104 learn_button (WButton
* button
, int action
)
111 d
= create_message (D_ERROR
, _("Teach me a key"),
112 _("Please press the %s\n"
113 "and then wait until this message disappears.\n\n"
114 "Then, press it again to see if OK appears\n"
115 "next to its button.\n\n"
116 "If you want to escape, press a single Escape key\n"
117 "and wait as well."), _(key_name_conv_tab
[action
- B_USER
].longname
));
119 if (learnkeys
[action
- B_USER
].sequence
!= NULL
)
121 g_free (learnkeys
[action
- B_USER
].sequence
);
122 learnkeys
[action
- B_USER
].sequence
= NULL
;
128 /* Esc hides the dialog and do not allow definitions of
131 gboolean seq_ok
= FALSE
;
133 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
134 && strcmp (seq
, "^m") && strcmp (seq
, "^i") && (seq
[1] || (*seq
< ' ' || *seq
> '~')))
138 learnkeys
[action
- B_USER
].sequence
= seq
;
139 seq
= convert_controls (seq
);
140 seq_ok
= define_sequence (key_name_conv_tab
[action
- B_USER
].code
, seq
, MCKEY_NOACTION
);
144 message (D_NORMAL
, _("Cannot accept this key"), _("You have entered \"%s\""), seq
);
151 dlg_select_widget (learnkeys
[action
- B_USER
].button
);
152 return 0; /* Do not kill learn_dlg */
155 /* --------------------------------------------------------------------------------------------- */
158 learn_move (int right
)
162 totalcols
= (learn_total
- 1) / ROWS
+ 1;
163 for (i
= 0; i
< learn_total
; i
++)
164 if (learnkeys
[i
].button
== (Widget
*) learn_dlg
->current
->data
)
168 if (i
< learn_total
- ROWS
)
177 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
178 i
+= (totalcols
- 2) * ROWS
;
180 i
+= (totalcols
- 1) * ROWS
;
182 dlg_select_widget (learnkeys
[i
].button
);
188 /* --------------------------------------------------------------------------------------------- */
191 learn_check_key (int c
)
195 for (i
= 0; i
< learn_total
; i
++)
197 if (key_name_conv_tab
[i
].code
!= c
|| learnkeys
[i
].ok
)
200 dlg_select_widget (learnkeys
[i
].button
);
201 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
202 label_set_text ((WLabel
*) learnkeys
[i
].label
, _("OK"));
205 if (learnok
>= learn_total
)
207 learn_dlg
->ret_value
= B_CANCEL
;
210 if (query_dialog (learn_title
,
212 ("It seems that all your keys already\n"
213 "work fine. That's great."), D_ERROR
, 2,
214 _("&Save"), _("&Discard")) == 0)
215 learn_dlg
->ret_value
= B_ENTER
;
219 message (D_ERROR
, learn_title
,
221 ("Great! You have a complete terminal database!\n"
222 "All your keys work well."));
224 dlg_stop (learn_dlg
);
232 return learn_move (0);
235 return learn_move (1);
237 dlg_one_down (learn_dlg
);
240 dlg_one_up (learn_dlg
);
244 /* Prevent from disappearing if a non-defined sequence is pressed
245 and contains a button hotkey. Only recognize hotkeys with ALT. */
246 if (c
< 255 && g_ascii_isalnum (c
))
252 /* --------------------------------------------------------------------------------------------- */
255 learn_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
260 common_dialog_repaint (h
);
264 return learn_check_key (parm
);
267 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
271 /* --------------------------------------------------------------------------------------------- */
277 const key_code_name_t
*key
;
278 char buffer
[BUF_TINY
];
281 static int i18n_flag
= 0;
284 learn_but
[0].text
= _(learn_but
[0].text
);
285 learn_but
[0].x
= 78 / 2 + 4;
287 learn_but
[1].text
= _(learn_but
[1].text
);
288 learn_but
[1].x
= 78 / 2 - (str_term_width1 (learn_but
[1].text
) + 9);
290 learn_title
= _(learn_title
);
293 #endif /* ENABLE_NLS */
298 create_dlg (TRUE
, 0, 0, 23, 78, dialog_colors
, learn_callback
,
299 "[Learn keys]", learn_title
, DLG_CENTER
| DLG_REVERSE
);
301 for (i
= 0; i
< BUTTONS
; i
++)
302 add_widget (learn_dlg
,
303 button_new (BY
+ learn_but
[i
].y
, learn_but
[i
].x
,
304 learn_but
[i
].ret_cmd
, learn_but
[i
].flags
, _(learn_but
[i
].text
), 0));
308 for (key
= key_name_conv_tab
, j
= 0;
309 key
->name
!= NULL
&& strcmp (key
->name
, "kpleft"); key
++, j
++)
311 learnkeys
= g_new (learnkey
, j
);
312 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
317 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--)
320 learnkeys
[i
].sequence
= NULL
;
321 g_snprintf (buffer
, sizeof (buffer
), "%-16s", _(key
->longname
));
322 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
323 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
, learn_button
));
324 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*) label_new (y
, x
+ 19, ""));
333 add_widget (learn_dlg
,
334 label_new (UY
+ 14, 5,
335 _("Press all the keys mentioned here. After you have done it, check")));
336 add_widget (learn_dlg
,
337 label_new (UY
+ 15, 5,
338 _("which keys are not marked with OK. Press space on the missing")));
339 add_widget (learn_dlg
,
340 label_new (UY
+ 16, 5,
341 _("key, or click with the mouse to define it. Move around with Tab.")));
344 /* --------------------------------------------------------------------------------------------- */
349 destroy_dlg (learn_dlg
);
353 /* --------------------------------------------------------------------------------------------- */
359 int profile_changed
= 0;
360 char *section
= g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL
);
363 for (i
= 0; i
< learn_total
; i
++)
365 if (learnkeys
[i
].sequence
!= NULL
)
369 esc_str
= strutils_escape (learnkeys
[i
].sequence
, -1, ";\\", TRUE
);
371 mc_config_set_string_raw (mc_main_config
, section
, key_name_conv_tab
[i
].name
, esc_str
);
377 /* On the one hand no good idea to save the complete setup but
378 * without 'Auto save setup' the new key-definitions will not be
379 * saved unless the user does an 'Options/Save Setup'.
380 * On the other hand a save-button that does not save anything to
381 * disk is much worse.
384 mc_config_save_file (mc_main_config
, NULL
);
389 /* --------------------------------------------------------------------------------------------- */
390 /*** public functions ****************************************************************************/
391 /* --------------------------------------------------------------------------------------------- */
396 int save_old_esc_mode
= old_esc_mode
;
397 int save_alternate_plus_minus
= alternate_plus_minus
;
399 old_esc_mode
= 0; /* old_esc_mode cannot work in learn keys dialog */
400 alternate_plus_minus
= 1; /* don't translate KP_ADD, KP_SUBTRACT and
401 KP_MULTIPLY to '+', '-' and '*' in
403 application_keypad_mode ();
408 old_esc_mode
= save_old_esc_mode
;
409 alternate_plus_minus
= save_alternate_plus_minus
;
411 if (!alternate_plus_minus
)
412 numeric_keypad_mode ();
414 switch (learn_dlg
->ret_value
)
424 /* --------------------------------------------------------------------------------------------- */