Fix best size computation for multiline wxTextCtrl in wxGTK

The initial value was not taken into account before because the best
size computed before it was set, i.e. for the empty control, was always
used, as it was never invalidated.

Do invalidate it now if the control is created with non-empty value, in
order to adjust its best, and initial, size appropriately to its
contents.

Closes #18507.

Closes https://github.com/wxWidgets/wxWidgets/pull/1560
This commit is contained in:
Vadim Zeitlin 2019-09-18 01:56:54 +02:00
parent bb1c3c4ebb
commit fcd734387a
2 changed files with 44 additions and 0 deletions

View File

@ -817,6 +817,7 @@ bool wxTextCtrl::Create( wxWindow *parent,
if (!value.empty())
{
SetValue( value );
InvalidateBestSize();
}
if (style & wxTE_PASSWORD)

View File

@ -23,6 +23,7 @@
#include "wx/textctrl.h"
#endif // WX_PRECOMP
#include "wx/scopedptr.h"
#include "wx/scopeguard.h"
#include "wx/uiaction.h"
@ -1298,4 +1299,46 @@ TEST_CASE("wxTextCtrl::ProcessEnter", "[wxTextCtrl][enter]")
TestProcessEnter(TextCtrlCreator());
}
TEST_CASE("wxTextCtrl::GetBestSize", "[wxTextCtrl][best-size]")
{
struct GetBestSizeFor
{
wxSize operator()(const wxString& text) const
{
wxScopedPtr<wxTextCtrl>
t(new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY, text,
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE));
return t->GetBestSize();
}
} getBestSizeFor;
wxString s;
const wxSize sizeEmpty = getBestSizeFor(s);
// Empty control should have some reasonable vertical size.
CHECK( sizeEmpty.y > 0 );
s += "1\n2\n3\n4\n5\n";
const wxSize sizeMedium = getBestSizeFor(s);
// Control with a few lines of text in it should be taller.
CHECK( sizeMedium.y > sizeEmpty.y );
s += "6\n7\n8\n9\n10\n";
const wxSize sizeLong = getBestSizeFor(s);
// And a control with many lines in it should be even more so.
CHECK( sizeLong.y > sizeMedium.y );
s += s;
s += s;
s += s;
const wxSize sizeVeryLong = getBestSizeFor(s);
// However there is a cutoff at 10 lines currently, so anything longer than
// that should still have the same best size.
CHECK( sizeVeryLong.y == sizeLong.y );
}
#endif //wxUSE_TEXTCTRL