//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "RadioBtn.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TRadioBtn *)
{
	new TRadioBtn(NULL);
}
//---------------------------------------------------------------------------
namespace Radiobtn
{
	void __fastcall PACKAGE Register()
	{
		TComponentClass classes[1] = {__classid(TRadioBtn)};
		RegisterComponents(L"Samples", classes, 0);
	}
}
//---------------------------------------------------------------------------
__fastcall TRadioBtn::TRadioBtn(System::Classes::TComponent* AOwner) : TPanel(AOwner)
{
	m_pRaBtn         = new TRadioButton(this);
	m_pRaBtn->Parent = this;
	m_pRaBtn->ControlStyle>>csOpaque;
	m_pRaBtn->Caption= L"";
	m_pRaBtn->OnClick = Onclicked;

	m_lbCaption          = new TLabel(this);
	m_lbCaption->Parent  = this;
	m_lbCaption->ControlStyle>>csOpaque;
	m_lbCaption->OnClick = Onclicked;
	m_lbCaption->OnMouseMove = MouseMoveEvent;

	this->OnMouseMove = MouseMoveEvent;

	FChecked = false;
	FEnabled = true;

	SetChildBounds();
	SetCaption(L"TRadioBtn");
	SetFont(Font);

	ControlStyle >> csOpaque;

	Width  = 100;
	Height = 17;
}
//---------------------------------------------------------------------------
__fastcall TRadioBtn::~TRadioBtn(void)
{
	if(m_lbCaption) delete m_lbCaption;
	if(m_pRaBtn) delete m_pRaBtn;
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::SetChildBounds()
{
	int nEdge = GetSystemMetrics(SM_CXMENUCHECK);

	int mHgt = m_lbCaption->Height;
	if( mHgt>Height ) Height = mHgt;

	m_lbCaption->SetBounds(nEdge+1, (Height-m_lbCaption->Height)/2,
		m_lbCaption->Width, m_lbCaption->Height);

	m_pRaBtn->SetBounds(0, 0, nEdge, Height);
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::CreateWnd(void)
{
	Vcl::Extctrls::TPanel::CreateWnd();

	SetWindowLong( Parent->Handle, GWL_STYLE,
			GetWindowLong(Parent->Handle, GWL_STYLE) & ~WS_CLIPCHILDREN );
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::CreateParams(Controls::TCreateParams &Params)
{
	Vcl::Extctrls::TPanel::CreateParams(Params);

	Params.ExStyle |= WS_EX_TRANSPARENT;
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::Paint()
{
	SetBkMode( Canvas->Handle, TRANSPARENT );
	StyleServices()->DrawParentBackground( Handle, Canvas->Handle, NULL, true);
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::WMSize(Winapi::Messages::TWMSize &Message)
{
	SetChildBounds();
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::SetCaption(String FCaptionTmp)
{
	m_lbCaption->Caption = FCaptionTmp;
	FCaption = FCaptionTmp;
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::SetFont(TFont *FFontTmp)
{
	m_lbCaption->Font = FFontTmp;
	SetChildBounds();
	this->Invalidate();
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::SetChecked(bool bChecked)
{
	m_pRaBtn->Checked = bChecked;
	FChecked = bChecked;
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::SetEnabled(bool bEnabled)
{
	FEnabled = bEnabled;

	m_lbCaption->Enabled = bEnabled;

	m_pRaBtn->Enabled = bEnabled;
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::SetCheckedSameparent(TWinControl* pCtrl)
{
	if( pCtrl->ClassNameIs( TRadioBtn::ClassName() ) ){
		TRadioBtn *pTRadioBtn = (TRadioBtn *)pCtrl;
		if( pTRadioBtn->Checked && pTRadioBtn!=this )
			pTRadioBtn->Checked = false;
	}
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::Onclicked(TObject *Sender)
{
	if(!FChecked) {
		SetChecked(true);
		for( int i=0; i<this->Parent->ControlCount; i++ ){
			SetCheckedSameparent( (TWinControl*)(this->Parent->Controls[i]) );
		}
		if( OnClick ) OnClick(this);
	}
}
//--------------------------------------------------------------------------
void __fastcall TRadioBtn::Click(void)
{
	if( !FEnabled ) return;

	Onclicked(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TRadioBtn::MouseMoveEvent(System::TObject* Sender, System::Classes::TShiftState Shift, int X, int Y)
{
	if( !FEnabled ) return;
}
//---------------------------------------------------------------------------
