1 /*
2 * Copyright (C) 2007 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebDropSource.h"
28
29 #include "WebKitDLL.h"
30 #include "WebView.h"
31
32 #include <WebCore/DragActions.h>
33 #include <WebCore/EventHandler.h>
34 #include <WebCore/Frame.h>
35 #include <WebCore/Page.h>
36 #include <WebCore/PlatformMouseEvent.h>
37 #include <wtf/CurrentTime.h>
38
39 #include <Windows.h>
40
41 using namespace WebCore;
42
43
createInstance(WebView * webView,IDropSource ** result)44 HRESULT WebDropSource::createInstance(WebView* webView, IDropSource** result)
45 {
46 if (!webView || !result)
47 return E_INVALIDARG;
48 *result = new WebDropSource(webView);
49 return S_OK;
50 }
51
WebDropSource(WebView * webView)52 WebDropSource::WebDropSource(WebView* webView)
53 : m_ref(1)
54 , m_dropped(false)
55 , m_webView(webView)
56 {
57 gClassCount++;
58 gClassNameCount.add("WebDropSource");
59 }
60
~WebDropSource()61 WebDropSource::~WebDropSource()
62 {
63 gClassCount--;
64 gClassNameCount.remove("WebDropSource");
65 }
66
QueryInterface(REFIID riid,void ** ppvObject)67 STDMETHODIMP WebDropSource::QueryInterface(REFIID riid, void** ppvObject)
68 {
69 *ppvObject = 0;
70 if (IsEqualIID(riid, IID_IUnknown) ||
71 IsEqualIID(riid, IID_IDropSource)) {
72 *ppvObject = this;
73 AddRef();
74
75 return S_OK;
76 }
77
78 return E_NOINTERFACE;
79 }
80
STDMETHODIMP_(ULONG)81 STDMETHODIMP_(ULONG) WebDropSource::AddRef(void)
82 {
83 return InterlockedIncrement(&m_ref);
84 }
85
STDMETHODIMP_(ULONG)86 STDMETHODIMP_(ULONG) WebDropSource::Release(void)
87 {
88 long c = InterlockedDecrement(&m_ref);
89 if (c == 0)
90 delete this;
91 return c;
92 }
93
generateMouseEvent(WebView * webView,bool isDrag)94 PlatformMouseEvent generateMouseEvent(WebView* webView, bool isDrag) {
95 POINTL pt;
96 ::GetCursorPos((LPPOINT)&pt);
97 POINTL localpt = pt;
98 HWND viewWindow;
99 if (SUCCEEDED(webView->viewWindow((OLE_HANDLE*)&viewWindow)))
100 ::ScreenToClient(viewWindow, (LPPOINT)&localpt);
101 return PlatformMouseEvent(IntPoint(localpt.x, localpt.y), IntPoint(pt.x, pt.y),
102 isDrag ? LeftButton : NoButton, MouseEventMoved, 0, false, false, false, false, currentTime());
103 }
104
QueryContinueDrag(BOOL fEscapePressed,DWORD grfKeyState)105 STDMETHODIMP WebDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
106 {
107 if (fEscapePressed || !(grfKeyState & (MK_LBUTTON|MK_RBUTTON))) {
108 m_dropped = !fEscapePressed;
109 if (Page* page = m_webView->page())
110 if (Frame* frame = page->mainFrame())
111 //FIXME: We need to figure out how to find out what actually happened in the drag <rdar://problem/5015961>
112 frame->eventHandler()->dragSourceEndedAt(generateMouseEvent(m_webView.get(), false), fEscapePressed ? DragOperationNone : DragOperationCopy);
113 return fEscapePressed? DRAGDROP_S_CANCEL : DRAGDROP_S_DROP;
114 } else if (Page* page = m_webView->page())
115 if (Frame* frame = page->mainFrame())
116 frame->eventHandler()->dragSourceMovedTo(generateMouseEvent(m_webView.get(), true));
117
118 return S_OK;
119 }
120
GiveFeedback(DWORD)121 STDMETHODIMP WebDropSource::GiveFeedback(DWORD)
122 {
123 return DRAGDROP_S_USEDEFAULTCURSORS;
124 }
125