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