Fix accounting for the last position in wxTextCtrl (wxOSX)

If the text in the control doesn't end with a new line,
last valid position is just past the last character in the
the last line.
But if it ends with new line, last valid position
is located at the beginning of the next virtual (empty) line.
So, this case has to be also taken into account in determining
whether position is valid or not.
This commit is contained in:
Artur Wieczorek 2017-08-31 21:34:17 +02:00
parent 4308386eff
commit ed638371a7

View File

@ -933,14 +933,34 @@ long wxNSTextViewControl::XYToPosition(long x, long y) const
i = NSMaxRange(lineRng);
} while ( i < txtLen && nline < y );
// In the last line, the last valid position is after
// the last real character, so we need to extended actual
// range to count this additional virtual character.
// the last real character, so we need to count
// this additional virtual character.
// In any other line, the last valid position is at
// the new line character which is already counted.
if ( i == txtLen )
lineRng.length++;
// Return error if contol contains
{
const bool endsWithNewLine = txtLen > 0 &&
[txt characterAtIndex:txtLen-1] == '\n';
// If text ends with new line, simulated character
// is placed in the additional virtual line.
if ( endsWithNewLine )
{
// Creating additional virtual line makes sense only
// if we look for a position beyond the last real line.
if ( nline < y )
{
nline++;
lineRng = NSMakeRange(i, 1);
}
}
else
{
// Just extended actual range to count
// additional virtual character
lineRng.length++;
}
}
// Return error if control contains
// less lines than given y position.
if ( nline != y )
return -1;