d6d867e917aca707d4ea8b37f77c1f016a25bc33
[WebKit-https.git] / Source / 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 "CachedImage.h"
24 #include "ClipboardGtk.h"
25 #include "DataObjectGtk.h"
26 #include "DocumentFragment.h"
27 #include "Frame.h"
28 #include "GOwnPtrGtk.h"
29 #include "HTMLNames.h"
30 #include "HTMLParserIdioms.h"
31 #include "KURL.h"
32 #include "NotImplemented.h"
33 #include "TextResourceDecoder.h"
34 #include "Image.h"
35 #include "RenderImage.h"
36 #include "markup.h"
37 #include <gtk/gtk.h>
38 #include <wtf/gobject/GRefPtr.h>
39 #include <wtf/text/CString.h>
40 #include <wtf/text/WTFString.h>
41
42 #if ENABLE(SVG)
43 #include "SVGNames.h"
44 #include "XLinkNames.h"
45 #endif
46
47 namespace WebCore {
48
49 Pasteboard* Pasteboard::generalPasteboard()
50 {
51     static Pasteboard* pasteboard = new Pasteboard();
52     return pasteboard;
53 }
54
55 Pasteboard::Pasteboard()
56 {
57 }
58
59 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
60 {
61     PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
62     GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
63
64     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
65     dataObject->clearAll();
66
67     dataObject->setText(frame->editor()->selectedText());
68     dataObject->setMarkup(createMarkup(selectedRange, 0, AnnotateForInterchange, false, ResolveNonLocalURLs));
69     helper->writeClipboardContents(clipboard, canSmartCopyOrDelete ? PasteboardHelper::IncludeSmartPaste : PasteboardHelper::DoNotIncludeSmartPaste);
70 }
71
72 void Pasteboard::writePlainText(const String& text, SmartReplaceOption smartReplaceOption)
73 {
74     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
75     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
76     dataObject->clearAll();
77
78     dataObject->setText(text);
79     PasteboardHelper::defaultPasteboardHelper()->writeClipboardContents(clipboard,
80         (smartReplaceOption == CanSmartReplace) ? PasteboardHelper::IncludeSmartPaste : PasteboardHelper::DoNotIncludeSmartPaste);
81 }
82
83 void Pasteboard::writeURL(const KURL& url, const String& label, Frame* frame)
84 {
85     if (url.isEmpty())
86         return;
87
88     PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
89     GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
90
91     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
92     dataObject->clearAll();
93
94     dataObject->setURL(url, label);
95     helper->writeClipboardContents(clipboard);
96 }
97
98 static KURL getURLForImageNode(Node* node)
99 {
100     // FIXME: Later this code should be shared with Chromium somehow. Chances are all platforms want it.
101     AtomicString urlString;
102     if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
103         urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
104 #if ENABLE(SVG)
105     else if (node->hasTagName(SVGNames::imageTag))
106         urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
107 #endif
108     else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
109         Element* element = static_cast<Element*>(node);
110         urlString = element->getAttribute(element->imageSourceAttributeName());
111     }
112     return urlString.isEmpty() ? KURL() : node->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
113 }
114
115 void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
116 {
117     ASSERT(node);
118
119     if (!(node->renderer() && node->renderer()->isImage()))
120         return;
121
122     RenderImage* renderer = toRenderImage(node->renderer());
123     CachedImage* cachedImage = renderer->cachedImage();
124     if (!cachedImage || cachedImage->errorOccurred())
125         return;
126     Image* image = cachedImage->imageForRenderer(renderer);
127     ASSERT(image);
128
129     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
130     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
131     dataObject->clearAll();
132
133     KURL url = getURLForImageNode(node);
134     if (!url.isEmpty()) {
135         dataObject->setURL(url, title);
136
137         dataObject->setMarkup(createMarkup(static_cast<Element*>(node), IncludeNode, 0, ResolveAllURLs));
138     }
139
140     GRefPtr<GdkPixbuf> pixbuf = adoptGRef(image->getGdkPixbuf());
141     dataObject->setImage(pixbuf.get());
142
143     PasteboardHelper::defaultPasteboardHelper()->writeClipboardContents(clipboard);
144 }
145
146 void Pasteboard::writeClipboard(Clipboard* clipboard)
147 {
148     GtkClipboard* gtkClipboard = static_cast<ClipboardGtk*>(clipboard)->clipboard();
149     if (gtkClipboard)
150         PasteboardHelper::defaultPasteboardHelper()->writeClipboardContents(gtkClipboard);
151 }
152
153 void Pasteboard::clear()
154 {
155     gtk_clipboard_clear(gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
156 }
157
158 bool Pasteboard::canSmartReplace()
159 {
160     return PasteboardHelper::defaultPasteboardHelper()->clipboardContentSupportsSmartReplace(
161         gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
162 }
163
164 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
165                                                           bool allowPlainText, bool& chosePlainText)
166 {
167     PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
168     GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
169     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
170     helper->getClipboardContents(clipboard);
171
172     chosePlainText = false;
173
174     if (dataObject->hasMarkup()) {
175         RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), dataObject->markup(), "", DisallowScriptingAndPluginContentIfNeeded);
176         if (fragment)
177             return fragment.release();
178     }
179
180     if (!allowPlainText)
181         return 0;
182
183     if (dataObject->hasText()) {
184         chosePlainText = true;
185         RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), dataObject->text());
186         if (fragment)
187             return fragment.release();
188     }
189
190     return 0;
191 }
192
193 String Pasteboard::plainText(Frame* frame)
194 {
195     PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
196     GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
197     DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
198
199     helper->getClipboardContents(clipboard);
200     return dataObject->text();
201 }
202
203 bool Pasteboard::isSelectionMode() const
204 {
205     return PasteboardHelper::defaultPasteboardHelper()->usePrimarySelectionClipboard();
206 }
207
208 void Pasteboard::setSelectionMode(bool selectionMode)
209 {
210     PasteboardHelper::defaultPasteboardHelper()->setUsePrimarySelectionClipboard(selectionMode);
211 }
212
213 }