DirView.pas 101 KB

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