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.

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