CustomDirView.pas 117 KB

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