1 /* 2 * This library is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU Lesser General Public 4 * License as published by the Free Software Foundation; either 5 * version 2 of the License, or (at your option) any later version. 6 * 7 * This library is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * Lesser General Public License for more details. 11 * 12 * You should have received a copy of the GNU Lesser General Public 13 * License along with this library; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 15 */ 16 17 #include "config.h" 18 #include "ClipboardGtk.h" 19 20 #include "CachedImage.h" 21 #include "CString.h" 22 #include "Editor.h" 23 #include "Element.h" 24 #include "FileList.h" 25 #include "Frame.h" 26 #include "markup.h" 27 #include "NotImplemented.h" 28 #include "RenderImage.h" 29 #include "StringHash.h" 30 31 #include <gtk/gtk.h> 32 33 namespace WebCore { 34 35 PassRefPtr<Clipboard> Editor::newGeneralClipboard(ClipboardAccessPolicy policy) 36 { 37 return ClipboardGtk::create(policy, false); 38 } 39 40 ClipboardGtk::ClipboardGtk(ClipboardAccessPolicy policy, bool forDragging) 41 : Clipboard(policy, forDragging) 42 { 43 } 44 45 ClipboardGtk::~ClipboardGtk() 46 { 47 } 48 49 void ClipboardGtk::clearData(const String&) 50 { 51 notImplemented(); 52 } 53 54 void ClipboardGtk::clearAllData() 55 { 56 notImplemented(); 57 } 58 59 String ClipboardGtk::getData(const String&, bool &success) const 60 { 61 notImplemented(); 62 success = false; 63 return String(); 64 } 65 66 bool ClipboardGtk::setData(const String&, const String&) 67 { 68 notImplemented(); 69 return false; 70 } 71 72 HashSet<String> ClipboardGtk::types() const 73 { 74 notImplemented(); 75 return HashSet<String>(); 76 } 77 78 PassRefPtr<FileList> ClipboardGtk::files() const 79 { 80 notImplemented(); 81 return 0; 82 } 83 84 IntPoint ClipboardGtk::dragLocation() const 85 { 86 notImplemented(); 87 return IntPoint(0, 0); 88 } 89 90 CachedImage* ClipboardGtk::dragImage() const 91 { 92 notImplemented(); 93 return 0; 94 } 95 96 void ClipboardGtk::setDragImage(CachedImage*, const IntPoint&) 97 { 98 notImplemented(); 99 } 100 101 Node* ClipboardGtk::dragImageElement() 102 { 103 notImplemented(); 104 return 0; 105 } 106 107 void ClipboardGtk::setDragImageElement(Node*, const IntPoint&) 108 { 109 notImplemented(); 110 } 111 112 DragImageRef ClipboardGtk::createDragImage(IntPoint&) const 113 { 114 notImplemented(); 115 return 0; 116 } 117 118 static CachedImage* getCachedImage(Element* element) 119 { 120 // Attempt to pull CachedImage from element 121 ASSERT(element); 122 RenderObject* renderer = element->renderer(); 123 if (!renderer || !renderer->isImage()) 124 return 0; 125 126 RenderImage* image = static_cast<RenderImage*>(renderer); 127 if (image->cachedImage() && !image->cachedImage()->errorOccurred()) 128 return image->cachedImage(); 129 130 return 0; 131 } 132 133 void ClipboardGtk::declareAndWriteDragImage(Element* element, const KURL& url, const String& label, Frame*) 134 { 135 CachedImage* cachedImage = getCachedImage(element); 136 if (!cachedImage || !cachedImage->isLoaded()) 137 return; 138 139 GdkPixbuf* pixbuf = cachedImage->image()->getGdkPixbuf(); 140 if (!pixbuf) 141 return; 142 143 GtkClipboard* imageClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardImage")); 144 gtk_clipboard_clear(imageClipboard); 145 146 gtk_clipboard_set_image(imageClipboard, pixbuf); 147 g_object_unref(pixbuf); 148 149 writeURL(url, label, 0); 150 } 151 152 void ClipboardGtk::writeURL(const KURL& url, const String& label, Frame*) 153 { 154 GtkClipboard* textClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardText")); 155 GtkClipboard* urlClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardUrl")); 156 GtkClipboard* urlLabelClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardUrlLabel")); 157 158 gtk_clipboard_clear(textClipboard); 159 gtk_clipboard_clear(urlClipboard); 160 gtk_clipboard_clear(urlLabelClipboard); 161 162 gtk_clipboard_set_text(textClipboard, url.string().utf8().data(), -1); 163 gtk_clipboard_set_text(urlClipboard, url.string().utf8().data(), -1); 164 gtk_clipboard_set_text(urlLabelClipboard, label.utf8().data(), -1); 165 } 166 167 void ClipboardGtk::writeRange(Range* range, Frame* frame) 168 { 169 GtkClipboard* textClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardText")); 170 GtkClipboard* htmlClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardHtml")); 171 172 gtk_clipboard_clear(textClipboard); 173 gtk_clipboard_clear(htmlClipboard); 174 175 gtk_clipboard_set_text(textClipboard, frame->selectedText().utf8().data(), -1); 176 gtk_clipboard_set_text(htmlClipboard, createMarkup(range, 0, AnnotateForInterchange).utf8().data(), -1); 177 } 178 179 void ClipboardGtk::writePlainText(const String& text) 180 { 181 GtkClipboard* textClipboard = gtk_clipboard_get(gdk_atom_intern_static_string("WebKitClipboardText")); 182 183 gtk_clipboard_clear(textClipboard); 184 185 gtk_clipboard_set_text(textClipboard, text.utf8().data(), -1); 186 } 187 188 bool ClipboardGtk::hasData() 189 { 190 notImplemented(); 191 return false; 192 } 193 194 } 195