home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / LABELS / FILELBL / FILELBL.ZIP / FileLbl.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  11.1 KB  |  278 lines

  1. { -------------------------------------------------------------------------------------}
  2. { An "File Label Adjuster" component for Delphi32.                                     }
  3. { Copyright 1996, Patrick Brisacier.  All Rights Reserved.                             }
  4. { This component can be freely used and distributed in commercial and private          }
  5. { environments, provided this notice is not modified in any way.                       }
  6. { -------------------------------------------------------------------------------------}
  7. { Feel free to contact us if you have any questions, comments or suggestions at        }
  8. { PBrisacier@mail.dotcom.fr (Patrick Brisacier)                                        }
  9. { -------------------------------------------------------------------------------------}
  10. { Date last modified:  07/18/96                                                        }
  11. { -------------------------------------------------------------------------------------}
  12.  
  13. { -------------------------------------------------------------------------------------}
  14. { TFileLabel v1.00                                                                     }
  15. { -------------------------------------------------------------------------------------}
  16. { Description:                                                                         }
  17. {   Display a filename with path and drive letter on a label without                   }
  18. {   without cutting the filename. If the width of the label is too                     }
  19. {   small TFileLabel hide some directories and replace them by '...'.                  }
  20. { Example :                                                                            }
  21. {   TFileLabel display 'C:\WINDOWS\SYSTEM\FOO.DLL' with one of                         }
  22. {   the following string depend on the width.                                          }
  23. {   'C:\WINDOWS\SYSTEM\FOO.DLL'                                                        }
  24. {   'C:\...\SYSTEM\FOO.DLL'                                                            }
  25. {   'C:\...\FOO.DLL'                                                                   }
  26. {   '...\FOO.DLL'                                                                      }
  27. {   'FOO.DLL'                                                                          }
  28. { Properties:                                                                          }
  29. {   TFileLabel is based on TCustomLabel. It has all the TLabel properties              }
  30. {   except Caption and add three properties : FileName, Separator and Direction.       }
  31. {   * FileName: String;                                                                }
  32. {       The filename you want to display.                                              }
  33. {   * Separator: String;                                                               }
  34. {       The separator is the string used to find the directories : '\' by              }
  35. {       default. But you can change the separator to '/' for an unixlike               }
  36. {       or an URL filename.                                                            }
  37. {   * Direction: TDirection;                                                           }
  38. {       Direction has two possible different values which are :                        }
  39. {       + drFromLeft : suppress directories from the LEFT.                             }
  40. {            Example : 'C:\WINDOWS\SYSTEM\FOO.BAR'                                     }
  41. {                      'C:\...\SYSTEM\FOO.BAR'                                         }
  42. {                      'C:\...\FOO.BAR'                                                }
  43. {                      '...\FOO.BAR'                                                   }
  44. {                      'FOO.BAR'                                                       }
  45. {       + drFromRight : suppress directories from the RIGHT.                           }
  46. {            Example : 'C:\WINDOWS\SYSTEM\FOO.BAR'                                     }
  47. {                      'C:\WINDOWS\...\FOO.BAR'                                        }
  48. {                      'C:\...\FOO.BAR'                                                }
  49. {                      '...\FOO.BAR'                                                   }
  50. {                      'FOO.BAR'                                                       }
  51. {       The default value for Direction is drFromLeft.                                 }
  52. {                                                                                      }
  53. { See example contained in example.zip file for more details.                          }
  54. { -------------------------------------------------------------------------------------}
  55. { Revision History:                                                                    }
  56. { 1.00:  + Initial release                                                             }
  57. { -------------------------------------------------------------------------------------}
  58.  
  59.  
  60. unit FileLbl;
  61.  
  62. interface
  63.  
  64. uses
  65.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  66.   StdCtrls;
  67.  
  68. type
  69.   TDirection = (drFromLeft, drFromRight);
  70.   TFileLabel = class(TCustomLabel)
  71.   private
  72.     { DΘclarations privΘes }
  73.     FFileName: String;
  74.     FSeparator: String;
  75.     FDirection: TDirection;
  76.     FParts: TStringList;
  77.     FFirstPartIsDrive: Boolean;
  78.     procedure SetFileName(AFileName: String);
  79.     procedure SetSeparator(ASeparator: String);
  80.     procedure SetDirection(ADirection: TDirection);
  81.     procedure FillParts;
  82.     procedure AdjustCaption;
  83. {    procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED; }
  84.   protected
  85.     { DΘclarations protΘgΘes }
  86.   public
  87.     { DΘclarations publiques }
  88.     constructor Create(AOwner: TComponent); override;
  89.     destructor Destroy; override;
  90.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  91.   published
  92.     { DΘclarations publiΘes }
  93.     property FileName: String read FFileName write SetFileName;
  94.     property Direction: TDirection read FDirection write SetDirection;
  95.     property Separator: String read FSeparator write SetSeparator;
  96.  
  97.     { publish the TLabel properties except Caption }
  98.     property Align;
  99.     property Alignment;
  100.     property AutoSize default False;
  101.     property Color;
  102.     property DragCursor;
  103.     property DragMode;
  104.     property Enabled;
  105.     property FocusControl;
  106.     property Font;
  107.     property ParentColor;
  108.     property ParentFont;
  109.     property ParentShowHint;
  110.     property PopupMenu;
  111.     property ShowAccelChar;
  112.     property ShowHint;
  113.     property Transparent;
  114.     property Visible;
  115.     property WordWrap default False;
  116.  
  117.     { publish all the events }
  118.     property OnClick;
  119.     property OnDblClick;
  120.     property OnDragDrop;
  121.     property OnDragOver;
  122.     property OnEndDrag;
  123.     property OnMouseDown;
  124.     property OnMouseMove;
  125.     property OnMouseUp;
  126.     property OnStartDrag;
  127.   end;
  128.  
  129. procedure Register;
  130. function sBreakApart(BaseString, BreakString: string; StringList: TStringList): TStringList;
  131.  
  132. implementation
  133.  
  134. procedure Register;
  135. begin
  136.   RegisterComponents('Exemples', [TFileLabel]);
  137. end;
  138.  
  139. {---------------------------------------------------------------------------}
  140. { TFileLabel                                                                }
  141. {---------------------------------------------------------------------------}
  142. constructor TFileLabel.Create(AOwner: TComponent);
  143. begin
  144.   inherited Create(AOwner);
  145.   FParts := TStringList.Create;
  146.   FSeparator := '\';
  147.   AutoSize := False;
  148.   WordWrap := False;
  149. end;
  150.  
  151. destructor TFileLabel.Destroy;
  152. begin
  153.   FParts.Free;
  154.   inherited Destroy;
  155. end;
  156.  
  157. procedure TFileLabel.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  158. begin
  159.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  160.   AdjustCaption;
  161. end;
  162.  
  163. { I try to adjust the caption when the font change but this method doesn't }
  164. { work. If anyone knows how to do this please tell me ! Thank !
  165. procedure TFileLabel.CMFontChanged(var Message: TMessage);
  166. begin
  167.   inherited;
  168.   AdjustCaption;
  169. end;
  170.  
  171. }
  172.  
  173. procedure TFileLabel.SetFileName(AFileName: String);
  174. begin
  175.   if FFileName <> AFileName then begin
  176.     FFileName := AFileName;
  177.     FillParts;
  178.     AdjustCaption;
  179.   end;
  180. end;
  181.  
  182. procedure TFileLabel.SetSeparator(ASeparator: String);
  183. begin
  184.   if FSeparator <> ASeparator then begin
  185.     FSeparator := ASeparator;
  186.     FillParts;
  187.     AdjustCaption;
  188.   end;
  189. end;
  190.  
  191. procedure TFileLabel.SetDirection(ADirection: TDirection);
  192. begin
  193.   if FDirection <> ADirection then begin
  194.     FDirection := ADirection;
  195.     AdjustCaption;
  196.   end;
  197. end;
  198.  
  199. procedure TFileLabel.FillParts;
  200. begin
  201.   FParts.Clear;
  202.   sBreakApart(FFileName, FSeparator, FParts);
  203.   if (FParts.Count <> 0) and (Length(FParts[0]) > 1)
  204.     and (FParts[0][Length(FParts[0])] = ':') then
  205.     FFirstPartIsDrive := True
  206.   else
  207.     FFirstPartIsDrive := False;
  208. end;
  209.  
  210. procedure TFileLabel.AdjustCaption;
  211. var
  212.   CurWidth, Start, Stop, iRep: Integer;
  213.   StrDrive, StrEtc, StrRep, StrName: String;
  214. begin
  215.   if (FFileName = '') or (Canvas.TextWidth(FFileName) <= Width) then
  216.     Caption := FFileName
  217.   else begin
  218.     StrEtc := '...' + FSeparator;
  219.     StrName := FParts[FParts.Count - 1];
  220.     CurWidth := Canvas.TextWidth(StrName);
  221.     if (CurWidth + Canvas.TextWidth(StrEtc)) < Width then begin
  222.       CurWidth := CurWidth + Canvas.TextWidth(StrEtc);
  223.       if FFirstPartIsDrive then begin
  224.         StrDrive := FParts[0] + FSeparator;
  225.         Start := 1;
  226.       end;
  227.       if (CurWidth + Canvas.TextWidth(StrDrive)) < Width then begin
  228.         CurWidth := CurWidth + Canvas.TextWidth(StrDrive);
  229.         Stop := FParts.Count - 2;
  230.         case FDirection of
  231.         drFromLeft: begin
  232.           for iRep := Stop downto Start do begin
  233.             if (CurWidth + Canvas.TextWidth(FParts[iRep] + FSeparator + StrRep)) > Width then break;
  234.             StrRep := FParts[iRep] + FSeparator + StrRep;
  235.           end;
  236.           Caption := StrDrive + StrEtc + StrRep + StrName;
  237.         end;
  238.         drFromRight: begin
  239.           for iRep := Start to Stop do begin
  240.             if (CurWidth + Canvas.TextWidth(StrRep + FParts[iRep] + FSeparator)) > Width then break;
  241.             StrRep := StrRep + FParts[iRep] + FSeparator;
  242.           end;
  243.           Caption := StrDrive + StrRep + StrEtc + StrName;
  244.         end;
  245.         end; {case Direction }
  246.       end { if StrDrive }
  247.       else
  248.         Caption := StrEtc + StrName;
  249.     end { if StrEtc }
  250.     else
  251.       Caption := StrName;
  252.   end; { else }
  253. end;
  254.  
  255. { This code came from Lloyd's help file! (begin) }
  256. { Thanks to Lloyd !!! This help file is VERY useful. }
  257. function sBreakApart(BaseString, BreakString: string; StringList: TStringList): TStringList;
  258. var
  259.   EndOfCurrentString: byte;
  260. begin
  261.   repeat
  262.     EndOfCurrentString := Pos(BreakString, BaseString);
  263.     if EndOfCurrentString = 0 then
  264.       StringList.add(BaseString)
  265.     else
  266.       StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
  267.     BaseString := Copy(BaseString, EndOfCurrentString + length(BreakString),
  268.                   length(BaseString) - EndOfCurrentString);
  269.   until EndOfCurrentString = 0;
  270.   result := StringList;
  271. end;
  272. { This code came from Lloyd's help file! (end) }
  273.  
  274. end.
  275.  
  276.  
  277.  
  278.