/
DirView.pas
3491 lines (3147 loc) · 102 KB
/
DirView.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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit DirView;
{===============================================================
Component TDirView / Version 2.6, January 2000
===============================================================
Description:
============
Displays files of a single directory as listview with shell
icons. Complete drag&Drop support for files and directories.
Author:
=======
(c) Ingo Eckel 1998, 1999
Sodener Weg 38
65812 Bad Soden
Germany
Modifications (for WinSCP):
===========================
(c) Martin Prikryl 2001- 2004
V2.6:
- Shows "shared"-symbol with directories
- Delphi5 compatible
For detailed documentation and history see TDirView.htm.
===============================================================}
{Required compiler options for TDirView:}
{$A+,B-,X+,H+,P+}
interface
{$WARN UNIT_PLATFORM OFF}
{$WARN SYMBOL_PLATFORM OFF}
uses
Windows, ShlObj, ComCtrls, CompThread, CustomDirView, ListExt,
ExtCtrls, Graphics, FileOperator, DiscMon, Classes, DirViewColProperties,
DragDrop, Messages, ListViewColProperties, CommCtrl, DragDropFilesEx,
FileCtrl, SysUtils, BaseUtils, Controls, CustomDriveView, System.Generics.Collections, Winapi.ShellAPI;
{$I ResStrings.pas }
type
TVolumeDisplayStyle = (doPrettyName, doDisplayName); {Diplaytext of drive node}
const
msThreadChangeDelay = 10; {TDiscMonitor: change delay}
MaxWaitTimeOut = 10; {TFileDeleteThread: wait nn seconds for deleting files or directories}
{$WARN SYMBOL_DEPRECATED OFF}
FileAttr = SysUtils.faAnyFile and (not SysUtils.faVolumeID);
{$WARN SYMBOL_DEPRECATED ON}
SpecialExtensions = 'EXE,LNK,ICO,ANI,CUR,PIF,JOB,CPL';
ExeExtension = 'EXE';
type
{Exceptions:}
EIUThread = class(Exception);
EDragDrop = class(Exception);
EInvalidFileName = class(Exception);
ERenameFileFailed = class(Exception);
TClipboardOperation = (cboNone, cboCut, cboCopy);
{Record for each file item:}
PFileRec = ^TFileRec;
TFileRec = record
Empty: Boolean;
IconEmpty: Boolean;
IsDirectory: Boolean;
IsRecycleBin: Boolean;
IsParentDir: Boolean;
FileName: string;
Displayname: string;
FileExt: string;
TypeName: string;
ImageIndex: Integer;
Size: Int64;
Attr: LongWord;
FileTime: TFileTime;
PIDL: PItemIDList; {Fully qualified PIDL}
end;
{Record for fileinfo caching:}
PInfoCache = ^TInfoCache;
TInfoCache = record
FileExt: string;
TypeName: string;
ImageIndex: Integer;
end;
{Additional events:}
type
TDirViewFileSizeChanged = procedure(Sender: TObject; Item: TListItem) of object;
TDirViewFileIconForName = procedure(Sender: TObject; Item: TListItem; var FileName: string) of object;
type
TDirView = class;
{ TIconUpdateThread (Fetch shell icons via thread) }
TIconUpdateThread = class(TCompThread)
private
FOwner: TDirView;
FIndex: Integer;
FMaxIndex: Integer;
FNewIcons: Boolean;
FSyncIcon: Integer;
CurrentIndex: Integer;
CurrentFilePath: string;
CurrentItemData: TFileRec;
InvalidItem: Boolean;
procedure SetIndex(Value: Integer);
procedure SetMaxIndex(Value: Integer);
protected
constructor Create(Owner: TDirView);
procedure DoFetchData;
procedure DoUpdateIcon;
procedure Execute; override;
property Index: Integer read FIndex write SetIndex;
property MaxIndex: Integer read FMaxIndex write SetMaxIndex;
public
procedure Terminate; override;
end;
{ TDirView }
TDirView = class(TCustomDirView)
private
FConfirmDelete: Boolean;
FConfirmOverwrite: Boolean;
FUseIconCache: Boolean;
FInfoCacheList: TListExt;
FDriveView: TCustomDriveView;
FChangeTimer: TTimer;
FChangeInterval: Cardinal;
FUseIconUpdateThread: Boolean;
FIUThreadFinished: Boolean;
FDriveType: Integer;
FCompressedColor: TColor;
FParentFolder: IShellFolder;
FDesktopFolder: IShellFolder;
FDirOK: Boolean;
FPath: string;
SelectNewFiles: Boolean;
FHiddenCount: Integer;
FFilteredCount: Integer;
{shFileOperation-shell component TFileOperator:}
FFileOperator: TFileOperator;
{Additional thread components:}
FIconUpdateThread: TIconUpdateThread;
FDiscMonitor: TDiscMonitor;
FHomeDirectory: string;
{Additional events:}
FOnFileIconForName: TDirViewFileIconForName;
iRecycleFolder: iShellFolder;
PIDLRecycle: PItemIDList;
FLastPath: TDictionary<string, string>;
FTimeoutShellIconRetrieval: Boolean;
{Drag&Drop:}
function GetDirColProperties: TDirViewColProperties;
function GetHomeDirectory: string;
{Drag&drop helper functions:}
procedure SignalFileDelete(Sender: TObject; Files: TStringList);
procedure PerformDragDropFileOperation(TargetPath: string; Effect: Integer;
RenameOnCollision: Boolean; Paste: Boolean);
procedure SetDirColProperties(Value: TDirViewColProperties);
protected
function NewColProperties: TCustomListViewColProperties; override;
function SortAscendingByDefault(Index: Integer): Boolean; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure Delete(Item: TListItem); override;
procedure DDError(ErrorNo: TDDError);
function GetCanUndoCopyMove: Boolean; virtual;
{Shell namespace functions:}
function GetShellFolder(Dir: string): iShellFolder;
function GetDirOK: Boolean; override;
procedure GetDisplayInfo(ListItem: TListItem; var DispInfo: TLVItem); override;
procedure DDDragDetect(grfKeyState: Longint; DetectStart, Point: TPoint;
DragStatus: TDragDetectStatus); override;
procedure DDMenuPopup(Sender: TObject; AMenu: HMenu; DataObj: IDataObject;
AMinCustCmd:integer; grfKeyState: Longint; pt: TPoint); override;
procedure DDMenuDone(Sender: TObject; AMenu: HMenu); override;
procedure DDDropHandlerSucceeded(Sender: TObject; grfKeyState: Longint;
Point: TPoint; dwEffect: Longint); override;
procedure DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer); override;
function GetPathName: string; override;
procedure SetChangeInterval(Value: Cardinal); virtual;
procedure LoadFromRecycleBin(Dir: string); virtual;
procedure SetLoadEnabled(Value: Boolean); override;
function GetPath: string; override;
procedure SetPath(Value: string); override;
procedure PathChanged; override;
procedure SetItemImageIndex(Item: TListItem; Index: Integer); override;
procedure SetCompressedColor(Value: TColor);
procedure ChangeDetected(Sender: TObject; const Directory: string;
var SubdirsChanged: Boolean);
procedure ChangeInvalid(Sender: TObject; const Directory: string; const ErrorStr: string);
procedure TimerOnTimer(Sender: TObject);
procedure ResetItemImage(Index: Integer);
procedure SetWatchForChanges(Value: Boolean); override;
procedure AddParentDirItem;
procedure AddToDragFileList(FileList: TFileList; Item: TListItem); override;
function DragCompleteFileList: Boolean; override;
procedure ExecuteFile(Item: TListItem); override;
function GetIsRoot: Boolean; override;
procedure InternalEdit(const HItem: TLVItem); override;
function ItemColor(Item: TListItem): TColor; override;
function ItemFileExt(Item: TListItem): string;
function ItemFileNameOnly(Item: TListItem): string;
function ItemImageIndex(Item: TListItem; Cache: Boolean): Integer; override;
function ItemIsFile(Item: TListItem): Boolean; override;
function ItemIsRecycleBin(Item: TListItem): Boolean; override;
function ItemMatchesFilter(Item: TListItem; const Filter: TFileFilter): Boolean; override;
function FileMatches(FileName: string; const SearchRec: TSearchRec): Boolean;
function ItemOverlayIndexes(Item: TListItem): Word; override;
procedure LoadFiles; override;
procedure PerformItemDragDropOperation(Item: TListItem; Effect: Integer; Paste: Boolean); override;
procedure SortItems; override;
procedure StartFileDeleteThread;
procedure WMDestroy(var Msg: TWMDestroy); message WM_DESTROY;
procedure CMRecreateWnd(var Message: TMessage); message CM_RECREATEWND;
procedure Load(DoFocusSomething: Boolean); override;
function GetFileInfo(pszPath: LPCWSTR; dwFileAttributes: DWORD; var psfi: TSHFileInfoW; cbFileInfo, uFlags: UINT): DWORD_PTR;
function HiddenCount: Integer; override;
function FilteredCount: Integer; override;
public
{Runtime, readonly properties:}
property DriveType: Integer read FDriveType;
{Linked component TDriveView:}
property DriveView: TCustomDriveView read FDriveView write FDriveView;
{ required, otherwise AV generated, when dragging columns}
property Columns stored False;
property ParentFolder: IShellFolder read FParentFolder;
{Drag&Drop runtime, readonly properties:}
property CanUndoCopyMove: Boolean read GetCanUndoCopyMove;
property DDFileOperator: TFileOperator read FFileOperator;
{Drag&Drop fileoperation methods:}
function UndoCopyMove: Boolean; dynamic;
{Clipboard fileoperation methods (requires drag&drop enabled):}
procedure EmptyClipboard; dynamic;
function CopyToClipBoard: Boolean; dynamic;
function CutToClipBoard: Boolean; dynamic;
function PasteFromClipBoard(TargetPath: string = ''): Boolean; override;
function DuplicateSelectedFiles: Boolean; dynamic;
procedure DisplayPropertiesMenu; override;
procedure DisplayContextMenu(Where: TPoint); override;
procedure ExecuteParentDirectory; override;
procedure ExecuteRootDirectory; override;
function ItemIsDirectory(Item: TListItem): Boolean; override;
function ItemFullFileName(Item: TListItem): string; override;
function ItemIsParentDirectory(Item: TListItem): Boolean; override;
function ItemFileName(Item: TListItem): string; override;
function ItemFileSize(Item: TListItem): Int64; override;
function ItemFileTime(Item: TListItem; var Precision: TDateTimePrecision): TDateTime; override;
{Thread handling: }
procedure StartWatchThread;
procedure StopWatchThread;
function WatchThreadActive: Boolean;
procedure StartIconUpdateThread;
procedure StopIconUpdateThread;
procedure TerminateThreads;
{Other additional functions: }
procedure ClearIconCache;
{Create a new file:}
function CreateFile(NewName: string): TListItem; dynamic;
{Create a new subdirectory:}
procedure CreateDirectory(DirName: string); override;
{Delete all selected files:}
{Check, if file or files still exists:}
procedure ValidateFile(Item: TListItem); overload;
procedure ValidateFile(FileName:TFileName); overload;
procedure ValidateSelectedFiles; dynamic;
{Access the internal data-structures:}
function AddItem(SRec: SysUtils.TSearchRec): TListItem; reintroduce;
procedure GetDisplayData(Item: TListItem; FetchIcon: Boolean);
function GetFileRec(Index: Integer): PFileRec;
{Populate / repopulate the filelist:}
procedure Reload(CacheIcons : Boolean); override;
procedure Reload2;
function FormatFileTime(FileTime: TFileTime): string; virtual;
function GetAttrString(Attr: Integer): string; virtual;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ExecuteHomeDirectory; override;
procedure ReloadDirectory; override;
procedure ExecuteDrive(Drive: string);
property HomeDirectory: string read GetHomeDirectory write FHomeDirectory;
property TimeoutShellIconRetrieval: Boolean read FTimeoutShellIconRetrieval write FTimeoutShellIconRetrieval;
published
property DirColProperties: TDirViewColProperties read GetDirColProperties write SetDirColProperties;
property PathLabel;
property OnUpdateStatusBar;
property DimmHiddenFiles;
property ShowHiddenFiles;
property WantUseDragImages;
property TargetPopupMenu;
property AddParentDir;
property OnSelectItem;
property OnStartLoading;
property OnLoaded;
property OnDDDragEnter;
property OnDDDragLeave;
property OnDDDragOver;
property OnDDDrop;
property OnDDQueryContinueDrag;
property OnDDGiveFeedback;
property OnDDDragDetect;
property OnDDCreateDragFileList;
property OnDDEnd;
property OnDDCreateDataObject;
property OnDDTargetHasDropHandler;
{Drag&Drop:}
property DDLinkOnExeDrag default True;
property OnDDProcessDropped;
property OnDDError;
property OnDDExecuted;
property OnDDFileOperation;
property OnDDFileOperationExecuted;
property OnExecFile;
property OnMatchMask;
property OnGetOverlay;
property OnGetItemColor;
property CompressedColor: TColor
read FCompressedColor write SetCompressedColor default clBlue;
{Confirm deleting files}
property ConfirmDelete: Boolean
read FConfirmDelete write FConfirmDelete default True;
{Confirm overwriting files}
property ConfirmOverwrite: Boolean
read FConfirmOverwrite write fConfirmOverwrite default True;
{Reload the directory after only the interval:}
property ChangeInterval: Cardinal
read FChangeInterval write SetChangeInterval default MSecsPerSec;
{Fetch shell icons by thread:}
property UseIconUpdateThread: Boolean
read FUseIconUpdateThread write FUseIconUpdateThread default False;
{Enables or disables icon caching for registered file extensions. Caching enabled
enhances the performance but does not take care about installed icon handlers, wich
may modify the display icon for registered files. Only the iconindex is cached not the
icon itself:}
property UseIconCache: Boolean
read FUseIconCache write FUseIconCache default False;
{Watch current directory for filename changes (create, rename, delete files)}
property WatchForChanges;
{Additional events:}
property OnFileIconForName: TDirViewFileIconForName
read FOnFileIconForName write FOnFileIconForName;
property UseSystemContextMenu;
property OnContextPopup;
property OnHistoryChange;
property OnHistoryGo;
property OnPathChange;
property OnBusy;
property ColumnClick;
property MultiSelect;
property ReadOnly;
// The only way to make Items stored automatically and survive handle recreation.
// Though we should implement custom persisting to avoid publishing this
property Items;
end; {Type TDirView}
procedure Register;
{Returns True, if the specified extension matches one of the extensions in ExtList:}
function MatchesFileExt(Ext: string; const FileExtList: string): Boolean;
function DropLink(Item: PFDDListItem; TargetPath: string): Boolean;
function DropFiles(
DragDropFilesEx: TCustomizableDragDropFilesEx; Effect: Integer; FileOperator: TFileOperator; TargetPath: string;
RenameOnCollision: Boolean; IsRecycleBin: Boolean; ConfirmDelete: Boolean; ConfirmOverwrite: Boolean; Paste: Boolean;
Sender: TObject; OnDDFileOperation: TDDFileOperationEvent;
out SourcePath: string; out SourceIsDirectory: Boolean): Boolean;
var
LastClipBoardOperation: TClipBoardOperation;
LastIOResult: DWORD;
implementation
uses
DriveView, OperationWithTimeout,
PIDL, Forms, Dialogs,
ComObj,
ActiveX, ImgList,
ShellDialogs, IEDriveInfo,
FileChanges, Math, PasTools, StrUtils, Types, UITypes;
var
DaylightHack: Boolean;
procedure Register;
begin
RegisterComponents('DriveDir', [TDirView]);
end; {Register}
function CompareInfoCacheItems(I1, I2: Pointer): Integer;
begin
if PInfoCache(I1)^.FileExt < PInfoCache(I2)^.FileExt then Result := fLess
else
if PInfoCache(I1)^.FileExt > PInfoCache(I2)^.FileExt then Result := fGreater
else Result := fEqual;
end; {CompareInfoCacheItems}
function MatchesFileExt(Ext: string; const FileExtList: string): Boolean;
begin
Result := (Length(Ext) = 3) and (Pos(Ext, FileExtList) <> 0);
end; {MatchesFileExt}
function FileTimeToDateTime(FileTime: TFileTime): TDateTime;
var
SysTime: TSystemTime;
UniverzalSysTime: TSystemTime;
LocalFileTime: TFileTime;
begin
// duplicated in Common.cpp
// The 0xFFF... is sometime seen for invalid timestamps,
// it would cause failure in SystemTimeToDateTime below
if FileTime.dwLowDateTime = High(DWORD) then
begin
Result := MinDateTime;
end
else
begin
if not DaylightHack then
begin
FileTimeToSystemTime(FileTime, UniverzalSysTime);
SystemTimeToTzSpecificLocalTime(nil, UniverzalSysTime, SysTime);
end
else
begin
FileTimeToLocalFileTime(FileTime, LocalFileTime);
FileTimeToSystemTime(LocalFileTime, SysTime);
end;
Result := SystemTimeToDateTime(SysTime);
end;
end;
function SizeFromSRec(const SRec: SysUtils.TSearchRec): Int64;
begin
with SRec do
begin
// Hopefuly TSearchRec.FindData is available with all Windows versions
{if Size >= 0 then Result := Size
else}
{$WARNINGS OFF}
Result := Int64(FindData.nFileSizeHigh) shl 32 + FindData.nFileSizeLow;
{$WARNINGS ON}
end;
end;
function DropLink(Item: PFDDListItem; TargetPath: string): Boolean;
var
Drive: string;
SourcePath: string;
SourceFile: string;
begin
SourceFile := Item.Name;
if IsRootPath(SourceFile) then
begin
Drive := DriveInfo.GetDriveKey(SourceFile);
SourcePath := Copy(DriveInfo.Get(Drive).PrettyName, 4, 255) + ' (' + Drive + ')'
end
else
begin
SourcePath := ExtractFileName(SourceFile);
end;
Result :=
CreateFileShortCut(SourceFile,
IncludeTrailingBackslash(TargetPath) + ChangeFileExt(SourcePath, '.lnk'),
ExtractFileNameOnly(SourceFile));
end;
function DropFiles(
DragDropFilesEx: TCustomizableDragDropFilesEx; Effect: Integer; FileOperator: TFileOperator; TargetPath: string;
RenameOnCollision: Boolean; IsRecycleBin: Boolean; ConfirmDelete: Boolean; ConfirmOverwrite: Boolean; Paste: Boolean;
Sender: TObject; OnDDFileOperation: TDDFileOperationEvent;
out SourcePath: string; out SourceIsDirectory: Boolean): Boolean;
var
Index: Integer;
DoFileOperation: Boolean;
begin
SourcePath := '';
{Set the source filenames:}
for Index := 0 to DragDropFilesEx.FileList.Count - 1 do
begin
FileOperator.OperandFrom.Add(
TFDDListItem(DragDropFilesEx.FileList[Index]^).Name);
if DragDropFilesEx.FileNamesAreMapped then
FileOperator.OperandTo.Add(IncludeTrailingPathDelimiter(TargetPath) +
TFDDListItem(DragDropFilesEx.FileList[Index]^).MappedName);
if SourcePath = '' then
begin
if DirectoryExists(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name) then
begin
SourcePath := TFDDListItem(DragDropFilesEx.FileList[Index]^).Name;
SourceIsDirectory := True;
end
else
begin
SourcePath := ExtractFilePath(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name);
SourceIsDirectory := False;
end;
end;
end;
FileOperator.Flags := [foAllowUndo, foNoConfirmMkDir];
if RenameOnCollision then
begin
FileOperator.Flags := FileOperator.Flags + [foRenameOnCollision];
FileOperator.WantMappingHandle := True;
end
else FileOperator.WantMappingHandle := False;
{Set the target directory or the target filenames:}
if DragDropFilesEx.FileNamesAreMapped and (not IsRecycleBin) then
begin
FileOperator.Flags := FileOperator.Flags + [foMultiDestFiles];
end
else
begin
FileOperator.Flags := FileOperator.Flags - [foMultiDestFiles];
FileOperator.OperandTo.Clear;
FileOperator.OperandTo.Add(TargetPath);
end;
{if the target directory is the recycle bin, then delete the selected files:}
if IsRecycleBin then
begin
FileOperator.Operation := foDelete;
end
else
begin
case Effect of
DropEffect_Copy: FileOperator.Operation := foCopy;
DropEffect_Move: FileOperator.Operation := foMove;
end;
end;
if IsRecycleBin then
begin
if not ConfirmDelete then
FileOperator.Flags := FileOperator.Flags + [foNoConfirmation];
end
else
begin
if not ConfirmOverwrite then
FileOperator.Flags := FileOperator.Flags + [foNoConfirmation];
end;
DoFileOperation := True;
if Assigned(OnDDFileOperation) then
begin
OnDDFileOperation(Sender, Effect, SourcePath, TargetPath, False, DoFileOperation);
end;
Result := DoFileOperation and (FileOperator.OperandFrom.Count > 0);
if Result then
begin
FileOperator.Execute;
if DragDropFilesEx.FileNamesAreMapped then
FileOperator.ClearUndo;
end;
end;
function GetShellDisplayName(
const ShellFolder: IShellFolder; IDList: PItemIDList; Flags: DWORD; var Name: string): Boolean;
var
Str: TStrRet;
begin
Result := True;
Name := '';
if ShellFolder.GetDisplayNameOf(IDList, Flags, Str) = NOERROR then
begin
case Str.uType of
STRRET_WSTR: Name := WideCharToString(Str.pOleStr);
STRRET_OFFSET: Name := PChar(UINT(IDList) + Str.uOffset);
STRRET_CSTR: Name := string(Str.cStr);
else Result := False;
end;
end
else Result := False;
end; {GetShellDisplayName}
{ TIconUpdateThread }
constructor TIconUpdateThread.Create(Owner: TDirView);
begin
inherited Create(True);
FOwner := Owner;
FIndex := 0;
FNewIcons := False;
if (FOwner.ViewStyle = vsReport) or (FOwner.ViewStyle = vsList) then
FMaxIndex := FOwner.VisibleRowCount
else FMaxIndex := 0;
FOwner.FIUThreadFinished := False;
end; {TIconUpdateThread.Create}
procedure TIconUpdateThread.SetMaxIndex(Value: Integer);
var
Point: TPoint;
Item: TListItem;
begin
if Value <> MaxIndex then
begin
FNewIcons := True;
if Value < FMaxIndex then
begin
if Suspended then FIndex := Value
else
begin
Point.X := 0;
Point.X := 0;
Item := FOwner.GetNearestItem(Point, TSearchDirection(sdAbove));
if Assigned(Item) then FIndex := Item.Index
else FIndex := Value;
end;
end
else FMaxIndex := Value;
end;
end; {SetMaxIndex}
procedure TIconUpdateThread.SetIndex(Value: Integer);
var
PageSize: Integer;
begin
if Value <> Index then
begin
PageSize := FOwner.VisibleRowCount;
FIndex := Value;
FNewIcons := True;
if FOwner.ViewStyle = vsList then FMaxIndex := Value + 2 * PageSize
else FMaxIndex := Value + PageSize;
end;
end; {SetIndex}
procedure TIconUpdateThread.Execute;
var
FileInfo: TShFileInfo;
Count: Integer;
Eaten: ULONG;
ShAttr: ULONG;
FileIconForName: string;
ForceByName: Boolean;
begin
if Assigned(FOwner.TopItem) then FIndex := FOwner.TopItem.Index
else FIndex := 0;
FNewIcons := (FIndex > 0);
while not Terminated do
begin
if FIndex > FMaxIndex then Suspend;
Count := FOwner.Items.Count;
if not Terminated and ((FIndex >= Count) or (Count = 0)) then
Suspend;
InvalidItem := True;
if Terminated then Break;
Synchronize(DoFetchData);
if (not InvalidItem) and (not Terminated) and
CurrentItemData.IconEmpty then
begin
try
ForceByName := False;
FileIconForName := CurrentFilePath;
if Assigned(FOwner.FOnFileIconForName) then
begin
FOwner.FOnFileIconForName(FOwner, nil, FileIconForName);
ForceByName := (FileIconForName <> CurrentFilePath);
end;
if not Assigned(CurrentItemData.PIDL) then
begin
ShAttr := 0;
FOwner.FDesktopFolder.ParseDisplayName(FOwner.ParentForm.Handle, nil,
PChar(CurrentFilePath), Eaten, CurrentItemData.PIDL, ShAttr);
end;
if (not ForceByName) and Assigned(CurrentItemData.PIDL) then
shGetFileInfo(PChar(CurrentItemData.PIDL), 0, FileInfo, SizeOf(FileInfo),
SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES or SHGFI_SYSICONINDEX or SHGFI_PIDL)
else
shGetFileInfo(PChar(FileIconForName), 0, FileInfo, SizeOf(FileInfo),
SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES or SHGFI_SYSICONINDEX);
except
{Capture exceptions generated by the shell}
FSyncIcon := UnKnownFileIcon;
end;
if Terminated then
begin
FreePIDL(CurrentItemData.PIDL);
Break;
end;
FSyncIcon := FileInfo.iIcon;
if FSyncIcon <> CurrentItemData.ImageIndex then
FNewIcons := True;
if not Terminated then
Synchronize(DoUpdateIcon);
FreePIDL(CurrentItemData.PIDL);
end;
SetLength(CurrentFilePath, 0);
if CurrentIndex = FIndex then Inc(FIndex);
SetLength(CurrentFilePath, 0);
end;
end; {TIconUpdateThread.Execute}
procedure TIconUpdateThread.DoFetchData;
begin
CurrentIndex := fIndex;
if not Terminated and
(Pred(FOwner.Items.Count) >= CurrentIndex) and
Assigned(FOwner.Items[CurrentIndex]) and
Assigned(FOwner.Items[CurrentIndex].Data) then
begin
CurrentFilePath := FOwner.ItemFullFileName(FOwner.Items[CurrentIndex]);
CurrentItemData := PFileRec(FOwner.Items[CurrentIndex].Data)^;
InvalidItem := False;
end
else InvalidItem := True;
end; {TIconUpdateThread.DoFetchData}
procedure TIconUpdateThread.DoUpdateIcon;
var
LVI: TLVItem;
begin
if (FOwner.Items.Count > CurrentIndex) and
not fOwner.Loading and not Terminated and
Assigned(FOwner.Items[CurrentIndex]) and
Assigned(FOwner.Items[CurrentIndex].Data) then
with FOwner.Items[CurrentIndex] do
begin
if (FSyncIcon >= 0) and (PFileRec(Data)^.ImageIndex <> FSyncIcon) then
begin
with PFileRec(Data)^ do
ImageIndex := FSyncIcon;
{To avoid flickering of the display use Listview_SetItem
instead of using the property ImageIndex:}
LVI.mask := LVIF_IMAGE;
LVI.iItem := CurrentIndex;
LVI.iSubItem := 0;
LVI.iImage := I_IMAGECALLBACK;
if not Terminated then
ListView_SetItem(FOwner.Handle, LVI);
FNewIcons := True;
end;
PFileRec(Data)^.IconEmpty := False;
end;
end; {TIconUpdateThread.DoUpdateIcon}
procedure TIconUpdateThread.Terminate;
begin
FOwner.FIUThreadFinished := True;
inherited;
end; {TIconUpdateThread.Terminate}
{ TDirView }
constructor TDirView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FInfoCacheList := TListExt.Create(SizeOf(TInfoCache));
FDriveType := DRIVE_UNKNOWN;
FUseIconCache := False;
FConfirmDelete := True;
FCompressedColor := clBlue;
FParentFolder := nil;
FDesktopFolder := nil;
SelectNewFiles := False;
DragOnDriveIsMove := True;
FHiddenCount := 0;
FFilteredCount := 0;
FFileOperator := TFileOperator.Create(Self);
FFileOperator.ProgressTitle := coFileOperatorTitle;
FFileOperator.Flags := [foAllowUndo, foNoConfirmMkDir];
FDirOK := True;
FPath := '';
FDiscMonitor := nil;
{ChangeTimer: }
if FChangeInterval = 0 then FChangeInterval := MSecsPerSec;
FChangeTimer := TTimer.Create(Self);
FChangeTimer.Interval := FChangeInterval;
FChangeTimer.Enabled := False;
FChangeTimer.OnTimer := TimerOnTimer;
{Drag&drop:}
FConfirmOverwrite := True;
DDLinkOnExeDrag := True;
with DragDropFilesEx do
begin
SourceEffects := DragSourceEffects;
TargetEffects := [deCopy, deMove, deLink];
ShellExtensions.DragDropHandler := True;
ShellExtensions.DropHandler := True;
end;
FLastPath := nil;
end; {Create}
destructor TDirView.Destroy;
begin
if Assigned(PIDLRecycle) then FreePIDL(PIDLRecycle);
FLastPath.Free;
FInfoCacheList.Free;
FFileOperator.Free;
FChangeTimer.Free;
inherited Destroy;
FPath := '';
end; {Destroy}
procedure TDirView.WMDestroy(var Msg: TWMDestroy);
begin
Selected := nil;
ClearItems;
TerminateThreads;
inherited;
end; {WMDestroy}
procedure TDirView.CMRecreateWnd(var Message: TMessage);
begin
// see comment in TDirView.StopIconUpdateThread
if not (csRecreating in ControlState) then
begin
inherited;
end;
end;
procedure TDirView.TerminateThreads;
begin
StopIconUpdateThread;
StopWatchThread;
if Assigned(FDiscMonitor) then
begin
FDiscMonitor.Free;
FDiscMonitor := nil;
end;
end; {TerminateThreads}
function TDirView.GetHomeDirectory: string;
begin
if FHomeDirectory <> '' then Result := FHomeDirectory
else
begin
Result := UserDocumentDirectory;
// in rare case the CSIDL_PERSONAL cannot be resolved
if Result = '' then
begin
Result := DriveInfo.AnyValidPath;
end;
end;
end; { GetHomeDirectory }
function TDirView.GetIsRoot: Boolean;
begin
Result := IsRootPath(Path);
end;
function TDirView.GetPath: string;
begin
Result := FPath;
end;
procedure TDirView.PathChanged;
var
Expanded: string;
begin
inherited;
// make sure to use PathName as Path maybe just X: what
// ExpandFileName resolves to current working directory
// on the drive, not to root path
Expanded := ExpandFileName(PathName);
if not Assigned(FLastPath) then
begin
FLastPath := TDictionary<string, string>.Create;
end;
FLastPath.AddOrSetValue(DriveInfo.GetDriveKey(Expanded), Expanded);
end;
procedure TDirView.SetPath(Value: string);
begin
// do checks before passing directory to drive view, because
// it would truncate non-existing directory to first superior existing
Value := ReplaceStr(Value, '/', '\');
if not DirectoryExists(ApiPath(Value)) then
raise Exception.CreateFmt(SDirNotExists, [Value]);
if Assigned(FDriveView) and
(FDriveView.Directory <> Value) then
begin
FDriveView.Directory := Value;
end
else
if FPath <> Value then
try
while (Length(Value) > 0) and (Value[Length(Value)] = '\') do
SetLength(Value, Length(Value) - 1);
PathChanging(True);
FPath := Value;
Load(True);
finally
PathChanged;
end;
end;
procedure TDirView.SetLoadEnabled(Value: Boolean);
begin
if Value <> LoadEnabled then
begin
FLoadEnabled := Enabled;
if LoadEnabled and Dirty then
begin
if Items.Count > 100 then Reload2
else Reload(True);
end;
end;
end; {SetLoadEnabled}
procedure TDirView.SetCompressedColor(Value: TColor);
begin
if Value <> CompressedColor then
begin
FCompressedColor := Value;
Invalidate;
end;
end; {SetCompressedColor}
function TDirView.GetPathName: string;
begin
if IsRoot then Result := IncludeTrailingBackslash(Path)
else Result := Path;
end; {GetPathName}
function TDirView.GetFileRec(Index: Integer): PFileRec;
begin
if Index > Pred(Items.Count) then Result := nil
else Result := Items[index].Data;
end; {GetFileRec}
function TDirView.HiddenCount: Integer;
begin
Result := FHiddenCount;
end;
function TDirView.FilteredCount: Integer;
begin
Result := FFilteredCount;
end;
function TDirView.AddItem(SRec: SysUtils.TSearchRec): TListItem;
var