lcl: TTreeNodeExpandedState: added OnGetNodeText

git-svn-id: trunk@53492 -
This commit is contained in:
mattias 2016-11-30 22:54:18 +00:00
parent 73e7b0a2f8
commit 276bf3674b
2 changed files with 36 additions and 9 deletions

View File

@ -3681,6 +3681,8 @@ type
end;
TTVGetNodeText = function(Node: TTreeNode): string of object;
{ TTreeNodeExpandedState }
{ class to store and restore the expanded state of a TTreeView
The nodes are identified by their Text property.
@ -3695,15 +3697,20 @@ type
}
TTreeNodeExpandedState = class
private
FOnGetNodeText: TTVGetNodeText;
function DefaultGetNodeText(Node: TTreeNode): string;
public
NodeText: string;
Children: TAvgLvlTree;
constructor Create(FirstTreeNode: TTreeNode);
constructor Create(TreeView: TCustomTreeView);
constructor Create(FirstTreeNode: TTreeNode; const GetNodeTextEvent: TTVGetNodeText = nil);
constructor Create(TreeView: TCustomTreeView; const GetNodeTextEvent: TTVGetNodeText = nil);
destructor Destroy; override;
procedure Clear;
procedure CreateChildNodes(FirstTreeNode: TTreeNode);
procedure Apply(FirstTreeNode: TTreeNode; CollapseToo: boolean = true);
procedure Apply(TreeView: TCustomTreeView; CollapseToo: boolean = true);
property OnGetNodeText: TTVGetNodeText read FOnGetNodeText write FOnGetNodeText;
end;

View File

@ -192,13 +192,28 @@ end;
{ TTreeNodeExpandedState }
constructor TTreeNodeExpandedState.Create(FirstTreeNode: TTreeNode);
function TTreeNodeExpandedState.DefaultGetNodeText(Node: TTreeNode): string;
begin
Result:=Node.Text;
end;
constructor TTreeNodeExpandedState.Create(FirstTreeNode: TTreeNode;
const GetNodeTextEvent: TTVGetNodeText);
begin
if GetNodeTextEvent<>nil then
FOnGetNodeText:=GetNodeTextEvent
else
FOnGetNodeText:=@DefaultGetNodeText;
CreateChildNodes(FirstTreeNode);
end;
constructor TTreeNodeExpandedState.Create(TreeView: TCustomTreeView);
constructor TTreeNodeExpandedState.Create(TreeView: TCustomTreeView;
const GetNodeTextEvent: TTVGetNodeText);
begin
if GetNodeTextEvent<>nil then
FOnGetNodeText:=GetNodeTextEvent
else
FOnGetNodeText:=@DefaultGetNodeText;
CreateChildNodes(TreeView.Items.GetFirstNode);
end;
@ -221,16 +236,19 @@ var
ChildNode: TTreeNode;
NewExpandedNode: TTreeNodeExpandedState;
begin
if (FirstTreeNode<>nil) and (FirstTreeNode.Parent<>nil) then
NodeText:=FirstTreeNode.Parent.Text
Clear;
if (FirstTreeNode=nil) then exit;
if (FirstTreeNode.Parent<>nil) then
NodeText:=OnGetNodeText(FirstTreeNode.Parent)
else
NodeText:='';
Clear;
ChildNode:=FirstTreeNode;
while ChildNode<>nil do begin
if ChildNode.Expanded then begin
if Children=nil then Children:=TAvgLvlTree.Create(@CompareExpandedNodes);
NewExpandedNode:=TTreeNodeExpandedState.Create(ChildNode.GetFirstChild);
NewExpandedNode:=TTreeNodeExpandedState.Create(ChildNode.GetFirstChild,OnGetNodeText);
if ChildNode.GetFirstChild=nil then
NewExpandedNode.NodeText:=OnGetNodeText(ChildNode);
Children.Add(NewExpandedNode);
end;
ChildNode:=ChildNode.GetNextSibling;
@ -246,8 +264,10 @@ begin
if Children=nil then exit;
ChildNode:=FirstTreeNode;
while ChildNode<>nil do begin
ChildNodeText:=ChildNode.Text;
ChildNodeText:=OnGetNodeText(ChildNode);
ANode:=Children.FindKey(PChar(ChildNodeText),@CompareTextWithExpandedNode);
if ChildNodeText='' then
debugln(['TTreeNodeExpandedState.Apply ',ChildNode.GetTextPath,' ChildNodeText="',ChildNodeText,'"']);
if ANode<>nil then
ChildNode.Expanded:=true
else if CollapseToo then