SkyFloat.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. // SkyFloat.cpp : implementation of DDX_SkyFloat
  13. /////////////////////////////////////////////////////////////////////////////
  14. // Public functions
  15. #include "StdAfx.h"
  16. //--------------------------------------------------------------------------------------------
  17. void AFXAPI DDX_SkyFloat(CDataExchange* pDX, int nIDC, float &fNumber)
  18. {
  19. BOOL bTrue = TRUE;
  20. DDX_SkyFloat( pDX, nIDC, fNumber, bTrue);
  21. }
  22. void AFXAPI DDX_SkyFloat(CDataExchange* pDX, int nIDC, float &fNumber, BOOL &bValid)
  23. {
  24. HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  25. if (pDX->m_bSaveAndValidate)
  26. {
  27. if (!FloatFromString(hWndCtrl, fNumber, bValid))
  28. {
  29. AfxMessageBox(L"Invalid character entered");
  30. pDX->Fail();
  31. }
  32. }
  33. else
  34. {
  35. StringFromFloat(hWndCtrl, fNumber, bValid);
  36. }
  37. }
  38. BOOL FloatFromString(HWND hWnd, float &fNumber, BOOL &bValid)
  39. {
  40. TCHAR szWindowText[20];
  41. ::GetWindowText(hWnd, szWindowText, 19);
  42. if( CTString( CStringA(szWindowText)) == "")
  43. {
  44. bValid = FALSE;
  45. return TRUE;
  46. }
  47. bValid = TRUE;
  48. float fTmpNumber = fNumber;
  49. int iNumLen, iRetLen;
  50. iNumLen = strlen( CStringA(szWindowText));
  51. iRetLen = sscanf( CStringA(szWindowText), "%f", &fTmpNumber);
  52. if( (iRetLen == 1) || ((iNumLen == 1) && (szWindowText[0] == '-') || (iNumLen == 0)) )
  53. {
  54. fNumber = fTmpNumber;
  55. return TRUE;
  56. }
  57. else
  58. {
  59. return FALSE;
  60. }
  61. }
  62. void StringFromFloat(HWND hWnd, float fNumber, BOOL &bValid)
  63. {
  64. if( !bValid)
  65. {
  66. ::SetWindowText(hWnd, L"");
  67. return;
  68. }
  69. CString str;
  70. str.Format(_T("%g"), fNumber);
  71. ::SetWindowText(hWnd, str.GetBufferSetLength(20));
  72. }
  73. //--------------------------------------------------------------------------------------------