Source code of Windows XP (NT5)
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.

130 lines
3.4 KiB

  1. // paredit.cpp: C++ derived edit control for numbers/letters etc
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. #include "stdafx.h"
  13. #include "ctrltest.h"
  14. #include "paredit.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // ParsedEdit
  17. CParsedEdit::CParsedEdit()
  18. {
  19. m_wParseStyle = 0;
  20. }
  21. BEGIN_MESSAGE_MAP(CParsedEdit, CEdit)
  22. //{{AFX_MSG_MAP(CParsedEdit)
  23. ON_WM_CHAR()
  24. ON_WM_VSCROLL() // for associated spin controls
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Creating from C++ code
  29. BOOL CParsedEdit::Create(DWORD dwStyle, const RECT& rect,
  30. CWnd* pParentWnd, UINT nID)
  31. {
  32. m_wParseStyle = LOWORD(dwStyle);
  33. // figure out edit control style
  34. DWORD dwEditStyle = MAKELONG(ES_LEFT, HIWORD(dwStyle));
  35. return CWnd::Create(_T("EDIT"), NULL, dwEditStyle, rect, pParentWnd, nID);
  36. }
  37. /////////////////////////////////////////////////////////////////////////////
  38. // Aliasing on top of an existing Edit control
  39. BOOL CParsedEdit::SubclassEdit(UINT nID, CWnd* pParent, WORD wParseStyle)
  40. {
  41. m_wParseStyle = wParseStyle;
  42. return SubclassDlgItem(nID, pParent);
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // Input character filter
  46. void CParsedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  47. {
  48. WORD type;
  49. if (nChar < 0x20)
  50. type = PES_ALL; // always allow control chars
  51. else if (IsCharAlphaNumeric((TCHAR)nChar) && !IsCharAlpha((TCHAR)nChar))
  52. type = PES_NUMBERS;
  53. else if (IsCharAlpha((TCHAR)nChar))
  54. type = PES_LETTERS;
  55. else
  56. type = PES_OTHERCHARS;
  57. if (m_wParseStyle & type)
  58. {
  59. CEdit::OnChar(nChar, nRepCnt, nFlags); // permitted
  60. }
  61. else
  62. {
  63. // illegal character - inform parent
  64. OnBadInput();
  65. }
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. // Spin controls will send scroll messages
  69. void CParsedEdit::OnVScroll(UINT nSBCode, UINT, CScrollBar*)
  70. {
  71. int nDelta = 0;
  72. if (nSBCode == SB_LINEDOWN)
  73. nDelta = -1;
  74. else if (nSBCode == SB_LINEUP)
  75. nDelta = +1;
  76. else
  77. return; // nothing special
  78. // set the focus to this edit item and select it all
  79. SetFocus();
  80. //Get the number in the control.
  81. BOOL bOk;
  82. int nOld = GetParent()->GetDlgItemInt(GetDlgCtrlID(), &bOk);
  83. if (bOk)
  84. {
  85. // The MuScrl32 control also supports range checking
  86. // for this example, we just prevent overflow
  87. int nNew = nOld + nDelta;
  88. if (nNew >= 0 && nNew <= 32767)
  89. GetParent()->SetDlgItemInt(GetDlgCtrlID(), nNew);
  90. else
  91. bOk = FALSE;
  92. }
  93. if (!bOk)
  94. OnBadInput();
  95. SetSel(0, -1);
  96. }
  97. /////////////////////////////////////////////////////////////////////////////
  98. // default bad input handler, beep (unless parent notification
  99. // returns -1. Most parent dialogs will return 0 or 1 for command
  100. // handlers (i.e. Beep is the default)
  101. void CParsedEdit::OnBadInput()
  102. {
  103. if (GetParent()->SendMessage(WM_COMMAND,
  104. MAKELONG(GetDlgCtrlID(), PEN_ILLEGALCHAR), (LPARAM)m_hWnd) != -1)
  105. {
  106. MessageBeep((UINT)-1);
  107. }
  108. }
  109. /////////////////////////////////////////////////////////////////////////////