TextBoxNumeric.cpp 1.4 KB

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