DirView.pas 100 KB

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