2008-02-04 Alp Toker <alp@atoker.com>
[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     gchar* text = g_strdup(frame->selectedText().utf8().data());
106     gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
107     PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);
108
109     gint n_targets;
110     GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
111     GtkClipboard* clipboard = m_helper->getClipboard(frame);
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     gtk_clipboard_set_text(clipboard, url.string().utf8().data(), url.string().utf8().length());
124 }
125
126 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
127 {
128     // TODO: Enable this when Image gets GdkPixbuf support
129
130     /*
131     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
132
133     ASSERT(node && node->renderer() && node->renderer()->isImage());
134     RenderImage* renderer = static_cast<RenderImage*>(node->renderer());
135     CachedImage* cachedImage = static_cast<CachedImage*>(renderer->cachedImage());
136     ASSERT(cachedImage);
137     Image* image = cachedImage->image();
138     ASSERT(image);
139
140     gtk_clipboard_set_image(clipboard, image->pixbuf());
141     */
142
143     notImplemented();
144 }
145
146 void Pasteboard::clear()
147 {
148     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
149
150     gtk_clipboard_clear(clipboard);
151 }
152
153 bool Pasteboard::canSmartReplace()
154 {
155     notImplemented();
156     return false;
157 }
158
159 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
160                                                           bool allowPlainText, bool& chosePlainText)
161 {
162 #if GTK_CHECK_VERSION(2,10,0)
163     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
164 #else
165     GdkAtom textHtml = gdk_atom_intern("text/html", false);
166 #endif
167     GtkClipboard* clipboard = m_helper->getClipboard(frame);
168     chosePlainText = false;
169
170     if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
171         ASSERT(data->data);
172         String html = String::fromUTF8(reinterpret_cast<gchar*>(data->data), data->length * data->format / 8);
173         gtk_selection_data_free(data);
174
175         if (!html.isEmpty()) {
176             RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, "");
177             if (fragment)
178                 return fragment.release();
179         }
180     }
181
182     if (!allowPlainText)
183         return 0;
184
185     if (gchar* utf8 = gtk_clipboard_wait_for_text(clipboard)) {
186         String text = String::fromUTF8(utf8);
187         g_free(utf8);
188
189         chosePlainText = true;
190         RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), text);
191         if (fragment)
192             return fragment.release();
193     }
194
195     return 0;
196 }
197
198 String Pasteboard::plainText(Frame* frame)
199 {
200     GtkClipboard* clipboard = m_helper->getClipboard(frame);
201
202     gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
203
204     if (!utf8)
205         return String();
206
207     String text = String::fromUTF8(utf8);
208     g_free(utf8);
209
210     return text;
211 }
212
213 }