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

Unified Diff: ui/views/cocoa/bridged_content_view.mm

Issue 329463002: MacViews: Implement text input. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More changes for tapted. Created 6 years, 6 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: 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 de2d12ea7d10a7d0e8335abe0a1326853ada4de6..0ef2faf26d62db2cce6af89ee6f2b2283754e9d3 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"
@@ -17,9 +21,18 @@
@end
+@interface BridgedContentView ()
tapted 2014/06/20 00:40:59 merge with above
Andre 2014/06/20 01:00:39 Oops, bad merge. Done.
+
+// 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);
@@ -48,6 +61,11 @@
hostedView_->GetWidget()->OnMouseEvent(&event);
}
+- (void)doCommandByID:(int)commandId {
+ if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId))
+ textInputClient_->ExecuteEditingCommand(commandId);
+}
+
// NSView implementation.
- (BOOL)acceptsFirstResponder {
@@ -119,4 +137,118 @@
[self handleMouseEvent:theEvent];
}
+- (void)keyDown:(NSEvent*)theEvent {
+ if (textInputClient_)
+ [self interpretKeyEvents:@[ theEvent ]];
tapted 2014/06/20 00:40:59 (just curious) If keyDown: is removed completely,
Andre 2014/06/20 01:00:39 No, I think NSResponder's implementation simply fo
+ else
+ [super keyDown: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

Powered by Google App Engine
This is Rietveld 408576698