Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(524)

Side by Side Diff: chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.mm

Issue 10915069: Add Copy URL option to Omnibox context menu when URL is replaced by Instant Extended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ShouldAllowCopyURLMenu fix Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 AutocompleteTextFieldObserver* observer = [self observer];
dhollowa 2012/09/11 20:25:34 |observer| is not used here except in DCHECK.
dominich 2012/09/12 15:23:09 Done.
222 DCHECK(observer);
223 NSString* label = l10n_util::GetNSStringWithFixup(IDS_COPY_URL_MAC);
224 [menu addItemWithTitle:label
225 action:@selector(copyURL:)
226 keyEquivalent:@""];
227 }
228
211 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) 229 [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE)
212 action:@selector(paste:) 230 action:@selector(paste:)
213 keyEquivalent:@""]; 231 keyEquivalent:@""];
214 232
215 // TODO(shess): If the control is not editable, should we show a 233 // TODO(shess): If the control is not editable, should we show a
216 // greyed-out "Paste and Go"? 234 // greyed-out "Paste and Go"?
217 if ([self isEditable]) { 235 if ([self isEditable]) {
218 // Paste and go/search. 236 // Paste and go/search.
219 AutocompleteTextFieldObserver* observer = [self observer]; 237 AutocompleteTextFieldObserver* observer = [self observer];
220 DCHECK(observer); 238 DCHECK(observer);
221 if (observer && observer->CanPasteAndGo()) { 239 const int string_id = observer->GetPasteActionStringId();
222 const int string_id = observer->GetPasteActionStringId(); 240 NSString* label = l10n_util::GetNSStringWithFixup(string_id);
223 NSString* label = l10n_util::GetNSStringWithFixup(string_id);
224 241
225 // TODO(rohitrao): If the clipboard is empty, should we show a 242 if ([label length]) {
226 // greyed-out "Paste and Go" or nothing at all? 243 [menu addItemWithTitle:label
227 if ([label length]) { 244 action:@selector(pasteAndGo:)
228 [menu addItemWithTitle:label 245 keyEquivalent:@""];
229 action:@selector(pasteAndGo:)
230 keyEquivalent:@""];
231 }
232 } 246 }
233 247
234 NSString* label = l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES); 248 NSString* search_engine_label =
235 DCHECK([label length]); 249 l10n_util::GetNSStringWithFixup(IDS_EDIT_SEARCH_ENGINES);
236 if ([label length]) { 250 DCHECK([search_engine_label length]);
251 if ([search_engine_label length]) {
dhollowa 2012/09/11 20:25:34 nit: No need to test for same thing as DCHECK.
Peter Kasting 2012/09/11 21:52:59 Nit: Also, be consistent in this function about wh
dominich 2012/09/12 15:23:09 Done.
dominich 2012/09/12 15:23:09 Done.
237 [menu addItem:[NSMenuItem separatorItem]]; 252 [menu addItem:[NSMenuItem separatorItem]];
238 NSMenuItem* item = [menu addItemWithTitle:label 253 NSMenuItem* item = [menu addItemWithTitle:search_engine_label
239 action:@selector(commandDispatch:) 254 action:@selector(commandDispatch:)
240 keyEquivalent:@""]; 255 keyEquivalent:@""];
241 [item setTag:IDC_EDIT_SEARCH_ENGINES]; 256 [item setTag:IDC_EDIT_SEARCH_ENGINES];
242 } 257 }
243 } 258 }
244 259
245 return menu; 260 return menu;
246 } 261 }
247 262
248 // (Overridden from NSResponder) 263 // (Overridden from NSResponder)
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 479
465 // The text has been changed programmatically. The observer should know 480 // The text has been changed programmatically. The observer should know
466 // this change, so setting |textChangedByKeyEvents_| to NO to 481 // this change, so setting |textChangedByKeyEvents_| to NO to
467 // prevent its OnDidChange() method from being called unnecessarily. 482 // prevent its OnDidChange() method from being called unnecessarily.
468 textChangedByKeyEvents_ = NO; 483 textChangedByKeyEvents_ = NO;
469 } 484 }
470 485
471 - (BOOL)validateMenuItem:(NSMenuItem*)item { 486 - (BOOL)validateMenuItem:(NSMenuItem*)item {
472 if ([item action] == @selector(copyToFindPboard:)) 487 if ([item action] == @selector(copyToFindPboard:))
473 return [self selectedRange].length > 0; 488 return [self selectedRange].length > 0;
489 if ([item action] == @selector(pasteAndGo:)) {
490 // TODO(rohitrao): If the clipboard is empty, should we show a
491 // greyed-out "Paste and Go" or nothing at all?
Peter Kasting 2012/09/11 21:52:59 Nit: Shouldn't this TODO go away?
dominich 2012/09/12 15:23:09 No. I'm not checking the contents of the clipboard
492 AutocompleteTextFieldObserver* observer = [self observer];
493 DCHECK(observer);
494 return observer->CanPasteAndGo();
495 }
496 if ([item action] == @selector(copyURL:)) {
497 AutocompleteTextFieldObserver* observer = [self observer];
498 DCHECK(observer);
499 return observer->ShouldAddCopyURL();
500 }
474 return [super validateMenuItem:item]; 501 return [super validateMenuItem:item];
475 } 502 }
476 503
477 - (void)copyToFindPboard:(id)sender { 504 - (void)copyToFindPboard:(id)sender {
478 NSRange selectedRange = [self selectedRange]; 505 NSRange selectedRange = [self selectedRange];
479 if (selectedRange.length == 0) 506 if (selectedRange.length == 0)
480 return; 507 return;
481 NSAttributedString* selection = 508 NSAttributedString* selection =
482 [self attributedSubstringForProposedRange:selectedRange 509 [self attributedSubstringForProposedRange:selectedRange
483 actualRange:NULL]; 510 actualRange:NULL];
484 if (!selection) 511 if (!selection)
485 return; 512 return;
486 513
487 [[FindPasteboard sharedInstance] setFindText:[selection string]]; 514 [[FindPasteboard sharedInstance] setFindText:[selection string]];
488 } 515 }
489 516
490 @end 517 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698