From fbc50d5434e4512d3ef539c2676cf50b13c23d19 Mon Sep 17 00:00:00 2001 From: 4O4 Date: Fri, 23 Dec 2016 18:00:47 +0100 Subject: [PATCH 1/2] Fix pasting text when its length is matching gui element's MaxLength If Editbox or MultiLineEditbox have MaxTextLength set to e.g. 100 characters then it is impossible to paste any text containing exactly 100 characters into it. The best moment to observe this is when user fills the control with text until it's full, then cuts all of it and tries to paste it again. Lua test script is available at: https://github.com/4O4/mtasa-lua-tests/tree/master/%5Btests%5D/test-gui-limited-textbox-paste --- Client/gui/CGUI_Impl.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Client/gui/CGUI_Impl.cpp b/Client/gui/CGUI_Impl.cpp index 4c249768ef..1bea5b6810 100644 --- a/Client/gui/CGUI_Impl.cpp +++ b/Client/gui/CGUI_Impl.cpp @@ -864,12 +864,18 @@ bool CGUI_Impl::Event_KeyDown ( const CEGUI::EventArgs& Args ) CloseClipboard(); return true; } - strEditText = WndEdit->getText (); + + strEditText = WndEdit->getText(); iSelectionStart = WndEdit->getSelectionStartIndex (); iSelectionLength = WndEdit->getSelectionLength(); iMaxLength = WndEdit->getMaxTextLength(); iCaratIndex = WndEdit->getCaratIndex(); bReplaceNewLines = false; + + // Plus one character, because there is always an extra '\n' in + // MultiLineEditbox's text data and it causes MaxLength limit to + // be exceeded during pasting the text + iMaxLength += 1; } std::wstring strClipboardText = ClipboardBuffer; @@ -901,7 +907,7 @@ bool CGUI_Impl::Event_KeyDown ( const CEGUI::EventArgs& Args ) // Put the editbox's data into a string and insert the data if it has not reached it's maximum text length std::wstring tmp = MbUTF8ToUTF16(strEditText.c_str()); - if ( ( strClipboardText.length () + tmp.length () ) < iMaxLength ) + if ( ( strClipboardText.length () + tmp.length () ) <= iMaxLength ) { // Are there characters selected? size_t sizeCaratIndex = 0; From cda09b4c6b6c9815e5f8d3520f763e7aace7194b Mon Sep 17 00:00:00 2001 From: 4O4 Date: Fri, 23 Dec 2016 18:24:44 +0100 Subject: [PATCH 2/2] Exclude selection when checking MaxLength limit during text paste The selected text is replaced after pasting so it should always be ignored. --- Client/gui/CGUI_Impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/gui/CGUI_Impl.cpp b/Client/gui/CGUI_Impl.cpp index 1bea5b6810..6b2ad38656 100644 --- a/Client/gui/CGUI_Impl.cpp +++ b/Client/gui/CGUI_Impl.cpp @@ -907,7 +907,7 @@ bool CGUI_Impl::Event_KeyDown ( const CEGUI::EventArgs& Args ) // Put the editbox's data into a string and insert the data if it has not reached it's maximum text length std::wstring tmp = MbUTF8ToUTF16(strEditText.c_str()); - if ( ( strClipboardText.length () + tmp.length () ) <= iMaxLength ) + if ( ( strClipboardText.length () + tmp.length () - iSelectionLength ) <= iMaxLength ) { // Are there characters selected? size_t sizeCaratIndex = 0;