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