From a69ab2907c24ac60af8512ca357124a7caa027a2 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sat, 29 Jul 2017 21:19:15 +0200 Subject: [PATCH] Fix wxTextCtrl::XYToPosition (wxMSW) When calculating character position for given (x,y) there is necessary to verify if passed coordinates are sane and return error status (-1) if not. y-coordinate has to be in the range [0..numLines-1] and x-coordinate cannot exceed the length of the text in the given line. --- docs/changes.txt | 1 + src/msw/textctrl.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index 8321344178..bc63e7a405 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -195,6 +195,7 @@ wxMSW: - Fix placing 0RGB wxBitmaps on the clipboard. - Fix handling wxClipboard data when wxUSE_OLE == 0. - Fix caching of wxFONTSTYLE_SLANT fonts in wxTheFontList. +- Fix wxTextCtrl::XYToPosition(). wxOSX: diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index ac6c64be55..a5efb01faf 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -1469,7 +1469,31 @@ int wxTextCtrl::GetNumberOfLines() const long wxTextCtrl::XYToPosition(long x, long y) const { // This gets the char index for the _beginning_ of this line - long charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, y, 0); + long charIndex; + if ( IsMultiLine() ) + { + charIndex = ::SendMessage(GetHwnd(), EM_LINEINDEX, y, 0); + if ( charIndex == -1 ) + return -1; + } + else + { + if ( y != 0 ) + return -1; + + charIndex = 0; + } + + // Line is identified by a character position! + long lineLength = ::SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0); + // For all lines but last one we need to adjust the length + // to include new line character (only one because both CR and LF + // are virtually "displayed" at the same position). + if ( y < GetNumberOfLines() - 1 ) + lineLength += 1; + + if ( x >= lineLength ) + return -1; return charIndex + x; }