From 23b68bc0d3470faffa0bb122cd1c203b24568c87 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 1 Feb 2012 03:25:16 +0300 Subject: Not allow enter not numeric chars in numeric text fields. --- src/gui/widgets/textfield.cpp | 56 +++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'src') 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(val >> (6 * (l - i - 1))); - if (i > 0) - buf[i] = static_cast((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(val >> (6 * (l - i - 1))); + if (i > 0) + buf[i] = static_cast((buf[i] & 63) | 128); + } - if (l > 1) - buf[0] |= static_cast(255 << (8 - l)); + if (l > 1) + buf[0] |= static_cast(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 -- cgit v1.2.3-70-g09d2