DirView.pas 99 KB

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