Skip to content

Commit

Permalink
Change for #832: Turn TVirtualNode.NextSibling into a readonly prope…
Browse files Browse the repository at this point in the history
…rty.
  • Loading branch information
joachimmarder committed Aug 13, 2023
1 parent db238f6 commit 5f08e00
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
46 changes: 23 additions & 23 deletions Source/VirtualTrees.BaseTree.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4325,7 +4325,7 @@ procedure TBaseVirtualTree.InitRootNode(OldSize: Cardinal = 0);
begin
// Indication that this node is the root node.
SetPrevSibling(FRoot);
NextSibling := FRoot;
SetNextSibling(FRoot);
SetParent(Pointer(Self));
States := [vsInitialized, vsExpanded, vsHasChildren, vsVisible];
TotalHeight := FDefaultNodeHeight;
Expand Down Expand Up @@ -5046,7 +5046,7 @@ procedure TBaseVirtualTree.SetChildCount(Node: PVirtualNode; NewChildCount: Card
Child.SetIndex(Index);
Child.SetPrevSibling(Node.LastChild);
if Assigned(Node.LastChild) then
Node.LastChild.NextSibling := Child;
Node.LastChild.SetNextSibling(Child);
Child.SetParent(Node);
Node.LastChild := Child;
if Node.FirstChild = nil then
Expand Down Expand Up @@ -13779,13 +13779,13 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode;
begin
Node.SetPrevSibling(Destination.PrevSibling);
Destination.SetPrevSibling(Node);
Node.NextSibling := Destination;
Node.SetNextSibling(Destination);
Node.SetParent(Destination.Parent);
Node.SetIndex(Destination.Index);
if Node.PrevSibling = nil then
Node.Parent.FirstChild := Node
else
Node.PrevSibling.NextSibling := Node;
Node.PrevSibling.SetNextSibling(Node);

// reindex all following nodes
Run := Destination;
Expand All @@ -13797,8 +13797,8 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode;
end;
amInsertAfter:
begin
Node.NextSibling := Destination.NextSibling;
Destination.NextSibling := Node;
Node.SetNextSibling(Destination.NextSibling);
Destination.SetNextSibling(Node);
Node.SetPrevSibling(Destination);
Node.SetParent(Destination.Parent);
if Node.NextSibling = nil then
Expand All @@ -13821,15 +13821,15 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode;
begin
// If there's a first child then there must also be a last child.
Destination.FirstChild.SetPrevSibling(Node);
Node.NextSibling := Destination.FirstChild;
Node.SetNextSibling(Destination.FirstChild);
Destination.FirstChild := Node;
end
else
begin
// First child node at this location.
Destination.FirstChild := Node;
Destination.LastChild := Node;
Node.NextSibling := nil;
Node.SetNextSibling(nil);
end;
Node.SetPrevSibling(nil);
Node.SetParent(Destination);
Expand All @@ -13847,7 +13847,7 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode;
if Assigned(Destination.LastChild) then
begin
// If there's a last child then there must also be a first child.
Destination.LastChild.NextSibling := Node;
Destination.LastChild.SetNextSibling(Node);
Node.SetPrevSibling(Destination.LastChild);
Destination.LastChild := Node;
end
Expand All @@ -13858,7 +13858,7 @@ procedure TBaseVirtualTree.InternalConnectNode(Node, Destination: PVirtualNode;
Destination.LastChild := Node;
Node.SetPrevSibling(nil);
end;
Node.NextSibling := nil;
Node.SetNextSibling(nil);
Node.SetParent(Destination);
if Assigned(Node.PrevSibling) then
Node.SetIndex(Node.PrevSibling.Index + 1)
Expand Down Expand Up @@ -13963,7 +13963,7 @@ procedure TBaseVirtualTree.InternalDisconnectNode(Node: PVirtualNode; KeepFocus:
System.Dec(FVisibleCount, CountVisibleChildren(Node) + Cardinal(IfThen(IsEffectivelyVisible[Node], 1)));

if Assigned(Node.PrevSibling) then
Node.PrevSibling.NextSibling := Node.NextSibling
Node.PrevSibling.SetNextSibling(Node.NextSibling)
else
Parent.FirstChild := Node.NextSibling;

Expand Down Expand Up @@ -15138,7 +15138,7 @@ function TBaseVirtualTree.ReadChunk(Stream: TStream; Version: Integer; Node: PVi
if Assigned(Run.PrevSibling) then
Run.SetIndex(Run.PrevSibling.Index + 1);
if Assigned(Node.LastChild) then
Node.LastChild.NextSibling := Run
Node.LastChild.SetNextSibling(Run)
else
Node.FirstChild := Run;
Node.LastChild := Run;
Expand Down Expand Up @@ -16902,7 +16902,7 @@ procedure TBaseVirtualTree.DeleteChildren(Node: PVirtualNode; ResetHasChildren:
Run := Run.PrevSibling;
// Important, to avoid exchange of invalid pointers while disconnecting the node.
if Assigned(Run) then
Run.NextSibling := nil;
Run.SetNextSibling(nil);
DeleteNode(Mark, False, True);
end;
if ResetHasChildren then
Expand Down Expand Up @@ -22327,23 +22327,23 @@ procedure TBaseVirtualTree.Sort(Node: PVirtualNode; Column: TColumnIndex; Direct

if CompareResult <= 0 then
begin
Result.NextSibling := A;
Result.SetNextSibling(A);
Result := A;
A := A.NextSibling;
end
else
begin
Result.NextSibling := B;
Result.SetNextSibling(B);
Result := B;
B := B.NextSibling;
end;
end;

// Just append the list which is not nil (or set end of result list to nil if both lists are nil).
if Assigned(A) then
Result.NextSibling := A
Result.SetNextSibling(A)
else
Result.NextSibling := B;
Result.SetNextSibling(B);
// return start of the new merged list
Result := Dummy.NextSibling;
end;
Expand All @@ -22370,23 +22370,23 @@ procedure TBaseVirtualTree.Sort(Node: PVirtualNode; Column: TColumnIndex; Direct

if CompareResult >= 0 then
begin
Result.NextSibling := A;
Result.SetNextSibling(A);
Result := A;
A := A.NextSibling;
end
else
begin
Result.NextSibling := B;
Result.SetNextSibling(B);
Result := B;
B := B.NextSibling;
end;
end;

// Just append the list which is not nil (or set end of result list to nil if both lists are nil).
if Assigned(A) then
Result.NextSibling := A
Result.SetNextSibling(A)
else
Result.NextSibling := B;
Result.SetNextSibling(B);
// Return start of the newly merged list.
Result := Dummy.NextSibling;
end;
Expand All @@ -22411,7 +22411,7 @@ procedure TBaseVirtualTree.Sort(Node: PVirtualNode; Column: TColumnIndex; Direct
begin
Result := Node;
Node := Node.NextSibling;
Result.NextSibling := nil;
Result.SetNextSibling(nil);
end;
end;

Expand All @@ -22435,7 +22435,7 @@ procedure TBaseVirtualTree.Sort(Node: PVirtualNode; Column: TColumnIndex; Direct
begin
Result := Node;
Node := Node.NextSibling;
Result.NextSibling := nil;
Result.SetNextSibling(nil);
end;
end;

Expand Down
13 changes: 10 additions & 3 deletions Source/VirtualTrees.Types.pas
Original file line number Diff line number Diff line change
Expand Up @@ -902,18 +902,20 @@ TScrollBarOptions = class(TPersistent)
// located at the end of the node! Hence if you want to add new member fields (except pointers to internal
// data) then put them before field Parent.
private
fParent: PVirtualNode; // link to the node's last child...
fParent: PVirtualNode; // reference to the node's parent (for the root this contains the treeview)
fPrevSibling: PVirtualNode; // link to the node's previous sibling or nil if it is the first node
public // reference to the node's parent (for the root this contains the treeview)
NextSibling, // link to the node's next sibling or nil if it is the last node
fNextSibling: PVirtualNode; // link to the node's next sibling or nil if it is the last node
public
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 SetPrevSibling(const pPrevSibling: PVirtualNode); inline; //internal method, do not call directly
procedure SetNextSibling(const pNextSibling: PVirtualNode); inline; //internal method, do not call directly
procedure SetIndex(const pIndex: Cardinal); inline; //internal method, do not call directly.
property Index: Cardinal read fIndex;
property Parent: PVirtualNode read fParent;
property PrevSibling: PVirtualNode read fPrevSibling;
property NextSibling: PVirtualNode read fNextSibling;
private
Data: record end; // this is a placeholder, each node gets extra data determined by NodeDataSize
public
Expand Down Expand Up @@ -1195,6 +1197,11 @@ procedure TVirtualNode.SetPrevSibling(const pPrevSibling: PVirtualNode);
fPrevSibling := pPrevSibling;
end;

procedure TVirtualNode.SetNextSibling(const pNextSibling: PVirtualNode);
begin
fNextSibling := pNextSibling;
end;

//----------------------------------------------------------------------------------------------------------------------


Expand Down

0 comments on commit 5f08e00

Please sign in to comment.