From cc93948936896e4852d1cf5e34acf51d5ef55817 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 23 Sep 2014 17:42:23 +0000 Subject: [PATCH] refine the wxEVT_TEXT_MAXLEN event handle implementation git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77823 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/univ/textctrl.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/univ/textctrl.cpp b/src/univ/textctrl.cpp index a911340b4d..979749d92c 100644 --- a/src/univ/textctrl.cpp +++ b/src/univ/textctrl.cpp @@ -4557,21 +4557,20 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig, { if ( IsEditable() && !strArg.empty() ) { - wxString acceptString = strArg; + bool isTextClipped = m_maxLength && (m_value.length() + strArg.length() > m_maxLength); + const wxString& acceptedString = isTextClipped ? strArg.Left(m_maxLength - m_value.length()) + : strArg; - unsigned long acceptableLength = m_maxLength - m_value.Length(); - - if ( m_maxLength > 0 && (strArg.Length() > acceptableLength) ) + if ( isTextClipped ) // text was clipped so emit wxEVT_TEXT_MAXLEN { wxCommandEvent event(wxEVT_TEXT_MAXLEN, m_windowId); InitCommandEvent(event); GetEventHandler()->ProcessEvent(event); - acceptString = strArg.Left(acceptableLength); } - if ( acceptString.Length() > 0 ) - // inserting text can be undone - command = new wxTextCtrlInsertCommand(acceptString); + if ( !acceptedString.empty() ) + // inserting text can be undone + command = new wxTextCtrlInsertCommand(acceptedString); } }