unit BasicControls;

interface

uses
  Classes, SysUtils, Graphics;

type

  { TControl }

  TControl = class
  private
    FEnabled: Boolean;
    FOnClick: TNotifyEvent;
    procedure SetEnabled(AValue: Boolean);
  public
    Ref: TObject;
    Bounds: TRect;
    Canvas: TCanvas;
    procedure MouseUp(Position: TPoint);
    constructor Create; virtual;
    procedure Paint; virtual;
    property Enabled: Boolean read FEnabled write SetEnabled;
    property OnClick: TNotifyEvent read FOnClick write FOnClick;
  end;

  { TLabel }

  TLabel = class(TControl)
    Text: string;
    procedure Paint; override;
  end;

  { TButton }

  TButton = class(TControl)
    Text: string;
    procedure Paint; override;
  end;

  { TImage }

  TImage = class(TControl)
    Bitmap: TBitmap;
    BitmapDisabled: TBitmap;
    constructor Create; override;
    destructor Destroy; override;
    procedure Paint; override;
  end;


implementation

{ TLabel }

procedure TLabel.Paint;
begin
  if not Assigned(Canvas) then Exit;
  with Canvas do begin
    TextOut(Bounds.Left, Bounds.Top, Text);
  end;
end;

{ TButton }

procedure TButton.Paint;
begin
  inherited Paint;
end;

{ TControl }

procedure TControl.SetEnabled(AValue: Boolean);
begin
  if FEnabled = AValue then Exit;
  FEnabled := AValue;
  if Assigned(Canvas) then Paint;
end;

procedure TControl.MouseUp(Position: TPoint);
begin
  if Enabled and Bounds.Contains(Position) then
  if Assigned(FOnClick) then FOnClick(Self);
end;

constructor TControl.Create;
begin
  FEnabled := True;
end;

procedure TControl.Paint;
begin
end;

{ TImage }

constructor TImage.Create;
begin
  inherited;
  Bitmap := TBitmap.Create;
  BitmapDisabled := TBitmap.Create;
end;

destructor TImage.Destroy;
begin
  FreeAndNil(Bitmap);
  FreeAndNil(BitmapDisabled);
  inherited;
end;

procedure TImage.Paint;
begin
  if not Assigned(Canvas) then Exit;
  if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
    else Canvas.Brush.Style := bsSolid;
  if FEnabled then Canvas.StretchDraw(Bounds, Bitmap)
    else Canvas.StretchDraw(Bounds, BitmapDisabled);
end;

end.

