DriveView.pas 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272
  1. unit DriveView;
  2. {==================================================================
  3. Component TDriveView / Version 2.6, January 2000
  4. ==================================================================
  5. Description:
  6. ============
  7. Displays the the directory structure of all drives as treeview
  8. with shell icons. Complete drag&Drop support for files and
  9. directories.
  10. Author:
  11. =======
  12. (c) Ingo Eckel 1998, 1999
  13. Sodener Weg 38
  14. 65812 Bad Soden
  15. Germany
  16. Modifications (for WinSCP):
  17. ===========================
  18. (c) Martin Prikryl 2004
  19. V2.6:
  20. - Shows "shared"-symbol with directories
  21. - Delphi5 compatible
  22. For detailed documentation and history see TDriveView.htm.
  23. {==================================================================}
  24. interface
  25. { Define ENHVALIDATE to scan all existing directories on a detected filesystem change:}
  26. {.$DEFINE ENHVALIDATE}
  27. {Required compiler options for TDriveView:}
  28. {$A+,B-,X+,H+,P+}
  29. {$WARN SYMBOL_PLATFORM OFF}
  30. uses
  31. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ComObj,
  32. Dialogs, ComCtrls, ShellApi, CommCtrl, ExtCtrls, ActiveX, ShlObj,
  33. DirView, ShellDialogs, DragDrop, DragDropFilesEx, FileChanges, FileOperator,
  34. DiscMon, IEDriveInfo, IEListView, BaseUtils, CustomDirView,
  35. CustomDriveView, System.Generics.Collections, CompThread;
  36. const
  37. msThreadChangeDelay = 50;
  38. ErrorNodeNA = '%s: Node not assigned';
  39. {Flags used by TDriveView.RefreshRootNodes:}
  40. dvdsFloppy = 8; {Include floppy drives}
  41. dvdsRereadAllways = 16; {Refresh drivestatus in any case}
  42. WM_USER_SHCHANGENOTIFY = WM_USER + $2000 + 13;
  43. WM_USER_SUBDIRREADER = WM_USER_SHCHANGENOTIFY + 1;
  44. type
  45. EInvalidDirName = class(Exception);
  46. ENodeNotAssigned = class(Exception);
  47. TDriveStatus = class
  48. Scanned: Boolean; {Drive allready scanned?}
  49. Verified: Boolean; {Drive completly scanned?}
  50. RootNode: TTreeNode; {Rootnode to drive}
  51. RootNodeIndex: Integer;
  52. DiscMonitor: TDiscMonitor; {Monitor thread}
  53. ChangeTimer: TTimer; {Change timer for the monitor thread}
  54. DefaultDir: string; {Current directory}
  55. DriveHandle: THandle;
  56. NotificationHandle: HDEVNOTIFY;
  57. end;
  58. TDriveStatusPair = TPair<string, TDriveStatus>;
  59. TScanDirInfo = record
  60. SearchNewDirs: Boolean;
  61. StartNode: TTreeNode;
  62. DriveType: Integer;
  63. end;
  64. PScanDirInfo = ^TScanDirInfo;
  65. TDriveView = class;
  66. TSubDirReaderSchedule = class
  67. Node: TTreeNode;
  68. Path: string;
  69. Deleted: Boolean;
  70. Processed: Boolean;
  71. end;
  72. TNodeData = class
  73. private
  74. FDirName: string;
  75. FAttr: Integer;
  76. FScanned: Boolean;
  77. FData: Pointer;
  78. FIsRecycleBin: Boolean;
  79. FIconEmpty: Boolean;
  80. FSchedule: TSubDirReaderSchedule;
  81. public
  82. DelayedSrec: TSearchRec;
  83. DelayedExclude: TStringList;
  84. constructor Create;
  85. destructor Destroy; override;
  86. property DirName: string read FDirName write FDirName;
  87. property Attr: Integer read FAttr write FAttr;
  88. property Scanned: Boolean read FScanned write FScanned;
  89. property Data: Pointer read FData write FData;
  90. property IsRecycleBin: Boolean read FIsRecycleBin;
  91. property IconEmpty: Boolean read FIconEmpty write FIconEmpty;
  92. property Schedule: TSubDirReaderSchedule read FSchedule write FSchedule;
  93. end;
  94. TDriveTreeNode = class(TTreeNode)
  95. procedure Assign(Source: TPersistent); override;
  96. end;
  97. TSubDirReaderThread = class(TCompThread)
  98. public
  99. destructor Destroy; override;
  100. procedure Terminate; override;
  101. protected
  102. constructor Create(DriveView: TDriveView);
  103. procedure Add(Node: TTreeNode; Path: string);
  104. procedure Delete(Node: TTreeNode);
  105. function Detach: Integer;
  106. procedure Reattach(Count: Integer);
  107. procedure Execute; override;
  108. private
  109. FDriveView: TDriveView;
  110. FEvent: THandle;
  111. FQueue: TStack<TSubDirReaderSchedule>;
  112. FResults: TQueue<TSubDirReaderSchedule>;
  113. FSection: TRTLCriticalSection;
  114. FTimer: TTimer;
  115. FWindowHandle: HWND;
  116. procedure TriggerEvent;
  117. procedure ScheduleProcess;
  118. procedure Process;
  119. function ProcessResult: Boolean;
  120. procedure Timer(Sender: TObject);
  121. procedure WndProc(var Msg: TMessage);
  122. end;
  123. TDriveViewRefreshDrives = procedure(Sender: TObject; Global: Boolean) of object;
  124. TDriveView = class(TCustomDriveView)
  125. private
  126. FDriveStatus: TObjectDictionary<string, TDriveStatus>;
  127. FConfirmDelete: Boolean;
  128. FConfirmOverwrite: Boolean;
  129. FWatchDirectory: Boolean;
  130. FDirectory: string;
  131. FShowVolLabel: Boolean;
  132. FVolDisplayStyle: TVolumeDisplayStyle;
  133. FChangeFlag: Boolean;
  134. FLastDir: string;
  135. FValidateFlag: Boolean;
  136. FCreating: Boolean;
  137. FForceRename: Boolean;
  138. FRenameNode: TTreeNode;
  139. FLastRenameName: string;
  140. FInternalWindowHandle: HWND;
  141. FChangeNotify: ULONG;
  142. FPrevSelected: TTreeNode;
  143. FPrevSelectedIndex: Integer;
  144. FChangeTimerSuspended: Integer;
  145. FSubDirReaderThread: TSubDirReaderThread;
  146. FDelayedNodes: TStringList;
  147. FDelayedNodeTimer: TTimer;
  148. {Additional events:}
  149. FOnDisplayContextMenu: TNotifyEvent;
  150. FOnRefreshDrives: TDriveViewRefreshDrives;
  151. FOnNeedHiddenDirectories: TNotifyEvent;
  152. {used components:}
  153. FDirView: TDirView;
  154. FFileOperator: TFileOperator;
  155. FChangeInterval: Cardinal;
  156. {Drag&drop:}
  157. FLastPathCut: string;
  158. {Drag&drop helper functions:}
  159. procedure SignalDirDelete(Sender: TObject; Files: TStringList);
  160. function GetSubDir(var SRec: TSearchRec): Boolean;
  161. function FindFirstSubDir(Path: string; var SRec: TSearchRec): Boolean;
  162. function FindNextSubDir(var SRec: TSearchRec): Boolean;
  163. procedure ReadSubDirs(Node: TTreeNode);
  164. procedure CancelDelayedNode(Node: TTreeNode);
  165. procedure DelayedNodeTimer(Sender: TObject);
  166. function ReadSubDirsBatch(Node: TTreeNode; var SRec: TSearchRec; CheckInterval, Limit: Integer): Boolean;
  167. procedure UpdateDelayedNodeTimer;
  168. {Callback-functions used by iteratesubtree:}
  169. function CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  170. procedure DeleteNode(Node: TTreeNode);
  171. { Notification procedures used by component TDiscMonitor: }
  172. procedure ChangeDetected(Sender: TObject; const Directory: string;
  173. var SubdirsChanged: Boolean);
  174. procedure ChangeInvalid(Sender: TObject; const Directory: string; const ErrorStr: string);
  175. {Notification procedure used by component TTimer:}
  176. procedure ChangeTimerOnTimer(Sender: TObject);
  177. protected
  178. procedure SetSelected(Node: TTreeNode);
  179. procedure SetWatchDirectory(Value: Boolean);
  180. procedure SetShowVolLabel(ShowIt: Boolean);
  181. procedure SetDirView(Value: TDirView);
  182. procedure SetDirectory(Value: string); override;
  183. function DoScanDir(FromNode: TTreeNode): Boolean;
  184. procedure AddChildNode(ParentNode: TTreeNode; ParentPath: string; SRec: TSearchRec);
  185. procedure CreateWatchThread(Drive: string);
  186. function NodeWatched(Node: TTreeNode): Boolean;
  187. procedure TerminateWatchThread(Drive: string);
  188. function WatchThreadActive: Boolean; overload;
  189. function WatchThreadActive(Drive: string): Boolean; overload;
  190. procedure InternalWndProc(var Msg: TMessage);
  191. procedure UpdateDriveNotifications(Drive: string);
  192. procedure DriveRemoved(Drive: string);
  193. procedure DriveRemoving(Drive: string);
  194. procedure CancelDriveRefresh;
  195. procedure DoRefreshDrives(Global: Boolean);
  196. procedure RefreshRootNodes(dsFlags: Integer);
  197. function DirAttrMask: Integer;
  198. function CreateDriveStatus: TDriveStatus;
  199. procedure ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  200. NewDirs: Boolean); override;
  201. procedure RebuildTree; override;
  202. procedure SetLastPathCut(Path: string);
  203. function GetCanUndoCopyMove: Boolean; virtual;
  204. procedure CreateWnd; override;
  205. procedure DestroyWnd; override;
  206. procedure Edit(const Item: TTVItem); override;
  207. procedure WMUserRename(var Message: TMessage); message WM_USER_RENAME;
  208. procedure CMRecreateWnd(var Msg: TMessage); message CM_RECREATEWND;
  209. function GetCustomDirView: TCustomDirView; override;
  210. procedure SetCustomDirView(Value: TCustomDirView); override;
  211. function NodePath(Node: TTreeNode): string; override;
  212. function NodeIsRecycleBin(Node: TTreeNode): Boolean; override;
  213. function NodePathExists(Node: TTreeNode): Boolean; override;
  214. function NodeColor(Node: TTreeNode): TColor; override;
  215. function FindPathNode(Path: string): TTreeNode; override;
  216. function DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  217. function CreateNode: TTreeNode; override;
  218. function DDSourceEffects: TDropEffectSet; override;
  219. procedure DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer); override;
  220. function DragCompleteFileList: Boolean; override;
  221. function DDExecute: TDragResult; override;
  222. public
  223. property Images;
  224. property StateImages;
  225. property Items stored False;
  226. property Selected Write SetSelected stored False;
  227. property DragImageList: TDragImageList read FDragImageList;
  228. property CanUndoCopyMove: Boolean read GetCanUndoCopyMove;
  229. property DDFileOperator: TFileOperator read FFileOperator;
  230. property LastPathCut: string read FLastPathCut write SetLastPathCut;
  231. function UndoCopyMove: Boolean; dynamic;
  232. procedure EmptyClipboard; dynamic;
  233. function CopyToClipBoard(Node: TTreeNode): Boolean; dynamic;
  234. function CutToClipBoard(Node: TTreeNode): Boolean; dynamic;
  235. function CanPasteFromClipBoard: Boolean; dynamic;
  236. function PasteFromClipBoard(TargetPath: string = ''): Boolean; dynamic;
  237. procedure PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer); override;
  238. {Drive handling:}
  239. function GetDriveStatus(Drive: string): TDriveStatus;
  240. function GetDriveTypetoNode(Node: TTreeNode): Integer; {Returns DRIVE_CDROM etc..}
  241. function GetDriveToNode(Node: TTreeNode): string;
  242. function GetDriveText(Drive: string): string;
  243. procedure ScanDrive(Drive: string);
  244. function GetDrives: TStrings;
  245. procedure ScheduleDriveRefresh;
  246. {Node handling:}
  247. procedure SetImageIndex(Node: TTreeNode); virtual;
  248. function FindNodeToPath(Path: string): TTreeNode;
  249. function TryFindNodeToPath(Path: string): TTreeNode;
  250. function RootNode(Node: TTreeNode): TTreeNode;
  251. function GetDirName(Node: TTreeNode): string;
  252. function GetDisplayName(Node: TTreeNode): string;
  253. function NodePathName(Node: TTreeNode): string; override;
  254. constructor Create(AOwner: TComponent); override;
  255. destructor Destroy; override;
  256. {Menu-handling:}
  257. procedure DisplayContextMenu(Node: TTreeNode; Point: TPoint); override;
  258. procedure DisplayPropertiesMenu(Node: TTreeNode); override;
  259. {Watchthread handling:}
  260. procedure StartWatchThread;
  261. procedure StopWatchThread;
  262. procedure SuspendChangeTimer;
  263. procedure ResumeChangeTimer;
  264. procedure StartAllWatchThreads;
  265. procedure StopAllWatchThreads;
  266. procedure ValidateCurrentDirectoryIfNotMonitoring;
  267. (* Modified Events: *)
  268. procedure GetImageIndex(Node: TTreeNode); override;
  269. function CanEdit(Node: TTreeNode): Boolean; override;
  270. function CanChange(Node: TTreeNode): Boolean; override;
  271. function CanExpand(Node: TTreeNode): Boolean; override;
  272. procedure Delete(Node: TTreeNode); override;
  273. procedure Loaded; override;
  274. procedure KeyPress(var Key: Char); override;
  275. procedure Change(Node: TTreeNode); override;
  276. published
  277. {Additional properties:}
  278. {Current selected directory:}
  279. property Directory;
  280. {Confirm deleting directories:}
  281. property ConfirmDelete: Boolean read FConfirmDelete write FConfirmDelete default True;
  282. {Confirm overwriting directories:}
  283. property ConfirmOverwrite: Boolean read FConfirmOverwrite write FConfirmOverwrite default True;
  284. {Enable automatic update on filesystem changes:}
  285. property WatchDirectory: Boolean read FWatchDirectory write SetWatchDirectory default False;
  286. {Linked component TDirView:}
  287. property DirView: TDirView read FDirView write SetDirView;
  288. {Show the volume labels of drives:}
  289. property ShowVolLabel: Boolean read FShowVolLabel write SetShowVolLabel default True;
  290. {Additional events:}
  291. property OnDisplayContextMenu: TNotifyEvent read FOnDisplayContextMenu
  292. write FOnDisplayContextMenu;
  293. property OnRefreshDrives: TDriveViewRefreshDrives read FOnRefreshDrives write FOnRefreshDrives;
  294. property OnBusy;
  295. property DDLinkOnExeDrag;
  296. property TargetPopUpMenu;
  297. property OnDDDragEnter;
  298. property OnDDDragLeave;
  299. property OnDDDragOver;
  300. property OnDDDrop;
  301. property OnDDQueryContinueDrag;
  302. property OnDDGiveFeedback;
  303. property OnDDDragDetect;
  304. property OnDDProcessDropped;
  305. property OnDDError;
  306. property OnDDExecuted;
  307. property OnDDFileOperation;
  308. property OnDDFileOperationExecuted;
  309. property Align;
  310. property Anchors;
  311. property AutoExpand;
  312. property BiDiMode;
  313. property BorderStyle;
  314. property BorderWidth;
  315. property ChangeDelay;
  316. property Color;
  317. property Ctl3D;
  318. property Constraints;
  319. property DoubleBuffered;
  320. {Delphi's drag&drop is not compatible with the OLE windows drag&drop:}
  321. property DragKind;
  322. property DragCursor;
  323. property DragMode Default dmAutomatic;
  324. property OnDragDrop;
  325. property OnDragOver;
  326. property Enabled;
  327. property Font;
  328. property HideSelection;
  329. property HotTrack;
  330. property Indent;
  331. property ParentBiDiMode;
  332. property ParentColor;
  333. property ParentCtl3D;
  334. property ParentDoubleBuffered;
  335. property ParentFont;
  336. property ParentShowHint;
  337. property PopupMenu;
  338. property ReadOnly;
  339. property RightClickSelect;
  340. property RowSelect;
  341. property ShowButtons;
  342. property ShowHint;
  343. property ShowLines;
  344. property TabOrder;
  345. property TabStop default True;
  346. property ToolTips;
  347. property Visible;
  348. property OnChange;
  349. property OnChanging;
  350. property OnClick;
  351. property OnCollapsing;
  352. property OnCollapsed;
  353. property OnCompare;
  354. property OnDblClick;
  355. property OnDeletion;
  356. property OnEdited;
  357. property OnEditing;
  358. property OnEndDock;
  359. property OnEndDrag;
  360. property OnEnter;
  361. property OnExit;
  362. property OnExpanding;
  363. property OnExpanded;
  364. property OnGetImageIndex;
  365. property OnGetSelectedIndex;
  366. property OnKeyDown;
  367. property OnKeyPress;
  368. property OnKeyUp;
  369. property OnMouseDown;
  370. property OnMouseMove;
  371. property OnMouseUp;
  372. property OnStartDock;
  373. property OnStartDrag;
  374. property OnNeedHiddenDirectories: TNotifyEvent read FOnNeedHiddenDirectories write FOnNeedHiddenDirectories;
  375. end;
  376. var
  377. DriveViewLoadingTooLongLimit: Integer = 0;
  378. procedure Register;
  379. implementation
  380. uses
  381. PasTools, UITypes, SyncObjs, IOUtils, System.DateUtils;
  382. type
  383. PInt = ^Integer;
  384. procedure Register;
  385. begin
  386. RegisterComponents('DriveDir', [TDriveView]);
  387. end; {Register}
  388. constructor TNodeData.Create;
  389. begin
  390. inherited;
  391. FAttr := 0;
  392. FScanned := False;
  393. FDirName := '';
  394. FIsRecycleBin := False;
  395. FIconEmpty := True;
  396. FSchedule := nil;
  397. DelayedExclude := nil;
  398. end; {TNodeData.Create}
  399. destructor TNodeData.Destroy;
  400. begin
  401. Assert(not Assigned(FSchedule));
  402. SetLength(FDirName, 0);
  403. inherited;
  404. end; {TNodeData.Destroy}
  405. { TSubDirReaderThread }
  406. constructor TSubDirReaderThread.Create(DriveView: TDriveView);
  407. begin
  408. inherited Create(True);
  409. FDriveView := DriveView;
  410. FSection.Initialize;
  411. FEvent := CreateEvent(nil, False, False, nil);
  412. FQueue := TStack<TSubDirReaderSchedule>.Create;
  413. FResults := TQueue<TSubDirReaderSchedule>.Create;
  414. FTimer := TTimer.Create(FDriveView);
  415. FTimer.Enabled := False;
  416. FTimer.Interval := 200;
  417. FTimer.OnTimer := Timer;
  418. FWindowHandle := AllocateHWnd(WndProc);
  419. end;
  420. destructor TSubDirReaderThread.Destroy;
  421. procedure DestroyScheduleList(List: TEnumerable<TSubDirReaderSchedule>);
  422. var
  423. Schedule: TSubDirReaderSchedule;
  424. begin
  425. for Schedule in List do
  426. begin
  427. if not Schedule.Deleted then
  428. TNodeData(Schedule.Node.Data).Schedule := nil;
  429. Schedule.Free;
  430. end;
  431. List.Destroy;
  432. end;
  433. begin
  434. inherited;
  435. DeallocateHWnd(FWindowHandle);
  436. DestroyScheduleList(FQueue);
  437. DestroyScheduleList(FResults);
  438. CloseHandle(FEvent);
  439. FTimer.Destroy;
  440. FSection.Destroy;
  441. end;
  442. procedure TSubDirReaderThread.WndProc(var Msg: TMessage);
  443. begin
  444. if Msg.Msg = WM_USER_SUBDIRREADER then
  445. ScheduleProcess
  446. else
  447. Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  448. end;
  449. procedure TSubDirReaderThread.Process;
  450. var
  451. Started: DWORD;
  452. Elapsed: Integer;
  453. Later: Boolean;
  454. begin
  455. Started := GetTickCount;
  456. Later := False;
  457. while (not Later) and ProcessResult do
  458. begin
  459. Elapsed := GetTickCount - Started;
  460. Later := (Elapsed < 0) or (Elapsed > 20);
  461. end;
  462. if not Later then
  463. FTimer.Enabled := False;
  464. end;
  465. procedure TSubDirReaderThread.Timer(Sender: TObject);
  466. begin
  467. Process;
  468. end;
  469. procedure TSubDirReaderThread.Add(Node: TTreeNode; Path: string);
  470. var
  471. NodeData: TNodeData;
  472. Schedule: TSubDirReaderSchedule;
  473. begin
  474. if Suspended then
  475. Resume;
  476. FSection.Enter;
  477. try
  478. NodeData := TNodeData(Node.Data);
  479. Assert(not Assigned(NodeData.Schedule));
  480. Schedule := TSubDirReaderSchedule.Create;
  481. Schedule.Node := Node;
  482. Schedule.Path := Path;
  483. Schedule.Deleted := False;
  484. Schedule.Processed := False;
  485. FQueue.Push(Schedule);
  486. NodeData.Schedule := Schedule;
  487. finally
  488. FSection.Leave;
  489. end;
  490. TriggerEvent;
  491. end;
  492. procedure TSubDirReaderThread.Delete(Node: TTreeNode);
  493. var
  494. NodeData: TNodeData;
  495. begin
  496. FSection.Enter;
  497. try
  498. NodeData := TNodeData(Node.Data);
  499. if Assigned(NodeData.Schedule) then
  500. begin
  501. NodeData.Schedule.Deleted := True;
  502. NodeData.Schedule := nil;
  503. end;
  504. finally
  505. FSection.Leave;
  506. end;
  507. TriggerEvent;
  508. end;
  509. function TSubDirReaderThread.Detach: Integer;
  510. procedure DetachList(List: TEnumerable<TSubDirReaderSchedule>);
  511. var
  512. Schedule: TSubDirReaderSchedule;
  513. begin
  514. for Schedule in List do
  515. begin
  516. if Schedule.Deleted then Schedule.Free
  517. else
  518. begin
  519. Assert(Schedule.Processed = (List = FResults));
  520. Schedule.Node := nil;
  521. Inc(Result);
  522. end;
  523. end;
  524. end;
  525. begin
  526. // block thread while handle is being recreated
  527. FSection.Enter;
  528. try
  529. Result := 0;
  530. DetachList(FQueue);
  531. DetachList(FResults);
  532. FQueue.Clear;
  533. FResults.Clear;
  534. except
  535. FSection.Leave;
  536. raise;
  537. end;
  538. end;
  539. procedure TSubDirReaderThread.Reattach(Count: Integer);
  540. var
  541. Node: TTreeNode;
  542. Schedule: TSubDirReaderSchedule;
  543. begin
  544. try
  545. if Count > 0 then
  546. begin
  547. Node := FDriveView.Items.GetFirstNode;
  548. while Assigned(Node) do
  549. begin
  550. Schedule := TNodeData(Node.Data).Schedule;
  551. if Assigned(Schedule) then
  552. begin
  553. Assert(not Assigned(Schedule.Node));
  554. Schedule.Node := Node;
  555. if not Schedule.Processed then
  556. FQueue.Push(Schedule)
  557. else
  558. FResults.Enqueue(Schedule);
  559. Assert(Count > 0);
  560. // Can be optimized to stop once Count = 0
  561. Dec(Count);
  562. end;
  563. Node := Node.GetNext;
  564. end;
  565. if Count = 0 then Assert(False); // shut up
  566. end;
  567. finally
  568. FSection.Leave;
  569. end;
  570. TriggerEvent;
  571. ScheduleProcess;
  572. end;
  573. procedure TSubDirReaderThread.Terminate;
  574. begin
  575. inherited;
  576. TriggerEvent;
  577. end;
  578. procedure TSubDirReaderThread.TriggerEvent;
  579. begin
  580. SetEvent(FEvent);
  581. end;
  582. function TSubDirReaderThread.ProcessResult: Boolean;
  583. var
  584. Node: TTreeNode;
  585. NodeData: TNodeData;
  586. Schedule: TSubDirReaderSchedule;
  587. begin
  588. FSection.Enter;
  589. try
  590. Result := (FResults.Count > 0);
  591. if Result then
  592. begin
  593. Schedule := FResults.Dequeue;
  594. if not Schedule.Deleted then
  595. begin
  596. Assert(Schedule.Processed);
  597. Node := Schedule.Node;
  598. Node.HasChildren := False;
  599. NodeData := TNodeData(Node.Data);
  600. NodeData.Scanned := not Node.HasChildren; // = True
  601. Assert(NodeData.Schedule = Schedule);
  602. NodeData.Schedule := nil;
  603. end;
  604. Schedule.Free;
  605. end;
  606. finally
  607. FSection.Leave;
  608. end;
  609. end;
  610. procedure TSubDirReaderThread.ScheduleProcess;
  611. begin
  612. // process the first batch immediatelly, to make it more likely that the first seen subdirectories
  613. // will immediatelly show correct status
  614. Process;
  615. FTimer.Enabled := True;
  616. end;
  617. procedure TSubDirReaderThread.Execute;
  618. var
  619. SRec: TSearchRec;
  620. HasSubDirs: Boolean;
  621. NodeData: TNodeData;
  622. Schedule: TSubDirReaderSchedule;
  623. DelayStart, DelayStartStep: Integer;
  624. begin
  625. DelayStart := 3000;
  626. DelayStartStep := 100;
  627. while (DelayStart > 0) and (not Terminated) do
  628. begin
  629. Sleep(DelayStartStep);
  630. Dec(DelayStart, DelayStartStep)
  631. end;
  632. while not Terminated do
  633. begin
  634. WaitForSingleObject(FEvent, INFINITE);
  635. while not Terminated do
  636. begin
  637. FSection.Enter;
  638. try
  639. if FQueue.Count = 0 then
  640. begin
  641. Schedule := nil; // shut up
  642. Break;
  643. end
  644. else
  645. begin
  646. Schedule := FQueue.Pop;
  647. if Schedule.Deleted then
  648. begin
  649. Schedule.Free;
  650. // Can be optimized to loop within locked critical section until first non-deleted schedule is found
  651. Continue;
  652. end;
  653. Assert(not Schedule.Processed);
  654. end
  655. finally
  656. FSection.Leave;
  657. end;
  658. HasSubDirs := FDriveView.FindFirstSubDir(IncludeTrailingBackslash(Schedule.Path) + '*.*', SRec);
  659. FindClose(SRec);
  660. FSection.Enter;
  661. try
  662. if Schedule.Deleted then
  663. begin
  664. Schedule.Free;
  665. end
  666. else
  667. begin
  668. Schedule.Processed := True;
  669. if not HasSubDirs then // optimization
  670. begin
  671. FResults.Enqueue(Schedule);
  672. if FResults.Count = 1 then
  673. PostMessage(FWindowHandle, WM_USER_SUBDIRREADER, 0, 0);
  674. end
  675. else
  676. begin
  677. // can happen only if the tree handle is just being recreated
  678. if Assigned(Schedule.Node) then
  679. begin
  680. NodeData := TNodeData(Schedule.Node.Data);
  681. NodeData.Schedule := nil;
  682. end;
  683. Schedule.Free;
  684. end;
  685. end;
  686. finally
  687. FSection.Leave;
  688. end;
  689. end;
  690. end;
  691. end;
  692. { TDriveTreeNode }
  693. // Not sure if this is ever used (possibly only then "assigning" tree view to another instance, what never do).
  694. // It is NOT used when recreating a tree view handle - for that a node is serialized and deserialized,
  695. // including a pointer to TNodeData. See csRecreating condition in TDriveView.Delete.
  696. procedure TDriveTreeNode.Assign(Source: TPersistent);
  697. var
  698. SourceData: TNodeData;
  699. NewData: TNodeData;
  700. begin
  701. Assert(False);
  702. inherited Assign(Source);
  703. if not Deleting and (Source is TTreeNode) then
  704. begin
  705. SourceData := TNodeData(TTreeNode(Source).Data);
  706. NewData := TNodeData.Create();
  707. NewData.DirName := SourceData.DirName;
  708. NewData.Attr := SourceData.Attr;
  709. NewData.Scanned := SourceData.Scanned;
  710. NewData.Data := SourceData.Data;
  711. NewData.FIsRecycleBin := SourceData.FIsRecycleBin;
  712. NewData.IconEmpty := SourceData.IconEmpty;
  713. TTreeNode(Source).Data := NewData;
  714. end;
  715. end;
  716. { TDriveView }
  717. constructor TDriveView.Create(AOwner: TComponent);
  718. var
  719. Drive: TRealDrive;
  720. ChangeNotifyEntry: TSHChangeNotifyEntry;
  721. Dummy: string;
  722. begin
  723. inherited;
  724. FCreating := True;
  725. FDriveStatus := TObjectDictionary<string, TDriveStatus>.Create([doOwnsValues]);
  726. FChangeInterval := MSecsPerSec;
  727. for Drive := FirstDrive to LastDrive do
  728. begin
  729. FDriveStatus.Add(Drive, CreateDriveStatus);
  730. end;
  731. FFileOperator := TFileOperator.Create(Self);
  732. FSubDirReaderThread := TSubDirReaderThread.Create(Self);
  733. FDelayedNodes := TStringList.Create;
  734. FDelayedNodeTimer := TTimer.Create(Self);
  735. UpdateDelayedNodeTimer;
  736. FDelayedNodeTimer.Interval := 250;
  737. FDelayedNodeTimer.OnTimer := DelayedNodeTimer;
  738. FShowVolLabel := True;
  739. FChangeFlag := False;
  740. FLastDir := EmptyStr;
  741. FValidateFlag := False;
  742. FConfirmDelete := True;
  743. FDirectory := EmptyStr;
  744. FForceRename := False;
  745. FLastRenameName := '';
  746. FRenameNode := nil;
  747. FPrevSelected := nil;
  748. FPrevSelectedIndex := -1;
  749. FChangeTimerSuspended := 0;
  750. FConfirmOverwrite := True;
  751. FLastPathCut := '';
  752. FStartPos.X := -1;
  753. FStartPos.Y := -1;
  754. FDragPos := FStartPos;
  755. FInternalWindowHandle := Classes.AllocateHWnd(InternalWndProc);
  756. // Source: petr.solin 2022-02-25
  757. FChangeNotify := 0;
  758. if SpecialFolderLocation(CSIDL_DESKTOP, Dummy, ChangeNotifyEntry.pidl) then
  759. begin
  760. ChangeNotifyEntry.fRecursive := False;
  761. FChangeNotify :=
  762. SHChangeNotifyRegister(
  763. FInternalWindowHandle, SHCNRF_ShellLevel or SHCNRF_NewDelivery,
  764. SHCNE_RENAMEFOLDER or SHCNE_MEDIAINSERTED or SHCNE_MEDIAREMOVED,
  765. WM_USER_SHCHANGENOTIFY, 1, ChangeNotifyEntry);
  766. end;
  767. with FDragDropFilesEx do
  768. begin
  769. ShellExtensions.DragDropHandler := True;
  770. end;
  771. end; {Create}
  772. destructor TDriveView.Destroy;
  773. var
  774. DriveStatusPair: TDriveStatusPair;
  775. begin
  776. if FChangeNotify <> 0 then SHChangeNotifyDeregister(FChangeNotify);
  777. Classes.DeallocateHWnd(FInternalWindowHandle);
  778. for DriveStatusPair in FDriveStatus do
  779. begin
  780. with DriveStatusPair.Value do
  781. begin
  782. if Assigned(DiscMonitor) then
  783. FreeAndNil(DiscMonitor);
  784. if Assigned(ChangeTimer) then
  785. FreeAndNil(ChangeTimer);
  786. end;
  787. UpdateDriveNotifications(DriveStatusPair.Key);
  788. end;
  789. FDriveStatus.Free;
  790. if Assigned(FFileOperator) then
  791. FFileOperator.Free;
  792. FSubDirReaderThread.Free;
  793. Assert(FDelayedNodes.Count = 0);
  794. FreeAndNil(FDelayedNodes);
  795. inherited Destroy;
  796. end; {Destroy}
  797. function TDriveView.CreateDriveStatus: TDriveStatus;
  798. begin
  799. Result := TDriveStatus.Create;
  800. with Result do
  801. begin
  802. Scanned := False;
  803. Verified := False;
  804. RootNode := nil;
  805. RootNodeIndex := -1;
  806. DiscMonitor := nil;
  807. DefaultDir := EmptyStr;
  808. {ChangeTimer: }
  809. ChangeTimer := TTimer.Create(Self);
  810. ChangeTimer.Interval := 0;
  811. ChangeTimer.Enabled := False;
  812. ChangeTimer.OnTimer := ChangeTimerOnTimer;
  813. DriveHandle := INVALID_HANDLE_VALUE;
  814. NotificationHandle := nil;
  815. end;
  816. end;
  817. procedure TDriveView.DriveRemoving(Drive: string);
  818. begin
  819. DriveRemoved(Drive);
  820. TerminateWatchThread(Drive);
  821. end;
  822. type
  823. PDevBroadcastHdr = ^TDevBroadcastHdr;
  824. TDevBroadcastHdr = record
  825. dbch_size: DWORD;
  826. dbch_devicetype: DWORD;
  827. dbch_reserved: DWORD;
  828. end;
  829. PDevBroadcastVolume = ^TDevBroadcastVolume;
  830. TDevBroadcastVolume = record
  831. dbcv_size: DWORD;
  832. dbcv_devicetype: DWORD;
  833. dbcv_reserved: DWORD;
  834. dbcv_unitmask: DWORD;
  835. dbcv_flags: WORD;
  836. end;
  837. PDEV_BROADCAST_HANDLE = ^DEV_BROADCAST_HANDLE;
  838. DEV_BROADCAST_HANDLE = record
  839. dbch_size : DWORD;
  840. dbch_devicetype : DWORD;
  841. dbch_reserved : DWORD;
  842. dbch_handle : THandle;
  843. dbch_hdevnotify : HDEVNOTIFY ;
  844. dbch_eventguid : TGUID;
  845. dbch_nameoffset : LongInt;
  846. dbch_data : Byte;
  847. end;
  848. PPItemIDList = ^PItemIDList;
  849. const
  850. DBT_DEVTYP_HANDLE = $00000006;
  851. DBT_CONFIGCHANGED = $0018;
  852. DBT_DEVICEARRIVAL = $8000;
  853. DBT_DEVICEQUERYREMOVE = $8001;
  854. DBT_DEVICEREMOVEPENDING = $8003;
  855. DBT_DEVICEREMOVECOMPLETE = $8004;
  856. DBT_DEVTYP_VOLUME = $00000002;
  857. // WORKAROUND Declaration in Winapi.ShlObj.pas is wrong
  858. function SHChangeNotification_Lock(hChange: THandle; dwProcId: DWORD;
  859. var PPidls: PPItemIDList; var plEvent: Longint): THANDLE; stdcall;
  860. external 'shell32.dll' name 'SHChangeNotification_Lock';
  861. procedure TDriveView.InternalWndProc(var Msg: TMessage);
  862. var
  863. DeviceType: DWORD;
  864. UnitMask: DWORD;
  865. DeviceHandle: THandle;
  866. Drive: Char;
  867. DriveStatusPair: TDriveStatusPair;
  868. PPIDL: PPItemIDList;
  869. Event: LONG;
  870. Lock: THandle;
  871. begin
  872. with Msg do
  873. begin
  874. if Msg = WM_USER_SHCHANGENOTIFY then
  875. begin
  876. Lock := SHChangeNotification_Lock(wParam, lParam, PPIDL, Event);
  877. try
  878. if (Event = SHCNE_RENAMEFOLDER) or // = drive rename
  879. (Event = SHCNE_MEDIAINSERTED) or // also bitlocker drive unlock (also sends SHCNE_UPDATEDIR)
  880. (Event = SHCNE_MEDIAREMOVED) then
  881. begin
  882. ScheduleDriveRefresh;
  883. end;
  884. finally
  885. SHChangeNotification_Unlock(Lock);
  886. end;
  887. end
  888. else
  889. if Msg = WM_DEVICECHANGE then
  890. begin
  891. if (wParam = DBT_CONFIGCHANGED) or
  892. (wParam = DBT_DEVICEARRIVAL) or
  893. (wParam = DBT_DEVICEREMOVECOMPLETE) then
  894. begin
  895. ScheduleDriveRefresh;
  896. end
  897. else
  898. if (wParam = DBT_DEVICEQUERYREMOVE) or
  899. (wParam = DBT_DEVICEREMOVEPENDING) then
  900. begin
  901. DeviceType := PDevBroadcastHdr(lParam)^.dbch_devicetype;
  902. // This is specifically for VeraCrypt.
  903. // For normal drives, see DBT_DEVTYP_HANDLE below
  904. // (and maybe now that we have generic implementation, this specific code for VeraCrypt might not be needed anymore)
  905. if DeviceType = DBT_DEVTYP_VOLUME then
  906. begin
  907. UnitMask := PDevBroadcastVolume(lParam)^.dbcv_unitmask;
  908. Drive := FirstDrive;
  909. while UnitMask > 0 do
  910. begin
  911. if UnitMask and $01 <> 0 then
  912. begin
  913. DriveRemoving(Drive);
  914. end;
  915. UnitMask := UnitMask shr 1;
  916. Drive := Chr(Ord(Drive) + 1);
  917. end;
  918. end
  919. else
  920. if DeviceType = DBT_DEVTYP_HANDLE then
  921. begin
  922. DeviceHandle := PDEV_BROADCAST_HANDLE(lParam)^.dbch_handle;
  923. for DriveStatusPair in FDriveStatus do
  924. if DriveStatusPair.Value.DriveHandle = DeviceHandle then
  925. begin
  926. DriveRemoving(DriveStatusPair.Key);
  927. end;
  928. end;
  929. end;
  930. end
  931. else
  932. if Msg = WM_TIMER then
  933. begin
  934. CancelDriveRefresh;
  935. try
  936. //DriveInfo.Load;
  937. RefreshRootNodes(dsAll or dvdsRereadAllways);
  938. DoRefreshDrives(True);
  939. except
  940. Application.HandleException(Self);
  941. end;
  942. end;
  943. Result := DefWindowProc(FInternalWindowHandle, Msg, wParam, lParam);
  944. end;
  945. end;
  946. procedure TDriveView.DoRefreshDrives(Global: Boolean);
  947. begin
  948. if Assigned(OnRefreshDrives) then
  949. OnRefreshDrives(Self, Global);
  950. end;
  951. procedure TDriveView.CancelDriveRefresh;
  952. begin
  953. KillTimer(FInternalWindowHandle, 1);
  954. end;
  955. procedure TDriveView.ScheduleDriveRefresh;
  956. begin
  957. CancelDriveRefresh;
  958. // Delay refreshing drives for a sec.
  959. // Particularly with CD/DVD drives, if we query display name
  960. // immediately after receiving DBT_DEVICEARRIVAL, we do not get media label.
  961. // Actually one sec does not help usually, but we do not want to wait any longer,
  962. // because we want to add USB drives asap.
  963. // And this problem might be solved now by SHChangeNotifyRegister/SHCNE_RENAMEFOLDER.
  964. SetTimer(FInternalWindowHandle, 1, MSecsPerSec, nil);
  965. end;
  966. procedure TDriveView.CreateWnd;
  967. var
  968. DriveStatus: TDriveStatus;
  969. begin
  970. inherited;
  971. if Assigned(PopupMenu) then
  972. PopupMenu.Autopopup := False;
  973. FDragDropFilesEx.SourceEffects := [deCopy, deMove, deLink];
  974. FDragDropFilesEx.TargetEffects := [deCopy, deMove, deLink];
  975. if FPrevSelectedIndex >= 0 then
  976. begin
  977. FPrevSelected := Items[FPrevSelectedIndex];
  978. FPrevSelectedIndex := -1;
  979. end;
  980. for DriveStatus in FDriveStatus.Values do
  981. with DriveStatus do
  982. begin
  983. if RootNodeIndex >= 0 then
  984. begin
  985. RootNode := Items[RootNodeIndex];
  986. RootNodeIndex := -1;
  987. end;
  988. end;
  989. UpdateDelayedNodeTimer;
  990. end; {CreateWnd}
  991. procedure TDriveView.DestroyWnd;
  992. var
  993. DriveStatus: TDriveStatus;
  994. I: Integer;
  995. begin
  996. FDelayedNodeTimer.Enabled := False;
  997. for I := 0 to FDelayedNodes.Count - 1 do
  998. FDelayedNodes.Objects[I] := nil;
  999. if not (csRecreating in ControlState) then
  1000. begin
  1001. FSubDirReaderThread.Terminate;
  1002. FSubDirReaderThread.WaitFor;
  1003. end
  1004. else
  1005. if CreateWndRestores and (Items.Count > 0) then
  1006. begin
  1007. FPrevSelectedIndex := -1;
  1008. if Assigned(FPrevSelected) then
  1009. begin
  1010. FPrevSelectedIndex := FPrevSelected.AbsoluteIndex;
  1011. FPrevSelected := nil;
  1012. end;
  1013. for DriveStatus in FDriveStatus.Values do
  1014. with DriveStatus do
  1015. begin
  1016. RootNodeIndex := -1;
  1017. if Assigned(RootNode) then
  1018. begin
  1019. RootNodeIndex := RootNode.AbsoluteIndex;
  1020. RootNode := nil;
  1021. end;
  1022. end;
  1023. end;
  1024. inherited;
  1025. end;
  1026. function TDriveView.NodeColor(Node: TTreeNode): TColor;
  1027. begin
  1028. Result := clDefaultItemColor;
  1029. with TNodeData(Node.Data) do
  1030. if not Node.Selected then
  1031. begin
  1032. {Colored display of compressed directories:}
  1033. if (Attr and FILE_ATTRIBUTE_COMPRESSED) <> 0 then
  1034. begin
  1035. if SupportsDarkMode and DarkMode then Result := clSkyBlue
  1036. else Result := clBlue;
  1037. end
  1038. else
  1039. {Dimmed display, if hidden-atrribut set:}
  1040. if FDimmHiddenDirs and ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  1041. Result := clGrayText
  1042. end;
  1043. end;
  1044. function TDriveView.GetCustomDirView: TCustomDirView;
  1045. begin
  1046. Result := DirView;
  1047. end;
  1048. procedure TDriveView.SetCustomDirView(Value: TCustomDirView);
  1049. begin
  1050. DirView := Value as TDirView;
  1051. end;
  1052. function TDriveView.NodePath(Node: TTreeNode): string;
  1053. var
  1054. ParentNode: TTreeNode;
  1055. begin
  1056. if not Assigned(Node) then
  1057. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDirPath']));
  1058. Result := GetDirName(Node);
  1059. ParentNode := Node.Parent;
  1060. while (ParentNode <> nil) and (ParentNode.Level >= 0) do
  1061. begin
  1062. if ParentNode.Level > 0 then
  1063. Result := GetDirName(ParentNode) + '\' + Result
  1064. else
  1065. Result := GetDirName(ParentNode) + Result;
  1066. ParentNode := ParentNode.Parent;
  1067. end;
  1068. if IsRootPath(Result) then
  1069. Result := ExcludeTrailingBackslash(Result);
  1070. end;
  1071. {NodePathName: Returns the complete path to Node with trailing backslash on rootnodes:
  1072. C:\ ,C:\WINDOWS, C:\WINDOWS\SYSTEM }
  1073. function TDriveView.NodePathName(Node: TTreeNode): string;
  1074. begin
  1075. Result := NodePath(Node);
  1076. if IsRootPath(Result) then
  1077. Result := IncludeTrailingBackslash(Result);
  1078. end; {NodePathName}
  1079. function TDriveView.NodeIsRecycleBin(Node: TTreeNode): Boolean;
  1080. begin
  1081. Result := TNodeData(Node.Data).IsRecycleBin;
  1082. end;
  1083. function TDriveView.NodePathExists(Node: TTreeNode): Boolean;
  1084. begin
  1085. Result := DirectoryExists(NodePathName(Node));
  1086. end;
  1087. function TDriveView.CanEdit(Node: TTreeNode): Boolean;
  1088. begin
  1089. Result := inherited CanEdit(Node) or FForceRename;
  1090. if Result then
  1091. begin
  1092. Result := Assigned(Node.Parent) and
  1093. (not TNodeData(Node.Data).IsRecycleBin) and
  1094. (not ReadOnly) and
  1095. (FDragDropFilesEx.DragDetectStatus <> ddsDrag) and
  1096. ((TNodeData(Node.Data).Attr and (faReadOnly or faSysFile)) = 0) and
  1097. (UpperCase(Node.Text) = UpperCase(GetDirName(Node)));
  1098. end;
  1099. FForceRename := False;
  1100. end; {CanEdit}
  1101. procedure TDriveView.Edit(const Item: TTVItem);
  1102. var
  1103. Node: TTreeNode;
  1104. Info: string;
  1105. i: Integer;
  1106. begin
  1107. Node := GetNodeFromHItem(Item);
  1108. if (Length(Item.pszText) > 0) and (Item.pszText <> Node.Text) then
  1109. begin
  1110. if StrContains(coInvalidDosChars, Item.pszText) then
  1111. begin
  1112. Info := coInvalidDosChars;
  1113. for i := Length(Info) downto 1 do
  1114. System.Insert(Space, Info, i);
  1115. if Length(Item.pszText) > 0 then
  1116. raise EInvalidDirName.Create(SErrorInvalidName + Space + Info);
  1117. Exit;
  1118. end;
  1119. StopWatchThread;
  1120. if Assigned(DirView) then
  1121. DirView.StopWatchThread;
  1122. with FFileOperator do
  1123. begin
  1124. Flags := FileOperatorDefaultFlags + [foNoConfirmation];
  1125. Operation := foRename;
  1126. OperandFrom.Clear;
  1127. OperandTo.Clear;
  1128. OperandFrom.Add(NodePath(Node));
  1129. OperandTo.Add(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText);
  1130. end;
  1131. try
  1132. if FFileOperator.Execute then
  1133. begin
  1134. Node.Text := Item.pszText;
  1135. TNodeData(Node.Data).DirName := Item.pszText;
  1136. SortChildren(Node.Parent, False);
  1137. inherited;
  1138. end
  1139. else
  1140. begin
  1141. if FileOrDirExists(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText) then
  1142. Info := SErrorRenameFileExists + Item.pszText
  1143. else
  1144. Info := SErrorRenameFile + Item.pszText;
  1145. MessageBeep(MB_ICONHAND);
  1146. if MessageDlg(FormatLastOSError(Info), mtError, [mbOK, mbAbort], 0) = mrOK then
  1147. begin
  1148. FLastRenameName := Item.pszText;
  1149. FRenameNode := Node;
  1150. PostMessage(Self.Handle, WM_USER_RENAME, 0, 0);
  1151. end;
  1152. end;
  1153. finally
  1154. StartWatchThread;
  1155. if Assigned(DirView) then
  1156. begin
  1157. DirView.Reload2;
  1158. DirView.StartWatchThread;
  1159. end;
  1160. end;
  1161. end;
  1162. end; {Edit}
  1163. procedure TDriveView.WMUserRename(var Message: TMessage);
  1164. begin
  1165. if Assigned(FRenameNode) then
  1166. begin
  1167. FForceRename := True;
  1168. TreeView_EditLabel(Handle, FRenameNode.ItemID);
  1169. SetWindowText(TreeView_GetEditControl(Self.Handle), PChar(FLastRenameName));
  1170. FRenameNode := nil;
  1171. end;
  1172. end; {WMUserRename}
  1173. function TDriveView.CanExpand(Node: TTreeNode): Boolean;
  1174. var
  1175. SubNode: TTreeNode;
  1176. Drive: string;
  1177. SaveCursor: TCursor;
  1178. begin
  1179. Result := inherited CanExpand(Node);
  1180. Drive := GetDriveToNode(Node);
  1181. if Node.HasChildren then
  1182. begin
  1183. if (Node.Level = 0) and
  1184. (not GetDriveStatus(Drive).Scanned) and
  1185. DriveInfo.IsFixedDrive(Drive) then
  1186. begin
  1187. SubNode := Node.GetFirstChild;
  1188. if not Assigned(SubNode) then
  1189. begin
  1190. ScanDrive(Drive);
  1191. SubNode := Node.GetFirstChild;
  1192. Node.HasChildren := Assigned(SubNode);
  1193. Result := Node.HasChildren;
  1194. if not Assigned(GetDriveStatus(Drive).DiscMonitor) then
  1195. CreateWatchThread(Drive);
  1196. end;
  1197. end
  1198. else
  1199. begin
  1200. SaveCursor := Screen.Cursor;
  1201. Screen.Cursor := crHourGlass;
  1202. try
  1203. if (not TNodeData(Node.Data).Scanned) and DoScanDir(Node) then
  1204. begin
  1205. ReadSubDirs(Node);
  1206. end;
  1207. finally
  1208. Screen.Cursor := SaveCursor;
  1209. end;
  1210. end;
  1211. end;
  1212. end; {CanExpand}
  1213. procedure TDriveView.GetImageIndex(Node: TTreeNode);
  1214. begin
  1215. if TNodeData(Node.Data).IconEmpty then
  1216. SetImageIndex(Node);
  1217. inherited;
  1218. end; {GetImageIndex}
  1219. procedure TDriveView.Loaded;
  1220. begin
  1221. inherited;
  1222. {Create the drive nodes:}
  1223. RefreshRootNodes(dsDisplayName or dvdsFloppy);
  1224. {Set the initial directory:}
  1225. if (Length(FDirectory) > 0) and DirectoryExists(FDirectory) then
  1226. Directory := FDirectory;
  1227. FCreating := False;
  1228. end; {Loaded}
  1229. function TDriveView.CreateNode: TTreeNode;
  1230. begin
  1231. Result := TDriveTreeNode.Create(Items);
  1232. end;
  1233. procedure TDriveView.Delete(Node: TTreeNode);
  1234. var
  1235. NodeData: TNodeData;
  1236. begin
  1237. if Node = FPrevSelected then
  1238. FPrevSelected := nil;
  1239. NodeData := nil;
  1240. if Assigned(Node) and Assigned(Node.Data) then
  1241. NodeData := TNodeData(Node.Data);
  1242. Node.Data := nil;
  1243. inherited;
  1244. if Assigned(NodeData) and not (csRecreating in ControlState) then
  1245. begin
  1246. FSubDirReaderThread.Delete(Node);
  1247. if Assigned(NodeData.DelayedExclude) then
  1248. begin
  1249. CancelDelayedNode(Node);
  1250. FDelayedNodes.Delete(FDelayedNodes.IndexOfObject(Node));
  1251. UpdateDelayedNodeTimer;
  1252. end;
  1253. NodeData.Destroy;
  1254. end;
  1255. end; {OnDelete}
  1256. procedure TDriveView.KeyPress(var Key: Char);
  1257. begin
  1258. inherited;
  1259. if Assigned(Selected) then
  1260. begin
  1261. if Pos(Key, coInvalidDosChars) <> 0 then
  1262. begin
  1263. Beep;
  1264. Key := #0;
  1265. end;
  1266. end;
  1267. end; {KeyPress}
  1268. function TDriveView.CanChange(Node: TTreeNode): Boolean;
  1269. var
  1270. Path: string;
  1271. Drive: string;
  1272. begin
  1273. Result := inherited CanChange(Node);
  1274. if not Reading and not (csRecreating in ControlState) then
  1275. begin
  1276. if Result and Assigned(Node) then
  1277. begin
  1278. Path := NodePathName(Node);
  1279. if Path <> FLastDir then
  1280. begin
  1281. Drive := DriveInfo.GetDriveKey(Path);
  1282. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  1283. if not DriveInfo.Get(Drive).DriveReady then
  1284. begin
  1285. MessageDlg(Format(SDriveNotReady, [Drive]), mtError, [mbOK], 0);
  1286. Result := False;
  1287. end
  1288. else
  1289. try
  1290. CheckCanOpenDirectory(Path);
  1291. except
  1292. Application.HandleException(Self);
  1293. Result := False;
  1294. end;
  1295. end;
  1296. end;
  1297. if Result and (csDestroying in ComponentState) then
  1298. begin
  1299. Result := False;
  1300. end;
  1301. if Result and
  1302. (not FCanChange) and
  1303. Assigned(Node) and
  1304. Assigned(Node.Data) and
  1305. Assigned(Selected) and
  1306. Assigned(Selected.Data) then
  1307. begin
  1308. DropTarget := Node;
  1309. Result := False;
  1310. end
  1311. else
  1312. begin
  1313. DropTarget := nil;
  1314. end;
  1315. end;
  1316. end; {CanChange}
  1317. procedure TDriveView.Change(Node: TTreeNode);
  1318. var
  1319. Drive: string;
  1320. OldSerial: DWORD;
  1321. NewDir: string;
  1322. PrevDrive: string;
  1323. begin
  1324. if not Reading and not (csRecreating in ControlState) then
  1325. begin
  1326. if Assigned(Node) then
  1327. begin
  1328. NewDir := NodePathName(Node);
  1329. if NewDir <> FLastDir then
  1330. begin
  1331. Drive := DriveInfo.GetDriveKey(NewDir);
  1332. if Length(FLastDir) > 0 then
  1333. PrevDrive := DriveInfo.GetDriveKey(FLastDir)
  1334. else
  1335. PrevDrive := '';
  1336. FChangeFlag := True;
  1337. FLastDir := NewDir;
  1338. OldSerial := DriveInfo.Get(Drive).DriveSerial;
  1339. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  1340. with DriveInfo.Get(Drive) do
  1341. begin
  1342. if Assigned(FDirView) and (FDirView.Path <> NewDir) then
  1343. FDirView.Path := NewDir;
  1344. if DriveReady then
  1345. begin
  1346. if not DirectoryExists(NewDir) then
  1347. begin
  1348. ValidateDirectory(GetDriveStatus(Drive).RootNode);
  1349. Exit;
  1350. end;
  1351. GetDriveStatus(Drive).DefaultDir := IncludeTrailingBackslash(NewDir);
  1352. if PrevDrive <> Drive then
  1353. begin
  1354. if (PrevDrive <> '') and
  1355. (DriveInfo.Get(PrevDrive).DriveType = DRIVE_REMOVABLE) then
  1356. begin
  1357. TerminateWatchThread(PrevDrive);
  1358. end;
  1359. {Drive serial has changed or is missing: allways reread the drive:}
  1360. if (DriveSerial <> OldSerial) or (DriveSerial = 0) then
  1361. begin
  1362. if TNodeData(GetDriveStatus(Drive).RootNode.Data).Scanned then
  1363. ScanDrive(Drive);
  1364. end;
  1365. end;
  1366. StartWatchThread;
  1367. end
  1368. else {Drive not ready:}
  1369. begin
  1370. GetDriveStatus(Drive).RootNode.DeleteChildren;
  1371. GetDriveStatus(Drive).DefaultDir := EmptyStr;
  1372. end;
  1373. end;
  1374. end;
  1375. if (not Assigned(FPrevSelected)) or (not FPrevSelected.HasAsParent(Node)) then
  1376. Node.Expand(False);
  1377. FPrevSelected := Node;
  1378. ValidateCurrentDirectoryIfNotMonitoring;
  1379. end;
  1380. end;
  1381. inherited;
  1382. end; {Change}
  1383. procedure TDriveView.SetImageIndex(Node: TTreeNode);
  1384. var
  1385. FileInfo: TShFileInfo;
  1386. Drive, NodePath: string;
  1387. begin
  1388. if Assigned(Node) and TNodeData(Node.Data).IconEmpty then
  1389. begin
  1390. NodePath := NodePathName(Node);
  1391. Drive := DriveInfo.GetDriveKey(NodePath);
  1392. if Node.Level = 0 then
  1393. begin
  1394. with DriveInfo.Get(Drive) do
  1395. begin
  1396. if ImageIndex = 0 then
  1397. begin
  1398. DriveInfo.ReadDriveStatus(Drive, dsImageIndex);
  1399. Node.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1400. end
  1401. else Node.ImageIndex := ImageIndex;
  1402. Node.SelectedIndex := Node.ImageIndex;
  1403. end;
  1404. end
  1405. else
  1406. begin
  1407. if DriveInfo.Get(Drive).DriveType = DRIVE_REMOTE then
  1408. begin
  1409. Node.ImageIndex := StdDirIcon;
  1410. Node.SelectedIndex := StdDirSelIcon;
  1411. end
  1412. else
  1413. begin
  1414. try
  1415. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1416. SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  1417. if (FileInfo.iIcon < Images.Count) and (FileInfo.iIcon > 0) then
  1418. begin
  1419. Node.ImageIndex := FileInfo.iIcon;
  1420. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1421. SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_OPENICON);
  1422. Node.SelectedIndex := FileInfo.iIcon;
  1423. end
  1424. else
  1425. begin
  1426. Node.ImageIndex := StdDirIcon;
  1427. Node.SelectedIndex := StdDirSelIcon;
  1428. end;
  1429. except
  1430. Node.ImageIndex := StdDirIcon;
  1431. Node.SelectedIndex := StdDirSelIcon;
  1432. end;
  1433. end;
  1434. end;
  1435. end; {IconEmpty}
  1436. TNodeData(Node.Data).IconEmpty := False;
  1437. end; {SetImageIndex}
  1438. function TDriveView.GetDriveText(Drive: string): string;
  1439. begin
  1440. if FShowVolLabel and (Length(DriveInfo.GetPrettyName(Drive)) > 0) then
  1441. begin
  1442. case FVolDisplayStyle of
  1443. doPrettyName: Result := DriveInfo.GetPrettyName(Drive);
  1444. doDisplayName: Result := DriveInfo.GetDisplayName(Drive);
  1445. end; {Case}
  1446. end
  1447. else
  1448. begin
  1449. Result := DriveInfo.GetSimpleName(Drive);
  1450. end;
  1451. end; {GetDriveText}
  1452. function CompareDrive(List: TStringList; Index1, Index2: Integer): Integer;
  1453. var
  1454. Drive1, Drive2: string;
  1455. RealDrive1, RealDrive2: Boolean;
  1456. begin
  1457. Drive1 := List[Index1];
  1458. Drive2 := List[Index2];
  1459. RealDrive1 := DriveInfo.IsRealDrive(Drive1);
  1460. RealDrive2 := DriveInfo.IsRealDrive(Drive2);
  1461. if RealDrive1 = RealDrive2 then
  1462. begin
  1463. Result := CompareText(Drive1, Drive2);
  1464. end
  1465. else
  1466. if RealDrive1 and (not RealDrive2) then
  1467. begin
  1468. Result := -1;
  1469. end
  1470. else
  1471. begin
  1472. Result := 1;
  1473. end;
  1474. end;
  1475. function TDriveView.GetDrives: TStrings;
  1476. var
  1477. DriveStatusPair: TDriveStatusPair;
  1478. Drives: TStringList;
  1479. begin
  1480. Drives := TStringList.Create;
  1481. { We could iterate only .Keys here, but that crashes IDE for some reason }
  1482. for DriveStatusPair in FDriveStatus do
  1483. begin
  1484. Drives.Add(DriveStatusPair.Key);
  1485. end;
  1486. Drives.CustomSort(CompareDrive);
  1487. Result := Drives;
  1488. end;
  1489. procedure TDriveView.DriveRemoved(Drive: string);
  1490. var
  1491. NewDrive: Char;
  1492. begin
  1493. if (Directory <> '') and (Directory[1] = Drive) then
  1494. begin
  1495. if DriveInfo.IsRealDrive(Drive) then NewDrive := Drive[1]
  1496. else NewDrive := SystemDrive;
  1497. repeat
  1498. if NewDrive < SystemDrive then NewDrive := SystemDrive
  1499. else
  1500. if NewDrive = SystemDrive then NewDrive := LastDrive
  1501. else Dec(NewDrive);
  1502. DriveInfo.ReadDriveStatus(NewDrive, dsSize or dsImageIndex);
  1503. if NewDrive = Drive then
  1504. begin
  1505. Break;
  1506. end;
  1507. if DriveInfo.Get(NewDrive).Valid and DriveInfo.Get(NewDrive).DriveReady and Assigned(GetDriveStatus(NewDrive).RootNode) then
  1508. begin
  1509. Directory := NodePathName(GetDriveStatus(NewDrive).RootNode);
  1510. break;
  1511. end;
  1512. until False;
  1513. if not Assigned(Selected) then
  1514. begin
  1515. Directory := NodePathName(GetDriveStatus(SystemDrive).RootNode);
  1516. end;
  1517. end;
  1518. end;
  1519. procedure TDriveView.RefreshRootNodes(dsFlags: Integer);
  1520. var
  1521. Drives: TStrings;
  1522. NewText: string;
  1523. SaveCursor: TCursor;
  1524. WasValid: Boolean;
  1525. NodeData: TNodeData;
  1526. DriveStatus: TDriveStatus;
  1527. NextDriveNode: TTreeNode;
  1528. Index: Integer;
  1529. Drive: string;
  1530. begin
  1531. SaveCursor := Screen.Cursor;
  1532. Screen.Cursor := crHourGlass;
  1533. Drives := nil;
  1534. try
  1535. Drives := GetDrives;
  1536. NextDriveNode := nil;
  1537. for Index := Drives.Count - 1 downto 0 do
  1538. begin
  1539. Drive := Drives[Index];
  1540. DriveStatus := GetDriveStatus(Drive);
  1541. if ((dsFlags and dvdsFloppy) <> 0) or DriveInfo.IsFixedDrive(Drive) then
  1542. begin
  1543. with DriveInfo.Get(Drive) do
  1544. begin
  1545. WasValid := Assigned(DriveStatus.RootNode);
  1546. end;
  1547. if ((dsFlags and dvdsReReadAllways) = 0) and
  1548. (Length(DriveInfo.Get(Drive).DisplayName) > 0) then
  1549. dsFlags := dsFlags and (not dsDisplayName);
  1550. DriveInfo.ReadDriveStatus(Drive, dsFlags);
  1551. with DriveInfo.Get(Drive), DriveStatus do
  1552. begin
  1553. if Valid then
  1554. begin
  1555. if not WasValid then
  1556. {New drive has arrived: insert new rootnode:}
  1557. begin
  1558. { Create root directory node }
  1559. NodeData := TNodeData.Create;
  1560. NodeData.DirName := DriveInfo.GetDriveRoot(Drive);
  1561. if Assigned(NextDriveNode) then
  1562. RootNode := Items.InsertObject(NextDriveNode, '', NodeData)
  1563. else
  1564. RootNode := Items.AddObject(nil, '', NodeData);
  1565. RootNode.Text := GetDisplayName(RootNode);
  1566. RootNode.HasChildren := True;
  1567. Scanned := False;
  1568. Verified := False;
  1569. end
  1570. else
  1571. if RootNode.ImageIndex <> DriveInfo.Get(Drive).ImageIndex then
  1572. begin {WasValid = True}
  1573. RootNode.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1574. RootNode.SelectedIndex := DriveInfo.Get(Drive).ImageIndex;
  1575. end;
  1576. if Assigned(RootNode) then
  1577. begin
  1578. NewText := GetDisplayName(RootNode);
  1579. if RootNode.Text <> NewText then
  1580. RootNode.Text := NewText;
  1581. end;
  1582. end
  1583. else
  1584. if WasValid then
  1585. {Drive has been removed => delete rootnode:}
  1586. begin
  1587. DriveRemoved(Drive);
  1588. Scanned := False;
  1589. Verified := False;
  1590. RootNode.Delete;
  1591. RootNode := nil;
  1592. end;
  1593. end;
  1594. end;
  1595. if Assigned(DriveStatus.RootNode) then
  1596. NextDriveNode := DriveStatus.RootNode;
  1597. end;
  1598. finally
  1599. Screen.Cursor := SaveCursor;
  1600. Drives.Free;
  1601. end;
  1602. end; {RefreshRootNodes}
  1603. procedure TDriveView.AddChildNode(ParentNode: TTreeNode; ParentPath: string; SRec: TSearchRec);
  1604. var
  1605. NewNode: TTreeNode;
  1606. NodeData: TNodeData;
  1607. begin
  1608. NodeData := TNodeData.Create;
  1609. NodeData.Attr := SRec.Attr;
  1610. NodeData.DirName := SRec.Name;
  1611. NodeData.FIsRecycleBin :=
  1612. (SRec.Attr and faSysFile <> 0) and
  1613. (ParentNode.Level = 0) and
  1614. (SameText(SRec.Name, 'RECYCLED') or
  1615. SameText(SRec.Name, 'RECYCLER') or
  1616. SameText(SRec.Name, '$RECYCLE.BIN'));
  1617. NodeData.Scanned := False;
  1618. NewNode := Self.Items.AddChildObject(ParentNode, '', NodeData);
  1619. NewNode.Text := GetDisplayName(NewNode);
  1620. NewNode.HasChildren := True;
  1621. if GetDriveTypeToNode(ParentNode) <> DRIVE_REMOTE then
  1622. FSubDirReaderThread.Add(NewNode, IncludeTrailingBackslash(ParentPath) + SRec.Name);
  1623. end; {AddChildNode}
  1624. function TDriveView.GetDriveStatus(Drive: string): TDriveStatus;
  1625. begin
  1626. if not FDriveStatus.TryGetValue(Drive, Result) then
  1627. begin
  1628. Result := CreateDriveStatus;
  1629. FDriveStatus.Add(Drive, Result);
  1630. RefreshRootNodes(dsAll or dvdsRereadAllways);
  1631. DoRefreshDrives(False);
  1632. end;
  1633. end; {GetDriveStatus}
  1634. function TDriveView.DoScanDir(FromNode: TTreeNode): Boolean;
  1635. begin
  1636. Result := not TNodeData(FromNode.Data).IsRecycleBin;
  1637. end; {DoScanDir}
  1638. function TDriveView.DirAttrMask: Integer;
  1639. begin
  1640. Result := faDirectory or faSysFile;
  1641. if ShowHiddenDirs then
  1642. Result := Result or faHidden;
  1643. end;
  1644. procedure TDriveView.ScanDrive(Drive: string);
  1645. begin {ScanDrive}
  1646. with Self.Items do
  1647. begin
  1648. ValidateDirectory(FindNodeToPath(DriveInfo.GetDriveRoot(Drive)));
  1649. GetDriveStatus(Drive).Scanned := True;
  1650. GetDriveStatus(Drive).Verified := False;
  1651. end;
  1652. end; {ScanDrive}
  1653. function TDriveView.DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  1654. var
  1655. SelectionHierarchy: array of TTreeNode;
  1656. SelectionHierarchyHeight: Integer;
  1657. function SearchSubDirs(ParentNode: TTreeNode; Path: string; Level: Integer): TTreeNode; forward;
  1658. function ExtractFirstName(S: string): string;
  1659. var
  1660. I: Integer;
  1661. begin
  1662. I := Pos('\', S);
  1663. if I = 0 then
  1664. I := Length(S);
  1665. Result := System.Copy(S, 1, I);
  1666. end;
  1667. function DoSearchSubDirs(ParentNode: TTreeNode; Path: string; Level: Integer): TTreeNode;
  1668. var
  1669. Node: TTreeNode;
  1670. Dir: string;
  1671. begin
  1672. {Extract first directory from path:}
  1673. Dir := ExtractFirstName(Path);
  1674. System.Delete(Path, 1, Length(Dir));
  1675. if Dir[Length(Dir)] = '\' then
  1676. SetLength(Dir, Pred(Length(Dir)));
  1677. // Optimization. Avoid iterating possibly thousands of nodes,
  1678. // when the node we are looking for is the selected node or its ancestor.
  1679. // This is often the case, when navigating under node that has lot of sibligs.
  1680. // Typically, when navigating in user's profile folder, and there are many [thousands] other user profile folders.
  1681. if (SelectionHierarchyHeight > 0) and
  1682. // Change of selection might indicate that the tree was rebuilt meanwhile and
  1683. // the references in SelectionHierarchy might not be valid anymore
  1684. (Selected = SelectionHierarchy[SelectionHierarchyHeight - 1]) and
  1685. (Level < SelectionHierarchyHeight) and
  1686. (Uppercase(GetDirName(SelectionHierarchy[Level])) = Dir) then
  1687. begin
  1688. Result := SelectionHierarchy[Level];
  1689. end
  1690. else
  1691. begin
  1692. Node := ParentNode.GetFirstChild;
  1693. if (not Assigned(Node)) and (not ExistingOnly) then
  1694. begin
  1695. ValidateDirectoryEx(ParentNode, rsRecursiveExisting, True);
  1696. Node := ParentNode.GetFirstChild;
  1697. end;
  1698. Result := nil;
  1699. while (not Assigned(Result)) and Assigned(Node) do
  1700. begin
  1701. if UpperCase(GetDirName(Node)) = Dir then
  1702. begin
  1703. Result := Node;
  1704. end
  1705. else
  1706. begin
  1707. Node := ParentNode.GetNextChild(Node);
  1708. end;
  1709. end;
  1710. end;
  1711. if Assigned(Result) and (Length(Path) > 0) then
  1712. begin
  1713. Result := SearchSubDirs(Result, Path, Level + 1);
  1714. end;
  1715. end;
  1716. function SearchSubDirs(ParentNode: TTreeNode; Path: string; Level: Integer): TTreeNode;
  1717. var
  1718. ParentPath, SubPath: string;
  1719. SRec: TSearchRec;
  1720. ParentNodeData: TNodeData;
  1721. begin
  1722. Result := nil;
  1723. if Length(Path) > 0 then
  1724. begin
  1725. ParentNodeData := TNodeData(ParentNode.Data);
  1726. if (not ParentNodeData.Scanned) and (not ExistingOnly) then
  1727. begin
  1728. ReadSubDirs(ParentNode);
  1729. end;
  1730. // Factored out of DoSearchSubDirs is remnant of Bug 956 superceded by Bug 1320
  1731. Result := DoSearchSubDirs(ParentNode, Path, Level);
  1732. ParentPath := NodePath(ParentNode);
  1733. if (not Assigned(Result)) and
  1734. DirectoryExists(IncludeTrailingBackslash(ParentPath) + Path) and
  1735. (not ExistingOnly) then
  1736. begin
  1737. SubPath := IncludeTrailingBackslash(ParentPath) + ExcludeTrailingBackslash(ExtractFirstName(Path));
  1738. if FindFirstSubDir(SubPath, SRec) then
  1739. begin
  1740. AddChildNode(ParentNode, ParentPath, SRec);
  1741. if Assigned(ParentNodeData.DelayedExclude) then
  1742. ParentNodeData.DelayedExclude.Add(SRec.Name);
  1743. SortChildren(ParentNode, False);
  1744. FindClose(SRec);
  1745. end;
  1746. Result := DoSearchSubDirs(ParentNode, Path, Level);
  1747. end;
  1748. end;
  1749. end; {SearchSubDirs}
  1750. var
  1751. Drive: string;
  1752. P, I: Integer;
  1753. RootNode, Node: TTreeNode;
  1754. begin {FindNodeToPath}
  1755. Result := nil;
  1756. if Length(Path) < 3 then
  1757. Exit;
  1758. // Particularly when used by TDirView to delegate browsing to
  1759. // hidden drive view, the handle may not be created
  1760. HandleNeeded;
  1761. Drive := DriveInfo.GetDriveKey(Path);
  1762. if (not Assigned(GetDriveStatus(Drive).RootNode)) and
  1763. // hidden or possibly recently un-hidden by other drive view (refresh is pending)
  1764. (DriveInfo.Get(Drive).Valid or DriveInfo.Get(Drive).ValidButHiddenByDrivePolicy) then
  1765. begin
  1766. if DriveInfo.Get(Drive).ValidButHiddenByDrivePolicy then
  1767. DriveInfo.OverrideDrivePolicy(Drive);
  1768. if DriveInfo.Get(Drive).Valid then
  1769. begin
  1770. CancelDriveRefresh; // cancel a possible pending refresh (see the previous comment)
  1771. RefreshRootNodes(dsAll or dvdsRereadAllways); // overkill and is likely already called by GetDriveStatus
  1772. DoRefreshDrives(False);
  1773. end;
  1774. end;
  1775. if Assigned(GetDriveStatus(Drive).RootNode) then
  1776. begin
  1777. if DriveInfo.IsRealDrive(Drive) then
  1778. begin
  1779. System.Delete(Path, 1, 3);
  1780. end
  1781. else
  1782. if IsUncPath(Path) then
  1783. begin
  1784. System.Delete(Path, 1, 2);
  1785. P := Pos('\', Path);
  1786. if P = 0 then
  1787. begin
  1788. Path := '';
  1789. end
  1790. else
  1791. begin
  1792. System.Delete(Path, 1, P);
  1793. P := Pos('\', Path);
  1794. if P = 0 then
  1795. begin
  1796. Path := '';
  1797. end
  1798. else
  1799. begin
  1800. System.Delete(Path, 1, P);
  1801. end;
  1802. end;
  1803. end
  1804. else
  1805. begin
  1806. raise EConvertError.Create(Format(ErrorInvalidDrive, [Path]))
  1807. end;
  1808. if Length(Path) > 0 then
  1809. begin
  1810. if (not GetDriveStatus(Drive).Scanned) and (not ExistingOnly) then
  1811. begin
  1812. ScanDrive(Drive);
  1813. end;
  1814. Node := Selected;
  1815. RootNode := GetDriveStatus(Drive).RootNode;
  1816. if not Assigned(Node) then
  1817. begin
  1818. SelectionHierarchyHeight := 0;
  1819. end
  1820. else
  1821. begin
  1822. SelectionHierarchyHeight := Node.Level + 1;
  1823. SetLength(SelectionHierarchy, SelectionHierarchyHeight);
  1824. for I := SelectionHierarchyHeight - 1 downto 0 do
  1825. begin
  1826. SelectionHierarchy[I] := Node;
  1827. Node := Node.Parent;
  1828. end;
  1829. Assert(Selected = SelectionHierarchy[SelectionHierarchyHeight - 1]);
  1830. Assert(RootNode = SelectionHierarchy[0]);
  1831. end;
  1832. Result := SearchSubDirs(RootNode, UpperCase(Path), 1);
  1833. end
  1834. else Result := GetDriveStatus(Drive).RootNode;
  1835. end;
  1836. end; {FindNodetoPath}
  1837. function TDriveView.FindNodeToPath(Path: string): TTreeNode;
  1838. begin
  1839. Result := DoFindNodeToPath(Path, False);
  1840. end;
  1841. function TDriveView.TryFindNodeToPath(Path: string): TTreeNode;
  1842. begin
  1843. Result := DoFindNodeToPath(Path, True);
  1844. end;
  1845. function TDriveView.GetSubDir(var SRec: TSearchRec): Boolean;
  1846. begin
  1847. Result := True;
  1848. while Result and
  1849. ((SRec.Name = '.' ) or
  1850. (SRec.Name = '..') or
  1851. ((SRec.Attr and faDirectory) = 0)) do
  1852. begin
  1853. if FindNext(SRec) <> 0 then
  1854. begin
  1855. Result := False;
  1856. end;
  1857. end;
  1858. end;
  1859. function TDriveView.FindFirstSubDir(Path: string; var SRec: TSearchRec): Boolean;
  1860. begin
  1861. Result := (FindFirstEx(ApiPath(Path), DirAttrMask, SRec, FIND_FIRST_EX_LARGE_FETCH_PAS, FindExSearchLimitToDirectories) = 0);
  1862. if Result then
  1863. begin
  1864. Result := GetSubDir(SRec);
  1865. if not Result then FindClose(SRec);
  1866. end;
  1867. end;
  1868. function TDriveView.FindNextSubDir(var SRec: TSearchRec): Boolean;
  1869. begin
  1870. Result := (FindNext(SRec) = 0) and GetSubDir(SRec);
  1871. end;
  1872. function TDriveView.ReadSubDirsBatch(Node: TTreeNode; var SRec: TSearchRec; CheckInterval, Limit: Integer): Boolean;
  1873. var
  1874. Start: TDateTime;
  1875. Cont: Boolean;
  1876. Path: string;
  1877. Count: Integer;
  1878. DelayedExclude: TStringList;
  1879. begin
  1880. Start := Now;
  1881. Path := NodePath(Node);
  1882. Result := True;
  1883. Count := 0;
  1884. DelayedExclude := TNodeData(Node.Data).DelayedExclude;
  1885. // At least from SetDirectory > DoFindNodeToPath and CanExpand, this is not called within BeginUpdate/EndUpdate block.
  1886. // But in any case, adding it here makes expanding (which calls CanExpand) noticeably slower, when there are lot of nodes,
  1887. // because EndUpdate triggers TVN_GETDISPINFO for all nodes in the tree.
  1888. repeat
  1889. if (not Assigned(DelayedExclude)) or
  1890. (DelayedExclude.IndexOf(SRec.Name) < 0) then
  1891. begin
  1892. AddChildNode(Node, Path, SRec);
  1893. Inc(Count);
  1894. end;
  1895. Cont := FindNextSubDir(SRec);
  1896. // There are two other directory reading loops, where this is not checked
  1897. if Cont and
  1898. ((Count mod CheckInterval) = 0) and
  1899. (Limit > 0) and
  1900. (MilliSecondsBetween(Now, Start) > Limit) then
  1901. begin
  1902. Result := False;
  1903. Cont := False;
  1904. end
  1905. until not Cont;
  1906. if Result then
  1907. FindClose(Srec);
  1908. end;
  1909. procedure TDriveView.DelayedNodeTimer(Sender: TObject);
  1910. var
  1911. Node: TTreeNode;
  1912. NodeData: TNodeData;
  1913. begin
  1914. Assert(FDelayedNodes.Count > 0);
  1915. if FDelayedNodes.Count > 0 then
  1916. begin
  1917. // Control was recreated
  1918. if not Assigned(FDelayedNodes.Objects[0]) then
  1919. begin
  1920. FDelayedNodes.Objects[0] := TryFindNodeToPath(FDelayedNodes.Strings[0]);
  1921. end;
  1922. Node := TTreeNode(FDelayedNodes.Objects[0]);
  1923. if not Assigned(Node) then
  1924. begin
  1925. FDelayedNodes.Delete(0);
  1926. end
  1927. else
  1928. begin
  1929. NodeData := TNodeData(Node.Data);
  1930. if ReadSubDirsBatch(Node, NodeData.DelayedSrec, 10, 50) then
  1931. begin
  1932. FreeAndNil(NodeData.DelayedExclude);
  1933. FDelayedNodes.Delete(0);
  1934. SortChildren(Node, False);
  1935. end;
  1936. end;
  1937. end;
  1938. UpdateDelayedNodeTimer;
  1939. end;
  1940. procedure TDriveView.UpdateDelayedNodeTimer;
  1941. begin
  1942. FDelayedNodeTimer.Enabled := HandleAllocated and (FDelayedNodes.Count > 0);
  1943. end;
  1944. procedure TDriveView.ReadSubDirs(Node: TTreeNode);
  1945. var
  1946. SRec: TSearchRec;
  1947. NodeData: TNodeData;
  1948. Path: string;
  1949. CheckInterval, Limit: Integer;
  1950. begin
  1951. NodeData := TNodeData(Node.Data);
  1952. Path := NodePath(Node);
  1953. if not FindFirstSubDir(IncludeTrailingBackslash(Path) + '*.*', SRec) then
  1954. begin
  1955. Node.HasChildren := False;
  1956. end
  1957. else
  1958. begin
  1959. CheckInterval := 100;
  1960. Limit := DriveViewLoadingTooLongLimit * 1000;
  1961. if not Showing then
  1962. begin
  1963. Limit := Limit div 10;
  1964. CheckInterval := CheckInterval div 10;
  1965. end;
  1966. if not ReadSubDirsBatch(Node, SRec, CheckInterval, Limit) then
  1967. begin
  1968. NodeData.DelayedSrec := SRec;
  1969. NodeData.DelayedExclude := TStringList.Create;
  1970. NodeData.DelayedExclude.CaseSensitive := False;
  1971. NodeData.DelayedExclude.Sorted := True;
  1972. FDelayedNodes.AddObject(Path, Node);
  1973. Assert(FDelayedNodes.Count < 20); // if more, something went likely wrong
  1974. UpdateDelayedNodeTimer;
  1975. end;
  1976. SortChildren(Node, False);
  1977. end;
  1978. NodeData.Scanned := True;
  1979. Application.ProcessMessages;
  1980. end; {ReadSubDirs}
  1981. procedure TDriveView.CancelDelayedNode(Node: TTreeNode);
  1982. var
  1983. NodeData: TNodeData;
  1984. begin
  1985. NodeData := TNodeData(Node.Data);
  1986. FindClose(NodeData.DelayedSrec);
  1987. FreeAndNil(NodeData.DelayedExclude);
  1988. end;
  1989. procedure TDriveView.DeleteNode(Node: TTreeNode);
  1990. var
  1991. ValidNode: TTreeNode;
  1992. begin
  1993. if Assigned(Selected) and Assigned(Node.Parent) and
  1994. ((Selected = Node) or Selected.HasAsParent(Node)) then
  1995. begin
  1996. ValidNode := Node.Parent;
  1997. while (not DirectoryExists(NodePathName(ValidNode))) and Assigned(ValidNode.Parent) do
  1998. ValidNode := ValidNode.Parent;
  1999. Selected := ValidNode;
  2000. end;
  2001. if DropTarget = Node then
  2002. DropTarget := nil;
  2003. Node.Delete;
  2004. end;
  2005. function TDriveView.CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  2006. var
  2007. WorkNode: TTreeNode;
  2008. DelNode: TTreeNode;
  2009. SRec: TSearchRec;
  2010. SrecList: TStringList;
  2011. SubDirList: TStringList;
  2012. R: Boolean;
  2013. Index: Integer;
  2014. NewDirFound: Boolean;
  2015. ParentDir: string;
  2016. NodeData: TNodeData;
  2017. ScanDirInfo: PScanDirInfo;
  2018. begin {CallBackValidateDir}
  2019. Result := True;
  2020. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  2021. Exit;
  2022. NewDirFound := False;
  2023. ScanDirInfo := PScanDirInfo(Data);
  2024. {Check, if directory still exists: (but not with root directory) }
  2025. if Assigned(Node.Parent) and (ScanDirInfo^.StartNode = Node) then
  2026. if not DirectoryExists(NodePathName(Node)) then
  2027. begin
  2028. DeleteNode(Node);
  2029. Node := nil;
  2030. Exit;
  2031. end;
  2032. WorkNode := Node.GetFirstChild;
  2033. NodeData := TNodeData(Node.Data);
  2034. if NodeData.Scanned and Assigned(WorkNode) then
  2035. {if node was already scanned: check wether the existing subnodes are still alive
  2036. and add all new subdirectories as subnodes:}
  2037. begin
  2038. if DoScanDir(Node) then
  2039. begin
  2040. ParentDir := IncludeTrailingBackslash(NodePath(Node));
  2041. {Build list of existing subnodes:}
  2042. SubDirList := TStringList.Create;
  2043. SubDirList.CaseSensitive := True; // We want to reflect changes in subfolder name case
  2044. while Assigned(WorkNode) do
  2045. begin
  2046. SubDirList.Add(TNodeData(WorkNode.Data).DirName);
  2047. WorkNode := Node.GetNextChild(WorkNode);
  2048. end;
  2049. // Nodes are sorted using natural sorting, while TStringList.Find uses simple sorting
  2050. SubDirList.Sort;
  2051. SRecList := TStringList.Create;
  2052. SRecList.CaseSensitive := True;
  2053. R := FindFirstSubDir(ParentDir + '*.*', SRec);
  2054. while R do
  2055. begin
  2056. SrecList.Add(Srec.Name);
  2057. if not SubDirList.Find(Srec.Name, Index) then
  2058. {Subnode does not exists: add it:}
  2059. begin
  2060. AddChildNode(Node, ParentDir, SRec);
  2061. NewDirFound := True;
  2062. end;
  2063. R := FindNextSubDir(Srec);
  2064. end;
  2065. FindClose(Srec);
  2066. Sreclist.Sort;
  2067. {Remove not existing subnodes:}
  2068. WorkNode := Node.GetFirstChild;
  2069. while Assigned(WorkNode) do
  2070. begin
  2071. if not Assigned(WorkNode.Data) or
  2072. not SrecList.Find(TNodeData(WorkNode.Data).DirName, Index) then
  2073. begin
  2074. DelNode := WorkNode;
  2075. WorkNode := Node.GetNextChild(WorkNode);
  2076. DeleteNode(DelNode);
  2077. end
  2078. else
  2079. begin
  2080. if (SrecList[Index] <> TNodeData(WorkNode.Data).DirName) then
  2081. begin
  2082. {Case of directory letters has changed:}
  2083. TNodeData(WorkNode.Data).DirName := SrecList[Index];
  2084. WorkNode.Text := SrecList[Index];
  2085. end;
  2086. WorkNode := Node.GetNextChild(WorkNode);
  2087. end;
  2088. end;
  2089. SrecList.Free;
  2090. SubDirList.Free;
  2091. {Sort subnodes:}
  2092. if NewDirFound then
  2093. SortChildren(Node, False);
  2094. end;
  2095. end
  2096. else
  2097. {Node was not already scanned:}
  2098. if (ScanDirInfo^.SearchNewDirs or
  2099. NodeData.Scanned or
  2100. (Node = ScanDirInfo^.StartNode)) and
  2101. DoScanDir(Node) then
  2102. begin
  2103. ReadSubDirs(Node);
  2104. end;
  2105. end; {CallBackValidateDir}
  2106. procedure TDriveView.RebuildTree;
  2107. var
  2108. Drive: string;
  2109. begin
  2110. for Drive in FDriveStatus.Keys do
  2111. with GetDriveStatus(Drive) do
  2112. if Assigned(RootNode) and Scanned then
  2113. ValidateDirectory(RootNode);
  2114. end;
  2115. procedure TDriveView.ValidateCurrentDirectoryIfNotMonitoring;
  2116. begin
  2117. if Assigned(Selected) and
  2118. not Assigned(GetDriveStatus(GetDriveToNode(Selected)).DiscMonitor) then
  2119. begin
  2120. ValidateDirectory(Selected);
  2121. end;
  2122. end;
  2123. procedure TDriveView.ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  2124. NewDirs: Boolean);
  2125. var
  2126. Info: PScanDirInfo;
  2127. SelDir: string;
  2128. SaveCursor: TCursor;
  2129. RestartWatchThread: Boolean;
  2130. SaveCanChange: Boolean;
  2131. CurrentPath: string;
  2132. Drive: string;
  2133. begin
  2134. if Assigned(Node) and Assigned(Node.Data) and
  2135. (not FValidateFlag) and DoScanDir(Node) then
  2136. begin
  2137. SelDir := Directory;
  2138. SaveCursor := Screen.Cursor;
  2139. if Self.Focused and (Screen.Cursor <> crHourGlass) then
  2140. Screen.Cursor := crHourGlass;
  2141. CurrentPath := NodePath(Node);
  2142. Drive := DriveInfo.GetDriveKey(CurrentPath);
  2143. if Node.Level = 0 then
  2144. GetDriveStatus(Drive).ChangeTimer.Enabled := False;
  2145. RestartWatchThread := WatchThreadActive;
  2146. try
  2147. if WatchThreadActive then
  2148. StopWatchThread;
  2149. FValidateFlag := True;
  2150. New(Info);
  2151. Info^.StartNode := Node;
  2152. Info^.SearchNewDirs := NewDirs;
  2153. Info^.DriveType := DriveInfo.Get(Drive).DriveType;
  2154. SaveCanChange := FCanChange;
  2155. FCanChange := True;
  2156. FChangeFlag := False;
  2157. Items.BeginUpdate;
  2158. try
  2159. IterateSubTree(Node, CallBackValidateDir, Recurse, coScanStartNode, Info);
  2160. finally
  2161. Items.EndUpdate;
  2162. end;
  2163. FValidateFlag := False;
  2164. if (not Assigned(Selected)) and (Length(SelDir) > 0) then
  2165. Directory := ExtractFileDrive(SelDir);
  2166. if (SelDir <> Directory) and (not FChangeFlag) then
  2167. Change(Selected);
  2168. FCanChange := SaveCanChange;
  2169. Dispose(Info);
  2170. finally
  2171. if RestartWatchThread and FWatchDirectory and not WatchThreadActive then
  2172. StartWatchThread;
  2173. if Screen.Cursor <> SaveCursor then
  2174. Screen.Cursor := SaveCursor;
  2175. end;
  2176. end;
  2177. end; {ValidateDirectoryEx}
  2178. function TDriveView.GetDriveTypeToNode(Node: TTreeNode): Integer;
  2179. begin
  2180. Assert(Assigned(Node));
  2181. Result := DriveInfo.Get(GetDriveToNode(Node)).DriveType;
  2182. end; {GetDriveTypeToNode}
  2183. procedure TDriveView.CreateWatchThread(Drive: string);
  2184. begin
  2185. if csDesigning in ComponentState then
  2186. Exit;
  2187. if (not Assigned(GetDriveStatus(Drive).DiscMonitor)) and
  2188. FWatchDirectory and
  2189. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) then
  2190. begin
  2191. with GetDriveStatus(Drive) do
  2192. begin
  2193. DiscMonitor := TDiscMonitor.Create(Self);
  2194. DiscMonitor.ChangeDelay := msThreadChangeDelay;
  2195. DiscMonitor.SubTree := True;
  2196. DiscMonitor.Filters := [moDirName];
  2197. DiscMonitor.OnChange := ChangeDetected;
  2198. DiscMonitor.OnInvalid := ChangeInvalid;
  2199. DiscMonitor.SetDirectory(DriveInfo.GetDriveRoot(Drive));
  2200. DiscMonitor.Open;
  2201. end;
  2202. UpdateDriveNotifications(Drive);
  2203. end;
  2204. end; {CreateWatchThread}
  2205. procedure TDriveView.SetWatchDirectory(Value: Boolean);
  2206. begin
  2207. if FWatchDirectory <> Value then
  2208. begin
  2209. FWatchDirectory := Value;
  2210. if (not (csDesigning in ComponentState)) and Value then
  2211. StartAllWatchThreads
  2212. else
  2213. StopAllWatchThreads;
  2214. end;
  2215. end; {SetAutoScan}
  2216. procedure TDriveView.SetDirView(Value: TDirView);
  2217. begin
  2218. if Assigned(FDirView) then
  2219. FDirView.DriveView := nil;
  2220. FDirView := Value;
  2221. if Assigned(FDirView) then
  2222. FDirView.DriveView := Self;
  2223. end; {SetDirView}
  2224. function TDriveView.NodeWatched(Node: TTreeNode): Boolean;
  2225. var
  2226. Drive: string;
  2227. begin
  2228. Drive := GetDriveToNode(Node);
  2229. Result := WatchThreadActive(Drive);
  2230. end; {NodeWatched}
  2231. procedure TDriveView.ChangeInvalid(Sender: TObject; const Directory: string;
  2232. const ErrorStr: string);
  2233. var
  2234. Drive: string;
  2235. begin
  2236. Drive := DriveInfo.GetDriveKey((Sender as TDiscMonitor).Directories[0]);
  2237. with GetDriveStatus(Drive) do
  2238. begin
  2239. DiscMonitor.Close;
  2240. end;
  2241. UpdateDriveNotifications(Drive);
  2242. end; {DirWatchChangeInvalid}
  2243. procedure TDriveView.ChangeDetected(Sender: TObject; const Directory: string;
  2244. var SubdirsChanged: Boolean);
  2245. var
  2246. DirChanged: string;
  2247. begin
  2248. if Sender is TDiscMonitor then
  2249. begin
  2250. DirChanged := (Sender as TDiscMonitor).Directories[0];
  2251. if Length(DirChanged) > 0 then
  2252. begin
  2253. with GetDriveStatus(DriveInfo.GetDriveKey(DirChanged)) do
  2254. begin
  2255. ChangeTimer.Interval := 0;
  2256. ChangeTimer.Interval := FChangeInterval;
  2257. ChangeTimer.Enabled := True;
  2258. end;
  2259. end;
  2260. end;
  2261. end; {DirWatchChangeDetected}
  2262. procedure TDriveView.ChangeTimerOnTimer(Sender: TObject);
  2263. var
  2264. DriveStatusPair: TDriveStatusPair;
  2265. begin
  2266. if (FChangeTimerSuspended = 0) and (Sender is TTimer) then
  2267. begin
  2268. for DriveStatusPair in FDriveStatus do
  2269. begin
  2270. if DriveStatusPair.Value.ChangeTimer = Sender then
  2271. begin
  2272. // Messages are processed during ValidateDirectory, so we may detect another change while
  2273. // updating the directory. Prevent the recursion.
  2274. // But retry the update afterwards (by reenabling the timer in ChangeDetected)
  2275. SuspendChangeTimer;
  2276. try
  2277. with DriveStatusPair.Value.ChangeTimer do
  2278. begin
  2279. Interval := 0;
  2280. Enabled := False;
  2281. end;
  2282. if Assigned(DriveStatusPair.Value.RootNode) then
  2283. begin
  2284. {Check also collapsed (invisible) subdirectories:}
  2285. ValidateDirectory(DriveStatusPair.Value.RootNode);
  2286. end;
  2287. finally
  2288. ResumeChangeTimer;
  2289. end;
  2290. end;
  2291. end;
  2292. end;
  2293. end; {ChangeTimerOnTimer}
  2294. procedure TDriveView.UpdateDriveNotifications(Drive: string);
  2295. var
  2296. NeedNotifications: Boolean;
  2297. Path: string;
  2298. DevBroadcastHandle: DEV_BROADCAST_HANDLE;
  2299. Size: Integer;
  2300. begin
  2301. if DriveInfo.IsFixedDrive(Drive) then
  2302. begin
  2303. with GetDriveStatus(Drive) do
  2304. begin
  2305. NeedNotifications :=
  2306. WatchThreadActive(Drive) and
  2307. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) and
  2308. DriveInfo.Get(Drive).DriveReady;
  2309. if NeedNotifications <> (DriveHandle <> INVALID_HANDLE_VALUE) then
  2310. begin
  2311. if NeedNotifications then
  2312. begin
  2313. Path := DriveInfo.GetDriveRoot(Drive);
  2314. DriveHandle :=
  2315. CreateFile(PChar(Path), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
  2316. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_ATTRIBUTE_NORMAL, 0);
  2317. if DriveHandle <> INVALID_HANDLE_VALUE then
  2318. begin
  2319. Size := SizeOf(DevBroadcastHandle);
  2320. ZeroMemory(@DevBroadcastHandle, Size);
  2321. DevBroadcastHandle.dbch_size := Size;
  2322. DevBroadcastHandle.dbch_devicetype := DBT_DEVTYP_HANDLE;
  2323. DevBroadcastHandle.dbch_handle := DriveHandle;
  2324. NotificationHandle :=
  2325. RegisterDeviceNotification(FInternalWindowHandle, @DevBroadcastHandle, DEVICE_NOTIFY_WINDOW_HANDLE);
  2326. if NotificationHandle = nil then
  2327. begin
  2328. CloseHandle(DriveHandle);
  2329. DriveHandle := INVALID_HANDLE_VALUE;
  2330. end;
  2331. end;
  2332. end
  2333. else
  2334. begin
  2335. UnregisterDeviceNotification(NotificationHandle);
  2336. NotificationHandle := nil;
  2337. CloseHandle(DriveHandle);
  2338. DriveHandle := INVALID_HANDLE_VALUE;
  2339. end;
  2340. end;
  2341. end;
  2342. end;
  2343. end;
  2344. procedure TDriveView.StartWatchThread;
  2345. var
  2346. Drive: string;
  2347. begin
  2348. if (csDesigning in ComponentState) or
  2349. not Assigned(Selected) or
  2350. not fWatchDirectory then Exit;
  2351. Drive := GetDriveToNode(Selected);
  2352. with GetDriveStatus(Drive) do
  2353. begin
  2354. if not Assigned(DiscMonitor) then
  2355. CreateWatchThread(Drive);
  2356. if Assigned(DiscMonitor) and not DiscMonitor.Enabled then
  2357. DiscMonitor.Enabled := True;
  2358. end;
  2359. UpdateDriveNotifications(Drive);
  2360. end; {StartWatchThread}
  2361. procedure TDriveView.StopWatchThread;
  2362. var
  2363. Drive: string;
  2364. begin
  2365. if Assigned(Selected) then
  2366. begin
  2367. Drive := GetDriveToNode(Selected);
  2368. with GetDriveStatus(Drive) do
  2369. if Assigned(DiscMonitor) then
  2370. DiscMonitor.Enabled := False;
  2371. UpdateDriveNotifications(Drive);
  2372. end;
  2373. end; {StopWatchThread}
  2374. procedure TDriveView.SuspendChangeTimer;
  2375. begin
  2376. Inc(FChangeTimerSuspended);
  2377. end;
  2378. procedure TDriveView.ResumeChangeTimer;
  2379. begin
  2380. Assert(FChangeTimerSuspended > 0);
  2381. Dec(FChangeTimerSuspended);
  2382. end;
  2383. procedure TDriveView.TerminateWatchThread(Drive: string);
  2384. begin
  2385. with GetDriveStatus(Drive) do
  2386. if Assigned(DiscMonitor) then
  2387. begin
  2388. DiscMonitor.Free;
  2389. DiscMonitor := nil;
  2390. end;
  2391. UpdateDriveNotifications(Drive);
  2392. end; {StopWatchThread}
  2393. procedure TDriveView.StartAllWatchThreads;
  2394. var
  2395. DriveStatusPair: TDriveStatusPair;
  2396. Drive: string;
  2397. begin
  2398. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2399. Exit;
  2400. for DriveStatusPair in FDriveStatus do
  2401. with DriveStatusPair.Value do
  2402. if Scanned then
  2403. begin
  2404. if not Assigned(DiscMonitor) then
  2405. CreateWatchThread(DriveStatusPair.Key);
  2406. if Assigned(DiscMonitor) and (not DiscMonitor.Active) then
  2407. begin
  2408. DiscMonitor.Open;
  2409. UpdateDriveNotifications(DriveStatusPair.Key);
  2410. end;
  2411. end;
  2412. if Assigned(Selected) then
  2413. begin
  2414. Drive := GetDriveToNode(Selected);
  2415. if not DriveInfo.IsFixedDrive(Drive) then
  2416. begin
  2417. StartWatchThread;
  2418. end;
  2419. end;
  2420. end; {StartAllWatchThreads}
  2421. procedure TDriveView.StopAllWatchThreads;
  2422. var
  2423. DriveStatusPair: TDriveStatusPair;
  2424. begin
  2425. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2426. Exit;
  2427. for DriveStatusPair in FDriveStatus do
  2428. with DriveStatusPair.Value do
  2429. begin
  2430. if Assigned(DiscMonitor) then
  2431. begin
  2432. DiscMonitor.Close;
  2433. UpdateDriveNotifications(DriveStatusPair.Key);
  2434. end;
  2435. end;
  2436. end; {StopAllWatchThreads}
  2437. function TDriveView.WatchThreadActive(Drive: string): Boolean;
  2438. begin
  2439. Result := FWatchDirectory and
  2440. Assigned(GetDriveStatus(Drive).DiscMonitor) and
  2441. GetDriveStatus(Drive).DiscMonitor.Active and
  2442. GetDriveStatus(Drive).DiscMonitor.Enabled;
  2443. end; {WatchThreadActive}
  2444. function TDriveView.WatchThreadActive: Boolean;
  2445. var
  2446. Drive: string;
  2447. begin
  2448. if not Assigned(Selected) then
  2449. begin
  2450. Result := False;
  2451. Exit;
  2452. end;
  2453. Drive := GetDriveToNode(Selected);
  2454. Result := WatchThreadActive(Drive);
  2455. end; {WatchThreadActive}
  2456. function TDriveView.FindPathNode(Path: string): TTreeNode;
  2457. var
  2458. PossiblyHiddenPath: string;
  2459. Attrs: Integer;
  2460. begin
  2461. if Assigned(FOnNeedHiddenDirectories) and
  2462. (not ShowHiddenDirs) and
  2463. DirectoryExistsFix(Path) then // do not even bother if the path does not exist
  2464. begin
  2465. PossiblyHiddenPath := ExcludeTrailingPathDelimiter(Path);
  2466. while (PossiblyHiddenPath <> '') and
  2467. (not IsRootPath(PossiblyHiddenPath)) do // Drives have hidden attribute
  2468. begin
  2469. Attrs := FileGetAttr(PossiblyHiddenPath, False);
  2470. if (Attrs and faHidden) = faHidden then
  2471. begin
  2472. if Assigned(FOnNeedHiddenDirectories) then
  2473. begin
  2474. FOnNeedHiddenDirectories(Self);
  2475. end;
  2476. Break;
  2477. end
  2478. else
  2479. begin
  2480. PossiblyHiddenPath := ExtractFileDir(PossiblyHiddenPath);
  2481. end;
  2482. end;
  2483. end;
  2484. {Find existing path or parent path of not existing path:}
  2485. repeat
  2486. Result := FindNodeToPath(Path);
  2487. if not Assigned(Result) then
  2488. Path := ExtractFilePath(ExcludeTrailingBackslash(Path));
  2489. until Assigned(Result) or (Length(Path) < 3);
  2490. end;
  2491. procedure TDriveView.SetDirectory(Value: string);
  2492. begin
  2493. Value := IncludeTrailingBackslash(Value);
  2494. FDirectory := Value;
  2495. inherited;
  2496. if Assigned(Selected) and (Selected.Level = 0) then
  2497. begin
  2498. if not GetDriveStatus(GetDriveToNode(Selected)).Scanned then
  2499. ScanDrive(GetDriveToNode(Selected));
  2500. end;
  2501. end; {SetDirectory}
  2502. function TDriveView.GetDirName(Node: TTreeNode): string;
  2503. begin
  2504. if Assigned(Node) and Assigned(Node.Data) then
  2505. Result := TNodeData(Node.Data).DirName
  2506. else
  2507. Result := '';
  2508. end; {GetDirName}
  2509. {GetDrive: returns the drive of the Node.}
  2510. function TDriveView.GetDriveToNode(Node: TTreeNode): string;
  2511. var
  2512. Path: string;
  2513. begin
  2514. if (not Assigned (Node)) or (not Assigned(Node.Data)) then
  2515. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDrive']));
  2516. Path := NodePath(Node);
  2517. Result := DriveInfo.GetDriveKey(Path);
  2518. end; {GetDrive}
  2519. {RootNode: returns the rootnode to the Node:}
  2520. function TDriveView.RootNode(Node: TTreeNode): TTreeNode;
  2521. begin
  2522. Result := Node;
  2523. if not Assigned(Node) then
  2524. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['RootNode']));
  2525. while Assigned(Result.Parent) do
  2526. Result := Result.Parent;
  2527. end; {RootNode}
  2528. function TDriveView.GetDisplayName(Node: TTreeNode): string;
  2529. begin
  2530. Result := '';
  2531. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  2532. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDisplayName']));
  2533. if Node.Level = 0 then Result := GetDriveText(GetDriveToNode(Node))
  2534. else
  2535. begin
  2536. Result := GetDirName(Node);
  2537. end;
  2538. end; {GetDisplayName}
  2539. procedure TDriveView.SetShowVolLabel(ShowIt: Boolean);
  2540. begin
  2541. if ShowIt = FShowVolLabel then
  2542. Exit;
  2543. FShowVolLabel := ShowIt;
  2544. RefreshRootNodes(dvdsFloppy);
  2545. end; {SetShowVolLabel}
  2546. procedure TDriveView.DisplayContextMenu(Node: TTreeNode; Point: TPoint);
  2547. var
  2548. Verb: string;
  2549. DirWatched: Boolean;
  2550. begin
  2551. DirWatched := NodeWatched(Node) and WatchThreadActive;
  2552. Assert(Node <> nil);
  2553. if Node <> Selected then
  2554. DropTarget := Node;
  2555. Verb := EmptyStr;
  2556. if Assigned(FOnDisplayContextMenu) then
  2557. FOnDisplayContextMenu(Self);
  2558. ShellDisplayContextMenu(FParentForm.Handle, Point, NodePathName(Node),
  2559. CanEdit(Node), Verb, False);
  2560. if Verb = shcRename then Node.EditText
  2561. else
  2562. if Verb = shcCut then
  2563. begin
  2564. LastClipBoardOperation := cboCut;
  2565. LastPathCut := NodePathName(Node);
  2566. end
  2567. else
  2568. if Verb = shcCopy then LastClipBoardOperation := cboCopy
  2569. else
  2570. if Verb = shcPaste then
  2571. PasteFromClipBoard(NodePathName(Node));
  2572. DropTarget := nil;
  2573. if not DirWatched then
  2574. ValidateDirectory(Node);
  2575. end; {DisplayContextMenu (2)}
  2576. procedure TDriveView.DisplayPropertiesMenu(Node: TTreeNode);
  2577. begin
  2578. Assert(Assigned(Node));
  2579. ShellExecuteContextCommand(FParentForm.Handle, shcProperties, NodePathName(Node));
  2580. end; {ContextMenu}
  2581. procedure TDriveView.SetSelected(Node: TTreeNode);
  2582. begin
  2583. if Node <> Selected then
  2584. begin
  2585. FChangeFlag := False;
  2586. FCanChange := True;
  2587. inherited Selected := Node;
  2588. if not FChangeFlag then
  2589. Change(Selected);
  2590. end;
  2591. end; {SetSelected}
  2592. {Called by TFileDeleteThread, when a file deletion was detected by the D&D receiving application:}
  2593. procedure TDriveView.SignalDirDelete(Sender: TObject; Files: TStringList);
  2594. begin
  2595. if Files.Count > 0 then
  2596. ValidateDirectory(FindNodeToPath(Files[0]));
  2597. end; {SignalDirDelete}
  2598. function TDriveView.DDSourceEffects: TDropEffectSet;
  2599. begin
  2600. if FDragNode.Level = 0 then
  2601. Result := [deLink]
  2602. else
  2603. Result := [deLink, deCopy, deMove];
  2604. end;
  2605. procedure TDriveView.DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer);
  2606. begin
  2607. if DropTarget = nil then Effect := DROPEFFECT_NONE
  2608. else
  2609. if (KeyState and (MK_CONTROL or MK_SHIFT) = 0) and (PreferredEffect = 0) then
  2610. begin
  2611. if FDragDrive <> '' then
  2612. begin
  2613. if FExeDrag and DriveInfo.IsFixedDrive(GetDriveToNode(DropTarget)) and DriveInfo.IsFixedDrive(FDragDrive) then
  2614. begin
  2615. Effect := DROPEFFECT_LINK;
  2616. end
  2617. else
  2618. if (Effect = DROPEFFECT_COPY) and
  2619. (SameText(FDragDrive, GetDriveToNode(DropTarget)) and
  2620. (FDragDropFilesEx.AvailableDropEffects and DROPEFFECT_MOVE <> 0)) then
  2621. begin
  2622. Effect := DROPEFFECT_MOVE;
  2623. end;
  2624. end;
  2625. end;
  2626. inherited;
  2627. end;
  2628. function TDriveView.DragCompleteFileList: Boolean;
  2629. begin
  2630. Result := (GetDriveTypeToNode(FDragNode) <> DRIVE_REMOVABLE);
  2631. end;
  2632. function TDriveView.DDExecute: TDragResult;
  2633. var
  2634. WatchThreadOK: Boolean;
  2635. DragParentPath: string;
  2636. DragPath: string;
  2637. begin
  2638. WatchThreadOK := WatchThreadActive;
  2639. Result := FDragDropFilesEx.Execute(nil);
  2640. if (Result = drMove) and (not WatchThreadOK) then
  2641. begin
  2642. DragPath := NodePathName(FDragNode);
  2643. if Assigned(FDragNode.Parent) then
  2644. DragParentPath := NodePathName(FDragNode.Parent)
  2645. else
  2646. DragParentPath := DragPath;
  2647. if (FDragNode.Level > 0) or (DragParentPath <> NodePathName(Selected.Parent)) then
  2648. begin
  2649. FDragNode := FindNodeToPath(DragPath);
  2650. if Assigned(FDragNode) then
  2651. begin
  2652. FDragFileList.Clear;
  2653. FDragFileList.Add(DragPath);
  2654. TFileDeleteThread.Create(FDragFileList, MaxWaitTimeOut, SignalDirDelete);
  2655. end;
  2656. end;
  2657. end;
  2658. end;
  2659. procedure TDriveView.PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer);
  2660. var
  2661. Index: Integer;
  2662. SourcePath: string;
  2663. SourceParentPath: string;
  2664. SourceIsDirectory: Boolean;
  2665. SaveCursor: TCursor;
  2666. SourceNode, TargetNode: TTreeNode;
  2667. TargetPath: string;
  2668. IsRecycleBin: Boolean;
  2669. begin
  2670. TargetPath := NodePathName(Node);
  2671. IsRecycleBin := NodeIsRecycleBin(Node);
  2672. if FDragDropFilesEx.FileList.Count = 0 then
  2673. Exit;
  2674. SaveCursor := Screen.Cursor;
  2675. Screen.Cursor := crHourGlass;
  2676. SourcePath := EmptyStr;
  2677. try
  2678. if (Effect = DROPEFFECT_COPY) or (Effect = DROPEFFECT_MOVE) then
  2679. begin
  2680. StopAllWatchThreads;
  2681. if Assigned(FDirView) then
  2682. FDirView.StopWatchThread;
  2683. if Assigned(DropSourceControl) and
  2684. (DropSourceControl is TDirView) and
  2685. (DropSourceControl <> FDirView) then
  2686. begin
  2687. TDirView(DropSourceControl).StopWatchThread;
  2688. end;
  2689. if DropFiles(
  2690. DragDropFilesEx, Effect, FFileOperator, TargetPath, false, IsRecycleBin, ConfirmDelete, ConfirmOverwrite, False,
  2691. Self, OnDDFileOperation, SourcePath, SourceIsDirectory) then
  2692. begin
  2693. if Assigned(FOnDDFileOperationExecuted) then
  2694. FOnDDFileOperationExecuted(Self, Effect, SourcePath, TargetPath);
  2695. end;
  2696. ClearDragFileList(FDragDropFilesEx.FileList);
  2697. // TDirView.PerformDragDropFileOperation validates the SourcePath and that actually seems correct
  2698. SourceParentPath := ExtractFilePath(ExcludeTrailingBackslash(SourcePath));
  2699. end
  2700. else
  2701. if Effect = DROPEFFECT_LINK then
  2702. { Create Link requested: }
  2703. begin
  2704. for Index := 0 to FDragDropFilesEx.FileList.Count - 1 do
  2705. begin
  2706. if not DropLink(PFDDListItem(FDragDropFilesEx.FileList[Index]), TargetPath) then
  2707. begin
  2708. DDError(DDCreateShortCutError);
  2709. end;
  2710. end;
  2711. end;
  2712. if Effect = DROPEFFECT_MOVE then
  2713. Items.BeginUpdate;
  2714. {Update source directory, if move-operation was performed:}
  2715. if ((Effect = DROPEFFECT_MOVE) or IsRecycleBin) then
  2716. begin
  2717. // See comment in corresponding operation in TDirView.PerformDragDropFileOperation
  2718. SourceNode := TryFindNodeToPath(SourceParentPath);
  2719. if Assigned(SourceNode) then
  2720. ValidateDirectory(SourceNode);
  2721. end;
  2722. {Update subdirectories of target directory:}
  2723. TargetNode := FindNodeToPath(TargetPath);
  2724. if Assigned(TargetNode) then
  2725. ValidateDirectory(TargetNode)
  2726. else
  2727. ValidateDirectory(GetDriveStatus(DriveInfo.GetDriveKey(TargetPath)).RootNode);
  2728. if Effect = DROPEFFECT_MOVE then
  2729. Items.EndUpdate;
  2730. {Update linked component TDirView:}
  2731. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2732. begin
  2733. case Effect of
  2734. DROPEFFECT_COPY,
  2735. DROPEFFECT_LINK:
  2736. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) then
  2737. FDirView.Reload2;
  2738. DROPEFFECT_MOVE:
  2739. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) or
  2740. (IncludeTrailingBackslash(SourceParentPath) = IncludeTrailingBackslash(DirView.Path)) then
  2741. begin
  2742. if FDirView <> DropSourceControl then FDirView.Reload2;
  2743. end;
  2744. end; {Case}
  2745. end;
  2746. {Update the DropSource control, if files are moved and it is a TDirView:}
  2747. if (Effect = DROPEFFECT_MOVE) and (DropSourceControl is TDirView) then
  2748. begin
  2749. TDirView(DropSourceControl).ValidateSelectedFiles;
  2750. end;
  2751. finally
  2752. FFileOperator.OperandFrom.Clear;
  2753. FFileOperator.OperandTo.Clear;
  2754. StartAllWatchThreads;
  2755. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2756. FDirView.StartWatchThread;
  2757. if Assigned(DropSourceControl) and (DropSourceControl is TDirView) and
  2758. (not TDirView(DropSourceControl).WatchThreadActive) then
  2759. TDirView(DropSourceControl).StartWatchThread;
  2760. Screen.Cursor := SaveCursor;
  2761. end;
  2762. end; {PerformDragDropFileOperation}
  2763. function TDriveView.GetCanUndoCopyMove: Boolean;
  2764. begin
  2765. Result := Assigned(FFileOperator) and FFileOperator.CanUndo;
  2766. end; {CanUndoCopyMove}
  2767. function TDriveView.UndoCopyMove: Boolean;
  2768. var
  2769. LastTarget: string;
  2770. LastSource: string;
  2771. begin
  2772. Result := False;
  2773. if FFileOperator.CanUndo then
  2774. begin
  2775. Lasttarget := FFileOperator.LastOperandTo[0];
  2776. LastSource := FFileOperator.LastOperandFrom[0];
  2777. StopAllWatchThreads;
  2778. Result := FFileOperator.UndoExecute;
  2779. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastTarget)));
  2780. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastSource)));
  2781. StartAllWatchThreads;
  2782. if Assigned(FDirView) then
  2783. with FDirView do
  2784. if not WatchThreadActive then
  2785. begin
  2786. if (IncludeTrailingBackslash(ExtractFilePath(LastTarget)) = IncludeTrailingBackslash(Path)) or
  2787. (IncludeTrailingBackslash(ExtractFilePath(LastSource)) = IncludeTrailingBackslash(Path)) then
  2788. Reload2;
  2789. end;
  2790. end;
  2791. end; {UndoCopyMove}
  2792. {Clipboard operations:}
  2793. procedure TDriveView.SetLastPathCut(Path: string);
  2794. var
  2795. Node: TTreeNode;
  2796. begin
  2797. if FLastPathCut <> Path then
  2798. begin
  2799. Node := FindNodeToPath(FLastPathCut);
  2800. if Assigned(Node) then
  2801. begin
  2802. FLastPathCut := Path;
  2803. Node.Cut := False;
  2804. end;
  2805. Node := FindNodeToPath(Path);
  2806. if Assigned(Node) then
  2807. begin
  2808. FLastPathCut := Path;
  2809. Node.Cut := True;
  2810. end;
  2811. end;
  2812. end; {SetLastNodeCut}
  2813. procedure TDriveView.EmptyClipboard;
  2814. begin
  2815. if Windows.OpenClipBoard(0) then
  2816. begin
  2817. Windows.EmptyClipBoard;
  2818. Windows.CloseClipBoard;
  2819. LastPathCut := '';
  2820. LastClipBoardOperation := cboNone;
  2821. if Assigned(FDirView) then
  2822. FDirView.EmptyClipboard;
  2823. end;
  2824. end; {EmptyClipBoard}
  2825. function TDriveView.CopyToClipBoard(Node: TTreeNode): Boolean;
  2826. begin
  2827. Result := Assigned(Selected);
  2828. if Result then
  2829. begin
  2830. EmptyClipBoard;
  2831. ClearDragFileList(FDragDropFilesEx.FileList);
  2832. AddToDragFileList(FDragDropFilesEx.FileList, Selected);
  2833. Result := FDragDropFilesEx.CopyToClipBoard;
  2834. LastClipBoardOperation := cboCopy;
  2835. end;
  2836. end; {CopyToClipBoard}
  2837. function TDriveView.CutToClipBoard(Node: TTreeNode): Boolean;
  2838. begin
  2839. Result := Assigned(Node) and (Node.Level > 0) and CopyToClipBoard(Node);
  2840. if Result then
  2841. begin
  2842. LastPathCut := NodePathName(Node);
  2843. LastClipBoardOperation := cboCut;
  2844. end;
  2845. end; {CutToClipBoard}
  2846. function TDriveView.CanPasteFromClipBoard: Boolean;
  2847. begin
  2848. Result := False;
  2849. if Assigned(Selected) and Windows.OpenClipboard(0) then
  2850. begin
  2851. Result := IsClipboardFormatAvailable(CF_HDROP);
  2852. Windows.CloseClipBoard;
  2853. end;
  2854. end; {CanPasteFromClipBoard}
  2855. function TDriveView.PasteFromClipBoard(TargetPath: String = ''): Boolean;
  2856. begin
  2857. ClearDragFileList(FDragDropFilesEx.FileList);
  2858. Result := False;
  2859. if CanPasteFromClipBoard and {MP}FDragDropFilesEx.GetFromClipBoard{/MP}
  2860. then
  2861. begin
  2862. if TargetPath = '' then
  2863. TargetPath := NodePathName(Selected);
  2864. case LastClipBoardOperation of
  2865. cboCopy,
  2866. cboNone:
  2867. begin
  2868. PerformDragDropFileOperation(Selected, DROPEFFECT_COPY);
  2869. if Assigned(FOnDDExecuted) then
  2870. FOnDDExecuted(Self, DROPEFFECT_COPY);
  2871. end;
  2872. cboCut:
  2873. begin
  2874. PerformDragDropFileOperation(Selected, DROPEFFECT_MOVE);
  2875. if Assigned(FOnDDExecuted) then
  2876. FOnDDExecuted(Self, DROPEFFECT_MOVE);
  2877. EmptyClipBoard;
  2878. end;
  2879. end;
  2880. Result := True;
  2881. end;
  2882. end; {PasteFromClipBoard}
  2883. procedure TDriveView.CMRecreateWnd(var Msg: TMessage);
  2884. var
  2885. ScheduledCount: Integer;
  2886. begin
  2887. ScheduledCount := FSubDirReaderThread.Detach;
  2888. try
  2889. inherited;
  2890. finally
  2891. FSubDirReaderThread.Reattach(ScheduledCount);
  2892. end;
  2893. end;
  2894. end.