#include <vcl.h>
#pragma hdrstop
#include "SwitchBttn1.h"
#pragma package(smart_init)
static inline void ValidCtrCheck(TSwitchBttn *)
{
new TSwitchBttn(NULL);
}
//---------------------------------------------------------------------------
__fastcall TSwitchBttn::TSwitchBttn(TComponent* Owner)
: TGraphicControl(Owner)
{
FMousePos = 0;
FBmpResID = 0;
FSwitchID = 0;
FSwitchMax = 0;
FHintID=0;
FAutoLoad = false;
FAutoSwitch = true;
Cursor = crHandPoint;
Width = 100;
Height = 36;
FBmp = new Graphics::TBitmap();
}
//---------------------------------------------------------------------------
__fastcall TSwitchBttn::~TSwitchBttn()
{
delete FBmp;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::Paint()
{
if(FBmp==NULL || FBmp->Empty || FBmp->Width<4 || FSwitchMax>(USHORT)FBmp->Height)
{
Canvas->Brush->Color = Color;
Canvas->Pen->Color = Color;
Canvas->FillRect(Canvas->ClipRect);
if(Width>2 && Height>2)
{
Canvas->Pen->Color = clBtnHighlight;
Canvas->MoveTo(0,0);
Canvas->LineTo(0,Height-1);
Canvas->MoveTo(0,0);
Canvas->LineTo(Width-1,0);
Canvas->Pen->Color = clBtnShadow;
Canvas->MoveTo(0,Height-1);
Canvas->LineTo(Width-1,Height-1);
Canvas->MoveTo(Width-1,0);
Canvas->LineTo(Width-1,Height-1);
}
return;
}
int TP = FSwitchID * Height;
if( TP > (int)(FBmp->Height-Height) )TP=0;
int LFT;
if(Enabled)
{
switch(FMousePos)
{
case CNTSMOUSE_IN : LFT = FBmp->Width/4; break;
case CNTSMOUSE_DOWN : LFT = FBmp->Width/2; break;
default : LFT=0; break;
}
}
else LFT = Width * 3;
TRect r;
r.Left = LFT;
r.Right = LFT + Width;
r.Top = TP;
r.Bottom = TP+Height;
Canvas->CopyRect(ClientRect, FBmp->Canvas, r);
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetBtnBitmap(Graphics::TBitmap *bmp)
{
if(bmp!=FBmp)
{
FBmp->Assign(bmp);
if(!FBmp->Empty)
{
Height = FSwitchMax==0 ? FBmp->Height : (FBmp->Height / (FSwitchMax+1));
if(FBmp->Width<4)return;
Width = FBmp->Width/4;
}
Paint();
}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetResourceID(USHORT v)
{
FBmpResID = v;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetSwitchMax(USHORT m)
{
if(m < (USHORT)0xFFFF)FSwitchMax = m;
else FSwitchMax = m-1;
if(FBmp->Empty)return;
if(FBmp->Height>(USHORT)FSwitchMax)
{
Height = FSwitchMax==0 ? FBmp->Height : (FBmp->Height / (FSwitchMax+1));
Paint();
}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetSwitchID(USHORT id)
{
if(id>FSwitchMax)FSwitchID=0;
else FSwitchID=id;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetHintID(USHORT v)
{
FHintID = v;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::WndProc(Messages::TMessage &Message)
{
switch ( Message.Msg )
{
case CM_MOUSEENTER : if(Enabled && !ComponentState.Contains(csDesigning)){FMousePos = CNTSMOUSE_IN; Paint();}; break;
case CM_MOUSELEAVE : if(Enabled && !ComponentState.Contains(csDesigning)){FMousePos = CNTSMOUSE_OUT; Paint();}; break;
default : break;
}
TGraphicControl::WndProc(Message); //??::WndProc
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseDown(Button, Shift, X, Y);
if(!Shift.Contains(ssLeft))return;
if(!Enabled)return;
FMousePos = CNTSMOUSE_DOWN;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseUp(Button, Shift, X, Y);
if(!Shift.Contains(ssLeft))return;
FMousePos = CNTSMOUSE_OUT;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::Loaded(void)
{
if(!ComponentState.Contains(csDesigning))
{
if(FAutoLoad)
{
if(FBmpResID!=0)
{
FBmp->LoadFromResourceID(0, FBmpResID);
if(!FBmp->Empty)
{
Height = FSwitchMax==0 ? FBmp->Height : (FBmp->Height / (FSwitchMax+1));
if(FBmp->Width<4)return;
Width = FBmp->Width/4;
}
Paint();
}
}
if(FHintID!=0)
{
Hint = LoadStr(FHintID);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetAutoLoad(bool b)
{
FAutoLoad = b;
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetEnabled(bool Value)
{
TControl::Enabled = Value;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetColor(TColor)
{
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::Switch(void)
{
if(FSwitchID==FSwitchMax)FSwitchID=0;
else FSwitchID++;
Paint();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SwitchTo(USHORT id)
{
if(id>FSwitchMax)return;
else
{
FSwitchID=id;
Paint();
}
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::Click()
{
TGraphicControl::Click();
if(FAutoSwitch && Enabled)Switch();
}
//---------------------------------------------------------------------------
void __fastcall TSwitchBttn::SetAutoSwitch(bool b)
{
FAutoSwitch = b;
}
//-------------------------------------------------------------------------
namespace Switchbttn1
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TSwitchBttn)};
RegisterComponents("JT Components", classes, 0);
}
}
//---------------------------------------------------------------------------