DirView.pas 98 KB

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