CustomDirView.pas 101 KB

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