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