2009-07-07 Jiahua Huang <jhuangjiahua@gmail.com>
[WebKit-https.git] / WebCore / platform / gtk / PasteboardGtk.cpp
index c500a6d..781baf5 100644 (file)
@@ -25,6 +25,7 @@
 #include "Frame.h"
 #include "NotImplemented.h"
 #include "PlatformString.h"
+#include "TextResourceDecoder.h"
 #include "Image.h"
 #include "RenderImage.h"
 #include "KURL.h"
@@ -51,9 +52,9 @@ public:
         g_free(m_text);
         g_free(m_markup);
     }
-    
+
     const gchar* text() const { return m_text; }
-    const gchar* markup() const { return m_markup; }    
+    const gchar* markup() const { return m_markup; }
 
 private:
     gchar* m_text;
@@ -102,28 +103,31 @@ void Pasteboard::setHelper(PasteboardHelper* helper)
 
 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
 {
+    GtkClipboard* clipboard = m_helper->getClipboard(frame);
     gchar* text = g_strdup(frame->selectedText().utf8().data());
     gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
     PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);
+
     gint n_targets;
     GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
-    GtkClipboard* clipboard = m_helper->getClipboard(frame);
     gtk_clipboard_set_with_data(clipboard, targets, n_targets,
                                 clipboard_get_contents_cb, clipboard_clear_contents_cb, data);
     gtk_target_table_free(targets, n_targets);
 }
 
-void Pasteboard::writeURL(const KURL&, const String&, Frame*)
+void Pasteboard::writeURL(const KURL& url, const String&, Frame* frame)
 {
-    notImplemented();
+    if (url.isEmpty())
+        return;
+
+    GtkClipboard* clipboard = m_helper->getClipboard(frame);
+    GtkClipboard* primary = m_helper->getPrimary(frame);
+    gtk_clipboard_set_text(clipboard, url.string().utf8().data(), url.string().utf8().length());
+    gtk_clipboard_set_text(primary, url.string().utf8().data(), url.string().utf8().length());
 }
 
 void Pasteboard::writeImage(Node* node, const KURL&, const String&)
 {
-    // TODO: Enable this when Image gets GdkPixbuf support
-
-    /*
     GtkClipboard* clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(), GDK_SELECTION_CLIPBOARD);
 
     ASSERT(node && node->renderer() && node->renderer()->isImage());
@@ -133,10 +137,9 @@ void Pasteboard::writeImage(Node* node, const KURL&, const String&)
     Image* image = cachedImage->image();
     ASSERT(image);
 
-    gtk_clipboard_set_image(clipboard, image->pixbuf());
-    */
-
-    notImplemented();
+    GdkPixbuf* pixbuf = image->getGdkPixbuf();
+    gtk_clipboard_set_image(clipboard, pixbuf);
+    g_object_unref(pixbuf);
 }
 
 void Pasteboard::clear()
@@ -155,17 +158,15 @@ bool Pasteboard::canSmartReplace()
 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context,
                                                           bool allowPlainText, bool& chosePlainText)
 {
-#if GLIB_CHECK_VERSION(2,10,0)
     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
-#else
-    GdkAtom textHtml = gdk_atom_intern("text/html", false);
-#endif
-    GtkClipboard* clipboard = m_helper->getClipboard(frame);
+    GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
     chosePlainText = false;
 
     if (GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard, textHtml)) {
         ASSERT(data->data);
-        String html = String::fromUTF8(reinterpret_cast<gchar*>(data->data), data->length * data->format / 8);
+        RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/plain", "UTF-8", true);
+        String html = decoder->decode(reinterpret_cast<char*>(data->data), data->length);
+        html += decoder->flush();
         gtk_selection_data_free(data);
 
         if (!html.isEmpty()) {
@@ -193,7 +194,7 @@ PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefP
 
 String Pasteboard::plainText(Frame* frame)
 {
-    GtkClipboard* clipboard = m_helper->getClipboard(frame);
+    GtkClipboard* clipboard = m_helper->getCurrentTarget(frame);
 
     gchar* utf8 = gtk_clipboard_wait_for_text(clipboard);