diff options
Diffstat (limited to 'src/gui/widgets/textbox.cpp')
-rw-r--r-- | src/gui/widgets/textbox.cpp | 259 |
1 files changed, 147 insertions, 112 deletions
diff --git a/src/gui/widgets/textbox.cpp b/src/gui/widgets/textbox.cpp index 3f3c9fb9e..b40a7dcb1 100644 --- a/src/gui/widgets/textbox.cpp +++ b/src/gui/widgets/textbox.cpp @@ -22,6 +22,9 @@ #include "gui/widgets/textbox.h" +#include "keydata.h" +#include "keyevent.h" + #include "gui/theme.h" #include <guichan/font.hpp> @@ -156,144 +159,176 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension) void TextBox::keyPressed(gcn::KeyEvent& keyEvent) { gcn::Key key = keyEvent.getKey(); + int action = static_cast<KeyEvent*>(&keyEvent)->getActionId(); - if (key.getValue() == gcn::Key::LEFT) + switch (action) { - --mCaretColumn; - if (mCaretColumn < 0) + case Input::KEY_GUI_LEFT: + { + --mCaretColumn; + if (mCaretColumn < 0) + { + --mCaretRow; + + if (mCaretRow < 0) + { + mCaretRow = 0; + mCaretColumn = 0; + } + else + { + mCaretColumn = mTextRows[mCaretRow].size(); + } + } + break; + } + + case Input::KEY_GUI_RIGHT: + { + ++mCaretColumn; + if (mCaretColumn > static_cast<int>(mTextRows[mCaretRow].size())) + { + ++ mCaretRow; + + if (mCaretRow >= static_cast<int>(mTextRows.size())) + { + mCaretRow = mTextRows.size() - 1; + if (mCaretRow < 0) + mCaretRow = 0; + + mCaretColumn = mTextRows[mCaretRow].size(); + } + else + { + mCaretColumn = 0; + } + } + break; + } + + case Input::KEY_GUI_DOWN: + { + setCaretRow(mCaretRow + 1); + break; + } + case Input::KEY_GUI_UP: + { + setCaretRow(mCaretRow - 1); + break; + } + case Input::KEY_GUI_HOME: + { + mCaretColumn = 0; + break; + } + case Input::KEY_GUI_END: { - --mCaretRow; + mCaretColumn = mTextRows[mCaretRow].size(); + break; + } - if (mCaretRow < 0) + case Input::KEY_GUI_SELECT2: + { + if (mEditable) { - mCaretRow = 0; + mTextRows.insert(mTextRows.begin() + mCaretRow + 1, + mTextRows[mCaretRow].substr(mCaretColumn, + mTextRows[mCaretRow].size() - mCaretColumn)); + mTextRows[mCaretRow].resize(mCaretColumn); + ++mCaretRow; mCaretColumn = 0; } - else + break; + } + + case Input::KEY_GUI_BACKSPACE: + { + if (mCaretColumn != 0 && mEditable) { - mCaretColumn = mTextRows[mCaretRow].size(); + mTextRows[mCaretRow].erase(mCaretColumn - 1, 1); + --mCaretColumn; } + else if (mCaretColumn == 0 && mCaretRow != 0 && mEditable) + { + mCaretColumn = mTextRows[mCaretRow - 1].size(); + mTextRows[mCaretRow - 1] += mTextRows[mCaretRow]; + mTextRows.erase(mTextRows.begin() + mCaretRow); + --mCaretRow; + } + break; } - } - else if (key.getValue() == gcn::Key::RIGHT) - { - ++mCaretColumn; - if (mCaretColumn > static_cast<int>(mTextRows[mCaretRow].size())) + + case Input::KEY_GUI_DELETE: { - ++ mCaretRow; + if (mCaretColumn < static_cast<int>( + mTextRows[mCaretRow].size()) && mEditable) + { + mTextRows[mCaretRow].erase(mCaretColumn, 1); + } + else if (mCaretColumn == static_cast<int>( + mTextRows[mCaretRow].size()) && + mCaretRow < (static_cast<int>(mTextRows.size()) - 1) && + mEditable) + { + mTextRows[mCaretRow] += mTextRows[mCaretRow + 1]; + mTextRows.erase(mTextRows.begin() + mCaretRow + 1); + } + break; + } + + case Input::KEY_GUI_PAGE_UP: + { + gcn::Widget* par = getParent(); - if (mCaretRow >= static_cast<int>(mTextRows.size())) + if (par) { - mCaretRow = mTextRows.size() - 1; + int rowsPerPage = par->getChildrenArea().height + / getFont()->getHeight(); + mCaretRow -= rowsPerPage; + if (mCaretRow < 0) mCaretRow = 0; - - mCaretColumn = mTextRows[mCaretRow].size(); - } - else - { - mCaretColumn = 0; } + break; } - } - else if (key.getValue() == gcn::Key::DOWN) - { - setCaretRow(mCaretRow + 1); - } - else if (key.getValue() == gcn::Key::UP) - { - setCaretRow(mCaretRow - 1); - } - else if (key.getValue() == gcn::Key::HOME) - { - mCaretColumn = 0; - } - else if (key.getValue() == gcn::Key::END) - { - mCaretColumn = mTextRows[mCaretRow].size(); - } - else if (key.getValue() == gcn::Key::ENTER && mEditable) - { - mTextRows.insert(mTextRows.begin() + mCaretRow + 1, - mTextRows[mCaretRow].substr(mCaretColumn, - mTextRows[mCaretRow].size() - mCaretColumn)); - mTextRows[mCaretRow].resize(mCaretColumn); - ++mCaretRow; - mCaretColumn = 0; - } - else if (key.getValue() == gcn::Key::BACKSPACE - && mCaretColumn != 0 - && mEditable) - { - mTextRows[mCaretRow].erase(mCaretColumn - 1, 1); - --mCaretColumn; - } - else if (key.getValue() == gcn::Key::BACKSPACE - && mCaretColumn == 0 - && mCaretRow != 0 - && mEditable) - { - mCaretColumn = mTextRows[mCaretRow - 1].size(); - mTextRows[mCaretRow - 1] += mTextRows[mCaretRow]; - mTextRows.erase(mTextRows.begin() + mCaretRow); - --mCaretRow; - } - else if (key.getValue() == gcn::Key::DELETE - && mCaretColumn < static_cast<int>( - mTextRows[mCaretRow].size()) && mEditable) - { - mTextRows[mCaretRow].erase(mCaretColumn, 1); - } - else if (key.getValue() == gcn::Key::DELETE - && mCaretColumn == static_cast<int>( - mTextRows[mCaretRow].size()) - && mCaretRow < (static_cast<int>(mTextRows.size()) - 1) - && mEditable) - { - mTextRows[mCaretRow] += mTextRows[mCaretRow + 1]; - mTextRows.erase(mTextRows.begin() + mCaretRow + 1); - } - else if (key.getValue() == gcn::Key::PAGE_UP) - { - gcn::Widget* par = getParent(); - if (par) + case Input::KEY_GUI_PAGE_DOWN: { - int rowsPerPage = par->getChildrenArea().height - / getFont()->getHeight(); - mCaretRow -= rowsPerPage; + gcn::Widget* par = getParent(); + + if (par) + { + int rowsPerPage = par->getChildrenArea().height + / getFont()->getHeight(); + mCaretRow += rowsPerPage; - if (mCaretRow < 0) - mCaretRow = 0; + if (mCaretRow >= static_cast<int>(mTextRows.size())) + mCaretRow = mTextRows.size() - 1; + } + break; } - } - else if (key.getValue() == gcn::Key::PAGE_DOWN) - { - gcn::Widget* par = getParent(); - if (par) + case Input::KEY_GUI_TAB: { - int rowsPerPage = par->getChildrenArea().height - / getFont()->getHeight(); - mCaretRow += rowsPerPage; + if (mEditable) + { + mTextRows[mCaretRow].insert(mCaretColumn, std::string(" ")); + mCaretColumn += 4; + } + break; + } - if (mCaretRow >= static_cast<int>(mTextRows.size())) - mCaretRow = mTextRows.size() - 1; + default: + { + if (key.isCharacter() && mEditable) + { + mTextRows[mCaretRow].insert(mCaretColumn, + std::string(1, static_cast<char>(key.getValue()))); + ++ mCaretColumn; + } + break; } } - else if (key.getValue() == gcn::Key::TAB - && mEditable) - { - mTextRows[mCaretRow].insert(mCaretColumn, std::string(" ")); - mCaretColumn += 4; - } - else if (key.isCharacter() - && mEditable) - { - mTextRows[mCaretRow].insert(mCaretColumn, - std::string(1, static_cast<char>(key.getValue()))); - ++ mCaretColumn; - } adjustSize(); scrollToCaret(); |