DirView.pas 100 KB

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