Commit 63097929 authored by alp@webkit.org's avatar alp@webkit.org

2007-12-22 Luca Bruno <lethalman88@gmail.com>

        Reviewed by Alp Toker.

        http://bugs.webkit.org/show_bug.cgi?id=16311
        [Gtk] Copy rich text to clipboard as text/plain and text/html.

        Add copy and paste targets for WebView and implement
        WebCore::PasteboardHelper.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@28963 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent ac9f98e2
2007-12-22 Luca Bruno <lethalman88@gmail.com>
Reviewed by Alp Toker.
http://bugs.webkit.org/show_bug.cgi?id=16311
[Gtk] Copy rich text to clipboard as text/plain and text/html.
Hook into the WebView to get the correct clipboard object on the
appropriate display. This is necessary because GTK+ does not have
global clipboards.
* WebCore.pro:
* platform/Pasteboard.h:
(WebCore::Pasteboard::setHelper): added for GTK
(WebCore::Pasteboard::m_helper): private added for GTK
* platform/gtk/PasteboardGtk.cpp:
(WebCore::PasteboardSelectionData): added to support GTK selection data callback
(WebCore::clipboard_get_contents_cb): added
(WebCore::clipboard_clear_contents_cb): added
(WebCore::Pasteboard::~Pasteboard):
(WebCore::Pasteboard::setHelper):
(WebCore::Pasteboard::writeSelection): copy rich text
(WebCore::Pasteboard::documentFragment):
(WebCore::Pasteboard::plainText):
* platform/gtk/PasteboardHelper.h: Added.
2007-12-22 Nikolas Zimmermann <zimmermann@kde.org>
Reviewed by Oliver.
......
......@@ -955,6 +955,7 @@ qt-port {
gtk-port {
HEADERS += \
../WebCore/platform/gtk/ClipboardGtk.h \
../WebCore/platform/gtk/PasteboardHelper.h \
../WebKit/gtk/WebView/webkit.h \
../WebKit/gtk/WebView/webkitdefines.h \
../WebKit/gtk/WebView/webkitnetworkrequest.h \
......@@ -967,7 +968,8 @@ gtk-port {
../WebKit/gtk/WebCoreSupport/DragClientGtk.h \
../WebKit/gtk/WebCoreSupport/EditorClientGtk.h \
../WebKit/gtk/WebCoreSupport/FrameLoaderClientGtk.h \
../WebKit/gtk/WebCoreSupport/InspectorClientGtk.h
../WebKit/gtk/WebCoreSupport/InspectorClientGtk.h \
../WebKit/gtk/WebCoreSupport/PasteboardHelperGtk.h
SOURCES += \
platform/graphics/StringTruncator.cpp \
platform/text/TextCodecICU.cpp \
......@@ -1042,7 +1044,8 @@ gtk-port {
../WebKit/gtk/WebCoreSupport/DragClientGtk.cpp \
../WebKit/gtk/WebCoreSupport/EditorClientGtk.cpp \
../WebKit/gtk/WebCoreSupport/FrameLoaderClientGtk.cpp \
../WebKit/gtk/WebCoreSupport/InspectorClientGtk.cpp
../WebKit/gtk/WebCoreSupport/InspectorClientGtk.cpp \
../WebKit/gtk/WebCoreSupport/PasteboardHelperGtk.cpp
}
contains(DEFINES, ENABLE_DATABASE=1) {
......
......@@ -34,6 +34,10 @@
#include <wtf/RetainPtr.h>
#endif
#if PLATFORM(GTK)
#include <PasteboardHelper.h>
#endif
// FIXME: This class is too high-level to be in the platform directory, since it
// uses the DOM and makes calls to Editor. It should either be divested of its
// knowledge of the frame and editor or moved into the editing directory.
......@@ -88,6 +92,10 @@ public:
PassRefPtr<DocumentFragment> documentFragment(Frame*, PassRefPtr<Range>, bool allowPlainText, bool& chosePlainText);
String plainText(Frame* = 0);
#if PLATFORM(GTK)
void setHelper(PasteboardHelper*);
#endif
private:
Pasteboard();
~Pasteboard();
......@@ -100,6 +108,11 @@ private:
#if PLATFORM(WIN)
HWND m_owner;
#endif
#if PLATFORM(GTK)
PasteboardHelper* m_helper;
#endif
};
} // namespace WebCore
......
......@@ -34,6 +34,51 @@
namespace WebCore {
/* FIXME: we must get rid of this and use the enum in webkitwebview.h someway */
typedef enum
{
WEBKIT_WEB_VIEW_TARGET_INFO_HTML = - 1,
WEBKIT_WEB_VIEW_TARGET_INFO_TEXT = - 2
} WebKitWebViewTargetInfo;
class PasteboardSelectionData {
public:
PasteboardSelectionData(gchar* text, gchar* markup)
: m_text(text)
, m_markup(markup) { }
~PasteboardSelectionData() {
g_free(m_text);
g_free(m_markup);
}
const gchar* text() const { return m_text; }
const gchar* markup() const { return m_markup; }
private:
gchar* m_text;
gchar* m_markup;
};
static void clipboard_get_contents_cb(GtkClipboard *clipboard, GtkSelectionData *selection_data,
guint info, gpointer data) {
PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
ASSERT(clipboardData);
if ((gint)info == WEBKIT_WEB_VIEW_TARGET_INFO_HTML) {
gtk_selection_data_set(selection_data, selection_data->target, 8,
reinterpret_cast<const guchar*>(clipboardData->markup()),
g_utf8_strlen(clipboardData->markup(), -1));
} else
gtk_selection_data_set_text(selection_data, clipboardData->text(), -1);
}
static void clipboard_clear_contents_cb(GtkClipboard *clipboard, gpointer data) {
PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
ASSERT(clipboardData);
delete clipboardData;
}
Pasteboard* Pasteboard::generalPasteboard()
{
static Pasteboard* pasteboard = new Pasteboard();
......@@ -47,15 +92,26 @@ Pasteboard::Pasteboard()
Pasteboard::~Pasteboard()
{
notImplemented();
delete m_helper;
}
void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
void Pasteboard::setHelper(PasteboardHelper* helper)
{
GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
m_helper = helper;
}
String text = frame->selectedText();
gtk_clipboard_set_text(clipboard, text.utf8().data(), text.utf8().length());
void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
{
gchar* text = g_strdup(frame->selectedText().utf8().data());
gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);
gint n_targets;
GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
GtkClipboard* clipboard = m_helper->getClipboard(frame);
gtk_clipboard_set_with_data(clipboard, targets, n_targets,
clipboard_get_contents_cb, clipboard_clear_contents_cb, data);
gtk_target_table_free(targets, n_targets);
}
void Pasteboard::writeURL(const KURL&, const String&, Frame*)
......@@ -104,7 +160,7 @@ PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefP
#else
GdkAtom textHtml = gdk_atom_intern("text/html", false);
#endif
GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
GtkClipboard* clipboard = m_helper->getClipboard(frame);
chosePlainText = false;
if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
......@@ -135,9 +191,9 @@ PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefP
return 0;
}
String Pasteboard::plainText(Frame*)
String Pasteboard::plainText(Frame* frame)
{
GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
GtkClipboard* clipboard = m_helper->getClipboard(frame);
gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
......@@ -149,4 +205,5 @@ String Pasteboard::plainText(Frame*)
return text;
}
}
/*
* Copyright (C) 2007 Luca Bruno <lethalman88@gmail.com>
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef PasteboardHelper_h
#define PasteboardHelper_h
/*
* FIXME: this is for WebCore support and must be removed once
* a better solution is found
*/
#include "Frame.h"
#include <gtk/gtk.h>
namespace WebCore {
class PasteboardHelper {
public:
virtual ~PasteboardHelper() {};
virtual GtkClipboard* getClipboard(Frame*) const = 0;
virtual GtkTargetList* getCopyTargetList(Frame*) const = 0;
virtual GtkTargetList* getPasteTargetList(Frame*) const = 0;
};
}
#endif // PasteboardHelper_h
2007-12-22 Luca Bruno <lethalman88@gmail.com>
Reviewed by Alp Toker.
http://bugs.webkit.org/show_bug.cgi?id=16311
[Gtk] Copy rich text to clipboard as text/plain and text/html.
Add copy and paste targets for WebView and implement
WebCore::PasteboardHelper.
* WebCoreSupport/PasteboardHelperGtk.cpp: Added.
(WebKit::PasteboardHelperGtk::getClipboard):
(WebKit::PasteboardHelperGtk::getCopyTargetList):
(WebKit::PasteboardHelperGtk::getPasteTargetList):
* WebCoreSupport/PasteboardHelperGtk.h: Added.
* WebView/webkitprivate.cpp:
(webkit_init): set the PasteboardHelperGtk as WebCore::Pasteboard helper
* WebView/webkitprivate.h:
(_WebKitWebViewPrivate::copy_target_list): added
(_WebKitWebViewPrivate::paste_target_list): added
* WebView/webkitwebview.cpp:
(webkit_web_view_get_property): added
(webkit_web_view_finalize):
(webkit_web_view_class_init): create properties for COPY_TARGET and PASTE_TARGET and create the copy and paste target lists
(webkit_web_view_get_copy_target_list): added
(webkit_web_view_get_paste_target_list): added
* WebView/webkitwebview.h:
(webkit_web_view_get_copy_target_list): added
(webkit_web_view_get_paste_target_list): added
2007-12-22 Xan Lopez <xan@gnome.org>
Reviewed by Alp Toker.
......
/*
* Copyright (C) 2007 Luca Bruno <lethalman88@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Frame.h"
#include "PasteboardHelperGtk.h"
#include "webkitwebframe.h"
#include "webkitwebview.h"
#include "webkitprivate.h"
#include <gtk/gtk.h>
using namespace WebCore;
namespace WebKit
{
GtkClipboard* PasteboardHelperGtk::getClipboard(Frame* frame) const {
WebKitWebView* webView = webkit_web_frame_get_web_view(kit(frame));
return gtk_widget_get_clipboard(GTK_WIDGET (webView),
GDK_SELECTION_CLIPBOARD);
}
GtkTargetList* PasteboardHelperGtk::getCopyTargetList(Frame* frame) const {
WebKitWebView* webView = webkit_web_frame_get_web_view(kit(frame));
return webkit_web_view_get_copy_target_list(webView);
}
GtkTargetList* PasteboardHelperGtk::getPasteTargetList(Frame* frame) const {
WebKitWebView* webView = webkit_web_frame_get_web_view(kit(frame));
return webkit_web_view_get_paste_target_list(webView);
}
}
/*
* Copyright (C) 2007 Luca Bruno <lethalman88@gmail.com>
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef PasteboardHelperGtk_h
#define PasteboardHelperGtk_h
/*
* FIXME: this is for WebCore support and must be removed once
* a better solution is found
*/
#include "Frame.h"
#include "PasteboardHelper.h"
#include <gtk/gtk.h>
using namespace WebCore;
namespace WebKit {
class PasteboardHelperGtk : public PasteboardHelper {
public:
PasteboardHelperGtk() { }
virtual GtkClipboard* getClipboard(Frame*) const;
virtual GtkTargetList* getCopyTargetList(Frame*) const;
virtual GtkTargetList* getPasteTargetList(Frame*) const;
};
}
#endif // PasteboardHelperGtk_h
......@@ -26,6 +26,8 @@
#include "FrameLoaderClientGtk.h"
#include "Logging.h"
#include "NotImplemented.h"
#include "Pasteboard.h"
#include "PasteboardHelperGtk.h"
using namespace WebCore;
......@@ -102,4 +104,6 @@ void webkit_init()
WebCore::DatabaseTracker::tracker().setDatabasePath(databaseDirectory);
g_free(databaseDirectory);
#endif
Pasteboard::generalPasteboard()->setHelper(new WebKit::PasteboardHelperGtk());
}
......@@ -64,6 +64,9 @@ extern "C" {
HashSet<GtkWidget*> children;
bool editable;
GtkIMContext* imContext;
GtkTargetList* copy_target_list;
GtkTargetList* paste_target_list;
};
#define WEBKIT_WEB_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_FRAME, WebKitWebFramePrivate))
......
......@@ -41,6 +41,7 @@
#include "FrameLoader.h"
#include "FrameView.h"
#include "Editor.h"
#include "PasteboardHelper.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformWheelEvent.h"
#include "SubstituteData.h"
......@@ -76,10 +77,32 @@ enum {
LAST_SIGNAL
};
enum {
PROP_0,
PROP_COPY_TARGET_LIST,
PROP_PASTE_TARGET_LIST
};
static guint webkit_web_view_signals[LAST_SIGNAL] = { 0, };
G_DEFINE_TYPE(WebKitWebView, webkit_web_view, GTK_TYPE_CONTAINER)
static void webkit_web_view_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
WebKitWebView* webView = WEBKIT_WEB_VIEW(object);
switch(prop_id) {
case PROP_COPY_TARGET_LIST:
g_value_set_boxed(value, webkit_web_view_get_copy_target_list(webView));
break;
case PROP_PASTE_TARGET_LIST:
g_value_set_boxed(value, webkit_web_view_get_paste_target_list(webView));
break;
default:
break;
}
}
static gboolean webkit_web_view_expose_event(GtkWidget* widget, GdkEventExpose* event)
{
Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
......@@ -426,6 +449,8 @@ static void webkit_web_view_finalize(GObject* object)
delete webViewData->settings;
g_object_unref(webViewData->mainFrame);
g_object_unref(webViewData->imContext);
gtk_target_list_unref(webViewData->copy_target_list);
gtk_target_list_unref(webViewData->paste_target_list);
delete webViewData->userAgent;
G_OBJECT_CLASS(webkit_web_view_parent_class)->finalize(object);
......@@ -765,6 +790,9 @@ static void webkit_web_view_class_init(WebKitWebViewClass* webViewClass)
G_OBJECT_CLASS(webViewClass)->finalize = webkit_web_view_finalize;
GObjectClass* objectClass = G_OBJECT_CLASS(webViewClass);
objectClass->get_property = webkit_web_view_get_property;
GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(webViewClass);
widgetClass->realize = webkit_web_view_realize;
widgetClass->expose_event = webkit_web_view_expose_event;
......@@ -818,6 +846,22 @@ static void webkit_web_view_class_init(WebKitWebViewClass* webViewClass)
"copy_clipboard", 0);
gtk_binding_entry_add_signal(binding_set, GDK_Insert, GDK_SHIFT_MASK,
"paste_clipboard", 0);
/* Properties */
GParamFlags flags = (GParamFlags)(G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
g_object_class_install_property(objectClass, PROP_COPY_TARGET_LIST,
g_param_spec_boxed("copy-target-list",
"Target list",
"The list of targets this Web view supports for copying to the clipboard",
GTK_TYPE_TARGET_LIST,
flags));
g_object_class_install_property(objectClass, PROP_PASTE_TARGET_LIST,
g_param_spec_boxed("paste-target-list",
"Target list",
"The list of targets this Web view supports for pasting to the clipboard",
GTK_TYPE_TARGET_LIST,
flags));
}
static void webkit_web_view_init(WebKitWebView* webView)
......@@ -843,6 +887,22 @@ static void webkit_web_view_init(WebKitWebView* webView)
GTK_WIDGET_SET_FLAGS(webView, GTK_CAN_FOCUS);
webViewData->mainFrame = WEBKIT_WEB_FRAME(webkit_web_frame_new(webView));
webViewData->editable = false;
#if GLIB_CHECK_VERSION(2,10,0)
GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
#else
GdkAtom textHtml = gdk_atom_intern("text/html", false);
#endif
/* Targets for copy */
webViewData->copy_target_list = gtk_target_list_new(NULL, 0);
gtk_target_list_add(webViewData->copy_target_list, textHtml, GTK_TARGET_OTHER_APP, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
gtk_target_list_add_text_targets(webViewData->copy_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
/* Targets for pasting */
webViewData->paste_target_list = gtk_target_list_new(NULL, 0);
gtk_target_list_add(webViewData->paste_target_list, textHtml, GTK_TARGET_OTHER_APP, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
gtk_target_list_add_text_targets(webViewData->paste_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
}
GtkWidget* webkit_web_view_new(void)
......@@ -1256,4 +1316,44 @@ void webkit_web_view_set_editable(WebKitWebView* webView, gboolean flag)
mainFrame->removeEditingStyleFromBodyElement();
}
/**
* webkit_web_view_get_copy_target_list:
* @web_view: a #WebKitWebView
*
* This function returns the list of targets this #WebKitWebView can
* provide for clipboard copying and as DND source. The targets in the list are
* added with %info values from the #WebKitWebViewTargetInfo enum,
* using gtk_target_list_add() and
* gtk_target_list_add_text_targets().
*
* Return value: the #GtkTargetList
**/
GtkTargetList* webkit_web_view_get_copy_target_list(WebKitWebView* webView)
{
g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
return webViewData->copy_target_list;
}
/**
* webkit_web_view_get_paste_target_list:
* @web_view: a #WebKitWebView
*
* This function returns the list of targets this #WebKitWebView can
* provide for clipboard pasting and as DND destination. The targets in the list are
* added with %info values from the #WebKitWebViewTargetInfo enum,
* using gtk_target_list_add() and
* gtk_target_list_add_text_targets().
*
* Return value: the #GtkTargetList
**/
GtkTargetList* webkit_web_view_get_paste_target_list(WebKitWebView* webView)
{
g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
return webViewData->paste_target_list;
}
}
......@@ -41,7 +41,11 @@ typedef enum {
WEBKIT_NAVIGATION_RESPONSE_DOWNLOAD
} WebKitNavigationResponse;
typedef enum
{
WEBKIT_WEB_VIEW_TARGET_INFO_HTML = - 1,
WEBKIT_WEB_VIEW_TARGET_INFO_TEXT = - 2
} WebKitWebViewTargetInfo;
struct _WebKitWebView {
GtkContainer parent;
......@@ -164,6 +168,12 @@ webkit_web_view_get_editable (WebKitWebView* web_view);
WEBKIT_API void
webkit_web_view_set_editable (WebKitWebView* web_view, gboolean flag);
WEBKIT_API GtkTargetList*
webkit_web_view_get_copy_target_list (WebKitWebView* webView);
WEBKIT_API GtkTargetList*
webkit_web_view_get_paste_target_list (WebKitWebView* webView);
G_END_DECLS
#endif
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