Omnibox should accept and navigate URLs dropped using drag and drop.

BUG=157690


Review URL: https://chromiumcodereview.appspot.com/11416327

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171125 0039d316-1c4b-4281-b951-d872f2087c98
parent 55e43ef0
...@@ -779,6 +779,27 @@ void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) { ...@@ -779,6 +779,27 @@ void OmniboxViewViews::OnWriteDragData(ui::OSExchangeData* data) {
data->SetURL(url, selected_text); data->SetURL(url, selected_text);
} }
void OmniboxViewViews::AppendDropFormats(
int* formats,
std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
*formats = *formats | ui::OSExchangeData::URL;
}
int OmniboxViewViews::OnDrop(const ui::OSExchangeData& data) {
if (data.HasURL()) {
GURL url;
string16 title;
if (data.GetURLAndTitle(&url, &title)) {
string16 text(StripJavascriptSchemas(UTF8ToUTF16(url.spec())));
if (model()->CanPasteAndGo(text)) {
model()->PasteAndGo(text);
return ui::DragDropTypes::DRAG_COPY;
}
}
}
return ui::DragDropTypes::DRAG_NONE;
}
void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) { void OmniboxViewViews::UpdateContextMenu(ui::SimpleMenuModel* menu_contents) {
// Minor note: We use IDC_ for command id here while the underlying textfield // Minor note: We use IDC_ for command id here while the underlying textfield
// is using IDS_ for all its command ids. This is because views cannot depend // is using IDS_ for all its command ids. This is because views cannot depend
......
...@@ -140,6 +140,10 @@ class OmniboxViewViews ...@@ -140,6 +140,10 @@ class OmniboxViewViews
virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE; virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE;
virtual void OnAfterCutOrCopy() OVERRIDE; virtual void OnAfterCutOrCopy() OVERRIDE;
virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE; virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE;
virtual void AppendDropFormats(
int* formats,
std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
virtual int OnDrop(const ui::OSExchangeData& data) OVERRIDE;
virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE; virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE;
virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
......
...@@ -196,11 +196,18 @@ bool NativeTextfieldViews::GetDropFormats( ...@@ -196,11 +196,18 @@ bool NativeTextfieldViews::GetDropFormats(
return false; return false;
// TODO(msw): Can we support URL, FILENAME, etc.? // TODO(msw): Can we support URL, FILENAME, etc.?
*formats = ui::OSExchangeData::STRING; *formats = ui::OSExchangeData::STRING;
TextfieldController* controller = textfield_->GetController();
if (controller)
controller->AppendDropFormats(formats, custom_formats);
return true; return true;
} }
bool NativeTextfieldViews::CanDrop(const OSExchangeData& data) { bool NativeTextfieldViews::CanDrop(const OSExchangeData& data) {
return textfield_->enabled() && !textfield_->read_only() && data.HasString(); int formats;
std::set<OSExchangeData::CustomFormat> custom_formats;
GetDropFormats(&formats, &custom_formats);
return textfield_->enabled() && !textfield_->read_only() &&
data.HasAnyFormat(formats, custom_formats);
} }
int NativeTextfieldViews::OnDragUpdated(const ui::DropTargetEvent& event) { int NativeTextfieldViews::OnDragUpdated(const ui::DropTargetEvent& event) {
...@@ -222,6 +229,14 @@ int NativeTextfieldViews::OnDragUpdated(const ui::DropTargetEvent& event) { ...@@ -222,6 +229,14 @@ int NativeTextfieldViews::OnDragUpdated(const ui::DropTargetEvent& event) {
int NativeTextfieldViews::OnPerformDrop(const ui::DropTargetEvent& event) { int NativeTextfieldViews::OnPerformDrop(const ui::DropTargetEvent& event) {
DCHECK(CanDrop(event.data())); DCHECK(CanDrop(event.data()));
TextfieldController* controller = textfield_->GetController();
if (controller) {
int drag_operation = controller->OnDrop(event.data());
if (drag_operation != ui::DragDropTypes::DRAG_NONE)
return drag_operation;
}
DCHECK(!initiating_drag_ || DCHECK(!initiating_drag_ ||
!GetRenderText()->IsPointInSelection(event.location())); !GetRenderText()->IsPointInSelection(event.location()));
OnBeforeUserAction(); OnBeforeUserAction();
......
...@@ -4,8 +4,14 @@ ...@@ -4,8 +4,14 @@
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/base/dragdrop/drag_drop_types.h"
namespace views { namespace views {
int TextfieldController::OnDrop(const ui::OSExchangeData& data) {
return ui::DragDropTypes::DRAG_NONE;
}
bool TextfieldController::IsCommandIdEnabled(int command_id) const { bool TextfieldController::IsCommandIdEnabled(int command_id) const {
return false; return false;
} }
......
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
#ifndef UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_ #ifndef UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_
#define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_ #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_CONTROLLER_H_
#include <set>
#include "base/string16.h" #include "base/string16.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/views/views_export.h" #include "ui/views/views_export.h"
namespace ui { namespace ui {
class KeyEvent; class KeyEvent;
class OSExchangeData;
class SimpleMenuModel; class SimpleMenuModel;
} // namespace ui } // namespace ui
...@@ -49,6 +51,17 @@ class VIEWS_EXPORT TextfieldController { ...@@ -49,6 +51,17 @@ class VIEWS_EXPORT TextfieldController {
// chance to modify the drag data. // chance to modify the drag data.
virtual void OnWriteDragData(ui::OSExchangeData* data) {} virtual void OnWriteDragData(ui::OSExchangeData* data) {}
// Enables the controller to append to the accepted drop formats.
virtual void AppendDropFormats(
int* formats,
std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {}
// Called when a drop of dragged data happens on the textfield. This method is
// called before regular handling of the drop. If this returns a drag
// operation other than |ui::DragDropTypes::DRAG_NONE|, regular handling is
// skipped.
virtual int OnDrop(const ui::OSExchangeData& data);
// Gives the controller a chance to modify the context menu contents. // Gives the controller a chance to modify the context menu contents.
virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) {} virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment