1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebDragClient.h"
28
29 #include "ShareableBitmap.h"
30 #include "WebCoreArgumentCoders.h"
31 #include "WebPage.h"
32 #include "WebPageProxyMessages.h"
33 #include <WebCore/BitmapInfo.h>
34 #include <WebCore/COMPtr.h>
35 #include <WebCore/ClipboardWin.h>
36 #include <WebCore/DragController.h>
37 #include <WebCore/Frame.h>
38 #include <WebCore/GraphicsContext.h>
39 #include <WebCore/Page.h>
40 #include <shlobj.h>
41
42 using namespace WebCore;
43
44 namespace WebKit {
45
draggingSourceOperationMaskToDragCursors(DragOperation op)46 static DWORD draggingSourceOperationMaskToDragCursors(DragOperation op)
47 {
48 DWORD result = DROPEFFECT_NONE;
49 if (op == DragOperationEvery)
50 return DROPEFFECT_COPY | DROPEFFECT_LINK | DROPEFFECT_MOVE;
51 if (op & DragOperationCopy)
52 result |= DROPEFFECT_COPY;
53 if (op & DragOperationLink)
54 result |= DROPEFFECT_LINK;
55 if (op & DragOperationMove)
56 result |= DROPEFFECT_MOVE;
57 if (op & DragOperationGeneric)
58 result |= DROPEFFECT_MOVE;
59 return result;
60 }
61
startDrag(DragImageRef image,const IntPoint & imageOrigin,const IntPoint & dragPoint,Clipboard * clipboard,Frame * frame,bool isLink)62 void WebDragClient::startDrag(DragImageRef image, const IntPoint& imageOrigin, const IntPoint& dragPoint, Clipboard* clipboard, Frame* frame, bool isLink)
63 {
64 COMPtr<IDataObject> dataObject = static_cast<ClipboardWin*>(clipboard)->dataObject();
65
66 if (!dataObject)
67 return;
68
69 OwnPtr<HDC> bitmapDC(CreateCompatibleDC(0));
70 BITMAPINFO bitmapInfo = {0};
71 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
72 GetDIBits(bitmapDC.get(), image, 0, 0, 0, &bitmapInfo, DIB_RGB_COLORS);
73 if (bitmapInfo.bmiHeader.biSizeImage <= 0)
74 bitmapInfo.bmiHeader.biSizeImage = bitmapInfo.bmiHeader.biWidth * abs(bitmapInfo.bmiHeader.biHeight) * (bitmapInfo.bmiHeader.biBitCount + 7) / 8;
75
76 RefPtr<SharedMemory> memoryBuffer = SharedMemory::create(bitmapInfo.bmiHeader.biSizeImage);
77
78 bitmapInfo.bmiHeader.biCompression = BI_RGB;
79 GetDIBits(bitmapDC.get(), image, 0, bitmapInfo.bmiHeader.biHeight, memoryBuffer->data(), &bitmapInfo, DIB_RGB_COLORS);
80
81 SharedMemory::Handle handle;
82 if (!memoryBuffer->createHandle(handle, SharedMemory::ReadOnly))
83 return;
84 DWORD okEffect = draggingSourceOperationMaskToDragCursors(m_page->corePage()->dragController()->sourceDragOperation());
85 DragData dragData(dataObject.get(), IntPoint(), IntPoint(), DragOperationNone);
86 m_page->send(Messages::WebPageProxy::StartDragDrop(imageOrigin, dragPoint, okEffect, dragData.dragDataMap(), IntSize(bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight), handle, isLink), m_page->pageID());
87 }
88
89 } // namespace WebKit
90