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/. */
5 
6 #include "nsNativeDragSource.h"
7 #include <stdio.h>
8 #include "nsISupportsImpl.h"
9 #include "nsString.h"
10 #include "nsIServiceManager.h"
11 #include "nsToolkit.h"
12 #include "nsWidgetsCID.h"
13 #include "nsIDragService.h"
14 
15 /*
16  * class nsNativeDragSource
17  */
nsNativeDragSource(nsIDOMDataTransfer * aDataTransfer)18 nsNativeDragSource::nsNativeDragSource(nsIDOMDataTransfer* aDataTransfer)
19     : m_cRef(0), m_hCursor(nullptr), mUserCancelled(false) {
20   mDataTransfer = do_QueryInterface(aDataTransfer);
21 }
22 
~nsNativeDragSource()23 nsNativeDragSource::~nsNativeDragSource() {}
24 
25 STDMETHODIMP
QueryInterface(REFIID riid,void ** ppv)26 nsNativeDragSource::QueryInterface(REFIID riid, void** ppv) {
27   *ppv = nullptr;
28 
29   if (IID_IUnknown == riid || IID_IDropSource == riid) *ppv = this;
30 
31   if (nullptr != *ppv) {
32     ((LPUNKNOWN)*ppv)->AddRef();
33     return S_OK;
34   }
35 
36   return E_NOINTERFACE;
37 }
38 
STDMETHODIMP_(ULONG)39 STDMETHODIMP_(ULONG)
40 nsNativeDragSource::AddRef(void) {
41   ++m_cRef;
42   NS_LOG_ADDREF(this, m_cRef, "nsNativeDragSource", sizeof(*this));
43   return m_cRef;
44 }
45 
STDMETHODIMP_(ULONG)46 STDMETHODIMP_(ULONG)
47 nsNativeDragSource::Release(void) {
48   --m_cRef;
49   NS_LOG_RELEASE(this, m_cRef, "nsNativeDragSource");
50   if (0 != m_cRef) return m_cRef;
51 
52   delete this;
53   return 0;
54 }
55 
56 STDMETHODIMP
QueryContinueDrag(BOOL fEsc,DWORD grfKeyState)57 nsNativeDragSource::QueryContinueDrag(BOOL fEsc, DWORD grfKeyState) {
58   static NS_DEFINE_IID(kCDragServiceCID, NS_DRAGSERVICE_CID);
59 
60   nsCOMPtr<nsIDragService> dragService = do_GetService(kCDragServiceCID);
61   if (dragService) {
62     DWORD pos = ::GetMessagePos();
63     dragService->DragMoved(GET_X_LPARAM(pos), GET_Y_LPARAM(pos));
64   }
65 
66   if (fEsc) {
67     mUserCancelled = true;
68     return DRAGDROP_S_CANCEL;
69   }
70 
71   if (!(grfKeyState & MK_LBUTTON) || (grfKeyState & MK_RBUTTON))
72     return DRAGDROP_S_DROP;
73 
74   return S_OK;
75 }
76 
77 STDMETHODIMP
GiveFeedback(DWORD dwEffect)78 nsNativeDragSource::GiveFeedback(DWORD dwEffect) {
79   // For drags involving tabs, we do some custom work with cursors.
80   if (mDataTransfer) {
81     nsAutoString cursor;
82     mDataTransfer->GetMozCursor(cursor);
83     if (cursor.EqualsLiteral("default")) {
84       m_hCursor = ::LoadCursor(0, IDC_ARROW);
85     } else {
86       m_hCursor = nullptr;
87     }
88   }
89 
90   if (m_hCursor) {
91     ::SetCursor(m_hCursor);
92     return S_OK;
93   }
94 
95   // Let the system choose which cursor to apply.
96   return DRAGDROP_S_USEDEFAULTCURSORS;
97 }
98