From fcd734387a10deb1615c2b18695dfa2f253b83b1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 18 Sep 2019 01:56:54 +0200 Subject: [PATCH] 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 --- src/gtk/textctrl.cpp | 1 + tests/controls/textctrltest.cpp | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 764a21c57d..eaad5c2809 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -817,6 +817,7 @@ bool wxTextCtrl::Create( wxWindow *parent, if (!value.empty()) { SetValue( value ); + InvalidateBestSize(); } if (style & wxTE_PASSWORD) diff --git a/tests/controls/textctrltest.cpp b/tests/controls/textctrltest.cpp index 2d25ebd674..b25f280d5e 100644 --- a/tests/controls/textctrltest.cpp +++ b/tests/controls/textctrltest.cpp @@ -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 + 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