From 2d464ca576851d104ce5953221d58a5e4c541b7b Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 31 Aug 2017 16:28:14 +0200 Subject: [PATCH] Account for the last position in wxTextCtrl (wxOSX) Take into account that last valid position is after the last character of the text. This applies also to empty controls for which XYToPosition(0, 0) should return 0. --- src/osx/cocoa/textctrl.mm | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index 28316d14f6..9b8f4d11c1 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -915,14 +915,24 @@ long wxNSTextViewControl::XYToPosition(long x, long y) const NSString* txt = [m_textView string]; const long txtLen = [txt length]; - long nline = 0; + long nline = -1; NSRange lineRng; - for ( long i = 0; i < txtLen && nline <= y; nline++ ) + long i = 0; + // We need to enter the counting loop at least once, + // even for empty text string. + do { + nline++; lineRng = [txt lineRangeForRange:NSMakeRange(i, 0)]; i = NSMaxRange(lineRng); - } - nline--; + } 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. + // 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 // less lines than given y position. @@ -1276,7 +1286,8 @@ long wxNSTextFieldControl::XYToPosition(long x, long y) const { wxCHECK_MSG( x >= 0 && y >= 0, -1, wxS("Invalid line/column number") ); - if ( y != 0 || x >= [[m_textField stringValue] length] ) + // Last valid position is after the last character. + if ( y != 0 || x > [[m_textField stringValue] length] ) return -1; return x;