(************************************************************************
*    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 AHMTOfficeButton;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics,
  Controls,AHMControls,AHMEnhancedClass,AHMTBackground;

type
  TAHMOfficeButton = class(TAHMGraphicControl)
  private
    { Private declarations }
    FAHMAboutStr : TAHMAboutEnhancedStr;
    FRoundSize: integer;
    FBorderColor: TColor;
    fback :  TAHMBackground;
    fin,fout,fnorm : TColor;
    f3d : TAHMFont3D;
    HasMouse: boolean;
    IsMouseDown: boolean;
    ExtraSpace: word;
    procedure SetBorderColor(value: TColor);
    procedure SetRoundSize(value: integer);
    procedure SetBack(value : TAHMBackground);
    procedure Setnorm(value : TColor);
    Procedure Set3D(value : TAHMFont3D);
    procedure Setcaption(const value : TCaption);
    function getcaption:TCaption;
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  protected
    { Protected declarations }
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure Paint; override;
    Procedure Notification(AComponent: TComponent;  Operation: TOperation); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    { Published declarations }
    property About : TAHMAboutEnhancedStr read FAHMAboutStr write FAHMAboutStr stored False;
    property Caption : TCaption read GetCaption write Setcaption;
    property BorderColor: TColor read FBorderColor write SetBorderColor default clBlack;
    property Font;
    property LedNormal : TColor read fnorm write SetNorm default clNavy;
    property LedIn : TColor read fin write fin default clAqua;
    Property LedOut : TColor read fout write fout default clTeal;
    property RoundSize: integer read FRoundSize write SetRoundSize default 20;
    property Font3D : TAHMFont3D read f3d write Set3D;
    Property Background : TAHMBackground read fback write SetBack;
    property Enabled;
    property ShowHint;
    property Visible;
    property OnClick;
    property OnDblClick;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
  end;
implementation

procedure TAHMOfficeButton.Setcaption(const value : TCaption);
begin
  if GetCaption <> Value then SetTextBuf(PChar(Value));
  Invalidate;
end;

Function TAHMOfficeButton.Getcaption:TCaption;
var
  Len: Integer;
begin
  Len := GetTextLen;
  SetString(Result, PChar(nil), Len);
  if Len <> 0 then GetTextBuf(Pointer(Result), Len + 1);
end;


procedure TAHMOfficeButton.Loaded;
begin
  inherited Loaded;
end;

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



procedure TAHMOfficeButton.SetBorderColor(value: TColor);
begin
  FBorderColor := Value;
  Invalidate;
end;

procedure TAHMOfficeButton.Set3D(value: TAHMFont3D);
begin
  f3d:=value;
  invalidate;
end;

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

procedure TAHMOfficeButton.SetNorm(value: TColor);
begin
  fnorm:=value;
  invalidate;
end;


procedure TAHMOfficeButton.SetRoundSize(value: integer);
begin
  if Value <> FRoundSize then
  begin
    FRoundSize := Value;
    Invalidate;
  end;
end;

procedure TAHMOfficeButton.CMMouseEnter(var Message: TMessage);
begin
  HasMouse := true;
  if IsMouseDown then ExtraSpace := 1 else ExtraSpace := 0;
  paint;
end;

procedure TAHMOfficeButton.CMMouseLeave(var Message: TMessage);
begin
  HasMouse := false;
  ExtraSpace := 0;
  paint;
end;

procedure TAHMOfficeButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if not(IsMouseDown) then
  begin
    IsMouseDown := true;
    ExtraSpace := 1;
    Paint;
  end;
  inherited MouseDown(Button, Shift, X, Y);
end;

procedure TAHMOfficeButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if IsMouseDown then
  begin
    IsMouseDown := false;
    ExtraSpace := 0;
    Paint;
  end;
  inherited MouseUp(Button, Shift, X, Y);
end;

Procedure AHMDrawLed(Canvas : TCanvas;x,y : Integer;LColor:TColor);
begin
  canvas.brush.color:=LColor;
  canvas.pen.color:=LColor;
  with canvas do
    Ellipse(x+1,y+1,x+11,y+11);
  Canvas.pixels[x+8,Y+2]:=clsilver;
  Canvas.pixels[x+4,Y+1]:=clsilver;
  Canvas.pixels[x+5,Y+1]:=clsilver;
  Canvas.pixels[x+6,Y+1]:=clsilver;
  Canvas.pixels[x+7,Y+1]:=clsilver;
  Canvas.pixels[x+3,Y+2]:=clsilver;
  Canvas.pixels[x+2,Y+3]:=clsilver;
  Canvas.pixels[x+1,Y+4]:=clsilver;
  Canvas.pixels[x+1,Y+5]:=clsilver;
  Canvas.pixels[x+1,Y+6]:=clsilver;
  Canvas.pixels[x+1,Y+7]:=clsilver;
  Canvas.pixels[x+2,Y+8]:=clsilver;
  Canvas.pixels[x+9,Y+3]:=clgray;
  Canvas.pixels[x+10,Y+4]:=clgray;
  Canvas.pixels[x+10,Y+5]:=clgray;
  Canvas.pixels[x+10,Y+6]:=clgray;
  Canvas.pixels[x+10,Y+7]:=clgray;
  Canvas.pixels[x+9,Y+8]:=clgray;
  Canvas.pixels[x+8,Y+9]:=clgray;
  Canvas.pixels[x+4,Y+10]:=clgray;
  Canvas.pixels[x+5,Y+10]:=clgray;
  Canvas.pixels[x+6,Y+10]:=clgray;
  Canvas.pixels[x+7,Y+10]:=clgray;
  Canvas.pixels[x+3,Y+9]:=clgray;
  Canvas.pixels[x+4,Y+3]:=clsilver;
  Canvas.pixels[x+3,Y+4]:=clsilver;
  Canvas.pixels[x+4,Y+4]:=clsilver;
  Canvas.pixels[x+5,Y+4]:=clsilver;
  Canvas.pixels[x+4,Y+5]:=clsilver;
{  Canvas.pixels[x+7,Y+9]:=LColor;}
end;


procedure TAHMOfficeButton.Paint;
var
  GlyphRect, BitmapRect: TRect;
  GlyphTop, GlyphLeft: integer;
  backbit : TBitmap;
  txtrect : TRect;
  truebit,forebit : TBitmap;
begin
  inherited paint;
  backbit:=TBitmap.create;
  forebit:=TBitmap.create;
  truebit:=TBitmap.create;
  forebit.dormant;
  forebit.dormant;
  backbit.dormant;
  forebit.height:=height; forebit.width:=width;
  backbit.height:=height; backbit.width:=width;
  truebit.height:=height; truebit.width:=width;
  if assigned(fback) then
    fback.draw(backbit.canvas.Handle,width,height,not((ismousedown) and (hasmouse)))
  else
  begin
    Backbit.Canvas.Brush.Color:=$00A4FFFF;
    Backbit.Canvas.Brush.Style:=bsSolid;
    txtrect:=Backbit.Canvas.ClipRect;
    backbit.canvas.Fillrect(txtrect);
  end;
  GlyphTop := ((Height - 10) div 2) + ExtraSpace;
  GlyphLeft := 7 + ExtraSpace;
  GlyphRect := Rect(GlyphLeft, GlyphTop, 10+GlyphLeft,10+GlyphTop);
  BitmapRect := Rect(0, 0, 10,10);
  truebit.canvas.font:=Font;
  forebit.canvas.Brush.Style := bsSolid;
  forebit.canvas.Brush.Color := clred;
  forebit.canvas.Pen.color := clred;
  forebit.canvas.Pen.Width := 1;
  forebit.canvas.RoundRect(3,3,Width-3,Height-3,FRoundSize,FRoundSize);
  with backbit.Canvas do
  begin
    brush.style:=bsclear;
    pen.width:=1;
    if not(ismousedown) then Pen.Color := clbtnhighlight else
                             Pen.Color := clbtnface;
    RoundRect(2,2,Width-3,Height-3,FRoundSize,FRoundSize);
    if ismousedown then Pen.Color := clbtnhighlight else
                        Pen.Color := clbtnface;
    RoundRect(3,3,Width-1,Height-1,FRoundSize,FRoundSize);
    AHMDrawTransparentBitmap(backbit.canvas,forebit,0,0,clred);

    BitBlt(Truebit.canvas.handle,0,0,width,height,canvas.handle,0,0,srccopy);
    AHMDrawTransparentBitmap(truebit.canvas,backbit,0,0,backbit.canvas.pixels[0,0]);
    if HasMouse then
    if IsMouseDown then AHMDrawLed(Truebit.canvas,Glyphrect.left,Glyphrect.top,fin)
      else AHMDrawLed(Truebit.canvas,Glyphrect.left,Glyphrect.top,fout)
      else AHMDrawLed(Truebit.canvas,Glyphrect.left,Glyphrect.top,fnorm);
      Txtrect:=Rect(((Width - TextWidth(Caption)) div 2)- f3d.offset -f3d.repetitionwidth+ GlyphLeft,
                  ((Height - TextHeight(Caption)) div 2)- f3d.offset -f3d.repetitionwidth+ExtraSpace,
                  ((Width  - TextWidth(Caption)) div 2)+ f3d.offset +f3d.repetitionwidth+ GlyphLeft+Textwidth(caption),
                  ((Height  - TextHeight(Caption)) div 2)+ f3d.offset +f3d.repetitionwidth+ExtraSpace+Textheight(caption));
    f3d.write(self.Font,Truebit.canvas.handle,TxtRect,Caption,DT_CENTER or DT_VCENTER or dt_singleline,enabled);
    with TrueBit.Canvas do
    begin
      if HasMouse then Pen.Color := clbtnHighlight else Pen.Color := fbordercolor;
      pen.width:=1;
      RoundRect(2,2,Width-2,Height-2,FRoundSize,FRoundSize);
    end;
  end;
  Bitblt(Canvas.handle,clientrect.left,clientrect.top,clientrect.right,clientrect.bottom,
                Truebit.canvas.handle,clientrect.left,clientrect.top,SRCCopy);
  forebit.free;
  truebit.free;
  backbit.free;
end;

constructor TAHMOfficeButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := [csClickEvents, csCaptureMouse, csSetCaption];
  f3d:=TAHMFont3d.create(self);
  fnorm:=clnavy;
  fin:=clAqua;
  fout:=clTeal;
  Width := 130;
  Height := 25;
  FRoundSize := 20;
  HasMouse := false;
  IsMouseDown := false;
  ExtraSpace := 0;
  FBorderColor := clblack;
end;

destructor TAHMOfficeButton.Destroy;
begin
  if fback<>nil then fback.OList.remove(self);
  f3d.free;
  inherited Destroy;
end;


end.
