2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebDropSource.h"
29 #include "WebKitDLL.h"
32 #include <WebCore/Cursor.h>
33 #include <WebCore/DragActions.h>
34 #include <WebCore/EventHandler.h>
35 #include <WebCore/Frame.h>
36 #include <WebCore/FrameView.h>
37 #include <WebCore/Page.h>
38 #include <WebCore/PlatformMouseEvent.h>
39 #include <wtf/CurrentTime.h>
43 using namespace WebCore;
46 HRESULT WebDropSource::createInstance(WebView* webView, IDropSource** result)
48 if (!webView || !result)
50 *result = new WebDropSource(webView);
54 WebDropSource::WebDropSource(WebView* webView)
60 gClassNameCount.add("WebDropSource");
63 WebDropSource::~WebDropSource()
66 gClassNameCount.remove("WebDropSource");
69 STDMETHODIMP WebDropSource::QueryInterface(REFIID riid, void** ppvObject)
72 if (IsEqualIID(riid, IID_IUnknown) ||
73 IsEqualIID(riid, IID_IDropSource)) {
83 STDMETHODIMP_(ULONG) WebDropSource::AddRef(void)
85 return InterlockedIncrement(&m_ref);
88 STDMETHODIMP_(ULONG) WebDropSource::Release(void)
90 long c = InterlockedDecrement(&m_ref);
96 PlatformMouseEvent generateMouseEvent(WebView* webView, bool isDrag)
99 ::GetCursorPos((LPPOINT)&pt);
102 if (SUCCEEDED(webView->viewWindow((OLE_HANDLE*)&viewWindow)))
103 ::ScreenToClient(viewWindow, (LPPOINT)&localpt);
104 return PlatformMouseEvent(IntPoint(localpt.x, localpt.y), IntPoint(pt.x, pt.y),
105 isDrag ? LeftButton : NoButton, PlatformEvent::MouseMoved, 0, false, false, false, false, currentTime());
108 STDMETHODIMP WebDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
110 if (fEscapePressed || !(grfKeyState & (MK_LBUTTON|MK_RBUTTON))) {
111 m_dropped = !fEscapePressed;
112 return fEscapePressed? DRAGDROP_S_CANCEL : DRAGDROP_S_DROP;
118 STDMETHODIMP WebDropSource::GiveFeedback(DWORD dwEffect)
120 BOOL showCustomCursors;
121 if (FAILED(WebPreferences::sharedStandardPreferences()->customDragCursorsEnabled(&showCustomCursors)))
122 return DRAGDROP_S_USEDEFAULTCURSORS;
124 // If we don't want to hide the stop icon, let Windows select the cursor.
125 if (!showCustomCursors)
126 return DRAGDROP_S_USEDEFAULTCURSORS;
128 // If we are going to show something other than the not allowed arrow, then let Windows
130 if (dwEffect != DROPEFFECT_NONE)
131 return DRAGDROP_S_USEDEFAULTCURSORS;
134 if (FAILED(m_webView->viewWindow(reinterpret_cast<OLE_HANDLE*>(&viewWindow))))
135 return DRAGDROP_S_USEDEFAULTCURSORS;
138 GetWindowRect(viewWindow, &webViewRect);
141 GetCursorPos(&cursorPoint);
143 if (!PtInRect(&webViewRect, cursorPoint)) {
144 // If our cursor is outside the bounds of the webView, we want to let Windows select the cursor.
145 return DRAGDROP_S_USEDEFAULTCURSORS;
148 FrameView* view = m_webView->page()->mainFrame()->view();
150 return DRAGDROP_S_USEDEFAULTCURSORS;
152 // When dragging inside a WebView and the drag is not allowed, don't show the not allowed icon,
153 // instead, show the pointer cursor.
154 // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
155 view->setCursor(pointerCursor());