From 4edae7238a324398ff0ba2ca9b441360715458d0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 19 Nov 2015 01:15:07 +0100 Subject: [PATCH] Fix skipping the event in wxEVT_TEXT_ENTER handler in wxMSW Skipping the event is supposed to have the same effect as not handling the event at all, but in wxMSW wxTE_PROCESS_ENTER style must be specified for a wxEVT_TEXT_ENTER handler to be executed at all and if this style is used, then the default handling in MSWProcessMessage() which normally happens before calling the handler doesn't take place at all. Work around this by explicitly performing the default "Enter" key action if the event generated by it wasn't handled to make wxMSW behaviour more intuitive. --- samples/dialogs/dialogs.cpp | 9 ++++++++- src/msw/textctrl.cpp | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp index 8e5e133c0f..23e7e5c4d9 100644 --- a/samples/dialogs/dialogs.cpp +++ b/samples/dialogs/dialogs.cpp @@ -2603,7 +2603,14 @@ void TestDefaultActionDialog::OnCatchListBoxDClick(wxCommandEvent& WXUNUSED(even void TestDefaultActionDialog::OnTextEnter(wxCommandEvent& event) { - wxLogMessage("Text \"%s\" entered.", event.GetString()); + const wxString& text = event.GetString(); + if ( text.empty() ) + { + event.Skip(); + return; + } + + wxLogMessage("Text \"%s\" entered.", text); } void MyFrame::OnTestDefaultActionDialog(wxCommandEvent& WXUNUSED(event)) diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 64bda4e190..9b4124e9da 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -2066,6 +2066,27 @@ wxTextCtrl::MSWHandleMessage(WXLRESULT *rc, { bool processed = wxTextCtrlBase::MSWHandleMessage(rc, nMsg, wParam, lParam); + // Handle the special case of "Enter" key: the user code needs to specify + // wxTE_PROCESS_ENTER style to get it in the first place, but if this flag + // is used, then even if the wxEVT_TEXT_ENTER handler skips the event, the + // normal action of this key is not performed because IsDialogMessage() is + // not called and, also, an annoying beep is generated by EDIT default + // WndProc. + // + // Fix these problems by explicitly performing the default function of this + // key (which would be done by MSWProcessMessage() if we didn't have + // wxTE_PROCESS_ENTER) and preventing the default WndProc from getting it. + if ( nMsg == WM_CHAR && + !processed && + HasFlag(wxTE_PROCESS_ENTER) && + wParam == VK_RETURN && + !wxIsAnyModifierDown() ) + { + MSWClickButtonIfPossible(MSWGetDefaultButtonFor(this)); + + processed = true; + } + switch ( nMsg ) { case WM_GETDLGCODE: