NortonLikeListView.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. unit NortonLikeListView;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. ComCtrls, ListViewColProperties, CommCtrl, Menus;
  6. type
  7. TCustomNortonLikeListView = class;
  8. TSelectMode = (smAll, smNone, smInvert);
  9. TNortonLikeMode = (nlOn, nlOff, nlKeyboard);
  10. TSelectMethod = (smNoneYet, smMouse, smKeyboard);
  11. TCustomNortonLikeListView = class(TCustomListView)
  12. private
  13. { Private declarations }
  14. FColProperties: TCustomListViewColProperties;
  15. FDontSelectItem: Boolean;
  16. FDontUnSelectItem: Boolean;
  17. FSelCount: Integer;
  18. FNortonLike: TNortonLikeMode;
  19. FLastDeletedItem: TListItem; // aby sme nepocitali smazany item 2x
  20. FFocusingItem: Boolean;
  21. FManageSelection: Boolean;
  22. FForceUpdateOnItemUnfocus: Boolean;
  23. FFirstSelected: Integer;
  24. FLastSelected: Integer;
  25. FFocused: TDateTime;
  26. FIgnoreSetFocusFrom: THandle;
  27. FSelectingImplicitly: Boolean;
  28. FAnyAndAllSelectedImplicitly: Boolean;
  29. FLButtonDownShiftState: TShiftState;
  30. FLButtonDownPos: TPoint;
  31. FLastSelectMethod: TSelectMethod;
  32. procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  33. procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
  34. procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  35. procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
  36. procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
  37. procedure WMChar(var Message: TWMChar); message WM_CHAR;
  38. procedure WMNotify(var Message: TWMNotify); message WM_NOTIFY;
  39. procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
  40. procedure LVMEditLabel(var Message: TMessage); message LVM_EDITLABEL;
  41. procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  42. procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
  43. function GetMarkedCount: Integer;
  44. function GetMarkedFile: TListItem;
  45. procedure ItemSelected(Item: TListItem; Index: Integer);
  46. procedure ItemUnselected(Item: TListItem; Index: Integer);
  47. procedure SelectAll(Mode: TSelectMode; Exclude: TListItem); reintroduce; overload;
  48. protected
  49. { Protected declarations }
  50. FClearingItems: Boolean;
  51. FUpdatingSelection: Integer;
  52. FNextCharToIgnore: Word;
  53. FHeaderHandle: HWND;
  54. procedure CreateWnd; override;
  55. procedure DestroyWnd; override;
  56. procedure BeginSelectionUpdate; virtual;
  57. procedure EndSelectionUpdate; virtual;
  58. function CanChangeSelection(Item: TListItem; Select: Boolean): Boolean; virtual;
  59. procedure ClearItems; virtual;
  60. procedure ItemsReordered;
  61. procedure Delete(Item: TListItem); override;
  62. function ExCanChange(Item: TListItem; Change: Integer;
  63. NewState, OldState: Word): Boolean; dynamic;
  64. procedure InsertItem(Item: TListItem); override;
  65. function NewColProperties: TCustomListViewColProperties; virtual; abstract;
  66. procedure FocusSomething; virtual;
  67. function EnableDragOnClick: Boolean; virtual;
  68. procedure FocusItem(Item: TListItem);
  69. function GetItemFromHItem(const Item: TLVItem): TListItem;
  70. function GetValid: Boolean; virtual;
  71. function GetSelCount: Integer; override;
  72. procedure DDBeforeDrag;
  73. function CanEdit(Item: TListItem): Boolean; override;
  74. function GetPopupMenu: TPopupMenu; override;
  75. procedure ChangeScale(M, D: Integer); override;
  76. public
  77. { Public declarations }
  78. constructor Create(AOwner: TComponent); override;
  79. destructor Destroy; override;
  80. function ClosestUnselected(Item: TListItem): TListItem;
  81. procedure SelectAll(Mode: TSelectMode); reintroduce; overload;
  82. procedure SelectCurrentItem(FocusNext: Boolean);
  83. function GetNextItem(StartItem: TListItem; Direction: TSearchDirection;
  84. States: TItemStates): TListItem;
  85. procedure MakeProgressVisible(Item: TListItem);
  86. property ColProperties: TCustomListViewColProperties read FColProperties write FColProperties stored False;
  87. property MultiSelect default True;
  88. property NortonLike: TNortonLikeMode read FNortonLike write FNortonLike default nlOn;
  89. property MarkedCount: Integer read GetMarkedCount;
  90. property MarkedFile: TListItem read GetMarkedFile;
  91. property Valid: Boolean read GetValid;
  92. property LastSelectMethod: TSelectMethod read FLastSelectMethod;
  93. end;
  94. implementation
  95. uses
  96. PasTools, Types;
  97. { TCustomNortonLikeListView }
  98. constructor TCustomNortonLikeListView.Create(AOwner: TComponent);
  99. begin
  100. inherited Create(AOwner);
  101. FSelCount := 0;
  102. FFirstSelected := -1;
  103. FLastSelected := -1;
  104. FClearingItems := False;
  105. MultiSelect := True;
  106. FDontSelectItem := False;
  107. FDontUnSelectItem := False;
  108. FNortonLike := nlOn;
  109. FColProperties := NewColProperties;
  110. FLastDeletedItem := nil;
  111. FUpdatingSelection := 0;
  112. FFocusingItem := False;
  113. FLastSelectMethod := smNoneYet;
  114. // On Windows Vista, native GetNextItem for selection stops working once we
  115. // disallow deselecting any item (see ExCanChange).
  116. // So we need to manage selection state ourselves
  117. // cannot use Win32MajorVersion as it is affected by compatibility mode and
  118. // the bug is present even in compatibility mode
  119. FManageSelection := IsVistaHard;
  120. FFocused := 0;
  121. FIgnoreSetFocusFrom := INVALID_HANDLE_VALUE;
  122. // On Windows 7 we have to force item update when it looses focus,
  123. // otherwise some remnants of focus rectangle remain
  124. // Doing the same on WinXP makes list view down from the item flicker,
  125. // so we avoid this there.
  126. // Not sure about Vista
  127. FForceUpdateOnItemUnfocus := IsWin7;
  128. FNextCharToIgnore := 0;
  129. end;
  130. destructor TCustomNortonLikeListView.Destroy;
  131. begin
  132. FColProperties.Free;
  133. inherited;
  134. end;
  135. procedure TCustomNortonLikeListView.ItemSelected(Item: TListItem; Index: Integer);
  136. begin
  137. Inc(FSelCount);
  138. if FSelectingImplicitly and (FSelCount = 1) then
  139. begin
  140. FAnyAndAllSelectedImplicitly := True;
  141. end
  142. else
  143. if not FSelectingImplicitly then
  144. begin
  145. FAnyAndAllSelectedImplicitly := False;
  146. end;
  147. if FManageSelection then
  148. begin
  149. if Index < 0 then
  150. Index := Item.Index;
  151. if FSelCount = 1 then
  152. begin
  153. Assert(FFirstSelected < 0);
  154. FFirstSelected := Index;
  155. Assert(FLastSelected < 0);
  156. FLastSelected := Index;
  157. end
  158. else
  159. begin
  160. // if reference is not assigned, do not assign it as we
  161. // cannot be sure that the item is actually first/last
  162. if (FFirstSelected >= 0) and (Index < FFirstSelected) then
  163. FFirstSelected := Index;
  164. if (FLastSelected >= 0) and (Index > FLastSelected) then
  165. FLastSelected := Index;
  166. end;
  167. end;
  168. end;
  169. procedure TCustomNortonLikeListView.ItemUnselected(Item: TListItem; Index: Integer);
  170. begin
  171. Dec(FSelCount);
  172. if (FSelCount = 0) or (not FSelectingImplicitly) then
  173. begin
  174. FAnyAndAllSelectedImplicitly := False;
  175. end;
  176. if FManageSelection then
  177. begin
  178. if Index < 0 then
  179. Index := Item.Index;
  180. if FFirstSelected = Index then
  181. begin
  182. if FSelCount = 1 then
  183. FFirstSelected := FLastSelected // may be -1
  184. else
  185. FFirstSelected := -1;
  186. end;
  187. if FLastSelected = Index then
  188. begin
  189. if FSelCount = 1 then
  190. FLastSelected := FFirstSelected // may be -1
  191. else
  192. FLastSelected := -1;
  193. end;
  194. end;
  195. end;
  196. procedure TCustomNortonLikeListView.Delete(Item: TListItem);
  197. var
  198. Index: Integer;
  199. begin
  200. if (FLastDeletedItem <> Item) and (not FClearingItems) then
  201. begin
  202. Index := Item.Index;
  203. if Item.Selected then
  204. ItemUnselected(Item, Index);
  205. if FManageSelection then
  206. begin
  207. if (FLastSelected >= 0) and (Index <= FLastSelected) then
  208. Dec(FLastSelected);
  209. if (FFirstSelected >= 0) and (Index <= FFirstSelected) then
  210. Dec(FFirstSelected);
  211. end;
  212. end;
  213. FLastDeletedItem := Item;
  214. inherited;
  215. FLastDeletedItem := nil;
  216. end;
  217. function TCustomNortonLikeListView.ExCanChange(Item: TListItem; Change: Integer;
  218. NewState, OldState: Word): Boolean;
  219. begin
  220. Assert(Assigned(Item));
  221. Result := True;
  222. if (Change = LVIF_STATE) and
  223. ((((OldState and LVIS_SELECTED) < (NewState and LVIS_SELECTED)) and
  224. (FDontSelectItem or (not CanChangeSelection(Item, True)))) or
  225. (((OldState and LVIS_SELECTED) > (NewState and LVIS_SELECTED)) and
  226. (FDontUnSelectItem or (not CanChangeSelection(Item, False))))) then
  227. begin
  228. if (OldState or LVIS_SELECTED) <> (NewState or LVIS_SELECTED) then
  229. begin
  230. ListView_SetItemState(Handle, Item.Index, NewState,
  231. (NewState or OldState) - LVIS_SELECTED);
  232. end;
  233. Result := False;
  234. end;
  235. end;
  236. function TCustomNortonLikeListView.CanChangeSelection(Item: TListItem;
  237. Select: Boolean): Boolean;
  238. begin
  239. Result := True;
  240. end;
  241. procedure TCustomNortonLikeListView.ClearItems;
  242. begin
  243. Items.BeginUpdate;
  244. try
  245. FClearingItems := True;
  246. Items.Clear;
  247. finally
  248. FSelCount := 0;
  249. if FManageSelection then
  250. begin
  251. FFirstSelected := -1;
  252. FLastSelected := -1;
  253. end;
  254. FClearingItems := False;
  255. Items.EndUpdate;
  256. end;
  257. end; { ClearItems }
  258. procedure TCustomNortonLikeListView.ItemsReordered;
  259. begin
  260. if FManageSelection then
  261. begin
  262. FFirstSelected := -1;
  263. FLastSelected := -1;
  264. end;
  265. end;
  266. function TCustomNortonLikeListView.ClosestUnselected(Item: TListItem): TListItem;
  267. var
  268. Index: Integer;
  269. begin
  270. if Assigned(Item) and (Item.Selected or ((NortonLike <> nlOff) and (SelCount = 0))) then
  271. begin
  272. Index := Item.Index + 1;
  273. while (Index < Items.Count) and Items[Index].Selected do Inc(Index);
  274. if (Index >= Items.Count) or Items[Index].Selected then
  275. begin
  276. Index := Item.Index - 1;
  277. while (Index >= 0) and Items[Index].Selected do Dec(Index);
  278. end;
  279. if (Index >= 0) and (Index < Items.Count) and (not Items[Index].Selected) then
  280. Result := Items[Index]
  281. else
  282. Result := nil;
  283. end
  284. else Result := Item;
  285. end;
  286. function TCustomNortonLikeListView.GetPopupMenu: TPopupMenu;
  287. begin
  288. // While editing pretend that we do not have a popup menu.
  289. // Otherwise Ctrl+V is swallowed by the TWinControl.CNKeyDown,
  290. // when it finds out (TWinControl.IsMenuKey) that there's a command with Ctrl+V shortcut in the list view context menu
  291. // (the "paste" file action)
  292. if IsEditing then
  293. begin
  294. Result := nil;
  295. end
  296. else
  297. begin
  298. Result := inherited;
  299. end;
  300. end;
  301. procedure TCustomNortonLikeListView.WMNotify(var Message: TWMNotify);
  302. var
  303. HDNotify: PHDNotify;
  304. begin
  305. if (FHeaderHandle <> 0) and (Message.NMHdr^.hWndFrom = FHeaderHandle) then
  306. begin
  307. HDNotify := PHDNotify(Message.NMHdr);
  308. // Disallow resizing of "invisible" (width=0) columns.
  309. // (We probably get only Unicode versions of the messages here as
  310. // controls are created as Unicode by VCL)
  311. case HDNotify.Hdr.code of
  312. HDN_BEGINTRACKA, HDN_TRACKA, HDN_BEGINTRACKW, HDN_TRACKW:
  313. if not ColProperties.Visible[HDNotify.Item] then
  314. begin
  315. Message.Result := 1;
  316. Exit;
  317. end;
  318. // We won't get here when user tries to resize the column by mouse,
  319. // as that's prevented above.
  320. // But we get here when other methods are used
  321. // (the only we know about atm is Ctrl-+ shortcut)
  322. // We are getting this notification also when control is being setup,
  323. // with mask including also other fields, not just HDI_WIDTH.
  324. // While it does not seem to hurt to swallow even those messages,
  325. // not sure it's good thing to do, so we swallow width-only messages only.
  326. // That's why there's "= HDI_WIDTH" not "and HDI_WIDTH <> 0".
  327. HDN_ITEMCHANGINGA, HDN_ITEMCHANGINGW:
  328. if (HDNotify.PItem.Mask = HDI_WIDTH) and
  329. (HDNotify.PItem.cxy <> 0) and
  330. (not ColProperties.Visible[HDNotify.Item]) then
  331. begin
  332. Message.Result := 1;
  333. Exit;
  334. end;
  335. end;
  336. end;
  337. inherited;
  338. end;
  339. procedure TCustomNortonLikeListView.DDBeforeDrag;
  340. begin
  341. FDontSelectItem := False;
  342. FDontUnSelectItem := False;
  343. end;
  344. procedure TCustomNortonLikeListView.CNNotify(var Message: TWMNotify);
  345. var
  346. Item: TListItem;
  347. begin
  348. with Message do
  349. case NMHdr^.code of
  350. LVN_ITEMCHANGING:
  351. with PNMListView(NMHdr)^ do
  352. begin
  353. Item := Items[iItem];
  354. if Valid and (not FClearingItems) and (Item <> FLastDeletedItem) and
  355. ((not CanChange(Item, uChanged)) or
  356. (not ExCanChange(Item, uChanged, uNewState, uOldState)))
  357. then
  358. begin
  359. Result := 1;
  360. end;
  361. end;
  362. LVN_ITEMCHANGED:
  363. begin
  364. with PNMListView(NMHdr)^ do
  365. begin
  366. Item := Items[iItem];
  367. if Valid and (not FClearingItems) and
  368. (uChanged = LVIF_STATE) and (Item <> FLastDeletedItem) then
  369. begin
  370. if FForceUpdateOnItemUnfocus and
  371. (NortonLike <> nlOff) and
  372. ((uOldState and LVIS_FOCUSED) > (uNewState and LVIS_FOCUSED)) then
  373. begin
  374. // force update, otherwise some remnants of focus rectangle remain
  375. Item.Update;
  376. end;
  377. if (uOldState and LVIS_SELECTED) <> (uNewState and LVIS_SELECTED) then
  378. begin
  379. if (uOldState and LVIS_SELECTED) <> 0 then
  380. begin
  381. ItemUnselected(Item, iItem);
  382. end
  383. else
  384. begin
  385. ItemSelected(Item, iItem);
  386. end;
  387. end;
  388. end;
  389. end;
  390. inherited;
  391. end;
  392. LVN_ENDLABELEDIT:
  393. begin
  394. FIgnoreSetFocusFrom := ListView_GetEditControl(Handle);
  395. inherited;
  396. end;
  397. else
  398. begin
  399. inherited;
  400. end;
  401. end;
  402. end;
  403. procedure TCustomNortonLikeListView.SelectCurrentItem(FocusNext: Boolean);
  404. var
  405. Item: TListItem;
  406. begin
  407. Item := ItemFocused;
  408. if Item = nil then Item := Items[0];
  409. Item.Selected := not Item.Selected;
  410. if FocusNext then
  411. begin
  412. SendMessage(Handle, WM_KEYDOWN, VK_DOWN, LongInt(0));
  413. end;
  414. end;
  415. procedure TCustomNortonLikeListView.WMKeyDown(var Message: TWMKeyDown);
  416. var
  417. PLastSelectMethod: TSelectMethod;
  418. PDontUnSelectItem: Boolean;
  419. PDontSelectItem: Boolean;
  420. begin
  421. FNextCharToIgnore := 0;
  422. if (NortonLike <> nlOff) and (Message.CharCode = VK_INSERT) then
  423. begin
  424. if Items.Count > 0 then
  425. begin
  426. PLastSelectMethod := FLastSelectMethod;
  427. FLastSelectMethod := smKeyboard;
  428. try
  429. SelectCurrentItem(True);
  430. finally
  431. FLastSelectMethod := PLastSelectMethod;
  432. end;
  433. Message.Result := 1;
  434. end;
  435. end
  436. else
  437. if Message.CharCode = VK_ADD then
  438. begin
  439. FNextCharToIgnore := Word('+');
  440. inherited;
  441. end
  442. else
  443. if Message.CharCode = VK_SUBTRACT then
  444. begin
  445. FNextCharToIgnore := Word('-');
  446. inherited;
  447. end
  448. else
  449. if Message.CharCode = VK_MULTIPLY then
  450. begin
  451. FNextCharToIgnore := Word('*');
  452. inherited;
  453. end
  454. else
  455. if (NortonLike <> nlOff) and (Message.CharCode in [VK_LEFT, VK_RIGHT]) and
  456. (ViewStyle = vsReport) and
  457. ((GetWindowLong(Handle, GWL_STYLE) and WS_HSCROLL) = 0) then
  458. begin
  459. if Items.Count > 0 then
  460. begin
  461. // do not focus item directly to make later selecting work
  462. if Message.CharCode = VK_LEFT then
  463. SendMessage(Handle, WM_KEYDOWN, VK_HOME, LongInt(0))
  464. else
  465. SendMessage(Handle, WM_KEYDOWN, VK_END, LongInt(0));
  466. end;
  467. Message.Result := 1;
  468. end
  469. else
  470. if (NortonLike <> nlOff) and (Message.CharCode = VK_SPACE) and
  471. ((KeyDataToShiftState(Message.KeyData) * [ssCtrl]) <> []) then
  472. begin
  473. // prevent Ctrl+Space landing in else branch below,
  474. // this can safely get processed by default handler as Ctrl+Space
  475. // toggles only focused item, not affecting others
  476. PLastSelectMethod := FLastSelectMethod;
  477. FLastSelectMethod := smKeyboard;
  478. try
  479. inherited;
  480. finally
  481. FLastSelectMethod := PLastSelectMethod;
  482. end;
  483. end
  484. else
  485. if (Message.CharCode in [VK_SPACE, VK_PRIOR, VK_NEXT, VK_END, VK_HOME, VK_LEFT,
  486. VK_UP, VK_RIGHT, VK_DOWN, VK_SELECT]) then
  487. begin
  488. PLastSelectMethod := FLastSelectMethod;
  489. PDontSelectItem := FDontSelectItem;
  490. PDontUnSelectItem := FDontUnSelectItem;
  491. FLastSelectMethod := smKeyboard;
  492. FDontSelectItem := FDontSelectItem or
  493. ((NortonLike <> nlOff) and
  494. ((KeyDataToShiftState(Message.KeyData) * [ssShift]) = []));
  495. // Note that Space (selecting toggling) is processed by default handler for WM_CHAR,
  496. // otherwise the below condition would prevent unselection
  497. FDontUnSelectItem :=
  498. FDontUnSelectItem or
  499. (NortonLike = nlOn) or
  500. ((NortonLike = nlKeyboard) and (not FAnyAndAllSelectedImplicitly));
  501. try
  502. inherited;
  503. finally
  504. FDontSelectItem := PDontSelectItem;
  505. FDontUnSelectItem := PDontUnSelectItem;
  506. FLastSelectMethod := PLastSelectMethod;
  507. end;
  508. end
  509. else inherited;
  510. end;
  511. procedure TCustomNortonLikeListView.WMSysCommand(var Message: TWMSysCommand);
  512. begin
  513. // Ugly workaround to avoid Windows beeping when Alt+Grey +/- are pressed
  514. // (for (Us)Select File with Same Ext commands)
  515. if (Message.CmdType = SC_KEYMENU) and
  516. ((Message.Key = Word('+')) or (Message.Key = Word('-'))) then
  517. begin
  518. Message.Result := 1;
  519. end
  520. else inherited;
  521. end;
  522. procedure TCustomNortonLikeListView.WMChar(var Message: TWMChar);
  523. var
  524. PLastSelectMethod: TSelectMethod;
  525. PDontUnSelectItem: Boolean;
  526. PDontSelectItem: Boolean;
  527. begin
  528. if Message.CharCode = FNextCharToIgnore then
  529. begin
  530. // ugly fix to avoid Windows beeping when these keys are processed by
  531. // WMKeyDown instead of here (WMChar)
  532. Message.Result := 1;
  533. end
  534. else
  535. if (NortonLike <> nlOff) and (Message.CharCode = Byte(' ')) then
  536. begin
  537. if (GetKeyState(VK_CONTROL) >= 0) then
  538. begin
  539. if Assigned(ItemFocused) then
  540. ItemFocused.Selected := not ItemFocused.Selected;
  541. end
  542. else inherited;
  543. end
  544. else
  545. begin
  546. PLastSelectMethod := FLastSelectMethod;
  547. PDontSelectItem := FDontSelectItem;
  548. PDontUnSelectItem := FDontUnSelectItem;
  549. FDontSelectItem := FDontSelectItem or (NortonLike <> nlOff);
  550. FLastSelectMethod := smKeyboard;
  551. FDontUnSelectItem :=
  552. FDontUnSelectItem or
  553. (NortonLike = nlOn) or
  554. ((NortonLike = nlKeyboard) and (not FAnyAndAllSelectedImplicitly));
  555. try
  556. inherited;
  557. finally
  558. FLastSelectMethod := PLastSelectMethod;
  559. FDontSelectItem := PDontSelectItem;
  560. FDontUnSelectItem := PDontUnSelectItem;
  561. end;
  562. end;
  563. FNextCharToIgnore := 0;
  564. end;
  565. procedure TCustomNortonLikeListView.FocusSomething;
  566. begin
  567. if Valid and (Items.Count > 0) and not Assigned(ItemFocused) then
  568. begin
  569. if (NortonLike <> nlOff) then SendMessage(Handle, WM_KEYDOWN, VK_DOWN, LongInt(0));
  570. if not Assigned(ItemFocused) then
  571. ItemFocused := Items[0];
  572. end;
  573. if Assigned(ItemFocused) then
  574. ItemFocused.MakeVisible(False);
  575. end;
  576. function TCustomNortonLikeListView.EnableDragOnClick: Boolean;
  577. begin
  578. Result := (not FFocusingItem);
  579. end;
  580. procedure TCustomNortonLikeListView.FocusItem(Item: TListItem);
  581. begin
  582. // This method was introduced in 3.7.6 for reasons long forgotten.
  583. // It simulated a mouse click on the item. Possibly because the mere ItemFocused := Item
  584. // did not work due to at-the-time-not-realized conflict with FocusSomething.
  585. // Now that this is fixed, the method is no longer needed.
  586. // Keeping it for a while with this comment, in case some regression is discovered.
  587. // References:
  588. // - 3.7.6: When reloading directory content, file panel tries to preserve its position.
  589. // - Bugs 999 and 1161
  590. ItemFocused := Item;
  591. end;
  592. procedure TCustomNortonLikeListView.SelectAll(Mode: TSelectMode; Exclude: TListItem);
  593. var
  594. Index: Integer;
  595. Item: TListItem;
  596. begin
  597. BeginSelectionUpdate;
  598. try
  599. for Index := 0 to Items.Count - 1 do
  600. begin
  601. Item := Items[Index];
  602. if Item <> Exclude then
  603. begin
  604. case Mode of
  605. smAll: Item.Selected := True;
  606. smNone: Item.Selected := False;
  607. smInvert: Item.Selected := not Item.Selected;
  608. end;
  609. end;
  610. end;
  611. finally
  612. EndSelectionUpdate;
  613. end;
  614. end;
  615. procedure TCustomNortonLikeListView.SelectAll(Mode: TSelectMode);
  616. begin
  617. SelectAll(Mode, nil);
  618. end;
  619. procedure TCustomNortonLikeListView.WMLButtonDown(var Message: TWMLButtonDown);
  620. var
  621. PLastSelectMethod: TSelectMethod;
  622. PDontUnSelectItem: Boolean;
  623. PDontSelectItem: Boolean;
  624. PSelectingImplicitly: Boolean;
  625. SelectingImplicitly: Boolean;
  626. Shift: TShiftState;
  627. Item: TListItem;
  628. begin
  629. Shift := KeysToShiftState(Message.Keys);
  630. PLastSelectMethod := FLastSelectMethod;
  631. PDontSelectItem := FDontSelectItem;
  632. PDontUnSelectItem := FDontUnSelectItem;
  633. PSelectingImplicitly := FSelectingImplicitly;
  634. FLastSelectMethod := smMouse;
  635. FDontSelectItem := FDontSelectItem or ((NortonLike = nlOn) and ((Shift * [ssCtrl, ssShift]) = []));
  636. FDontUnSelectItem := FDontUnSelectItem or ((NortonLike = nlOn) and ((Shift * [ssCtrl]) = []));
  637. SelectingImplicitly := ((Shift * [ssCtrl, ssShift]) = []);
  638. if SelectingImplicitly and (NortonLike = nlKeyboard) then
  639. begin
  640. // in general, when clicking, we clear selection only after mouse button is released,
  641. // from within WMLButtonUp, so we know we are not starting dragging,
  642. // so we do not want to clear the selection.
  643. // on the other hand, when clicking outside of the selection,
  644. // we want to explicitly clear the selection, no matter what
  645. Item := GetItemAt(Message.XPos, Message.YPos);
  646. if (Item = nil) or (not Item.Selected) then
  647. SelectAll(smNone);
  648. end;
  649. FSelectingImplicitly := FSelectingImplicitly or SelectingImplicitly;
  650. FLButtonDownShiftState := Shift;
  651. FLButtonDownPos := Point(Message.XPos, Message.YPos);
  652. try
  653. inherited;
  654. finally
  655. FLastSelectMethod := PLastSelectMethod;
  656. FDontSelectItem := PDontSelectItem;
  657. FDontUnSelectItem := PDontUnSelectItem;
  658. FSelectingImplicitly := PSelectingImplicitly;
  659. end;
  660. end;
  661. procedure TCustomNortonLikeListView.WMRButtonDown(var Message: TWMRButtonDown);
  662. var
  663. PLastSelectMethod: TSelectMethod;
  664. PDontUnSelectItem: Boolean;
  665. PDontSelectItem: Boolean;
  666. PSelectingImplicitly: Boolean;
  667. SelectingImplicitly: Boolean;
  668. Shift: TShiftState;
  669. begin
  670. Shift := KeysToShiftState(Message.Keys);
  671. PLastSelectMethod := FLastSelectMethod;
  672. PDontSelectItem := FDontSelectItem;
  673. PDontUnSelectItem := FDontUnSelectItem;
  674. PSelectingImplicitly := FSelectingImplicitly;
  675. FLastSelectMethod := smMouse;
  676. FDontSelectItem := FDontSelectItem or (NortonLike = nlOn);
  677. FDontUnSelectItem := FDontUnSelectItem or (NortonLike = nlOn);
  678. SelectingImplicitly := ((Shift * [ssCtrl, ssShift]) = []);
  679. // TODO unselect all when clicking outside of selection
  680. // (is not done automatically when focused item is not selected)
  681. FSelectingImplicitly := FSelectingImplicitly or SelectingImplicitly;
  682. try
  683. inherited;
  684. finally
  685. FLastSelectMethod := PLastSelectMethod;
  686. FDontSelectItem := PDontSelectItem;
  687. FDontUnSelectItem := PDontUnSelectItem;
  688. FSelectingImplicitly := PSelectingImplicitly;
  689. end;
  690. end;
  691. procedure TCustomNortonLikeListView.WMLButtonUp(var Message: TWMLButtonUp);
  692. var
  693. SelectingImplicitly: Boolean;
  694. Shift: TShiftState;
  695. begin
  696. // Workaround
  697. // For some reason Message.Keys is always 0 here,
  698. // so we use shift state from the LButtonDown as a workaround
  699. Shift := KeysToShiftState(Message.Keys);
  700. SelectingImplicitly :=
  701. ((Shift * [ssCtrl, ssShift]) = []) and
  702. ((FLButtonDownShiftState * [ssCtrl, ssShift]) = []);
  703. if SelectingImplicitly and (csClicked in ControlState) and
  704. (Abs(FLButtonDownPos.X - Message.XPos) <= 4) and
  705. (Abs(FLButtonDownPos.Y - Message.YPos) <= 4) then
  706. begin
  707. SelectAll(smNone, ItemFocused);
  708. // Because condition in ItemSelected is not triggered as we first select
  709. // the new item and then unselect the previous.
  710. // This probably means that we can get rid of the code in ItemSelected.
  711. FAnyAndAllSelectedImplicitly := True;
  712. end;
  713. inherited;
  714. end;
  715. function TCustomNortonLikeListView.GetMarkedFile: TListItem;
  716. begin
  717. if Assigned(Selected) then Result := Selected
  718. else
  719. if Assigned(ItemFocused) and (NortonLike <> nlOff) then Result := ItemFocused
  720. else Result := nil;
  721. end;
  722. function TCustomNortonLikeListView.GetNextItem(StartItem: TListItem;
  723. Direction: TSearchDirection; States: TItemStates): TListItem;
  724. var
  725. Start, Index, First, Last: Integer;
  726. begin
  727. if not FManageSelection then
  728. begin
  729. Result := inherited GetNextItem(StartItem, Direction, States);
  730. end
  731. else
  732. begin
  733. Assert(Direction = sdAll);
  734. if States = [isSelected] then
  735. begin
  736. if FSelCount = 0 then
  737. begin
  738. Result := nil
  739. end
  740. else
  741. if (not Assigned(StartItem)) and (FFirstSelected >= 0) then
  742. begin
  743. Result := Items[FFirstSelected]
  744. end
  745. else
  746. begin
  747. if Assigned(StartItem) then
  748. Start := StartItem.Index
  749. else
  750. Start := -1;
  751. if (FFirstSelected >= 0) and (Start < FFirstSelected) then
  752. First := FFirstSelected
  753. else
  754. First := Start + 1;
  755. if FLastSelected >= 0 then
  756. Last := FLastSelected
  757. else
  758. Last := Items.Count - 1;
  759. if Start > Last then
  760. begin
  761. Result := nil;
  762. end
  763. else
  764. begin
  765. Index := First;
  766. while (Index <= Last) and (not (Items[Index].Selected)) do
  767. begin
  768. Inc(Index);
  769. end;
  770. if Index > Last then
  771. begin
  772. Result := nil;
  773. if Assigned(StartItem) and StartItem.Selected then
  774. begin
  775. Assert((FLastSelected < 0) or (FLastSelected = Start));
  776. FLastSelected := Start;
  777. end;
  778. end
  779. else
  780. begin
  781. Result := Items[Index];
  782. Assert(Result.Selected);
  783. if not Assigned(StartItem) then
  784. begin
  785. Assert((FFirstSelected < 0) or (FFirstSelected = Index));
  786. FFirstSelected := Index;
  787. end;
  788. end;
  789. end;
  790. end;
  791. end
  792. else
  793. if States = [isCut] then
  794. begin
  795. Result := inherited GetNextItem(StartItem, Direction, States);
  796. end
  797. else
  798. begin
  799. Assert(False);
  800. Result := nil;
  801. end;
  802. end;
  803. end;
  804. function TCustomNortonLikeListView.GetSelCount: Integer;
  805. begin
  806. Result := FSelCount;
  807. end;
  808. procedure TCustomNortonLikeListView.InsertItem(Item: TListItem);
  809. begin
  810. inherited;
  811. if Item.Selected then
  812. ItemSelected(Item, -1);
  813. end;
  814. function TCustomNortonLikeListView.GetItemFromHItem(const Item: TLVItem): TListItem;
  815. begin
  816. with Item do
  817. if (state and LVIF_PARAM) <> 0 then Result := Pointer(lParam)
  818. else Result := Items[iItem];
  819. end;
  820. function TCustomNortonLikeListView.GetMarkedCount: Integer;
  821. begin
  822. if (SelCount > 0) or (NortonLike = nlOff) then Result := SelCount
  823. else
  824. if Assigned(ItemFocused) then Result := 1
  825. else Result := 0;
  826. end;
  827. function TCustomNortonLikeListView.GetValid: Boolean;
  828. begin
  829. // Note that TCustomDirView::GetValid don't inherit
  830. // this method because of optimalization
  831. Result := (not (csDestroying in ComponentState)) and (not FClearingItems);
  832. end;
  833. procedure TCustomNortonLikeListView.BeginSelectionUpdate;
  834. begin
  835. // Higher value is probably some nesting error
  836. Assert(FUpdatingSelection in [0..4]);
  837. Inc(FUpdatingSelection);
  838. end; { BeginUpdatingSelection }
  839. procedure TCustomNortonLikeListView.EndSelectionUpdate;
  840. begin
  841. Assert(FUpdatingSelection > 0);
  842. Dec(FUpdatingSelection);
  843. end; { EndUpdatingSelection }
  844. procedure TCustomNortonLikeListView.CreateWnd;
  845. begin
  846. try
  847. Assert(ColProperties <> nil);
  848. inherited;
  849. FHeaderHandle := ListView_GetHeader(Handle);
  850. ColProperties.ListViewWndCreated;
  851. finally
  852. end;
  853. end;
  854. procedure TCustomNortonLikeListView.DestroyWnd;
  855. begin
  856. ColProperties.ListViewWndDestroying;
  857. try
  858. inherited;
  859. finally
  860. ColProperties.ListViewWndDestroyed;
  861. end;
  862. end;
  863. procedure TCustomNortonLikeListView.LVMEditLabel(var Message: TMessage);
  864. begin
  865. // explicitly requesting editing (e.g. F2),
  866. // so we do not care anymore when the view was focused
  867. FFocused := 0;
  868. inherited;
  869. end;
  870. function TCustomNortonLikeListView.CanEdit(Item: TListItem): Boolean;
  871. var
  872. N: TDateTime;
  873. Delta: Double;
  874. begin
  875. N := Now;
  876. Result := inherited CanEdit(Item);
  877. if Result and (FFocused > 0) then
  878. begin
  879. Delta := N - FFocused;
  880. // it takes little more than 500ms to trigger editing after click
  881. Result := Delta > (750.0/MSecsPerDay);
  882. end;
  883. FFocused := 0;
  884. end;
  885. procedure TCustomNortonLikeListView.WMSetFocus(var Message: TWMSetFocus);
  886. begin
  887. inherited;
  888. if Message.FocusedWnd <> FIgnoreSetFocusFrom then
  889. FFocused := Now;
  890. end;
  891. procedure TCustomNortonLikeListView.CMWantSpecialKey(var Message: TCMWantSpecialKey);
  892. begin
  893. inherited;
  894. if IsEditing and (Message.CharCode = VK_TAB) then
  895. Message.Result := 1;
  896. end;
  897. procedure TCustomNortonLikeListView.MakeProgressVisible(Item: TListItem);
  898. var
  899. DisplayRect: TRect;
  900. begin
  901. if ViewStyle = vsReport then
  902. begin
  903. DisplayRect := Item.DisplayRect(drBounds);
  904. if DisplayRect.Bottom > ClientHeight then
  905. begin
  906. Scroll(0, Item.Top - TopItem.Top);
  907. end;
  908. end;
  909. Item.MakeVisible(False);
  910. end;
  911. procedure TCustomNortonLikeListView.ChangeScale(M, D: Integer);
  912. begin
  913. if M <> D then
  914. begin
  915. // When font is scaled, while the control is being re-created, previous font is restored once
  916. // read from the persistence data in TCustomListView.CreateWnd.
  917. // Requiring handle, makes sure the re-create phase is closed.
  918. // We could limit impact by checking ControlHasRecreationPersistenceData,
  919. // but for now, we actually prefer larger impact to test this change better.
  920. HandleNeeded;
  921. end;
  922. inherited;
  923. end;
  924. end.