NortonLikeListView.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. function GetItemFromHItem(const Item: TLVItem): TListItem;
  69. function GetValid: Boolean; virtual;
  70. function GetSelCount: Integer; override;
  71. procedure DDBeforeDrag;
  72. function CanEdit(Item: TListItem): Boolean; override;
  73. function GetPopupMenu: TPopupMenu; override;
  74. procedure ChangeScale(M, D: Integer); override;
  75. public
  76. { Public declarations }
  77. constructor Create(AOwner: TComponent); override;
  78. destructor Destroy; override;
  79. function ClosestUnselected(Item: TListItem): TListItem;
  80. procedure SelectAll(Mode: TSelectMode); reintroduce; overload;
  81. procedure SelectCurrentItem(FocusNext: Boolean);
  82. function GetNextItem(StartItem: TListItem; Direction: TSearchDirection;
  83. States: TItemStates): TListItem;
  84. procedure MakeProgressVisible(Item: TListItem);
  85. procedure FocusItem(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 not handled in TCustomScpExplorerForm::DirViewKeyPress
  540. if not DoKeyPress(Message) then
  541. begin
  542. if Assigned(ItemFocused) then
  543. ItemFocused.Selected := not ItemFocused.Selected;
  544. end;
  545. end
  546. else inherited;
  547. end
  548. else
  549. begin
  550. PLastSelectMethod := FLastSelectMethod;
  551. PDontSelectItem := FDontSelectItem;
  552. PDontUnSelectItem := FDontUnSelectItem;
  553. FDontSelectItem := FDontSelectItem or (NortonLike <> nlOff);
  554. FLastSelectMethod := smKeyboard;
  555. FDontUnSelectItem :=
  556. FDontUnSelectItem or
  557. (NortonLike = nlOn) or
  558. ((NortonLike = nlKeyboard) and (not FAnyAndAllSelectedImplicitly));
  559. try
  560. inherited;
  561. finally
  562. FLastSelectMethod := PLastSelectMethod;
  563. FDontSelectItem := PDontSelectItem;
  564. FDontUnSelectItem := PDontUnSelectItem;
  565. end;
  566. end;
  567. FNextCharToIgnore := 0;
  568. end;
  569. procedure TCustomNortonLikeListView.FocusSomething;
  570. begin
  571. if Valid and (Items.Count > 0) and not Assigned(ItemFocused) then
  572. begin
  573. if (NortonLike <> nlOff) then
  574. begin
  575. SendMessage(Handle, WM_KEYDOWN, VK_DOWN, LongInt(0));
  576. end;
  577. if not Assigned(ItemFocused) then
  578. begin
  579. ItemFocused := Items[0];
  580. end;
  581. end;
  582. if Assigned(ItemFocused) then
  583. begin
  584. ItemFocused.MakeVisible(False);
  585. end;
  586. end;
  587. function TCustomNortonLikeListView.EnableDragOnClick: Boolean;
  588. begin
  589. Result := (not FFocusingItem);
  590. end;
  591. procedure TCustomNortonLikeListView.FocusItem(Item: TListItem);
  592. var
  593. P: TPoint;
  594. PLastSelectMethod: TSelectMethod;
  595. PDontUnSelectItem: Boolean;
  596. PDontSelectItem: Boolean;
  597. WParam: UINT_PTR;
  598. LParam: INT_PTR;
  599. begin
  600. // This whole is replacement for mere ItemFocused := Item
  601. // because that does not reset some internal focused pointer,
  602. // causing subsequent Shift-Click selects range from the first item,
  603. // not from focused item.
  604. Item.MakeVisible(False);
  605. Assert(Focused);
  606. if Focused then
  607. begin
  608. P := Item.GetPosition;
  609. PLastSelectMethod := FLastSelectMethod;
  610. PDontSelectItem := FDontSelectItem;
  611. PDontUnSelectItem := FDontUnSelectItem;
  612. FLastSelectMethod := smNoneYet;
  613. FDontSelectItem := True;
  614. FDontUnSelectItem := True;
  615. FFocusingItem := True;
  616. try
  617. // HACK
  618. // WM_LBUTTONDOWN enters loop, waiting for WM_LBUTTONUP,
  619. // so we have to post it in advance to break the loop immediately
  620. // Without MK_CONTROL, if there are more items selected,
  621. // they won't get unselected on subsequent focus change
  622. // (with explorer-style selection).
  623. // And it also makes the click the least obtrusive, affecting the focused
  624. // file only.
  625. WParam := MK_LBUTTON or MK_CONTROL;
  626. LParam := MAKELPARAM(P.X, P.Y);
  627. PostMessage(Handle, WM_LBUTTONUP, WParam, LParam);
  628. SendMessage(Handle, WM_LBUTTONDOWN, WParam, LParam);
  629. finally
  630. FFocusingItem := False;
  631. FLastSelectMethod := PLastSelectMethod;
  632. FDontSelectItem := PDontSelectItem;
  633. FDontUnSelectItem := PDontUnSelectItem;
  634. end;
  635. end;
  636. if ItemFocused <> Item then
  637. ItemFocused := Item;
  638. end;
  639. procedure TCustomNortonLikeListView.SelectAll(Mode: TSelectMode; Exclude: TListItem);
  640. var
  641. Index: Integer;
  642. Item: TListItem;
  643. begin
  644. BeginSelectionUpdate;
  645. try
  646. for Index := 0 to Items.Count - 1 do
  647. begin
  648. Item := Items[Index];
  649. if Item <> Exclude then
  650. begin
  651. case Mode of
  652. smAll: Item.Selected := True;
  653. smNone: Item.Selected := False;
  654. smInvert: Item.Selected := not Item.Selected;
  655. end;
  656. end;
  657. end;
  658. finally
  659. EndSelectionUpdate;
  660. end;
  661. end;
  662. procedure TCustomNortonLikeListView.SelectAll(Mode: TSelectMode);
  663. begin
  664. SelectAll(Mode, nil);
  665. end;
  666. procedure TCustomNortonLikeListView.WMLButtonDown(var Message: TWMLButtonDown);
  667. var
  668. PLastSelectMethod: TSelectMethod;
  669. PDontUnSelectItem: Boolean;
  670. PDontSelectItem: Boolean;
  671. PSelectingImplicitly: Boolean;
  672. SelectingImplicitly: Boolean;
  673. Shift: TShiftState;
  674. Item: TListItem;
  675. begin
  676. Shift := KeysToShiftState(Message.Keys);
  677. PLastSelectMethod := FLastSelectMethod;
  678. PDontSelectItem := FDontSelectItem;
  679. PDontUnSelectItem := FDontUnSelectItem;
  680. PSelectingImplicitly := FSelectingImplicitly;
  681. FLastSelectMethod := smMouse;
  682. FDontSelectItem := FDontSelectItem or ((NortonLike = nlOn) and ((Shift * [ssCtrl, ssShift]) = []));
  683. FDontUnSelectItem := FDontUnSelectItem or ((NortonLike = nlOn) and ((Shift * [ssCtrl]) = []));
  684. SelectingImplicitly := ((Shift * [ssCtrl, ssShift]) = []);
  685. if SelectingImplicitly and (NortonLike = nlKeyboard) then
  686. begin
  687. // in general, when clicking, we clear selection only after mouse button is released,
  688. // from within WMLButtonUp, so we know we are not starting dragging,
  689. // so we do not want to clear the selection.
  690. // on the other hand, when clicking outside of the selection,
  691. // we want to explicitly clear the selection, no matter what
  692. Item := GetItemAt(Message.XPos, Message.YPos);
  693. if (Item = nil) or (not Item.Selected) then
  694. SelectAll(smNone);
  695. end;
  696. FSelectingImplicitly := FSelectingImplicitly or SelectingImplicitly;
  697. FLButtonDownShiftState := Shift;
  698. FLButtonDownPos := Point(Message.XPos, Message.YPos);
  699. try
  700. inherited;
  701. finally
  702. FLastSelectMethod := PLastSelectMethod;
  703. FDontSelectItem := PDontSelectItem;
  704. FDontUnSelectItem := PDontUnSelectItem;
  705. FSelectingImplicitly := PSelectingImplicitly;
  706. end;
  707. end;
  708. procedure TCustomNortonLikeListView.WMRButtonDown(var Message: TWMRButtonDown);
  709. var
  710. PLastSelectMethod: TSelectMethod;
  711. PDontUnSelectItem: Boolean;
  712. PDontSelectItem: Boolean;
  713. PSelectingImplicitly: Boolean;
  714. SelectingImplicitly: Boolean;
  715. Shift: TShiftState;
  716. begin
  717. Shift := KeysToShiftState(Message.Keys);
  718. PLastSelectMethod := FLastSelectMethod;
  719. PDontSelectItem := FDontSelectItem;
  720. PDontUnSelectItem := FDontUnSelectItem;
  721. PSelectingImplicitly := FSelectingImplicitly;
  722. FLastSelectMethod := smMouse;
  723. FDontSelectItem := FDontSelectItem or (NortonLike = nlOn);
  724. FDontUnSelectItem := FDontUnSelectItem or (NortonLike = nlOn);
  725. SelectingImplicitly := ((Shift * [ssCtrl, ssShift]) = []);
  726. // TODO unselect all when clicking outside of selection
  727. // (is not done automatically when focused item is not selected)
  728. FSelectingImplicitly := FSelectingImplicitly or SelectingImplicitly;
  729. try
  730. inherited;
  731. finally
  732. FLastSelectMethod := PLastSelectMethod;
  733. FDontSelectItem := PDontSelectItem;
  734. FDontUnSelectItem := PDontUnSelectItem;
  735. FSelectingImplicitly := PSelectingImplicitly;
  736. end;
  737. end;
  738. procedure TCustomNortonLikeListView.WMLButtonUp(var Message: TWMLButtonUp);
  739. var
  740. SelectingImplicitly: Boolean;
  741. Shift: TShiftState;
  742. begin
  743. // Workaround
  744. // For some reason Message.Keys is always 0 here,
  745. // so we use shift state from the LButtonDown as a workaround
  746. Shift := KeysToShiftState(Message.Keys);
  747. SelectingImplicitly :=
  748. ((Shift * [ssCtrl, ssShift]) = []) and
  749. ((FLButtonDownShiftState * [ssCtrl, ssShift]) = []);
  750. if SelectingImplicitly and (csClicked in ControlState) and
  751. (Abs(FLButtonDownPos.X - Message.XPos) <= 4) and
  752. (Abs(FLButtonDownPos.Y - Message.YPos) <= 4) then
  753. begin
  754. SelectAll(smNone, ItemFocused);
  755. // Because condition in ItemSelected is not triggered as we first select
  756. // the new item and then unselect the previous.
  757. // This probably means that we can get rid of the code in ItemSelected.
  758. FAnyAndAllSelectedImplicitly := True;
  759. end;
  760. inherited;
  761. end;
  762. function TCustomNortonLikeListView.GetMarkedFile: TListItem;
  763. begin
  764. if Assigned(Selected) then Result := Selected
  765. else
  766. if Assigned(ItemFocused) and (NortonLike <> nlOff) then Result := ItemFocused
  767. else Result := nil;
  768. end;
  769. function TCustomNortonLikeListView.GetNextItem(StartItem: TListItem;
  770. Direction: TSearchDirection; States: TItemStates): TListItem;
  771. var
  772. Start, Index, First, Last: Integer;
  773. begin
  774. if not FManageSelection then
  775. begin
  776. Result := inherited GetNextItem(StartItem, Direction, States);
  777. end
  778. else
  779. begin
  780. Assert(Direction = sdAll);
  781. if States = [isSelected] then
  782. begin
  783. if FSelCount = 0 then
  784. begin
  785. Result := nil
  786. end
  787. else
  788. if (not Assigned(StartItem)) and (FFirstSelected >= 0) then
  789. begin
  790. Result := Items[FFirstSelected]
  791. end
  792. else
  793. begin
  794. if Assigned(StartItem) then
  795. Start := StartItem.Index
  796. else
  797. Start := -1;
  798. if (FFirstSelected >= 0) and (Start < FFirstSelected) then
  799. First := FFirstSelected
  800. else
  801. First := Start + 1;
  802. if FLastSelected >= 0 then
  803. Last := FLastSelected
  804. else
  805. Last := Items.Count - 1;
  806. if Start > Last then
  807. begin
  808. Result := nil;
  809. end
  810. else
  811. begin
  812. Index := First;
  813. while (Index <= Last) and (not (Items[Index].Selected)) do
  814. begin
  815. Inc(Index);
  816. end;
  817. if Index > Last then
  818. begin
  819. Result := nil;
  820. if Assigned(StartItem) and StartItem.Selected then
  821. begin
  822. Assert((FLastSelected < 0) or (FLastSelected = Start));
  823. FLastSelected := Start;
  824. end;
  825. end
  826. else
  827. begin
  828. Result := Items[Index];
  829. Assert(Result.Selected);
  830. if not Assigned(StartItem) then
  831. begin
  832. Assert((FFirstSelected < 0) or (FFirstSelected = Index));
  833. FFirstSelected := Index;
  834. end;
  835. end;
  836. end;
  837. end;
  838. end
  839. else
  840. if States = [isCut] then
  841. begin
  842. Result := inherited GetNextItem(StartItem, Direction, States);
  843. end
  844. else
  845. if States = [] then
  846. begin
  847. if Assigned(StartItem) then
  848. Start := StartItem.Index
  849. else
  850. Start := -1;
  851. Inc(Start);
  852. if Start < Items.Count then
  853. Result := Items[Start]
  854. else
  855. Result := nil;
  856. end
  857. else
  858. begin
  859. Assert(False);
  860. Result := nil;
  861. end;
  862. end;
  863. end;
  864. function TCustomNortonLikeListView.GetSelCount: Integer;
  865. begin
  866. Result := FSelCount;
  867. end;
  868. procedure TCustomNortonLikeListView.InsertItem(Item: TListItem);
  869. begin
  870. inherited;
  871. if Item.Selected then
  872. ItemSelected(Item, -1);
  873. end;
  874. function TCustomNortonLikeListView.GetItemFromHItem(const Item: TLVItem): TListItem;
  875. begin
  876. with Item do
  877. if (state and LVIF_PARAM) <> 0 then Result := Pointer(lParam)
  878. else Result := Items[iItem];
  879. end;
  880. function TCustomNortonLikeListView.GetMarkedCount: Integer;
  881. begin
  882. if (SelCount > 0) or (NortonLike = nlOff) then Result := SelCount
  883. else
  884. if Assigned(ItemFocused) then Result := 1
  885. else Result := 0;
  886. end;
  887. function TCustomNortonLikeListView.GetValid: Boolean;
  888. begin
  889. // Note that TCustomDirView::GetValid don't inherit
  890. // this method because of optimalization
  891. Result := (not (csDestroying in ComponentState)) and (not FClearingItems);
  892. end;
  893. procedure TCustomNortonLikeListView.BeginSelectionUpdate;
  894. begin
  895. // Higher value is probably some nesting error
  896. Assert(FUpdatingSelection in [0..4]);
  897. Inc(FUpdatingSelection);
  898. end; { BeginUpdatingSelection }
  899. procedure TCustomNortonLikeListView.EndSelectionUpdate;
  900. begin
  901. Assert(FUpdatingSelection > 0);
  902. Dec(FUpdatingSelection);
  903. end; { EndUpdatingSelection }
  904. procedure TCustomNortonLikeListView.CreateWnd;
  905. begin
  906. try
  907. Assert(ColProperties <> nil);
  908. inherited;
  909. FHeaderHandle := ListView_GetHeader(Handle);
  910. ColProperties.ListViewWndCreated;
  911. finally
  912. end;
  913. end;
  914. procedure TCustomNortonLikeListView.DestroyWnd;
  915. begin
  916. ColProperties.ListViewWndDestroying;
  917. try
  918. inherited;
  919. finally
  920. ColProperties.ListViewWndDestroyed;
  921. end;
  922. end;
  923. procedure TCustomNortonLikeListView.LVMEditLabel(var Message: TMessage);
  924. begin
  925. // explicitly requesting editing (e.g. F2),
  926. // so we do not care anymore when the view was focused
  927. FFocused := 0;
  928. inherited;
  929. end;
  930. function TCustomNortonLikeListView.CanEdit(Item: TListItem): Boolean;
  931. var
  932. N: TDateTime;
  933. Delta: Double;
  934. begin
  935. N := Now;
  936. Result := inherited CanEdit(Item);
  937. if Result and (FFocused > 0) then
  938. begin
  939. Delta := N - FFocused;
  940. // it takes little more than 500ms to trigger editing after click
  941. Result := Delta > (750.0/MSecsPerDay);
  942. end;
  943. FFocused := 0;
  944. end;
  945. procedure TCustomNortonLikeListView.WMSetFocus(var Message: TWMSetFocus);
  946. begin
  947. inherited;
  948. if Message.FocusedWnd <> FIgnoreSetFocusFrom then
  949. FFocused := Now;
  950. end;
  951. procedure TCustomNortonLikeListView.CMWantSpecialKey(var Message: TCMWantSpecialKey);
  952. begin
  953. inherited;
  954. if IsEditing and (Message.CharCode = VK_TAB) then
  955. Message.Result := 1;
  956. end;
  957. procedure TCustomNortonLikeListView.MakeProgressVisible(Item: TListItem);
  958. var
  959. DisplayRect: TRect;
  960. begin
  961. if ViewStyle = vsReport then
  962. begin
  963. DisplayRect := Item.DisplayRect(drBounds);
  964. if DisplayRect.Bottom > ClientHeight then
  965. begin
  966. Scroll(0, Item.Top - TopItem.Top);
  967. end;
  968. end;
  969. Item.MakeVisible(False);
  970. end;
  971. procedure TCustomNortonLikeListView.ChangeScale(M, D: Integer);
  972. begin
  973. if M <> D then
  974. begin
  975. // When font is scaled, while the control is being re-created, previous font is restored once
  976. // read from the persistence data in TCustomListView.CreateWnd.
  977. // Requiring handle, makes sure the re-create phase is closed.
  978. // We could limit impact by checking ControlHasRecreationPersistenceData,
  979. // but for now, we actually prefer larger impact to test this change better.
  980. HandleNeeded;
  981. end;
  982. inherited;
  983. end;
  984. end.