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