CustomDirView.pas 106 KB

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