DirView.pas 100 KB

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