1
0

DirView.pas 101 KB

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