OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" | 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
9 #include "chrome/app/chrome_command_ids.h" // IDC_* | 9 #include "chrome/app/chrome_command_ids.h" // IDC_* |
10 #include "chrome/browser/ui/browser_list.h" | 10 #include "chrome/browser/ui/browser_list.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 DCHECK(observer); | 89 DCHECK(observer); |
90 if (observer && observer->CanCopy()) | 90 if (observer && observer->CanCopy()) |
91 observer->CopyToPasteboard([NSPasteboard generalPasteboard]); | 91 observer->CopyToPasteboard([NSPasteboard generalPasteboard]); |
92 } | 92 } |
93 | 93 |
94 - (void)cut:(id)sender { | 94 - (void)cut:(id)sender { |
95 [self copy:sender]; | 95 [self copy:sender]; |
96 [self delete:nil]; | 96 [self delete:nil]; |
97 } | 97 } |
98 | 98 |
| 99 - (void)copyURL:(id)sender { |
| 100 AutocompleteTextFieldObserver* observer = [self observer]; |
| 101 DCHECK(observer); |
| 102 if (observer->CanCopy()) |
| 103 observer->CopyURLToPasteboard([NSPasteboard generalPasteboard]); |
| 104 } |
| 105 |
99 // This class assumes that the delegate is an AutocompleteTextField. | 106 // This class assumes that the delegate is an AutocompleteTextField. |
100 // Enforce that assumption. | 107 // Enforce that assumption. |
101 - (AutocompleteTextField*)delegate { | 108 - (AutocompleteTextField*)delegate { |
102 AutocompleteTextField* delegate = | 109 AutocompleteTextField* delegate = |
103 static_cast<AutocompleteTextField*>([super delegate]); | 110 static_cast<AutocompleteTextField*>([super delegate]); |
104 DCHECK(delegate == nil || | 111 DCHECK(delegate == nil || |
105 [delegate isKindOfClass:[AutocompleteTextField class]]); | 112 [delegate isKindOfClass:[AutocompleteTextField class]]); |
106 return delegate; | 113 return delegate; |
107 } | 114 } |
108 | 115 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 if (actionMenu) | 208 if (actionMenu) |
202 return actionMenu; | 209 return actionMenu; |
203 | 210 |
204 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease]; | 211 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease]; |
205 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) | 212 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) |
206 action:@selector(cut:) | 213 action:@selector(cut:) |
207 keyEquivalent:@""]; | 214 keyEquivalent:@""]; |
208 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY) | 215 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY) |
209 action:@selector(copy:) | 216 action:@selector(copy:) |
210 keyEquivalent:@""]; | 217 keyEquivalent:@""]; |
| 218 |
| 219 if ([self isEditable]) { |
| 220 // Copy URL if the URL has been replaced by the Extended Instant API. |
| 221 DCHECK([self observer]); |
| 222 NSString* label = l10n_util::GetNSStringWithFixup(IDS_COPY_URL_MAC); |
| 223 [menu addItemWithTitle:label |
| 224 action:@selector(copyURL:) |
| 225 keyEquivalent:@""]; |
| 226 } |
| 227 |
211 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) | 228 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) |
212 action:@selector(paste:) | 229 action:@selector(paste:) |
213 keyEquivalent:@""]; | 230 keyEquivalent:@""]; |
214 | 231 |
215 // TODO(shess): If the control is not editable, should we show a | 232 // TODO(shess): If the control is not editable, should we show a |
216 // greyed-out "Paste and Go"? | 233 // greyed-out "Paste and Go"? |
217 if ([self isEditable]) { | 234 if ([self isEditable]) { |
218 // Paste and go/search. | 235 // Paste and go/search. |
219 AutocompleteTextFieldObserver* observer = [self observer]; | 236 AutocompleteTextFieldObserver* observer = [self observer]; |
220 DCHECK(observer); | 237 DCHECK(observer); |
221 if (observer && observer->CanPasteAndGo()) { | 238 const int string_id = observer->GetPasteActionStringId(); |
222 const int string_id = observer->GetPasteActionStringId(); | 239 NSString* label = l10n_util::GetNSStringWithFixup(string_id); |
223 NSString* label = l10n_util::GetNSStringWithFixup(string_id); | 240 DCHECK([label length]); |
| 241 [menu addItemWithTitle:label |
| 242 action:@selector(pasteAndGo:) |
| 243 keyEquivalent:@""]; |
224 | 244 |
225 // TODO(rohitrao): If the clipboard is empty, should we show a | 245 NSString* search_engine_label = |
226 // greyed-out "Paste and Go" or nothing at all? | 246 l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES); |
227 if ([label length]) { | 247 DCHECK([search_engine_label length]); |
228 [menu addItemWithTitle:label | 248 [menu addItem:[NSMenuItem separatorItem]]; |
229 action:@selector(pasteAndGo:) | 249 NSMenuItem* item = [menu addItemWithTitle:search_engine_label |
230 keyEquivalent:@""]; | 250 action:@selector(commandDispatch:) |
231 } | 251 keyEquivalent:@""]; |
232 } | 252 [item setTag:IDC_EDIT_SEARCH_ENGINES]; |
233 | |
234 NSString* label = l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES); | |
235 DCHECK([label length]); | |
236 if ([label length]) { | |
237 [menu addItem:[NSMenuItem separatorItem]]; | |
238 NSMenuItem* item = [menu addItemWithTitle:label | |
239 action:@selector(commandDispatch:) | |
240 keyEquivalent:@""]; | |
241 [item setTag:IDC_EDIT_SEARCH_ENGINES]; | |
242 } | |
243 } | 253 } |
244 | 254 |
245 return menu; | 255 return menu; |
246 } | 256 } |
247 | 257 |
248 // (Overridden from NSResponder) | 258 // (Overridden from NSResponder) |
249 - (BOOL)becomeFirstResponder { | 259 - (BOOL)becomeFirstResponder { |
250 BOOL doAccept = [super becomeFirstResponder]; | 260 BOOL doAccept = [super becomeFirstResponder]; |
251 AutocompleteTextField* field = [self delegate]; | 261 AutocompleteTextField* field = [self delegate]; |
252 // Only lock visibility if we've been set up with a delegate (the text field). | 262 // Only lock visibility if we've been set up with a delegate (the text field). |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 | 474 |
465 // The text has been changed programmatically. The observer should know | 475 // The text has been changed programmatically. The observer should know |
466 // this change, so setting |textChangedByKeyEvents_| to NO to | 476 // this change, so setting |textChangedByKeyEvents_| to NO to |
467 // prevent its OnDidChange() method from being called unnecessarily. | 477 // prevent its OnDidChange() method from being called unnecessarily. |
468 textChangedByKeyEvents_ = NO; | 478 textChangedByKeyEvents_ = NO; |
469 } | 479 } |
470 | 480 |
471 - (BOOL)validateMenuItem:(NSMenuItem*)item { | 481 - (BOOL)validateMenuItem:(NSMenuItem*)item { |
472 if ([item action] == @selector(copyToFindPboard:)) | 482 if ([item action] == @selector(copyToFindPboard:)) |
473 return [self selectedRange].length > 0; | 483 return [self selectedRange].length > 0; |
| 484 if ([item action] == @selector(pasteAndGo:)) { |
| 485 // TODO(rohitrao): If the clipboard is empty, should we show a |
| 486 // greyed-out "Paste and Go" or nothing at all? |
| 487 AutocompleteTextFieldObserver* observer = [self observer]; |
| 488 DCHECK(observer); |
| 489 return observer->CanPasteAndGo(); |
| 490 } |
| 491 if ([item action] == @selector(copyURL:)) { |
| 492 AutocompleteTextFieldObserver* observer = [self observer]; |
| 493 DCHECK(observer); |
| 494 return observer->ShouldEnableCopyURL(); |
| 495 } |
474 return [super validateMenuItem:item]; | 496 return [super validateMenuItem:item]; |
475 } | 497 } |
476 | 498 |
477 - (void)copyToFindPboard:(id)sender { | 499 - (void)copyToFindPboard:(id)sender { |
478 NSRange selectedRange = [self selectedRange]; | 500 NSRange selectedRange = [self selectedRange]; |
479 if (selectedRange.length == 0) | 501 if (selectedRange.length == 0) |
480 return; | 502 return; |
481 NSAttributedString* selection = | 503 NSAttributedString* selection = |
482 [self attributedSubstringForProposedRange:selectedRange | 504 [self attributedSubstringForProposedRange:selectedRange |
483 actualRange:NULL]; | 505 actualRange:NULL]; |
484 if (!selection) | 506 if (!selection) |
485 return; | 507 return; |
486 | 508 |
487 [[FindPasteboard sharedInstance] setFindText:[selection string]]; | 509 [[FindPasteboard sharedInstance] setFindText:[selection string]]; |
488 } | 510 } |
489 | 511 |
490 @end | 512 @end |
OLD | NEW |