Don't send EVT_TEXT_ENTER to controls without wxTE_PROCESS_ENTER

wxMSW always sent this event to multiline text controls, even when they
didn't have wxTE_PROCESS_ENTER style, contrary to what was documented.

Avoid sending this event unless wxTE_PROCESS_ENTER is used and add unit
tests checking that multiline text controls don't get it without this
style (but still do get it with it).
This commit is contained in:
Vadim Zeitlin 2019-09-08 18:50:02 +02:00
parent 4c075c2128
commit 84f29ce472
5 changed files with 84 additions and 5 deletions

View File

@ -73,6 +73,11 @@ Changes in behaviour not resulting in compilation errors
- wxDC::DrawCheckMark() draws the same shape under all platforms now, use the - wxDC::DrawCheckMark() draws the same shape under all platforms now, use the
new wxRendererNative::DrawCheckMark() to draw MSW-specific themed check mark. new wxRendererNative::DrawCheckMark() to draw MSW-specific themed check mark.
- wxTE_PROCESS_ENTER must be used to receive wxEVT_TEXT_ENTER events from even
multiline wxTextCtrl, conforming to the documentation, but contrary to the
previous behaviour in wxMSW, when these events were always generated in this
case. Please add wxTE_PROCESS_ENTER style if you relied on the old behaviour.
Changes in behaviour which may result in build errors Changes in behaviour which may result in build errors
----------------------------------------------------- -----------------------------------------------------

View File

