-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVSTDragDrop.pas
170 lines (158 loc) · 6.79 KB
/
VSTDragDrop.pas
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
{******************************************************************************}
{ }
{ Functions for VirtualTree Drag&Drop implementation }
{ }
{ -------------------------------------------------------------------------- }
{ }
{ PNDTools is Copyright ©2011-2013 Janek Schäfer }
{ }
{ This file is part of PNDTools }
{ }
{ PNDTools 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 3 of the License, or }
{ (at your option) any later version. }
{ }
{ PNDTools 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, see <http://www.gnu.org/licenses/>. }
{ }
{******************************************************************************}
unit VSTDragDrop;
interface
uses
VirtualTrees,
VSTUtils,
Classes, Types, ActiveX,
{$Ifdef Win32}
ShellAPI, Windows
{$Else}
//
{$Endif};
type
{ Implements the drag&drop functions called by the files tree in frmMain
VSTDragDrop is called on dropping a file (or any content), identifies the
dropped content and calles AddItem ultimately
GetFileListFromObj is a helper function to transfer an IDataObject into
filepath strings, which will get added to a passed StringList }
TDragEvent = class
procedure VSTDragDrop(Sender: TBaseVirtualTree;
Source: TObject; DataObject: IDataObject; Formats: TFormatArray;
Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
procedure GetFileListFromObj(const DataObj: IDataObject;
FileList: TStringList);
end;
implementation
uses ComObj;
procedure TDragEvent.VSTDragDrop(Sender: TBaseVirtualTree;
Source: TObject; DataObject: IDataObject; Formats: TFormatArray;
Shift: TShiftState; Pt: TPoint; var Effect: Integer; Mode: TDropMode);
{$Ifdef Win32}
var
I : Integer;
MyList : TStringList;
Node : PVirtualNode;
begin
MyList := TStringList.Create;
Sender.BeginUpdate;
try
for I := 0 to High(formats) - 1 do
begin
if Formats[i] = CF_HDROP then // Explorer drop
begin
GetFileListFromObj(DataObject, MyList);
//here we have all filenames
if Mode = dmOnNode then
begin
AddItemList(Sender,Sender.DropTargetNode,MyList,true);
end else
begin
AddItemList(Sender,nil,MyList,true);
end;
end else
if Formats[I] = CF_VIRTUALTREE then // VirtualTree drop
begin
case Mode of
dmNowhere: Sender.ProcessDrop(DataObject,nil,Effect,amAddChildLast);
dmAbove:
begin
Node := Sender.GetPrevious(Sender.DropTargetNode,true);
if not IsFile(Sender,Node) then
Sender.ProcessDrop(DataObject,Node,Effect,amAddChildLast)
else
Sender.ProcessDrop(DataObject,Node.Parent,Effect,amAddChildLast)
end;
dmOnNode..dmBelow:
begin
if not IsFile(Sender,Sender.DropTargetNode) then
Sender.ProcessDrop(DataObject,Sender.DropTargetNode,Effect,amAddChildLast)
else
Sender.ProcessDrop(DataObject,Sender.DropTargetNode.Parent,Effect,amAddChildLast)
end;
end;
end;
end;
finally
MyList.Free;
Sender.SortTree(0,sdAscending,true);
Sender.EndUpdate;
end;
end;
{$Else}
begin
{$Message Error 'This function needs to be implemented for Linux!'}
end;
{$Endif}
procedure TDragEvent.GetFileListFromObj(const DataObj: IDataObject;
FileList: TStringList);
{$Ifdef Win32}
var
FmtEtc: TFormatEtc; // specifies required data format
Medium: TStgMedium; // storage medium containing file list
DroppedFileCount: Integer; // number of dropped files
I: Integer; // loops thru dropped files
FileNameLength: Integer; // length of a dropped file name
FileName: String; // name of a dropped file
Buffer: Array [0..MAX_PATH] of Char;
begin
// Get required storage medium from data object
FmtEtc.cfFormat := CF_HDROP;
FmtEtc.ptd := nil;
FmtEtc.dwAspect := DVASPECT_CONTENT;
FmtEtc.lindex := -1;
FmtEtc.tymed := TYMED_HGLOBAL;
OleCheck(DataObj.GetData(FmtEtc, Medium));
try
try
// Get count of files dropped
DroppedFileCount := DragQueryFile(Medium.hGlobal, $FFFFFFFF, nil, 0);
// Get name of each file dropped and process it
for I := 0 to Pred(DroppedFileCount) do
begin
// get length of file name, then name itself
FileNameLength := DragQueryFile(Medium.hGlobal, I, @buffer, sizeof(buffer));
FileName := buffer;
//SetLength(FileName, FileNameLength);
//DragQueryFileW(Medium.hGlobal, I, PWideChar(FileName), FileNameLength + 1);
// add file name to list
FileList.Append(FileName);
end;
finally
// Tidy up - release the drop handle
// don't use DropH again after this
DragFinish(Medium.hGlobal);
end;
finally
ReleaseStgMedium(Medium);
end;
end;
{$Else}
begin
{$Message Error 'This function needs to be implemented for Linux!'}
end;
{$Endif}
end.