From d585bb1ebd30b61b2eba1816cca8412393dbc98f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 27 Oct 2021 21:39:37 +0100 Subject: [PATCH] Automatically turn wxTE_RICH2 for wxMSW wxTextCtrl if necessary When creating a wxTextCtrl with the initial text which is too long to fit into a plain EDIT, automatically create RICHEDIT instead. This is not perfect, e.g. it still doesn't make calling SetValue() with long text later work, but it seems to still be preferable to failing to create the window completely, which results in many other more difficult to diagnose problems later. --- src/msw/textctrl.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 4e06cff953..dfd2b08f10 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -583,7 +583,22 @@ bool wxTextCtrl::MSWCreateText(const wxString& value, m_updatesCount = -2; if ( !MSWCreateControl(windowClass.t_str(), msStyle, pos, size, valueWin) ) + { + // There is one case in which window creation may realistically fail + // and this is when we create a plain EDIT control with too long text, + // so try to detect this and transparently switch to using RICHEDIT in + // this case (note that the exact length cut off is unknown and might + // be system-dependent, but even though plain EDIT works for texts + // longer than 64KiB, we don't lose much by trying to use RICHEDIT if + // creating it failed). + if ( !HasFlag(wxTE_RICH | wxTE_RICH2) && value.length() >= 0x10000 ) + { + m_windowStyle |= wxTE_RICH2; + return MSWCreateText(value, pos, size); + } + return false; + } m_updatesCount = -1;