Index: ui/views/cocoa/bridged_content_view.mm |
diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm |
index 5e39c1164db24f9e85da6bfac9d7816d10868488..7fa5e1bcd7bbbac82a89d689bcff93463f42c3ab 100644 |
--- a/ui/views/cocoa/bridged_content_view.mm |
+++ b/ui/views/cocoa/bridged_content_view.mm |
@@ -5,7 +5,11 @@ |
#import "ui/views/cocoa/bridged_content_view.h" |
#include "base/logging.h" |
+#include "base/strings/sys_string_conversions.h" |
+#include "grit/ui_strings.h" |
+#include "ui/base/ime/text_input_client.h" |
#include "ui/gfx/canvas_paint_mac.h" |
+#include "ui/gfx/geometry/rect.h" |
#include "ui/views/view.h" |
#include "ui/views/widget/widget.h" |
@@ -18,11 +22,16 @@ |
// the event to NativeWidgetMac for handling. |
- (void)handleMouseEvent:(NSEvent*)theEvent; |
+// Execute a command on the currently focused TextInputClient. |
+// |commandId| should be a resource ID from ui_strings.grd. |
+- (void)doCommandByID:(int)commandId; |
+ |
@end |
@implementation BridgedContentView |
@synthesize hostedView = hostedView_; |
+@synthesize textInputClient = textInputClient_; |
- (id)initWithView:(views::View*)viewToHost { |
DCHECK(viewToHost); |
@@ -59,6 +68,11 @@ |
hostedView_->GetWidget()->OnMouseEvent(&event); |
} |
+- (void)doCommandByID:(int)commandId { |
+ if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId)) |
+ textInputClient_->ExecuteEditingCommand(commandId); |
+} |
+ |
// NSView implementation. |
- (BOOL)acceptsFirstResponder { |
@@ -82,7 +96,10 @@ |
} |
- (void)keyDown:(NSEvent*)theEvent { |
- [self handleUntranslatedKeyEvent:theEvent]; |
+ if (textInputClient_) |
+ [self interpretKeyEvents:@[ theEvent ]]; |
+ else |
+ [super keyDown:theEvent]; |
} |
- (void)keyUp:(NSEvent*)theEvent { |
tapted
2014/06/25 10:38:08
note you'll need to rebase against master (this ch
|
@@ -138,4 +155,111 @@ |
[self handleMouseEvent:theEvent]; |
} |
+- (void)deleteBackward:(id)sender { |
+ [self doCommandByID:IDS_DELETE_BACKWARD]; |
+} |
+ |
+- (void)deleteForward:(id)sender { |
+ [self doCommandByID:IDS_DELETE_FORWARD]; |
+} |
+ |
+- (void)moveLeft:(id)sender { |
+ [self doCommandByID:IDS_MOVE_LEFT]; |
+} |
+ |
+- (void)moveRight:(id)sender { |
+ [self doCommandByID:IDS_MOVE_RIGHT]; |
+} |
+ |
+// NSTextInputClient protocol implementation. |
+ |
+- (NSAttributedString*) |
+ attributedSubstringForProposedRange:(NSRange)range |
+ actualRange:(NSRangePointer)actualRange { |
+ base::string16 substring; |
+ if (textInputClient_) { |
+ gfx::Range textRange; |
+ textInputClient_->GetTextRange(&textRange); |
+ gfx::Range subrange = textRange.Intersect(gfx::Range(range)); |
+ textInputClient_->GetTextFromRange(subrange, &substring); |
+ if (actualRange) |
+ *actualRange = subrange.ToNSRange(); |
+ } |
+ return [[[NSAttributedString alloc] |
+ initWithString:base::SysUTF16ToNSString(substring)] autorelease]; |
+} |
+ |
+- (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { |
+ NOTIMPLEMENTED(); |
+ return 0; |
+} |
+ |
+- (void)doCommandBySelector:(SEL)selector { |
+ if ([self respondsToSelector:selector]) |
+ [self performSelector:selector withObject:nil]; |
+ else |
+ [[self nextResponder] doCommandBySelector:selector]; |
+} |
+ |
+- (NSRect)firstRectForCharacterRange:(NSRange)range |
+ actualRange:(NSRangePointer)actualRange { |
+ NOTIMPLEMENTED(); |
+ return NSZeroRect; |
+} |
+ |
+- (BOOL)hasMarkedText { |
+ return textInputClient_ && textInputClient_->HasCompositionText(); |
+} |
+ |
+- (void)insertText:(id)text replacementRange:(NSRange)replacementRange { |
+ if (!textInputClient_) |
+ return; |
+ |
+ if ([text isKindOfClass:[NSAttributedString class]]) |
+ text = [text string]; |
+ textInputClient_->DeleteRange(gfx::Range(replacementRange)); |
+ textInputClient_->InsertText(base::SysNSStringToUTF16(text)); |
+} |
+ |
+- (NSRange)markedRange { |
+ if (!textInputClient_) |
+ return NSMakeRange(NSNotFound, 0); |
+ |
+ gfx::Range range; |
+ textInputClient_->GetCompositionTextRange(&range); |
+ return range.ToNSRange(); |
+} |
+ |
+- (NSRange)selectedRange { |
+ if (!textInputClient_) |
+ return NSMakeRange(NSNotFound, 0); |
+ |
+ gfx::Range range; |
+ textInputClient_->GetSelectionRange(&range); |
+ return range.ToNSRange(); |
+} |
+ |
+- (void)setMarkedText:(id)text |
+ selectedRange:(NSRange)selectedRange |
+ replacementRange:(NSRange)replacementRange { |
+ if (!textInputClient_) |
+ return; |
+ |
+ if ([text isKindOfClass:[NSAttributedString class]]) |
+ text = [text string]; |
+ ui::CompositionText composition; |
+ composition.text = base::SysNSStringToUTF16(text); |
+ composition.selection = gfx::Range(selectedRange); |
+ textInputClient_->SetCompositionText(composition); |
+} |
+ |
+- (void)unmarkText { |
+ if (textInputClient_) |
+ textInputClient_->ConfirmCompositionText(); |
+} |
+ |
+- (NSArray*)validAttributesForMarkedText { |
+ return @[]; |
+} |
+ |
@end |