summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-01 21:03:08 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-01 21:03:08 +0000
commit8bbe7efe110336916c819fdc7f5566afa3a97495 (patch)
tree6d473efd35ca0f48584dd3254e8b4e5ebd708fbb
parentd294668d4c62e94c342cff58b1096af8106113ef (diff)
downloadchromium_src-8bbe7efe110336916c819fdc7f5566afa3a97495.zip
chromium_src-8bbe7efe110336916c819fdc7f5566afa3a97495.tar.gz
chromium_src-8bbe7efe110336916c819fdc7f5566afa3a97495.tar.bz2
Enable the X selection clipboard for copying html and text out of a linux test shell.
I implemented it in the webview delegate, so when we switch to the multiprocess architecture, we will need to find out if the clipboard callback can be async (the current method would require a sync call from browser to renderer to get the selected text). Review URL: http://codereview.chromium.org/12700 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6160 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--webkit/glue/editor_client_impl.cc9
-rw-r--r--webkit/glue/webframe.h4
-rw-r--r--webkit/glue/webframe_impl.cc10
-rw-r--r--webkit/glue/webframe_impl.h1
-rw-r--r--webkit/glue/webview_delegate.h2
-rw-r--r--webkit/tools/test_shell/mac/test_webview_delegate.mm4
-rw-r--r--webkit/tools/test_shell/test_webview_delegate.cc3
-rw-r--r--webkit/tools/test_shell/test_webview_delegate.h3
-rw-r--r--webkit/tools/test_shell/test_webview_delegate_gtk.cc56
-rw-r--r--webkit/tools/test_shell/test_webview_delegate_win.cc4
10 files changed, 90 insertions, 6 deletions
diff --git a/webkit/glue/editor_client_impl.cc b/webkit/glue/editor_client_impl.cc
index 4bea274..d6b2e2c 100644
--- a/webkit/glue/editor_client_impl.cc
+++ b/webkit/glue/editor_client_impl.cc
@@ -15,6 +15,7 @@ MSVC_PUSH_WARNING_LEVEL(0);
#include "Editor.h"
#include "EventHandler.h"
#include "EventNames.h"
+#include "Frame.h"
#include "KeyboardCodes.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
@@ -235,8 +236,11 @@ void EditorClientImpl::didBeginEditing() {
void EditorClientImpl::respondToChangedSelection() {
if (use_editor_delegate_) {
WebViewDelegate* d = web_view_->delegate();
- if (d)
- d->DidChangeSelection();
+ if (d) {
+ WebCore::Frame* frame = web_view_->GetFocusedWebCoreFrame();
+ if (frame)
+ d->DidChangeSelection(!frame->selection()->isRange());
+ }
}
}
@@ -844,4 +848,3 @@ std::wstring EditorClientImpl::Describe(WebCore::CSSStyleDeclaration* style) {
// an example. But because none of them use it, it's not yet important.
return std::wstring();
}
-
diff --git a/webkit/glue/webframe.h b/webkit/glue/webframe.h
index 05fd9327..7c6c1fc 100644
--- a/webkit/glue/webframe.h
+++ b/webkit/glue/webframe.h
@@ -272,6 +272,10 @@ class WebFrame : public base::RefCounted<WebFrame> {
// Clear any text selection in the frame.
virtual void ClearSelection() = 0;
+ // Returns the selected text if there is any. If |as_html| is true, returns
+ // the selection as HTML. The return value is encoded in utf-8.
+ virtual std::string GetSelection(bool as_html) = 0;
+
// Paints the contents of this web view in a bitmapped image. This image
// will not have plugins drawn. Devices are cheap to copy because the data is
// internally refcounted so we allocate and return a new copy
diff --git a/webkit/glue/webframe_impl.cc b/webkit/glue/webframe_impl.cc
index 70cbc61..78ac3d4 100644
--- a/webkit/glue/webframe_impl.cc
+++ b/webkit/glue/webframe_impl.cc
@@ -1340,6 +1340,16 @@ void WebFrameImpl::ClearSelection() {
frame()->selection()->clear();
}
+std::string WebFrameImpl::GetSelection(bool as_html) {
+ RefPtr<Range> range = frame()->selection()->toRange();
+ if (as_html) {
+ String markup = WebCore::createMarkup(range.get(), 0);
+ return webkit_glue::StringToStdString(markup);
+ } else {
+ return webkit_glue::StringToStdString(range->text());
+ }
+}
+
void WebFrameImpl::CreateFrameView() {
ASSERT(frame_); // If frame_ doesn't exist, we probably didn't init properly.
diff --git a/webkit/glue/webframe_impl.h b/webkit/glue/webframe_impl.h
index 320a114..61c9851 100644
--- a/webkit/glue/webframe_impl.h
+++ b/webkit/glue/webframe_impl.h
@@ -140,6 +140,7 @@ class WebFrameImpl : public WebFrame {
virtual void Undo();
virtual void Redo();
virtual void ClearSelection();
+ virtual std::string GetSelection(bool as_html);
virtual void SetInViewSourceMode(bool enable);
diff --git a/webkit/glue/webview_delegate.h b/webkit/glue/webview_delegate.h
index dd6ba52..1987999 100644
--- a/webkit/glue/webview_delegate.h
+++ b/webkit/glue/webview_delegate.h
@@ -634,7 +634,7 @@ class WebViewDelegate : virtual public WebWidgetDelegate {
return false;
}
virtual void DidBeginEditing() { }
- virtual void DidChangeSelection() { }
+ virtual void DidChangeSelection(bool is_empty_selection) { }
virtual void DidChangeContents() { }
virtual void DidEndEditing() { }
diff --git a/webkit/tools/test_shell/mac/test_webview_delegate.mm b/webkit/tools/test_shell/mac/test_webview_delegate.mm
index 3672f05..ee23e80 100644
--- a/webkit/tools/test_shell/mac/test_webview_delegate.mm
+++ b/webkit/tools/test_shell/mac/test_webview_delegate.mm
@@ -127,6 +127,10 @@ void TestWebViewDelegate::RunModal(WebWidget* webwidget) {
NOTIMPLEMENTED();
}
+void TestWebViewDelegate::UpdateSelectionClipboard(bool is_empty_selection) {
+ // No selection clipboard on mac, do nothing.
+}
+
// Private methods -----------------------------------------------------------
void TestWebViewDelegate::SetPageTitle(const std::wstring& title) {
diff --git a/webkit/tools/test_shell/test_webview_delegate.cc b/webkit/tools/test_shell/test_webview_delegate.cc
index e6166b4..905b9be 100644
--- a/webkit/tools/test_shell/test_webview_delegate.cc
+++ b/webkit/tools/test_shell/test_webview_delegate.cc
@@ -552,11 +552,12 @@ void TestWebViewDelegate::DidBeginEditing() {
}
}
-void TestWebViewDelegate::DidChangeSelection() {
+void TestWebViewDelegate::DidChangeSelection(bool is_empty_selection) {
if (shell_->ShouldDumpEditingCallbacks()) {
printf("EDITING DELEGATE: "
"webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
}
+ UpdateSelectionClipboard(is_empty_selection);
}
void TestWebViewDelegate::DidChangeContents() {
diff --git a/webkit/tools/test_shell/test_webview_delegate.h b/webkit/tools/test_shell/test_webview_delegate.h
index df214bd..bc8d226 100644
--- a/webkit/tools/test_shell/test_webview_delegate.h
+++ b/webkit/tools/test_shell/test_webview_delegate.h
@@ -176,7 +176,7 @@ class TestWebViewDelegate : public base::RefCounted<TestWebViewDelegate>,
std::wstring range);
virtual bool SmartInsertDeleteEnabled();
virtual void DidBeginEditing();
- virtual void DidChangeSelection();
+ virtual void DidChangeSelection(bool is_empty_selection);
virtual void DidChangeContents();
virtual void DidEndEditing();
@@ -269,6 +269,7 @@ class TestWebViewDelegate : public base::RefCounted<TestWebViewDelegate>,
void UpdateForCommittedLoad(WebFrame* webframe, bool is_new_navigation);
void UpdateURL(WebFrame* frame);
void UpdateSessionHistory(WebFrame* frame);
+ void UpdateSelectionClipboard(bool is_empty_selection);
// Get a string suitable for dumping a frame to the console.
std::wstring GetFrameDescription(WebFrame* webframe);
diff --git a/webkit/tools/test_shell/test_webview_delegate_gtk.cc b/webkit/tools/test_shell/test_webview_delegate_gtk.cc
index 0874429..3866e3d 100644
--- a/webkit/tools/test_shell/test_webview_delegate_gtk.cc
+++ b/webkit/tools/test_shell/test_webview_delegate_gtk.cc
@@ -26,6 +26,42 @@
#include "webkit/tools/test_shell/test_navigation_controller.h"
#include "webkit/tools/test_shell/test_shell.h"
+namespace {
+
+
+enum SelectionClipboardType {
+ TEXT_HTML,
+ PLAIN_TEXT,
+};
+
+GdkAtom GetTextHtmlAtom() {
+ GdkAtom kTextHtmlGdkAtom = gdk_atom_intern_static_string("text/html");
+ return kTextHtmlGdkAtom;
+}
+
+void SelectionClipboardGetContents(GtkClipboard* clipboard,
+ GtkSelectionData* selection_data, guint info, gpointer data) {
+ // Ignore formats that we don't know about.
+ if (info != TEXT_HTML && info != PLAIN_TEXT)
+ return;
+
+ WebView* webview = static_cast<WebView*>(data);
+ WebFrame* frame = webview->GetFocusedFrame();
+ std::string selection = frame->GetSelection(TEXT_HTML == info);
+ if (TEXT_HTML == info) {
+ gtk_selection_data_set(selection_data,
+ GetTextHtmlAtom(),
+ 8 /* bits per data unit, ie, char */,
+ reinterpret_cast<const guchar*>(selection.data()),
+ selection.length());
+ } else {
+ gtk_selection_data_set_text(selection_data, selection.data(),
+ selection.length());
+ }
+}
+
+} // namespace
+
// WebViewDelegate -----------------------------------------------------------
TestWebViewDelegate::~TestWebViewDelegate() {
@@ -150,6 +186,26 @@ void TestWebViewDelegate::RunModal(WebWidget* webwidget) {
NOTIMPLEMENTED();
}
+void TestWebViewDelegate::UpdateSelectionClipboard(bool is_empty_selection) {
+ if (is_empty_selection)
+ return;
+
+ GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+ // Put data on the X clipboard. This doesn't actually grab the text from
+ // the HTML, it just registers a callback for when someone tries to paste.
+ GtkTargetList* target_list = gtk_target_list_new(NULL, 0);
+ gtk_target_list_add(target_list, GetTextHtmlAtom(), 0, TEXT_HTML);
+ gtk_target_list_add_text_targets(target_list, PLAIN_TEXT);
+
+ gint num_targets = 0;
+ GtkTargetEntry* targets = gtk_target_table_new_from_list(target_list,
+ &num_targets);
+ gtk_clipboard_set_with_data(clipboard, targets, num_targets,
+ SelectionClipboardGetContents, NULL,
+ shell_->webView());
+ gtk_target_table_free(targets, num_targets);
+}
+
// Private methods -----------------------------------------------------------
void TestWebViewDelegate::SetPageTitle(const std::wstring& title) {
diff --git a/webkit/tools/test_shell/test_webview_delegate_win.cc b/webkit/tools/test_shell/test_webview_delegate_win.cc
index db51893..64b1e17 100644
--- a/webkit/tools/test_shell/test_webview_delegate_win.cc
+++ b/webkit/tools/test_shell/test_webview_delegate_win.cc
@@ -154,6 +154,10 @@ void TestWebViewDelegate::RunModal(WebWidget* webwidget) {
EnableWindow(*i, TRUE);
}
+void TestWebViewDelegate::UpdateSelectionClipboard(bool is_empty_selection) {
+ // No selection clipboard on windows, do nothing.
+}
+
// Private methods -----------------------------------------------------------
void TestWebViewDelegate::SetPageTitle(const std::wstring& title) {