home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dlgfloat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  3.6 KB  |  126 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include <float.h>              // floating point precision
  13.  
  14. #ifdef AFX_CORE3_SEG
  15. #pragma code_seg(AFX_CORE3_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Extra data validation procs for float/double support
  25. //  see "dlgdata.cpp" for non-floating point support
  26. /////////////////////////////////////////////////////////////////////////////
  27.  
  28. AFX_STATIC BOOL AFXAPI _AfxSimpleFloatParse(LPCTSTR lpszText, double& d)
  29. {
  30.     ASSERT(lpszText != NULL);
  31.     while (*lpszText == ' ' || *lpszText == '\t')
  32.         lpszText++;
  33.  
  34.     TCHAR chFirst = lpszText[0];
  35.     d = _tcstod(lpszText, (LPTSTR*)&lpszText);
  36.     if (d == 0.0 && chFirst != '0')
  37.         return FALSE;   // could not convert
  38.     while (*lpszText == ' ' || *lpszText == '\t')
  39.         lpszText++;
  40.  
  41.     if (*lpszText != '\0')
  42.         return FALSE;   // not terminated properly
  43.  
  44.     return TRUE;
  45. }
  46.  
  47. void AFXAPI AfxTextFloatFormat(CDataExchange* pDX, int nIDC,
  48.     void* pData, double value, int nSizeGcvt)
  49. {
  50.     ASSERT(pData != NULL);
  51.  
  52.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  53.     TCHAR szBuffer[32];
  54.     if (pDX->m_bSaveAndValidate)
  55.     {
  56.         ::GetWindowText(hWndCtrl, szBuffer, _countof(szBuffer));
  57.         double d;
  58.         if (!_AfxSimpleFloatParse(szBuffer, d))
  59.         {
  60.             AfxMessageBox(AFX_IDP_PARSE_REAL);
  61.             pDX->Fail();            // throws exception
  62.         }
  63.         if (nSizeGcvt == FLT_DIG)
  64.             *((float*)pData) = (float)d;
  65.         else
  66.             *((double*)pData) = d;
  67.     }
  68.     else
  69.     {
  70.         _stprintf(szBuffer, _T("%.*g"), nSizeGcvt, value);
  71.         AfxSetWindowText(hWndCtrl, szBuffer);
  72.     }
  73. }
  74.  
  75. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, float& value)
  76. {
  77.     AfxTextFloatFormat(pDX, nIDC, &value, value, FLT_DIG);
  78. }
  79.  
  80. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, double& value)
  81. {
  82.     AfxTextFloatFormat(pDX, nIDC, &value, value, DBL_DIG);
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // Validation procs
  87.  
  88. AFX_STATIC void AFXAPI _AfxFailMinMaxReal(CDataExchange* pDX,
  89.      double minVal, double maxVal, int precision, UINT nIDPrompt)
  90.     // error string must have '%1' and '%2' in it
  91. {
  92.     if (!pDX->m_bSaveAndValidate)
  93.     {
  94.         TRACE0("Warning: initial dialog data is out of range.\n");
  95.         return;         // don't stop now
  96.     }
  97.     TCHAR szMin[32], szMax[32];
  98.     CString prompt;
  99.  
  100.     _stprintf(szMin, _T("%.*g"), precision, minVal);
  101.     _stprintf(szMax, _T("%.*g"), precision, maxVal);
  102.     AfxFormatString2(prompt, nIDPrompt, szMin, szMax);
  103.  
  104.     AfxMessageBox(prompt, MB_ICONEXCLAMATION, nIDPrompt);
  105.     prompt.Empty(); // exception prep
  106.     pDX->Fail();
  107. }
  108.  
  109. void AFXAPI DDV_MinMaxFloat(CDataExchange* pDX, float const& value, float minVal, float maxVal)
  110. {
  111.     ASSERT(minVal <= maxVal);
  112.     if (value < minVal || value > maxVal)
  113.         _AfxFailMinMaxReal(pDX, (double)minVal, (double)maxVal, FLT_DIG,
  114.             AFX_IDP_PARSE_REAL_RANGE);
  115. }
  116.  
  117. void AFXAPI DDV_MinMaxDouble(CDataExchange* pDX, double const& value, double minVal, double maxVal)
  118. {
  119.     ASSERT(minVal <= maxVal);
  120.     if (value < minVal || value > maxVal)
  121.         _AfxFailMinMaxReal(pDX, (double)minVal, (double)maxVal, DBL_DIG,
  122.             AFX_IDP_PARSE_REAL_RANGE);
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126.