2009-05-20 Gustavo Noronha Silva <gustavo.noronha@collabora.co.uk>
[WebKit-https.git] / WebCore / platform / gtk / PasteboardGtk.cpp
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
35 namespace WebCore {
36
37 /* FIXME: we must get rid of this and use the enum in webkitwebview.h someway */
38 typedef enum
39 {
40     WEBKIT_WEB_VIEW_TARGET_INFO_HTML = - 1,
41     WEBKIT_WEB_VIEW_TARGET_INFO_TEXT = - 2
42 } WebKitWebViewTargetInfo;
43
44 class PasteboardSelectionData {
45 public:
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
58 private:
59     gchar* m_text;
60     gchar* m_markup;
61 };
62
63 static 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
75 static 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
82 Pasteboard* Pasteboard::generalPasteboard()
83 {
84     static Pasteboard* pasteboard = new Pasteboard();
85     return pasteboard;
86 }
87
88 Pasteboard::Pasteboard()
89 {
90     notImplemented();
91 }
92
93 Pasteboard::~Pasteboard()
94 {
95     delete m_helper;
96 }
97
98 void Pasteboard::setHelper(PasteboardHelper* helper)
99 {
100     m_helper = helper;
101 }
102
103 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
104 {
105     GtkClipboard* clipboard = m_helper->getClipboard(frame);
106     gchar* text = g_strdup(frame->selectedText().utf8().data());
107     gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
108     PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);
109
110     gint n_targets;
111     GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
112     gtk_clipboard_set_with_data(clipboard, targets, n_targets,
113                                 clipboard_get_contents_cb, clipboard_clear_contents_cb, data);
114     gtk_target_table_free(targets, n_targets);
115 }
116
117 void Pasteboard::writeURL(const KURL& url, const String&, Frame* frame)
118 {
119     if (url.isEmpty())
120         return;
121
122     GtkClipboard* clipboard = m_helper->getClipboard(frame);
123     GtkClipboard* primary = m_helper->getPrimary(frame);
124     gtk_clipboard_set_text(clipboard, url.string().utf8().data(), url.string().utf8().length());
125     gtk_clipboard_set_text(primary, url.string().utf8().data(), url.string().utf8().length());
126 }
127
128 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
129 {
130     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
131
132     ASSERT(node && node->renderer() && node->renderer()->isImage());
133     RenderImage* renderer = static_cast<RenderImage*>(node->renderer());
134     CachedImage* cachedImage = static_cast<CachedImage*>(renderer->cachedImage());
135     ASSERT(cachedImage);
136     Image* image = cachedImage->image();
137     ASSERT(image);
138
139     GdkPixbuf* pixbuf = image->getGdkPixbuf();
140     gtk_clipboard_set_image(clipboard, pixbuf);
141     g_object_unref(pixbuf);
142 }
143
144 void Pasteboard::clear()
145 {
146     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
147
148     gtk_clipboard_clear(clipboard);
149 }
150
151 bool Pasteboard::canSmartReplace()
152 {
153     notImplemented();
154     return false;
155 }
156
157 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
158                                                           bool allowPlainText, bool& chosePlainText)
159 {
160     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
161     GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
162     chosePlainText = false;
163
164     if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
165         ASSERT(data->data);
166         String html = String::fromUTF8(reinterpret_cast<gchar*>(data->data), data->length * data->format / 8);
167         gtk_selection_data_free(data);
168
169         if (!html.isEmpty()) {
170             RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, "");
171             if (fragment)
172                 return fragment.release();
173         }
174     }
175
176     if (!allowPlainText)
177         return 0;
178
179     if (gchar* utf8 = gtk_clipboard_wait_for_text(clipboard)) {
180         String text = String::fromUTF8(utf8);
181         g_free(utf8);
182
183         chosePlainText = true;
184         RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), text);
185         if (fragment)
186             return fragment.release();
187     }
188
189     return 0;
190 }
191
192 String Pasteboard::plainText(Frame* frame)
193 {
194     GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
195
196     gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
197
198     if (!utf8)
199         return String();
200
201     String text = String::fromUTF8(utf8);
202     g_free(utf8);
203
204     return text;
205 }
206
207 }