home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue152 / delphi / copydelp.exe / TreeDir3 / TreeD3.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-05  |  7.1 KB  |  227 lines

  1. unit TreeD3;
  2.    { PC Plus sample Delphi 3.0 file manager.
  3.    Illustrates a way of creating a TreeView Explorer-like directory browser.
  4.  
  5.    Author: Huw Collingbourne
  6.  
  7.    Remarks: This is a fairly simple application which displays files
  8.    and executes a file when double-clicked. You may want to develop this
  9.    by adding additional file management routines, multiple disk display
  10.    and appropriate exception handling. }
  11.  
  12. interface
  13.  
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  16.   StdCtrls, ComCtrls, ExtCtrls, ShellAPI;
  17.  
  18. type
  19.   TForm1 = class(TForm)
  20.     TreeView1: TTreeView;
  21.     StatusBar1: TStatusBar;
  22.     ImageList1: TImageList;
  23.     Splitter1: TSplitter;
  24.     ListView1: TListView;
  25.     procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
  26.       var AllowExpansion: Boolean);
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure TreeView1Click(Sender: TObject);
  29.     procedure ListView1Click(Sender: TObject);
  30.     procedure ListView1DblClick(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.   public
  34.     { Public declarations }
  35.     function isDirectory( sr : TSearchRec ) : boolean;
  36.     function excludeFromListView( sr : TSearchRec ) : boolean;
  37.     function hasSubDirectories( d : string ) : boolean;
  38.     function fullPath( Node : TTreeNode ) : string;
  39.     procedure addSubDirs( rootDir : string;  Node   : TTreeNode );
  40.     procedure upDatePanels;
  41.     procedure upDateListView;
  42.     function  RunFile(const FileName, Params, DefaultDir: string;
  43.                  ShowCmd: Integer): THandle;
  44.   end;
  45.  
  46. const    // specify directory to appear as the root in TreeView
  47.   rootDir = 'C:';
  48.  
  49. var
  50.   Form1: TForm1;
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55. function TForm1.RunFile(const FileName, Params, DefaultDir: string;
  56.   ShowCmd: Integer): THandle;
  57. // executes the specified file (if possible)
  58. var
  59.   zFileName, zParams, zDir: array[0..79] of Char;
  60. begin
  61.   Result := ShellExecute(Application.MainForm.Handle, nil,
  62.     StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
  63.     StrPCopy(zDir, DefaultDir), ShowCmd);
  64. end;
  65.  
  66. procedure TForm1.upDateListView;
  67. // displays file names in current directory
  68. var
  69.    NewItem : TListItem;
  70.    d : string;
  71.    Result : integer;
  72.    SearchRec: TSearchRec;
  73. begin
  74.     d := fullPath(TreeView1.Selected);
  75.     ListView1.Items.Clear;
  76.     Result := FindFirst(d+'*.*', faAnyFile, SearchRec);
  77.     while Result = 0 do
  78.     begin // filter any unwanted files from display
  79.       if not excludeFromListView( SearchRec ) then
  80.       begin
  81.          NewItem := ListView1.Items.Add;    // add a file name
  82.          NewItem.Caption := SearchRec.Name;
  83.          if isDirectory(SearchRec) then
  84.             NewItem.ImageIndex := 0        // show icon before file or dir
  85.          else
  86.             NewItem.ImageIndex := 2;
  87.       end;
  88.       Result := FindNext(SearchRec);
  89.     end;
  90.     FindClose(SearchRec);
  91. end;
  92.  
  93. procedure TForm1.upDatePanels;
  94. // display path and number of sub directories in the Statusbar panels
  95. var
  96.   p0text : string;
  97. begin
  98.   p0text := fullPath(TreeView1.Selected);
  99.   if TreeView1.Selected.Expanded then p0text := p0text +
  100.         ' [ ' + IntToStr( TreeView1.Selected.Count ) + ' subdirectories ]';
  101.   StatusBar1.Panels.Items[0].Text := p0text;
  102. end;
  103.  
  104. function TForm1.excludeFromListView( sr : TSearchRec ) : boolean;
  105. // don't display these files in the file list
  106. begin
  107.     if isDirectory( sr ) then
  108.        excludeFromListView := true
  109.     else if ((sr.Name = '.') or (sr.Name = '..')) then
  110.      excludeFromListView := true
  111.   else excludeFromListView := false;
  112. end;
  113.  
  114. function TForm1.isDirectory( sr : TSearchRec ) : boolean;
  115. // test if current file is a directory
  116. begin
  117.    isDirectory := ((sr.Attr and faDirectory > 0)
  118.                   and
  119.                   (sr.Name <> '.') and (sr.Name <> '..'));
  120. end;
  121.  
  122. function TForm1.fullPath( Node : TTreeNode ) : string;
  123. // return a string showing path - e.g. C:\GP\Parent\NodeDir\
  124. var
  125.   ANode, FirstNode : TTreeNode;
  126.   path : string;
  127. begin
  128.   ANode := Node;
  129.   path := ANode.Text + '\';
  130.   FirstNode := TreeView1.Items.GetFirstNode;
  131.   while ANode <> FirstNode do
  132.   begin
  133.      ANode := ANode.Parent;
  134.      path := ANode.Text + '\' + path;
  135.   end;
  136.   fullPath := path;
  137. end;
  138.  
  139. function TForm1.hasSubDirectories( d : string ) : boolean;
  140. // test in the directory indicated by the string 'd' contains
  141. // subdirectories. If so, return true.
  142. var
  143.   SearchRec: TSearchRec;
  144.   SResult : integer;
  145.   noSubDirFound : boolean;
  146. begin    // find first file, Result is 0 if successful
  147.     noSubDirFound := true;
  148.     SResult := FindFirst(d + '\*.*', faDirectory, SearchRec);
  149.     while ((SResult = 0) and (noSubDirFound)) do
  150.     begin
  151.       if isDirectory(SearchRec) then noSubDirFound := false;
  152.       SResult := FindNext(SearchRec);
  153.     end;
  154.     FindClose(SearchRec);
  155.     hasSubDirectories := (noSubDirFound = false);
  156. end;
  157.  
  158. procedure TForm1.addSubDirs( rootDir : string;  Node : TTreeNode );
  159. { add directory names as branches of the TreeView . If a directory contains
  160.   any subdirectories, a '+' is placed alongside the directory name in
  161.   the TreeView }
  162. var
  163.   SearchRec: TSearchRec;
  164.   Result : integer;
  165.   NewNode : TTreeNode;
  166. begin    // find first file, Result is 0 if successful
  167.     Result := FindFirst(rootDir + '*.*', faDirectory, SearchRec);
  168.     while Result = 0 do
  169.     begin
  170.       if isDirectory(SearchRec) then
  171.       begin  // if directory is found add it as a child of prev node in TreeView
  172.          NewNode := TreeView1.Items.AddChild(Node, SearchRec.Name);
  173.          NewNode.SelectedIndex := 1;
  174.          if (hasSubDirectories( rootDir + SearchRec.Name ) ) then
  175.             // making HasChildren true adds a '+' to the directory node
  176.             // even if that node hasn't really got any child nodes
  177.             NewNode.HasChildren := true;
  178.       end; // then continue searching }
  179.       Result := FindNext(SearchRec);
  180.     end;
  181.     FindClose(SearchRec);
  182.     Node.AlphaSort;
  183. end;
  184.  
  185. procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
  186.   var AllowExpansion: Boolean);
  187. begin
  188.     if Node.GetFirstChild = nil then
  189.        addSubDirs(fullPath(Node) , Node );
  190.   Node.Selected := true; { if expanding the node, select it }
  191. end;
  192.  
  193. procedure TForm1.FormCreate(Sender: TObject);
  194. var
  195.   Node   : TTreeNode;
  196. begin
  197.   Node := TreeView1.Items.Add(TreeView1.Selected, rootDir);
  198.   Node.SelectedIndex := 1;
  199.   Node.Selected := true;
  200.   addSubDirs(rootDir+'\', Node);
  201.   TreeView1.AlphaSort;
  202.             // Expand(false) means - don't recurse to expand all child items
  203.             // Expand(true) would havethe effect of logging (and expanding)
  204.             // all subdirectories beneath the root!
  205.   TreeView1.Items[0].Expand(false);
  206.   updateListView;
  207.   updatePanels;
  208. end;
  209.  
  210. procedure TForm1.TreeView1Click(Sender: TObject);
  211. begin
  212.     upDateListView;
  213.     updatePanels;
  214. end;
  215.  
  216. procedure TForm1.ListView1Click(Sender: TObject);
  217. begin
  218.   StatusBar1.Panels.Items[1].Text := ListView1.Selected.Caption;
  219. end;
  220.  
  221. procedure TForm1.ListView1DblClick(Sender: TObject);
  222. begin
  223.   RunFile(ListView1.Selected.Caption, '', fullPath(TreeView1.Selected), SW_SHOW);
  224. end;
  225.  
  226. end.
  227.