Skip to content

Commit

Permalink
Change for #832: Turn TVirtualNode.Index into a readonly property.
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmarder committed Apr 1, 2023
1 parent e38734e commit dd066ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
24 changes: 12 additions & 12 deletions Source/VirtualTrees.BaseTree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion Source/VirtualTrees.Types.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand All @@ -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
Expand Down Expand Up @@ -1169,6 +1173,11 @@ procedure TVirtualNode.SetData<T>(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;
Expand Down

0 comments on commit dd066ed

Please sign in to comment.