(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTFileLabel3D;

interface
{$I AHM.INC}
uses
  SysUtils, Windows, Controls,Dialogs,
  StdCtrls, Classes, Graphics, Menus,AHMEnhancedClass,ahmtbackground;

type
  TAHMFileLabel3D = class(TCustomLabel)
  private
    FAHMAboutStr : TAHMAboutEnhancedStr;
    f3D : TAHMFont3D;
    fback : TAHMBackground;
    fback2 : TAHMBackground;
    fframe : TAHMFrame;
    FFilename: String;
    FSeparator: Char;
    FParts: TStringList;
    FFirstPartIsDrive: Boolean;
    procedure SetFilename(AFilename: String);
    procedure SetSeparator(ASeparator: Char);
    procedure FillParts;
    procedure AdjustCaption;
    procedure SetBack(value : TAHMBackground);
    procedure SetBack2(value : TAHMBackground);
    procedure Set3D(Value: TAHMFont3D);
    procedure SetFrm(value : TAHMFrame);
  protected
    procedure Paint; override;
    procedure Notification(AComponent: TComponent;Operation: TOperation); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
    property WordWrap default false;
    property AutoSize default False;
  published
    property About : TAHMAboutEnhancedStr read FAHMAboutStr write FAHMAboutStr stored False;
    {$IFDEF AHMONLY4}
    property Anchors;
    property BiDiMode;
    property Constraints;
    property DragKind;
    property ParentBiDiMode;
    {$ENDIF}
    property Filename : String read FFilename write SetFilename;
    property Separator: Char read FSeparator write SetSeparator default '\';
    property Align;
    property Alignment;
    property Color;
    property Enabled;
    property FocusControl;
    property FontBackground : TAHMBackground read fback write Setback;
    property Font;
    property Font3D : TAHMFont3D read f3d write Set3D;
    property Frame  : TAHMFrame read fframe write Setfrm;
    property Background : TAHMBackground read fback2 write Setback2;
    property Transparent;
    property ShowHint;
    property Visible;
    property OnClick;
    property OnDblClick;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnDragDrop;
    property OnEndDrag;
    property OnStartDrag;
    property OnDragOver;
    property DragCursor;
    property DragMode;
    property ParentColor;
    property ParentFont;
    Property ShowAccelChar;
  end;

implementation

function AHMStringIntoPieces(BaseString, BreakString: string; StringList: TStringList): TStringList;
var
  EndOfCurrentString: byte;
begin
  repeat
    EndOfCurrentString := Pos(BreakString, BaseString);
    if EndOfCurrentString = 0 then
      StringList.add(BaseString)
    else
      StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
    BaseString := Copy(BaseString, EndOfCurrentString + length(BreakString),
                  length(BaseString) - EndOfCurrentString);
  until EndOfCurrentString = 0;
  result := StringList;
end;


constructor TAHMFileLabel3D.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  f3d           := TAHMFont3d.create(Self);
  ControlStyle  := ControlStyle + [csReplicatable];
  fframe        := TAHMFrame.create(Self);
  fframe.framestyle:=framenone;
  FParts        := TStringList.Create;
  FSeparator    := '\';
  AutoSize      := False;
  WordWrap      := False;
end;

procedure TAHMFileLabel3D.Notification(AComponent: TComponent;Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and  (AComponent = Background) then SetBack2(nil);
  if (Operation = opRemove) and  (AComponent = FontBackground) then SetBack(nil);
end;


procedure TAHMFileLabel3D.Loaded;
begin
  inherited Loaded;
end;

destructor TAHMFileLabel3D.Destroy;
begin
  if fback<>nil then fback.OList.remove(self);
  if fback2<>nil then fback2.OList.remove(self);
  F3d.free;
  fframe.free;
  FParts.Free;
  inherited Destroy;
end;

procedure TAHMFileLabel3D.Paint;
const
  Alignments: array[TAlignment] of Word =(DT_LEFT, DT_RIGHT, DT_CENTER);
  WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
  TempRect:TRect;
  flags : Longint;
begin
  TempRect:=Clientrect;
  if not(Transparent) then
  begin
    if assigned(fback2) then fback2.draw(Canvas.Handle,width,height,true) else
    begin
      Canvas.Brush.Color := Self.Color;
      Canvas.Brush.Style := bsSolid;
      Canvas.FillRect(ClientRect);
    end;
  end;
  Canvas.Brush.Style := bsClear;
  flags := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment];
  Text := GetLabelText;
  if (Flags and DT_CALCRECT <> 0) and ((Text = '') or ShowAccelChar and
    (Text[1] = '&') and (Text[2] = #0)) then Text := Text + ' ';
  inflaterect(Temprect,-fframe.Totalwidth,-fframe.Totalwidth);
  if assigned(fback) then
  begin
    fback.write(f3d,Font,canvas,Temprect,text,flags);
  end else f3d.write(Font,canvas.handle,Temprect,text,flags,enabled);
  inflaterect(Temprect,fframe.Totalwidth,fframe.Totalwidth);
  fframe.draw(Canvas.handle,Temprect);
end;

procedure TAHMFileLabel3D.Set3D(Value: TAHMFont3D);
begin
  if value<>f3d then
  begin
    f3d:=value;
    invalidate;
  end;
end;

procedure TAHMFileLabel3D.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
  inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  AdjustCaption;
end;

procedure TAHMFileLabel3D.SetFilename(AFilename: String);
begin
  if FFilename <> AFilename then begin
    FFilename := AFilename;
    FillParts;
    AdjustCaption;
  end;
end;

procedure TAHMFileLabel3D.SetSeparator(ASeparator: Char);
begin
  if FSeparator <> ASeparator then begin
    FSeparator := ASeparator;
    FillParts;
    AdjustCaption;
  end;
end;

procedure TAHMFileLabel3D.FillParts;
begin
  FParts.Clear;
  AHMStringIntoPieces(FFilename, FSeparator, FParts);
  if (FParts.Count <> 0) and (Length(FParts[0]) > 1)
    and (FParts[0][Length(FParts[0])] = ':') then
    FFirstPartIsDrive := True
  else
    FFirstPartIsDrive := False;
end;

procedure TAHMFileLabel3D.AdjustCaption;
var
  CurWidth, Start, Stop, iRep: Integer;
  StrDrive, StrEtc, StrRep, StrName: String;
begin
  start:=1;
  if (FFilename = '') or (Canvas.TextWidth(FFilename) <= Width) then Caption := FFilename
  else
  begin
    StrEtc := '...' + FSeparator;
    StrName := FParts[FParts.Count - 1];
    CurWidth := Canvas.TextWidth(StrName);
    if (CurWidth + Canvas.TextWidth(StrEtc)) < Width then
    begin
      CurWidth := CurWidth + Canvas.TextWidth(StrEtc);
      if FFirstPartIsDrive then
      begin
        StrDrive := FParts[0] + FSeparator;
        Start := 1;
      end;
      if (CurWidth + Canvas.TextWidth(StrDrive)) < Width then
      begin
        CurWidth := CurWidth + Canvas.TextWidth(StrDrive);
        Stop := FParts.Count - 2;
        for iRep := Stop downto Start do
        begin
          if (CurWidth + Canvas.TextWidth(FParts[iRep] + FSeparator + StrRep)) > Width then break;
          StrRep := FParts[iRep] + FSeparator + StrRep;
        end;
        Caption := StrDrive + StrEtc + StrRep + StrName;
      end
      else Caption := StrEtc + StrName;
    end
    else
      Caption := StrName;
  end;
end;

procedure TAHMFileLabel3D.Setfrm(Value: TAHMFrame);
begin
  if value<>fframe then
  begin
    fframe:=value;
    invalidate;
  end;
end;

procedure TAHMFileLabel3D.Setback(Value: TAHMBackground);
begin
  if fback<>nil then fback.OList.remove(self);
  if value<>nil then value.additem(self);
  fback:=value;
  invalidate;
end;

procedure TAHMFileLabel3D.Setback2(Value: TAHMBackground);
begin
  if fback2<>nil then fback2.OList.remove(self);
  if value<>nil then value.additem(self);
  fback2:=value;
  invalidate;
end;


end.

