diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-02-01 03:25:16 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-02-01 04:20:09 +0300 |
commit | 23b68bc0d3470faffa0bb122cd1c203b24568c87 (patch) | |
tree | cdd1cc8e3764dff1f17fb69bc1d6c18b645440c5 /src | |
parent | 292968544ed758ae58c29b613cb89a7b67e551fa (diff) | |
download | manaplus-23b68bc0d3470faffa0bb122cd1c203b24568c87.tar.gz manaplus-23b68bc0d3470faffa0bb122cd1c203b24568c87.tar.bz2 manaplus-23b68bc0d3470faffa0bb122cd1c203b24568c87.tar.xz manaplus-23b68bc0d3470faffa0bb122cd1c203b24568c87.zip |
Not allow enter not numeric chars in numeric text fields.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/widgets/textfield.cpp | 56 |
1 files changed, 35 insertions, 21 deletions
diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index 4ad02ccbb..4dba2eb57 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -183,7 +183,7 @@ int TextField::getValue() const if (value < mMinimum) return mMinimum; - if (value > mMaximum) + if (value > (signed)mMaximum) return mMaximum; return value; @@ -193,31 +193,45 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) { int val = keyEvent.getKey().getValue(); - if (val >= 32 && (mNumeric || !mMaximum || mText.size() < mMaximum)) + if (val >= 32) { - int l; - if (val < 128) - l = 1; // 0xxxxxxx - else if (val < 0x800) - l = 2; // 110xxxxx 10xxxxxx - else if (val < 0x10000) - l = 3; // 1110xxxx 10xxxxxx 10xxxxxx - else - l = 4; // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - - char buf[4]; - for (int i = 0; i < l; ++i) + if (mNumeric) { - buf[i] = static_cast<char>(val >> (6 * (l - i - 1))); - if (i > 0) - buf[i] = static_cast<char>((buf[i] & 63) | 128); + if ((val >= '0' && val <= '9') || (val == '-' && !mCaretPosition)) + { + char buf[2]; + buf[0] = val; + buf[1] = 0; + mText.insert(mCaretPosition, std::string(buf)); + mCaretPosition += 1; + } } + else if (!mMaximum || mText.size() < mMaximum) + { + int l; + if (val < 128) + l = 1; // 0xxxxxxx + else if (val < 0x800) + l = 2; // 110xxxxx 10xxxxxx + else if (val < 0x10000) + l = 3; // 1110xxxx 10xxxxxx 10xxxxxx + else + l = 4; // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + + char buf[4]; + for (int i = 0; i < l; ++i) + { + buf[i] = static_cast<char>(val >> (6 * (l - i - 1))); + if (i > 0) + buf[i] = static_cast<char>((buf[i] & 63) | 128); + } - if (l > 1) - buf[0] |= static_cast<char>(255 << (8 - l)); + if (l > 1) + buf[0] |= static_cast<char>(255 << (8 - l)); - mText.insert(mCaretPosition, std::string(buf, buf + l)); - mCaretPosition += l; + mText.insert(mCaretPosition, std::string(buf, buf + l)); + mCaretPosition += l; + } } /* In UTF-8, 10xxxxxx is only used for inner parts of characters. So skip |