Skip to content

Commit

Permalink
#289: ESC now closes all dialogs deriving from DialogBase
Browse files Browse the repository at this point in the history
Use the wxEVT_CHAR_HOOK event to catch the ESC key and invoke the Close()
method. Affects all dialogs mentioned in the bug (Create Entity, Create Model
and Add Property) as well as any others which are deriving from DialogBase.
  • Loading branch information
Matthew Mott committed Feb 23, 2021
1 parent 7a69f45 commit d4e8fd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
33 changes: 17 additions & 16 deletions libs/wxutil/dialog/DialogBase.cpp
Expand Up @@ -2,7 +2,7 @@

namespace wxutil
{

namespace
{
inline wxWindow* FindTopLevelWindow()
Expand All @@ -11,30 +11,31 @@ namespace wxutil
{
return GlobalMainFrame().getWxTopLevelWindow();
}

return nullptr;
}
}

void DialogBase::_onDelete(wxCloseEvent& ev)
{
if (_onDeleteEvent())
{
ev.Veto();
}
else
{
EndModal(wxID_CANCEL);
}
}

DialogBase::DialogBase(const std::string& title, wxWindow* parent)
: wxDialog(parent ? parent : FindTopLevelWindow(),
wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
wxCAPTION | wxSYSTEM_MENU | wxRESIZE_BORDER)
{
Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(DialogBase::_onDelete),
nullptr, this);
// Allow subclasses to override close event
Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent& e) {
if (_onDeleteEvent())
e.Veto();
else
EndModal(wxID_CANCEL);
});

// Allow ESC to close all dialogs
Bind(wxEVT_CHAR_HOOK, [this](wxKeyEvent& e) {
if (e.GetKeyCode() == WXK_ESCAPE)
Close();
else
e.Skip();
});
}

void DialogBase::FitToScreen(float xProp, float yProp)
Expand Down
2 changes: 0 additions & 2 deletions libs/wxutil/dialog/DialogBase.h
Expand Up @@ -20,8 +20,6 @@ namespace wxutil
class DialogBase :
public wxDialog
{
void _onDelete(wxCloseEvent& ev);

public:
/// Construct and initialise
DialogBase(const std::string& title, wxWindow* parent = NULL);
Expand Down

0 comments on commit d4e8fd7

Please sign in to comment.