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.
This commit is contained in:
Artur Wieczorek 2017-07-29 21:19:15 +02:00
parent c4e1fb4ef9
commit a69ab2907c
2 changed files with 26 additions and 1 deletions

View File

@ -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:

View File

@ -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;
}