CustomDirView.pas 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292
  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. IncludeAttr: Word; { see TSearchRec.Attr }
  69. ExcludeAttr: Word;
  70. Directories: Boolean;
  71. FileSizeFrom: Int64;
  72. FileSizeTo: Int64;
  73. ModificationFrom: TDateTime;
  74. ModificationTo: TDateTime;
  75. end;
  76. TDirViewNotifyEvent = procedure(Sender: TCustomDirView) of object;
  77. TDVGetFilterEvent = procedure(Sender: TCustomDirView; Select: Boolean;
  78. var Filter: TFileFilter) of object;
  79. TDVHistoryGoEvent = procedure(Sender: TCustomDirView; Index: Integer; var Cancel: Boolean) of object;
  80. TCompareCriteria = (ccTime, ccSize);
  81. TCompareCriterias = set of TCompareCriteria;
  82. TWMXMouse = packed record
  83. Msg: Cardinal;
  84. Keys: Word;
  85. Button: Word;
  86. Pos: TSmallPoint;
  87. Result: Longint
  88. end;
  89. TCustomizableDragDropFilesEx = class(TDragDropFilesEx)
  90. public
  91. function Execute(DataObject: TDataObject): TDragResult;
  92. end;
  93. TCustomDirView = class(TCustomIEListView)
  94. private
  95. FAddParentDir: Boolean;
  96. FDimmHiddenFiles: Boolean;
  97. FFormatSizeBytes: TFormatBytesStyle;
  98. FWantUseDragImages: Boolean;
  99. FDragDropFilesEx: TCustomizableDragDropFilesEx;
  100. FUseSystemContextMenu: Boolean;
  101. FOnGetSelectFilter: TDVGetFilterEvent;
  102. FOnStartLoading: TNotifyEvent;
  103. FOnLoaded: TNotifyEvent;
  104. FDragDrive: TDrive;
  105. FExeDrag: Boolean;
  106. FDDLinkOnExeDrag: Boolean;
  107. FOnDDDragEnter: TDDOnDragEnter;
  108. FOnDDDragLeave: TDDOnDragLeave;
  109. FOnDDDragOver: TDDOnDragOver;
  110. FOnDDDrop: TDDOnDrop;
  111. FOnDDQueryContinueDrag: TDDOnQueryContinueDrag;
  112. FOnDDGiveFeedback: TDDOnGiveFeedback;
  113. FOnDDChooseEffect: TDDOnChooseEffect;
  114. FOnDDDragDetect: TDDOnDragDetect;
  115. FOnDDCreateDragFileList: TDDOnCreateDragFileList;
  116. FOnDDProcessDropped: TOnProcessDropped;
  117. FOnDDError: TDDErrorEvent;
  118. FOnDDExecuted: TDDExecutedEvent;
  119. FOnDDFileOperation: TDDFileOperationEvent;
  120. FOnDDFileOperationExecuted: TDDFileOperationExecutedEvent;
  121. FOnDDEnd: TNotifyEvent;
  122. FOnDDCreateDataObject: TDDOnCreateDataObject;
  123. FOnDDTargetHasDropHandler: TDDOnTargetHasDropHandler;
  124. FOnDDMenuPopup: TOnMenuPopup;
  125. FOnExecFile: TDirViewExecFileEvent;
  126. FForceRename: Boolean;
  127. FLastDDResult: TDragResult;
  128. FLastRenameName: string;
  129. FContextMenu: Boolean;
  130. FDragEnabled: Boolean;
  131. FDragPos: TPoint;
  132. FStartPos: TPoint;
  133. FDDOwnerIsSource: Boolean;
  134. FAbortLoading: Boolean;
  135. FAnimation: TAnimate;
  136. FBackCount: Integer;
  137. FDontRecordPath: Boolean;
  138. FDragOnDriveIsMove: Boolean;
  139. FNotifyEnabled: Boolean;
  140. FDragStartTime: TFileTime;
  141. FHistoryPaths: TStrings;
  142. FImageList16: TImageList;
  143. FImageList32: TImageList;
  144. FLoadAnimation: Boolean;
  145. FMaxHistoryCount: Integer;
  146. FPathLabel: TCustomPathLabel;
  147. FOnUpdateStatusBar: TDirViewUpdateStatusBarEvent;
  148. FOnHistoryChange: TDirViewNotifyEvent;
  149. FOnHistoryGo: TDVHistoryGoEvent;
  150. FOnPathChange: TDirViewNotifyEvent;
  151. FShowHiddenFiles: Boolean;
  152. FSavedSelection: Boolean;
  153. FSavedSelectionFile: string;
  154. FSavedSelectionLastFile: string;
  155. FSavedNames: TStringList;
  156. FPendingFocusSomething: Boolean;
  157. FOnMatchMask: TMatchMaskEvent;
  158. FOnGetOverlay: TDirViewGetOverlayEvent;
  159. FMask: string;
  160. FScrollOnDragOver: TListViewScrollOnDragOver;
  161. FStatusFileInfo: TStatusFileInfo;
  162. FDoubleBufferedScrollingWorkaround: Boolean;
  163. FOnBusy: TDirViewBusy;
  164. procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
  165. procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
  166. procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
  167. procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  168. procedure WMContextMenu(var Message: TWMContextMenu); message WM_CONTEXTMENU;
  169. procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  170. procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
  171. procedure WMXButtonUp(var Message: TWMXMouse); message WM_XBUTTONUP;
  172. procedure WMAppCommand(var Message: TMessage); message WM_APPCOMMAND;
  173. procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
  174. procedure LVMSetExtendedListViewStyle(var Message: TMessage); message LVM_SETEXTENDEDLISTVIEWSTYLE;
  175. procedure DumbCustomDrawItem(Sender: TCustomListView; Item: TListItem;
  176. State: TCustomDrawState; var DefaultDraw: Boolean);
  177. procedure DumbCustomDrawSubItem(Sender: TCustomListView;
  178. Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  179. var DefaultDraw: Boolean);
  180. function GetFilesMarkedSize: Int64;
  181. function GetForwardCount: Integer;
  182. function GetHistoryPath(Index: Integer): string;
  183. function GetSelectedNamesSaved: Boolean;
  184. function GetTargetPopupMenu: Boolean;
  185. function GetUseDragImages: Boolean;
  186. procedure SetMaxHistoryCount(Value: Integer);
  187. procedure SetPathLabel(Value: TCustomPathLabel);
  188. procedure SetTargetPopupMenu(Value: Boolean);
  189. procedure WMUserRename(var Message: TMessage); message WM_User_Rename;
  190. protected
  191. FCaseSensitive: Boolean;
  192. FDirty: Boolean;
  193. FFilesSize: Int64;
  194. FFilesSelSize: Int64;
  195. FHasParentDir: Boolean;
  196. FIsRecycleBin: Boolean;
  197. FLastPath: string;
  198. FHistoryPath: string;
  199. FLoadEnabled: Boolean;
  200. FLoading: Boolean;
  201. FSelectFile: string;
  202. FWatchForChanges: Boolean;
  203. FInvalidNameChars: string;
  204. procedure AddToDragFileList(FileList: TFileList; Item: TListItem); virtual;
  205. function CanEdit(Item: TListItem): Boolean; override;
  206. function CanChangeSelection(Item: TListItem; Select: Boolean): Boolean; override;
  207. procedure CancelEdit;
  208. procedure ClearItems; override;
  209. function GetDirOK: Boolean; virtual; abstract;
  210. procedure DDDragDetect(grfKeyState: Longint; DetectStart, Point: TPoint; DragStatus: TDragDetectStatus); virtual;
  211. procedure DDDragEnter(DataObj: IDataObject; grfKeyState: Longint; Point: TPoint; var dwEffect: longint; var Accept: Boolean);
  212. procedure DDDragLeave;
  213. procedure DDDragOver(grfKeyState: Longint; Point: TPoint; var dwEffect: Longint);
  214. procedure DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer); virtual;
  215. procedure DDDrop(DataObj: IDataObject; grfKeyState: LongInt; Point: TPoint; var dwEffect: Longint);
  216. procedure DDDropHandlerSucceeded(Sender: TObject; grfKeyState: Longint; Point: TPoint; dwEffect: Longint); virtual;
  217. procedure DDGiveFeedback(dwEffect: Longint; var Result: HResult); virtual;
  218. procedure DDMenuPopup(Sender: TObject; AMenu: HMenu; DataObj: IDataObject;
  219. AMinCustCmd:integer; grfKeyState: Longint; pt: TPoint); virtual;
  220. procedure DDMenuDone(Sender: TObject; AMenu: HMenu); virtual;
  221. procedure DDProcessDropped(Sender: TObject; grfKeyState: Longint;
  222. Point: TPoint; dwEffect: Longint);
  223. procedure DDQueryContinueDrag(FEscapePressed: LongBool;
  224. grfKeyState: Longint; var Result: HResult); virtual;
  225. procedure DDSpecifyDropTarget(Sender: TObject; DragDropHandler: Boolean;
  226. Point: TPoint; var pidlFQ : PItemIDList; var Filename: string); virtual;
  227. procedure GetDisplayInfo(ListItem: TListItem; var DispInfo: TLVItem); virtual;
  228. function GetDragSourceEffects: TDropEffectSet; virtual;
  229. function GetPathName: string; virtual; abstract;
  230. function GetFilesCount: Integer; virtual;
  231. procedure ColClick(Column: TListColumn); override;
  232. procedure CreateWnd; override;
  233. procedure DestroyWnd; override;
  234. function CustomCreateFileList(Focused, OnlyFocused: Boolean;
  235. FullPath: Boolean; FileList: TStrings = nil; ItemObject: Boolean = False): TStrings;
  236. function CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  237. Stage: TCustomDrawStage): Boolean; override;
  238. function CustomDrawSubItem(Item: TListItem; SubItem: Integer;
  239. State: TCustomDrawState; Stage: TCustomDrawStage): Boolean; override;
  240. procedure CustomSortItems(SortProc: Pointer);
  241. procedure Delete(Item: TListItem); override;
  242. procedure DoAnimation(Start: Boolean);
  243. procedure DoHistoryChange; dynamic;
  244. function DragCompleteFileList: Boolean; virtual;
  245. procedure Edit(const HItem: TLVItem); override;
  246. procedure EndSelectionUpdate; override;
  247. procedure Execute(Item: TListItem); virtual;
  248. procedure ExecuteFile(Item: TListItem); virtual; abstract;
  249. procedure FocusSomething; override;
  250. function GetIsRoot: Boolean; virtual; abstract;
  251. procedure IconsSetImageList; virtual;
  252. function ItemCanDrag(Item: TListItem): Boolean; virtual;
  253. function ItemColor(Item: TListItem): TColor; virtual;
  254. function ItemImageIndex(Item: TListItem; Cache: Boolean): Integer; virtual; abstract;
  255. // ItemIsDirectory and ItemFullFileName is in public block
  256. function ItemIsRecycleBin(Item: TListItem): Boolean; virtual;
  257. procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  258. procedure KeyPress(var Key: Char); override;
  259. procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  260. procedure LoadFiles; virtual; abstract;
  261. procedure PerformItemDragDropOperation(Item: TListItem; Effect: Integer); virtual; abstract;
  262. procedure ProcessChangedFiles(DirView: TCustomDirView;
  263. FileList: TStrings; FullPath: Boolean; ExistingOnly: Boolean;
  264. Criterias: TCompareCriterias);
  265. procedure ReloadForce(CacheIcons : Boolean);
  266. procedure RetryRename(NewName: string);
  267. procedure SetAddParentDir(Value: Boolean); virtual;
  268. procedure SetDimmHiddenFiles(Value: Boolean); virtual;
  269. procedure SetItemImageIndex(Item: TListItem; Index: Integer); virtual; abstract;
  270. procedure SetLoadEnabled(Enabled : Boolean); virtual;
  271. procedure SetMultiSelect(Value: Boolean); override;
  272. function GetPath: string; virtual; abstract;
  273. function GetValid: Boolean; override;
  274. procedure InternalEdit(const HItem: TLVItem); virtual; abstract;
  275. function ItemIsFile(Item: TListItem): Boolean; virtual; abstract;
  276. function ItemMatchesFilter(Item: TListItem; const Filter: TFileFilter): Boolean; virtual; abstract;
  277. function ItemOverlayIndexes(Item: TListItem): Word; virtual;
  278. procedure LimitHistorySize;
  279. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  280. procedure PathChanged; virtual;
  281. procedure PathChanging(Relative: Boolean);
  282. procedure SetPath(Value: string); virtual; abstract;
  283. procedure SetShowHiddenFiles(Value: Boolean); virtual;
  284. procedure SetFormatSizeBytes(Value: TFormatBytesStyle);
  285. procedure SetViewStyle(Value: TViewStyle); override;
  286. procedure SetWatchForChanges(Value: Boolean); virtual;
  287. function TargetHasDropHandler(Item: TListItem; Effect: Integer): Boolean; virtual;
  288. procedure UpdatePathLabel; dynamic;
  289. procedure UpdatePathLabelCaption; dynamic;
  290. procedure UpdateStatusBar; dynamic;
  291. function FileNameMatchesMasks(FileName: string; Directory: Boolean; Size: Int64; Modification: TDateTime; Masks: string; AllowImplicitMatches: Boolean): Boolean;
  292. function EnableDragOnClick: Boolean; override;
  293. procedure SetMask(Value: string); virtual;
  294. procedure ScrollOnDragOverBeforeUpdate(ObjectToValidate: TObject);
  295. procedure ScrollOnDragOverAfterUpdate;
  296. procedure DoHistoryGo(Index: Integer);
  297. procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  298. procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  299. procedure EnsureSelectionRedrawn;
  300. function HiddenCount: Integer; virtual; abstract;
  301. function FilteredCount: Integer; virtual; abstract;
  302. function DoBusy(Busy: Integer): Boolean;
  303. function StartBusy: Boolean;
  304. procedure EndBusy;
  305. function IsBusy: Boolean;
  306. procedure BusyOperation(Operation: TBusyOperation);
  307. procedure DoDisplayPropertiesMenu;
  308. procedure DoExecute(Item: TListItem);
  309. procedure DoExecuteParentDirectory;
  310. procedure Load(DoFocusSomething: Boolean); virtual;
  311. property ImageList16: TImageList read FImageList16;
  312. property ImageList32: TImageList read FImageList32;
  313. public
  314. constructor Create(AOwner: TComponent); override;
  315. destructor Destroy; override;
  316. procedure Reload(CacheIcons: Boolean); virtual;
  317. function CreateFocusedFileList(FullPath: Boolean; FileList: TStrings = nil): TStrings;
  318. function CreateFileList(Focused: Boolean; FullPath: Boolean; FileList: TStrings = nil): TStrings;
  319. function AnyFileSelected(OnlyFocused: Boolean; FilesOnly: Boolean;
  320. FocusedFileOnlyWhenFocused: Boolean): Boolean;
  321. function DoSelectByMask(Select: Boolean): Boolean; override;
  322. procedure SelectFiles(Filter: TFileFilter; Select: Boolean);
  323. procedure ExecuteHomeDirectory; virtual; abstract;
  324. procedure ExecuteParentDirectory; virtual; abstract;
  325. procedure ExecuteRootDirectory; virtual; abstract;
  326. procedure ExecuteCurrentFile();
  327. procedure CreateDirectory(DirName: string); virtual; abstract;
  328. function FindFileItem(FileName: string): TListItem;
  329. procedure HistoryGo(Index: Integer);
  330. function ItemIsDirectory(Item: TListItem): Boolean; virtual; abstract;
  331. function ItemIsParentDirectory(Item: TListItem): Boolean; virtual; abstract;
  332. function ItemFullFileName(Item: TListItem): string; virtual; abstract;
  333. function ItemFileName(Item: TListItem): string; virtual; abstract;
  334. function ItemFileSize(Item: TListItem): Int64; virtual; abstract;
  335. function ItemFileTime(Item: TListItem; var Precision: TDateTimePrecision): TDateTime; virtual; abstract;
  336. procedure ReloadDirectory; virtual; abstract;
  337. procedure DisplayPropertiesMenu; virtual; abstract;
  338. function CreateChangedFileList(DirView: TCustomDirView; FullPath: Boolean;
  339. ExistingOnly: Boolean; Criterias: TCompareCriterias): TStrings;
  340. procedure CompareFiles(DirView: TCustomDirView; ExistingOnly: Boolean;
  341. Criterias: TCompareCriterias); virtual;
  342. procedure SaveSelection;
  343. procedure RestoreSelection;
  344. procedure DiscardSavedSelection;
  345. procedure SaveSelectedNames;
  346. procedure RestoreSelectedNames;
  347. procedure ContinueSession(Continue: Boolean);
  348. function CanPasteFromClipBoard: Boolean; dynamic;
  349. function PasteFromClipBoard(TargetPath: string = ''): Boolean; virtual; abstract;
  350. function SaveState: TObject;
  351. procedure RestoreState(AState: TObject);
  352. procedure ClearState;
  353. procedure DisplayContextMenu(Where: TPoint); virtual; abstract;
  354. procedure DisplayContextMenuInSitu;
  355. property AddParentDir: Boolean read FAddParentDir write SetAddParentDir default False;
  356. property DimmHiddenFiles: Boolean read FDimmHiddenFiles write SetDimmHiddenFiles default True;
  357. property DragDropFilesEx: TCustomizableDragDropFilesEx read FDragDropFilesEx;
  358. property FormatSizeBytes: TFormatBytesStyle read FFormatSizeBytes write SetFormatSizeBytes default fbNone;
  359. property WantUseDragImages: Boolean read FWantUseDragImages write FWantUseDragImages default False;
  360. property UseDragImages: Boolean read GetUseDragImages stored False;
  361. property FullDrag default True;
  362. property TargetPopupMenu: Boolean read GetTargetPopupMenu write SetTargetPopupMenu default True;
  363. property DDOwnerIsSource: Boolean read FDDOwnerIsSource;
  364. property FilesSize: Int64 read FFilesSize;
  365. property FilesSelSize: Int64 read FFilesSelSize;
  366. property FilesCount: Integer read GetFilesCount;
  367. property FilesMarkedSize: Int64 read GetFilesMarkedSize;
  368. property HasParentDir: Boolean read FHasParentDir;
  369. property Path: string read GetPath write SetPath;
  370. property PathName: string read GetPathName;
  371. property UseSystemContextMenu: Boolean read FUseSystemContextMenu
  372. write FUseSystemContextMenu default True;
  373. property Loading: Boolean read FLoading;
  374. property AbortLoading: Boolean read FAbortLoading write FAbortLoading stored False;
  375. property BackCount: Integer read FBackCount;
  376. {Enable or disable populating the item list:}
  377. property LoadAnimation: Boolean read FLoadAnimation write FLoadAnimation default True;
  378. property LoadEnabled: Boolean read FLoadEnabled write SetLoadEnabled default True;
  379. {Displayed data is not valid => reload required}
  380. property Dirty: Boolean read FDirty;
  381. property DirOK: Boolean read GetDirOK;
  382. property LastPath: string read FLastPath;
  383. property IsRecycleBin: Boolean read FIsRecycleBin;
  384. property DDLinkOnExeDrag: Boolean read FDDLinkOnExeDrag
  385. write FDDLinkOnExeDrag default False;
  386. property DragDrive: TDrive read FDragDrive;
  387. property DragOnDriveIsMove: Boolean read FDragOnDriveIsMove write FDragOnDriveIsMove;
  388. property DragSourceEffects: TDropEffectSet read GetDragSourceEffects{ write FDragSourceEffects};
  389. property ExeDrag: Boolean read FExeDrag;
  390. property ForwardCount: Integer read GetForwardCount;
  391. property HistoryPath[Index: Integer]: string read GetHistoryPath;
  392. property IsRoot: Boolean read GetIsRoot;
  393. property LastDDResult: TDragResult read FLastDDResult;
  394. property SmallImages;
  395. property LargeImages;
  396. property MaxHistoryCount: Integer read FMaxHistoryCount write SetMaxHistoryCount default DefaultHistoryCount;
  397. property SelectedNamesSaved: Boolean read GetSelectedNamesSaved;
  398. {filemask, multiple filters are possible: '*.pas;*.dfm'}
  399. property Mask: string read FMask write SetMask;
  400. property OnContextPopup;
  401. property OnGetSelectFilter: TDVGetFilterEvent read FOnGetSelectFilter write FOnGetSelectFilter;
  402. property OnStartLoading: TNotifyEvent read FOnStartLoading write FOnStartLoading;
  403. property OnLoaded: TNotifyEvent read FOnLoaded write FOnLoaded;
  404. {The mouse has entered the component window as a target of a drag&drop operation:}
  405. property OnDDDragEnter: TDDOnDragEnter read FOnDDDragEnter write FOnDDDragEnter;
  406. {The mouse has leaved the component window as a target of a drag&drop operation:}
  407. property OnDDDragLeave: TDDOnDragLeave read FOnDDDragLeave write FOnDDDragLeave;
  408. {The mouse is dragging in the component window as a target of a drag&drop operation:}
  409. property OnDDDragOver: TDDOnDragOver read FOnDDDragOver write FOnDDDragOver;
  410. {The Drag&drop operation is about to be executed:}
  411. property OnDDDrop: TDDOnDrop read FOnDDDrop write FOnDDDrop;
  412. property OnDDQueryContinueDrag: TDDOnQueryContinueDrag
  413. read FOnDDQueryContinueDrag write FOnDDQueryContinueDrag;
  414. property OnDDGiveFeedback: TDDOnGiveFeedback
  415. read FOnDDGiveFeedback write FOnDDGiveFeedback;
  416. property OnDDChooseEffect: TDDOnChooseEffect
  417. read FOnDDChooseEffect write FOnDDChooseEffect;
  418. {A drag&drop operation is about to be initiated whith
  419. the components window as the source:}
  420. property OnDDDragDetect: TDDOnDragDetect
  421. read FOnDDDragDetect write FOnDDDragDetect;
  422. property OnDDCreateDragFileList: TDDOnCreateDragFileList
  423. read FOnDDCreateDragFileList write FOnDDCreateDragFileList;
  424. property OnDDEnd: TNotifyEvent
  425. read FOnDDEnd write FOnDDEnd;
  426. property OnDDCreateDataObject: TDDOnCreateDataObject
  427. read FOnDDCreateDataObject write FOnDDCreateDataObject;
  428. property OnDDTargetHasDropHandler: TDDOnTargetHasDropHandler
  429. read FOnDDTargetHasDropHandler write FOnDDTargetHasDropHandler;
  430. {The component window is the target of a drag&drop operation:}
  431. property OnDDProcessDropped: TOnProcessDropped
  432. read FOnDDProcessDropped write FOnDDProcessDropped;
  433. {An error has occurred during a drag&drop operation:}
  434. property OnDDError: TDDErrorEvent read FOnDDError write FOnDDError;
  435. {The drag&drop operation has been executed:}
  436. property OnDDExecuted: TDDExecutedEvent
  437. read FOnDDExecuted write FOnDDExecuted;
  438. {Event is fired just before executing the fileoperation. This event is also fired when
  439. files are pasted from the clipboard:}
  440. property OnDDFileOperation: TDDFileOperationEvent
  441. read FOnDDFileOperation write FOnDDFileOperation;
  442. {Event is fired after executing the fileoperation. This event is also fired when
  443. files are pasted from the clipboard:}
  444. property OnDDFileOperationExecuted: TDDFileOperationExecutedEvent
  445. read FOnDDFileOperationExecuted write FOnDDFileOperationExecuted;
  446. {Set AllowExec to false, if actual file should not be executed:}
  447. property OnDDMenuPopup: TOnMenuPopup read FOnDDMenuPopup write FOnDDMenuPopup;
  448. property OnExecFile: TDirViewExecFileEvent
  449. read FOnExecFile write FOnExecFile;
  450. property OnHistoryChange: TDirViewNotifyEvent read FOnHistoryChange write FOnHistoryChange;
  451. property OnHistoryGo: TDVHistoryGoEvent read FOnHistoryGo write FOnHistoryGo;
  452. property OnPathChange: TDirViewNotifyEvent read FOnPathChange write FOnPathChange;
  453. property OnMatchMask: TMatchMaskEvent read FOnMatchMask write FOnMatchMask;
  454. property OnGetOverlay: TDirViewGetOverlayEvent read FOnGetOverlay write FOnGetOverlay;
  455. property PathLabel: TCustomPathLabel read FPathLabel write SetPathLabel;
  456. property ShowHiddenFiles: Boolean read FShowHiddenFiles write SetShowHiddenFiles default True;
  457. property OnUpdateStatusBar: TDirViewUpdateStatusBarEvent read FOnUpdateStatusBar write FOnUpdateStatusBar;
  458. property OnBusy: TDirViewBusy read FOnBusy write FOnBusy;
  459. {Watch current directory for filename changes (create, rename, delete files)}
  460. property WatchForChanges: Boolean read FWatchForChanges write SetWatchForChanges default False;
  461. end;
  462. resourcestring
  463. SErrorOpenFile = 'Can''t open file: ';
  464. SErrorRenameFile = 'Can''t rename file or directory: ';
  465. SErrorRenameFileExists = 'File already exists: ';
  466. SErrorInvalidName= 'Filename contains invalid characters:';
  467. STextFileExt = 'File %s';
  468. STextFiles = '%u Files';
  469. STextDirectories = '%u Directories';
  470. SParentDir = 'Parent directory';
  471. SIconUpdateThreadTerminationError = 'Can''t terminate icon update thread.';
  472. SDragDropError = 'DragDrop Error: %d';
  473. SDriveNotReady = 'Drive ''%s:'' is not ready.';
  474. SDirNotExists = 'Directory ''%s'' doesn''t exist.';
  475. {Additional non-component specific functions:}
  476. {Create and resolve a shell link (file shortcut):}
  477. function CreateFileShortCut(SourceFile, Target, DisplayName: string;
  478. UpdateIfExists: Boolean = False): Boolean;
  479. function ResolveFileShortCut(SourceFile: string; ShowDialog: Boolean = False): string;
  480. {Gets the shell's display icon for registered file extensions:}
  481. function GetIconIndex(const AFile: string; Attrs: DWORD; Flags: UINT): Integer;
  482. {Gets the shell's inforecord for registered fileextensions:}
  483. function GetshFileInfo(const AFile: string; Attrs: DWORD; Flags: UINT): TSHFileInfo;
  484. {Returns the displayname as used by the shell:}
  485. function GetShellDisplayName(const ShellFolder: IShellFolder; IDList: PItemIDList;
  486. Flags: DWORD; var Name: string): Boolean;
  487. function IsExecutable(FileName: string): Boolean;
  488. function GetNextMask(var Mask: string): string;
  489. procedure DefaultFileFilter(var Filter: TFileFilter);
  490. function OverlayImageList(Size: Integer): TImageList;
  491. var
  492. StdDirIcon: Integer;
  493. StdDirSelIcon: Integer;
  494. DropSourceControl: TObject;
  495. UnknownFileIcon: Integer = 0;
  496. StdDirTypeName: string;
  497. DefaultExeIcon: Integer;
  498. UserDocumentDirectory: string;
  499. implementation
  500. uses
  501. Math, DirViewColProperties, UITypes, Types;
  502. const
  503. Space = ' ';
  504. ResDirUp = 'DIRUP%2.2d';
  505. ResLink = 'LINK%2.2d';
  506. ResBrokenLink = 'BROKEN%2.2d';
  507. ResPartial = 'PARTIAL%2.2d';
  508. var
  509. WinDir: string;
  510. TempDir: string;
  511. GlobalsInitialized: Boolean = False;
  512. procedure InitGlobals;
  513. begin
  514. if not GlobalsInitialized then
  515. begin
  516. GlobalsInitialized := True;
  517. // Calling GetshFileInfo in Windows Session 0 sometime cause crash
  518. // (not immediately, but very shortly afterwards [few ms]).
  519. // So this code was moved from initialization section to avoid it
  520. // being used for non-GUI runs.
  521. UnknownFileIcon := GetshFileInfo('$#)(.#$)', FILE_ATTRIBUTE_NORMAL,
  522. SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES).iIcon;
  523. DefaultExeIcon := GetshFileInfo('.COM',
  524. FILE_ATTRIBUTE_NORMAL, SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES).iIcon;
  525. with GetshFileInfo(WinDir, FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY,
  526. SHGFI_TYPENAME or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES) do
  527. begin
  528. StdDirTypeName := szTypeName;
  529. StdDirIcon := iIcon;
  530. end;
  531. StdDirSelIcon := GetIconIndex(WinDir,
  532. FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY, SHGFI_OPENICON);
  533. end;
  534. end;
  535. type
  536. TDirViewState = class(TObject)
  537. public
  538. destructor Destroy; override;
  539. private
  540. HistoryPaths: TStrings;
  541. BackCount: Integer;
  542. SortStr: string;
  543. Mask: string;
  544. FocusedItem: string;
  545. end;
  546. destructor TDirViewState.Destroy;
  547. begin
  548. HistoryPaths.Free;
  549. inherited;
  550. end;
  551. function IsExecutable(FileName: string): Boolean;
  552. var
  553. FileExt: string;
  554. begin
  555. FileExt := UpperCase(ExtractFileExt(FileName));
  556. Result := (FileExt = '.EXE') or (FileExt = '.COM');
  557. end;
  558. function GetNextMask(var Mask: string): string;
  559. var
  560. NextPos: Integer;
  561. begin
  562. NextPos := Pos(';', Mask);
  563. if NextPos = 0 then
  564. begin
  565. Result := Mask;
  566. SetLength(Mask, 0);
  567. end
  568. else
  569. begin
  570. Result := Copy(Mask, 1, NextPos - 1);
  571. Delete(Mask, 1, NextPos);
  572. end;
  573. end;
  574. procedure DefaultFileFilter(var Filter: TFileFilter);
  575. begin
  576. with Filter do
  577. begin
  578. SetLength(Masks, 0);
  579. IncludeAttr := 0;
  580. ExcludeAttr := 0;
  581. Directories := False;
  582. FileSizeFrom := 0;
  583. FileSizeTo := 0;
  584. ModificationFrom := 0;
  585. ModificationTo := 0;
  586. end;
  587. end;
  588. { Shortcut-handling }
  589. function ResolveFileShortCut(SourceFile: string; ShowDialog: Boolean = False): string;
  590. var
  591. IUnk: IUnknown;
  592. HRes: HRESULT; // OLE-Operation Result
  593. SL: IShellLink; // Interface for ShellLink
  594. PF: IPersistFile; // Interface for PersistentFile
  595. SRec: TWIN32FINDDATA; // SearchRec of targetfile
  596. TargetDir: array[1..Max_Path] of Char; // Working directory of targetfile
  597. Flags: DWORD;
  598. begin
  599. Result := '';
  600. IUnk := CreateComObject(CLSID_ShellLink);
  601. SL := IUnk as IShellLink;
  602. PF := IUnk as IPersistFile;
  603. HRes := PF.Load(PChar(SourceFile), STGM_READ);
  604. if Succeeded(Hres) then
  605. begin
  606. if not ShowDialog then Flags := SLR_NOUPDATE or (1500 shl 8) or SLR_NO_UI
  607. else Flags := SLR_NOUPDATE;
  608. HRes := SL.Resolve(Application.Handle, Flags);
  609. if Succeeded(HRes) then
  610. begin
  611. HRes := SL.GetPath(@TargetDir, MAX_PATH, SRec, {SLGP_UNCPRIORITY}{SLGP_SHORTPATH} 0);
  612. if Succeeded(HRes) then
  613. Result := string(PChar(@TargetDir));
  614. end;
  615. end;
  616. end; {ResolveShortCut}
  617. function CreateFileShortCut(SourceFile, Target, DisplayName: string;
  618. UpdateIfExists: Boolean): Boolean;
  619. var
  620. IUnk: IUnknown;
  621. Hres: HRESULT;
  622. ShellLink: IShellLink; // Interface to ShellLink
  623. IPFile: IPersistFile; // Interface to PersistentFile
  624. TargetFile: string;
  625. begin
  626. Result := False;
  627. if Target = '' then TargetFile := SourceFile + '.lnk'
  628. else TargetFile := Target;
  629. IUnk := CreateComObject(CLSID_ShellLink);
  630. ShellLink := IUnk as IShellLink;
  631. IPFile := IUnk as IPersistFile;
  632. if FileExists(ApiPath(TargetFile)) and UpdateIfExists then
  633. begin
  634. HRes := IPFile.Load(PChar(TargetFile), 0);
  635. if not Succeeded(HRes) then Exit;
  636. end;
  637. with ShellLink do
  638. begin
  639. HRes := SetPath(PChar(SourceFile));
  640. if Succeeded(HRes) then
  641. HRes := SetWorkingDirectory(PChar(ExtractFilePath(SourceFile)));
  642. if Succeeded(HRes) and (DisplayName <> '') then
  643. HRes := SetDescription(PChar(DisplayName));
  644. end;
  645. if Succeeded(Hres) then
  646. begin
  647. HRes := IPFile.Save(PChar(TargetFile),False);
  648. if Succeeded(HRes) then Result := True;
  649. end;
  650. end; {CreateShortCut}
  651. function GetIconIndex(const AFile: string; Attrs: DWORD; Flags: UINT): Integer;
  652. var
  653. FileInfo: TSHFileInfo;
  654. begin
  655. try
  656. SHGetFileInfo(PChar(AFile), Attrs, FileInfo, SizeOf(TSHFileInfo),
  657. Flags or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);
  658. Result := FileInfo.iIcon;
  659. except
  660. Result := -1;
  661. end;
  662. end; {GetIconIndex}
  663. function GetshFileInfo(const AFile: string; Attrs: DWORD; Flags: UINT): TSHFileInfo;
  664. begin
  665. try
  666. SHGetFileInfo(PChar(AFile), Attrs, Result, SizeOf(TSHFileInfo), Flags);
  667. except
  668. FillChar(Result, SizeOf(Result), 0);
  669. end;
  670. end; {GetshFileInfo}
  671. function GetShellDisplayName(const ShellFolder: IShellFolder; IDList: PItemIDList;
  672. Flags: DWORD; var Name: string): Boolean;
  673. var
  674. Str: TStrRet;
  675. begin
  676. Result := True;
  677. Name := '';
  678. if ShellFolder.GetDisplayNameOf(IDList, Flags, Str) = NOERROR then
  679. begin
  680. case Str.uType of
  681. STRRET_WSTR: Name := WideCharToString(Str.pOleStr);
  682. STRRET_OFFSET: Name := PChar(UINT(IDList) + Str.uOffset);
  683. STRRET_CSTR: Name := string(Str.cStr);
  684. else Result := False;
  685. end;
  686. end
  687. else Result := False;
  688. end; {GetShellDisplayName}
  689. function OverlayImageList(Size: Integer): TImageList;
  690. procedure GetOverlayBitmap(ImageList: TImageList; BitmapName: string);
  691. var
  692. Bitmap: TBitmap;
  693. begin
  694. Bitmap := TBitmap.Create;
  695. try
  696. Bitmap.LoadFromResourceName(hInstance, BitmapName);
  697. ImageList.AddMasked(Bitmap, Bitmap.Canvas.Pixels[0, 0]);
  698. finally
  699. Bitmap.Free;
  700. end;
  701. end; {GetOverlayBitmap}
  702. var
  703. PixelsPerInch: Integer;
  704. Factor: Integer;
  705. begin
  706. // Hardcoded according to sizes of overlays we have in resources
  707. PixelsPerInch := Screen.PixelsPerInch;
  708. if PixelsPerInch >= 192 then Factor := 200
  709. else
  710. if PixelsPerInch >= 144 then Factor := 150
  711. else
  712. if PixelsPerInch >= 120 then Factor := 124
  713. else Factor := 100;
  714. Size := MulDiv(Size, Factor, 100);
  715. Result := TImageList.CreateSize(Size, Size);
  716. Result.DrawingStyle := dsTransparent;
  717. Result.BkColor := clNone;
  718. GetOverlayBitmap(Result, Format(ResDirUp, [Size]));
  719. GetOverlayBitmap(Result, Format(ResLink, [Size]));
  720. GetOverlayBitmap(Result, Format(ResBrokenLink, [Size]));
  721. GetOverlayBitmap(Result, Format(ResPartial, [Size]));
  722. end;
  723. { TLoadAnimationStartThread }
  724. {constructor TLoadAnimationStartThread.Create(AInterval: Integer; AAnimation: TAnimate);
  725. begin
  726. inherited Create(True);
  727. FInterval := AInterval;
  728. FAnimation := AAnimation;
  729. Resume;
  730. end;
  731. procedure TLoadAnimationStartThread.Execute;
  732. var
  733. XInterval: Integer;
  734. begin
  735. XInterval := FInterval;
  736. while (not Terminated) and (XInterval > 0) do
  737. begin
  738. Sleep(10);
  739. Dec(XInterval, 10);
  740. end;
  741. if (not Terminated) and Assigned(FAnimation) then
  742. Synchronize(StartAnimation);
  743. end;
  744. procedure TLoadAnimationStartThread.StartAnimation;
  745. begin
  746. FAnimation.Visible := True;
  747. FAnimation.Active := True;
  748. end; }
  749. { TCustomizableDragDropFilesEx }
  750. function TCustomizableDragDropFilesEx.Execute(DataObject: TDataObject): TDragResult;
  751. begin
  752. if not Assigned(DataObject) then
  753. begin
  754. DataObject := CreateDataObject;
  755. end;
  756. Result := ExecuteOperation(DataObject);
  757. end;
  758. { TCustomDirView }
  759. constructor TCustomDirView.Create(AOwner: TComponent);
  760. begin
  761. InitGlobals;
  762. inherited;
  763. FWatchForChanges := False;
  764. FFilesSize := 0;
  765. FFilesSelSize := 0;
  766. FDimmHiddenFiles := True;
  767. FShowHiddenFiles := True;
  768. FFormatSizeBytes := fbNone;
  769. FWantUseDragImages := False;
  770. FAddParentDir := False;
  771. FullDrag := True;
  772. FInvalidNameChars := '\/:*?"<>|';
  773. FHasParentDir := False;
  774. FDragOnDriveIsMove := False;
  775. FCaseSensitive := False;
  776. FLoadAnimation := True;
  777. FAnimation := nil;
  778. FIsRecycleBin := False;
  779. FLoading := False;
  780. FLoadEnabled := True;
  781. FAbortLoading := False;
  782. FDirty := False;
  783. FLastPath := '';
  784. FHistoryPath := '';
  785. FNotifyEnabled := True;
  786. FForceRename := False;
  787. FLastRenameName := '';
  788. FSavedSelection := False;
  789. FPendingFocusSomething := False;
  790. FSavedNames := TStringList.Create;
  791. FContextMenu := False;
  792. FUseSystemContextMenu := True;
  793. FStartPos.X := -1;
  794. FStartPos.Y := -1;
  795. FDragPos := FStartPos;
  796. FDragEnabled := False;
  797. FDDOwnerIsSource := False;
  798. FDDLinkOnExeDrag := False;
  799. FDragDrive := #0;
  800. FExeDrag := False;
  801. FMask := '';
  802. FDoubleBufferedScrollingWorkaround := not IsVistaHard();
  803. FOnHistoryChange := nil;
  804. FOnPathChange := nil;
  805. FHistoryPaths := TStringList.Create;
  806. FBackCount := 0;
  807. FDontRecordPath := False;
  808. FMaxHistoryCount := DefaultHistoryCount;
  809. FStatusFileInfo.FilesCount := -1;
  810. OnCustomDrawItem := DumbCustomDrawItem;
  811. OnCustomDrawSubItem := DumbCustomDrawSubItem;
  812. FOnMatchMask := nil;
  813. FOnGetOverlay := nil;
  814. FDragDropFilesEx := TCustomizableDragDropFilesEx.Create(Self);
  815. with FDragDropFilesEx do
  816. begin
  817. AutoDetectDnD := False;
  818. DragDetectDelta := 4;
  819. AcceptOwnDnD := True;
  820. BringToFront := True;
  821. CompleteFileList := True;
  822. NeedValid := [nvFileName];
  823. RenderDataOn := rdoEnterAndDropSync;
  824. TargetPopUpMenu := True;
  825. SourceEffects := DragSourceEffects;
  826. TargetEffects := [deCopy, deMove];
  827. OnDragEnter := DDDragEnter;
  828. OnDragLeave := DDDragLeave;
  829. OnDragOver := DDDragOver;
  830. OnDrop := DDDrop;
  831. OnQueryContinueDrag := DDQueryContinueDrag;
  832. OnSpecifyDropTarget := DDSpecifyDropTarget;
  833. OnMenuPopup := DDMenuPopup;
  834. OnMenuDestroy := DDMenuDone;
  835. OnDropHandlerSucceeded := DDDropHandlerSucceeded;
  836. OnGiveFeedback := DDGiveFeedback;
  837. OnProcessDropped := DDProcessDropped;
  838. OnDragDetect := DDDragDetect;
  839. end;
  840. FScrollOnDragOver := TListViewScrollOnDragOver.Create(Self, False);
  841. FScrollOnDragOver.OnBeforeUpdate := ScrollOnDragOverBeforeUpdate;
  842. FScrollOnDragOver.OnAfterUpdate := ScrollOnDragOverAfterUpdate;
  843. end;
  844. procedure TCustomDirView.ClearItems;
  845. begin
  846. CancelEdit;
  847. if Assigned(DropTarget) then DropTarget := nil;
  848. try
  849. inherited;
  850. finally
  851. FFilesSelSize := 0;
  852. FFilesSize := 0;
  853. UpdateStatusBar;
  854. end;
  855. end;
  856. procedure TCustomDirView.CNNotify(var Message: TWMNotify);
  857. procedure DrawOverlayImage(DC: HDC; Image: Integer);
  858. var
  859. ImageList: TCustomImageList;
  860. Rect: TRect;
  861. Point: TPoint;
  862. Index: Integer;
  863. begin
  864. Rect := Items[PNMCustomDraw(Message.NMHdr)^.dwItemSpec].DisplayRect(drIcon);
  865. Point := Rect.TopLeft;
  866. if ViewStyle = vsIcon then
  867. begin
  868. ImageList := ImageList32;
  869. end
  870. else
  871. begin
  872. ImageList := ImageList16;
  873. end;
  874. // center on the rect
  875. Inc(Point.X, (Rect.Width - ImageList.Width) div 2);
  876. Inc(Point.Y, (Rect.Height - ImageList.Height) div 2);
  877. Index := 0;
  878. while Image > 1 do
  879. begin
  880. Inc(Index);
  881. Image := Image shr 1;
  882. end;
  883. if 8 + ImageList.Width <= Columns[0].Width then
  884. begin
  885. ImageList_Draw(ImageList.Handle, Index, DC,
  886. Point.X, Point.Y, ILD_TRANSPARENT);
  887. end;
  888. end;
  889. var
  890. FileSize: Int64;
  891. Item: TListItem;
  892. InfoMask: LongWord;
  893. OverlayIndex: Word;
  894. OverlayIndexes: Word;
  895. UpdateStatusBarPending: Boolean;
  896. begin
  897. UpdateStatusBarPending := False;
  898. case Message.NMHdr^.code of
  899. LVN_ITEMCHANGED:
  900. with PNMListView(Message.NMHdr)^ do
  901. if (uChanged = LVIF_STATE) and Valid and (not FClearingItems) then
  902. begin
  903. if ((uOldState and (LVIS_SELECTED or LVIS_FOCUSED)) <>
  904. (uNewState and (LVIS_SELECTED or LVIS_FOCUSED))) then
  905. UpdateStatusBarPending := True;
  906. if ((uOldState and LVIS_SELECTED) <> (uNewState and LVIS_SELECTED)) then
  907. begin
  908. FileSize := ItemFileSize(Items[iItem]);
  909. if (uOldState and LVIS_SELECTED) <> 0 then Dec(FFilesSelSize, FileSize)
  910. else Inc(FFilesSelSize, FileSize);
  911. end;
  912. end;
  913. LVN_ENDLABELEDIT:
  914. // enable loading now only when editing was canceled.
  915. // when it was confirmed, it will be enabled only after actual
  916. // file renaming is completed. see Edit().
  917. with PLVDispInfo(Message.NMHdr)^ do
  918. if (item.pszText = nil) or (item.IItem = -1) then
  919. LoadEnabled := True;
  920. LVN_BEGINDRAG:
  921. if FDragEnabled and (not Loading) then
  922. begin
  923. DDBeforeDrag;
  924. DDDragDetect(MK_LBUTTON, FStartPos, Mouse.CursorPos, ddsDrag);
  925. end;
  926. LVN_BEGINRDRAG:
  927. if FDragEnabled and (not Loading) then
  928. begin
  929. DDBeforeDrag;
  930. DDDragDetect(MK_RBUTTON, FStartPos, Mouse.CursorPos, ddsDrag);
  931. end;
  932. end;
  933. inherited;
  934. if (Message.NMHdr.code = LVN_GETDISPINFO) and
  935. FNotifyEnabled and Valid and (not Loading) then
  936. with PLVDispInfo(Pointer(Message.NMHdr))^.Item do
  937. try
  938. InfoMask := PLVDispInfo(Pointer(Message.NMHdr))^.item.Mask;
  939. if (InfoMask and LVIF_PARAM) <> 0 then Item := TListItem(lParam)
  940. else
  941. if iItem < Items.Count then Item := Items[iItem]
  942. else Item := nil;
  943. if Assigned(Item) and Assigned(Item.Data) then
  944. GetDisplayInfo(Item, PLVDispInfo(Pointer(Message.NMHdr))^.item);
  945. except
  946. end;
  947. if (Message.NMHdr.code = NM_CUSTOMDRAW) and
  948. Valid and (not Loading) then
  949. with PNMLVCustomDraw(Message.NMHdr)^ do
  950. try
  951. Message.Result := Message.Result or CDRF_NOTIFYPOSTPAINT;
  952. if (nmcd.dwDrawStage = CDDS_ITEMPOSTPAINT) and
  953. ((nmcd.dwDrawStage and CDDS_SUBITEM) = 0) and
  954. Assigned(Columns[0]) and (Columns[0].Width > 0) then
  955. begin
  956. Assert(Assigned(Items[nmcd.dwItemSpec]));
  957. OverlayIndexes := ItemOverlayIndexes(Items[nmcd.dwItemSpec]);
  958. OverlayIndex := 1;
  959. while OverlayIndexes > 0 do
  960. begin
  961. if (OverlayIndex and OverlayIndexes) <> 0 then
  962. begin
  963. DrawOverlayImage(nmcd.hdc, OverlayIndex);
  964. Dec(OverlayIndexes, OverlayIndex);
  965. end;
  966. OverlayIndex := OverlayIndex shl 1;
  967. end;
  968. end;
  969. except
  970. end;
  971. if UpdateStatusBarPending then UpdateStatusBar;
  972. end;
  973. function TCustomDirView.FileNameMatchesMasks(FileName: string;
  974. Directory: Boolean; Size: Int64; Modification: TDateTime; Masks: string;
  975. AllowImplicitMatches: Boolean): Boolean;
  976. begin
  977. Result := False;
  978. if Assigned(OnMatchMask) then
  979. OnMatchMask(Self, FileName, Directory, Size, Modification, Masks, Result, AllowImplicitMatches)
  980. end;
  981. procedure TCustomDirView.SetAddParentDir(Value: Boolean);
  982. begin
  983. if FAddParentDir <> Value then
  984. begin
  985. FAddParentDir := Value;
  986. if DirOK then Reload(True);
  987. end;
  988. end;
  989. procedure TCustomDirView.SetDimmHiddenFiles(Value: Boolean);
  990. begin
  991. if Value <> FDimmHiddenFiles then
  992. begin
  993. FDimmHiddenFiles := Value;
  994. Self.Repaint;
  995. end;
  996. end; {SetDimmHiddenFiles}
  997. procedure TCustomDirView.SetPathLabel(Value: TCustomPathLabel);
  998. begin
  999. if FPathLabel <> Value then
  1000. begin
  1001. if Assigned(FPathLabel) and (FPathLabel.FocusControl = Self) then
  1002. FPathLabel.FocusControl := nil;
  1003. FPathLabel := Value;
  1004. if Assigned(Value) then
  1005. begin
  1006. Value.FreeNotification(Self);
  1007. if not Assigned(Value.FocusControl) then
  1008. Value.FocusControl := Self;
  1009. UpdatePathLabel;
  1010. end;
  1011. end;
  1012. end; { SetPathLabel }
  1013. procedure TCustomDirView.SetShowHiddenFiles(Value: Boolean);
  1014. begin
  1015. if ShowHiddenFiles <> Value then
  1016. begin
  1017. FShowHiddenFiles := Value;
  1018. if DirOK then Reload(False);
  1019. end;
  1020. end;
  1021. procedure TCustomDirView.SetFormatSizeBytes(Value: TFormatBytesStyle);
  1022. begin
  1023. if Value <> FFormatSizeBytes then
  1024. begin
  1025. FFormatSizeBytes := Value;
  1026. Self.Repaint;
  1027. end;
  1028. end; {SetFormatSizeBytes}
  1029. function TCustomDirView.GetDragSourceEffects: TDropEffectSet;
  1030. begin
  1031. Result := [deCopy, deMove, deLink];
  1032. end;
  1033. function TCustomDirView.GetUseDragImages: Boolean;
  1034. begin
  1035. Result := FWantUseDragImages;
  1036. end;
  1037. procedure TCustomDirView.SetTargetPopupMenu(Value: Boolean);
  1038. begin
  1039. if Assigned(FDragDropFilesEx) then FDragDropFilesEx.TargetPopupMenu := Value;
  1040. end;
  1041. procedure TCustomDirView.CreateWnd;
  1042. begin
  1043. inherited;
  1044. if Assigned(PopupMenu) then
  1045. PopupMenu.Autopopup := False;
  1046. FDragDropFilesEx.DragDropControl := Self;
  1047. if not Assigned(FImageList16) then
  1048. FImageList16 := OverlayImageList(16);
  1049. if not Assigned(FImageList32) then
  1050. FImageList32 := OverlayImageList(32);
  1051. IconsSetImageList;
  1052. end;
  1053. procedure TCustomDirView.LVMSetExtendedListViewStyle(var Message: TMessage);
  1054. // Only TWinControl.DoubleBuffered actually prevents flicker
  1055. // on Win7 when moving mouse over list view, not LVS_EX_DOUBLEBUFFER.
  1056. // But LVS_EX_DOUBLEBUFFER brings nice alpha blended marquee selection.
  1057. // Double buffering introduces artefacts when scrolling using
  1058. // keyboard (Page-up/Down). This gets fixed by LVS_EX_TRANSPARENTBKGND,
  1059. // but that works on Vista and newer only. See WMKeyDown
  1060. // for workaround on earlier systems.
  1061. const
  1062. RequiredStyles = LVS_EX_DOUBLEBUFFER or LVS_EX_TRANSPARENTBKGND;
  1063. begin
  1064. // This prevents TCustomListView.ResetExStyles resetting our styles
  1065. if (Message.WParam = 0) and
  1066. ((Message.LParam and RequiredStyles) <> RequiredStyles) then
  1067. begin
  1068. ListView_SetExtendedListViewStyle(Handle, Message.LParam or RequiredStyles);
  1069. end
  1070. else
  1071. begin
  1072. inherited;
  1073. end;
  1074. end;
  1075. procedure TCustomDirView.DestroyWnd;
  1076. begin
  1077. // to force drag&drop re-registration when recreating handle
  1078. // (occurs when changing ViewStyle)
  1079. FDragDropFilesEx.DragDropControl := nil;
  1080. // Destroy the animation, as we keep getting reports that the animation fails to recreate
  1081. DoAnimation(False);
  1082. inherited;
  1083. end;
  1084. function TCustomDirView.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  1085. Stage: TCustomDrawStage): Boolean;
  1086. var
  1087. FItemColor: TColor;
  1088. begin
  1089. if (Item <> nil) and (Stage = cdPrePaint) then
  1090. begin
  1091. FItemColor := ItemColor(Item);
  1092. if (FItemColor <> clDefaultItemColor) and
  1093. (Canvas.Font.Color <> FItemColor) then
  1094. Canvas.Font.Color := FItemColor;
  1095. end;
  1096. Result := inherited CustomDrawItem(Item, State, Stage);
  1097. end;
  1098. function TCustomDirView.CustomDrawSubItem(Item: TListItem; SubItem: Integer;
  1099. State: TCustomDrawState; Stage: TCustomDrawStage): Boolean;
  1100. var
  1101. FItemColor: TColor;
  1102. begin
  1103. if Stage = cdPrePaint then
  1104. begin
  1105. FItemColor := ItemColor(Item);
  1106. if (FItemColor <> clDefaultItemColor) and
  1107. (Canvas.Font.Color <> FItemColor) then
  1108. Canvas.Font.Color := FItemColor;
  1109. end;
  1110. Result := inherited CustomDrawSubItem(Item, SubItem, State, Stage);
  1111. end;
  1112. procedure TCustomDirView.Delete(Item: TListItem);
  1113. begin
  1114. if Assigned(Item) then
  1115. begin
  1116. // This causes access violation when size is stored in structure
  1117. // pointed by TListItem->Data and this structure is not valid any more
  1118. if Valid then Dec(FFilesSize, ItemFileSize(Item));
  1119. inherited Delete(Item);
  1120. end;
  1121. end;
  1122. destructor TCustomDirView.Destroy;
  1123. begin
  1124. Assert(not FSavedSelection);
  1125. FreeAndNil(FScrollOnDragOver);
  1126. FreeAndNil(FSavedNames);
  1127. FreeAndNil(FHistoryPaths);
  1128. FreeAndNil(FDragDropFilesEx);
  1129. FreeAndNil(FImageList16);
  1130. FreeAndNil(FImageList32);
  1131. if Assigned(SmallImages) then
  1132. begin
  1133. SmallImages.Free;
  1134. SmallImages := nil;
  1135. end;
  1136. if Assigned(LargeImages) then
  1137. begin
  1138. LargeImages.Free;
  1139. LargeImages := nil;
  1140. end;
  1141. FreeAndNil(FAnimation);
  1142. inherited;
  1143. end;
  1144. procedure TCustomDirView.SelectFiles(Filter: TFileFilter; Select: Boolean);
  1145. var
  1146. Item: TListItem;
  1147. Index: Integer;
  1148. OldCursor: TCursor;
  1149. begin
  1150. Assert(Valid);
  1151. OldCursor := Screen.Cursor;
  1152. Items.BeginUpdate;
  1153. BeginSelectionUpdate;
  1154. try
  1155. Screen.Cursor := crHourGlass;
  1156. for Index := 0 to Items.Count-1 do
  1157. begin
  1158. Item := Items[Index];
  1159. Assert(Assigned(Item));
  1160. if (Item.Selected <> Select) and
  1161. ItemMatchesFilter(Item, Filter) then
  1162. Item.Selected := Select;
  1163. end;
  1164. finally
  1165. Screen.Cursor := OldCursor;
  1166. Items.EndUpdate;
  1167. EndSelectionUpdate;
  1168. end;
  1169. end;
  1170. function TCustomDirView.DoSelectByMask(Select: Boolean): Boolean;
  1171. var
  1172. Filter: TFileFilter;
  1173. begin
  1174. Result := inherited DoSelectByMask(Select);
  1175. if Assigned(FOnGetSelectFilter) then
  1176. begin
  1177. DefaultFileFilter(Filter);
  1178. FOnGetSelectFilter(Self, Select, Filter);
  1179. SelectFiles(Filter, Select);
  1180. Result := True;
  1181. end;
  1182. end;
  1183. function TCustomDirView.DragCompleteFileList: Boolean;
  1184. begin
  1185. Result := (MarkedCount <= 100) and (not IsRecycleBin);
  1186. end;
  1187. procedure TCustomDirView.DumbCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  1188. begin
  1189. end;
  1190. procedure TCustomDirView.DumbCustomDrawSubItem(Sender: TCustomListView;
  1191. Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  1192. var DefaultDraw: Boolean);
  1193. begin
  1194. end;
  1195. function TCustomDirView.GetTargetPopupMenu: Boolean;
  1196. begin
  1197. if Assigned(FDragDropFilesEx) then Result := FDragDropFilesEx.TargetPopupMenu
  1198. else Result := True;
  1199. end;
  1200. procedure TCustomDirView.SetMultiSelect(Value: Boolean);
  1201. begin
  1202. if Value <> MultiSelect then
  1203. begin
  1204. inherited SetMultiSelect(Value);
  1205. if not (csLoading in ComponentState) and Assigned(ColProperties) then
  1206. begin
  1207. ColProperties.RecreateColumns;
  1208. SetColumnImages;
  1209. if DirOK then Reload(True);
  1210. end;
  1211. end;
  1212. end;
  1213. function TCustomDirView.GetValid: Boolean;
  1214. begin
  1215. Result := (not (csDestroying in ComponentState)) and
  1216. (not Loading) and (not FClearingItems);
  1217. end;
  1218. function TCustomDirView.ItemCanDrag(Item: TListItem): Boolean;
  1219. begin
  1220. Result := (not ItemIsParentDirectory(Item));
  1221. end;
  1222. function TCustomDirView.ItemColor(Item: TListItem): TColor;
  1223. begin
  1224. Result := clDefaultItemColor;
  1225. end;
  1226. function TCustomDirView.GetFilesMarkedSize: Int64;
  1227. begin
  1228. if SelCount > 0 then Result := FilesSelSize
  1229. else
  1230. if Assigned(ItemFocused) then Result := ItemFileSize(ItemFocused)
  1231. else Result := 0;
  1232. end;
  1233. procedure TCustomDirView.IconsSetImageList;
  1234. begin
  1235. if not Assigned(SmallImages) then
  1236. SmallImages := ShellImageList(Self, SHGFI_SMALLICON);
  1237. if not Assigned(LargeImages) then
  1238. LargeImages := ShellImageList(Self, SHGFI_LARGEICON);
  1239. end; {IconsSetImageList}
  1240. function TCustomDirView.ItemIsRecycleBin(Item: TListItem): Boolean;
  1241. begin
  1242. Result := False;
  1243. end;
  1244. function TCustomDirView.ItemOverlayIndexes(Item: TListItem): Word;
  1245. begin
  1246. Result := oiNoOverlay;
  1247. if Assigned(OnGetOverlay) then
  1248. OnGetOverlay(Self, Item, Result);
  1249. end;
  1250. procedure TCustomDirView.WMKeyDown(var Message: TWMKeyDown);
  1251. begin
  1252. if DoubleBuffered and (Message.CharCode in [VK_PRIOR, VK_NEXT]) and
  1253. FDoubleBufferedScrollingWorkaround then
  1254. begin
  1255. // WORKAROUND
  1256. // When scrolling with double-buffering enabled, ugly artefacts
  1257. // are shown temporarily.
  1258. // LVS_EX_TRANSPARENTBKGND fixes it on Vista and newer
  1259. SendMessage(Handle, WM_SETREDRAW, 0, 0);
  1260. try
  1261. inherited;
  1262. finally
  1263. SendMessage(Handle, WM_SETREDRAW, 1, 0);
  1264. end;
  1265. Repaint;
  1266. end
  1267. else
  1268. begin
  1269. inherited;
  1270. end;
  1271. end;
  1272. procedure TCustomDirView.DoDisplayPropertiesMenu;
  1273. begin
  1274. if not IsBusy then
  1275. DisplayPropertiesMenu;
  1276. end;
  1277. procedure TCustomDirView.DoExecute(Item: TListItem);
  1278. begin
  1279. BusyOperation(procedure begin Execute(Item); end);
  1280. end;
  1281. procedure TCustomDirView.DoExecuteParentDirectory;
  1282. begin
  1283. BusyOperation(ExecuteParentDirectory);
  1284. end;
  1285. procedure TCustomDirView.KeyDown(var Key: Word; Shift: TShiftState);
  1286. begin
  1287. if Valid and (not IsEditing) and (not Loading) then
  1288. begin
  1289. if (Key = VK_RETURN) or
  1290. ((Key = VK_NEXT) and (ssCtrl in Shift)) then
  1291. begin
  1292. if Assigned(ItemFocused) then
  1293. begin
  1294. Key := 0;
  1295. if (Key = VK_RETURN) and (Shift = [ssAlt]) then DoDisplayPropertiesMenu
  1296. else
  1297. if (Key <> 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. if Length(HItem.pszText) = 0 then LoadEnabled := True
  2200. else
  2201. begin
  2202. {Does the changed filename contains invalid characters?}
  2203. if StrContains(FInvalidNameChars, HItem.pszText) then
  2204. begin
  2205. Info := FInvalidNameChars;
  2206. for Index := Length(Info) downto 1 do
  2207. System.Insert(Space, Info, Index);
  2208. MessageBeep(MB_ICONHAND);
  2209. if MessageDlg(SErrorInvalidName + Space + Info, mtError,
  2210. [mbOK, mbAbort], 0) = mrOK then RetryRename(HItem.pszText);
  2211. LoadEnabled := True;
  2212. end
  2213. else
  2214. begin
  2215. InternalEdit(HItem);
  2216. end;
  2217. end;
  2218. end; {Edit}
  2219. procedure TCustomDirView.EndSelectionUpdate;
  2220. begin
  2221. inherited;
  2222. if FUpdatingSelection = 0 then
  2223. UpdateStatusBar;
  2224. end; { EndUpdatingSelection }
  2225. procedure TCustomDirView.ExecuteCurrentFile;
  2226. begin
  2227. Assert(Assigned(ItemFocused));
  2228. Execute(ItemFocused);
  2229. end;
  2230. procedure TCustomDirView.Execute(Item: TListItem);
  2231. var
  2232. AllowExec: Boolean;
  2233. begin
  2234. Assert(Assigned(Item));
  2235. if Assigned(Item) and Assigned(Item.Data) and (not Loading) then
  2236. begin
  2237. if IsRecycleBin and (not ItemIsParentDirectory(Item)) then DisplayPropertiesMenu
  2238. else
  2239. begin
  2240. AllowExec := True;
  2241. if Assigned(FOnExecFile) then FOnExecFile(Self, Item, AllowExec);
  2242. if AllowExec then
  2243. begin
  2244. if ItemIsParentDirectory(Item) then ExecuteParentDirectory
  2245. else ExecuteFile(Item);
  2246. end;
  2247. end;
  2248. end;
  2249. end;
  2250. procedure TCustomDirView.GetDisplayInfo(ListItem: TListItem;
  2251. var DispInfo: TLVItem);
  2252. begin
  2253. // Nothing
  2254. end;
  2255. procedure TCustomDirView.WMUserRename(var Message: TMessage);
  2256. begin
  2257. if Assigned(ItemFocused) then
  2258. begin
  2259. FForceRename := True;
  2260. ListView_EditLabel(Handle, ItemFocused.Index);
  2261. SetWindowText(ListView_GetEditControl(Self.Handle),
  2262. PChar(FLastRenameName));
  2263. end;
  2264. end;
  2265. procedure TCustomDirView.RetryRename(NewName: string);
  2266. begin
  2267. FLastRenameName := NewName;
  2268. PostMessage(Self.Handle, WM_USER_RENAME, Longint(PChar(NewName)), 0);
  2269. end;
  2270. procedure TCustomDirView.AddToDragFileList(FileList: TFileList; Item: TListItem);
  2271. begin
  2272. FileList.AddItem(nil, ItemFullFileName(Item));
  2273. end;
  2274. procedure TCustomDirView.DDDragDetect(grfKeyState: Integer; DetectStart,
  2275. Point: TPoint; DragStatus: TDragDetectStatus);
  2276. var
  2277. FilesCount: Integer;
  2278. DirsCount: Integer;
  2279. Item: TListItem;
  2280. FirstItem : TListItem;
  2281. Bitmap: TBitmap;
  2282. ImageListHandle: HImageList;
  2283. Spot: TPoint;
  2284. ItemPos: TPoint;
  2285. DragText: string;
  2286. ClientPoint: TPoint;
  2287. FileListCreated: Boolean;
  2288. AvoidDragImage: Boolean;
  2289. DataObject: TDataObject;
  2290. begin
  2291. if Assigned(FOnDDDragDetect) then
  2292. FOnDDDragDetect(Self, grfKeyState, DetectStart, Point, DragStatus);
  2293. FLastDDResult := drCancelled;
  2294. if (DragStatus = ddsDrag) and (not Loading) and (MarkedCount > 0) then
  2295. begin
  2296. DragDropFilesEx.CompleteFileList := DragCompleteFileList;
  2297. DragDropFilesEx.FileList.Clear;
  2298. FirstItem := nil;
  2299. FilesCount := 0;
  2300. DirsCount := 0;
  2301. FileListCreated := False;
  2302. AvoidDragImage := False;
  2303. if Assigned(OnDDCreateDragFileList) then
  2304. begin
  2305. OnDDCreateDragFileList(Self, DragDropFilesEx.FileList, FileListCreated);
  2306. if FileListCreated then
  2307. begin
  2308. AvoidDragImage := True;
  2309. end;
  2310. end;
  2311. if not FileListCreated then
  2312. begin
  2313. if Assigned(ItemFocused) and (not ItemFocused.Selected) then
  2314. begin
  2315. if ItemCanDrag(ItemFocused) then
  2316. begin
  2317. FirstItem := ItemFocused;
  2318. AddToDragFileList(DragDropFilesEx.FileList, ItemFocused);
  2319. if ItemIsDirectory(ItemFocused) then Inc(DirsCount)
  2320. else Inc(FilesCount);
  2321. end;
  2322. end
  2323. else
  2324. if SelCount > 0 then
  2325. begin
  2326. Item := GetNextItem(nil, sdAll, [isSelected]);
  2327. while Assigned(Item) do
  2328. begin
  2329. if ItemCanDrag(Item) then
  2330. begin
  2331. if not Assigned(FirstItem) then FirstItem := Item;
  2332. AddToDragFileList(DragDropFilesEx.FileList, Item);
  2333. if ItemIsDirectory(Item) then Inc(DirsCount)
  2334. else Inc(FilesCount);
  2335. end;
  2336. Item := GetNextItem(Item, sdAll, [isSelected]);
  2337. end;
  2338. end;
  2339. end;
  2340. if DragDropFilesEx.FileList.Count > 0 then
  2341. begin
  2342. FDragEnabled := False;
  2343. {Create the dragimage:}
  2344. GlobalDragImageList := DragImageList;
  2345. // This code is not used anymore
  2346. if UseDragImages and (not AvoidDragImage) then
  2347. begin
  2348. ImageListHandle := ListView_CreateDragImage(Handle, FirstItem.Index, Spot);
  2349. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2350. if ImageListHandle <> Invalid_Handle_Value then
  2351. begin
  2352. GlobalDragImageList.Handle := ImageListHandle;
  2353. if FilesCount + DirsCount = 1 then
  2354. begin
  2355. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2356. GlobalDragImageList.SetDragImage(0,
  2357. DetectStart.X - ItemPos.X, DetectStart.Y - ItemPos.Y);
  2358. end
  2359. else
  2360. begin
  2361. GlobalDragImageList.Clear;
  2362. GlobalDragImageList.Width := 32;
  2363. GlobalDragImageList.Height := 32;
  2364. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES', 0,
  2365. [lrTransparent], $FFFFFF) Then
  2366. begin
  2367. Bitmap := TBitmap.Create;
  2368. try
  2369. try
  2370. GlobalDragImageList.GetBitmap(0, Bitmap);
  2371. Bitmap.Canvas.Font.Assign(Self.Font);
  2372. DragText := '';
  2373. if FilesCount > 0 then
  2374. DragText := Format(STextFiles, [FilesCount]);
  2375. if DirsCount > 0 then
  2376. begin
  2377. if FilesCount > 0 then
  2378. DragText := DragText + ', ';
  2379. DragText := DragText + Format(STextDirectories, [DirsCount]);
  2380. end;
  2381. Bitmap.Width := 33 + Bitmap.Canvas.TextWidth(DragText);
  2382. Bitmap.TransparentMode := tmAuto;
  2383. Bitmap.Canvas.TextOut(33,
  2384. Max(24 - Abs(Canvas.Font.Height), 0), DragText);
  2385. GlobalDragImageList.Clear;
  2386. GlobalDragImageList.Width := Bitmap.Width;
  2387. GlobalDragImageList.AddMasked(Bitmap,
  2388. Bitmap.Canvas.Pixels[0, 0]);
  2389. GlobalDragImageList.SetDragImage(0, 25, 20);
  2390. except
  2391. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES',
  2392. 0, [lrTransparent], $FFFFFF) then
  2393. GlobalDragImageList.SetDragImage(0, 25, 20);
  2394. end;
  2395. finally
  2396. Bitmap.Free;
  2397. end;
  2398. end;
  2399. end;
  2400. ClientPoint := ParentForm.ScreenToClient(Point);
  2401. GlobalDragImageList.BeginDrag(ParentForm.Handle,
  2402. ClientPoint.X, ClientPoint.Y);
  2403. GlobalDragImageList.HideDragImage;
  2404. ShowCursor(True);
  2405. end;
  2406. end;
  2407. FContextMenu := False;
  2408. if IsRecycleBin then DragDropFilesEx.SourceEffects := [deMove]
  2409. else DragDropFilesEx.SourceEffects := DragSourceEffects;
  2410. DropSourceControl := Self;
  2411. try
  2412. GetSystemTimeAsFileTime(FDragStartTime);
  2413. DataObject := nil;
  2414. if Assigned(OnDDCreateDataObject) then
  2415. begin
  2416. OnDDCreateDataObject(Self, DataObject);
  2417. end;
  2418. {Execute the drag&drop-Operation:}
  2419. FLastDDResult := DragDropFilesEx.Execute(DataObject);
  2420. // The drag&drop operation is finished, so clean up the used drag image.
  2421. // This also restores the default mouse cursor
  2422. // (which is set to "none" in GlobalDragImageList.BeginDrag above)
  2423. // But it's actually too late, we would need to do it when mouse button
  2424. // is realesed already. Otherwise the cursor is hidden when hovering over
  2425. // main window, while target application is processing dropped file
  2426. // (particularly when Explorer displays progress window or
  2427. // overwrite confirmation prompt)
  2428. GlobalDragImageList.EndDrag;
  2429. GlobalDragImageList.Clear;
  2430. Application.ProcessMessages;
  2431. finally
  2432. DragDropFilesEx.FileList.Clear;
  2433. FContextMenu := False;
  2434. try
  2435. if Assigned(OnDDEnd) then
  2436. OnDDEnd(Self);
  2437. finally
  2438. DropTarget := nil;
  2439. DropSourceControl := nil;
  2440. end;
  2441. end;
  2442. end;
  2443. end;
  2444. end;
  2445. procedure TCustomDirView.Notification(AComponent: TComponent; Operation: TOperation);
  2446. begin
  2447. inherited;
  2448. if Operation = opRemove then
  2449. begin
  2450. if AComponent = PathLabel then FPathLabel := nil;
  2451. end;
  2452. end; { Notification }
  2453. procedure TCustomDirView.WMAppCommand(var Message: TMessage);
  2454. var
  2455. Command: Integer;
  2456. Shift: TShiftState;
  2457. begin
  2458. Command := HiWord(Message.lParam) and (not FAPPCOMMAND_MASK);
  2459. Shift := KeyDataToShiftState(HiWord(Message.lParam) and FAPPCOMMAND_MASK);
  2460. if Shift * [ssShift, ssAlt, ssCtrl] = [] then
  2461. begin
  2462. if Command = APPCOMMAND_BROWSER_BACKWARD then
  2463. begin
  2464. Message.Result := 1;
  2465. if BackCount >= 1 then DoHistoryGo(-1);
  2466. end
  2467. else
  2468. if Command = APPCOMMAND_BROWSER_FORWARD then
  2469. begin
  2470. Message.Result := 1;
  2471. if ForwardCount >= 1 then DoHistoryGo(1);
  2472. end
  2473. else
  2474. if Command = APPCOMMAND_BROWSER_REFRESH then
  2475. begin
  2476. Message.Result := 1;
  2477. BusyOperation(ReloadDirectory);
  2478. end
  2479. else
  2480. if Command = APPCOMMAND_BROWSER_HOME then
  2481. begin
  2482. Message.Result := 1;
  2483. BusyOperation(ExecuteHomeDirectory);
  2484. end
  2485. else inherited;
  2486. end
  2487. else inherited;
  2488. end;
  2489. procedure TCustomDirView.CMColorChanged(var Message: TMessage);
  2490. begin
  2491. inherited;
  2492. ForceColorChange(Self);
  2493. end;
  2494. function TCustomDirView.FindFileItem(FileName: string): TListItem;
  2495. type
  2496. TFileNameCompare = function(const S1, S2: string): Integer;
  2497. var
  2498. Index: Integer;
  2499. CompareFunc: TFileNameCompare;
  2500. begin
  2501. if FCaseSensitive then CompareFunc := CompareStr
  2502. else CompareFunc := CompareText;
  2503. for Index := 0 to Items.Count - 1 do
  2504. begin
  2505. if CompareFunc(FileName, ItemFileName(Items[Index])) = 0 then
  2506. begin
  2507. Result := Items[Index];
  2508. Exit;
  2509. end;
  2510. end;
  2511. Result := nil;
  2512. end;
  2513. procedure TCustomDirView.DoAnimation(Start: Boolean);
  2514. begin
  2515. if Start and LoadAnimation then
  2516. begin
  2517. if not Assigned(FAnimation) then
  2518. begin
  2519. FAnimation := TAnimate.Create(Self);
  2520. try
  2521. FAnimation.Top := (Height - FAnimation.Height) div 2;
  2522. FAnimation.Left := (Width - FAnimation.Width) div 2;
  2523. FAnimation.Parent := Self;
  2524. FAnimation.CommonAVI := aviFindFolder;
  2525. FAnimation.Transparent := True;
  2526. FAnimation.Active := True;
  2527. except
  2528. FreeAndNil(FAnimation);
  2529. end;
  2530. end;
  2531. end
  2532. else
  2533. if not Start then
  2534. begin
  2535. FreeAndNil(FAnimation);
  2536. end;
  2537. end; { DoAnimation }
  2538. function TCustomDirView.GetForwardCount: Integer;
  2539. begin
  2540. Result := FHistoryPaths.Count - BackCount;
  2541. end; { GetForwardCount }
  2542. procedure TCustomDirView.LimitHistorySize;
  2543. begin
  2544. while FHistoryPaths.Count > MaxHistoryCount do
  2545. begin
  2546. if BackCount > 0 then
  2547. begin
  2548. FHistoryPaths.Delete(0);
  2549. Dec(FBackCount);
  2550. end
  2551. else
  2552. FHistoryPaths.Delete(FHistoryPaths.Count-1);
  2553. end;
  2554. end; { LimitHistorySize }
  2555. function TCustomDirView.GetHistoryPath(Index: Integer): string;
  2556. begin
  2557. Assert(Assigned(FHistoryPaths));
  2558. if Index = 0 then Result := PathName
  2559. else
  2560. if Index < 0 then Result := FHistoryPaths[Index + BackCount]
  2561. else
  2562. if Index > 0 then Result := FHistoryPaths[Index + BackCount - 1];
  2563. end; { GetHistoryPath }
  2564. procedure TCustomDirView.SetMaxHistoryCount(Value: Integer);
  2565. begin
  2566. if FMaxHistoryCount <> Value then
  2567. begin
  2568. FMaxHistoryCount := Value;
  2569. DoHistoryChange;
  2570. end;
  2571. end; { SetMaxHistoryCount }
  2572. procedure TCustomDirView.DoHistoryChange;
  2573. begin
  2574. LimitHistorySize;
  2575. if Assigned(OnHistoryChange) then
  2576. OnHistoryChange(Self);
  2577. end; { DoHistoryChange }
  2578. procedure TCustomDirView.DoHistoryGo(Index: Integer);
  2579. var
  2580. Cancel: Boolean;
  2581. begin
  2582. if StartBusy then
  2583. try
  2584. Cancel := False;
  2585. if Assigned(OnHistoryGo) then
  2586. OnHistoryGo(Self, Index, Cancel);
  2587. if not Cancel then HistoryGo(Index);
  2588. finally
  2589. EndBusy;
  2590. end;
  2591. end;
  2592. procedure TCustomDirView.HistoryGo(Index: Integer);
  2593. var
  2594. PrevPath: string;
  2595. begin
  2596. if Index <> 0 then
  2597. begin
  2598. PrevPath := FHistoryPath;
  2599. FDontRecordPath := True;
  2600. try
  2601. Path := HistoryPath[Index];
  2602. finally
  2603. FDontRecordPath := False;
  2604. end;
  2605. FHistoryPaths.Insert(FBackCount, PrevPath);
  2606. FHistoryPaths.Delete(Index + BackCount);
  2607. Inc(FBackCount, Index);
  2608. DoHistoryChange;
  2609. end;
  2610. end; { HistoryGo }
  2611. procedure TCustomDirView.PathChanging(Relative: Boolean);
  2612. begin
  2613. if Relative then FLastPath := PathName
  2614. else FLastPath := '';
  2615. FSavedNames.Clear;
  2616. end;
  2617. procedure TCustomDirView.PathChanged;
  2618. var
  2619. Index: Integer;
  2620. begin
  2621. if Assigned(OnPathChange) then
  2622. OnPathChange(Self);
  2623. if (not FDontRecordPath) and (FHistoryPath <> '') and (FHistoryPath <> PathName) then
  2624. begin
  2625. Assert(Assigned(FHistoryPaths));
  2626. for Index := FHistoryPaths.Count - 1 downto BackCount do
  2627. FHistoryPaths.Delete(Index);
  2628. FHistoryPaths.Add(FHistoryPath);
  2629. Inc(FBackCount);
  2630. DoHistoryChange;
  2631. end;
  2632. FHistoryPath := PathName;
  2633. end; { PathChanged }
  2634. procedure TCustomDirView.ProcessChangedFiles(DirView: TCustomDirView;
  2635. FileList: TStrings; FullPath: Boolean; ExistingOnly: Boolean;
  2636. Criterias: TCompareCriterias);
  2637. var
  2638. Item, MirrorItem: TListItem;
  2639. FileTime, MirrorFileTime: TDateTime;
  2640. OldCursor: TCursor;
  2641. Index: Integer;
  2642. Changed: Boolean;
  2643. SameTime: Boolean;
  2644. Precision, MirrorPrecision: TDateTimePrecision;
  2645. begin
  2646. Assert(Valid);
  2647. OldCursor := Screen.Cursor;
  2648. if not Assigned(FileList) then
  2649. begin
  2650. Items.BeginUpdate;
  2651. BeginSelectionUpdate;
  2652. end;
  2653. try
  2654. Screen.Cursor := crHourGlass;
  2655. for Index := 0 to Items.Count-1 do
  2656. begin
  2657. Item := Items[Index];
  2658. Changed := False;
  2659. if not ItemIsDirectory(Item) then
  2660. begin
  2661. MirrorItem := DirView.FindFileItem(ItemFileName(Item));
  2662. if MirrorItem = nil then
  2663. begin
  2664. Changed := not ExistingOnly;
  2665. end
  2666. else
  2667. begin
  2668. if ccTime in Criterias then
  2669. begin
  2670. FileTime := ItemFileTime(Item, Precision);
  2671. MirrorFileTime := DirView.ItemFileTime(MirrorItem, MirrorPrecision);
  2672. if MirrorPrecision < Precision then Precision := MirrorPrecision;
  2673. if Precision <> tpMillisecond then
  2674. begin
  2675. ReduceDateTimePrecision(FileTime, Precision);
  2676. ReduceDateTimePrecision(MirrorFileTime, Precision);
  2677. end;
  2678. SameTime := (FileTime = MirrorFileTime);
  2679. if Precision = tpSecond then
  2680. begin
  2681. // 1 ms more solves the rounding issues
  2682. // (see also Common.cpp)
  2683. MirrorFileTime := MirrorFileTime + EncodeTime(0, 0, 1, 1);
  2684. end;
  2685. Changed :=
  2686. (FileTime > MirrorFileTime);
  2687. end
  2688. else
  2689. begin
  2690. SameTime := True;
  2691. end;
  2692. if (not Changed) and SameTime and (ccSize in Criterias) then
  2693. begin
  2694. Changed := ItemFileSize(Item) <> DirView.ItemFileSize(MirrorItem);
  2695. end
  2696. end;
  2697. end;
  2698. if Assigned(FileList) then
  2699. begin
  2700. if Changed then
  2701. begin
  2702. if FullPath then
  2703. begin
  2704. FileList.AddObject(ItemFullFileName(Item), Item.Data)
  2705. end
  2706. else
  2707. begin
  2708. FileList.AddObject(ItemFileName(Item), Item.Data);
  2709. end;
  2710. end;
  2711. end
  2712. else
  2713. begin
  2714. Item.Selected := Changed;
  2715. end;
  2716. end;
  2717. finally
  2718. Screen.Cursor := OldCursor;
  2719. if not Assigned(FileList) then
  2720. begin
  2721. Items.EndUpdate;
  2722. EndSelectionUpdate;
  2723. end;
  2724. end;
  2725. end;
  2726. function TCustomDirView.CreateChangedFileList(DirView: TCustomDirView;
  2727. FullPath: Boolean; ExistingOnly: Boolean; Criterias: TCompareCriterias): TStrings;
  2728. begin
  2729. Result := TStringList.Create;
  2730. try
  2731. ProcessChangedFiles(DirView, Result, FullPath, ExistingOnly, Criterias);
  2732. except
  2733. FreeAndNil(Result);
  2734. raise;
  2735. end;
  2736. end;
  2737. function TCustomDirView.CanPasteFromClipBoard: Boolean;
  2738. begin
  2739. Result := False;
  2740. if DirOK and (Path <> '') and Windows.OpenClipboard(0) then
  2741. begin
  2742. Result := IsClipboardFormatAvailable(CF_HDROP);
  2743. Windows.CloseClipBoard;
  2744. end;
  2745. end; {CanPasteFromClipBoard}
  2746. procedure TCustomDirView.CompareFiles(DirView: TCustomDirView;
  2747. ExistingOnly: Boolean; Criterias: TCompareCriterias);
  2748. begin
  2749. ProcessChangedFiles(DirView, nil, True, ExistingOnly, Criterias);
  2750. end;
  2751. procedure TCustomDirView.FocusSomething;
  2752. begin
  2753. if FSavedSelection then FPendingFocusSomething := True
  2754. else inherited;
  2755. end;
  2756. procedure TCustomDirView.SaveSelection;
  2757. var
  2758. Closest: TListItem;
  2759. begin
  2760. Assert(not FSavedSelection);
  2761. FSavedSelectionFile := '';
  2762. FSavedSelectionLastFile := '';
  2763. if Assigned(ItemFocused) then
  2764. begin
  2765. FSavedSelectionLastFile := ItemFocused.Caption;
  2766. end;
  2767. Closest := ClosestUnselected(ItemFocused);
  2768. if Assigned(Closest) then
  2769. begin
  2770. FSavedSelectionFile := Closest.Caption;
  2771. end;
  2772. FSavedSelection := True;
  2773. end;
  2774. procedure TCustomDirView.RestoreSelection;
  2775. var
  2776. ItemToSelect: TListItem;
  2777. begin
  2778. Assert(FSavedSelection);
  2779. FSavedSelection := False;
  2780. if (FSavedSelectionLastFile <> '') and
  2781. ((not Assigned(ItemFocused)) or
  2782. (ItemFocused.Caption <> FSavedSelectionLastFile)) then
  2783. begin
  2784. ItemToSelect := FindFileItem(FSavedSelectionFile);
  2785. if Assigned(ItemToSelect) then
  2786. begin
  2787. ItemFocused := ItemToSelect;
  2788. end;
  2789. end;
  2790. if not Assigned(ItemFocused) then FocusSomething
  2791. else ItemFocused.MakeVisible(False);
  2792. end;
  2793. procedure TCustomDirView.DiscardSavedSelection;
  2794. begin
  2795. Assert(FSavedSelection);
  2796. FSavedSelection := False;
  2797. if FPendingFocusSomething then
  2798. begin
  2799. FPendingFocusSomething := False;
  2800. FocusSomething;
  2801. end;
  2802. end;
  2803. procedure TCustomDirView.SaveSelectedNames;
  2804. var
  2805. Index: Integer;
  2806. Item: TListItem;
  2807. begin
  2808. FSavedNames.Clear;
  2809. FSavedNames.CaseSensitive := FCaseSensitive;
  2810. if SelCount > 0 then // optimalisation
  2811. begin
  2812. for Index := 0 to Items.Count-1 do
  2813. begin
  2814. Item := Items[Index];
  2815. if Item.Selected then
  2816. FSavedNames.Add(ItemFileName(Item));
  2817. end;
  2818. end;
  2819. // as optimalisation the list is sorted only when the selection is restored
  2820. end;
  2821. procedure TCustomDirView.RestoreSelectedNames;
  2822. var
  2823. Index, FoundIndex: Integer;
  2824. Item: TListItem;
  2825. begin
  2826. FSavedNames.Sort;
  2827. for Index := 0 to Items.Count - 1 do
  2828. begin
  2829. Item := Items[Index];
  2830. Item.Selected := FSavedNames.Find(ItemFileName(Item), FoundIndex);
  2831. end;
  2832. end;
  2833. function TCustomDirView.GetSelectedNamesSaved: Boolean;
  2834. begin
  2835. Result := (FSavedNames.Count > 0);
  2836. end;
  2837. procedure TCustomDirView.ContinueSession(Continue: Boolean);
  2838. begin
  2839. if Continue then FLastPath := PathName
  2840. else FLastPath := '';
  2841. end;
  2842. function TCustomDirView.SaveState: TObject;
  2843. var
  2844. State: TDirViewState;
  2845. DirColProperties: TCustomDirViewColProperties;
  2846. begin
  2847. State := TDirViewState.Create;
  2848. State.HistoryPaths := TStringList.Create;
  2849. State.HistoryPaths.Assign(FHistoryPaths);
  2850. State.BackCount := FBackCount;
  2851. // TCustomDirViewColProperties should not be here
  2852. DirColProperties := ColProperties as TCustomDirViewColProperties;
  2853. Assert(Assigned(DirColProperties));
  2854. State.SortStr := DirColProperties.SortStr;
  2855. State.Mask := Mask;
  2856. if Assigned(ItemFocused) then State.FocusedItem := ItemFocused.Caption
  2857. else State.FocusedItem := '';
  2858. Result := State;
  2859. end;
  2860. procedure TCustomDirView.RestoreState(AState: TObject);
  2861. var
  2862. State: TDirViewState;
  2863. DirColProperties: TCustomDirViewColProperties;
  2864. ListItem: TListItem;
  2865. begin
  2866. Assert(AState is TDirViewState);
  2867. State := AState as TDirViewState;
  2868. Assert(Assigned(State));
  2869. FHistoryPaths.Assign(State.HistoryPaths);
  2870. FBackCount := State.BackCount;
  2871. DoHistoryChange;
  2872. // TCustomDirViewColProperties should not be here
  2873. DirColProperties := ColProperties as TCustomDirViewColProperties;
  2874. Assert(Assigned(DirColProperties));
  2875. DirColProperties.SortStr := State.SortStr;
  2876. Mask := State.Mask;
  2877. if State.FocusedItem <> '' then
  2878. begin
  2879. ListItem := FindFileItem(State.FocusedItem);
  2880. if Assigned(ListItem) then
  2881. begin
  2882. ItemFocused := ListItem;
  2883. ListItem.MakeVisible(False);
  2884. end;
  2885. end;
  2886. end;
  2887. procedure TCustomDirView.ClearState;
  2888. begin
  2889. FHistoryPaths.Clear;
  2890. FBackCount := 0;
  2891. DoHistoryChange;
  2892. end;
  2893. procedure TCustomDirView.SetMask(Value: string);
  2894. begin
  2895. if Mask <> Value then
  2896. begin
  2897. FMask := Value;
  2898. UpdatePathLabel;
  2899. if DirOK then Reload(False);
  2900. end;
  2901. end;{SetMask}
  2902. // WM_SETFOCUS works even when focus is moved to another window/app,
  2903. // while .Enter works only when focus is moved to order control of the same window.
  2904. procedure TCustomDirView.WMSetFocus(var Message: TWMSetFocus);
  2905. begin
  2906. inherited;
  2907. EnsureSelectionRedrawn;
  2908. UpdatePathLabel;
  2909. end;
  2910. procedure TCustomDirView.WMKillFocus(var Message: TWMKillFocus);
  2911. begin
  2912. inherited;
  2913. EnsureSelectionRedrawn;
  2914. UpdatePathLabel;
  2915. end;
  2916. procedure TCustomDirView.EnsureSelectionRedrawn;
  2917. begin
  2918. // WORKAROUND
  2919. // when receiving/losing focus, selection is not redrawn in report view
  2920. // (except for focus item selection),
  2921. // probably when double buffering is enabled (LVS_EX_DOUBLEBUFFER).
  2922. // But even without LVS_EX_DOUBLEBUFFER, selection behind file icon is not updated.
  2923. if ViewStyle = vsReport then
  2924. begin
  2925. if (SelCount >= 2) or ((SelCount >= 1) and ((not Assigned(ItemFocused)) or (not ItemFocused.Selected))) then
  2926. begin
  2927. Invalidate;
  2928. end
  2929. else
  2930. if Assigned(ItemFocused) and ItemFocused.Selected then
  2931. begin
  2932. // Optimization. When no item is selected, redraw just the focused item.
  2933. ItemFocused.Update;
  2934. end;
  2935. end;
  2936. end;
  2937. function TCustomDirView.DoBusy(Busy: Integer): Boolean;
  2938. begin
  2939. Result := True;
  2940. if Assigned(OnBusy) then
  2941. begin
  2942. OnBusy(Self, Busy, Result);
  2943. end;
  2944. end;
  2945. function TCustomDirView.StartBusy: Boolean;
  2946. begin
  2947. Result := DoBusy(1);
  2948. end;
  2949. function TCustomDirView.IsBusy: Boolean;
  2950. begin
  2951. Result := DoBusy(0);
  2952. end;
  2953. procedure TCustomDirView.EndBusy;
  2954. begin
  2955. DoBusy(-1);
  2956. end;
  2957. procedure TCustomDirView.BusyOperation(Operation: TBusyOperation);
  2958. begin
  2959. if StartBusy then
  2960. try
  2961. Operation;
  2962. finally
  2963. EndBusy;
  2964. end;
  2965. end;
  2966. initialization
  2967. DropSourceControl := nil;
  2968. SetLength(WinDir, MAX_PATH);
  2969. SetLength(WinDir, GetWindowsDirectory(PChar(WinDir), MAX_PATH));
  2970. SetLength(TempDir, MAX_PATH);
  2971. SetLength(TempDir, GetTempPath(MAX_PATH, PChar(TempDir)));
  2972. SpecialFolderLocation(CSIDL_PERSONAL, UserDocumentDirectory);
  2973. WinDir := IncludeTrailingPathDelimiter(WinDir);
  2974. TempDir := IncludeTrailingPathDelimiter(TempDir);
  2975. finalization
  2976. SetLength(StdDirTypeName, 0);
  2977. SetLength(WinDir, 0);
  2978. SetLength(TempDir, 0);
  2979. end.