Backed out changeset 8517afe50156 (bug 540456) for reftest failures.
[gecko.git] / widget / windows / nsNativeDragTarget.cpp
blobb728f3d63c7a82759249b37b7263a3ca6deb22a6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <stdio.h>
7 #include "nsIDragService.h"
8 #include "nsWidgetsCID.h"
9 #include "nsNativeDragTarget.h"
10 #include "nsDragService.h"
11 #include "nsIServiceManager.h"
12 #include "nsIDOMNode.h"
13 #include "nsCOMPtr.h"
15 #include "nsIWidget.h"
16 #include "nsWindow.h"
17 #include "nsClipboard.h"
18 #include "KeyboardLayout.h"
20 #include "mozilla/MouseEvents.h"
22 using namespace mozilla;
23 using namespace mozilla::widget;
25 /* Define Class IDs */
26 static NS_DEFINE_IID(kCDragServiceCID, NS_DRAGSERVICE_CID);
28 /* Define Interface IDs */
29 static NS_DEFINE_IID(kIDragServiceIID, NS_IDRAGSERVICE_IID);
31 // This is cached for Leave notification
32 static POINTL gDragLastPoint;
35 * class nsNativeDragTarget
37 nsNativeDragTarget::nsNativeDragTarget(nsIWidget * aWidget)
38 : m_cRef(0),
39 mEffectsAllowed(DROPEFFECT_MOVE | DROPEFFECT_COPY | DROPEFFECT_LINK),
40 mEffectsPreferred(DROPEFFECT_NONE),
41 mTookOwnRef(false), mWidget(aWidget), mDropTargetHelper(nullptr)
43 mHWnd = (HWND)mWidget->GetNativeData(NS_NATIVE_WINDOW);
46 * Create/Get the DragService that we have implemented
48 CallGetService(kCDragServiceCID, &mDragService);
51 nsNativeDragTarget::~nsNativeDragTarget()
53 NS_RELEASE(mDragService);
55 if (mDropTargetHelper) {
56 mDropTargetHelper->Release();
57 mDropTargetHelper = nullptr;
61 // IUnknown methods - see iunknown.h for documentation
62 STDMETHODIMP
63 nsNativeDragTarget::QueryInterface(REFIID riid, void** ppv)
65 *ppv=nullptr;
67 if (IID_IUnknown == riid || IID_IDropTarget == riid)
68 *ppv=this;
70 if (nullptr!=*ppv) {
71 ((LPUNKNOWN)*ppv)->AddRef();
72 return S_OK;
75 return E_NOINTERFACE;
78 STDMETHODIMP_(ULONG)
79 nsNativeDragTarget::AddRef(void)
81 ++m_cRef;
82 NS_LOG_ADDREF(this, m_cRef, "nsNativeDragTarget", sizeof(*this));
83 return m_cRef;
86 STDMETHODIMP_(ULONG) nsNativeDragTarget::Release(void)
88 --m_cRef;
89 NS_LOG_RELEASE(this, m_cRef, "nsNativeDragTarget");
90 if (0 != m_cRef)
91 return m_cRef;
93 delete this;
94 return 0;
97 void
98 nsNativeDragTarget::GetGeckoDragAction(DWORD grfKeyState, LPDWORD pdwEffect,
99 uint32_t * aGeckoAction)
101 // If a window is disabled or a modal window is on top of it
102 // (which implies it is disabled), then we should not allow dropping.
103 if (!mWidget->IsEnabled()) {
104 *pdwEffect = DROPEFFECT_NONE;
105 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
106 return;
109 // If the user explicitly uses a modifier key, they want the associated action
110 // Shift + Control -> LINK, Shift -> MOVE, Ctrl -> COPY
111 DWORD desiredEffect = DROPEFFECT_NONE;
112 if ((grfKeyState & MK_CONTROL) && (grfKeyState & MK_SHIFT)) {
113 desiredEffect = DROPEFFECT_LINK;
114 } else if (grfKeyState & MK_SHIFT) {
115 desiredEffect = DROPEFFECT_MOVE;
116 } else if (grfKeyState & MK_CONTROL) {
117 desiredEffect = DROPEFFECT_COPY;
120 // Determine the desired effect from what is allowed and preferred.
121 if (!(desiredEffect &= mEffectsAllowed)) {
122 // No modifier key effect is set which is also allowed, check
123 // the preference of the data.
124 desiredEffect = mEffectsPreferred & mEffectsAllowed;
125 if (!desiredEffect) {
126 // No preference is set, so just fall back to the allowed effect itself
127 desiredEffect = mEffectsAllowed;
131 // Otherwise we should specify the first available effect
132 // from MOVE, COPY, or LINK.
133 if (desiredEffect & DROPEFFECT_MOVE) {
134 *pdwEffect = DROPEFFECT_MOVE;
135 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_MOVE;
136 } else if (desiredEffect & DROPEFFECT_COPY) {
137 *pdwEffect = DROPEFFECT_COPY;
138 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_COPY;
139 } else if (desiredEffect & DROPEFFECT_LINK) {
140 *pdwEffect = DROPEFFECT_LINK;
141 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_LINK;
142 } else {
143 *pdwEffect = DROPEFFECT_NONE;
144 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
148 inline
149 bool
150 IsKeyDown(char key)
152 return GetKeyState(key) < 0;
155 void
156 nsNativeDragTarget::DispatchDragDropEvent(uint32_t aEventType, POINTL aPT)
158 nsEventStatus status;
159 WidgetDragEvent event(true, aEventType, mWidget);
161 nsWindow * win = static_cast<nsWindow *>(mWidget);
162 win->InitEvent(event);
163 POINT cpos;
165 cpos.x = aPT.x;
166 cpos.y = aPT.y;
168 if (mHWnd != nullptr) {
169 ::ScreenToClient(mHWnd, &cpos);
170 event.refPoint.x = cpos.x;
171 event.refPoint.y = cpos.y;
172 } else {
173 event.refPoint.x = 0;
174 event.refPoint.y = 0;
177 ModifierKeyState modifierKeyState;
178 modifierKeyState.InitInputEvent(event);
180 event.inputSource = static_cast<nsBaseDragService*>(mDragService)->GetInputSource();
182 mWidget->DispatchEvent(&event, status);
185 void
186 nsNativeDragTarget::ProcessDrag(uint32_t aEventType,
187 DWORD grfKeyState,
188 POINTL ptl,
189 DWORD* pdwEffect)
191 // Before dispatching the event make sure we have the correct drop action set
192 uint32_t geckoAction;
193 GetGeckoDragAction(grfKeyState, pdwEffect, &geckoAction);
195 // Set the current action into the Gecko specific type
196 nsCOMPtr<nsIDragSession> currSession;
197 mDragService->GetCurrentSession(getter_AddRefs(currSession));
198 if (!currSession) {
199 return;
202 currSession->SetDragAction(geckoAction);
204 // Dispatch the event into Gecko
205 DispatchDragDropEvent(aEventType, ptl);
207 if (aEventType != NS_DRAGDROP_DROP) {
208 // Get the cached drag effect from the drag service, the data member should
209 // have been set by whoever handled the WidgetGUIEvent or nsIDOMEvent on
210 // drags.
211 bool canDrop;
212 currSession->GetCanDrop(&canDrop);
213 if (!canDrop) {
214 *pdwEffect = DROPEFFECT_NONE;
218 // Clear the cached value
219 currSession->SetCanDrop(false);
222 // IDropTarget methods
223 STDMETHODIMP
224 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource,
225 DWORD grfKeyState,
226 POINTL ptl,
227 DWORD* pdwEffect)
229 if (!mDragService) {
230 return E_FAIL;
233 mEffectsAllowed = *pdwEffect;
234 AddLinkSupportIfCanBeGenerated(pIDataSource);
236 // Drag and drop image helper
237 if (GetDropTargetHelper()) {
238 POINT pt = { ptl.x, ptl.y };
239 GetDropTargetHelper()->DragEnter(mHWnd, pIDataSource, &pt, *pdwEffect);
242 // save a ref to this, in case the window is destroyed underneath us
243 NS_ASSERTION(!mTookOwnRef, "own ref already taken!");
244 this->AddRef();
245 mTookOwnRef = true;
247 // tell the drag service about this drag (it may have come from an
248 // outside app).
249 mDragService->StartDragSession();
251 void* tempOutData = nullptr;
252 uint32_t tempDataLen = 0;
253 nsresult loadResult = nsClipboard::GetNativeDataOffClipboard(
254 pIDataSource, 0, ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT), nullptr, &tempOutData, &tempDataLen);
255 if (NS_SUCCEEDED(loadResult) && tempOutData) {
256 mEffectsPreferred = *((DWORD*)tempOutData);
257 nsMemory::Free(tempOutData);
258 } else {
259 // We have no preference if we can't obtain it
260 mEffectsPreferred = DROPEFFECT_NONE;
263 // Set the native data object into drag service
265 // This cast is ok because in the constructor we created a
266 // the actual implementation we wanted, so we know this is
267 // a nsDragService. It should be a private interface, though.
268 nsDragService * winDragService =
269 static_cast<nsDragService *>(mDragService);
270 winDragService->SetIDataObject(pIDataSource);
272 // Now process the native drag state and then dispatch the event
273 ProcessDrag(NS_DRAGDROP_ENTER, grfKeyState, ptl, pdwEffect);
275 return S_OK;
278 void
279 nsNativeDragTarget::AddLinkSupportIfCanBeGenerated(LPDATAOBJECT aIDataSource)
281 // If we don't have a link effect, but we can generate one, fix the
282 // drop effect to include it.
283 if (!(mEffectsAllowed & DROPEFFECT_LINK) && aIDataSource) {
284 if (S_OK == ::OleQueryLinkFromData(aIDataSource)) {
285 mEffectsAllowed |= DROPEFFECT_LINK;
290 STDMETHODIMP
291 nsNativeDragTarget::DragOver(DWORD grfKeyState,
292 POINTL ptl,
293 LPDWORD pdwEffect)
295 if (!mDragService) {
296 return E_FAIL;
299 // If a LINK effect could be generated previously from a DragEnter(),
300 // then we should include it as an allowed effect.
301 mEffectsAllowed = (*pdwEffect) | (mEffectsAllowed & DROPEFFECT_LINK);
303 nsCOMPtr<nsIDragSession> currentDragSession;
304 mDragService->GetCurrentSession(getter_AddRefs(currentDragSession));
305 if (!currentDragSession) {
306 return S_OK; // Drag was canceled.
309 // without the AddRef() |this| can get destroyed in an event handler
310 this->AddRef();
312 // Drag and drop image helper
313 if (GetDropTargetHelper()) {
314 POINT pt = { ptl.x, ptl.y };
315 GetDropTargetHelper()->DragOver(&pt, *pdwEffect);
318 mDragService->FireDragEventAtSource(NS_DRAGDROP_DRAG);
319 // Now process the native drag state and then dispatch the event
320 ProcessDrag(NS_DRAGDROP_OVER, grfKeyState, ptl, pdwEffect);
322 this->Release();
324 return S_OK;
327 STDMETHODIMP
328 nsNativeDragTarget::DragLeave()
330 if (!mDragService) {
331 return E_FAIL;
334 // Drag and drop image helper
335 if (GetDropTargetHelper()) {
336 GetDropTargetHelper()->DragLeave();
339 // dispatch the event into Gecko
340 DispatchDragDropEvent(NS_DRAGDROP_EXIT, gDragLastPoint);
342 nsCOMPtr<nsIDragSession> currentDragSession;
343 mDragService->GetCurrentSession(getter_AddRefs(currentDragSession));
345 if (currentDragSession) {
346 nsCOMPtr<nsIDOMNode> sourceNode;
347 currentDragSession->GetSourceNode(getter_AddRefs(sourceNode));
349 if (!sourceNode) {
350 // We're leaving a window while doing a drag that was
351 // initiated in a different app. End the drag session, since
352 // we're done with it for now (until the user drags back into
353 // mozilla).
354 mDragService->EndDragSession(false);
358 // release the ref that was taken in DragEnter
359 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
360 if (mTookOwnRef) {
361 this->Release();
362 mTookOwnRef = false;
365 return S_OK;
368 void
369 nsNativeDragTarget::DragCancel()
371 // Cancel the drag session if we did DragEnter.
372 if (mTookOwnRef) {
373 if (GetDropTargetHelper()) {
374 GetDropTargetHelper()->DragLeave();
376 if (mDragService) {
377 mDragService->EndDragSession(false);
379 this->Release(); // matching the AddRef in DragEnter
380 mTookOwnRef = false;
384 STDMETHODIMP
385 nsNativeDragTarget::Drop(LPDATAOBJECT pData,
386 DWORD grfKeyState,
387 POINTL aPT,
388 LPDWORD pdwEffect)
390 if (!mDragService) {
391 return E_FAIL;
394 mEffectsAllowed = *pdwEffect;
395 AddLinkSupportIfCanBeGenerated(pData);
397 // Drag and drop image helper
398 if (GetDropTargetHelper()) {
399 POINT pt = { aPT.x, aPT.y };
400 GetDropTargetHelper()->Drop(pData, &pt, *pdwEffect);
403 // Set the native data object into the drag service
405 // This cast is ok because in the constructor we created a
406 // the actual implementation we wanted, so we know this is
407 // a nsDragService (but it should still be a private interface)
408 nsDragService* winDragService = static_cast<nsDragService*>(mDragService);
409 winDragService->SetIDataObject(pData);
411 // NOTE: ProcessDrag spins the event loop which may destroy arbitrary objects.
412 // We use strong refs to prevent it from destroying these:
413 nsRefPtr<nsNativeDragTarget> kungFuDeathGrip = this;
414 nsCOMPtr<nsIDragService> serv = mDragService;
416 // Now process the native drag state and then dispatch the event
417 ProcessDrag(NS_DRAGDROP_DROP, grfKeyState, aPT, pdwEffect);
419 nsCOMPtr<nsIDragSession> currentDragSession;
420 serv->GetCurrentSession(getter_AddRefs(currentDragSession));
421 if (!currentDragSession) {
422 return S_OK; // DragCancel() was called.
425 // Let the win drag service know whether this session experienced
426 // a drop event within the application. Drop will not oocur if the
427 // drop landed outside the app. (used in tab tear off, bug 455884)
428 winDragService->SetDroppedLocal();
430 // tell the drag service we're done with the session
431 // Use GetMessagePos to get the position of the mouse at the last message
432 // seen by the event loop. (Bug 489729)
433 DWORD pos = ::GetMessagePos();
434 POINT cpos;
435 cpos.x = GET_X_LPARAM(pos);
436 cpos.y = GET_Y_LPARAM(pos);
437 winDragService->SetDragEndPoint(nsIntPoint(cpos.x, cpos.y));
438 serv->EndDragSession(true);
440 // release the ref that was taken in DragEnter
441 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
442 if (mTookOwnRef) {
443 this->Release();
444 mTookOwnRef = false;
447 return S_OK;
451 * By lazy loading mDropTargetHelper we save 50-70ms of startup time
452 * which is ~5% of startup time.
454 IDropTargetHelper*
455 nsNativeDragTarget::GetDropTargetHelper()
457 if (!mDropTargetHelper) {
458 CoCreateInstance(CLSID_DragDropHelper, nullptr, CLSCTX_INPROC_SERVER,
459 IID_IDropTargetHelper, (LPVOID*)&mDropTargetHelper);
462 return mDropTargetHelper;