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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <sys/types.h>
37 #include "dialog.h" /* For do_refresh() */
38 #include "profile.h" /* Save profile */
56 int ret_cmd
, flags
, y
, x
;
59 } learn_but
[BUTTONS
] = {
60 { B_CANCEL
, NORMAL_BUTTON
, 0, 39, 'C', N_("&Cancel") },
61 { B_ENTER
, DEFPUSH_BUTTON
, 0, 25, 'S', N_("&Save") }
64 static Dlg_head
*learn_dlg
;
71 static learnkey
*learnkeys
= NULL
;
72 static int learn_total
;
74 static int learnchanged
;
75 static char* learn_title
= N_("Learn keys");
78 static int learn_button (int action
, void *param
)
81 Dlg_head
*d
= message (D_INSERT
| 1, _(" Teach me a key "),
82 _("Please press the %s\n"
83 "and then wait until this message disappears.\n\n"
84 "Then, press it again to see if OK appears\n"
85 "next to its button.\n\n"
86 "If you want to escape, press a single Escape key\n"
88 _(key_name_conv_tab
[action
- B_USER
].longname
));
90 if (learnkeys
[action
- B_USER
].sequence
!= NULL
) {
91 g_free (learnkeys
[action
- B_USER
].sequence
);
92 learnkeys
[action
- B_USER
].sequence
= NULL
;
97 /* Esc hides the dialog and do not allow definitions of
102 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
103 && strcmp (seq
, "^m" ) && strcmp (seq
, "^i" )
104 && (seq
[1] || (*seq
< ' ' || *seq
> '~'))){
107 learnkeys
[action
- B_USER
].sequence
= seq
;
108 seq
= convert_controls (seq
);
109 seq_ok
= define_sequence (key_name_conv_tab
[action
- B_USER
].code
,
110 seq
, MCKEY_NOACTION
);
116 message (0, _(" Cannot accept this key "),
117 _(" You have entered \"%s\""), seq
);
125 dlg_select_widget (learn_dlg
, learnkeys
[action
- B_USER
].button
);
126 return 0; /* Do not kill learn_dlg */
129 static int learn_move (int right
)
133 totalcols
= (learn_total
- 1) / ROWS
+ 1;
134 for (i
= 0; i
< learn_total
; i
++)
135 if (learnkeys
[i
].button
== learn_dlg
->current
->widget
) {
137 if (i
< learn_total
- ROWS
)
144 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
145 i
+= (totalcols
- 2) * ROWS
;
147 i
+= (totalcols
- 1) * ROWS
;
149 dlg_select_widget (learn_dlg
, (void *) learnkeys
[i
].button
);
156 learn_check_key (int c
)
160 for (i
= 0; i
< learn_total
; i
++) {
161 if (key_name_conv_tab
[i
].code
!= c
|| learnkeys
[i
].ok
)
164 dlg_select_widget (learn_dlg
, learnkeys
[i
].button
);
165 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
166 label_set_text ((WLabel
*) learnkeys
[i
].label
, _("OK"));
169 if (learnok
>= learn_total
) {
170 learn_dlg
->ret_value
= B_CANCEL
;
172 if (query_dialog (learn_title
,
174 ("It seems that all your keys already\n"
175 "work fine. That's great."), 1, 2,
176 _("&Save"), _("&Discard")) == 0)
177 learn_dlg
->ret_value
= B_ENTER
;
179 message (1, learn_title
,
181 ("Great! You have a complete terminal database!\n"
182 "All your keys work well."));
184 dlg_stop (learn_dlg
);
191 return learn_move (0);
194 return learn_move (1);
196 dlg_one_down (learn_dlg
);
199 dlg_one_up (learn_dlg
);
203 /* Prevent from disappearing if a non-defined sequence is pressed
204 and contains s or c. Use ALT('s') or ALT('c'). */
205 if (c
< 255 && isalpha (c
)) {
207 for (i
= 0; i
< BUTTONS
; i
++)
208 if (c
== learn_but
[i
].hotkey
)
215 static int learn_callback (Dlg_head
* h
, int Par
, int Msg
)
219 common_dialog_repaint (h
);
222 return learn_check_key (Par
);
227 static void init_learn (void)
230 key_code_name_t
*key
;
231 char buffer
[BUF_TINY
];
234 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 - (strlen (learn_but
[1].text
) + 9);
245 for (i
= 0; i
< BUTTONS
; i
++)
247 cp
= strchr(learn_but
[i
].text
, '&');
248 if (cp
!= NULL
&& *++cp
!= '\0')
249 learn_but
[i
].hotkey
= toupper(*cp
);
252 learn_title
= _(learn_title
);
255 #endif /* ENABLE_NLS */
259 learn_dlg
= create_dlg (0, 0, 23, 78, dialog_colors
,
260 learn_callback
, "[Learn keys]",
261 learn_title
, DLG_CENTER
);
263 #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
265 for (i
= 0; i
< BUTTONS
; i
++)
266 add_widget (learn_dlg
, button_new (XTRACT (i
)));
270 for (key
= key_name_conv_tab
, j
= 0; key
->name
!= NULL
&&
271 strcmp (key
->name
, "kpleft"); key
++, j
++);
272 learnkeys
= g_new (learnkey
, j
);
273 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
278 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--) {
279 learnkeys
[i
].ok
= 0;
280 learnkeys
[i
].sequence
= NULL
;
281 g_snprintf (buffer
, sizeof (buffer
), "%-16s", _(key
->longname
));
282 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
283 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
, learn_button
, 0, NULL
));
284 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*)
285 label_new (y
, x
+ 19, "", NULL
));
293 add_widget (learn_dlg
,
294 label_new (UY
+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL
));
295 add_widget (learn_dlg
,
296 label_new (UY
+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL
));
297 add_widget (learn_dlg
,
298 label_new (UY
+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL
));
301 static void learn_done (void)
303 destroy_dlg (learn_dlg
);
311 int profile_changed
= 0;
312 char *section
= g_strconcat ("terminal:", getenv ("TERM"), NULL
);
314 for (i
= 0; i
< learn_total
; i
++) {
315 if (learnkeys
[i
].sequence
!= NULL
) {
317 WritePrivateProfileString (section
, key_name_conv_tab
[i
].name
,
318 learnkeys
[i
].sequence
, profile_name
);
322 /* On the one hand no good idea to save the complete setup but
323 * without 'Auto save setup' the new key-definitions will not be
324 * saved unless the user does an 'Options/Save Setup'.
325 * On the other hand a save-button that does not save anything to
326 * disk is much worse.
334 void learn_keys (void)
336 int save_old_esc_mode
= old_esc_mode
;
337 int save_alternate_plus_minus
= alternate_plus_minus
;
339 old_esc_mode
= 0; /* old_esc_mode cannot work in learn keys dialog */
340 alternate_plus_minus
= 1; /* don't translate KP_ADD, KP_SUBTRACT and
341 KP_MULTIPLY to '+', '-' and '*' in
343 application_keypad_mode ();
348 old_esc_mode
= save_old_esc_mode
;
349 alternate_plus_minus
= save_alternate_plus_minus
;
351 if (!alternate_plus_minus
)
352 numeric_keypad_mode ();
354 switch (learn_dlg
->ret_value
) {