WebDropSource.cpp revision 1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353
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 <WebCore/SystemTime.h>
38
39#include <Windows.h>
40
41using namespace WebCore;
42
43
44HRESULT 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
52WebDropSource::WebDropSource(WebView* webView)
53: m_ref(1)
54, m_dropped(false)
55, m_webView(webView)
56{
57    gClassCount++;
58    gClassNameCount.add("WebDropSource");
59}
60
61WebDropSource::~WebDropSource()
62{
63    gClassCount--;
64    gClassNameCount.remove("WebDropSource");
65}
66
67STDMETHODIMP 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
81STDMETHODIMP_(ULONG) WebDropSource::AddRef(void)
82{
83    return InterlockedIncrement(&m_ref);
84}
85
86STDMETHODIMP_(ULONG) WebDropSource::Release(void)
87{
88    long c = InterlockedDecrement(&m_ref);
89    if (c == 0)
90        delete this;
91    return c;
92}
93
94PlatformMouseEvent 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
105STDMETHODIMP WebDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
106{
107    if(fEscapePressed)
108        return DRAGDROP_S_CANCEL;
109
110    if(!(grfKeyState & (MK_LBUTTON|MK_RBUTTON))) {
111        m_dropped = true;
112        if (Page* page = m_webView->page())
113            if (Frame* frame = page->mainFrame())
114                //FIXME: We need to figure out how to find out what actually happened in the drag <rdar://problem/5015961>
115                frame->eventHandler()->dragSourceEndedAt(generateMouseEvent(m_webView.get(), false), DragOperationCopy);
116        return DRAGDROP_S_DROP;
117    } else if (Page* page = m_webView->page())
118        if (Frame* frame = page->mainFrame())
119            frame->eventHandler()->dragSourceMovedTo(generateMouseEvent(m_webView.get(), true));
120
121    return S_OK;
122
123}
124
125STDMETHODIMP WebDropSource::GiveFeedback(DWORD)
126{
127    return DRAGDROP_S_USEDEFAULTCURSORS;
128}
129