summaryrefslogtreecommitdiff
path: root/src/corelib/gdi/fpg_oledragdrop.pas
blob: 29ebe7c3a853b7f4537056add37dac8d3fb026a0 (plain)
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    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.

    Description:
      This unit implements the OLE Drag-n-Drop functionality of Windows.
}
unit fpg_OLEDragDrop;

{$mode delphi}{$H+}

interface

uses
  Windows, Classes, ActiveX, ShellAPI, fpg_base;

const
  CFSTR_FILENAMEMAPA          = 'FileNameMap';            { CF_FILENAMEMAPA }
  CFSTR_FILENAMEMAP           = CFSTR_FILENAMEMAPA;
  CFSTR_FILEDESCRIPTORA       = 'FileGroupDescriptor';    { CF_FILEGROUPDESCRIPTORA }
  CFSTR_FILEDESCRIPTOR        = CFSTR_FILEDESCRIPTORA;
  CFSTR_FILECONTENTS          = 'FileContents';           { CF_FILECONTENTS }

type
  TfpgOLEFormatEtcList = class(TList)
  private
    function    GetFormatEtc(Index: Integer): PFormatEtc;
  protected
    procedure   Notify(Ptr: Pointer; Action: TListNotification); override;
  public
    constructor CreateCopy(AFormatEtcList: TfpgOLEFormatEtcList);
    property    FormatEtc[Index: Integer]: PFormatEtc read GetFormatEtc; default;
  end;


  TfpgOLEStgMediumList = class(TList)
  private
    function    GetStgMedium(Index: Integer): PStgMedium;
  protected
    procedure   Notify(Ptr: Pointer; Action: TListNotification); override;
  public
    property    StgMedium[Index: Integer]: PStgMedium read GetStgMedium; default;
  end;


  TfpgOLEDropSource = class(TInterfacedObject, IDropSource)
  private
    { IDropSource }
    function    QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; stdcall;
    function    GiveFeedback(dwEffect: Longint): HResult; stdcall;
  end;


  TfpgOLEDragDropEffect = (deNone, deCopy, deMove, deLink);
  TfpgOLEDragEnterEvent = procedure(Sender: TObject; DataObj: IDataObject; KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect) of object;
  TfpgOLEDragOverEvent = procedure(Sender: TObject; KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect) of object;
  TfpgOLEDragDropEvent = procedure(Sender: TObject; DataObj: IDataObject; KeyState: Longint; PT: TPoint; Effect: TfpgOLEDragDropEffect) of object;


  TfpgOLEDropTarget = class(TObject, IInterface, IDropTarget)
  private
    FDropTarget: TfpgWindowBase;
    FRegistered: Boolean;
    FOnDragEnter: TfpgOLEDragEnterEvent;
    FOnDragOver: TfpgOLEDragOverEvent;
    FOnDragLeave: TNotifyEvent;
    FOnDragDrop: TfpgOLEDragDropEvent;
  protected
    procedure   DoDragEnter(DataObj: IDataObject; KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect); virtual;
    procedure   DoDragOver(KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect); virtual;
    procedure   DoDragDrop(DataObj: IDataObject; KeyState: Longint; PT: TPoint; Effect: TfpgOLEDragDropEffect); virtual;
    { IInterface }
    function    QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function    _AddRef: Integer; stdcall;
    function    _Release: Integer; stdcall;
    { IDropTarget }
    function    DragEnter(const dataObj: IDataObject; grfKeyState: DWORD; pt: TPoint; var dwEffect: DWORD): HResult;StdCall;
    function    DragOver(grfKeyState: DWORD; pt: TPoint; var dwEffect: DWORD): HResult; stdcall;
    function    DragLeave: HResult; stdcall;
    function    Drop(const dataObj: IDataObject; grfKeyState: DWORD; pt: TPoint; var dwEffect: DWORD): HResult; stdcall;

    property    OnDragEnter: TfpgOLEDragEnterEvent read FOnDragEnter write FOnDragEnter;
    property    OnDragOver: TfpgOLEDragOverEvent read FOnDragOver write FOnDragOver;
    property    OnDragLeave: TNotifyEvent read FOnDragLeave write FOnDragLeave;
    property    OnDragDrop: TfpgOLEDragDropEvent read FOnDragDrop write FOnDragDrop;
  public
    constructor Create(ADropTargetWidget: TfpgWindowBase); reintroduce; { Actually a TfpgWidget }
    destructor  Destroy; override;
    procedure   RegisterDragDrop;
    procedure   RevokeDragDrop;
    property    DropTarget: TfpgWindowBase read FDropTarget;
  end;


  TfpgOLEDataObject = class(TInterfacedObject, IDataObject)
  private
    FFormatEtcList: TfpgOLEFormatEtcList;
    FStgMediumList: TfpgOLEStgMediumList;
    function    LookupFormatEtc(AFormat: TFormatEtc): Integer;
  protected
    { IDataObject }
    function    GetData(const formatetcIn: TFormatEtc; out medium: TStgMedium): HResult; stdcall;
    function    GetDataHere(const formatetc: TFormatEtc; out medium: TStgMedium): HResult; stdcall;
    function    QueryGetData(const formatetc: TFormatEtc): HResult; stdcall;
    function    GetCanonicalFormatEtc(const formatetc: TFormatEtc; out formatetcOut: TFormatEtc): HResult; stdcall;
    function    SetData(const formatetc: TFormatEtc; const medium: TStgMedium; fRelease: BOOL): HResult; stdcall;
    function    EnumFormatEtc(dwDirection: DWORD; out enumFormatEtc: IEnumFormatEtc): HResult; stdcall;
    function    DAdvise(const formatetc: TFormatEtc; advf: DWORD; const advSink: IAdviseSink; out dwConnection: DWORD): HResult; stdcall;
    function    DUnadvise(dwConnection: DWORD): HResult; stdcall;
    function    EnumDAdvise(out enumAdvise: IEnumStatData): HResult; stdcall;
  public
    constructor Create; overload;
    constructor Create(AFormatEtcList: TfpgOLEFormatEtcList); overload;
    destructor  Destroy; override;
    property    FormatEtcList: TfpgOLEFormatEtcList read FFormatEtcList;
    property    StgMediumList: TfpgOLEStgMediumList read FStgMediumList;
  end;


  TfpgOLEEnumFormatEtc = class(TInterfacedObject, IEnumFORMATETC)
  private
    FFormatEtcList: TfpgOLEFormatEtcList;
    FIndex: Integer;
  protected
    { IEnumFORMATETC }
    function    Next(celt: ULong; out elt:FormatEtc; pceltFetched: pULong=nil): HResult; stdcall;
    function    Skip(celt: ULong): HResult; stdcall;
    function    Reset: HResult; stdcall;
    function    Clone(out Enum: IEnumFormatEtc): HResult; stdcall;
  public
    constructor Create(AFormatEtcList: TfpgOLEFormatEtcList);
    destructor  Destroy; override;
  end;


  TDragFilesSource = class(TObject)
  private
    FFileNames: TStrings;
    FAliasFileNames: TStrings;
    function    GetAliasFileNames: TStrings;
    function    GetSourceFileNames: TStrings;
    procedure   SetAliasFileNames(const Value: TStrings);
    procedure   SetSourceFileNames(const Value: TStrings);
  public
    constructor Create;
    destructor  Destroy; override;
    procedure   Execute;
    property    SourceFileNames: TStrings read GetSourceFileNames write SetSourceFileNames;
    property    AliasFileNames: TStrings read GetAliasFileNames write SetAliasFileNames;
  end;


  TDragAcceptFilesEvent = function(Sender: TObject; FileNames: TStrings): Boolean of object;
  TDragAcceptPositionEvent = function(Sender: TObject; PT: TPoint): Boolean of object;
  TDropFilesEvent = procedure(Sender: TObject; PT: TPoint; FileNames: TStrings) of object;


  TDragFilesTarget = class(TfpgOLEDropTarget)
  private
    FDragAcceptFiles: Boolean;
    FOnDragAcceptFiles: TDragAcceptFilesEvent;
    FOnDragAcceptPosition: TDragAcceptPositionEvent;
    FOnDropFiles: TDropFilesEvent;
    procedure   GetFileNamesFromDropHandle(DropHandle: HDROP; SL: TStrings);
    procedure   StreamToFile(Stream: IStream; const FileName: string);
  protected
    function    DoDragAcceptFiles(DataObj: IDataObject): Boolean;
    function    DoDragAcceptPosition(PT: TPoint): Boolean;
    procedure   DoDropFiles(DataObj: IDataObject; PT: TPoint);
    procedure   DoDragEnter(DataObj: IDataObject; KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect); override;
    procedure   DoDragOver(KeyState: Longint; PT: TPoint; var Effect: TfpgOLEDragDropEffect); override;
    procedure   DoDragDrop(DataObj: IDataObject; KeyState: Longint; PT: TPoint; Effect: TfpgOLEDragDropEffect); override;
  public
    property    OnDragAcceptFiles: TDragAcceptFilesEvent read FOnDragAcceptFiles write FOnDragAcceptFiles;
    property    OnDragAcceptPosition: TDragAcceptPositionEvent read FOnDragAcceptPosition write FOnDragAcceptPosition;
    property    OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
  end;


