2 * Copyright (C) 2007 Holger Hans Peter Freyther
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
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.
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.
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
21 #include "Pasteboard.h"
23 #include "DataObjectGtk.h"
24 #include "DocumentFragment.h"
26 #include "GOwnPtrGtk.h"
27 #include "HTMLNames.h"
28 #include "HTMLParserIdioms.h"
30 #include "NotImplemented.h"
31 #include "PlatformString.h"
32 #include "TextResourceDecoder.h"
34 #include "RenderImage.h"
37 #include <wtf/gobject/GRefPtr.h>
38 #include <wtf/text/CString.h>
42 #include "XLinkNames.h"
47 Pasteboard* Pasteboard::generalPasteboard()
49 static Pasteboard* pasteboard = new Pasteboard();
53 Pasteboard::Pasteboard()
57 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
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);
67 void Pasteboard::writePlainText(const String& text)
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);
75 void Pasteboard::writeURL(const KURL& url, const String& label, Frame* frame)
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);
87 static KURL getURLForImageNode(Node* node)
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);
94 else if (node->hasTagName(SVGNames::imageTag))
95 urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
97 else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
98 Element* element = static_cast<Element*>(node);
99 urlString = element->getAttribute(element->imageSourceAttributeName());
101 return urlString.isEmpty() ? KURL() : node->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
104 void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
108 if (!(node->renderer() && node->renderer()->isImage()))
111 RenderImage* renderer = toRenderImage(node->renderer());
112 CachedImage* cachedImage = renderer->cachedImage();
113 if (!cachedImage || cachedImage->errorOccurred())
115 Image* image = cachedImage->image();
118 GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
119 DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
121 KURL url = getURLForImageNode(node);
122 if (!url.isEmpty()) {
123 dataObject->setURL(url, title);
125 dataObject->setMarkup(createMarkup(static_cast<Element*>(node), IncludeNode, 0, ResolveAllURLs));
128 GRefPtr<GdkPixbuf> pixbuf = adoptGRef(image->getGdkPixbuf());
129 dataObject->setImage(pixbuf.get());
131 PasteboardHelper::defaultPasteboardHelper()->writeClipboardContents(clipboard);
134 void Pasteboard::clear()
136 gtk_clipboard_clear(gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
139 bool Pasteboard::canSmartReplace()
141 return PasteboardHelper::defaultPasteboardHelper()->clipboardContentSupportsSmartReplace(
142 gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
145 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
146 bool allowPlainText, bool& chosePlainText)
148 PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
149 GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
150 DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
151 helper->getClipboardContents(clipboard);
153 chosePlainText = false;
155 if (dataObject->hasMarkup()) {
156 RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), dataObject->markup(), "", FragmentScriptingNotAllowed);
158 return fragment.release();
164 if (dataObject->hasText()) {
165 chosePlainText = true;
166 RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), dataObject->text());
168 return fragment.release();
174 String Pasteboard::plainText(Frame* frame)
176 PasteboardHelper* helper = PasteboardHelper::defaultPasteboardHelper();
177 GtkClipboard* clipboard = helper->getCurrentClipboard(frame);
178 DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
180 helper->getClipboardContents(clipboard);
181 return dataObject->text();