unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
ExtCtrls, Classes, SysUtils, Printers;
type
TPerson = class
Name: string;
BirthDate: string;
procedure SaveToStream(Stream: TStream);
procedure LoadFromStream(Stream: TStream);
end;
{ TMAINFORM }
TMAINFORM = class(TForm)
ButtonEXIT: TButton;
TreeView: TTreeView;
AddButton: TButton;
EditButton: TButton;
DeleteButton: TButton;
SaveButton: TButton;
LoadButton: TButton;
PrintButton: TButton;
NameEdit: TEdit;
BirthDateEdit: TEdit;
NameLabel: TLabel;
BirthDateLabel: TLabel;
Panel1: TPanel;
procedure ButtonEXITClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure AddButtonClick(Sender: TObject);
procedure EditButtonClick(Sender: TObject);
procedure DeleteButtonClick(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure LoadButtonClick(Sender: TObject);
procedure PrintButtonClick(Sender: TObject);
private
procedure SaveTree(Node: TTreeNode; Stream:TStream);
procedure LoadTree(Node: TTreeNode; Stream:TStream);
public
end;
var
MAINFORM: TMAINFORM;
implementation
{$R *.lfm}
{ TPerson }
procedure TPerson.SaveToStream(Stream: TStream);
var
NameLen, BirthDateLen: Integer;
begin
NameLen := Length(Name);
Stream.WriteBuffer(NameLen, SizeOf(NameLen));
Stream.WriteBuffer(Name[1], NameLen);
BirthDateLen := Length(BirthDate);
Stream.WriteBuffer(BirthDateLen, SizeOf(BirthDateLen));
Stream.WriteBuffer(BirthDate[1], BirthDateLen);
end;
procedure TPerson.LoadFromStream(Stream: TStream);
var
NameLen, BirthDateLen: Integer;
begin
BirthDateLen:=0;
NameLen:=0;
Stream.ReadBuffer(NameLen, SizeOf(NameLen));
SetLength(Name, NameLen);
Stream.ReadBuffer(Name[1], NameLen);
Stream.ReadBuffer(BirthDateLen, SizeOf(BirthDateLen));
SetLength(BirthDate, BirthDateLen);
Stream.ReadBuffer(BirthDate[1], BirthDateLen);
end;
{ TMainForm }
procedure TMAINFORM.FormCreate(Sender: TObject);
begin
TreeView.Items.Add(nil, 'Family Tree');
end;
procedure TMAINFORM.ButtonEXITClick(Sender: TObject);
begin
Close;
end;
procedure TMAINFORM.AddButtonClick(Sender: TObject);
var
Node: TTreeNode;
Person: TPerson;
begin
if NameEdit.Text = '' then
begin
ShowMessage('Name input ....');
Exit;
end;
Person := TPerson.Create;
Person.Name := NameEdit.Text;
Person.BirthDate := BirthDateEdit.Text;
if TreeView.Selected <> nil then
Node := TreeView.Items.AddChildObject(TreeView.Selected, Person.Name, Person)
else
Node := TreeView.Items.AddObject(nil, Person.Name, Person);
Node.Text := Person.Name + ' (' + Person.BirthDate + ')';
NameEdit.Clear;
BirthDateEdit.Clear;
end;
procedure TMAINFORM.EditButtonClick(Sender: TObject);
var
Person: TPerson;
begin
if TreeView.Selected = nil then
begin
ShowMessage('Select EDIT button.');
Exit;
end;
if NameEdit.Text = '' then
begin
ShowMessage('Name input.');
Exit;
end;
Person := TPerson(TreeView.Selected.Data);
Person.Name := NameEdit.Text;
Person.BirthDate := BirthDateEdit.Text;
TreeView.Selected.Text := Person.Name + ' (' + Person.BirthDate + ')';
NameEdit.Clear;
BirthDateEdit.Clear;
end;
procedure TMAINFORM.DeleteButtonClick(Sender: TObject);
begin
if TreeView.Selected = nil then
begin
ShowMessage('Select DELETE button.');
Exit;
end;
TreeView.Items.Delete(TreeView.Selected);
end;
procedure TMAINFORM.SaveButtonClick(Sender: TObject);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create('FamilyTree.dat', fmCreate);
try
SaveTree(TreeView.Items[0], Stream);
finally
Stream.Free;
end;
end;
procedure TMAINFORM.LoadButtonClick(Sender: TObject);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create('FamilyTree.dat', fmOpenRead);
try
TreeView.Items.Clear;
LoadTree(TreeView.Items.Add(nil, 'Family Tree'), Stream);
finally
Stream.Free;
end;
end;
procedure TMAINFORM.PrintButtonClick(Sender: TObject);
var
Node: TTreeNode;
Line: string;
begin
Printer.BeginDoc;
try
Node := TreeView.Items.GetFirstNode;
while Node <> nil do
begin
Line := StringOfChar(' ', Node.Level * 4) + Node.Text;
Printer.Canvas.TextOut(100, 100 + Node.Index * 20, Line);
Node := Node.GetNext;
end;
finally
Printer.EndDoc;
end;
end;
procedure TMainForm.SaveTree(Node: TTreeNode; Stream: TStream);
var
Person: TPerson;
ChildNode: TTreeNode;
ChildCount: Integer;
begin
if Node.Data <> nil then
begin
Person := TPerson(Node.Data);
Person.SaveToStream(Stream);
end;
ChildCount := Node.Count;
Stream.WriteBuffer(ChildCount, SizeOf(ChildCount));
ChildNode := Node.GetFirstChild;
while ChildNode <> nil do
begin
SaveTree(ChildNode, Stream);
ChildNode := Node.GetNextChild(ChildNode);
end;
end;
procedure TMainForm.LoadTree(Node: TTreeNode; Stream: TStream);
var
Person: TPerson;
ChildCount, i: Integer;
begin
ChildCount:=0;
if Stream.Position < Stream.Size then
begin
Person := TPerson.Create;
Person.LoadFromStream(Stream);
Node.Data := Person;
Node.Text := Person.Name + ' (' + Person.BirthDate + ')';
end;
Stream.ReadBuffer(ChildCount, SizeOf(ChildCount));
for i := 0 to ChildCount - 1 do
LoadTree(TreeView.Items.AddChild(Node, ''), Stream);
end;
end.