implementation

uses
  SysUtils, ShlObj, fpg_widget;

var
  CF_FILENAMEMAP: Cardinal;
  CF_FILEDESCRIPTOR: Cardinal;
  CF_FILECONTENTS: Cardinal;

procedure DeepCopyFormatEtc(P1, P2: PFormatEtc);
begin
  P2^ := P1^;
  if P1^.ptd <> nil then begin
    P2^.ptd := CoTaskMemAlloc(SizeOf(tagDVTARGETDEVICE));
    P2^.ptd^ := P1^.ptd^;
  end;
end;

function DupGlobalMem(hMem: HGLOBAL): HGLOBAL;
var
  len: DWORD;
  Source: Pointer;
  Dest: HGLOBAL;
begin
  len := GlobalSize(hMem);
  Source := GlobalLock(hMem);
  Dest := GlobalAlloc(GMEM_FIXED, len);
  Move(Source^, Pointer(Dest)^, len);
  GlobalUnlock(hMem);
  Result := Dest;
end;

{ TDragFilesSource }

constructor TDragFilesSource.Create;
begin
  inherited Create;
  FFileNames := TStringList.Create;
  FAliasFileNames := TStringList.Create;
end;

destructor TDragFilesSource.Destroy;
begin
  FreeAndNil(FFileNames);
  FreeAndNil(FAliasFileNames);
  inherited Destroy;
end;

procedure TDragFilesSource.Execute;
var
  DataObject: TfpgOLEDataObject;
  DropSource: TfpgOLEDropSource;
  dwEffect: DWORD;
  dwResult: HRESULT;
  I: Integer;
  F: PFormatEtc;
  S: string;
  M: PStgMedium;
begin
  DataObject := TfpgOLEDataObject.Create;

  S := '';
  for I := 0 to FFileNames.Count - 1 do begin
    SetLength(S, Length(S)+Length(FFileNames[I])+1);
    Move(FFileNames[I][1], S[Length(S)-Length(FFileNames[I])], Length(FFileNames[I]));
    S[Length(S)] := #0;
  end;
  SetLength(S, Length(S)+1);
  S[Length(S)] := #0;

  New(F);
  F^.cfFormat := CF_HDROP;
  F^.ptd := nil;
  F^.dwAspect := DVASPECT_CONTENT;
  F^.lindex := -1;
  F^.tymed := TYMED_HGLOBAL;
  DataObject.FormatEtcList.Add(F);

  New(M);
  M^.tymed := TYMED_HGLOBAL;
  M^.hGlobal := Cardinal(GlobalAlloc(GMEM_FIXED, SizeOf(TDropFiles)+Length(S)));
  PDropFiles(M^.hGlobal)^.pFiles := SizeOf(TDropFiles);
  PDropFiles(M^.hGlobal)^.pt := Point(0,0);
  PDropFiles(M^.hGlobal)^.fNC := FALSE;
  PDropFiles(M^.hGlobal)^.fWide := FALSE;
  Move(S[1], PChar(M^.hGlobal+SizeOf(TDropFiles))^, Length(S));
  DataObject.StgMediumList.Add(M);

  if (FAliasFileNames.Count > 0) and (FAliasFileNames.Count = FFileNames.Count) then begin
    S := '';
    for I := 0 to FAliasFileNames.Count - 1 do begin
      SetLength(S, Length(S)+Length(FAliasFileNames[I])+1);
      Move(FAliasFileNames[I][1], S[Length(S)-Length(FAliasFileNames[I])], Length(FAliasFileNames[I]));
      S[Length(S)] := #0;
    end;
    SetLength(S, Length(S)+1);
    S[Length(S)] := #0;

    New(F);
    F^.cfFormat := CF_FILENAMEMAP;
    F^.ptd := nil;
    F^.dwAspect := DVASPECT_CONTENT;
    F^.lindex := -1;
    F^.tymed := TYMED_HGLOBAL;
    DataObject.FormatEtcList.Add(F);

    New(M);
    M^.tymed := TYMED_HGLOBAL;
    M^.hGlobal := Cardinal(GlobalAlloc(GMEM_FIXED, Length(S)));
    Move(S[1], PChar(M^.hGlobal)^, Length(S));
    DataObject.StgMediumList.Add(M);
  end;

  DropSource := TfpgOLEDropSource.Create;
  dwResult := ActiveX.DoDragDrop(DataObject as IDataObject, DropSource as IDropSource, DROPEFFECT_COPY, @dwEffect);

  if dwResult = DRAGDROP_S_DROP then begin
    if dwEffect = DROPEFFECT_COPY then begin
    end;
  end;
end;

function TDragFilesSource.GetAliasFileNames: TStrings;
begin
  Result := FAliasFileNames;
end;

function TDragFilesSource.GetSourceFileNames: TStrings;
begin
  Result := FFileNames;
end;

procedure TDragFilesSource.SetAliasFileNames(const Value: TStrings);
begin
  FAliasFileNames.Assign(Value);
end;

procedure TDragFilesSource.SetSourceFileNames(const Value: TStrings);
begin
  FFileNames.Assign(Value);
end;

{ TfpgOLEDropSource }

function TfpgOLEDropSource.GiveFeedback(dwEffect: Integer): HResult;
begin
  Result := DRAGDROP_S_USEDEFAULTCURSORS;
end;

function TfpgOLEDropSource.QueryContinueDrag(fEscapePressed: BOOL;
  grfKeyState: Integer): HResult;
begin
  if FEscapePressed then
    Result := DRAGDROP_S_CANCEL
  else if (grfKeyState and (MK_LBUTTON or MK_RBUTTON) = 0) then
    Result := DRAGDROP_S_DROP
  else
    Result := S_OK;
end;

{ TfpgOLEDataObject }

constructor TfpgOLEDataObject.Create(AFormatEtcList: TfpgOLEFormatEtcList);
begin
  inherited Create;
  FFormatEtcList := TfpgOLEFormatEtcList.CreateCopy(AFormatEtcList);
  FStgMediumList := TfpgOLEStgMediumList.Create;
end;

constructor TfpgOLEDataObject.Create;
begin
  inherited Create;
  FFormatEtcList := TfpgOLEFormatEtcList.Create;
  FStgMediumList := TfpgOLEStgMediumList.Create;
end;

function TfpgOLEDataObject.DAdvise(const formatetc: TFormatEtc; advf: DWORD;
  const advSink: IAdviseSink; out dwConnection: DWORD): HResult;
begin
  Result := OLE_E_ADVISENOTSUPPORTED;
end;

destructor TfpgOLEDataObject.Destroy;
begin
  FreeAndNil(FFormatEtcList);
  FreeAndNil(FStgMediumList);
  inherited Destroy;
end;

function TfpgOLEDataObject.DUnadvise(dwConnection: DWORD): HResult;
begin
  Result := OLE_E_ADVISENOTSUPPORTED;
end;

function TfpgOLEDataObject.EnumDAdvise(out enumAdvise: IEnumStatData): HResult;
begin
  Result := OLE_E_ADVISENOTSUPPORTED;
end;

function TfpgOLEDataObject.EnumFormatEtc(dwDirection: DWORD;
  out enumFormatEtc: IEnumFormatEtc): HResult;
begin
  if dwDirection = DATADIR_GET then begin
    enumFormatEtc := TfpgOLEEnumFormatEtc.Create(FFormatEtcList) as IEnumFormatEtc;
    Result := S_OK;
  end
  else begin
    Result := E_NOTIMPL;
  end;
end;

function TfpgOLEDataObject.GetCanonicalFormatEtc(const formatetc: TFormatEtc;
  out formatetcOut: TFormatEtc): HResult;
begin
  // Apparently we have to set this field to NULL even though we don't do anything else
  formatetcOut.ptd := nil;
  Result := E_NOTIMPL;
end;

function TfpgOLEDataObject.GetData(const formatetcIn: TFormatEtc;
  out medium: TStgMedium): HResult;
var
  Idx: Integer;
begin
  Idx := LookupFormatEtc(formatetcIn);
  if Idx = -1 then
    Result := DV_E_FORMATETC
  else begin
    medium.tymed := FFormatEtcList[Idx]^.tymed;
    medium.PUnkForRelease := nil;
    if medium.tymed = TYMED_HGLOBAL then begin
      medium.hGlobal := DupGlobalMem(FStgMediumList[Idx]^.hGlobal);
      Result := S_OK;
    end
    else
      Result := DV_E_FORMATETC;
  end;
end;

function TfpgOLEDataObject.GetDataHere(const formatetc: TFormatEtc;
  out medium: TStgMedium): HResult;
begin
  Result := DV_E_FORMATETC;
end;

function TfpgOLEDataObject.LookupFormatEtc(AFormat: TFormatEtc): Integer;
var
  I: Integer;
