Merge branch 'multiline-enter-fix'

Fix handling Enter in multiline text controls in wxMSW.

And other improvements, notably don't send wxEVT_TEXT_ENTER for controls
without wxTE_PROCESS_ENTER style.

Closes #18491.

See https://github.com/wxWidgets/wxWidgets/pull/1539
This commit is contained in:
Vadim Zeitlin
2019-09-12 00:49:28 +02:00
7 changed files with 194 additions and 38 deletions

View File

@@ -233,9 +233,10 @@ void ComboBoxTestCase::IsEmpty()
TEST_CASE("wxComboBox::ProcessEnter", "[wxComboBox][enter]")
{
struct ComboBoxCreator
class ComboBoxCreator : public TextLikeControlCreator
{
static wxControl* Do(wxWindow* parent, int style)
public:
virtual wxControl* Create(wxWindow* parent, int style) const wxOVERRIDE
{
const wxString choices[] = { "foo", "bar", "baz" };
@@ -246,7 +247,7 @@ TEST_CASE("wxComboBox::ProcessEnter", "[wxComboBox][enter]")
}
};
TestProcessEnter(&ComboBoxCreator::Do);
TestProcessEnter(ComboBoxCreator());
}
#endif //wxUSE_COMBOBOX

View File

@@ -1271,17 +1271,31 @@ void TextCtrlTestCase::XYToPositionSingleLine()
TEST_CASE("wxTextCtrl::ProcessEnter", "[wxTextCtrl][enter]")
{
struct TextCtrlCreator
class TextCtrlCreator : public TextLikeControlCreator
{
static wxControl* Do(wxWindow* parent, int style)
public:
explicit TextCtrlCreator(int styleToAdd = 0)
: m_styleToAdd(styleToAdd)
{
}
virtual wxControl* Create(wxWindow* parent, int style) const wxOVERRIDE
{
return new wxTextCtrl(parent, wxID_ANY, wxString(),
wxDefaultPosition, wxDefaultSize,
style);
style | m_styleToAdd);
}
virtual TextLikeControlCreator* CloneAsMultiLine() const wxOVERRIDE
{
return new TextCtrlCreator(wxTE_MULTILINE);
}
private:
int m_styleToAdd;
};
TestProcessEnter(&TextCtrlCreator::Do);
TestProcessEnter(TextCtrlCreator());
}
#endif //wxUSE_TEXTCTRL

View File

@@ -21,6 +21,8 @@
#include "textentrytest.h"
#include "testableframe.h"
#include "wx/scopedptr.h"
#include "wx/uiaction.h"
void TextEntryTestCase::SetValue()
@@ -384,21 +386,20 @@ enum ProcessEnter
class TestDialog : public wxDialog
{
public:
explicit TestDialog(TextLikeControlCreator controlCreator,
explicit TestDialog(const TextLikeControlCreator& controlCreator,
ProcessEnter processEnter)
: wxDialog(wxTheApp->GetTopWindow(), wxID_ANY, "Test dialog"),
m_control((*controlCreator)(this,
processEnter == ProcessEnter_No
? 0
: wxTE_PROCESS_ENTER)),
m_control
(
controlCreator.Create
(
this,
processEnter == ProcessEnter_No ? 0 : wxTE_PROCESS_ENTER
)
),
m_processEnter(processEnter),
m_gotEnter(false)
{
// We can't always bind this handler because wx will helpfully
// complain if we bind it without using wxTE_PROCESS_ENTER.
if ( processEnter != ProcessEnter_No )
m_control->Bind(wxEVT_TEXT_ENTER, &TestDialog::OnTextEnter, this);
wxSizer* const sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(m_control, wxSizerFlags().Expand());
sizer->Add(CreateStdDialogButtonSizer(wxOK));
@@ -435,6 +436,25 @@ 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, but in a different way,
// so use a different ID to be able to distinguish between this
// scenario and Enter activating the default button.
EndModal(wxID_CLOSE);
break;
case ProcessEnter_WithoutSkipping:
FAIL("Shouldn't be getting wxEVT_TEXT if handled");
break;
}
}
void OnTimeOut(wxTimerEvent&)
{
EndModal(wxID_CANCEL);
@@ -451,11 +471,22 @@ private:
const ProcessEnter m_processEnter;
wxTimer m_timer;
bool m_gotEnter;
wxDECLARE_EVENT_TABLE();
};
// Note that we must use event table macros here instead of Bind() because
// binding wxEVT_TEXT_ENTER handler for a control without wxTE_PROCESS_ENTER
// style would fail with an assertion failure, due to wx helpfully complaining
// about it.
wxBEGIN_EVENT_TABLE(TestDialog, wxDialog)
EVT_TEXT(wxID_ANY, TestDialog::OnText)
EVT_TEXT_ENTER(wxID_ANY, TestDialog::OnTextEnter)
wxEND_EVENT_TABLE()
} // anonymous namespace
void TestProcessEnter(TextLikeControlCreator controlCreator)
void TestProcessEnter(const TextLikeControlCreator& controlCreator)
{
if ( !EnableUITests() )
{
@@ -483,11 +514,47 @@ void TestProcessEnter(TextLikeControlCreator controlCreator)
REQUIRE( dlgProcessEnter.ShowModal() == wxID_APPLY );
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_CLOSE );
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_CLOSE );
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
void TestProcessEnter(TextLikeControlCreator WXUNUSED(controlCreator))
void TestProcessEnter(const TextLikeControlCreator& WXUNUSED(controlCreator))
{
WARN("Skipping wxTE_PROCESS_ENTER tests: wxUIActionSimulator not available");
}

View File

@@ -75,11 +75,32 @@ private:
wxDECLARE_NO_COPY_CLASS(TextEntryTestCase);
};
// Function to call to test that wxTE_PROCESS_ENTER is handled correctly for
// the controls of the type created by the given creator function when they're
// placed in a dialog with a default button.
typedef wxControl* (*TextLikeControlCreator)(wxWindow* parent, int style);
// Helper used for creating the control of the specific type (currently either
// wxTextCtrl or wxComboBox) with the given flag.
class TextLikeControlCreator
{
public:
TextLikeControlCreator() {}
void TestProcessEnter(TextLikeControlCreator controlCreator);
// Create the control of the right type using the given parent and style.
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
// supposed to be used polymorphically.
virtual ~TextLikeControlCreator() {}
private:
wxDECLARE_NO_COPY_CLASS(TextLikeControlCreator);
};
// Use the given control creator to check that various combinations of
// specifying and not specifying wxTE_PROCESS_ENTER and handling or not
// handling the resulting event work as expected.
void TestProcessEnter(const TextLikeControlCreator& controlCreator);
#endif // _WX_TESTS_CONTROLS_TEXTENTRYTEST_H_