DirView.pas 100 KB

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