-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDropTarget.pas
909 lines (808 loc) · 27.6 KB
/
DropTarget.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
unit DropTarget;
// -----------------------------------------------------------------------------
// Project: Drag and Drop Component Suite
// Component Names: TDropFileTarget, TDropTextTarget
// Module: DropTarget
// Description: Implements Dragging & Dropping of text and files
// INTO your application FROM another.
// Version: 3.7
// Date: 22-APR-1999
// Target: Win32, Delphi 3 - Delphi 5, C++ Builder 3, C++ Builder 4
// Authors: Angus Johnson, [email protected]
// Anders Melander, [email protected]
// http://www.melander.dk
// Copyright © 1997-99 Angus Johnson & Anders Melander
// -----------------------------------------------------------------------------
interface
uses
DropSource,
Windows, ActiveX, Classes, Controls, CommCtrl, ExtCtrls,
LifeGen, LifeConst;
{$include DragDrop.inc}
type
TScrollDirection = (sdHorizontal, sdVertical);
TScrollDirections = set of TScrollDirection;
TDropTargetEvent = procedure(Sender: TObject;
ShiftState: TShiftState; Point: TPoint; var Effect: Longint) of Object;
//Note: TInterfacedComponent is declared in DropSource.pas
TDropTarget = class(TInterfacedComponent, IDropTarget)
private
fDataObj: IDataObject;
fDragTypes: TDragTypes;
fRegistered: boolean;
fTarget: TWinControl;
fGetDataOnEnter: boolean;
fOnEnter: TDropTargetEvent;
fOnDragOver: TDropTargetEvent;
fOnLeave: TNotifyEvent;
fOnDrop: TDropTargetEvent;
fGetDropEffectEvent: TDropTargetEvent;
fImages: TImageList;
fDragImageHandle: HImageList;
fShowImage: boolean;
fImageHotSpot: TPoint;
fLastPoint: TPoint; //Point where DragImage was last painted (used internally)
fTargetScrollMargin: integer;
fScrollDirs: TScrollDirections; //enables auto scrolling of target window during drags
fScrollTimer: TTimer; //and paints any drag image 'cleanly'.
procedure DoTargetScroll(Sender: TObject);
procedure SetShowImage(Show: boolean);
protected
// IDropTarget methods...
function DragEnter(const DataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HRESULT; stdcall;
function DragOver(grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HRESULT; stdcall;
function DragLeave: HRESULT; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HRESULT; stdcall;
procedure DoEnter(ShiftState: TShiftState; Point: TPoint; var Effect: Longint); virtual;
procedure DoDragOver(ShiftState: TShiftState; Point: TPoint; var Effect: Longint); virtual;
procedure DoDrop(ShiftState: TShiftState; Point: TPoint; var Effect: Longint); virtual;
procedure DoLeave; virtual;
function DoGetData: boolean; virtual; abstract;
procedure ClearData; virtual; abstract;
function HasValidFormats: boolean; virtual; abstract;
function GetValidDropEffect(ShiftState: TShiftState;
pt: TPoint; dwEffect: LongInt): LongInt; virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Register(Target: TWinControl);
procedure Unregister;
function PasteFromClipboard: longint; virtual;
property DataObject: IDataObject read fDataObj;
property Target: TWinControl read fTarget;
published
property Dragtypes: TDragTypes read fDragTypes write fDragTypes;
property GetDataOnEnter: Boolean read fGetDataOnEnter write fGetDataOnEnter;
//Events...
property OnEnter: TDropTargetEvent read fOnEnter write fOnEnter;
property OnDragOver: TDropTargetEvent read fOnDragOver write fOnDragOver;
property OnLeave: TNotifyEvent read fOnLeave write fOnLeave;
property OnDrop: TDropTargetEvent read fOnDrop write fOnDrop;
property OnGetDropEffect: TDropTargetEvent
read fGetDropEffectEvent write fGetDropEffectEvent;
//Drag Images...
property ShowImage: boolean read fShowImage write SetShowImage;
end;
TDropFileTarget = class(TDropTarget)
private
fFiles: TStrings;
fMappedNames: TStrings;
fFileNameMapFormatEtc,
fFileNameMapWFormatEtc: TFormatEtc;
protected
procedure ClearData; override;
function DoGetData: boolean; override;
function HasValidFormats: boolean; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function PasteFromClipboard: longint; Override;
property Files: TStrings Read fFiles;
//MappedNames is only needed if files need to be renamed after a drag op
//eg dragging from 'Recycle Bin'.
property MappedNames: TStrings read fMappedNames;
end;
TDropTextTarget = class(TDropTarget)
private
fText: String;
protected
procedure ClearData; override;
function DoGetData: boolean; override;
function HasValidFormats: boolean; override;
public
function PasteFromClipboard: longint; override;
property Text: string read fText write fText;
end;
TDropLife32Target = class(TDropTarget)
private
FUniverse: TUniverse;
protected
procedure ClearData; override;
function DoGetData: boolean; override;
function HasValidFormats: boolean; override;
public
function PasteFromClipboard: longint; override;
property Universe: TUniverse read FUniverse write FUniverse;
end;
TDropDummy = class(TDropTarget)
protected
procedure ClearData; override;
function DoGetData: boolean; override;
function HasValidFormats: boolean; override;
end;
const
HDropFormatEtc: TFormatEtc = (cfFormat: CF_HDROP;
ptd: nil; dwAspect: DVASPECT_CONTENT; lindex: -1; tymed: TYMED_HGLOBAL);
TextFormatEtc: TFormatEtc = (cfFormat: CF_TEXT;
ptd: nil; dwAspect: DVASPECT_CONTENT; lindex: -1; tymed: TYMED_HGLOBAL);
function GetFilesFromHGlobal(const HGlob: HGlobal; Files: TStrings): boolean;
function ClientPtToWindowPt(Handle: HWND; pt: TPoint): TPoint;
procedure Register;
implementation
uses
SysUtils, Graphics, Messages, ShlObj, ClipBrd, Forms,
ComCtrls;
procedure Register;
begin
RegisterComponents('DragDrop',[TDropFileTarget, TDropTextTarget, TDropDummy]);
RegisterComponents('DragDrop',[TDropLife32Target]);
end;
// TDummyWinControl is declared just to expose the protected property - Font -
// which is used to calculate the 'scroll margin' for the target window.
type
TDummyWinControl = Class(TWinControl);
// -----------------------------------------------------------------------------
// Miscellaneous functions ...
// -----------------------------------------------------------------------------
function ClientPtToWindowPt(Handle: HWND; pt: TPoint): TPoint;
var
Rect: TRect;
begin
ClientToScreen(Handle, pt);
GetWindowRect(Handle, Rect);
Result.X:= pt.X - Rect.Left;
Result.Y:= pt.Y - Rect.Top;
end;
// -----------------------------------------------------------------------------
function GetFilesFromHGlobal(const HGlob: HGlobal; Files: TStrings): boolean;
var
DropFiles: PDropFiles;
Filename: PChar;
begin
{ TODO -oanme -cImprovement : Shouldn't there be a Files.Clear here? }
DropFiles:= PDropFiles(GlobalLock(HGlob));
try
Filename:= PChar(DropFiles) + DropFiles^.pFiles;
while (Filename^ <> #0) do
begin
if (DropFiles^.fWide) then // -> NT4 & Asian compatibility
begin
Files.Add(PWideChar(FileName));
inc(Filename, (Length(PWideChar(FileName)) + 1) * 2);
end else
begin
Files.Add(Filename);
inc(Filename, Length(Filename) + 1);
end;
end;
finally
GlobalUnlock(HGlob);
end;
Result:= (Files.count > 0);
end;
// -----------------------------------------------------------------------------
// TDropTarget
// -----------------------------------------------------------------------------
function TDropTarget.DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HRESULT;
var
ShiftState: TShiftState;
TargetStyles: longint;
begin
ClearData;
fDataObj:= dataObj;
//fDataObj._AddRef; removed, this was a bug
result:= S_OK;
pt:= fTarget.ScreenToClient(pt);
fLastPoint:= pt;
fDragImageHandle:= 0;
if ShowImage then
begin
fDragImageHandle:= ImageList_GetDragImage(nil,@fImageHotSpot);
if (fDragImageHandle <> 0) then
begin
//Currently we will just replace any 'embedded' cursor with our
//blank (transparent) image otherwise we sometimes get 2 cursors ...
ImageList_SetDragCursorImage(fImages.Handle,0,fImageHotSpot.x,fImageHotSpot.y);
with ClientPtToWindowPt(fTarget.handle,pt) do
ImageList_DragEnter(fTarget.handle,x,y);
end;
end;
if not HasValidFormats then
begin
fDataObj:= nil;
dwEffect:= DROPEFFECT_NONE;
//result:= E_FAIL;
exit;
end;
fScrollDirs:= [];
//thanks to a suggestion by Praful Kapadia ...
fTargetScrollMargin:= abs(TDummyWinControl(fTarget).font.height);
TargetStyles:= GetWindowLong(fTarget.handle,GWL_STYLE);
if (TargetStyles and WS_HSCROLL <> 0) then
fScrollDirs:= fScrollDirs + [sdHorizontal];
if (TargetStyles and WS_VSCROLL <> 0) then
fScrollDirs:= fScrollDirs + [sdVertical];
//It's generally more efficient to get data only if a drop occurs
//rather than on entering a potential target window.
//However - sometimes there is a good reason to get it here.
if fGetDataOnEnter then DoGetData;
ShiftState:= KeysToShiftState(grfKeyState);
dwEffect:= GetValidDropEffect(ShiftState,Pt,dwEffect);
DoEnter(ShiftState, pt, dwEffect);
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.DoEnter(ShiftState: TShiftState; Point: TPoint; var Effect: Longint);
begin
if Assigned(fOnEnter) then
fOnEnter(Self, ShiftState, Point, Effect);
end;
// -----------------------------------------------------------------------------
function TDropTarget.DragOver(grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult;
var
ShiftState: TShiftState;
IsScrolling: boolean;
begin
pt:= fTarget.ScreenToClient(pt);
if fDataObj = nil then
begin
//fDataObj = nil when no valid formats .... see DragEnter method.
dwEffect:= DROPEFFECT_NONE;
IsScrolling:= false;
end else
begin
ShiftState:= KeysToShiftState(grfKeyState);
dwEffect:= GetValidDropEffect(ShiftState, pt, dwEffect);
DoDragOver(ShiftState, pt, dwEffect);
if (fScrollDirs <> []) and (dwEffect and DROPEFFECT_SCROLL <> 0) then
begin
IsScrolling:= true;
fScrollTimer.enabled:= true
end else
begin
IsScrolling:= false;
fScrollTimer.enabled:= false;
end;
end;
if (fDragImageHandle <> 0) and
((fLastPoint.x <> pt.x) or (fLastPoint.y <> pt.y)) then
begin
fLastPoint:= pt;
if IsScrolling then
//fScrollTimer.enabled:= true
else with ClientPtToWindowPt(fTarget.handle,pt) do
ImageList_DragMove(X,Y);
end
else
fLastPoint:= pt;
RESULT:= S_OK;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.DoDragOver(ShiftState: TShiftState; Point: TPoint; var Effect: Longint);
begin
if Assigned(fOnDragOver) then
fOnDragOver(Self, ShiftState, Point, Effect);
end;
// -----------------------------------------------------------------------------
function TDropTarget.DragLeave: HResult;
begin
ClearData;
fScrollTimer.enabled:= false;
fDataObj:= nil;
if (fDragImageHandle <> 0) then
ImageList_DragLeave(fTarget.handle);
DoLeave;
Result:= S_OK;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.DoLeave;
begin
if Assigned(fOnLeave) then fOnLeave(Self);
end;
// -----------------------------------------------------------------------------
function TDropTarget.Drop(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult;
var
ShiftState: TShiftState;
begin
RESULT:= S_OK;
if fDataObj = nil then begin
dwEffect:= DROPEFFECT_NONE;
exit;
end;
fScrollTimer.enabled:= false;
if (fDragImageHandle <> 0) then ImageList_DragLeave(fTarget.handle);
ShiftState:= KeysToShiftState(grfKeyState);
pt:= fTarget.ScreenToClient(pt);
dwEffect:= GetValidDropEffect(ShiftState, pt, dwEffect);
if (not fGetDataOnEnter) and (not DoGetData) then dwEffect:= DROPEFFECT_NONE
else DoDrop(ShiftState, pt, dwEffect);
// clean up!
ClearData;
fDataObj:= nil;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.DoDrop(ShiftState: TShiftState; Point: TPoint; var Effect: Longint);
begin
if Assigned(fOnDrop) then
fOnDrop(Self, ShiftState, Point, Effect);
end;
// -----------------------------------------------------------------------------
constructor TDropTarget.Create( AOwner: TComponent );
var
bm: TBitmap;
begin
inherited Create( AOwner );
fScrollTimer:= TTimer.create(self);
fScrollTimer.interval:= 100;
fScrollTimer.enabled:= false;
fScrollTimer.OnTimer:= DoTargetScroll;
_AddRef;
fGetDataOnEnter:= false;
fImages:= TImageList.create(self);
//Create a blank image for fImages which we...
//will use to hide any cursor 'embedded' in a drag image.
//This avoids the possibility of two cursors showing.
bm:= TBitmap.Create;
with bm do
begin
height:= 32;
width:= 32;
Canvas.Brush.Color:=clWindow;
Canvas.FillRect(Rect(0,0,31,31));
fImages.AddMasked(bm,clWindow);
free;
end;
fDataObj:= nil;
ShowImage:= true;
end;
// -----------------------------------------------------------------------------
destructor TDropTarget.Destroy;
begin
fImages.free;
fScrollTimer.free;
Unregister;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.Register(Target: TWinControl);
begin
if fTarget = Target then exit;
if (fTarget <> nil) then Unregister;
fTarget:= target;
if fTarget = nil then exit;
//CoLockObjectExternal(self,true,false);
if not RegisterDragDrop(fTarget.handle,self) = S_OK then
raise Exception.create('Failed to Register '+ fTarget.name);
fRegistered:= true;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.Unregister;
begin
fRegistered:= false;
if (fTarget = nil) or not fTarget.handleallocated then exit;
if not RevokeDragDrop(fTarget.handle) = S_OK then
raise Exception.create('Failed to Unregister '+ fTarget.name);
//CoLockObjectExternal(self,false,false);
fTarget:= nil;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.SetShowImage(Show: boolean);
begin
fShowImage:= Show;
if fDataObj <> nil then
ImageList_DragShowNolock(fShowImage);
end;
// -----------------------------------------------------------------------------
function TDropTarget.GetValidDropEffect(ShiftState: TShiftState;
pt: TPoint; dwEffect: LongInt): LongInt;
begin
//dwEffect 'in' parameter = set of drop effects allowed by drop source.
//Now filter out the effects disallowed by target...
if not (dtCopy in fDragTypes) then
dwEffect:= dwEffect and not DROPEFFECT_COPY;
if not (dtMove in fDragTypes) then
dwEffect:= dwEffect and not DROPEFFECT_MOVE;
if not (dtLink in fDragTypes) then
dwEffect:= dwEffect and not DROPEFFECT_LINK;
Result:= dwEffect;
//'Default' behaviour can be overriden by assigning OnGetDropEffect.
if Assigned(fGetDropEffectEvent) then
fGetDropEffectEvent(self, ShiftState, pt, Result)
else
begin
//As we're only interested in ssShift & ssCtrl here
//mouse buttons states are screened out ...
ShiftState:= ([ssShift, ssCtrl] * ShiftState);
if (ShiftState = [ssShift, ssCtrl]) and
(dwEffect and DROPEFFECT_LINK <> 0) then result:= DROPEFFECT_LINK
else if (ShiftState = [ssShift]) and
(dwEffect and DROPEFFECT_MOVE <> 0) then result:= DROPEFFECT_MOVE
else if (dwEffect and DROPEFFECT_COPY <> 0) then result:= DROPEFFECT_COPY
else if (dwEffect and DROPEFFECT_MOVE <> 0) then result:= DROPEFFECT_MOVE
else if (dwEffect and DROPEFFECT_LINK <> 0) then result:= DROPEFFECT_LINK
else result:= DROPEFFECT_NONE;
//Add Scroll effect if necessary...
if fScrollDirs = [] then exit;
if (sdHorizontal in fScrollDirs) and
((pt.x < fTargetScrollMargin) or
(pt.x>fTarget.ClientWidth - fTargetScrollMargin)) then
result:= result or integer(DROPEFFECT_SCROLL)
else if (sdVertical in fScrollDirs) and
((pt.y < fTargetScrollMargin) or
(pt.y>fTarget.ClientHeight - fTargetScrollMargin)) then
result:= result or integer(DROPEFFECT_SCROLL);
end;
end;
// -----------------------------------------------------------------------------
procedure TDropTarget.DoTargetScroll(Sender: TObject);
begin
with fTarget, fLastPoint do
begin
if (fDragImageHandle <> 0) then ImageList_DragLeave(handle);
if (Y < fTargetScrollMargin) then
Perform(WM_VSCROLL,SB_LINEUP,0)
else if (Y>ClientHeight - fTargetScrollMargin) then
Perform(WM_VSCROLL,SB_LINEDOWN,0);
if (X < fTargetScrollMargin) then
Perform(WM_HSCROLL,SB_LINEUP,0)
else if (X > ClientWidth - fTargetScrollMargin) then
Perform(WM_HSCROLL,SB_LINEDOWN,0);
if (fDragImageHandle <> 0) then
with ClientPtToWindowPt(handle,fLastPoint) do
ImageList_DragEnter(handle,x,y);
end;
end;
// -----------------------------------------------------------------------------
function TDropTarget.PasteFromClipboard: longint;
var
Global: HGlobal;
pEffect: ^DWORD;
begin
if not ClipBoard.HasFormat(CF_PREFERREDDROPEFFECT) then
result:= DROPEFFECT_NONE
else
begin
Global:= Clipboard.GetAsHandle(CF_PREFERREDDROPEFFECT);
pEffect:= pointer(GlobalLock(Global)); // DROPEFFECT_COPY, DROPEFFECT_MOVE ...
result:= pEffect^;
GlobalUnlock(Global);
end;
end;
// -----------------------------------------------------------------------------
// TDropFileTarget
// -----------------------------------------------------------------------------
constructor TDropFileTarget.Create( AOwner: TComponent );
begin
inherited Create( AOwner );
fFiles:= TStringList.Create;
fMappedNames:= TStringList.Create;
with fFileNameMapFormatEtc do
begin
cfFormat:= CF_FILENAMEMAP;
ptd:= nil;
dwAspect:= DVASPECT_CONTENT;
lindex:= -1;
tymed:= TYMED_HGLOBAL;
end;
with fFileNameMapWFormatEtc do
begin
cfFormat:= CF_FILENAMEMAPW;
ptd:= nil;
dwAspect:= DVASPECT_CONTENT;
lindex:= -1;
tymed:= TYMED_HGLOBAL;
end;
end;
// -----------------------------------------------------------------------------
destructor TDropFileTarget.Destroy;
begin
fFiles.Free;
fMappedNames.Free;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
function TDropFileTarget.PasteFromClipboard: longint;
var
Global: HGlobal;
Preferred: longint;
begin
result := DROPEFFECT_NONE;
if not ClipBoard.HasFormat(CF_HDROP) then exit;
Global:= Clipboard.GetAsHandle(CF_HDROP);
fFiles.clear;
if not GetFilesFromHGlobal(Global,fFiles) then exit;
Preferred:= inherited PasteFromClipboard;
//if no Preferred DropEffect then return copy else return Preferred ...
if (Preferred = DROPEFFECT_NONE) then
result:= DROPEFFECT_COPY else
result:= Preferred;
end;
// -----------------------------------------------------------------------------
function TDropFileTarget.HasValidFormats: boolean;
begin
result:= (fDataObj.QueryGetData(HDropFormatEtc) = S_OK);
end;
// -----------------------------------------------------------------------------
procedure TDropFileTarget.ClearData;
begin
fFiles.clear;
fMappedNames.clear;
end;
// -----------------------------------------------------------------------------
function TDropFileTarget.DoGetData: boolean;
var
medium: TStgMedium;
pFilename: pChar;
pFilenameW: PWideChar;
sFilename: string;
begin
ClearData;
result:= false;
if (fDataObj.GetData(HDropFormatEtc, medium) <> S_OK) then
exit;
try
if (medium.tymed = TYMED_HGLOBAL) and
GetFilesFromHGlobal(medium.HGlobal,fFiles) then
result:= true else
result:= false;
finally
//Don't forget to clean-up!
ReleaseStgMedium(medium);
end;
//OK, now see if file name mapping is also used ...
if (fDataObj.GetData(fFileNameMapFormatEtc, medium) = S_OK) then
try
if (medium.tymed = TYMED_HGLOBAL) then
begin
pFilename:= GlobalLock(medium.HGlobal);
try
while true do
begin
sFilename:= pFilename;
if sFilename = '' then break;
fMappedNames.add(sFilename);
inc(pFilename, length(sFilename)+1);
end;
if fFiles.count <> fMappedNames.count then
fMappedNames.clear;
finally
GlobalUnlock(medium.HGlobal);
end;
end;
finally
ReleaseStgMedium(medium);
end
else if (fDataObj.GetData(fFileNameMapWFormatEtc, medium) = S_OK) then
try
if (medium.tymed = TYMED_HGLOBAL) then
begin
pFilenameW:= GlobalLock(medium.HGlobal);
try
while true do
begin
sFilename:= WideCharToString(pFilenameW);
if sFilename = '' then break;
fMappedNames.add(sFilename);
inc(pFilenameW, length(sFilename)+1);
end;
if fFiles.count <> fMappedNames.count then
fMappedNames.clear;
finally
GlobalUnlock(medium.HGlobal);
end;
end;
finally
ReleaseStgMedium(medium);
end;
end;
// -----------------------------------------------------------------------------
// TDropTextTarget
// -----------------------------------------------------------------------------
function TDropTextTarget.PasteFromClipboard: longint;
var
Global: HGlobal;
TextPtr: pChar;
begin
result:= DROPEFFECT_NONE;
if not ClipBoard.HasFormat(CF_TEXT) then exit;
Global:= Clipboard.GetAsHandle(CF_TEXT);
TextPtr:= GlobalLock(Global);
fText:= TextPtr;
GlobalUnlock(Global);
result:= DROPEFFECT_COPY;
end;
// -----------------------------------------------------------------------------
function TDropTextTarget.HasValidFormats: boolean;
begin
result:= (fDataObj.QueryGetData(TextFormatEtc) = S_OK);
end;
// -----------------------------------------------------------------------------
procedure TDropTextTarget.ClearData;
begin
fText:= '';
end;
// -----------------------------------------------------------------------------
function TDropTextTarget.DoGetData: boolean;
var
medium: TStgMedium;
cText: pchar;
begin
result:= false;
medium.hGlobal:= 0;
if fText <> '' then result:= true // already got it!
else if (fDataObj.GetData(TextFormatEtc, medium) = S_OK) then
begin
try
if (medium.tymed <> TYMED_HGLOBAL) then exit;
cText:= PChar(GlobalLock(medium.HGlobal));
fText:= cText;
GlobalUnlock(medium.HGlobal);
result:= true;
finally
ReleaseStgMedium(medium);
end;
end
else
result:= false;
end;
// -----------------------------------------------------------------------------
// TDropLife32Target
// -----------------------------------------------------------------------------
function TDropLife32Target.PasteFromClipboard: longint;
var
Global: HGlobal;
TextPtr: pChar;
TempString: TStringList;
begin
Result:= DROPEFFECT_NONE;
if ClipBoard.HasFormat(CF_TEXT) then begin
Global:= Clipboard.GetAsHandle(CF_TEXT);
TextPtr:= GlobalLock(Global);
try
TempString:= TStringList.Create;
try
TempString.SetText(TextPtr);
if (FUniverse = nil) then FUniverse:= TUniverse.Create('',nbDefault);
FUniverse.LoadFromStringList(TempString);
finally TempString.Free;
end; {try}
finally GlobalUnlock(Global);
end; {try}
result:= DROPEFFECT_COPY;
end;
end;
// -----------------------------------------------------------------------------
function TDropLife32Target.HasValidFormats: boolean;
var
Life32PatternListEtc: TFormatEtc;
Life32FormatEtc: TFormatEtc;
begin
with Life32PatternListEtc do begin
cfFormat:= CF_LIFE32PATTERNLIST;
ptd:= nil; dwAspect:= DVASPECT_CONTENT; lindex:= -1; tymed:= TYMED_HGLOBAL;
end; {with}
with Life32FormatEtc do begin
cfFormat:= CF_LIFE32;
ptd:= nil; dwAspect:= DVASPECT_CONTENT; lindex:= -1; tymed:= TYMED_HGLOBAL;
end; {with}
Result:= (fDataObj.QueryGetData(TextFormatEtc) = S_OK) or
(fDataObj.QueryGetData(Life32PatternListEtc) = S_OK) or
(fDataObj.QueryGetData(Life32FormatEtc) = S_OK);
end;
// -----------------------------------------------------------------------------
procedure TDropLife32Target.ClearData;
begin
//FUniverse:= nil;
end;
// -----------------------------------------------------------------------------
function TDropLife32Target.DoGetData: boolean;
var
medium: TStgMedium;
cText: pchar;
NewPos: TPoint;
AStream: TMemoryStream;
ptr: Pointer;
TempString: TStringList;
var
Life32PatternListEtc: TFormatEtc;
Life32FormatEtc: TFormatEtc;
begin
with Life32PatternListEtc do begin
cfFormat:= CF_LIFE32PATTERNLIST;
ptd:= nil; dwAspect:= DVASPECT_CONTENT; lindex:= -1; tymed:= TYMED_HGLOBAL;
end; {with}
with Life32FormatEtc do begin
cfFormat:= CF_LIFE32;
ptd:= nil; dwAspect:= DVASPECT_CONTENT; lindex:= -1; tymed:= TYMED_HGLOBAL;
end; {with}
result:= false;
medium.hGlobal:= 0;
//if (FUniverse <> nil) then result:= true // already got it!
//Move an Item dragged inside the PatternList.
if (fDataObj.GetData(Life32PatternListEtc,medium) = S_OK) and
(Target is TListView) then begin
if (medium.tymed <> TYMED_HGLOBAL) then exit;
with Target as TListView do try
GetCursorPos(NewPos);
NewPos:= ScreenToClient(NewPos);
if Assigned(Selected) then Selected.SetPosition(NewPos);
finally
ReleaseStgMedium(medium);
end; {with try}
Result:= true;
end {else if}
//Data is in the form of a SnapshotStream.
else if (fDataObj.GetData(Life32FormatEtc,medium) = S_OK) then begin
try
ptr:= GlobalLock(medium.hGlobal);
AStream:= TMemoryStream.Create;
try
AStream.Write(ptr^,GlobalSize(medium.hGlobal));
FUniverse:= TUniverse.Create('',nbDefault);
FUniverse.RewindToSnapshot(AStream);
finally
AStream.Free;
GlobalUnlock(medium.hGlobal);
end; {try}
finally
ReleaseStgMedium(medium);
end; {try}
result:= true;
end {else if}
else if (fDataObj.GetData(TextFormatEtc, medium) = S_OK) then begin
try
if (medium.tymed <> TYMED_HGLOBAL) then exit;
cText:= PChar(GlobalLock(medium.HGlobal));
TempString:= TStringList.Create;
try
TempString.SetText(cText);
FUniverse:= TUniverse.Create('',nbDefault);
FUniverse.LoadFromStringList(TempString);
finally
TempString.Free;
GlobalUnlock(medium.hGlobal);
end; {try}
finally
ReleaseStgMedium(medium);
end; {try}
Result:= true;
end {else if}
else Result:= false;
end;
// -----------------------------------------------------------------------------
// TDropDummy
// This component is designed just to display drag images over the
// registered TWincontrol but where no drop is desired (eg a TForm).
// -----------------------------------------------------------------------------
function TDropDummy.HasValidFormats: boolean;
begin
result:= true;
end;
// -----------------------------------------------------------------------------
procedure TDropDummy.ClearData;
begin
//abstract method override
end;
// -----------------------------------------------------------------------------
function TDropDummy.DoGetData: boolean;
begin
result:= false;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.