begin
  Result := -1;
  for I := 0 to FFormatEtcList.Count - 1 do begin
    if (FFormatEtcList[I]^.cfFormat = AFormat.cfFormat) and
       (FFormatEtcList[I]^.dwAspect = AFormat.dwAspect) and
       (FFormatEtcList[I]^.tymed = AFormat.tymed) then begin
      Result := I;
      Break;
    end;
  end;
end;

function TfpgOLEDataObject.QueryGetData(const formatetc: TFormatEtc): HResult;
begin
  if LookupFormatEtc(formatetc) >= 0 then begin
    Result := S_OK;
  end
  else begin
    Result := DV_E_FORMATETC;
  end;
end;

function TfpgOLEDataObject.SetData(const formatetc: TFormatEtc;
  const medium: TStgMedium; fRelease: BOOL): HResult;
begin
  Result := E_NOTIMPL;
end;

{ TfpgOLEEnumFormatEtc }

function TfpgOLEEnumFormatEtc.Clone(out Enum: IEnumFormatEtc): HResult;
var
  C: TfpgOLEEnumFormatEtc;
begin
  // make a duplicate enumerator
  C := TfpgOLEEnumFormatEtc.Create(FFormatEtcList);
  // manually set the index state
  C.FIndex := FIndex;
  Enum := C as IEnumFormatEtc;
  Result := S_OK;
end;

constructor TfpgOLEEnumFormatEtc.Create(AFormatEtcList: TfpgOLEFormatEtcList);
begin
  FFormatEtcList := TfpgOLEFormatEtcList.CreateCopy(AFormatEtcList);
  FIndex := 0;
end;

destructor TfpgOLEEnumFormatEtc.Destroy;
begin
  FreeAndNil(FFormatEtcList);
  inherited;
end;

function TfpgOLEEnumFormatEtc.Next(celt: ULong; out elt:FormatEtc;
  pceltFetched: pULong): HResult;
var
  Copied: Integer;
  OutBuf: PFormatEtc;
begin
  // copy the FORMATETC structures into the caller's buffer
  OutBuf := PFormatEtc(@elt);
  Copied := 0;
  while(FIndex < FFormatEtcList.Count) and (Copied < celt) do begin
    DeepCopyFormatEtc(FFormatEtcList[FIndex], OutBuf);
    OutBuf := PFormatEtc(Cardinal(OutBuf) + SizeOf(TFormatEtc));
    Inc(Copied);
    FIndex := FIndex + 1;
  end;

  // store result
  if (pceltFetched <> nil) then
    pceltFetched^ := Copied;

  // did we copy all that was requested?
  if (Copied = celt) then Result := S_OK
  else Result := S_FALSE;
end;

function TfpgOLEEnumFormatEtc.Reset: HResult;
begin
  FIndex := 0;
  Result := S_OK;
end;

function TfpgOLEEnumFormatEtc.Skip(celt: ULong): HResult;
begin
  FIndex := FIndex + celt;
  if FIndex <= FFormatEtcList.Count then Result := S_OK
  else Result := S_FALSE;
end;

{ TfpgOLEFormatEtcList }

constructor TfpgOLEFormatEtcList.CreateCopy(AFormatEtcList: TfpgOLEFormatEtcList);
var
  I: Integer;
  P: PFormatEtc;
begin
  Create;
  for I := 0 to AFormatEtcList.Count - 1 do begin
    New(P);
    DeepCopyFormatEtc(AFormatEtcList[I], P);
    Add(P);
  end;
end;

function TfpgOLEFormatEtcList.GetFormatEtc(Index: Integer): PFormatEtc;
begin
  Result := PFormatEtc(Get(Index));
end;

procedure TfpgOLEFormatEtcList.Notify(Ptr: Pointer; Action: TListNotification);
begin
  if Action = lnDeleted then begin
    if PFormatEtc(Ptr)^.ptd <> nil then begin
      CoTaskMemFree(PFormatEtc(Ptr)^.ptd);
    end;
    Dispose(PFormatEtc(Ptr));
  end;
  inherited;
end;

{ TfpgOLEStgMediumList }

function TfpgOLEStgMediumList.GetStgMedium(Index: Integer): PStgMedium;
begin
  Result := PStgMedium(Get(Index));
end;

procedure TfpgOLEStgMediumList.Notify(Ptr: Pointer; Action: TListNotification);
begin
  if Action = lnDeleted then begin
    if PStgMedium(Ptr)^.hGlobal <> 0 then begin
      GlobalFree(PStgMedium(Ptr)^.hGlobal);
    end;
    Dispose(Ptr);
  end;
  inherited;
