OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebKit / gtk / WebCoreSupport / DragClientGtk.cpp
index f4b0df1..be0fb10 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * Copyright (C) 2009, 2010 Igalia S.L.
+ *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
  *  License as published by the Free Software Foundation; either
 #include "config.h"
 #include "DragClientGtk.h"
 
+#include "ClipboardGtk.h"
+#include "ClipboardUtilitiesGtk.h"
+#include "DataObjectGtk.h"
 #include "Document.h"
+#include "DragController.h"
 #include "Element.h"
 #include "Frame.h"
+#include "GOwnPtrGtk.h"
+#include "GRefPtrGtk.h"
+#include "GtkVersioning.h"
 #include "NotImplemented.h"
+#include "PasteboardHelper.h"
 #include "RenderObject.h"
+#include "webkitprivate.h"
 #include "webkitwebview.h"
-
+#include <gdk/gdk.h>
 #include <gtk/gtk.h>
-#if !GTK_CHECK_VERSION(2, 14, 0)
-#define gtk_widget_get_window(widget) (widget)->window
-#endif
 
 using namespace WebCore;
 
 namespace WebKit {
 
+#ifdef GTK_API_VERSION_2
+static gboolean dragIconWindowDrawEventCallback(GtkWidget* widget, GdkEventExpose* event, DragClient* client)
+{
+    RefPtr<cairo_t> context = adoptRef(gdk_cairo_create(event->window));
+    client->drawDragIconWindow(widget, context.get());
+    return TRUE;
+}
+#else
+static gboolean dragIconWindowDrawEventCallback(GtkWidget* widget, cairo_t* context, DragClient* client)
+{
+    if (!gdk_cairo_get_clip_rectangle(context, 0))
+        return FALSE;
+    client->drawDragIconWindow(widget, context);
+    return TRUE;
+}
+#endif // GTK_API_VERSION_2
+
 DragClient::DragClient(WebKitWebView* webView)
     : m_webView(webView)
     , m_startPos(0, 0)
+    , m_dragIconWindow(gtk_window_new(GTK_WINDOW_POPUP))
+{
+#ifdef GTK_API_VERSION_2
+    g_signal_connect(m_dragIconWindow.get(), "expose-event", G_CALLBACK(dragIconWindowDrawEventCallback), this);
+#else
+    g_signal_connect(m_dragIconWindow.get(), "draw", G_CALLBACK(dragIconWindowDrawEventCallback), this);
+#endif
+}
+
+DragClient::~DragClient()
 {
+    g_signal_handlers_disconnect_by_func(m_dragIconWindow.get(), (gpointer) dragIconWindowDrawEventCallback, this);
 }
 
 void DragClient::willPerformDragDestinationAction(DragDestinationAction, DragData*)
 {
-    notImplemented();
 }
 
 void DragClient::willPerformDragSourceAction(DragSourceAction, const IntPoint& startPos, Clipboard*)
@@ -61,50 +96,58 @@ DragSourceAction DragClient::dragSourceActionMaskForPoint(const IntPoint&)
     return DragSourceActionAny;
 }
 
