2 * Copyright (C) 2009, 2010 Igalia S.L.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "DragClientGtk.h"
22 #include "ClipboardGtk.h"
23 #include "ClipboardUtilitiesGtk.h"
24 #include "DataObjectGtk.h"
26 #include "DragController.h"
29 #include "GOwnPtrGtk.h"
30 #include "GRefPtrGtk.h"
31 #include "GtkVersioning.h"
32 #include "NotImplemented.h"
33 #include "PasteboardHelper.h"
34 #include "RenderObject.h"
35 #include "webkitwebframeprivate.h"
36 #include "webkitwebviewprivate.h"
37 #include "webkitwebview.h"
41 using namespace WebCore;
45 #ifdef GTK_API_VERSION_2
46 static gboolean dragIconWindowDrawEventCallback(GtkWidget* widget, GdkEventExpose* event, DragClient* client)
48 RefPtr<cairo_t> context = adoptRef(gdk_cairo_create(event->window));
49 client->drawDragIconWindow(widget, context.get());
53 static gboolean dragIconWindowDrawEventCallback(GtkWidget* widget, cairo_t* context, DragClient* client)
55 if (!gdk_cairo_get_clip_rectangle(context, 0))
57 client->drawDragIconWindow(widget, context);
60 #endif // GTK_API_VERSION_2
62 DragClient::DragClient(WebKitWebView* webView)
65 , m_dragIconWindow(gtk_window_new(GTK_WINDOW_POPUP))
67 #ifdef GTK_API_VERSION_2
68 g_signal_connect(m_dragIconWindow, "expose-event", G_CALLBACK(dragIconWindowDrawEventCallback), this);
70 g_signal_connect(m_dragIconWindow, "draw", G_CALLBACK(dragIconWindowDrawEventCallback), this);
74 DragClient::~DragClient()
76 gtk_widget_destroy(m_dragIconWindow);
79 void DragClient::willPerformDragDestinationAction(DragDestinationAction, DragData*)
83 void DragClient::willPerformDragSourceAction(DragSourceAction, const IntPoint& startPos, Clipboard*)
85 m_startPos = startPos;
88 DragDestinationAction DragClient::actionMaskForDrag(DragData*)
91 return DragDestinationActionAny;
94 DragSourceAction DragClient::dragSourceActionMaskForPoint(const IntPoint&)
97 return DragSourceActionAny;
100 void DragClient::startDrag(DragImageRef image, const IntPoint& dragImageOrigin, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
102 ClipboardGtk* clipboardGtk = reinterpret_cast<ClipboardGtk*>(clipboard);
104 WebKitWebView* webView = webkit_web_frame_get_web_view(kit(frame));
105 RefPtr<DataObjectGtk> dataObject = clipboardGtk->dataObject();
106 GRefPtr<GtkTargetList> targetList(PasteboardHelper::defaultPasteboardHelper()->targetListForDataObject(dataObject.get()));
107 GOwnPtr<GdkEvent> currentEvent(gtk_get_current_event());
109 GdkDragContext* context = gtk_drag_begin(GTK_WIDGET(m_webView), targetList.get(), dragOperationToGdkDragActions(clipboard->sourceOperation()), 1, currentEvent.get());
110 webView->priv->draggingDataObjects.set(context, dataObject);
112 // A drag starting should prevent a double-click from happening. This might
113 // happen if a drag is followed very quickly by another click (like in the DRT).
114 webView->priv->clickCounter.reset();
116 // This strategy originally comes from Chromium:
117 // src/chrome/browser/gtk/tab_contents_drag_source.cc
120 IntSize imageSize(cairo_image_surface_get_width(image), cairo_image_surface_get_height(image));
121 gtk_window_resize(GTK_WINDOW(m_dragIconWindow), imageSize.width(), imageSize.height());
123 if (!gtk_widget_get_realized(m_dragIconWindow)) {
124 GdkScreen* screen = gtk_widget_get_screen(m_dragIconWindow);
125 #ifdef GTK_API_VERSION_2
126 GdkColormap* rgba = gdk_screen_get_rgba_colormap(screen);
128 gtk_widget_set_colormap(m_dragIconWindow, rgba);
130 GdkVisual* visual = gdk_screen_get_rgba_visual(screen);
132 visual = gdk_screen_get_system_visual(screen);
133 gtk_widget_set_visual(m_dragIconWindow, visual);
134 #endif // GTK_API_VERSION_2
137 IntSize origin = eventPos - dragImageOrigin;
138 gtk_drag_set_icon_widget(context, m_dragIconWindow,
139 origin.width(), origin.height());
141 gtk_drag_set_icon_default(context);
144 void DragClient::drawDragIconWindow(GtkWidget* widget, cairo_t* context)
146 cairo_rectangle(context, 0, 0,
147 cairo_image_surface_get_width(m_dragImage.get()),
148 cairo_image_surface_get_height(m_dragImage.get()));
149 cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
150 cairo_set_source_surface(context, m_dragImage.get(), 0, 0);
154 void DragClient::dragControllerDestroyed()