home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto06 / drwsutl2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-03-05  |  71.7 KB  |  1,903 lines

  1. unit Drwsutl2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl, DRWSUtl1;
  8.  
  9. const
  10.   EOC_CHANGEDIR = 1;  { Error Operation Code for change directory failure }
  11.   EOC_SOURCECOPY = 2; { Error Operation Code for source copy failure      }
  12.   EOC_DESTCOPY = 3;   { Error Operation Code for destination copy failure }
  13.   EOC_DELETEFILE = 4; { Error Operation Code for file delete failure      }
  14.   EOC_DELETEDIR = 5;  { Error Operation Code for directory delete failure }
  15.   EOC_RENAMEFILE = 6; { Error Operation Code for renaming failure         }
  16.   EOC_MAKEDIR = 7;    { Error Operation Code for MkDir failure            }
  17.   EOC_SETATTR = 8;    { Error Operation Code for Set Attributes failure   }
  18.  
  19.   FAC_COPY = 1;       { File Action Code for recursive copying            }
  20.   FAC_MOVE = 2;       { File Action Code for recursive moving             }
  21.   FAC_DELETE = 3;     { File Action Code for recursive deletion           }
  22. type
  23.   { This is a descendant of TFileListbox }
  24.   { Which puts icons of files into the   }
  25.   { Objects array rather than the stand- }
  26.   { ard bitmaps.                         }
  27.   TIconFileListBox = class( TFileListBox )
  28.   public
  29.     { public methods and data }
  30.     function GetNextSelection( SourceDirectory : String;
  31.               var CurrentItem : Integer ) : String;
  32.   end;
  33.   TFileWorkBench = class( TComponent )
  34.   public
  35.     GlobalError        : Integer;  { This is used by FMXUCopyFile for er code }
  36.     GlobalErrorType    : Integer;  { This holds the Operation code            }
  37.     function ForceTrailingBackSlash( const TheFileName : String ) : String;
  38.     function StripNonRootTrailingBackSlash(
  39.               const TheFileName : String ) : String;
  40.     procedure GetFileAttributes( TheFile : String; var IsDirectory , IsArchive ,
  41.                 IsVolumeID , IsHidden , IsReadOnly , IsSysFile : Boolean );
  42.     procedure HandleIOException( TheOpCode : Integer; ThePath : String;
  43.                                  TheMessage : String; TheCode : Integer );
  44.     procedure HandleDOSError( TheOpCode : Integer; ThePath : String;
  45.                 TheCode : Integer );
  46.     procedure FMXUCopyFile(const FileName, DestName: String);
  47.     function CopyFile( TargetPath ,
  48.                DestinationPath : String ) : Boolean;
  49.     procedure ChangeTheDirectory( NewPath : String );
  50.     procedure ChangeTheDriveAndDirectory( NewDrive : Integer );
  51.     procedure CopyTheFile( OldPath , NewPath : String );
  52.     procedure MoveTheFile( OldPath , NewPath : String );
  53.     procedure DeleteTheFile( ThePath : String );
  54.     procedure RenameTheFile( OldPath , NewName : String );
  55.     procedure CreateNewDirectory( NewPath : String );
  56.     procedure RemoveDirectory( ThePath : String );
  57.     procedure RecursivelyCopyDirectory( OldPath , NewPath : String );
  58.     procedure RecursivelyMoveDirectory( OldPath , NewPath : String );
  59.     procedure RecursivelyDeleteDirectory( ThePath : String );
  60.     procedure HandleRecursiveAction( StartingPath , NewPath : String;
  61.                ActionCode : Integer );
  62.   end;
  63.   TFileIconPanel = class( TPanel )
  64.   private
  65.     { Private declarations }
  66.     FHighlightColor : TColor;                 { This holds bright edge bevel }
  67.     FShadowColor    : TColor;                 { This holds dark edge bevel   }
  68.     procedure TheClick( Sender : TObject );   { This holds override click    }
  69.     procedure TheDblClick( Sender : TObject );{ This holds override dblclick }
  70.   protected                                   { event method procedure.      }
  71.     { Protected declarations }
  72.     procedure Paint; override;                { This allows custom painting  }
  73.   public
  74.     { Public declarations }
  75.     FTheIcon : TIcon;                         { This is the display icon    }
  76.     FTheName : String;                        { This is the filename        }
  77.     FTheLabel : TLabel;                       { This is the display label   }
  78.     Selected : Boolean;                       { This holds selection status }
  79.     constructor Create(AOwner : TComponent); override; { override create    }
  80.     procedure Initialize( PanelX              ,             { Left          }
  81.                           PanelY              ,             { Top           }
  82.                           PanelWidth          ,             { Width         }
  83.                           PanelHeight         ,             { Height        }
  84.                           PanelBevelWidth     ,             { Bevel Width   }
  85.                           LabelFontSize         : Integer;  { Font size     }
  86.                           PanelColor          ,             { Main color    }
  87.                           PanelHighlightColor ,             { Bright color  }
  88.                           PanelShadowColor    ,             { Dark color    }
  89.                           LabelTextColor        : TColor;   { Text color    }
  90.                           TheFilename         ,             { Filename      }
  91.                           LabelFontName         : String;   { Font name     }
  92.                           LabelFontStyle        : TFontStyles;  { Font style}
  93.                           ExtraData             : Integer       );  { Drive }
  94.     destructor Destroy; override;             { override destroy to free    }
  95.   end;
  96.   TFileIconPanelScrollBox = class( TScrollBox )
  97.   public
  98.     { Public methods and data }
  99.     TheFWB              : TFileWorkBench; { Used for file manipulation         }
  100.     IconsNeedRefreshing : Boolean;                   { Flag to redo display    }
  101.     TheIconSize        : Integer;   { Holds Individual Icon size               }
  102.     TheIconSpacing     : Integer;   { Holds total icon footprint               }
  103.     MaxIconsInARow     : Integer;   { Set for screen size.                     }
  104.     TheStoredHandle    : HWnd;
  105.     procedure Update;                                { Called to reset display }
  106.     constructor Create( AOwner : TComponent ); override;  { Override inherited }
  107.     procedure ClearTheFIPs;                          { Clears the FIPs safely  }
  108.     procedure AddDriveIcons( var XCounter , YCounter : Integer ); { Add drives }
  109.     procedure GetColorsForFileIcon( TheFile : String;
  110.                var BC , HC , SC , TC : TColor );
  111.     procedure GetIconsForEntireDirectory( TargetPath  : String );
  112.     function GetNextSelection( SourceDirectory : String;
  113.               var CurrentItem : Integer ) : String;
  114.     procedure DisplayRecursiveSearchResults(
  115.       TheStartingDirectory : String );
  116.   end;
  117.  
  118.   { This procedure gets an icon for a file using FindExecutable  }
  119.   { and ExtractIcon. (assumes file/dir is passed)                }
  120.   procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  121.   { This procedure spaces out the bitbtn components on a tpanel }
  122.   procedure SpacePanelButtons( WhichPanel : TPanel );
  123.  
  124. implementation
  125. {$R DRWSUTL2.RES}                 { Import custom resource file }
  126.  
  127. { This procedure spaces out the bitbtn components on a tpanel }
  128. procedure SpacePanelButtons( WhichPanel : TPanel );
  129. var TheCalculatedSpacing     ,            { Holds primary spacing }
  130.     TheFullCalculatedSpacing   : Integer; { Holds full spacing    }
  131.     Counter_1                  : Integer; { Loop counter          }
  132.     TotalIBs                   : Integer; { Gets total buttons    }
  133. begin
  134.   { Set up spacing values }
  135.   TotalIBs := WhichPanel.ControlCount;
  136.   TheCalculatedSpacing := (( WhichPanel.Width - 6 - ( TotalIbs * 49 ))
  137.    div ( TotalIbs + 1 ));
  138.   TheFullCalculatedSpacing := TheCalculatedSpacing + 49;
  139.   { Loop through all imported buttons and set their Left values }
  140.   for Counter_1 := 1 to WhichPanel.ControlCount do
  141.   begin
  142.     if Counter_1 = 1 then
  143.     begin
  144.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  145.        TheCalculatedSpacing;
  146.     end
  147.     else
  148.     begin
  149.       TBitBtn( WhichPanel.Controls[ Counter_1 - 1 ] ).Left := 3 +
  150.        (( Counter_1 - 1 ) * TheFullCalculatedSpacing ) + TheCalculatedSpacing;
  151.     end;
  152.   end;
  153. end;
  154.  
  155. { This procedure gets an icon for a file using FindExecutable  }
  156. { and ExtractIcon. (assumes file/dir is passed)                }
  157. procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
  158. var TheExt           : String; { File extension holder }
  159.     TheOtherPChar  ,           { Windows ASCIIZ string }
  160.     ThePChar         : PChar;  { Windows ASCIIZ string }
  161.     Dummy : Word;
  162. begin
  163.   { Check for directory and if so get directory icon from RES file }
  164.   if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
  165.   begin
  166.     { Set up the PChar to communicate with Windows }
  167.     GetMem( TheOtherPChar , 255 );
  168.     { Convert Pascal-style string to ASCIIZ Pchar }
  169.     StrPCopy( TheOtherPChar , 'DIRECTORY' );
  170.     { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
  171.     TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  172.     { Release memory from PChar }
  173.     FreeMem( TheOtherPChar , 255 );
  174.     { Leave }
  175.     exit;
  176.   end;
  177.   { Assume archive file; get its extension }
  178.   TheExt := Uppercase( ExtractFileExt( TheName ));
  179.   { If not an executable/image file then use FindExecutable to get icon }
  180.   if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
  181.       ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
  182.   begin
  183.     { Grab three chunks of memory }
  184.     GetMem( ThePChar , 255 );
  185.     { Set up the name and its directory in Windows string formats }
  186.     StrPCopy( ThePChar, TheName );
  187.     Dummy := 65535;
  188.     {**** Windows 95 Specialized call ****** }
  189.     TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
  190.     if TheIcon.Handle = 0 then
  191.     begin
  192.       GetMem( TheOtherPChar , 255 );
  193.       StrPCopy( TheOtherPChar , 'NOICON' );
  194.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  195.       FreeMem( TheOtherPChar , 255 );
  196.       exit;
  197.     end;
  198.     FreeMem( ThePChar , 255 );
  199.   end
  200.   else
  201.   { Assume Windows Executable file, so get icon from it with ExtractIcon API }
  202.   begin
  203.     GetMem( ThePChar , 255 );
  204.     StrPCopy( ThePChar , TheName );
  205.     { Try to get first icon for file }
  206.     Dummy := 65535;
  207.     TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
  208.     FreeMem( ThePChar , 255 );
  209.     { If handle is 0 invalid icon format so use default from RES file }
  210.     if TheIcon.Handle = 0 then
  211.     begin
  212.       GetMem( TheOtherPChar , 255 );
  213.       StrPCopy( TheOtherPChar , 'NOICON' );
  214.       TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  215.       FreeMem( TheOtherPChar , 255 );
  216.       exit;
  217.     end;
  218.   end;
  219. end;
  220.  
  221. { This procedure does a fully error-trapped change directory }
  222. procedure TFileWorkBench.ChangeTheDirectory( NewPath : String );
  223. var CurrentDirectory : String;
  224. begin
  225.   if NewPath = '..' then
  226.   begin { Back up one level }
  227.     {$I+}
  228.     try
  229.       { Find the current directory }
  230.       GetDir( 0 , CurrentDirectory );
  231.       { Use EFP to move up one level }
  232.       CurrentDirectory := ExtractFilePath( CurrentDirectory );
  233.       { Strip trailing \ if not root }
  234.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  235.       { Try the change to the new drive }
  236.       ChDir( CurrentDirectory );
  237.     except
  238.       { if any exception occurs instantiate exception and show }
  239.       On E:EInOutError do
  240.       begin
  241.         { Call custom error display/lookup procedure }
  242.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  243.          E.Message , E.ErrorCode );
  244.       end;
  245.     end;
  246.   end
  247.   else
  248.   begin { Change to explicit path }
  249.     {$I+}
  250.     try
  251.       { Get target directory path }
  252.       CurrentDirectory := NewPath;
  253.       { Strip trailing \ if not root }
  254.       CurrentDirectory := StripNonRootTrailingBackSlash( CurrentDirectory );
  255.       { Try the change to the new drive }
  256.       ChDir( CurrentDirectory );
  257.     except
  258.       { if any exception occurs instantiate exception and show }
  259.       On E:EInOutError do
  260.       begin
  261.         { Call custom error display/lookup procedure }
  262.         HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  263.          E.Message , E.ErrorCode );
  264.       end;
  265.     end;
  266.   end;
  267. end;
  268.  
  269. { This procedure does a fully error-trapped change directory }
  270. procedure TFileWorkBench.ChangeTheDriveAndDirectory( NewDrive : Integer );
  271. var CurrentDirectory : String;
  272. begin
  273.   {$I+}
  274.   try
  275.     { Find the working directory on new drive }
  276.     GetDir( NewDrive , CurrentDirectory );
  277.     { Try the change to the new drive }
  278.     ChDir( CurrentDirectory );
  279.   except
  280.     { if any exception occurs instantiate exception and show }
  281.     On E:EInOutError do
  282.     begin
  283.       { Call custom error display/lookup procedure }
  284.       HandleIOException( EOC_CHANGEDIR , CurrentDirectory ,
  285.        E.Message , E.ErrorCode );
  286.     end;
  287.   end;
  288. end;
  289.  
  290. { This procedure copies a single file with error trapping }
  291. procedure TFileWorkBench.CopyTheFile( OldPath , NewPath : String );
  292. var AResult : Boolean; { Internal data flag }
  293. begin
  294.   { If Copyfile returns false an error occurred }
  295.   AResult := CopyFile( OldPath , NewPath +
  296.    ExtractFileName( OldPath ));
  297.   { Display meaningful error message }
  298.   if not AResult then HandleDOSError( GlobalErrorType ,
  299.    ExtractFileName( OldPath ) , GlobalError );
  300. end;
  301.  
  302. { This procedure moves a file by copying and delete it }
  303. procedure TFileWorkBench.MoveTheFile( OldPath , NewPath : String );
  304. var AResult : Boolean; { Internal data flag }
  305.     TheFile : File;    { Use to get errors  }
  306. begin
  307.   { If Copyfile returns false an error occurred }
  308.   AResult := CopyFile( OldPath , NewPath +
  309.     ExtractFileName( OldPath ));
  310.   { Display meaningful error message }
  311.   if not AResult then HandleDOSError( GlobalErrorType ,
  312.    ExtractFileName( OldPath ), GlobalError );
  313.   { After valid copying, delete source file }
  314.   {$I+}
  315.   if AResult then try
  316.     { Use this trick to get valid exception handling }
  317.     AssignFile( TheFile , OldPath );
  318.     { Use erase because Deletefile doesn't give exceptions! }
  319.     Erase( TheFile );
  320.   except
  321.     { if any exception occurs instantiate exception and show }
  322.     On E:EInOutError do
  323.     begin
  324.       { Call custom error display/lookup procedure }
  325.       HandleIOException( EOC_DELETEFILE , ExtractFileName( OldPath ) ,
  326.        E.Message , E.ErrorCode );
  327.     end;
  328.   end;
  329. end;
  330.  
  331. { This procedure safely deletes a single file }
  332. procedure TFileWorkBench.DeleteTheFile( ThePath : String );
  333. var TheFile : File; { Internal file handle }
  334. begin
  335.   {$I+}
  336.   try
  337.     { Use this trick to get valid exception handling }
  338.     AssignFile( TheFile , ThePath );
  339.     { Use erase because Deletefile doesn't give exceptions! }
  340.     Erase( TheFile );
  341.   except
  342.     { if any exception occurs instantiate exception and show }
  343.     On E:EInOutError do
  344.     begin
  345.       { Call custom error display/lookup procedure }
  346.       HandleIOException( EOC_DELETEFILE , ExtractFileName( ThePath ) ,
  347.        E.Message , E.ErrorCode );
  348.     end;
  349.   end;
  350. end;
  351.  
  352. { This procedure renames a file with full error trapping }
  353. procedure TFileWorkBench.RenameTheFile( OldPath , NewName : String );
  354. var TheFile : File; { Internal file handle }
  355. begin
  356.   {$I+}
  357.   try
  358.     { Use this trick to get valid exception handling }
  359.     AssignFile( TheFile , OldPath );
  360.     { Use this because RenameFile doesn't give exceptions! }
  361.     Rename( TheFile , NewName );
  362.   except
  363.     { if any exception occurs instantiate exception and show }
  364.     On E:EInOutError do
  365.     begin
  366.       { Call custom error display/lookup procedure }
  367.       HandleIOException( EOC_RENAMEFILE , ExtractFileName( OldPath ) ,
  368.        E.Message , E.ErrorCode );
  369.     end;
  370.   end;
  371. end;
  372.  
  373. { This procedure creates a new directory with full error trapping }
  374. procedure TFileWorkBench.CreateNewDirectory( NewPath : String );
  375. begin
  376.   {$I+}
  377.   try
  378.     Mkdir( NewPath );
  379.   except
  380.     { if any exception occurs instantiate exception and show }
  381.     On E:EInOutError do
  382.     begin
  383.       { Call custom error display/lookup procedure }
  384.       HandleIOException( EOC_MAKEDIR , ExtractFileName( NewPath ) ,
  385.        E.Message , E.ErrorCode );
  386.     end;
  387.   end;
  388. end;
  389.  
  390. { This procedure remove a directory with full error trapping }
  391. procedure TFileWorkBench.RemoveDirectory( ThePath : String );
  392. begin
  393.   {$I+}
  394.   try
  395.     Rmdir( ThePath );
  396.   except
  397.     { if any exception occurs instantiate exception and show }
  398.     On E:EInOutError do
  399.     begin
  400.       { Call custom error display/lookup procedure }
  401.       HandleIOException( EOC_DELETEDIR , ExtractFileName( ThePath ) ,
  402.        E.Message , E.ErrorCode );
  403.     end;
  404.   end;
  405. end;
  406.  
  407. { This procedure recursively copies a directory to a new path }
  408. procedure TFileWorkBench.RecursivelyCopyDirectory( OldPath , NewPath : String );
  409. var TheDir : String; { Holds source directory }
  410. begin
  411.   { Get the source directory to copy }
  412.   TheDir := ExtractFileName( OldPath );
  413.   { Force a backslash to the newpath variable }
  414.   NewPath := ForceTrailingBackSlash( NewPath );
  415.   { Add the source directory to the target path }
  416.   NewPath := NewPath + TheDir;
  417.   { Create a new directory with the new name }
  418.   CreateNewDirectory( NewPath );
  419.   { Force a backslash for compatibility }
  420.   NewPath := FOrcetrailingBackSlash( NewPath );
  421.   { Do the recursive call }
  422.   HandleRecursiveAction( OldPath , NewPath , FAC_COPY );
  423. end;
  424.  
  425. { This procedure recursively moves a directory tree }
  426. procedure TFileWorkBench.RecursivelyMoveDirectory( OldPath , NewPath : String );
  427. var TheDir    : String; { Holds source directory  }
  428.     SavedPath : String; { Holds saved dir to kill }
  429. begin
  430.   { Get the source directory to move }
  431.   TheDir := ExtractFileName( OldPath );
  432.   { Force a backslash to the newpath variable }
  433.   NewPath := ForceTrailingBackSlash( NewPath );
  434.   { Save the starting path just in case }
  435.   SavedPath := OldPath;
  436.   { Add the source directory to the target path }
  437.   NewPath := NewPath + TheDir;
  438.   { Create a new directory with the new name }
  439.   CreateNewDirectory( NewPath );
  440.   { Force a backslash for compatibility }
  441.   NewPath := FOrcetrailingBackSlash( NewPath );
  442.   { Do the recursive call }
  443.   HandleRecursiveAction( OldPath , NewPath , FAC_MOVE );
  444.   { Remove the source directory }
  445.   RemoveDirectory( SavedPath );
  446. end;
  447.  
  448. { This procedure handles recursively deleting an entire directory tree }
  449. procedure TFileWorkBench.RecursivelyDeleteDirectory( ThePath : String );
  450. begin
  451.   HandleRecursiveAction( ThePath , '' , FAC_DELETE );
  452. end;
  453.  
  454.  
  455. { This is the generic routine to copy, move, and delete whole directory trees }
  456. procedure TFileWorkBench.HandleRecursiveAction( StartingPath , NewPath : String;
  457.            ActionCode : Integer );
  458. { VITAL!!! These variables MUST be local for recursrion to work! }
  459. var
  460.     Finished        : Boolean;         { Loop flag              }
  461.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  462.     TheResult       : Integer;         { return variable        }
  463.     TargetPath ,
  464.     FileMask   ,
  465.     TheWorkingDirectory ,
  466.     TheStoredWorkingDirectory ,
  467.     ModifiedDirectory  : String;       { path for FF/FN         }
  468.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  469.     ButtonColor   ,                    { main panel color       }
  470.     ButtonHLColor ,                    { bright panel color     }
  471.     ButtonSColor  ,                    { dark panel color       }
  472.     Textcolor       : TColor;          { label text color       }
  473.     TheFile         : File;
  474.  
  475. begin
  476.   { Set up the initial variables }
  477.   Finished := false;
  478.   TheWorkingDirectory := StartingPath;
  479.   TheStoredWorkingDirectory := TheWorkingDirectory;
  480.   TheWorkingDirectory := TheWorkingDirectory + '\*.*';
  481.   TargetPath := ExtractFilePath( TheWorkingDirectory );
  482.   { Make the call to FindFirst set to get any file }
  483.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  484.   { loop through all files in the directory and delete them }
  485.   while not Finished do
  486.   begin
  487.     { Make call to FindNext, using only SearchRecord from FindFirst }
  488.     TheResult := FindNext( TheSR );
  489.     { A -1 result means no more files so exit }
  490.     if TheResult <> 0 then finished := true else
  491.     begin
  492.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  493.        <> faDirectory ) then
  494.       begin { A File }
  495.         case ActionCode of
  496.           FAC_COPY :
  497.               begin
  498.                 CopyTheFile( TargetPath + TheSR.Name , NewPath );
  499.               end;
  500.           FAC_MOVE :
  501.               begin
  502.                 MoveTheFile( TargetPath + TheSR.Name , NewPath );
  503.               end;
  504.           FAC_DELETE :
  505.               begin { Delete }
  506.                 if MessageDlg( 'Delete file ' + TargetPath + TheSR.Name + '?',
  507.                    mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  508.                     DeleteTheFile( TargetPath + TheSR.Name );
  509.               end;
  510.         end;
  511.       end;
  512.     end;
  513.   end;
  514.   { Set up the variables to do recursive calls on all directories}
  515.   Finished := false;
  516.   ModifiedDirectory := TheStoredWorkingdirectory + '\*.*';
  517.   { Make the call to FindFirst set to get any file, ignore result }
  518.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  519.   while not Finished do
  520.   begin
  521.     { Make call to FindNext, using only SearchRecord from FindFirst }
  522.     TheResult := FindNext( TheSR );
  523.     { A -1 result means no more files so exit }
  524.     if TheResult <> 0 then
  525.       finished := true
  526.     else
  527.     begin
  528.       if TheSR.Name <> '..' then { Ignore backup in this case }
  529.       begin
  530.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  531.          = faDirectory ) then
  532.         begin
  533.           { Send in the new directory name }
  534.           ModifiedDirectory := TheStoredWorkingDirectory  + '\' +
  535.            TheSR.Name;
  536.           { Reproduce directory structure for recursion in copy/move }
  537.           NewPath := NewPath + TheSR.Name;
  538.           case ActionCode of
  539.             FAC_COPY , FAC_MOVE :
  540.                begin { Create ahead for move and copy }
  541.                  { Make the new directory for moving and copying }
  542.                  CreateNewDirectory( NewPath );
  543.                  { Force a backslash for compatibility }
  544.                  NewPath := ForceTrailingBackSlash( NewPath );
  545.                end;
  546.             FAC_DELETE :
  547.                begin  { No prior action needed for Delete }
  548.                end;
  549.           end;
  550.           { Do the recursive call }
  551.           HandleRecursiveAction( ModifiedDirectory , NewPath , ActionCode );
  552.           case ActionCode of
  553.             FAC_COPY :
  554.                begin { no action for copy }
  555.                end;
  556.             FAC_MOVE , FAC_DELETE :
  557.                begin  { Delete }
  558.                  { Get a confirmation }
  559.                  if MessageDlg( 'Remove Directory ' + TargetPath + TheSR.Name
  560.                   + '?', mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  561.                    RemoveDirectory( TargetPath + TheSR.Name );
  562.                end;
  563.           end;
  564.         end;
  565.       end;
  566.     end;
  567.   end;
  568. end;
  569.  
  570. { This is a generic copy routine taken from Delphi sample code }
  571. { It has been edited to return viable error codes!             }
  572. procedure TFileWorkBench.FMXUCopyFile(const FileName, DestName: String);
  573. var
  574.   CopyBuffer: Pointer; { buffer for copying }
  575.   BytesCopied: Longint;
  576.   TheAttr : Integer;
  577.   Source, Dest: Integer; { handles }
  578. const
  579.   ChunkSize: Longint = 8192; { copy in 8K chunks }
  580. begin
  581.   GetMem(CopyBuffer, ChunkSize); { allocate the buffer }
  582.   Source := FileOpen(FileName, fmShareDenyWrite); { open source file }
  583.   if Source < 0 then
  584.   begin  { error creating source file }
  585.     GlobalErrorType := EOC_SOURCECOPY;
  586.     GlobalError := -IOResult;
  587.     if GlobalError = 0 then GlobalError := -157;
  588.     FreeMem( CopyBuffer, ChunkSize );
  589.     exit;
  590.   end;
  591.   Dest := FileCreate(DestName); { create output file; overwrite existing }
  592.   if Dest < 0 then
  593.   begin  { error creating destination file }
  594.     FileClose( Source );
  595.     GlobalErrorType := EOC_DESTCOPY;
  596.     GlobalError := -IOResult;
  597.     if GlobalError = 0 then GlobalError := -159;
  598.     FreeMem( CopyBuffer , ChunkSize );
  599.     exit;
  600.   end;
  601.   {$I-}
  602.   repeat
  603.     BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk}
  604.     if BytesCopied > 0 then { if we read anything... }
  605.     FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }
  606.   until BytesCopied < ChunkSize; { until we run out of chunks }
  607.   {$I+}
  608.   GlobalError := -IOResult;  { get any error code which happens during copying }
  609.   FileClose(Dest); { close the destination file }
  610.   FileClose(Source); { close the source file }
  611.   FreeMem(CopyBuffer, ChunkSize); { free the buffer }
  612. end;
  613.  
  614. { This function calls the sample Copy code and handles errors }
  615. function TFileWorkBench.CopyFile( TargetPath ,
  616.           DestinationPath : String ) : Boolean;
  617. begin
  618.   { Set global error value to no error }
  619.   GlobalError := 0;
  620.   { Call the sample procedure to do the copy }
  621.   FMXUCopyFile( TargetPath, DestinationPath );
  622.   { If no error return true else return false }
  623.   if GlobalError < 0 then CopyFile := false else
  624.    CopyFile := true;
  625. end;
  626.  
  627. { This procedure handles displaying a user-friendly Dialog box with a }
  628. { Message for Delphi IO exception errors.                             }
  629. procedure TFileWorkBench.HandleIOException( TheOpCode : Integer;
  630.            ThePath : String; TheMessage : String; TheCode : Integer );
  631. var ErrorMessageString : String;  { Holds internal data }
  632.     OperationString    : String;  { Holds internal data }
  633. begin
  634.   { clear to check for unrecognized code }
  635.   ErrorMessageString := '';
  636.   { Check against imported code }
  637.   case TheCode of
  638.     2    : ErrorMessageString := 'File not found';
  639.     3    : ErrorMessageString := 'Path not found';
  640.     4    : ErrorMessageString := 'Too many open files';
  641.     5    : ErrorMessageString := 'File access denied';
  642.     6    : ErrorMessageString := 'Invalid file handle';
  643.     12    : ErrorMessageString := 'Invalid file access code';
  644.     15    : ErrorMessageString := 'Invalid drive number';
  645.     16  : ErrorMessageString := 'Cannot remove current directory';
  646.     17    : ErrorMessageString := 'Cannot rename across drives';
  647.     100    : ErrorMessageString := 'Disk read error';
  648.     101    : ErrorMessageString := 'Disk write error';
  649.     102    : ErrorMessageString := 'File not assigned';
  650.     103    : ErrorMessageString := 'File not open';
  651.     104    : ErrorMessageString := 'File not open for input';
  652.     105    : ErrorMessageString := 'File not open for output';
  653.   end;
  654.   case TheOpCode of
  655.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  656.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  657.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  658.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  659.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  660.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  661.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  662.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  663.   end;
  664.   { If not recognized use message; not a DOS error; reset cursor for neatness }
  665.   if ErrorMessageString = '' then
  666.   begin
  667.     Screen.Cursor := crDefault;
  668.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  669.      TheMessage , mtError , [mbOK],0);
  670.   end
  671.   else
  672.   begin
  673.     { Recognized DOS exception, reset cursor for neatness }
  674.     Screen.Cursor := crDefault;
  675.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' ' +
  676.      ErrorMessageString , mtError , [mbOK], 0 );
  677.   end;
  678. end;
  679.  
  680. { This procedure handles displaying a user-friendly Dialog box with a }
  681. { Message for DOS error codes.                                        }
  682. procedure TFileWorkBench.HandleDOSError( TheOpCode : Integer;
  683.            ThePath : String;  TheCode : Integer );
  684. var ErrorMessageString : String;  { internal message holder }
  685.     OperationString : String;     { internal message holder }
  686. begin
  687.   { clear the message holder to check for unrecognized code }
  688.   ErrorMessageString := '';
  689.   { Negate the code back to normal number and check to set string }
  690.   case -TheCode of
  691.     2    : ErrorMessageString := 'File not found';
  692.     3    : ErrorMessageString := 'Path not found';
  693.     4    : ErrorMessageString := 'Too many open files';
  694.     5    : ErrorMessageString := 'File access denied';
  695.     6    : ErrorMessageString := 'Invalid file handle';
  696.     12    : ErrorMessageString := 'Invalid file access code';
  697.     15    : ErrorMessageString := 'Invalid drive number';
  698.     16  : ErrorMessageString := 'Cannot remove current directory';
  699.     17    : ErrorMessageString := 'Cannot rename across drives';
  700.     100    : ErrorMessageString := 'Disk read error';
  701.     101    : ErrorMessageString := 'Disk write error';
  702.     102    : ErrorMessageString := 'File not assigned';
  703.     103    : ErrorMessageString := 'File not open';
  704.     104    : ErrorMessageString := 'File not open for input';
  705.     105    : ErrorMessageString := 'File not open for output';
  706.     157 : ErrormessageString := 'Could not open Source File';
  707.     159 : ErrormessageString := 'Could not open Target File';
  708.   end;
  709.   case TheOpCode of
  710.     EOC_CHANGEDIR : OperationString := 'Unable to Change Directory due to ';
  711.     EOC_SOURCECOPY : OperationString := 'Unable to Copy due to Source File ';
  712.     EOC_DESTCOPY : OperationString := 'Unable to Copy due to Destination File ';
  713.     EOC_DELETEFILE : OperationString := 'Unable to Delete File due to ';
  714.     EOC_DELETEDIR : OperationString := 'Unable to Delete Directory due to ';
  715.     EOC_RENAMEFILE : OperationString := 'Unable to Rename File due to ';
  716.     EOC_MAKEDIR : OperationString := 'Unable to Create New Directory due to ';
  717.     EOC_SETATTR : OperationString := 'Unable to Set File Attributes due to ';
  718.   end;
  719.   { If the string is empty an unrecognized code was sent in }
  720.   if ErrorMessageString = '' then
  721.   begin
  722.     { Sent up db based on source or target error; reset cursor for neatness }
  723.     Screen.Cursor := crDefault;
  724.     MessageDlg( OperationString + ExtractFileName( ThePath ) + ' Error Code: ' +
  725.      IntToStr( TheCode ) , mtError , [mbOK],0);
  726.   end
  727.   else  { Code is recognized, use message from case statement }
  728.   begin
  729.     { Format the output for source or target error }
  730.     Screen.Cursor := crDefault;
  731.     MessageDlg( OperationString + ExtractFilePath( ThePath ) + ' ' +
  732.      ErrorMessageString , mtError , [mbOK], 0 );
  733.   end;
  734. end;
  735.  
  736. { This procedure sets the imported booleans to the file's attributes }
  737. procedure TFileWorkBench.GetFileAttributes( TheFile : String; var IsDirectory ,
  738.            IsArchive , IsVolumeID , IsHidden , IsReadOnly ,
  739.             IsSysFile : Boolean );
  740. var TheResult : Integer; { Traps for error code on VolumeID }
  741. begin
  742.   { Clear the imported flags for default }
  743.   IsDirectory := false;
  744.   IsArchive := false;
  745.   IsVolumeID := false;
  746.   IsHidden := False;
  747.   IsReadOnly := false;
  748.   IsSysFile := false;
  749.   { Make the Dos call }
  750.   TheResult := FileGetAttr( TheFile );
  751.   if TheResult < 0 then
  752.   begin
  753.     { Volume ID returns -2 (?) }
  754.     IsVolumeID := true;
  755.     { It has no other properties }
  756.     exit;
  757.   end;
  758.   { Use AND test to set all other properties }
  759.   if (( TheResult and faDirectory ) = faDirectory ) then IsDirectory := true;
  760.   if (( TheResult and faArchive ) = faArchive ) then IsArchive := true;
  761.   if (( TheResult and faVolumeID ) = faVolumeID ) then IsVolumeID := true;
  762.   if (( TheResult and faReadOnly ) = faReadOnly ) then IsReadOnly := true;
  763.   if (( TheResult and faHidden ) = faHidden ) then IsHidden := true;
  764.   if (( TheResult and faSysFile ) = faSysFile ) then IsSysFile := true;
  765. end;
  766.  
  767. { This function makes sure a pathname has a trailing \ }
  768. function TFileWorkBench.ForceTrailingBackSlash(
  769.           const TheFileName : String ) : String;
  770. var TempString : String;  { Used to hold function result }
  771. begin
  772.   { If no trailing \ add one (root will already have one.) }
  773.   if TheFileName[ Length( TheFileName ) ] <> '\' then
  774.    TempString := TheFileName + '\' else TempString := TheFileName;
  775.   { Return modified or non-modified string }
  776.   ForceTrailingBackslash := TempString;
  777. end;
  778.  
  779. { This function makes sure a non-root dir has no trailing \ }
  780. function TFileWorkBench.StripNonRootTrailingBackSlash(
  781.           const TheFileName : String ) : String;
  782. var TempString : String ; { Used to hold function result }
  783. begin
  784.   { Default is no change }
  785.   TempString := TheFileName;
  786.   { If not root then }
  787.   if Length( TheFileName ) > 3 then
  788.   begin
  789.     { If has a trailing backslash remove it }
  790.     if TheFileName[ Length( TheFileName )] = '\' then
  791.     begin
  792.       TempString := Copy( TheFileName , 1 ,
  793.        Length( TheFileName ) - 1 );
  794.     end;
  795.   end;
  796.   { Export the final result }
  797.   StripNonRootTrailingBackSlash := TempString;
  798. end;
  799.  
  800. { This gets the next selected listbox item }
  801. function TIconFileListBox.GetNextSelection( SourceDirectory : String;
  802.           var CurrentItem : Integer ): String;
  803. var TheResult : String;  { Internal storage }
  804.     finished  : boolean; { Loop flag        }
  805. begin
  806.   { If out of items to check signal and exit }
  807.   if CurrentItem > Items.Count then TheResult := '' else
  808.   begin
  809.     { Otherwise scan from current position till match or end }
  810.     finished := false;
  811.     while not finished do
  812.     begin
  813.       { Check against selected property }
  814.       if Selected[ CurrentItem - 1 ] then
  815.       begin
  816.         { If selected then return it and abort loop }
  817.         TheResult := SourceDirectory + Items[ CurrentItem - 1 ];
  818.         finished := true;
  819.         { Increment current position }
  820.         CurrentItem := CurrentItem + 1;
  821.      end
  822.       else
  823.       begin
  824.         { Increment current position }
  825.         CurrentItem := CurrentItem + 1;
  826.         { Otherwise check for end of data and abort if out of entries }
  827.         if CurrentItem > Items.Count then
  828.         begin
  829.           TheResult := '';
  830.           finished := true;
  831.         end;
  832.       end;
  833.     end;
  834.   end;
  835.   { Return stored result }
  836.   GetNextSelection := TheResult;
  837. end;
  838.  
  839. { Create method for FIP                                }
  840. constructor TFileIconPanel.Create( AOwner : TComponent );
  841. begin
  842.   { call inherited -- VITAL! }
  843.   inherited Create( AOwner );
  844.   { create icon and label components, making self owner/displayer }
  845.   FTheIcon := TIcon.Create;
  846.   FTheLabel := TLabel.Create( Self );
  847.   FThelabel.Parent := Self;
  848.   { Set own and labels mouse methods to stored methods }
  849.   OnClick := TheClick;
  850.   OnDblClick := TheDblClick;
  851.   FTheLabel.OnClick := TheClick;
  852.   FTheLabel.OnDblClick := TheDblClick;
  853.   { Set alignment and autosize properties of the label }
  854.   FTheLabel.Autosize := false;
  855.   FTheLabel.Alignment := taCenter;
  856.   { Set selected to false }
  857.   Selected := false;
  858. end;
  859.  
  860. { Initialization method for FIP                                         }
  861. procedure TFileIconPanel.Initialize( PanelX              ,
  862.                                      PanelY              ,
  863.                                      PanelWidth          ,
  864.                                      PanelHeight         ,
  865.                                      PanelBevelWidth     ,
  866.                                      LabelFontSize         : Integer;
  867.                                      PanelColor          ,
  868.                                      PanelHighlightColor ,
  869.                                      PanelShadowColor    ,
  870.                                      LabelTextColor        : TColor;
  871.                                      TheFilename         ,
  872.                                      LabelFontName         : String;
  873.                                      LabelFontStyle        : TFontStyles;
  874.                                      ExtraData             : Integer );
  875.  
  876. var TheLabelHeight ,             { Holder for label pixel height }
  877.     TheLabelWidth    : Integer;  { Holder for label pixel width  }
  878.     TheOtherPChar    : PChar;    { Windows ASCIIZ string         }
  879. begin
  880.   { Set the basic properties based on imported parameters }
  881.   Left := PanelX;
  882.   Top := PanelY;
  883.   Width := PanelWidth;
  884.   Height := PanelHeight;
  885.   Color := PanelColor;
  886.   BevelWidth := PanelBevelWidth;
  887.   FHighlightColor := PanelHighlightColor;
  888.   FShadowColor := PanelShadowColor;
  889.   FTheName := TheFilename;
  890.   { If the ExtraData field is non-0 then a drive is being sent in }
  891.   if ExtraData <> 0 then
  892.   begin
  893.     { Use the data field value to determine which icon to get from RES file }
  894.     case ExtraData of
  895.       1 : begin
  896.             GetMem( TheOtherPChar , 255 );
  897.             StrPCopy( TheOtherPChar , 'FLOPPY35' );
  898.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  899.             FreeMem( TheOtherPChar , 255 );
  900.           end;
  901.       2 : begin
  902.             GetMem( TheOtherPChar , 255 );
  903.             StrPCopy( TheOtherPChar , 'FIXEDHD' );
  904.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  905.             FreeMem( TheOtherPChar , 255 );
  906.           end;
  907.       3 : begin
  908.             GetMem( TheOtherPChar , 255 );
  909.             StrPCopy( TheOtherPChar , 'NETWORKHD' );
  910.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  911.             FreeMem( TheOtherPChar , 255 );
  912.           end;
  913.       4 : begin
  914.             GetMem( TheOtherPChar , 255 );
  915.             StrPCopy( TheOtherPChar , 'CDROM' );
  916.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  917.             FreeMem( TheOtherPChar , 255 );
  918.           end;
  919.       5 : begin
  920.             GetMem( TheOtherPChar , 255 );
  921.             StrPCopy( TheOtherPChar , 'RAM' );
  922.             FTheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
  923.             FreeMem( TheOtherPChar , 255 );
  924.           end;
  925.     end;
  926.     { The FileNme property is already set up for the caption; use directly }
  927.     FTheLabel.Caption := TheFilename;
  928.     { Set up the hint for later use (make sure to set ShowHint) }
  929.     Hint := 'Change to ' + TheFileName;
  930.     ShowHint := true;
  931.     { Set up all imported label properties and center it for drawing }
  932.     with FTheLabel do
  933.     begin
  934.       Font.Name := LabelFontName;
  935.       Font.Size := LabelFontSize;
  936.       Font.Style := LabelFontStyle;
  937.       Font.Color := LabelTextColor;
  938.       Canvas.Brush.Color := PanelColor;
  939.       Canvas.Font := Font;
  940.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  941.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  942.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  943.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  944.       Top := Top + Round( Self.Height * 0.75 );
  945.       Height := TheLabelHeight;
  946.       Width := TheLabelWidth;
  947.     end;
  948.   end
  949.   else
  950.   begin
  951.     { A file or directory has been sent in; use GetIconForFile to obtain an }
  952.     { icon either from the file, its owner, or a RES file default.          }
  953.     GetIconForFile( FTheName , FTheIcon );
  954.     { Check for the Backup caption and set it specially }
  955.     if ExtractfileName( FThename ) = '..' then
  956.     begin
  957.       FTheLabel.Caption := '..';
  958.       Hint := 'Up One Level';
  959.     end
  960.     else
  961.     begin
  962.       { Otherwise just get the filename for the label caption }
  963.       { And the full path for the hint (used later.)          }
  964.       FTheLabel.caption := ExtractFileName( UpperCase( FTheName ));
  965.       Hint := FTheName;
  966.     end;
  967.     { Activate showhint so hints are seen }
  968.     ShowHint := true;
  969.     { Set label properties with imported values and center for display }
  970.     with FTheLabel do
  971.     begin
  972.       Font.Name := LabelFontName;
  973.       Font.Size := LabelFontSize;
  974.       Font.Style := LabelFontStyle;
  975.       Font.Color := LabelTextColor;
  976.       Canvas.Brush.Color := PanelColor;
  977.       Canvas.Font := Font;
  978.       TheLabelHeight := Canvas.Textheight( Caption ) + 4;
  979.       TheLabelWidth := Canvas.Textwidth( Caption ) + 4;
  980.       Left := (( Self.Width - TheLabelWidth ) div 2 ) + 1;
  981.       Top := ((( Round( Self.Height * 0.25 ) - 6 ) - TheLabelHeight) div 2) + 1;
  982.       Top := Top + Round( Self.Height * 0.75 );
  983.       Height := TheLabelHeight;
  984.       Width := TheLabelWidth;
  985.     end;
  986.   end;
  987. end;
  988.  
  989. { Destroy method for FIP }
  990. destructor TFileIconPanel.Destroy;
  991. begin
  992.   { free component resources }
  993.   FTheIcon.Free;
  994.   FTheLabel.Free;
  995.   { call inherited -- VITAL! }
  996.   inherited Destroy;
  997. end;
  998.  
  999. { TheClick method for FIP; used for event responses }
  1000. procedure TFileIconPanel.TheClick( Sender : TObject );
  1001. begin
  1002.   { Currently ignore drive clicks }
  1003.   if Pos( 'DRIVE' , FTheName ) > 0 then exit;
  1004.   { Flip status of bevels }
  1005.   if BevelOuter = bvRaised then BevelOuter := bvLowered else
  1006.    BevelOuter := bvRaised;
  1007.   { Flip selected variable }
  1008.   Selected := not Selected;
  1009.   { Set redisplay }
  1010.   Invalidate;
  1011. end;
  1012.  
  1013. { TheDblClick method for FIP; used for event responses }
  1014. procedure TFileIconPanel.TheDblClick( Sender : TObject );
  1015. var CurrentDirectory : String;    { Use to store dirs }
  1016.     TheDrive         : String;    { Get drive letter  }
  1017.     WhichDrive       : Integer;   { Get drive number  }
  1018.     ErrorCheck       : Integer;
  1019.     TheFWB           : TFileWorkBench;
  1020. begin
  1021.   { Create FileWorkBench for later use }
  1022.   TheFWB := TFileWorkBench.Create( Self );
  1023.   { Check for label or FIP sender }
  1024.   if Sender is TFileIconPanel then
  1025.   begin
  1026.     if FTheLabel.Caption = '..' then
  1027.     begin { deal with backup request }
  1028.       { Change to new directory }
  1029.       TheFWB.ChangeTheDirectory( '..' );
  1030.       { Call special method due to SendMessage problem! }
  1031.       TFileIconPanelScrollBox( Parent ).Update;
  1032.     end
  1033.     else
  1034.     begin
  1035.       { Check for DRIVE id in name }
  1036.       if Pos( 'DRIVE' , FTheName ) <> 0 then
  1037.       begin { Double Click on a Drive Icon }
  1038.         { Pull out the letter from name }
  1039.         TheDrive := Copy( FtheName , 7 , 1 );
  1040.         { Convert it to a number }
  1041.         WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1042.         TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1043.         { Call special method due to SendMessage problem! }
  1044.         TFileIconPanelScrollBox( Parent ).Update;
  1045.       end
  1046.       else
  1047.       begin { Double click on a dir/file icon }
  1048.         if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1049.         begin { A directory, change to it }
  1050.           { Since full path in name, simply change to it! }
  1051.           TheFWB.ChangeTheDirectory( FTheName );
  1052.           { Call special method due to SendMessage problem! }
  1053.           TFileIconPanelScrollBox( Parent ).Update;
  1054.         end
  1055.         else
  1056.         begin { A file; attempt to shellexecute it }
  1057.           { Call shellexec as a wrapper around ShellExecute API call }
  1058.           { False indicates failure, signal error                    }
  1059.           if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL , false )
  1060.            then MessageDlg('Could not Shell out to ' + FTheName , mtError,
  1061.             [mbOK], 0);
  1062.         end;
  1063.       end;
  1064.     end;
  1065.   end
  1066.   else
  1067.   begin
  1068.     with Sender as TLabel do
  1069.     begin
  1070.       if Caption = '..' then
  1071.       begin { Deal with backup request }
  1072.         { Change to new directory }
  1073.         TheFWB.ChangeTheDirectory( '..' );
  1074.         { Call special method due to SendMessage problem! }
  1075.         TFileIconPanelScrollBox( Parent ).Update;
  1076.       end
  1077.       else
  1078.       begin
  1079.         with Parent as TFileIconPanel do
  1080.         begin
  1081.           { Check for DRIVE id in name }
  1082.           if Pos( 'DRIVE' , FTheName ) <> 0 then
  1083.           begin { Double Click on a Drive Icon }
  1084.             { Pull out the letter from name }
  1085.             TheDrive := Copy( FtheName , 7 , 1 );
  1086.             { Convert it to a number }
  1087.             WhichDrive := ( Ord( TheDrive[ 1 ] ) - Ord( 'A' )) + 1;
  1088.             { Call the method to change to default dir on new drive }
  1089.             TheFWB.ChangeTheDriveAndDirectory( WhichDrive );
  1090.             { Call special method due to SendMessage problem! }
  1091.             TFileIconPanelScrollBox( Parent ).Update;
  1092.           end
  1093.           else
  1094.           begin { Double click on a dir/file icon }
  1095.             if (( FileGetAttr( FTheName ) and faDirectory ) = faDirectory ) then
  1096.             begin { A directory, change to it }
  1097.               { Since full path in name, simply change to it! }
  1098.               TheFWB.ChangeTheDirectory( FTheName );
  1099.               { Call special method due to SendMessage problem! }
  1100.               TFileIconPanelScrollBox( Parent ).Update;
  1101.             end
  1102.             else
  1103.             begin { A file; attempt to shellexecute it }
  1104.               { Call shellexec as a wrapper around ShellExecute API call }
  1105.               { False indicates failure, signal error                    }
  1106.               if not ShellExec( FTheName , '' , '', false , SW_SHOWNORMAL ,
  1107.                false ) then MessageDlg('Could not Shell out to ' + FTheName ,
  1108.                 mtError, [mbOK], 0);
  1109.             end;
  1110.           end;
  1111.         end;
  1112.       end;
  1113.     end;
  1114.   end;
  1115.   TheFWB.Free; { This prevents resource leak }
  1116. end;
  1117.  
  1118. { Paint method for FIP; overrides normal paint }
  1119. procedure TFileIconPanel.Paint;
  1120. var
  1121.   TheOtherRect   : TRect;   { Holds clientrect   }
  1122.   TopColor     ,            { Holds bright color }
  1123.   BottomColor    : TColor;  { Holds dark color   }
  1124.  
  1125. { These methods are from Borland Intl., copyright 1995 }
  1126. procedure Frame3D(    Canvas       : TCanvas;
  1127.                   var TheRect      : TRect;
  1128.                       TopColor   ,
  1129.                       BottomColor  : TColor;
  1130.                       Width        : Integer );
  1131.  
  1132. procedure DoRect;
  1133. var
  1134.   TopRight, BottomLeft: TPoint;
  1135. begin
  1136.   with Canvas, TheRect do
  1137.   begin
  1138.     TopRight.X := Right;
  1139.     TopRight.Y := Top;
  1140.     BottomLeft.X := Left;
  1141.     BottomLeft.Y := Bottom;
  1142.     Pen.Color := TopColor;
  1143.     PolyLine([BottomLeft, TopLeft, TopRight]);
  1144.     Pen.Color := BottomColor;
  1145.     Dec(BottomLeft.X);
  1146.     PolyLine([TopRight, BottomRight, BottomLeft]);
  1147.   end;
  1148. end;
  1149.  
  1150. begin
  1151.   Canvas.Pen.Width := 1;
  1152.   Dec(TheRect.Bottom); Dec(TheRect.Right);
  1153.   while Width > 0 do
  1154.   begin
  1155.     Dec(Width);
  1156.     DoRect;
  1157.     InflateRect(TheRect, -1, -1);
  1158.   end;
  1159.   Inc(TheRect.Bottom); Inc(TheRect.Right);
  1160. end;
  1161.  
  1162. procedure AdjustColors(Bevel: TPanelBevel);
  1163. begin
  1164.   TopColor := FHighlightColor;
  1165.   if Bevel = bvLowered then TopColor := FShadowColor;
  1166.   BottomColor := FShadowColor;
  1167.   if Bevel = bvLowered then BottomColor := FHighlightColor;
  1168. end;
  1169.  
  1170. { Custom code begins here }
  1171. begin
  1172.   { Get the rectangle of the control with API/method call }
  1173.   TheOtherRect := GetClientRect;
  1174.   { draw basic rectangle with basic color }
  1175.   with Canvas do
  1176.   begin
  1177.     Brush.Color := Color;
  1178.     FillRect(TheOtherRect);
  1179.   end;
  1180.   { Set up for top "icon" frame  and draw it with frame3d }
  1181.   TheOtherRect.Right := Width;
  1182.   TheOtherRect.Bottom := Round( Height * 0.75 ) - 6 ;
  1183.   if BevelOuter <> bvNone then
  1184.   begin
  1185.     AdjustColors(BevelOuter);
  1186.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1187.   end;
  1188.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1189.   if BevelInner <> bvNone then
  1190.   begin
  1191.     AdjustColors(BevelInner);
  1192.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1193.   end;
  1194.   { Do the same for the lower "label" frame }
  1195.   TheOtherRect.Top := Round( Height * 0.75 ) - 5;
  1196.   TheOtherRect.Left := 0;
  1197.   TheOtherRect.Bottom := Height;
  1198.   TheOtherRect.Right := Width;
  1199.   if BevelOuter <> bvNone then
  1200.   begin
  1201.     AdjustColors(BevelOuter);
  1202.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1203.   end;
  1204.   Frame3D(Canvas, TheOtherRect, Color, Color, BorderWidth);
  1205.   if BevelInner <> bvNone then
  1206.   begin
  1207.     AdjustColors(BevelInner);
  1208.     Frame3D(Canvas, TheOtherRect, TopColor, BottomColor, BevelWidth);
  1209.   end;
  1210.   { Then draw the icon using canvas draw method }
  1211.   Canvas.Draw( (( Width - 32 ) div 2 ) + 1 ,
  1212.   ((( Round( Height * 0.75 ) - 6 ) - 32 ) div 2 ) + 1 , FTheIcon );
  1213. end;
  1214.  
  1215. { This procedure clears a scrollbox of all FileIconPanels }
  1216. procedure TFileIconPanelScrollbox.ClearTheFIPs;
  1217. var Counter_1 : Integer;
  1218.     TheComponent : TComponent;
  1219. begin
  1220.   { Note that must use while loop since component count continually }
  1221.   { decreases as removes are made!                                  }
  1222.   while ComponentCount > 0 do
  1223.   begin
  1224.     { Save the component as a generic TComponent }
  1225.     TheComponent := Components[ 0 ];
  1226.     { Call removecomponent to pull it out of the owner list for sb }
  1227.     { This avoids GPF when freeing the sb.                         }
  1228.     RemoveComponent( Components[ 0 ]);
  1229.     { Typecast the pointer and free it to release memory and res. }
  1230.     TFileIconPanel( TheComponent ).Free;
  1231.   end;
  1232. end;
  1233.  
  1234. { This procedure scans for drives and obtains their type and creates file }
  1235. { icon panels to represent them.                                          }
  1236. procedure TFileIconPanelScrollBox.AddDriveIcons( var XCounter ,
  1237.            YCounter : Integer );
  1238. type
  1239.   { This if from filectrl unit; reproduce here for completeness }
  1240.   TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,
  1241.                 dtRAM);
  1242. var
  1243.   DrivePC         : array[0..256] of char;
  1244.   DriveNum        : Integer;         { Used to get next drive via DOS fn   }
  1245.   IconType        : Integer;         { Used to hold icon type (defacto dt) }
  1246.   DriveChar       : Char;            { Used to hold drive letter           }
  1247.   DriveType       : TDriveType;      { Used for set-valued drive type      }
  1248.   Finished        : Boolean;         { Loop flag                           }
  1249.   TheFIP          : TFileIconPanel;  { Generic FileIconPanel variable      }
  1250.   ButtonColor   ,                    { Main panel color                    }
  1251.   ButtonHLColor ,                    { Bright panel color                  }
  1252.   ButtonSColor  ,                    { Dark panel color                    }
  1253.   Textcolor       : TColor;          { Label text color                    }
  1254.  
  1255. (*{ This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1256. { Check whether drive is a CD-ROM.  Returns True if MSCDEX is installed }
  1257. {  and the drive is using a CD driver                                   }
  1258.  
  1259. function IsCDROM(DriveNum: Integer): Boolean; assembler;
  1260. asm
  1261.   MOV   AX,1500h { look for MSCDEX }
  1262.   XOR   BX,BX
  1263.   INT   2fh
  1264.   OR    BX,BX
  1265.   JZ    @Finish
  1266.   MOV   AX,150Bh { check for using CD driver }
  1267.   MOV   CX,DriveNum
  1268.   INT   2fh
  1269.   OR    AX,AX
  1270.   @Finish:
  1271. end;
  1272.  
  1273. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1274. { Check whether drive is a RAM drive.                                   }
  1275. function IsRAMDrive(DriveNum: Integer): Boolean; assembler;
  1276. var
  1277.   TempResult: Boolean;
  1278. asm
  1279.   MOV   TempResult,False
  1280.   PUSH  DS
  1281.   MOV   BX,SS
  1282.   MOV   DS,BX
  1283.   SUB   SP,0200h
  1284.   MOV   BX,SP
  1285.   MOV   AX,DriveNum
  1286.   MOV   CX,1
  1287.   XOR   DX,DX
  1288.   INT   25h  { read boot sector }
  1289.   ADD   SP,2
  1290.   JC    @ItsNot
  1291.   MOV   BX,SP
  1292.   CMP   BYTE PTR SS:[BX+15h],0F8h  { reverify fixed disk }
  1293.   JNE   @ItsNot
  1294.   CMP   BYTE PTR SS:[BX+10h],1  { check for single FAT }
  1295.   JNE   @ItsNot
  1296.   MOV   TempResult,True
  1297.   @ItsNot:
  1298.   ADD   SP,0200h
  1299.   POP   DS
  1300.   MOV   AL, TempResult
  1301. end;
  1302.  
  1303. { This code is from the FileCtrl Unit; copyright Borland Intl 1995      }
  1304. { Finds the type of a drive letter.                                     }
  1305. function FindDriveType(DriveNum: Integer): TDriveType;
  1306. begin
  1307.   Result := TDriveType(GetDriveType(DriveNum));
  1308.   if (Result = dtFixed) or (Result = dtNetwork) then
  1309.   begin
  1310.     if IsCDROM(DriveNum) then Result := dtCDROM
  1311.     else if (Result = dtFixed) then
  1312.     begin
  1313.         { do not check for RAMDrive under Windows NT }
  1314.       if ((GetWinFlags and $4000) = 0) and IsRAMDrive(DriveNum) then
  1315.         Result := dtRAM;
  1316.     end;
  1317.   end;
  1318. end;*)
  1319.  
  1320. begin
  1321.   { Set the button colors to an aquamarine color scheme for drives }
  1322.   ButtonColor := clTeal;
  1323.   ButtonHLColor := clAqua;
  1324.   ButtonSColor := clNavy;
  1325.   TextColor := clblack;
  1326.   { Set initial variables before looping for all drives }
  1327.   finished := false;
  1328.   DriveNum := 0;
  1329.   while not finished do
  1330.   begin
  1331.     { Start with no drive found }
  1332.     IconType := 0;
  1333.     (*=============REMOVED DUE TO WINDOWS 95=========
  1334.     { Call the Borland method to get the drive info }
  1335.     DriveType := FindDriveType(DriveNum);
  1336.     ===============END WINDOWS 95 REMOVAL==========*)
  1337.     { Set its letter and make it uppercase }
  1338.     DriveChar := Chr(DriveNum + ord('a'));
  1339.     DriveChar := Upcase(DriveChar);
  1340.     StrPCopy( DrivePC , DriveChar + ':\' );
  1341.     {*&&&&&&&&&&&&&&&  WIN 95 CALL  &&&&&&&&&&&&&&&&&&&*}
  1342.     DriveType := TDriveType(GetDriveType( DrivePC ));
  1343.     { Assign an icon based on the drive type; if no drive exists type is nil }
  1344.     case DriveType of
  1345.       dtFloppy  : IconType := 1;
  1346.       dtFixed   : IconType := 2;
  1347.       dtNetwork : IconType := 3;
  1348.       dtCDROM   : IconType := 4;
  1349.       dtRAM     : IconType := 5;
  1350.     end;
  1351.     { Set to check next drive letter }
  1352.     DriveNum := DriveNum + 1;
  1353.     { But if no match then out of drives so set exit flag }
  1354.     if IconType = 0 then finished := true;
  1355.     { If drive was valid then set up the new FileIconPanel on the imported }
  1356.     { Scrollbox                                                            }
  1357.     if not finished then
  1358.     begin
  1359.       { Create the FileIconPanel and set its parent for memory mgmt and display}
  1360.       TheFIP := TFileIconPanel.Create( Self );
  1361.       TheFIP.Parent := Self;
  1362.       { Call its initialize method with imported position values and the   }
  1363.       { preset color scheme, a drive caption, and a minimum font. Note the }
  1364.       { setting of the ExtraData field to non-zero; this signals a drive   }
  1365.       { rather than a file being sent in.                                  }
  1366.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1367.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1368.         7 , ButtonColor, ButtonHLColor,
  1369.        ButtonSColor , TextColor , 'DRIVE ' + DriveChar + ':' , 'MS Serif' , [] ,
  1370.        IconType );
  1371.       { Increment the column counter; if it exceeds max move to new row      }
  1372.       { Note that these are 'var' parameters and will export final position. }
  1373.       XCounter := XCounter + 1;
  1374.       if XCounter > MaxIconsInARow then
  1375.       begin
  1376.         XCounter := 1;
  1377.         YCounter := YCounter + 1;
  1378.       end;
  1379.     end;
  1380.   end;
  1381. end;
  1382.  
  1383. { This procedure assigns colors to FIP's based on file attributes }
  1384. procedure TFileIconPanelScrollBox.GetColorsForFileIcon( TheFile : String;
  1385.            var BC , HC , SC , TC : TColor );
  1386. var AmADir      ,             { Booleans hold file attribs }
  1387.     AmAnArchive ,
  1388.     AmAVolumeId ,
  1389.     AmHidden    ,
  1390.     AmReadOnly  ,
  1391.     AmSystem      : Boolean;
  1392. begin
  1393.   { Make the call to internal fileworkbench to set attributes }
  1394.   TheFWB.GetFileAttributes( TheFile , AmADir , AmAnArchive , AmAVolumeId ,
  1395.    AmHidden , AmReadOnly , AmSystem );
  1396.   { Volume ID has no subtypes }
  1397.   if AmAVolumeID then
  1398.   begin
  1399.     BC := clOlive;
  1400.     HC := clYellow;
  1401.     SC := clBlack;
  1402.     TC := clWhite;
  1403.     exit;
  1404.   end;
  1405.   { Check all directory combinations }
  1406.   if AmADir then
  1407.   begin
  1408.     BC := clNavy;
  1409.     HC := clBlue;
  1410.     SC := clBlack;
  1411.     TC := clWhite;
  1412.     if AmHidden then
  1413.     begin
  1414.       if AmReadOnly then
  1415.       begin
  1416.         if AmSystem then
  1417.         begin { One HECK of a file! }
  1418.           BC := clBlack;
  1419.           HC := clSilver;
  1420.           SC := clGray;
  1421.           TC := clWhite;
  1422.         end
  1423.         else
  1424.         begin { Dir,RO,Hid }
  1425.           BC := clMaroon;
  1426.           HC := clFuchsia;
  1427.           SC := clGreen;
  1428.           TC := clWhite;
  1429.         end;
  1430.       end
  1431.       else
  1432.       begin { Dir,Hid }
  1433.         BC := clPurple;
  1434.         HC := clFuchsia;
  1435.         SC := clBlack;
  1436.         TC := clWhite;
  1437.       end;
  1438.     end
  1439.     else
  1440.     begin
  1441.       if AmReadOnly then
  1442.       begin
  1443.         if AmSystem then
  1444.         begin { Dir,RO,Sys }
  1445.           BC := clMaroon;
  1446.           HC := clLime;
  1447.           SC := clGreen;
  1448.           TC := clWhite;
  1449.         end
  1450.         else
  1451.         begin { Dir,RO }
  1452.           BC := clGreen;
  1453.           HC := clLime;
  1454.           SC := clBlack;
  1455.           TC := clWhite;
  1456.         end;
  1457.       end
  1458.       else
  1459.       begin
  1460.         if AmSystem then
  1461.         begin { Dir,Sys }
  1462.           BC := clMaroon;
  1463.           HC := clRed;
  1464.           SC := clBlack;
  1465.           TC := clWhite;
  1466.         end;
  1467.       end;
  1468.     end;
  1469.   end
  1470.   else { Archive Only; check all combinations }
  1471.   begin
  1472.     BC := clSilver;
  1473.     HC := clWhite;
  1474.     SC := clGray;
  1475.     TC := clBlack;
  1476.     if AmHidden then
  1477.     begin
  1478.       if AmReadOnly then
  1479.       begin
  1480.         if AmSystem then
  1481.         begin { Hid,RO,Sys }
  1482.           BC := clRed;
  1483.           HC := clLime;
  1484.           SC := clPurple;
  1485.           TC := clBlack;
  1486.         end
  1487.         else
  1488.         begin { RO,Hid }
  1489.           BC := clLime;
  1490.           HC := clFuchsia;
  1491.           SC := clMaroon;
  1492.           TC := clBlack;
  1493.         end;
  1494.       end
  1495.       else
  1496.       begin { Hid }
  1497.         BC := clFuchsia;
  1498.         HC := clWhite;
  1499.         SC := clPurple;
  1500.         TC := clBlack;
  1501.       end;
  1502.     end
  1503.     else
  1504.     begin
  1505.       if AmReadOnly then
  1506.       begin
  1507.         if AmSystem then
  1508.         begin { RO,Sys }
  1509.           BC := clRed;
  1510.           HC := clLime;
  1511.           SC := clMaroon;
  1512.           TC := clBlack;
  1513.         end
  1514.         else
  1515.         begin { RO }
  1516.           BC := clLime;
  1517.           HC := clWhite;
  1518.           SC := clGreen;
  1519.           TC := clBlack;
  1520.         end;
  1521.       end
  1522.       else
  1523.       begin
  1524.         if AmSystem then
  1525.         begin { System }
  1526.           BC := clRed;
  1527.           HC := clWhite;
  1528.           SC := clMaroon;
  1529.           TC := clBlack;
  1530.         end;
  1531.       end;
  1532.     end;
  1533.   end;
  1534. end;
  1535.  
  1536. { This procedure gets all icons for an given directory, including drives and }
  1537. { standard subdirectories. It does not get special combinations or h/ro/sys  }
  1538. procedure TFileIconPanelScrollbox.GetIconsForEntireDirectory(
  1539.             TargetPath  : String );
  1540. var Finished        : Boolean;         { Loop flag              }
  1541.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1542.     TheResult       : Integer;         { return variable        }
  1543.     TempPath        : String;          { path for FF/FN         }
  1544.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1545.     RowCounter    ,                    { position in row of FIP }
  1546.     ColumnCounter   : Integer;         { position in col of FIP }
  1547.     ButtonColor   ,                    { main panel color       }
  1548.     ButtonHLColor ,                    { bright panel color     }
  1549.     ButtonSColor  ,                    { dark panel color       }
  1550.     Textcolor       : TColor;          { label text color       }
  1551.     IsADir ,                           { Variable for file attr }
  1552.     IsAnArchive ,
  1553.     IsAVolumeID,
  1554.     IsAReadOnlyFile,
  1555.     IsAHiddenFile ,
  1556.     IsASystemFile     : Boolean;
  1557.     MaxTextLength     : Integer;       { Used to safely set size}
  1558. begin
  1559.   { hide during refresh }
  1560.   Visible := false;
  1561.   { Delete the current set, if any }
  1562.   ClearTheFIPs;
  1563.   { Get the icon sizes }
  1564.   TheFIP := TFileIconPanel.Create( Self );
  1565.   TheFIP.Parent := Self;
  1566.   TheFIP.FTheLabel.Canvas.Font.Name := 'MS Serif';
  1567.   TheFIP.FTheLabel.Canvas.Font.Size := 7;
  1568.   MaxTextLength := TheFIP.FTheLabel.Canvas.TextWidth( 'COMMAND.COM' );
  1569.   TheFIP.Free;
  1570.   TheIconSize := MaxTextLength + 13;
  1571.   TheIconSpacing := TheIconSize + 5;
  1572.   { Set up maximum icons per row based on screen size }
  1573.   MaxIconsInARow := ( Screen.Width div TheIconSpacing );
  1574.   { Set up the position counters }
  1575.   RowCounter := 1;
  1576.   ColumnCounter := 1;
  1577.   { Get the drives for the current machine }
  1578.   AddDriveIcons( ColumnCounter , RowCounter  );
  1579.   { Set up the initial variables }
  1580.   Finished := false;
  1581.   TempPath := TargetPath + '*.*';
  1582.   { Make the call to FindFirst set to get any file; will return '.' }
  1583.   { so discard it.                                                  }
  1584.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1585.   { loop through all files in the directory and look for directories }
  1586.   while not Finished do
  1587.   begin
  1588.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1589.     TheResult := FindNext( TheSR );
  1590.     { A -1 result means no more files so exit }
  1591.     if TheResult <> 0 then finished := true else
  1592.     begin
  1593.       { Otherwise check for a directory attribute }
  1594.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1595.        faDirectory ) then
  1596.       begin
  1597.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1598.          ButtonHLColor , ButtonSColor , TextColor );
  1599.         { If found create a new FileIconPanel on the imported scrollbox }
  1600.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1601.         TheFIP := TFileIconPanel.Create( Self );
  1602.         TheFIP.Parent := Self;
  1603.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1604.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize ,
  1605.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1606.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1607.         { Increment column counter and move to new row if past limit }
  1608.         ColumnCounter := ColumnCounter + 1;
  1609.         if ColumnCounter > MaxIconsInARow then
  1610.         begin
  1611.           ColumnCounter := 1;
  1612.           RowCounter := RowCounter + 1;
  1613.         end;
  1614.       end;
  1615.     end;
  1616.   end;
  1617.   { Set up new initialization variables }
  1618.   Finished := false;
  1619.   TempPath := TargetPath + '*.*';
  1620.   { Make needed call to FindFirst and discard '.' }
  1621.   TheResult := FindFirst( TempPath , faAnyFile , TheSR );
  1622.   while not Finished do
  1623.   begin
  1624.     { Loop through file again, this time getting only archive files }
  1625.     TheResult := FindNext( TheSR );
  1626.     { Result of -1 indicates no more files }
  1627.     if TheResult <> 0 then Finished := true else
  1628.     begin
  1629.       { If faArchive file then add new FileIconPanel }
  1630.       TheFWB.GetFileAttributes(( Targetpath + TheSR.Name ) , IsADir ,
  1631.        IsAnArchive , IsAVolumeId , IsAHiddenFile , IsAReadOnlyFile ,
  1632.         IsASystemFile );
  1633.       if (( IsAnArchive ) and ( not IsADir )) then
  1634.       begin
  1635.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1636.          ButtonHLColor , ButtonSColor , TextColor );
  1637.         { Initialize new FileIconPanel and call initialize, sending 0 ED }
  1638.         TheFIP := TFileIconPanel.Create( Self );
  1639.         TheFIP.Parent := Self;
  1640.         TheFIP.Initialize((( ColumnCounter - 1 ) * TheIconSpacing ),
  1641.          (( RowCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize ,
  1642.           3 , 7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1643.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1644.         { Increment column counter and if needed row counter }
  1645.         ColumnCounter := ColumnCounter + 1;
  1646.         if ColumnCounter > MaxIconsInARow then
  1647.         begin
  1648.           ColumnCounter := 1;
  1649.           RowCounter := RowCounter + 1;
  1650.         end;
  1651.       end;
  1652.     end;
  1653.   end;
  1654.   { Reset to visible }
  1655.   Visible := true;
  1656. end;
  1657.  
  1658. { Update method for FIPscrollbox }
  1659. procedure TFileIconPanelScrollBox.Update;
  1660. begin
  1661.   IconsNeedRefreshing := true;
  1662.   { Force a repaint }
  1663.   InvalidateRect( TheStoredHandle , nil , true );
  1664. end;
  1665.  
  1666. { Create method for FIPScrollbox }
  1667. constructor TFileIconPanelScrollBox.Create( AOwner : TComponent );
  1668. begin
  1669.   inherited Create( AOwner );
  1670.   TheFWB := TFileWorkBench.Create( Self );
  1671. end;
  1672.  
  1673. { This function returns the next selected file's name }
  1674. function TFileIconPanelScrollBox.GetNextSelection( SourceDirectory : String;
  1675.                            var CurrentItem : Integer ) : String;
  1676. var TheResult    : String;      { Holds result of function }
  1677.     TheComponent : TComponent;  { Used for typecast        }
  1678.     finished     : boolean;     { Loop control variable    }
  1679.     TheComponentCount : Integer;
  1680. begin
  1681.   TheComponentCount := ComponentCount;
  1682.   { If past end of components exit with no result }
  1683.   if CurrentItem > TheComponentCount then TheResult := '' else
  1684.   begin
  1685.     { Set loop counter and run till find match or run out }
  1686.     finished := false;
  1687.     while not finished do
  1688.     begin
  1689.       { Pull component out of the list and check it }
  1690.       TheComponent := Components[ CurrentItem - 1 ];
  1691.       { Increment counter for later }
  1692.       CurrentItem := CurrentItem + 1;
  1693.       { Do the typecast with AS }
  1694.       with TheComponent as TFileIconPanel do
  1695.       begin
  1696.         { If its selected make sure OK }
  1697.         if Selected then
  1698.         begin
  1699.           { Don't accept backup for this level of operation }
  1700.           if FTheLabel.Caption <> '..' then
  1701.           begin
  1702.             { Otherwise return the name and abort the loop }
  1703.             TheResult := FTheName;
  1704.             finished := true;
  1705.           end;
  1706.         end
  1707.         else
  1708.         begin
  1709.           { Check to see if out of components }
  1710.           if CurrentItem > TheComponentCount then
  1711.           begin
  1712.             { If so signal error and abort }
  1713.             TheResult := '';
  1714.             finished := true;
  1715.           end;
  1716.         end;
  1717.       end;
  1718.     end;
  1719.   end;
  1720.   GetNextSelection := TheResult;
  1721. end;
  1722.  
  1723. { This procedure places a selection of files in the display based on wildcards }
  1724. procedure TFileIconPanelScrollBox.DisplayRecursiveSearchResults(
  1725.            TheStartingDirectory : String );
  1726. var XCounter ,
  1727.     YCounter   : Integer;
  1728.  
  1729. { This procedure does a recursive file search by first getting all matches (in-}
  1730. { cluding directories) and adding them to the list. Then it checks for ALL the }
  1731. { subdirectories and does the same trick on them til there are no more matches }
  1732. { and no more subdirectories, at which point it exits and recurses back up.    }
  1733. procedure RecursiveFileSearch( TheWorkingDirectory : String; var XCounter ,
  1734.                                YCounter : Integer );
  1735.  
  1736. { VITAL!!! These variables MUST be local for recursrion to work! }
  1737. var
  1738.     Finished        : Boolean;         { Loop flag              }
  1739.     TheSR           : TSearchRec;      { Searchrecord for FF/FN }
  1740.     TheResult       : Integer;         { return variable        }
  1741.     TargetPath ,
  1742.     FileMask   ,
  1743.     TheStoredWorkingDirectory ,
  1744.     ModifiedDirectory  : String;       { path for FF/FN         }
  1745.     TheFIP          : TFileIconPanel;  { generic FIP holder     }
  1746.     ButtonColor   ,                    { main panel color       }
  1747.     ButtonHLColor ,                    { bright panel color     }
  1748.     ButtonSColor  ,                    { dark panel color       }
  1749.     Textcolor       : TColor;          { label text color       }
  1750.  
  1751. begin
  1752.   { Set up the initial variables }
  1753.   Finished := false;
  1754.   TheStoredWorkingDirectory := TheWorkingDirectory;
  1755.   Targetpath := ExtractFilePath( TheWorkingDirectory );
  1756.   FileMask := ExtractFileName( TheWorkingDirectory );
  1757.   { Make the call to FindFirst set to get any file }
  1758.   TheResult := FindFirst( TheWorkingDirectory , faAnyFile , TheSR );
  1759.   if TheResult < 0 then finished := true;
  1760.   if (( TheSr.Name <> '.' ) and ( TheSr.Name <> '..' ) and ( TheResult >= 0 ))
  1761.   then begin
  1762.     if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1763.      faDirectory ) then
  1764.     begin { A directory }
  1765.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1766.        ButtonHLColor , ButtonSColor , TextColor );
  1767.       { If found create a new FileIconPanel on the imported scrollbox }
  1768.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1769.       TheFIP := TFileIconPanel.Create( Self );
  1770.       TheFIP.Parent := Self;
  1771.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1772.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1773.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1774.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1775.       { Increment column counter and move to new row if past limit }
  1776.       XCounter := XCounter + 1;
  1777.       if XCounter > MaxIconsInARow then
  1778.       begin
  1779.         XCounter := 1;
  1780.         YCounter := YCounter + 1;
  1781.       end;
  1782.     end
  1783.     else
  1784.     begin { A File }
  1785.       { Set up the default color scheme for files }
  1786.       GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1787.        ButtonHLColor , ButtonSColor , TextColor );
  1788.       { If found create a new FileIconPanel on the imported scrollbox }
  1789.       { Note sending 0 ExtraData parameter to indicate file not drive }
  1790.       TheFIP := TFileIconPanel.Create( Self );
  1791.       TheFIP.Parent := Self;
  1792.       TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1793.        (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize, TheIconSize , 3 ,
  1794.         7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor , TargetPath
  1795.          + TheSr.Name , 'MS Serif' , [] , 0 );
  1796.       { Increment column counter and move to new row if past limit }
  1797.       XCounter := XCounter + 1;
  1798.       if XCounter > MaxIconsInARow then
  1799.       begin
  1800.         XCounter := 1;
  1801.         YCounter := YCounter + 1;
  1802.       end;
  1803.     end;
  1804.   end;
  1805.   { loop through all files in the directory and look for matches }
  1806.   while not Finished do
  1807.   begin
  1808.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1809.     TheResult := FindNext( TheSR );
  1810.     { A -1 result means no more files so exit }
  1811.     if TheResult <> 0 then finished := true else
  1812.     begin
  1813.       if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory ) =
  1814.        faDirectory ) then
  1815.       begin { A directory }
  1816.         { Set up the blue color scheme for directories }
  1817.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1818.          ButtonHLColor , ButtonSColor , TextColor );
  1819.         { If found create a new FileIconPanel on the imported scrollbox }
  1820.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1821.         TheFIP := TFileIconPanel.Create( Self );
  1822.         TheFIP.Parent := Self;
  1823.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1824.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1825.            7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1826.             TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1827.         { Increment column counter and move to new row if past limit }
  1828.         XCounter := XCounter + 1;
  1829.         if XCounter > MaxIconsInARow then
  1830.         begin
  1831.           XCounter := 1;
  1832.           YCounter := YCounter + 1;
  1833.         end;
  1834.       end
  1835.       else
  1836.       begin { A File }
  1837.         { Set up the default color scheme for files }
  1838.         GetColorsForFileIcon( TargetPath + TheSR.Name , ButtonColor ,
  1839.          ButtonHLColor , ButtonSColor , TextColor );
  1840.         { If found create a new FileIconPanel on the imported scrollbox }
  1841.         { Note sending 0 ExtraData parameter to indicate file not drive }
  1842.         TheFIP := TFileIconPanel.Create( Self );
  1843.         TheFIP.Parent := Self;
  1844.         TheFIP.Initialize((( XCounter - 1 ) * TheIconSpacing ),
  1845.          (( YCounter - 1  ) * TheIconSpacing ) , TheIconSize , TheIconSize , 3 ,
  1846.           7 , ButtonColor, ButtonHLColor , ButtonSColor , TextColor ,
  1847.            TargetPath + TheSr.Name , 'MS Serif' , [] , 0 );
  1848.         { Increment column counter and move to new row if past limit }
  1849.         XCounter := XCounter + 1;
  1850.         if XCounter > MaxIconsInARow then
  1851.         begin
  1852.           XCounter := 1;
  1853.           YCounter := YCounter + 1;
  1854.         end;
  1855.       end;
  1856.     end;
  1857.   end;
  1858.   { Set up the variables to do recursive calls on all directories}
  1859.   Finished := false;
  1860.   ModifiedDirectory := ExtractFilePath( TheWorkingdirectory ) + '*.*';
  1861.   { Make the call to FindFirst set to get any file, ignore result }
  1862.   TheResult := FindFirst( ModifiedDirectory , faDirectory , TheSR );
  1863.   while not Finished do
  1864.   begin
  1865.     { Make call to FindNext, using only SearchRecord from FindFirst }
  1866.     TheResult := FindNext( TheSR );
  1867.     { A -1 result means no more files so exit }
  1868.     if TheResult <> 0 then finished := true
  1869.     else
  1870.     begin
  1871.       if TheSR.Name <> '..' then { Ignore backup in this case }
  1872.       begin
  1873.         { Do second check due to bug in FindNext }
  1874.         if (( FileGetAttr( TargetPath + TheSR.Name ) and faDirectory )
  1875.         = faDirectory ) then
  1876.         begin
  1877.           { Set up modified directory to recurse into }
  1878.           ModifiedDirectory := ExtractFilePath( TheStoredWorkingDirectory ) +
  1879.            TheSR.Name + '\' + FileMask;
  1880.           { Perform the recursion }
  1881.           RecursiveFileSearch( ModifiedDirectory , XCounter , YCounter );
  1882.         end;
  1883.       end;
  1884.     end;
  1885.   end;
  1886. end;
  1887.  
  1888. begin
  1889.   { Keep the scrollbox from updating during refresh }
  1890.   Visible := false;
  1891.   { Make the clear call }
  1892.   ClearTheFIPs;
  1893.   XCounter := 1;
  1894.   YCounter := 1;
  1895.   { Get the drives for the current machine }
  1896.   AddDriveIcons( XCounter , YCounter );
  1897.   RecursiveFileSearch( TheStartingDirectory , XCounter , YCounter );
  1898.   { Make the scrollbox visible again }
  1899.   Visible := true;
  1900. end;
  1901.  
  1902. end.
  1903.