Fix pasting long strings in wxTextCtrl under MSW

Adjust the length limit before pasting to ensure that all text on
clipboard will be successfully pasted, instead of only pasting the part
of it which fits.

Add a unit test checking that this works.

Closes #4646.
This commit is contained in:
Vadim Zeitlin
2019-10-09 01:01:21 +02:00
parent 7a9e969dca
commit efc2a9da2d
3 changed files with 149 additions and 0 deletions

View File

@@ -27,6 +27,11 @@
#include "wx/scopeguard.h"
#include "wx/uiaction.h"
#if wxUSE_CLIPBOARD
#include "wx/clipbrd.h"
#include "wx/dataobj.h"
#endif // wxUSE_CLIPBOARD
#ifdef __WXGTK__
#include "wx/stopwatch.h"
#endif
@@ -1341,4 +1346,74 @@ TEST_CASE("wxTextCtrl::GetBestSize", "[wxTextCtrl][best-size]")
CHECK( sizeVeryLong.y == sizeLong.y );
}
#if wxUSE_CLIPBOARD
TEST_CASE("wxTextCtrl::LongPaste", "[wxTextCtrl][clipboard][paste]")
{
long style = 0;
SECTION("Plain")
{
style = wxTE_MULTILINE;
}
// wxTE_RICH[2] style only makes any different under MSW, so don't bother
// testing it under the other platforms.
#ifdef __WXMSW__
SECTION("Rich")
{
style = wxTE_MULTILINE | wxTE_RICH;
}
SECTION("Rich v2")
{
style = wxTE_MULTILINE | wxTE_RICH2;
}
#endif // __WXMSW__
if ( !style )
{
// This can happen when explicitly selecting just a single section to
// execute -- this code still runs even if the corresponding section is
// skipped, so we have to explicitly skip it too in this case.
return;
}
wxScopedPtr<wxTextCtrl>
text(new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, wxString(),
wxDefaultPosition, wxDefaultSize, style));
// This could actually be much higher, but it makes the test proportionally
// slower, so use a relatively small (but still requiring more space than
// the default maximum length under MSW) number here.
const int NUM_LINES = 10000;
{
wxClipboardLocker lock;
// Build a longish string.
wxString s;
s.reserve(NUM_LINES*5 + 10);
for ( int n = 0; n < NUM_LINES; ++n )
{
s += wxString::Format("%04d\n", n);
}
s += "THE END";
wxTheClipboard->AddData(new wxTextDataObject(s));
}
text->ChangeValue("THE BEGINNING\n");
text->SetInsertionPointEnd();
text->Paste();
const int numLines = text->GetNumberOfLines();
CHECK( numLines == NUM_LINES + 2 );
CHECK( text->GetLineText(numLines - 1) == "THE END" );
}
#endif // wxUSE_CLIPBOARD
#endif //wxUSE_TEXTCTRL