1 /*
2 	GWEN
3 	Copyright (c) 2010 Facepunch Studios
4 	See license in Gwen.h
5 */
6 
7 #include "Gwen/Gwen.h"
8 #include "Gwen/Controls/TextBox.h"
9 #include "Gwen/Skin.h"
10 #include "Gwen/Utility.h"
11 #include "Gwen/Platform.h"
12 
13 using namespace Gwen;
14 using namespace Gwen::Controls;
15 
GWEN_CONTROL_CONSTRUCTOR(TextBoxNumeric)16 GWEN_CONTROL_CONSTRUCTOR(TextBoxNumeric)
17 {
18 	SetText(L"0");
19 }
20 
IsTextAllowed(const Gwen::UnicodeString & str,int iPos)21 bool TextBoxNumeric::IsTextAllowed(const Gwen::UnicodeString& str, int iPos)
22 {
23 	const UnicodeString& strString = GetText();
24 
25 	if (str.length() == 0)
26 		return true;
27 
28 	for (size_t i = 0; i < str.length(); i++)
29 	{
30 		if (str[i] == L'-')
31 		{
32 			// Has to be at the start
33 			if (i != 0 || iPos != 0)
34 				return false;
35 
36 			// Can only be one
37 			if (std::count(strString.begin(), strString.end(), L'-') > 0)
38 				return false;
39 
40 			continue;
41 		}
42 
43 		if (str[i] == L'0') continue;
44 		if (str[i] == L'1') continue;
45 		if (str[i] == L'2') continue;
46 		if (str[i] == L'3') continue;
47 		if (str[i] == L'4') continue;
48 		if (str[i] == L'5') continue;
49 		if (str[i] == L'6') continue;
50 		if (str[i] == L'7') continue;
51 		if (str[i] == L'8') continue;
52 		if (str[i] == L'9') continue;
53 
54 		if (str[i] == L'.')
55 		{
56 			// Already a fullstop
57 			if (std::count(strString.begin(), strString.end(), L'.') > 0)
58 				return false;
59 
60 			continue;
61 		}
62 
63 		return false;
64 	}
65 
66 	return true;
67 }
68 
GetFloatFromText()69 float TextBoxNumeric::GetFloatFromText()
70 {
71 	double temp = GwenUtil_WideStringToFloat(GetText().c_str());
72 	return temp;
73 }