2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <stdlib.h> /* For malloc() */
28 #include <sys/types.h>
29 #include <sys/param.h>
33 #include "util.h" /* Needed for the externs and convert_controls */
38 #include "dialog.h" /* For do_refresh() */
39 #include "profile.h" /* Save profile */
54 int ret_cmd
, flags
, y
, x
;
57 } learn_but
[BUTTONS
] = {
58 { B_CANCEL
, NORMAL_BUTTON
, 0, 39, 'C', N_("&Cancel") },
59 { B_ENTER
, DEFPUSH_BUTTON
, 0, 25, 'S', N_("&Save") }
62 static Dlg_head
*learn_dlg
;
69 static learnkey
*learnkeys
= NULL
;
70 static int learn_total
;
72 static int learnchanged
;
73 static char* learn_title
= N_(" Learn keys ");
76 static void learn_refresh (void)
78 attrset (COLOR_NORMAL
);
79 dlg_erase (learn_dlg
);
81 draw_box (learn_dlg
, 1, 2, learn_dlg
->lines
- 2, learn_dlg
->cols
- 4);
83 attrset (COLOR_HOT_NORMAL
);
84 dlg_move (learn_dlg
, 1, (learn_dlg
->cols
- strlen (learn_title
)) / 2);
89 static int learn_button (int action
, void *param
)
92 Dlg_head
*d
= message (D_INSERT
| 1, _(" Teach me a key "),
93 _("Please press the %s\n"
94 "and then wait until this message disappears.\n\n"
95 "Then, press it again to see if OK appears\n"
96 "next to its button.\n\n"
97 "If you want to escape, press a single Escape key\n"
99 _(key_name_conv_tab
[action
- B_USER
].longname
));
101 if (learnkeys
[action
- B_USER
].sequence
!= NULL
) {
102 free (learnkeys
[action
- B_USER
].sequence
);
103 learnkeys
[action
- B_USER
].sequence
= NULL
;
108 /* Esc hides the dialog and do not allow definitions of
111 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
112 && strcmp (seq
, "^m" )
113 && (seq
[1] || (*seq
< ' ' || *seq
> '~'))){
116 learnkeys
[action
- B_USER
].sequence
= seq
;
117 seq
= convert_controls (seq
);
118 define_sequence (key_name_conv_tab
[action
- B_USER
].code
, seq
,
121 message (0, _(" Cannot accept this key "),
122 _(" You have entered \"%s\""), seq
);
130 dlg_select_widget (learn_dlg
, learnkeys
[action
- B_USER
].button
);
131 return 0; /* Do not kill learn_dlg */
134 static int learn_move (int right
)
138 totalcols
= (learn_total
- 1) / ROWS
+ 1;
139 for (i
= 0; i
< learn_total
; i
++)
140 if (learnkeys
[i
].button
== learn_dlg
->current
->widget
) {
142 if (i
< learn_total
- ROWS
)
149 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
150 i
+= (totalcols
- 2) * ROWS
;
152 i
+= (totalcols
- 1) * ROWS
;
154 dlg_select_widget (learn_dlg
, (void *) learnkeys
[i
].button
);
160 static int learn_check_key (int c
)
164 for (i
= 0; i
< learn_total
; i
++) {
165 if (key_name_conv_tab
[i
].code
== c
) {
166 if (!learnkeys
[i
].ok
) {
167 dlg_select_widget (learn_dlg
, learnkeys
[i
].button
);
168 label_set_text ((WLabel
*) learnkeys
[i
].label
,
170 learnkeys
[i
].ok
= 1;
172 if (learnok
>= learn_total
) {
173 learn_dlg
->ret_value
= B_CANCEL
;
175 if (query_dialog (learn_title
,
176 _("It seems that all your keys already\n"
177 "work fine. That's great."),
178 1, 2, _("&Save"), _("&Discard")) == 0)
179 learn_dlg
->ret_value
= B_ENTER
;
181 message (1, learn_title
,
182 _("Great! You have a complete terminal database!\n"
183 "All your keys work well."));
185 dlg_stop (learn_dlg
);
194 return learn_move (0);
197 return learn_move (1);
199 dlg_one_down (learn_dlg
);
202 dlg_one_up (learn_dlg
);
206 /* Prevent from disappearing if a non-defined sequence is pressed
207 and contains s or c. Use ALT('s') or ALT('c'). */
208 if (c
< 255 && isalpha(c
))
211 for (i
= 0; i
< BUTTONS
; i
++)
212 if (c
== learn_but
[i
].hotkey
)
219 static int learn_callback (Dlg_head
* h
, int Par
, int Msg
)
226 return learn_check_key (Par
);
231 static void init_learn (void)
234 key_code_name_t
*key
;
236 static int i18n_flag
= 0;
245 learn_but
[0].text
= _(learn_but
[0].text
);
246 learn_but
[0].x
= 78 / 2 + 4;
248 learn_but
[1].text
= _(learn_but
[1].text
);
249 learn_but
[1].x
= 78 / 2 - (strlen (learn_but
[1].text
) + 9);
251 for (i
= 0; i
< BUTTONS
; i
++)
253 cp
= strchr(learn_but
[i
].text
, '&');
254 if (cp
!= NULL
&& *++cp
!= '\0')
255 learn_but
[i
].hotkey
= toupper(*cp
);
258 learn_title
= _(learn_title
);
261 #endif /* ENABLE_NLS */
263 learn_dlg
= create_dlg (0, 0, 23, 78, dialog_colors
,
264 learn_callback
, "[Learn keys]", "Learn keys",
266 x_set_dialog_title (learn_dlg
, _("Learn keys"));
268 #define XTRACT(i) BY+learn_but[i].y, learn_but[i].x, learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0, 0, NULL
270 for (i
= 0; i
< BUTTONS
; i
++)
271 add_widget (learn_dlg
, button_new (XTRACT (i
)));
275 for (key
= key_name_conv_tab
, j
= 0; key
->name
!= NULL
&&
276 strcmp (key
->name
, "kpleft"); key
++, j
++);
277 learnkeys
= (learnkey
*) xmalloc (sizeof (learnkey
) * j
, "Learn keys");
278 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
283 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--) {
284 learnkeys
[i
].ok
= 0;
285 learnkeys
[i
].sequence
= NULL
;
286 sprintf (buffer
, "%-16s", _(key
->longname
));
287 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
288 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
, learn_button
, 0, NULL
));
289 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*)
290 label_new (y
, x
+ 19, "", NULL
));
298 add_widget (learn_dlg
,
299 label_new (UY
+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL
));
300 add_widget (learn_dlg
,
301 label_new (UY
+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL
));
302 add_widget (learn_dlg
,
303 label_new (UY
+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL
));
306 static void learn_done (void)
308 destroy_dlg (learn_dlg
);
312 void learn_save (void)
315 int profile_changed
= 0;
316 char *section
= copy_strings ("terminal:", getenv ("TERM"), NULL
);
318 for (i
= 0; i
< learn_total
; i
++) {
319 if (learnkeys
[i
].sequence
!= NULL
) {
321 WritePrivateProfileString (section
, key_name_conv_tab
[i
].name
,
322 learnkeys
[i
].sequence
, profile_name
);
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.
336 void learn_keys (void)
338 int save_old_esc_mode
= old_esc_mode
;
339 int save_alternate_plus_minus
= alternate_plus_minus
;
341 old_esc_mode
= 0; /* old_esc_mode cannot work in learn keys dialog */
342 alternate_plus_minus
= 1; /* don't translate KP_ADD, KP_SUBTRACT and
343 KP_MULTIPLY to '+', '-' and '*' in
346 application_keypad_mode ();
352 old_esc_mode
= save_old_esc_mode
;
353 alternate_plus_minus
= save_alternate_plus_minus
;
356 if (!alternate_plus_minus
)
357 numeric_keypad_mode ();
361 switch (learn_dlg
->ret_value
) {