@ -2075,14 +2075,18 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
switch ( event.GetKeyCode() ) switch ( event.GetKeyCode() )
{ {
case WXK_RETURN: case WXK_RETURN:
// Single line controls only get this key code if they have
// wxTE_PROCESS_ENTER style, but multiline ones always get it
// because they need it for themselves. However we shouldn't
// generate wxEVT_TEXT_ENTER for the controls without this style,
// so test for it explicitly.
if ( HasFlag(wxTE_PROCESS_ENTER) )
{ {
wxCommandEvent evt(wxEVT_TEXT_ENTER, m_windowId); wxCommandEvent evt(wxEVT_TEXT_ENTER, m_windowId);
InitCommandEvent(evt); InitCommandEvent(evt);
evt.SetString(GetValue()); evt.SetString(GetValue());
if ( HandleWindowEvent(evt) ) if ( HandleWindowEvent(evt) )
if ( !HasFlag(wxTE_MULTILINE) ) return;
return;
//else: multiline controls need Enter for themselves
} }
break; break;

View File

@ -1274,7 +1274,8 @@ TEST_CASE("wxTextCtrl::ProcessEnter", "[wxTextCtrl][enter]")
class TextCtrlCreator : public TextLikeControlCreator class TextCtrlCreator : public TextLikeControlCreator
{ {
public: public:
explicit TextCtrlCreator() explicit TextCtrlCreator(int styleToAdd = 0)
: m_styleToAdd(styleToAdd)
{ {
} }
@ -1282,8 +1283,16 @@ TEST_CASE("wxTextCtrl::ProcessEnter", "[wxTextCtrl][enter]")
{ {
return new wxTextCtrl(parent, wxID_ANY, wxString(), return new wxTextCtrl(parent, wxID_ANY, wxString(),
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,
style); style | m_styleToAdd);
} }
virtual TextLikeControlCreator* CloneAsMultiLine() const wxOVERRIDE
{
return new TextCtrlCreator(wxTE_MULTILINE);
}
private:
int m_styleToAdd;
}; };
TestProcessEnter(TextCtrlCreator()); TestProcessEnter(TextCtrlCreator());

View File

@ -21,6 +21,8 @@
#include "textentrytest.h" #include "textentrytest.h"
#include "testableframe.h" #include "testableframe.h"
#include "wx/scopedptr.h"
#include "wx/uiaction.h" #include "wx/uiaction.h"
void TextEntryTestCase::SetValue() void TextEntryTestCase::SetValue()
@ -434,6 +436,23 @@ private:
} }
} }
void OnText(wxCommandEvent& WXUNUSED(e))
{
// This should only happen for the multiline text controls.
switch ( m_processEnter )
{
case ProcessEnter_No:
case ProcessEnter_ButSkip:
// We consider that the text succeeded.
EndModal(wxID_OK);
break;
case ProcessEnter_WithoutSkipping:
FAIL("Shouldn't be getting wxEVT_TEXT if handled");
break;
}
}
void OnTimeOut(wxTimerEvent&) void OnTimeOut(wxTimerEvent&)
{ {
EndModal(wxID_CANCEL); EndModal(wxID_CANCEL);
@ -459,6 +478,7 @@ private:
// style would fail with an assertion failure, due to wx helpfully complaining // style would fail with an assertion failure, due to wx helpfully complaining
// about it. // about it.
wxBEGIN_EVENT_TABLE(TestDialog, wxDialog) wxBEGIN_EVENT_TABLE(TestDialog, wxDialog)
EVT_TEXT(wxID_ANY, TestDialog::OnText)
EVT_TEXT_ENTER(wxID_ANY, TestDialog::OnTextEnter) EVT_TEXT_ENTER(wxID_ANY, TestDialog::OnTextEnter)
wxEND_EVENT_TABLE() wxEND_EVENT_TABLE()
@ -492,6 +512,42 @@ void TestProcessEnter(const TextLikeControlCreator& controlCreator)
REQUIRE( dlgProcessEnter.ShowModal() == wxID_APPLY ); REQUIRE( dlgProcessEnter.ShowModal() == wxID_APPLY );
CHECK( dlgProcessEnter.GotEnter() ); CHECK( dlgProcessEnter.GotEnter() );
} }
SECTION("Without wxTE_PROCESS_ENTER but with wxTE_MULTILINE")
{
wxScopedPtr<TextLikeControlCreator>
multiLineCreator(controlCreator.CloneAsMultiLine());
if ( !multiLineCreator )
return;
TestDialog dlg(*multiLineCreator, ProcessEnter_No);
REQUIRE( dlg.ShowModal() == wxID_OK );
CHECK( !dlg.GotEnter() );
}
SECTION("With wxTE_PROCESS_ENTER and wxTE_MULTILINE but skipping")
{
wxScopedPtr<TextLikeControlCreator>
multiLineCreator(controlCreator.CloneAsMultiLine());
if ( !multiLineCreator )
return;
TestDialog dlg(*multiLineCreator, ProcessEnter_ButSkip);
REQUIRE( dlg.ShowModal() == wxID_OK );
CHECK( dlg.GotEnter() );
}
SECTION("With wxTE_PROCESS_ENTER and wxTE_MULTILINE without skipping")
{
wxScopedPtr<TextLikeControlCreator>
multiLineCreator(controlCreator.CloneAsMultiLine());
if ( !multiLineCreator )
return;
TestDialog dlg(*multiLineCreator, ProcessEnter_WithoutSkipping);
REQUIRE( dlg.ShowModal() == wxID_APPLY );
CHECK( dlg.GotEnter() );
}
} }
#else // !wxUSE_UIACTIONSIMULATOR #else // !wxUSE_UIACTIONSIMULATOR

View File

@ -85,6 +85,11 @@ public:
// Create the control of the right type using the given parent and style. // Create the control of the right type using the given parent and style.
virtual wxControl* Create(wxWindow* parent, int style) const = 0; virtual wxControl* Create(wxWindow* parent, int style) const = 0;
// Return another creator similar to this one, but creating multiline
// version of the control. If the returned pointer is non-null, it must be
// deleted by the caller.
virtual TextLikeControlCreator* CloneAsMultiLine() const { return NULL; }
// Give it a virtual dtor to avoid warnings even though this class is not // Give it a virtual dtor to avoid warnings even though this class is not
// supposed to be used polymorphically. // supposed to be used polymorphically.
virtual ~TextLikeControlCreator() {} virtual ~TextLikeControlCreator() {}