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/accessibility/ax_view_state.h" | |
11 #include "ui/base/ime/text_input_client.h" | |
12 #include "ui/gfx/canvas.h" | |
8 #include "ui/gfx/canvas_paint_mac.h" | 13 #include "ui/gfx/canvas_paint_mac.h" |
14 #include "ui/gfx/geometry/rect.h" | |
15 #import "ui/views/cocoa/bridged_native_widget.h" | |
9 #include "ui/views/view.h" | 16 #include "ui/views/view.h" |
10 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
11 | 18 |
12 @interface BridgedContentView () | 19 @interface BridgedContentView () |
13 | 20 |
14 // Translates the location of |theEvent| to toolkit-views coordinates and passes | 21 // Translates the location of |theEvent| to toolkit-views coordinates and passes |
15 // the event to NativeWidgetMac for handling. | 22 // the event to NativeWidgetMac for handling. |
16 - (void)handleMouseEvent:(NSEvent*)theEvent; | 23 - (void)handleMouseEvent:(NSEvent*)theEvent; |
17 | 24 |
25 // Execute a command on the currently focused TextInputClient. | |
tapted
2014/06/19 01:29:16
nit: mention that |commandId| should be a resource
Andre
2014/06/19 23:20:26
Done.
| |
26 - (void)doCommandByID:(int)commandId; | |
27 | |
18 @end | 28 @end |
19 | 29 |
20 @implementation BridgedContentView | 30 @implementation BridgedContentView |
21 | 31 |
22 @synthesize hostedView = hostedView_; | 32 @synthesize hostedView = hostedView_; |
33 @synthesize textInputClient = textInputClient_; | |
23 | 34 |
24 - (id)initWithView:(views::View*)viewToHost { | 35 - (id)initWithView:(views::View*)viewToHost |
36 parent:(views::BridgedNativeWidget*)parent { | |
25 DCHECK(viewToHost); | 37 DCHECK(viewToHost); |
38 DCHECK(parent); | |
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 parent_ = parent; |
47 if (hostedView_->GetFocusManager()) | |
48 hostedView_->GetFocusManager()->AddFocusChangeListener(parent_); | |
tapted
2014/06/19 01:29:15
Hm - I just looked at this some more. Does this al
Andre
2014/06/19 23:20:26
I like it, moved this to BridgedNativeWidget.
| |
49 } | |
34 return self; | 50 return self; |
35 } | 51 } |
36 | 52 |
37 - (void)clearView { | 53 - (void)clearView { |
38 hostedView_ = NULL; | 54 if (hostedView_) { |
tapted
2014/06/19 01:29:16
if (!hostedView_)
return;
or perhaps even
DCHE
Andre
2014/06/19 23:20:26
Removed this change from the patch.
| |
55 if (hostedView_->GetFocusManager()) | |
tapted
2014/06/19 01:29:16
note that we'll have to ensure the lifetime of thi
| |
56 hostedView_->GetFocusManager()->RemoveFocusChangeListener(parent_); | |
57 hostedView_ = NULL; | |
58 } | |
39 } | 59 } |
40 | 60 |
41 // BridgedContentView private implementation. | 61 // BridgedContentView private implementation. |
42 | 62 |
43 - (void)handleMouseEvent:(NSEvent*)theEvent { | 63 - (void)handleMouseEvent:(NSEvent*)theEvent { |
44 if (!hostedView_) | 64 if (!hostedView_) |
45 return; | 65 return; |
46 | 66 |
47 ui::MouseEvent event(theEvent); | 67 ui::MouseEvent event(theEvent); |
48 hostedView_->GetWidget()->OnMouseEvent(&event); | 68 hostedView_->GetWidget()->OnMouseEvent(&event); |
49 } | 69 } |
50 | 70 |
71 - (void)doCommandByID:(int)commandId { | |
72 if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId)) | |
73 textInputClient_->ExecuteEditingCommand(commandId); | |
74 } | |
75 | |
51 // NSView implementation. | 76 // NSView implementation. |
52 | 77 |
78 - (BOOL)acceptsFirstResponder { | |
79 return YES; | |
80 } | |
81 | |
53 - (void)setFrameSize:(NSSize)newSize { | 82 - (void)setFrameSize:(NSSize)newSize { |
54 [super setFrameSize:newSize]; | 83 [super setFrameSize:newSize]; |
55 if (!hostedView_) | 84 if (!hostedView_) |
56 return; | 85 return; |
57 | 86 |
58 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 87 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
59 } | 88 } |
60 | 89 |
61 - (void)drawRect:(NSRect)dirtyRect { | 90 - (void)drawRect:(NSRect)dirtyRect { |
62 if (!hostedView_) | 91 if (!hostedView_) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 } | 140 } |
112 | 141 |
113 - (void)mouseEntered:(NSEvent*)theEvent { | 142 - (void)mouseEntered:(NSEvent*)theEvent { |
114 [self handleMouseEvent:theEvent]; | 143 [self handleMouseEvent:theEvent]; |
115 } | 144 } |
116 | 145 |
117 - (void)mouseExited:(NSEvent*)theEvent { | 146 - (void)mouseExited:(NSEvent*)theEvent { |
118 [self handleMouseEvent:theEvent]; | 147 [self handleMouseEvent:theEvent]; |
119 } | 148 } |
120 | 149 |
150 - (void)keyDown:(NSEvent*)theEvent { | |
151 if (textInputClient_) | |
152 [self interpretKeyEvents:@[ theEvent ]]; | |
153 else | |
154 [super keyDown:theEvent]; | |
155 } | |
156 | |
157 - (void)deleteBackward:(id)sender { | |
158 [self doCommandByID:IDS_DELETE_BACKWARD]; | |
159 } | |
160 | |
161 - (void)deleteForward:(id)sender { | |
162 [self doCommandByID:IDS_DELETE_FORWARD]; | |
163 } | |
164 | |
165 - (void)moveLeft:(id)sender { | |
166 [self doCommandByID:IDS_MOVE_LEFT]; | |
167 } | |
168 | |
169 - (void)moveRight:(id)sender { | |
170 [self doCommandByID:IDS_MOVE_RIGHT]; | |
171 } | |
172 | |
173 // NSTextInputClient protocol implementation. | |
174 | |
175 - (NSAttributedString*) | |
176 attributedSubstringForProposedRange:(NSRange)range | |
177 actualRange:(NSRangePointer)actualRange { | |
178 base::string16 substring; | |
179 if (textInputClient_) { | |
180 gfx::Range textRange; | |
181 textInputClient_->GetTextRange(&textRange); | |
182 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); | |
183 textInputClient_->GetTextFromRange(subrange, &substring); | |
184 if (actualRange) | |
185 *actualRange = subrange.ToNSRange(); | |
186 } | |
187 return [[[NSAttributedString alloc] | |
188 initWithString:base::SysUTF16ToNSString(substring)] autorelease]; | |
189 } | |
190 | |
191 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { | |
192 NOTIMPLEMENTED(); | |
193 return 0; | |
194 } | |
195 | |
196 - (void)doCommandBySelector:(SEL)selector { | |
197 if ([self respondsToSelector:selector]) | |
198 [self performSelector:selector withObject:nil]; | |
199 else | |
200 [[self nextResponder] doCommandBySelector:selector]; | |
201 } | |
202 | |
203 - (NSRect)firstRectForCharacterRange:(NSRange)range | |
204 actualRange:(NSRangePointer)actualRange { | |
205 NOTIMPLEMENTED(); | |
206 return NSZeroRect; | |
207 } | |
208 | |
209 - (BOOL)hasMarkedText { | |
210 return textInputClient_ && textInputClient_->HasCompositionText(); | |
211 } | |
212 | |
213 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | |
214 if (textInputClient_) { | |
215 if ([text isKindOfClass:[NSAttributedString class]]) | |
216 text = [text string]; | |
217 textInputClient_->DeleteRange(gfx::Range(replacementRange)); | |
218 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); | |
219 } | |
220 } | |
221 | |
222 - (NSRange)markedRange { | |
223 if (!textInputClient_) | |
224 return NSMakeRange(0, 0); | |
tapted
2014/06/19 01:29:16
oops - my bad. this should be NSMakeRange(NSNotFou
Andre
2014/06/19 23:20:26
Done.
| |
225 | |
226 gfx::Range range; | |
227 textInputClient_->GetCompositionTextRange(&range); | |
228 return range.ToNSRange(); | |
229 } | |
230 | |
231 - (NSRange)selectedRange { | |
232 if (!textInputClient_) | |
233 return NSMakeRange(0, 0); | |
234 | |
235 gfx::Range range; | |
236 textInputClient_->GetSelectionRange(&range); | |
237 return range.ToNSRange(); | |
238 } | |
239 | |
240 - (void)setMarkedText:(id)text | |
241 selectedRange:(NSRange)selectedRange | |
242 replacementRange:(NSRange)replacementRange { | |
243 if (textInputClient_) { | |
tapted
2014/06/19 01:29:16
same here - prefer early return / less indenting
Andre
2014/06/19 23:20:26
Done.
| |
244 if ([text isKindOfClass:[NSAttributedString class]]) | |
245 text = [text string]; | |
246 ui::CompositionText composition; | |
247 composition.text = base::SysNSStringToUTF16(text); | |
248 composition.selection = gfx::Range(selectedRange); | |
249 textInputClient_->SetCompositionText(composition); | |
250 } | |
251 } | |
252 | |
253 - (void)unmarkText { | |
254 if (textInputClient_) | |
255 textInputClient_->ConfirmCompositionText(); | |
256 } | |
257 | |
258 - (NSArray*)validAttributesForMarkedText { | |
259 return @[]; | |
260 } | |
261 | |
121 @end | 262 @end |
OLD | NEW |