1
0
mirror of synced 2024-12-23 01:33:08 +01:00
x2utils/Test/Forms/FBTree.pas

131 lines
2.9 KiB
ObjectPascal
Raw Normal View History

2004-08-20 12:03:59 +02:00
unit FBTree;
interface
uses
Forms,
Classes,
Controls,
Windows,
dxorgchr,
X2UtBinaryTree;
type
TfrmBTree = class(TForm)
ocTree: TdxOrgChart;
2004-08-20 12:03:59 +02:00
procedure ocTreeDblClick(Sender: TObject);
private
FTree: TX2CustomBTree;
2004-08-20 12:03:59 +02:00
protected
procedure BuildTree(const ARoot: PX2BTreeNode;
2004-08-20 12:03:59 +02:00
const AParent: TdxOcNode = nil);
public
class procedure Execute(const ATree: TX2CustomBTree;
2004-08-20 12:03:59 +02:00
const AShowModal: Boolean = True);
end;
implementation
uses
Graphics,
SysUtils,
TypInfo;
type
THackBTree = class(TX2CustomBTree);
2004-08-20 12:03:59 +02:00
{$R *.dfm}
class procedure TfrmBTree.Execute;
begin
with TfrmBTree.Create(nil) do
try
FTree := ATree;
BuildTree(THackBTree(ATree).Root);
if Assigned(ocTree.RootNode) then
ocTree.RootNode.Expand(True);
if AShowModal then
ShowModal()
else
Show();
finally
// Yes, yes, memory leak I know. Should have an owner of something. Anyways,
// I'm too lazy to fix it in this test application...
if AShowModal then
Free();
end;
end;
procedure TfrmBTree.BuildTree;
function CreateTextNode(const AParent: TdxOcNode;
const AText: String): TdxOcNode;
begin
Result := ocTree.AddChild(AParent, nil);
Result.Text := AText;
Result.Color := clInfoBk;
end;
function CreateNode(const AParent: TdxOcNode;
const ANode: PX2BTreeNode): TdxOcNode;
2004-08-20 12:03:59 +02:00
begin
Result := CreateTextNode(AParent, IntToStr(ANode^.Index) + #13#10 +
IntToStr(ANode^.Balance));
Result.Data := ANode;
end;
var
pNode: TdxOcNode;
pLeft: TdxOcNode;
pRight: TdxOcNode;
begin
if not Assigned(ARoot) then
exit;
pLeft := nil;
pRight := nil;
if not Assigned(AParent) then
pNode := CreateNode(nil, ARoot)
else
pNode := AParent;
if Assigned(ARoot^.Children[0]) then
pLeft := CreateNode(pNode, ARoot^.Children[0])
else if Assigned(ARoot^.Children[1]) then
CreateTextNode(pNode, '<nil>');
if Assigned(ARoot^.Children[1]) then
pRight := CreateNode(pNode, ARoot^.Children[1])
else if Assigned(ARoot^.Children[0]) then
CreateTextNode(pNode, '<nil>');
if Assigned(ARoot^.Children[0]) then
BuildTree(ARoot^.Children[0], pLeft);
if Assigned(ARoot^.Children[1]) then
BuildTree(ARoot^.Children[1], pRight);
end;
procedure TfrmBTree.ocTreeDblClick;
var
pNode: TdxOcNode;
begin
pNode := ocTree.Selected;
if Assigned(pNode) and Assigned(pNode.Data) then
begin
FTree.Delete(PX2BTreeNode(pNode.Data)^.Index);
2004-08-20 12:03:59 +02:00
ocTree.Clear();
BuildTree(THackBTree(FTree).Root);
if Assigned(ocTree.RootNode) then
ocTree.RootNode.Expand(True);
end;
end;
end.