dlgfloat.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "stdafx.h"
  11. #include <float.h> // floating point precision
  12. #ifdef AFX_CORE3_SEG
  13. #pragma code_seg(AFX_CORE3_SEG)
  14. #endif
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Extra data validation procs for float/double support
  21. // see "dlgdata.cpp" for non-floating point support
  22. /////////////////////////////////////////////////////////////////////////////
  23. AFX_STATIC BOOL AFXAPI _AfxSimpleFloatParse(LPCTSTR lpszText, double& d)
  24. {
  25. ASSERT(lpszText != NULL);
  26. while (*lpszText == ' ' || *lpszText == '\t')
  27. lpszText++;
  28. TCHAR chFirst = lpszText[0];
  29. d = _tcstod(lpszText, (LPTSTR*)&lpszText);
  30. if (d == 0.0 && chFirst != '0')
  31. return FALSE; // could not convert
  32. while (*lpszText == ' ' || *lpszText == '\t')
  33. lpszText++;
  34. if (*lpszText != '\0')
  35. return FALSE; // not terminated properly
  36. return TRUE;
  37. }
  38. void AFXAPI AfxTextFloatFormat(CDataExchange* pDX, int nIDC,
  39. void* pData, double value, int nSizeGcvt)
  40. {
  41. ASSERT(pData != NULL);
  42. HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  43. TCHAR szBuffer[32];
  44. if (pDX->m_bSaveAndValidate)
  45. {
  46. ::GetWindowText(hWndCtrl, szBuffer, _countof(szBuffer));
  47. double d;
  48. if (!_AfxSimpleFloatParse(szBuffer, d))
  49. {
  50. AfxMessageBox(AFX_IDP_PARSE_REAL);
  51. pDX->Fail(); // throws exception
  52. }
  53. if (nSizeGcvt == FLT_DIG)
  54. *((float*)pData) = (float)d;
  55. else
  56. *((double*)pData) = d;
  57. }
  58. else
  59. {
  60. _stprintf(szBuffer, _T("%.*g"), nSizeGcvt, value);
  61. AfxSetWindowText(hWndCtrl, szBuffer);
  62. }
  63. }
  64. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, float& value)
  65. {
  66. AfxTextFloatFormat(pDX, nIDC, &value, value, FLT_DIG);
  67. }
  68. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, double& value)
  69. {
  70. AfxTextFloatFormat(pDX, nIDC, &value, value, DBL_DIG);
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // Validation procs
  74. AFX_STATIC void AFXAPI _AfxFailMinMaxReal(CDataExchange* pDX,
  75. double minVal, double maxVal, int precision, UINT nIDPrompt)
  76. // error string must have '%1' and '%2' in it
  77. {
  78. if (!pDX->m_bSaveAndValidate)
  79. {
  80. TRACE0("Warning: initial dialog data is out of range.\n");
  81. return; // don't stop now
  82. }
  83. TCHAR szMin[32], szMax[32];
  84. CString prompt;
  85. _stprintf(szMin, _T("%.*g"), precision, minVal);
  86. _stprintf(szMax, _T("%.*g"), precision, maxVal);
  87. AfxFormatString2(prompt, nIDPrompt, szMin, szMax);
  88. AfxMessageBox(prompt, MB_ICONEXCLAMATION, nIDPrompt);
  89. prompt.Empty(); // exception prep
  90. pDX->Fail();
  91. }
  92. void AFXAPI DDV_MinMaxFloat(CDataExchange* pDX, float const& value, float minVal, float maxVal)
  93. {
  94. ASSERT(minVal <= maxVal);
  95. if (value < minVal || value > maxVal)
  96. _AfxFailMinMaxReal(pDX, (double)minVal, (double)maxVal, FLT_DIG,
  97. AFX_IDP_PARSE_REAL_RANGE);
  98. }
  99. void AFXAPI DDV_MinMaxDouble(CDataExchange* pDX, double const& value, double minVal, double maxVal)
  100. {
  101. ASSERT(minVal <= maxVal);
  102. if (value < minVal || value > maxVal)
  103. _AfxFailMinMaxReal(pDX, (double)minVal, (double)maxVal, DBL_DIG,
  104. AFX_IDP_PARSE_REAL_RANGE);
  105. }
  106. /////////////////////////////////////////////////////////////////////////////