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

interface

uses
  Windows,StdCtrls,Classes,Controls,Messages,Graphics,AHMEnhancedClass,AHMTBackground;

type
  TAHMCheckbox = class(TCheckbox)
  private
    FAHMAboutStr : TAHMAboutEnhancedStr;
    FFlat        : Boolean;
    FImage       : TBitmap;
    FWordWrap    : Boolean;
    FAsButton  : Boolean;
    F3D          : TAHMFont3D;
    FAlign       : TAlignment;
    FAlignButton : TLeftRight;
    FBack        : TAHMBackground;
    {Function Definitions}
    Function IsAsButton : Boolean;
    Function IsNotAsButton : Boolean;
    Function GetText: TCaption;
    {Procedure Definitions}
    Procedure SetFlat(Value : Boolean);
    Procedure Set3D(Value : TAHMFont3D);
    Procedure SetWordWrap(Value : Boolean);
    Procedure SetAlign(Value : TAlignment);
    Procedure SetText(const Value: TCaption);
    Procedure SetAsButton(Value : Boolean);
    Procedure SetBack(Value : TAHMBackground);
    Procedure SetAlignButton(Value : TLeftRight);
    Procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  protected
    procedure Createparams(var Params: TCreateParams); override;
    procedure Notification(AComponent: TComponent;Operation: TOperation); override;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
  published
    property About : TAHMAboutEnhancedStr read FAHMAboutStr write FAHMAboutStr stored False;
    Property Alignment  : TAlignment read FAlign write SetAlign default taLeftJustify;
    Property Caption: TCaption read GetText write SetText;
    Property Font3D : TAHMFont3D read F3D write Set3D;
    Property WordWrap : Boolean read fWordWrap write SetWordWrap default false;
    Property Background : TAHMBackground read FBack write SetBack;
    Property AsButton : Boolean read FAsButton write SetAsButton default false;
    Property ButtonSunk : Boolean read FFlat write SetFlat stored IsAsButton default True;
    Property AlignButton : TLeftRight read FAlignButton write SetAlignButton stored IsNotAsButton default taLeftJustify;
  end;
  TAHMRadiobutton = class(TRadiobutton)
  private
    FAHMAboutStr : TAHMAboutEnhancedStr;
    FFlat        : Boolean;
    FImage       : TBitmap;
    FWordWrap    : Boolean;
    FAsButton  : Boolean;
    F3D          : TAHMFont3D;
    FAlign       : TAlignment;
    FAlignButton : TLeftRight;
    FBack        : TAHMBackground;
    {Function Definitions}
    Function IsAsButton : Boolean;
    Function IsNotAsButton : Boolean;
    Function GetText: TCaption;
    {Procedure Definitions}
    Procedure SetFlat(Value : Boolean);
    Procedure Set3D(Value : TAHMFont3D);
    Procedure SetWordWrap(Value : Boolean);
    Procedure SetAlign(Value : TAlignment);
    Procedure SetText(const Value: TCaption);
    Procedure SetAsButton(Value : Boolean);
    Procedure SetBack(Value : TAHMBackground);
    Procedure SetAlignButton(Value : TLeftRight);
    Procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  protected
    Procedure Createparams(var Params: TCreateParams); override;
    Procedure Notification(AComponent: TComponent;Operation: TOperation); override;
  public
    Constructor Create(AOwner : TComponent); override;
    Destructor Destroy; override;
    Procedure Loaded; override;
  published
    property About : TAHMAboutEnhancedStr read FAHMAboutStr write FAHMAboutStr stored False;
    Property Alignment  : TAlignment read FAlign write SetAlign default taLeftJustify;
    Property Caption: TCaption read GetText write SetText;
    Property Font3D : TAHMFont3D read F3D write Set3D;
    Property WordWrap : Boolean read fWordWrap write SetWordWrap default false;
    Property Background : TAHMBackground read FBack write SetBack;
    Property AsButton : Boolean read FAsButton write SetAsButton default false;
    Property ButtonSunk : Boolean read FFlat write SetFlat stored IsAsButton default True;
    Property AlignButton : TLeftRight read FAlignButton write SetAlignButton stored IsNotAsButton default taLeftJustify;
  end;

implementation

Constructor TAHMCheckbox.Create(AOwner : TComponent);
begin
  inherited create(AOwner);
  Width:=120;
  Height:=22;
  FFlat:=True;
  FImage:=TBitmap.create;
  F3D:=TAHMFont3D.Create(self);
  FWordwrap:=False;
  FAsButton:=False;
  Controlstyle:=Controlstyle + [csSetCaption];
