2009-07-07 Jiahua Huang <jhuangjiahua@gmail.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 "TextResourceDecoder.h"
29 #include "Image.h"
30 #include "RenderImage.h"
31 #include "KURL.h"
32 #include "markup.h"
33
34 #include <gtk/gtk.h>
35
36 namespace WebCore {
37
38 /* FIXME: we must get rid of this and use the enum in webkitwebview.h someway */
39 typedef enum
40 {
41     WEBKIT_WEB_VIEW_TARGET_INFO_HTML = - 1,
42     WEBKIT_WEB_VIEW_TARGET_INFO_TEXT = - 2
43 } WebKitWebViewTargetInfo;
44
45 class PasteboardSelectionData {
46 public:
47     PasteboardSelectionData(gchar* text, gchar* markup)
48         : m_text(text)
49         , m_markup(markup) { }
50
51     ~PasteboardSelectionData() {
52         g_free(m_text);
53         g_free(m_markup);
54     }
55
56     const gchar* text() const { return m_text; }
57     const gchar* markup() const { return m_markup; }
58
59 private:
60     gchar* m_text;
61     gchar* m_markup;
62 };
63
64 static void clipboard_get_contents_cb(GtkClipboard *clipboard, GtkSelectionData *selection_data,
65                                       guint info, gpointer data) {
66     PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
67     ASSERT(clipboardData);
68     if ((gint)info == WEBKIT_WEB_VIEW_TARGET_INFO_HTML) {
69         gtk_selection_data_set(selection_data, selection_data->target, 8,
70                                reinterpret_cast<const guchar*>(clipboardData->markup()),
71                                g_utf8_strlen(clipboardData->markup(), -1));
72     } else
73         gtk_selection_data_set_text(selection_data, clipboardData->text(), -1);
74 }
75
76 static void clipboard_clear_contents_cb(GtkClipboard *clipboard, gpointer data) {
77     PasteboardSelectionData* clipboardData = reinterpret_cast<PasteboardSelectionData*>(data);
78     ASSERT(clipboardData);
79     delete clipboardData;
80 }
81
82
83 Pasteboard* Pasteboard::generalPasteboard()
84 {
85     static Pasteboard* pasteboard = new Pasteboard();
86     return pasteboard;
87 }
88
89 Pasteboard::Pasteboard()
90 {
91     notImplemented();
92 }
93
94 Pasteboard::~Pasteboard()
95 {
96     delete m_helper;
97 }
98
99 void Pasteboard::setHelper(PasteboardHelper* helper)
100 {
101     m_helper = helper;
102 }
103
104 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
105 {
106     GtkClipboard* clipboard = m_helper->getClipboard(frame);
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 }
117
118 void Pasteboard::writeURL(const KURL& url, const String&, Frame* frame)
119 {
120     if (url.isEmpty())
121         return;
122
123     GtkClipboard* clipboard = m_helper->getClipboard(frame);
124     GtkClipboard* primary = m_helper->getPrimary(frame);
125     gtk_clipboard_set_text(clipboard, url.string().utf8().data(), url.string().utf8().length());
126     gtk_clipboard_set_text(primary, url.string().utf8().data(), url.string().utf8().length());
127 }
128
129 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
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     GdkPixbuf* pixbuf = image->getGdkPixbuf();
141     gtk_clipboard_set_image(clipboard, pixbuf);
142     g_object_unref(pixbuf);
143 }
144
145 void Pasteboard::clear()
146 {
147     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
148
149     gtk_clipboard_clear(clipboard);
150 }
151
152 bool Pasteboard::canSmartReplace()
153 {
154     notImplemented();
155     return false;
156 }
157
158 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
159                                                           bool allowPlainText, bool& chosePlainText)
160 {
161     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
162     GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
163     chosePlainText = false;
164
165     if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
166         ASSERT(data->data);
167         RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/plain", "UTF-8", true);
168         String html = decoder->decode(reinterpret_cast<char*>(data->data), data->length);
169         html += decoder->flush();
170         gtk_selection_data_free(data);
171
172         if (!html.isEmpty()) {
173             RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, "");
174             if (fragment)
175                 return fragment.release();
176         }
177     }
178
179     if (!allowPlainText)
180         return 0;
181
182     if (gchar* utf8 = gtk_clipboard_wait_for_text(clipboard)) {
183         String text = String::fromUTF8(utf8);
184         g_free(utf8);
185
186         chosePlainText = true;
187         RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), text);
188         if (fragment)
189             return fragment.release();
190     }
191
192     return 0;
193 }
194
195 String Pasteboard::plainText(Frame* frame)
196 {
197     GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
198
199     gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
200
201     if (!utf8)
202         return String();
203
204     String text = String::fromUTF8(utf8);
205     g_free(utf8);
206
207     return text;
208 }
209
210 }