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

#include "PanelDB.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TPanelDB::TPanelDB(TComponent* AOwner)
	: Vcl::Extctrls::TPanel(AOwner)
{
	ParentColor 	= false;
	Ctl3D 		    = false;
	ParentCtl3D     = false;
	DoubleBuffered  = true;

	m_bBkgDrawed    = false;
	m_bDBCreated    = false;

	m_BtnSel = m_BtnFocus = NULL;
	m_bLeft = false;

	m_CanvasDr = new TCanvas();
    m_CanvasDr->Font->Name = "Tahoma";

	m_BMPBkg   = NULL;
}
//---------------------------------------------------------------------------
__fastcall TPanelDB::~TPanelDB()
{
	SAFE_DELETE(m_BMPBkg)
	BtnClear();
	DCRelease();
	SAFE_DELETE( m_CanvasDr );
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::CreateWnd(void)
{
	Vcl::Extctrls::TPanel::CreateWnd();
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::CreateParams(Controls::TCreateParams &Params)
{
	Vcl::Extctrls::TPanel::CreateParams(Params);
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::WMEraseBkgnd(TWMEraseBkgnd &Message)
{
	Message.Result = true;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::WMSize(Winapi::Messages::TWMSize &Message)
{
	DCRelease();
	DCInit();
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::DCInit()
{
	m_BufDC     = CreateCompatibleDC(Canvas->Handle);
	m_BufBitmap = CreateCompatibleBitmap(Canvas->Handle, Width, Height);
	SelectObject(m_BufDC, m_BufBitmap);
	m_CanvasDr->Handle = m_BufDC;
	m_bDBCreated = true;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::DCRelease()
{
	DeleteDC(m_BufDC);
	DeleteObject(m_BufBitmap);
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::Paint()
{
	if( !m_bDBCreated || !m_CanvasDr ) DCInit();
	DoPaintBKG(m_CanvasDr);

	if(FOnPaintBKG) FOnPaintBKG(this, m_CanvasDr, TRect(0, 0, Width, Height));

	DoPaintLst(m_CanvasDr);

	BitBlt(Canvas->Handle,
		   0, 0, Width, Height,
		   m_CanvasDr->Handle,
		   0, 0, SRCCOPY);
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::DoPaintBKG(TCanvas *pCanvas)
{
	if( m_BMPBkg && Width==m_BMPBkg->Width && Height==m_BMPBkg->Height) {
		StretchBlt(pCanvas->Handle, 0, 0, Width, Height,
				m_BMPBkg->Canvas->Handle, 0, 0, Width, Height,
				SRCCOPY);
	}
	else {
    	m_bBkgDrawed = false;
	}

	if( !m_bBkgDrawed) {

		TRect rtThis = TRect(0, 0, Width, Height);

		if( FOnDrawBKG ) {
			FOnDrawBKG(this, pCanvas, rtThis);
		}
		else {
			pCanvas->Brush->Color = this->Color;
			pCanvas->FillRect(rtThis);
		}

		if( !m_BMPBkg ) {
			m_BMPBkg = new TBitmap();
		}
		m_BMPBkg->SetSize(Width, Height);
		StretchBlt(m_BMPBkg->Canvas->Handle, 0, 0,Width, Height,
							pCanvas->Handle, 0, 0,Width, Height,
							SRCCOPY);
		m_bBkgDrawed = true;
	}
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::DoPaintLst(TCanvas *pCanvas)
{
	BTNLST::iterator lst_FBTNLST;
	for( lst_FBTNLST  = m_LstBtn.begin();
		 lst_FBTNLST != m_LstBtn.end();
		 lst_FBTNLST++ )
	{
		(*lst_FBTNLST)->Paint(pCanvas);
	}
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::MouseDown(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y)
{
	TPoint pt = TPoint(X, Y);
	TStateBtn *pCur = GetBtnCur(pt);
	NULL_RETURN(pCur)

	if( Button==mbLeft ) {
		m_bLeft = true;
		BtnDown(pCur, pt);
	}
	else if( Button==mbRight ) {
		m_bLeft = false;
		BtnDown(pCur, pt, false);
	}

	m_BtnSel = pCur;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::MouseUp(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y)
{
	TPoint pt = TPoint(X, Y);
	if( Button==mbLeft ) {
		BtnUp(m_BtnFocus, m_BtnSel, pt);
	}
	else if( Button==mbRight ) {
		BtnUp(m_BtnFocus, m_BtnSel, pt, false);
	}

	if( m_BtnFocus==m_BtnSel && m_BtnSel ) {
		if( m_bLeft && m_BtnSel->OnClick )
			m_BtnSel->OnClick(m_BtnSel);
		else if( !m_bLeft && m_BtnSel->OnClickR )
			m_BtnSel->OnClickR(m_BtnSel);
	}

	m_BtnSel = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::MouseMove(System::Classes::TShiftState Shift, int X, int Y)
{
	TPoint pt = TPoint(X, Y);
	TStateBtn *pCur = GetBtnCur(pt);

	BtnMove(pCur, pt);

	m_BtnFocus = pCur;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::CMMouseLeave(TMessage &Message)
{
	BtnMove(NULL, TPoint(-1, -1));

	BtnLeave();

	m_BtnFocus = NULL;
	m_BtnSel = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::Click(void)
{
	if( OnClick ) OnClick(this);
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::DblClick(void)
{
	if( m_BtnFocus ) {
		if( m_bLeft && m_BtnFocus->OnDBClick )
			m_BtnFocus->OnDBClick(m_BtnFocus);
	}

	if( OnDblClick ) OnDblClick(this);
}
//---------------------------------------------------------------------------
TStateBtn* __fastcall TPanelDB::GetBtnCur(TPoint pt)
{
	TStateBtn *pCur = 0;
	BTNLST::iterator lst_FBTNLST;
	for( lst_FBTNLST = m_LstBtn.begin();
		 lst_FBTNLST!= m_LstBtn.end();
		 lst_FBTNLST++ )
	{
		pCur = (*lst_FBTNLST)->GetSubCusor( pt );
		if( pCur ) { break; }
	}

	return pCur;
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::BtnAdd(TStateBtn *pBtn)
{
	m_LstBtn.push_back(pBtn);
}
//---------------------------------------------------------------------------
void __fastcall TPanelDB::BtnClear()
{
	if(m_LstBtn.size()<=0) return;

	BTNLST::iterator lst_FBTNLST;
	for( lst_FBTNLST = m_LstBtn.begin();
		 lst_FBTNLST!= m_LstBtn.end();
		 lst_FBTNLST++ )
	{
		TStateBtn *pBtn = *lst_FBTNLST;
		SAFE_DELETE(pBtn)
	}
	m_LstBtn.clear();
}
//---------------------------------------------------------------------------
