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 #import "base/mac/mac_util.h" | 8 #import "base/mac/mac_util.h" |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #import "base/mac/sdk_forward_declarations.h" | 10 #import "base/mac/sdk_forward_declarations.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #include "skia/ext/skia_utils_mac.h" | 12 #include "skia/ext/skia_utils_mac.h" |
13 #include "ui/base/cocoa/cocoa_base_utils.h" | 13 #include "ui/base/cocoa/cocoa_base_utils.h" |
14 #include "ui/base/dragdrop/drag_drop_types.h" | 14 #include "ui/base/dragdrop/drag_drop_types.h" |
15 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 15 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" |
16 #include "ui/base/ime/input_method.h" | 16 #include "ui/base/ime/input_method.h" |
17 #include "ui/base/ime/text_edit_commands.h" | 17 #include "ui/base/ime/text_edit_commands.h" |
18 #include "ui/base/ime/text_input_client.h" | 18 #include "ui/base/ime/text_input_client.h" |
19 #include "ui/compositor/canvas_painter.h" | 19 #include "ui/compositor/canvas_painter.h" |
20 #import "ui/events/cocoa/cocoa_event_utils.h" | 20 #import "ui/events/cocoa/cocoa_event_utils.h" |
21 #include "ui/events/event_utils.h" | 21 #include "ui/events/event_utils.h" |
22 #include "ui/events/keycodes/dom/dom_code.h" | 22 #include "ui/events/keycodes/dom/dom_code.h" |
23 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 23 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
24 #include "ui/gfx/canvas_paint_mac.h" | 24 #include "ui/gfx/canvas_paint_mac.h" |
25 #include "ui/gfx/decorated_text.h" | 25 #include "ui/gfx/decorated_text.h" |
| 26 #include "ui/gfx/decorated_text_mac.h" |
26 #include "ui/gfx/geometry/rect.h" | 27 #include "ui/gfx/geometry/rect.h" |
27 #import "ui/gfx/mac/coordinate_conversion.h" | 28 #import "ui/gfx/mac/coordinate_conversion.h" |
28 #include "ui/gfx/path.h" | 29 #include "ui/gfx/path.h" |
29 #import "ui/gfx/path_mac.h" | 30 #import "ui/gfx/path_mac.h" |
30 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 31 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
31 #import "ui/views/cocoa/bridged_native_widget.h" | 32 #import "ui/views/cocoa/bridged_native_widget.h" |
32 #import "ui/views/cocoa/drag_drop_client_mac.h" | 33 #import "ui/views/cocoa/drag_drop_client_mac.h" |
33 #include "ui/views/controls/label.h" | 34 #include "ui/views/controls/label.h" |
34 #include "ui/views/controls/menu/menu_config.h" | 35 #include "ui/views/controls/menu/menu_config.h" |
35 #include "ui/views/controls/menu/menu_controller.h" | 36 #include "ui/views/controls/menu/menu_controller.h" |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); | 206 DCHECK([event type] == NSKeyDown || [event type] == NSKeyUp); |
206 DCHECK_EQ(1u, [[event characters] length]); | 207 DCHECK_EQ(1u, [[event characters] length]); |
207 | 208 |
208 // [NSEvent characters] already considers the pressed key modifiers. Hence | 209 // [NSEvent characters] already considers the pressed key modifiers. Hence |
209 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. | 210 // send ui::EF_NONE as the key modifier to the KeyEvent constructor. |
210 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. | 211 // E.g. For Alt+S, [NSEvent characters] is 'ß' and not 'S'. |
211 return ui::KeyEvent([[event characters] characterAtIndex:0], | 212 return ui::KeyEvent([[event characters] characterAtIndex:0], |
212 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); | 213 ui::KeyboardCodeFromNSEvent(event), ui::EF_NONE); |
213 } | 214 } |
214 | 215 |
215 NSAttributedString* GetAttributedString( | |
216 const gfx::DecoratedText& decorated_text) { | |
217 base::scoped_nsobject<NSMutableAttributedString> str( | |
218 [[NSMutableAttributedString alloc] | |
219 initWithString:base::SysUTF16ToNSString(decorated_text.text)]); | |
220 [str beginEditing]; | |
221 | |
222 NSValue* const line_style = | |
223 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid); | |
224 | |
225 for (const auto& attribute : decorated_text.attributes) { | |
226 DCHECK(!attribute.range.is_reversed()); | |
227 DCHECK_LE(attribute.range.end(), [str length]); | |
228 | |
229 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; | |
230 NSRange range = attribute.range.ToNSRange(); | |
231 | |
232 if (attribute.font.GetNativeFont()) | |
233 attrs[NSFontAttributeName] = attribute.font.GetNativeFont(); | |
234 | |
235 // NSFont does not have underline as an attribute. Hence handle it | |
236 // separately. | |
237 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE; | |
238 if (underline) | |
239 attrs[NSUnderlineStyleAttributeName] = line_style; | |
240 | |
241 if (attribute.strike) | |
242 attrs[NSStrikethroughStyleAttributeName] = line_style; | |
243 | |
244 [str setAttributes:attrs range:range]; | |
245 } | |
246 | |
247 [str endEditing]; | |
248 return str.autorelease(); | |
249 } | |
250 | |
251 ui::TextEditCommand GetTextEditCommandForMenuAction(SEL action) { | 216 ui::TextEditCommand GetTextEditCommandForMenuAction(SEL action) { |
252 if (action == @selector(undo:)) | 217 if (action == @selector(undo:)) |
253 return ui::TextEditCommand::UNDO; | 218 return ui::TextEditCommand::UNDO; |
254 if (action == @selector(redo:)) | 219 if (action == @selector(redo:)) |
255 return ui::TextEditCommand::REDO; | 220 return ui::TextEditCommand::REDO; |
256 if (action == @selector(cut:)) | 221 if (action == @selector(cut:)) |
257 return ui::TextEditCommand::CUT; | 222 return ui::TextEditCommand::CUT; |
258 if (action == @selector(copy:)) | 223 if (action == @selector(copy:)) |
259 return ui::TextEditCommand::COPY; | 224 return ui::TextEditCommand::COPY; |
260 if (action == @selector(paste:)) | 225 if (action == @selector(paste:)) |
261 return ui::TextEditCommand::PASTE; | 226 return ui::TextEditCommand::PASTE; |
262 if (action == @selector(selectAll:)) | 227 if (action == @selector(selectAll:)) |
263 return ui::TextEditCommand::SELECT_ALL; | 228 return ui::TextEditCommand::SELECT_ALL; |
264 return ui::TextEditCommand::INVALID_COMMAND; | 229 return ui::TextEditCommand::INVALID_COMMAND; |
265 } | 230 } |
266 | 231 |
267 } // namespace | 232 } // namespace |
268 | 233 |
| 234 // TODO(spqchan): Implement support with the Speech submenu is the main menu. |
269 @interface BridgedContentView () | 235 @interface BridgedContentView () |
270 | 236 |
271 // Returns the active menu controller corresponding to |hostedView_|, | 237 // Returns the active menu controller corresponding to |hostedView_|, |
272 // nil otherwise. | 238 // nil otherwise. |
273 - (MenuController*)activeMenuController; | 239 - (MenuController*)activeMenuController; |
274 | 240 |
275 // Passes |event| to the InputMethod for dispatch. | 241 // Passes |event| to the InputMethod for dispatch. |
276 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 242 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
277 | 243 |
278 // Allows accelerators to be handled at different points in AppKit key event | 244 // Allows accelerators to be handled at different points in AppKit key event |
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 return; | 795 return; |
830 | 796 |
831 views::WordLookupClient* wordLookupClient = target->GetWordLookupClient(); | 797 views::WordLookupClient* wordLookupClient = target->GetWordLookupClient(); |
832 if (!wordLookupClient) | 798 if (!wordLookupClient) |
833 return; | 799 return; |
834 | 800 |
835 gfx::Point locationInTarget = locationInContent; | 801 gfx::Point locationInTarget = locationInContent; |
836 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); | 802 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); |
837 gfx::DecoratedText decoratedWord; | 803 gfx::DecoratedText decoratedWord; |
838 gfx::Point baselinePoint; | 804 gfx::Point baselinePoint; |
839 if (!wordLookupClient->GetDecoratedWordAtPoint( | 805 if (!wordLookupClient->GetDecoratedWordAndBaselineAtPoint( |
840 locationInTarget, &decoratedWord, &baselinePoint)) { | 806 locationInTarget, &decoratedWord, &baselinePoint)) { |
841 return; | 807 return; |
842 } | 808 } |
843 | 809 |
844 // Convert |baselinePoint| to the coordinate system of |hostedView_|. | 810 // Convert |baselinePoint| to the coordinate system of |hostedView_|. |
845 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); | 811 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); |
846 NSPoint baselinePointAppKit = NSMakePoint( | 812 NSPoint baselinePointAppKit = NSMakePoint( |
847 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); | 813 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); |
848 [self showDefinitionForAttributedString:GetAttributedString(decoratedWord) | 814 [self showDefinitionForAttributedString: |
| 815 gfx::GetAttributedStringFromDecoratedText(decoratedWord) |
849 atPoint:baselinePointAppKit]; | 816 atPoint:baselinePointAppKit]; |
850 } | 817 } |
851 | 818 |
852 //////////////////////////////////////////////////////////////////////////////// | 819 //////////////////////////////////////////////////////////////////////////////// |
853 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 820 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
854 // 10.9 SDK). The list should eventually be complete. Anything not defined will | 821 // 10.9 SDK). The list should eventually be complete. Anything not defined will |
855 // beep when interpretKeyEvents: would otherwise call it. | 822 // beep when interpretKeyEvents: would otherwise call it. |
856 // TODO(tapted): Make this list complete, except for insert* methods which are | 823 // TODO(tapted): Make this list complete, except for insert* methods which are |
857 // dispatched as regular key events in doCommandBySelector:. | 824 // dispatched as regular key events in doCommandBySelector:. |
858 | 825 |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1470 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1437 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
1471 } | 1438 } |
1472 | 1439 |
1473 - (id)accessibilityFocusedUIElement { | 1440 - (id)accessibilityFocusedUIElement { |
1474 if (!hostedView_) | 1441 if (!hostedView_) |
1475 return nil; | 1442 return nil; |
1476 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; | 1443 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; |
1477 } | 1444 } |
1478 | 1445 |
1479 @end | 1446 @end |
OLD | NEW |