end;

{ TfpgOLEDropTarget }

function TfpgOLEDropTarget.DragEnter(const dataObj: IDataObject;
  grfKeyState: DWORD; pt: TPoint; var dwEffect: DWORD): HResult;
var
  Effect: TfpgOLEDragDropEffect;
begin
  dwEffect := DROPEFFECT_NONE;
  Effect := deNone;
  DoDragEnter(dataObj, grfKeyState, pt, Effect);
  case Effect of
    deNone: dwEffect := DROPEFFECT_NONE;
    deCopy: dwEffect := DROPEFFECT_COPY;
    deMove: dwEffect := DROPEFFECT_MOVE;
    deLink: dwEffect := DROPEFFECT_LINK;
  end;
  Result := S_OK;
end;

function TfpgOLEDropTarget.DragLeave: HResult;
begin
  Result := S_OK;
end;

function TfpgOLEDropTarget.DragOver(grfKeyState: DWORD; pt: TPoint;
  var dwEffect: DWORD): HResult;
var
  Effect: TfpgOLEDragDropEffect;
begin
  if ((MK_SHIFT and grfKeyState) = MK_SHIFT) and
     ((dwEffect and DROPEFFECT_MOVE) = DROPEFFECT_MOVE) then begin
    Effect := deMove;
  end;
  if ((MK_CONTROL and grfKeyState) = MK_CONTROL) and
     ((dwEffect and DROPEFFECT_COPY) = DROPEFFECT_COPY) then begin
    Effect := deCopy;
  end;
  if dwEffect and DROPEFFECT_COPY > 0 then Effect := deCopy
  else if dwEffect and DROPEFFECT_MOVE  > 0 then Effect := deMove
  else if dwEffect and DROPEFFECT_LINK > 0 then Effect := deLink
  else Effect := deNone;
  DoDragOver(grfKeyState, pt, Effect);
  case Effect of
    deNone: dwEffect := DROPEFFECT_NONE;
    deCopy: dwEffect := DROPEFFECT_COPY;
    deMove: dwEffect := DROPEFFECT_MOVE;
    deLink: dwEffect := DROPEFFECT_LINK;
  end;
  Result := S_OK;
end;

function TfpgOLEDropTarget.Drop(const dataObj: IDataObject;
  grfKeyState: DWORD; pt: TPoint; var dwEffect: DWORD): HResult;
var
  Effect: TfpgOLEDragDropEffect;
begin
  if dwEffect and DROPEFFECT_COPY > 0 then Effect := deCopy
  else if dwEffect and DROPEFFECT_MOVE  > 0 then Effect := deMove
  else if dwEffect and DROPEFFECT_LINK > 0 then Effect := deLink
  else Effect := deNone;
  DoDragDrop(dataObj, grfKeyState, pt, Effect);
  Result := S_OK;
end;

function TfpgOLEDropTarget._AddRef: Integer;
begin
  Result := 1;
end;

function TfpgOLEDropTarget._Release: Integer;
begin
  Result := 1;
end;

function TfpgOLEDropTarget.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

constructor TfpgOLEDropTarget.Create(ADropTargetWidget: TfpgWindowBase);
begin
  inherited Create;
  FDropTarget := ADropTargetWidget;
  FRegistered := False;
end;

procedure TfpgOLEDropTarget.RegisterDragDrop;
begin
  ActiveX.RegisterDragDrop(TfpgWidget(FDropTarget).WinHandle, Self as IDropTarget);
  FRegistered := True;
end;

procedure TfpgOLEDropTarget.RevokeDragDrop;
begin
  FRegistered := False;
  ActiveX.RevokeDragDrop(TfpgWidget(FDropTarget).WinHandle);
end;

destructor TfpgOLEDropTarget.Destroy;
begin
  if FRegistered then RevokeDragDrop;
  inherited;
end;

procedure TfpgOLEDropTarget.DoDragEnter(DataObj: IDataObject;
  KeyState: Integer; PT: TPoint; var Effect: TfpgOLEDragDropEffect);
begin
  if Assigned(FOnDragEnter) then begin
    FOnDragEnter(Self, DataObj, KeyState, PT, Effect);
  end;
end;

procedure TfpgOLEDropTarget.DoDragOver(KeyState: Integer; PT: TPoint;
  var Effect: TfpgOLEDragDropEffect);
begin
  if Assigned(FOnDragOver) then begin
    FOnDragOver(Self, KeyState, PT, Effect);
  end;
