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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/*
* 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,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*/
/** \file
* \ingroup GHOST
*/
#include "GHOST_Debug.h"
#include "GHOST_DropTargetWin32.h"
#include <shellapi.h>
#include "utf_winfunc.h"
#include "utfconv.h"
#ifdef GHOST_DEBUG
// utility
void printLastError(void);
#endif // GHOST_DEBUG
GHOST_DropTargetWin32::GHOST_DropTargetWin32(GHOST_WindowWin32 *window, GHOST_SystemWin32 *system)
: m_window(window), m_system(system)
{
m_cRef = 1;
m_hWnd = window->getHWND();
m_draggedObjectType = GHOST_kDragnDropTypeUnknown;
}
GHOST_DropTargetWin32::~GHOST_DropTargetWin32()
{
}
/*
* IUnknown::QueryInterface
*/
HRESULT __stdcall GHOST_DropTargetWin32::QueryInterface(REFIID riid, void **ppvObj)
{
if (!ppvObj)
return E_INVALIDARG;
*ppvObj = NULL;
if (riid == IID_IUnknown || riid == IID_IDropTarget) {
AddRef();
*ppvObj = (void *)this;
return S_OK;
}
else {
*ppvObj = NULL;
return E_NOINTERFACE;
}
}
/*
* IUnknown::AddRef
*/
ULONG __stdcall GHOST_DropTargetWin32::AddRef(void)
{
return ::InterlockedIncrement(&m_cRef);
}
/*
* IUnknown::Release
*/
ULONG __stdcall GHOST_DropTargetWin32::Release(void)
{
ULONG refs = ::InterlockedDecrement(&m_cRef);
if (refs == 0) {
delete this;
return 0;
}
else {
return refs;
}
}
/*
* Implementation of IDropTarget::DragEnter
*/
HRESULT __stdcall GHOST_DropTargetWin32::DragEnter(IDataObject *pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD *pdwEffect)
{
// we accept all drop by default
m_window->setAcceptDragOperation(true);
*pdwEffect = DROPEFFECT_NONE;
m_draggedObjectType = getGhostType(pDataObject);
m_system->pushDragDropEvent(
GHOST_kEventDraggingEntered, m_draggedObjectType, m_window, pt.x, pt.y, NULL);
return S_OK;
}
/*
* Implementation of IDropTarget::DragOver
*/
HRESULT __stdcall GHOST_DropTargetWin32::DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
if (m_window->canAcceptDragOperation()) {
*pdwEffect = allowedDropEffect(*pdwEffect);
}
else {
*pdwEffect = DROPEFFECT_NONE;
// *pdwEffect = DROPEFFECT_COPY; // XXX Uncomment to test drop. Drop will not be called if pdwEffect == DROPEFFECT_NONE.
}
m_system->pushDragDropEvent(
GHOST_kEventDraggingUpdated, m_draggedObjectType, m_window, pt.x, pt.y, NULL);
return S_OK;
}
/*
* Implementation of IDropTarget::DragLeave
*/
HRESULT __stdcall GHOST_DropTargetWin32::DragLeave(void)
{
m_system->pushDragDropEvent(
GHOST_kEventDraggingExited, m_draggedObjectType, m_window, 0, 0, NULL);
m_draggedObjectType = GHOST_kDragnDropTypeUnknown;
return S_OK;
}
/* Implementation of IDropTarget::Drop
* This function will not be called if pdwEffect is set to DROPEFFECT_NONE in
* the implementation of IDropTarget::DragOver
*/
HRESULT __stdcall GHOST_DropTargetWin32::Drop(IDataObject *pDataObject,
DWORD grfKeyState,
POINTL pt,
DWORD *pdwEffect)
{
void *data = getGhostData(pDataObject);
if (m_window->canAcceptDragOperation()) {
*pdwEffect = allowedDropEffect(*pdwEffect);
}
else {
*pdwEffect = DROPEFFECT_NONE;
}
if (data)
m_system->pushDragDropEvent(
GHOST_kEventDraggingDropDone, m_draggedObjectType, m_window, pt.x, pt.y, data);
m_draggedObjectType = GHOST_kDragnDropTypeUnknown;
return S_OK;
}
/*
* Helpers
*/
DWORD GHOST_DropTargetWin32::allowedDropEffect(DWORD dwAllowed)
{
DWORD dwEffect = DROPEFFECT_NONE;
if (dwAllowed & DROPEFFECT_COPY)
dwEffect = DROPEFFECT_COPY;
return dwEffect;
}
GHOST_TDragnDropTypes GHOST_DropTargetWin32::getGhostType(IDataObject *pDataObject)
{
/* Text
* Note: Unicode text is available as CF_TEXT too, the system can do the
* conversion, but we do the conversion ourself with WC_NO_BEST_FIT_CHARS.
*/
FORMATETC fmtetc = {CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
if (pDataObject->QueryGetData(&fmtetc) == S_OK) {
return GHOST_kDragnDropTypeString;
}
// Filesnames
fmtetc.cfFormat = CF_HDROP;
if (pDataObject->QueryGetData(&fmtetc) == S_OK) {
return GHOST_kDragnDropTypeFilenames;
}
return GHOST_kDragnDropTypeUnknown;
}
void *GHOST_DropTargetWin32::getGhostData(IDataObject *pDataObject)
{
GHOST_TDragnDropTypes type = getGhostType(pDataObject);
switch (type) {
case GHOST_kDragnDropTypeFilenames:
return getDropDataAsFilenames(pDataObject);
break;
case GHOST_kDragnDropTypeString:
return getDropDataAsString(pDataObject);
break;
case GHOST_kDragnDropTypeBitmap:
//return getDropDataAsBitmap(pDataObject);
break;
default:
#ifdef GHOST_DEBUG
::printf("\nGHOST_kDragnDropTypeUnknown");
#endif // GHOST_DEBUG
return NULL;
break;
}
return NULL;
}
void *GHOST_DropTargetWin32::getDropDataAsFilenames(IDataObject *pDataObject)
{
UINT totfiles, nvalid = 0;
WCHAR fpath[MAX_PATH];
char *temp_path;
GHOST_TStringArray *strArray = NULL;
FORMATETC fmtetc = {CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
STGMEDIUM stgmed;
HDROP hdrop;
// Check if dataobject supplies the format we want.
// Double checking here, first in getGhostType.
if (pDataObject->QueryGetData(&fmtetc) == S_OK) {
if (pDataObject->GetData(&fmtetc, &stgmed) == S_OK) {
hdrop = (HDROP)::GlobalLock(stgmed.hGlobal);
totfiles = ::DragQueryFileW(hdrop, -1, NULL, 0);
if (!totfiles) {
::GlobalUnlock(stgmed.hGlobal);
return NULL;
}
strArray = (GHOST_TStringArray *)::malloc(sizeof(GHOST_TStringArray));
strArray->count = 0;
strArray->strings = (GHOST_TUns8 **)::malloc(totfiles * sizeof(GHOST_TUns8 *));
for (UINT nfile = 0; nfile < totfiles; nfile++) {
if (::DragQueryFileW(hdrop, nfile, fpath, MAX_PATH) > 0) {
if (!(temp_path = alloc_utf_8_from_16(fpath, 0))) {
continue;
}
// Just ignore paths that could not be converted verbatim.
strArray->strings[nvalid] = (GHOST_TUns8 *)temp_path;
strArray->count = nvalid + 1;
nvalid++;
}
}
// Free up memory.
::GlobalUnlock(stgmed.hGlobal);
::ReleaseStgMedium(&stgmed);
return strArray;
}
}
return NULL;
}
void *GHOST_DropTargetWin32::getDropDataAsString(IDataObject *pDataObject)
{
char *tmp_string;
FORMATETC fmtetc = {CF_UNICODETEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
STGMEDIUM stgmed;
// Try unicode first.
// Check if dataobject supplies the format we want.
if (pDataObject->QueryGetData(&fmtetc) == S_OK) {
if (pDataObject->GetData(&fmtetc, &stgmed) == S_OK) {
LPCWSTR wstr = (LPCWSTR)::GlobalLock(stgmed.hGlobal);
if (!(tmp_string = alloc_utf_8_from_16((wchar_t *)wstr, 0))) {
::GlobalUnlock(stgmed.hGlobal);
return NULL;
}
// Free memory
::GlobalUnlock(stgmed.hGlobal);
::ReleaseStgMedium(&stgmed);
#ifdef GHOST_DEBUG
::printf("\n<converted droped unicode string>\n%s\n</droped converted unicode string>\n",
tmp_string);
#endif // GHOST_DEBUG
return tmp_string;
}
}
fmtetc.cfFormat = CF_TEXT;
if (pDataObject->QueryGetData(&fmtetc) == S_OK) {
if (pDataObject->GetData(&fmtetc, &stgmed) == S_OK) {
char *str = (char *)::GlobalLock(stgmed.hGlobal);
tmp_string = (char *)::malloc(::strlen(str) + 1);
if (!tmp_string) {
::GlobalUnlock(stgmed.hGlobal);
return NULL;
}
if (!::strcpy(tmp_string, str)) {
::free(tmp_string);
::GlobalUnlock(stgmed.hGlobal);
return NULL;
}
// Free memory
::GlobalUnlock(stgmed.hGlobal);
::ReleaseStgMedium(&stgmed);
return tmp_string;
}
}
return NULL;
}
int GHOST_DropTargetWin32::WideCharToANSI(LPCWSTR in, char *&out)
{
int size;
out = NULL; //caller should free if != NULL
// Get the required size.
size = ::WideCharToMultiByte(CP_ACP, //System Default Codepage
0x00000400, // WC_NO_BEST_FIT_CHARS
in,
-1, //-1 null terminated, makes output null terminated too.
NULL,
0,
NULL,
NULL);
if (!size) {
#ifdef GHOST_DEBUG
::printLastError();
#endif // GHOST_DEBUG
return 0;
}
out = (char *)::malloc(size);
if (!out) {
::printf("\nmalloc failed!!!");
return 0;
}
size = ::WideCharToMultiByte(CP_ACP, 0x00000400, in, -1, (LPSTR)out, size, NULL, NULL);
if (!size) {
#ifdef GHOST_DEBUG
::printLastError();
#endif //GHOST_DEBUG
::free(out);
out = NULL;
}
return size;
}
#ifdef GHOST_DEBUG
void printLastError(void)
{
LPTSTR s;
DWORD err;
err = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
0,
(LPTSTR)&s,
0,
NULL)) {
printf("\nLastError: (%d) %s\n", (int)err, s);
LocalFree(s);
}
}
#endif // GHOST_DEBUG
|