Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Unified Diff: chrome/browser/renderer_host/render_widget_host_view_mac.mm

Issue 6289009: [Mac] Implement the system dictionary popup by implementing NSTextInput methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Work on IME Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 943c8cc64a5b8566afd8b83569fcaec1a0b74222..a46f423a30443d32e903b14025f42c189a53e8a2 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"
@@ -40,6 +41,7 @@
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/mac/WebInputEventFactory.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
+#include "ui/gfx/point.h"
#include "webkit/glue/webaccessibility.h"
#include "webkit/plugins/npapi/webplugin.h"
#import "third_party/mozilla/ComplexTextInputPanel.h"
@@ -58,10 +60,14 @@ static inline int ToWebKitModifiers(NSUInteger flags) {
return modifiers;
}
-@interface RenderWidgetHostViewCocoa (Private)
+// Private methods:
+@interface RenderWidgetHostViewCocoa ()
+@property(nonatomic, assign) NSRange selectedRange;
+@property(nonatomic, assign) NSRange markedRange;
+
+ (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)checkForPluginImeCancellation;
@end
@@ -820,19 +826,16 @@ 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() {
[cocoa_view_ cancelComposition];
}
+void RenderWidgetHostViewMac::ImeCompositionRangeChanged(int start, int end) {
+ [cocoa_view_ setMarkedRange:NSMakeRange(start, end - start)];
James Su 2011/02/17 02:03:00 Do we need to care about invalid range like the co
Robert Sesek 2011/02/17 20:05:04 No; added a comment as to why.
+}
+
void RenderWidgetHostViewMac::DidUpdateBackingStore(
const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy,
const std::vector<gfx::Rect>& copy_rects) {
@@ -950,8 +953,14 @@ 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);
+ if (range_start == 0 && range_end == 0)
+ range.location = NSNotFound;
+ [cocoa_view_ setSelectedRange:range];
}
BackingStore* RenderWidgetHostViewMac::AllocBackingStore(
@@ -1370,7 +1379,8 @@ void RenderWidgetHostViewMac::SetTextInputActive(bool active) {
@implementation RenderWidgetHostViewCocoa
-@synthesize caretRect = caretRect_;
+@synthesize selectedRange = selectedRange_;
+@synthesize markedRange = markedRange_;
- (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r {
self = [super initWithFrame:NSZeroRect];
@@ -2440,31 +2450,36 @@ 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;
+
+ NSUInteger point =
+ TextInputClientMac::GetInstance()->GetCharacterIndexAtPoint(
+ renderWidgetHostView_->render_widget_host_,
+ gfx::Point(thePoint.x, thePoint.y));
+ NSLog(@"%s:%@ -> %d", _cmd, NSStringFromPoint(thePoint), point);
+ return point;
}
- (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;
-}
+ NSRect rect = TextInputClientMac::GetInstance()->GetFirstRectForRange(
+ renderWidgetHostView_->render_widget_host_, theRange);
-- (NSRange)selectedRange {
- // Return the selected range saved in the setMarkedText method.
- return hasMarkedText_ ? selectedRange_ : NSMakeRange(NSNotFound, 0);
+ // 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];
+ NSLog(@"%s:%@ -> %@", _cmd, NSStringFromRange(theRange), NSStringFromRect(rect));
+ return rect;
}
- (NSRange)markedRange {
@@ -2474,15 +2489,16 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
// calls the setMarkedText method and we can update the composition node
// there. (When this method returns an empty range, the input method doesn't
// call the setMarkedText method.)
+ NSLog(@"%s -> %@", _cmd, NSStringFromRange(markedRange_));
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 {
+ NSAttributedString* str =
+ TextInputClientMac::GetInstance()->GetAttributedSubstringFromRange(
+ renderWidgetHostView_->render_widget_host_, range);
+ NSLog(@"%s:%@ -> %@", _cmd, NSStringFromRange(range), [str string]);
+ return str;
}
- (NSInteger)conversationIdentifier {
@@ -2541,7 +2557,7 @@ extern NSString *NSTextInputReplacementRangeAttributeName;
NSString* im_text = isAttributedString ? [string string] : string;
int length = [im_text length];
- markedRange_ = NSMakeRange(0, length);
+ // |markedRange_| will get set on a callback from ImeSetComposition().
selectedRange_ = newSelRange;
markedText_ = base::SysNSStringToUTF16(im_text);
hasMarkedText_ = (length > 0);

Powered by Google App Engine
This is Rietveld 408576698