DirView.pas 100 KB

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