summaryrefslogtreecommitdiff
path: root/src/gui/widgets/textbox.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-04-17 11:31:47 +0300
committerAndrei Karas <akaras@inbox.ru>2012-04-17 11:31:47 +0300
commit693879649420c0997bfb29bea7f5752f6d9ac526 (patch)
tree01d6c56532d561385edbfe2ad0235df6c5a58c5f /src/gui/widgets/textbox.cpp
parent8df35828d69f3debd89557a74c26359a8b249f87 (diff)
downloadmv-693879649420c0997bfb29bea7f5752f6d9ac526.tar.gz
mv-693879649420c0997bfb29bea7f5752f6d9ac526.tar.bz2
mv-693879649420c0997bfb29bea7f5752f6d9ac526.tar.xz
mv-693879649420c0997bfb29bea7f5752f6d9ac526.zip
Move keyboard handling from guichan to own classes.
Diffstat (limited to 'src/gui/widgets/textbox.cpp')
-rw-r--r--src/gui/widgets/textbox.cpp148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/gui/widgets/textbox.cpp b/src/gui/widgets/textbox.cpp
index 01cb4bddb..3f3c9fb9e 100644
--- a/src/gui/widgets/textbox.cpp
+++ b/src/gui/widgets/textbox.cpp
@@ -152,3 +152,151 @@ void TextBox::setTextWrapped(const std::string &text, int minDimension)
gcn::TextBox::setText(wrappedStream.str());
}
+
+void TextBox::keyPressed(gcn::KeyEvent& keyEvent)
+{
+ gcn::Key key = keyEvent.getKey();
+
+ if (key.getValue() == gcn::Key::LEFT)
+ {
+ --mCaretColumn;
+ if (mCaretColumn < 0)
+ {
+ --mCaretRow;
+
+ if (mCaretRow < 0)
+ {
+ mCaretRow = 0;
+ mCaretColumn = 0;
+ }
+ else
+ {
+ mCaretColumn = mTextRows[mCaretRow].size();
+ }
+ }
+ }
+ else if (key.getValue() == gcn::Key::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;
+ }
+ }
+ }
+ 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)
+ {
+ int rowsPerPage = par->getChildrenArea().height
+ / getFont()->getHeight();
+ mCaretRow -= rowsPerPage;
+
+ if (mCaretRow < 0)
+ mCaretRow = 0;
+ }
+ }
+ else if (key.getValue() == gcn::Key::PAGE_DOWN)
+ {
+ gcn::Widget* par = getParent();
+
+ if (par)
+ {
+ int rowsPerPage = par->getChildrenArea().height
+ / getFont()->getHeight();
+ mCaretRow += rowsPerPage;
+
+ if (mCaretRow >= static_cast<int>(mTextRows.size()))
+ mCaretRow = mTextRows.size() - 1;
+ }
+ }
+ 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();
+
+ keyEvent.consume();
+}