DirView.pas 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420
  1. unit DirView;
  2. {===============================================================
  3. Component TDirView / Version 2.6, January 2000
  4. ===============================================================
  5. Description:
  6. ============
  7. Displays files of a single directory as listview with shell
  8. icons. Complete drag&Drop support for files and directories.
  9. Author:
  10. =======
  11. (c) Ingo Eckel 1998, 1999
  12. Sodener Weg 38
  13. 65812 Bad Soden
  14. Germany
  15. Modifications (for WinSCP):
  16. ===========================
  17. (c) Martin Prikryl 2001- 2004
  18. V2.6:
  19. - Shows "shared"-symbol with directories
  20. - Delphi5 compatible
  21. For detailed documentation and history see TDirView.htm.
  22. ===============================================================}
  23. {Required compiler options for TDirView:}
  24. {$A+,B-,X+,H+,P+}
  25. interface
  26. {$WARN UNIT_PLATFORM OFF}
  27. {$WARN SYMBOL_PLATFORM OFF}
  28. uses
  29. Windows, ShlObj, ComCtrls, CompThread, CustomDirView,
  30. ExtCtrls, Graphics, FileOperator, DiscMon, Classes, DirViewColProperties,
  31. DragDrop, Messages, ListViewColProperties, CommCtrl, DragDropFilesEx,
  32. FileCtrl, SysUtils, BaseUtils, Controls, CustomDriveView, System.Generics.Collections, Winapi.ShellAPI;
  33. type
  34. TVolumeDisplayStyle = (doPrettyName, doDisplayName); {Diplaytext of drive node}
  35. const
  36. msThreadChangeDelay = 10; {TDiscMonitor: change delay}
  37. MaxWaitTimeOut = 10; {TFileDeleteThread: wait nn seconds for deleting files or directories}
  38. {$WARN SYMBOL_DEPRECATED OFF}
  39. FileAttr = SysUtils.faAnyFile and (not SysUtils.faVolumeID);
  40. {$WARN SYMBOL_DEPRECATED ON}
  41. SpecialExtensions = 'EXE,LNK,ICO,ANI,CUR,PIF,JOB,CPL';
  42. ExeExtension = 'EXE';
  43. type
  44. {Exceptions:}
  45. EIUThread = class(Exception);
  46. EDragDrop = class(Exception);
  47. EInvalidFileName = class(Exception);
  48. ERenameFileFailed = class(Exception);
  49. TClipboardOperation = (cboNone, cboCut, cboCopy);
  50. {Record for each file item:}
  51. PFileRec = ^TFileRec;
  52. TFileRec = record
  53. Empty: Boolean;
  54. IconEmpty: Boolean;
  55. IsDirectory: Boolean;
  56. IsRecycleBin: Boolean;
  57. IsParentDir: Boolean;
  58. FileName: string;
  59. Displayname: string;
  60. FileExt: string;
  61. TypeName: string;
  62. ImageIndex: Integer;
  63. Size: Int64;
  64. Attr: LongWord;
  65. FileTime: TFileTime;
  66. PIDL: PItemIDList; {Fully qualified PIDL}
  67. CalculatedSize: Int64;
  68. end;
  69. {Additional events:}
  70. type
  71. TDirViewFileSizeChanged = procedure(Sender: TObject; Item: TListItem) of object;
  72. TDirViewFileIconForName = procedure(Sender: TObject; var FileName: string) of object;
  73. type
  74. TDirView = class;
  75. { TIconUpdateThread (Fetch shell icons via thread) }
  76. TIconUpdateThread = class(TCompThread)
  77. private
  78. FOwner: TDirView;
  79. FIndex: Integer;
  80. FMaxIndex: Integer;
  81. FNewIcons: Boolean;
  82. FSyncIcon: Integer;
  83. CurrentIndex: Integer;
  84. CurrentFilePath: string;
  85. CurrentItemData: TFileRec;
  86. InvalidItem: Boolean;
  87. procedure SetIndex(Value: Integer);
  88. procedure SetMaxIndex(Value: Integer);
  89. protected
  90. constructor Create(Owner: TDirView);
  91. procedure DoFetchData;
  92. procedure DoUpdateIcon;
  93. procedure Execute; override;
  94. property Index: Integer read FIndex write SetIndex;
  95. property MaxIndex: Integer read FMaxIndex write SetMaxIndex;
  96. public
  97. procedure Terminate; override;
  98. end;
  99. { TDirView }
  100. TDirView = class(TCustomDirView)
  101. private
  102. FConfirmDelete: Boolean;
  103. FConfirmOverwrite: Boolean;
  104. FDriveView: TCustomDriveView;
  105. FChangeTimer: TTimer;
  106. FChangeInterval: Cardinal;
  107. FUseIconUpdateThread: Boolean;
  108. FIUThreadFinished: Boolean;
  109. FDriveType: Integer;
  110. FParentFolder: IShellFolder;
  111. FDesktopFolder: IShellFolder;
  112. FDirOK: Boolean;
  113. FPath: string;
  114. SelectNewFiles: Boolean;
  115. FHiddenCount: Integer;
  116. FFilteredCount: Integer;
  117. FNotRelative: Boolean;
  118. {shFileOperation-shell component TFileOperator:}
  119. FFileOperator: TFileOperator;
  120. {Additional thread components:}
  121. FIconUpdateThread: TIconUpdateThread;
  122. FDiscMonitor: TDiscMonitor;
  123. FHomeDirectory: string;
  124. {Additional events:}
  125. FOnFileIconForName: TDirViewFileIconForName;
  126. iRecycleFolder: iShellFolder;
  127. PIDLRecycle: PItemIDList;
  128. FLastPath: TDictionary<string, string>;
  129. FTimeoutShellIconRetrieval: Boolean;
  130. {Drag&Drop:}
  131. function GetDirColProperties: TDirViewColProperties;
  132. function GetHomeDirectory: string;
  133. {Drag&drop helper functions:}
  134. procedure SignalFileDelete(Sender: TObject; Files: TStringList);
  135. procedure PerformDragDropFileOperation(TargetPath: string; Effect: Integer;
  136. RenameOnCollision: Boolean; Paste: Boolean);
  137. procedure SetDirColProperties(Value: TDirViewColProperties);
  138. protected
  139. function NewColProperties: TCustomListViewColProperties; override;
  140. function SortAscendingByDefault(Index: Integer): Boolean; override;
  141. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  142. procedure Delete(Item: TListItem); override;
  143. procedure DDError(ErrorNo: TDDError);
  144. function GetCanUndoCopyMove: Boolean; virtual;
  145. {Shell namespace functions:}
  146. function GetShellFolder(Dir: string): iShellFolder;
  147. function GetDirOK: Boolean; override;
  148. procedure GetDisplayInfo(ListItem: TListItem; var DispInfo: TLVItem); override;
  149. procedure DDDragDetect(grfKeyState: Longint; DetectStart, Point: TPoint;
  150. DragStatus: TDragDetectStatus); override;
  151. procedure DDMenuPopup(Sender: TObject; AMenu: HMenu; DataObj: IDataObject;
  152. AMinCustCmd:integer; grfKeyState: Longint; pt: TPoint); override;
  153. procedure DDMenuDone(Sender: TObject; AMenu: HMenu); override;
  154. procedure DDDropHandlerSucceeded(Sender: TObject; grfKeyState: Longint;
  155. Point: TPoint; dwEffect: Longint); override;
  156. procedure DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer; PreferredEffect: Integer); override;
  157. function GetPathName: string; override;
  158. procedure SetChangeInterval(Value: Cardinal); virtual;
  159. procedure LoadFromRecycleBin(Dir: string); virtual;
  160. procedure SetLoadEnabled(Value: Boolean); override;
  161. function GetPath: string; override;
  162. procedure SetPath(Value: string); override;
  163. procedure PathChanged; override;
  164. procedure SetItemImageIndex(Item: TListItem; Index: Integer); override;
  165. procedure ChangeDetected(Sender: TObject; const Directory: string;
  166. var SubdirsChanged: Boolean);
  167. procedure ChangeInvalid(Sender: TObject; const Directory: string; const ErrorStr: string);
  168. procedure TimerOnTimer(Sender: TObject);
  169. procedure ResetItemImage(Index: Integer);
  170. procedure SetWatchForChanges(Value: Boolean); override;
  171. procedure AddParentDirItem;
  172. procedure AddToDragFileList(FileList: TFileList; Item: TListItem); override;
  173. function DragCompleteFileList: Boolean; override;
  174. procedure ExecuteFile(Item: TListItem); override;
  175. function GetIsRoot: Boolean; override;
  176. procedure InternalEdit(const HItem: TLVItem); override;
  177. function ItemColor(Item: TListItem): TColor; override;
  178. function ItemFileExt(Item: TListItem): string;
  179. function ItemFileNameOnly(Item: TListItem): string;
  180. function ItemImageIndex(Item: TListItem; Cache: Boolean): Integer; override;
  181. function ItemIsFile(Item: TListItem): Boolean; override;
  182. function ItemIsRecycleBin(Item: TListItem): Boolean; override;
  183. function ItemMatchesFilter(Item: TListItem; const Filter: TFileFilter): Boolean; override;
  184. function FileMatches(FileName: string; const SearchRec: TSearchRec): Boolean;
  185. function ItemOverlayIndexes(Item: TListItem): Word; override;
  186. procedure LoadFiles; override;
  187. procedure PerformItemDragDropOperation(Item: TListItem; Effect: Integer; Paste: Boolean); override;
  188. procedure SortItems; override;
  189. procedure StartFileDeleteThread;
  190. procedure WMDestroy(var Msg: TWMDestroy); message WM_DESTROY;
  191. procedure CMRecreateWnd(var Message: TMessage); message CM_RECREATEWND;
  192. procedure Load(DoFocusSomething: Boolean); override;
  193. procedure DoFetchIcon(
  194. FilePath: string; IsSpecialExt: Boolean; CanTimeout: Boolean; FileRec: PFileRec; var ImageIndex: Integer; var TypeName: string);
  195. function GetFileInfo(
  196. CanUsePIDL: Boolean; PIDL: PItemIDList; Path: string; CanTimeout: Boolean;
  197. dwFileAttributes: DWORD; var psfi: TSHFileInfoW; uFlags: UINT): DWORD_PTR;
  198. function DoCopyToClipboard(Focused: Boolean; Cut: Boolean; Operation: TClipBoardOperation): Boolean;
  199. function HiddenCount: Integer; override;
  200. function FilteredCount: Integer; override;
  201. public
  202. {Runtime, readonly properties:}
  203. property DriveType: Integer read FDriveType;
  204. {Linked component TDriveView:}
  205. property DriveView: TCustomDriveView read FDriveView write FDriveView;
  206. { required, otherwise AV generated, when dragging columns}
  207. property Columns stored False;
  208. property ParentFolder: IShellFolder read FParentFolder;
  209. {Drag&Drop runtime, readonly properties:}
  210. property CanUndoCopyMove: Boolean read GetCanUndoCopyMove;
  211. property DDFileOperator: TFileOperator read FFileOperator;
  212. {Drag&Drop fileoperation methods:}
  213. function UndoCopyMove: Boolean; dynamic;
  214. {Clipboard fileoperation methods (requires drag&drop enabled):}
  215. procedure EmptyClipboard; dynamic;
  216. function CopyToClipBoard(Focused: Boolean): Boolean; dynamic;
  217. function CutToClipBoard(Focused: Boolean): Boolean; dynamic;
  218. function PasteFromClipBoard(TargetPath: string = ''): Boolean; override;
  219. function DuplicateSelectedFiles: Boolean; dynamic;
  220. procedure DisplayPropertiesMenu; override;
  221. procedure DisplayContextMenu(Where: TPoint); override;
  222. procedure ExecuteParentDirectory; override;
  223. procedure ExecuteRootDirectory; override;
  224. function ItemIsDirectory(Item: TListItem): Boolean; override;
  225. function ItemFullFileName(Item: TListItem): string; override;
  226. function ItemIsParentDirectory(Item: TListItem): Boolean; override;
  227. function ItemFileName(Item: TListItem): string; override;
  228. function ItemFileSize(Item: TListItem): Int64; override;
  229. function ItemFileTime(Item: TListItem; var Precision: TDateTimePrecision): TDateTime; override;
  230. procedure SetItemCalculatedSize(Item: TListItem; ASize: Int64); override;
  231. procedure OpenFallbackPath(Value: string);
  232. {Thread handling: }
  233. procedure StartWatchThread;
  234. procedure StopWatchThread;
  235. function WatchThreadActive: Boolean;
  236. procedure StartIconUpdateThread;
  237. procedure StopIconUpdateThread;
  238. procedure TerminateThreads;
  239. {Create a new subdirectory:}
  240. procedure CreateDirectory(DirName: string); override;
  241. {Delete all selected files:}
  242. {Check, if file or files still exists:}
  243. procedure ValidateFile(Item: TListItem); overload;
  244. procedure ValidateFile(FileName:TFileName); overload;
  245. procedure ValidateSelectedFiles; dynamic;
  246. {Access the internal data-structures:}
  247. function AddItem(SRec: SysUtils.TSearchRec): TListItem; reintroduce;
  248. procedure GetDisplayData(Item: TListItem; FetchIcon: Boolean);
  249. function GetFileRec(Index: Integer): PFileRec;
  250. {Populate / repopulate the filelist:}
  251. procedure Reload(CacheIcons : Boolean); override;
  252. procedure Reload2;
  253. function GetAttrString(Attr: Integer): string; virtual;
  254. constructor Create(AOwner: TComponent); override;
  255. destructor Destroy; override;
  256. procedure ExecuteHomeDirectory; override;
  257. procedure ReloadDirectory; override;
  258. procedure ExecuteDrive(Drive: string);
  259. property HomeDirectory: string read GetHomeDirectory write FHomeDirectory;
  260. property TimeoutShellIconRetrieval: Boolean read FTimeoutShellIconRetrieval write FTimeoutShellIconRetrieval;
  261. published
  262. property DirColProperties: TDirViewColProperties read GetDirColProperties write SetDirColProperties;
  263. property PathLabel;
  264. property OnUpdateStatusBar;
  265. property DimmHiddenFiles;
  266. property ShowHiddenFiles;
  267. property WantUseDragImages;
  268. property TargetPopupMenu;
  269. property AddParentDir;
  270. property OnSelectItem;
  271. property OnStartLoading;
  272. property OnLoaded;
  273. property OnDDDragEnter;
  274. property OnDDDragLeave;
  275. property OnDDDragOver;
  276. property OnDDDrop;
  277. property OnDDQueryContinueDrag;
  278. property OnDDGiveFeedback;
  279. property OnDDDragDetect;
  280. property OnDDCreateDragFileList;
  281. property OnDDEnd;
  282. property OnDDCreateDataObject;
  283. property OnDDTargetHasDropHandler;
  284. {Drag&Drop:}
  285. property DDLinkOnExeDrag default True;
  286. property OnDDProcessDropped;
  287. property OnDDError;
  288. property OnDDExecuted;
  289. property OnDDFileOperation;
  290. property OnDDFileOperationExecuted;
  291. property OnExecFile;
  292. property OnMatchMask;
  293. property OnGetOverlay;
  294. property OnGetItemColor;
  295. {Confirm deleting files}
  296. property ConfirmDelete: Boolean
  297. read FConfirmDelete write FConfirmDelete default True;
  298. {Confirm overwriting files}
  299. property ConfirmOverwrite: Boolean
  300. read FConfirmOverwrite write fConfirmOverwrite default True;
  301. {Reload the directory after only the interval:}
  302. property ChangeInterval: Cardinal
  303. read FChangeInterval write SetChangeInterval default MSecsPerSec;
  304. {Fetch shell icons by thread:}
  305. property UseIconUpdateThread: Boolean
  306. read FUseIconUpdateThread write FUseIconUpdateThread default False;
  307. {Watch current directory for filename changes (create, rename, delete files)}
  308. property WatchForChanges;
  309. {Additional events:}
  310. property OnFileIconForName: TDirViewFileIconForName
  311. read FOnFileIconForName write FOnFileIconForName;
  312. property UseSystemContextMenu;
  313. property OnContextPopup;
  314. property OnHistoryChange;
  315. property OnHistoryGo;
  316. property OnPathChange;
  317. property OnBusy;
  318. property OnChangeFocus;
  319. property ColumnClick;
  320. property MultiSelect;
  321. property ReadOnly;
  322. // The only way to make Items stored automatically and survive handle recreation.
  323. // Though we should implement custom persisting to avoid publishing this
  324. property Items;
  325. end; {Type TDirView}
  326. procedure Register;
  327. {Returns True, if the specified extension matches one of the extensions in ExtList:}
  328. function MatchesFileExt(Ext: string; const FileExtList: string): Boolean;
  329. function DropLink(Item: PFDDListItem; TargetPath: string): Boolean;
  330. function DropFiles(
  331. DragDropFilesEx: TCustomizableDragDropFilesEx; Effect: Integer; FileOperator: TFileOperator; TargetPath: string;
  332. RenameOnCollision: Boolean; IsRecycleBin: Boolean; ConfirmDelete: Boolean; ConfirmOverwrite: Boolean; Paste: Boolean;
  333. Sender: TObject; OnDDFileOperation: TDDFileOperationEvent;
  334. out SourcePath: string; out SourceIsDirectory: Boolean): Boolean;
  335. procedure CheckCanOpenDirectory(Path: string);
  336. var
  337. LastClipBoardOperation: TClipBoardOperation;
  338. implementation
  339. uses
  340. DriveView, OperationWithTimeout,
  341. PIDL, Forms, Dialogs,
  342. ComObj,
  343. ActiveX, ImgList,
  344. ShellDialogs, IEDriveInfo,
  345. FileChanges, Math, PasTools, StrUtils, Types, UITypes;
  346. var
  347. DaylightHack: Boolean;
  348. procedure Register;
  349. begin
  350. RegisterComponents('DriveDir', [TDirView]);
  351. end; {Register}
  352. function MatchesFileExt(Ext: string; const FileExtList: string): Boolean;
  353. begin
  354. Result := (Length(Ext) = 3) and (Pos(Ext, FileExtList) <> 0);
  355. end; {MatchesFileExt}
  356. function FileTimeToDateTime(FileTime: TFileTime): TDateTime;
  357. var
  358. SysTime: TSystemTime;
  359. UniverzalSysTime: TSystemTime;
  360. LocalFileTime: TFileTime;
  361. begin
  362. // duplicated in Common.cpp
  363. // The 0xFFF... is sometime seen for invalid timestamps,
  364. // it would cause failure in SystemTimeToDateTime below
  365. if FileTime.dwLowDateTime = High(DWORD) then
  366. begin
  367. Result := MinDateTime;
  368. end
  369. else
  370. begin
  371. if not DaylightHack then
  372. begin
  373. FileTimeToSystemTime(FileTime, UniverzalSysTime);
  374. SystemTimeToTzSpecificLocalTime(nil, UniverzalSysTime, SysTime);
  375. end
  376. else
  377. begin
  378. FileTimeToLocalFileTime(FileTime, LocalFileTime);
  379. FileTimeToSystemTime(LocalFileTime, SysTime);
  380. end;
  381. Result := SystemTimeToDateTime(SysTime);
  382. end;
  383. end;
  384. function SizeFromSRec(const SRec: SysUtils.TSearchRec): Int64;
  385. begin
  386. with SRec do
  387. begin
  388. // Hopefuly TSearchRec.FindData is available with all Windows versions
  389. {if Size >= 0 then Result := Size
  390. else}
  391. {$WARNINGS OFF}
  392. Result := Int64(FindData.nFileSizeHigh) shl 32 + FindData.nFileSizeLow;
  393. {$WARNINGS ON}
  394. end;
  395. end;
  396. function DropLink(Item: PFDDListItem; TargetPath: string): Boolean;
  397. var
  398. Drive: string;
  399. SourcePath: string;
  400. SourceFile: string;
  401. begin
  402. SourceFile := Item.Name;
  403. if IsRootPath(SourceFile) then
  404. begin
  405. Drive := DriveInfo.GetDriveKey(SourceFile);
  406. SourcePath := Copy(DriveInfo.Get(Drive).PrettyName, 4, 255) + ' (' + Drive + ')'
  407. end
  408. else
  409. begin
  410. SourcePath := ExtractFileName(SourceFile);
  411. end;
  412. Result :=
  413. CreateFileShortCut(SourceFile,
  414. IncludeTrailingBackslash(TargetPath) + ChangeFileExt(SourcePath, '.lnk'),
  415. ExtractFileNameOnly(SourceFile));
  416. end;
  417. function DropFiles(
  418. DragDropFilesEx: TCustomizableDragDropFilesEx; Effect: Integer; FileOperator: TFileOperator; TargetPath: string;
  419. RenameOnCollision: Boolean; IsRecycleBin: Boolean; ConfirmDelete: Boolean; ConfirmOverwrite: Boolean; Paste: Boolean;
  420. Sender: TObject; OnDDFileOperation: TDDFileOperationEvent;
  421. out SourcePath: string; out SourceIsDirectory: Boolean): Boolean;
  422. var
  423. Index: Integer;
  424. DoFileOperation: Boolean;
  425. begin
  426. SourcePath := '';
  427. {Set the source filenames:}
  428. for Index := 0 to DragDropFilesEx.FileList.Count - 1 do
  429. begin
  430. FileOperator.OperandFrom.Add(
  431. TFDDListItem(DragDropFilesEx.FileList[Index]^).Name);
  432. if DragDropFilesEx.FileNamesAreMapped then
  433. FileOperator.OperandTo.Add(IncludeTrailingPathDelimiter(TargetPath) +
  434. TFDDListItem(DragDropFilesEx.FileList[Index]^).MappedName);
  435. if SourcePath = '' then
  436. begin
  437. if DirectoryExists(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name) then
  438. begin
  439. SourcePath := TFDDListItem(DragDropFilesEx.FileList[Index]^).Name;
  440. SourceIsDirectory := True;
  441. end
  442. else
  443. begin
  444. SourcePath := ExtractFilePath(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name);
  445. SourceIsDirectory := False;
  446. end;
  447. end;
  448. end;
  449. FileOperator.Flags := FileOperatorDefaultFlags;
  450. if RenameOnCollision then
  451. begin
  452. FileOperator.Flags := FileOperator.Flags + [foRenameOnCollision];
  453. FileOperator.WantMappingHandle := True;
  454. end
  455. else FileOperator.WantMappingHandle := False;
  456. {Set the target directory or the target filenames:}
  457. if DragDropFilesEx.FileNamesAreMapped and (not IsRecycleBin) then
  458. begin
  459. FileOperator.Flags := FileOperator.Flags + [foMultiDestFiles];
  460. end
  461. else
  462. begin
  463. FileOperator.Flags := FileOperator.Flags - [foMultiDestFiles];
  464. FileOperator.OperandTo.Clear;
  465. FileOperator.OperandTo.Add(TargetPath);
  466. end;
  467. {if the target directory is the recycle bin, then delete the selected files:}
  468. if IsRecycleBin then
  469. begin
  470. FileOperator.Operation := foDelete;
  471. end
  472. else
  473. begin
  474. case Effect of
  475. DROPEFFECT_COPY: FileOperator.Operation := foCopy;
  476. DROPEFFECT_MOVE: FileOperator.Operation := foMove;
  477. end;
  478. end;
  479. if IsRecycleBin then
  480. begin
  481. if not ConfirmDelete then
  482. FileOperator.Flags := FileOperator.Flags + [foNoConfirmation];
  483. end
  484. else
  485. begin
  486. if not ConfirmOverwrite then
  487. FileOperator.Flags := FileOperator.Flags + [foNoConfirmation];
  488. end;
  489. DoFileOperation := True;
  490. if Assigned(OnDDFileOperation) then
  491. begin
  492. OnDDFileOperation(Sender, Effect, SourcePath, TargetPath, False, DoFileOperation);
  493. end;
  494. Result := DoFileOperation and (FileOperator.OperandFrom.Count > 0);
  495. if Result then
  496. begin
  497. FileOperator.Execute;
  498. if DragDropFilesEx.FileNamesAreMapped then
  499. FileOperator.ClearUndo;
  500. end;
  501. end;
  502. function GetShellDisplayName(
  503. const ShellFolder: IShellFolder; IDList: PItemIDList; Flags: DWORD; var Name: string): Boolean;
  504. var
  505. Str: TStrRet;
  506. begin
  507. Result := True;
  508. Name := '';
  509. if ShellFolder.GetDisplayNameOf(IDList, Flags, Str) = NOERROR then
  510. begin
  511. case Str.uType of
  512. STRRET_WSTR: Name := WideCharToString(Str.pOleStr);
  513. STRRET_OFFSET: Name := PChar(UINT(IDList) + Str.uOffset);
  514. STRRET_CSTR: Name := string(Str.cStr);
  515. else Result := False;
  516. end;
  517. end
  518. else Result := False;
  519. end; {GetShellDisplayName}
  520. procedure CheckCanOpenDirectory(Path: string);
  521. var
  522. DosError: Integer;
  523. SRec: SysUtils.TSearchRec;
  524. begin
  525. if not DirectoryExistsFix(Path) then
  526. raise Exception.CreateFmt(SDirNotExists, [Path]);
  527. DosError := SysUtils.FindFirst(ApiPath(IncludeTrailingPathDelimiter(Path) + '*.*'), FileAttr, SRec);
  528. if DosError = ERROR_SUCCESS then
  529. begin
  530. FindClose(SRec);
  531. end
  532. else
  533. begin
  534. // File not found is expected when accessing a root folder of an empty drive
  535. if DosError <> ERROR_FILE_NOT_FOUND then
  536. begin
  537. RaiseLastOSError;
  538. end;
  539. end;
  540. end;
  541. { TIconUpdateThread }
  542. constructor TIconUpdateThread.Create(Owner: TDirView);
  543. begin
  544. inherited Create(True);
  545. FOwner := Owner;
  546. FIndex := 0;
  547. FNewIcons := False;
  548. if (FOwner.ViewStyle = vsReport) or (FOwner.ViewStyle = vsList) then
  549. FMaxIndex := FOwner.VisibleRowCount
  550. else FMaxIndex := 0;
  551. FOwner.FIUThreadFinished := False;
  552. end; {TIconUpdateThread.Create}
  553. procedure TIconUpdateThread.SetMaxIndex(Value: Integer);
  554. var
  555. Point: TPoint;
  556. Item: TListItem;
  557. begin
  558. if Value <> MaxIndex then
  559. begin
  560. FNewIcons := True;
  561. if Value < FMaxIndex then
  562. begin
  563. if Suspended then FIndex := Value
  564. else
  565. begin
  566. Point.X := 0;
  567. Point.X := 0;
  568. Item := FOwner.GetNearestItem(Point, TSearchDirection(sdAbove));
  569. if Assigned(Item) then FIndex := Item.Index
  570. else FIndex := Value;
  571. end;
  572. end
  573. else FMaxIndex := Value;
  574. end;
  575. end; {SetMaxIndex}
  576. procedure TIconUpdateThread.SetIndex(Value: Integer);
  577. var
  578. PageSize: Integer;
  579. begin
  580. if Value <> Index then
  581. begin
  582. PageSize := FOwner.VisibleRowCount;
  583. FIndex := Value;
  584. FNewIcons := True;
  585. if FOwner.ViewStyle = vsList then FMaxIndex := Value + 2 * PageSize
  586. else FMaxIndex := Value + PageSize;
  587. end;
  588. end; {SetIndex}
  589. procedure TIconUpdateThread.Execute;
  590. var
  591. Count: Integer;
  592. IsSpecialExt: Boolean;
  593. TypeName: string;
  594. begin
  595. CoInitialize(nil); // needed for SHGetFileInfo
  596. if Assigned(FOwner.TopItem) then FIndex := FOwner.TopItem.Index
  597. else FIndex := 0;
  598. FNewIcons := (FIndex > 0);
  599. while not Terminated do
  600. begin
  601. if FIndex > FMaxIndex then Suspend;
  602. Count := FOwner.Items.Count;
  603. if not Terminated and ((FIndex >= Count) or (Count = 0)) then
  604. Suspend;
  605. InvalidItem := True;
  606. if Terminated then Break;
  607. Synchronize(DoFetchData);
  608. if (not InvalidItem) and (not Terminated) and
  609. CurrentItemData.IconEmpty then
  610. begin
  611. try
  612. IsSpecialExt := MatchesFileExt(CurrentItemData.FileExt, SpecialExtensions);
  613. FOwner.DoFetchIcon(CurrentFilePath, IsSpecialExt, False, @CurrentItemData, FSyncIcon, TypeName);
  614. except
  615. {Capture exceptions generated by the shell}
  616. FSyncIcon := UnKnownFileIcon;
  617. end;
  618. if Terminated then
  619. begin
  620. FreePIDL(CurrentItemData.PIDL);
  621. Break;
  622. end;
  623. if FSyncIcon <> CurrentItemData.ImageIndex then
  624. FNewIcons := True;
  625. if not Terminated then
  626. begin
  627. Synchronize(DoUpdateIcon);
  628. end;
  629. FreePIDL(CurrentItemData.PIDL);
  630. end;
  631. SetLength(CurrentFilePath, 0);
  632. if CurrentIndex = FIndex then Inc(FIndex);
  633. SetLength(CurrentFilePath, 0);
  634. end;
  635. end; {TIconUpdateThread.Execute}
  636. procedure TIconUpdateThread.DoFetchData;
  637. begin
  638. CurrentIndex := fIndex;
  639. if not Terminated and
  640. (Pred(FOwner.Items.Count) >= CurrentIndex) and
  641. Assigned(FOwner.Items[CurrentIndex]) and
  642. Assigned(FOwner.Items[CurrentIndex].Data) then
  643. begin
  644. CurrentFilePath := FOwner.ItemFullFileName(FOwner.Items[CurrentIndex]);
  645. CurrentItemData := PFileRec(FOwner.Items[CurrentIndex].Data)^;
  646. InvalidItem := False;
  647. end
  648. else InvalidItem := True;
  649. end; {TIconUpdateThread.DoFetchData}
  650. procedure TIconUpdateThread.DoUpdateIcon;
  651. var
  652. LVI: TLVItem;
  653. begin
  654. if (FOwner.Items.Count > CurrentIndex) and
  655. not fOwner.Loading and not Terminated and
  656. Assigned(FOwner.Items[CurrentIndex]) and
  657. Assigned(FOwner.Items[CurrentIndex].Data) then
  658. with FOwner.Items[CurrentIndex] do
  659. begin
  660. if (FSyncIcon >= 0) and (PFileRec(Data)^.ImageIndex <> FSyncIcon) then
  661. begin
  662. with PFileRec(Data)^ do
  663. ImageIndex := FSyncIcon;
  664. {To avoid flickering of the display use Listview_SetItem
  665. instead of using the property ImageIndex:}
  666. LVI.mask := LVIF_IMAGE;
  667. LVI.iItem := CurrentIndex;
  668. LVI.iSubItem := 0;
  669. LVI.iImage := I_IMAGECALLBACK;
  670. if not Terminated then
  671. ListView_SetItem(FOwner.Handle, LVI);
  672. FNewIcons := True;
  673. end;
  674. PFileRec(Data)^.IconEmpty := False;
  675. end;
  676. end; {TIconUpdateThread.DoUpdateIcon}
  677. procedure TIconUpdateThread.Terminate;
  678. begin
  679. FOwner.FIUThreadFinished := True;
  680. inherited;
  681. end; {TIconUpdateThread.Terminate}
  682. { TDirView }
  683. constructor TDirView.Create(AOwner: TComponent);
  684. begin
  685. inherited Create(AOwner);
  686. FDriveType := DRIVE_UNKNOWN;
  687. FConfirmDelete := True;
  688. FParentFolder := nil;
  689. FDesktopFolder := nil;
  690. SelectNewFiles := False;
  691. DragOnDriveIsMove := True;
  692. FHiddenCount := 0;
  693. FFilteredCount := 0;
  694. FNotRelative := False;
  695. FFileOperator := TFileOperator.Create(Self);
  696. FDirOK := True;
  697. FPath := '';
  698. FDiscMonitor := nil;
  699. {ChangeTimer: }
  700. if FChangeInterval = 0 then FChangeInterval := MSecsPerSec;
  701. FChangeTimer := TTimer.Create(Self);
  702. FChangeTimer.Interval := FChangeInterval;
  703. FChangeTimer.Enabled := False;
  704. FChangeTimer.OnTimer := TimerOnTimer;
  705. {Drag&drop:}
  706. FConfirmOverwrite := True;
  707. DDLinkOnExeDrag := True;
  708. with DragDropFilesEx do
  709. begin
  710. SourceEffects := DragSourceEffects;
  711. TargetEffects := [deCopy, deMove, deLink];
  712. ShellExtensions.DragDropHandler := True;
  713. ShellExtensions.DropHandler := True;
  714. end;
  715. FLastPath := nil;
  716. end; {Create}
  717. destructor TDirView.Destroy;
  718. begin
  719. if Assigned(PIDLRecycle) then FreePIDL(PIDLRecycle);
  720. FLastPath.Free;
  721. FFileOperator.Free;
  722. FChangeTimer.Free;
  723. inherited Destroy;
  724. FPath := '';
  725. end; {Destroy}
  726. procedure TDirView.WMDestroy(var Msg: TWMDestroy);
  727. begin
  728. Selected := nil;
  729. ClearItems;
  730. TerminateThreads;
  731. inherited;
  732. end; {WMDestroy}
  733. procedure TDirView.CMRecreateWnd(var Message: TMessage);
  734. begin
  735. // see comment in TDirView.StopIconUpdateThread
  736. if not (csRecreating in ControlState) then
  737. begin
  738. inherited;
  739. end;
  740. end;
  741. procedure TDirView.TerminateThreads;
  742. begin
  743. StopIconUpdateThread;
  744. StopWatchThread;
  745. if Assigned(FDiscMonitor) then
  746. begin
  747. FDiscMonitor.Free;
  748. FDiscMonitor := nil;
  749. end;
  750. end; {TerminateThreads}
  751. function TDirView.GetHomeDirectory: string;
  752. begin
  753. if FHomeDirectory <> '' then Result := FHomeDirectory
  754. else
  755. begin
  756. Result := UserDocumentDirectory;
  757. // in rare case the CSIDL_PERSONAL cannot be resolved
  758. if Result = '' then
  759. begin
  760. Result := DriveInfo.AnyValidPath;
  761. end;
  762. end;
  763. end; { GetHomeDirectory }
  764. function TDirView.GetIsRoot: Boolean;
  765. begin
  766. Result := IsRootPath(Path);
  767. end;
  768. function TDirView.GetPath: string;
  769. begin
  770. Result := FPath;
  771. end;
  772. procedure TDirView.PathChanged;
  773. var
  774. Expanded: string;
  775. begin
  776. inherited;
  777. // make sure to use PathName as Path maybe just X: what
  778. // ExpandFileName resolves to current working directory
  779. // on the drive, not to root path
  780. Expanded := ExpandFileName(PathName);
  781. if not Assigned(FLastPath) then
  782. begin
  783. FLastPath := TDictionary<string, string>.Create;
  784. end;
  785. FLastPath.AddOrSetValue(DriveInfo.GetDriveKey(Expanded), Expanded);
  786. end;
  787. procedure TDirView.SetPath(Value: string);
  788. begin
  789. // do checks before passing directory to drive view, because
  790. // it would truncate non-existing directory to first superior existing
  791. Value := ReplaceStr(Value, '/', '\');
  792. CheckCanOpenDirectory(Value);
  793. if Assigned(FDriveView) and
  794. (FDriveView.Directory <> Value) then
  795. begin
  796. FDriveView.Directory := Value;
  797. end
  798. else
  799. if FPath <> Value then
  800. try
  801. while ExcludeTrailingPathDelimiter(Value) <> Value do
  802. begin
  803. Value := ExcludeTrailingPathDelimiter(Value);
  804. end;
  805. PathChanging(not FNotRelative);
  806. FPath := Value;
  807. Load(True);
  808. finally
  809. PathChanged;
  810. end;
  811. end;
  812. procedure TDirView.OpenFallbackPath(Value: string);
  813. var
  814. APath: string;
  815. begin
  816. while True do
  817. begin
  818. APath := ExtractFileDir(Value);
  819. if (APath = '') or (APath = Value) then
  820. begin
  821. Break;
  822. end
  823. else
  824. begin
  825. try
  826. Path := APath;
  827. Break;
  828. except
  829. Value := APath;
  830. end;
  831. end;
  832. end;
  833. end;
  834. procedure TDirView.SetLoadEnabled(Value: Boolean);
  835. begin
  836. if Value <> LoadEnabled then
  837. begin
  838. FLoadEnabled := Enabled;
  839. if LoadEnabled and Dirty then
  840. begin
  841. if Items.Count > 100 then Reload2
  842. else Reload(True);
  843. end;
  844. end;
  845. end; {SetLoadEnabled}
  846. function TDirView.GetPathName: string;
  847. begin
  848. if IsRoot then Result := IncludeTrailingBackslash(Path)
  849. else Result := Path;
  850. end; {GetPathName}
  851. function TDirView.GetFileRec(Index: Integer): PFileRec;
  852. begin
  853. if Index > Pred(Items.Count) then Result := nil
  854. else Result := Items[index].Data;
  855. end; {GetFileRec}
  856. function TDirView.HiddenCount: Integer;
  857. begin
  858. Result := FHiddenCount;
  859. end;
  860. function TDirView.FilteredCount: Integer;
  861. begin
  862. Result := FFilteredCount;
  863. end;
  864. function TDirView.AddItem(SRec: SysUtils.TSearchRec): TListItem;
  865. var
  866. PItem: PFileRec;
  867. Item: TListItem;
  868. begin
  869. Item := TListItem.Create(Items);
  870. New(PItem);
  871. with PItem^ do
  872. begin
  873. // must be set as soon as possible, at least before Caption is set,
  874. // because if come column is "autosized" setting Caption invokes some callbacks
  875. Item.Data := PItem;
  876. FileName := SRec.Name;
  877. FileExt := UpperCase(ExtractFileExt(Srec.Name));
  878. FileExt := Copy(FileExt, 2, Length(FileExt) - 1);
  879. DisplayName := FileName;
  880. {$WARNINGS OFF}
  881. Attr := SRec.FindData.dwFileAttributes;
  882. {$WARNINGS ON}
  883. IsParentDir := False;
  884. IsDirectory := ((Attr and SysUtils.faDirectory) <> 0);
  885. IsRecycleBin := IsDirectory and (Length(Path) = 2) and
  886. Bool(Attr and SysUtils.faSysFile) and
  887. ((UpperCase(FileName) = 'RECYCLED') or (UpperCase(FileName) = 'RECYCLER'));
  888. if not IsDirectory then Size := SizeFromSRec(SRec)
  889. else Size := -1;
  890. {$WARNINGS OFF}
  891. FileTime := SRec.FindData.ftLastWriteTime;
  892. {$WARNINGS ON}
  893. Empty := True;
  894. IconEmpty := True;
  895. if Size > 0 then Inc(FFilesSize, Size);
  896. PIDL := nil;
  897. CalculatedSize := -1;
  898. // Need to add before assigning to .Caption and .OverlayIndex,
  899. // as the setters these call back to owning view.
  900. // Assignment is redundant
  901. Item := Items.AddItem(Item);
  902. if not Self.IsRecycleBin then Item.Caption := SRec.Name;
  903. if FileExt = 'LNK' then Item.OverlayIndex := 1;
  904. end;
  905. if SelectNewFiles then Item.Selected := True;
  906. Result := Item;
  907. end; {AddItem}
  908. procedure TDirView.AddParentDirItem;
  909. var
  910. PItem: PFileRec;
  911. Item: TListItem;
  912. SRec: SysUtils.TSearchRec;
  913. begin
  914. FHasParentDir := True;
  915. Item := Items.Add;
  916. New(PItem);
  917. if FindFirst(ApiPath(FPath), faAnyFile, SRec) = 0 then
  918. FindClose(SRec);
  919. with PItem^ do
  920. begin
  921. Item.Data := PItem;
  922. FileName := '..';
  923. FileExt := '';
  924. DisplayName := '..';
  925. Attr := SRec.Attr;
  926. IsDirectory := True;
  927. IsRecycleBin := False;
  928. IsParentDir := True;
  929. Size := -1;
  930. CalculatedSize := -1;
  931. Item.Caption := '..';
  932. {$WARNINGS OFF}
  933. FileTime := SRec.FindData.ftLastWriteTime;
  934. {$WARNINGS ON}
  935. Empty := True;
  936. IconEmpty := False;
  937. PIDL := nil;
  938. ImageIndex := StdDirIcon;
  939. TypeName := SParentDir;
  940. Empty := False;
  941. end;
  942. end; {AddParentDirItem}
  943. procedure TDirView.LoadFromRecycleBin(Dir: string);
  944. var
  945. PIDLRecycleLocal: PItemIDList;
  946. PCurrList: PItemIDList;
  947. FQPIDL: PItemIDList;
  948. EnumList: IEnumIDList;
  949. Fetched: ULONG;
  950. SRec: SysUtils.TSearchRec;
  951. DisplayName: string;
  952. FullPath: string;
  953. NewItem: TListItem;
  954. FileRec: PFileRec;
  955. FileInfo: TSHFileInfo;
  956. DosError: Integer;
  957. begin
  958. if not Assigned(iRecycleFolder) then
  959. begin
  960. PIDLRecycleLocal := nil;
  961. try
  962. OLECheck(SHGetSpecialFolderLocation(Self.Handle,
  963. CSIDL_BITBUCKET, PIDLRecycleLocal));
  964. PIDLRecycle := PIDL_Concatenate(nil, PIDLRecycleLocal);
  965. if not SUCCEEDED(FDesktopFolder.BindToObject(PIDLRecycle, nil,
  966. IID_IShellFolder, Pointer(iRecycleFolder))) then Exit;
  967. finally
  968. if Assigned(PIDLRecycleLocal) then
  969. FreePIDL(PIDLRecycleLocal);
  970. end;
  971. end;
  972. FParentFolder := iRecycleFolder;
  973. if AddParentDir then AddParentDirItem;
  974. FHiddenCount := 0;
  975. FFilteredCount := 0;
  976. if SUCCEEDED(iRecycleFolder.EnumObjects(Self.Handle,
  977. SHCONTF_FOLDERS or SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN, EnumList)) then
  978. begin
  979. while (EnumList.Next(1, PCurrList, Fetched) = S_OK) and not AbortLoading do
  980. begin
  981. if Assigned(PCurrList) then
  982. try
  983. FQPIDL := PIDL_Concatenate(PIDLRecycle, PCurrList);
  984. {Physical filename:}
  985. SetLength(FullPath, MAX_PATH);
  986. if shGetPathFromIDList(FQPIDL, PChar(FullPath)) then
  987. SetLength(FullPath, StrLen(PChar(FullPath)));
  988. {Filesize, attributes and -date:}
  989. DosError := FindFirst(ApiPath(FullPath), faAnyFile, SRec);
  990. FindClose(Srec);
  991. SRec.Name := ExtractFilePath(FullPath) + SRec.Name;
  992. {Displayname:}
  993. GetShellDisplayName(iRecycleFolder, PCurrList, SHGDN_FORPARSING, DisplayName);
  994. if (DosError = 0) and
  995. (((SRec.Attr and faDirectory) <> 0) or
  996. FileMatches(DisplayName, SRec)) then
  997. begin
  998. {Filetype and icon:}
  999. GetFileInfo(True, FQPIDL, '', False, 0, FileInfo, SHGFI_TYPENAME or SHGFI_SYSICONINDEX);
  1000. NewItem := AddItem(Srec);
  1001. NewItem.Caption := DisplayName;
  1002. FileRec := NewItem.Data;
  1003. FileRec^.Empty := False;
  1004. FileRec^.IconEmpty := False;
  1005. FileRec^.DisplayName := DisplayName;
  1006. FileRec^.PIDL := FQPIDL;
  1007. FileRec^.TypeName := FileInfo.szTypeName;
  1008. if FileRec^.Typename = EmptyStr then
  1009. FileRec^.TypeName := Format(STextFileExt, [FileRec.FileExt]);
  1010. FileRec^.ImageIndex := FileInfo.iIcon;
  1011. end
  1012. else
  1013. begin
  1014. FreePIDL(FQPIDL);
  1015. end;
  1016. FreePIDL(PCurrList);
  1017. except
  1018. if Assigned(PCurrList) then
  1019. try
  1020. FreePIDL(PCurrList);
  1021. except
  1022. end;
  1023. end;
  1024. end; {While EnumList ...}
  1025. end;
  1026. end; {LoadFromRecycleBin}
  1027. function TDirView.GetShellFolder(Dir: string): iShellFolder;
  1028. var
  1029. Eaten: ULONG;
  1030. Attr: ULONG;
  1031. NewPIDL: PItemIDList;
  1032. begin
  1033. Result := nil;
  1034. if not Assigned(FDesktopFolder) then
  1035. SHGetDesktopFolder(FDesktopFolder);
  1036. if Assigned(FDesktopFolder) then
  1037. begin
  1038. Attr := 0;
  1039. if Succeeded(FDesktopFolder.ParseDisplayName(
  1040. ParentForm.Handle, nil, PChar(Dir), Eaten, NewPIDL, Attr)) then
  1041. begin
  1042. try
  1043. assert(Assigned(NewPIDL));
  1044. FDesktopFolder.BindToObject(NewPIDL, nil, IID_IShellFolder, Pointer(Result));
  1045. Assert(Assigned(Result));
  1046. finally
  1047. FreePIDL(NewPIDL);
  1048. end;
  1049. end;
  1050. end;
  1051. end; {GetShellFolder}
  1052. function TDirView.ItemIsDirectory(Item: TListItem): Boolean;
  1053. begin
  1054. Result :=
  1055. (Assigned(Item) and Assigned(Item.Data) and
  1056. PFileRec(Item.Data)^.IsDirectory);
  1057. end;
  1058. function TDirView.ItemIsFile(Item: TListItem): Boolean;
  1059. begin
  1060. Result :=
  1061. (Assigned(Item) and Assigned(Item.Data) and
  1062. (not PFileRec(Item.Data)^.IsParentDir));
  1063. end;
  1064. function TDirView.ItemIsParentDirectory(Item: TListItem): Boolean;
  1065. begin
  1066. Result :=
  1067. (Assigned(Item) and Assigned(Item.Data) and
  1068. PFileRec(Item.Data)^.IsParentDir);
  1069. end;
  1070. function TDirView.ItemIsRecycleBin(Item: TListItem): Boolean;
  1071. begin
  1072. Result := (Assigned(Item) and Assigned(Item.Data) and
  1073. PFileRec(Item.Data)^.IsRecycleBin);
  1074. end;
  1075. function TDirView.ItemMatchesFilter(Item: TListItem; const Filter: TFileFilter): Boolean;
  1076. var
  1077. FileRec: PFileRec;
  1078. begin
  1079. Assert(Assigned(Item) and Assigned(Item.Data));
  1080. FileRec := PFileRec(Item.Data);
  1081. Result :=
  1082. ((Filter.Masks = '') or
  1083. FileNameMatchesMasks(FileRec^.FileName, FileRec^.IsDirectory,
  1084. FileRec^.Size, FileTimeToDateTime(FileRec^.FileTime), Filter.Masks, False) or
  1085. (FileRec^.IsDirectory and Filter.Directories and
  1086. FileNameMatchesMasks(FileRec^.FileName, False,
  1087. FileRec^.Size, FileTimeToDateTime(FileRec^.FileTime), Filter.Masks, False)));
  1088. end;
  1089. function TDirView.FileMatches(FileName: string; const SearchRec: TSearchRec): Boolean;
  1090. var
  1091. Directory: Boolean;
  1092. FileSize: Int64;
  1093. begin
  1094. Result := (ShowHiddenFiles or ((SearchRec.Attr and SysUtils.faHidden) = 0));
  1095. if not Result then
  1096. begin
  1097. Inc(FHiddenCount);
  1098. end
  1099. else
  1100. if Mask <> '' then
  1101. begin
  1102. Directory := ((SearchRec.Attr and faDirectory) <> 0);
  1103. if Directory then FileSize := 0
  1104. else FileSize := SizeFromSRec(SearchRec);
  1105. Result :=
  1106. FileNameMatchesMasks(
  1107. FileName,
  1108. Directory,
  1109. FileSize,
  1110. FileTimeToDateTime(SearchRec.FindData.ftLastWriteTime),
  1111. Mask, True);
  1112. if not Result then
  1113. begin
  1114. Inc(FFilteredCount);
  1115. end;
  1116. end;
  1117. end;
  1118. function TDirView.ItemOverlayIndexes(Item: TListItem): Word;
  1119. begin
  1120. Result := inherited ItemOverlayIndexes(Item);
  1121. if Assigned(Item) and Assigned(Item.Data) then
  1122. begin
  1123. if PFileRec(Item.Data)^.IsParentDir then
  1124. Inc(Result, oiDirUp);
  1125. end;
  1126. end;
  1127. procedure TDirView.Load(DoFocusSomething: Boolean);
  1128. begin
  1129. try
  1130. StopIconUpdateThread;
  1131. StopWatchThread;
  1132. FChangeTimer.Enabled := False;
  1133. FChangeTimer.Interval := 0;
  1134. inherited;
  1135. finally
  1136. if DirOK and not AbortLoading then
  1137. begin
  1138. if FUseIconUpdateThread and (not IsRecycleBin) then
  1139. StartIconUpdateThread;
  1140. StartWatchThread;
  1141. end;
  1142. end;
  1143. end;
  1144. procedure TDirView.LoadFiles;
  1145. var
  1146. SRec: SysUtils.TSearchRec;
  1147. DosError: Integer;
  1148. DirsCount: Integer;
  1149. SelTreeNode: TTreeNode;
  1150. Node: TTreeNode;
  1151. Drive: string;
  1152. begin
  1153. FHiddenCount := 0;
  1154. FFilteredCount := 0;
  1155. try
  1156. if Length(FPath) > 0 then
  1157. begin
  1158. Drive := DriveInfo.GetDriveKey(FPath);
  1159. DriveInfo.ReadDriveStatus(Drive, dsSize);
  1160. FDriveType := DriveInfo.Get(Drive).DriveType;
  1161. FDirOK := DriveInfo.Get(Drive).DriveReady and DirectoryExists(FPath);
  1162. end
  1163. else
  1164. begin
  1165. FDriveType := DRIVE_UNKNOWN;
  1166. FDirOK := False;
  1167. end;
  1168. if DirOK then
  1169. begin
  1170. if Assigned(FDriveView) then
  1171. SelTreeNode := TDriveView(FDriveView).FindNodeToPath(FPath)
  1172. else SelTreeNode := nil;
  1173. if Assigned(FDriveView) and Assigned(SelTreeNode) then
  1174. FIsRecycleBin := TNodeData(SelTreeNode.Data).IsRecycleBin
  1175. else
  1176. FIsRecycleBin :=
  1177. (Uppercase(Copy(FPath, 2, 10)) = ':\RECYCLED') or
  1178. (Uppercase(Copy(FPath, 2, 10)) = ':\RECYCLER');
  1179. if not Assigned(FDesktopFolder) then
  1180. SHGetDesktopFolder(FDesktopFolder);
  1181. if IsRecycleBin then LoadFromRecycleBin(Path)
  1182. else
  1183. begin
  1184. FParentFolder := GetShellFolder(PathName);
  1185. DosError := SysUtils.FindFirst(ApiPath(IncludeTrailingPathDelimiter(FPath) + '*.*'),
  1186. FileAttr, SRec);
  1187. while (DosError = 0) and (not AbortLoading) do
  1188. begin
  1189. if (SRec.Attr and faDirectory) = 0 then
  1190. begin
  1191. if FileMatches(SRec.Name, SRec) then
  1192. begin
  1193. AddItem(SRec);
  1194. end;
  1195. end;
  1196. DosError := FindNext(SRec);
  1197. end;
  1198. SysUtils.FindClose(SRec);
  1199. if AddParentDir and (not IsRoot) then
  1200. begin
  1201. AddParentDirItem;
  1202. end;
  1203. {Search for directories:}
  1204. DirsCount := 0;
  1205. DosError := SysUtils.FindFirst(ApiPath(IncludeTrailingPathDelimiter(FPath) + '*.*'),
  1206. DirAttrMask, SRec);
  1207. while (DosError = 0) and (not AbortLoading) do
  1208. begin
  1209. if (SRec.Name <> '.') and (SRec.Name <> '..') and
  1210. ((Srec.Attr and faDirectory) <> 0) then
  1211. begin
  1212. Inc(DirsCount);
  1213. if FileMatches(SRec.Name, SRec) then
  1214. begin
  1215. AddItem(Srec);
  1216. end;
  1217. end;
  1218. DosError := FindNext(SRec);
  1219. end;
  1220. SysUtils.FindClose(SRec);
  1221. {Update TDriveView's subdir indicator:}
  1222. if Assigned(FDriveView) and (FDriveType = DRIVE_REMOTE) then
  1223. with TDriveView(FDriveView) do
  1224. begin
  1225. Node := FindNodeToPath(PathName);
  1226. if Assigned(Node) and Assigned(Node.Data) and
  1227. not TNodeData(Node.Data).Scanned then
  1228. begin
  1229. if DirsCount = 0 then
  1230. begin
  1231. Node.HasChildren := False;
  1232. TNodeData(Node.Data).Scanned := True;
  1233. end;
  1234. end;
  1235. end;
  1236. end; {not isRecycleBin}
  1237. end
  1238. else FIsRecycleBin := False;
  1239. finally
  1240. //if Assigned(Animate) then Animate.Free;
  1241. end; {Finally}
  1242. end;
  1243. procedure TDirView.Reload2;
  1244. type
  1245. PEFileRec = ^TEFileRec;
  1246. TEFileRec = record
  1247. iSize: Int64;
  1248. iAttr: Integer;
  1249. iFileTime: TFileTime;
  1250. iIndex: Integer;
  1251. end;
  1252. var
  1253. Index: Integer;
  1254. EItems: TStringList;
  1255. FItems: TStringList;
  1256. NewItems: TStringList;
  1257. Srec: SysUtils.TSearchRec;
  1258. DosError: Integer;
  1259. PSrec: ^SysUtils.TSearchRec;
  1260. Dummy: Integer;
  1261. ItemIndex: Integer;
  1262. AnyUpdate: Boolean;
  1263. PUpdate: Boolean;
  1264. PEFile: PEFileRec;
  1265. SaveCursor: TCursor;
  1266. FSize: Int64;
  1267. FocusedIsVisible: Boolean;
  1268. R: TRect;
  1269. begin
  1270. if (not Loading) and LoadEnabled then
  1271. begin
  1272. if IsRecycleBin then Reload(True)
  1273. else
  1274. begin
  1275. if not DirectoryExists(Path) then
  1276. begin
  1277. ClearItems;
  1278. FDirOK := False;
  1279. FDirty := False;
  1280. end
  1281. else
  1282. begin
  1283. if Assigned(ItemFocused) then
  1284. begin
  1285. R := ItemFocused.DisplayRect(drBounds);
  1286. // btw, we use vsReport only, nothing else was tested
  1287. Assert(ViewStyle = vsReport);
  1288. case ViewStyle of
  1289. vsReport:
  1290. FocusedIsVisible := (TopItem.Index <= ItemFocused.Index) and
  1291. (ItemFocused.Index < TopItem.Index + VisibleRowCount);
  1292. vsList:
  1293. // do not know how to implement that
  1294. FocusedIsVisible := False;
  1295. else // vsIcon and vsSmallIcon
  1296. FocusedIsVisible :=
  1297. IntersectRect(R,
  1298. Classes.Rect(ViewOrigin, Point(ViewOrigin.X + ClientWidth, ViewOrigin.Y + ClientHeight)),
  1299. ItemFocused.DisplayRect(drBounds));
  1300. end;
  1301. end
  1302. else FocusedIsVisible := False; // shut up
  1303. SaveCursor := Screen.Cursor;
  1304. Screen.Cursor := crHourGlass;
  1305. FChangeTimer.Enabled := False;
  1306. FChangeTimer.Interval := 0;
  1307. EItems := TStringlist.Create;
  1308. EItems.CaseSensitive := True; // We want to reflect changes in file name case
  1309. FItems := TStringlist.Create;
  1310. FItems.CaseSensitive := True;
  1311. NewItems := TStringlist.Create;
  1312. PUpdate := False;
  1313. AnyUpdate := False;
  1314. FHiddenCount := 0;
  1315. FFilteredCount := 0;
  1316. try
  1317. {Store existing files and directories:}
  1318. for Index := 0 to Items.Count - 1 do
  1319. begin
  1320. New(PEFile);
  1321. with PFileRec(Items[Index].Data)^ do
  1322. begin
  1323. PEFile^.iSize := Size;
  1324. PEFile^.iAttr := Attr;
  1325. PEFile^.iFileTime := FileTime;
  1326. PEFile^.iIndex := Index;
  1327. end;
  1328. EItems.AddObject(PFileRec(Items[Index].Data)^.FileName, Pointer(PEFile));
  1329. end;
  1330. EItems.Sort;
  1331. DosError := SysUtils.FindFirst(ApiPath(IncludeTrailingPathDelimiter(FPath) + '*.*'),
  1332. FileAttr, SRec);
  1333. while DosError = 0 do
  1334. begin
  1335. if (SRec.Attr and faDirectory) = 0 then
  1336. begin
  1337. if FileMatches(SRec.Name, SRec) then
  1338. begin
  1339. ItemIndex := -1;
  1340. if not EItems.Find(SRec.Name, ItemIndex) then
  1341. begin
  1342. New(PSrec);
  1343. PSRec^ := SRec;
  1344. NewItems.AddObject(SRec.Name, Pointer(PSrec));
  1345. FItems.Add(Srec.Name);
  1346. end
  1347. else
  1348. begin
  1349. FSize := SizeFromSRec(SRec);
  1350. with PEFileRec(EItems.Objects[ItemIndex])^ do
  1351. {$WARNINGS OFF}
  1352. if (iSize <> FSize) or (iAttr <> SRec.Attr) or
  1353. not CompareMem(@iFileTime, @SRec.FindData.ftLastWriteTime,
  1354. SizeOf(iFileTime)) Then
  1355. {$WARNINGS ON}
  1356. begin
  1357. with PFileRec(Items[iIndex].Data)^ do
  1358. begin
  1359. Dec(FFilesSize, Size);
  1360. Inc(FFilesSize, FSize);
  1361. if Items[iIndex].Selected then
  1362. begin
  1363. Dec(FFilesSelSize, Size);
  1364. Inc(FFilesSelSize, FSize);
  1365. end;
  1366. Size := FSize;
  1367. Attr := SRec.Attr;
  1368. {$WARNINGS OFF}
  1369. FileTime := SRec.FindData.ftLastWriteTime;
  1370. {$WARNINGS ON}
  1371. end;
  1372. // alternative to TListItem.Update (which causes flicker)
  1373. R := Items[iIndex].DisplayRect(drBounds);
  1374. InvalidateRect(Handle, @R, True);
  1375. AnyUpdate := True;
  1376. end;
  1377. FItems.Add(Srec.Name);
  1378. end;
  1379. end;
  1380. end;
  1381. DosError := FindNext(Srec);
  1382. end;
  1383. SysUtils.FindClose(Srec);
  1384. {Search new directories:}
  1385. DosError := SysUtils.FindFirst(ApiPath(FPath + '\*.*'), DirAttrMask, SRec);
  1386. while DosError = 0 do
  1387. begin
  1388. if (Srec.Attr and faDirectory) <> 0 then
  1389. begin
  1390. if (SRec.Name <> '.') and (SRec.Name <> '..') then
  1391. begin
  1392. if not EItems.Find(SRec.Name, ItemIndex) then
  1393. begin
  1394. if FileMatches(SRec.Name, SRec) then
  1395. begin
  1396. New(PSrec);
  1397. PSrec^ := SRec;
  1398. NewItems.AddObject(Srec.Name, Pointer(PSrec));
  1399. FItems.Add(SRec.Name);
  1400. end;
  1401. end
  1402. else
  1403. begin
  1404. FItems.Add(SRec.Name);
  1405. end;
  1406. end
  1407. else
  1408. begin
  1409. FItems.Add(SRec.Name);
  1410. end;
  1411. end;
  1412. DosError := FindNext(SRec);
  1413. end;
  1414. SysUtils.FindClose(SRec);
  1415. {Check wether displayed Items still exists:}
  1416. FItems.Sort;
  1417. for Index := Items.Count - 1 downto 0 do
  1418. begin
  1419. if not FItems.Find(PFileRec(Items[Index].Data)^.FileName, Dummy) then
  1420. begin
  1421. if not PUpdate then
  1422. begin
  1423. PUpdate := True;
  1424. Items.BeginUpdate;
  1425. end;
  1426. AnyUpdate := True;
  1427. with PFileRec(Items[Index].Data)^ do
  1428. begin
  1429. Dec(FFilesSize, Size);
  1430. // No need to decrease FFilesSelSize here as LVIF_STATE/deselect
  1431. // is called for item being deleted
  1432. end;
  1433. Items[Index].Delete;
  1434. end;
  1435. end;
  1436. finally
  1437. try
  1438. for Index := 0 to EItems.Count - 1 do
  1439. Dispose(PEFileRec(EItems.Objects[Index]));
  1440. EItems.Free;
  1441. FItems.Free;
  1442. for Index := 0 to NewItems.Count - 1 do
  1443. begin
  1444. if not PUpdate then
  1445. begin
  1446. PUpdate := True;
  1447. Items.BeginUpdate;
  1448. end;
  1449. AnyUpdate := True;
  1450. PSrec := Pointer(NewItems.Objects[Index]);
  1451. AddItem(PSrec^);
  1452. Dispose(PSrec);
  1453. end;
  1454. NewItems.Free;
  1455. // if we are sorted by name and there were only updates to existing
  1456. // items, there is no need for sorting
  1457. if PUpdate or
  1458. (AnyUpdate and (DirColProperties.SortDirColumn <> dvName)) then
  1459. begin
  1460. SortItems;
  1461. end;
  1462. if PUpdate then
  1463. Items.EndUpdate;
  1464. finally
  1465. FDirOK := True;
  1466. FDirty := false;
  1467. if FUseIconUpdateThread and (not FisRecycleBin) then
  1468. StartIconUpdateThread;
  1469. StartWatchThread;
  1470. // make focused item visible, only if it was before
  1471. if FocusedIsVisible and Assigned(ItemFocused) then
  1472. ItemFocused.MakeVisible(False);
  1473. DoUpdateStatusBar;
  1474. Screen.Cursor := SaveCursor;
  1475. end;
  1476. end; {Finally}
  1477. end;
  1478. if Assigned(FDriveView) then
  1479. begin
  1480. TDriveView(FDriveView).ValidateCurrentDirectoryIfNotMonitoring;
  1481. end;
  1482. end;
  1483. end;
  1484. end; {Reload2}
  1485. procedure TDirView.PerformItemDragDropOperation(Item: TListItem; Effect: Integer; Paste: Boolean);
  1486. var
  1487. TargetPath: string;
  1488. RenameOnCollision: Boolean;
  1489. begin
  1490. TargetPath := '';
  1491. RenameOnCollision := False;
  1492. if Assigned(Item) then
  1493. begin
  1494. if Assigned(Item.Data) then
  1495. begin
  1496. if ItemIsParentDirectory(Item) then
  1497. TargetPath := ExcludeTrailingPathDelimiter(ExtractFilePath(Path))
  1498. else
  1499. TargetPath := IncludeTrailingPathDelimiter(PathName) + ItemFileName(Item);
  1500. end;
  1501. end
  1502. else
  1503. begin
  1504. TargetPath := PathName;
  1505. RenameOnCollision := DDOwnerIsSource and (Effect = DROPEFFECT_COPY);
  1506. end;
  1507. if TargetPath <> '' then
  1508. PerformDragDropFileOperation(TargetPath, Effect, RenameOnCollision, Paste);
  1509. end;
  1510. procedure TDirView.ReLoad(CacheIcons: Boolean);
  1511. begin
  1512. if not FLoadEnabled then FDirty := True
  1513. else inherited;
  1514. end; {ReLoad}
  1515. function TDirView.GetAttrString(Attr: Integer): string;
  1516. const
  1517. Attrs: array[1..5] of Integer =
  1518. (FILE_ATTRIBUTE_COMPRESSED, FILE_ATTRIBUTE_ARCHIVE,
  1519. FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_HIDDEN,
  1520. FILE_ATTRIBUTE_READONLY);
  1521. AttrChars: array[1..5] of Char = ('c', 'a', 's', 'h', 'r');
  1522. var
  1523. Index: Integer;
  1524. LowBound: Integer;
  1525. begin
  1526. Result := '';
  1527. if Attr <> 0 then
  1528. begin
  1529. LowBound := Low(Attrs);
  1530. for Index := LowBound to High(Attrs) do
  1531. if (Attr and Attrs[Index] <> 0) then
  1532. Result := Result + AttrChars[Index]
  1533. else
  1534. Result := Result;
  1535. end;
  1536. end; {GetAttrString}
  1537. function TDirView.GetFileInfo(
  1538. CanUsePIDL: Boolean; PIDL: PItemIDList; Path: string; CanTimeout: Boolean;
  1539. dwFileAttributes: DWORD; var psfi: TSHFileInfoW; uFlags: UINT): DWORD_PTR;
  1540. var
  1541. pszPath: LPCWSTR;
  1542. cbFileInfo: UINT;
  1543. begin
  1544. cbFileInfo := SizeOf(psfi);
  1545. FillChar(psfi, cbFileInfo, #0);
  1546. if CanUsePIDL and Assigned(PIDL) then
  1547. begin
  1548. pszPath := PChar(PIDL);
  1549. uFlags := uFlags or SHGFI_PIDL;
  1550. end
  1551. else pszPath := PChar(Path);
  1552. // CanTimeout is False in scenarios, where we did not have any reports of hangs, to avoid thread overhead.
  1553. if TimeoutShellIconRetrieval and CanTimeout then
  1554. begin
  1555. Result := SHGetFileInfoWithTimeout(pszPath, dwFileAttributes, psfi, cbFileInfo, uFlags, MSecsPerSec div 4);
  1556. end
  1557. else
  1558. begin
  1559. Result := SHGetFileInfo(pszPath, dwFileAttributes, psfi, cbFileInfo, uFlags);
  1560. end;
  1561. if Result = 0 then
  1562. begin
  1563. psfi.szTypeName[0] := #0;
  1564. psfi.iIcon := 0;
  1565. end;
  1566. end;
  1567. procedure TDirView.DoFetchIcon(
  1568. FilePath: string; IsSpecialExt: Boolean; CanTimeout: Boolean; FileRec: PFileRec; var ImageIndex: Integer; var TypeName: string);
  1569. var
  1570. Eaten: ULONG;
  1571. shAttr: ULONG;
  1572. FileInfo: TShFileInfo;
  1573. ForceByName: Boolean;
  1574. FileIconForName: string;
  1575. begin
  1576. {Fetch the Item FQ-PIDL:}
  1577. if not Assigned(FileRec^.PIDL) and IsSpecialExt then
  1578. begin
  1579. try
  1580. ShAttr := 0;
  1581. FDesktopFolder.ParseDisplayName(
  1582. ParentForm.Handle, nil, PChar(FilePath), Eaten, FileRec^.PIDL, ShAttr);
  1583. except
  1584. end;
  1585. end;
  1586. if FileRec^.IsDirectory then
  1587. begin
  1588. if FDriveType = DRIVE_FIXED then
  1589. begin
  1590. try
  1591. {Retrieve icon and typename for the directory}
  1592. GetFileInfo(True, FileRec^.PIDL, FilePath, False, 0, FileInfo, SHGFI_TYPENAME or SHGFI_SYSICONINDEX);
  1593. if (FileInfo.iIcon <= 0) or (FileInfo.iIcon > SmallImages.Count) then
  1594. begin
  1595. {Invalid icon returned: retry with access file attribute flag:}
  1596. GetFileInfo(
  1597. False, nil, FilePath, False,
  1598. FILE_ATTRIBUTE_DIRECTORY, FileInfo, SHGFI_TYPENAME or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);
  1599. end;
  1600. TypeName := FileInfo.szTypeName;
  1601. ImageIndex := FileInfo.iIcon;
  1602. except
  1603. {Capture exceptions generated by the shell}
  1604. TypeName := StdDirTypeName;
  1605. ImageIndex := StdDirIcon;
  1606. end;
  1607. end
  1608. else
  1609. begin
  1610. TypeName := StdDirTypeName;
  1611. ImageIndex := StdDirIcon;
  1612. end;
  1613. end
  1614. else
  1615. begin
  1616. {Retrieve icon and typename for the file}
  1617. try
  1618. ForceByName := False;
  1619. FileIconForName := FilePath;
  1620. if Assigned(OnFileIconForName) then
  1621. begin
  1622. OnFileIconForName(Self, FileIconForName);
  1623. ForceByName := (FileIconForName <> FilePath);
  1624. end;
  1625. // Files with PIDL are typically .exe files.
  1626. // It may take long to retrieve an icon from exe file.
  1627. // We typically do not get here, now that we have UseIconUpdateThread enabled.
  1628. if GetFileInfo(
  1629. (not ForceByName), FileRec^.PIDL, FileIconForName, CanTimeout, FILE_ATTRIBUTE_NORMAL, FileInfo,
  1630. SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES or SHGFI_SYSICONINDEX) = 0 then
  1631. begin
  1632. if Assigned(FileRec^.PIDL) then
  1633. begin
  1634. FileInfo.iIcon := DefaultExeIcon;
  1635. end;
  1636. end;
  1637. TypeName := FileInfo.szTypeName;
  1638. ImageIndex := FileInfo.iIcon;
  1639. except
  1640. {Capture exceptions generated by the shell}
  1641. ImageIndex := UnKnownFileIcon;
  1642. end;
  1643. end;
  1644. end;
  1645. procedure TDirView.GetDisplayData(Item: TListItem; FetchIcon: Boolean);
  1646. var
  1647. FileInfo: TShFileInfo;
  1648. IsSpecialExt: Boolean;
  1649. FileRec: PFileRec;
  1650. FilePath: string;
  1651. FileAttributes: UINT;
  1652. begin
  1653. Assert(Assigned(Item) and Assigned(Item.Data));
  1654. FileRec := PFileRec(Item.Data);
  1655. with FileRec^ do
  1656. begin
  1657. IsSpecialExt := MatchesFileExt(FileExt, SpecialExtensions);
  1658. FetchIcon := IconEmpty and (FetchIcon or not IsSpecialExt);
  1659. if Empty or FetchIcon then
  1660. begin
  1661. FilePath := FPath + '\' + FileName;
  1662. if FetchIcon then
  1663. begin
  1664. DoFetchIcon(FilePath, IsSpecialExt, True, FileRec, ImageIndex, TypeName);
  1665. IconEmpty := False;
  1666. if Length(TypeName) = 0 then
  1667. TypeName := Format(STextFileExt, [FileExt]);
  1668. end
  1669. else
  1670. begin
  1671. try
  1672. if IsDirectory then FileAttributes := FILE_ATTRIBUTE_DIRECTORY
  1673. else FileAttributes := FILE_ATTRIBUTE_NORMAL;
  1674. GetFileInfo(
  1675. False, nil, FilePath, False, FileAttributes, FileInfo,
  1676. SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES);
  1677. TypeName := FileInfo.szTypeName;
  1678. except
  1679. {Capture exceptions generated by the shell}
  1680. TypeName := '';
  1681. end;
  1682. if IconEmpty then
  1683. begin
  1684. if FileExt = ExeExtension then ImageIndex := DefaultExeIcon
  1685. else ImageIndex := UnKnownFileIcon;
  1686. end;
  1687. end;
  1688. Empty := False;
  1689. end;
  1690. end;
  1691. end; {GetDisplayData}
  1692. function TDirView.GetDirOK: Boolean;
  1693. begin
  1694. Result := FDirOK;
  1695. end;
  1696. function TDirView.ItemFullFileName(Item: TListItem): string;
  1697. begin
  1698. if Assigned(Item) and Assigned(Item.Data) then
  1699. begin
  1700. if not IsRecycleBin then
  1701. begin
  1702. if PFileRec(Item.Data)^.IsParentDir then
  1703. begin
  1704. Result := ExcludeTrailingBackslash(ExtractFilePath(FPath));
  1705. end
  1706. else
  1707. begin
  1708. Result := FPath + '\' + PFileRec(Item.Data)^.FileName;
  1709. end;
  1710. end
  1711. else
  1712. Result := PFileRec(Item.Data)^.FileName;
  1713. end
  1714. else
  1715. Result := EmptyStr;
  1716. end; {ItemFullFileName}
  1717. function TDirView.ItemFileNameOnly(Item: TListItem): string;
  1718. begin
  1719. Assert(Assigned(Item) and Assigned(Item.Data));
  1720. Result := PFileRec(Item.Data)^.FileName;
  1721. SetLength(Result, Length(Result) - Length(ItemFileExt(Item)));
  1722. end; {ItemFileNameOnly}
  1723. function TDirView.ItemFileExt(Item: TListItem): string;
  1724. begin
  1725. Assert(Assigned(Item) and Assigned(Item.Data));
  1726. Result := ExtractFileExt(PFileRec(Item.Data)^.FileName);
  1727. end; {ItemFileExt}
  1728. function CompareFileType(I1, I2: TListItem; P1, P2: PFileRec): Integer;
  1729. var
  1730. Key1, Key2: string;
  1731. begin
  1732. if P1.Empty then TDirView(I1.ListView).GetDisplayData(I1, False);
  1733. if P2.Empty then TDirView(I2.ListView).GetDisplayData(I2, False);
  1734. if P1.IsDirectory then
  1735. begin
  1736. Key1 := P1.TypeName + ' ' + P1.DisplayName;
  1737. Key2 := P2.TypeName + ' ' + P2.DisplayName;
  1738. end
  1739. else
  1740. begin
  1741. Key1 := P1.TypeName + ' ' + P1.FileExt + ' ' + P1.DisplayName;
  1742. Key2 := P2.TypeName + ' ' + P2.FileExt + ' ' + P2.DisplayName;
  1743. end;
  1744. Result := CompareLogicalTextPas(Key1, Key2, TDirView(I1.ListView).NaturalOrderNumericalSorting);
  1745. end;
  1746. function CompareFileTime(P1, P2: PFileRec): Integer;
  1747. var
  1748. Time1, Time2: Int64;
  1749. begin
  1750. Time1 := Int64(P1.FileTime.dwHighDateTime) shl 32 + P1.FileTime.dwLowDateTime;
  1751. Time2 := Int64(P2.FileTime.dwHighDateTime) shl 32 + P2.FileTime.dwLowDateTime;
  1752. if Time1 < Time2 then Result := -1
  1753. else
  1754. if Time1 > Time2 then Result := 1
  1755. else Result := 0; // fallback
  1756. end;
  1757. function GetItemFileSize(P: PFileRec): Int64; inline;
  1758. begin
  1759. Result := 0;
  1760. if P.Size >= 0 then Result := P.Size
  1761. else
  1762. if P.CalculatedSize >= 0 then Result := P.CalculatedSize;
  1763. end;
  1764. function CompareFile(I1, I2: TListItem; AOwner: TDirView): Integer; stdcall;
  1765. var
  1766. ConsiderDirection: Boolean;
  1767. P1, P2: PFileRec;
  1768. begin
  1769. ConsiderDirection := True;
  1770. if I1 = I2 then Result := 0
  1771. else
  1772. if I1 = nil then Result := -1
  1773. else
  1774. if I2 = nil then Result := 1
  1775. else
  1776. begin
  1777. P1 := PFileRec(I1.Data);
  1778. P2 := PFileRec(I2.Data);
  1779. if P1.isParentDir then
  1780. begin
  1781. Result := -1;
  1782. ConsiderDirection := False;
  1783. end
  1784. else
  1785. if P2.isParentDir then
  1786. begin
  1787. Result := 1;
  1788. ConsiderDirection := False;
  1789. end
  1790. else
  1791. {Directories should always appear "grouped":}
  1792. if P1.isDirectory <> P2.isDirectory then
  1793. begin
  1794. if P1.isDirectory then
  1795. begin
  1796. Result := -1;
  1797. ConsiderDirection := False;
  1798. end
  1799. else
  1800. begin
  1801. Result := 1;
  1802. ConsiderDirection := False;
  1803. end;
  1804. end
  1805. else
  1806. begin
  1807. Result := 0;
  1808. if P1.isDirectory and AOwner.AlwaysSortDirectoriesByName then
  1809. begin
  1810. // fallback
  1811. end
  1812. else
  1813. begin
  1814. case AOwner.DirColProperties.SortDirColumn of
  1815. dvName:
  1816. ; // fallback
  1817. dvSize:
  1818. if GetItemFileSize(P1) < GetItemFileSize(P2) then Result := -1
  1819. else
  1820. if GetItemFileSize(P1) > GetItemFileSize(P2) then Result := 1
  1821. else ; // fallback
  1822. dvType:
  1823. Result := CompareFileType(I1, I2, P1, P2);
  1824. dvChanged:
  1825. Result := CompareFileTime(P1, P2);
  1826. dvAttr:
  1827. if P1.Attr < P2.Attr then Result := -1
  1828. else
  1829. if P1.Attr > P2.Attr then Result := 1
  1830. else ; // fallback
  1831. dvExt:
  1832. if not P1.isDirectory then
  1833. begin
  1834. Result := CompareLogicalTextPas(
  1835. P1.FileExt + ' ' + P1.DisplayName, P2.FileExt + ' ' + P2.DisplayName,
  1836. AOwner.NaturalOrderNumericalSorting);
  1837. end
  1838. else ; //fallback
  1839. else
  1840. ; // fallback
  1841. end;
  1842. end;
  1843. if Result = 0 then
  1844. begin
  1845. Result := CompareLogicalTextPas(P1.DisplayName, P2.DisplayName, AOwner.NaturalOrderNumericalSorting)
  1846. end;
  1847. end;
  1848. end;
  1849. if ConsiderDirection and (not AOwner.SortAscending) then
  1850. begin
  1851. Result := -Result;
  1852. end;
  1853. end;
  1854. procedure TDirView.SortItems;
  1855. begin
  1856. if HandleAllocated then
  1857. begin
  1858. StopIconUpdateThread;
  1859. try
  1860. CustomSortItems(@CompareFile);
  1861. finally
  1862. if (not Loading) and FUseIconUpdateThread then
  1863. StartIconUpdateThread;
  1864. end;
  1865. end
  1866. end;
  1867. procedure TDirView.ValidateFile(Item : TListItem);
  1868. var
  1869. Index: Integer;
  1870. begin
  1871. if Assigned(Item) and Assigned(Item.Data) then
  1872. begin
  1873. Index := Item.Index;
  1874. if not FileExists(ApiPath(ItemFullFileName(Items[Index]))) then
  1875. begin
  1876. Item.Delete;
  1877. end;
  1878. end;
  1879. end; {ValidateFile}
  1880. procedure TDirView.ValidateFile(FileName: TFileName);
  1881. var
  1882. FilePath: string;
  1883. begin
  1884. FilePath := ExcludeTrailingPathDelimiter(ExtractFilePath(FileName));
  1885. if IsRecycleBin then ValidateFile(FindFileItem(FileName))
  1886. else
  1887. if FilePath = Path then
  1888. ValidateFile(FindFileItem(ExtractFileName(FileName)));
  1889. end; {ValidateFile}
  1890. procedure TDirView.ValidateSelectedFiles;
  1891. var
  1892. FileList: TStrings;
  1893. i: Integer;
  1894. ToDelete: Boolean;
  1895. Updating: Boolean;
  1896. Updated: Boolean;
  1897. Item: TListItem;
  1898. begin
  1899. if SelCount > 50 then Reload2
  1900. else
  1901. begin
  1902. Updating := False;
  1903. Updated := False;
  1904. FileList := CustomCreateFileList(True, False, True, nil, True);
  1905. try
  1906. for i := 0 to FileList.Count - 1 do
  1907. begin
  1908. Item := TListItem(FileList.Objects[i]);
  1909. if ItemIsDirectory(Item) then
  1910. ToDelete := not DirectoryExists(ApiPath(FileList[i]))
  1911. else
  1912. ToDelete := not FileExists(ApiPath(FileList[i]));
  1913. if ToDelete then
  1914. begin
  1915. if (SelCount > 10) and (not Updating) then
  1916. begin
  1917. Items.BeginUpdate;
  1918. Updating := True;
  1919. end;
  1920. with PFileRec(Item.Data)^ do
  1921. begin
  1922. Dec(FFilesSize, Size);
  1923. // No need to decrease FFilesSelSize here as LVIF_STATE/deselect
  1924. // is called for item being deleted
  1925. end;
  1926. Item.Delete;
  1927. Updated := True;
  1928. end;
  1929. end;
  1930. finally
  1931. if Updating then
  1932. Items.EndUpdate;
  1933. if Updated then
  1934. DoUpdateStatusBar;
  1935. FileList.Free;
  1936. end;
  1937. end;
  1938. end; {ValidateSelectedFiles}
  1939. procedure TDirView.CreateDirectory(DirName: string);
  1940. var
  1941. SRec: SysUtils.TSearchRec;
  1942. Item: TListItem;
  1943. begin
  1944. // keep absolute path as is
  1945. if ExtractFileDrive(DirName) = '' then
  1946. DirName := Path + '\' + DirName;
  1947. if WatchForChanges then StopWatchThread;
  1948. if Assigned(FDriveView) then
  1949. TDriveView(FDriveView).StopWatchThread;
  1950. StopIconUpdateThread;
  1951. try
  1952. {create the physical directory:}
  1953. Win32Check(Windows.CreateDirectory(PChar(ApiPath(DirName)), nil));
  1954. if IncludeTrailingBackslash(ExtractFilePath(ExpandFileName(DirName))) =
  1955. IncludeTrailingBackslash(Path) then
  1956. begin
  1957. {Create the TListItem:}
  1958. if FindFirst(ApiPath(DirName), faAnyFile, SRec) = 0 then
  1959. begin
  1960. Item := AddItem(SRec);
  1961. ItemFocused := FindFileItem(GetFileRec(Item.Index)^.FileName);
  1962. SortItems;
  1963. if Assigned(ItemFocused) then
  1964. begin
  1965. ItemFocused.MakeVisible(False);
  1966. end;
  1967. end;
  1968. FindClose(SRec);
  1969. end;
  1970. finally
  1971. if FUseIconUpdateThread then
  1972. StartIconUpdateThread;
  1973. if WatchForChanges then StartWatchThread;
  1974. if Assigned(DriveView) then
  1975. with DriveView do
  1976. begin
  1977. if Assigned(Selected) then
  1978. ValidateDirectory(Selected);
  1979. TDriveView(FDriveView).StartWatchThread;
  1980. end;
  1981. end;
  1982. end; {CreateDirectory}
  1983. procedure TDirView.DisplayContextMenu(Where: TPoint);
  1984. var
  1985. FileList: TStringList;
  1986. Index: Integer;
  1987. Item: TListItem;
  1988. DefDir: string;
  1989. Verb: string;
  1990. PIDLArray: PPIDLArray;
  1991. Count: Integer;
  1992. DiffSelectedPath: Boolean;
  1993. WithEdit: Boolean;
  1994. PIDLRel: PItemIDList;
  1995. PIDLPath: PItemIDList;
  1996. Handled: Boolean;
  1997. begin
  1998. GetDir(0, DefDir);
  1999. ChDir(PathName);
  2000. Verb := EmptyStr;
  2001. StopWatchThread;
  2002. try
  2003. try
  2004. if Assigned(OnContextPopup) then
  2005. begin
  2006. Handled := False;
  2007. OnContextPopup(Self, ScreenToClient(Where), Handled);
  2008. if Handled then Abort;
  2009. end;
  2010. if (MarkedCount > 1) and
  2011. ((not Assigned(ItemFocused)) or ItemFocused.Selected) then
  2012. begin
  2013. if FIsRecycleBin then
  2014. begin
  2015. Count := 0;
  2016. GetMem(PIDLArray, SizeOf(PItemIDList) * SelCount);
  2017. try
  2018. FillChar(PIDLArray^, Sizeof(PItemIDList) * SelCount, #0);
  2019. for Index := Selected.Index to Items.Count - 1 do
  2020. if Items[Index].Selected then
  2021. begin
  2022. PIDL_GetRelative(PFileRec(Items[Index].Data)^.PIDL, PIDLPath, PIDLRel);
  2023. FreePIDL(PIDLPath);
  2024. PIDLArray^[Count] := PIDLRel;
  2025. Inc(Count);
  2026. end;
  2027. try
  2028. ShellDisplayContextMenu(ParentForm.Handle, Where, iRecycleFolder, Count,
  2029. PidlArray^[0], False, Verb, False);
  2030. finally
  2031. for Index := 0 to Count - 1 do
  2032. FreePIDL(PIDLArray[Index]);
  2033. end;
  2034. finally
  2035. FreeMem(PIDLArray, Count);
  2036. end;
  2037. end
  2038. else
  2039. begin
  2040. FileList := TStringList.Create;
  2041. CreateFileList(False, True, FileList);
  2042. for Index := 0 to FileList.Count - 1 do
  2043. FileList[Index] := ExtractFileName(FileList[Index]);
  2044. ShellDisplayContextMenu(ParentForm.Handle, Where, PathName,
  2045. FileList, Verb, False);
  2046. FileList.Destroy;
  2047. end;
  2048. {------------ Cut -----------}
  2049. if Verb = shcCut then
  2050. begin
  2051. LastClipBoardOperation := cboCut;
  2052. {Clear items previous marked as cut:}
  2053. Item := GetNextItem(nil, sdAll, [isCut]);
  2054. while Assigned(Item) do
  2055. begin
  2056. Item.Cut := False;
  2057. Item := GetNextItem(Item, sdAll, [isCut]);
  2058. end;
  2059. {Set property cut to TRUE for all selected items:}
  2060. Item := GetNextItem(nil, sdAll, [isSelected]);
  2061. while Assigned(Item) do
  2062. begin
  2063. Item.Cut := True;
  2064. Item := GetNextItem(Item, sdAll, [isSelected]);
  2065. end;
  2066. end
  2067. else
  2068. {----------- Copy -----------}
  2069. if Verb = shcCopy then LastClipBoardOperation := cboCopy
  2070. else
  2071. {----------- Paste ----------}
  2072. if Verb = shcPaste then
  2073. PasteFromClipBoard(ItemFullFileName(Selected))
  2074. else
  2075. if not FIsRecycleBin then Reload2;
  2076. end
  2077. else
  2078. if Assigned(ItemFocused) and Assigned(ItemFocused.Data) then
  2079. begin
  2080. Verb := EmptyStr;
  2081. WithEdit := not FisRecycleBin and CanEdit(ItemFocused);
  2082. LoadEnabled := True;
  2083. if FIsRecycleBin then
  2084. begin
  2085. PIDL_GetRelative(PFileRec(ItemFocused.Data)^.PIDL, PIDLPath, PIDLRel);
  2086. ShellDisplayContextMenu(ParentForm.Handle, Where,
  2087. iRecycleFolder, 1, PIDLRel, False, Verb, False);
  2088. FreePIDL(PIDLRel);
  2089. FreePIDL(PIDLPath);
  2090. end
  2091. else
  2092. begin
  2093. ShellDisplayContextMenu(ParentForm.Handle, Where,
  2094. ItemFullFileName(ItemFocused), WithEdit, Verb,
  2095. not PFileRec(ItemFocused.Data)^.isDirectory);
  2096. LoadEnabled := True;
  2097. end; {not FisRecycleBin}
  2098. {---------- Rename ----------}
  2099. if Verb = shcRename then ItemFocused.EditCaption
  2100. else
  2101. {------------ Cut -----------}
  2102. if Verb = shcCut then
  2103. begin
  2104. LastClipBoardOperation := cboCut;
  2105. Item := GetNextItem(nil, sdAll, [isCut]);
  2106. while Assigned(Item) do
  2107. begin
  2108. Item.Cut := False;
  2109. Item := GetNextItem(ITem, sdAll, [isCut]);
  2110. end;
  2111. ItemFocused.Cut := True;
  2112. end
  2113. else
  2114. {----------- Copy -----------}
  2115. if Verb = shcCopy then LastClipBoardOperation := cboCopy
  2116. else
  2117. {----------- Paste ----------}
  2118. if Verb = shcPaste then
  2119. begin
  2120. if PFileRec(ItemFocused.Data)^.IsDirectory then
  2121. PasteFromClipBoard(ItemFullFileName(ItemFocused));
  2122. end
  2123. else
  2124. if not FIsRecycleBin then Reload2;
  2125. end;
  2126. finally
  2127. ChDir(DefDir);
  2128. end;
  2129. if IsRecycleBin and (Verb <> shcCut) and (Verb <> shcProperties) and (SelCount > 0) then
  2130. begin
  2131. DiffSelectedPath := False;
  2132. for Index := Selected.Index to Items.Count - 1 do
  2133. if ExtractFilePath(PFileRec(Items[Index].Data)^.FileName) <> FPath + '\' then
  2134. begin
  2135. DiffSelectedPath := True;
  2136. Break;
  2137. end;
  2138. if DiffSelectedPath then
  2139. begin
  2140. StartFileDeleteThread;
  2141. Exit;
  2142. end;
  2143. end;
  2144. Sleep(250);
  2145. ValidateSelectedFiles;
  2146. finally
  2147. StartWatchThread;
  2148. end;
  2149. end;
  2150. procedure TDirView.GetDisplayInfo(ListItem: TListItem;
  2151. var DispInfo: TLVItem);
  2152. var
  2153. Value: string;
  2154. ASize: Int64;
  2155. begin
  2156. Assert(Assigned(ListItem) and Assigned(ListItem.Data));
  2157. with PFileRec(ListItem.Data)^, DispInfo do
  2158. begin
  2159. {Fetch display data of current file:}
  2160. if Empty then
  2161. GetDisplayData(ListItem, IconEmpty and
  2162. (not FUseIconUpdateThread or
  2163. (ViewStyle <> vsReport)));
  2164. if IconEmpty and
  2165. (not FUseIconUpdateThread or
  2166. (ViewStyle <> vsReport)) and
  2167. ((DispInfo.Mask and LVIF_IMAGE) <> 0) then
  2168. GetDisplayData(ListItem, True);
  2169. {Set IconUpdatethread :}
  2170. if IconEmpty and Assigned(FIconUpdateThread) then
  2171. begin
  2172. if Assigned(TopItem) then
  2173. {Viewstyle is vsReport or vsList:}
  2174. FIconUpdateThread.Index := Self.TopItem.Index
  2175. else
  2176. {Viewstyle is vsIcon or vsSmallIcon:}
  2177. FIconUpdateThread.MaxIndex := ListItem.Index;
  2178. if FIconUpdateThread.Suspended and not FIsRecycleBin then
  2179. FIconUpdateThread.Resume;
  2180. end;
  2181. if (DispInfo.Mask and LVIF_TEXT) <> 0 then
  2182. begin
  2183. Value := '';
  2184. if iSubItem = 0 then Value := DisplayName
  2185. else
  2186. if iSubItem < DirViewColumns then
  2187. begin
  2188. case TDirViewCol(iSubItem) of
  2189. dvSize: {Size: }
  2190. begin
  2191. if not IsDirectory then ASize := Size
  2192. else ASize := CalculatedSize;
  2193. if ASize >= 0 then Value := FormatPanelBytes(ASize, FormatSizeBytes);
  2194. end;
  2195. dvType: {FileType: }
  2196. Value := TypeName;
  2197. dvChanged: {Date}
  2198. // Keep consistent with UserModificationStr
  2199. Value := FormatDateTime('ddddd tt', FileTimeToDateTime(FileTime));
  2200. dvAttr: {Attrs:}
  2201. Value := GetAttrString(Attr);
  2202. dvExt:
  2203. Value := FileExt;
  2204. end {Case}
  2205. end; {SubItem}
  2206. StrPLCopy(pszText, Value, cchTextMax - 1);
  2207. end;
  2208. {Set display icon of current file:}
  2209. if (iSubItem = 0) and ((DispInfo.Mask and LVIF_IMAGE) <> 0) then
  2210. begin
  2211. iImage := PFileRec(ListItem.Data).ImageIndex;
  2212. Mask := Mask or LVIF_DI_SETITEM;
  2213. end;
  2214. end; {With PFileRec Do}
  2215. {Mask := Mask Or LVIF_DI_SETITEM; {<== causes flickering display and icons not to be updated on renaming the item}
  2216. end;
  2217. function TDirView.ItemColor(Item: TListItem): TColor;
  2218. begin
  2219. if PFileRec(Item.Data).Attr and FILE_ATTRIBUTE_COMPRESSED <> 0 then
  2220. begin
  2221. if SupportsDarkMode and DarkMode then Result := clSkyBlue
  2222. else Result := clBlue;
  2223. end
  2224. else
  2225. if DimmHiddenFiles and not Item.Selected and
  2226. (PFileRec(Item.Data).Attr and FILE_ATTRIBUTE_HIDDEN <> 0) then
  2227. Result := clGrayText
  2228. else
  2229. Result := clDefaultItemColor;
  2230. end;
  2231. procedure TDirView.StartFileDeleteThread;
  2232. var
  2233. Files: TStringList;
  2234. begin
  2235. Files := TStringList.Create;
  2236. try
  2237. CreateFileList(False, True, Files);
  2238. TFileDeleteThread.Create(Files, MaxWaitTimeOut, SignalFileDelete);
  2239. finally
  2240. Files.Free;
  2241. end;
  2242. end;
  2243. procedure TDirView.StartIconUpdateThread;
  2244. begin
  2245. if DirOK then
  2246. begin
  2247. if not Assigned(FIconUpdateThread) then
  2248. begin
  2249. if Items.Count > 0 then
  2250. FIconUpdateThread := TIconUpdateThread.Create(Self);
  2251. end
  2252. else
  2253. begin
  2254. Assert(not FIconUpdateThread.Terminated);
  2255. FIconUpdateThread.Index := 0;
  2256. if ViewStyle = vsReport then
  2257. FIconUpdateThread.Resume;
  2258. end;
  2259. end;
  2260. end; {StartIconUpdateThread}
  2261. procedure TDirView.StopIconUpdateThread;
  2262. begin
  2263. if Assigned(FIconUpdateThread) then
  2264. begin
  2265. FIconUpdateThread.Terminate;
  2266. FIconUpdateThread.Priority := tpHigher;
  2267. if FIconUpdateThread.Suspended then
  2268. FIconUpdateThread.Resume;
  2269. if not FIconUpdateThread.WaitFor(MSecsPerSec div 4) then
  2270. begin
  2271. // This prevents Destroy from waiting for (stalled) thread
  2272. FIconUpdateThread.Suspend;
  2273. end;
  2274. FIconUpdateThread.Destroy;
  2275. FIconUpdateThread := nil;
  2276. end;
  2277. end; {StopIconUpdateThread}
  2278. procedure TDirView.StopWatchThread;
  2279. begin
  2280. if Assigned(FDiscMonitor) then
  2281. begin
  2282. FDiscMonitor.Enabled := False;
  2283. end;
  2284. end; {StopWatchThread}
  2285. procedure TDirView.StartWatchThread;
  2286. begin
  2287. if (Length(Path) > 0) and WatchForChanges and DirOK then
  2288. begin
  2289. if not Assigned(FDiscMonitor) then
  2290. begin
  2291. FDiscMonitor := TDiscMonitor.Create(Self);
  2292. with FDiscMonitor do
  2293. begin
  2294. ChangeDelay := msThreadChangeDelay;
  2295. SubTree := False;
  2296. Filters := [moDirName, moFileName, moSize, moAttributes, moLastWrite];
  2297. SetDirectory(PathName);
  2298. OnChange := ChangeDetected;
  2299. OnInvalid := ChangeInvalid;
  2300. Open;
  2301. end;
  2302. end
  2303. else
  2304. begin
  2305. FDiscMonitor.SetDirectory(PathName);
  2306. FDiscMonitor.Enabled := True;
  2307. end;
  2308. end;
  2309. end; {StartWatchThread}
  2310. procedure TDirView.TimerOnTimer(Sender: TObject);
  2311. begin
  2312. if not Loading then
  2313. begin
  2314. // fix by MP: disable timer and reload directory before call to event
  2315. FChangeTimer.Enabled := False;
  2316. FChangeTimer.Interval := 0;
  2317. Reload2;
  2318. end;
  2319. end; {TimerOnTimer}
  2320. procedure TDirView.ChangeDetected(Sender: TObject; const Directory: string;
  2321. var SubdirsChanged: Boolean);
  2322. begin
  2323. // avoid prolonging the actual update with each change, as if continous change
  2324. // is occuring in current directory, the panel will never be updated
  2325. if not FChangeTimer.Enabled then
  2326. begin
  2327. FDirty := True;
  2328. FChangeTimer.Interval := FChangeInterval;
  2329. FChangeTimer.Enabled := True;
  2330. end;
  2331. end; {ChangeDetected}
  2332. procedure TDirView.ChangeInvalid(Sender: TObject; const Directory: string;
  2333. const ErrorStr: string);
  2334. begin
  2335. FDiscMonitor.Close;
  2336. end; {ChangeInvalid}
  2337. function TDirView.WatchThreadActive: Boolean;
  2338. begin
  2339. Result := WatchForChanges and Assigned(FDiscMonitor) and
  2340. FDiscMonitor.Active and FDiscMonitor.Enabled;
  2341. end; {WatchThreadActive}
  2342. procedure TDirView.SetChangeInterval(Value: Cardinal);
  2343. begin
  2344. if Value > 0 then
  2345. begin
  2346. FChangeInterval := Value;
  2347. FChangeTimer.Interval := Value;
  2348. end;
  2349. end; {SetChangeInterval}
  2350. procedure TDirView.SetDirColProperties(Value: TDirViewColProperties);
  2351. begin
  2352. if Value <> ColProperties then
  2353. ColProperties := Value;
  2354. end;
  2355. function TDirView.GetDirColProperties: TDirViewColProperties;
  2356. begin
  2357. Result := TDirViewColProperties(ColProperties);
  2358. end;
  2359. procedure TDirView.SetWatchForChanges(Value: Boolean);
  2360. begin
  2361. if WatchForChanges <> Value then
  2362. begin
  2363. FWatchForChanges := Value;
  2364. if not (csDesigning in ComponentState) then
  2365. begin
  2366. if Value then StartWatchThread
  2367. else StopWatchThread;
  2368. end;
  2369. end;
  2370. end; {SetWatchForChanges}
  2371. procedure TDirView.DisplayPropertiesMenu;
  2372. var
  2373. FileList: TStringList;
  2374. Index: Integer;
  2375. PIDLRel: PItemIDList;
  2376. PIDLPath: PItemIDList;
  2377. begin
  2378. if not Assigned(ItemFocused) then
  2379. ShellExecuteContextCommand(ParentForm.Handle, shcProperties, PathName)
  2380. else
  2381. if (not IsRecycleBin) and (MarkedCount > 1) and ItemFocused.Selected then
  2382. begin
  2383. FileList := TStringList.Create;
  2384. try
  2385. CreateFileList(False, True, FileList);
  2386. for Index := 0 to Pred(FileList.Count) do
  2387. FileList[Index] := ExtractFileName(FileList[Index]);
  2388. ShellExecuteContextCommand(ParentForm.Handle, shcProperties,
  2389. PathName, FileList);
  2390. finally
  2391. FileList.Free;
  2392. end;
  2393. end
  2394. else
  2395. if Assigned(ItemFocused.Data) then
  2396. begin
  2397. if IsRecycleBin then
  2398. begin
  2399. if Assigned(PFileRec(ItemFocused.Data)^.PIDL) then
  2400. begin
  2401. PIDL_GetRelative(PFileRec(ItemFocused.Data)^.PIDL, PIDLPath, PIDLRel);
  2402. ShellExecuteContextCommand(ParentForm.Handle, shcProperties, iRecycleFolder, 1, PIDLRel);
  2403. FreePIDL(PIDLRel);
  2404. FreePIDL(PIDLPath);
  2405. end;
  2406. end
  2407. else
  2408. ShellExecuteContextCommand(ParentForm.Handle, shcProperties,
  2409. ItemFullFileName(ItemFocused));
  2410. end;
  2411. end;
  2412. procedure TDirView.ExecuteFile(Item: TListItem);
  2413. var
  2414. DefDir: string;
  2415. FileName: string;
  2416. begin
  2417. if (UpperCase(PFileRec(Item.Data)^.FileExt) = 'LNK') or
  2418. PFileRec(Item.Data)^.IsDirectory then
  2419. begin
  2420. if PFileRec(Item.Data)^.IsDirectory then
  2421. begin
  2422. FileName := ItemFullFileName(Item);
  2423. if not DirectoryExistsFix(FileName) then
  2424. begin
  2425. Reload2;
  2426. if Assigned(FDriveView) and Assigned(FDriveView.Selected) then
  2427. with FDriveView do
  2428. ValidateDirectory(Selected);
  2429. Exit;
  2430. end;
  2431. end
  2432. else
  2433. FileName := ResolveFileShortCut(ItemFullFileName(Item), True);
  2434. if DirectoryExistsFix(FileName) then
  2435. begin
  2436. Path := FileName;
  2437. Exit;
  2438. end
  2439. else
  2440. if not FileExistsFix(ApiPath(FileName)) then
  2441. begin
  2442. Exit;
  2443. end;
  2444. end;
  2445. GetDir(0, DefDir);
  2446. ChDir(PathName);
  2447. try
  2448. ShellExecuteContextCommand(ParentForm.Handle, shcDefault,
  2449. ItemFullFileName(Item));
  2450. finally
  2451. ChDir(DefDir);
  2452. end;
  2453. end;
  2454. procedure TDirView.ExecuteDrive(Drive: string);
  2455. var
  2456. APath: string;
  2457. DriveRoot: string;
  2458. begin
  2459. if Assigned(FLastPath) and FLastPath.ContainsKey(Drive) then
  2460. begin
  2461. APath := FLastPath[Drive];
  2462. if not DirectoryExists(ApiPath(APath)) then
  2463. begin
  2464. if DriveInfo.IsRealDrive(Drive) then
  2465. APath := Format('%s:', [Drive])
  2466. else
  2467. APath := Drive;
  2468. end;
  2469. end
  2470. else
  2471. begin
  2472. if DriveInfo.IsRealDrive(Drive) then
  2473. begin
  2474. GetDir(Integer(Drive[1]) - Integer('A') + 1, APath);
  2475. DriveRoot := DriveInfo.GetDriveRoot(Drive);
  2476. // When the drive is not valid, the GetDir returns the current drive working directory, detect that,
  2477. // and let it fail later when trying to open root of the invalid drive.
  2478. if not StartsText(DriveRoot, APath) then
  2479. APath := DriveRoot;
  2480. APath := ExcludeTrailingPathDelimiter(APath);
  2481. end
  2482. else
  2483. begin
  2484. APath := Drive;
  2485. end;
  2486. end;
  2487. if Path <> APath then
  2488. Path := APath;
  2489. end;
  2490. procedure TDirView.ExecuteHomeDirectory;
  2491. begin
  2492. Path := HomeDirectory;
  2493. end;
  2494. procedure TDirView.ExecuteParentDirectory;
  2495. begin
  2496. if Valid then
  2497. begin
  2498. if Assigned(DriveView) and Assigned(DriveView.Selected) then
  2499. begin
  2500. DriveView.Selected := DriveView.Selected.Parent
  2501. end
  2502. else
  2503. begin
  2504. Path := ExtractFilePath(Path);
  2505. end;
  2506. end;
  2507. end;
  2508. procedure TDirView.ExecuteRootDirectory;
  2509. begin
  2510. if Valid then
  2511. begin
  2512. FNotRelative := True;
  2513. try
  2514. Path := ExtractFileDrive(Path);
  2515. finally
  2516. FNotRelative := False;
  2517. end;
  2518. end;
  2519. end;
  2520. procedure TDirView.Delete(Item: TListItem);
  2521. begin
  2522. if Assigned(Item) and Assigned(Item.Data) and not (csRecreating in ControlState) then
  2523. with PFileRec(Item.Data)^ do
  2524. begin
  2525. SetLength(FileName, 0);
  2526. SetLength(TypeName, 0);
  2527. SetLength(DisplayName, 0);
  2528. if Assigned(PIDL) then FreePIDL(PIDL);
  2529. Dispose(PFileRec(Item.Data));
  2530. Item.Data := nil;
  2531. end;
  2532. inherited Delete(Item);
  2533. end; {Delete}
  2534. procedure TDirView.InternalEdit(const HItem: TLVItem);
  2535. var
  2536. Item: TListItem;
  2537. Info: string;
  2538. NewCaption: string;
  2539. IsDirectory: Boolean;
  2540. begin
  2541. Item := GetItemFromHItem(HItem);
  2542. IsDirectory := DirectoryExists(ItemFullFileName(Item));
  2543. NewCaption := HItem.pszText;
  2544. StopWatchThread;
  2545. if IsDirectory and Assigned(FDriveView) then
  2546. TDriveView(FDriveView).StopWatchThread;
  2547. with FFileOperator do
  2548. begin
  2549. Flags := FileOperatorDefaultFlags + [foNoConfirmation];
  2550. Operation := foRename;
  2551. OperandFrom.Clear;
  2552. OperandTo.Clear;
  2553. OperandFrom.Add(ItemFullFileName(Item));
  2554. OperandTo.Add(FPath + '\' + HItem.pszText);
  2555. end;
  2556. try
  2557. if FFileOperator.Execute then
  2558. begin
  2559. if IsDirectory and Assigned(FDriveView) then
  2560. with FDriveView do
  2561. if Assigned(Selected) then
  2562. ValidateDirectory(Selected);
  2563. with GetFileRec(Item.Index)^ do
  2564. begin
  2565. Empty := True;
  2566. IconEmpty := True;
  2567. FileName := NewCaption;
  2568. DisplayName := FileName;
  2569. FileExt := UpperCase(ExtractFileExt(HItem.pszText));
  2570. FileExt := Copy(FileExt, 2, Length(FileExt) - 1);
  2571. TypeName := EmptyStr;
  2572. if Assigned(PIDL) then
  2573. FreePIDL(PIDL);
  2574. end;
  2575. GetDisplayData(Item, True);
  2576. ResetItemImage(Item.Index);
  2577. UpdateItems(Item.Index, Item.Index);
  2578. if Assigned(OnEdited) then OnEdited(Self, Item, NewCaption);
  2579. if Item <> nil then Item.Caption := NewCaption;
  2580. SortItems;
  2581. if Assigned(ItemFocused) then ItemFocused.MakeVisible(False);
  2582. end
  2583. else
  2584. begin
  2585. Item.Caption := GetFileRec(Item.Index)^.FileName;
  2586. Item.Update;
  2587. if FileOrDirExists(IncludeTrailingPathDelimiter(FPath) + HItem.pszText) then
  2588. Info := SErrorRenameFileExists + HItem.pszText
  2589. else
  2590. Info := SErrorRenameFile + HItem.pszText;
  2591. MessageBeep(MB_ICONHAND);
  2592. if MessageDlg(FormatLastOSError(Info), mtError, [mbOK, mbAbort], 0) = mrOK then
  2593. RetryRename(HItem.pszText);
  2594. end;
  2595. finally
  2596. Sleep(0);
  2597. LoadEnabled := True;
  2598. if FWatchForChanges and (not WatchThreadActive) then
  2599. StartWatchThread;
  2600. if Assigned(FDriveView) then
  2601. TDriveView(FDriveView).StartWatchThread;
  2602. end;
  2603. end;
  2604. function TDirView.ItemFileName(Item: TListItem): string;
  2605. begin
  2606. if Assigned(Item) and Assigned(Item.Data) then
  2607. Result := ExtractFileName(PFileRec(Item.Data)^.FileName)
  2608. else
  2609. Result := '';
  2610. end;
  2611. function TDirView.ItemFileSize(Item: TListItem): Int64;
  2612. begin
  2613. Result := 0;
  2614. if Assigned(Item) and Assigned(Item.Data) then
  2615. Result := GetItemFileSize(PFileRec(Item.Data));
  2616. end;
  2617. function TDirView.ItemFileTime(Item: TListItem;
  2618. var Precision: TDateTimePrecision): TDateTime;
  2619. begin
  2620. Result := FileTimeToDateTime(PFileRec(Item.Data)^.FileTime);
  2621. Precision := tpMillisecond;
  2622. end;
  2623. function TDirView.ItemImageIndex(Item: TListItem;
  2624. Cache: Boolean): Integer;
  2625. begin
  2626. if Assigned(Item) and Assigned(Item.Data) then
  2627. begin
  2628. if PFileRec(Item.Data)^.IconEmpty then
  2629. begin
  2630. if Cache then Result := -1
  2631. else Result := UnknownFileIcon;
  2632. end
  2633. else
  2634. begin
  2635. if (not Cache) or MatchesFileExt(PFileRec(Item.Data)^.FileExt, SpecialExtensions) then
  2636. Result := PFileRec(Item.Data)^.ImageIndex
  2637. else
  2638. Result := -1
  2639. end;
  2640. end
  2641. else Result := -1;
  2642. end;
  2643. procedure TDirView.Notification(AComponent: TComponent; Operation: TOperation);
  2644. begin
  2645. inherited Notification(AComponent, Operation);
  2646. if (Operation = opRemove) and (AComponent = FDriveView) then
  2647. FDriveView := nil;
  2648. end; {Notification}
  2649. procedure TDirView.ReloadDirectory;
  2650. begin
  2651. Reload(True);
  2652. end;
  2653. procedure TDirView.ResetItemImage(Index: Integer);
  2654. var
  2655. LVI: TLVItem;
  2656. begin
  2657. with PFileRec(Items[Index].Data)^, LVI do
  2658. begin
  2659. {Update imageindex:}
  2660. Mask := LVIF_STATE or LVIF_DI_SETITEM or LVIF_IMAGE;
  2661. iItem := Index;
  2662. iSubItem := 0;
  2663. if ListView_GetItem(Handle, LVI) then
  2664. begin
  2665. iImage := I_IMAGECALLBACK;
  2666. Mask := Mask and (not LVIF_DI_SETITEM);
  2667. ListView_SetItem(Handle, LVI);
  2668. end;
  2669. end; {With}
  2670. end; {ResetItemImage}
  2671. { Drag&Drop handling }
  2672. procedure TDirView.SignalFileDelete(Sender: TObject; Files: TStringList);
  2673. {Called by TFileDeleteThread, when a file was deleted by the Drag&Drop target window:}
  2674. var
  2675. Index: Integer;
  2676. begin
  2677. if Files.Count > 0 then
  2678. for Index := 0 to Files.Count - 1 do
  2679. ValidateFile(Files[Index]);
  2680. end;
  2681. procedure TDirView.DDMenuPopup(Sender: TObject; AMenu: HMenu; DataObj: IDataObject;
  2682. AMinCustCmd: Integer; grfKeyState: Longint; pt: TPoint);
  2683. begin
  2684. if Assigned(FDriveView) then
  2685. begin
  2686. // When a change is detected while menu is popped up
  2687. // it loses focus (or something similar)
  2688. // preventing it from handling subsequent click.
  2689. // This typically happens when right-dragging from remote to local panel,
  2690. // what causes temp directory being created+deleted.
  2691. // This is HACK, we should implement some uniform watch disabling/enabling
  2692. TDriveView(FDriveView).SuspendChangeTimer;
  2693. end;
  2694. inherited;
  2695. end;
  2696. procedure TDirView.DDMenuDone(Sender: TObject; AMenu: HMenu);
  2697. begin
  2698. if not WatchThreadActive then
  2699. begin
  2700. FChangeTimer.Interval := Min(FChangeInterval * 2, 3000);
  2701. FChangeTimer.Enabled := True;
  2702. end;
  2703. if Assigned(FDriveView) then
  2704. begin
  2705. TDriveView(FDriveView).ResumeChangeTimer;
  2706. end;
  2707. inherited;
  2708. end;
  2709. procedure TDirView.DDDropHandlerSucceeded(Sender: TObject; grfKeyState: Longint;
  2710. Point: TPoint; dwEffect: Longint);
  2711. begin
  2712. // Not sure why is this here. There's no "disable" counterparty.
  2713. if not WatchThreadActive then
  2714. begin
  2715. FChangeTimer.Interval := FChangeInterval;
  2716. FChangeTimer.Enabled := True;
  2717. end;
  2718. inherited;
  2719. end;
  2720. procedure TDirView.AddToDragFileList(FileList: TFileList; Item: TListItem);
  2721. begin
  2722. Assert(Assigned(Item));
  2723. if IsRecycleBin then
  2724. begin
  2725. if Assigned(Item.Data) then
  2726. begin
  2727. if UpperCase(ExtractFileExt(PFileRec(Item.Data)^.DisplayName)) =
  2728. ('.' + PFileRec(Item.Data)^.FileExt) then
  2729. FileList.AddItemEx(PFileRec(Item.Data)^.PIDL,
  2730. ItemFullFileName(Item), PFileRec(Item.Data)^.DisplayName)
  2731. else
  2732. FileList.AddItemEx(PFileRec(Item.Data)^.PIDL,
  2733. ItemFullFileName(Item), PFileRec(Item.Data)^.DisplayName +
  2734. ExtractFileExt(PFileRec(Item.Data)^.FileName));
  2735. end;
  2736. end
  2737. else inherited;
  2738. end;
  2739. procedure TDirView.DDDragDetect(grfKeyState: Longint; DetectStart, Point: TPoint;
  2740. DragStatus: TDragDetectStatus);
  2741. var
  2742. WasWatchThreadActive: Boolean;
  2743. begin
  2744. if (DragStatus = ddsDrag) and (MarkedCount > 0) then
  2745. begin
  2746. WasWatchThreadActive := WatchThreadActive;
  2747. inherited;
  2748. if (LastDDResult = drMove) and (not WasWatchThreadActive) then
  2749. StartFileDeleteThread;
  2750. end;
  2751. end; {DDDragDetect}
  2752. procedure TDirView.DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer; PreferredEffect: Integer);
  2753. begin
  2754. if DragDropFilesEx.OwnerIsSource and
  2755. (dwEffect = DROPEFFECT_COPY) and (not Assigned(DropTarget)) then
  2756. begin
  2757. dwEffect := DROPEFFECT_NONE
  2758. end
  2759. else
  2760. if (grfKeyState and (MK_CONTROL or MK_SHIFT) = 0) and (PreferredEffect = 0) then
  2761. begin
  2762. if FDragDrive <> '' then
  2763. begin
  2764. if ExeDrag and DriveInfo.IsFixedDrive(DriveInfo.GetDriveKey(Path)) and DriveInfo.IsFixedDrive(FDragDrive) then
  2765. begin
  2766. dwEffect := DROPEFFECT_LINK;
  2767. end
  2768. else
  2769. begin
  2770. if DragOnDriveIsMove and
  2771. (not DDOwnerIsSource or Assigned(DropTarget)) and
  2772. ((SameText(FDragDrive, DriveInfo.GetDriveKey(Path)) and (dwEffect = DROPEFFECT_COPY) and
  2773. (DragDropFilesEx.AvailableDropEffects and DROPEFFECT_MOVE <> 0))
  2774. or IsRecycleBin) then
  2775. begin
  2776. dwEffect := DROPEFFECT_MOVE;
  2777. end;
  2778. end;
  2779. end;
  2780. end;
  2781. inherited;
  2782. end;
  2783. procedure TDirView.PerformDragDropFileOperation(TargetPath: string;
  2784. Effect: Integer; RenameOnCollision: Boolean; Paste: Boolean);
  2785. var
  2786. Index: Integer;
  2787. SourcePath: string;
  2788. OldCursor: TCursor;
  2789. OldWatchForChanges: Boolean;
  2790. IsRecycleBin: Boolean;
  2791. SourceIsDirectory: Boolean;
  2792. Node: TTreeNode;
  2793. begin
  2794. if DragDropFilesEx.FileList.Count > 0 then
  2795. begin
  2796. if not DirectoryExists(TargetPath) then
  2797. begin
  2798. Reload(True);
  2799. DDError(DDPathNotFoundError);
  2800. end
  2801. else
  2802. begin
  2803. IsRecycleBin := Self.IsRecycleBin or
  2804. ((DropTarget <> nil) and ItemIsRecycleBin(DropTarget));
  2805. if not (DragDropFilesEx.FileNamesAreMapped and IsRecycleBin) then
  2806. begin
  2807. OldCursor := Screen.Cursor;
  2808. OldWatchForChanges := WatchForChanges;
  2809. SourceIsDirectory := True;
  2810. SourcePath := EmptyStr;
  2811. try
  2812. Screen.Cursor := crHourGlass;
  2813. WatchForChanges := False;
  2814. if Effect in [DROPEFFECT_COPY, DROPEFFECT_MOVE] then
  2815. begin
  2816. StopWatchThread;
  2817. if Assigned(DriveView) then
  2818. TDriveView(DriveView).StopWatchThread;
  2819. if (DropSourceControl <> Self) and
  2820. (DropSourceControl is TDirView) then
  2821. TDirView(DropSourceControl).StopWatchThread;
  2822. if DropFiles(
  2823. DragDropFilesEx, Effect, FFileOperator, TargetPath, RenameOnCollision, IsRecycleBin,
  2824. ConfirmDelete, ConfirmOverwrite, Paste,
  2825. Self, OnDDFileOperation, SourcePath, SourceIsDirectory) then
  2826. begin
  2827. ReLoad2;
  2828. if Assigned(OnDDFileOperationExecuted) then
  2829. OnDDFileOperationExecuted(Self, Effect, SourcePath, TargetPath);
  2830. end;
  2831. end
  2832. else
  2833. if Effect = DROPEFFECT_LINK then
  2834. (* Create Link requested: *)
  2835. begin
  2836. StopWatchThread;
  2837. for Index := 0 to DragDropFilesEx.FileList.Count - 1 do
  2838. begin
  2839. if not DropLink(PFDDListItem(DragDropFilesEx.FileList[Index]), TargetPath) then
  2840. begin
  2841. DDError(DDCreateShortCutError);
  2842. end;
  2843. end;
  2844. ReLoad2;
  2845. end;
  2846. if Assigned(DropSourceControl) and
  2847. (DropSourceControl is TDirView) and
  2848. (DropSourceControl <> Self) and
  2849. (Effect = DROPEFFECT_MOVE) then
  2850. begin
  2851. TDirView(DropSourceControl).ValidateSelectedFiles;
  2852. end;
  2853. if Assigned(FDriveView) and SourceIsDirectory then
  2854. begin
  2855. with TDriveView(FDriveView) do
  2856. begin
  2857. try
  2858. ValidateDirectory(FindNodeToPath(TargetPath));
  2859. except
  2860. end;
  2861. if (Effect = DROPEFFECT_MOVE) or IsRecycleBin then
  2862. try
  2863. Node := TryFindNodeToPath(SourcePath);
  2864. // If the path is not even in the tree, do not bother.
  2865. // This is particularly for dragging from remote folder, when the source path in %TEMP% and
  2866. // calling ValidateDirectory would load whole TEMP (and typically also "C:\Users")
  2867. if Assigned(Node) then
  2868. begin
  2869. if Assigned(Node.Parent) then
  2870. Node := Node.Parent;
  2871. ValidateDirectory(Node);
  2872. end;
  2873. except
  2874. end;
  2875. end;
  2876. end;
  2877. finally
  2878. FFileOperator.OperandFrom.Clear;
  2879. FFileOperator.OperandTo.Clear;
  2880. if Assigned(FDriveView) then
  2881. TDriveView(FDriveView).StartWatchThread;
  2882. Sleep(0);
  2883. WatchForChanges := OldWatchForChanges;
  2884. if (DropSourceControl <> Self) and (DropSourceControl is TDirView) then
  2885. TDirView(DropSourceControl).StartWatchThread;
  2886. Screen.Cursor := OldCursor;
  2887. end;
  2888. end;
  2889. end;
  2890. end;
  2891. end; {PerformDragDropFileOperation}
  2892. procedure TDirView.DDError(ErrorNo: TDDError);
  2893. begin
  2894. if Assigned(OnDDError) then OnDDError(Self, ErrorNo)
  2895. else
  2896. raise EDragDrop.Create(Format(SDragDropError, [Ord(ErrorNo)]));
  2897. end; {DDError}
  2898. function TDirView.GetCanUndoCopyMove: Boolean;
  2899. begin
  2900. Result := Assigned(FFileOperator) and FFileOperator.CanUndo;
  2901. end; {CanUndoCopyMove}
  2902. function TDirView.UndoCopyMove : Boolean;
  2903. var
  2904. LastTarget: string;
  2905. LastSource: string;
  2906. begin
  2907. Result := False;
  2908. if FFileOperator.CanUndo then
  2909. begin
  2910. Lasttarget := FFileOperator.LastOperandTo[0];
  2911. LastSource := FFileOperator.LastOperandFrom[0];
  2912. if Assigned(FDriveView) then
  2913. TDriveView(FDriveView).StopAllWatchThreads;
  2914. Result := FFileOperator.UndoExecute;
  2915. if not WatchthreadActive then
  2916. Reload2;
  2917. if Assigned(FDriveView) then
  2918. with TDriveView(FDriveView) do
  2919. begin
  2920. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastTarget)));
  2921. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastSource)));
  2922. StartAllWatchThreads;
  2923. end;
  2924. end;
  2925. end; {UndoCopyMove}
  2926. procedure TDirView.EmptyClipboard;
  2927. var
  2928. Item: TListItem;
  2929. begin
  2930. if Windows.OpenClipBoard(0) then
  2931. begin
  2932. Windows.EmptyClipBoard;
  2933. Windows.CloseClipBoard;
  2934. if LastClipBoardOperation <> cboNone then
  2935. begin
  2936. Item := GetNextItem(nil, sdAll, [isCut]);
  2937. while Assigned(Item) do
  2938. begin
  2939. Item.Cut := False;
  2940. Item := GetNextItem(Item, sdAll, [isCut]);
  2941. end;
  2942. end;
  2943. LastClipBoardOperation := cboNone;
  2944. if Assigned(FDriveView) then
  2945. TDriveView(FDriveView).LastPathCut := '';
  2946. end;
  2947. end; {EmptyClipBoard}
  2948. function TDirView.DoCopyToClipboard(Focused: Boolean; Cut: Boolean; Operation: TClipBoardOperation): Boolean;
  2949. var
  2950. Item: TListItem;
  2951. SaveCursor: TCursor;
  2952. begin
  2953. SaveCursor := Screen.Cursor;
  2954. Screen.Cursor := crHourGlass;
  2955. try
  2956. Result := False;
  2957. EmptyClipBoard;
  2958. DragDropFilesEx.FileList.Clear;
  2959. if OperateOnFocusedFile(Focused) or (SelCount > 0) then
  2960. begin
  2961. if OperateOnFocusedFile(Focused) then
  2962. begin
  2963. DragDropFilesEx.FileList.AddItem(nil, ItemFullFileName(ItemFocused));
  2964. end
  2965. else
  2966. begin
  2967. Item := GetNextItem(nil, sdAll, [isSelected]);
  2968. while Assigned(Item) do
  2969. begin
  2970. DragDropFilesEx.FileList.AddItem(nil, ItemFullFileName(Item));
  2971. Item.Cut := Cut;
  2972. Item := GetNextItem(Item, sdAll, [isSelected]);
  2973. end;
  2974. end;
  2975. Result := DragDropFilesEx.CopyToClipBoard;
  2976. LastClipBoardOperation := Operation;
  2977. end;
  2978. finally
  2979. Screen.Cursor := SaveCursor;
  2980. end;
  2981. end; {DoCopyToClipBoard}
  2982. function TDirView.CopyToClipBoard(Focused: Boolean): Boolean;
  2983. begin
  2984. Result := DoCopyToClipboard(Focused, False, cboCopy);
  2985. end;
  2986. function TDirView.CutToClipBoard(Focused: Boolean): Boolean;
  2987. begin
  2988. Result := DoCopyToClipboard(Focused, True, cboCut);
  2989. end;
  2990. function TDirView.PasteFromClipBoard(TargetPath: string): Boolean;
  2991. begin
  2992. DragDropFilesEx.FileList.Clear;
  2993. Result := False;
  2994. if CanPasteFromClipBoard and {MP}DragDropFilesEx.GetFromClipBoard{/MP}
  2995. then
  2996. begin
  2997. if TargetPath = '' then
  2998. TargetPath := PathName;
  2999. case LastClipBoardOperation of
  3000. cboNone:
  3001. begin
  3002. PerformDragDropFileOperation(TargetPath, DROPEFFECT_COPY, False, True);
  3003. if Assigned(OnDDExecuted) then OnDDExecuted(Self, DROPEFFECT_COPY);
  3004. end;
  3005. cboCopy:
  3006. begin
  3007. PerformDragDropFileOperation(TargetPath, DROPEFFECT_COPY,
  3008. ExcludeTrailingPathDelimiter(ExtractFilePath(TFDDListItem(DragDropFilesEx.FileList[0]^).Name)) = Path, True);
  3009. if Assigned(OnDDExecuted) then OnDDExecuted(Self, DROPEFFECT_COPY);
  3010. end;
  3011. cboCut:
  3012. begin
  3013. PerformDragDropFileOperation(TargetPath, DROPEFFECT_MOVE, False, True);
  3014. if Assigned(OnDDExecuted) then OnDDExecuted(Self, DROPEFFECT_MOVE);
  3015. EmptyClipBoard;
  3016. end;
  3017. end;
  3018. Result := True;
  3019. end;
  3020. end; {PasteFromClipBoard}
  3021. function TDirView.DragCompleteFileList: Boolean;
  3022. begin
  3023. Result := inherited DragCompleteFileList and
  3024. (FDriveType <> DRIVE_REMOVABLE);
  3025. end;
  3026. function TDirView.DuplicateSelectedFiles: Boolean;
  3027. begin
  3028. Result := False;
  3029. if SelCount > 0 then
  3030. begin
  3031. Result := CopyToClipBoard(False);
  3032. if Result then
  3033. try
  3034. SelectNewFiles := True;
  3035. Selected := nil;
  3036. Result := PasteFromClipBoard();
  3037. finally
  3038. SelectNewFiles := False;
  3039. if Assigned(Selected) then
  3040. begin
  3041. ItemFocused := Selected;
  3042. Selected.MakeVisible(False);
  3043. if SelCount = 1 then
  3044. Selected.EditCaption;
  3045. end;
  3046. end;
  3047. end;
  3048. EmptyClipBoard;
  3049. end; {DuplicateFiles}
  3050. function TDirView.NewColProperties: TCustomListViewColProperties;
  3051. begin
  3052. Result := TDirViewColProperties.Create(Self);
  3053. end;
  3054. function TDirView.SortAscendingByDefault(Index: Integer): Boolean;
  3055. begin
  3056. Result := not (TDirViewCol(Index) in [dvSize, dvChanged]);
  3057. end;
  3058. procedure TDirView.SetItemImageIndex(Item: TListItem; Index: Integer);
  3059. begin
  3060. Assert(Assigned(Item));
  3061. if Assigned(Item.Data) then
  3062. with PFileRec(Item.Data)^ do
  3063. begin
  3064. ImageIndex := Index;
  3065. IconEmpty := (ImageIndex < 0);
  3066. end;
  3067. end;
  3068. procedure TDirView.SetItemCalculatedSize(Item: TListItem; ASize: Int64);
  3069. var
  3070. OldSize: Int64;
  3071. begin
  3072. Assert(Assigned(Item) and Assigned(Item.Data));
  3073. with PFileRec(Item.Data)^ do
  3074. begin
  3075. OldSize := CalculatedSize;
  3076. CalculatedSize := ASize;
  3077. end;
  3078. ItemCalculatedSizeUpdated(Item, OldSize, ASize);
  3079. end;
  3080. {=================================================================}
  3081. initialization
  3082. LastClipBoardOperation := cboNone;
  3083. DaylightHack := (not IsWin7);
  3084. end.