#include "WebDragClient.h"
#include "WebDropSource.h"
#include "WebKitGraphics.h"
#include "WebView.h"
#include <WebCore/DataTransfer.h>
#include <WebCore/DragController.h>
#include <WebCore/DragData.h>
#include <WebCore/EventHandler.h>
#include <WebCore/FrameView.h>
#include <WebCore/GraphicsContext.h>
#include <WebCore/MainFrame.h>
#include <WebCore/Page.h>
#include <WebCore/Pasteboard.h>
#include <WebCore/PlatformMouseEvent.h>
#include <shlobj.h>
using namespace WebCore;
static DWORD draggingSourceOperationMaskToDragCursors(DragOperation op)
{
DWORD result = DROPEFFECT_NONE;
if (op == DragOperationEvery)
return DROPEFFECT_COPY | DROPEFFECT_LINK | DROPEFFECT_MOVE;
if (op & DragOperationCopy)
result |= DROPEFFECT_COPY;
if (op & DragOperationLink)
result |= DROPEFFECT_LINK;
if (op & DragOperationMove)
result |= DROPEFFECT_MOVE;
if (op & DragOperationGeneric)
result |= DROPEFFECT_MOVE;
return result;
}
WebDragClient::WebDragClient(WebView* webView)
: m_webView(webView)
{
ASSERT(webView);
}
DragDestinationAction WebDragClient::actionMaskForDrag(DragData& dragData)
{
COMPtr<IWebUIDelegate> delegateRef = 0;
WebDragDestinationAction mask = WebDragDestinationActionAny;
if (SUCCEEDED(m_webView->uiDelegate(&delegateRef)))
delegateRef->dragDestinationActionMaskForDraggingInfo(m_webView, dragData.platformData(), &mask);
return (DragDestinationAction)mask;
}
void WebDragClient::willPerformDragDestinationAction(DragDestinationAction action, DragData& dragData)
{
COMPtr<IWebUIDelegate> delegateRef = 0;
if (SUCCEEDED(m_webView->uiDelegate(&delegateRef)))
delegateRef->willPerformDragDestinationAction(m_webView, (WebDragDestinationAction)action, dragData.platformData());
}
DragSourceAction WebDragClient::dragSourceActionMaskForPoint(const IntPoint& windowPoint)
{
COMPtr<IWebUIDelegate> delegateRef = 0;
WebDragSourceAction action = WebDragSourceActionAny;
POINT localpt = core(m_webView)->mainFrame().view()->windowToContents(windowPoint);
if (SUCCEEDED(m_webView->uiDelegate(&delegateRef)))
delegateRef->dragSourceActionMaskForPoint(m_webView, &localpt, &action);
return (DragSourceAction)action;
}
void WebDragClient::willPerformDragSourceAction(DragSourceAction action, const IntPoint& intPoint, DataTransfer& dataTransfer)
{
COMPtr<IWebUIDelegate> uiDelegate;
if (!SUCCEEDED(m_webView->uiDelegate(&uiDelegate)))
return;
POINT point = intPoint;
COMPtr<IDataObject> dataObject = dataTransfer.pasteboard().dataObject();
COMPtr<IDataObject> newDataObject;
HRESULT result = uiDelegate->willPerformDragSourceAction(m_webView, static_cast<WebDragSourceAction>(action), &point, dataObject.get(), &newDataObject);
if (result == S_OK && newDataObject != dataObject)
const_cast<Pasteboard&>(dataTransfer.pasteboard()).setExternalDataObject(newDataObject.get());
}
void WebDragClient::startDrag(DragImageRef image, const IntPoint& imageOrigin, const IntPoint& dragPoint, DataTransfer& dataTransfer, Frame& frame, bool isLink)
{
RefPtr<Frame> frameProtector = &frame;
COMPtr<IDragSourceHelper> helper;
COMPtr<IDataObject> dataObject;
COMPtr<WebView> viewProtector = m_webView;
COMPtr<IDropSource> source;
if (FAILED(WebDropSource::createInstance(m_webView, &source)))
return;
dataObject = dataTransfer.pasteboard().dataObject();
if (source && (image || dataObject)) {
if (image) {
if(SUCCEEDED(CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER,
IID_IDragSourceHelper,(LPVOID*)&helper))) {
BITMAP b;
GetObject(image, sizeof(BITMAP), &b);
SHDRAGIMAGE sdi;
sdi.sizeDragImage.cx = b.bmWidth;
sdi.sizeDragImage.cy = b.bmHeight;
sdi.crColorKey = 0xffffffff;
sdi.hbmpDragImage = image;
sdi.ptOffset.x = dragPoint.x() - imageOrigin.x();
sdi.ptOffset.y = dragPoint.y() - imageOrigin.y();
if (isLink)
sdi.ptOffset.y = b.bmHeight - sdi.ptOffset.y;
helper->InitializeFromBitmap(&sdi, dataObject.get());
}
}
DWORD okEffect = draggingSourceOperationMaskToDragCursors(m_webView->page()->dragController().sourceDragOperation());
DWORD effect = DROPEFFECT_NONE;
COMPtr<IWebUIDelegate> ui;
HRESULT hr = E_NOTIMPL;
if (SUCCEEDED(m_webView->uiDelegate(&ui))) {
COMPtr<IWebUIDelegatePrivate> uiPrivate;
if (SUCCEEDED(ui->QueryInterface(IID_IWebUIDelegatePrivate, (void**)&uiPrivate)))
hr = uiPrivate->doDragDrop(m_webView, dataObject.get(), source.get(), okEffect, &effect);
}
if (hr == E_NOTIMPL)
hr = DoDragDrop(dataObject.get(), source.get(), okEffect, &effect);
DragOperation operation = DragOperationNone;
if (hr == DRAGDROP_S_DROP) {
if (effect & DROPEFFECT_COPY)
operation = DragOperationCopy;
else if (effect & DROPEFFECT_LINK)
operation = DragOperationLink;
else if (effect & DROPEFFECT_MOVE)
operation = DragOperationMove;
}
frame.eventHandler().dragSourceEndedAt(generateMouseEvent(m_webView, false), operation);
}
}
void WebDragClient::dragControllerDestroyed()
{
delete this;
}