From 373f1aca8bb651332fec7031853d9a685ebc896e Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Wed, 4 Feb 2009 20:57:04 -0700 Subject: Added key and mouse events to the table class. Signed-off-by: Ira Rice --- src/gui/skill.cpp | 1 + src/gui/table.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/gui/table.h | 33 ++++++++++----- 3 files changed, 143 insertions(+), 12 deletions(-) diff --git a/src/gui/skill.cpp b/src/gui/skill.cpp index 4587e75a..dd7c3a4e 100644 --- a/src/gui/skill.cpp +++ b/src/gui/skill.cpp @@ -127,6 +127,7 @@ SkillDialog::SkillDialog(): mTable = new GuiTable(mTableModel); mTable->setOpaque(false); mTable->setLinewiseSelection(true); + mTable->setWrappingEnabled(true); mTable->setActionEventId("skill"); mTable->addActionListener(this); diff --git a/src/gui/table.cpp b/src/gui/table.cpp index af42165a..3bed48d8 100644 --- a/src/gui/table.cpp +++ b/src/gui/table.cpp @@ -21,12 +21,15 @@ #include #include +#include #include "colour.h" #include "table.h" #include "../configuration.h" +#include "../utils/dtor.h" + float GuiTable::mAlpha = config.getValue("guialpha", 0.8); class GuiTableActionListener : public gcn::ActionListener @@ -78,6 +81,7 @@ void GuiTableActionListener::action(const gcn::ActionEvent& actionEvent) GuiTable::GuiTable(TableModel *initial_model, gcn::Color background, bool opacity) : mLinewiseMode(false), + mWrappingEnabled(false), mOpaque(opacity), mBackgroundColor(background), mModel(NULL), @@ -86,6 +90,8 @@ GuiTable::GuiTable(TableModel *initial_model, gcn::Color background, mTopWidget(NULL) { setModel(initial_model); + setFocusable(true); + addMouseListener(this); addKeyListener(this); } @@ -177,10 +183,63 @@ int GuiTable::getColumnWidth(int i) return 0; } +void GuiTable::setSelectedRow(int selected) +{ + if (mModel == NULL) + { + mSelectedRow = -1; + } + else + { + if (selected < 0) + { + mSelectedRow = -1; + } + else if (selected >= mModel->getRows() && mWrappingEnabled) + { + mSelectedRow = 0; + } + else if (selected >= mModel->getRows() && !mWrappingEnabled) + { + mSelectedRow = mModel->getRows() - 1; + } + else + { + mSelectedRow = selected; + } + } +} + +void GuiTable::setSelectedColumn(int selected) +{ + if (mModel == NULL) + { + mSelectedColumn = -1; + } + else + { + if (selected < 0) + { + mSelectedColumn = -1; + } + else if (selected >= mModel->getColumns() && mWrappingEnabled) + { + mSelectedColumn = 0; + } + else if (selected >= mModel->getColumns() && !mWrappingEnabled) + { + mSelectedColumn = mModel->getColumns() - 1; + } + else + { + mSelectedColumn = selected; + } + } +} + void GuiTable::uninstallActionListeners(void) { - for (std::vector::const_iterator it = action_listeners.begin(); it != action_listeners.end(); it++) - delete *it; + delete_all(action_listeners); action_listeners.clear(); } @@ -333,6 +392,49 @@ gcn::Rectangle GuiTable::getChildrenArea(void) // -- KeyListener notifications void GuiTable::keyPressed(gcn::KeyEvent& keyEvent) { + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == gcn::Key::ENTER || key.getValue() == gcn::Key::SPACE) + { + distributeActionEvent(); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::UP) + { + setSelectedRow(mSelectedRow - 1); + + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::DOWN) + { + setSelectedRow(mSelectedRow + 1); + + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::LEFT) + { + setSelectedColumn(mSelectedColumn - 1); + + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::RIGHT) + { + setSelectedColumn(mSelectedColumn + 1); + + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::HOME) + { + setSelectedRow(0); + setSelectedColumn(0); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::END) + { + setSelectedRow(mModel->getRows() - 1); + setSelectedColumn(mModel->getColumns() - 1); + keyEvent.consume(); + } } // -- MouseListener notifications @@ -355,10 +457,25 @@ void GuiTable::mousePressed(gcn::MouseEvent& mouseEvent) void GuiTable::mouseWheelMovedUp(gcn::MouseEvent& mouseEvent) { + if (isFocused()) + { + if (getSelectedRow() > 0 ) + { + setSelectedRow(getSelectedRow() - 1); + } + + mouseEvent.consume(); + } } void GuiTable::mouseWheelMovedDown(gcn::MouseEvent& mouseEvent) { + if (isFocused()) + { + setSelectedRow(getSelectedRow() + 1); + + mouseEvent.consume(); + } } void GuiTable::mouseDragged(gcn::MouseEvent& mouseEvent) diff --git a/src/gui/table.h b/src/gui/table.h index d63b1fb9..912eb284 100644 --- a/src/gui/table.h +++ b/src/gui/table.h @@ -33,8 +33,9 @@ class GuiTableActionListener; /** - * A table, with rows and columns made out of sub-widgets. Largely inspired by (and can be thought of as a generalisation of) - * the guichan listbox implementation. + * A table, with rows and columns made out of sub-widgets. Largely inspired by + * (and can be thought of as a generalisation of) the guichan listbox + * implementation. * * Normally you want this within a ScrollArea. * @@ -60,27 +61,38 @@ public: /** * Sets the table model * - * Note that actions issued by widgets returned from the model will update the table - * selection, but only AFTER any event handlers installed within the widget have been - *triggered. To be notified after such an update, add an action listener to the table - * instead. + * Note that actions issued by widgets returned from the model will update + * the table selection, but only AFTER any event handlers installed within + * the widget have been triggered. To be notified after such an update, add + * an action listener to the table instead. */ void setModel(TableModel *m); + const TableModel* getModel() {return mModel;} + void setSelected(int row, int column); int getSelectedRow(void); int getSelectedColumn(void); + void setSelectedRow(int selected); + + void setSelectedColumn(int selected); + + bool isWrappingEnabled() const {return mWrappingEnabled;} + + void setWrappingEnabled(bool wrappingEnabled) + {mWrappingEnabled = wrappingEnabled;} + gcn::Rectangle getChildrenArea(void); /** - * Toggle whether to use linewise selection mode, in which the table selects an entire - * line at a time, rather than a single cell. + * Toggle whether to use linewise selection mode, in which the table selects + * an entire line at a time, rather than a single cell. * - * Note that column information is tracked even in linewise selection mode; this mode - * therefore only affects visualisation. + * Note that column information is tracked even in linewise selection mode; + * this mode therefore only affects visualisation. * * Disabled by default. * @@ -144,6 +156,7 @@ private: int getColumnForX(int x); // -1 on error void recomputeDimensions(void); bool mLinewiseMode; + bool mWrappingEnabled; bool mOpaque; static float mAlpha; -- cgit v1.2.3-70-g09d2