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