summaryrefslogtreecommitdiff
path: root/src/gui/textfield.cpp
diff options
context:
space:
mode:
authorDouglas Boffey <DougABoffey@netscape.net>2008-09-04 16:56:10 +0000
committerDouglas Boffey <DougABoffey@netscape.net>2008-09-04 16:56:10 +0000
commit7946cd875877870e7cab002cba099d21cf9fc063 (patch)
tree38dd4c1c4a766edda7b3156e2bc1af08dbd94a6a /src/gui/textfield.cpp
parent27e8d20ad8b5590995d1d4af3563f8956f180373 (diff)
downloadmana-7946cd875877870e7cab002cba099d21cf9fc063.tar.gz
mana-7946cd875877870e7cab002cba099d21cf9fc063.tar.bz2
mana-7946cd875877870e7cab002cba099d21cf9fc063.tar.xz
mana-7946cd875877870e7cab002cba099d21cf9fc063.zip
Added code to change text colouring
Diffstat (limited to 'src/gui/textfield.cpp')
-rw-r--r--src/gui/textfield.cpp68
1 files changed, 67 insertions, 1 deletions
diff --git a/src/gui/textfield.cpp b/src/gui/textfield.cpp
index 95291267..21c3adbc 100644
--- a/src/gui/textfield.cpp
+++ b/src/gui/textfield.cpp
@@ -38,7 +38,9 @@ int TextField::instances = 0;
ImageRect TextField::skin;
TextField::TextField(const std::string& text):
- gcn::TextField(text)
+ gcn::TextField(text),
+ mNumeric(false),
+ mListener(0)
{
setFrameSize(2);
@@ -102,3 +104,67 @@ void TextField::drawFrame(gcn::Graphics *graphics)
static_cast<Graphics*>(graphics)->drawImageRect(0, 0, w, h, skin);
}
+
+void TextField::setNumeric(bool numeric)
+{
+ mNumeric = numeric;
+ if (!numeric)
+ {
+ return;
+ }
+ const char *text = mText.c_str();
+ for (const char *textPtr = text; *textPtr; ++textPtr)
+ {
+ if (*textPtr < '0' || *textPtr > '9')
+ {
+ setText(mText.substr(0, textPtr - text));
+ return;
+ }
+ }
+}
+
+void TextField::keyPressed(gcn::KeyEvent &keyEvent)
+{
+ if (mNumeric)
+ {
+ while (true)
+ {
+ const gcn::Key &key = keyEvent.getKey();
+ if (key.isNumber())
+ {
+ break;
+ }
+ int value = key.getValue();
+ if (value == gcn::Key::LEFT || value == gcn::Key::RIGHT ||
+ value == gcn::Key::HOME || value == gcn::Key::END ||
+ value == gcn::Key::BACKSPACE || value == gcn::Key::DELETE)
+ {
+ break;
+ }
+ return;
+ }
+ }
+ gcn::TextField::keyPressed(keyEvent);
+ if (mListener)
+ {
+ mListener->listen(this);
+ }
+}
+
+int TextField::getValue() const
+{
+ if (!mNumeric)
+ {
+ return 0;
+ }
+ int value = atoi(mText.c_str());
+ if (value < mMinimum)
+ {
+ return mMinimum;
+ }
+ if (value > mMaximum)
+ {
+ return mMaximum;
+ }
+ return value;
+}