1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ///////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
27 // Key translations - I.Ioannou (roryt@hol.gr) //
28 // Athens - Greece December 18, 1996 02:56am //
29 // Reads a .cfg file and keeps the definitions //
30 // modified for alternate keymap swiching //
31 // by Andrey V. Smilianets (smile@head.aval.kiev.ua) //
32 // Kiev - Ukraine, December 1997. //
33 // modified to work with MSVC and the Standard Template //
34 // library by Paul Brannan <pbranna@clemson.edu>, //
36 // updated June 7, 1998 by Paul Brannan to remove cout and //
38 // APP_KEY and APP2_Key added July 12, 1998 by Paul Brannan //
39 ///////////////////////////////////////////////////////////////////
40 // class KeyTranslator //
41 // Load : loads or replaces the keymap //
42 // TranslateKey : returns a char * to the key def //
43 // AddKeyDef : Changes or adds the key translation //
44 // DeleteKeyDef : Deletes a key def from the list //
45 ///////////////////////////////////////////////////////////////////
49 // changed to make work with VC++ (Paul Brannan 5/25/98)
50 // FIX ME !!! Ioannou: This must be __BORLANDC__ && VERSION < 5
51 // but what is the directive for Borland version ????
52 // FIXED Sept. 31, 2000 (Bernard Badger)
54 #if defined(__BORLANDC__) && (__BORLANDC < 0x0500)
63 /////////////////////////////////////////////////////////////
64 // class KeyTranslator //
65 // Load : loads or replaces the keymap //
66 // TranslateKey : returns a sz to the key def //
67 // AddKeyDef : Changes or adds the key translation //
68 // DeleteKeyDef : Deletes a key def from the list //
69 /////////////////////////////////////////////////////////////
72 KeyTranslator::KeyTranslator():
73 mapArray(0,0,sizeof(KeyMap
)),
74 globals(0,0,sizeof(TKeyDef
)) {
75 ext_mode
= 0; // Paul Brannan 8/28/98
76 currentKeyMap
= mainKeyMap
= -1;
80 // perform keymap switching
81 int KeyTranslator::switchMap(TKeyDef
& tk
) {
82 if ( mapArray
.IsEmpty() ) {
83 return currentKeyMap
= -1;
85 int i
= mapArray
.Find(KeyMap(tk
));
87 if (currentKeyMap
== i
)
88 currentKeyMap
= mainKeyMap
; // restore to default
89 else currentKeyMap
= i
;
95 // Let the calling function interpret the error code (Paul Brannan 12/17/98)
96 int KeyTranslator::SwitchTo(int to
) {
98 int max
= mapArray
.GetItemsInContainer();
99 if (max
== 0) return -1;
100 if (to
< 0 || to
> (max
-1)) return 0;
107 // rewrited to support multiple keymaps
108 const char *KeyTranslator::TranslateKey(WORD wVirtualKeyCode
,
109 DWORD dwControlKeyState
)
111 if ( mapArray
.IsEmpty() ) return NULL
;
113 TKeyDef
ask(NULL
, dwControlKeyState
, wVirtualKeyCode
);
115 // if a keymap switch pressed
116 if ( switchMap(ask
) > 0 ) return "";
118 int i
= mapArray
[currentKeyMap
].map
.Find(ask
);
120 if ( i
!= INT_MAX
) return mapArray
[currentKeyMap
].map
[i
].GetszKey();
122 // if not found in current keymap
123 if ( currentKeyMap
!= mainKeyMap
) {
124 i
= mapArray
[mainKeyMap
].map
.Find(ask
);
125 if ( i
!= INT_MAX
) return mapArray
[mainKeyMap
].map
[i
].GetszKey();
132 // rewrited to support multiple keymaps
133 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
,
136 if ( ! mapArray
[currentKeyMap
].map
.IsEmpty() ) {
137 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
139 mapArray
[currentKeyMap
].map
[i
] = lpzKeyDef
;
143 return mapArray
[currentKeyMap
].map
.Add( TKeyDef(lpzKeyDef
, dwControlKeyState
, wVirtualKeyCode
));
146 // Paul Brannan Feb. 22, 1999
147 int KeyTranslator::AddKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
,
153 if ( ! mapArray
[currentKeyMap
].map
.IsEmpty() ) {
154 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
156 mapArray
[currentKeyMap
].map
[i
] = op
;
160 return mapArray
[currentKeyMap
].map
.Add( TKeyDef(op
, dwControlKeyState
, wVirtualKeyCode
));
164 int KeyTranslator::LookOnGlobal(char* vkey
) {
165 if ( ! globals
.IsEmpty() ) {
166 int max
= globals
.GetItemsInContainer();
167 for ( int i
= 0; i
< max
; i
++ )
168 if ( stricmp(globals
[i
].GetszKey(), vkey
) == 0 )
174 int KeyTranslator::AddGlobalDef(WORD wVirtualKeyCode
, char*lpzKeyDef
) {
175 if ( ! globals
.IsEmpty() ) {
176 int max
= globals
.GetItemsInContainer();
177 for ( int i
= 0; i
< max
; i
++ ) {
178 const char *s
= globals
[i
].GetszKey();
179 if ( stricmp(s
, lpzKeyDef
) == 0 ) {
180 globals
[i
] = DWORD(wVirtualKeyCode
);
185 return globals
.Add( TKeyDef(lpzKeyDef
, 0, wVirtualKeyCode
));
190 // rewrited to support multiple keymaps
191 int KeyTranslator::DeleteKeyDef(WORD wVirtualKeyCode
, DWORD dwControlKeyState
)
193 if ( mapArray
.IsEmpty() || mapArray
[currentKeyMap
].map
.IsEmpty() )
196 int i
= mapArray
[currentKeyMap
].map
.Find(TKeyDef(NULL
, dwControlKeyState
, wVirtualKeyCode
));
199 mapArray
[currentKeyMap
].map
.Destroy(i
);
206 // rewritten to support multiple keymaps
207 void KeyTranslator::DeleteAllDefs(void)
209 // This code wants to crash under the STL; Apparently the Destroy()
210 // function actually deletes the entry, rather than simply releasing
211 // memory. I think flush() should do the same thing, at least the
212 // way it is written with STL_BIDS (Paul Brannan 5/25/98).
215 max
= mapArray
.GetItemsInContainer();
216 if ( ! mapArray
.IsEmpty() ) {
217 for ( int i
= 0; i
< max
; i
++ ) {
218 if ( !mapArray
[i
].map
.IsEmpty() ) {
219 mapArray
[i
].map
.Flush();