DirView.pas 99 KB

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