CustomDirView.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  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. property ImageList16: TImageList read FImageList16;
  311. property ImageList32: TImageList read FImageList32;
  312. public
  313. constructor Create(AOwner: TComponent); override;
  314. destructor Destroy; override;
  315. procedure Load; virtual;
  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. inherited;
  1081. end;
  1082. function TCustomDirView.CustomDrawItem(Item: TListItem; State: TCustomDrawState;
  1083. Stage: TCustomDrawStage): Boolean;
  1084. var
  1085. FItemColor: TColor;
  1086. begin
  1087. if (Item <> nil) and (Stage = cdPrePaint) then
  1088. begin
  1089. FItemColor := ItemColor(Item);
  1090. if (FItemColor <> clDefaultItemColor) and
  1091. (Canvas.Font.Color <> FItemColor) then
  1092. Canvas.Font.Color := FItemColor;
  1093. end;
  1094. Result := inherited CustomDrawItem(Item, State, Stage);
  1095. end;
  1096. function TCustomDirView.CustomDrawSubItem(Item: TListItem; SubItem: Integer;
  1097. State: TCustomDrawState; Stage: TCustomDrawStage): Boolean;
  1098. var
  1099. FItemColor: TColor;
  1100. begin
  1101. if Stage = cdPrePaint then
  1102. begin
  1103. FItemColor := ItemColor(Item);
  1104. if (FItemColor <> clDefaultItemColor) and
  1105. (Canvas.Font.Color <> FItemColor) then
  1106. Canvas.Font.Color := FItemColor;
  1107. end;
  1108. Result := inherited CustomDrawSubItem(Item, SubItem, State, Stage);
  1109. end;
  1110. procedure TCustomDirView.Delete(Item: TListItem);
  1111. begin
  1112. if Assigned(Item) then
  1113. begin
  1114. // This causes access violation when size is stored in structure
  1115. // pointed by TListItem->Data and this structure is not valid any more
  1116. if Valid then Dec(FFilesSize, ItemFileSize(Item));
  1117. inherited Delete(Item);
  1118. end;
  1119. end;
  1120. destructor TCustomDirView.Destroy;
  1121. begin
  1122. Assert(not FSavedSelection);
  1123. FreeAndNil(FScrollOnDragOver);
  1124. FreeAndNil(FSavedNames);
  1125. FreeAndNil(FHistoryPaths);
  1126. FreeAndNil(FDragDropFilesEx);
  1127. FreeAndNil(FImageList16);
  1128. FreeAndNil(FImageList32);
  1129. if Assigned(SmallImages) then
  1130. begin
  1131. SmallImages.Free;
  1132. SmallImages := nil;
  1133. end;
  1134. if Assigned(LargeImages) then
  1135. begin
  1136. LargeImages.Free;
  1137. LargeImages := nil;
  1138. end;
  1139. FreeAndNil(FAnimation);
  1140. inherited;
  1141. end;
  1142. procedure TCustomDirView.SelectFiles(Filter: TFileFilter; Select: Boolean);
  1143. var
  1144. Item: TListItem;
  1145. Index: Integer;
  1146. OldCursor: TCursor;
  1147. begin
  1148. Assert(Valid);
  1149. OldCursor := Screen.Cursor;
  1150. Items.BeginUpdate;
  1151. BeginSelectionUpdate;
  1152. try
  1153. Screen.Cursor := crHourGlass;
  1154. for Index := 0 to Items.Count-1 do
  1155. begin
  1156. Item := Items[Index];
  1157. Assert(Assigned(Item));
  1158. if (Item.Selected <> Select) and
  1159. ItemMatchesFilter(Item, Filter) then
  1160. Item.Selected := Select;
  1161. end;
  1162. finally
  1163. Screen.Cursor := OldCursor;
  1164. Items.EndUpdate;
  1165. EndSelectionUpdate;
  1166. end;
  1167. end;
  1168. function TCustomDirView.DoSelectByMask(Select: Boolean): Boolean;
  1169. var
  1170. Filter: TFileFilter;
  1171. begin
  1172. Result := inherited DoSelectByMask(Select);
  1173. if Assigned(FOnGetSelectFilter) then
  1174. begin
  1175. DefaultFileFilter(Filter);
  1176. FOnGetSelectFilter(Self, Select, Filter);
  1177. SelectFiles(Filter, Select);
  1178. Result := True;
  1179. end;
  1180. end;
  1181. function TCustomDirView.DragCompleteFileList: Boolean;
  1182. begin
  1183. Result := (MarkedCount <= 100) and (not IsRecycleBin);
  1184. end;
  1185. procedure TCustomDirView.DumbCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  1186. begin
  1187. end;
  1188. procedure TCustomDirView.DumbCustomDrawSubItem(Sender: TCustomListView;
  1189. Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  1190. var DefaultDraw: Boolean);
  1191. begin
  1192. end;
  1193. function TCustomDirView.GetTargetPopupMenu: Boolean;
  1194. begin
  1195. if Assigned(FDragDropFilesEx) then Result := FDragDropFilesEx.TargetPopupMenu
  1196. else Result := True;
  1197. end;
  1198. procedure TCustomDirView.SetMultiSelect(Value: Boolean);
  1199. begin
  1200. if Value <> MultiSelect then
  1201. begin
  1202. inherited SetMultiSelect(Value);
  1203. if not (csLoading in ComponentState) and Assigned(ColProperties) then
  1204. begin
  1205. ColProperties.RecreateColumns;
  1206. SetColumnImages;
  1207. if DirOK then Reload(True);
  1208. end;
  1209. end;
  1210. end;
  1211. function TCustomDirView.GetValid: Boolean;
  1212. begin
  1213. Result := (not (csDestroying in ComponentState)) and
  1214. (not Loading) and (not FClearingItems);
  1215. end;
  1216. function TCustomDirView.ItemCanDrag(Item: TListItem): Boolean;
  1217. begin
  1218. Result := (not ItemIsParentDirectory(Item));
  1219. end;
  1220. function TCustomDirView.ItemColor(Item: TListItem): TColor;
  1221. begin
  1222. Result := clDefaultItemColor;
  1223. end;
  1224. function TCustomDirView.GetFilesMarkedSize: Int64;
  1225. begin
  1226. if SelCount > 0 then Result := FilesSelSize
  1227. else
  1228. if Assigned(ItemFocused) then Result := ItemFileSize(ItemFocused)
  1229. else Result := 0;
  1230. end;
  1231. procedure TCustomDirView.IconsSetImageList;
  1232. begin
  1233. if not Assigned(SmallImages) then
  1234. SmallImages := ShellImageList(Self, SHGFI_SMALLICON);
  1235. if not Assigned(LargeImages) then
  1236. LargeImages := ShellImageList(Self, SHGFI_LARGEICON);
  1237. end; {IconsSetImageList}
  1238. function TCustomDirView.ItemIsRecycleBin(Item: TListItem): Boolean;
  1239. begin
  1240. Result := False;
  1241. end;
  1242. function TCustomDirView.ItemOverlayIndexes(Item: TListItem): Word;
  1243. begin
  1244. Result := oiNoOverlay;
  1245. if Assigned(OnGetOverlay) then
  1246. OnGetOverlay(Self, Item, Result);
  1247. end;
  1248. procedure TCustomDirView.WMKeyDown(var Message: TWMKeyDown);
  1249. begin
  1250. if DoubleBuffered and (Message.CharCode in [VK_PRIOR, VK_NEXT]) and
  1251. FDoubleBufferedScrollingWorkaround then
  1252. begin
  1253. // WORKAROUND
  1254. // When scrolling with double-buffering enabled, ugly artefacts
  1255. // are shown temporarily.
  1256. // LVS_EX_TRANSPARENTBKGND fixes it on Vista and newer
  1257. SendMessage(Handle, WM_SETREDRAW, 0, 0);
  1258. try
  1259. inherited;
  1260. finally
  1261. SendMessage(Handle, WM_SETREDRAW, 1, 0);
  1262. end;
  1263. Repaint;
  1264. end
  1265. else
  1266. begin
  1267. inherited;
  1268. end;
  1269. end;
  1270. procedure TCustomDirView.DoDisplayPropertiesMenu;
  1271. begin
  1272. if not IsBusy then
  1273. DisplayPropertiesMenu;
  1274. end;
  1275. procedure TCustomDirView.DoExecute(Item: TListItem);
  1276. begin
  1277. BusyOperation(procedure begin Execute(Item); end);
  1278. end;
  1279. procedure TCustomDirView.DoExecuteParentDirectory;
  1280. begin
  1281. BusyOperation(ExecuteParentDirectory);
  1282. end;
  1283. procedure TCustomDirView.KeyDown(var Key: Word; Shift: TShiftState);
  1284. begin
  1285. if Valid and (not IsEditing) and (not Loading) then
  1286. begin
  1287. if (Key = VK_RETURN) or
  1288. ((Key = VK_NEXT) and (ssCtrl in Shift)) then
  1289. begin
  1290. if Assigned(ItemFocused) then
  1291. begin
  1292. Key := 0;
  1293. if (Key = VK_RETURN) and (Shift = [ssAlt]) then DoDisplayPropertiesMenu
  1294. else
  1295. if (Key <> VK_RETURN) or (Shift = []) then DoExecute(ItemFocused);
  1296. end;
  1297. end
  1298. else
  1299. if ((Key = VK_BACK) or ((Key = VK_PRIOR) and (ssCtrl in Shift))) and
  1300. (not IsRoot) then
  1301. begin
  1302. Key := 0;
  1303. DoExecuteParentDirectory;
  1304. end
  1305. else
  1306. if ((Key = VK_UP) and (ssAlt in Shift)) and
  1307. (not IsRoot) then
  1308. begin
  1309. Key := 0;
  1310. // U+25D8 is 'INVERSE BULLET', what is glyph representing '\x8' (or '\b')
  1311. // ('up' key is the '8' key on numeric pad)
  1312. // We could obtain the value programatically using
  1313. // MultiByteToWideChar(CP_OEMCP, MB_USEGLYPHCHARS, "\x8", 1, ...)
  1314. FNextCharToIgnore := $25D8;
  1315. DoExecuteParentDirectory;
  1316. end
  1317. else
  1318. if (Key = 220 { backslash }) and (ssCtrl in Shift) and (not IsRoot) then
  1319. begin
  1320. Key := 0;
  1321. BusyOperation(ExecuteRootDirectory);
  1322. end
  1323. else
  1324. if (Key = VK_LEFT) and (ssAlt in Shift) then
  1325. begin
  1326. if BackCount >= 1 then DoHistoryGo(-1);
  1327. end
  1328. else
  1329. if (Key = VK_RIGHT) and (ssAlt in Shift) then
  1330. begin
  1331. if ForwardCount >= 1 then DoHistoryGo(1);
  1332. end
  1333. else
  1334. begin
  1335. inherited;
  1336. end;
  1337. end
  1338. else
  1339. begin
  1340. inherited;
  1341. end;
  1342. end;
  1343. procedure TCustomDirView.KeyPress(var Key: Char);
  1344. begin
  1345. if IsEditing and (Pos(Key, FInvalidNameChars) <> 0) Then
  1346. begin
  1347. Beep;
  1348. Key := #0;
  1349. end;
  1350. inherited;
  1351. end;
  1352. procedure TCustomDirView.DisplayContextMenuInSitu;
  1353. var
  1354. R: TRect;
  1355. P: TPoint;
  1356. begin
  1357. if Assigned(ItemFocused) then
  1358. begin
  1359. R := ItemFocused.DisplayRect(drIcon);
  1360. P.X := (R.Left + R.Right) div 2;
  1361. P.Y := (R.Top + R.Bottom) div 2;
  1362. end
  1363. else
  1364. begin
  1365. P.X := 0;
  1366. P.Y := 0;
  1367. end;
  1368. P := ClientToScreen(P);
  1369. DisplayContextMenu(P);
  1370. end;
  1371. procedure TCustomDirView.KeyUp(var Key: Word; Shift: TShiftState);
  1372. var
  1373. P: TPoint;
  1374. begin
  1375. if Key = VK_APPS then
  1376. begin
  1377. if (not Loading) and (not IsBusy) then
  1378. begin
  1379. if MarkedCount > 0 then
  1380. begin
  1381. DisplayContextMenuInSitu;
  1382. end
  1383. else
  1384. if Assigned(PopupMenu) then
  1385. begin
  1386. P.X := 0;
  1387. P.Y := 0;
  1388. P := ClientToScreen(P);
  1389. PopupMenu.Popup(P.X, P.Y);
  1390. end;
  1391. end;
  1392. end
  1393. else
  1394. inherited KeyUp(Key, Shift);
  1395. end;
  1396. procedure TCustomDirView.SetWatchForChanges(Value: Boolean);
  1397. begin
  1398. if FWatchForChanges <> Value then
  1399. FWatchForChanges := Value;
  1400. end;
  1401. function TCustomDirView.TargetHasDropHandler(Item: TListItem; Effect: Integer): Boolean;
  1402. begin
  1403. Assert(Assigned(DragDropFilesEx) and Assigned(Item));
  1404. Result :=
  1405. DragDropFilesEx.TargetHasDropHandler(nil, ItemFullFileName(Item), Effect);
  1406. if Assigned(OnDDTargetHasDropHandler) then
  1407. begin
  1408. OnDDTargetHasDropHandler(Self, Item, Effect, Result);
  1409. end;
  1410. end;
  1411. procedure TCustomDirView.UpdatePathLabelCaption;
  1412. begin
  1413. PathLabel.Caption := PathName;
  1414. PathLabel.Mask := Mask;
  1415. end;
  1416. procedure TCustomDirView.UpdatePathLabel;
  1417. begin
  1418. if Assigned(PathLabel) then
  1419. begin
  1420. if csDesigning in ComponentState then
  1421. begin
  1422. PathLabel.Caption := PathLabel.Name;
  1423. PathLabel.Mask := '';
  1424. end
  1425. else
  1426. begin
  1427. UpdatePathLabelCaption;
  1428. end;
  1429. PathLabel.UpdateStatus;
  1430. end;
  1431. end; { UpdatePathLabel }
  1432. procedure TCustomDirView.UpdateStatusBar;
  1433. var
  1434. StatusFileInfo: TStatusFileInfo;
  1435. begin
  1436. if (FUpdatingSelection = 0) and Assigned(OnUpdateStatusBar) then
  1437. begin
  1438. with StatusFileInfo do
  1439. begin
  1440. SelectedSize := FilesSelSize;
  1441. FilesSize := Self.FilesSize;
  1442. SelectedCount := SelCount;
  1443. FilesCount := Self.FilesCount;
  1444. HiddenCount := Self.HiddenCount;
  1445. FilteredCount := Self.FilteredCount;
  1446. end;
  1447. if not CompareMem(@StatusFileInfo, @FStatusFileInfo, SizeOf(StatusFileInfo)) then
  1448. begin
  1449. FStatusFileInfo := StatusFileInfo;
  1450. OnUpdateStatusBar(Self, FStatusFileInfo);
  1451. end;
  1452. end;
  1453. end; { UpdateStatusBar }
  1454. procedure TCustomDirView.WMContextMenu(var Message: TWMContextMenu);
  1455. var
  1456. Point: TPoint;
  1457. begin
  1458. FDragEnabled := False;
  1459. if Assigned(PopupMenu) then
  1460. PopupMenu.AutoPopup := False;
  1461. //inherited;
  1462. if FContextMenu and (not Loading) then
  1463. begin
  1464. Point.X := Message.XPos;
  1465. Point.Y := Message.YPos;
  1466. Point := ScreenToClient(Point);
  1467. if Assigned(OnMouseDown) then
  1468. begin
  1469. OnMouseDown(Self, mbRight, [], Point.X, Point.Y);
  1470. end;
  1471. if FUseSystemContextMenu and Assigned(ItemFocused) and
  1472. (GetItemAt(Point.X, Point.Y) = ItemFocused) then
  1473. begin
  1474. Point.X := Message.XPos;
  1475. Point.Y := Message.YPos;
  1476. DisplayContextMenu(Point);
  1477. end
  1478. else
  1479. if Assigned(PopupMenu) and (not PopupMenu.AutoPopup) then
  1480. begin
  1481. PopupMenu.Popup(Message.XPos, Message.YPos);
  1482. end;
  1483. end;
  1484. FContextMenu := False;
  1485. //inherited;
  1486. end;
  1487. function TCustomDirView.EnableDragOnClick: Boolean;
  1488. begin
  1489. Result := (not Loading) and inherited EnableDragOnClick;
  1490. end;
  1491. procedure TCustomDirView.WMLButtonDown(var Message: TWMLButtonDown);
  1492. begin
  1493. GetCursorPos(FStartPos);
  1494. FDragEnabled := EnableDragOnClick;
  1495. inherited;
  1496. end;
  1497. procedure TCustomDirView.WMRButtonDown(var Message: TWMRButtonDown);
  1498. begin
  1499. GetCursorPos(FStartPos);
  1500. if FDragDropFilesEx.DragDetectStatus <> ddsDrag then
  1501. FDragEnabled := EnableDragOnClick;
  1502. FContextMenu := True;
  1503. inherited;
  1504. end;
  1505. procedure TCustomDirView.WMLButtonDblClk(var Message: TWMLButtonDblClk);
  1506. begin
  1507. inherited;
  1508. if Assigned(ItemFocused) and (not Loading) and
  1509. (GetItemAt(Message.XPos, Message.YPos) = ItemFocused) then
  1510. begin
  1511. if GetKeyState(VK_MENU) < 0 then DoDisplayPropertiesMenu
  1512. else DoExecute(ItemFocused);
  1513. end;
  1514. end;
  1515. procedure TCustomDirView.WMLButtonUp(var Message: TWMLButtonUp);
  1516. begin
  1517. FDragEnabled := False;
  1518. inherited;
  1519. end;
  1520. procedure TCustomDirView.WMXButtonUp(var Message: TWMXMouse);
  1521. begin
  1522. if Message.Button = _XBUTTON1 then
  1523. begin
  1524. if BackCount >= 1 then DoHistoryGo(-1);
  1525. Message.Result := 1;
  1526. end
  1527. else
  1528. if Message.Button = _XBUTTON2 then
  1529. begin
  1530. if ForwardCount >= 1 then DoHistoryGo(1);
  1531. Message.Result := 1;
  1532. end;
  1533. end;
  1534. procedure TCustomDirView.CancelEdit;
  1535. begin
  1536. // - Do nothing when handle is not allocated (we cannot be editing anyway
  1537. // without a handle), otherwise this causes handle allocation,
  1538. // what is wrong particularly when we are called from ClearItems
  1539. // when we are being destroyed
  1540. // - If editing, it has to be focused item
  1541. if HandleAllocated and IsEditing and Assigned(ItemFocused) then
  1542. begin
  1543. ItemFocused.CancelEdit;
  1544. FLoadEnabled := True;
  1545. end;
  1546. end;
  1547. procedure TCustomDirView.Reload(CacheIcons: Boolean);
  1548. var
  1549. OldSelection: TStringList;
  1550. OldItemFocused: string;
  1551. OldFocusedShown: Boolean;
  1552. OldShownItemOffset: Integer;
  1553. Index: Integer;
  1554. FoundIndex: Integer;
  1555. IconCache: TStringList;
  1556. Item: TListItem;
  1557. ItemToFocus: TListItem;
  1558. FileName: string;
  1559. R: TRect;
  1560. P: TPoint;
  1561. begin
  1562. if Path <> '' then
  1563. begin
  1564. CancelEdit;
  1565. OldSelection := nil;
  1566. IconCache := nil;
  1567. Items.BeginUpdate;
  1568. try
  1569. OldSelection := TStringList.Create;
  1570. OldSelection.CaseSensitive := FCaseSensitive;
  1571. if CacheIcons then
  1572. IconCache := TStringList.Create;
  1573. for Index := 0 to Items.Count-1 do
  1574. begin
  1575. Item := Items[Index];
  1576. // cannot use ItemFileName as for TUnixDirView the file object
  1577. // is no longer valid
  1578. FileName := Item.Caption;
  1579. if Item.Selected then
  1580. OldSelection.Add(FileName);
  1581. if CacheIcons and (ItemImageIndex(Item, True) >= 0) then
  1582. IconCache.AddObject(FileName, TObject(ItemImageIndex(Item, True)));
  1583. end;
  1584. if FSelectFile <> '' then
  1585. begin
  1586. OldItemFocused := FSelectFile;
  1587. OldFocusedShown := False;
  1588. OldShownItemOffset := -1;
  1589. FSelectFile := '';
  1590. end
  1591. else
  1592. begin
  1593. if Assigned(ItemFocused) then
  1594. begin
  1595. if ViewStyle = vsReport then
  1596. begin
  1597. if Assigned(TopItem) then
  1598. begin
  1599. R := ItemFocused.DisplayRect(drBounds);
  1600. if (R.Top < TopItem.DisplayRect(drBounds).Top) or (R.Top > ClientHeight) then
  1601. begin
  1602. OldFocusedShown := False;
  1603. OldShownItemOffset := TopItem.Index;
  1604. end
  1605. else
  1606. begin
  1607. OldFocusedShown := True;
  1608. OldShownItemOffset := ItemFocused.Index - TopItem.Index;
  1609. end;
  1610. end
  1611. else
  1612. begin
  1613. // seen with one user only
  1614. OldFocusedShown := False;
  1615. OldShownItemOffset := 0;
  1616. end;
  1617. end
  1618. else
  1619. begin
  1620. // to satisfy compiler, never used
  1621. OldFocusedShown := False;
  1622. OldShownItemOffset := -1;
  1623. end;
  1624. OldItemFocused := ItemFocused.Caption;
  1625. end
  1626. else
  1627. begin
  1628. OldItemFocused := '';
  1629. OldFocusedShown := False;
  1630. if Assigned(TopItem) then OldShownItemOffset := TopItem.Index
  1631. else OldShownItemOffset := -1;
  1632. end;
  1633. end;
  1634. Load;
  1635. OldSelection.Sort;
  1636. if CacheIcons then IconCache.Sort;
  1637. ItemToFocus := nil;
  1638. for Index := 0 to Items.Count - 1 do
  1639. begin
  1640. Item := Items[Index];
  1641. FileName := ItemFileName(Item);
  1642. if FileName = OldItemFocused then
  1643. ItemToFocus := Item;
  1644. if OldSelection.Find(FileName, FoundIndex) then
  1645. Item.Selected := True;
  1646. if CacheIcons and (ItemImageIndex(Item, True) < 0) then
  1647. begin
  1648. FoundIndex := IconCache.IndexOf(FileName);
  1649. if FoundIndex >= 0 then
  1650. SetItemImageIndex(Item, Integer(IconCache.Objects[FoundIndex]));
  1651. end;
  1652. end;
  1653. finally
  1654. Items.EndUpdate;
  1655. OldSelection.Free;
  1656. if CacheIcons then IconCache.Free;
  1657. end;
  1658. // This is below Items.EndUpdate(), to make Scroll() work properly
  1659. if Assigned(ItemToFocus) then
  1660. begin
  1661. // we have found item that was previously focused and visible, scroll to it
  1662. if (ViewStyle = vsReport) and OldFocusedShown and
  1663. (ItemToFocus.Index > OldShownItemOffset) then
  1664. begin
  1665. P := Items[ItemToFocus.Index - OldShownItemOffset].GetPosition;
  1666. // GetPosition is shifted bit low below actual row top.
  1667. // Scroll to the GetPosition would scroll one line lower.
  1668. Scroll(0, P.Y - Items[0].GetPosition.Y);
  1669. end;
  1670. FocusItem(ItemToFocus);
  1671. end;
  1672. // cannot scroll when focus is not visible because
  1673. // of hack-implementation of FocusItem()
  1674. {$IF False}
  1675. // previously focus item was not visible, scroll to the same position
  1676. // as before
  1677. if (ViewStyle = vsReport) and (not OldFocusedShown) and
  1678. (OldShownItemOffset >= 0) and (Items.Count > 0) then
  1679. begin
  1680. if OldShownItemOffset < Items.Count - VisibleRowCount then
  1681. Scroll(0, OldShownItemOffset)
  1682. else
  1683. Items.Item[Items.Count - 1].MakeVisible(false);
  1684. end
  1685. // do not know where to scroll to, so scroll to focus
  1686. // (or we have tried to scroll to previously focused and visible item,
  1687. // now make sute that it is really visible)
  1688. else {$IFEND}
  1689. if Assigned(ItemToFocus) then ItemToFocus.MakeVisible(false);
  1690. FocusSomething;
  1691. end;
  1692. end;
  1693. procedure TCustomDirView.Load;
  1694. var
  1695. SaveCursor: TCursor;
  1696. Delimiters: string;
  1697. LastDirName: string;
  1698. begin
  1699. if not FLoadEnabled or Loading then
  1700. begin
  1701. FDirty := True;
  1702. FAbortLoading := True;
  1703. end
  1704. else
  1705. begin
  1706. FLoading := True;
  1707. try
  1708. FHasParentDir := False;
  1709. if Assigned(FOnStartLoading) then FOnStartLoading(Self);
  1710. SaveCursor := Screen.Cursor;
  1711. Screen.Cursor := crHourGlass;
  1712. try
  1713. FNotifyEnabled := False;
  1714. ClearItems;
  1715. FFilesSize := 0;
  1716. FFilesSelSize := 0;
  1717. SortType := stNone;
  1718. Items.BeginUpdate;
  1719. try
  1720. try
  1721. DoAnimation(True);
  1722. LoadFiles;
  1723. finally
  1724. DoAnimation(False);
  1725. end;
  1726. finally
  1727. Items.EndUpdate;
  1728. end;
  1729. finally
  1730. Screen.Cursor := SaveCursor;
  1731. end;
  1732. finally
  1733. FLoading := False;
  1734. try
  1735. if FAbortLoading then
  1736. begin
  1737. FAbortLoading := False;
  1738. Reload(False);
  1739. end
  1740. else
  1741. begin
  1742. if DirOK then SortItems;
  1743. FAbortLoading := False;
  1744. FDirty := False;
  1745. if (Length(LastPath) > Length(PathName)) and
  1746. (Copy(LastPath, 1, Length(PathName)) = PathName) and
  1747. (Items.Count > 0) then
  1748. begin
  1749. LastDirName := Copy(LastPath, Length(PathName) + 1, MaxInt);
  1750. Delimiters := '\:/';
  1751. if IsDelimiter(Delimiters, LastDirName, 1) then
  1752. begin
  1753. LastDirName := Copy(LastDirName, 2, MaxInt);
  1754. end;
  1755. if LastDelimiter('\:/', LastDirName) = 0 then
  1756. begin
  1757. ItemFocused := FindFileItem(LastDirName);
  1758. end;
  1759. end;
  1760. end;
  1761. finally
  1762. // nested try .. finally block is included
  1763. // because we really want these to be executed
  1764. FNotifyEnabled := True;
  1765. FocusSomething;
  1766. if Assigned(FOnLoaded) then
  1767. begin
  1768. FOnLoaded(Self);
  1769. end;
  1770. UpdatePathLabel;
  1771. UpdateStatusBar;
  1772. end;
  1773. end;
  1774. end;
  1775. end;
  1776. procedure TCustomDirView.SetLoadEnabled(Enabled: Boolean);
  1777. begin
  1778. if Enabled <> LoadEnabled then
  1779. begin
  1780. FLoadEnabled := Enabled;
  1781. if Enabled and Dirty then Reload(True);
  1782. end;
  1783. end;
  1784. function TCustomDirView.GetFilesCount: Integer;
  1785. begin
  1786. Result := Items.Count;
  1787. if (Result > 0) and HasParentDir then Dec(Result);
  1788. end;
  1789. procedure TCustomDirView.SetViewStyle(Value: TViewStyle);
  1790. begin
  1791. if (Value <> ViewStyle) and (not FLoading) then
  1792. begin
  1793. FNotifyEnabled := False;
  1794. inherited;
  1795. FNotifyEnabled := True;
  1796. // this is workaround for bug in TCustomNortonLikeListView
  1797. // that clears Items on recreating wnd (caused by change to ViewStyle)
  1798. Reload(True);
  1799. end;
  1800. end;
  1801. procedure TCustomDirView.ColClick(Column: TListColumn);
  1802. var
  1803. ScrollToFocused: Boolean;
  1804. begin
  1805. ScrollToFocused := Assigned(ItemFocused);
  1806. inherited;
  1807. if ScrollToFocused and Assigned(ItemFocused) then
  1808. ItemFocused.MakeVisible(False);
  1809. end;
  1810. procedure TCustomDirView.CustomSortItems(SortProc: Pointer);
  1811. var
  1812. SavedCursor: TCursor;
  1813. SavedNotifyEnabled: Boolean;
  1814. begin
  1815. if HandleAllocated then
  1816. begin
  1817. SavedNotifyEnabled := FNotifyEnabled;
  1818. SavedCursor := Screen.Cursor;
  1819. Items.BeginUpdate;
  1820. try
  1821. Screen.Cursor := crHourglass;
  1822. FNotifyEnabled := False;
  1823. CustomSort(TLVCompare(SortProc), Integer(Pointer(Self)));
  1824. finally
  1825. Screen.Cursor := SavedCursor;
  1826. FNotifyEnabled := SavedNotifyEnabled;
  1827. Items.EndUpdate;
  1828. ItemsReordered;
  1829. end;
  1830. end;
  1831. end;
  1832. procedure TCustomDirView.ReloadForce(CacheIcons: Boolean);
  1833. begin
  1834. FLoadEnabled := True;
  1835. FDirty := False;
  1836. Reload(CacheIcons);
  1837. end;
  1838. procedure TCustomDirView.ScrollOnDragOverBeforeUpdate(ObjectToValidate: TObject);
  1839. begin
  1840. GlobalDragImageList.HideDragImage;
  1841. end;
  1842. procedure TCustomDirView.ScrollOnDragOverAfterUpdate;
  1843. begin
  1844. GlobalDragImageList.ShowDragImage;
  1845. end;
  1846. procedure TCustomDirView.DDDragEnter(DataObj: IDataObject; grfKeyState: Longint;
  1847. Point: TPoint; var dwEffect: longint; var Accept: Boolean);
  1848. var
  1849. Index: Integer;
  1850. begin
  1851. Accept := Accept and DirOK and (not Loading);
  1852. if Accept and
  1853. (DragDropFilesEx.FileList.Count > 0) and
  1854. (Length(TFDDListItem(DragDropFilesEx.FileList[0]^).Name) > 0) and
  1855. (not IsRecycleBin or not DragDropFilesEx.FileNamesAreMapped) then
  1856. begin
  1857. FDragDrive := Upcase(TFDDListItem(DragDropFilesEx.FileList[0]^).Name[1]);
  1858. FExeDrag := FDDLinkOnExeDrag and
  1859. (deLink in DragDropFilesEx.TargetEffects) and
  1860. ((DragDropFilesEx.AvailableDropEffects and DropEffect_Link) <> 0);
  1861. if FExeDrag then
  1862. begin
  1863. for Index := 0 to DragDropFilesEx.FileList.Count - 1 do
  1864. if not IsExecutable(TFDDListItem(DragDropFilesEx.FileList[Index]^).Name) then
  1865. begin
  1866. FExeDrag := False;
  1867. Break;
  1868. end;
  1869. end;
  1870. end
  1871. else
  1872. begin
  1873. FDragDrive := #0;
  1874. end;
  1875. FScrollOnDragOver.StartDrag;
  1876. if Assigned(FOnDDDragEnter) then
  1877. FOnDDDragEnter(Self, DataObj, grfKeyState, Point, dwEffect, Accept);
  1878. end;
  1879. procedure TCustomDirView.DDDragLeave;
  1880. begin
  1881. if Assigned(DropTarget) then
  1882. begin
  1883. if GlobalDragImageList.Dragging then
  1884. GlobalDragImageList.HideDragImage;
  1885. DropTarget := nil;
  1886. Update; {ie30}
  1887. end
  1888. else DropTarget := nil;
  1889. if Assigned(FOnDDDragLeave) then
  1890. FOnDDDragLeave(Self);
  1891. end;
  1892. procedure TCustomDirView.DDDragOver(grfKeyState: Integer; Point: TPoint;
  1893. var dwEffect: Integer);
  1894. var
  1895. DropItem: TListItem;
  1896. CanDrop: Boolean;
  1897. HasDropHandler: Boolean;
  1898. begin
  1899. FDDOwnerIsSource := DragDropFilesEx.OwnerIsSource;
  1900. {Set droptarget if target is directory:}
  1901. if not Loading then DropItem := GetItemAt(Point.X, Point.Y)
  1902. else DropItem := nil;
  1903. HasDropHandler := (Assigned(DropItem) and (not IsRecycleBin) and
  1904. TargetHasDropHandler(DropItem, dwEffect));
  1905. CanDrop := Assigned(DropItem) and (not IsRecycleBin) and
  1906. (ItemIsDirectory(DropItem) or HasDropHandler);
  1907. if (CanDrop and (DropTarget <> DropItem)) or
  1908. (not CanDrop and Assigned(DropTarget)) then
  1909. begin
  1910. if GlobalDragImageList.Dragging then
  1911. begin
  1912. GlobalDragImageList.HideDragImage;
  1913. DropTarget := nil;
  1914. Update;
  1915. if CanDrop then
  1916. begin
  1917. DropTarget := DropItem;
  1918. Update;
  1919. end;
  1920. GlobalDragImageList.ShowDragImage;
  1921. end
  1922. else
  1923. begin
  1924. DropTarget := nil;
  1925. if CanDrop then DropTarget := DropItem;
  1926. end;
  1927. end;
  1928. if not Loading then
  1929. FScrollOnDragOver.DragOver(Point);
  1930. {Set dropeffect:}
  1931. if (not HasDropHandler) and (not Loading) then
  1932. begin
  1933. DDChooseEffect(grfKeyState, dwEffect);
  1934. if Assigned(FOnDDDragOver) then
  1935. FOnDDDragOver(Self, grfKeyState, Point, dwEffect);
  1936. // cannot drop to dragged files
  1937. if DragDropFilesEx.OwnerIsSource and Assigned(DropItem) then
  1938. begin
  1939. if Assigned(ItemFocused) and (not ItemFocused.Selected) then
  1940. begin
  1941. if DropItem = ItemFocused then
  1942. begin
  1943. dwEffect := DropEffect_None;
  1944. end;
  1945. end
  1946. else
  1947. if DropItem.Selected then
  1948. begin
  1949. dwEffect := DropEffect_None;
  1950. end;
  1951. end;
  1952. if DragDropFilesEx.OwnerIsSource and (dwEffect = DropEffect_Move) and
  1953. (not Assigned(DropTarget)) then
  1954. begin
  1955. dwEffect := DropEffect_None;
  1956. end
  1957. else
  1958. if Assigned(DropTarget) and ItemIsRecycleBin(DropTarget) and
  1959. (dwEffect <> DropEffect_None) then
  1960. begin
  1961. dwEffect := DropEffect_Move;
  1962. end;
  1963. end;
  1964. end;
  1965. function TCustomDirView.CustomCreateFileList(Focused, OnlyFocused: Boolean;
  1966. FullPath: Boolean; FileList: TStrings; ItemObject: Boolean): TStrings;
  1967. procedure AddItem(Item: TListItem);
  1968. var
  1969. AObject: TObject;
  1970. begin
  1971. Assert(Assigned(Item));
  1972. if ItemObject then AObject := Item
  1973. else AObject := Item.Data;
  1974. if FullPath then Result.AddObject(ItemFullFileName(Item), AObject)
  1975. else Result.AddObject(ItemFileName(Item), AObject);
  1976. end;
  1977. var
  1978. Item: TListItem;
  1979. begin
  1980. if Assigned(FileList) then Result := FileList
  1981. else Result := TStringList.Create;
  1982. try
  1983. if Assigned(ItemFocused) and
  1984. ((Focused and (not ItemFocused.Selected)) or (SelCount = 0) or OnlyFocused) then
  1985. begin
  1986. AddItem(ItemFocused)
  1987. end
  1988. else
  1989. begin
  1990. Item := GetNextItem(nil, sdAll, [isSelected]);
  1991. while Assigned(Item) do
  1992. begin
  1993. AddItem(Item);
  1994. Item := GetNextItem(Item, sdAll, [isSelected]);
  1995. end;
  1996. end;
  1997. except
  1998. if not Assigned(FileList) then FreeAndNil(Result);
  1999. raise;
  2000. end;
  2001. end;
  2002. function TCustomDirView.CreateFocusedFileList(FullPath: Boolean; FileList: TStrings): TStrings;
  2003. begin
  2004. Result := CustomCreateFileList(False, True, FullPath, FileList);
  2005. end;
  2006. function TCustomDirView.CreateFileList(Focused: Boolean; FullPath: Boolean;
  2007. FileList: TStrings): TStrings;
  2008. begin
  2009. Result := CustomCreateFileList(Focused, False, FullPath, FileList);
  2010. end;
  2011. procedure TCustomDirView.DDDrop(DataObj: IDataObject; grfKeyState: Integer;
  2012. Point: TPoint; var dwEffect: Integer);
  2013. begin
  2014. if GlobalDragImageList.Dragging then
  2015. GlobalDragImageList.HideDragImage;
  2016. if dwEffect = DropEffect_None then
  2017. DropTarget := nil;
  2018. if Assigned(OnDDDrop) then
  2019. OnDDDrop(Self, DataObj, grfKeyState, Point, dwEffect);
  2020. end;
  2021. procedure TCustomDirView.DDQueryContinueDrag(FEscapePressed: LongBool;
  2022. grfKeyState: Integer; var Result: HResult);
  2023. var
  2024. MousePos: TPoint;
  2025. KnowTime: TFileTime;
  2026. begin
  2027. // this method cannot throw exceptions, if it does d&d will not be possible
  2028. // anymore (see TDragDrop.ExecuteOperation, global GInternalSource)
  2029. if Result = DRAGDROP_S_DROP then
  2030. begin
  2031. GetSystemTimeAsFileTime(KnowTime);
  2032. if ((Int64(KnowTime) - Int64(FDragStartTime)) <= DDDragStartDelay) then
  2033. Result := DRAGDROP_S_CANCEL;
  2034. end;
  2035. if Assigned(OnDDQueryContinueDrag) then
  2036. begin
  2037. OnDDQueryContinueDrag(Self, FEscapePressed, grfKeyState, Result);
  2038. end;
  2039. try
  2040. if FEscapePressed then
  2041. begin
  2042. if GlobalDragImageList.Dragging then
  2043. GlobalDragImageList.HideDragImage;
  2044. end
  2045. else
  2046. begin
  2047. if GlobalDragImageList.Dragging Then
  2048. begin
  2049. MousePos := ParentForm.ScreenToClient(Mouse.CursorPos);
  2050. {Move the drag image to the new position and show it:}
  2051. if (MousePos.X <> FDragPos.X) or (MousePos.Y <> FDragPos.Y) then
  2052. begin
  2053. FDragPos := MousePos;
  2054. if PtInRect(ParentForm.BoundsRect, Mouse.CursorPos) then
  2055. begin
  2056. GlobalDragImageList.DragMove(MousePos.X, MousePos.Y);
  2057. GlobalDragImageList.ShowDragImage;
  2058. end
  2059. else GlobalDragImageList.HideDragImage;
  2060. end;
  2061. end;
  2062. end;
  2063. except
  2064. // do not care if the above fails
  2065. // (Mouse.CursorPos fails when desktop is locked by user)
  2066. end;
  2067. end;
  2068. procedure TCustomDirView.DDSpecifyDropTarget(Sender: TObject;
  2069. DragDropHandler: Boolean; Point: TPoint; var pidlFQ: PItemIDList;
  2070. var Filename: string);
  2071. var
  2072. Item: TListItem;
  2073. begin
  2074. pidlFQ := nil;
  2075. if DirOK and (not Loading) then
  2076. begin
  2077. if DragDropHandler then
  2078. begin
  2079. if Assigned(DropTarget) and ItemIsDirectory(DropTarget) then
  2080. FileName := ItemFullFileName(DropTarget)
  2081. else
  2082. FileName := PathName;
  2083. end
  2084. else
  2085. begin
  2086. Item := GetItemAt(Point.X, Point.Y);
  2087. if Assigned(Item) and (not ItemIsDirectory(Item)) and
  2088. (not IsRecycleBin) then
  2089. FileName := ItemFullFileName(Item)
  2090. else
  2091. FileName := '';
  2092. end;
  2093. end
  2094. else FileName := '';
  2095. end;
  2096. procedure TCustomDirView.DDMenuPopup(Sender: TObject; AMenu: HMenu;
  2097. DataObj: IDataObject; AMinCustCmd: Integer; grfKeyState: Longint; pt: TPoint);
  2098. begin
  2099. if Assigned(OnDDMenuPopup) then
  2100. begin
  2101. OnDDMenuPopup(Self, AMenu, DataObj, AMinCustCmd, grfKeyState, pt);
  2102. end;
  2103. end;
  2104. procedure TCustomDirView.DDMenuDone(Sender: TObject; AMenu: HMenu);
  2105. begin
  2106. end;
  2107. procedure TCustomDirView.DDDropHandlerSucceeded(Sender: TObject;
  2108. grfKeyState: Integer; Point: TPoint; dwEffect: Integer);
  2109. begin
  2110. DropTarget := nil;
  2111. end;
  2112. procedure TCustomDirView.DDChooseEffect(grfKeyState: Integer; var dwEffect: Integer);
  2113. begin
  2114. if Assigned(FOnDDChooseEffect) then
  2115. FOnDDChooseEffect(Self, grfKeyState, dwEffect);
  2116. end;
  2117. procedure TCustomDirView.DDGiveFeedback(dwEffect: Integer;
  2118. var Result: HResult);
  2119. begin
  2120. if Assigned(FOnDDGiveFeedback) then
  2121. FOnDDGiveFeedback(Self, dwEffect, Result);
  2122. end;
  2123. procedure TCustomDirView.DDProcessDropped(Sender: TObject;
  2124. grfKeyState: Integer; Point: TPoint; dwEffect: Integer);
  2125. begin
  2126. if DirOK and (not Loading) then
  2127. try
  2128. try
  2129. if Assigned(FOnDDProcessDropped) then
  2130. FOnDDProcessDropped(Self, grfKeyState, Point, dwEffect);
  2131. if dwEffect <> DropEffect_None then
  2132. begin
  2133. PerformItemDragDropOperation(DropTarget, dwEffect);
  2134. if Assigned(FOnDDExecuted) then
  2135. FOnDDExecuted(Self, dwEffect);
  2136. end;
  2137. finally
  2138. DragDropFilesEx.FileList.Clear;
  2139. DropTarget := nil;
  2140. end;
  2141. except
  2142. Application.HandleException(Self);
  2143. end;
  2144. end;
  2145. function TCustomDirView.AnyFileSelected(
  2146. OnlyFocused: Boolean; FilesOnly: Boolean; FocusedFileOnlyWhenFocused: Boolean): Boolean;
  2147. var
  2148. Item: TListItem;
  2149. begin
  2150. if OnlyFocused or
  2151. ((SelCount = 0) and
  2152. ((not FocusedFileOnlyWhenFocused) or
  2153. (Focused and (GetParentForm(Self).Handle = GetForegroundWindow())))) then
  2154. begin
  2155. Result := Assigned(ItemFocused) and ItemIsFile(ItemFocused) and
  2156. ((not FilesOnly) or (not ItemIsDirectory(ItemFocused)));
  2157. end
  2158. else
  2159. begin
  2160. Result := True;
  2161. Item := GetNextItem(nil, sdAll, [isSelected]);
  2162. while Assigned(Item) do
  2163. begin
  2164. if ItemIsFile(Item) and
  2165. ((not FilesOnly) or (not ItemIsDirectory(Item))) then Exit;
  2166. Item := GetNextItem(Item, sdAll, [isSelected]);
  2167. end;
  2168. Result := False;
  2169. end;
  2170. end;
  2171. function TCustomDirView.CanEdit(Item: TListItem): Boolean;
  2172. begin
  2173. Result :=
  2174. (inherited CanEdit(Item) or FForceRename) and (not Loading) and
  2175. Assigned(Item) and (not ReadOnly) and (not IsRecycleBin) and
  2176. (not ItemIsParentDirectory(Item));
  2177. if Result then FLoadEnabled := False;
  2178. FForceRename := False;
  2179. end;
  2180. function TCustomDirView.CanChangeSelection(Item: TListItem;
  2181. Select: Boolean): Boolean;
  2182. begin
  2183. Result :=
  2184. (not Loading) and
  2185. not (Assigned(Item) and Assigned(Item.Data) and
  2186. ItemIsParentDirectory(Item));
  2187. end;
  2188. procedure TCustomDirView.Edit(const HItem: TLVItem);
  2189. var
  2190. Info: string;
  2191. Index: Integer;
  2192. begin
  2193. if Length(HItem.pszText) = 0 then LoadEnabled := True
  2194. else
  2195. begin
  2196. {Does the changed filename contains invalid characters?}
  2197. if StrContains(FInvalidNameChars, HItem.pszText) then
  2198. begin
  2199. Info := FInvalidNameChars;
  2200. for Index := Length(Info) downto 1 do
  2201. System.Insert(Space, Info, Index);
  2202. MessageBeep(MB_ICONHAND);
  2203. if MessageDlg(SErrorInvalidName + Space + Info, mtError,
  2204. [mbOK, mbAbort], 0) = mrOK then RetryRename(HItem.pszText);
  2205. LoadEnabled := True;
  2206. end
  2207. else
  2208. begin
  2209. InternalEdit(HItem);
  2210. end;
  2211. end;
  2212. end; {Edit}
  2213. procedure TCustomDirView.EndSelectionUpdate;
  2214. begin
  2215. inherited;
  2216. if FUpdatingSelection = 0 then
  2217. UpdateStatusBar;
  2218. end; { EndUpdatingSelection }
  2219. procedure TCustomDirView.ExecuteCurrentFile;
  2220. begin
  2221. Assert(Assigned(ItemFocused));
  2222. Execute(ItemFocused);
  2223. end;
  2224. procedure TCustomDirView.Execute(Item: TListItem);
  2225. var
  2226. AllowExec: Boolean;
  2227. begin
  2228. Assert(Assigned(Item));
  2229. if Assigned(Item) and Assigned(Item.Data) and (not Loading) then
  2230. begin
  2231. if IsRecycleBin and (not ItemIsParentDirectory(Item)) then DisplayPropertiesMenu
  2232. else
  2233. begin
  2234. AllowExec := True;
  2235. if Assigned(FOnExecFile) then FOnExecFile(Self, Item, AllowExec);
  2236. if AllowExec then
  2237. begin
  2238. if ItemIsParentDirectory(Item) then ExecuteParentDirectory
  2239. else ExecuteFile(Item);
  2240. end;
  2241. end;
  2242. end;
  2243. end;
  2244. procedure TCustomDirView.GetDisplayInfo(ListItem: TListItem;
  2245. var DispInfo: TLVItem);
  2246. begin
  2247. // Nothing
  2248. end;
  2249. procedure TCustomDirView.WMUserRename(var Message: TMessage);
  2250. begin
  2251. if Assigned(ItemFocused) then
  2252. begin
  2253. FForceRename := True;
  2254. ListView_EditLabel(Handle, ItemFocused.Index);
  2255. SetWindowText(ListView_GetEditControl(Self.Handle),
  2256. PChar(FLastRenameName));
  2257. end;
  2258. end;
  2259. procedure TCustomDirView.RetryRename(NewName: string);
  2260. begin
  2261. FLastRenameName := NewName;
  2262. PostMessage(Self.Handle, WM_USER_RENAME, Longint(PChar(NewName)), 0);
  2263. end;
  2264. procedure TCustomDirView.AddToDragFileList(FileList: TFileList; Item: TListItem);
  2265. begin
  2266. FileList.AddItem(nil, ItemFullFileName(Item));
  2267. end;
  2268. procedure TCustomDirView.DDDragDetect(grfKeyState: Integer; DetectStart,
  2269. Point: TPoint; DragStatus: TDragDetectStatus);
  2270. var
  2271. FilesCount: Integer;
  2272. DirsCount: Integer;
  2273. Item: TListItem;
  2274. FirstItem : TListItem;
  2275. Bitmap: TBitmap;
  2276. ImageListHandle: HImageList;
  2277. Spot: TPoint;
  2278. ItemPos: TPoint;
  2279. DragText: string;
  2280. ClientPoint: TPoint;
  2281. FileListCreated: Boolean;
  2282. AvoidDragImage: Boolean;
  2283. DataObject: TDataObject;
  2284. begin
  2285. if Assigned(FOnDDDragDetect) then
  2286. FOnDDDragDetect(Self, grfKeyState, DetectStart, Point, DragStatus);
  2287. FLastDDResult := drCancelled;
  2288. if (DragStatus = ddsDrag) and (not Loading) and (MarkedCount > 0) then
  2289. begin
  2290. DragDropFilesEx.CompleteFileList := DragCompleteFileList;
  2291. DragDropFilesEx.FileList.Clear;
  2292. FirstItem := nil;
  2293. FilesCount := 0;
  2294. DirsCount := 0;
  2295. FileListCreated := False;
  2296. AvoidDragImage := False;
  2297. if Assigned(OnDDCreateDragFileList) then
  2298. begin
  2299. OnDDCreateDragFileList(Self, DragDropFilesEx.FileList, FileListCreated);
  2300. if FileListCreated then
  2301. begin
  2302. AvoidDragImage := True;
  2303. end;
  2304. end;
  2305. if not FileListCreated then
  2306. begin
  2307. if Assigned(ItemFocused) and (not ItemFocused.Selected) then
  2308. begin
  2309. if ItemCanDrag(ItemFocused) then
  2310. begin
  2311. FirstItem := ItemFocused;
  2312. AddToDragFileList(DragDropFilesEx.FileList, ItemFocused);
  2313. if ItemIsDirectory(ItemFocused) then Inc(DirsCount)
  2314. else Inc(FilesCount);
  2315. end;
  2316. end
  2317. else
  2318. if SelCount > 0 then
  2319. begin
  2320. Item := GetNextItem(nil, sdAll, [isSelected]);
  2321. while Assigned(Item) do
  2322. begin
  2323. if ItemCanDrag(Item) then
  2324. begin
  2325. if not Assigned(FirstItem) then FirstItem := Item;
  2326. AddToDragFileList(DragDropFilesEx.FileList, Item);
  2327. if ItemIsDirectory(Item) then Inc(DirsCount)
  2328. else Inc(FilesCount);
  2329. end;
  2330. Item := GetNextItem(Item, sdAll, [isSelected]);
  2331. end;
  2332. end;
  2333. end;
  2334. if DragDropFilesEx.FileList.Count > 0 then
  2335. begin
  2336. FDragEnabled := False;
  2337. {Create the dragimage:}
  2338. GlobalDragImageList := DragImageList;
  2339. // This code is not used anymore
  2340. if UseDragImages and (not AvoidDragImage) then
  2341. begin
  2342. ImageListHandle := ListView_CreateDragImage(Handle, FirstItem.Index, Spot);
  2343. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2344. if ImageListHandle <> Invalid_Handle_Value then
  2345. begin
  2346. GlobalDragImageList.Handle := ImageListHandle;
  2347. if FilesCount + DirsCount = 1 then
  2348. begin
  2349. ItemPos := ClientToScreen(FirstItem.DisplayRect(drBounds).TopLeft);
  2350. GlobalDragImageList.SetDragImage(0,
  2351. DetectStart.X - ItemPos.X, DetectStart.Y - ItemPos.Y);
  2352. end
  2353. else
  2354. begin
  2355. GlobalDragImageList.Clear;
  2356. GlobalDragImageList.Width := 32;
  2357. GlobalDragImageList.Height := 32;
  2358. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES', 0,
  2359. [lrTransparent], $FFFFFF) Then
  2360. begin
  2361. Bitmap := TBitmap.Create;
  2362. try
  2363. try
  2364. GlobalDragImageList.GetBitmap(0, Bitmap);
  2365. Bitmap.Canvas.Font.Assign(Self.Font);
  2366. DragText := '';
  2367. if FilesCount > 0 then
  2368. DragText := Format(STextFiles, [FilesCount]);
  2369. if DirsCount > 0 then
  2370. begin
  2371. if FilesCount > 0 then
  2372. DragText := DragText + ', ';
  2373. DragText := DragText + Format(STextDirectories, [DirsCount]);
  2374. end;
  2375. Bitmap.Width := 33 + Bitmap.Canvas.TextWidth(DragText);
  2376. Bitmap.TransparentMode := tmAuto;
  2377. Bitmap.Canvas.TextOut(33,
  2378. Max(24 - Abs(Canvas.Font.Height), 0), DragText);
  2379. GlobalDragImageList.Clear;
  2380. GlobalDragImageList.Width := Bitmap.Width;
  2381. GlobalDragImageList.AddMasked(Bitmap,
  2382. Bitmap.Canvas.Pixels[0, 0]);
  2383. GlobalDragImageList.SetDragImage(0, 25, 20);
  2384. except
  2385. if GlobalDragImageList.GetResource(rtBitMap, 'DRAGFILES',
  2386. 0, [lrTransparent], $FFFFFF) then
  2387. GlobalDragImageList.SetDragImage(0, 25, 20);
  2388. end;
  2389. finally
  2390. Bitmap.Free;
  2391. end;
  2392. end;
  2393. end;
  2394. ClientPoint := ParentForm.ScreenToClient(Point);
  2395. GlobalDragImageList.BeginDrag(ParentForm.Handle,
  2396. ClientPoint.X, ClientPoint.Y);
  2397. GlobalDragImageList.HideDragImage;
  2398. ShowCursor(True);
  2399. end;
  2400. end;
  2401. FContextMenu := False;
  2402. if IsRecycleBin then DragDropFilesEx.SourceEffects := [deMove]
  2403. else DragDropFilesEx.SourceEffects := DragSourceEffects;
  2404. DropSourceControl := Self;
  2405. try
  2406. GetSystemTimeAsFileTime(FDragStartTime);
  2407. DataObject := nil;
  2408. if Assigned(OnDDCreateDataObject) then
  2409. begin
  2410. OnDDCreateDataObject(Self, DataObject);
  2411. end;
  2412. {Execute the drag&drop-Operation:}
  2413. FLastDDResult := DragDropFilesEx.Execute(DataObject);
  2414. // The drag&drop operation is finished, so clean up the used drag image.
  2415. // This also restores the default mouse cursor
  2416. // (which is set to "none" in GlobalDragImageList.BeginDrag above)
  2417. // But it's actually too late, we would need to do it when mouse button
  2418. // is realesed already. Otherwise the cursor is hidden when hovering over
  2419. // main window, while target application is processing dropped file
  2420. // (particularly when Explorer displays progress window or
  2421. // overwrite confirmation prompt)
  2422. GlobalDragImageList.EndDrag;
  2423. GlobalDragImageList.Clear;
  2424. Application.ProcessMessages;
  2425. finally
  2426. DragDropFilesEx.FileList.Clear;
  2427. FContextMenu := False;
  2428. try
  2429. if Assigned(OnDDEnd) then
  2430. OnDDEnd(Self);
  2431. finally
  2432. DropTarget := nil;
  2433. DropSourceControl := nil;
  2434. end;
  2435. end;
  2436. end;
  2437. end;
  2438. end;
  2439. procedure TCustomDirView.Notification(AComponent: TComponent; Operation: TOperation);
  2440. begin
  2441. inherited;
  2442. if Operation = opRemove then
  2443. begin
  2444. if AComponent = PathLabel then FPathLabel := nil;
  2445. end;
  2446. end; { Notification }
  2447. procedure TCustomDirView.WMAppCommand(var Message: TMessage);
  2448. var
  2449. Command: Integer;
  2450. Shift: TShiftState;
  2451. begin
  2452. Command := HiWord(Message.lParam) and (not FAPPCOMMAND_MASK);
  2453. Shift := KeyDataToShiftState(HiWord(Message.lParam) and FAPPCOMMAND_MASK);
  2454. if Shift * [ssShift, ssAlt, ssCtrl] = [] then
  2455. begin
  2456. if Command = APPCOMMAND_BROWSER_BACKWARD then
  2457. begin
  2458. Message.Result := 1;
  2459. if BackCount >= 1 then DoHistoryGo(-1);
  2460. end
  2461. else
  2462. if Command = APPCOMMAND_BROWSER_FORWARD then
  2463. begin
  2464. Message.Result := 1;
  2465. if ForwardCount >= 1 then DoHistoryGo(1);
  2466. end
  2467. else
  2468. if Command = APPCOMMAND_BROWSER_REFRESH then
  2469. begin
  2470. Message.Result := 1;
  2471. BusyOperation(ReloadDirectory);
  2472. end
  2473. else
  2474. if Command = APPCOMMAND_BROWSER_HOME then
  2475. begin
  2476. Message.Result := 1;
  2477. BusyOperation(ExecuteHomeDirectory);
  2478. end
  2479. else inherited;
  2480. end
  2481. else inherited;
  2482. end;
  2483. procedure TCustomDirView.CMColorChanged(var Message: TMessage);
  2484. begin
  2485. inherited;
  2486. ForceColorChange(Self);
  2487. end;
  2488. function TCustomDirView.FindFileItem(FileName: string): TListItem;
  2489. type
  2490. TFileNameCompare = function(const S1, S2: string): Integer;
  2491. var
  2492. Index: Integer;
  2493. CompareFunc: TFileNameCompare;
  2494. begin
  2495. if FCaseSensitive then CompareFunc := CompareStr
  2496. else CompareFunc := CompareText;
  2497. for Index := 0 to Items.Count - 1 do
  2498. begin
  2499. if CompareFunc(FileName, ItemFileName(Items[Index])) = 0 then
  2500. begin
  2501. Result := Items[Index];
  2502. Exit;
  2503. end;
  2504. end;
  2505. Result := nil;
  2506. end;
  2507. procedure TCustomDirView.DoAnimation(Start: Boolean);
  2508. begin
  2509. if Start and LoadAnimation then
  2510. begin
  2511. if not Assigned(FAnimation) then
  2512. begin
  2513. FAnimation := TAnimate.Create(Self);
  2514. try
  2515. FAnimation.Top := (Height - FAnimation.Height) div 2;
  2516. FAnimation.Left := (Width - FAnimation.Width) div 2;
  2517. FAnimation.Parent := Self;
  2518. FAnimation.CommonAVI := aviFindFolder;
  2519. FAnimation.Transparent := True;
  2520. FAnimation.Active := True;
  2521. except
  2522. FreeAndNil(FAnimation);
  2523. end;
  2524. end;
  2525. end
  2526. else
  2527. if not Start then
  2528. begin
  2529. FreeAndNil(FAnimation);
  2530. end;
  2531. end; { DoAnimation }
  2532. function TCustomDirView.GetForwardCount: Integer;
  2533. begin
  2534. Result := FHistoryPaths.Count - BackCount;
  2535. end; { GetForwardCount }
  2536. procedure TCustomDirView.LimitHistorySize;
  2537. begin
  2538. while FHistoryPaths.Count > MaxHistoryCount do
  2539. begin
  2540. if BackCount > 0 then
  2541. begin
  2542. FHistoryPaths.Delete(0);
  2543. Dec(FBackCount);
  2544. end
  2545. else
  2546. FHistoryPaths.Delete(FHistoryPaths.Count-1);
  2547. end;
  2548. end; { LimitHistorySize }
  2549. function TCustomDirView.GetHistoryPath(Index: Integer): string;
  2550. begin
  2551. Assert(Assigned(FHistoryPaths));
  2552. if Index = 0 then Result := PathName
  2553. else
  2554. if Index < 0 then Result := FHistoryPaths[Index + BackCount]
  2555. else
  2556. if Index > 0 then Result := FHistoryPaths[Index + BackCount - 1];
  2557. end; { GetHistoryPath }
  2558. procedure TCustomDirView.SetMaxHistoryCount(Value: Integer);
  2559. begin
  2560. if FMaxHistoryCount <> Value then
  2561. begin
  2562. FMaxHistoryCount := Value;
  2563. DoHistoryChange;
  2564. end;
  2565. end; { SetMaxHistoryCount }
  2566. procedure TCustomDirView.DoHistoryChange;
  2567. begin
  2568. LimitHistorySize;
  2569. if Assigned(OnHistoryChange) then
  2570. OnHistoryChange(Self);
  2571. end; { DoHistoryChange }
  2572. procedure TCustomDirView.DoHistoryGo(Index: Integer);
  2573. var
  2574. Cancel: Boolean;
  2575. begin
  2576. if StartBusy then
  2577. try
  2578. Cancel := False;
  2579. if Assigned(OnHistoryGo) then
  2580. OnHistoryGo(Self, Index, Cancel);
  2581. if not Cancel then HistoryGo(Index);
  2582. finally
  2583. EndBusy;
  2584. end;
  2585. end;
  2586. procedure TCustomDirView.HistoryGo(Index: Integer);
  2587. var
  2588. PrevPath: string;
  2589. begin
  2590. if Index <> 0 then
  2591. begin
  2592. PrevPath := FHistoryPath;
  2593. FDontRecordPath := True;
  2594. try
  2595. Path := HistoryPath[Index];
  2596. finally
  2597. FDontRecordPath := False;
  2598. end;
  2599. FHistoryPaths.Insert(FBackCount, PrevPath);
  2600. FHistoryPaths.Delete(Index + BackCount);
  2601. Inc(FBackCount, Index);
  2602. DoHistoryChange;
  2603. end;
  2604. end; { HistoryGo }
  2605. procedure TCustomDirView.PathChanging(Relative: Boolean);
  2606. begin
  2607. if Relative then FLastPath := PathName
  2608. else FLastPath := '';
  2609. FSavedNames.Clear;
  2610. end;
  2611. procedure TCustomDirView.PathChanged;
  2612. var
  2613. Index: Integer;
  2614. begin
  2615. if Assigned(OnPathChange) then
  2616. OnPathChange(Self);
  2617. if (not FDontRecordPath) and (FHistoryPath <> '') and (FHistoryPath <> PathName) then
  2618. begin
  2619. Assert(Assigned(FHistoryPaths));
  2620. for Index := FHistoryPaths.Count - 1 downto BackCount do
  2621. FHistoryPaths.Delete(Index);
  2622. FHistoryPaths.Add(FHistoryPath);
  2623. Inc(FBackCount);
  2624. DoHistoryChange;
  2625. end;
  2626. FHistoryPath := PathName;
  2627. end; { PathChanged }
  2628. procedure TCustomDirView.ProcessChangedFiles(DirView: TCustomDirView;
  2629. FileList: TStrings; FullPath: Boolean; ExistingOnly: Boolean;
  2630. Criterias: TCompareCriterias);
  2631. var
  2632. Item, MirrorItem: TListItem;
  2633. FileTime, MirrorFileTime: TDateTime;
  2634. OldCursor: TCursor;
  2635. Index: Integer;
  2636. Changed: Boolean;
  2637. SameTime: Boolean;
  2638. Precision, MirrorPrecision: TDateTimePrecision;
  2639. begin
  2640. Assert(Valid);
  2641. OldCursor := Screen.Cursor;
  2642. if not Assigned(FileList) then
  2643. begin
  2644. Items.BeginUpdate;
  2645. BeginSelectionUpdate;
  2646. end;
  2647. try
  2648. Screen.Cursor := crHourGlass;
  2649. for Index := 0 to Items.Count-1 do
  2650. begin
  2651. Item := Items[Index];
  2652. Changed := False;
  2653. if not ItemIsDirectory(Item) then
  2654. begin
  2655. MirrorItem := DirView.FindFileItem(ItemFileName(Item));
  2656. if MirrorItem = nil then
  2657. begin
  2658. Changed := not ExistingOnly;
  2659. end
  2660. else
  2661. begin
  2662. if ccTime in Criterias then
  2663. begin
  2664. FileTime := ItemFileTime(Item, Precision);
  2665. MirrorFileTime := DirView.ItemFileTime(MirrorItem, MirrorPrecision);
  2666. if MirrorPrecision < Precision then Precision := MirrorPrecision;
  2667. if Precision <> tpMillisecond then
  2668. begin
  2669. ReduceDateTimePrecision(FileTime, Precision);
  2670. ReduceDateTimePrecision(MirrorFileTime, Precision);
  2671. end;
  2672. SameTime := (FileTime = MirrorFileTime);
  2673. if Precision = tpSecond then
  2674. begin
  2675. // 1 ms more solves the rounding issues
  2676. // (see also Common.cpp)
  2677. MirrorFileTime := MirrorFileTime + EncodeTime(0, 0, 1, 1);
  2678. end;
  2679. Changed :=
  2680. (FileTime > MirrorFileTime);
  2681. end
  2682. else
  2683. begin
  2684. SameTime := True;
  2685. end;
  2686. if (not Changed) and SameTime and (ccSize in Criterias) then
  2687. begin
  2688. Changed := ItemFileSize(Item) <> DirView.ItemFileSize(MirrorItem);
  2689. end
  2690. end;
  2691. end;
  2692. if Assigned(FileList) then
  2693. begin
  2694. if Changed then
  2695. begin
  2696. if FullPath then
  2697. begin
  2698. FileList.AddObject(ItemFullFileName(Item), Item.Data)
  2699. end
  2700. else
  2701. begin
  2702. FileList.AddObject(ItemFileName(Item), Item.Data);
  2703. end;
  2704. end;
  2705. end
  2706. else
  2707. begin
  2708. Item.Selected := Changed;
  2709. end;
  2710. end;
  2711. finally
  2712. Screen.Cursor := OldCursor;
  2713. if not Assigned(FileList) then
  2714. begin
  2715. Items.EndUpdate;
  2716. EndSelectionUpdate;
  2717. end;
  2718. end;
  2719. end;
  2720. function TCustomDirView.CreateChangedFileList(DirView: TCustomDirView;
  2721. FullPath: Boolean; ExistingOnly: Boolean; Criterias: TCompareCriterias): TStrings;
  2722. begin
  2723. Result := TStringList.Create;
  2724. try
  2725. ProcessChangedFiles(DirView, Result, FullPath, ExistingOnly, Criterias);
  2726. except
  2727. FreeAndNil(Result);
  2728. raise;
  2729. end;
  2730. end;
  2731. function TCustomDirView.CanPasteFromClipBoard: Boolean;
  2732. begin
  2733. Result := False;
  2734. if DirOK and (Path <> '') and Windows.OpenClipboard(0) then
  2735. begin
  2736. Result := IsClipboardFormatAvailable(CF_HDROP);
  2737. Windows.CloseClipBoard;
  2738. end;
  2739. end; {CanPasteFromClipBoard}
  2740. procedure TCustomDirView.CompareFiles(DirView: TCustomDirView;
  2741. ExistingOnly: Boolean; Criterias: TCompareCriterias);
  2742. begin
  2743. ProcessChangedFiles(DirView, nil, True, ExistingOnly, Criterias);
  2744. end;
  2745. procedure TCustomDirView.FocusSomething;
  2746. begin
  2747. if FSavedSelection then FPendingFocusSomething := True
  2748. else inherited;
  2749. end;
  2750. procedure TCustomDirView.SaveSelection;
  2751. var
  2752. Closest: TListItem;
  2753. begin
  2754. Assert(not FSavedSelection);
  2755. FSavedSelectionFile := '';
  2756. FSavedSelectionLastFile := '';
  2757. if Assigned(ItemFocused) then
  2758. begin
  2759. FSavedSelectionLastFile := ItemFocused.Caption;
  2760. end;
  2761. Closest := ClosestUnselected(ItemFocused);
  2762. if Assigned(Closest) then
  2763. begin
  2764. FSavedSelectionFile := Closest.Caption;
  2765. end;
  2766. FSavedSelection := True;
  2767. end;
  2768. procedure TCustomDirView.RestoreSelection;
  2769. var
  2770. ItemToSelect: TListItem;
  2771. begin
  2772. Assert(FSavedSelection);
  2773. FSavedSelection := False;
  2774. if (FSavedSelectionLastFile <> '') and
  2775. ((not Assigned(ItemFocused)) or
  2776. (ItemFocused.Caption <> FSavedSelectionLastFile)) then
  2777. begin
  2778. ItemToSelect := FindFileItem(FSavedSelectionFile);
  2779. if Assigned(ItemToSelect) then
  2780. begin
  2781. ItemFocused := ItemToSelect;
  2782. end;
  2783. end;
  2784. if not Assigned(ItemFocused) then FocusSomething
  2785. else ItemFocused.MakeVisible(False);
  2786. end;
  2787. procedure TCustomDirView.DiscardSavedSelection;
  2788. begin
  2789. Assert(FSavedSelection);
  2790. FSavedSelection := False;
  2791. if FPendingFocusSomething then
  2792. begin
  2793. FPendingFocusSomething := False;
  2794. FocusSomething;
  2795. end;
  2796. end;
  2797. procedure TCustomDirView.SaveSelectedNames;
  2798. var
  2799. Index: Integer;
  2800. Item: TListItem;
  2801. begin
  2802. FSavedNames.Clear;
  2803. FSavedNames.CaseSensitive := FCaseSensitive;
  2804. if SelCount > 0 then // optimalisation
  2805. begin
  2806. for Index := 0 to Items.Count-1 do
  2807. begin
  2808. Item := Items[Index];
  2809. if Item.Selected then
  2810. FSavedNames.Add(ItemFileName(Item));
  2811. end;
  2812. end;
  2813. // as optimalisation the list is sorted only when the selection is restored
  2814. end;
  2815. procedure TCustomDirView.RestoreSelectedNames;
  2816. var
  2817. Index, FoundIndex: Integer;
  2818. Item: TListItem;
  2819. begin
  2820. FSavedNames.Sort;
  2821. for Index := 0 to Items.Count - 1 do
  2822. begin
  2823. Item := Items[Index];
  2824. Item.Selected := FSavedNames.Find(ItemFileName(Item), FoundIndex);
  2825. end;
  2826. end;
  2827. function TCustomDirView.GetSelectedNamesSaved: Boolean;
  2828. begin
  2829. Result := (FSavedNames.Count > 0);
  2830. end;
  2831. procedure TCustomDirView.ContinueSession(Continue: Boolean);
  2832. begin
  2833. if Continue then FLastPath := PathName
  2834. else FLastPath := '';
  2835. end;
  2836. function TCustomDirView.SaveState: TObject;
  2837. var
  2838. State: TDirViewState;
  2839. DirColProperties: TCustomDirViewColProperties;
  2840. begin
  2841. State := TDirViewState.Create;
  2842. State.HistoryPaths := TStringList.Create;
  2843. State.HistoryPaths.Assign(FHistoryPaths);
  2844. State.BackCount := FBackCount;
  2845. // TCustomDirViewColProperties should not be here
  2846. DirColProperties := ColProperties as TCustomDirViewColProperties;
  2847. Assert(Assigned(DirColProperties));
  2848. State.SortStr := DirColProperties.SortStr;
  2849. State.Mask := Mask;
  2850. if Assigned(ItemFocused) then State.FocusedItem := ItemFocused.Caption
  2851. else State.FocusedItem := '';
  2852. Result := State;
  2853. end;
  2854. procedure TCustomDirView.RestoreState(AState: TObject);
  2855. var
  2856. State: TDirViewState;
  2857. DirColProperties: TCustomDirViewColProperties;
  2858. ListItem: TListItem;
  2859. begin
  2860. Assert(AState is TDirViewState);
  2861. State := AState as TDirViewState;
  2862. Assert(Assigned(State));
  2863. FHistoryPaths.Assign(State.HistoryPaths);
  2864. FBackCount := State.BackCount;
  2865. DoHistoryChange;
  2866. // TCustomDirViewColProperties should not be here
  2867. DirColProperties := ColProperties as TCustomDirViewColProperties;
  2868. Assert(Assigned(DirColProperties));
  2869. DirColProperties.SortStr := State.SortStr;
  2870. Mask := State.Mask;
  2871. if State.FocusedItem <> '' then
  2872. begin
  2873. ListItem := FindFileItem(State.FocusedItem);
  2874. if Assigned(ListItem) then
  2875. begin
  2876. ItemFocused := ListItem;
  2877. ListItem.MakeVisible(False);
  2878. end;
  2879. end;
  2880. end;
  2881. procedure TCustomDirView.ClearState;
  2882. begin
  2883. FHistoryPaths.Clear;
  2884. FBackCount := 0;
  2885. DoHistoryChange;
  2886. end;
  2887. procedure TCustomDirView.SetMask(Value: string);
  2888. begin
  2889. if Mask <> Value then
  2890. begin
  2891. FMask := Value;
  2892. UpdatePathLabel;
  2893. if DirOK then Reload(False);
  2894. end;
  2895. end;{SetMask}
  2896. // WM_SETFOCUS works even when focus is moved to another window/app,
  2897. // while .Enter works only when focus is moved to order control of the same window.
  2898. procedure TCustomDirView.WMSetFocus(var Message: TWMSetFocus);
  2899. begin
  2900. inherited;
  2901. EnsureSelectionRedrawn;
  2902. UpdatePathLabel;
  2903. end;
  2904. procedure TCustomDirView.WMKillFocus(var Message: TWMKillFocus);
  2905. begin
  2906. inherited;
  2907. EnsureSelectionRedrawn;
  2908. UpdatePathLabel;
  2909. end;
  2910. procedure TCustomDirView.EnsureSelectionRedrawn;
  2911. begin
  2912. // WORKAROUND
  2913. // when receiving/losing focus, selection is not redrawn in report view
  2914. // (except for focus item selection),
  2915. // probably when double buffering is enabled (LVS_EX_DOUBLEBUFFER).
  2916. // But even without LVS_EX_DOUBLEBUFFER, selection behind file icon is not updated.
  2917. if ViewStyle = vsReport then
  2918. begin
  2919. if (SelCount >= 2) or ((SelCount >= 1) and ((not Assigned(ItemFocused)) or (not ItemFocused.Selected))) then
  2920. begin
  2921. Invalidate;
  2922. end
  2923. else
  2924. if Assigned(ItemFocused) and ItemFocused.Selected then
  2925. begin
  2926. // Optimization. When no item is selected, redraw just the focused item.
  2927. ItemFocused.Update;
  2928. end;
  2929. end;
  2930. end;
  2931. function TCustomDirView.DoBusy(Busy: Integer): Boolean;
  2932. begin
  2933. Result := True;
  2934. if Assigned(OnBusy) then
  2935. begin
  2936. OnBusy(Self, Busy, Result);
  2937. end;
  2938. end;
  2939. function TCustomDirView.StartBusy: Boolean;
  2940. begin
  2941. Result := DoBusy(1);
  2942. end;
  2943. function TCustomDirView.IsBusy: Boolean;
  2944. begin
  2945. Result := DoBusy(0);
  2946. end;
  2947. procedure TCustomDirView.EndBusy;
  2948. begin
  2949. DoBusy(-1);
  2950. end;
  2951. procedure TCustomDirView.BusyOperation(Operation: TBusyOperation);
  2952. begin
  2953. if StartBusy then
  2954. try
  2955. Operation;
  2956. finally
  2957. EndBusy;
  2958. end;
  2959. end;
  2960. initialization
  2961. DropSourceControl := nil;
  2962. SetLength(WinDir, MAX_PATH);
  2963. SetLength(WinDir, GetWindowsDirectory(PChar(WinDir), MAX_PATH));
  2964. SetLength(TempDir, MAX_PATH);
  2965. SetLength(TempDir, GetTempPath(MAX_PATH, PChar(TempDir)));
  2966. SpecialFolderLocation(CSIDL_PERSONAL, UserDocumentDirectory);
  2967. WinDir := IncludeTrailingPathDelimiter(WinDir);
  2968. TempDir := IncludeTrailingPathDelimiter(TempDir);
  2969. finalization
  2970. SetLength(StdDirTypeName, 0);
  2971. SetLength(WinDir, 0);
  2972. SetLength(TempDir, 0);
  2973. end.