Skip to content
Snippets Groups Projects
Commit 6ecc0d57 authored by rohitrao@chromium.org's avatar rohitrao@chromium.org
Browse files

[Mac] Implement Paste and Go/Search.

BUG=http://crbug.com/10937
BUG=http://crbug.com/13021
TEST=Omnibox context menu should contain paste and go/search.
Review URL: http://codereview.chromium.org/192008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26603 0039d316-1c4b-4281-b951-d872f2087c98
parent 8057b040
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "chrome/browser/autocomplete/autocomplete_edit_view.h" #include "chrome/browser/autocomplete/autocomplete_edit_view.h"
#include "chrome/browser/toolbar_model.h" #include "chrome/browser/toolbar_model.h"
#include "chrome/common/page_transition_types.h" #include "chrome/common/page_transition_types.h"
#include "grit/generated_resources.h"
#include "webkit/glue/window_open_disposition.h" #include "webkit/glue/window_open_disposition.h"
class AutocompleteEditController; class AutocompleteEditController;
...@@ -113,6 +114,19 @@ class AutocompleteEditViewMac : public AutocompleteEditView { ...@@ -113,6 +114,19 @@ class AutocompleteEditViewMac : public AutocompleteEditView {
// Called when the user attempts to paste into |field_|. // Called when the user attempts to paste into |field_|.
void OnPaste(); void OnPaste();
// Returns true if the current clipboard text supports paste and go (or paste
// and search).
bool CanPasteAndGo();
// Returns the appropriate "Paste and Go" or "Paste and Search" context menu
// string, depending on what is currently in the clipboard. Must not be
// called unless CanPasteAndGo() returns true.
int GetPasteActionStringId();
// Called when the user initiates a "paste and go" or "paste and search" into
// |field_|.
void OnPasteAndGo();
// Checks if a keyword search is possible and forwards to |model_| // Checks if a keyword search is possible and forwards to |model_|
// if so. Returns true if the tab should be eaten. // if so. Returns true if the tab should be eaten.
bool OnTabPressed(); bool OnTabPressed();
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <Carbon/Carbon.h> // kVK_Return #include <Carbon/Carbon.h> // kVK_Return
#include "app/gfx/font.h" #include "app/gfx/font.h"
#include "app/l10n_util_mac.h"
#include "app/resource_bundle.h" #include "app/resource_bundle.h"
#include "base/clipboard.h" #include "base/clipboard.h"
#import "base/cocoa_protocols_mac.h" #import "base/cocoa_protocols_mac.h"
...@@ -18,6 +19,7 @@ ...@@ -18,6 +19,7 @@
#include "chrome/browser/cocoa/autocomplete_text_field.h" #include "chrome/browser/cocoa/autocomplete_text_field.h"
#include "chrome/browser/cocoa/event_utils.h" #include "chrome/browser/cocoa/event_utils.h"
#include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tab_contents/tab_contents.h"
#include "grit/generated_resources.h"
// Focus-handling between |field_| and |model_| is a bit subtle. // Focus-handling between |field_| and |model_| is a bit subtle.
// Other platforms detect change of focus, which is inconvenient // Other platforms detect change of focus, which is inconvenient
...@@ -606,6 +608,27 @@ void AutocompleteEditViewMac::OnPaste() { ...@@ -606,6 +608,27 @@ void AutocompleteEditViewMac::OnPaste() {
OnAfterPossibleChange(); OnAfterPossibleChange();
} }
bool AutocompleteEditViewMac::CanPasteAndGo() {
return
model_->CanPasteAndGo(GetClipboardText(g_browser_process->clipboard()));
}
int AutocompleteEditViewMac::GetPasteActionStringId() {
DCHECK(CanPasteAndGo());
// Use PASTE_AND_SEARCH as the default fallback (although the DCHECK above
// should never trigger).
if (!model_->is_paste_and_search())
return IDS_PASTE_AND_GO;
else
return IDS_PASTE_AND_SEARCH;
}
void AutocompleteEditViewMac::OnPasteAndGo() {
if (CanPasteAndGo())
model_->PasteAndGo();
}
bool AutocompleteEditViewMac::OnTabPressed() { bool AutocompleteEditViewMac::OnTabPressed() {
if (model_->is_keyword_hint() && !model_->keyword().empty()) { if (model_->is_keyword_hint() && !model_->keyword().empty()) {
model_->AcceptKeyword(); model_->AcceptKeyword();
...@@ -836,6 +859,18 @@ std::wstring AutocompleteEditViewMac::GetClipboardText(Clipboard* clipboard) { ...@@ -836,6 +859,18 @@ std::wstring AutocompleteEditViewMac::GetClipboardText(Clipboard* clipboard) {
return NO; return NO;
} }
- (NSString*)control:(NSControl*)control
textPasteActionString:(NSText*)fieldEditor {
if (!edit_view_->CanPasteAndGo())
return nil;
return l10n_util::GetNSStringWithFixup(edit_view_->GetPasteActionStringId());
}
- (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor {
edit_view_->OnPasteAndGo();
}
// Signal that we've lost focus when the window resigns key. // Signal that we've lost focus when the window resigns key.
- (void)windowDidResignKey:(NSNotification*)notification { - (void)windowDidResignKey:(NSNotification*)notification {
edit_view_->OnDidResignKey(); edit_view_->OnDidResignKey();
......
...@@ -33,6 +33,11 @@ ...@@ -33,6 +33,11 @@
// edited. See AutocompleteTextFieldEditor implementation. // edited. See AutocompleteTextFieldEditor implementation.
- (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor; - (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor;
// Returns nil if paste actions are not supported.
- (NSString*)control:(NSControl*)control
textPasteActionString:(NSText*)fieldEditor;
- (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor;
// Let the delegate track -flagsChanged: events. // Let the delegate track -flagsChanged: events.
- (void)control:(NSControl*)control flagsChanged:(NSEvent*)theEvent; - (void)control:(NSControl*)control flagsChanged:(NSEvent*)theEvent;
......
...@@ -25,6 +25,21 @@ ...@@ -25,6 +25,21 @@
return YES; return YES;
} }
- (NSString*)textPasteActionString:(NSText*)fieldEditor {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(control:textPasteActionString:)]) {
return [delegate control:self textPasteActionString:fieldEditor];
}
return nil;
}
- (void)textDidPasteAndGo:(NSText*)fieldEditor {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(control:textDidPasteAndGo:)]) {
[delegate control:self textDidPasteAndGo:fieldEditor];
}
}
- (void)flagsChanged:(NSEvent*)theEvent { - (void)flagsChanged:(NSEvent*)theEvent {
id delegate = [self delegate]; id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(control:flagsChanged:)]) { if ([delegate respondsToSelector:@selector(control:flagsChanged:)]) {
......
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
// (or handle it internally) by returning NO. // (or handle it internally) by returning NO.
- (BOOL)textShouldPaste:(NSText*)fieldEditor; - (BOOL)textShouldPaste:(NSText*)fieldEditor;
// Returns nil if paste actions are not supported.
- (NSString*)textPasteActionString:(NSText*)fieldEditor;
- (void)textDidPasteAndGo:(NSText*)fieldEditor;
@end @end
// Field editor used for the autocomplete field. // Field editor used for the autocomplete field.
......
...@@ -4,8 +4,9 @@ ...@@ -4,8 +4,9 @@
#import "chrome/browser/cocoa/autocomplete_text_field_editor.h" #import "chrome/browser/cocoa/autocomplete_text_field_editor.h"
#include "app/l10n_util_mac.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/sys_string_conversions.h" #include "grit/generated_resources.h"
@implementation AutocompleteTextFieldEditor @implementation AutocompleteTextFieldEditor
...@@ -45,6 +46,12 @@ ...@@ -45,6 +46,12 @@
} }
} }
- (void)pasteAndGo:sender {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(textDidPasteAndGo:)])
[delegate textDidPasteAndGo:self];
}
// We have rich text, but it shouldn't be modified by the user, so // We have rich text, but it shouldn't be modified by the user, so
// don't update the font panel. In theory, -setUsesFontPanel: should // don't update the font panel. In theory, -setUsesFontPanel: should
// accomplish this, but that gets called frequently with YES when // accomplish this, but that gets called frequently with YES when
...@@ -58,4 +65,32 @@ ...@@ -58,4 +65,32 @@
- (void)updateRuler { - (void)updateRuler {
} }
- (NSMenu*)menuForEvent:(NSEvent*)event {
NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease];
[menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT)
action:@selector(cut:)
keyEquivalent:@"" atIndex:0];
[menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY)
action:@selector(copy:)
keyEquivalent:@"" atIndex:1];
[menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE)
action:@selector(paste:)
keyEquivalent:@"" atIndex:2];
// Paste and go/search.
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(textPasteActionString:)]) {
NSString* label = [delegate textPasteActionString:self];
// TODO(rohitrao): If the clipboard is empty, should we show a greyed-out
// "Paste and Go" or nothing at all?
if (label) {
[menu insertItemWithTitle:label action:@selector(pasteAndGo:)
keyEquivalent:@"" atIndex:3];
}
}
return menu;
}
@end @end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment