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 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
11 | 15 |
12 @interface BridgedContentView () | 16 @interface BridgedContentView () |
13 | 17 |
14 // Translates the location of |theEvent| to toolkit-views coordinates and passes | 18 // Translates the location of |theEvent| to toolkit-views coordinates and passes |
15 // the event to NativeWidgetMac for handling. | 19 // the event to NativeWidgetMac for handling. |
16 - (void)handleMouseEvent:(NSEvent*)theEvent; | 20 - (void)handleMouseEvent:(NSEvent*)theEvent; |
17 | 21 |
18 @end | 22 @end |
19 | 23 |
24 @interface BridgedContentView () | |
tapted
2014/06/20 00:40:59
merge with above
Andre
2014/06/20 01:00:39
Oops, bad merge. Done.
| |
25 | |
26 // Execute a command on the currently focused TextInputClient. | |
27 // |commandId| should be a resource ID from ui_strings.grd. | |
28 - (void)doCommandByID:(int)commandId; | |
29 | |
30 @end | |
31 | |
20 @implementation BridgedContentView | 32 @implementation BridgedContentView |
21 | 33 |
22 @synthesize hostedView = hostedView_; | 34 @synthesize hostedView = hostedView_; |
35 @synthesize textInputClient = textInputClient_; | |
23 | 36 |
24 - (id)initWithView:(views::View*)viewToHost { | 37 - (id)initWithView:(views::View*)viewToHost { |
25 DCHECK(viewToHost); | 38 DCHECK(viewToHost); |
26 gfx::Rect bounds = viewToHost->bounds(); | 39 gfx::Rect bounds = viewToHost->bounds(); |
27 // To keep things simple, assume the origin is (0, 0) until there exists a use | 40 // To keep things simple, assume the origin is (0, 0) until there exists a use |
28 // case for something other than that. | 41 // case for something other than that. |
29 DCHECK(bounds.origin().IsOrigin()); | 42 DCHECK(bounds.origin().IsOrigin()); |
30 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); | 43 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); |
31 if ((self = [super initWithFrame:initialFrame])) | 44 if ((self = [super initWithFrame:initialFrame])) |
32 hostedView_ = viewToHost; | 45 hostedView_ = viewToHost; |
33 | 46 |
34 return self; | 47 return self; |
35 } | 48 } |
36 | 49 |
37 - (void)clearView { | 50 - (void)clearView { |
38 hostedView_ = NULL; | 51 hostedView_ = NULL; |
39 } | 52 } |
40 | 53 |
41 // BridgedContentView private implementation. | 54 // BridgedContentView private implementation. |
42 | 55 |
43 - (void)handleMouseEvent:(NSEvent*)theEvent { | 56 - (void)handleMouseEvent:(NSEvent*)theEvent { |
44 if (!hostedView_) | 57 if (!hostedView_) |
45 return; | 58 return; |
46 | 59 |
47 ui::MouseEvent event(theEvent); | 60 ui::MouseEvent event(theEvent); |
48 hostedView_->GetWidget()->OnMouseEvent(&event); | 61 hostedView_->GetWidget()->OnMouseEvent(&event); |
49 } | 62 } |
50 | 63 |
64 - (void)doCommandByID:(int)commandId { | |
65 if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId)) | |
66 textInputClient_->ExecuteEditingCommand(commandId); | |
67 } | |
68 | |
51 // NSView implementation. | 69 // NSView implementation. |
52 | 70 |
53 - (BOOL)acceptsFirstResponder { | 71 - (BOOL)acceptsFirstResponder { |
54 return YES; | 72 return YES; |
55 } | 73 } |
56 | 74 |
57 - (void)setFrameSize:(NSSize)newSize { | 75 - (void)setFrameSize:(NSSize)newSize { |
58 [super setFrameSize:newSize]; | 76 [super setFrameSize:newSize]; |
59 if (!hostedView_) | 77 if (!hostedView_) |
60 return; | 78 return; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 // and exited events for the view heirarchy. | 130 // and exited events for the view heirarchy. |
113 // TODO(tapted): Install/uninstall tracking areas when required so that the | 131 // TODO(tapted): Install/uninstall tracking areas when required so that the |
114 // NSView will receive these events outside of tests. | 132 // NSView will receive these events outside of tests. |
115 [self handleMouseEvent:theEvent]; | 133 [self handleMouseEvent:theEvent]; |
116 } | 134 } |
117 | 135 |
118 - (void)scrollWheel:(NSEvent*)theEvent { | 136 - (void)scrollWheel:(NSEvent*)theEvent { |
119 [self handleMouseEvent:theEvent]; | 137 [self handleMouseEvent:theEvent]; |
120 } | 138 } |
121 | 139 |
140 - (void)keyDown:(NSEvent*)theEvent { | |
141 if (textInputClient_) | |
142 [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
| |
143 else | |
144 [super keyDown:theEvent]; | |
145 } | |
146 | |
147 - (void)deleteBackward:(id)sender { | |
148 [self doCommandByID:IDS_DELETE_BACKWARD]; | |
149 } | |
150 | |
151 - (void)deleteForward:(id)sender { | |
152 [self doCommandByID:IDS_DELETE_FORWARD]; | |
153 } | |
154 | |
155 - (void)moveLeft:(id)sender { | |
156 [self doCommandByID:IDS_MOVE_LEFT]; | |
157 } | |
158 | |
159 - (void)moveRight:(id)sender { | |
160 [self doCommandByID:IDS_MOVE_RIGHT]; | |
161 } | |
162 | |
163 // NSTextInputClient protocol implementation. | |
164 | |
165 - (NSAttributedString*) | |
166 attributedSubstringForProposedRange:(NSRange)range | |
167 actualRange:(NSRangePointer)actualRange { | |
168 base::string16 substring; | |
169 if (textInputClient_) { | |
170 gfx::Range textRange; | |
171 textInputClient_->GetTextRange(&textRange); | |
172 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); | |
173 textInputClient_->GetTextFromRange(subrange, &substring); | |
174 if (actualRange) | |
175 *actualRange = subrange.ToNSRange(); | |
176 } | |
177 return [[[NSAttributedString alloc] | |
178 initWithString:base::SysUTF16ToNSString(substring)] autorelease]; | |
179 } | |
180 | |
181 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { | |
182 NOTIMPLEMENTED(); | |
183 return 0; | |
184 } | |
185 | |
186 - (void)doCommandBySelector:(SEL)selector { | |
187 if ([self respondsToSelector:selector]) | |
188 [self performSelector:selector withObject:nil]; | |
189 else | |
190 [[self nextResponder] doCommandBySelector:selector]; | |
191 } | |
192 | |
193 - (NSRect)firstRectForCharacterRange:(NSRange)range | |
194 actualRange:(NSRangePointer)actualRange { | |
195 NOTIMPLEMENTED(); | |
196 return NSZeroRect; | |
197 } | |
198 | |
199 - (BOOL)hasMarkedText { | |
200 return textInputClient_ && textInputClient_->HasCompositionText(); | |
201 } | |
202 | |
203 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | |
204 if (!textInputClient_) | |
205 return; | |
206 | |
207 if ([text isKindOfClass:[NSAttributedString class]]) | |
208 text = [text string]; | |
209 textInputClient_->DeleteRange(gfx::Range(replacementRange)); | |
210 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); | |
211 } | |
212 | |
213 - (NSRange)markedRange { | |
214 if (!textInputClient_) | |
215 return NSMakeRange(NSNotFound, 0); | |
216 | |
217 gfx::Range range; | |
218 textInputClient_->GetCompositionTextRange(&range); | |
219 return range.ToNSRange(); | |
220 } | |
221 | |
222 - (NSRange)selectedRange { | |
223 if (!textInputClient_) | |
224 return NSMakeRange(NSNotFound, 0); | |
225 | |
226 gfx::Range range; | |
227 textInputClient_->GetSelectionRange(&range); | |
228 return range.ToNSRange(); | |
229 } | |
230 | |
231 - (void)setMarkedText:(id)text | |
232 selectedRange:(NSRange)selectedRange | |
233 replacementRange:(NSRange)replacementRange { | |
234 if (!textInputClient_) | |
235 return; | |
236 | |
237 if ([text isKindOfClass:[NSAttributedString class]]) | |
238 text = [text string]; | |
239 ui::CompositionText composition; | |
240 composition.text = base::SysNSStringToUTF16(text); | |
241 composition.selection = gfx::Range(selectedRange); | |
242 textInputClient_->SetCompositionText(composition); | |
243 } | |
244 | |
245 - (void)unmarkText { | |
246 if (textInputClient_) | |
247 textInputClient_->ConfirmCompositionText(); | |
248 } | |
249 | |
250 - (NSArray*)validAttributesForMarkedText { | |
251 return @[]; | |
252 } | |
253 | |
122 @end | 254 @end |
OLD | NEW |