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>
28 #include <sys/param.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
110 if (*seq
&& strcmp (seq
, "\\e") && strcmp (seq
, "\\e\\e")
111 && strcmp (seq
, "^m" )
112 && (seq
[1] || (*seq
< ' ' || *seq
> '~'))){
115 learnkeys
[action
- B_USER
].sequence
= seq
;
116 seq
= convert_controls (seq
);
117 define_sequence (key_name_conv_tab
[action
- B_USER
].code
, seq
,
120 message (0, _(" Cannot accept this key "),
121 _(" You have entered \"%s\""), seq
);
129 dlg_select_widget (learn_dlg
, learnkeys
[action
- B_USER
].button
);
130 return 0; /* Do not kill learn_dlg */
133 static int learn_move (int right
)
137 totalcols
= (learn_total
- 1) / ROWS
+ 1;
138 for (i
= 0; i
< learn_total
; i
++)
139 if (learnkeys
[i
].button
== learn_dlg
->current
->widget
) {
141 if (i
< learn_total
- ROWS
)
148 else if (i
+ (totalcols
- 1) * ROWS
>= learn_total
)
149 i
+= (totalcols
- 2) * ROWS
;
151 i
+= (totalcols
- 1) * ROWS
;
153 dlg_select_widget (learn_dlg
, (void *) learnkeys
[i
].button
);
159 static int learn_check_key (int c
)
163 for (i
= 0; i
< learn_total
; i
++) {
164 if (key_name_conv_tab
[i
].code
== c
) {
165 if (!learnkeys
[i
].ok
) {
166 dlg_select_widget (learn_dlg
, learnkeys
[i
].button
);
167 label_set_text ((WLabel
*) learnkeys
[i
].label
,
169 learnkeys
[i
].ok
= 1;
171 if (learnok
>= learn_total
) {
172 learn_dlg
->ret_value
= B_CANCEL
;
174 if (query_dialog (learn_title
,
175 _("It seems that all your keys already\n"
176 "work fine. That's great."),
177 1, 2, _("&Save"), _("&Discard")) == 0)
178 learn_dlg
->ret_value
= B_ENTER
;
180 message (1, learn_title
,
181 _("Great! You have a complete terminal database!\n"
182 "All your keys work well."));
184 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 s or c. Use ALT('s') or ALT('c'). */
207 if (c
< 255 && isalpha(c
))
210 for (i
= 0; i
< BUTTONS
; i
++)
211 if (c
== learn_but
[i
].hotkey
)
218 static int learn_callback (Dlg_head
* h
, int Par
, int Msg
)
225 return learn_check_key (Par
);
230 static void init_learn (void)
233 key_code_name_t
*key
;
234 char buffer
[BUF_TINY
];
235 static int i18n_flag
= 0;
244 learn_but
[0].text
= _(learn_but
[0].text
);
245 learn_but
[0].x
= 78 / 2 + 4;
247 learn_but
[1].text
= _(learn_but
[1].text
);
248 learn_but
[1].x
= 78 / 2 - (strlen (learn_but
[1].text
) + 9);
250 for (i
= 0; i
< BUTTONS
; i
++)
252 cp
= strchr(learn_but
[i
].text
, '&');
253 if (cp
!= NULL
&& *++cp
!= '\0')
254 learn_but
[i
].hotkey
= toupper(*cp
);
257 learn_title
= _(learn_title
);
260 #endif /* ENABLE_NLS */
262 learn_dlg
= create_dlg (0, 0, 23, 78, dialog_colors
,
263 learn_callback
, "[Learn keys]", "Learn keys",
265 x_set_dialog_title (learn_dlg
, _("Learn keys"));
267 #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
269 for (i
= 0; i
< BUTTONS
; i
++)
270 add_widget (learn_dlg
, button_new (XTRACT (i
)));
274 for (key
= key_name_conv_tab
, j
= 0; key
->name
!= NULL
&&
275 strcmp (key
->name
, "kpleft"); key
++, j
++);
276 learnkeys
= g_new (learnkey
, j
);
277 x
+= ((j
- 1) / ROWS
) * COLSHIFT
;
282 for (i
= j
- 1, key
= key_name_conv_tab
+ j
- 1; i
>= 0; i
--, key
--) {
283 learnkeys
[i
].ok
= 0;
284 learnkeys
[i
].sequence
= NULL
;
285 g_snprintf (buffer
, sizeof (buffer
), "%-16s", _(key
->longname
));
286 add_widget (learn_dlg
, learnkeys
[i
].button
= (Widget
*)
287 button_new (y
, x
, B_USER
+ i
, NARROW_BUTTON
, buffer
, learn_button
, 0, NULL
));
288 add_widget (learn_dlg
, learnkeys
[i
].label
= (Widget
*)
289 label_new (y
, x
+ 19, "", NULL
));
297 add_widget (learn_dlg
,
298 label_new (UY
+14, 5, _("Press all the keys mentioned here. After you have done it, check"), NULL
));
299 add_widget (learn_dlg
,
300 label_new (UY
+15, 5, _("which keys are not marked with OK. Press space on the missing"), NULL
));
301 add_widget (learn_dlg
,
302 label_new (UY
+16, 5, _("key, or click with the mouse to define it. Move around with Tab."), NULL
));
305 static void learn_done (void)
307 destroy_dlg (learn_dlg
);
315 int profile_changed
= 0;
316 char *section
= g_strconcat ("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
345 application_keypad_mode ();
350 old_esc_mode
= save_old_esc_mode
;
351 alternate_plus_minus
= save_alternate_plus_minus
;
353 if (!alternate_plus_minus
)
354 numeric_keypad_mode ();
356 switch (learn_dlg
->ret_value
) {