OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "grit/ui_strings.h" |
| 10 #include "ui/base/ime/text_input_client.h" |
8 #include "ui/gfx/canvas_paint_mac.h" | 11 #include "ui/gfx/canvas_paint_mac.h" |
| 12 #include "ui/gfx/geometry/rect.h" |
9 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
10 | 14 |
| 15 @interface BridgedContentView () |
| 16 |
| 17 // Execute a command on the currently focused TextInputClient. |
| 18 // |commandId| should be a resource ID from ui_strings.grd. |
| 19 - (void)doCommandByID:(int)commandId; |
| 20 |
| 21 @end |
| 22 |
11 @implementation BridgedContentView | 23 @implementation BridgedContentView |
12 | 24 |
13 @synthesize hostedView = hostedView_; | 25 @synthesize hostedView = hostedView_; |
| 26 @synthesize textInputClient = textInputClient_; |
14 | 27 |
15 - (id)initWithView:(views::View*)viewToHost { | 28 - (id)initWithView:(views::View*)viewToHost { |
16 DCHECK(viewToHost); | 29 DCHECK(viewToHost); |
17 gfx::Rect bounds = viewToHost->bounds(); | 30 gfx::Rect bounds = viewToHost->bounds(); |
18 // To keep things simple, assume the origin is (0, 0) until there exists a use | 31 // To keep things simple, assume the origin is (0, 0) until there exists a use |
19 // case for something other than that. | 32 // case for something other than that. |
20 DCHECK(bounds.origin().IsOrigin()); | 33 DCHECK(bounds.origin().IsOrigin()); |
21 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); | 34 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); |
22 if ((self = [super initWithFrame:initialFrame])) | 35 if ((self = [super initWithFrame:initialFrame])) |
23 hostedView_ = viewToHost; | 36 hostedView_ = viewToHost; |
24 | 37 |
25 return self; | 38 return self; |
26 } | 39 } |
27 | 40 |
28 - (void)clearView { | 41 - (void)clearView { |
29 hostedView_ = NULL; | 42 hostedView_ = NULL; |
30 } | 43 } |
31 | 44 |
| 45 // BridgedContentView private implementation. |
| 46 |
| 47 - (void)doCommandByID:(int)commandId { |
| 48 if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId)) |
| 49 textInputClient_->ExecuteEditingCommand(commandId); |
| 50 } |
| 51 |
32 // NSView implementation. | 52 // NSView implementation. |
33 | 53 |
34 - (void)setFrameSize:(NSSize)newSize { | 54 - (void)setFrameSize:(NSSize)newSize { |
35 [super setFrameSize:newSize]; | 55 [super setFrameSize:newSize]; |
36 if (!hostedView_) | 56 if (!hostedView_) |
37 return; | 57 return; |
38 | 58 |
39 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 59 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
40 } | 60 } |
41 | 61 |
42 - (void)drawRect:(NSRect)dirtyRect { | 62 - (void)drawRect:(NSRect)dirtyRect { |
43 if (!hostedView_) | 63 if (!hostedView_) |
44 return; | 64 return; |
45 | 65 |
46 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); | 66 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
47 hostedView_->Paint(&canvas, views::CullSet()); | 67 hostedView_->Paint(&canvas, views::CullSet()); |
48 } | 68 } |
49 | 69 |
| 70 - (void)keyDown:(NSEvent*)theEvent { |
| 71 if (textInputClient_) |
| 72 [self interpretKeyEvents:@[ theEvent ]]; |
| 73 else |
| 74 [super keyDown:theEvent]; |
| 75 } |
| 76 |
| 77 - (void)deleteBackward:(id)sender { |
| 78 [self doCommandByID:IDS_DELETE_BACKWARD]; |
| 79 } |
| 80 |
| 81 - (void)deleteForward:(id)sender { |
| 82 [self doCommandByID:IDS_DELETE_FORWARD]; |
| 83 } |
| 84 |
| 85 - (void)moveLeft:(id)sender { |
| 86 [self doCommandByID:IDS_MOVE_LEFT]; |
| 87 } |
| 88 |
| 89 - (void)moveRight:(id)sender { |
| 90 [self doCommandByID:IDS_MOVE_RIGHT]; |
| 91 } |
| 92 |
| 93 // NSTextInputClient protocol implementation. |
| 94 |
| 95 - (NSAttributedString*) |
| 96 attributedSubstringForProposedRange:(NSRange)range |
| 97 actualRange:(NSRangePointer)actualRange { |
| 98 base::string16 substring; |
| 99 if (textInputClient_) { |
| 100 gfx::Range textRange; |
| 101 textInputClient_->GetTextRange(&textRange); |
| 102 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); |
| 103 textInputClient_->GetTextFromRange(subrange, &substring); |
| 104 if (actualRange) |
| 105 *actualRange = subrange.ToNSRange(); |
| 106 } |
| 107 return [[[NSAttributedString alloc] |
| 108 initWithString:base::SysUTF16ToNSString(substring)] autorelease]; |
| 109 } |
| 110 |
| 111 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { |
| 112 NOTIMPLEMENTED(); |
| 113 return 0; |
| 114 } |
| 115 |
| 116 - (void)doCommandBySelector:(SEL)selector { |
| 117 if ([self respondsToSelector:selector]) |
| 118 [self performSelector:selector withObject:nil]; |
| 119 else |
| 120 [[self nextResponder] doCommandBySelector:selector]; |
| 121 } |
| 122 |
| 123 - (NSRect)firstRectForCharacterRange:(NSRange)range |
| 124 actualRange:(NSRangePointer)actualRange { |
| 125 NOTIMPLEMENTED(); |
| 126 return NSZeroRect; |
| 127 } |
| 128 |
| 129 - (BOOL)hasMarkedText { |
| 130 return textInputClient_ && textInputClient_->HasCompositionText(); |
| 131 } |
| 132 |
| 133 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { |
| 134 if (!textInputClient_) |
| 135 return; |
| 136 |
| 137 if ([text isKindOfClass:[NSAttributedString class]]) |
| 138 text = [text string]; |
| 139 textInputClient_->DeleteRange(gfx::Range(replacementRange)); |
| 140 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); |
| 141 } |
| 142 |
| 143 - (NSRange)markedRange { |
| 144 if (!textInputClient_) |
| 145 return NSMakeRange(NSNotFound, 0); |
| 146 |
| 147 gfx::Range range; |
| 148 textInputClient_->GetCompositionTextRange(&range); |
| 149 return range.ToNSRange(); |
| 150 } |
| 151 |
| 152 - (NSRange)selectedRange { |
| 153 if (!textInputClient_) |
| 154 return NSMakeRange(NSNotFound, 0); |
| 155 |
| 156 gfx::Range range; |
| 157 textInputClient_->GetSelectionRange(&range); |
| 158 return range.ToNSRange(); |
| 159 } |
| 160 |
| 161 - (void)setMarkedText:(id)text |
| 162 selectedRange:(NSRange)selectedRange |
| 163 replacementRange:(NSRange)replacementRange { |
| 164 if (!textInputClient_) |
| 165 return; |
| 166 |
| 167 if ([text isKindOfClass:[NSAttributedString class]]) |
| 168 text = [text string]; |
| 169 ui::CompositionText composition; |
| 170 composition.text = base::SysNSStringToUTF16(text); |
| 171 composition.selection = gfx::Range(selectedRange); |
| 172 textInputClient_->SetCompositionText(composition); |
| 173 } |
| 174 |
| 175 - (void)unmarkText { |
| 176 if (textInputClient_) |
| 177 textInputClient_->ConfirmCompositionText(); |
| 178 } |
| 179 |
| 180 - (NSArray*)validAttributesForMarkedText { |
| 181 return @[]; |
| 182 } |
| 183 |
50 @end | 184 @end |
OLD | NEW |