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"
11 #include "nsIServiceManager.h"
12 #include "nsIDOMNode.h"
15 #include "nsIWidget.h"
17 #include "nsClipboard.h"
18 #include "KeyboardLayout.h"
20 using namespace mozilla::widget
;
22 /* Define Class IDs */
23 static NS_DEFINE_IID(kCDragServiceCID
, NS_DRAGSERVICE_CID
);
25 /* Define Interface IDs */
26 static NS_DEFINE_IID(kIDragServiceIID
, NS_IDRAGSERVICE_IID
);
28 // This is cached for Leave notification
29 static POINTL gDragLastPoint
;
32 * class nsNativeDragTarget
34 nsNativeDragTarget::nsNativeDragTarget(nsIWidget
* aWidget
)
36 mEffectsAllowed(DROPEFFECT_MOVE
| DROPEFFECT_COPY
| DROPEFFECT_LINK
),
37 mEffectsPreferred(DROPEFFECT_NONE
),
38 mTookOwnRef(false), mWidget(aWidget
), mDropTargetHelper(nullptr)
40 mHWnd
= (HWND
)mWidget
->GetNativeData(NS_NATIVE_WINDOW
);
43 * Create/Get the DragService that we have implemented
45 CallGetService(kCDragServiceCID
, &mDragService
);
48 nsNativeDragTarget::~nsNativeDragTarget()
50 NS_RELEASE(mDragService
);
52 if (mDropTargetHelper
) {
53 mDropTargetHelper
->Release();
54 mDropTargetHelper
= nullptr;
58 // IUnknown methods - see iunknown.h for documentation
60 nsNativeDragTarget::QueryInterface(REFIID riid
, void** ppv
)
64 if (IID_IUnknown
== riid
|| IID_IDropTarget
== riid
)
68 ((LPUNKNOWN
)*ppv
)->AddRef();
76 nsNativeDragTarget::AddRef(void)
79 NS_LOG_ADDREF(this, m_cRef
, "nsNativeDragTarget", sizeof(*this));
83 STDMETHODIMP_(ULONG
) nsNativeDragTarget::Release(void)
86 NS_LOG_RELEASE(this, m_cRef
, "nsNativeDragTarget");
95 nsNativeDragTarget::GetGeckoDragAction(DWORD grfKeyState
, LPDWORD pdwEffect
,
96 uint32_t * aGeckoAction
)
98 // If a window is disabled or a modal window is on top of it
99 // (which implies it is disabled), then we should not allow dropping.
100 if (!mWidget
->IsEnabled()) {
101 *pdwEffect
= DROPEFFECT_NONE
;
102 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_NONE
;
106 // If the user explicitly uses a modifier key, they want the associated action
107 // Shift + Control -> LINK, Shift -> MOVE, Ctrl -> COPY
108 DWORD desiredEffect
= DROPEFFECT_NONE
;
109 if ((grfKeyState
& MK_CONTROL
) && (grfKeyState
& MK_SHIFT
)) {
110 desiredEffect
= DROPEFFECT_LINK
;
111 } else if (grfKeyState
& MK_SHIFT
) {
112 desiredEffect
= DROPEFFECT_MOVE
;
113 } else if (grfKeyState
& MK_CONTROL
) {
114 desiredEffect
= DROPEFFECT_COPY
;
117 // Determine the desired effect from what is allowed and preferred.
118 if (!(desiredEffect
&= mEffectsAllowed
)) {
119 // No modifier key effect is set which is also allowed, check
120 // the preference of the data.
121 desiredEffect
= mEffectsPreferred
& mEffectsAllowed
;
122 if (!desiredEffect
) {
123 // No preference is set, so just fall back to the allowed effect itself
124 desiredEffect
= mEffectsAllowed
;
128 // Otherwise we should specify the first available effect
129 // from MOVE, COPY, or LINK.
130 if (desiredEffect
& DROPEFFECT_MOVE
) {
131 *pdwEffect
= DROPEFFECT_MOVE
;
132 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_MOVE
;
133 } else if (desiredEffect
& DROPEFFECT_COPY
) {
134 *pdwEffect
= DROPEFFECT_COPY
;
135 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_COPY
;
136 } else if (desiredEffect
& DROPEFFECT_LINK
) {
137 *pdwEffect
= DROPEFFECT_LINK
;
138 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_LINK
;
140 *pdwEffect
= DROPEFFECT_NONE
;
141 *aGeckoAction
= nsIDragService::DRAGDROP_ACTION_NONE
;
149 return GetKeyState(key
) < 0;
153 nsNativeDragTarget::DispatchDragDropEvent(uint32_t aEventType
, POINTL aPT
)
155 nsEventStatus status
;
156 nsDragEvent
event(true, aEventType
, mWidget
);
158 nsWindow
* win
= static_cast<nsWindow
*>(mWidget
);
159 win
->InitEvent(event
);
166 ::ScreenToClient(mHWnd
, &cpos
);
167 event
.refPoint
.x
= cpos
.x
;
168 event
.refPoint
.y
= cpos
.y
;
170 event
.refPoint
.x
= 0;
171 event
.refPoint
.y
= 0;
174 ModifierKeyState modifierKeyState
;
175 modifierKeyState
.InitInputEvent(event
);
177 event
.inputSource
= static_cast<nsBaseDragService
*>(mDragService
)->GetInputSource();
179 mWidget
->DispatchEvent(&event
, status
);
183 nsNativeDragTarget::ProcessDrag(uint32_t aEventType
,
188 // Before dispatching the event make sure we have the correct drop action set
189 uint32_t geckoAction
;
190 GetGeckoDragAction(grfKeyState
, pdwEffect
, &geckoAction
);
192 // Set the current action into the Gecko specific type
193 nsCOMPtr
<nsIDragSession
> currSession
;
194 mDragService
->GetCurrentSession(getter_AddRefs(currSession
));
199 currSession
->SetDragAction(geckoAction
);
201 // Dispatch the event into Gecko
202 DispatchDragDropEvent(aEventType
, ptl
);
204 if (aEventType
!= NS_DRAGDROP_DROP
) {
205 // Get the cached drag effect from the drag service, the data member should
206 // have been set by whoever handled the nsGUIEvent or nsIDOMEvent on drags.
208 currSession
->GetCanDrop(&canDrop
);
210 *pdwEffect
= DROPEFFECT_NONE
;
214 // Clear the cached value
215 currSession
->SetCanDrop(false);
218 // IDropTarget methods
220 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource
,
229 mEffectsAllowed
= *pdwEffect
;
230 AddLinkSupportIfCanBeGenerated(pIDataSource
);
232 // Drag and drop image helper
233 if (GetDropTargetHelper()) {
234 POINT pt
= { ptl
.x
, ptl
.y
};
235 GetDropTargetHelper()->DragEnter(mHWnd
, pIDataSource
, &pt
, *pdwEffect
);
238 // save a ref to this, in case the window is destroyed underneath us
239 NS_ASSERTION(!mTookOwnRef
, "own ref already taken!");
243 // tell the drag service about this drag (it may have come from an
245 mDragService
->StartDragSession();
247 void* tempOutData
= nullptr;
248 uint32_t tempDataLen
= 0;
249 nsresult loadResult
= nsClipboard::GetNativeDataOffClipboard(
250 pIDataSource
, 0, ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT
), nullptr, &tempOutData
, &tempDataLen
);
251 if (NS_SUCCEEDED(loadResult
) && tempOutData
) {
252 mEffectsPreferred
= *((DWORD
*)tempOutData
);
253 nsMemory::Free(tempOutData
);
255 // We have no preference if we can't obtain it
256 mEffectsPreferred
= DROPEFFECT_NONE
;
259 // Set the native data object into drag service
261 // This cast is ok because in the constructor we created a
262 // the actual implementation we wanted, so we know this is
263 // a nsDragService. It should be a private interface, though.
264 nsDragService
* winDragService
=
265 static_cast<nsDragService
*>(mDragService
);
266 winDragService
->SetIDataObject(pIDataSource
);
268 // Now process the native drag state and then dispatch the event
269 ProcessDrag(NS_DRAGDROP_ENTER
, grfKeyState
, ptl
, pdwEffect
);
275 nsNativeDragTarget::AddLinkSupportIfCanBeGenerated(LPDATAOBJECT aIDataSource
)
277 // If we don't have a link effect, but we can generate one, fix the
278 // drop effect to include it.
279 if (!(mEffectsAllowed
& DROPEFFECT_LINK
) && aIDataSource
) {
280 if (S_OK
== ::OleQueryLinkFromData(aIDataSource
)) {
281 mEffectsAllowed
|= DROPEFFECT_LINK
;
287 nsNativeDragTarget::DragOver(DWORD grfKeyState
,
295 // If a LINK effect could be generated previously from a DragEnter(),
296 // then we should include it as an allowed effect.
297 mEffectsAllowed
= (*pdwEffect
) | (mEffectsAllowed
& DROPEFFECT_LINK
);
299 nsCOMPtr
<nsIDragSession
> currentDragSession
;
300 mDragService
->GetCurrentSession(getter_AddRefs(currentDragSession
));
301 if (!currentDragSession
) {
302 return S_OK
; // Drag was canceled.
305 // without the AddRef() |this| can get destroyed in an event handler
308 // Drag and drop image helper
309 if (GetDropTargetHelper()) {
310 POINT pt
= { ptl
.x
, ptl
.y
};
311 GetDropTargetHelper()->DragOver(&pt
, *pdwEffect
);
314 mDragService
->FireDragEventAtSource(NS_DRAGDROP_DRAG
);
315 // Now process the native drag state and then dispatch the event
316 ProcessDrag(NS_DRAGDROP_OVER
, grfKeyState
, ptl
, pdwEffect
);
324 nsNativeDragTarget::DragLeave()
330 // Drag and drop image helper
331 if (GetDropTargetHelper()) {
332 GetDropTargetHelper()->DragLeave();
335 // dispatch the event into Gecko
336 DispatchDragDropEvent(NS_DRAGDROP_EXIT
, gDragLastPoint
);
338 nsCOMPtr
<nsIDragSession
> currentDragSession
;
339 mDragService
->GetCurrentSession(getter_AddRefs(currentDragSession
));
341 if (currentDragSession
) {
342 nsCOMPtr
<nsIDOMNode
> sourceNode
;
343 currentDragSession
->GetSourceNode(getter_AddRefs(sourceNode
));
346 // We're leaving a window while doing a drag that was
347 // initiated in a different app. End the drag session, since
348 // we're done with it for now (until the user drags back into
350 mDragService
->EndDragSession(false);
354 // release the ref that was taken in DragEnter
355 NS_ASSERTION(mTookOwnRef
, "want to release own ref, but not taken!");
365 nsNativeDragTarget::DragCancel()
367 // Cancel the drag session if we did DragEnter.
369 if (GetDropTargetHelper()) {
370 GetDropTargetHelper()->DragLeave();
373 mDragService
->EndDragSession(false);
375 this->Release(); // matching the AddRef in DragEnter
381 nsNativeDragTarget::Drop(LPDATAOBJECT pData
,
390 mEffectsAllowed
= *pdwEffect
;
391 AddLinkSupportIfCanBeGenerated(pData
);
393 // Drag and drop image helper
394 if (GetDropTargetHelper()) {
395 POINT pt
= { aPT
.x
, aPT
.y
};
396 GetDropTargetHelper()->Drop(pData
, &pt
, *pdwEffect
);
399 // Set the native data object into the drag service
401 // This cast is ok because in the constructor we created a
402 // the actual implementation we wanted, so we know this is
403 // a nsDragService (but it should still be a private interface)
404 nsDragService
* winDragService
= static_cast<nsDragService
*>(mDragService
);
405 winDragService
->SetIDataObject(pData
);
407 // NOTE: ProcessDrag spins the event loop which may destroy arbitrary objects.
408 // We use strong refs to prevent it from destroying these:
409 nsRefPtr
<nsNativeDragTarget
> kungFuDeathGrip
= this;
410 nsCOMPtr
<nsIDragService
> serv
= mDragService
;
412 // Now process the native drag state and then dispatch the event
413 ProcessDrag(NS_DRAGDROP_DROP
, grfKeyState
, aPT
, pdwEffect
);
415 nsCOMPtr
<nsIDragSession
> currentDragSession
;
416 serv
->GetCurrentSession(getter_AddRefs(currentDragSession
));
417 if (!currentDragSession
) {
418 return S_OK
; // DragCancel() was called.
421 // Let the win drag service know whether this session experienced
422 // a drop event within the application. Drop will not oocur if the
423 // drop landed outside the app. (used in tab tear off, bug 455884)
424 winDragService
->SetDroppedLocal();
426 // tell the drag service we're done with the session
427 // Use GetMessagePos to get the position of the mouse at the last message
428 // seen by the event loop. (Bug 489729)
429 DWORD pos
= ::GetMessagePos();
431 cpos
.x
= GET_X_LPARAM(pos
);
432 cpos
.y
= GET_Y_LPARAM(pos
);
433 winDragService
->SetDragEndPoint(nsIntPoint(cpos
.x
, cpos
.y
));
434 serv
->EndDragSession(true);
436 // release the ref that was taken in DragEnter
437 NS_ASSERTION(mTookOwnRef
, "want to release own ref, but not taken!");
447 * By lazy loading mDropTargetHelper we save 50-70ms of startup time
448 * which is ~5% of startup time.
451 nsNativeDragTarget::GetDropTargetHelper()
453 if (!mDropTargetHelper
) {
454 CoCreateInstance(CLSID_DragDropHelper
, NULL
, CLSCTX_INPROC_SERVER
,
455 IID_IDropTargetHelper
, (LPVOID
*)&mDropTargetHelper
);
458 return mDropTargetHelper
;