Chromium Code Reviews| Index: chrome/browser/renderer_host/render_widget_host_view_mac.mm |
| diff --git a/chrome/browser/renderer_host/render_widget_host_view_mac.mm b/chrome/browser/renderer_host/render_widget_host_view_mac.mm |
| index f93e8b930e15bee74c520b819de64ed39e7d4639..611b10077260d6884d673b9383051d467fe259be 100644 |
| --- a/chrome/browser/renderer_host/render_widget_host_view_mac.mm |
| +++ b/chrome/browser/renderer_host/render_widget_host_view_mac.mm |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -27,6 +27,7 @@ |
| #include "chrome/browser/renderer_host/render_process_host.h" |
| #include "chrome/browser/renderer_host/render_view_host.h" |
| #include "chrome/browser/renderer_host/render_widget_host.h" |
| +#import "chrome/browser/renderer_host/text_input_client_mac.h" |
| #include "chrome/browser/spellchecker_platform_engine.h" |
| #import "chrome/browser/ui/cocoa/rwhvm_editcommand_helper.h" |
| #import "chrome/browser/ui/cocoa/view_id_util.h" |
| @@ -36,6 +37,7 @@ |
| #include "chrome/common/gpu_messages.h" |
| #include "chrome/common/plugin_messages.h" |
| #include "chrome/common/render_messages.h" |
| +#include "gfx/point.h" |
| #include "skia/ext/platform_canvas.h" |
| #include "third_party/skia/include/core/SkColor.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/mac/WebInputEventFactory.h" |
| @@ -61,8 +63,9 @@ static inline int ToWebKitModifiers(NSUInteger flags) { |
| @interface RenderWidgetHostViewCocoa (Private) |
| + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event; |
| - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r; |
| -- (void)keyEvent:(NSEvent *)theEvent wasKeyEquivalent:(BOOL)equiv; |
| +- (void)keyEvent:(NSEvent*)theEvent wasKeyEquivalent:(BOOL)equiv; |
| - (void)cancelChildPopups; |
| +- (void)setSelectedRange:(NSRange)range; |
| @end |
| // This API was published since 10.6. Provide the declaration so it can be |
| @@ -728,13 +731,6 @@ void RenderWidgetHostViewMac::ImeUpdateTextInputState( |
| if (HasFocus()) |
| SetTextInputActive(true); |
| } |
| - |
| - // We need to convert the coordinate of the cursor rectangle sent from the |
| - // renderer and save it. Our input method backend uses a coordinate system |
| - // whose origin is the upper-left corner of this view. On the other hand, |
| - // Cocoa uses a coordinate system whose origin is the lower-left corner of |
| - // this view. So, we convert the cursor rectangle and save it. |
| - [cocoa_view_ setCaretRect:[cocoa_view_ flipRectToNSRect:caret_rect]]; |
| } |
| void RenderWidgetHostViewMac::ImeCancelComposition() { |
| @@ -858,8 +854,12 @@ void RenderWidgetHostViewMac::SetTooltipText(const std::wstring& tooltip_text) { |
| // RenderWidgetHostViewCocoa uses the stored selection text, |
| // which implements NSServicesRequests protocol. |
| // |
| -void RenderWidgetHostViewMac::SelectionChanged(const std::string& text) { |
| +void RenderWidgetHostViewMac::SelectionChanged(const std::string& text, |
| + int range_start, |
| + int range_end) { |
| selected_text_ = text; |
| + NSRange range = NSMakeRange(range_start, range_end - range_start); |
| + [cocoa_view_ setSelectedRange:range]; |
| } |
| BackingStore* RenderWidgetHostViewMac::AllocBackingStore( |
| @@ -1319,8 +1319,6 @@ void RenderWidgetHostViewMac::SetTextInputActive(bool active) { |
| @implementation RenderWidgetHostViewCocoa |
| -@synthesize caretRect = caretRect_; |
| - |
| - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r { |
| self = [super initWithFrame:NSZeroRect]; |
| if (self) { |
| @@ -2346,31 +2344,53 @@ extern NSString *NSTextInputReplacementRangeAttributeName; |
| } |
| - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint { |
| - NOTIMPLEMENTED(); |
| - return NSNotFound; |
| + DCHECK([self window]); |
| + // |thePoint| is in screen coordinates, but needs to be converted to WebKit |
| + // coordinates (upper left origin). Scroll offsets will be taken care of in |
| + // the renderer. |
| + thePoint = [[self window] convertScreenToBase:thePoint]; |
| + thePoint = [self convertPoint:thePoint fromView:nil]; |
| + thePoint.y = NSHeight([self frame]) - thePoint.y; |
| + |
| + TextInputClientMac* service = TextInputClientMac::GetInstance(); |
| + service->BeforeRequest(); |
| + renderWidgetHostView_->render_widget_host_->Send( |
| + new ViewMsg_CharacterIndexForPoint( |
| + renderWidgetHostView_->render_widget_host_->routing_id(), |
| + gfx::Point(thePoint.x, thePoint.y))); |
| + NSUInteger index = service->WaitForCharacterIndex(); |
| + service->AfterRequest(); |
| + return index; |
| } |
| - (NSRect)firstRectForCharacterRange:(NSRange)theRange { |
| - // An input method requests a cursor rectangle to display its candidate |
| - // window. |
| - // Calculate the screen coordinate of the cursor rectangle saved in |
| - // RenderWidgetHostViewMac::ImeUpdateTextInputState() and send it to the |
| - // input method. |
| - // Since this window may be moved since we receive the cursor rectangle last |
| - // time we sent the cursor rectangle to the input method, so we should map |
| - // from the view coordinate to the screen coordinate every time when an input |
| - // method need it. |
| - NSRect resultRect = [self convertRect:caretRect_ toView:nil]; |
| - NSWindow* window = [self window]; |
| - if (window) |
| - resultRect.origin = [window convertBaseToScreen:resultRect.origin]; |
| - |
| - return resultRect; |
| + TextInputClientMac* service = TextInputClientMac::GetInstance(); |
| + service->BeforeRequest(); |
| + renderWidgetHostView_->render_widget_host_->Send( |
| + new ViewMsg_FirstRectForCharacterRange( |
| + renderWidgetHostView_->render_widget_host_->routing_id(), |
| + theRange.location, |
| + theRange.length)); |
| + NSRect rect = service->WaitForFirstRect(); |
| + service->AfterRequest(); |
| + // The returned rectangle is in WebKit coordinates (upper left origin), so |
| + // flip the coordinate system and then convert it into screen coordinates for |
| + // return. |
| + NSRect viewFrame = [self frame]; |
| + rect.origin.y = NSHeight(viewFrame) - rect.origin.y; |
| + rect.origin.y -= rect.size.height; |
| + rect = [self convertRectToBase:rect]; |
| + rect.origin = [[self window] convertBaseToScreen:rect.origin]; |
| + return rect; |
|
James Su
2011/01/20 23:46:06
We probably can do an optimization here:
Usually w
|
| +} |
| + |
| +// Called from RWHVM::OnSelectionChanged. |
| +- (void)setSelectedRange:(NSRange)range { |
| + selectedRange_ = range; |
|
James Su
2011/01/20 23:46:06
How about to use @synthesize for this property?
Robert Sesek
2011/01/21 23:40:07
Done.
|
| } |
| - (NSRange)selectedRange { |
| - // Return the selected range saved in the setMarkedText method. |
| - return hasMarkedText_ ? selectedRange_ : NSMakeRange(NSNotFound, 0); |
| + return selectedRange_; |
| } |
| - (NSRange)markedRange { |
| @@ -2383,12 +2403,17 @@ extern NSString *NSTextInputReplacementRangeAttributeName; |
| return hasMarkedText_ ? markedRange_ : NSMakeRange(NSNotFound, 0); |
| } |
| -- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range { |
| - // TODO(hbono): Even though many input method works without implementing |
| - // this method, we need to save a copy of the string in the setMarkedText |
| - // method and create a NSAttributedString with the given range. |
| - // http://crbug.com/37715 |
| - return nil; |
| +- (NSAttributedString*)attributedSubstringFromRange:(NSRange)range { |
| + TextInputClientMac* service = TextInputClientMac::GetInstance(); |
| + service->BeforeRequest(); |
| + renderWidgetHostView_->render_widget_host_->Send( |
| + new ViewMsg_StringForRange( |
| + renderWidgetHostView_->render_widget_host_->routing_id(), |
| + range.location, |
| + range.length)); |
| + NSAttributedString* string = service->WaitForSubstring(); |
| + service->AfterRequest(); |
| + return string; |
| } |
| - (NSInteger)conversationIdentifier { |