From 321fc5b1a695edc59e0c50a096fb34d11e2e5254 Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 19 Sep 2020 20:01:56 +0200 Subject: [PATCH] #5288: ESC to exit search field in child window instead of closing the dialog --- libs/wxutil/TreeView.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/libs/wxutil/TreeView.cpp b/libs/wxutil/TreeView.cpp index b65fe2b274..f1b605e47a 100644 --- a/libs/wxutil/TreeView.cpp +++ b/libs/wxutil/TreeView.cpp @@ -35,7 +35,6 @@ class TreeView::Search : ~Search(); - void OnIntervalReached(wxTimerEvent& ev); void HandleKeyEvent(wxKeyEvent& ev); @@ -46,6 +45,8 @@ class TreeView::Search : private: void HighlightMatch(const wxDataViewItem& item); + void _onIntervalReached(wxTimerEvent& ev); + void _onTreeViewCharHook(wxKeyEvent& keyEvent); }; TreeView::TreeView(wxWindow* parent, TreeModel::Ptr model, long style) : @@ -367,7 +368,7 @@ class TreeView::SearchPopupWindow : _owner.Close(); } } - + void _onParentMoved(wxMoveEvent&) { Reposition(); @@ -397,7 +398,8 @@ TreeView::Search::Search(TreeView& treeView) : _popup->Show(); _curSearchMatch = wxDataViewItem(); - Bind(wxEVT_TIMER, std::bind(&Search::OnIntervalReached, this, std::placeholders::_1)); + _treeView.Bind(wxEVT_CHAR_HOOK, &Search::_onTreeViewCharHook, this); + Bind(wxEVT_TIMER, &Search::_onIntervalReached, this); _closeTimer.Start(MSECS_TO_AUTO_CLOSE_POPUP); } @@ -406,6 +408,9 @@ TreeView::Search::~Search() { _closeTimer.Stop(); + _treeView.Unbind(wxEVT_CHAR_HOOK, &Search::_onTreeViewCharHook, this); + Unbind(wxEVT_TIMER, &Search::_onIntervalReached, this); + // Always hide popup windows before destroying them, otherwise the // wx-internal wxCurrentPopupWindow pointer doesn't get cleared (in MSW at least) _popup->Hide(); @@ -414,7 +419,20 @@ TreeView::Search::~Search() _curSearchMatch = wxDataViewItem(); } -void TreeView::Search::OnIntervalReached(wxTimerEvent& ev) +void TreeView::Search::_onTreeViewCharHook(wxKeyEvent& ev) +{ + if (ev.GetKeyCode() == WXK_ESCAPE) + { + // The parent dialog listens to ESC keys to close the window + // While this search window is alive, eat the event and close the popup instead + _treeView.CloseSearch(); + return; + } + + ev.Skip(); +} + +void TreeView::Search::_onIntervalReached(wxTimerEvent& ev) { // Disconnect the timing event _closeTimer.Stop();