CustomDirView.pas 101 KB

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