home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue152 / delphi / copydelp.exe / TreeDir2 / TreeD2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-02-12  |  7.2 KB  |  230 lines

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