DirView.pas 101 KB

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