-void DragClient::startDrag(DragImageRef image, const IntPoint& dragImageOrigin, const IntPoint& eventPos, Clipboard*, Frame* frame, bool linkDrag)
+void DragClient::startDrag(DragImageRef image, const IntPoint& dragImageOrigin, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
 {
-    Element* targetElement = frame->document()->elementFromPoint(m_startPos.x(), m_startPos.y());
-    bool imageDrag = false;
-
-    if (targetElement)
-        imageDrag = targetElement->renderer()->isImage();
-
-    GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
-    GdkAtom netscapeUrl = gdk_atom_intern_static_string("_NETSCAPE_URL");
-
-    GtkTargetList* targetList = gtk_target_list_new(NULL, 0);
-    gtk_target_list_add(targetList, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
-    gtk_target_list_add_text_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
-
-    if (linkDrag || imageDrag) {
-        gtk_target_list_add(targetList, netscapeUrl, 0, WEBKIT_WEB_VIEW_TARGET_INFO_NETSCAPE_URL);
-        gtk_target_list_add_uri_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_URI_LIST);
-    }
-
-    if (imageDrag)
-        gtk_target_list_add_image_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_IMAGE, false);
-
-    GdkDragAction dragAction = GDK_ACTION_COPY;
-    if (linkDrag) {
-        dragAction = GDK_ACTION_LINK;
-        if (imageDrag)
-            dragAction = (GdkDragAction)(dragAction | GDK_ACTION_COPY);
-    }
-
-    GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS);
-    reinterpret_cast<GdkEventButton*>(event)->window = gtk_widget_get_window(GTK_WIDGET(m_webView));
-    reinterpret_cast<GdkEventButton*>(event)->time = GDK_CURRENT_TIME;
-
-    GdkDragContext* context = gtk_drag_begin(GTK_WIDGET(m_webView),
-                                             targetList, dragAction, 1, event);
-    g_object_ref(context);
-
-    if (image)
-        gtk_drag_set_icon_pixbuf(context, image, eventPos.x() - dragImageOrigin.x(), eventPos.y() - dragImageOrigin.y());
-    else
+    ClipboardGtk* clipboardGtk = reinterpret_cast<ClipboardGtk*>(clipboard);
+
+    WebKitWebView* webView = webkit_web_frame_get_web_view(kit(frame));
+    RefPtr<DataObjectGtk> dataObject = clipboardGtk->dataObject();
+    PlatformRefPtr<GtkTargetList> targetList(clipboardGtk->helper()->targetListForDataObject(dataObject.get()));
+    GOwnPtr<GdkEvent> currentEvent(gtk_get_current_event());
+
+    GdkDragContext* context = gtk_drag_begin(GTK_WIDGET(m_webView), targetList.get(), dragOperationToGdkDragActions(clipboard->sourceOperation()), 1, currentEvent.get());
+    webView->priv->draggingDataObjects.set(context, dataObject);
+
+    // A drag starting should prevent a double-click from happening. This might
+    // happen if a drag is followed very quickly by another click (like in the DRT).
+    webView->priv->previousClickTime = 0;
+
+    // This strategy originally comes from Chromium:
+    // src/chrome/browser/gtk/tab_contents_drag_source.cc
+    if (image) {
+        m_dragImage = image;
+        IntSize imageSize(cairo_image_surface_get_width(image), cairo_image_surface_get_height(image));
+        gtk_window_resize(GTK_WINDOW(m_dragIconWindow.get()), imageSize.width(), imageSize.height());
+
+        if (!gtk_widget_get_realized(m_dragIconWindow.get())) {
+            GdkScreen* screen = gtk_widget_get_screen(m_dragIconWindow.get());
+#ifdef GTK_API_VERSION_2
+            GdkColormap* rgba = gdk_screen_get_rgba_colormap(screen);
+            if (rgba)
+                gtk_widget_set_colormap(m_dragIconWindow.get(), rgba);
+#else
+            GdkVisual* visual = gdk_screen_get_rgba_visual(screen);
+            if (!visual)
+                visual = gdk_screen_get_system_visual(screen);
+            gtk_widget_set_visual(m_dragIconWindow.get(), visual);
+#endif // GTK_API_VERSION_2
+        }
+
+        IntSize origin = eventPos - dragImageOrigin;
+        gtk_drag_set_icon_widget(context, m_dragIconWindow.get(),
+                                 origin.width(), origin.height());
+    else
         gtk_drag_set_icon_default(context);
+}
 
-    gtk_target_list_unref(targetList);
+void DragClient::drawDragIconWindow(GtkWidget* widget, cairo_t* context)
+{
+    cairo_rectangle(context, 0, 0,
+                    cairo_image_surface_get_width(m_dragImage.get()),
+                    cairo_image_surface_get_height(m_dragImage.get()));
+    cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
+    cairo_set_source_surface(context, m_dragImage.get(), 0, 0);
+    cairo_fill(context);
 }
 
 DragImageRef DragClient::createDragImageForLink(KURL&, const String&, Frame*)