From 693879649420c0997bfb29bea7f5752f6d9ac526 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 17 Apr 2012 11:31:47 +0300 Subject: Move keyboard handling from guichan to own classes. --- src/gui/speechbubble.cpp | 5 +- src/gui/widgets/checkbox.cpp | 12 +++ src/gui/widgets/checkbox.h | 2 + src/gui/widgets/radiobutton.cpp | 13 ++++ src/gui/widgets/radiobutton.h | 2 + src/gui/widgets/slider.cpp | 36 +++++++++ src/gui/widgets/slider.h | 2 + src/gui/widgets/tabbedarea.cpp | 31 ++++++++ src/gui/widgets/tabbedarea.h | 2 + src/gui/widgets/textbox.cpp | 148 ++++++++++++++++++++++++++++++++++++ src/gui/widgets/textbox.h | 2 + src/guichan/widgets/checkbox.cpp | 10 +-- src/guichan/widgets/listbox.cpp | 47 +----------- src/guichan/widgets/radiobutton.cpp | 11 +-- src/guichan/widgets/slider.cpp | 34 +-------- src/guichan/widgets/tabbedarea.cpp | 29 +------ src/guichan/widgets/textbox.cpp | 146 +---------------------------------- 17 files changed, 258 insertions(+), 274 deletions(-) diff --git a/src/gui/speechbubble.cpp b/src/gui/speechbubble.cpp index 877715137..6dc528a0d 100644 --- a/src/gui/speechbubble.cpp +++ b/src/gui/speechbubble.cpp @@ -28,12 +28,11 @@ #include "gui/gui.h" #include "gui/theme.h" +#include "gui/widgets/label.h" #include "gui/widgets/textbox.h" #include -#include - #include "debug.h" SpeechBubble::SpeechBubble(): @@ -43,7 +42,7 @@ SpeechBubble::SpeechBubble(): setMinWidth(29); setMinHeight(29); - mCaption = new gcn::Label; + mCaption = new Label; mCaption->setFont(boldFont); mSpeechBox = new TextBox; diff --git a/src/gui/widgets/checkbox.cpp b/src/gui/widgets/checkbox.cpp index 0689c5395..f599e7831 100644 --- a/src/gui/widgets/checkbox.cpp +++ b/src/gui/widgets/checkbox.cpp @@ -190,3 +190,15 @@ void CheckBox::mouseExited(gcn::MouseEvent& event A_UNUSED) { mHasMouse = false; } + +void CheckBox::keyPressed(gcn::KeyEvent& keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == gcn::Key::ENTER || + key.getValue() == gcn::Key::SPACE) + { + toggleSelected(); + keyEvent.consume(); + } +} diff --git a/src/gui/widgets/checkbox.h b/src/gui/widgets/checkbox.h index 0c8e48553..e86e4b214 100644 --- a/src/gui/widgets/checkbox.h +++ b/src/gui/widgets/checkbox.h @@ -74,6 +74,8 @@ class CheckBox : public gcn::CheckBox */ void mouseExited(gcn::MouseEvent& event); + void keyPressed(gcn::KeyEvent& keyEvent); + private: static int instances; static float mAlpha; diff --git a/src/gui/widgets/radiobutton.cpp b/src/gui/widgets/radiobutton.cpp index 94152a716..4a9a912b1 100644 --- a/src/gui/widgets/radiobutton.cpp +++ b/src/gui/widgets/radiobutton.cpp @@ -158,3 +158,16 @@ void RadioButton::mouseExited(gcn::MouseEvent& event A_UNUSED) { mHasMouse = false; } + +void RadioButton::keyPressed(gcn::KeyEvent& keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == gcn::Key::ENTER || + key.getValue() == gcn::Key::SPACE) + { + setSelected(true); + distributeActionEvent(); + keyEvent.consume(); + } +} diff --git a/src/gui/widgets/radiobutton.h b/src/gui/widgets/radiobutton.h index 7692d478a..f796a4fc9 100644 --- a/src/gui/widgets/radiobutton.h +++ b/src/gui/widgets/radiobutton.h @@ -65,6 +65,8 @@ class RadioButton : public gcn::RadioButton */ void mouseExited(gcn::MouseEvent& event); + void keyPressed(gcn::KeyEvent& keyEvent); + private: static int instances; static float mAlpha; diff --git a/src/gui/widgets/slider.cpp b/src/gui/widgets/slider.cpp index 7142cd202..24a2a5128 100644 --- a/src/gui/widgets/slider.cpp +++ b/src/gui/widgets/slider.cpp @@ -297,3 +297,39 @@ void Slider::mouseExited(gcn::MouseEvent& event A_UNUSED) { mHasMouse = false; } + +void Slider::keyPressed(gcn::KeyEvent& keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (getOrientation() == HORIZONTAL) + { + if (key.getValue() == gcn::Key::RIGHT) + { + setValue(getValue() + getStepLength()); + distributeActionEvent(); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::LEFT) + { + setValue(getValue() - getStepLength()); + distributeActionEvent(); + keyEvent.consume(); + } + } + else + { + if (key.getValue() == gcn::Key::UP) + { + setValue(getValue() + getStepLength()); + distributeActionEvent(); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::DOWN) + { + setValue(getValue() - getStepLength()); + distributeActionEvent(); + keyEvent.consume(); + } + } +} diff --git a/src/gui/widgets/slider.h b/src/gui/widgets/slider.h index 8eed984b1..00aefcb1a 100644 --- a/src/gui/widgets/slider.h +++ b/src/gui/widgets/slider.h @@ -75,6 +75,8 @@ class Slider : public gcn::Slider */ void mouseExited(gcn::MouseEvent& event); + void keyPressed(gcn::KeyEvent& keyEvent); + private: /** * Used to initialize instances. diff --git a/src/gui/widgets/tabbedarea.cpp b/src/gui/widgets/tabbedarea.cpp index fc6526a13..bb8ea063b 100644 --- a/src/gui/widgets/tabbedarea.cpp +++ b/src/gui/widgets/tabbedarea.cpp @@ -477,6 +477,37 @@ void TabbedArea::removeAll() } } +void TabbedArea::keyPressed(gcn::KeyEvent& keyEvent) +{ + if (keyEvent.isConsumed() || !isFocused()) + return; + + if (keyEvent.getKey().getValue() == gcn::Key::LEFT) + { + int index = getSelectedTabIndex(); + index--; + + if (index < 0) + return; + else + setSelectedTab(mTabs[index].first); + + keyEvent.consume(); + } + else if (keyEvent.getKey().getValue() == gcn::Key::RIGHT) + { + int index = getSelectedTabIndex(); + index++; + + if (index >= static_cast(mTabs.size())) + return; + else + setSelectedTab(mTabs[index].first); + + keyEvent.consume(); + } +} + /* void TabbedArea::moveLeft(gcn::Tab *tab) { diff --git a/src/gui/widgets/tabbedarea.h b/src/gui/widgets/tabbedarea.h index cceaf56b7..c36a3ab1d 100644 --- a/src/gui/widgets/tabbedarea.h +++ b/src/gui/widgets/tabbedarea.h @@ -148,6 +148,8 @@ class TabbedArea : public gcn::TabbedArea, public gcn::WidgetListener void fixSize() { adjustSize(); } + void keyPressed(gcn::KeyEvent& keyEvent); + private: typedef std::vector< std::pair > TabContainer; 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(mTextRows[mCaretRow].size())) + { + ++ mCaretRow; + + if (mCaretRow >= static_cast(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( + mTextRows[mCaretRow].size()) && mEditable) + { + mTextRows[mCaretRow].erase(mCaretColumn, 1); + } + else if (key.getValue() == gcn::Key::DELETE + && mCaretColumn == static_cast( + mTextRows[mCaretRow].size()) + && mCaretRow < (static_cast(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(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(key.getValue()))); + ++ mCaretColumn; + } + + adjustSize(); + scrollToCaret(); + + keyEvent.consume(); +} diff --git a/src/gui/widgets/textbox.h b/src/gui/widgets/textbox.h index a052247c4..5f69dcf51 100644 --- a/src/gui/widgets/textbox.h +++ b/src/gui/widgets/textbox.h @@ -64,6 +64,8 @@ class TextBox : public gcn::TextBox gcn::TextBox::draw(graphics); } + void keyPressed(gcn::KeyEvent& keyEvent); + private: int mMinWidth; const gcn::Color *mTextColor; diff --git a/src/guichan/widgets/checkbox.cpp b/src/guichan/widgets/checkbox.cpp index c7ec3e1e0..413f8e6af 100644 --- a/src/guichan/widgets/checkbox.cpp +++ b/src/guichan/widgets/checkbox.cpp @@ -99,16 +99,8 @@ namespace gcn mCaption = caption; } - void CheckBox::keyPressed(KeyEvent& keyEvent) + void CheckBox::keyPressed(KeyEvent& keyEvent A_UNUSED) { - Key key = keyEvent.getKey(); - - if (key.getValue() == Key::ENTER || - key.getValue() == Key::SPACE) - { - toggleSelected(); - keyEvent.consume(); - } } void CheckBox::mouseClicked(MouseEvent& mouseEvent) diff --git a/src/guichan/widgets/listbox.cpp b/src/guichan/widgets/listbox.cpp index 5760025c2..3152890bc 100644 --- a/src/guichan/widgets/listbox.cpp +++ b/src/guichan/widgets/listbox.cpp @@ -189,53 +189,8 @@ namespace gcn distributeValueChangedEvent(); } - void ListBox::keyPressed(KeyEvent& keyEvent) + void ListBox::keyPressed(KeyEvent& keyEvent A_UNUSED) { - Key key = keyEvent.getKey(); - - if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) - { - distributeActionEvent(); - keyEvent.consume(); - } - else if (key.getValue() == Key::UP) - { - setSelected(mSelected - 1); - - if (mSelected == -1) - { - if (mWrappingEnabled) - setSelected(getListModel()->getNumberOfElements() - 1); - else - setSelected(0); - } - - keyEvent.consume(); - } - else if (key.getValue() == Key::DOWN) - { - if (mWrappingEnabled - && getSelected() == getListModel()->getNumberOfElements() - 1) - { - setSelected(0); - } - else - { - setSelected(getSelected() + 1); - } - - keyEvent.consume(); - } - else if (key.getValue() == Key::HOME) - { - setSelected(0); - keyEvent.consume(); - } - else if (key.getValue() == Key::END) - { - setSelected(getListModel()->getNumberOfElements() - 1); - keyEvent.consume(); - } } void ListBox::mousePressed(MouseEvent& mouseEvent) diff --git a/src/guichan/widgets/radiobutton.cpp b/src/guichan/widgets/radiobutton.cpp index 7d0391cbe..7811ea558 100644 --- a/src/guichan/widgets/radiobutton.cpp +++ b/src/guichan/widgets/radiobutton.cpp @@ -128,17 +128,8 @@ namespace gcn mCaption = caption; } - void RadioButton::keyPressed(KeyEvent& keyEvent) + void RadioButton::keyPressed(KeyEvent& keyEvent A_UNUSED) { - Key key = keyEvent.getKey(); - - if (key.getValue() == Key::ENTER || - key.getValue() == Key::SPACE) - { - setSelected(true); - distributeActionEvent(); - keyEvent.consume(); - } } void RadioButton::mouseClicked(MouseEvent& mouseEvent) diff --git a/src/guichan/widgets/slider.cpp b/src/guichan/widgets/slider.cpp index 36f067eab..d7e8084cc 100644 --- a/src/guichan/widgets/slider.cpp +++ b/src/guichan/widgets/slider.cpp @@ -191,40 +191,8 @@ namespace gcn mMarkerLength = length; } - void Slider::keyPressed(KeyEvent& keyEvent) + void Slider::keyPressed(KeyEvent& keyEvent A_UNUSED) { - Key key = keyEvent.getKey(); - - if (getOrientation() == HORIZONTAL) - { - if (key.getValue() == Key::RIGHT) - { - setValue(getValue() + getStepLength()); - distributeActionEvent(); - keyEvent.consume(); - } - else if (key.getValue() == Key::LEFT) - { - setValue(getValue() - getStepLength()); - distributeActionEvent(); - keyEvent.consume(); - } - } - else - { - if (key.getValue() == Key::UP) - { - setValue(getValue() + getStepLength()); - distributeActionEvent(); - keyEvent.consume(); - } - else if (key.getValue() == Key::DOWN) - { - setValue(getValue() - getStepLength()); - distributeActionEvent(); - keyEvent.consume(); - } - } } void Slider::setOrientation(Slider::Orientation orientation) diff --git a/src/guichan/widgets/tabbedarea.cpp b/src/guichan/widgets/tabbedarea.cpp index 3e7178548..fb6619bed 100644 --- a/src/guichan/widgets/tabbedarea.cpp +++ b/src/guichan/widgets/tabbedarea.cpp @@ -298,35 +298,8 @@ namespace gcn adjustSize(); } - void TabbedArea::keyPressed(KeyEvent& keyEvent) + void TabbedArea::keyPressed(KeyEvent& keyEvent A_UNUSED) { - if (keyEvent.isConsumed() || !isFocused()) - return; - - if (keyEvent.getKey().getValue() == Key::LEFT) - { - int index = getSelectedTabIndex(); - index--; - - if (index < 0) - return; - else - setSelectedTab(mTabs[index].first); - - keyEvent.consume(); - } - else if (keyEvent.getKey().getValue() == Key::RIGHT) - { - int index = getSelectedTabIndex(); - index++; - - if (index >= static_cast(mTabs.size())) - return; - else - setSelectedTab(mTabs[index].first); - - keyEvent.consume(); - } } void TabbedArea::death(const Event& event) diff --git a/src/guichan/widgets/textbox.cpp b/src/guichan/widgets/textbox.cpp index da7edaac7..44fb5d718 100644 --- a/src/guichan/widgets/textbox.cpp +++ b/src/guichan/widgets/textbox.cpp @@ -168,152 +168,8 @@ namespace gcn mouseEvent.consume(); } - void TextBox::keyPressed(KeyEvent& keyEvent) + void TextBox::keyPressed(KeyEvent& keyEvent A_UNUSED) { - Key key = keyEvent.getKey(); - - if (key.getValue() == Key::LEFT) - { - --mCaretColumn; - if (mCaretColumn < 0) - { - --mCaretRow; - - if (mCaretRow < 0) - { - mCaretRow = 0; - mCaretColumn = 0; - } - else - { - mCaretColumn = mTextRows[mCaretRow].size(); - } - } - } - else if (key.getValue() == Key::RIGHT) - { - ++mCaretColumn; - if (mCaretColumn > static_cast(mTextRows[mCaretRow].size())) - { - ++ mCaretRow; - - if (mCaretRow >= static_cast(mTextRows.size())) - { - mCaretRow = mTextRows.size() - 1; - if (mCaretRow < 0) - mCaretRow = 0; - - mCaretColumn = mTextRows[mCaretRow].size(); - } - else - { - mCaretColumn = 0; - } - } - } - else if (key.getValue() == Key::DOWN) - { - setCaretRow(mCaretRow + 1); - } - else if (key.getValue() == Key::UP) - { - setCaretRow(mCaretRow - 1); - } - else if (key.getValue() == Key::HOME) - { - mCaretColumn = 0; - } - else if (key.getValue() == Key::END) - { - mCaretColumn = mTextRows[mCaretRow].size(); - } - else if (key.getValue() == 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() == Key::BACKSPACE - && mCaretColumn != 0 - && mEditable) - { - mTextRows[mCaretRow].erase(mCaretColumn - 1, 1); - --mCaretColumn; - } - else if (key.getValue() == 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() == Key::DELETE - && mCaretColumn < static_cast( - mTextRows[mCaretRow].size()) && mEditable) - { - mTextRows[mCaretRow].erase(mCaretColumn, 1); - } - else if (key.getValue() == Key::DELETE - && mCaretColumn == static_cast( - mTextRows[mCaretRow].size()) - && mCaretRow < (static_cast(mTextRows.size()) - 1) - && mEditable) - { - mTextRows[mCaretRow] += mTextRows[mCaretRow + 1]; - mTextRows.erase(mTextRows.begin() + mCaretRow + 1); - } - else if (key.getValue() == Key::PAGE_UP) - { - Widget* par = getParent(); - - if (par) - { - int rowsPerPage = par->getChildrenArea().height - / getFont()->getHeight(); - mCaretRow -= rowsPerPage; - - if (mCaretRow < 0) - mCaretRow = 0; - } - } - else if (key.getValue() == Key::PAGE_DOWN) - { - Widget* par = getParent(); - - if (par) - { - int rowsPerPage = par->getChildrenArea().height - / getFont()->getHeight(); - mCaretRow += rowsPerPage; - - if (mCaretRow >= static_cast(mTextRows.size())) - mCaretRow = mTextRows.size() - 1; - } - } - else if (key.getValue() == 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(key.getValue()))); - ++ mCaretColumn; - } - - adjustSize(); - scrollToCaret(); - - keyEvent.consume(); } void TextBox::adjustSize() -- cgit v1.2.3-70-g09d2