Chromium Code Reviews| 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 // Passes the event to NativeWidgetMac for handling. | 18 // Passes the event to NativeWidgetMac for handling. |
| 15 - (void)handleUntranslatedKeyEvent:(NSEvent*)theEvent; | 19 - (void)handleUntranslatedKeyEvent:(NSEvent*)theEvent; |
| 16 | 20 |
| 17 // Translates the location of |theEvent| to toolkit-views coordinates and passes | 21 // Translates the location of |theEvent| to toolkit-views coordinates and passes |
| 18 // the event to NativeWidgetMac for handling. | 22 // the event to NativeWidgetMac for handling. |
| 19 - (void)handleMouseEvent:(NSEvent*)theEvent; | 23 - (void)handleMouseEvent:(NSEvent*)theEvent; |
| 20 | 24 |
| 25 // Execute a command on the currently focused TextInputClient. | |
| 26 // |commandId| should be a resource ID from ui_strings.grd. | |
| 27 - (void)doCommandByID:(int)commandId; | |
| 28 | |
| 21 @end | 29 @end |
| 22 | 30 |
| 23 @implementation BridgedContentView | 31 @implementation BridgedContentView |
| 24 | 32 |
| 25 @synthesize hostedView = hostedView_; | 33 @synthesize hostedView = hostedView_; |
| 34 @synthesize textInputClient = textInputClient_; | |
| 26 | 35 |
| 27 - (id)initWithView:(views::View*)viewToHost { | 36 - (id)initWithView:(views::View*)viewToHost { |
| 28 DCHECK(viewToHost); | 37 DCHECK(viewToHost); |
| 29 gfx::Rect bounds = viewToHost->bounds(); | 38 gfx::Rect bounds = viewToHost->bounds(); |
| 30 // To keep things simple, assume the origin is (0, 0) until there exists a use | 39 // To keep things simple, assume the origin is (0, 0) until there exists a use |
| 31 // case for something other than that. | 40 // case for something other than that. |
| 32 DCHECK(bounds.origin().IsOrigin()); | 41 DCHECK(bounds.origin().IsOrigin()); |
| 33 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); | 42 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); |
| 34 if ((self = [super initWithFrame:initialFrame])) | 43 if ((self = [super initWithFrame:initialFrame])) |
| 35 hostedView_ = viewToHost; | 44 hostedView_ = viewToHost; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 52 } | 61 } |
| 53 | 62 |
| 54 - (void)handleMouseEvent:(NSEvent*)theEvent { | 63 - (void)handleMouseEvent:(NSEvent*)theEvent { |
| 55 if (!hostedView_) | 64 if (!hostedView_) |
| 56 return; | 65 return; |
| 57 | 66 |
| 58 ui::MouseEvent event(theEvent); | 67 ui::MouseEvent event(theEvent); |
| 59 hostedView_->GetWidget()->OnMouseEvent(&event); | 68 hostedView_->GetWidget()->OnMouseEvent(&event); |
| 60 } | 69 } |
| 61 | 70 |
| 71 - (void)doCommandByID:(int)commandId { | |
| 72 if (textInputClient_ && textInputClient_->IsEditingCommandEnabled(commandId)) | |
| 73 textInputClient_->ExecuteEditingCommand(commandId); | |
| 74 } | |
| 75 | |
| 62 // NSView implementation. | 76 // NSView implementation. |
| 63 | 77 |
| 64 - (BOOL)acceptsFirstResponder { | 78 - (BOOL)acceptsFirstResponder { |
| 65 return YES; | 79 return YES; |
| 66 } | 80 } |
| 67 | 81 |
| 68 - (void)setFrameSize:(NSSize)newSize { | 82 - (void)setFrameSize:(NSSize)newSize { |
| 69 [super setFrameSize:newSize]; | 83 [super setFrameSize:newSize]; |
| 70 if (!hostedView_) | 84 if (!hostedView_) |
| 71 return; | 85 return; |
| 72 | 86 |
| 73 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); | 87 hostedView_->SetSize(gfx::Size(newSize.width, newSize.height)); |
| 74 } | 88 } |
| 75 | 89 |
| 76 - (void)drawRect:(NSRect)dirtyRect { | 90 - (void)drawRect:(NSRect)dirtyRect { |
| 77 if (!hostedView_) | 91 if (!hostedView_) |
| 78 return; | 92 return; |
| 79 | 93 |
| 80 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); | 94 gfx::CanvasSkiaPaint canvas(dirtyRect, false /* opaque */); |
| 81 hostedView_->Paint(&canvas, views::CullSet()); | 95 hostedView_->Paint(&canvas, views::CullSet()); |
| 82 } | 96 } |
| 83 | 97 |
| 84 - (void)keyDown:(NSEvent*)theEvent { | 98 - (void)keyDown:(NSEvent*)theEvent { |
| 85 [self handleUntranslatedKeyEvent:theEvent]; | 99 if (textInputClient_) |
| 100 [self interpretKeyEvents:@[ theEvent ]]; | |
| 101 else | |
| 102 [super keyDown:theEvent]; | |
| 86 } | 103 } |
| 87 | 104 |
| 88 - (void)keyUp:(NSEvent*)theEvent { | 105 - (void)keyUp:(NSEvent*)theEvent { |
|
tapted
2014/06/25 10:38:08
note you'll need to rebase against master (this ch
| |
| 89 [self handleUntranslatedKeyEvent:theEvent]; | 106 [self handleUntranslatedKeyEvent:theEvent]; |
| 90 } | 107 } |
| 91 | 108 |
| 92 - (void)mouseDown:(NSEvent*)theEvent { | 109 - (void)mouseDown:(NSEvent*)theEvent { |
| 93 [self handleMouseEvent:theEvent]; | 110 [self handleMouseEvent:theEvent]; |
| 94 } | 111 } |
| 95 | 112 |
| 96 - (void)rightMouseDown:(NSEvent*)theEvent { | 113 - (void)rightMouseDown:(NSEvent*)theEvent { |
| 97 [self handleMouseEvent:theEvent]; | 114 [self handleMouseEvent:theEvent]; |
| 98 } | 115 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 // and exited events for the view heirarchy. | 148 // and exited events for the view heirarchy. |
| 132 // TODO(tapted): Install/uninstall tracking areas when required so that the | 149 // TODO(tapted): Install/uninstall tracking areas when required so that the |
| 133 // NSView will receive these events outside of tests. | 150 // NSView will receive these events outside of tests. |
| 134 [self handleMouseEvent:theEvent]; | 151 [self handleMouseEvent:theEvent]; |
| 135 } | 152 } |
| 136 | 153 |
| 137 - (void)scrollWheel:(NSEvent*)theEvent { | 154 - (void)scrollWheel:(NSEvent*)theEvent { |
| 138 [self handleMouseEvent:theEvent]; | 155 [self handleMouseEvent:theEvent]; |
| 139 } | 156 } |
| 140 | 157 |
| 158 - (void)deleteBackward:(id)sender { | |
| 159 [self doCommandByID:IDS_DELETE_BACKWARD]; | |
| 160 } | |
| 161 | |
| 162 - (void)deleteForward:(id)sender { | |
| 163 [self doCommandByID:IDS_DELETE_FORWARD]; | |
| 164 } | |
| 165 | |
| 166 - (void)moveLeft:(id)sender { | |
| 167 [self doCommandByID:IDS_MOVE_LEFT]; | |
| 168 } | |
| 169 | |
| 170 - (void)moveRight:(id)sender { | |
| 171 [self doCommandByID:IDS_MOVE_RIGHT]; | |
| 172 } | |
| 173 | |
| 174 // NSTextInputClient protocol implementation. | |
| 175 | |
| 176 - (NSAttributedString*) | |
| 177 attributedSubstringForProposedRange:(NSRange)range | |
| 178 actualRange:(NSRangePointer)actualRange { | |
| 179 base::string16 substring; | |
| 180 if (textInputClient_) { | |
| 181 gfx::Range textRange; | |
| 182 textInputClient_->GetTextRange(&textRange); | |
| 183 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); | |
| 184 textInputClient_->GetTextFromRange(subrange, &substring); | |
| 185 if (actualRange) | |
| 186 *actualRange = subrange.ToNSRange(); | |
| 187 } | |
| 188 return [[[NSAttributedString alloc] | |
| 189 initWithString:base::SysUTF16ToNSString(substring)] autorelease]; | |
| 190 } | |
| 191 | |
| 192 - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { | |
| 193 NOTIMPLEMENTED(); | |
| 194 return 0; | |
| 195 } | |
| 196 | |
| 197 - (void)doCommandBySelector:(SEL)selector { | |
| 198 if ([self respondsToSelector:selector]) | |
| 199 [self performSelector:selector withObject:nil]; | |
| 200 else | |
| 201 [[self nextResponder] doCommandBySelector:selector]; | |
| 202 } | |
| 203 | |
| 204 - (NSRect)firstRectForCharacterRange:(NSRange)range | |
| 205 actualRange:(NSRangePointer)actualRange { | |
| 206 NOTIMPLEMENTED(); | |
| 207 return NSZeroRect; | |
| 208 } | |
| 209 | |
| 210 - (BOOL)hasMarkedText { | |
| 211 return textInputClient_ && textInputClient_->HasCompositionText(); | |
| 212 } | |
| 213 | |
| 214 - (void)insertText:(id)text replacementRange:(NSRange)replacementRange { | |
| 215 if (!textInputClient_) | |
| 216 return; | |
| 217 | |
| 218 if ([text isKindOfClass:[NSAttributedString class]]) | |
| 219 text = [text string]; | |
| 220 textInputClient_->DeleteRange(gfx::Range(replacementRange)); | |
| 221 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); | |
| 222 } | |
| 223 | |
| 224 - (NSRange)markedRange { | |
| 225 if (!textInputClient_) | |
| 226 return NSMakeRange(NSNotFound, 0); | |
| 227 | |
| 228 gfx::Range range; | |
| 229 textInputClient_->GetCompositionTextRange(&range); | |
| 230 return range.ToNSRange(); | |
| 231 } | |
| 232 | |
| 233 - (NSRange)selectedRange { | |
| 234 if (!textInputClient_) | |
| 235 return NSMakeRange(NSNotFound, 0); | |
| 236 | |
| 237 gfx::Range range; | |
| 238 textInputClient_->GetSelectionRange(&range); | |
| 239 return range.ToNSRange(); | |
| 240 } | |
| 241 | |
| 242 - (void)setMarkedText:(id)text | |
| 243 selectedRange:(NSRange)selectedRange | |
| 244 replacementRange:(NSRange)replacementRange { | |
| 245 if (!textInputClient_) | |
| 246 return; | |
| 247 | |
| 248 if ([text isKindOfClass:[NSAttributedString class]]) | |
| 249 text = [text string]; | |
| 250 ui::CompositionText composition; | |
| 251 composition.text = base::SysNSStringToUTF16(text); | |
| 252 composition.selection = gfx::Range(selectedRange); | |
| 253 textInputClient_->SetCompositionText(composition); | |
| 254 } | |
| 255 | |
| 256 - (void)unmarkText { | |
| 257 if (textInputClient_) | |
| 258 textInputClient_->ConfirmCompositionText(); | |
| 259 } | |
| 260 | |
| 261 - (NSArray*)validAttributesForMarkedText { | |
| 262 return @[]; | |
| 263 } | |
| 264 | |
| 141 @end | 265 @end |
| OLD | NEW |