root/trunk/WebCore/platform/gtk/PasteboardGtk.cpp

Revision 30374, 6.5 KB (checked in by alp@webkit.org, 10 months ago)

2008-02-18 Alp Toker <alp@atoker.com>

Build fix for GTK+ < 2.10. Fall back to simple text clipboard copy
with older GTK+ versions for now.

  • platform/gtk/PasteboardGtk.cpp: (WebCore::Pasteboard::writeSelection):
  • Property svn:eol-style set to native
Line 
1/*
2 *  Copyright (C) 2007 Holger Hans Peter Freyther
3 *  Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include "config.h"
21#include "Pasteboard.h"
22
23#include "CString.h"
24#include "DocumentFragment.h"
25#include "Frame.h"
26#include "NotImplemented.h"
27#include "PlatformString.h"
28#include "Image.h"
29#include "RenderImage.h"
30#include "KURL.h"
31#include "markup.h"
32
33#include <gtk/gtk.h>
34
35namespace WebCore {
36
37/* FIXME: we must get rid of this and use the enum in webkitwebview.h someway */
38typedef enum
39{
40    WEBKIT_WEB_VIEW_TARGET_INFO_HTML = - 1,
41    WEBKIT_WEB_VIEW_TARGET_INFO_TEXT = - 2
42} WebKitWebViewTargetInfo;
43
44class PasteboardSelectionData {
45public:
46    PasteboardSelectionData(gchar* text, gchar* markup)
47        : m_text(text)
48        , m_markup(markup) { }
49
50    ~PasteboardSelectionData() {
51        g_free(m_text);
52        g_free(m_markup);
53    }
54
55    const gchar* text() const { return m_text; }
56    const gchar* markup() const { return m_markup; }
57
58private:
59    gchar* m_text;
60    gchar* m_markup;
61};
62
63static void clipboard_get_contents_cb(GtkClipboard *clipboard, GtkSelectionData *selection_data,
64                                      guint info, gpointer data) {
65    PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
66    ASSERT(clipboardData);
67    if ((gint)info == WEBKIT_WEB_VIEW_TARGET_INFO_HTML) {
68        gtk_selection_data_set(selection_data, selection_data->target, 8,
69                               reinterpret_cast<const guchar*>(clipboardData->markup()),
70                               g_utf8_strlen(clipboardData->markup(), -1));
71    } else
72        gtk_selection_data_set_text(selection_data, clipboardData->text(), -1);
73}
74
75static void clipboard_clear_contents_cb(GtkClipboard *clipboard, gpointer data) {
76    PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
77    ASSERT(clipboardData);
78    delete clipboardData;
79}
80
81
82Pasteboard* Pasteboard::generalPasteboard()
83{
84    static Pasteboard* pasteboard = new Pasteboard();
85    return pasteboard;
86}
87
88Pasteboard::Pasteboard()
89{
90    notImplemented();
91}
92
93Pasteboard::~Pasteboard()
94{
95    delete m_helper;
96}
97
98void Pasteboard::setHelper(PasteboardHelper* helper)
99{
100    m_helper = helper;
101}
102
103void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
104{
105    GtkClipboard* clipboard = m_helper->getClipboard(frame);
106#if GTK_CHECK_VERSION(2,10,0)
107    gchar* text = g_strdup(frame->selectedText().utf8().data());
108    gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
109    PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);
110
111    gint n_targets;
112    GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
113    gtk_clipboard_set_with_data(clipboard, targets, n_targets,
114                                clipboard_get_contents_cb, clipboard_clear_contents_cb, data);
115    gtk_target_table_free(targets, n_targets);
116#else
117    gtk_clipboard_set_text(clipboard, frame->selectedText().utf8().data(), frame->selectedText().utf8().length());
118#endif
119}
120
121void Pasteboard::writeURL(const KURL& url, const String&, Frame* frame)
122{
123    if (url.isEmpty())
124        return;
125
126    GtkClipboard* clipboard = m_helper->getClipboard(frame);
127    gtk_clipboard_set_text(clipboard, url.string().utf8().data(), url.string().utf8().length());
128}
129
130void Pasteboard::writeImage(Node* node, const KURL&, const String&)
131{
132    // TODO: Enable this when Image gets GdkPixbuf support
133
134    /*
135    GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
136
137    ASSERT(node && node->renderer() && node->renderer()->isImage());
138    RenderImage* renderer = static_cast<RenderImage*>(node->renderer());
139    CachedImage* cachedImage = static_cast<CachedImage*>(renderer->cachedImage());
140    ASSERT(cachedImage);
141    Image* image = cachedImage->image();
142    ASSERT(image);
143
144    gtk_clipboard_set_image(clipboard, image->pixbuf());
145    */
146
147    notImplemented();
148}
149
150void Pasteboard::clear()
151{
152    GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
153
154    gtk_clipboard_clear(clipboard);
155}
156
157bool Pasteboard::canSmartReplace()
158{
159    notImplemented();
160    return false;
161}
162
163PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
164                                                          bool allowPlainText, bool& chosePlainText)
165{
166#if GTK_CHECK_VERSION(2,10,0)
167    GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
168#else
169    GdkAtom textHtml = gdk_atom_intern("text/html", false);
170#endif
171    GtkClipboard* clipboard = m_helper->getClipboard(frame);
172    chosePlainText = false;
173
174    if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
175        ASSERT(data->data);
176        String html = String::fromUTF8(reinterpret_cast<gchar*>(data->data), data->length * data->format / 8);
177        gtk_selection_data_free(data);
178
179        if (!html.isEmpty()) {
180            RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, "");
181            if (fragment)
182                return fragment.release();
183        }
184    }
185
186    if (!allowPlainText)
187        return 0;
188
189    if (gchar* utf8 = gtk_clipboard_wait_for_text(clipboard)) {
190        String text = String::fromUTF8(utf8);
191        g_free(utf8);
192
193        chosePlainText = true;
194        RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), text);
195        if (fragment)
196            return fragment.release();
197    }
198
199    return 0;
200}
201
202String Pasteboard::plainText(Frame* frame)
203{
204    GtkClipboard* clipboard = m_helper->getClipboard(frame);
205
206    gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
207
208    if (!utf8)
209        return String();
210
211    String text = String::fromUTF8(utf8);
212    g_free(utf8);
213
214    return text;
215}
216
217}
Note: See TracBrowser for help on using the browser.