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/. */
7 #include "nsIDragService.h"
8 #include "nsWidgetsCID.h"
9 #include "nsNativeDragTarget.h"
10 #include "nsDragService.h"
14 #include "nsIWidget.h"
16 #include "nsClipboard.h"
17 #include "KeyboardLayout.h"
19 #include "mozilla/dom/MouseEventBinding.h"
20 #include "mozilla/MouseEvents.h"
22 using namespace mozilla
;
23 using namespace mozilla::widget
;
25 // This is cached for Leave notification
26 static POINTL gDragLastPoint
;
28 bool nsNativeDragTarget::gDragImageChanged
= false;
31 * class nsNativeDragTarget
33 nsNativeDragTarget::nsNativeDragTarget(nsIWidget
* aWidget
)
35 mEffectsAllowed(DROPEFFECT_MOVE
| DROPEFFECT_COPY
| DROPEFFECT_LINK
),
36 mEffectsPreferred(DROPEFFECT_NONE
),
39 mDropTargetHelper(nullptr) {
40 mHWnd
= (HWND
)mWidget
->GetNativeData(NS_NATIVE_WINDOW
);
42 mDragService
= do_GetService("@mozilla.org/widget/dragservice;1");
45 nsNativeDragTarget::~nsNativeDragTarget() {
46 if (mDropTargetHelper
) {
47 mDropTargetHelper
->Release();
48 mDropTargetHelper
= nullptr;
52 // IUnknown methods - see iunknown.h for documentation
54 nsNativeDragTarget::QueryInterface(REFIID riid
, void** ppv
) {
57 if (IID_IUnknown
== riid
|| IID_IDropTarget
== riid
) *ppv
= this;
59 if (nullptr != *ppv
) {
60 ((LPUNKNOWN
)*ppv
)->AddRef();
68 nsNativeDragTarget::AddRef(void) {
70 NS_LOG_ADDREF(this, m_cRef
, "nsNativeDragTarget", sizeof(*this));
74 STDMETHODIMP_(ULONG
) nsNativeDragTarget::Release(void) {
76 NS_LOG_RELEASE(this, m_cRef
, "nsNativeDragTarget");
77 if (0 != m_cRef
) return m_cRef
;
83 void nsNativeDragTarget::GetGeckoDragAction(DWORD grfKeyState
,
85 uint32_t* aGeckoAction
) {
86 // If a window is disabled or a modal window is on top of it
87 // (which implies it is disabled), then we should not allow dropping.
88 if (!mWidget
->IsEnabled()) {
89 *pdwEffect
= DROPEFFECT_NONE
;
90 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_NONE
;
94 // If the user explicitly uses a modifier key, they want the associated action
95 // Shift + Control -> LINK, Shift -> MOVE, Ctrl -> COPY
96 DWORD desiredEffect
= DROPEFFECT_NONE
;
97 if ((grfKeyState
& MK_CONTROL
) && (grfKeyState
& MK_SHIFT
)) {
98 desiredEffect
= DROPEFFECT_LINK
;
99 } else if (grfKeyState
& MK_SHIFT
) {
100 desiredEffect
= DROPEFFECT_MOVE
;
101 } else if (grfKeyState
& MK_CONTROL
) {
102 desiredEffect
= DROPEFFECT_COPY
;
105 // Determine the desired effect from what is allowed and preferred.
106 if (!(desiredEffect
&= mEffectsAllowed
)) {
107 // No modifier key effect is set which is also allowed, check
108 // the preference of the data.
109 desiredEffect
= mEffectsPreferred
& mEffectsAllowed
;
110 if (!desiredEffect
) {
111 // No preference is set, so just fall back to the allowed effect itself
112 desiredEffect
= mEffectsAllowed
;
116 // Otherwise we should specify the first available effect
117 // from MOVE, COPY, or LINK.
118 if (desiredEffect
& DROPEFFECT_MOVE
) {
119 *pdwEffect
= DROPEFFECT_MOVE
;
120 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_MOVE
;
121 } else if (desiredEffect
& DROPEFFECT_COPY
) {
122 *pdwEffect
= DROPEFFECT_COPY
;
123 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_COPY
;
124 } else if (desiredEffect
& DROPEFFECT_LINK
) {
125 *pdwEffect
= DROPEFFECT_LINK
;
126 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_LINK
;
128 *pdwEffect
= DROPEFFECT_NONE
;
129 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_NONE
;
133 inline bool IsKeyDown(char key
) { return GetKeyState(key
) < 0; }
135 void nsNativeDragTarget::DispatchDragDropEvent(EventMessage aEventMessage
,
137 WidgetDragEvent
event(true, aEventMessage
, mWidget
);
139 nsWindow
* win
= static_cast<nsWindow
*>(mWidget
);
140 win
->InitEvent(event
);
146 if (mHWnd
!= nullptr) {
147 ::ScreenToClient(mHWnd
, &cpos
);
148 event
.mRefPoint
= LayoutDeviceIntPoint(cpos
.x
, cpos
.y
);
150 event
.mRefPoint
= LayoutDeviceIntPoint(0, 0);
153 ModifierKeyState modifierKeyState
;
154 modifierKeyState
.InitInputEvent(event
);
156 nsDragSession
* currSession
=
157 static_cast<nsDragSession
*>(mDragService
->GetCurrentSession(mWidget
));
159 event
.mInputSource
= currSession
->GetInputSource();
161 event
.mInputSource
= dom::MouseEvent_Binding::MOZ_SOURCE_MOUSE
;
164 mWidget
->DispatchInputEvent(&event
);
167 void nsNativeDragTarget::ProcessDrag(EventMessage aEventMessage
,
168 DWORD grfKeyState
, POINTL ptl
,
170 // Before dispatching the event make sure we have the correct drop action set
171 uint32_t geckoAction
;
172 GetGeckoDragAction(grfKeyState
, pdwEffect
, &geckoAction
);
174 // Set the current action into the Gecko specific type
175 RefPtr
<nsDragSession
> currSession
=
176 static_cast<nsDragSession
*>(mDragService
->GetCurrentSession(mWidget
));
181 currSession
->SetDragAction(geckoAction
);
183 // Dispatch the event into Gecko
184 DispatchDragDropEvent(aEventMessage
, ptl
);
186 // If TakeChildProcessDragAction returns something other than
187 // DRAGDROP_ACTION_UNINITIALIZED, it means that the last event was sent
188 // to the child process and this event is also being sent to the child
189 // process. In this case, use the last event's action instead.
190 currSession
->GetDragAction(&geckoAction
);
192 int32_t childDragAction
= currSession
->TakeChildProcessDragAction();
193 if (childDragAction
!= nsIDragService::DRAGDROP_ACTION_UNINITIALIZED
) {
194 geckoAction
= childDragAction
;
197 if (nsIDragService::DRAGDROP_ACTION_LINK
& geckoAction
) {
198 *pdwEffect
= DROPEFFECT_LINK
;
199 } else if (nsIDragService::DRAGDROP_ACTION_COPY
& geckoAction
) {
200 *pdwEffect
= DROPEFFECT_COPY
;
201 } else if (nsIDragService::DRAGDROP_ACTION_MOVE
& geckoAction
) {
202 *pdwEffect
= DROPEFFECT_MOVE
;
204 *pdwEffect
= DROPEFFECT_NONE
;
207 if (aEventMessage
!= eDrop
) {
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
212 currSession
->GetCanDrop(&canDrop
);
214 *pdwEffect
= DROPEFFECT_NONE
;
218 // Clear the cached value
219 currSession
->SetCanDrop(false);
222 // IDropTarget methods
224 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource
, DWORD grfKeyState
,
225 POINTL ptl
, DWORD
* pdwEffect
) {
230 mEffectsAllowed
= *pdwEffect
;
231 AddLinkSupportIfCanBeGenerated(pIDataSource
);
233 // Drag and drop image helper
234 if (GetDropTargetHelper()) {
235 // We get a lot of crashes (often uncaught by our handler) later on during
236 // DragOver calls, see bug 1465513. It looks like this might be because
237 // we're not cleaning up previous drags fully and now released resources get
238 // used. Calling IDropTargetHelper::DragLeave before DragEnter seems to fix
239 // this for at least one reproduction of this crash.
240 GetDropTargetHelper()->DragLeave();
241 POINT pt
= {ptl
.x
, ptl
.y
};
242 GetDropTargetHelper()->DragEnter(mHWnd
, pIDataSource
, &pt
, *pdwEffect
);
245 // save a ref to this, in case the window is destroyed underneath us
246 NS_ASSERTION(!mTookOwnRef
, "own ref already taken!");
250 // tell the drag service about this drag (it may have come from an
252 RefPtr
<nsDragSession
> session
=
253 static_cast<nsDragSession
*>(mDragService
->StartDragSession(mWidget
));
256 void* tempOutData
= nullptr;
257 uint32_t tempDataLen
= 0;
258 nsresult loadResult
= nsClipboard::GetNativeDataOffClipboard(
259 pIDataSource
, 0, ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT
),
260 nullptr, &tempOutData
, &tempDataLen
);
261 if (NS_SUCCEEDED(loadResult
) && tempOutData
) {
262 mEffectsPreferred
= *((DWORD
*)tempOutData
);
265 // We have no preference if we can't obtain it
266 mEffectsPreferred
= DROPEFFECT_NONE
;
269 // Set the native data object into drag session
270 session
->SetIDataObject(pIDataSource
);
272 // Now process the native drag state and then dispatch the event
273 ProcessDrag(eDragEnter
, grfKeyState
, ptl
, pdwEffect
);
278 void nsNativeDragTarget::AddLinkSupportIfCanBeGenerated(
279 LPDATAOBJECT aIDataSource
) {
280 // If we don't have a link effect, but we can generate one, fix the
281 // drop effect to include it.
282 if (!(mEffectsAllowed
& DROPEFFECT_LINK
) && aIDataSource
) {
283 if (S_OK
== ::OleQueryLinkFromData(aIDataSource
)) {
284 mEffectsAllowed
|= DROPEFFECT_LINK
;
290 nsNativeDragTarget::DragOver(DWORD grfKeyState
, POINTL ptl
, LPDWORD pdwEffect
) {
295 bool dragImageChanged
= gDragImageChanged
;
296 gDragImageChanged
= false;
298 // If a LINK effect could be generated previously from a DragEnter(),
299 // then we should include it as an allowed effect.
300 mEffectsAllowed
= (*pdwEffect
) | (mEffectsAllowed
& DROPEFFECT_LINK
);
302 RefPtr
<nsDragSession
> currentDragSession
=
303 static_cast<nsDragSession
*>(mDragService
->GetCurrentSession(mWidget
));
304 if (!currentDragSession
) {
305 return S_OK
; // Drag was canceled.
308 // without the AddRef() |this| can get destroyed in an event handler
311 // Drag and drop image helper
312 if (GetDropTargetHelper()) {
313 if (dragImageChanged
) {
314 // See comment in nsNativeDragTarget::DragEnter.
315 GetDropTargetHelper()->DragLeave();
316 // The drop helper only updates the image during DragEnter, so emulate
317 // a DragEnter if the image was changed.
318 POINT pt
= {ptl
.x
, ptl
.y
};
319 GetDropTargetHelper()->DragEnter(
320 mHWnd
, currentDragSession
->GetDataObject(), &pt
, *pdwEffect
);
322 POINT pt
= {ptl
.x
, ptl
.y
};
323 GetDropTargetHelper()->DragOver(&pt
, *pdwEffect
);
326 ModifierKeyState modifierKeyState
;
327 currentDragSession
->FireDragEventAtSource(eDrag
,
328 modifierKeyState
.GetModifiers());
329 // Now process the native drag state and then dispatch the event
330 ProcessDrag(eDragOver
, grfKeyState
, ptl
, pdwEffect
);
338 nsNativeDragTarget::DragLeave() {
343 // Drag and drop image helper
344 if (GetDropTargetHelper()) {
345 GetDropTargetHelper()->DragLeave();
348 // dispatch the event into Gecko
349 DispatchDragDropEvent(eDragExit
, gDragLastPoint
);
351 nsCOMPtr
<nsIDragSession
> currentDragSession
=
352 mDragService
->GetCurrentSession(mWidget
);
354 if (currentDragSession
) {
355 nsCOMPtr
<nsINode
> sourceNode
;
356 currentDragSession
->GetSourceNode(getter_AddRefs(sourceNode
));
359 // We're leaving a window while doing a drag that was
360 // initiated in a different app. End the drag session, since
361 // we're done with it for now (until the user drags back into
363 ModifierKeyState modifierKeyState
;
364 currentDragSession
->EndDragSession(false,
365 modifierKeyState
.GetModifiers());
369 // release the ref that was taken in DragEnter
370 NS_ASSERTION(mTookOwnRef
, "want to release own ref, but not taken!");
379 void nsNativeDragTarget::DragCancel() {
380 // Cancel the drag session if we did DragEnter.
382 if (GetDropTargetHelper()) {
383 GetDropTargetHelper()->DragLeave();
386 ModifierKeyState modifierKeyState
;
387 RefPtr
<nsIDragSession
> session
= mDragService
->GetCurrentSession(mWidget
);
389 session
->EndDragSession(false, modifierKeyState
.GetModifiers());
392 this->Release(); // matching the AddRef in DragEnter
398 nsNativeDragTarget::Drop(LPDATAOBJECT pData
, DWORD grfKeyState
, POINTL aPT
,
404 mEffectsAllowed
= *pdwEffect
;
405 AddLinkSupportIfCanBeGenerated(pData
);
407 // Drag and drop image helper
408 if (GetDropTargetHelper()) {
409 POINT pt
= {aPT
.x
, aPT
.y
};
410 GetDropTargetHelper()->Drop(pData
, &pt
, *pdwEffect
);
413 // Set the native data object into the drag service
414 RefPtr
<nsDragSession
> currentDragSession
=
415 static_cast<nsDragSession
*>(mDragService
->GetCurrentSession(mWidget
));
416 if (!currentDragSession
) {
419 currentDragSession
->SetIDataObject(pData
);
421 // NOTE: ProcessDrag spins the event loop which may destroy arbitrary objects.
422 // We use strong refs to prevent it from destroying these:
423 RefPtr
<nsNativeDragTarget
> kungFuDeathGrip
= this;
425 // Now process the native drag state and then dispatch the event
426 ProcessDrag(eDrop
, grfKeyState
, aPT
, pdwEffect
);
429 static_cast<nsDragSession
*>(mDragService
->GetCurrentSession(mWidget
));
430 if (!currentDragSession
) {
431 return S_OK
; // DragCancel() was called.
434 // Let the win drag session know whether it experienced
435 // a drop event within the application. Drop will not oocur if the
436 // drop landed outside the app. (used in tab tear off, bug 455884)
437 currentDragSession
->SetDroppedLocal();
439 // Tell the drag session we're done with it.
440 // Use GetMessagePos to get the position of the mouse at the last message
441 // seen by the event loop. (Bug 489729)
442 DWORD pos
= ::GetMessagePos();
444 cpos
.x
= GET_X_LPARAM(pos
);
445 cpos
.y
= GET_Y_LPARAM(pos
);
446 currentDragSession
->SetDragEndPoint(cpos
.x
, cpos
.y
);
447 ModifierKeyState modifierKeyState
;
448 currentDragSession
->EndDragSession(true, modifierKeyState
.GetModifiers());
450 // release the ref that was taken in DragEnter
451 NS_ASSERTION(mTookOwnRef
, "want to release own ref, but not taken!");
461 * By lazy loading mDropTargetHelper we save 50-70ms of startup time
462 * which is ~5% of startup time.
464 IDropTargetHelper
* nsNativeDragTarget::GetDropTargetHelper() {
465 if (!mDropTargetHelper
) {
466 CoCreateInstance(CLSID_DragDropHelper
, nullptr, CLSCTX_INPROC_SERVER
,
467 IID_IDropTargetHelper
, (LPVOID
*)&mDropTargetHelper
);
470 return mDropTargetHelper
;