| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 } | 927 } |
| 928 | 928 |
| 929 void OmniboxViewMac::OnMouseDown(NSInteger button_number) { | 929 void OmniboxViewMac::OnMouseDown(NSInteger button_number) { |
| 930 // Restore caret visibility whenever the user clicks in the the omnibox. This | 930 // Restore caret visibility whenever the user clicks in the the omnibox. This |
| 931 // is not always covered by OnSetFocus() because when clicking while the | 931 // is not always covered by OnSetFocus() because when clicking while the |
| 932 // omnibox has invisible focus does not trigger a new OnSetFocus() call. | 932 // omnibox has invisible focus does not trigger a new OnSetFocus() call. |
| 933 if (button_number == 0 || button_number == 1) | 933 if (button_number == 0 || button_number == 1) |
| 934 model()->SetCaretVisibility(true); | 934 model()->SetCaretVisibility(true); |
| 935 } | 935 } |
| 936 | 936 |
| 937 bool OmniboxViewMac::ShouldSelectAllOnMouseDown() { | |
| 938 return !controller()->GetToolbarModel()->WouldPerformSearchTermReplacement( | |
| 939 false); | |
| 940 } | |
| 941 | |
| 942 bool OmniboxViewMac::CanCopy() { | 937 bool OmniboxViewMac::CanCopy() { |
| 943 const NSRange selection = GetSelectedRange(); | 938 const NSRange selection = GetSelectedRange(); |
| 944 return selection.length > 0; | 939 return selection.length > 0; |
| 945 } | 940 } |
| 946 | 941 |
| 947 base::scoped_nsobject<NSPasteboardItem> OmniboxViewMac::CreatePasteboardItem() { | 942 base::scoped_nsobject<NSPasteboardItem> OmniboxViewMac::CreatePasteboardItem() { |
| 948 DCHECK(CanCopy()); | 943 DCHECK(CanCopy()); |
| 949 | 944 |
| 950 const NSRange selection = GetSelectedRange(); | 945 const NSRange selection = GetSelectedRange(); |
| 951 base::string16 text = base::SysNSStringToUTF16( | 946 base::string16 text = base::SysNSStringToUTF16( |
| 952 [[field_ stringValue] substringWithRange:selection]); | 947 [[field_ stringValue] substringWithRange:selection]); |
| 953 | 948 |
| 954 // Copy the URL unless this is the search URL and it's being replaced by the | 949 // Copy the URL. |
| 955 // Extended Instant API. | |
| 956 GURL url; | 950 GURL url; |
| 957 bool write_url = false; | 951 bool write_url = false; |
| 958 if (!controller()->GetToolbarModel()->WouldPerformSearchTermReplacement( | 952 model()->AdjustTextForCopy(selection.location, IsSelectAll(), &text, &url, |
| 959 false)) { | 953 &write_url); |
| 960 model()->AdjustTextForCopy(selection.location, IsSelectAll(), &text, &url, | |
| 961 &write_url); | |
| 962 } | |
| 963 | 954 |
| 964 if (IsSelectAll()) | 955 if (IsSelectAll()) |
| 965 UMA_HISTOGRAM_COUNTS(OmniboxEditModel::kCutOrCopyAllTextHistogram, 1); | 956 UMA_HISTOGRAM_COUNTS(OmniboxEditModel::kCutOrCopyAllTextHistogram, 1); |
| 966 | 957 |
| 967 NSString* nstext = base::SysUTF16ToNSString(text); | 958 NSString* nstext = base::SysUTF16ToNSString(text); |
| 968 if (write_url) { | 959 if (write_url) { |
| 969 return ui::ClipboardUtil::PasteboardItemFromUrl( | 960 return ui::ClipboardUtil::PasteboardItemFromUrl( |
| 970 base::SysUTF8ToNSString(url.spec()), nstext); | 961 base::SysUTF8ToNSString(url.spec()), nstext); |
| 971 } else { | 962 } else { |
| 972 return ui::ClipboardUtil::PasteboardItemFromString(nstext); | 963 return ui::ClipboardUtil::PasteboardItemFromString(nstext); |
| 973 } | 964 } |
| 974 } | 965 } |
| 975 | 966 |
| 976 void OmniboxViewMac::CopyToPasteboard(NSPasteboard* pboard) { | 967 void OmniboxViewMac::CopyToPasteboard(NSPasteboard* pboard) { |
| 977 [pboard clearContents]; | 968 [pboard clearContents]; |
| 978 base::scoped_nsobject<NSPasteboardItem> item(CreatePasteboardItem()); | 969 base::scoped_nsobject<NSPasteboardItem> item(CreatePasteboardItem()); |
| 979 [pboard writeObjects:@[ item.get() ]]; | 970 [pboard writeObjects:@[ item.get() ]]; |
| 980 } | 971 } |
| 981 | 972 |
| 982 void OmniboxViewMac::ShowURL() { | |
| 983 DCHECK(ShouldEnableShowURL()); | |
| 984 OmniboxView::ShowURL(); | |
| 985 } | |
| 986 | |
| 987 void OmniboxViewMac::OnPaste() { | 973 void OmniboxViewMac::OnPaste() { |
| 988 // This code currently expects |field_| to be focused. | 974 // This code currently expects |field_| to be focused. |
| 989 DCHECK([field_ currentEditor]); | 975 DCHECK([field_ currentEditor]); |
| 990 | 976 |
| 991 base::string16 text = GetClipboardText(); | 977 base::string16 text = GetClipboardText(); |
| 992 if (text.empty()) { | 978 if (text.empty()) { |
| 993 return; | 979 return; |
| 994 } | 980 } |
| 995 NSString* s = base::SysUTF16ToNSString(text); | 981 NSString* s = base::SysUTF16ToNSString(text); |
| 996 | 982 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1009 // Force a Paste operation to trigger the text_changed code in | 995 // Force a Paste operation to trigger the text_changed code in |
| 1010 // OnAfterPossibleChange(), even if identical contents are pasted | 996 // OnAfterPossibleChange(), even if identical contents are pasted |
| 1011 // into the text box. | 997 // into the text box. |
| 1012 state_before_change_.text.clear(); | 998 state_before_change_.text.clear(); |
| 1013 | 999 |
| 1014 [editor replaceCharactersInRange:selectedRange withString:s]; | 1000 [editor replaceCharactersInRange:selectedRange withString:s]; |
| 1015 [editor didChangeText]; | 1001 [editor didChangeText]; |
| 1016 } | 1002 } |
| 1017 } | 1003 } |
| 1018 | 1004 |
| 1019 // TODO(dominich): Move to OmniboxView base class? Currently this is defined on | |
| 1020 // the AutocompleteTextFieldObserver but the logic is shared between all | |
| 1021 // platforms. Some refactor might be necessary to simplify this. Or at least | |
| 1022 // this method could call the OmniboxView version. | |
| 1023 bool OmniboxViewMac::ShouldEnableShowURL() { | |
| 1024 return controller()->GetToolbarModel()->WouldReplaceURL(); | |
| 1025 } | |
| 1026 | |
| 1027 bool OmniboxViewMac::CanPasteAndGo() { | 1005 bool OmniboxViewMac::CanPasteAndGo() { |
| 1028 return model()->CanPasteAndGo(GetClipboardText()); | 1006 return model()->CanPasteAndGo(GetClipboardText()); |
| 1029 } | 1007 } |
| 1030 | 1008 |
| 1031 int OmniboxViewMac::GetPasteActionStringId() { | 1009 int OmniboxViewMac::GetPasteActionStringId() { |
| 1032 base::string16 text(GetClipboardText()); | 1010 base::string16 text(GetClipboardText()); |
| 1033 DCHECK(model()->CanPasteAndGo(text)); | 1011 DCHECK(model()->CanPasteAndGo(text)); |
| 1034 return model()->IsPasteAndSearch(text) ? | 1012 return model()->IsPasteAndSearch(text) ? |
| 1035 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO; | 1013 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO; |
| 1036 } | 1014 } |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1161 display_text); | 1139 display_text); |
| 1162 NSDictionary* notification_info = @{ | 1140 NSDictionary* notification_info = @{ |
| 1163 NSAccessibilityAnnouncementKey : announcement, | 1141 NSAccessibilityAnnouncementKey : announcement, |
| 1164 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) | 1142 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) |
| 1165 }; | 1143 }; |
| 1166 NSAccessibilityPostNotificationWithUserInfo( | 1144 NSAccessibilityPostNotificationWithUserInfo( |
| 1167 [field_ window], | 1145 [field_ window], |
| 1168 NSAccessibilityAnnouncementRequestedNotification, | 1146 NSAccessibilityAnnouncementRequestedNotification, |
| 1169 notification_info); | 1147 notification_info); |
| 1170 } | 1148 } |
| OLD | NEW |