//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "VmxCustomCheck.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TVmxCustomCheck *)
{
        new TVmxCustomCheck(NULL);
}
//---------------------------------------------------------------------------
__fastcall TVmxCustomCheck::TVmxCustomCheck(TComponent* Owner)
        : TGraphicControl(Owner)
{
  NCN = new Graphics::TBitmap;
  NCO = new Graphics::TBitmap;
  NCD = new Graphics::TBitmap;
  CN = new Graphics::TBitmap;
  CO = new Graphics::TBitmap;
  CD = new Graphics::TBitmap;
  State = vcNormal;
}
//---------------------------------------------------------------------------

void __fastcall TVmxCustomCheck::SetNCN (Graphics::TBitmap *New)
{
  NCN->Assign(New);
  if (!FChecked)
    Invalidate ();
}

void __fastcall TVmxCustomCheck::SetCN (Graphics::TBitmap *New)
{
  CN->Assign(New);
  if (FChecked)
    Invalidate ();
}

void __fastcall TVmxCustomCheck::SetNCO (Graphics::TBitmap *New)
{
  NCO->Assign(New);
}

void __fastcall TVmxCustomCheck::SetNCD (Graphics::TBitmap *New)
{
  NCD->Assign(New);
}

void __fastcall TVmxCustomCheck::SetCO (Graphics::TBitmap *New)
{
  CO->Assign(New);
}

void __fastcall TVmxCustomCheck::SetCD (Graphics::TBitmap *New)
{
  CD->Assign(New);
}

void __fastcall TVmxCustomCheck::SetCaption (AnsiString New)
{
  FCaption = New;
  Invalidate ();
}

void __fastcall TVmxCustomCheck::SetChecked (bool New)
{
  FChecked = New;
  Invalidate ();
}

void __fastcall TVmxCustomCheck::Paint (void)
{
  Graphics::TBitmap *Bmp;
  switch (State)
  {
    case vcNormal: if (FChecked) Bmp = CN; else Bmp = NCN; break;
    case vcHover: if (FChecked) Bmp = CO; else Bmp = NCO; break;
    case vcDown: if (FChecked) Bmp = CD; else Bmp = NCD; break;
  }
  int y = (Canvas->TextHeight(FCaption)>NCN->Height?Canvas->TextHeight(FCaption)/2-NCN->Height/2:0);
  int ty = (Canvas->TextHeight(FCaption)>NCN->Height?0:NCN->Height/2-Canvas->TextHeight(FCaption)/2);
  Canvas->Draw(0,y,Bmp);
  Canvas->Brush->Style=bsClear;
  Canvas->Font->Assign(Font);
  Canvas->TextOutA(Bmp->Width+4,ty,FCaption);
}

void __fastcall TVmxCustomCheck::CMMouseEnter (TMessage &Msg)
{
  if (State != vcDown)
  {
    State = vcHover;
    Invalidate ();
  }
}

void __fastcall TVmxCustomCheck::CMMouseLeave (TMessage &Msg)
{
  if (State != vcDown)
  {
    State = vcNormal;
    Invalidate ();
  }
}

void __fastcall TVmxCustomCheck::MouseDown (TMouseButton Button, TShiftState Shift, int X, int Y)
{
  State = vcDown;
  Invalidate ();
}

void __fastcall TVmxCustomCheck::MouseUp (TMouseButton Button, TShiftState Shift, int X, int Y)
{
  State = vcHover;
  FChecked = !FChecked;
  Invalidate ();
  if (FOnChange!=NULL)
    FOnChange(this);
}

namespace Vmxcustomcheck
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TVmxCustomCheck)};
                 RegisterComponents("VMX", classes, 0);
        }
}
//---------------------------------------------------------------------------
