Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
14 KiB

  1. //******************************************************************************
  2. // File: \wacker\TDLL\KEY_DLG.C Created: 6/5/98 By: Dwayne M. Newsome
  3. //
  4. // Copyright 1998 by Hilgraeve Inc. --- Monroe, MI
  5. // All rights reserved
  6. //
  7. // Description:
  8. // This file is the key dialog. This allows for a key macro to be edited
  9. // or modified.
  10. //
  11. // $Revision: 6 $
  12. // $Date: 8/15/01 4:50p $
  13. // $Id: key_dlg.c 1.6 1998/09/10 16:10:17 bld Exp $
  14. //
  15. //******************************************************************************
  16. #include <windows.h>
  17. #pragma hdrstop
  18. #include "stdtyp.h"
  19. #include "mc.h"
  20. #ifdef INCL_KEY_MACROS
  21. #include <term\res.h>
  22. #include "misc.h"
  23. #include "hlptable.h"
  24. #include "keyutil.h"
  25. #include "errorbox.h"
  26. #include <tdll\assert.h>
  27. #include "globals.h"
  28. #include <emu\emu.h>
  29. #include "session.h"
  30. #include "chars.h"
  31. #if !defined(DlgParseCmd)
  32. #define DlgParseCmd(i,n,c,w,l) i=LOWORD(w);n=HIWORD(w);c=(HWND)l;
  33. #endif
  34. #define IDC_EF_KEYS_KEYNAME 101
  35. #define IDC_ML_KEYS_MACRO 105
  36. BOOL isSystemKey( KEYDEF aKey );
  37. BOOL isAcceleratorKey( KEYDEF aKey, UINT aTableId );
  38. BOOL validateKey( keyMacro * pKeyMacro, HWND hDlg );
  39. //******************************************************************************
  40. // FUNCTION:
  41. // KeyDlg
  42. //
  43. // DESCRIPTION:
  44. // This is the dialog proc for the key macro dialog.
  45. //
  46. // ARGUMENTS: Standard Windows dialog manager
  47. //
  48. // RETURNS: Standard Windows dialog manager
  49. //
  50. //
  51. BOOL CALLBACK KeyDlg(HWND hDlg, UINT wMsg, WPARAM wPar, LPARAM lPar)
  52. {
  53. HWND hwndChild;
  54. HWND keyNameEdit;
  55. HWND keyMacroEdit;
  56. INT nId;
  57. INT nNtfy;
  58. INT keyIndex;
  59. TCHAR keyName[35];
  60. TCHAR keyList[2048];
  61. keyMacro * pKeyMacro;
  62. keyMacro * pKeyNameData;
  63. keyMacro * pKeyMacroData;
  64. static DWORD aHlpTable[] = {IDC_EF_KEYS_KEYNAME , IDH_EF_KEYS_KEYNAME,
  65. IDC_ML_KEYS_MACRO , IDH_ML_KEYS_MACRO,
  66. IDCANCEL, IDH_CANCEL,
  67. IDOK, IDH_OK,
  68. 0, 0};
  69. //
  70. // process messages
  71. //
  72. switch (wMsg)
  73. {
  74. case WM_INITDIALOG:
  75. pKeyMacro = (keyMacro *)lPar;
  76. if ( pKeyMacro == 0 )
  77. {
  78. EndDialog(hDlg, FALSE);
  79. }
  80. SetWindowLongPtr( hDlg, DWLP_USER, (LONG_PTR)pKeyMacro );
  81. //
  82. // set up the name edit field
  83. //
  84. keyNameEdit = GetDlgItem( hDlg, IDC_EF_KEYS_KEYNAME );
  85. pKeyNameData = keysCloneKeyMacro( pKeyMacro );
  86. pKeyNameData->lpWndProc = (WNDPROC)GetWindowLongPtr( keyNameEdit, GWLP_WNDPROC );
  87. pKeyNameData->keyCount = 1;
  88. SetWindowLongPtr( keyNameEdit, GWLP_WNDPROC, (LONG_PTR)keyEditWindowProc );
  89. SetWindowLongPtr( keyNameEdit, GWLP_USERDATA, (LONG_PTR)pKeyNameData );
  90. //
  91. // set up the name edit field
  92. //
  93. keyMacroEdit = GetDlgItem( hDlg, IDC_ML_KEYS_MACRO );
  94. pKeyMacroData = keysCloneKeyMacro( pKeyMacro );
  95. pKeyMacroData->lpWndProc = (WNDPROC)GetWindowLongPtr( keyMacroEdit, GWLP_WNDPROC );
  96. pKeyMacroData->keyCount = KEYS_MAX_KEYS;
  97. SetWindowLongPtr( keyMacroEdit, GWLP_WNDPROC, (LONG_PTR)keyEditWindowProc );
  98. SetWindowLongPtr( keyMacroEdit, GWLP_USERDATA, (LONG_PTR)pKeyMacroData );
  99. //
  100. // set up initial values if we are in edit mode
  101. //
  102. if ( pKeyMacro->editMode == KEYS_EDIT_MODE_EDIT )
  103. {
  104. keysGetDisplayString( &pKeyMacro->keyName, 1, keyName, sizeof(keyName) );
  105. SetDlgItemText( hDlg, IDC_EF_KEYS_KEYNAME, keyName );
  106. keysGetDisplayString( pKeyMacro->keyMacro, pKeyMacro->macroLen,
  107. keyList, sizeof(keyList) );
  108. SetDlgItemText( hDlg, IDC_ML_KEYS_MACRO, keyList );
  109. }
  110. mscCenterWindowOnWindow(hDlg, GetParent(hDlg));
  111. break;
  112. case WM_DESTROY:
  113. keyNameEdit = GetDlgItem( hDlg, IDC_EF_KEYS_KEYNAME );
  114. pKeyNameData = (keyMacro *) GetWindowLongPtr( keyNameEdit, GWLP_USERDATA );
  115. SetWindowLongPtr( keyNameEdit, GWLP_WNDPROC, (LONG_PTR)pKeyNameData->lpWndProc );
  116. free( pKeyNameData );
  117. pKeyNameData = 0;
  118. keyMacroEdit = GetDlgItem( hDlg, IDC_ML_KEYS_MACRO );
  119. pKeyMacroData = (keyMacro *) GetWindowLongPtr( keyMacroEdit, GWLP_USERDATA );
  120. SetWindowLongPtr( keyMacroEdit, GWLP_WNDPROC, (LONG_PTR)pKeyMacroData->lpWndProc );
  121. free( pKeyMacroData );
  122. pKeyMacroData = 0;
  123. break;
  124. case WM_CONTEXTMENU:
  125. doContextHelp(aHlpTable, wPar, lPar, TRUE, TRUE);
  126. break;
  127. case WM_HELP:
  128. doContextHelp(aHlpTable, wPar, lPar, FALSE, FALSE);
  129. break;
  130. case WM_COMMAND:
  131. DlgParseCmd(nId, nNtfy, hwndChild, wPar, lPar);
  132. switch (nId)
  133. {
  134. case IDOK:
  135. keyNameEdit = GetDlgItem( hDlg, IDC_EF_KEYS_KEYNAME );
  136. pKeyNameData = (keyMacro *) GetWindowLongPtr( keyNameEdit, GWLP_USERDATA );
  137. keyMacroEdit = GetDlgItem( hDlg, IDC_ML_KEYS_MACRO );
  138. pKeyMacroData = (keyMacro *) GetWindowLongPtr( keyMacroEdit, GWLP_USERDATA );
  139. pKeyMacro = (keyMacro *)GetWindowLongPtr( hDlg, DWLP_USER );
  140. //
  141. // if we are in edit mode then update the previos macro with the
  142. // edited macro
  143. //
  144. if ( pKeyMacro->editMode == KEYS_EDIT_MODE_EDIT )
  145. {
  146. keyIndex = keysFindMacro( pKeyMacro );
  147. assert( keyIndex >= 0 );
  148. if ( pKeyMacro->keyName != pKeyNameData->keyName &&
  149. validateKey( pKeyNameData, hDlg ) == FALSE )
  150. {
  151. SetFocus( keyNameEdit );
  152. break;
  153. }
  154. //
  155. // combine the values from the name and macro edit controls
  156. // and update the previous macro with the new data
  157. //
  158. pKeyMacro->keyName = pKeyNameData->keyName;
  159. pKeyMacro->macroLen = pKeyMacroData->macroLen;
  160. if (pKeyMacroData->macroLen)
  161. MemCopy( pKeyMacro->keyMacro, pKeyMacroData->keyMacro,
  162. pKeyMacroData->macroLen * sizeof(KEYDEF) );
  163. keysUpdateMacro( keyIndex, pKeyMacro );
  164. }
  165. else
  166. {
  167. if ( validateKey( pKeyNameData, hDlg ) == FALSE )
  168. {
  169. SetFocus( keyNameEdit );
  170. break;
  171. }
  172. //
  173. // combine the values from the name and macro edit controls
  174. // and add the new macro
  175. //
  176. pKeyMacro->keyName = pKeyNameData->keyName;
  177. pKeyMacro->macroLen = pKeyMacroData->macroLen;
  178. if (pKeyMacroData->macroLen)
  179. MemCopy( pKeyMacro->keyMacro, pKeyMacroData->keyMacro,
  180. pKeyMacroData->macroLen * sizeof(KEYDEF) );
  181. keysAddMacro( pKeyMacro );
  182. }
  183. EndDialog(hDlg, TRUE);
  184. break;
  185. case IDCANCEL:
  186. EndDialog(hDlg, FALSE);
  187. break;
  188. default:
  189. return FALSE;
  190. }
  191. break;
  192. default:
  193. return FALSE;
  194. }
  195. return TRUE;
  196. }
  197. //******************************************************************************
  198. // Method:
  199. // isAcceleratorKey
  200. //
  201. // Description:
  202. // Checks if the key the user wants to define is already defined as a windows
  203. // accelerator key.
  204. //
  205. // Arguments:
  206. // aKey - The key to check
  207. // aTableId - The id of the accelerator table
  208. //
  209. // Returns:
  210. // True if the key is defined as an accelerator
  211. //
  212. // Throws:
  213. // None
  214. //
  215. // Author: Dwayne M. Newsome, 06/09/1998
  216. //
  217. //
  218. BOOL isAcceleratorKey( KEYDEF aKey, UINT aTableId )
  219. {
  220. BOOL fIsAccelerator = FALSE;
  221. int iIndex;
  222. int iAcceleratorEntries;
  223. KEYDEF lKeyDef;
  224. ACCEL * pAccelerators;
  225. HACCEL hAccel = LoadAccelerators( glblQueryDllHinst(),
  226. MAKEINTRESOURCE( aTableId ));
  227. if ( hAccel != NULL )
  228. {
  229. iAcceleratorEntries = CopyAcceleratorTable( hAccel, NULL, 0 );
  230. pAccelerators = (ACCEL*)malloc( sizeof(ACCEL) * iAcceleratorEntries);
  231. iAcceleratorEntries = CopyAcceleratorTable( hAccel, pAccelerators,
  232. iAcceleratorEntries );
  233. for ( iIndex = 0; iIndex < iAcceleratorEntries; iIndex++ )
  234. {
  235. lKeyDef = pAccelerators[iIndex].key;
  236. if ( keysIsKeyHVK( lKeyDef ) )
  237. {
  238. lKeyDef |= VIRTUAL_KEY;
  239. }
  240. if ( pAccelerators[iIndex].fVirt & FALT )
  241. {
  242. lKeyDef |= ALT_KEY;
  243. }
  244. if ( pAccelerators[iIndex].fVirt & FCONTROL )
  245. {
  246. lKeyDef |= CTRL_KEY;
  247. }
  248. if ( pAccelerators[iIndex].fVirt & FSHIFT )
  249. {
  250. lKeyDef |= SHIFT_KEY;
  251. }
  252. if ( lKeyDef == aKey )
  253. {
  254. fIsAccelerator = TRUE;
  255. break;
  256. }
  257. }
  258. free( pAccelerators );
  259. pAccelerators = NULL;
  260. }
  261. return fIsAccelerator;
  262. }
  263. //******************************************************************************
  264. // Function:
  265. // isSystemKey
  266. //
  267. // Description:
  268. // Checks if the key the user wants to define is already defined as a windows
  269. // system key. Note I could not find any way to get this information out of
  270. // the WIN32 API hence the hard coded table definition.
  271. //
  272. // Arguments:
  273. // aKeyDef - The key the user wants to define.
  274. //
  275. // Returns:
  276. // TRUE if the key is a windows system key.
  277. //
  278. // Author: Dwayne Newsome 10/08/96
  279. //
  280. BOOL isSystemKey( KEYDEF aKeyDef )
  281. {
  282. int iIndex;
  283. KEYDEF aKeyDefList[2];
  284. BOOL fIsSystemKey = FALSE;
  285. aKeyDefList[0] = VK_F4 | VIRTUAL_KEY | ALT_KEY;
  286. aKeyDefList[1] = VK_F4 | VIRTUAL_KEY | CTRL_KEY;
  287. for ( iIndex = 0; iIndex < 2; iIndex++ )
  288. {
  289. if ( aKeyDefList[iIndex] == aKeyDef )
  290. {
  291. fIsSystemKey = TRUE;
  292. break;
  293. }
  294. }
  295. return fIsSystemKey;
  296. }
  297. //******************************************************************************
  298. // Method:
  299. // validateKey
  300. //
  301. // Description:
  302. //
  303. // Arguments:
  304. // pKeyMacro - pointer to the key to be validated
  305. // hDlg - Parent dialog used for error messages
  306. //
  307. // Returns:
  308. // True if the key is valid, false otherwise
  309. //
  310. // Throws:
  311. // None
  312. //
  313. // Author: Dwayne M. Newsome, 06/09/1998
  314. //
  315. //
  316. BOOL validateKey( keyMacro * pKeyMacro, HWND hDlg )
  317. {
  318. TCHAR errorMsg[256];
  319. TCHAR errorMsgFmt[256];
  320. TCHAR msgTitle[100];
  321. TCHAR keyName[35];
  322. int lNameLen = 0;
  323. KEYDEF aUserKey;
  324. HWND keyNameEdit;
  325. HEMU hEmu;
  326. HSESSION hSession;
  327. //
  328. // make sure a key has been entered
  329. //
  330. keyNameEdit = GetDlgItem( hDlg, IDC_EF_KEYS_KEYNAME );
  331. lNameLen = SendMessage( keyNameEdit, EM_LINELENGTH, 0, 0 );
  332. if ( lNameLen == 0 )
  333. {
  334. LoadString(glblQueryDllHinst(), IDS_MISSING_KEY_MACRO,
  335. errorMsg, sizeof(errorMsg) / sizeof(TCHAR));
  336. LoadString(glblQueryDllHinst(), IDS_MB_TITLE_WARN, msgTitle,
  337. sizeof(msgTitle) / sizeof(TCHAR));
  338. TimedMessageBox(hDlg, errorMsg, msgTitle,
  339. MB_OKCANCEL | MB_ICONEXCLAMATION, 0);
  340. return FALSE;
  341. }
  342. //
  343. // make sure the key specified is not a duplicate
  344. //
  345. if ( keysFindMacro( pKeyMacro ) >= 0 )
  346. {
  347. LoadString(glblQueryDllHinst(), IDS_DUPLICATE_KEY_MACRO,
  348. errorMsgFmt, sizeof(errorMsgFmt) / sizeof(TCHAR));
  349. LoadString(glblQueryDllHinst(), IDS_MB_TITLE_WARN, msgTitle,
  350. sizeof(msgTitle) / sizeof(TCHAR));
  351. keysGetDisplayString( &pKeyMacro->keyName, 1, keyName,
  352. sizeof(keyName) );
  353. wsprintf( errorMsg, errorMsgFmt, keyName );
  354. TimedMessageBox(hDlg, errorMsg, msgTitle,
  355. MB_OK | MB_ICONEXCLAMATION, 0);
  356. return FALSE;
  357. }
  358. //
  359. // warn user if the key specified is in use as a system key, emulator key or
  360. // windows accelerator
  361. //
  362. hSession = pKeyMacro->hSession;
  363. assert(hSession);
  364. hEmu = sessQueryEmuHdl(hSession);
  365. assert(hEmu);
  366. aUserKey = pKeyMacro->keyName;
  367. if (( isAcceleratorKey( aUserKey, IDA_WACKER )) ||
  368. ( isSystemKey( aUserKey )) ||
  369. ( emuIsEmuKey( hEmu, aUserKey )))
  370. {
  371. LoadString(glblQueryDllHinst(), IDS_KEY_MACRO_REDEFINITION,
  372. errorMsgFmt, sizeof(errorMsgFmt) / sizeof(TCHAR));
  373. LoadString(glblQueryDllHinst(), IDS_MB_TITLE_WARN, msgTitle,
  374. sizeof(msgTitle) / sizeof(TCHAR));
  375. keysGetDisplayString( &aUserKey, 1, keyName, sizeof(keyName) );
  376. wsprintf( errorMsg, errorMsgFmt, keyName );
  377. if ( TimedMessageBox(hDlg, errorMsg, msgTitle,
  378. MB_YESNO | MB_ICONEXCLAMATION, 0) == IDNO )
  379. {
  380. return FALSE;
  381. }
  382. }
  383. return TRUE;
  384. }
  385. #endif