{------------------------------------------------------------------------------}
//Populate a directory tree with an image
{------------------------------------------------------------------------------}
procedure AddImageToTree(Tree: TTreeView;ImageToUse: TDiscImage);
var
//Used as a marker to make sure all directories are displayed.
//Some double sided discs have each side as separate discs
highdir : Integer;
begin
//Clear the tree view, prior to populating it
Tree.Items.Clear;
//Set the highdir to zero - which will be root to start with
highdir:=0;
//Then add the directories, if there is at least one
if Length(ImageToUse.Disc)>0 then
begin
//Start by adding the root (could be more than one root, particularly on
//double sided discs)
repeat
//This will initiate the recursion through the directory structure, per side
AddDirectoryToTree(Tree.Items.Add(nil,ImageToUse.Disc[highdir].Directory),
highdir,Tree,ImageToUse,highdir);
//Finished on this directory structure, so increase the highdir
inc(highdir);
//and continue until we have everything on the disc. This will, in effect,
//add the second root for double sided discs.
until highdir=Length(ImageToUse.Disc);
//Expand the top level of the tree (but not MMB)
if ImageToUse.FormatNumber>>4<>diMMFS then Tree.TopItem.Expand(False);
//And the root for the other side of the disc
if ImageToUse.DoubleSided then
begin
//First, we need to find it
repeat
inc(highdir);
//If there is one, of course - but it must be a directory
until(highdir>=Tree.Items.Count) or (TMyTreeNode(Tree.Items[highdir-1]).IsDir);
if highdir>Tree.Items.Count then
highdir:=Tree.Items.Count;
//Found? then expand it
if TMyTreeNode(Tree.Items[highdir-1]).IsDir then
Tree.Items[highdir-1].Expand(False);
end;
end;
end;
{------------------------------------------------------------------------------}
//Adds a directory to the TreeView - is called recursively to drill down the tree
{------------------------------------------------------------------------------}
procedure AddDirectoryToTree(CurrDir:TTreeNode;dir:Integer;Tree:TTreeView;
ImageToUse:TDiscImage;var highdir:Integer);
var
entry: Integer;
Node: TTreeNode;
begin
//Make a note of the dir ref, it is the highest
if dir>highdir then highdir:=dir;
//Set the 'IsDir' flag to true, as this is a directory
TMyTreeNode(CurrDir).IsDir:=True;
//Iterate though all the entries
for entry:=0 to Length(ImageToUse.Disc[dir].Entries)-1 do
begin
//Adding new nodes for each one
Node:=AddFileToTree(CurrDir,ImageToUse.Disc[dir].Entries[entry].Filename,
entry,false,Tree,ImageToUse);
//If it is, indeed, a direcotry, the dir ref will point to the sub-dir
if ImageToUse.Disc[dir].Entries[entry].DirRef>=0 then
//and we'll recursively call ourself to add these entries
AddDirectoryToTree(Node,ImageToUse.Disc[dir].Entries[entry].DirRef,Tree,
ImageToUse,highdir);
end;
end;
{------------------------------------------------------------------------------}
//Add a file or directory to the TTreeView, under ParentNode
{------------------------------------------------------------------------------}
function AddFileToTree(ParentNode: TTreeNode;importfilename: String;
index: Integer;dir: Boolean;Tree:TTreeView;ImageToUse:TDiscImage): TTreeNode;
begin
Result:=nil;
if(ParentNode=nil)or(index<0)then exit;
RemoveTopBit(importfilename);
//Now add the entry to the Directory List
if ParentNode.HasChildren then
//Insert it before the one specified
if index<ParentNode.Count then
Result:=Tree.Items.Insert(ParentNode.Items[index],importfilename)
else //Unless this is the last one
Result:=Tree.Items.AddChild(ParentNode,importfilename)
else
//Is the first child, so just add it
Result:=Tree.Items.AddChildFirst(ParentNode,importfilename);
if Result<>nil then
begin
if ParentNode.Parent<>nil then //No parent, so will be the root
TMyTreeNode(Result).ParentDir:=ImageToUse.Disc[TMyTreeNode(ParentNode).ParentDir].
Entries[ParentNode.Index].DirRef
else
TMyTreeNode(Result).ParentDir:=ParentNode.Index; //But may not be the only root
TMyTreeNode(Result).IsDir:=dir;
Tree.Repaint;
//And update the free space display
if ImageToUse=MainForm.Image then MainForm.UpdateImageInfo;
end;
end;