DirView.pas 100 KB

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