From 1e68ad9c6fdc6ce61f82b90c56fac6aaf6740dd6 Mon Sep 17 00:00:00 2001 From: Stephen Pohl Date: Tue, 29 Jul 2014 10:32:40 -0400 Subject: [PATCH] Bug 308471: Implement NSTextInput -characterIndexForPoint. Credits to mstange for virtually all the work in this patch. r=masayuki --- widget/cocoa/TextInputHandler.mm | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/widget/cocoa/TextInputHandler.mm b/widget/cocoa/TextInputHandler.mm index 8322fd846994..41b486c92be0 100644 --- a/widget/cocoa/TextInputHandler.mm +++ b/widget/cocoa/TextInputHandler.mm @@ -3199,15 +3199,34 @@ IMEInputHandler::FirstRectForCharacterRange(NSRange& aRange, NSUInteger IMEInputHandler::CharacterIndexForPoint(NSPoint& aPoint) { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; + PR_LOG(gLog, PR_LOG_ALWAYS, ("%p IMEInputHandler::CharacterIndexForPoint, aPoint={ x=%f, y=%f }", this, aPoint.x, aPoint.y)); - //nsRefPtr kungFuDeathGrip(this); + NSWindow* mainWindow = [NSApp mainWindow]; + if (!mWidget || !mainWindow) { + return NSNotFound; + } - // To implement this, we'd have to grovel in text frames looking at text - // offsets. - return 0; + WidgetQueryContentEvent charAt(true, NS_QUERY_CHARACTER_AT_POINT, mWidget); + NSPoint ptInWindow = [mainWindow convertScreenToBase:aPoint]; + NSPoint ptInView = [mView convertPoint:ptInWindow fromView:nil]; + charAt.refPoint.x = + static_cast(ptInView.x) * mWidget->BackingScaleFactor(); + charAt.refPoint.y = + static_cast(ptInView.y) * mWidget->BackingScaleFactor(); + mWidget->DispatchWindowEvent(charAt); + if (!charAt.mSucceeded || + charAt.mReply.mOffset == WidgetQueryContentEvent::NOT_FOUND || + charAt.mReply.mOffset >= static_cast(NSNotFound)) { + return NSNotFound; + } + + return charAt.mReply.mOffset; + + NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NSNotFound); } NSArray*