DirView.pas 101 KB

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