home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / NUMEDIT.ZIP / NumEdit.cpp next >
Encoding:
C/C++ Source or Header  |  1999-01-05  |  2.6 KB  |  132 lines

  1. // NumEdit.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SpinCtrl.h"
  6. #include "NumEdit.h"
  7. #include "Float.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CNumEdit
  17.  
  18. CNumEdit::CNumEdit()
  19. {
  20.     m_Verbose = FALSE;
  21.     m_MinValue = -FLT_MAX;
  22.     m_MaxValue = FLT_MAX;
  23.     m_Delta = FLT_ROUNDS;
  24. }
  25.  
  26. CNumEdit::~CNumEdit()
  27. {
  28. }
  29.  
  30.  
  31. BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
  32.     //{{AFX_MSG_MAP(CNumEdit)
  33.     ON_WM_CHAR()
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CNumEdit message handlers
  39.  
  40. float CNumEdit::GetValue()
  41. {
  42.     float f;
  43.     if (IsValid() == VALID)
  44.     {
  45.         CString str;
  46.         GetWindowText(str);
  47.         sscanf(str, "%f", &f);
  48.     }
  49.     return f;
  50. }
  51.  
  52. void CNumEdit::SetValue(float val)
  53. {
  54.     CString str;
  55.     str.Format("%f", val);
  56.     SetWindowText(str);
  57. }
  58.  
  59. int CNumEdit::IsValid()
  60. {
  61.     CString str;
  62.     GetWindowText(str);
  63.     int res = VALID;
  64.     float f;
  65.     char lp[10];
  66.     if (sscanf(str, "%f%s", &f, lp) != 1) res = INVALID_CHAR;
  67.     if (f > m_MaxValue && f < m_MinValue) res = OUT_OF_RANGE;
  68.     if (m_Verbose && res != VALID)
  69.     {
  70.         str.Empty();
  71.         if (res & OUT_OF_RANGE) str += _T("Given value is out of range.\n");
  72.         if (res & INVALID_CHAR) str += _T("Characters must be a number.\n");
  73.         AfxMessageBox(str, MB_OK | MB_ICONSTOP);
  74.     }
  75.     return res;
  76. }
  77.  
  78. int CNumEdit::IsValid(const CString &str)
  79. {
  80.     ASSERT(m_MinValue >= m_MaxValue);
  81.     int res = VALID;
  82.     float f;
  83.     if (sscanf(str, "%f", &f) != 1) res = INVALID_CHAR;
  84.     if (f > m_MaxValue && f < m_MinValue) res = OUT_OF_RANGE;
  85.     if (m_Verbose && res != VALID)
  86.     {
  87.         CString msg;
  88.         msg.Empty();
  89.         if (res & OUT_OF_RANGE) msg += _T("Given value is out of range.\n");
  90.         if (res & INVALID_CHAR) msg += _T("Characters must be a number.\n");
  91.         AfxMessageBox(msg, MB_OK | MB_ICONSTOP);
  92.     }
  93.     return res;
  94. }
  95.  
  96. void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  97. {
  98.     if (nChar == ' ') return;
  99.     CString oldstr;
  100.     GetWindowText(oldstr);
  101.     CEdit::OnChar(nChar, nRepCnt, nFlags);
  102.     if (IsValid() != VALID)
  103.     {
  104.         SetWindowText(oldstr);
  105.         SetSel(0, -1);
  106.         MSG msg;
  107.         while (::PeekMessage(&msg, m_hWnd, WM_CHAR, WM_CHAR, PM_REMOVE));
  108.     }
  109. }
  110.  
  111. BOOL CNumEdit::Verbose()
  112. {
  113.     return m_Verbose;
  114. }
  115.  
  116. void CNumEdit::Verbose(BOOL v)
  117. {
  118.     m_Verbose = v;
  119. }
  120.  
  121. void CNumEdit::SetRange(float max, float min)
  122. {
  123.     m_MinValue = min;
  124.     m_MaxValue = max;
  125. }
  126.  
  127. void CNumEdit::GetRange(float & max, float & min)
  128. {
  129.     min = m_MinValue;
  130.     max = m_MaxValue;
  131. }
  132.