DirView.pas 101 KB

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