end;

procedure TfpgOLEDropTarget.DoDragDrop(DataObj: IDataObject; KeyState: Integer;
  PT: TPoint; Effect: TfpgOLEDragDropEffect);
begin
  if Assigned(FOnDragDrop) then begin
    FOnDragDrop(Self, DataObj, KeyState, PT, Effect);
  end;
end;

{ TDragFilesTarget }

function TDragFilesTarget.DoDragAcceptFiles(DataObj: IDataObject): Boolean;
const
  FormatEtcHDrop:           TFormatEtc = (cfFormat:CF_HDROP;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_HGLOBAL);
  FormatEtcFileDescriptor:  TFormatEtc = (cfFormat:0;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_HGLOBAL);
  FormatEtcFileContents:    TFormatEtc = (cfFormat:0;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_ISTREAM);
var
  StgMedium: TStgMedium;
  DropHandle: HDROP;
  EnumFormatEtc: IEnumFORMATETC;
  FE: TFormatEtc;
  FetchedCount: Longint;
  FormatName: array[0..MAX_PATH] of Char;
  FileGroupDescriptor: PFileGroupDescriptorA;
  I, Count: Integer;
  FileDescriptor: TFileDescriptorA;
  FileNames: TStringList;
begin
  FileNames := TStringList.Create;
  try
    if Assigned(FOnDragAcceptFiles) then
    begin
      Result := False;
      FormatEtcFileDescriptor.cfFormat := CF_FILEDESCRIPTOR;
      FormatEtcFileContents.cfFormat := CF_FILECONTENTS;

      if (DataObj.QueryGetData(FormatEtcHDrop) = S_OK) and
         (DataObj.GetData(FormatEtcHDrop,StgMedium) = S_OK) then
      begin
        DropHandle := StgMedium.hGlobal;
        GetFileNamesFromDropHandle(DropHandle, FileNames);
        Result := FOnDragAcceptFiles(Self, FileNames);
        ReleaseStgMedium(StgMedium);
      end
      else
      if (DataObj.QueryGetData(FormatEtcFileDescriptor) = S_OK) and
         (DataObj.QueryGetData(FormatEtcFileContents) = S_OK) and
         (DataObj.GetData(FormatEtcFileDescriptor,StgMedium) = S_OK) then
      begin
        FileGroupDescriptor := GlobalLock(StgMedium.hGlobal);
        if FileGroupDescriptor <> nil then
        begin
          Count := FileGroupDescriptor^.cItems;
          I := 0;
          while I < Count do
          begin
            FileDescriptor := FileGroupDescriptor^.fgd[I];
            FileNames.Add(FileDescriptor.cFileName);
            Inc(I);
          end;
          GlobalUnlock(StgMedium.hGlobal);
        end;
        Result := FOnDragAcceptFiles(Self, FileNames);
        ReleaseStgMedium(StgMedium);
      end
      else
      begin
//        DataObj.EnumFormatEtc(DATADIR_GET, EnumFormatEtc);
//        EnumFormatEtc.Reset;
//        while EnumFormatEtc.Next(1, FE, @FetchedCount) = S_OK do begin
//          GetClipboardFormatName(FE.cfFormat,FormatName,MAX_PATH);
//          ShowMessage(FormatName);
//        end;
      end;
    end
    else
    begin
      Result := True;
    end;
  finally
    FileNames.Free;
  end;
end;

procedure TDragFilesTarget.DoDragEnter(DataObj: IDataObject;
  KeyState: Integer; PT: TPoint; var Effect: TfpgOLEDragDropEffect);
begin
  FDragAcceptFiles := DoDragAcceptFiles(DataObj);
  if FDragAcceptFiles and DoDragAcceptPosition(PT) then begin
    inherited;
  end else begin
    Effect := deNone;
  end;
end;

procedure TDragFilesTarget.DoDragOver(KeyState: Integer; PT: TPoint;
  var Effect: TfpgOLEDragDropEffect);
begin
  if FDragAcceptFiles and DoDragAcceptPosition(PT) then begin
    inherited;
  end else begin
    Effect := deNone;
  end;
end;

procedure TDragFilesTarget.DoDragDrop(DataObj: IDataObject;
  KeyState: Integer; PT: TPoint; Effect: TfpgOLEDragDropEffect);
begin
  DoDropFiles(DataObj, PT);
  inherited;
end;

function TDragFilesTarget.DoDragAcceptPosition(PT: TPoint): Boolean;
begin
  if Assigned(FOnDragAcceptPosition) then begin
    Result := FOnDragAcceptPosition(Self, PT);
  end else begin
    Result := True;
  end;
