Skip to content

Commit

Permalink
Fix handling of Buttons of messagebox and inputbox:
Browse files Browse the repository at this point in the history
Use `actionListener` instead of `mouseListener` so activate button with keyboard also works.
  • Loading branch information
Jarod42 committed Sep 28, 2024
1 parent b3b2cbe commit 0c9536b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
6 changes: 3 additions & 3 deletions include/guisan/widgets/inputbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace gcn
/**
* A non-movable window to get a short string from the user.
*/
class GCN_CORE_DECLSPEC InputBox : public Window
class GCN_CORE_DECLSPEC InputBox : public Window, public ActionListener
{
public:

Expand Down Expand Up @@ -118,9 +118,9 @@ namespace gcn
*/
int getClickedButton() const;

// Inherited from MouseListener
// Inherited from ActionListener

void mouseReleased(MouseEvent& mouseEvent) override;
void action(const ActionEvent& actionEvent) override;

protected:
int mClickedButton = -1;
Expand Down
7 changes: 4 additions & 3 deletions include/guisan/widgets/messagebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#ifndef GCN_MESSAGEBOX_HPP
#define GCN_MESSAGEBOX_HPP

#include "guisan/actionlistener.hpp"
#include "guisan/platform.hpp"
#include "guisan/widgets/button.hpp"
#include "guisan/widgets/label.hpp"
Expand All @@ -71,7 +72,7 @@ namespace gcn
/**
* A non-movable window to display a message with some buttons.
*/
class GCN_CORE_DECLSPEC MessageBox : public Window
class GCN_CORE_DECLSPEC MessageBox : public Window, public ActionListener
{
public:
/**
Expand Down Expand Up @@ -142,9 +143,9 @@ namespace gcn
*/
void addToContainer(Container* container);

// Inherited from MouseListener
// Inherited from ActionListener

void mouseReleased(MouseEvent& mouseEvent) override;
void action(const ActionEvent& keyEvent) override;

protected:
Graphics::Alignment mButtonAlignment = Graphics::Alignment::Left;
Expand Down
12 changes: 5 additions & 7 deletions src/widgets/inputbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ namespace gcn
const std::string& cancel) :
Window(caption)
{
addMouseListener(this);
setMovable(false);

mLabel = std::make_unique<Label>(message);
Expand All @@ -85,12 +84,12 @@ namespace gcn

mButtonOK = std::make_unique<Button>(ok);
mButtonOK->setAlignment(Graphics::Center);
mButtonOK->addMouseListener(this);
mButtonOK->addActionListener(this);
mButtonOK->adjustSize();

mButtonCancel = std::make_unique<Button>(cancel);
mButtonCancel->setAlignment(Graphics::Center);
mButtonCancel->addMouseListener(this);
mButtonCancel->addActionListener(this);
mButtonCancel->adjustSize();

// Look-and-feel: make both buttons the same width
Expand Down Expand Up @@ -136,20 +135,19 @@ namespace gcn
releaseModalFocus();
}

void InputBox::mouseReleased(MouseEvent& mouseEvent)
void InputBox::action(const ActionEvent& actionEvent)
{
if (mouseEvent.getSource() == mButtonOK.get())
if (actionEvent.getSource() == mButtonOK.get())
{
mClickedButton = 0;
distributeActionEvent();
}
else if (mouseEvent.getSource() == mButtonCancel.get())
else if (actionEvent.getSource() == mButtonCancel.get())
{
mClickedButton = 1;
setVisible(false);
distributeActionEvent();
}
Window::mouseReleased(mouseEvent);
}

int InputBox::getClickedButton() const
Expand Down
22 changes: 7 additions & 15 deletions src/widgets/messagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ namespace gcn
int size) :
Window(caption)
{
addMouseListener(this);
setMovable(false);

mLabel = std::make_unique<Label>(message);
Expand All @@ -102,7 +101,7 @@ namespace gcn
mButtons.emplace_back(std::make_unique<Button>(button_captions[i]));
auto& button = mButtons.back();
button->setAlignment(Graphics::Center);
button->addMouseListener(this);
button->addActionListener(this);
maxButtonWidth = std::max(maxButtonWidth, button->getWidth());
maxButtonHeight = std::max(maxButtonHeight, button->getHeight());
}
Expand Down Expand Up @@ -184,24 +183,17 @@ namespace gcn
return mButtonAlignment;
}

void MessageBox::mouseReleased(MouseEvent& mouseEvent)
void MessageBox::action(const ActionEvent& actionEvent)
{
if (mouseEvent.getSource() != this)
for (std::size_t i = 0; i != mButtons.size(); ++i)
{
for (std::size_t i = 0; i != mButtons.size(); ++i)
if (actionEvent.getSource() == mButtons[i].get())
{
if (mouseEvent.getSource() == mButtons[i].get())
{
mClickedButton = static_cast<int>(i);
distributeActionEvent();
break;
}
mClickedButton = static_cast<int>(i);
distributeActionEvent();
break;
}
}
else
{
Window::mouseReleased(mouseEvent);
}
}

int MessageBox::getClickedButton() const
Expand Down

0 comments on commit 0c9536b

Please sign in to comment.