From dd066edf436288be052e9c28133e75cc1cd2a357 Mon Sep 17 00:00:00 2001 From: Joachim Marder Date: Sat, 1 Apr 2023 18:30:16 +0200 Subject: [PATCH] Change for #832: Turn TVirtualNode.Index into a readonly property. --- Source/VirtualTrees.BaseTree.pas | 24 ++++++++++++------------ Source/VirtualTrees.Types.pas | 11 ++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Source/VirtualTrees.BaseTree.pas b/Source/VirtualTrees.BaseTree.pas index 36c2f67d4..d5c6ec6d9 100644 --- a/Source/VirtualTrees.BaseTree.pas +++ b/Source/VirtualTrees.BaseTree.pas @@ -5045,7 +5045,7 @@ procedure TBaseVirtualTree.SetChildCount(Node: PVirtualNode; NewChildCount: Card while Remaining > 0 do begin Child := MakeNewNode; - Child.Index := Index; + Child.SetIndex(Index); Child.PrevSibling := Node.LastChild; if Assigned(Node.LastChild) then Node.LastChild.NextSibling := Child; @@ -13776,7 +13776,7 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode; Destination.PrevSibling := Node; Node.NextSibling := Destination; Node.SetParent(Destination.Parent); - Node.Index := Destination.Index; + Node.SetIndex(Destination.Index); if Node.PrevSibling = nil then Node.Parent.FirstChild := Node else @@ -13786,7 +13786,7 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode; Run := Destination; while Assigned(Run) do begin - System.Inc(Run.Index); + Run.SetIndex(Run.Index + 1); Run := Run.NextSibling; end; end; @@ -13800,13 +13800,13 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode; Node.Parent.LastChild := Node else Node.NextSibling.PrevSibling := Node; - Node.Index := Destination.Index; + Node.SetIndex(Destination.Index); // reindex all following nodes Run := Node; while Assigned(Run) do begin - System.Inc(Run.Index); + Run.SetIndex(Run.Index + 1); Run := Run.NextSibling; end; end; @@ -13828,12 +13828,12 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode; end; Node.PrevSibling := nil; Node.SetParent(Destination); - Node.Index := 0; + Node.SetIndex(0); // reindex all following nodes Run := Node.NextSibling; while Assigned(Run) do begin - System.Inc(Run.Index); + Run.SetIndex(Run.Index + 1); Run := Run.NextSibling; end; end; @@ -13856,9 +13856,9 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode; Node.NextSibling := nil; Node.SetParent(Destination); if Assigned(Node.PrevSibling) then - Node.Index := Node.PrevSibling.Index + 1 + Node.SetIndex(Node.PrevSibling.Index + 1) else - Node.Index := 0; + Node.SetIndex(0); end; else // amNoWhere: do nothing @@ -13972,7 +13972,7 @@ procedure TBaseVirtualTree.InternalDisconnectNode(Node: PVirtualNode; KeepFocus: Index := Node.Index; while Assigned(Run) do begin - Run.Index := Index; + Run.SetIndex(Index); System.Inc(Index); Run := Run.NextSibling; end; @@ -15131,7 +15131,7 @@ function TBaseVirtualTree.ReadChunk(Stream: TStream; Version: Integer; Node: PVi Run.PrevSibling := Node.LastChild; if Assigned(Run.PrevSibling) then - Run.Index := Run.PrevSibling.Index + 1; + Run.SetIndex(Run.PrevSibling.Index + 1); if Assigned(Node.LastChild) then Node.LastChild.NextSibling := Run else @@ -22479,7 +22479,7 @@ procedure TBaseVirtualTree.Sort(Node: PVirtualNode; Column: TColumnIndex; Direct Run.PrevSibling := nil; Index := 0; repeat - Run.Index := Index; + Run.SetIndex(Index); System.Inc(Index); if Run.NextSibling = nil then Break; diff --git a/Source/VirtualTrees.Types.pas b/Source/VirtualTrees.Types.pas index a6212d3da..12be05a8a 100644 --- a/Source/VirtualTrees.Types.pas +++ b/Source/VirtualTrees.Types.pas @@ -878,7 +878,9 @@ TScrollBarOptions = class(TPersistent) PVirtualNode = ^TVirtualNode; TVirtualNode = packed record - Index, // index of node with regard to its parent + private + fIndex: Cardinal; // index of node with regard to its parent + public ChildCount: Cardinal; // number of child nodes NodeHeight: TDimension; // height in pixels States: TVirtualNodeStates; // states describing various properties of the node (expanded, initialized etc.) @@ -901,6 +903,8 @@ TScrollBarOptions = class(TPersistent) FirstChild, // link to the node's first child... LastChild: PVirtualNode; // link to the node's last child... procedure SetParent(const pParent: PVirtualNode); inline; //internal method, do not call directly but use Parent[Node] := x on tree control. + procedure SetIndex(const pIndex: Cardinal); inline; //internal method, do not call directly. + property Index: Cardinal read fIndex; property Parent: PVirtualNode read fParent; private Data: record end; // this is a placeholder, each node gets extra data determined by NodeDataSize @@ -1169,6 +1173,11 @@ procedure TVirtualNode.SetData(pUserData: T); Include(Self.States, vsOnFreeNodeCallRequired); end; +procedure TVirtualNode.SetIndex(const pIndex: Cardinal); +begin + fIndex := pIndex; +end; + procedure TVirtualNode.SetParent(const pParent: PVirtualNode); begin fParent := pParent;