DirView.pas 102 KB

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