-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDropFiles.h
339 lines (293 loc) · 11.2 KB
/
DropFiles.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#pragma once
#include <Shlobj.h>
#include <string>
#include <vector>
#include <Shldisp.h>
#ifndef __IDataObjectAsyncCapability_FWD_DEFINED__
# define IDataObjectAsyncCapability IAsyncOperation
#endif
#define DRAG_NUMFORMATS 2
/**
* Use this class to create the DROPFILES structure which is needed to
* support drag and drop of file names to other applications.
*/
class CDropFiles
{
public:
CDropFiles();
~CDropFiles();
/**
* Add a file with an absolute file name. This file will later be
* included the DROPFILES structure.
*/
void AddFile(const std::wstring& sFile);
/**
* Returns the number of files which have been added
*/
size_t GetCount() const;
/**
* Call this method when dragging begins. It will fill
* the DROPFILES structure with the files previously
* added with AddFile(...)
*/
void CreateStructure(HWND hWnd) const;
protected:
std::vector<std::wstring> m_arFiles;
};
class CDragSourceNotify : IDropSourceNotify
{
private:
LONG refCount;
public:
CDragSourceNotify()
{
refCount = 0;
}
virtual ~CDragSourceNotify()
{
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override
{
if (!ppvObject)
{
return E_POINTER;
}
if (riid == IID_IUnknown)
{
*ppvObject = static_cast<IUnknown*>(dynamic_cast<IDropSourceNotify*>(this));
}
else if (riid == IID_IDropSourceNotify)
{
*ppvObject = dynamic_cast<IDropSourceNotify*>(this);
}
else
{
*ppvObject = nullptr;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE AddRef() override
{
return InterlockedIncrement(&refCount);
}
ULONG STDMETHODCALLTYPE Release() override
{
ULONG ret = InterlockedDecrement(&refCount);
if (!ret)
{
delete this;
}
return ret;
}
HRESULT STDMETHODCALLTYPE DragEnterTarget(HWND /*hWndTarget*/) override
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE DragLeaveTarget() override
{
return S_OK;
}
};
// ReSharper disable once CppInconsistentNaming
class CIDropSource : public IDropSource
{
long m_cRefCount;
public:
bool m_bDropped;
IDataObject* m_pIDataObj;
CDragSourceNotify* pDragSourceNotify;
CIDropSource()
: m_cRefCount(0)
, m_bDropped(false)
, m_pIDataObj(nullptr)
{
pDragSourceNotify = new CDragSourceNotify();
pDragSourceNotify->AddRef();
}
virtual ~CIDropSource()
{
if (m_pIDataObj)
{
m_pIDataObj->Release();
m_pIDataObj = nullptr;
}
if (pDragSourceNotify)
{
pDragSourceNotify->Release();
pDragSourceNotify = nullptr;
}
}
//IUnknown
HRESULT STDMETHODCALLTYPE QueryInterface(/* [in] */ REFIID riid,
/* [iid_is][out] */ void __RPC_FAR* __RPC_FAR* ppvObject) override;
ULONG STDMETHODCALLTYPE AddRef() override;
ULONG STDMETHODCALLTYPE Release() override;
//IDropSource
HRESULT STDMETHODCALLTYPE QueryContinueDrag(/* [in] */ BOOL fEscapePressed,
/* [in] */ DWORD grfKeyState) override;
HRESULT STDMETHODCALLTYPE GiveFeedback(/* [in] */ DWORD dwEffect) override;
};
extern CLIPFORMAT CF_FILECONTENTS;
extern CLIPFORMAT CF_FILEDESCRIPTOR;
extern CLIPFORMAT CF_PREFERREDDROPEFFECT;
/**
* Represents a path as an IDataObject.
* This can be used for drag and drop operations to other applications like
* the shell itself.
*/
class FileDataObject : public IDataObject
, public IDataObjectAsyncCapability
{
public:
/**
* Constructs the FileDataObject.
* \param paths a list of paths.
*/
FileDataObject(const std::vector<std::wstring>& paths);
virtual ~FileDataObject();
//IUnknown
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) override;
ULONG STDMETHODCALLTYPE AddRef() override;
ULONG STDMETHODCALLTYPE Release() override;
//IDataObject
HRESULT STDMETHODCALLTYPE GetData(FORMATETC* pformatetcIn, STGMEDIUM* pmedium) override;
HRESULT STDMETHODCALLTYPE GetDataHere(FORMATETC* pformatetc, STGMEDIUM* pmedium) override;
HRESULT STDMETHODCALLTYPE QueryGetData(FORMATETC* pformatetc) override;
HRESULT STDMETHODCALLTYPE GetCanonicalFormatEtc(FORMATETC* pformatectIn, FORMATETC* pformatetcOut) override;
HRESULT STDMETHODCALLTYPE SetData(FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease) override;
HRESULT STDMETHODCALLTYPE EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc) override;
HRESULT STDMETHODCALLTYPE DAdvise(FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink, DWORD* pdwConnection) override;
HRESULT STDMETHODCALLTYPE DUnadvise(DWORD dwConnection) override;
HRESULT STDMETHODCALLTYPE EnumDAdvise(IEnumSTATDATA** ppenumAdvise) override;
//IDataObjectAsyncCapability
HRESULT STDMETHODCALLTYPE SetAsyncMode(BOOL fDoOpAsync) override;
HRESULT STDMETHODCALLTYPE GetAsyncMode(BOOL* pfIsOpAsync) override;
HRESULT STDMETHODCALLTYPE StartOperation(IBindCtx* pbcReserved) override;
HRESULT STDMETHODCALLTYPE InOperation(BOOL* pfInAsyncOp) override;
HRESULT STDMETHODCALLTYPE EndOperation(HRESULT hResult, IBindCtx* pbcReserved, DWORD dwEffects) override;
virtual HRESULT STDMETHODCALLTYPE SetDropDescription(DROPIMAGETYPE image, LPCWSTR format, LPCWSTR insert);
private:
static void CopyMedium(STGMEDIUM* pMedDest, STGMEDIUM* pMedSrc, FORMATETC* pFmtSrc);
std::vector<std::wstring> m_allPaths;
long m_cRefCount;
BOOL m_bInOperation;
BOOL m_bIsAsync;
std::vector<FORMATETC*> m_vecFormatEtc;
std::vector<STGMEDIUM*> m_vecStgMedium;
};
/**
* Helper class for the FileDataObject class: implements the enumerator
* for the supported clipboard formats of the FileDataObject class.
*/
class CEnumFormatEtcHelper : public IEnumFORMATETC
{
public:
CEnumFormatEtcHelper(const std::vector<FORMATETC*>& vec);
CEnumFormatEtcHelper(const std::vector<FORMATETC>& vec);
virtual ~CEnumFormatEtcHelper() = default;
//IUnknown members
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) override;
ULONG STDMETHODCALLTYPE AddRef() override;
ULONG STDMETHODCALLTYPE Release() override;
//IEnumFORMATETC members
HRESULT STDMETHODCALLTYPE Next(ULONG, LPFORMATETC, ULONG*) override;
HRESULT STDMETHODCALLTYPE Skip(ULONG) override;
HRESULT STDMETHODCALLTYPE Reset() override;
HRESULT STDMETHODCALLTYPE Clone(IEnumFORMATETC**) override;
private:
void Init();
std::vector<FORMATETC> m_vecFormatEtc;
FORMATETC m_formats[DRAG_NUMFORMATS];
ULONG m_cRefCount;
size_t m_iCur;
};
class CDragSourceHelper
{
IDragSourceHelper2* pDragSourceHelper2;
IDragSourceHelper* pDragSourceHelper;
public:
CDragSourceHelper()
{
pDragSourceHelper = nullptr;
pDragSourceHelper2 = nullptr;
if (FAILED(CoCreateInstance(CLSID_DragDropHelper,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDragSourceHelper2,
reinterpret_cast<void**>(&pDragSourceHelper2))))
{
pDragSourceHelper2 = nullptr;
if (FAILED(CoCreateInstance(CLSID_DragDropHelper,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDragSourceHelper,
reinterpret_cast<void**>(&pDragSourceHelper))))
pDragSourceHelper = nullptr;
}
}
virtual ~CDragSourceHelper()
{
if (pDragSourceHelper2 != nullptr)
{
pDragSourceHelper2->Release();
pDragSourceHelper2 = nullptr;
}
if (pDragSourceHelper != nullptr)
{
pDragSourceHelper->Release();
pDragSourceHelper = nullptr;
}
}
// IDragSourceHelper
HRESULT InitializeFromBitmap(HBITMAP hBitmap,
POINT& pt, // cursor position in client coords of the window
RECT& rc, // selected item's bounding rect
IDataObject* pDataObject,
BOOL allowDropDescription = TRUE,
COLORREF crColorKey = GetSysColor(COLOR_WINDOW) // color of the window used for transparent effect.
) const
{
if ((pDragSourceHelper == nullptr) && (pDragSourceHelper2 == nullptr))
return E_FAIL;
if ((allowDropDescription) && (pDragSourceHelper2))
pDragSourceHelper2->SetFlags(DSH_ALLOWDROPDESCRIPTIONTEXT);
SHDRAGIMAGE di;
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
di.sizeDragImage.cx = bm.bmWidth;
di.sizeDragImage.cy = bm.bmHeight;
di.hbmpDragImage = hBitmap;
di.crColorKey = crColorKey;
di.ptOffset.x = pt.x - rc.left;
di.ptOffset.y = pt.y - rc.top;
if (pDragSourceHelper2)
return pDragSourceHelper2->InitializeFromBitmap(&di, pDataObject);
return pDragSourceHelper->InitializeFromBitmap(&di, pDataObject);
}
HRESULT InitializeFromWindow(HWND hwnd, POINT& pt, IDataObject* pDataObject, BOOL allowDropDescription = TRUE) const
{
if ((pDragSourceHelper == nullptr) && (pDragSourceHelper2 == nullptr))
return E_FAIL;
if ((allowDropDescription) && (pDragSourceHelper2))
pDragSourceHelper2->SetFlags(DSH_ALLOWDROPDESCRIPTIONTEXT);
if (pDragSourceHelper2)
return pDragSourceHelper2->InitializeFromWindow(hwnd, &pt, pDataObject);
return pDragSourceHelper->InitializeFromWindow(hwnd, &pt, pDataObject);
}
};