DirView.pas 99 KB

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