2007-12-14 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 Pasteboard* Pasteboard::generalPasteboard()
38 {
39     static Pasteboard* pasteboard = new Pasteboard();
40     return pasteboard;
41 }
42
43 Pasteboard::Pasteboard()
44 {
45     notImplemented();
46 }
47
48 Pasteboard::~Pasteboard()
49 {
50     notImplemented();
51 }
52
53 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
54 {
55     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
56
57     String text = frame->selectedText();
58     gtk_clipboard_set_text(clipboard, text.utf8().data(), text.utf8().length());
59 }
60
61 void Pasteboard::writeURL(const KURL&, const String&, Frame*)
62 {
63     notImplemented();
64 }
65
66 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
67 {
68     // TODO: Enable this when Image gets GdkPixbuf support
69
70     /*
71     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
72
73     ASSERT(node && node->renderer() && node->renderer()->isImage());
74     RenderImage* renderer = static_cast<RenderImage*>(node->renderer());
75     CachedImage* cachedImage = static_cast<CachedImage*>(renderer->cachedImage());
76     ASSERT(cachedImage);
77     Image* image = cachedImage->image();
78     ASSERT(image);
79
80     gtk_clipboard_set_image(clipboard, image->pixbuf());
81     */
82
83     notImplemented();
84 }
85
86 void Pasteboard::clear()
87 {
88     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
89
90     gtk_clipboard_clear(clipboard);
91 }
92
93 bool Pasteboard::canSmartReplace()
94 {
95     notImplemented();
96     return false;
97 }
98
99 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
100                                                           bool allowPlainText, bool& chosePlainText)
101 {
102 #if GLIB_CHECK_VERSION(2,10,0)
103     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
104 #else
105     GdkAtom textHtml = gdk_atom_intern("text/html", false);
106 #endif
107     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
108     chosePlainText = false;
109
110     if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
111         ASSERT(data->data);
112         String html = String::fromUTF8(reinterpret_cast<gchar*>(data->data), data->length * data->format / 8);
113         gtk_selection_data_free(data);
114
115         if (!html.isEmpty()) {
116             RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, "");
117             if (fragment)
118                 return fragment.release();
119         }
120     }
121
122     if (!allowPlainText)
123         return 0;
124
125     if (gchar* utf8 = gtk_clipboard_wait_for_text(clipboard)) {
126         String text = String::fromUTF8(utf8);
127         g_free(utf8);
128
129         chosePlainText = true;
130         RefPtr<DocumentFragment> fragment = createFragmentFromText(context.get(), text);
131         if (fragment)
132             return fragment.release();
133     }
134
135     return 0;
136 }
137
138 String Pasteboard::plainText(Frame*)
139 {
140     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
141
142     gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);
143
144     if (!utf8)
145         return String();
146
147     String text = String::fromUTF8(utf8);
148     g_free(utf8);
149
150     return text;
151 }
152 }