-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRDragDropFiles.cpp
More file actions
306 lines (246 loc) · 8.24 KB
/
RDragDropFiles.cpp
File metadata and controls
306 lines (246 loc) · 8.24 KB
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
// RDragDropFiles.cpp: implementation of the RDragDropFiles class.
//
/*
Author: Richard Chambers, rickcham@mindspring.com
Copyright: Richard Chambers, 2002
Version: 1.0
Rights of Use: This source code may be used by anyone for any reason
so long as the copyright information is maintained.
There is no warranty, express or implied, as to fitness
of purpose nor is there any other warranty, expressed
or implied, as to correct functioning.
This software depends on the existance of two functions
in the Windows API as well as the correct function of
those functions. See the Requires section below.
Requires: RDragDropFiles.h containing the class declaration.
DragQueryFile function from the Windows API.
Client must provide a correct HDROP handle to the constructor.
Client must initialize file drop using DragAcceptFiles.
Assumptions: If debug is turned on, the macro _DEBUG is defined and ASSERT
is enabled. If debug is turned off, the macro _DEBUG is
undefined and ASSERT is disabled.
The DragQueryFile Windows API function remains unchanged
across Windows versions (Windows 95/98/ME/NT/2000).
Edit History:
*/
/*
Description:
This file implements the file drag and drop functionality
by encapsulating the DragQueryFile function call for the
Windows file drag and drop.
This is not the same as the Windows OLE drag and drop.
In order to activate a window as a drop target, you must
insert a call to DragAcceptFiles into your OnCreate
handler of your window.
Then catch the WM_DROPFILES message (OnDropFiles method of
a CView class using Visual C++ Class Wizard) and use this
class in the method.
void CScreen01View::OnDropFiles(HDROP hDropInfo)
{
rjc::RDragDropFiles myFiles (hDropInfo);
CString buf;
while (myFiles ()) {
myFiles.sNextFile (buf);
... do something with buf .....
}
}
*/
#include "stdafx.h"
//#include "screen01.h"
#include "RDragDropFiles.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
using namespace rjc;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
RDragDropFiles::RDragDropFiles(HDROP hDrop)
:hDropInfo (hDrop), nFiles (0), iPosition (0)
{
ASSERT (hDropInfo);
if (hDropInfo) {
nFiles = DragQueryFile (hDropInfo, 0xFFFFFFFF, NULL, 0);
}
ASSERT (nFiles > 0);
}
RDragDropFiles::~RDragDropFiles()
{
// Release the drag and drop resources
// We check that hDropInfo is valid before
// doing the DragFinish in case the client has
// called the Clear method in order to prevent
// the use of DragFinish when the object goes out
// of scope.
// Reference counting is left as an exercise.
if (hDropInfo) {
DragFinish(hDropInfo);
}
}
/*
In the following three methods for iterating over the list
of files, we require the client to provide us a CString
for the buffer. We require the client's CString so that
we don't have to do funky pointers and stuff. The client
can provide us an auto object on the stack which will be
cleaned up when it goes out of scope.
We will also set the CString to Empty if there is not a file
name to put into it. That way the client can depend on the
value of the CString to be logically correct.
The client can either call the constructor and then start
iterating using sNextFile or the client can use sFirstFile
or Reset to start at the beginning of the list after iterating
some of it then continue with sNextFile.
iPosition, which is the file position in the list of files,
is a 1 index rather an a 0 index. However, DragQueryFile
uses a 0 index so we subtract 1 from iPosition. We are
using 1 index in our class as it simplifies the code when
mixing sFirstFile, sNextFile, and Reset.
sFirstFile forces iPosition to 1 for the first file in
the list. sCurrFile forces iPosition to 1 if it is 0 so
as to ensure the correct functioning of sNextFile if it is
used after a sCurrFile.
We use DragQueryFile to find the length of the pathname
we want to get, set our CString length accordingly, and
then copy the pathname of the file into our CString. The
pathname is a standard C string with an end of string
terminator at the end for which we also have to account.
When we do the CString.ReleaseBuffer, CString will recalc
the length information based on the end of string terminator.
*/
CString & RDragDropFiles::sFirstFile (CString & sBuff)
{
ASSERT (hDropInfo);
iPosition = 1;
if (iPosition <= nFiles) {
UINT nLen = DragQueryFile (hDropInfo, iPosition - 1, NULL, 0);
#ifdef _UNICODE
wchar_t *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#else
char *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#endif
DragQueryFile (hDropInfo, iPosition - 1, pzsFN, nLen+2);
sBuff.ReleaseBuffer ();
}
else {
sBuff.Empty ();
}
return sBuff;
}
CString & RDragDropFiles::sNextFile (CString & sBuff)
{
ASSERT (hDropInfo);
ASSERT (iPosition < nFiles);
ASSERT (iPosition >= 0);
if (iPosition < nFiles) {
iPosition++;
UINT nLen = DragQueryFile (hDropInfo, iPosition - 1, NULL, 0);
#ifdef _UNICODE
wchar_t *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#else
char *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#endif
DragQueryFile (hDropInfo, iPosition - 1, pzsFN, nLen+2);
sBuff.ReleaseBuffer ();
}
else {
sBuff.Empty ();
}
return sBuff;
}
CString & RDragDropFiles::sCurrFile (CString & sBuff)
{
ASSERT (hDropInfo);
ASSERT (iPosition <= nFiles);
if (iPosition < 1)
iPosition = 1;
if (iPosition <= nFiles) {
UINT nLen = DragQueryFile (hDropInfo, iPosition - 1, NULL, 0);
#ifdef _UNICODE
wchar_t *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#else
char *pzsFN = sBuff.GetBufferSetLength (nLen+2);
#endif
DragQueryFile (hDropInfo, iPosition - 1, pzsFN, nLen+2);
sBuff.ReleaseBuffer ();
}
else {
sBuff.Empty ();
}
return sBuff;
}
#ifdef _DEBUG
// non-debug versions of these are inline in RDragDropFiles.h
// Any changes made to functionality should be reflected in the
// in-line versions as well.
// Need to do retesting in both debug and non-debug configurations.
/*
uCount - provide a count of the files in the drop list.
*/
UINT RDragDropFiles::uCount ()
{
ASSERT (hDropInfo);
if (hDropInfo) {
return nFiles;
}
else {
return 0;
}
}
/*
function operator - returns whether there are any more
files to obtain from the list of dropped files.
iPosition == 0 if after Reset or newly constructed.
iPosition == 1 if after sFirstFile or sCurrFile or first sNextFile (iPosition was 0)
We are using increment before fetch in sNextFile so iPosition == nFiles on
exit from last legal sNextFile. iPostion should be less than nFiles if
the next call to sNextFile will return an item from the file list.
*/
boolean RDragDropFiles::operator () ()
{
ASSERT (hDropInfo);
return (hDropInfo && (iPosition < nFiles));
}
/*
IsEmpty - return whether the list of files is empty.
An empty list is either hDropInfo is not a good
item or the number of files being dropped is less
than 1 (in other words zero).
IsEmpty tells us if there are any files in the list and not
whether iPosition points to a legal list entry.
*/
boolean RDragDropFiles::IsEmpty ()
{
return (!hDropInfo || (nFiles < 1));
}
/*
Reset - reset the position so that we can iterate over
the list from the beginning. Set iPosition to 0
allowing sNextFile to work correctly if called
after doing a Reset.
*/
void RDragDropFiles::Reset ()
{
ASSERT (hDropInfo);
iPosition = 0;
ASSERT (iPosition < nFiles);
}
/*
Clear - clear the object's stored data.
The purpose of clear is to object's data before
its destructor is called in those cases where
the client wants to use the hDropIno for something
else.
The main reason to use this method is to prevent
the call to DragFinish(hDropInfo); when the object's
destructor is called as it goes out of scope.
*/
void RDragDropFiles::Clear ()
{
ASSERT (hDropInfo);
hDropInfo = 0;
nFiles = 0;
}
#endif