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