| 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 #import "ui/base/cocoa/appkit_utils.h" | 13 #import "ui/base/cocoa/appkit_utils.h" |
| 14 #include "ui/base/cocoa/cocoa_base_utils.h" | 14 #include "ui/base/cocoa/cocoa_base_utils.h" |
| 15 #include "ui/base/dragdrop/drag_drop_types.h" | 15 #include "ui/base/dragdrop/drag_drop_types.h" |
| 16 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 16 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" |
| 17 #include "ui/base/ime/input_method.h" | 17 #include "ui/base/ime/input_method.h" |
| 18 #include "ui/base/ime/text_edit_commands.h" | 18 #include "ui/base/ime/text_edit_commands.h" |
| 19 #include "ui/base/ime/text_input_client.h" | 19 #include "ui/base/ime/text_input_client.h" |
| 20 #include "ui/compositor/canvas_painter.h" | 20 #include "ui/compositor/canvas_painter.h" |
| 21 #import "ui/events/cocoa/cocoa_event_utils.h" | 21 #import "ui/events/cocoa/cocoa_event_utils.h" |
| 22 #include "ui/events/event_utils.h" | 22 #include "ui/events/event_utils.h" |
| 23 #include "ui/events/keycodes/dom/dom_code.h" | 23 #include "ui/events/keycodes/dom/dom_code.h" |
| 24 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 24 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 25 #include "ui/gfx/canvas_paint_mac.h" | 25 #include "ui/gfx/canvas_paint_mac.h" |
| 26 #include "ui/gfx/decorated_text.h" | 26 #include "ui/gfx/decorated_text.h" |
| 27 #import "ui/gfx/decorated_text_mac.h" | |
| 28 #include "ui/gfx/geometry/rect.h" | 27 #include "ui/gfx/geometry/rect.h" |
| 29 #import "ui/gfx/mac/coordinate_conversion.h" | 28 #import "ui/gfx/mac/coordinate_conversion.h" |
| 30 #include "ui/gfx/path.h" | 29 #include "ui/gfx/path.h" |
| 31 #import "ui/gfx/path_mac.h" | 30 #import "ui/gfx/path_mac.h" |
| 32 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 31 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 33 #import "ui/views/cocoa/bridged_native_widget.h" | 32 #import "ui/views/cocoa/bridged_native_widget.h" |
| 34 #import "ui/views/cocoa/drag_drop_client_mac.h" | 33 #import "ui/views/cocoa/drag_drop_client_mac.h" |
| 35 #include "ui/views/controls/label.h" | 34 #include "ui/views/controls/label.h" |
| 36 #include "ui/views/controls/menu/menu_config.h" | 35 #include "ui/views/controls/menu/menu_config.h" |
| 37 #include "ui/views/controls/menu/menu_controller.h" | 36 #include "ui/views/controls/menu/menu_controller.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 // This is a special case for which the complete string should should be | 193 // This is a special case for which the complete string should should be |
| 195 // returned. NSTextView also follows this, though the same is not mentioned in | 194 // returned. NSTextView also follows this, though the same is not mentioned in |
| 196 // NSTextInputClient documentation. | 195 // NSTextInputClient documentation. |
| 197 if (!requested_range.IsValid()) | 196 if (!requested_range.IsValid()) |
| 198 *actual_range = text_range; | 197 *actual_range = text_range; |
| 199 | 198 |
| 200 client->GetTextFromRange(*actual_range, &substring); | 199 client->GetTextFromRange(*actual_range, &substring); |
| 201 return substring; | 200 return substring; |
| 202 } | 201 } |
| 203 | 202 |
| 203 NSAttributedString* GetAttributedString( |
| 204 const gfx::DecoratedText& decorated_text) { |
| 205 base::scoped_nsobject<NSMutableAttributedString> str( |
| 206 [[NSMutableAttributedString alloc] |
| 207 initWithString:base::SysUTF16ToNSString(decorated_text.text)]); |
| 208 [str beginEditing]; |
| 209 |
| 210 NSValue* const line_style = |
| 211 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid); |
| 212 |
| 213 for (const auto& attribute : decorated_text.attributes) { |
| 214 DCHECK(!attribute.range.is_reversed()); |
| 215 DCHECK_LE(attribute.range.end(), [str length]); |
| 216 |
| 217 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| 218 NSRange range = attribute.range.ToNSRange(); |
| 219 |
| 220 if (attribute.font.GetNativeFont()) |
| 221 attrs[NSFontAttributeName] = attribute.font.GetNativeFont(); |
| 222 |
| 223 // NSFont does not have underline as an attribute. Hence handle it |
| 224 // separately. |
| 225 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE; |
| 226 if (underline) |
| 227 attrs[NSUnderlineStyleAttributeName] = line_style; |
| 228 |
| 229 if (attribute.strike) |
| 230 attrs[NSStrikethroughStyleAttributeName] = line_style; |
| 231 |
| 232 [str setAttributes:attrs range:range]; |
| 233 } |
| 234 |
| 235 [str endEditing]; |
| 236 return str.autorelease(); |
| 237 } |
| 238 |
| 204 ui::TextEditCommand GetTextEditCommandForMenuAction(SEL action) { | 239 ui::TextEditCommand GetTextEditCommandForMenuAction(SEL action) { |
| 205 if (action == @selector(undo:)) | 240 if (action == @selector(undo:)) |
| 206 return ui::TextEditCommand::UNDO; | 241 return ui::TextEditCommand::UNDO; |
| 207 if (action == @selector(redo:)) | 242 if (action == @selector(redo:)) |
| 208 return ui::TextEditCommand::REDO; | 243 return ui::TextEditCommand::REDO; |
| 209 if (action == @selector(cut:)) | 244 if (action == @selector(cut:)) |
| 210 return ui::TextEditCommand::CUT; | 245 return ui::TextEditCommand::CUT; |
| 211 if (action == @selector(copy:)) | 246 if (action == @selector(copy:)) |
| 212 return ui::TextEditCommand::COPY; | 247 return ui::TextEditCommand::COPY; |
| 213 if (action == @selector(paste:)) | 248 if (action == @selector(paste:)) |
| 214 return ui::TextEditCommand::PASTE; | 249 return ui::TextEditCommand::PASTE; |
| 215 if (action == @selector(selectAll:)) | 250 if (action == @selector(selectAll:)) |
| 216 return ui::TextEditCommand::SELECT_ALL; | 251 return ui::TextEditCommand::SELECT_ALL; |
| 217 return ui::TextEditCommand::INVALID_COMMAND; | 252 return ui::TextEditCommand::INVALID_COMMAND; |
| 218 } | 253 } |
| 219 | 254 |
| 220 } // namespace | 255 } // namespace |
| 221 | 256 |
| 222 // TODO(spqchan): Implement support when the Speech submenu is in the main | |
| 223 // menu. | |
| 224 @interface BridgedContentView () | 257 @interface BridgedContentView () |
| 225 | 258 |
| 226 // Returns the active menu controller corresponding to |hostedView_|, | 259 // Returns the active menu controller corresponding to |hostedView_|, |
| 227 // nil otherwise. | 260 // nil otherwise. |
| 228 - (MenuController*)activeMenuController; | 261 - (MenuController*)activeMenuController; |
| 229 | 262 |
| 230 // Passes |event| to the InputMethod for dispatch. | 263 // Passes |event| to the InputMethod for dispatch. |
| 231 - (void)handleKeyEvent:(ui::KeyEvent*)event; | 264 - (void)handleKeyEvent:(ui::KeyEvent*)event; |
| 232 | 265 |
| 233 // Allows accelerators to be handled at different points in AppKit key event | 266 // Allows accelerators to be handled at different points in AppKit key event |
| (...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 return; | 844 return; |
| 812 | 845 |
| 813 views::WordLookupClient* wordLookupClient = target->GetWordLookupClient(); | 846 views::WordLookupClient* wordLookupClient = target->GetWordLookupClient(); |
| 814 if (!wordLookupClient) | 847 if (!wordLookupClient) |
| 815 return; | 848 return; |
| 816 | 849 |
| 817 gfx::Point locationInTarget = locationInContent; | 850 gfx::Point locationInTarget = locationInContent; |
| 818 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); | 851 views::View::ConvertPointToTarget(hostedView_, target, &locationInTarget); |
| 819 gfx::DecoratedText decoratedWord; | 852 gfx::DecoratedText decoratedWord; |
| 820 gfx::Point baselinePoint; | 853 gfx::Point baselinePoint; |
| 821 if (!wordLookupClient->GetDecoratedWordAndBaselineAtPoint( | 854 if (!wordLookupClient->GetDecoratedWordAtPoint( |
| 822 locationInTarget, &decoratedWord, &baselinePoint)) { | 855 locationInTarget, &decoratedWord, &baselinePoint)) { |
| 823 return; | 856 return; |
| 824 } | 857 } |
| 825 | 858 |
| 826 // Convert |baselinePoint| to the coordinate system of |hostedView_|. | 859 // Convert |baselinePoint| to the coordinate system of |hostedView_|. |
| 827 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); | 860 views::View::ConvertPointToTarget(target, hostedView_, &baselinePoint); |
| 828 NSPoint baselinePointAppKit = NSMakePoint( | 861 NSPoint baselinePointAppKit = NSMakePoint( |
| 829 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); | 862 baselinePoint.x(), NSHeight([self frame]) - baselinePoint.y()); |
| 830 [self showDefinitionForAttributedString: | 863 [self showDefinitionForAttributedString:GetAttributedString(decoratedWord) |
| 831 gfx::GetAttributedStringFromDecoratedText(decoratedWord) | |
| 832 atPoint:baselinePointAppKit]; | 864 atPoint:baselinePointAppKit]; |
| 833 } | 865 } |
| 834 | 866 |
| 835 //////////////////////////////////////////////////////////////////////////////// | 867 //////////////////////////////////////////////////////////////////////////////// |
| 836 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 868 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
| 837 // 10.9 SDK). The list should eventually be complete. Anything not defined will | 869 // 10.9 SDK). The list should eventually be complete. Anything not defined will |
| 838 // beep when interpretKeyEvents: would otherwise call it. | 870 // beep when interpretKeyEvents: would otherwise call it. |
| 839 // TODO(tapted): Make this list complete, except for insert* methods which are | 871 // TODO(tapted): Make this list complete, except for insert* methods which are |
| 840 // dispatched as regular key events in doCommandBySelector:. | 872 // dispatched as regular key events in doCommandBySelector:. |
| 841 | 873 |
| (...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1457 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1489 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 1458 } | 1490 } |
| 1459 | 1491 |
| 1460 - (id)accessibilityFocusedUIElement { | 1492 - (id)accessibilityFocusedUIElement { |
| 1461 if (!hostedView_) | 1493 if (!hostedView_) |
| 1462 return nil; | 1494 return nil; |
| 1463 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; | 1495 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; |
| 1464 } | 1496 } |
| 1465 | 1497 |
| 1466 @end | 1498 @end |
| OLD | NEW |