1
0

CustomDirView.pas 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758
  1. unit CustomDirView;
  2. interface
  3. {$R DirImg.res}
  4. {$WARN UNIT_PLATFORM OFF}
  5. {$WARN SYMBOL_PLATFORM OFF}
  6. uses
  7. Windows, Messages, Classes, Graphics, Controls,
  8. Forms, ComCtrls, ShellAPI, ComObj, ShlObj, Dialogs,
  9. ActiveX, CommCtrl, Extctrls, ImgList, Menus, FileCtrl,
  10. PIDL, BaseUtils, DragDrop, DragDropFilesEx, IEDriveInfo,
  11. IEListView, PathLabel, SysUtils, PasTools;
  12. const
  13. clDefaultItemColor = -(COLOR_ENDCOLORS + 1);
  14. WM_USER_RENAME = WM_USER + 57;
  15. WM_USER_INVALIDATEITEM = WM_USER + $2000 + 16;
  16. oiNoOverlay = $00;
  17. oiDirUp = $01;
  18. oiLink = $02;
  19. oiBrokenLink = $04;
  20. oiPartial = $08;
  21. oiEncrypted = $10;
  22. DefaultHistoryCount = 200;
  23. const
  24. DDDragStartDelay = 500000;
  25. DirAttrMask = SysUtils.faDirectory or SysUtils.faSysFile or SysUtils.faHidden;
  26. const
  27. _XBUTTON1 = $0001;
  28. _XBUTTON2 = $0002;
  29. type
  30. TStatusFileInfo = record
  31. FilesCount: Integer;
  32. SelectedCount: Integer;
  33. FilesSize: Int64;
  34. SelectedSize: Int64;
  35. HiddenCount: Integer;
  36. FilteredCount: Integer;
  37. end;
  38. type
  39. {Drag&Drop events:}
  40. TDDError = (DDCreateShortCutError, DDPathNotFoundError);
  41. TDDOnDragEnter = procedure(Sender: TObject; DataObj: IDataObject; grfKeyState: Longint; Point: TPoint; var dwEffect: Longint; var Accept: Boolean) of object;
  42. TDDOnDragLeave = procedure(Sender: TObject) of object;
  43. TDDOnDragOver = procedure(Sender: TObject; grfKeyState: Longint; Point: TPoint; var dwEffect: Longint) of object;
  44. TDDOnDrop = procedure(Sender: TObject; DataObj: IDataObject; grfKeyState: Longint; Point: TPoint; var dwEffect: Longint) of object;
  45. TDDOnQueryContinueDrag = procedure(Sender: TObject; FEscapePressed: BOOL; grfKeyState: Longint; var Result: HResult) of object;
  46. TDDOnGiveFeedback = procedure(Sender: TObject; dwEffect: Longint; var Result: HResult) of object;
  47. TDDOnChooseEffect = procedure(Sender: TObject; grfKeyState: Longint; var dwEffect: Longint) of object;
  48. TDDOnDragDetect = procedure(Sender: TObject; grfKeyState: Longint; DetectStart, Point: TPoint; DragStatus: TDragDetectStatus) of object;
  49. TDDOnCreateDragFileList = procedure(Sender: TObject; FileList: TFileList; var Created: Boolean) of object;
  50. TDDOnCreateDataObject = procedure(Sender: TObject; var DataObject: TDataObject) of object;
  51. TDDOnTargetHasDropHandler = procedure(Sender: TObject; Item: TListItem; var Effect: Integer; var DropHandler: Boolean) of object;
  52. TOnProcessDropped = procedure(Sender: TObject; grfKeyState: Longint; Point: TPoint; var dwEffect: Longint) of object;
  53. TDDErrorEvent = procedure(Sender: TObject; ErrorNo: TDDError) of object;
  54. TDDExecutedEvent = procedure(Sender: TObject; dwEffect: Longint) of object;
  55. TDDFileOperationEvent =
  56. procedure(Sender: TObject; dwEffect: LongInt; SourcePath, TargetPath: string; Paste: Boolean;
  57. var DoOperation: Boolean) of object;
  58. TDDFileOperationExecutedEvent = procedure(Sender: TObject; dwEffect: LongInt; SourcePath, TargetPath: string) of object;
  59. TDirViewExecFileEvent = procedure(Sender: TObject; Item: TListItem; var AllowExec: Boolean) of object;
  60. TMatchMaskEvent = procedure(Sender: TObject; FileName: string; Directory: Boolean; Size: Int64; Modification: TDateTime; Masks: string; var Matches: Boolean; AllowImplicitMatches: Boolean) of object;
  61. TDirViewGetOverlayEvent = procedure(Sender: TObject; Item: TListItem; var Indexes: Word) of object;
  62. TDirViewGetItemColorEvent = procedure(Sender: TObject; FileName: string; Directory: Boolean; Size: Int64; Modification: TDateTime; var Color: TColor) of object;
  63. TDirViewUpdateStatusBarEvent = procedure(Sender: TObject; const FileInfo: TStatusFileInfo) of object;
  64. TDirViewBusy = procedure(Sender: TObject; Busy: Integer; var State: Boolean) of object;
  65. TDirViewChangeFocusEvent = procedure(Sender: TObject; Item: TListItem) of object;
  66. TBusyOperation = reference to procedure;
  67. type
  68. TCustomDirView = class;
  69. TSelAttr = (selDontCare, selYes, selNo);
  70. TFileFilter = record
  71. Masks: string;
  72. Directories: Boolean;
  73. end;
  74. TDirViewNotifyEvent = procedure(Sender: TCustomDirView) of object;
  75. TDVGetFilterEvent = procedure(Sender: TCustomDirView; Select: Boolean;
  76. var Filter: TFileFilter) of object;
  77. TDVHistoryGoEvent = procedure(Sender: TCustomDirView; Index: Integer; var Cancel: Boolean) of object;
  78. TCompareCriteria = (ccTime, ccSize);
  79. TCompareCriterias = set of TCompareCriteria;
  80. // First four must match TViewStyle
  81. TDirViewStyle = (dvsIcon, dvsSmallIcon, dvsList, dvsReport, dvsThumbnail);
  82. TWMXMouse = packed record
  83. Msg: Cardinal;
  84. Keys: Word;
  85. Button: Word;
  86. Pos: TSmallPoint;
  87. Result: Longint
  88. end;
  89. TCustomizableDragDropFilesEx = class(TDragDropFilesEx)
  90. public
  91. function Execute(DataObject: TDataObject): TDragResult;
  92. end;
  93. TCustomDirView = class(TCustomIEListView)
  94. private
  95. FAddParentDir: Boolean;
  96. FDimmHiddenFiles: Boolean;
  97. FFormatSizeBytes: TFormatBytesStyle;
  98. FWantUseDragImages: Boolean;
  99. FDragDropFilesEx: TCustomizableDragDropFilesEx;
  100. FUseSystemContextMenu: Boolean;
  101. FOnStartLoading: TNotifyEvent;
  102. FOnLoaded: TNotifyEvent;
  103. FExeDrag: Boolean;
  104. FDDLinkOnExeDrag: Boolean;
  105. FOnDDDragEnter: TDDOnDragEnter;
  106. FOnDDDragLeave: TDDOnDragLeave;
  107. FOnDDDragOver: TDDOnDragOver;
  108. FOnDDDrop: TDDOnDrop;
  109. FOnDDQueryContinueDrag: TDDOnQueryContinueDrag;
  110. FOnDDGiveFeedback: TDDOnGiveFeedback;
  111. FOnDDChooseEffect: TDDOnChooseEffect;
  112. FOnDDDragDetect: TDDOnDragDetect;
  113. FOnDDCreateDragFileList: TDDOnCreateDragFileList;
  114. FOnDDProcessDropped: TOnProcessDropped;
  115. FOnDDError: TDDErrorEvent;
  116. FOnDDExecuted: TDDExecutedEvent;
  117. FOnDDFileOperation: TDDFileOperationEvent;
  118. FOnDDFileOperationExecuted: TDDFileOperationExecutedEvent;
  119. FOnDDEnd: TNotifyEvent;
  120. FOnDDCreateDataObject: TDDOnCreateDataObject;
  121. FOnDDTargetHasDropHandler: TDDOnTargetHasDropHandler;
  122. FOnExecFile: TDirViewExecFileEvent;
  123. FForceRename: Boolean;
  124. FLastDDResult: TDragResult;
  125. FLastRenameName: string;
  126. FContextMenu: Boolean;
  127. FDragEnabled: Boolean;
  128. FDragPos: TPoint;
  129. FStartPos: TPoint;
  130. FDDOwnerIsSource: Boolean;
  131. FAbortLoading: Boolean;
  132. FBackCount: Integer;
  133. FDontRecordPath: Boolean;
  134. FDragOnDriveIsMove: Boolean;
  135. FNotifyEnabled: Boolean;
  136. FDragStartTime: TFileTime;
  137. FHistoryPaths: TStrings;
  138. FOverlaySmallImages: TImageList;
  139. FOverlayLargeImages: TImageList;
  140. FThumbnailShellImages: TImageList;
  141. FThumbnailImages: TImageList;
  142. FMaxHistoryCount: Integer;
  143. FPathLabel: TCustomPathLabel;
  144. FOnUpdateStatusBar: TDirViewUpdateStatusBarEvent;
  145. FOnHistoryChange: TDirViewNotifyEvent;
  146. FOnHistoryGo: TDVHistoryGoEvent;
  147. FOnPathChange: TDirViewNotifyEvent;
  148. FShowHiddenFiles: Boolean;
  149. FSavedSelection: Boolean;
  150. FSavedSelectionFile: string;
  151. FSavedSelectionLastFile: string;
  152. FSavedNames: TStringList;
  153. FPendingFocusSomething: Integer;
  154. FOnMatchMask: TMatchMaskEvent;
  155. FOnGetOverlay: TDirViewGetOverlayEvent;
  156. FOnGetItemColor: TDirViewGetItemColorEvent;
  157. FMask: string;
  158. FNaturalOrderNumericalSorting: Boolean;
  159. FAlwaysSortDirectoriesByName: Boolean;
  160. FDarkMode: Boolean;
  161. FScrollOnDragOver: TListViewScrollOnDragOver;
  162. FStatusFileInfo: TStatusFileInfo;
  163. FOnBusy: TDirViewBusy;
  164. FOnChangeFocus: TDirViewChangeFocusEvent;
  165. FFallbackThumbnail: array[Boolean] of TBitmap;
  166. FFallbackThumbnailSize: TSize;
  167. FRecreatingWnd: Integer;
  168. procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
  169. procedure WMNotify(var Msg: TWMNotify); message WM_NOTIFY;
  170. procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
  171. procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  172. procedure WMContextMenu(var Message: TWMContextMenu); message WM_CONTEXTMENU;
  173. procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  174. procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
  175. procedure WMXButtonUp(var Message: TWMXMouse); message WM_XBUTTONUP;
  176. procedure WMAppCommand(var Message: TMessage); message WM_APPCOMMAND;
  177. procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
  178. procedure LVMSetExtendedListViewStyle(var Message: TMessage); message LVM_SETEXTENDEDLISTVIEWSTYLE;
  179. procedure CMRecreateWnd(var Message: TMessage); message CM_RECREATEWND;
  180. procedure CMDPIChanged(var Message: TMessage); message CM_DPICHANGED;
  181. procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  182. procedure CNKeyDown(var Message: TWMKeyDown); message CN_KEYDOWN;
  183. procedure WMUserInvalidateItem(var Message: TMessage); message WM_USER_INVALIDATEITEM;
  184. procedure DumbCustomDrawItem(Sender: TCustomListView; Item: TListItem;
  185. State: TCustomDrawState; var DefaultDraw: Boolean);
  186. procedure DumbCustomDrawSubItem(Sender: TCustomListView;
  187. Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  188. var DefaultDraw: Boolean);
  189. function GetFilesMarkedSize: Int64;
  190. function GetForwardCount: Integer;
  191. function GetHistoryPath(Index: Integer): string;
  192. function GetSelectedNamesSaved: Boolean;
  193. function GetDirViewStyle: TDirViewStyle;
  194. procedure SetDirViewStyle(Value: TDirViewStyle);
  195. procedure ViewStyleChanged;
  196. function GetTargetPopupMenu: Boolean;
  197. function GetUseDragImages: Boolean;
  198. procedure SetMaxHistoryCount(Value: Integer);
  199. procedure SetPathLabel(Value: TCustomPathLabel);
  200. procedure SetTargetPopupMenu(Value: Boolean);
  201. procedure WMUserRename(var Message: TMessage); message WM_User_Rename;
  202. protected
  203. FCaseSensitive: Boolean;
  204. FDirty: Boolean;
  205. FFilesSize: Int64;
  206. FFilesSelSize: Int64;
  207. FHasParentDir: Boolean;
  208. FIsRecycleBin: Boolean;
  209. FLastPath: string;
  210. FHistoryPath: string;
  211. FLoadEnabled: Boolean;
  212. FLoading: Boolean;
  213. FSelectFile: string;
  214. FWatchForChanges: Boolean;
  215. FInvalidNameChars: string;
  216. FDragDrive: string;
  217. FAnnouncedState: TObject;
  218. FThumbnail: Boolean;
  219. FEffectiveMask: string;
  220. procedure AddToDragFileList(FileList: TFileList; Item: TListItem); virtual;
  221. function CanEdit(Item: TListItem): Boolean; override;
  222. function CanChangeSelection(Item: TListItem; Select: Boolean): Boolean; override;
  223. procedure CancelEdit;
  224. procedure ClearItems; override;
  225. function GetDirOK: Boolean; virtual; abstract;
  226. procedure DDDragDetect(grfKeyState: Longint; DetectStart, Point: TPoint; DragStatus: TDragDetectStatus); virtual;
  227. procedure DDDragEnter(DataObj: IDataObject; grfKeyState: Longint; Point: TPoint; var dwEffect: longint; var Accept: Boolean);
  228. procedure DDDragLeave(Dummy: Integer);
  229. procedure DDDragOver(grfKeyState: Longint; Point: TPoint; var dwEffect: Longint; PreferredEffect: LongInt);
  230. procedure DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer; PreferredEffect: Integer); virtual;
  231. procedure DDDrop(DataObj: IDataObject; grfKeyState: LongInt; Point: TPoint; var dwEffect: Longint);
  232. procedure DDDropHandlerSucceeded(Sender: TObject; grfKeyState: Longint; Point: TPoint; dwEffect: Longint); virtual;
  233. procedure DDGiveFeedback(dwEffect: Longint; var Result: HResult); virtual;
  234. procedure DDMenuPopup(Sender: TObject; AMenu: HMenu; DataObj: IDataObject;
  235. AMinCustCmd:integer; grfKeyState: Longint; pt: TPoint); virtual;
  236. procedure DDMenuDone(Sender: TObject; AMenu: HMenu); virtual;
  237. procedure DDProcessDropped(Sender: TObject; grfKeyState: Longint;
  238. Point: TPoint; dwEffect: Longint);
  239. procedure DDQueryContinueDrag(FEscapePressed: LongBool;
  240. grfKeyState: Longint; var Result: HResult); virtual;
  241. procedure DDSpecifyDropTarget(Sender: TObject; DragDropHandler: Boolean;
  242. Point: TPoint; var pidlFQ : PItemIDList; var Filename: string); virtual;
  243. procedure GetDisplayInfo(ListItem: TListItem; var DispInfo: TLVItem); virtual;
  244. function GetDragSourceEffects: TDropEffectSet; virtual;
  245. function GetPathName: string; virtual; abstract;
  246. function GetFilesCount: Integer; virtual;
  247. procedure ColClick(Column: TListColumn); override;
  248. procedure CreateWnd; override;
  249. procedure DestroyWnd; override;
  250. function OperateOnFocusedFile(Focused: Boolean; OnlyFocused: Boolean = False): Boolean;
  251. function CustomCreateFileList(Focused, OnlyFocused: Boolean;
  252. FullPath: Boolean; FileList: TStrings = nil; ItemObject: Boolean = False): TStrings;
  253. function CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  254. Stage: TCustomDrawStage): Boolean; override;
  255. function CustomDrawSubItem(Item: TListItem; SubItem: Integer;
  256. State: TCustomDrawState; Stage: TCustomDrawStage): Boolean; override;
  257. procedure CustomSortItems(SortProc: Pointer);
  258. procedure Delete(Item: TListItem); override;
  259. procedure DoHistoryChange; dynamic;
  260. function DragCompleteFileList: Boolean; virtual;
  261. procedure Edit(const HItem: TLVItem); override;
  262. procedure EndSelectionUpdate; override;
  263. function DoExecFile(Item: TListItem; ForceEnter: Boolean): Boolean; virtual;
  264. procedure Execute(Item: TListItem; ForceEnter: Boolean); virtual;
  265. procedure ExecuteFile(Item: TListItem); virtual; abstract;
  266. procedure FocusSomething(ForceMakeVisible: Boolean); override;
  267. function GetIsRoot: Boolean; virtual; abstract;
  268. function ItemCanDrag(Item: TListItem): Boolean; virtual;
  269. function DoItemColor(Item: TListItem): TColor;
  270. function ItemColor(Item: TListItem): TColor; virtual;
  271. function ItemImageIndex(Item: TListItem; Cache: Boolean): Integer; virtual; abstract;
  272. function ItemThumbnail(Item: TListItem; Size: TSize): TBitmap; virtual;
  273. procedure FreeThumbnails;
  274. function FallbackThumbnail(Dir: Boolean; Size: TSize): TBitmap;
  275. procedure DrawThumbnail(Item: TListItem; DC: HDC);
  276. // ItemIsDirectory and ItemFullFileName is in public block
  277. function ItemIsRecycleBin(Item: TListItem): Boolean; virtual;
  278. procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  279. procedure KeyPress(var Key: Char); override;
  280. procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  281. procedure LoadFiles; virtual; abstract;
  282. procedure PerformItemDragDropOperation(Item: TListItem; Effect: Integer; Paste: Boolean); virtual; abstract;
  283. procedure ProcessChangedFiles(DirView: TCustomDirView;
  284. FileList: TStrings; FullPath: Boolean; ExistingOnly: Boolean;
  285. Criterias: TCompareCriterias);
  286. procedure ReloadForce(CacheIcons : Boolean);
  287. procedure RetryRename(NewName: string);
  288. procedure SetAddParentDir(Value: Boolean); virtual;
  289. procedure SetDimmHiddenFiles(Value: Boolean); virtual;
  290. procedure SetItemImageIndex(Item: TListItem; Index: Integer); virtual; abstract;
  291. procedure SetLoadEnabled(Enabled : Boolean); virtual;
  292. procedure SetMultiSelect(Value: Boolean); override;
  293. function GetPath: string; virtual; abstract;
  294. function GetValid: Boolean; override;
  295. procedure InternalEdit(const HItem: TLVItem); virtual; abstract;
  296. function ItemIsFile(Item: TListItem): Boolean; virtual; abstract;
  297. function ItemMatchesFilter(Item: TListItem; const Filter: TFileFilter): Boolean; virtual; abstract;
  298. function ItemOverlayIndexes(Item: TListItem): Word; virtual;
  299. procedure LimitHistorySize;
  300. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  301. procedure PathChanged; virtual;
  302. procedure PathChanging(Relative: Boolean);
  303. procedure SetPath(Value: string); virtual; abstract;
  304. procedure SetShowHiddenFiles(Value: Boolean); virtual;
  305. procedure SetFormatSizeBytes(Value: TFormatBytesStyle);
  306. procedure SetViewStyle(Value: TViewStyle); override;
  307. procedure SetWatchForChanges(Value: Boolean); virtual;
  308. function TargetHasDropHandler(Item: TListItem; Effect: Integer): Boolean; virtual;
  309. procedure UpdatePathLabel; dynamic;
  310. procedure UpdatePathLabelCaption; dynamic;
  311. function FileNameMatchesMasks(FileName: string; Directory: Boolean; Size: Int64; Modification: TDateTime; Masks: string; AllowImplicitMatches: Boolean): Boolean;
  312. function EnableDragOnClick: Boolean; override;
  313. procedure SetMask(Value: string); virtual;
  314. procedure SetNaturalOrderNumericalSorting(Value: Boolean);
  315. procedure SetAlwaysSortDirectoriesByName(Value: Boolean);
  316. procedure SetDarkMode(Value: Boolean);
  317. procedure ScrollOnDragOverBeforeUpdate(ObjectToValidate: TObject);
  318. procedure ScrollOnDragOverAfterUpdate;
  319. procedure DoHistoryGo(Index: Integer);
  320. procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  321. procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  322. procedure WMThemeChanged(var Message: TMessage); message WM_THEMECHANGED;
  323. procedure EnsureSelectionRedrawn;
  324. function HiddenCount: Integer; virtual; abstract;
  325. function FilteredCount: Integer; virtual; abstract;
  326. function DoBusy(Busy: Integer): Boolean;
  327. function StartBusy: Boolean;
  328. procedure EndBusy;
  329. function IsBusy: Boolean;
  330. procedure BusyOperation(Operation: TBusyOperation);
  331. procedure DoDisplayPropertiesMenu;
  332. procedure DoExecute(Item: TListItem; ForceEnter: Boolean);
  333. procedure DoExecuteParentDirectory;
  334. procedure Load(DoFocusSomething: Boolean); virtual;
  335. function NeedImageList(Size: TImageListSize; Recreate: Boolean; var OverlayImages: TImageList): TImageList;
  336. procedure NeedImageLists(Recreate: Boolean);
  337. procedure FreeImageLists;
  338. procedure UpdateDarkMode;
  339. procedure DoUpdateStatusBar(Force: Boolean = False);
  340. procedure DoCustomDrawItem(Item: TListItem; Stage: TCustomDrawStage);
  341. procedure ItemCalculatedSizeUpdated(Item: TListItem; OldSize, NewSize: Int64);
  342. procedure SaveItemsState(var FocusedItem: string; var FocusedShown: Boolean; var ShownItemOffset: Integer);
  343. procedure RestoreItemsState(ItemToFocus: TListItem; FocusedShown: Boolean; ShownItemOffset: Integer); overload;
  344. procedure RestoreItemsState(AState: TObject); overload;
  345. function FindFileItemIfNotEmpty(FileName: string): TListItem;
  346. public
  347. constructor Create(AOwner: TComponent); override;
  348. destructor Destroy; override;
  349. procedure Reload(CacheIcons: Boolean); virtual;
  350. function CreateFocusedFileList(FullPath: Boolean; FileList: TStrings = nil): TStrings;
  351. function CreateFileList(Focused: Boolean; FullPath: Boolean; FileList: TStrings = nil;
  352. ItemObject: Boolean = False): TStrings;
  353. function AnyFileSelected(OnlyFocused: Boolean; FilesOnly: Boolean;
  354. FocusedFileOnlyWhenFocused: Boolean): Boolean;
  355. procedure SelectFiles(Filter: TFileFilter; Select: Boolean);
  356. procedure ExecuteHomeDirectory; virtual; abstract;
  357. procedure ExecuteParentDirectory; virtual; abstract;
  358. procedure ExecuteRootDirectory; virtual; abstract;
  359. procedure ExecuteCurrentFile();
  360. procedure CreateDirectory(DirName: string); virtual; abstract;
  361. function FindFileItem(FileName: string): TListItem;
  362. procedure HistoryGo(Index: Integer);
  363. function ItemIsDirectory(Item: TListItem): Boolean; virtual; abstract;
  364. function ItemIsParentDirectory(Item: TListItem): Boolean; virtual; abstract;
  365. function ItemFullFileName(Item: TListItem): string; virtual; abstract;
  366. function ItemFileName(Item: TListItem): string; virtual; abstract;
  367. function ItemFileSize(Item: TListItem): Int64; virtual; abstract;
  368. function ItemFileTime(Item: TListItem; var Precision: TDateTimePrecision): TDateTime; virtual; abstract;
  369. function ItemData(Item: TListItem): TObject; virtual;
  370. procedure SetItemCalculatedSize(Item: TListItem; Size: Int64); virtual; abstract;
  371. procedure ReloadDirectory; virtual; abstract;
  372. procedure DisplayPropertiesMenu; virtual; abstract;
  373. function CreateChangedFileList(DirView: TCustomDirView; FullPath: Boolean;
  374. ExistingOnly: Boolean; Criterias: TCompareCriterias): TStrings;
  375. procedure CompareFiles(DirView: TCustomDirView; ExistingOnly: Boolean;
  376. Criterias: TCompareCriterias); virtual;
  377. function GetColumnText(ListItem: TListItem; Index: Integer): string;
  378. procedure SaveSelection;
  379. procedure RestoreSelection;
  380. procedure DiscardSavedSelection;
  381. procedure SaveSelectedNames;
  382. procedure RestoreSelectedNames;
  383. procedure ContinueSession(Continue: Boolean);
  384. function CanPasteFromClipBoard: Boolean; dynamic;
  385. function PasteFromClipBoard(TargetPath: string = ''): Boolean; virtual; abstract;
  386. function SaveState: TObject; virtual;
  387. procedure RestoreState(AState: TObject); virtual;
  388. procedure AnnounceState(AState: TObject); virtual;
  389. procedure FocusByName(FileName: string);
  390. procedure DisplayContextMenu(Where: TPoint); virtual; abstract;
  391. procedure DisplayContextMenuInSitu;
  392. procedure UpdateStatusBar;
  393. procedure InvalidateItem(Item: TListItem);
  394. property AddParentDir: Boolean read FAddParentDir write SetAddParentDir default False;
  395. property DimmHiddenFiles: Boolean read FDimmHiddenFiles write SetDimmHiddenFiles default True;
  396. property DragDropFilesEx: TCustomizableDragDropFilesEx read FDragDropFilesEx;
  397. property FormatSizeBytes: TFormatBytesStyle read FFormatSizeBytes write SetFormatSizeBytes default fbNone;
  398. property WantUseDragImages: Boolean read FWantUseDragImages write FWantUseDragImages default False;
  399. property UseDragImages: Boolean read GetUseDragImages stored False;
  400. property FullDrag default True;
  401. property TargetPopupMenu: Boolean read GetTargetPopupMenu write SetTargetPopupMenu default True;
  402. property DDOwnerIsSource: Boolean read FDDOwnerIsSource;
  403. property FilesSize: Int64 read FFilesSize;
  404. property FilesSelSize: Int64 read FFilesSelSize;
  405. property FilesCount: Integer read GetFilesCount;
  406. property FilesMarkedSize: Int64 read GetFilesMarkedSize;
  407. property HasParentDir: Boolean read FHasParentDir;
  408. property Path: string read GetPath write SetPath;
  409. property PathName: string read GetPathName;
  410. property UseSystemContextMenu: Boolean read FUseSystemContextMenu
  411. write FUseSystemContextMenu default True;
  412. property Loading: Boolean read FLoading;
  413. property AbortLoading: Boolean read FAbortLoading write FAbortLoading stored False;
  414. property BackCount: Integer read FBackCount;
  415. {Enable or disable populating the item list:}
  416. property LoadEnabled: Boolean read FLoadEnabled write SetLoadEnabled default True;
  417. {Displayed data is not valid => reload required}
  418. property Dirty: Boolean read FDirty;
  419. property DirOK: Boolean read GetDirOK;
  420. property LastPath: string read FLastPath;
  421. property IsRecycleBin: Boolean read FIsRecycleBin;
  422. property DDLinkOnExeDrag: Boolean read FDDLinkOnExeDrag
  423. write FDDLinkOnExeDrag default False;
  424. property DragOnDriveIsMove: Boolean read FDragOnDriveIsMove write FDragOnDriveIsMove;
  425. property DragSourceEffects: TDropEffectSet read GetDragSourceEffects{ write FDragSourceEffects};
  426. property ExeDrag: Boolean read FExeDrag;
  427. property ForwardCount: Integer read GetForwardCount;
  428. property HistoryPath[Index: Integer]: string read GetHistoryPath;
  429. property IsRoot: Boolean read GetIsRoot;
  430. property LastDDResult: TDragResult read FLastDDResult;
  431. property SmallImages;
  432. property LargeImages;
  433. property MaxHistoryCount: Integer read FMaxHistoryCount write SetMaxHistoryCount default DefaultHistoryCount;
  434. property SelectedNamesSaved: Boolean read GetSelectedNamesSaved;
  435. {filemask, multiple filters are possible: '*.pas;*.dfm'}
  436. property Mask: string read FMask write SetMask;
  437. property NaturalOrderNumericalSorting: Boolean read FNaturalOrderNumericalSorting write SetNaturalOrderNumericalSorting;
  438. property AlwaysSortDirectoriesByName: Boolean read FAlwaysSortDirectoriesByName write SetAlwaysSortDirectoriesByName;
  439. property DarkMode: Boolean read FDarkMode write SetDarkMode;
  440. property DirViewStyle: TDirViewStyle read GetDirViewStyle write SetDirViewStyle;
  441. property OnContextPopup;
  442. property OnStartLoading: TNotifyEvent read FOnStartLoading write FOnStartLoading;
  443. property OnLoaded: TNotifyEvent read FOnLoaded write FOnLoaded;
  444. {The mouse has entered the component window as a target of a drag&drop operation:}
  445. property OnDDDragEnter: TDDOnDragEnter read FOnDDDragEnter write FOnDDDragEnter;
  446. {The mouse has leaved the component window as a target of a drag&drop operation:}
  447. property OnDDDragLeave: TDDOnDragLeave read FOnDDDragLeave write FOnDDDragLeave;
  448. {The mouse is dragging in the component window as a target of a drag&drop operation:}
  449. property OnDDDragOver: TDDOnDragOver read FOnDDDragOver write FOnDDDragOver;
  450. {The Drag&drop operation is about to be executed:}
  451. property OnDDDrop: TDDOnDrop read FOnDDDrop write FOnDDDrop;
  452. property OnDDQueryContinueDrag: TDDOnQueryContinueDrag
  453. read FOnDDQueryContinueDrag write FOnDDQueryContinueDrag;
  454. property OnDDGiveFeedback: TDDOnGiveFeedback
  455. read FOnDDGiveFeedback write FOnDDGiveFeedback;
  456. property OnDDChooseEffect: TDDOnChooseEffect
  457. read FOnDDChooseEffect write FOnDDChooseEffect;
  458. {A drag&drop operation is about to be initiated whith
  459. the components window as the source:}
  460. property OnDDDragDetect: TDDOnDragDetect
  461. read FOnDDDragDetect write FOnDDDragDetect;
  462. property OnDDCreateDragFileList: TDDOnCreateDragFileList
  463. read FOnDDCreateDragFileList write FOnDDCreateDragFileList;
  464. property OnDDEnd: TNotifyEvent
  465. read FOnDDEnd write FOnDDEnd;
  466. property OnDDCreateDataObject: TDDOnCreateDataObject
  467. read FOnDDCreateDataObject write FOnDDCreateDataObject;
  468. property OnDDTargetHasDropHandler: TDDOnTargetHasDropHandler
  469. read FOnDDTargetHasDropHandler write FOnDDTargetHasDropHandler;
  470. {The component window is the target of a drag&drop operation:}
  471. property OnDDProcessDropped: TOnProcessDropped
  472. read FOnDDProcessDropped write FOnDDProcessDropped;
  473. {An error has occurred during a drag&drop operation:}
  474. property OnDDError: TDDErrorEvent read FOnDDError write FOnDDError;
  475. {The drag&drop operation has been executed:}
  476. property OnDDExecuted: TDDExecutedEvent
  477. read FOnDDExecuted write FOnDDExecuted;
  478. {Event is fired just before executing the fileoperation. This event is also fired when
  479. files are pasted from the clipboard:}
  480. property OnDDFileOperation: TDDFileOperationEvent
  481. read FOnDDFileOperation write FOnDDFileOperation;
  482. {Event is fired after executing the fileoperation. This event is also fired when
  483. files are pasted from the clipboard:}
  484. property OnDDFileOperationExecuted: TDDFileOperationExecutedEvent
  485. read FOnDDFileOperationExecuted write FOnDDFileOperationExecuted;
  486. {Set AllowExec to false, if actual file should not be executed:}
  487. property OnExecFile: TDirViewExecFileEvent
  488. read FOnExecFile write FOnExecFile;
  489. property OnHistoryChange: TDirViewNotifyEvent read FOnHistoryChange write FOnHistoryChange;
  490. property OnHistoryGo: TDVHistoryGoEvent read FOnHistoryGo write FOnHistoryGo;
  491. property OnPathChange: TDirViewNotifyEvent read FOnPathChange write FOnPathChange;
  492. property OnMatchMask: TMatchMaskEvent read FOnMatchMask write FOnMatchMask;
  493. property OnGetOverlay: TDirViewGetOverlayEvent read FOnGetOverlay write FOnGetOverlay;
  494. property OnGetItemColor: TDirViewGetItemColorEvent read FOnGetItemColor write FOnGetItemColor;
  495. property PathLabel: TCustomPathLabel read FPathLabel write SetPathLabel;
  496. property ShowHiddenFiles: Boolean read FShowHiddenFiles write SetShowHiddenFiles default True;
  497. property OnUpdateStatusBar: TDirViewUpdateStatusBarEvent read FOnUpdateStatusBar write FOnUpdateStatusBar;
  498. property OnBusy: TDirViewBusy read FOnBusy write FOnBusy;
  499. property OnChangeFocus: TDirViewChangeFocusEvent read FOnChangeFocus write FOnChangeFocus;
  500. {Watch current directory for filename changes (create, rename, delete files)}
  501. property WatchForChanges: Boolean read FWatchForChanges write SetWatchForChanges default False;
  502. end;
  503. resourcestring
  504. SErrorRenameFile = 'Can''t rename file or directory: ';
  505. SErrorRenameFileExists = 'File already exists: ';
  506. SErrorInvalidName = 'Filename contains invalid characters:';
  507. STextFileExt = 'File %s';
  508. STextFiles = '%u Files';
  509. STextDirectories = '%u Directories';
  510. SParentDir = 'Parent directory';
  511. SDragDropError = 'DragDrop Error: %d';
  512. SDriveNotReady = 'Drive ''%s:'' is not ready.';
  513. SDirNotExists = 'Directory ''%s'' doesn''t exist.';
  514. {Additional non-component specific functions:}
  515. {Create and resolve a shell link (file shortcut):}
  516. function CreateFileShortCut(SourceFile, Target, DisplayName: string;
  517. UpdateIfExists: Boolean = False): Boolean;
  518. function ResolveFileShortCut(SourceFile: string; ShowDialog: Boolean = False): string;
  519. function IsExecutable(FileName: string): Boolean;
  520. function GetNextMask(var Mask: string): string;
  521. procedure DefaultFileFilter(var Filter: TFileFilter);
  522. function CompareLogicalTextPas(const S1, S2: string; NaturalOrderNumericalSorting: Boolean): Integer;
  523. function OverlayImageList(Size: Integer): TImageList;
  524. procedure InitFileControls;
  525. var
  526. StdDirIcon: Integer;
  527. StdDirSelIcon: Integer;
  528. DropSourceControl: TObject;
  529. UnknownFileIcon: Integer = 0;
  530. StdDirTypeName: string;
  531. DefaultExeIcon: Integer;
  532. UserDocumentDirectory: string;
  533. const
  534. coInvalidDosChars = '\/:*?"<>|';
  535. Space = ' ';
  536. implementation
  537. uses
  538. Math, DirViewColProperties, UITypes, Types, OperationWithTimeout, Winapi.UxTheme, Vcl.Themes, System.IOUtils;
  539. const
  540. ResDirUp = 'DIRUP%2.2d';
  541. ResLink = 'LINK%2.2d';
  542. ResBrokenLink = 'BROKEN%2.2d';
  543. ResPartial = 'PARTIAL%2.2d';
  544. ResEncrypted = 'ENCRYPTED%2.2d';
  545. var
  546. WinDir: string;
  547. TempDir: string;
  548. GlobalsInitialized: Boolean = False;
  549. function GetIconIndex(const AFile: string; Attrs: DWORD; Flags: UINT): Integer;
  550. var
  551. FileInfo: TSHFileInfo;
  552. begin
  553. try
  554. SHGetFileInfo(PChar(AFile), Attrs, FileInfo, SizeOf(TSHFileInfo),
  555. Flags or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);
  556. Result := FileInfo.iIcon;
  557. except
  558. Result := -1;
  559. end;
  560. end; {GetIconIndex}
  561. function GetshFileInfo(const AFile: string; Attrs: DWORD; Flags: UINT): TSHFileInfo;
  562. begin
  563. try
  564. SHGetFileInfoWithTimeout(PChar(AFile), Attrs, Result, SizeOf(TSHFileInfo), Flags, 1000);
  565. except
  566. FillChar(Result, SizeOf(Result), 0);
  567. end;
  568. end; {GetshFileInfo}
  569. procedure InitFileControls;
  570. begin
  571. if not GlobalsInitialized then
  572. begin
  573. GlobalsInitialized := True;
  574. // See the comment in the call from Execute()
  575. NeedShellImageLists;
  576. // Calling GetshFileInfo in Windows Session 0 sometime cause crash
  577. // (not immediately, but very shortly afterwards [few ms]).
  578. // So this code was moved from initialization section to avoid it
  579. // being used for non-GUI runs.
  580. UnknownFileIcon := GetshFileInfo('$#)(.#$)', FILE_ATTRIBUTE_NORMAL,
  581. SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES).iIcon;
  582. DefaultExeIcon := GetshFileInfo('.COM',
  583. FILE_ATTRIBUTE_NORMAL, SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES).iIcon;
  584. with GetshFileInfo(WinDir, FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY,
  585. SHGFI_TYPENAME or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES) do
  586. begin
  587. StdDirTypeName := szTypeName;
  588. StdDirIcon := iIcon;
  589. end;
  590. StdDirSelIcon := GetIconIndex(WinDir,
  591. FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY, SHGFI_OPENICON);
  592. end;
  593. end;
  594. type
  595. TDirViewState = class(TObject)
  596. public
  597. constructor Create;
  598. destructor Destroy; override;
  599. private
  600. HistoryPaths: TStrings;
  601. BackCount: Integer;
  602. SortStr: string;
  603. Mask: string;
  604. FocusedItem: string;
  605. FocusedShown: Boolean;
  606. ShownItemOffset: Integer;
  607. end;
  608. constructor TDirViewState.Create;
  609. begin
  610. inherited;
  611. end;
  612. destructor TDirViewState.Destroy;
  613. begin
  614. HistoryPaths.Free;
  615. inherited;
  616. end;
  617. function IsExecutable(FileName: string): Boolean;
  618. var
  619. FileExt: string;
  620. begin
  621. FileExt := UpperCase(ExtractFileExt(FileName));
  622. Result := (FileExt = '.EXE') or (FileExt = '.COM');
  623. end;
  624. function GetNextMask(var Mask: string): string;
  625. var
  626. NextPos: Integer;
  627. begin
  628. NextPos := Pos(';', Mask);
  629. if NextPos = 0 then
  630. begin
  631. Result := Mask;
  632. SetLength(Mask, 0);
  633. end
  634. else
  635. begin
  636. Result := Copy(Mask, 1, NextPos - 1);
  637. Delete(Mask, 1, NextPos);
  638. end;
  639. end;
  640. procedure DefaultFileFilter(var Filter: TFileFilter);
  641. begin
  642. with Filter do
  643. begin
  644. SetLength(Masks, 0);
  645. Directories := False;
  646. end;
  647. end;
  648. function StrCmpLogicalW(const sz1, sz2: UnicodeString): Integer; stdcall; external 'shlwapi.dll';
  649. function CompareLogicalTextPas(const S1, S2: string; NaturalOrderNumericalSorting: Boolean): Integer;
  650. begin
  651. // Keep in sync with CompareLogicalText
  652. if NaturalOrderNumericalSorting then
  653. Result := StrCmpLogicalW(PChar(S1), PChar(S2))
  654. else
  655. Result := lstrcmpi(PChar(S1), PChar(S2));
  656. // For deterministics results
  657. if Result = 0 then
  658. Result := lstrcmp(PChar(S1), PChar(S2));
  659. end;
  660. { Shortcut-handling }
  661. function ResolveFileShortCut(SourceFile: string; ShowDialog: Boolean = False): string;
  662. var
  663. IUnk: IUnknown;
  664. HRes: HRESULT; // OLE-Operation Result
  665. SL: IShellLink; // Interface for ShellLink
  666. PF: IPersistFile; // Interface for PersistentFile
  667. SRec: TWIN32FINDDATA; // SearchRec of targetfile
  668. TargetDir: array[1..Max_Path] of Char; // Working directory of targetfile
  669. Flags: DWORD;
  670. begin
  671. Result := '';
  672. IUnk := CreateComObject(CLSID_ShellLink);
  673. SL := IUnk as IShellLink;
  674. PF := IUnk as IPersistFile;
  675. HRes := PF.Load(PChar(SourceFile), STGM_READ);
  676. if Succeeded(Hres) then
  677. begin
  678. if not ShowDialog then Flags := SLR_NOUPDATE or (1500 shl 8) or SLR_NO_UI
  679. else Flags := SLR_NOUPDATE;
  680. HRes := SL.Resolve(Application.Handle, Flags);
  681. if Succeeded(HRes) then
  682. begin
  683. HRes := SL.GetPath(@TargetDir, MAX_PATH, SRec, {SLGP_UNCPRIORITY}{SLGP_SHORTPATH} 0);
  684. if Succeeded(HRes) then
  685. Result := string(PChar(@TargetDir));
  686. end;
  687. end;
  688. end; {ResolveShortCut}
  689. function CreateFileShortCut(SourceFile, Target, DisplayName: string;
  690. UpdateIfExists: Boolean): Boolean;
  691. var
  692. IUnk: IUnknown;
  693. Hres: HRESULT;
  694. ShellLink: IShellLink; // Interface to ShellLink
  695. IPFile: IPersistFile; // Interface to PersistentFile
  696. TargetFile: string;
  697. begin
  698. Result := False;
  699. if Target = '' then TargetFile := SourceFile + '.lnk'
  700. else TargetFile := Target;
  701. IUnk := CreateComObject(CLSID_ShellLink);
  702. ShellLink := IUnk as IShellLink;
  703. IPFile := IUnk as IPersistFile;
  704. if FileExists(ApiPath(TargetFile)) and UpdateIfExists then
  705. begin
  706. HRes := IPFile.Load(PChar(TargetFile), 0);
  707. if not Succeeded(HRes) then Exit;
  708. end;
  709. with ShellLink do
  710. begin
  711. HRes := SetPath(PChar(SourceFile));
  712. if Succeeded(HRes) then
  713. HRes := SetWorkingDirectory(PChar(ExtractFilePath(SourceFile)));
  714. if Succeeded(HRes) and (DisplayName <> '') then
  715. HRes := SetDescription(PChar(DisplayName));
  716. end;
  717. if Succeeded(Hres) then
  718. begin
  719. HRes := IPFile.Save(PChar(TargetFile),False);
  720. if Succeeded(HRes) then Result := True;
  721. end;
  722. end; {CreateShortCut}
  723. function OverlayImageList(Size: Integer): TImageList;
  724. procedure GetOverlayBitmap(ImageList: TImageList; BitmapName: string);
  725. var
  726. Bitmap: TBitmap;
  727. begin
  728. Bitmap := TBitmap.Create;
  729. try
  730. Bitmap.LoadFromResourceName(hInstance, BitmapName);
  731. ImageList.AddMasked(Bitmap, Bitmap.Canvas.Pixels[0, 0]);
  732. finally
  733. Bitmap.Free;
  734. end;
  735. end; {GetOverlayBitmap}
  736. begin
  737. // Hardcoded according to sizes of overlays we have in resources
  738. if Size >= 64 then Size := 64
  739. else
  740. if Size >= 48 then Size := 48
  741. else
  742. if Size >= 40 then Size := 40
  743. else
  744. if Size >= 32 then Size := 32
  745. else
  746. if Size >= 24 then Size := 24
  747. else
  748. if Size >= 20 then Size := 20
  749. else Size := 16;
  750. Result := TImageList.CreateSize(Size, Size);
  751. Result.DrawingStyle := dsTransparent;
  752. Result.BkColor := clNone;
  753. GetOverlayBitmap(Result, Format(ResDirUp, [Size]));
  754. GetOverlayBitmap(Result, Format(ResLink, [Size]));
  755. GetOverlayBitmap(Result, Format(ResBrokenLink, [Size]));
  756. GetOverlayBitmap(Result, Format(ResPartial, [Size]));
  757. GetOverlayBitmap(Result, Format(ResEncrypted, [Size]));
  758. end;
  759. { TCustomizableDragDropFilesEx }
  760. function TCustomizableDragDropFilesEx.Execute(DataObject: TDataObject): TDragResult;
  761. begin
  762. if not Assigned(DataObject) then
  763. begin
  764. DataObject := CreateDataObject;
  765. end;
  766. Result := ExecuteOperation(DataObject);
  767. end;
  768. { TCustomDirView }
  769. constructor TCustomDirView.Create(AOwner: TComponent);
  770. begin
  771. InitFileControls;
  772. inherited;
  773. FWatchForChanges := False;
  774. FFilesSize := 0;
  775. FFilesSelSize := 0;
  776. FDimmHiddenFiles := True;
  777. FShowHiddenFiles := True;
  778. FFormatSizeBytes := fbNone;
  779. FWantUseDragImages := False;
  780. FAddParentDir := False;
  781. FullDrag := True;
  782. FInvalidNameChars := coInvalidDosChars;
  783. FHasParentDir := False;
  784. FDragOnDriveIsMove := False;
  785. FCaseSensitive := False;
  786. FIsRecycleBin := False;
  787. FLoading := False;
  788. FLoadEnabled := True;
  789. FAbortLoading := False;
  790. FDirty := False;
  791. FLastPath := '';
  792. FHistoryPath := '';
  793. FNotifyEnabled := True;
  794. FForceRename := False;
  795. FLastRenameName := '';
  796. FSavedSelection := False;
  797. FPendingFocusSomething := -1;
  798. FSavedNames := TStringList.Create;
  799. FContextMenu := False;
  800. FUseSystemContextMenu := True;
  801. FStartPos.X := -1;
  802. FStartPos.Y := -1;
  803. FDragPos := FStartPos;
  804. FDragEnabled := False;
  805. FDDOwnerIsSource := False;
  806. FDDLinkOnExeDrag := False;
  807. FDragDrive := '';
  808. FExeDrag := False;
  809. FMask := '';
  810. FNaturalOrderNumericalSorting := True;
  811. FAlwaysSortDirectoriesByName := False;
  812. FDarkMode := False;
  813. FOnHistoryChange := nil;
  814. FOnPathChange := nil;
  815. FHistoryPaths := TStringList.Create;
  816. FBackCount := 0;
  817. FDontRecordPath := False;
  818. FMaxHistoryCount := DefaultHistoryCount;
  819. FStatusFileInfo.FilesCount := -1;
  820. OnCustomDrawItem := DumbCustomDrawItem;
  821. OnCustomDrawSubItem := DumbCustomDrawSubItem;
  822. FOnMatchMask := nil;
  823. FOnGetOverlay := nil;
  824. FOnGetItemColor := nil;
  825. FDragDropFilesEx := TCustomizableDragDropFilesEx.Create(Self);
  826. with FDragDropFilesEx do
  827. begin
  828. AutoDetectDnD := False;
  829. DragDetectDelta := 4;
  830. AcceptOwnDnD := True;
  831. BringToFront := True;
  832. CompleteFileList := True;
  833. NeedValid := [nvFileName];
  834. RenderDataOn := rdoEnterAndDropSync;
  835. TargetPopUpMenu := True;
  836. SourceEffects := DragSourceEffects;
  837. TargetEffects := [deCopy, deMove];
  838. OnDragEnter := DDDragEnter;
  839. OnDragLeave := DDDragLeave;
  840. OnDragOver := DDDragOver;
  841. OnDrop := DDDrop;
  842. OnQueryContinueDrag := DDQueryContinueDrag;
  843. OnSpecifyDropTarget := DDSpecifyDropTarget;
  844. OnMenuPopup := DDMenuPopup;
  845. OnMenuDestroy := DDMenuDone;
  846. OnDropHandlerSucceeded := DDDropHandlerSucceeded;
  847. OnGiveFeedback := DDGiveFeedback;
  848. OnProcessDropped := DDProcessDropped;
  849. OnDragDetect := DDDragDetect;
  850. end;
  851. FScrollOnDragOver := TListViewScrollOnDragOver.Create(Self, False);
  852. FScrollOnDragOver.OnBeforeUpdate := ScrollOnDragOverBeforeUpdate;
  853. FScrollOnDragOver.OnAfterUpdate := ScrollOnDragOverAfterUpdate;
  854. end;
  855. procedure TCustomDirView.ClearItems;
  856. begin
  857. CancelEdit;
  858. if Assigned(DropTarget) then DropTarget := nil;
  859. try
  860. inherited;
  861. finally
  862. FFilesSelSize := 0;
  863. FFilesSize := 0;
  864. DoUpdateStatusBar;
  865. end;
  866. end;
  867. procedure TCustomDirView.WMNotify(var Msg: TWMNotify);
  868. begin
  869. // This all is to make header text white in dark mode.
  870. if Msg.NMHdr.code = NM_CUSTOMDRAW then
  871. begin
  872. if DarkMode and SupportsDarkMode and
  873. GetSysDarkTheme and // When system app theme is light, headers are not dark
  874. (FHeaderHandle <> 0) and (Msg.NMHdr^.hWndFrom = FHeaderHandle) then
  875. begin
  876. with PNMLVCustomDraw(Msg.NMHdr)^ do
  877. begin
  878. if nmcd.dwDrawStage = CDDS_PREPAINT then
  879. begin
  880. inherited;
  881. Msg.Result := Msg.Result or CDRF_NOTIFYITEMDRAW;
  882. end
  883. else
  884. if nmcd.dwDrawStage = CDDS_ITEMPREPAINT then
  885. begin
  886. SetTextColor(nmcd.hdc, ColorToRGB(Font.Color));
  887. Msg.Result := CDRF_DODEFAULT;
  888. inherited;
  889. end
  890. else inherited;
  891. end;
  892. end
  893. else inherited;
  894. end
  895. else inherited;
  896. end;
  897. procedure TCustomDirView.DrawThumbnail(Item: TListItem; DC: HDC);
  898. var
  899. Rect: TRect;
  900. Thumbnail: TBitmap;
  901. Size: TSize;
  902. Left, Top: Integer;
  903. BlendFunction: TBlendFunction;
  904. ThumbnailDC: HDC;
  905. begin
  906. Rect := Item.DisplayRect(drIcon);
  907. // For thumbnails: The larger side (e.g. height for portrait oriented images) is rescaled to Size.Width
  908. // For icons: The "generated" images is exactly as requested
  909. Size.Height := MulDiv(Min(Rect.Width, Rect.Height), 9, 10);
  910. Size.Width := Size.Height;
  911. Thumbnail := ItemThumbnail(Item, Size);
  912. if not Assigned(Thumbnail) then
  913. Thumbnail := FallbackThumbnail(ItemIsDirectory(Item), Size);
  914. if Assigned(Thumbnail) then
  915. begin
  916. Left := Rect.Left + ((Rect.Width - Thumbnail.Width) div 2);
  917. Top := Rect.Bottom - Thumbnail.Height - MulDiv(Rect.Height, 1, 20); // Bottom-aligned, as Explorer does
  918. // https://stackoverflow.com/q/24595717/850848
  919. // https://stackoverflow.com/q/10028531/850848#10044325
  920. ThumbnailDC := CreateCompatibleDC(0);
  921. try
  922. SelectObject(ThumbnailDC, Thumbnail.Handle);
  923. BlendFunction.BlendOp := AC_SRC_OVER;
  924. BlendFunction.BlendFlags := 0;
  925. BlendFunction.SourceConstantAlpha := 255;
  926. BlendFunction.AlphaFormat := AC_SRC_ALPHA;
  927. AlphaBlend(DC, Left, Top, Thumbnail.Width, Thumbnail.Height, ThumbnailDC, 0, 0, Thumbnail.Width, Thumbnail.Height, BlendFunction);
  928. finally
  929. DeleteDC(ThumbnailDC);
  930. end;
  931. end;
  932. end;
  933. procedure TCustomDirView.CNNotify(var Message: TWMNotify);
  934. procedure DrawOverlayImage(DC: HDC; Image: Integer; Item: TListItem);
  935. var
  936. OverlayImages: TCustomImageList;
  937. Rect: TRect;
  938. Point: TPoint;
  939. Index: Integer;
  940. begin
  941. Rect := Item.DisplayRect(drIcon);
  942. Point := Rect.TopLeft;
  943. if ViewStyle = vsIcon then
  944. begin
  945. OverlayImages := FOverlayLargeImages;
  946. end
  947. else
  948. begin
  949. OverlayImages := FOverlaySmallImages;
  950. end;
  951. // center on the rect
  952. Inc(Point.X, (Rect.Width - OverlayImages.Width) div 2);
  953. Inc(Point.Y, (Rect.Height - OverlayImages.Height) div 2);
  954. Index := 0;
  955. while Image > 1 do
  956. begin
  957. Inc(Index);
  958. Image := Image shr 1;
  959. end;
  960. if (ViewStyle <> vsReport) or
  961. (8 + OverlayImages.Width <= Columns[0].Width) then
  962. begin
  963. ImageList_Draw(OverlayImages.Handle, Index, DC, Point.X, Point.Y, ILD_TRANSPARENT);
  964. end;
  965. end;
  966. var
  967. FileSize: Int64;
  968. Item: TListItem;
  969. InfoMask: LongWord;
  970. OverlayIndex: Word;
  971. OverlayIndexes: Word;
  972. UpdateStatusBarPending: Boolean;
  973. Nmcd: PNMCustomDraw;
  974. begin
  975. UpdateStatusBarPending := False;
  976. case Message.NMHdr^.code of
  977. LVN_ITEMCHANGED:
  978. with PNMListView(Message.NMHdr)^ do
  979. if (uChanged = LVIF_STATE) and Valid and (not FClearingItems) then
  980. begin
  981. if ((uOldState and (LVIS_SELECTED or LVIS_FOCUSED)) <> (uNewState and (LVIS_SELECTED or LVIS_FOCUSED))) then
  982. begin
  983. Item := Items[iItem];
  984. UpdateStatusBarPending := True;
  985. if (uOldState and LVIS_SELECTED) <> (uNewState and LVIS_SELECTED) then
  986. begin
  987. FileSize := ItemFileSize(Item);
  988. if (uOldState and LVIS_SELECTED) <> 0 then Dec(FFilesSelSize, FileSize)
  989. else Inc(FFilesSelSize, FileSize);
  990. end;
  991. if (uOldState and LVIS_FOCUSED) <> (uNewState and LVIS_FOCUSED) then
  992. begin
  993. if Assigned(OnChangeFocus) then
  994. OnChangeFocus(Self, Item);
  995. end;
  996. end;
  997. end;
  998. LVN_ENDLABELEDIT:
  999. // enable loading now only when editing was canceled.
  1000. // when it was confirmed, it will be enabled only after actual
  1001. // file renaming is completed. see Edit().
  1002. with PLVDispInfo(Message.NMHdr)^ do
  1003. if (item.pszText = nil) or (item.IItem = -1) then
  1004. LoadEnabled := True;
  1005. LVN_BEGINDRAG:
  1006. if FDragEnabled and (not Loading) then
  1007. begin
  1008. DDBeforeDrag;
  1009. DDDragDetect(MK_LBUTTON, FStartPos, Mouse.CursorPos, ddsDrag);
  1010. end;
  1011. LVN_BEGINRDRAG:
  1012. if FDragEnabled and (not Loading) then
  1013. begin
  1014. DDBeforeDrag;
  1015. DDDragDetect(MK_RBUTTON, FStartPos, Mouse.CursorPos, ddsDrag);
  1016. end;
  1017. end;
  1018. inherited;
  1019. if Message.NMHdr.code = LVN_GETDISPINFO then
  1020. begin
  1021. if FNotifyEnabled and Valid and (not Loading) then
  1022. with PLVDispInfo(Pointer(Message.NMHdr))^.Item do
  1023. try
  1024. InfoMask := PLVDispInfo(Pointer(Message.NMHdr))^.item.Mask;
  1025. if (InfoMask and LVIF_PARAM) <> 0 then Item := TListItem(lParam)
  1026. else
  1027. if iItem < Items.Count then Item := Items[iItem]
  1028. else Item := nil;
  1029. if Assigned(Item) and Assigned(Item.Data) then
  1030. GetDisplayInfo(Item, PLVDispInfo(Pointer(Message.NMHdr))^.item);
  1031. except
  1032. end;
  1033. end;
  1034. if (Message.NMHdr.code = NM_CUSTOMDRAW) and Valid and (not Loading) then
  1035. begin
  1036. Nmcd := @PNMLVCustomDraw(Message.NMHdr).nmcd;
  1037. try
  1038. if (Nmcd.dwDrawStage and CDDS_SUBITEM) = 0 then
  1039. begin
  1040. if (Nmcd.dwDrawStage and CDDS_ITEMPREPAINT) = CDDS_ITEMPREPAINT then
  1041. begin
  1042. Message.Result := Message.Result or CDRF_NOTIFYPOSTPAINT;
  1043. end
  1044. else
  1045. if ((Nmcd.dwDrawStage and CDDS_ITEMPOSTPAINT) = CDDS_ITEMPOSTPAINT) and
  1046. (Nmcd.rc.Width > 0) then // We get called many times with empty rect while view is recreating (e.g. when switching to thumnails mode)
  1047. begin
  1048. Item := Items[Nmcd.dwItemSpec];
  1049. if IsItemVisible(Item) then // particularly the thumbnail drawing is expensive
  1050. begin
  1051. if FThumbnail then
  1052. begin
  1053. DrawThumbnail(Item, Nmcd.hdc);
  1054. end
  1055. else
  1056. begin
  1057. OverlayIndexes := ItemOverlayIndexes(Item);
  1058. OverlayIndex := 1;
  1059. while OverlayIndexes > 0 do
  1060. begin
  1061. if (OverlayIndex and OverlayIndexes) <> 0 then
  1062. begin
  1063. DrawOverlayImage(Nmcd.hdc, OverlayIndex, Item);
  1064. Dec(OverlayIndexes, OverlayIndex);
  1065. end;
  1066. OverlayIndex := OverlayIndex shl 1;
  1067. end;
  1068. end;
  1069. end;
  1070. end;
  1071. end;
  1072. except
  1073. end;
  1074. end;
  1075. if UpdateStatusBarPending then DoUpdateStatusBar;
  1076. end;
  1077. function TCustomDirView.FileNameMatchesMasks(FileName: string;
  1078. Directory: Boolean; Size: Int64; Modification: TDateTime; Masks: string;
  1079. AllowImplicitMatches: Boolean): Boolean;
  1080. begin
  1081. Result := False;
  1082. if Assigned(OnMatchMask) then
  1083. OnMatchMask(Self, FileName, Directory, Size, Modification, Masks, Result, AllowImplicitMatches)
  1084. end;
  1085. procedure TCustomDirView.SetAddParentDir(Value: Boolean);
  1086. begin
  1087. if FAddParentDir <> Value then
  1088. begin
  1089. FAddParentDir := Value;
  1090. if DirOK then Reload(True);
  1091. end;
  1092. end;
  1093. procedure TCustomDirView.SetDimmHiddenFiles(Value: Boolean);
  1094. begin
  1095. if Value <> FDimmHiddenFiles then
  1096. begin
  1097. FDimmHiddenFiles := Value;
  1098. Self.Repaint;
  1099. end;
  1100. end; {SetDimmHiddenFiles}
  1101. procedure TCustomDirView.SetPathLabel(Value: TCustomPathLabel);
  1102. begin
  1103. if FPathLabel <> Value then
  1104. begin
  1105. if Assigned(FPathLabel) and (FPathLabel.FocusControl = Self) then
  1106. FPathLabel.FocusControl := nil;
  1107. FPathLabel := Value;
  1108. if Assigned(Value) then
  1109. begin
  1110. Value.FreeNotification(Self);
  1111. if not Assigned(Value.FocusControl) then
  1112. Value.FocusControl := Self;
  1113. UpdatePathLabel;
  1114. end;
  1115. end;
  1116. end; { SetPathLabel }
  1117. procedure TCustomDirView.SetShowHiddenFiles(Value: Boolean);
  1118. begin
  1119. if ShowHiddenFiles <> Value then
  1120. begin
  1121. FShowHiddenFiles := Value;
  1122. if DirOK then Reload(False);
  1123. end;
  1124. end;
  1125. procedure TCustomDirView.SetFormatSizeBytes(Value: TFormatBytesStyle);
  1126. begin
  1127. if Value <> FFormatSizeBytes then
  1128. begin
  1129. FFormatSizeBytes := Value;
  1130. Self.Repaint;
  1131. end;
  1132. end; {SetFormatSizeBytes}
  1133. function TCustomDirView.GetDragSourceEffects: TDropEffectSet;
  1134. begin
  1135. Result := [deCopy, deMove, deLink];
  1136. end;
  1137. function TCustomDirView.GetUseDragImages: Boolean;
  1138. begin
  1139. Result := FWantUseDragImages;
  1140. end;
  1141. procedure TCustomDirView.SetTargetPopupMenu(Value: Boolean);
  1142. begin
  1143. if Assigned(FDragDropFilesEx) then FDragDropFilesEx.TargetPopupMenu := Value;
  1144. end;
  1145. function TCustomDirView.NeedImageList(Size: TImageListSize; Recreate: Boolean; var OverlayImages: TImageList): TImageList;
  1146. begin
  1147. Result := ShellImageListForControl(Self, Size);
  1148. if (not Assigned(OverlayImages)) or Recreate then
  1149. begin
  1150. FreeAndNil(OverlayImages);
  1151. OverlayImages := OverlayImageList(Result.Width);
  1152. end;
  1153. end;
  1154. procedure TCustomDirView.NeedImageLists(Recreate: Boolean);
  1155. var
  1156. ALargeImages: TImageList;
  1157. ThumbnailSize: Integer;
  1158. begin
  1159. SmallImages := NeedImageList(ilsSmall, Recreate, FOverlaySmallImages);
  1160. ALargeImages := NeedImageList(ilsLarge, Recreate, FOverlayLargeImages);
  1161. if FThumbnail then
  1162. begin
  1163. ThumbnailSize := ScaleByPixelsPerInch(128, Self);
  1164. // ShellImageListForSize would normally prefer smaller icons (for row sizing purposes).
  1165. // But for thumbnails, we prefer larger version as will will scale it when painting.
  1166. // The *2 is hackish way to achieve that.
  1167. FThumbnailShellImages := ShellImageListForSize(ThumbnailSize * 2);
  1168. if (not Assigned(FThumbnailImages)) or (FThumbnailImages.Width <> ThumbnailSize) then
  1169. begin
  1170. if Assigned(FThumbnailImages) then
  1171. FreeAndNil(FThumbnailImages);
  1172. // Dummy image list, whose sole purpose it to autosize the items in the view
  1173. FThumbnailImages := TImageList.CreateSize(ThumbnailSize, ThumbnailSize);
  1174. end;
  1175. LargeImages := FThumbnailImages
  1176. end
  1177. else LargeImages := ALargeImages;
  1178. end;
  1179. procedure TCustomDirView.CMDPIChanged(var Message: TMessage);
  1180. begin
  1181. inherited;
  1182. NeedImageLists(True);
  1183. end;
  1184. const
  1185. RequiredStyles = LVS_EX_DOUBLEBUFFER or LVS_EX_TRANSPARENTBKGND;
  1186. procedure TCustomDirView.CMEnabledChanged(var Message: TMessage);
  1187. var
  1188. ListViewStyle: DWORD;
  1189. begin
  1190. inherited;
  1191. // We need this so that we can control background color of disabled file panel for dark theme.
  1192. // See comment in LVMSetExtendedListViewStyle for an explanation.
  1193. ListViewStyle := ListView_GetExtendedListViewStyle(Handle);
  1194. if Enabled then
  1195. begin
  1196. ListView_SetExtendedListViewStyle(Handle, (ListViewStyle or RequiredStyles));
  1197. end
  1198. else
  1199. begin
  1200. ListView_SetExtendedListViewStyle(Handle, (ListViewStyle and (not RequiredStyles)));
  1201. end;
  1202. end;
  1203. procedure TCustomDirView.FreeImageLists;
  1204. begin
  1205. FreeAndNil(FOverlaySmallImages);
  1206. FreeAndNil(FOverlayLargeImages);
  1207. FreeAndNil(FThumbnailImages);
  1208. SmallImages := nil;
  1209. LargeImages := nil;
  1210. end;
  1211. procedure TCustomDirView.WMThemeChanged(var Message: TMessage);
  1212. begin
  1213. if SupportsDarkMode then // To reduce impact
  1214. begin
  1215. UpdateDarkMode;
  1216. RedrawWindow(Handle, nil, 0, RDW_FRAME or RDW_INVALIDATE);
  1217. end;
  1218. inherited;
  1219. end;
  1220. procedure TCustomDirView.UpdateDarkMode;
  1221. begin
  1222. if SupportsDarkMode then // To reduce impact
  1223. begin
  1224. AllowDarkModeForWindow(Self, DarkMode);
  1225. if FHeaderHandle <> 0 then
  1226. begin
  1227. AllowDarkModeForWindow(FHeaderHandle, DarkMode);
  1228. SendMessage(FHeaderHandle, WM_THEMECHANGED, 0, 0);
  1229. end;
  1230. end;
  1231. end;
  1232. procedure TCustomDirView.CreateWnd;
  1233. begin
  1234. inherited;
  1235. if Assigned(PopupMenu) then
  1236. PopupMenu.Autopopup := False;
  1237. FDragDropFilesEx.DragDropControl := Self;
  1238. if SupportsDarkMode then
  1239. begin
  1240. // This enables dark mode - List view itself supports dark mode somewhat even in the our 'Explorer' theme.
  1241. // The 'ItemsView' has better (Explorer-like) dark mode selection color, but on the other hand it does not have dark scrollbars.
  1242. // win32-darkmode has ugly fix for that (FixDarkScrollBar), which we do not want to employ.
  1243. // The 'DarkMode_Explorer' uses the standard selection color (bright blue).
  1244. // Enables dark headers:
  1245. SetWindowTheme(FHeaderHandle, 'ItemsView', nil);
  1246. if DarkMode then UpdateDarkMode;
  1247. end;
  1248. NeedImageLists(False);
  1249. end;
  1250. procedure TCustomDirView.LVMSetExtendedListViewStyle(var Message: TMessage);
  1251. // Only TWinControl.DoubleBuffered actually prevents flicker
  1252. // on Win7 when moving mouse over list view, not LVS_EX_DOUBLEBUFFER.
  1253. // But LVS_EX_DOUBLEBUFFER brings nice alpha blended marquee selection.
  1254. // Double buffering introduces artefacts when scrolling using
  1255. // keyboard (Page-up/Down). This gets fixed by LVS_EX_TRANSPARENTBKGND,
  1256. // but that works on Vista and newer only. See WMKeyDown
  1257. // for workaround on earlier systems.
  1258. begin
  1259. // This prevents TCustomListView.ResetExStyles resetting our styles
  1260. if Enabled and
  1261. (Message.WParam = 0) and
  1262. ((Message.LParam and RequiredStyles) <> RequiredStyles) then
  1263. begin
  1264. ListView_SetExtendedListViewStyle(Handle, Message.LParam or RequiredStyles);
  1265. end
  1266. else
  1267. begin
  1268. inherited;
  1269. end;
  1270. end;
  1271. procedure TCustomDirView.DestroyWnd;
  1272. begin
  1273. // to force drag&drop re-registration when recreating handle
  1274. // (occurs when changing ViewStyle)
  1275. FDragDropFilesEx.DragDropControl := nil;
  1276. inherited;
  1277. end;
  1278. procedure TCustomDirView.CMRecreateWnd(var Message: TMessage);
  1279. var
  1280. HadHandle: Boolean;
  1281. begin
  1282. Inc(FRecreatingWnd);
  1283. if FRecreatingWnd = 1 then
  1284. begin
  1285. HadHandle := HandleAllocated;
  1286. try
  1287. // Prevent nesting
  1288. while FRecreatingWnd > 0 do
  1289. begin
  1290. inherited;
  1291. Dec(FRecreatingWnd);
  1292. end;
  1293. finally
  1294. FRecreatingWnd := 0;
  1295. end;
  1296. // See comment in TCustomDriveView.CMRecreateWnd
  1297. if HadHandle then
  1298. begin
  1299. HandleNeeded;
  1300. end;
  1301. end;
  1302. end;
  1303. function TCustomDirView.DoItemColor(Item: TListItem): TColor;
  1304. var
  1305. Precision: TDateTimePrecision;
  1306. begin
  1307. Result := clDefaultItemColor;
  1308. if Assigned(OnGetItemColor) then
  1309. begin
  1310. OnGetItemColor(Self, ItemFullFileName(Item), ItemIsDirectory(Item), ItemFileSize(Item), ItemFileTime(Item, Precision), Result);
  1311. end;
  1312. end;
  1313. procedure TCustomDirView.DoCustomDrawItem(Item: TListItem; Stage: TCustomDrawStage);
  1314. var
  1315. Color: TColor;
  1316. begin
  1317. if (Item <> nil) and (Stage = cdPrePaint) then
  1318. begin
  1319. Color := DoItemColor(Item);
  1320. if Color = clDefaultItemColor then Color := ItemColor(Item);
  1321. if (Color <> clDefaultItemColor) and
  1322. (Canvas.Font.Color <> Color) then
  1323. begin
  1324. Canvas.Font.Color := Color;
  1325. end;
  1326. end;
  1327. end;
  1328. function TCustomDirView.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  1329. Stage: TCustomDrawStage): Boolean;
  1330. begin
  1331. DoCustomDrawItem(Item, Stage);
  1332. Result := inherited CustomDrawItem(Item, State, Stage);
  1333. end;
  1334. function TCustomDirView.CustomDrawSubItem(Item: TListItem; SubItem: Integer;
  1335. State: TCustomDrawState; Stage: TCustomDrawStage): Boolean;
  1336. begin
  1337. DoCustomDrawItem(Item, Stage);
  1338. Result := inherited CustomDrawSubItem(Item, SubItem, State, Stage);
  1339. end;
  1340. procedure TCustomDirView.Delete(Item: TListItem);
  1341. begin
  1342. if Assigned(Item) then
  1343. begin
  1344. // This causes access violation when size is stored in structure
  1345. // pointed by TListItem->Data and this structure is not valid any more
  1346. if Valid then Dec(FFilesSize, ItemFileSize(Item));
  1347. inherited Delete(Item);
  1348. end;
  1349. end;
  1350. destructor TCustomDirView.Destroy;
  1351. begin
  1352. Assert(not FSavedSelection);
  1353. FreeAndNil(FScrollOnDragOver);
  1354. FreeAndNil(FSavedNames);
  1355. FreeAndNil(FHistoryPaths);
  1356. FreeAndNil(FDragDropFilesEx);
  1357. FreeImageLists;
  1358. FreeThumbnails;
  1359. inherited;
  1360. end;
  1361. procedure TCustomDirView.SelectFiles(Filter: TFileFilter; Select: Boolean);
  1362. var
  1363. Item: TListItem;
  1364. Index: Integer;
  1365. OldCursor: TCursor;
  1366. begin
  1367. Assert(Valid);
  1368. OldCursor := Screen.Cursor;
  1369. Items.BeginUpdate;
  1370. BeginSelectionUpdate;
  1371. try
  1372. Screen.Cursor := crHourGlass;
  1373. for Index := 0 to Items.Count-1 do
  1374. begin
  1375. Item := Items[Index];
  1376. Assert(Assigned(Item));
  1377. if (GetItemSelectedByIndex(Index) <> Select) and
  1378. ItemMatchesFilter(Item, Filter) then
  1379. SetItemSelectedByIndex(Index, Select);
  1380. end;
  1381. finally
  1382. Screen.Cursor := OldCursor;
  1383. Items.EndUpdate;
  1384. EndSelectionUpdate;
  1385. end;
  1386. end;
  1387. function TCustomDirView.DragCompleteFileList: Boolean;
  1388. begin
  1389. Result := (MarkedCount <= 100) and (not IsRecycleBin);
  1390. end;
  1391. procedure TCustomDirView.DumbCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  1392. begin
  1393. end;
  1394. procedure TCustomDirView.DumbCustomDrawSubItem(Sender: TCustomListView;
  1395. Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  1396. var DefaultDraw: Boolean);
  1397. begin
  1398. end;
  1399. function TCustomDirView.GetTargetPopupMenu: Boolean;
  1400. begin
  1401. if Assigned(FDragDropFilesEx) then Result := FDragDropFilesEx.TargetPopupMenu
  1402. else Result := True;
  1403. end;
  1404. procedure TCustomDirView.SetMultiSelect(Value: Boolean);
  1405. begin
  1406. if Value <> MultiSelect then
  1407. begin
  1408. inherited SetMultiSelect(Value);
  1409. if not (csLoading in ComponentState) and Assigned(ColProperties) then
  1410. begin
  1411. ColProperties.RecreateColumns;
  1412. SetColumnImages;
  1413. if DirOK then Reload(True);
  1414. end;
  1415. end;
  1416. end;
  1417. function TCustomDirView.GetValid: Boolean;
  1418. begin
  1419. Result := (not (csDestroying in ComponentState)) and
  1420. (not Loading) and (not FClearingItems);
  1421. end;
  1422. function TCustomDirView.ItemCanDrag(Item: TListItem): Boolean;
  1423. begin
  1424. Result := (not ItemIsParentDirectory(Item));
  1425. end;
  1426. function TCustomDirView.ItemColor(Item: TListItem): TColor;
  1427. begin
  1428. Result := clDefaultItemColor;
  1429. end;
  1430. function TCustomDirView.ItemThumbnail(Item: TListItem; Size: TSize): TBitmap;
  1431. begin
  1432. Result := nil;
  1433. end;
  1434. procedure TCustomDirView.FreeThumbnails;
  1435. begin
  1436. FreeAndNil(FFallbackThumbnail[True]);
  1437. FreeAndNil(FFallbackThumbnail[False]);
  1438. end;
  1439. function TCustomDirView.FallbackThumbnail(Dir: Boolean; Size: TSize): TBitmap;
  1440. var
  1441. FallbackPath: string;
  1442. Existed: Boolean;
  1443. Index: Integer;
  1444. begin
  1445. Result := nil;
  1446. try
  1447. if FFallbackThumbnailSize <> Size then
  1448. begin
  1449. FreeThumbnails;
  1450. end;
  1451. if not Assigned(FFallbackThumbnail[Dir]) then
  1452. begin
  1453. Index := 1;
  1454. repeat
  1455. FallbackPath := TPath.Combine(TempDir, 'default.' + IntToStr(Index) + '.thumbnailimage');
  1456. Existed := FileExists(FallbackPath) or DirectoryExists(FallbackPath);
  1457. if not Existed then
  1458. begin
  1459. if Dir then
  1460. CreateDir(FallbackPath)
  1461. else
  1462. TFile.WriteAllText(FallbackPath, '');
  1463. end;
  1464. Inc(Index);
  1465. until not Existed;
  1466. FFallbackThumbnailSize := Size;
  1467. FFallbackThumbnail[Dir] := GetThumbnail(FallbackPath, Size);
  1468. if Existed then
  1469. begin
  1470. if Dir then
  1471. RemoveDir(FallbackPath)
  1472. else
  1473. DeleteFile(FallbackPath);
  1474. end;
  1475. end;
  1476. Result := FFallbackThumbnail[Dir];
  1477. except
  1478. end;
  1479. end;
  1480. function TCustomDirView.GetFilesMarkedSize: Int64;
  1481. begin
  1482. if SelCount > 0 then Result := FilesSelSize
  1483. else
  1484. if Assigned(ItemFocused) then Result := ItemFileSize(ItemFocused)
  1485. else Result := 0;
  1486. end;
  1487. function TCustomDirView.ItemIsRecycleBin(Item: TListItem): Boolean;
  1488. begin
  1489. Result := False;
  1490. end;
  1491. function TCustomDirView.ItemOverlayIndexes(Item: TListItem): Word;
  1492. begin
  1493. Result := oiNoOverlay;
  1494. if Assigned(OnGetOverlay) then
  1495. OnGetOverlay(Self, Item, Result);
  1496. end;
  1497. procedure TCustomDirView.DoDisplayPropertiesMenu;
  1498. begin
  1499. if not IsBusy then
  1500. DisplayPropertiesMenu;
  1501. end;
  1502. procedure TCustomDirView.DoExecute(Item: TListItem; ForceEnter: Boolean);
  1503. begin
  1504. BusyOperation(procedure begin Execute(Item, ForceEnter); end);
  1505. end;
  1506. procedure TCustomDirView.DoExecuteParentDirectory;
  1507. begin
  1508. BusyOperation(ExecuteParentDirectory);
  1509. end;
  1510. procedure TCustomDirView.CNKeyDown(var Message: TWMKeyDown);
  1511. begin
  1512. // Prevent Backspace being handled via "Parent directory" command in the context menu.
  1513. // We want it handled here in KeyDown
  1514. // (among other as the mechanism there makes sure it works differently while incrementally searching).
  1515. if Message.CharCode <> VK_BACK then
  1516. begin
  1517. inherited;
  1518. end;
  1519. end;
  1520. procedure TCustomDirView.KeyDown(var Key: Word; Shift: TShiftState);
  1521. var
  1522. AKey: Word;
  1523. begin
  1524. if Valid and (not IsEditing) and (not Loading) then
  1525. begin
  1526. if (Key = VK_RETURN) or
  1527. ((Key = VK_NEXT) and (ssCtrl in Shift)) then
  1528. begin
  1529. if Assigned(ItemFocused) then
  1530. begin
  1531. AKey := Key;
  1532. Key := 0;
  1533. if (AKey = VK_RETURN) and (Shift = [ssAlt]) then DoDisplayPropertiesMenu
  1534. else
  1535. if (AKey <> VK_RETURN) or (Shift = []) then DoExecute(ItemFocused, (AKey <> VK_RETURN));
  1536. end;
  1537. end
  1538. else
  1539. if ((Key = VK_BACK) or ((Key = VK_PRIOR) and (ssCtrl in Shift))) and
  1540. (not IsRoot) then
  1541. begin
  1542. inherited;
  1543. // If not handled in TCustomScpExplorerForm::DirViewKeyDown
  1544. if Key <> 0 then
  1545. begin
  1546. Key := 0;
  1547. DoExecuteParentDirectory;
  1548. end;
  1549. end
  1550. else
  1551. if ((Key = VK_UP) and (ssAlt in Shift)) and
  1552. (not IsRoot) then
  1553. begin
  1554. Key := 0;
  1555. // U+25D8 is 'INVERSE BULLET', what is glyph representing '\x8' (or '\b')
  1556. // ('up' key is the '8' key on numeric pad)
  1557. // We could obtain the value programatically using
  1558. // MultiByteToWideChar(CP_OEMCP, MB_USEGLYPHCHARS, "\x8", 1, ...)
  1559. FNextCharToIgnore := $25D8;
  1560. DoExecuteParentDirectory;
  1561. end
  1562. else
  1563. if (Key = 220 { backslash }) and (ssCtrl in Shift) and (not IsRoot) then
  1564. begin
  1565. Key := 0;
  1566. BusyOperation(ExecuteRootDirectory);
  1567. end
  1568. else
  1569. if (Key = VK_LEFT) and (ssAlt in Shift) then
  1570. begin
  1571. if BackCount >= 1 then DoHistoryGo(-1);
  1572. end
  1573. else
  1574. if (Key = VK_RIGHT) and (ssAlt in Shift) then
  1575. begin
  1576. if ForwardCount >= 1 then DoHistoryGo(1);
  1577. end
  1578. else
  1579. begin
  1580. inherited;
  1581. end;
  1582. end
  1583. else
  1584. begin
  1585. inherited;
  1586. end;
  1587. end;
  1588. procedure TCustomDirView.KeyPress(var Key: Char);
  1589. begin
  1590. if IsEditing and (Pos(Key, FInvalidNameChars) <> 0) Then
  1591. begin
  1592. Beep;
  1593. Key := #0;
  1594. end;
  1595. inherited;
  1596. end;
  1597. procedure TCustomDirView.DisplayContextMenuInSitu;
  1598. var
  1599. R: TRect;
  1600. P: TPoint;
  1601. begin
  1602. if Assigned(ItemFocused) then
  1603. begin
  1604. R := ItemFocused.DisplayRect(drIcon);
  1605. P.X := (R.Left + R.Right) div 2;
  1606. P.Y := (R.Top + R.Bottom) div 2;
  1607. end
  1608. else
  1609. begin
  1610. P.X := 0;
  1611. P.Y := 0;
  1612. end;
  1613. P := ClientToScreen(P);
  1614. DisplayContextMenu(P);
  1615. end;
  1616. procedure TCustomDirView.KeyUp(var Key: Word; Shift: TShiftState);
  1617. var
  1618. P: TPoint;
  1619. begin
  1620. if Key = VK_APPS then
  1621. begin
  1622. if (not Loading) and (not IsBusy) then
  1623. begin
  1624. if MarkedCount > 0 then
  1625. begin
  1626. DisplayContextMenuInSitu;
  1627. end
  1628. else
  1629. if Assigned(PopupMenu) then
  1630. begin
  1631. P.X := 0;
  1632. P.Y := 0;
  1633. P := ClientToScreen(P);
  1634. PopupMenu.Popup(P.X, P.Y);
  1635. end;
  1636. end;
  1637. end
  1638. else
  1639. inherited KeyUp(Key, Shift);
  1640. end;
  1641. procedure TCustomDirView.SetWatchForChanges(Value: Boolean);
  1642. begin
  1643. if FWatchForChanges <> Value then
  1644. FWatchForChanges := Value;
  1645. end;
  1646. function TCustomDirView.TargetHasDropHandler(Item: TListItem; Effect: Integer): Boolean;
  1647. begin
  1648. Assert(Assigned(DragDropFilesEx) and Assigned(Item));
  1649. Result :=
  1650. DragDropFilesEx.TargetHasDropHandler(nil, ItemFullFileName(Item), Effect);
  1651. if Assigned(OnDDTargetHasDropHandler) then
  1652. begin
  1653. OnDDTargetHasDropHandler(Self, Item, Effect, Result);
  1654. end;
  1655. end;
  1656. procedure TCustomDirView.UpdatePathLabelCaption;
  1657. begin
  1658. PathLabel.Caption := PathName;
  1659. // Do not display mask on otherwise empty label (i.e. on a disconnected remote panel)
  1660. if PathLabel.Caption <> '' then PathLabel.Mask := Mask
  1661. else PathLabel.Mask := '';
  1662. end;
  1663. procedure TCustomDirView.UpdatePathLabel;
  1664. begin
  1665. if Assigned(PathLabel) then
  1666. begin
  1667. if csDesigning in ComponentState then
  1668. begin
  1669. PathLabel.Caption := PathLabel.Name;
  1670. PathLabel.Mask := '';
  1671. end
  1672. else
  1673. begin
  1674. UpdatePathLabelCaption;
  1675. end;
  1676. PathLabel.UpdateStatus;
  1677. end;
  1678. end; { UpdatePathLabel }
  1679. procedure TCustomDirView.DoUpdateStatusBar(Force: Boolean);
  1680. var
  1681. StatusFileInfo: TStatusFileInfo;
  1682. begin
  1683. if (FUpdatingSelection = 0) and Assigned(OnUpdateStatusBar) then
  1684. begin
  1685. with StatusFileInfo do
  1686. begin
  1687. SelectedSize := FilesSelSize;
  1688. FilesSize := Self.FilesSize;
  1689. SelectedCount := SelCount;
  1690. FilesCount := Self.FilesCount;
  1691. HiddenCount := Self.HiddenCount;
  1692. FilteredCount := Self.FilteredCount;
  1693. end;
  1694. if Force or (not CompareMem(@StatusFileInfo, @FStatusFileInfo, SizeOf(StatusFileInfo))) then
  1695. begin
  1696. FStatusFileInfo := StatusFileInfo;
  1697. OnUpdateStatusBar(Self, FStatusFileInfo);
  1698. end;
  1699. end;
  1700. end; { UpdateStatusBar }
  1701. procedure TCustomDirView.UpdateStatusBar;
  1702. begin
  1703. DoUpdateStatusBar(True);
  1704. end;
  1705. procedure TCustomDirView.WMContextMenu(var Message: TWMContextMenu);
  1706. var
  1707. Point: TPoint;
  1708. begin
  1709. FDragEnabled := False;
  1710. if Assigned(PopupMenu) then
  1711. PopupMenu.AutoPopup := False;
  1712. //inherited;
  1713. if FContextMenu and (not Loading) then
  1714. begin
  1715. Point.X := Message.XPos;
  1716. Point.Y := Message.YPos;
  1717. Point := ScreenToClient(Point);
  1718. if Assigned(OnMouseDown) then
  1719. begin
  1720. OnMouseDown(Self, mbRight, [], Point.X, Point.Y);
  1721. end;
  1722. if FUseSystemContextMenu and Assigned(ItemFocused) and
  1723. (GetItemAt(Point.X, Point.Y) = ItemFocused) then
  1724. begin
  1725. Point.X := Message.XPos;
  1726. Point.Y := Message.YPos;
  1727. DisplayContextMenu(Point);
  1728. end
  1729. else
  1730. if Assigned(PopupMenu) and (not PopupMenu.AutoPopup) then
  1731. begin
  1732. PopupMenu.Popup(Message.XPos, Message.YPos);
  1733. end;
  1734. end;
  1735. FContextMenu := False;
  1736. //inherited;
  1737. end;
  1738. function TCustomDirView.EnableDragOnClick: Boolean;
  1739. begin
  1740. Result := (not Loading) and inherited EnableDragOnClick;
  1741. end;
  1742. procedure TCustomDirView.WMLButtonDown(var Message: TWMLButtonDown);
  1743. begin
  1744. GetCursorPos(FStartPos);
  1745. FDragEnabled := EnableDragOnClick;
  1746. inherited;
  1747. end;
  1748. procedure TCustomDirView.WMRButtonDown(var Message: TWMRButtonDown);
  1749. begin
  1750. GetCursorPos(FStartPos);
  1751. if FDragDropFilesEx.DragDetectStatus <> ddsDrag then
  1752. FDragEnabled := EnableDragOnClick;
  1753. FContextMenu := True;
  1754. inherited;
  1755. end;
  1756. procedure TCustomDirView.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  1757. begin
  1758. inherited;
  1759. if Assigned(ItemFocused) and (not Loading) and
  1760. (GetItemAt(Message.XPos, Message.YPos) = ItemFocused) then
  1761. begin
  1762. if GetKeyState(VK_MENU) < 0 then DoDisplayPropertiesMenu
  1763. else DoExecute(ItemFocused, False);
  1764. end;
  1765. end;
  1766. procedure TCustomDirView.WMLButtonUp(var Message: TWMLButtonUp);
  1767. begin
  1768. FDragEnabled := False;
  1769. inherited;
  1770. end;
  1771. procedure TCustomDirView.WMXButtonUp(var Message: TWMXMouse);
  1772. begin
  1773. if Message.Button = _XBUTTON1 then
  1774. begin
  1775. if BackCount >= 1 then DoHistoryGo(-1);
  1776. Message.Result := 1;
  1777. end
  1778. else
  1779. if Message.Button = _XBUTTON2 then
  1780. begin
  1781. if ForwardCount >= 1 then DoHistoryGo(1);
  1782. Message.Result := 1;
  1783. end;
  1784. end;
  1785. procedure TCustomDirView.CancelEdit;
  1786. begin
  1787. // - Do nothing when handle is not allocated (we cannot be editing anyway
  1788. // without a handle), otherwise this causes handle allocation,
  1789. // what is wrong particularly when we are called from ClearItems
  1790. // when we are being destroyed
  1791. // - If editing, it has to be focused item
  1792. if HandleAllocated and IsEditing and Assigned(ItemFocused) then
  1793. begin
  1794. ItemFocused.CancelEdit;
  1795. FLoadEnabled := True;
  1796. end;
  1797. end;
  1798. procedure TCustomDirView.Reload(CacheIcons: Boolean);
  1799. var
  1800. OldSelection: TStringList;
  1801. OldItemFocused: string;
  1802. OldFocusedShown: Boolean;
  1803. OldShownItemOffset: Integer;
  1804. Index: Integer;
  1805. FoundIndex: Integer;
  1806. IconCache: TStringList;
  1807. Item: TListItem;
  1808. ItemToFocus: TListItem;
  1809. FileName: string;
  1810. begin
  1811. if Path <> '' then
  1812. begin
  1813. CancelEdit;
  1814. OldSelection := nil;
  1815. IconCache := nil;
  1816. Items.BeginUpdate;
  1817. try
  1818. OldSelection := TStringList.Create;
  1819. OldSelection.CaseSensitive := FCaseSensitive;
  1820. if CacheIcons then
  1821. IconCache := TStringList.Create;
  1822. for Index := 0 to Items.Count-1 do
  1823. begin
  1824. Item := Items[Index];
  1825. // cannot use ItemFileName as for TUnixDirView the file object
  1826. // is no longer valid
  1827. FileName := Item.Caption;
  1828. if GetItemSelectedByIndex(Index) then
  1829. OldSelection.Add(FileName);
  1830. if CacheIcons and (ItemImageIndex(Item, True) >= 0) then
  1831. IconCache.AddObject(FileName, TObject(ItemImageIndex(Item, True)));
  1832. end;
  1833. if FSelectFile <> '' then
  1834. begin
  1835. OldItemFocused := FSelectFile;
  1836. OldFocusedShown := False;
  1837. OldShownItemOffset := -1;
  1838. FSelectFile := '';
  1839. end
  1840. else
  1841. begin
  1842. SaveItemsState(OldItemFocused, OldFocusedShown, OldShownItemOffset);
  1843. end;
  1844. Load(False);
  1845. OldSelection.Sort;
  1846. if CacheIcons then IconCache.Sort;
  1847. ItemToFocus := nil;
  1848. for Index := 0 to Items.Count - 1 do
  1849. begin
  1850. Item := Items[Index];
  1851. FileName := ItemFileName(Item);
  1852. if FileName = OldItemFocused then
  1853. ItemToFocus := Item;
  1854. if OldSelection.Find(FileName, FoundIndex) then
  1855. SetItemSelectedByIndex(Index, True);
  1856. if CacheIcons and (ItemImageIndex(Item, True) < 0) then
  1857. begin
  1858. FoundIndex := IconCache.IndexOf(FileName);
  1859. if FoundIndex >= 0 then
  1860. SetItemImageIndex(Item, Integer(IconCache.Objects[FoundIndex]));
  1861. end;
  1862. end;
  1863. finally
  1864. Items.EndUpdate;
  1865. OldSelection.Free;
  1866. if CacheIcons then IconCache.Free;
  1867. end;
  1868. // This is below Items.EndUpdate(), to make Scroll() work properly
  1869. RestoreItemsState(ItemToFocus, OldFocusedShown, OldShownItemOffset);
  1870. FocusSomething(False);
  1871. end;
  1872. end;
  1873. procedure TCustomDirView.Load(DoFocusSomething: Boolean);
  1874. var
  1875. SaveCursor: TCursor;
  1876. Delimiters: string;
  1877. LastDirName: string;
  1878. ForceMakeVisible: Boolean;
  1879. begin
  1880. if not FLoadEnabled or Loading then
  1881. begin
  1882. FDirty := True;
  1883. FAbortLoading := True;
  1884. end
  1885. else
  1886. begin
  1887. FLoading := True;
  1888. FInsertingNewUnselectedItem := True;
  1889. try
  1890. FHasParentDir := False;
  1891. if Assigned(FOnStartLoading) then FOnStartLoading(Self);
  1892. SaveCursor := Screen.Cursor;
  1893. Screen.Cursor := crHourGlass;
  1894. try
  1895. FNotifyEnabled := False;
  1896. ClearItems;
  1897. FFilesSize := 0;
  1898. FFilesSelSize := 0;
  1899. SortType := stNone;
  1900. Items.BeginUpdate;
  1901. try
  1902. LoadFiles;
  1903. finally
  1904. Items.EndUpdate;
  1905. end;
  1906. finally
  1907. Screen.Cursor := SaveCursor;
  1908. end;
  1909. finally
  1910. FLoading := False;
  1911. FInsertingNewUnselectedItem := False;
  1912. try
  1913. if FAbortLoading then
  1914. begin
  1915. FAbortLoading := False;
  1916. Reload(False);
  1917. end
  1918. else
  1919. begin
  1920. if DirOK then SortItems;
  1921. FAbortLoading := False;
  1922. FDirty := False;
  1923. if (Length(LastPath) > Length(PathName)) and
  1924. (Copy(LastPath, 1, Length(PathName)) = PathName) and
  1925. (Items.Count > 0) then
  1926. begin
  1927. LastDirName := Copy(LastPath, Length(PathName) + 1, MaxInt);
  1928. Delimiters := '\:/';
  1929. if IsDelimiter(Delimiters, LastDirName, 1) then
  1930. begin
  1931. LastDirName := Copy(LastDirName, 2, MaxInt);
  1932. end;
  1933. if LastDelimiter(Delimiters, LastDirName) = 0 then
  1934. begin
  1935. ItemFocused := FindFileItem(LastDirName);
  1936. end;
  1937. end;
  1938. end;
  1939. finally
  1940. // nested try .. finally block is included
  1941. // because we really want these to be executed
  1942. FNotifyEnabled := True;
  1943. if DoFocusSomething then
  1944. begin
  1945. ForceMakeVisible := True;
  1946. if FAnnouncedState is TDirViewState then
  1947. begin
  1948. // "AnnouncedState" should not be combined with "LastPath"
  1949. Assert(not Assigned(ItemFocused));
  1950. RestoreItemsState(FAnnouncedState);
  1951. ForceMakeVisible := False;
  1952. end;
  1953. FocusSomething(ForceMakeVisible);
  1954. end;
  1955. if Assigned(FOnLoaded) then
  1956. begin
  1957. FOnLoaded(Self);
  1958. end;
  1959. UpdatePathLabel;
  1960. DoUpdateStatusBar;
  1961. end;
  1962. end;
  1963. end;
  1964. end;
  1965. procedure TCustomDirView.SetLoadEnabled(Enabled: Boolean);
  1966. begin
  1967. if Enabled <> LoadEnabled then
  1968. begin
  1969. FLoadEnabled := Enabled;
  1970. if Enabled and Dirty then Reload(True);
  1971. end;
  1972. end;
  1973. function TCustomDirView.GetFilesCount: Integer;
  1974. begin
  1975. Result := Items.Count;
  1976. if (Result > 0) and HasParentDir then Dec(Result);
  1977. end;
  1978. procedure TCustomDirView.ViewStyleChanged;
  1979. begin
  1980. // this is workaround for bug in TCustomNortonLikeListView
  1981. // that clears Items on recreating wnd (caused by change to ViewStyle)
  1982. Reload(True);
  1983. end;
  1984. procedure TCustomDirView.SetViewStyle(Value: TViewStyle);
  1985. begin
  1986. if (Value <> ViewStyle) and (not FLoading) then
  1987. begin
  1988. FNotifyEnabled := False;
  1989. inherited;
  1990. FNotifyEnabled := True;
  1991. ViewStyleChanged;
  1992. end;
  1993. end;
  1994. procedure TCustomDirView.ColClick(Column: TListColumn);
  1995. var
  1996. ScrollToFocused: Boolean;
  1997. begin
  1998. ScrollToFocused := Assigned(ItemFocused);
  1999. inherited;
  2000. if ScrollToFocused and Assigned(ItemFocused) then
  2001. ItemFocused.MakeVisible(False);
  2002. end;
  2003. procedure TCustomDirView.CustomSortItems(SortProc: Pointer);
  2004. var
  2005. SavedCursor: TCursor;
  2006. SavedNotifyEnabled: Boolean;
  2007. begin
  2008. if HandleAllocated then
  2009. begin
  2010. SavedNotifyEnabled := FNotifyEnabled;
  2011. SavedCursor := Screen.Cursor;
  2012. Items.BeginUpdate;
  2013. try
  2014. Screen.Cursor := crHourglass;
  2015. FNotifyEnabled := False;
  2016. CustomSort(TLVCompare(SortProc), Integer(Pointer(Self)));
  2017. finally
  2018. Screen.Cursor := SavedCursor;
  2019. FNotifyEnabled := SavedNotifyEnabled;
  2020. Items.EndUpdate;
  2021. ItemsReordered;
  2022. end;
  2023. end;
  2024. end;
  2025. procedure TCustomDirView.ReloadForce(CacheIcons: Boolean);
  2026. begin
  2027. FLoadEnabled := True;
  2028. FDirty := False;
  2029. Reload(CacheIcons);
  2030. end;
  2031. procedure TCustomDirView.ScrollOnDragOverBeforeUpdate(ObjectToValidate: TObject);
  2032. begin
  2033. GlobalDragImageList.HideDragImage;
  2034. end;
  2035. procedure TCustomDirView.ScrollOnDragOverAfterUpdate;
  2036. begin
  2037. GlobalDragImageList.ShowDragImage;
  2038. end;
  2039. procedure TCustomDirView.DDDragEnter(DataObj: IDataObject; grfKeyState: Longint;
  2040. Point: TPoint; var dwEffect: longint; var Accept: Boolean);
  2041. var
  2042. Index: Integer;
  2043. begin
  2044. Accept := Accept and DirOK and (not Loading);
  2045. if Accept and
  2046. (DragDropFilesEx.FileList.Count > 0) and
  2047. (Length(TFDDListItem(DragDropFilesEx.FileList[0]^).Name) > 0) and
  2048. (not IsRecycleBin or not DragDropFilesEx.FileNamesAreMapped) then
  2049. begin
  2050. try
  2051. FDragDrive := DriveInfo.GetDriveKey(TFDDListItem(DragDropFilesEx.FileList[0]^).Name);
  2052. except
  2053. // WinRAR gives us only filename on "enter", we get a full path only on "drop".
  2054. FDragDrive := '';
  2055. end;
  2056. FExeDrag := FDDLinkOnExeDrag and
  2057. (deLink in DragDropFilesEx.TargetEffects) and
  2058. ((DragDropFilesEx.AvailableDropEffects and DROPEFFECT_LINK) <> 0);
  2059. if FExeDrag then
  2060. begin
  2061. for Index := 0 to DragDropFilesEx.FileList.Count - 1 do
  2062. if not IsExecutable(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name) then
  2063. begin
  2064. FExeDrag := False;
  2065. Break;
  2066. end;
  2067. end;
  2068. end
  2069. else
  2070. begin
  2071. FDragDrive := '';
  2072. end;
  2073. FScrollOnDragOver.StartDrag;
  2074. if Assigned(FOnDDDragEnter) then
  2075. FOnDDDragEnter(Self, DataObj, grfKeyState, Point, dwEffect, Accept);
  2076. end;
  2077. procedure TCustomDirView.DDDragLeave;
  2078. begin
  2079. if Assigned(DropTarget) then
  2080. begin
  2081. if GlobalDragImageList.Dragging then
  2082. GlobalDragImageList.HideDragImage;
  2083. DropTarget := nil;
  2084. Update; {ie30}
  2085. end
  2086. else DropTarget := nil;
  2087. if Assigned(FOnDDDragLeave) then
  2088. FOnDDDragLeave(Self);
  2089. end;
  2090. procedure TCustomDirView.DDDragOver(grfKeyState: Integer; Point: TPoint;
  2091. var dwEffect: Integer; PreferredEffect: Integer);
  2092. var
  2093. DropItem: TListItem;
  2094. CanDrop: Boolean;
  2095. HasDropHandler: Boolean;
  2096. begin
  2097. FDDOwnerIsSource := DragDropFilesEx.OwnerIsSource;
  2098. {Set droptarget if target is directory:}
  2099. if not Loading then DropItem := GetItemAt(Point.X, Point.Y)
  2100. else DropItem := nil;
  2101. HasDropHandler := (Assigned(DropItem) and (not IsRecycleBin) and
  2102. TargetHasDropHandler(DropItem, dwEffect));
  2103. CanDrop := Assigned(DropItem) and (not IsRecycleBin) and
  2104. (ItemIsDirectory(DropItem) or HasDropHandler);
  2105. if (CanDrop and (DropTarget <> DropItem)) or
  2106. (not CanDrop and Assigned(DropTarget)) then
  2107. begin
  2108. if GlobalDragImageList.Dragging then
  2109. begin
  2110. GlobalDragImageList.HideDragImage;
  2111. DropTarget := nil;
  2112. Update;
  2113. if CanDrop then
  2114. begin
  2115. DropTarget := DropItem;
  2116. Update;
  2117. end;
  2118. GlobalDragImageList.ShowDragImage;
  2119. end
  2120. else
  2121. begin
  2122. DropTarget := nil;
  2123. if CanDrop then DropTarget := DropItem;
  2124. end;
  2125. end;
  2126. if not Loading then
  2127. FScrollOnDragOver.DragOver(Point);
  2128. {Set dropeffect:}
  2129. if (not HasDropHandler) and (not Loading) then
  2130. begin
  2131. DDChooseEffect(grfKeyState, dwEffect, PreferredEffect);
  2132. if Assigned(FOnDDDragOver) then
  2133. FOnDDDragOver(Self, grfKeyState, Point, dwEffect);
  2134. // cannot drop to dragged files
  2135. if DragDropFilesEx.OwnerIsSource and Assigned(DropItem) then
  2136. begin
  2137. if Assigned(ItemFocused) and (not ItemFocused.Selected) then
  2138. begin
  2139. if DropItem = ItemFocused then
  2140. begin
  2141. dwEffect := DROPEFFECT_NONE;
  2142. end;
  2143. end
  2144. else
  2145. if DropItem.Selected then
  2146. begin
  2147. dwEffect := DROPEFFECT_NONE;
  2148. end;
  2149. end;
  2150. if DragDropFilesEx.OwnerIsSource and (dwEffect = DROPEFFECT_MOVE) and
  2151. (not Assigned(DropTarget)) then
  2152. begin
  2153. dwEffect := DROPEFFECT_NONE;
  2154. end
  2155. else
  2156. if Assigned(DropTarget) and ItemIsRecycleBin(DropTarget) and
  2157. (dwEffect <> DROPEFFECT_NONE) then
  2158. begin
  2159. dwEffect := DROPEFFECT_MOVE;
  2160. end;
  2161. end;
  2162. end;
  2163. function TCustomDirView.ItemData(Item: TListItem): TObject;
  2164. begin
  2165. Result := nil;
  2166. end;
  2167. function TCustomDirView.OperateOnFocusedFile(Focused, OnlyFocused: Boolean): Boolean;
  2168. begin
  2169. Result :=
  2170. Assigned(ItemFocused) and
  2171. ((Focused and (not ItemFocused.Selected)) or (SelCount = 0) or OnlyFocused);
  2172. end;
  2173. function TCustomDirView.CustomCreateFileList(Focused, OnlyFocused: Boolean;
  2174. FullPath: Boolean; FileList: TStrings; ItemObject: Boolean): TStrings;
  2175. procedure AddItem(Item: TListItem);
  2176. var
  2177. AObject: TObject;
  2178. begin
  2179. Assert(Assigned(Item));
  2180. if ItemObject then AObject := Item
  2181. else AObject := ItemData(Item);
  2182. if FullPath then Result.AddObject(ItemFullFileName(Item), AObject)
  2183. else Result.AddObject(ItemFileName(Item), AObject);
  2184. end;
  2185. var
  2186. Item: TListItem;
  2187. begin
  2188. if Assigned(FileList) then Result := FileList
  2189. else Result := TStringList.Create;
  2190. try
  2191. if OperateOnFocusedFile(Focused, OnlyFocused) then
  2192. begin
  2193. AddItem(ItemFocused)
  2194. end
  2195. else
  2196. begin
  2197. Item := GetNextItem(nil, sdAll, [isSelected]);
  2198. while Assigned(Item) do
  2199. begin
  2200. AddItem(Item);
  2201. Item := GetNextItem(Item, sdAll, [isSelected]);
  2202. end;
  2203. end;
  2204. except
  2205. if not Assigned(FileList) then FreeAndNil(Result);
  2206. raise;
  2207. end;
  2208. end;
  2209. function TCustomDirView.CreateFocusedFileList(FullPath: Boolean; FileList: TStrings): TStrings;
  2210. begin
  2211. Result := CustomCreateFileList(False, True, FullPath, FileList);
  2212. end;
  2213. function TCustomDirView.CreateFileList(Focused: Boolean; FullPath: Boolean;
  2214. FileList: TStrings; ItemObject: Boolean): TStrings;
  2215. begin
  2216. Result := CustomCreateFileList(Focused, False, FullPath, FileList, ItemObject);
  2217. end;
  2218. procedure TCustomDirView.DDDrop(DataObj: IDataObject; grfKeyState: Integer;
  2219. Point: TPoint; var dwEffect: Integer);
  2220. begin
  2221. if GlobalDragImageList.Dragging then
  2222. GlobalDragImageList.HideDragImage;
  2223. if dwEffect = DROPEFFECT_NONE then
  2224. DropTarget := nil;
  2225. if Assigned(OnDDDrop) then
  2226. OnDDDrop(Self, DataObj, grfKeyState, Point, dwEffect);
  2227. end;
  2228. procedure TCustomDirView.DDQueryContinueDrag(FEscapePressed: LongBool;
  2229. grfKeyState: Integer; var Result: HResult);
  2230. var
  2231. MousePos: TPoint;
  2232. KnowTime: TFileTime;
  2233. begin
  2234. // this method cannot throw exceptions, if it does d&d will not be possible
  2235. // anymore (see TDragDrop.ExecuteOperation, global GInternalSource)
  2236. if Result = DRAGDROP_S_DROP then
  2237. begin
  2238. GetSystemTimeAsFileTime(KnowTime);
  2239. if ((Int64(KnowTime) - Int64(FDragStartTime)) <= DDDragStartDelay) then
  2240. Result := DRAGDROP_S_CANCEL;
  2241. end;
  2242. if Assigned(OnDDQueryContinueDrag) then
  2243. begin
  2244. OnDDQueryContinueDrag(Self, FEscapePressed, grfKeyState, Result);
  2245. end;
  2246. try
  2247. if FEscapePressed then
  2248. begin
  2249. if GlobalDragImageList.Dragging then
  2250. GlobalDragImageList.HideDragImage;
  2251. end
  2252. else
  2253. begin
  2254. if GlobalDragImageList.Dragging Then
  2255. begin
  2256. MousePos := ParentForm.ScreenToClient(Mouse.CursorPos);
  2257. {Move the drag image to the new position and show it:}
  2258. if (MousePos.X <> FDragPos.X) or (MousePos.Y <> FDragPos.Y) then
  2259. begin
  2260. FDragPos := MousePos;
  2261. if PtInRect(ParentForm.BoundsRect, Mouse.CursorPos) then
  2262. begin
  2263. GlobalDragImageList.DragMove(MousePos.X, MousePos.Y);
  2264. GlobalDragImageList.ShowDragImage;
  2265. end
  2266. else GlobalDragImageList.HideDragImage;
  2267. end;
  2268. end;
  2269. end;
  2270. except
  2271. // do not care if the above fails
  2272. // (Mouse.CursorPos fails when desktop is locked by user)
  2273. end;
  2274. end;
  2275. procedure TCustomDirView.DDSpecifyDropTarget(Sender: TObject;
  2276. DragDropHandler: Boolean; Point: TPoint; var pidlFQ: PItemIDList;
  2277. var Filename: string);
  2278. var
  2279. Item: TListItem;
  2280. begin
  2281. pidlFQ := nil;
  2282. if DirOK and (not Loading) then
  2283. begin
  2284. if DragDropHandler then
  2285. begin
  2286. if Assigned(DropTarget) and ItemIsDirectory(DropTarget) then
  2287. FileName := ItemFullFileName(DropTarget)
  2288. else
  2289. FileName := PathName;
  2290. end
  2291. else
  2292. begin
  2293. Item := GetItemAt(Point.X, Point.Y);
  2294. if Assigned(Item) and (not ItemIsDirectory(Item)) and
  2295. (not IsRecycleBin) then
  2296. FileName := ItemFullFileName(Item)
  2297. else
  2298. FileName := '';
  2299. end;
  2300. end
  2301. else FileName := '';
  2302. end;
  2303. procedure TCustomDirView.DDMenuPopup(Sender: TObject; AMenu: HMenu;
  2304. DataObj: IDataObject; AMinCustCmd: Integer; grfKeyState: Longint; pt: TPoint);
  2305. begin
  2306. end;
  2307. procedure TCustomDirView.DDMenuDone(Sender: TObject; AMenu: HMenu);
  2308. begin
  2309. end;
  2310. procedure TCustomDirView.DDDropHandlerSucceeded(Sender: TObject;
  2311. grfKeyState: Integer; Point: TPoint; dwEffect: Integer);
  2312. begin
  2313. DropTarget := nil;
  2314. end;
  2315. procedure TCustomDirView.DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer; PreferredEffect: Integer);
  2316. begin
  2317. if Assigned(FOnDDChooseEffect) then
  2318. FOnDDChooseEffect(Self, grfKeyState, dwEffect);
  2319. end;
  2320. procedure TCustomDirView.DDGiveFeedback(dwEffect: Integer;
  2321. var Result: HResult);
  2322. begin
  2323. if Assigned(FOnDDGiveFeedback) then
  2324. FOnDDGiveFeedback(Self, dwEffect, Result);
  2325. end;
  2326. procedure TCustomDirView.DDProcessDropped(Sender: TObject;
  2327. grfKeyState: Integer; Point: TPoint; dwEffect: Integer);
  2328. begin
  2329. if DirOK and (not Loading) then
  2330. try
  2331. try
  2332. if Assigned(FOnDDProcessDropped) then
  2333. FOnDDProcessDropped(Self, grfKeyState, Point, dwEffect);
  2334. if dwEffect <> DROPEFFECT_NONE then
  2335. begin
  2336. PerformItemDragDropOperation(DropTarget, dwEffect, False);
  2337. if Assigned(FOnDDExecuted) then
  2338. FOnDDExecuted(Self, dwEffect);
  2339. end;
  2340. finally
  2341. DragDropFilesEx.FileList.Clear;
  2342. DropTarget := nil;
  2343. end;
  2344. except
  2345. Application.HandleException(Self);
  2346. end;
  2347. end;
  2348. function TCustomDirView.AnyFileSelected(
  2349. OnlyFocused: Boolean; FilesOnly: Boolean; FocusedFileOnlyWhenFocused: Boolean): Boolean;
  2350. var
  2351. Item: TListItem;
  2352. begin
  2353. if OnlyFocused or
  2354. ((SelCount = 0) and
  2355. ((not FocusedFileOnlyWhenFocused) or
  2356. (Focused and (GetParentForm(Self).Handle = GetForegroundWindow())))) then
  2357. begin
  2358. Result := Assigned(ItemFocused) and ItemIsFile(ItemFocused) and
  2359. ((not FilesOnly) or (not ItemIsDirectory(ItemFocused)));
  2360. end
  2361. else
  2362. begin
  2363. Result := True;
  2364. Item := GetNextItem(nil, sdAll, [isSelected]);
  2365. while Assigned(Item) do
  2366. begin
  2367. if ItemIsFile(Item) and
  2368. ((not FilesOnly) or (not ItemIsDirectory(Item))) then Exit;
  2369. Item := GetNextItem(Item, sdAll, [isSelected]);
  2370. end;
  2371. Result := False;
  2372. end;
  2373. end;
  2374. function TCustomDirView.CanEdit(Item: TListItem): Boolean;
  2375. begin
  2376. Result :=
  2377. (inherited CanEdit(Item) or FForceRename) and (not Loading) and
  2378. Assigned(Item) and (not ReadOnly) and (not IsRecycleBin) and
  2379. (not ItemIsParentDirectory(Item));
  2380. if Result then FLoadEnabled := False;
  2381. FForceRename := False;
  2382. end;
  2383. function TCustomDirView.CanChangeSelection(Item: TListItem;
  2384. Select: Boolean): Boolean;
  2385. begin
  2386. Result :=
  2387. (not Loading) and
  2388. not (Assigned(Item) and Assigned(Item.Data) and
  2389. ItemIsParentDirectory(Item));
  2390. end;
  2391. procedure TCustomDirView.Edit(const HItem: TLVItem);
  2392. var
  2393. Info: string;
  2394. Index: Integer;
  2395. begin
  2396. // When rename is confirmed by clicking outside of the edit box, and the actual rename operation
  2397. // displays error message or simply pumps a message queue (like during lenghty remote directory reload),
  2398. // drag mouse selection start. It posssibly happens only on the remote panel due to it being completely reloaded.
  2399. ReleaseCapture;
  2400. if Length(HItem.pszText) = 0 then LoadEnabled := True
  2401. else
  2402. begin
  2403. {Does the changed filename contains invalid characters?}
  2404. if StrContains(FInvalidNameChars, HItem.pszText) then
  2405. begin
  2406. Info := FInvalidNameChars;
  2407. for Index := Length(Info) downto 1 do
  2408. System.Insert(Space, Info, Index);
  2409. MessageBeep(MB_ICONHAND);
  2410. if MessageDlg(SErrorInvalidName + Space + Info, mtError,
  2411. [mbOK, mbAbort], 0) = mrOK then RetryRename(HItem.pszText);
  2412. LoadEnabled := True;
  2413. end
  2414. else
  2415. begin
  2416. InternalEdit(HItem);
  2417. end;
  2418. end;
  2419. end; {Edit}
  2420. procedure TCustomDirView.EndSelectionUpdate;
  2421. begin
  2422. inherited;
  2423. if FUpdatingSelection = 0 then
  2424. DoUpdateStatusBar;
  2425. end; { EndUpdatingSelection }
  2426. procedure TCustomDirView.ExecuteCurrentFile;
  2427. begin
  2428. Assert(Assigned(ItemFocused));
  2429. Execute(ItemFocused, False);
  2430. end;
  2431. function TCustomDirView.DoExecFile(Item: TListItem; ForceEnter: Boolean): Boolean;
  2432. begin
  2433. Result := True;
  2434. if Assigned(FOnExecFile) then FOnExecFile(Self, Item, Result);
  2435. end;
  2436. procedure TCustomDirView.Execute(Item: TListItem; ForceEnter: Boolean);
  2437. begin
  2438. Assert(Assigned(Item));
  2439. if Assigned(Item) and Assigned(Item.Data) and (not Loading) then
  2440. begin
  2441. if IsRecycleBin and (not ItemIsParentDirectory(Item)) then DisplayPropertiesMenu
  2442. else
  2443. if DoExecFile(Item, ForceEnter) then
  2444. begin
  2445. if ItemIsParentDirectory(Item) then ExecuteParentDirectory
  2446. else ExecuteFile(Item);
  2447. end;
  2448. end;
  2449. end;
  2450. procedure TCustomDirView.GetDisplayInfo(ListItem: TListItem;
  2451. var DispInfo: TLVItem);
  2452. begin
  2453. // Nothing
  2454. end;
  2455. procedure TCustomDirView.WMUserRename(var Message: TMessage);
  2456. var
  2457. Dummy: Boolean;
  2458. begin
  2459. if Assigned(ItemFocused) then
  2460. begin
  2461. FForceRename := True;
  2462. ListView_EditLabel(Handle, ItemFocused.Index);
  2463. SetWindowText(ListView_GetEditControl(Self.Handle),
  2464. PChar(FLastRenameName));
  2465. // This was called already by VCL.
  2466. // But we do it again for the base-name selection side effect this has in TCustomScpExplorerForm::DirViewEditing,
  2467. // after we have updated the text above.
  2468. if Assigned(OnEditing) then
  2469. OnEditing(Self, ItemFocused, Dummy);
  2470. end;
  2471. end;
  2472. procedure TCustomDirView.RetryRename(NewName: string);
  2473. begin
  2474. FLastRenameName := NewName;
  2475. PostMessage(Self.Handle, WM_USER_RENAME, Longint(PChar(NewName)), 0);
  2476. end;
  2477. procedure TCustomDirView.AddToDragFileList(FileList: TFileList; Item: TListItem);
  2478. begin
  2479. FileList.AddItem(nil, ItemFullFileName(Item));
  2480. end;
  2481. procedure TCustomDirView.DDDragDetect(grfKeyState: Integer; DetectStart,
  2482. Point: TPoint; DragStatus: TDragDetectStatus);
  2483. var
  2484. FilesCount: Integer;
  2485. DirsCount: Integer;
  2486. Item: TListItem;
  2487. FirstItem : TListItem;
  2488. Bitmap: TBitmap;
  2489. ImageListHandle: HImageList;
  2490. Spot: TPoint;
  2491. ItemPos: TPoint;
  2492. DragText: string;
  2493. ClientPoint: TPoint;
  2494. FileListCreated: Boolean;
  2495. AvoidDragImage: Boolean;
  2496. DataObject: TDataObject;
  2497. begin
  2498. if Assigned(FOnDDDragDetect) then
  2499. FOnDDDragDetect(Self, grfKeyState, DetectStart, Point, DragStatus);
  2500. FLastDDResult := drCancelled;
  2501. if (DragStatus = ddsDrag) and (not Loading) and (MarkedCount > 0) then
  2502. begin
  2503. DragDropFilesEx.CompleteFileList := DragCompleteFileList;
  2504. DragDropFilesEx.FileList.Clear;
  2505. FirstItem := nil;
  2506. FilesCount := 0;
  2507. DirsCount := 0;
  2508. FileListCreated := False;
  2509. AvoidDragImage := False;
  2510. if Assigned(OnDDCreateDragFileList) then
  2511. begin
  2512. OnDDCreateDragFileList(Self, DragDropFilesEx.FileList, FileListCreated);
  2513. if FileListCreated then
  2514. begin
  2515. AvoidDragImage := True;
  2516. end;
  2517. end;
  2518. if not FileListCreated then
  2519. begin
  2520. if Assigned(ItemFocused) and (not ItemFocused.Selected) then
  2521. begin
  2522. if ItemCanDrag(ItemFocused) then
  2523. begin
  2524. FirstItem := ItemFocused;
  2525. AddToDragFileList(DragDropFilesEx.FileList, ItemFocused);
  2526. if ItemIsDirectory(ItemFocused) then Inc(DirsCount)
  2527. else Inc(FilesCount);
  2528. end;
  2529. end
  2530. else
  2531. if SelCount > 0 then
  2532. begin
  2533. Item := GetNextItem(nil, sdAll, [isSelected]);
  2534. while Assigned(Item) do
  2535. begin
  2536. if ItemCanDrag(Item) then
  2537. begin
  2538. if not Assigned(FirstItem) then FirstItem := Item;
  2539. AddToDragFileList(DragDropFilesEx.FileList, Item);
  2540. if ItemIsDirectory(Item) then Inc(DirsCount)
  2541. else Inc(FilesCount);
  2542. end;
  2543. Item := GetNextItem(Item, sdAll, [isSelected]);
  2544. end;
  2545. end;
  2546. end;
  2547. if DragDropFilesEx.FileList.Count > 0 then
  2548. begin
  2549. FDragEnabled := False;
  2550. {Create the dragimage:}
  2551. GlobalDragImageList := DragImageList;
  2552. // This code is not used anymore
  2553. if UseDragImages and (not AvoidDragImage) then
  2554. begin
  2555. ImageListHandle := ListView_CreateDragImage(Handle, FirstItem.Index, Spot);
  2556. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2557. if ImageListHandle <> Invalid_Handle_Value then
  2558. begin
  2559. GlobalDragImageList.Handle := ImageListHandle;
  2560. if FilesCount + DirsCount = 1 then
  2561. begin
  2562. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2563. GlobalDragImageList.SetDragImage(0,
  2564. DetectStart.X - ItemPos.X, DetectStart.Y - ItemPos.Y);
  2565. end
  2566. else
  2567. begin
  2568. GlobalDragImageList.Clear;
  2569. GlobalDragImageList.Width := 32;
  2570. GlobalDragImageList.Height := 32;
  2571. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES', 0,
  2572. [lrTransparent], $FFFFFF) Then
  2573. begin
  2574. Bitmap := TBitmap.Create;
  2575. try
  2576. try
  2577. GlobalDragImageList.GetBitmap(0, Bitmap);
  2578. Bitmap.Canvas.Font.Assign(Self.Font);
  2579. DragText := '';
  2580. if FilesCount > 0 then
  2581. DragText := Format(STextFiles, [FilesCount]);
  2582. if DirsCount > 0 then
  2583. begin
  2584. if FilesCount > 0 then
  2585. DragText := DragText + ', ';
  2586. DragText := DragText + Format(STextDirectories, [DirsCount]);
  2587. end;
  2588. Bitmap.Width := 33 + Bitmap.Canvas.TextWidth(DragText);
  2589. Bitmap.TransparentMode := tmAuto;
  2590. Bitmap.Canvas.TextOut(33,
  2591. Max(24 - Abs(Canvas.Font.Height), 0), DragText);
  2592. GlobalDragImageList.Clear;
  2593. GlobalDragImageList.Width := Bitmap.Width;
  2594. GlobalDragImageList.AddMasked(Bitmap,
  2595. Bitmap.Canvas.Pixels[0, 0]);
  2596. GlobalDragImageList.SetDragImage(0, 25, 20);
  2597. except
  2598. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES',
  2599. 0, [lrTransparent], $FFFFFF) then
  2600. GlobalDragImageList.SetDragImage(0, 25, 20);
  2601. end;
  2602. finally
  2603. Bitmap.Free;
  2604. end;
  2605. end;
  2606. end;
  2607. ClientPoint := ParentForm.ScreenToClient(Point);
  2608. GlobalDragImageList.BeginDrag(ParentForm.Handle,
  2609. ClientPoint.X, ClientPoint.Y);
  2610. GlobalDragImageList.HideDragImage;
  2611. ShowCursor(True);
  2612. end;
  2613. end;
  2614. FContextMenu := False;
  2615. if IsRecycleBin then DragDropFilesEx.SourceEffects := [deMove]
  2616. else DragDropFilesEx.SourceEffects := DragSourceEffects;
  2617. DropSourceControl := Self;
  2618. try
  2619. GetSystemTimeAsFileTime(FDragStartTime);
  2620. DataObject := nil;
  2621. if Assigned(OnDDCreateDataObject) then
  2622. begin
  2623. OnDDCreateDataObject(Self, DataObject);
  2624. end;
  2625. {Execute the drag&drop-Operation:}
  2626. FLastDDResult := DragDropFilesEx.Execute(DataObject);
  2627. // The drag&drop operation is finished, so clean up the used drag image.
  2628. // This also restores the default mouse cursor
  2629. // (which is set to "none" in GlobalDragImageList.BeginDrag above)
  2630. // But it's actually too late, we would need to do it when mouse button
  2631. // is realesed already. Otherwise the cursor is hidden when hovering over
  2632. // main window, while target application is processing dropped file
  2633. // (particularly when Explorer displays progress window or
  2634. // overwrite confirmation prompt)
  2635. GlobalDragImageList.EndDrag;
  2636. GlobalDragImageList.Clear;
  2637. Application.ProcessMessages;
  2638. finally
  2639. DragDropFilesEx.FileList.Clear;
  2640. FContextMenu := False;
  2641. try
  2642. if Assigned(OnDDEnd) then
  2643. OnDDEnd(Self);
  2644. finally
  2645. DropTarget := nil;
  2646. DropSourceControl := nil;
  2647. end;
  2648. end;
  2649. end;
  2650. end;
  2651. end;
  2652. procedure TCustomDirView.Notification(AComponent: TComponent; Operation: TOperation);
  2653. begin
  2654. inherited;
  2655. if Operation = opRemove then
  2656. begin
  2657. if AComponent = PathLabel then FPathLabel := nil;
  2658. end;
  2659. end; { Notification }
  2660. procedure TCustomDirView.WMAppCommand(var Message: TMessage);
  2661. var
  2662. Command: Integer;
  2663. Shift: TShiftState;
  2664. begin
  2665. Command := HiWord(Message.lParam) and (not FAPPCOMMAND_MASK);
  2666. Shift := KeyDataToShiftState(HiWord(Message.lParam) and FAPPCOMMAND_MASK);
  2667. if Shift * [ssShift, ssAlt, ssCtrl] = [] then
  2668. begin
  2669. if Command = APPCOMMAND_BROWSER_BACKWARD then
  2670. begin
  2671. Message.Result := 1;
  2672. if BackCount >= 1 then DoHistoryGo(-1);
  2673. end
  2674. else
  2675. if Command = APPCOMMAND_BROWSER_FORWARD then
  2676. begin
  2677. Message.Result := 1;
  2678. if ForwardCount >= 1 then DoHistoryGo(1);
  2679. end
  2680. else
  2681. if Command = APPCOMMAND_BROWSER_REFRESH then
  2682. begin
  2683. Message.Result := 1;
  2684. BusyOperation(ReloadDirectory);
  2685. end
  2686. else
  2687. if Command = APPCOMMAND_BROWSER_HOME then
  2688. begin
  2689. Message.Result := 1;
  2690. BusyOperation(ExecuteHomeDirectory);
  2691. end
  2692. else inherited;
  2693. end
  2694. else inherited;
  2695. end;
  2696. procedure TCustomDirView.CMColorChanged(var Message: TMessage);
  2697. begin
  2698. inherited;
  2699. ForceColorChange(Self);
  2700. end;
  2701. function TCustomDirView.FindFileItemIfNotEmpty(FileName: string): TListItem;
  2702. begin
  2703. Result := nil;
  2704. if FileName <> '' then
  2705. Result := FindFileItem(FileName);
  2706. end;
  2707. function TCustomDirView.FindFileItem(FileName: string): TListItem;
  2708. type
  2709. TFileNameCompare = function(const S1, S2: string): Integer;
  2710. var
  2711. Index: Integer;
  2712. CompareFunc: TFileNameCompare;
  2713. begin
  2714. if FCaseSensitive then CompareFunc := CompareStr
  2715. else CompareFunc := CompareText;
  2716. // Optimization to avoid duplicate lookups in consequent RestoreFocus calls from Load and RestoreState.
  2717. if Assigned(ItemFocused) and (CompareFunc(FileName, ItemFileName(ItemFocused)) = 0) then
  2718. begin
  2719. Result := ItemFocused;
  2720. Exit;
  2721. end
  2722. else
  2723. begin
  2724. for Index := 0 to Items.Count - 1 do
  2725. begin
  2726. if CompareFunc(FileName, ItemFileName(Items[Index])) = 0 then
  2727. begin
  2728. Result := Items[Index];
  2729. Exit;
  2730. end;
  2731. end;
  2732. end;
  2733. Result := nil;
  2734. end;
  2735. function TCustomDirView.GetForwardCount: Integer;
  2736. begin
  2737. Result := FHistoryPaths.Count - BackCount;
  2738. end; { GetForwardCount }
  2739. procedure TCustomDirView.LimitHistorySize;
  2740. begin
  2741. while FHistoryPaths.Count > MaxHistoryCount do
  2742. begin
  2743. if BackCount > 0 then
  2744. begin
  2745. FHistoryPaths.Delete(0);
  2746. Dec(FBackCount);
  2747. end
  2748. else
  2749. FHistoryPaths.Delete(FHistoryPaths.Count-1);
  2750. end;
  2751. end; { LimitHistorySize }
  2752. function TCustomDirView.GetHistoryPath(Index: Integer): string;
  2753. begin
  2754. Assert(Assigned(FHistoryPaths));
  2755. if Index = 0 then Result := PathName
  2756. else
  2757. if Index < 0 then Result := FHistoryPaths[Index + BackCount]
  2758. else
  2759. if Index > 0 then Result := FHistoryPaths[Index + BackCount - 1];
  2760. end; { GetHistoryPath }
  2761. procedure TCustomDirView.SetMaxHistoryCount(Value: Integer);
  2762. begin
  2763. if FMaxHistoryCount <> Value then
  2764. begin
  2765. FMaxHistoryCount := Value;
  2766. DoHistoryChange;
  2767. end;
  2768. end; { SetMaxHistoryCount }
  2769. procedure TCustomDirView.DoHistoryChange;
  2770. begin
  2771. LimitHistorySize;
  2772. if Assigned(OnHistoryChange) then
  2773. OnHistoryChange(Self);
  2774. end; { DoHistoryChange }
  2775. procedure TCustomDirView.DoHistoryGo(Index: Integer);
  2776. var
  2777. Cancel: Boolean;
  2778. begin
  2779. if StartBusy then
  2780. try
  2781. Cancel := False;
  2782. if Assigned(OnHistoryGo) then
  2783. OnHistoryGo(Self, Index, Cancel);
  2784. if not Cancel then HistoryGo(Index);
  2785. finally
  2786. EndBusy;
  2787. end;
  2788. end;
  2789. procedure TCustomDirView.HistoryGo(Index: Integer);
  2790. var
  2791. PrevPath: string;
  2792. begin
  2793. if Index <> 0 then
  2794. begin
  2795. PrevPath := FHistoryPath;
  2796. FDontRecordPath := True;
  2797. try
  2798. Path := HistoryPath[Index];
  2799. finally
  2800. FDontRecordPath := False;
  2801. end;
  2802. FHistoryPaths.Insert(FBackCount, PrevPath);
  2803. FHistoryPaths.Delete(Index + BackCount);
  2804. Inc(FBackCount, Index);
  2805. DoHistoryChange;
  2806. end;
  2807. end; { HistoryGo }
  2808. procedure TCustomDirView.PathChanging(Relative: Boolean);
  2809. begin
  2810. if Relative then FLastPath := PathName
  2811. else FLastPath := '';
  2812. FSavedNames.Clear;
  2813. end;
  2814. procedure TCustomDirView.PathChanged;
  2815. var
  2816. Index: Integer;
  2817. begin
  2818. if Assigned(OnPathChange) then
  2819. OnPathChange(Self);
  2820. if (not FDontRecordPath) and (FHistoryPath <> '') and (FHistoryPath <> PathName) then
  2821. begin
  2822. Assert(Assigned(FHistoryPaths));
  2823. for Index := FHistoryPaths.Count - 1 downto BackCount do
  2824. FHistoryPaths.Delete(Index);
  2825. FHistoryPaths.Add(FHistoryPath);
  2826. Inc(FBackCount);
  2827. DoHistoryChange;
  2828. end;
  2829. FHistoryPath := PathName;
  2830. end; { PathChanged }
  2831. procedure TCustomDirView.ProcessChangedFiles(DirView: TCustomDirView;
  2832. FileList: TStrings; FullPath: Boolean; ExistingOnly: Boolean;
  2833. Criterias: TCompareCriterias);
  2834. var
  2835. Item, MirrorItem: TListItem;
  2836. FileTime, MirrorFileTime: TDateTime;
  2837. OldCursor: TCursor;
  2838. Index: Integer;
  2839. Changed: Boolean;
  2840. SameTime: Boolean;
  2841. Precision, MirrorPrecision: TDateTimePrecision;
  2842. begin
  2843. Assert(Valid);
  2844. OldCursor := Screen.Cursor;
  2845. if not Assigned(FileList) then
  2846. begin
  2847. Items.BeginUpdate;
  2848. BeginSelectionUpdate;
  2849. end;
  2850. try
  2851. Screen.Cursor := crHourGlass;
  2852. for Index := 0 to Items.Count-1 do
  2853. begin
  2854. Item := Items[Index];
  2855. Changed := False;
  2856. if not ItemIsDirectory(Item) then
  2857. begin
  2858. MirrorItem := DirView.FindFileItem(ItemFileName(Item));
  2859. if MirrorItem = nil then
  2860. begin
  2861. Changed := not ExistingOnly;
  2862. end
  2863. else
  2864. begin
  2865. if ccTime in Criterias then
  2866. begin
  2867. FileTime := ItemFileTime(Item, Precision);
  2868. MirrorFileTime := DirView.ItemFileTime(MirrorItem, MirrorPrecision);
  2869. if MirrorPrecision < Precision then Precision := MirrorPrecision;
  2870. if Precision <> tpMillisecond then
  2871. begin
  2872. ReduceDateTimePrecision(FileTime, Precision);
  2873. ReduceDateTimePrecision(MirrorFileTime, Precision);
  2874. end;
  2875. SameTime := (FileTime = MirrorFileTime);
  2876. if Precision = tpSecond then
  2877. begin
  2878. // 1 ms more solves the rounding issues
  2879. // (see also Common.cpp)
  2880. MirrorFileTime := MirrorFileTime + EncodeTime(0, 0, 1, 1);
  2881. end;
  2882. Changed :=
  2883. (FileTime > MirrorFileTime);
  2884. end
  2885. else
  2886. begin
  2887. SameTime := True;
  2888. end;
  2889. if (not Changed) and SameTime and (ccSize in Criterias) then
  2890. begin
  2891. Changed := ItemFileSize(Item) <> DirView.ItemFileSize(MirrorItem);
  2892. end
  2893. end;
  2894. end;
  2895. if Assigned(FileList) then
  2896. begin
  2897. if Changed then
  2898. begin
  2899. if FullPath then
  2900. begin
  2901. FileList.AddObject(ItemFullFileName(Item), Item.Data)
  2902. end
  2903. else
  2904. begin
  2905. FileList.AddObject(ItemFileName(Item), Item.Data);
  2906. end;
  2907. end;
  2908. end
  2909. else
  2910. begin
  2911. Item.Selected := Changed;
  2912. end;
  2913. end;
  2914. finally
  2915. Screen.Cursor := OldCursor;
  2916. if not Assigned(FileList) then
  2917. begin
  2918. Items.EndUpdate;
  2919. EndSelectionUpdate;
  2920. end;
  2921. end;
  2922. end;
  2923. function TCustomDirView.CreateChangedFileList(DirView: TCustomDirView;
  2924. FullPath: Boolean; ExistingOnly: Boolean; Criterias: TCompareCriterias): TStrings;
  2925. begin
  2926. Result := TStringList.Create;
  2927. try
  2928. ProcessChangedFiles(DirView, Result, FullPath, ExistingOnly, Criterias);
  2929. except
  2930. FreeAndNil(Result);
  2931. raise;
  2932. end;
  2933. end;
  2934. function TCustomDirView.CanPasteFromClipBoard: Boolean;
  2935. begin
  2936. Result := False;
  2937. if DirOK and (Path <> '') and Windows.OpenClipboard(0) then
  2938. begin
  2939. Result := IsClipboardFormatAvailable(CF_HDROP);
  2940. Windows.CloseClipBoard;
  2941. end;
  2942. end; {CanPasteFromClipBoard}
  2943. procedure TCustomDirView.CompareFiles(DirView: TCustomDirView;
  2944. ExistingOnly: Boolean; Criterias: TCompareCriterias);
  2945. begin
  2946. ProcessChangedFiles(DirView, nil, True, ExistingOnly, Criterias);
  2947. end;
  2948. function TCustomDirView.GetColumnText(ListItem: TListItem; Index: Integer): string;
  2949. var
  2950. DispInfo: TLVItem;
  2951. begin
  2952. FillChar(DispInfo, SizeOf(DispInfo), 0);
  2953. DispInfo.Mask := LVIF_TEXT;
  2954. DispInfo.iSubItem := Index;
  2955. DispInfo.cchTextMax := 260;
  2956. SetLength(Result, DispInfo.cchTextMax);
  2957. DispInfo.pszText := PChar(Result);
  2958. GetDisplayInfo(ListItem, DispInfo);
  2959. SetLength(Result, StrLen(PChar(Result)));
  2960. end;
  2961. procedure TCustomDirView.FocusSomething(ForceMakeVisible: Boolean);
  2962. begin
  2963. if FSavedSelection then
  2964. begin
  2965. if ForceMakeVisible then FPendingFocusSomething := 1
  2966. else FPendingFocusSomething := 0
  2967. end
  2968. else inherited;
  2969. end;
  2970. procedure TCustomDirView.SaveSelection;
  2971. var
  2972. Closest: TListItem;
  2973. begin
  2974. Assert(not FSavedSelection);
  2975. FSavedSelectionFile := '';
  2976. FSavedSelectionLastFile := '';
  2977. if Assigned(ItemFocused) then
  2978. begin
  2979. FSavedSelectionLastFile := ItemFocused.Caption;
  2980. end;
  2981. Closest := ClosestUnselected(ItemFocused);
  2982. if Assigned(Closest) then
  2983. begin
  2984. FSavedSelectionFile := Closest.Caption;
  2985. end;
  2986. FSavedSelection := True;
  2987. end;
  2988. procedure TCustomDirView.RestoreSelection;
  2989. var
  2990. ItemToSelect: TListItem;
  2991. begin
  2992. Assert(FSavedSelection);
  2993. FSavedSelection := False;
  2994. if (FSavedSelectionLastFile <> '') and
  2995. ((not Assigned(ItemFocused)) or
  2996. (ItemFocused.Caption <> FSavedSelectionLastFile)) then
  2997. begin
  2998. ItemToSelect := FindFileItem(FSavedSelectionFile);
  2999. if Assigned(ItemToSelect) then
  3000. begin
  3001. ItemFocused := ItemToSelect;
  3002. end;
  3003. end;
  3004. if not Assigned(ItemFocused) then FocusSomething(True)
  3005. else ItemFocused.MakeVisible(False);
  3006. end;
  3007. procedure TCustomDirView.DiscardSavedSelection;
  3008. begin
  3009. Assert(FSavedSelection);
  3010. FSavedSelection := False;
  3011. if FPendingFocusSomething >= 0 then
  3012. begin
  3013. FocusSomething(FPendingFocusSomething > 0);
  3014. FPendingFocusSomething := -1;
  3015. end;
  3016. end;
  3017. procedure TCustomDirView.SaveSelectedNames;
  3018. var
  3019. Index: Integer;
  3020. Item: TListItem;
  3021. begin
  3022. FSavedNames.Clear;
  3023. FSavedNames.CaseSensitive := FCaseSensitive;
  3024. if SelCount > 0 then // optimalisation
  3025. begin
  3026. for Index := 0 to Items.Count-1 do
  3027. begin
  3028. Item := Items[Index];
  3029. if Item.Selected then
  3030. FSavedNames.Add(ItemFileName(Item));
  3031. end;
  3032. end;
  3033. // as optimalisation the list is sorted only when the selection is restored
  3034. end;
  3035. procedure TCustomDirView.RestoreSelectedNames;
  3036. var
  3037. Index, FoundIndex: Integer;
  3038. Item: TListItem;
  3039. begin
  3040. FSavedNames.Sort;
  3041. for Index := 0 to Items.Count - 1 do
  3042. begin
  3043. Item := Items[Index];
  3044. Item.Selected := FSavedNames.Find(ItemFileName(Item), FoundIndex);
  3045. end;
  3046. end;
  3047. function TCustomDirView.GetSelectedNamesSaved: Boolean;
  3048. begin
  3049. Result := (FSavedNames.Count > 0);
  3050. end;
  3051. procedure TCustomDirView.ContinueSession(Continue: Boolean);
  3052. begin
  3053. if Continue then FLastPath := PathName
  3054. else FLastPath := '';
  3055. end;
  3056. procedure TCustomDirView.AnnounceState(AState: TObject);
  3057. var
  3058. State: TDirViewState;
  3059. begin
  3060. FAnnouncedState := AState;
  3061. if Assigned(FAnnouncedState) then
  3062. begin
  3063. State := AState as TDirViewState;
  3064. if Assigned(State) then
  3065. begin
  3066. FEffectiveMask := State.Mask;
  3067. end;
  3068. end
  3069. else
  3070. begin
  3071. FEffectiveMask := Mask;
  3072. end;
  3073. end;
  3074. procedure TCustomDirView.SaveItemsState(
  3075. var FocusedItem: string; var FocusedShown: Boolean; var ShownItemOffset: Integer);
  3076. begin
  3077. if Assigned(ItemFocused) then
  3078. begin
  3079. if ViewStyle = vsReport then
  3080. begin
  3081. if Assigned(TopItem) then
  3082. begin
  3083. FocusedShown := IsItemVisible(ItemFocused);
  3084. if not FocusedShown then
  3085. begin
  3086. ShownItemOffset := TopItem.Index;
  3087. end
  3088. else
  3089. begin
  3090. ShownItemOffset := ItemFocused.Index - TopItem.Index;
  3091. end;
  3092. end
  3093. else
  3094. begin
  3095. // seen with one user only
  3096. FocusedShown := False;
  3097. ShownItemOffset := 0;
  3098. end;
  3099. end
  3100. else
  3101. begin
  3102. // to satisfy compiler, never used
  3103. FocusedShown := False;
  3104. ShownItemOffset := -1;
  3105. end;
  3106. FocusedItem := ItemFocused.Caption;
  3107. end
  3108. else
  3109. begin
  3110. FocusedItem := '';
  3111. FocusedShown := False;
  3112. if Assigned(TopItem) then ShownItemOffset := TopItem.Index
  3113. else ShownItemOffset := -1;
  3114. end;
  3115. end;
  3116. procedure TCustomDirView.RestoreItemsState(ItemToFocus: TListItem; FocusedShown: Boolean; ShownItemOffset: Integer);
  3117. begin
  3118. if Assigned(ItemToFocus) then
  3119. begin
  3120. // we have found item that was previously focused and visible, scroll to it
  3121. if (ViewStyle = vsReport) and FocusedShown and
  3122. (ItemToFocus.Index > ShownItemOffset) then
  3123. begin
  3124. MakeTopItem(Items[ItemToFocus.Index - ShownItemOffset]);
  3125. end;
  3126. // Strangely after this mouse selection works correctly, so we do not have to call FocusItem.
  3127. ItemFocused := ItemToFocus;
  3128. end;
  3129. // previously focused item was not visible, scroll to the same position
  3130. // as before
  3131. if (ViewStyle = vsReport) and (not FocusedShown) and
  3132. (ShownItemOffset >= 0) and (Items.Count > 0) then
  3133. begin
  3134. if ShownItemOffset < Items.Count - VisibleRowCount then
  3135. MakeTopItem(Items[ShownItemOffset])
  3136. else
  3137. Items.Item[Items.Count - 1].MakeVisible(false);
  3138. end
  3139. // do not know where to scroll to, so scroll to focus
  3140. // (or we have tried to scroll to previously focused and visible item,
  3141. // now make sure that it is really visible)
  3142. else
  3143. if Assigned(ItemToFocus) then ItemToFocus.MakeVisible(False);
  3144. end;
  3145. procedure TCustomDirView.RestoreItemsState(AState: TObject);
  3146. var
  3147. State: TDirViewState;
  3148. ItemToFocus: TListItem;
  3149. begin
  3150. State := TDirViewState(AState);
  3151. ItemToFocus := FindFileItemIfNotEmpty(State.FocusedItem);
  3152. RestoreItemsState(ItemToFocus, State.FocusedShown, State.ShownItemOffset);
  3153. end;
  3154. function TCustomDirView.SaveState: TObject;
  3155. var
  3156. State: TDirViewState;
  3157. DirColProperties: TCustomDirViewColProperties;
  3158. begin
  3159. State := TDirViewState.Create;
  3160. State.HistoryPaths := TStringList.Create;
  3161. State.HistoryPaths.Assign(FHistoryPaths);
  3162. State.BackCount := FBackCount;
  3163. // TCustomDirViewColProperties should not be here
  3164. DirColProperties := ColProperties as TCustomDirViewColProperties;
  3165. Assert(Assigned(DirColProperties));
  3166. State.SortStr := DirColProperties.SortStr;
  3167. State.Mask := Mask;
  3168. SaveItemsState(State.FocusedItem, State.FocusedShown, State.ShownItemOffset);
  3169. Result := State;
  3170. end;
  3171. procedure TCustomDirView.FocusByName(FileName: string);
  3172. var
  3173. ListItem: TListItem;
  3174. begin
  3175. ListItem := FindFileItemIfNotEmpty(FileName);
  3176. if Assigned(ListItem) then
  3177. begin
  3178. ItemFocused := ListItem;
  3179. ListItem.MakeVisible(False);
  3180. end;
  3181. end;
  3182. procedure TCustomDirView.RestoreState(AState: TObject);
  3183. var
  3184. State: TDirViewState;
  3185. DirColProperties: TCustomDirViewColProperties;
  3186. begin
  3187. if Assigned(AState) then
  3188. begin
  3189. Assert(AState is TDirViewState);
  3190. State := AState as TDirViewState;
  3191. Assert(Assigned(State));
  3192. Assert((not Assigned(FAnnouncedState)) or (FAnnouncedState = AState));
  3193. FHistoryPaths.Assign(State.HistoryPaths);
  3194. FBackCount := State.BackCount;
  3195. DoHistoryChange;
  3196. // TCustomDirViewColProperties should not be here
  3197. DirColProperties := ColProperties as TCustomDirViewColProperties;
  3198. Assert(Assigned(DirColProperties));
  3199. DirColProperties.SortStr := State.SortStr;
  3200. Mask := State.Mask;
  3201. RestoreItemsState(State);
  3202. end
  3203. else
  3204. begin
  3205. FHistoryPaths.Clear;
  3206. FBackCount := 0;
  3207. DoHistoryChange;
  3208. end;
  3209. end;
  3210. procedure TCustomDirView.SetMask(Value: string);
  3211. begin
  3212. if Mask <> Value then
  3213. begin
  3214. if Assigned(FAnnouncedState) then
  3215. Assert(FEffectiveMask = Value)
  3216. else
  3217. Assert(FEffectiveMask = Mask);
  3218. FMask := Value;
  3219. UpdatePathLabel;
  3220. if FEffectiveMask <> Value then
  3221. begin
  3222. FEffectiveMask := Value;
  3223. if DirOK then Reload(False);
  3224. end;
  3225. end;
  3226. end;{SetMask}
  3227. procedure TCustomDirView.SetNaturalOrderNumericalSorting(Value: Boolean);
  3228. begin
  3229. if NaturalOrderNumericalSorting <> Value then
  3230. begin
  3231. FNaturalOrderNumericalSorting := Value;
  3232. SortItems;
  3233. end;
  3234. end;
  3235. procedure TCustomDirView.SetAlwaysSortDirectoriesByName(Value: Boolean);
  3236. begin
  3237. if AlwaysSortDirectoriesByName <> Value then
  3238. begin
  3239. FAlwaysSortDirectoriesByName := Value;
  3240. SortItems;
  3241. end;
  3242. end;
  3243. procedure TCustomDirView.SetDarkMode(Value: Boolean);
  3244. begin
  3245. if DarkMode <> Value then
  3246. begin
  3247. FDarkMode := Value;
  3248. // Call only when switching to dark more and when switching back to the light mode.
  3249. // But not for initial light mode - To reduce an impact of calling an undocumented function.
  3250. if HandleAllocated then UpdateDarkMode;
  3251. end;
  3252. end;
  3253. // WM_SETFOCUS works even when focus is moved to another window/app,
  3254. // while .Enter works only when focus is moved to other control of the same window.
  3255. procedure TCustomDirView.WMSetFocus(var Message: TWMSetFocus);
  3256. begin
  3257. inherited;
  3258. EnsureSelectionRedrawn;
  3259. UpdatePathLabel;
  3260. end;
  3261. procedure TCustomDirView.WMKillFocus(var Message: TWMKillFocus);
  3262. begin
  3263. inherited;
  3264. EnsureSelectionRedrawn;
  3265. UpdatePathLabel;
  3266. end;
  3267. procedure TCustomDirView.EnsureSelectionRedrawn;
  3268. begin
  3269. // WORKAROUND
  3270. // when receiving/losing focus, selection is not redrawn in report view
  3271. // (except for focus item selection),
  3272. // probably when double buffering is enabled (LVS_EX_DOUBLEBUFFER).
  3273. // But even without LVS_EX_DOUBLEBUFFER, selection behind file icon is not updated.
  3274. if ViewStyle = vsReport then
  3275. begin
  3276. if (SelCount >= 2) or ((SelCount >= 1) and ((not Assigned(ItemFocused)) or (not ItemFocused.Selected))) then
  3277. begin
  3278. Invalidate;
  3279. end
  3280. else
  3281. if Assigned(ItemFocused) and ItemFocused.Selected then
  3282. begin
  3283. // Optimization. When no item is selected, redraw just the focused item.
  3284. ItemFocused.Update;
  3285. end;
  3286. end;
  3287. end;
  3288. function TCustomDirView.DoBusy(Busy: Integer): Boolean;
  3289. begin
  3290. Result := True;
  3291. if Assigned(OnBusy) then
  3292. begin
  3293. OnBusy(Self, Busy, Result);
  3294. end;
  3295. end;
  3296. function TCustomDirView.StartBusy: Boolean;
  3297. begin
  3298. Result := DoBusy(1);
  3299. end;
  3300. function TCustomDirView.IsBusy: Boolean;
  3301. begin
  3302. Result := DoBusy(0);
  3303. end;
  3304. procedure TCustomDirView.EndBusy;
  3305. begin
  3306. DoBusy(-1);
  3307. end;
  3308. procedure TCustomDirView.BusyOperation(Operation: TBusyOperation);
  3309. begin
  3310. if StartBusy then
  3311. try
  3312. Operation;
  3313. finally
  3314. EndBusy;
  3315. end;
  3316. end;
  3317. procedure TCustomDirView.ItemCalculatedSizeUpdated(Item: TListItem; OldSize, NewSize: Int64);
  3318. begin
  3319. if OldSize >= 0 then
  3320. begin
  3321. Dec(FFilesSize, OldSize);
  3322. if Item.Selected then Dec(FFilesSelSize, OldSize);
  3323. end;
  3324. if NewSize >= 0 then
  3325. begin
  3326. Inc(FFilesSize, NewSize);
  3327. if Item.Selected then Inc(FFilesSelSize, NewSize);
  3328. end;
  3329. Item.Update;
  3330. UpdateStatusBar;
  3331. end;
  3332. function TCustomDirView.GetDirViewStyle: TDirViewStyle;
  3333. begin
  3334. if (ViewStyle = vsIcon) and FThumbnail then Result := dvsThumbnail
  3335. else Result := TDirViewStyle(ViewStyle);
  3336. end;
  3337. procedure TCustomDirView.SetDirViewStyle(Value: TDirViewStyle);
  3338. var
  3339. NewViewStyle: TViewStyle;
  3340. begin
  3341. if DirViewStyle <> Value then
  3342. begin
  3343. FThumbnail := (Value = dvsThumbnail);
  3344. // Create thumbnail images before recreating the view
  3345. NeedImageLists(False);
  3346. if FThumbnail then NewViewStyle := vsIcon
  3347. else NewViewStyle := TViewStyle(Value);
  3348. if ViewStyle <> NewViewStyle then
  3349. begin
  3350. ViewStyle := NewViewStyle;
  3351. end
  3352. else
  3353. begin
  3354. // Changing ViewStyle recreates the view, we want to be consistent.
  3355. if not (csLoading in ComponentState) then
  3356. begin
  3357. RecreateWnd;
  3358. end;
  3359. // Again, for consistency (among other this clears thumbnail cache)
  3360. ViewStyleChanged;
  3361. end;
  3362. end;
  3363. end;
  3364. procedure TCustomDirView.InvalidateItem(Item: TListItem);
  3365. var
  3366. R: TRect;
  3367. begin
  3368. R := Item.DisplayRect(drBounds);
  3369. // alternative to TListItem.Update (which causes flicker)
  3370. InvalidateRect(Handle, @R, True);
  3371. end;
  3372. procedure TCustomDirView.WMUserInvalidateItem(var Message: TMessage);
  3373. var
  3374. Index: Integer;
  3375. begin
  3376. Index := Integer(Message.WParam);
  3377. if (Index >= 0) and (Index < Items.Count) then
  3378. InvalidateItem(Items[Index]);
  3379. end;
  3380. initialization
  3381. DropSourceControl := nil;
  3382. SetLength(WinDir, MAX_PATH);
  3383. SetLength(WinDir, GetWindowsDirectory(PChar(WinDir), MAX_PATH));
  3384. SetLength(TempDir, MAX_PATH);
  3385. SetLength(TempDir, GetTempPath(MAX_PATH, PChar(TempDir)));
  3386. SpecialFolderLocation(CSIDL_PERSONAL, UserDocumentDirectory);
  3387. WinDir := IncludeTrailingPathDelimiter(WinDir);
  3388. TempDir := IncludeTrailingPathDelimiter(TempDir);
  3389. finalization
  3390. SetLength(StdDirTypeName, 0);
  3391. SetLength(WinDir, 0);
  3392. SetLength(TempDir, 0);
  3393. end.