DirView.pas 102 KB

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