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