summaryrefslogtreecommitdiff
path: root/src/interface/Textbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/interface/Textbox.cpp')
-rw-r--r--src/interface/Textbox.cpp74
1 files changed, 65 insertions, 9 deletions
diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp
index ad5597f..29e36a3 100644
--- a/src/interface/Textbox.cpp
+++ b/src/interface/Textbox.cpp
@@ -15,7 +15,8 @@ Textbox::Textbox(Point position, Point size, std::string textboxText, std::strin
masked(false),
border(true),
mouseDown(false),
- limit(0)
+ limit(std::string::npos),
+ inputType(All)
{
placeHolder = textboxPlaceholder;
@@ -74,6 +75,26 @@ void Textbox::SetText(std::string newText)
}
}
+Textbox::ValidInput Textbox::GetInputType()
+{
+ return inputType;
+}
+
+void Textbox::SetInputType(ValidInput input)
+{
+ inputType = input;
+}
+
+void Textbox::SetLimit(size_t limit)
+{
+ this->limit = limit;
+}
+
+size_t Textbox::GetLimit()
+{
+ return limit;
+}
+
void Textbox::SetDisplayText(std::string newText)
{
Label::SetText(text);
@@ -194,6 +215,20 @@ void Textbox::pasteIntoSelection()
}
}
+bool Textbox::CharacterValid(Uint16 character)
+{
+ switch(inputType)
+ {
+ case Number:
+ case Numeric:
+ return (character >= '0' && character <= '9');
+ case All:
+ default:
+ return (character >= ' ' && character < 127);
+ }
+ return false;
+}
+
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
bool changed = false;
@@ -219,17 +254,21 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
{
case KEY_HOME:
cursor = 0;
+ ClearSelection();
break;
case KEY_END:
cursor = backingText.length();
+ ClearSelection();
break;
case KEY_LEFT:
if(cursor > 0)
cursor--;
+ ClearSelection();
break;
case KEY_RIGHT:
if(cursor < backingText.length())
cursor++;
+ ClearSelection();
break;
case KEY_DELETE:
if(HasSelection())
@@ -248,6 +287,7 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
backingText.erase(cursor, 1);
changed = true;
}
+ ClearSelection();
break;
case KEY_BACKSPACE:
if(HasSelection())
@@ -272,9 +312,10 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
}
changed = true;
}
+ ClearSelection();
break;
}
- if(character >= ' ' && character < 127)
+ if(CharacterValid(character))
{
if(HasSelection())
{
@@ -284,24 +325,39 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
cursor = getLowerSelectionBound();
}
- if(cursor == backingText.length())
- {
- backingText += character;
- }
- else
+ if(limit==std::string::npos || backingText.length() < limit)
{
- backingText.insert(cursor, 1, (char)character);
+ if(cursor == backingText.length())
+ {
+ backingText += character;
+ }
+ else
+ {
+ backingText.insert(cursor, 1, (char)character);
+ }
}
cursor++;
changed = true;
+ ClearSelection();
}
- ClearSelection();
}
catch(std::out_of_range &e)
{
cursor = 0;
backingText = "";
}
+ if(inputType == Number)
+ {
+ if(backingText.length()>1)
+ {
+ while(backingText[0] == '0')
+ backingText.erase(backingText.begin());
+ }
+ if(!backingText.length())
+ backingText = "0";
+ }
+ if(cursor > backingText.length())
+ cursor = backingText.length();
if(changed)
{
if(masked)