CustomDirView.pas 109 KB

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