CustomDirView.pas 107 KB

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