end;

procedure TDragFilesTarget.DoDropFiles(DataObj: IDataObject; PT: TPoint);
const
  FormatEtcHDrop: TFormatEtc = (cfFormat:CF_HDROP;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_HGLOBAL);
  FormatEtcFileDescriptor: TFormatEtc =
    (cfFormat:0;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_HGLOBAL);
  FormatEtcFileContents: TFormatEtc =
    (cfFormat:0;ptd:nil;dwAspect:DVASPECT_CONTENT;lindex:-1;tymed:TYMED_ISTREAM);
var
  StgMedium, StgMediumContents: TStgMedium;
  DropHandle: HDROP;
  FileNames: TStringList;
  FileGroupDescriptor: PFileGroupDescriptorA;
  I, Count: Integer;
  FileDescriptor: TFileDescriptorA;
  Path: array[0..MAX_PATH] of Char;
  TempFileName: string;
begin
  if not Assigned(FOnDropFiles) then Exit;
  FileNames := TStringList.Create;
  try
    FormatEtcFileDescriptor.cfFormat := CF_FILEDESCRIPTOR;
    FormatEtcFileContents.cfFormat := CF_FILECONTENTS;
    if (DataObj.QueryGetData(FormatEtcHDrop) = S_OK) and
       (DataObj.GetData(FormatEtcHDrop,StgMedium) = S_OK) then begin
      DropHandle := StgMedium.hGlobal;
      GetFileNamesFromDropHandle(DropHandle, FileNames);
      FOnDropFiles(Self, PT, FileNames);
      ReleaseStgMedium(StgMedium);
    end else
    if (DataObj.QueryGetData(FormatEtcFileDescriptor) = S_OK) and
       (DataObj.QueryGetData(FormatEtcFileContents) = S_OK) and
       (DataObj.GetData(FormatEtcFileDescriptor,StgMedium) = S_OK) then begin
      GetTempPath(MAX_PATH, Path);
      GetTempFileName(Path, 'PXM', 0, Path);
      FileGroupDescriptor := GlobalLock(StgMedium.hGlobal);
      if FileGroupDescriptor <> nil then begin
        Count := FileGroupDescriptor^.cItems;
        I := 0;
        while I < Count do begin
          FileDescriptor := FileGroupDescriptor^.fgd[I];
          TempFileName := ChangeFileExt(Path, ExtractFileExt(FileDescriptor.cFileName));
          FormatEtcFileContents.lindex := I;
          if (DataObj.GetData(FormatEtcFileContents,StgMediumContents) = S_OK) then begin
            StreamToFile(IStream(StgMediumContents.pstm), TempFileName);
            FileNames.Clear;
            FileNames.Add(TempFileName);
            FOnDropFiles(Self, PT, FileNames);
            ReleaseStgMedium(StgMediumContents);
          end;
          Inc(I);
        end;
        GlobalUnlock(StgMedium.hGlobal);
      end;
      FOnDropFiles(Self, PT, FileNames);
      ReleaseStgMedium(StgMedium);
      ReleaseStgMedium(StgMediumContents);
    end;
  finally
    FileNames.Free;
  end;
end;

procedure TDragFilesTarget.GetFileNamesFromDropHandle(DropHandle: HDROP; SL: TStrings);
var
  I: Integer;
  Path: array[0..MAX_PATH] of Char;
begin
  for I := 0 to DragQueryFile(DropHandle, $FFFFFFFF, nil, 0) do begin
    DragQueryFile(DropHandle, I, Path, MAX_PATH);
    SL.Add(Path);
  end;
  DragFinish(DropHandle);
end;

procedure TDragFilesTarget.StreamToFile(Stream: IStream; const FileName: string);
const
  BLOCK_SIZE = 4096;
var
  BytesRead: Longint;
  FileStream: TFileStream;
  Buffer: array[0..BLOCK_SIZE] of Byte;
begin
  FileStream := TFileStream.Create(FileName, fmCreate);
  try
    while (Stream.Read(@Buffer[0], BLOCK_SIZE, @BytesRead) = S_OK) and (BytesRead > 0) do begin
      FileStream.Write(Buffer, BytesRead);
    end;
  finally
    FileStream.Free;
  end;
end;

initialization
  CF_FILENAMEMAP := RegisterClipboardFormat(CFSTR_FILENAMEMAP);
  CF_FILEDESCRIPTOR := RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
  CF_FILECONTENTS := RegisterClipboardFormat(CFSTR_FILECONTENTS);

finalization

end.