diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-01-07 23:32:02 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-01-07 23:32:02 +0300 |
commit | 276a5f9f7ef2f1fd5fad7339db2d2a9154ce9870 (patch) | |
tree | b3e2aa22629dab18547b0b98a1a0f48a4f2217e9 /src/gui/widgets/textfield.cpp | |
parent | cce1a905f8d798eb689956395daf6161f034ae6f (diff) | |
download | mv-276a5f9f7ef2f1fd5fad7339db2d2a9154ce9870.tar.gz mv-276a5f9f7ef2f1fd5fad7339db2d2a9154ce9870.tar.bz2 mv-276a5f9f7ef2f1fd5fad7339db2d2a9154ce9870.tar.xz mv-276a5f9f7ef2f1fd5fad7339db2d2a9154ce9870.zip |
In input fields add support for combinations Ctrl+left, Ctrl+right.
Diffstat (limited to 'src/gui/widgets/textfield.cpp')
-rw-r--r-- | src/gui/widgets/textfield.cpp | 371 |
1 files changed, 223 insertions, 148 deletions
diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index ddee77233..58a1f7e18 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -24,9 +24,7 @@ #include "client.h" -#if defined ANDROID || defined USE_SDL2 #include "input/inputmanager.h" -#endif #include "input/keydata.h" #include "input/keyevent.h" @@ -275,71 +273,7 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) mLastEventPaste = 0; bool consumed(false); - switch (val) - { - case 2: // Ctrl+b - { - moveCaretBack(); - consumed = true; - break; - } - - case 6: // Ctrl+f - { - moveCaretForward(); - consumed = true; - break; - } - - case 4: // Ctrl+d - { - caretDelete(); - consumed = true; - break; - } - - case 8: // Ctrl+h - deleteCharLeft(mText, &mCaretPosition); - consumed = true; - break; - - case 5: // Ctrl+e - mCaretPosition = static_cast<int>(mText.size()); - consumed = true; - break; - - case 11: // Ctrl+k - mText = mText.substr(0, mCaretPosition); - consumed = true; - break; - - case 21: // Ctrl+u - caretDeleteToStart(); - consumed = true; - break; - - case 3: // Ctrl+c - handleCopy(); - consumed = true; - break; - - case 22: // Control code 22, SYNCHRONOUS IDLE, sent on Ctrl+v - // hack to prevent paste key sticking - if (mLastEventPaste && mLastEventPaste > cur_time) - break; - handlePaste(); - mLastEventPaste = cur_time + 2; - consumed = true; - break; - - case 23: // Ctrl+w - caretDeleteWord(); - consumed = true; - break; - - default: - break; - } + handleSDLKeys(val, consumed); if (consumed) { @@ -352,7 +286,39 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) } #endif - const int action = static_cast<KeyEvent*>(&keyEvent)->getActionId(); + if (consumed) + { + keyEvent.consume(); + } + else + { + const int action = static_cast<KeyEvent*>(&keyEvent)->getActionId(); + if (!inputManager.isActionActive(static_cast<int>( + Input::KEY_GUI_CTRL))) + { + if (!handleNormalKeys(action, consumed)) + { + if (consumed) + keyEvent.consume(); + return; + } + } + else + { + handleCtrlKeys(action, consumed); + } + } + + if (mSendAlwaysEvents) + distributeActionEvent(); + + if (consumed) + keyEvent.consume(); + fixScroll(); +} + +bool TextField::handleNormalKeys(const int action, bool &consumed) +{ switch (action) { case Input::KEY_GUI_LEFT: @@ -407,9 +373,9 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) case Input::KEY_GUI_SELECT2: distributeActionEvent(); - keyEvent.consume(); + consumed = true; fixScroll(); - return; + return false; case Input::KEY_GUI_HOME: mCaretPosition = 0; @@ -423,99 +389,169 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) case Input::KEY_GUI_TAB: if (mLoseFocusOnTab) - return; + return false; consumed = true; break; default: break; } + return true; +} - if (mSendAlwaysEvents) - distributeActionEvent(); - - if (consumed) +void TextField::handleCtrlKeys(const int action, bool &consumed) +{ + switch (action) { - keyEvent.consume(); - } + case Input::KEY_GUI_LEFT: + { + moveCaretWordBack(); + consumed = true; + break; + } + case Input::KEY_GUI_RIGHT: + { + moveCaretWordForward(); + consumed = true; + break; + } #ifdef USE_SDL2 - else + case Input::KEY_GUI_B: + { + moveCaretBack(); + consumed = true; + break; + } + case Input::KEY_GUI_C: + { + handleCopy(); + consumed = true; + break; + } + case Input::KEY_GUI_D: + { + caretDelete(); + consumed = true; + break; + } + case Input::KEY_GUI_E: + { + mCaretPosition = static_cast<int>(mText.size()); + consumed = true; + break; + } + case Input::KEY_GUI_F: + { + moveCaretBack(); + consumed = true; + break; + } + case Input::KEY_GUI_H: + { + deleteCharLeft(mText, &mCaretPosition); + consumed = true; + break; + } + case Input::KEY_GUI_U: + { + caretDeleteToStart(); + consumed = true; + break; + } + case Input::KEY_GUI_K: + { + mText = mText.substr(0, mCaretPosition); + consumed = true; + break; + } + case Input::KEY_GUI_V: + { + handlePaste(); + consumed = true; + break; + } + case Input::KEY_GUI_W: + { + caretDeleteWord(); + consumed = true; + break; + } +#endif + default: + break; + } +} + +#ifndef USE_SDL2 +void TextField::handleSDLKeys(const int val, bool &consumed) +{ + switch (val) { - if (inputManager.isActionActive(static_cast<int>(Input::KEY_GUI_CTRL))) + case 2: // Ctrl+b { - switch (action) - { - case Input::KEY_GUI_B: - { - moveCaretBack(); - consumed = true; - break; - } - case Input::KEY_GUI_C: - { - handleCopy(); - consumed = true; - break; - } - case Input::KEY_GUI_D: - { - caretDelete(); - consumed = true; - break; - } - case Input::KEY_GUI_E: - { - mCaretPosition = static_cast<int>(mText.size()); - consumed = true; - break; - } - case Input::KEY_GUI_F: - { - moveCaretBack(); - consumed = true; - break; - } - case Input::KEY_GUI_H: - { - deleteCharLeft(mText, &mCaretPosition); - consumed = true; - break; - } - case Input::KEY_GUI_U: - { - caretDeleteToStart(); - consumed = true; - break; - } - case Input::KEY_GUI_K: - { - mText = mText.substr(0, mCaretPosition); - consumed = true; - break; - } - case Input::KEY_GUI_V: - { - handlePaste(); - consumed = true; - break; - } - case Input::KEY_GUI_W: - { - caretDeleteWord(); - consumed = true; - break; - } + moveCaretBack(); + consumed = true; + break; + } - default: - break; - } + case 6: // Ctrl+f + { + moveCaretForward(); + consumed = true; + break; } + + case 4: // Ctrl+d + { + caretDelete(); + consumed = true; + break; + } + + case 8: // Ctrl+h + deleteCharLeft(mText, &mCaretPosition); + consumed = true; + break; + + case 5: // Ctrl+e + mCaretPosition = static_cast<int>(mText.size()); + consumed = true; + break; + + case 11: // Ctrl+k + mText = mText.substr(0, mCaretPosition); + consumed = true; + break; + + case 21: // Ctrl+u + caretDeleteToStart(); + consumed = true; + break; + + case 3: // Ctrl+c + handleCopy(); + consumed = true; + break; + + case 22: // Control code 22, SYNCHRONOUS IDLE, sent on Ctrl+v + // hack to prevent paste key sticking + if (mLastEventPaste && mLastEventPaste > cur_time) + break; + handlePaste(); + mLastEventPaste = cur_time + 2; + consumed = true; + break; + + case 23: // Ctrl+w + caretDeleteWord(); + consumed = true; + break; + + default: + break; } - if (consumed) - keyEvent.consume(); -#endif - fixScroll(); } +#endif void TextField::moveCaretBack() { @@ -571,6 +607,45 @@ void TextField::caretDeleteToStart() } } +void TextField::moveCaretWordBack() +{ + const int oldCaret = mCaretPosition; + while (mCaretPosition > 0) + { + if (!isWordSeparator(mText[mCaretPosition - 1])) + break; + mCaretPosition --; + } + if (oldCaret != mCaretPosition) + return; + while (mCaretPosition > 0) + { + if (isWordSeparator(mText[mCaretPosition - 1])) + break; + mCaretPosition --; + } +} + +void TextField::moveCaretWordForward() +{ + const unsigned sz = static_cast<unsigned>(mText.size()); + const int oldCaret = mCaretPosition; + while (mCaretPosition < sz) + { + if (!isWordSeparator(mText[mCaretPosition])) + break; + mCaretPosition ++; + } + if (oldCaret != mCaretPosition) + return; + while (mCaretPosition < sz) + { + if (isWordSeparator(mText[mCaretPosition])) + break; + mCaretPosition ++; + } +} + void TextField::caretDeleteWord() { while (mCaretPosition > 0) |