DirView.pas 99 KB

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