end;

Destructor TAHMCheckbox.Destroy;
begin
  if FBack<>nil then FBack.OList.remove(self);
  F3D.Free;
  FImage.Free;
  inherited Destroy;
end;

Procedure TAHMCheckbox.Loaded;
begin
  inherited Loaded;
end;

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

Procedure TAHMCheckbox.SetBack(Value : TAHMBackground);
begin
  if FBack<>nil then FBack.OList.remove(self);
  if value<>nil then value.additem(self);
  FBack:=Value;
  Invalidate;
end;

Procedure TAHMCheckbox.WMPaint(var Message: TWMPaint);
Const
  Alignments: array[TAlignment] of Word =(DT_LEFT, DT_RIGHT, DT_CENTER);
  WordWraps: array[Boolean] of Word = (DT_SINGLELINE, DT_WORDBREAK);
var FRect : TRect;
    FFlags : LongInt;
begin
  FImage.Width:=Clientrect.Right-Clientrect.Left;
  FImage.Height:=Clientrect.Bottom-Clientrect.Top;
  FImage.Canvas.Brush.color:=Color;
  FImage.Canvas.Fillrect(Clientrect);
  if assigned(FBack) then FBack.drawtrans(Fimage.Canvas,width,height,true,color);
  FRect:=Clientrect;
  InFlateRect(FRect,-6,-6);
  if not(FAsButton) then
  begin
    if FAlignButton=taRightJustify then FRect.Right:=FRect.Right-12
                                   else FRect.Left:=FRect.Left+12;
  end;
  FFlags := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment]
                          or DT_END_ELLIPSIS or DT_VCENTER;
  F3D.Write(self.Font,Fimage.Canvas.Handle,FRect,Text,FFlags,True);
  Sendmessage(Handle,BM_SETIMAGE,IMAGE_BITMAP,FImage.handle);
  inherited;
end;

Procedure TAHMCheckbox.Set3D(Value : TAHMFont3D);
begin
  F3D.Assign(Value);
end;

Procedure TAHMCheckbox.createparams(var Params: TCreateParams);
begin
  Inherited createparams(Params);
  with Params do
  begin
    style:=style and not(BS_LEFT or BS_RIGHT or BS_CENTER OR
                         BS_LEFTTEXT or BS_RIGHTBUTTON OR
                         BS_TOP OR BS_BOTTOM OR BS_VCENTER);
    style:=style or bs_vcenter;
    if FAsButton then style := style or bs_pushlike or bs_center;
    if FAlignButton=taRightJustify then style:=style or BS_RIGHTBUTTON or bs_left
                                   else style := style or bs_right;
    if FWordwrap then style:=style or bs_MultiLine;
    if FAsButton and FFlat then exstyle := exstyle or ws_ex_clientedge;
    style := style or bs_bitmap;
  end;
end;

Procedure TAHMCheckbox.SetAlign(Value : TAlignment);
begin
  If (Value <> fAlign) then
  begin
    fAlign := Value;
    Invalidate;
  end;
end;

Procedure TAHMCheckbox.SetAlignButton(Value : TLeftRight);
begin
  If (Value <> FAlignButton) then
  begin
    FAlignButton := Value;
    Recreatewnd;
  end;
end;

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


Procedure TAHMCheckbox.SetText(const Value: TCaption);
begin
  if GetText <> Value then SetTextBuf(PChar(Value));
  Invalidate;
end;


Procedure TAHMCheckbox.SetAsButton(Value : Boolean);
begin
   If (Value <> fAsButton) then
   begin
     FAsButton := Value;
     Recreatewnd;
   end;
end;

Procedure TAHMCheckbox.SetFlat(Value : Boolean);
begin
   If (Value <> FFlat) then
   begin
     FFlat := Value;
     Recreatewnd;
   end;
end;

Procedure TAHMCheckbox.SetWordWrap(Value : Boolean);
begin
   If (Value <> fWordwrap) then
   begin
     FWordwrap := Value;
     Invalidate;
   end;
end;

Function TAHMCheckbox.IsAsButton : Boolean;
begin
  result:=FAsButton;
end;

Function TAHMCheckbox.IsNotAsButton : Boolean;
begin
  result:=Not(FAsButton);
end;

Constructor TAHMRadiobutton.Create(AOwner : TComponent);
begin
  inherited create(AOwner);
  Width:=120;
  Height:=22;
  FFlat:=True;
  FWordwrap:=false;
  FImage:=TBitmap.create;
  F3D:=TAHMFont3D.Create(self);
  FAsButton:=False;
  Controlstyle:=Controlstyle + [csSetCaption];
