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 */
55 int ret_cmd
, flags
, y
, x
;
58 } learn_but
[BUTTONS
] = {
59 { B_CANCEL
, NORMAL_BUTTON
, 0, 39, 'C', N_("&Cancel") },
60 { B_ENTER
, DEFPUSH_BUTTON
, 0, 25, 'S', N_("&Save") }
63 static Dlg_head
*learn_dlg
;
70 static learnkey
*learnkeys
= NULL
;
71 static int learn_total
;
73 static int learnchanged
;
74 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);
88 static int learn_button (int action
, void *param
)
91 Dlg_head
*d
= message (D_INSERT
| 1, _(" Teach me a key "),
92 _("Please press the %s\n"
93 "and then wait until this message disappears.\n\n"
94 "Then, press it again to see if OK appears\n"
95 "next to its button.\n\n"
96 "If you want to escape, press a single Escape key\n"
98 _(key_name_conv_tab
[action
- B_USER
].longname
));
100 if (learnkeys
[action
- B_USER
].sequence
!= NULL
) {
101 g_free (learnkeys
[action
- B_USER
].sequence
);
102 learnkeys
[action
- B_USER
].sequence
= NULL
;
107 /* Esc hides the dialog and do not allow definitions of
112 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
113 && strcmp (seq
, "^m" )
114 && (seq
[1] || (*seq
< ' ' || *seq
> '~'))){
117 learnkeys
[action
- B_USER
].sequence
= seq
;
118 seq
= convert_controls (seq
);
119 seq_ok
= define_sequence (key_name_conv_tab
[action
- B_USER
].code
,
120 seq
, MCKEY_NOACTION
);
126 message (0, _(" Cannot accept this key "),
127 _(" You have entered \"%s\""), seq
);
135 dlg_select_widget (learn_dlg
, learnkeys
[action
- B_USER
].button
);
136 return 0; /* Do not kill learn_dlg */
139 static int learn_move (int right
)
143 totalcols
= (learn_total
- 1) / ROWS
+ 1;
144 for (i
= 0; i
< learn_total
; i
++)
145 if (learnkeys
[i
].button
== learn_dlg
->current
->widget
) {
147 if (i
< learn_total
- ROWS
)
154 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
155 i
+= (totalcols
- 2) * ROWS
;
157 i
+= (totalcols
- 1) * ROWS
;
159 dlg_select_widget (learn_dlg
, (void *) learnkeys
[i
].button
);
165 static int learn_check_key (int c
)
169 for (i
= 0; i
< learn_total
; i
++) {
170 if (key_name_conv_tab
[i
].code
== c
) {
171 if (!learnkeys
[i
].ok
) {
172 dlg_select_widget (learn_dlg
, learnkeys
[i
].button
);
173 label_set_text ((WLabel
*) learnkeys
[i
].label
,
175 learnkeys
[i
].ok
= 1;
177 if (learnok
>= learn_total
) {
178 learn_dlg
->ret_value
= B_CANCEL
;
180 if (query_dialog (learn_title
,
181 _("It seems that all your keys already\n"
182 "work fine. That's great."),
183 1, 2, _("&Save"), _("&Discard")) == 0)
184 learn_dlg
->ret_value
= B_ENTER
;
186 message (1, learn_title
,
187 _("Great! You have a complete terminal database!\n"
188 "All your keys work well."));
190 dlg_stop (learn_dlg
);
199 return learn_move (0);
202 return learn_move (1);
204 dlg_one_down (learn_dlg
);
207 dlg_one_up (learn_dlg
);
211 /* Prevent from disappearing if a non-defined sequence is pressed
212 and contains s or c. Use ALT('s') or ALT('c'). */
213 if (c
< 255 && isalpha(c
))
216 for (i
= 0; i
< BUTTONS
; i
++)
217 if (c
== learn_but
[i
].hotkey
)
224 static int learn_callback (Dlg_head
* h
, int Par
, int Msg
)
231 return learn_check_key (Par
);
236 static void init_learn (void)
239 key_code_name_t
*key
;
240 char buffer
[BUF_TINY
];
243 static int i18n_flag
= 0;
248 learn_but
[0].text
= _(learn_but
[0].text
);
249 learn_but
[0].x
= 78 / 2 + 4;
251 learn_but
[1].text
= _(learn_but
[1].text
);
252 learn_but
[1].x
= 78 / 2 - (strlen (learn_but
[1].text
) + 9);
254 for (i
= 0; i
< BUTTONS
; i
++)
256 cp
= strchr(learn_but
[i
].text
, '&');
257 if (cp
!= NULL
&& *++cp
!= '\0')
258 learn_but
[i
].hotkey
= toupper(*cp
);
261 learn_title
= _(learn_title
);
264 #endif /* ENABLE_NLS */
268 learn_dlg
= create_dlg (0, 0, 23, 78, dialog_colors
,
269 learn_callback
, "[Learn keys]", "Learn keys",
271 x_set_dialog_title (learn_dlg
, _("Learn keys"));
273 #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
275 for (i
= 0; i
< BUTTONS
; i
++)
276 add_widget (learn_dlg
, button_new (XTRACT (i
)));
280 for (key
= key_name_conv_tab
, j
= 0; key
->name
!= NULL
&&
281 strcmp (key
->name
, "kpleft"); key
++, j
++);
282 learnkeys
= g_new (learnkey
, j
);
283 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
288 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--) {
289 learnkeys
[i
].ok
= 0;
290 learnkeys
[i
].sequence
= NULL
;
291 g_snprintf (buffer
, sizeof (buffer
), "%-16s", _(key
->longname
));
292 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
293 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
, learn_button
, 0, NULL
));
294 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*)
295 label_new (y
, x
+ 19, "", NULL
));
303 add_widget (learn_dlg
,
304 label_new (UY
+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL
));
305 add_widget (learn_dlg
,
306 label_new (UY
+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL
));
307 add_widget (learn_dlg
,
308 label_new (UY
+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL
));
311 static void learn_done (void)
313 destroy_dlg (learn_dlg
);
321 int profile_changed
= 0;
322 char *section
= g_strconcat ("terminal:", getenv ("TERM"), NULL
);
324 for (i
= 0; i
< learn_total
; i
++) {
325 if (learnkeys
[i
].sequence
!= NULL
) {
327 WritePrivateProfileString (section
, key_name_conv_tab
[i
].name
,
328 learnkeys
[i
].sequence
, profile_name
);
332 /* On the one hand no good idea to save the complete setup but
333 * without 'Auto save setup' the new key-definitions will not be
334 * saved unless the user does an 'Options/Save Setup'.
335 * On the other hand a save-button that does not save anything to
336 * disk is much worse.
344 void learn_keys (void)
346 int save_old_esc_mode
= old_esc_mode
;
347 int save_alternate_plus_minus
= alternate_plus_minus
;
349 old_esc_mode
= 0; /* old_esc_mode cannot work in learn keys dialog */
350 alternate_plus_minus
= 1; /* don't translate KP_ADD, KP_SUBTRACT and
351 KP_MULTIPLY to '+', '-' and '*' in
353 application_keypad_mode ();
358 old_esc_mode
= save_old_esc_mode
;
359 alternate_plus_minus
= save_alternate_plus_minus
;
361 if (!alternate_plus_minus
)
362 numeric_keypad_mode ();
364 switch (learn_dlg
->ret_value
) {