home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK11 / MFC / SAMPLES / CTRLTEST / PAREDIT.CP$ / paredit
Encoding:
Text File  |  1992-03-16  |  3.6 KB  |  133 lines

  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 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 Microsoft
  9. // QuickHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "ctrltest.h"
  14.  
  15. #include "paredit.h"
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // ParsedEdit
  19.  
  20. CParsedEdit::CParsedEdit()
  21. {
  22.     m_wParseStyle = 0;
  23. }
  24.  
  25. BEGIN_MESSAGE_MAP(CParsedEdit, CEdit)
  26.     ON_WM_CHAR()
  27.     ON_WM_VSCROLL()     // for associated spin controls
  28. END_MESSAGE_MAP()
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Creating from C++ code
  32.  
  33. BOOL CParsedEdit::Create(DWORD dwStyle, const RECT& rect,
  34.         CWnd* pParentWnd, UINT nID)
  35. {
  36.     m_wParseStyle = LOWORD(dwStyle);
  37.     // figure out edit control style
  38.     DWORD dwEditStyle = MAKELONG(ES_LEFT, HIWORD(dwStyle));
  39.     return CWnd::Create("EDIT", NULL, dwEditStyle, rect, pParentWnd, nID);
  40. }
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Aliasing on top of an existing Edit control
  44.  
  45. BOOL CParsedEdit::SubclassEdit(UINT nID, CWnd* pParent, WORD wParseStyle)
  46. {
  47.     m_wParseStyle = wParseStyle;
  48.     HWND hWndEdit = ::GetDlgItem(pParent->m_hWnd, nID);
  49.     if (hWndEdit == NULL)
  50.         return FALSE;
  51.     return SubclassWindow(hWndEdit);
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Input character filter
  56.  
  57. void CParsedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  58. {
  59.     WORD type;
  60.  
  61.     if (nChar < 0x20)
  62.         type = PES_ALL;                         // always allow control chars
  63.     else if (nChar >= '0' && nChar <= '9')
  64.         type = PES_NUMBERS;
  65.     else if (nChar >= 'A' && nChar <= 'Z')      // hard coded to english
  66.         type = PES_LETTERS;
  67.     else if (nChar >= 'a' && nChar <= 'z')
  68.         type = PES_LETTERS;
  69.     else
  70.         type = PES_OTHERCHARS;
  71.  
  72.     if (m_wParseStyle & type)
  73.     {
  74.         CEdit::OnChar(nChar, nRepCnt, nFlags);  // permitted
  75.     }
  76.     else
  77.     {
  78.         // illegal character - inform parent
  79.         OnBadInput();
  80.     }
  81. }
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Spin controls will send scroll messages
  85.  
  86. void CParsedEdit::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  87. {
  88.     int nDelta = 0;
  89.     if (nSBCode == SB_LINEDOWN)
  90.         nDelta = -1;
  91.     else if (nSBCode == SB_LINEUP)
  92.         nDelta = +1;
  93.     else
  94.         return; // nothing special
  95.  
  96.     // set the focus to this edit item and select it all
  97.     SetFocus();
  98.  
  99.     //Get the number in the control.
  100.     BOOL bOk;
  101.     int nOld = GetParent()->GetDlgItemInt(GetDlgCtrlID(), &bOk);
  102.     if (bOk)
  103.     {
  104.         // The MuScroll control also supports range checking
  105.         // for this example, we just prevent overflow
  106.         int nNew = nOld + nDelta;
  107.         if (nNew >= 0 && nNew <= 32767)
  108.             GetParent()->SetDlgItemInt(GetDlgCtrlID(), nNew);
  109.         else
  110.             bOk = FALSE;
  111.     }
  112.  
  113.     if (!bOk)
  114.         OnBadInput();
  115.     SetSel(0, -1);
  116. }
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // default bad input handler, beep (unless parent notification
  120. //    returns -1.  Most parent dialogs will return 0 or 1 for command
  121. //    handlers (i.e. Beep is the default)
  122.  
  123. void CParsedEdit::OnBadInput()
  124. {
  125.     if (GetParent()->SendMessage(WM_COMMAND,
  126.         GetDlgCtrlID(), MAKELONG(m_hWnd, PEN_ILLEGALCHAR)) != -1) 
  127.     {
  128.         MessageBeep(-1);
  129.     }
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133.