end;

Destructor TAHMRadiobutton.Destroy;
begin
  if FBack<>nil then FBack.OList.remove(self);
  F3D.Free;
  FImage.Free;
  inherited Destroy;
end;

Procedure TAHMRadiobutton.Loaded;
begin
  inherited Loaded;
end;

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

Procedure TAHMRadiobutton.SetBack(Value : TAHMBackground);
begin
  if FBack<>nil then FBack.OList.remove(self);
  if value<>nil then value.additem(self);
  FBack:=Value;
  invalidate;
end;

Procedure TAHMRadiobutton.WMPaint(var Message: TWMPaint);
Const
  Alignments: array[TAlignment] of Word =(DT_LEFT, DT_RIGHT, DT_CENTER);
  WordWraps: array[Boolean] of Word = (DT_SINGLELINE, DT_WORDBREAK);
var FRect : TRect;
    FFlags : LongInt;
begin
  FImage.Width:=Clientrect.Right-Clientrect.Left;
  FImage.Height:=Clientrect.Bottom-Clientrect.Top;
  FImage.Canvas.Brush.color:=Color;
  FImage.Canvas.Fillrect(Clientrect);
  if assigned(FBack) then FBack.drawtrans(Fimage.Canvas,width,height,true,color);
  FRect:=Clientrect;
  InFlateRect(FRect,-6,-2);
  if not(FAsButton) then
  begin
    if FAlignButton=taRightJustify then FRect.Right:=FRect.Right-10
                                   else FRect.Left:=FRect.Left+10;
  end;
  SetBkMode(Handle, TRANSPARENT);
  FFlags := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment]
                          or DT_END_ELLIPSIS or DT_VCENTER;
  F3D.Write(self.Font,Fimage.Canvas.Handle,FRect,Text,FFlags,True);
  Sendmessage(Handle,BM_SETIMAGE,IMAGE_BITMAP,FImage.handle);
  inherited;
end;

Procedure TAHMRadiobutton.Set3D(Value : TAHMFont3D);
begin
  F3D.Assign(Value);
end;

Procedure TAHMRadiobutton.createparams(var Params: TCreateParams);
begin
  Inherited createparams(Params);
  with Params do
  begin
    style:=style and not(BS_LEFT or BS_RIGHT or BS_CENTER OR
                         BS_LEFTTEXT or BS_RIGHTBUTTON OR
                         BS_TOP OR BS_BOTTOM OR BS_VCENTER);
    style:=style or bs_vcenter;
    if FAsButton then style := style or bs_pushlike or bs_center;
    if FAlignButton=taRightJustify then style:=style or BS_RIGHTBUTTON or bs_left
                                   else style := style or bs_right;
    if FWordwrap then style:=style or bs_MultiLine;
    if FAsButton and FFlat then exstyle := exstyle or ws_ex_clientedge;
    style := style or bs_bitmap;
  end;
end;

Procedure TAHMRadiobutton.SetAlign(Value : TAlignment);
begin
  If (Value <> fAlign) then
  begin
    fAlign := Value;
    Invalidate;
  end;
end;

Procedure TAHMRadiobutton.SetAlignButton(Value : TLeftRight);
begin
  If (Value <> FAlignButton) then
  begin
    FAlignButton := Value;
    Recreatewnd;
  end;
end;

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


Procedure TAHMRadiobutton.SetText(const Value: TCaption);
begin
  if GetText <> Value then SetTextBuf(PChar(Value));
  Invalidate;
end;


Procedure TAHMRadiobutton.SetAsButton(Value : Boolean);
begin
   If (Value <> fAsButton) then
   begin
     FAsButton := Value;
     Recreatewnd;
   end;
end;

Procedure TAHMRadiobutton.SetFlat(Value : Boolean);
begin
   If (Value <> FFlat) then
   begin
     FFlat := Value;
     Recreatewnd;
   end;
end;

Procedure TAHMRadiobutton.SetWordWrap(Value : Boolean);
begin
   If (Value <> fWordwrap) then
   begin
     FWordwrap := Value;
     Invalidate;
   end;
end;

Function TAHMRadiobutton.IsAsButton : Boolean;
begin
  result:=FAsButton;
end;

Function TAHMRadiobutton.IsNotAsButton : Boolean;
begin
  result:=Not(FAsButton);
end;


end.
