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.

576 lines
18 KiB

  1. #ifndef __tmplEdit_h
  2. #define __tmplEdit_h
  3. #include <imm.h>
  4. /////////////////////////////////////////////////////////////////////////////
  5. // CWindowImplNoImm
  6. // Purpose - Meant to prevent entry of DBCS characters into an edit box by
  7. // disabling IME
  8. //
  9. // Usage - CWindowImplNoImm<> m_NoImmEditWindow1;
  10. // CDialog::OnInitDialog(..)
  11. // {
  12. // ...
  13. // m_NoImmEditWindow1.SubClassWindow( GetDlgItem( IDC_NOIMMEDITWINDOW1 ));
  14. // ...
  15. // }
  16. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  17. class CWindowImplNoImm : public CWindowImpl< T, TBase, TWinTraits >
  18. {
  19. public:
  20. BEGIN_MSG_MAP(CWindowImplNoImm)
  21. END_MSG_MAP()
  22. BOOL SubclassWindow(HWND hWnd)
  23. {
  24. BOOL bRC = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow( hWnd );
  25. if ( bRC )
  26. ImmAssociateContext( hWnd, NULL );
  27. return bRC;
  28. }
  29. void LTrim()
  30. {
  31. LPTSTR psBuffer;
  32. unsigned int i = 0;
  33. DWORD dwSize;
  34. dwSize = ::GetWindowTextLength( m_hWnd );
  35. if ( 0 < dwSize )
  36. {
  37. dwSize++;
  38. psBuffer = new TCHAR[dwSize];
  39. ::GetWindowText( m_hWnd , psBuffer, dwSize );
  40. while ( i < dwSize && 32 == psBuffer[i] )
  41. i++;
  42. if ( 0 < i )
  43. ::SetWindowText( m_hWnd, psBuffer + i );
  44. delete [] psBuffer;
  45. }
  46. }
  47. void RTrim()
  48. {
  49. LPTSTR psBuffer;
  50. unsigned int i = 0;
  51. DWORD dwSize;
  52. dwSize = ::GetWindowTextLength( m_hWnd );
  53. if ( 0 < dwSize )
  54. {
  55. i = dwSize;
  56. dwSize++;
  57. psBuffer = new TCHAR[dwSize];
  58. ::GetWindowText( m_hWnd , psBuffer, dwSize );
  59. while ( 0 < i && ( 32 == psBuffer[i] || 0 == psBuffer[i] ))
  60. {
  61. psBuffer[i] = 0;
  62. i--;
  63. }
  64. if ( i < dwSize - 1 )
  65. ::SetWindowText( m_hWnd, psBuffer );
  66. delete [] psBuffer;
  67. }
  68. }
  69. };
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CWindowImplNoPaste
  72. // Purpose - Meant to prevent entry of DBCS characters into an edit box by
  73. // disabling IME and not allowing any pasting of charaters
  74. //
  75. // Usage - CWindowImplNoPaste<> m_NoPasteEditWindow1;
  76. // CDialog::OnInitDialog(..)
  77. // {
  78. // ...
  79. // m_NoPasteEditWindow1.SubClassWindow( GetDlgItem( IDC_NOPASTEEDITWINDOW1 ));
  80. // ...
  81. // }
  82. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  83. class CWindowImplNoPaste : public CWindowImpl< T, TBase, TWinTraits >
  84. {
  85. public:
  86. BEGIN_MSG_MAP(CWindowImplNoPaste)
  87. MESSAGE_HANDLER( WM_PASTE, OnPaste )
  88. END_MSG_MAP()
  89. LRESULT OnPaste( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
  90. {
  91. MessageBeep( MB_ICONEXCLAMATION );
  92. return 0;
  93. }
  94. };
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CWindowImplNoCopy
  97. // Purpose - Meant to prevent entry of DBCS characters into an edit box by
  98. // disabling IME and not allowing any pasting of charaters
  99. //
  100. // Usage - CWindowImplNoCopy<> m_NoCopyEditWindow1;
  101. // CDialog::OnInitDialog(..)
  102. // {
  103. // ...
  104. // m_NoCopyEditWindow1.SubClassWindow( GetDlgItem( IDC_NOCOPYEDITWINDOW1 ));
  105. // ...
  106. // }
  107. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  108. class CWindowImplNoCopy : public CWindowImpl< T, TBase, TWinTraits >
  109. {
  110. public:
  111. BEGIN_MSG_MAP(CWindowImplNoCopy)
  112. MESSAGE_HANDLER( WM_COPY, OnCopy )
  113. END_MSG_MAP()
  114. LRESULT OnCopy( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
  115. {
  116. MessageBeep( MB_ICONEXCLAMATION );
  117. return 0;
  118. }
  119. };
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CWindowImplPhoneNumber
  122. // Purpose - Meant to allow entry of only telephone number related characters
  123. //
  124. // Usage - CWindowImplPhoneNumber<> m_PhoneNumberEditWindow1;
  125. // CDialog::OnInitDialog(..)
  126. // {
  127. // ...
  128. // m_PhoneNumberEditWindow1.SubClassWindow( GetDlgItem( IDC_PHONENOEDITWINDOW1 ));
  129. // ...
  130. // }
  131. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  132. class CWindowImplPhoneNumber : public CWindowImpl< T, TBase, TWinTraits >
  133. {
  134. public:
  135. BEGIN_MSG_MAP(CWindowImplPhoneNumber)
  136. MESSAGE_HANDLER( WM_CHAR, OnChar )
  137. END_MSG_MAP()
  138. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  139. {
  140. int i = GetKeyState( VK_CONTROL );
  141. if ( !(( _T( '0' ) <= wParam && _T( '9' ) >= wParam ) || _T( ' ' ) == wParam || _T( '-' ) == wParam || _T( ',' ) == wParam
  142. || VK_BACK == wParam || VK_SPACE == wParam || VK_DELETE == wParam || VK_INSERT == wParam )
  143. && 0 == HIWORD( i ))
  144. {
  145. MessageBeep( static_cast<unsigned int>(-1) );
  146. return TRUE;
  147. }
  148. bHandled = FALSE;
  149. return FALSE;
  150. }
  151. };
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CWindowImplFileChar
  154. // Purpose - Meant to allow entry of any characters except those specifically disallowed for files
  155. //
  156. // Usage - CWindowImplFileChar<> m_FileNameEditWindow1;
  157. // CDialog::OnInitDialog(..)
  158. // {
  159. // ...
  160. // m_FileNameEditWindow1.SubClassWindow( GetDlgItem( IDC_FILENAMEEDITWINDOW1 ));
  161. // ...
  162. // }
  163. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  164. class CWindowImplFileChar : public CWindowImpl< T, TBase, TWinTraits >
  165. {
  166. public:
  167. CWindowImplFileChar() : m_isWildCardsAllowed( false ),
  168. m_isFullPathAllowed( false )
  169. {}
  170. BEGIN_MSG_MAP(CWindowImplFileChar)
  171. MESSAGE_HANDLER( WM_CHAR, OnChar )
  172. END_MSG_MAP()
  173. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  174. {
  175. TCHAR sCharList[] = { _T('/'), _T('"'), _T('<'), _T('>'), _T('|'), 0, 0, 0, 0, 0};
  176. if (! m_isWildCardsAllowed)
  177. {
  178. sCharList[_tcslen(sCharList)] = _T('*');
  179. sCharList[_tcslen(sCharList)] = _T('?');
  180. }
  181. if (! m_isFullPathAllowed)
  182. {
  183. sCharList[_tcslen(sCharList)] = _T('\\');
  184. sCharList[_tcslen(sCharList)] = _T(':');
  185. }
  186. int i = GetKeyState( VK_CONTROL );
  187. if ( (NULL != _tcschr(sCharList, static_cast<TCHAR>(wParam))) && (0 == HIWORD(i)) )
  188. {
  189. MessageBeep( static_cast<unsigned int>(-1) );
  190. return TRUE;
  191. }
  192. bHandled = FALSE;
  193. return FALSE;
  194. }
  195. public:
  196. // Attributes:
  197. bool m_isWildCardsAllowed;
  198. bool m_isFullPathAllowed;
  199. };
  200. /////////////////////////////////////////////////////////////////////////////
  201. // CWindowImplASCII
  202. // Purpose - Meant to allow entry of only ASCII characters (a-z, A-Z, 0-9, space)
  203. //
  204. // Usage - CWindowImplASCII<> m_ASCIIEditWindow1;
  205. // CDialog::OnInitDialog(..)
  206. // {
  207. // ...
  208. // m_ASCIIEditWindow1.SubClassWindow( GetDlgItem( IDC_ASCIIEDITWINDOW1 ));
  209. // ...
  210. // }
  211. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  212. class CWindowImplASCII : public CWindowImpl< T, TBase, TWinTraits >
  213. {
  214. public:
  215. BEGIN_MSG_MAP(CWindowImplASCII)
  216. MESSAGE_HANDLER( WM_CHAR, OnChar )
  217. END_MSG_MAP()
  218. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  219. {
  220. int i = GetKeyState( VK_CONTROL );
  221. if ( !(( 32 <= wParam && 126 >= wParam )
  222. || VK_BACK == wParam )
  223. && 0 == HIWORD( i ))
  224. {
  225. MessageBeep( static_cast<unsigned int>(-1) );
  226. return TRUE;
  227. }
  228. bHandled = FALSE;
  229. return FALSE;
  230. }
  231. };
  232. /////////////////////////////////////////////////////////////////////////////
  233. // CWindowImplNoImmPaste = CWindowImplNoImm + CWindowImplNoPaste
  234. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  235. class CWindowImplNoImmPaste : public CWindowImplNoImm< T, TBase, TWinTraits >, public CWindowImplNoPaste< T, TBase, TWinTraits >
  236. {
  237. public:
  238. BEGIN_MSG_MAP(CWindowImplNoPaste)
  239. if(CWindowImplNoPaste< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  240. return TRUE;
  241. END_MSG_MAP()
  242. BOOL SubclassWindow(HWND hWnd)
  243. {
  244. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  245. }
  246. };
  247. /////////////////////////////////////////////////////////////////////////////
  248. // CWindowImplNoImmPN = CWindowImplNoImm + CWindowImplPhoneNumber
  249. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  250. class CWindowImplNoImmPN : public CWindowImplNoImm< T, TBase, TWinTraits >, public CWindowImplPhoneNumber< T, TBase, TWinTraits >
  251. {
  252. public:
  253. BEGIN_MSG_MAP(CWindowImplNoImmPastePN)
  254. if(CWindowImplPhoneNumber< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  255. return TRUE;
  256. END_MSG_MAP()
  257. BOOL SubclassWindow(HWND hWnd)
  258. {
  259. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  260. }
  261. };
  262. /////////////////////////////////////////////////////////////////////////////
  263. // CWindowImplNoImmPasteASCII = CWindowImplNoImm + CWindowImplNoPaste + CWindowImplASCII
  264. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  265. class CWindowImplNoImmPasteASCII : public CWindowImplNoImm< T, TBase, TWinTraits >, public CWindowImplNoPaste< T, TBase, TWinTraits >, public CWindowImplASCII< T, TBase, TWinTraits >
  266. {
  267. public:
  268. BEGIN_MSG_MAP(CWindowImplNoPasteASCII)
  269. if(CWindowImplNoPaste< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  270. return TRUE;
  271. if(CWindowImplASCII< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  272. return TRUE;
  273. END_MSG_MAP()
  274. BOOL SubclassWindow(HWND hWnd)
  275. {
  276. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  277. }
  278. };
  279. /////////////////////////////////////////////////////////////////////////////
  280. // CWindowImplNoImmASCII = CWindowImplNoImm + CWindowImplASCII
  281. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  282. class CWindowImplNoImmASCII : public CWindowImplNoImm< T, TBase, TWinTraits >, public CWindowImplASCII< T, TBase, TWinTraits >
  283. {
  284. public:
  285. BEGIN_MSG_MAP(CWindowImplNoPasteASCII)
  286. if(CWindowImplASCII< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  287. return TRUE;
  288. END_MSG_MAP()
  289. BOOL SubclassWindow(HWND hWnd)
  290. {
  291. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  292. }
  293. };
  294. //-----------------------------------------------------------------------------
  295. // CWindowImplComputerName = CWindowImplNoImm + CWindowImplNoPaste + computer name
  296. // character checking
  297. //-----------------------------------------------------------------------------
  298. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  299. class CWindowImplComputerName : public CWindowImplNoImm< T, TBase, TWinTraits >,
  300. public CWindowImplNoPaste< T, TBase, TWinTraits >
  301. {
  302. public:
  303. BEGIN_MSG_MAP(CWindowImplNoPaste)
  304. MESSAGE_HANDLER(WM_CHAR, OnChar)
  305. MESSAGE_HANDLER(WM_GETDLGCODE, OnDlgCode)
  306. if(CWindowImplNoPaste< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  307. return TRUE;
  308. END_MSG_MAP()
  309. CWindowImplComputerName() :
  310. m_hWndButton(NULL),
  311. m_hWndEdit(NULL)
  312. {
  313. }
  314. VOID SetHandles( HWND hWndButton, HWND hWndEdit )
  315. {
  316. m_hWndButton = hWndButton;
  317. m_hWndEdit = hWndEdit;
  318. }
  319. BOOL SubclassWindow(HWND hWnd)
  320. {
  321. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  322. }
  323. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  324. {
  325. if( (wParam >= _T('a') && wParam <= _T('z')) ||
  326. (wParam >= _T('A') && wParam <= _T('Z')) ||
  327. (wParam >= _T('0') && wParam <= _T('9')) ||
  328. (wParam == _T('-')) ||
  329. (wParam == VK_BACK) )
  330. {
  331. }
  332. else if( wParam == VK_RETURN )
  333. {
  334. ::SendMessage( m_hWndButton, BM_CLICK, 0, 0 );
  335. bHandled = TRUE;
  336. return FALSE;
  337. }
  338. else
  339. {
  340. MessageBeep( static_cast<unsigned int>(-1) );
  341. return TRUE;
  342. }
  343. bHandled = FALSE;
  344. return FALSE;
  345. }
  346. LRESULT OnDlgCode( UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
  347. {
  348. if( m_hWndEdit )
  349. {
  350. MSG* pMsg = (MSG*)lParam;
  351. if( pMsg )
  352. {
  353. if( (pMsg->message == WM_KEYDOWN) &&
  354. (LOWORD(pMsg->wParam) == VK_RETURN) )
  355. {
  356. // only take it if we have text
  357. if( ::SendMessage(m_hWndEdit, WM_GETTEXTLENGTH, 0, 0) )
  358. {
  359. bHandled = TRUE;
  360. return DLGC_WANTALLKEYS;
  361. }
  362. }
  363. }
  364. }
  365. bHandled = FALSE;
  366. return 0;
  367. }
  368. HWND m_hWndButton;
  369. HWND m_hWndEdit;
  370. };
  371. /////////////////////////////////////////////////////////////////////////////
  372. // CWindowImplAlias = CWindowImplNoImm + Alias ASCII checking
  373. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  374. class CWindowImplAlias : public CWindowImplNoImm< T, TBase, TWinTraits >
  375. {
  376. public:
  377. BEGIN_MSG_MAP(CWindowImplAlias)
  378. MESSAGE_HANDLER(WM_CHAR, OnChar)
  379. END_MSG_MAP()
  380. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  381. {
  382. if ( (wParam >= 35 && wParam <= 39) ||
  383. (wParam >= 42 && wParam <= 43) ||
  384. (wParam >= 45 && wParam <= 57) ||
  385. (wParam >= 63 && wParam <= 90) ||
  386. (wParam >= 94 && wParam <= 126) ||
  387. (wParam == 33) ||
  388. (wParam == 61) ||
  389. (wParam == 92) ||
  390. (wParam == VK_BACK) || // Backspace!
  391. (wParam == VK_INSERT) || // Insert!
  392. (wParam == 0x18) || // Allow Cutting!
  393. (wParam == 0x1A) || // Allow Undoing!
  394. (wParam == 0x03) || // Allow Copying!
  395. (wParam == 0x16)) // Allow Pasting!
  396. {
  397. }
  398. else
  399. {
  400. MessageBeep( static_cast<unsigned int>(-1) );
  401. return TRUE;
  402. }
  403. bHandled = FALSE;
  404. return FALSE;
  405. }
  406. };
  407. /////////////////////////////////////////////////////////////////////////////
  408. // CWindowImplPassword = CWindowImplNoImm + CWindowImplNoCopy + CWindowImplASCII
  409. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  410. class CWindowImplPassword : public CWindowImplNoImm< T, TBase, TWinTraits >, public CWindowImplNoCopy< T, TBase, TWinTraits >, public CWindowImplASCII< T, TBase, TWinTraits >
  411. {
  412. public:
  413. BEGIN_MSG_MAP(CWindowImplPassword)
  414. if(CWindowImplNoCopy< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  415. return TRUE;
  416. if(CWindowImplASCII< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  417. return TRUE;
  418. END_MSG_MAP()
  419. BOOL SubclassWindow(HWND hWnd)
  420. {
  421. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  422. }
  423. };
  424. //-----------------------------------------------------------------------------
  425. // CWindowImplDiskSpace = CWindowImplNoImm + CWindowImplNoPaste + disk space
  426. // character checking
  427. //-----------------------------------------------------------------------------
  428. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  429. class CWindowImplDiskSpace : public CWindowImplNoImm< T, TBase, TWinTraits >,
  430. public CWindowImplNoPaste< T, TBase, TWinTraits >
  431. {
  432. public:
  433. BEGIN_MSG_MAP(CWindowImplNoPaste)
  434. MESSAGE_HANDLER(WM_CHAR, OnChar)
  435. if(CWindowImplNoPaste< T, TBase, TWinTraits >::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult))
  436. return TRUE;
  437. END_MSG_MAP()
  438. BOOL SubclassWindow(HWND hWnd)
  439. {
  440. return CWindowImplNoImm< T, TBase, TWinTraits >::SubclassWindow(hWnd);
  441. }
  442. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  443. {
  444. if( !_istdigit((INT)wParam) &&
  445. (wParam != _T('.')) &&
  446. (wParam != VK_BACK) )
  447. {
  448. MessageBeep( static_cast<unsigned int>(-1) );
  449. return TRUE;
  450. }
  451. bHandled = FALSE;
  452. return FALSE;
  453. }
  454. };
  455. //-----------------------------------------------------------------------------
  456. // CWindowImplComputerName = CWindowImplNoImm + domain name character checking
  457. //-----------------------------------------------------------------------------
  458. template <class T = CWindow, class TBase = CWindow, class TWinTraits = CControlWinTraits>
  459. class CWindowImplDomainName : public CWindowImplNoImm< T, TBase, TWinTraits >
  460. {
  461. public:
  462. BEGIN_MSG_MAP(CWindowImplDomainName)
  463. MESSAGE_HANDLER(WM_CHAR, OnChar)
  464. END_MSG_MAP()
  465. LRESULT OnChar( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled )
  466. {
  467. if( (wParam >= _T('a') && wParam <= _T('z')) ||
  468. (wParam >= _T('A') && wParam <= _T('Z')) ||
  469. (wParam >= _T('0') && wParam <= _T('9')) ||
  470. (wParam == _T('-')) ||
  471. (wParam == _T('.')) ||
  472. (wParam == VK_BACK) ||
  473. (wParam == VK_DELETE) ||
  474. (wParam == VK_INSERT) ||
  475. (wParam == 0x18) || // Allow Cutting!
  476. (wParam == 0x1A) || // Allow Undoing!
  477. (wParam == 0x03) || // Allow Copying!
  478. (wParam == 0x16)) // Allow Pasting!
  479. {
  480. }
  481. else
  482. {
  483. MessageBeep( static_cast<unsigned int>(-1) );
  484. return TRUE;
  485. }
  486. bHandled = FALSE;
  487. return FALSE;
  488. }
  489. };
  490. #endif // #ifndef __tmplEdit.h