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

#include <vcl.h>

#pragma hdrstop

#include "VmxCustomProgress.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(TVmxCustomProgress *)
{
        new TVmxCustomProgress(NULL);
}
//---------------------------------------------------------------------------
__fastcall TVmxCustomProgress::TVmxCustomProgress(TComponent* Owner)
        : TGraphicControl(Owner)
{
  FPr = 0;
  FMax = 100;
  Width = 100;
  Height = 16;
  FColor1 = clActiveCaption;
  FColor2 = clGradientActiveCaption;
}
//---------------------------------------------------------------------------

void __fastcall TVmxCustomProgress::SetColor1 (TColor New)
{
  FColor1 = New;
  Invalidate ();
}

void __fastcall TVmxCustomProgress::SetColor2 (TColor New)
{
  FColor2 = New;
  Invalidate ();
}

void __fastcall TVmxCustomProgress::SetGradientType (TGradientType New)
{
  FGradType = New;
  Invalidate ();
}

void __fastcall TVmxCustomProgress::SetBevel (TVmxBorderType New)
{
  FBevel = New;
  Invalidate ();
}

void __fastcall TVmxCustomProgress::SetPr (int New)
{
  if (New < 0 || New > FMax)
    return;
  FPr = New;
  Invalidate ();
}

void __fastcall TVmxCustomProgress::SetMax (int New)
{
  if (New <= 0)
    return;
  FMax = New;
  Invalidate();
}

#define R(a) ((BYTE)(a))
#define G(a) ((BYTE)(a>>8))
#define B(a) ((BYTE)(a>>16))

void __fastcall TVmxCustomProgress::Paint (void)
{
  Canvas->Brush->Color=Color;
  Canvas->FillRect(TRect(0,0,Width,Height));
  int r0, g0, b0;
  r0 = R(FColor1);
  g0 = G(FColor1);
  b0 = B(FColor1);
  int dr = R(FColor2) - r0;
  int dg = G(FColor2) - g0;
  int db = B(FColor2) - b0;
  if (FGradType == gtHorizontal)
  {
    for (int i = 0; i < Width*FPr/FMax; i++)
    {
      Canvas->Pen->Color = RGB (r0+dr*i/(Width-1),g0+dg*i/(Width-1),b0+db*i/(Width-1));
      Canvas->MoveTo(i,0);
      Canvas->LineTo(i,Height);
    }
  }
  else
  {
    for (int i = 0; i < Height; i++)
    {
      Canvas->Pen->Color = RGB (r0+dr*i/(Height-1),g0+dg*i/(Height-1),b0+db*i/(Height-1));
      Canvas->MoveTo (0,i);
      Canvas->LineTo (Width*FPr/FMax,i);
    }
  }
  switch (FBevel)
  {
    case vbRaised: Frame3D (Canvas, TRect(0,0,Width,Height), clBtnHighlight, clBtnShadow, 1); break;
    case vbLowered: Frame3D (Canvas, TRect(0,0,Width,Height), clBtnShadow, clBtnHighlight, 1); break;
  }
}

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