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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <QuartzCore/QuartzCore.h> 5 #include <QuartzCore/QuartzCore.h>
6 6
7 #include "chrome/browser/renderer_host/render_widget_host_view_mac.h" 7 #include "chrome/browser/renderer_host/render_widget_host_view_mac.h"
8 8
9 #include "app/app_switches.h" 9 #include "app/app_switches.h"
10 #include "app/surface/io_surface_support_mac.h" 10 #include "app/surface/io_surface_support_mac.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/scoped_cftyperef.h" 13 #include "base/mac/scoped_cftyperef.h"
14 #import "base/mac/scoped_nsautorelease_pool.h" 14 #import "base/mac/scoped_nsautorelease_pool.h"
15 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
16 #import "base/scoped_nsobject.h" 16 #import "base/scoped_nsobject.h"
17 #include "base/string_util.h" 17 #include "base/string_util.h"
18 #include "base/sys_info.h" 18 #include "base/sys_info.h"
19 #include "base/sys_string_conversions.h" 19 #include "base/sys_string_conversions.h"
20 #import "chrome/browser/accessibility/browser_accessibility_cocoa.h" 20 #import "chrome/browser/accessibility/browser_accessibility_cocoa.h"
21 #include "chrome/browser/accessibility/browser_accessibility_state.h" 21 #include "chrome/browser/accessibility/browser_accessibility_state.h"
22 #include "chrome/browser/browser_thread.h" 22 #include "chrome/browser/browser_thread.h"
23 #include "chrome/browser/browser_trial.h" 23 #include "chrome/browser/browser_trial.h"
24 #include "chrome/browser/gpu_process_host.h" 24 #include "chrome/browser/gpu_process_host.h"
25 #include "chrome/browser/plugin_process_host.h" 25 #include "chrome/browser/plugin_process_host.h"
26 #include "chrome/browser/renderer_host/backing_store_mac.h" 26 #include "chrome/browser/renderer_host/backing_store_mac.h"
27 #include "chrome/browser/renderer_host/render_process_host.h" 27 #include "chrome/browser/renderer_host/render_process_host.h"
28 #include "chrome/browser/renderer_host/render_view_host.h" 28 #include "chrome/browser/renderer_host/render_view_host.h"
29 #include "chrome/browser/renderer_host/render_widget_host.h" 29 #include "chrome/browser/renderer_host/render_widget_host.h"
30 #import "chrome/browser/renderer_host/text_input_client_mac.h"
30 #include "chrome/browser/spellchecker_platform_engine.h" 31 #include "chrome/browser/spellchecker_platform_engine.h"
31 #import "chrome/browser/ui/cocoa/rwhvm_editcommand_helper.h" 32 #import "chrome/browser/ui/cocoa/rwhvm_editcommand_helper.h"
32 #import "chrome/browser/ui/cocoa/view_id_util.h" 33 #import "chrome/browser/ui/cocoa/view_id_util.h"
33 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/native_web_keyboard_event.h" 35 #include "chrome/common/native_web_keyboard_event.h"
35 #include "chrome/common/edit_command.h" 36 #include "chrome/common/edit_command.h"
36 #include "chrome/common/gpu_messages.h" 37 #include "chrome/common/gpu_messages.h"
37 #include "chrome/common/plugin_messages.h" 38 #include "chrome/common/plugin_messages.h"
38 #include "chrome/common/render_messages.h" 39 #include "chrome/common/render_messages.h"
39 #include "skia/ext/platform_canvas.h" 40 #include "skia/ext/platform_canvas.h"
40 #include "third_party/skia/include/core/SkColor.h" 41 #include "third_party/skia/include/core/SkColor.h"
41 #include "third_party/WebKit/Source/WebKit/chromium/public/mac/WebInputEventFact ory.h" 42 #include "third_party/WebKit/Source/WebKit/chromium/public/mac/WebInputEventFact ory.h"
42 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 43 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
44 #include "ui/gfx/point.h"
43 #include "webkit/glue/webaccessibility.h" 45 #include "webkit/glue/webaccessibility.h"
44 #include "webkit/plugins/npapi/webplugin.h" 46 #include "webkit/plugins/npapi/webplugin.h"
45 #import "third_party/mozilla/ComplexTextInputPanel.h" 47 #import "third_party/mozilla/ComplexTextInputPanel.h"
46 48
47 using WebKit::WebInputEvent; 49 using WebKit::WebInputEvent;
48 using WebKit::WebInputEventFactory; 50 using WebKit::WebInputEventFactory;
49 using WebKit::WebMouseEvent; 51 using WebKit::WebMouseEvent;
50 using WebKit::WebMouseWheelEvent; 52 using WebKit::WebMouseWheelEvent;
51 53
52 static inline int ToWebKitModifiers(NSUInteger flags) { 54 static inline int ToWebKitModifiers(NSUInteger flags) {
53 int modifiers = 0; 55 int modifiers = 0;
54 if (flags & NSControlKeyMask) modifiers |= WebInputEvent::ControlKey; 56 if (flags & NSControlKeyMask) modifiers |= WebInputEvent::ControlKey;
55 if (flags & NSShiftKeyMask) modifiers |= WebInputEvent::ShiftKey; 57 if (flags & NSShiftKeyMask) modifiers |= WebInputEvent::ShiftKey;
56 if (flags & NSAlternateKeyMask) modifiers |= WebInputEvent::AltKey; 58 if (flags & NSAlternateKeyMask) modifiers |= WebInputEvent::AltKey;
57 if (flags & NSCommandKeyMask) modifiers |= WebInputEvent::MetaKey; 59 if (flags & NSCommandKeyMask) modifiers |= WebInputEvent::MetaKey;
58 return modifiers; 60 return modifiers;
59 } 61 }
60 62
61 @interface RenderWidgetHostViewCocoa (Private) 63 // Private methods:
64 @interface RenderWidgetHostViewCocoa ()
65 @property(nonatomic, assign) NSRange selectedRange;
66 @property(nonatomic, assign) NSRange markedRange;
67
62 + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event; 68 + (BOOL)shouldAutohideCursorForEvent:(NSEvent*)event;
63 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r; 69 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r;
64 - (void)keyEvent:(NSEvent *)theEvent wasKeyEquivalent:(BOOL)equiv; 70 - (void)keyEvent:(NSEvent*)theEvent wasKeyEquivalent:(BOOL)equiv;
65 - (void)cancelChildPopups; 71 - (void)cancelChildPopups;
66 - (void)checkForPluginImeCancellation; 72 - (void)checkForPluginImeCancellation;
67 @end 73 @end
68 74
69 // This API was published since 10.6. Provide the declaration so it can be 75 // This API was published since 10.6. Provide the declaration so it can be
70 // // called below when building with the 10.5 SDK. 76 // // called below when building with the 10.5 SDK.
71 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 77 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
72 @class NSTextInputContext; 78 @class NSTextInputContext;
73 @interface NSResponder (AppKitDetails) 79 @interface NSResponder (AppKitDetails)
74 - (NSTextInputContext *)inputContext; 80 - (NSTextInputContext *)inputContext;
(...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after
813 } 819 }
814 820
815 void RenderWidgetHostViewMac::ImeUpdateTextInputState( 821 void RenderWidgetHostViewMac::ImeUpdateTextInputState(
816 WebKit::WebTextInputType type, 822 WebKit::WebTextInputType type,
817 const gfx::Rect& caret_rect) { 823 const gfx::Rect& caret_rect) {
818 if (text_input_type_ != type) { 824 if (text_input_type_ != type) {
819 text_input_type_ = type; 825 text_input_type_ = type;
820 if (HasFocus()) 826 if (HasFocus())
821 SetTextInputActive(true); 827 SetTextInputActive(true);
822 } 828 }
823
824 // We need to convert the coordinate of the cursor rectangle sent from the
825 // renderer and save it. Our input method backend uses a coordinate system
826 // whose origin is the upper-left corner of this view. On the other hand,
827 // Cocoa uses a coordinate system whose origin is the lower-left corner of
828 // this view. So, we convert the cursor rectangle and save it.
829 [cocoa_view_ setCaretRect:[cocoa_view_ flipRectToNSRect:caret_rect]];
830 } 829 }
831 830
832 void RenderWidgetHostViewMac::ImeCancelComposition() { 831 void RenderWidgetHostViewMac::ImeCancelComposition() {
833 [cocoa_view_ cancelComposition]; 832 [cocoa_view_ cancelComposition];
834 } 833 }
835 834
835 void RenderWidgetHostViewMac::ImeCompositionRangeChanged(int start, int end) {
836 [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.
837 }
838
836 void RenderWidgetHostViewMac::DidUpdateBackingStore( 839 void RenderWidgetHostViewMac::DidUpdateBackingStore(
837 const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy, 840 const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy,
838 const std::vector<gfx::Rect>& copy_rects) { 841 const std::vector<gfx::Rect>& copy_rects) {
839 if (!is_hidden_) { 842 if (!is_hidden_) {
840 std::vector<gfx::Rect> rects(copy_rects); 843 std::vector<gfx::Rect> rects(copy_rects);
841 844
842 // Because the findbar might be open, we cannot use scrollRect:by: here. For 845 // Because the findbar might be open, we cannot use scrollRect:by: here. For
843 // now, simply mark all of scroll rect as dirty. 846 // now, simply mark all of scroll rect as dirty.
844 if (!scroll_rect.IsEmpty()) 847 if (!scroll_rect.IsEmpty())
845 rects.push_back(scroll_rect); 848 rects.push_back(scroll_rect);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 946
944 NSString* tooltip_nsstring = base::SysWideToNSString(display_text); 947 NSString* tooltip_nsstring = base::SysWideToNSString(display_text);
945 [cocoa_view_ setToolTipAtMousePoint:tooltip_nsstring]; 948 [cocoa_view_ setToolTipAtMousePoint:tooltip_nsstring];
946 } 949 }
947 } 950 }
948 951
949 // 952 //
950 // RenderWidgetHostViewCocoa uses the stored selection text, 953 // RenderWidgetHostViewCocoa uses the stored selection text,
951 // which implements NSServicesRequests protocol. 954 // which implements NSServicesRequests protocol.
952 // 955 //
953 void RenderWidgetHostViewMac::SelectionChanged(const std::string& text) { 956 void RenderWidgetHostViewMac::SelectionChanged(const std::string& text,
957 int range_start,
958 int range_end) {
954 selected_text_ = text; 959 selected_text_ = text;
960 NSRange range = NSMakeRange(range_start, range_end - range_start);
961 if (range_start == 0 && range_end == 0)
962 range.location = NSNotFound;
963 [cocoa_view_ setSelectedRange:range];
955 } 964 }
956 965
957 BackingStore* RenderWidgetHostViewMac::AllocBackingStore( 966 BackingStore* RenderWidgetHostViewMac::AllocBackingStore(
958 const gfx::Size& size) { 967 const gfx::Size& size) {
959 return new BackingStoreMac(render_widget_host_, size); 968 return new BackingStoreMac(render_widget_host_, size);
960 } 969 }
961 970
962 // Sets whether or not to accept first responder status. 971 // Sets whether or not to accept first responder status.
963 void RenderWidgetHostViewMac::SetTakesFocusOnlyOnMouseDown(bool flag) { 972 void RenderWidgetHostViewMac::SetTakesFocusOnlyOnMouseDown(bool flag) {
964 [cocoa_view_ setTakesFocusOnlyOnMouseDown:flag]; 973 [cocoa_view_ setTakesFocusOnlyOnMouseDown:flag];
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
1363 } else { 1372 } else {
1364 if (text_input_type_ == WebKit::WebTextInputTypePassword) 1373 if (text_input_type_ == WebKit::WebTextInputTypePassword)
1365 DisablePasswordInput(); 1374 DisablePasswordInput();
1366 } 1375 }
1367 } 1376 }
1368 1377
1369 // RenderWidgetHostViewCocoa --------------------------------------------------- 1378 // RenderWidgetHostViewCocoa ---------------------------------------------------
1370 1379
1371 @implementation RenderWidgetHostViewCocoa 1380 @implementation RenderWidgetHostViewCocoa
1372 1381
1373 @synthesize caretRect = caretRect_; 1382 @synthesize selectedRange = selectedRange_;
1383 @synthesize markedRange = markedRange_;
1374 1384
1375 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r { 1385 - (id)initWithRenderWidgetHostViewMac:(RenderWidgetHostViewMac*)r {
1376 self = [super initWithFrame:NSZeroRect]; 1386 self = [super initWithFrame:NSZeroRect];
1377 if (self) { 1387 if (self) {
1378 editCommand_helper_.reset(new RWHVMEditCommandHelper); 1388 editCommand_helper_.reset(new RWHVMEditCommandHelper);
1379 editCommand_helper_->AddEditingSelectorsToClass([self class]); 1389 editCommand_helper_->AddEditingSelectorsToClass([self class]);
1380 1390
1381 renderWidgetHostView_.reset(r); 1391 renderWidgetHostView_.reset(r);
1382 canBeKeyView_ = YES; 1392 canBeKeyView_ = YES;
1383 takesFocusOnlyOnMouseDown_ = NO; 1393 takesFocusOnlyOnMouseDown_ = NO;
(...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after
2433 NSUnderlineStyleAttributeName, 2443 NSUnderlineStyleAttributeName,
2434 NSUnderlineColorAttributeName, 2444 NSUnderlineColorAttributeName,
2435 NSMarkedClauseSegmentAttributeName, 2445 NSMarkedClauseSegmentAttributeName,
2436 NSTextInputReplacementRangeAttributeName, 2446 NSTextInputReplacementRangeAttributeName,
2437 nil]); 2447 nil]);
2438 } 2448 }
2439 return validAttributesForMarkedText_.get(); 2449 return validAttributesForMarkedText_.get();
2440 } 2450 }
2441 2451
2442 - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint { 2452 - (NSUInteger)characterIndexForPoint:(NSPoint)thePoint {
2443 NOTIMPLEMENTED(); 2453 DCHECK([self window]);
2444 return NSNotFound; 2454 // |thePoint| is in screen coordinates, but needs to be converted to WebKit
2455 // coordinates (upper left origin). Scroll offsets will be taken care of in
2456 // the renderer.
2457 thePoint = [[self window] convertScreenToBase:thePoint];
2458 thePoint = [self convertPoint:thePoint fromView:nil];
2459 thePoint.y = NSHeight([self frame]) - thePoint.y;
2460
2461 NSUInteger point =
2462 TextInputClientMac::GetInstance()->GetCharacterIndexAtPoint(
2463 renderWidgetHostView_->render_widget_host_,
2464 gfx::Point(thePoint.x, thePoint.y));
2465 NSLog(@"%s:%@ -> %d", _cmd, NSStringFromPoint(thePoint), point);
2466 return point;
2445 } 2467 }
2446 2468
2447 - (NSRect)firstRectForCharacterRange:(NSRange)theRange { 2469 - (NSRect)firstRectForCharacterRange:(NSRange)theRange {
2448 // An input method requests a cursor rectangle to display its candidate 2470 NSRect rect = TextInputClientMac::GetInstance()->GetFirstRectForRange(
2449 // window. 2471 renderWidgetHostView_->render_widget_host_, theRange);
2450 // Calculate the screen coordinate of the cursor rectangle saved in
2451 // RenderWidgetHostViewMac::ImeUpdateTextInputState() and send it to the
2452 // input method.
2453 // Since this window may be moved since we receive the cursor rectangle last
2454 // time we sent the cursor rectangle to the input method, so we should map
2455 // from the view coordinate to the screen coordinate every time when an input
2456 // method need it.
2457 NSRect resultRect = [self convertRect:caretRect_ toView:nil];
2458 NSWindow* window = [self window];
2459 if (window)
2460 resultRect.origin = [window convertBaseToScreen:resultRect.origin];
2461 2472
2462 return resultRect; 2473 // The returned rectangle is in WebKit coordinates (upper left origin), so
2463 } 2474 // flip the coordinate system and then convert it into screen coordinates for
2464 2475 // return.
2465 - (NSRange)selectedRange { 2476 NSRect viewFrame = [self frame];
2466 // Return the selected range saved in the setMarkedText method. 2477 rect.origin.y = NSHeight(viewFrame) - rect.origin.y;
2467 return hasMarkedText_ ? selectedRange_ : NSMakeRange(NSNotFound, 0); 2478 rect.origin.y -= rect.size.height;
2479 rect = [self convertRectToBase:rect];
2480 rect.origin = [[self window] convertBaseToScreen:rect.origin];
2481 NSLog(@"%s:%@ -> %@", _cmd, NSStringFromRange(theRange), NSStringFromRect(rect ));
2482 return rect;
2468 } 2483 }
2469 2484
2470 - (NSRange)markedRange { 2485 - (NSRange)markedRange {
2471 // An input method calls this method to check if an application really has 2486 // An input method calls this method to check if an application really has
2472 // a text being composed when hasMarkedText call returns true. 2487 // a text being composed when hasMarkedText call returns true.
2473 // Returns the range saved in the setMarkedText method so the input method 2488 // Returns the range saved in the setMarkedText method so the input method
2474 // calls the setMarkedText method and we can update the composition node 2489 // calls the setMarkedText method and we can update the composition node
2475 // there. (When this method returns an empty range, the input method doesn't 2490 // there. (When this method returns an empty range, the input method doesn't
2476 // call the setMarkedText method.) 2491 // call the setMarkedText method.)
2492 NSLog(@"%s -> %@", _cmd, NSStringFromRange(markedRange_));
2477 return hasMarkedText_ ? markedRange_ : NSMakeRange(NSNotFound, 0); 2493 return hasMarkedText_ ? markedRange_ : NSMakeRange(NSNotFound, 0);
2478 } 2494 }
2479 2495
2480 - (NSAttributedString *)attributedSubstringFromRange:(NSRange)range { 2496 - (NSAttributedString*)attributedSubstringFromRange:(NSRange)range {
2481 // TODO(hbono): Even though many input method works without implementing 2497 NSAttributedString* str =
2482 // this method, we need to save a copy of the string in the setMarkedText 2498 TextInputClientMac::GetInstance()->GetAttributedSubstringFromRange(
2483 // method and create a NSAttributedString with the given range. 2499 renderWidgetHostView_->render_widget_host_, range);
2484 // http://crbug.com/37715 2500 NSLog(@"%s:%@ -> %@", _cmd, NSStringFromRange(range), [str string]);
2485 return nil; 2501 return str;
2486 } 2502 }
2487 2503
2488 - (NSInteger)conversationIdentifier { 2504 - (NSInteger)conversationIdentifier {
2489 return reinterpret_cast<NSInteger>(self); 2505 return reinterpret_cast<NSInteger>(self);
2490 } 2506 }
2491 2507
2492 // Each RenderWidgetHostViewCocoa has its own input context, but we return 2508 // Each RenderWidgetHostViewCocoa has its own input context, but we return
2493 // nil when the caret is in non-editable content or password box to avoid 2509 // nil when the caret is in non-editable content or password box to avoid
2494 // making input methods do their work. 2510 // making input methods do their work.
2495 - (NSTextInputContext *)inputContext { 2511 - (NSTextInputContext *)inputContext {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2534 } 2550 }
2535 2551
2536 - (void)setMarkedText:(id)string selectedRange:(NSRange)newSelRange { 2552 - (void)setMarkedText:(id)string selectedRange:(NSRange)newSelRange {
2537 // An input method updates the composition string. 2553 // An input method updates the composition string.
2538 // We send the given text and range to the renderer so it can update the 2554 // We send the given text and range to the renderer so it can update the
2539 // composition node of WebKit. 2555 // composition node of WebKit.
2540 BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; 2556 BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]];
2541 NSString* im_text = isAttributedString ? [string string] : string; 2557 NSString* im_text = isAttributedString ? [string string] : string;
2542 int length = [im_text length]; 2558 int length = [im_text length];
2543 2559
2544 markedRange_ = NSMakeRange(0, length); 2560 // |markedRange_| will get set on a callback from ImeSetComposition().
2545 selectedRange_ = newSelRange; 2561 selectedRange_ = newSelRange;
2546 markedText_ = base::SysNSStringToUTF16(im_text); 2562 markedText_ = base::SysNSStringToUTF16(im_text);
2547 hasMarkedText_ = (length > 0); 2563 hasMarkedText_ = (length > 0);
2548 2564
2549 underlines_.clear(); 2565 underlines_.clear();
2550 if (isAttributedString) { 2566 if (isAttributedString) {
2551 ExtractUnderlines(string, &underlines_); 2567 ExtractUnderlines(string, &underlines_);
2552 } else { 2568 } else {
2553 // Use a thin black underline by default. 2569 // Use a thin black underline by default.
2554 underlines_.push_back( 2570 underlines_.push_back(
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
2826 if (!string) return NO; 2842 if (!string) return NO;
2827 2843
2828 // If the user is currently using an IME, confirm the IME input, 2844 // If the user is currently using an IME, confirm the IME input,
2829 // and then insert the text from the service, the same as TextEdit and Safari. 2845 // and then insert the text from the service, the same as TextEdit and Safari.
2830 [self confirmComposition]; 2846 [self confirmComposition];
2831 [self insertText:string]; 2847 [self insertText:string];
2832 return YES; 2848 return YES;
2833 } 2849 }
2834 2850
2835 @end 2851 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698