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

#include "StateBtn.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#include "PanelDB.h"
//---------------------------------------------------------------------------
__fastcall TStateBtn::TStateBtn(TPanelDB *panel)
{
	m_Panel  = panel;

	FIndex = 0;
	FLeft = FTop = FWidth = FHeight = 0;
	FState = 0;
	FStateCount = 4;

	FEnable     = true;
	FEnter      = false;
	FDown 		= false;
	FStateCus   = false;

	m_UP = m_PRE = m_NEXT = m_PAR = NULL;
	m_Data = NULL;
}
//---------------------------------------------------------------------------
__fastcall TStateBtn::~TStateBtn()
{
	Clear();
}
//---------------------------------------------------------------------------
void __fastcall TStateBtn::Clear()
{
	if( m_LstSub.size() ) {

		BTNLST::iterator lst_FBTNLST;
		for( lst_FBTNLST = m_LstSub.begin();
			 lst_FBTNLST!= m_LstSub.end();
			 lst_FBTNLST++ )
		{
			TStateBtn *pBtn = *lst_FBTNLST;
			SAFE_DELETE(pBtn)
		}
		m_LstSub.clear();
	}
}
//---------------------------------------------------------------------------
TRect __fastcall TStateBtn::GetBounds()
{
	TRect rtThis;
	if(m_PAR) {
		rtThis = TRect(m_PAR->Left+FLeft, m_PAR->Top+FTop,
			m_PAR->Left+FLeft+FWidth, m_PAR->Top+FTop+FHeight);
	}
	else {
		rtThis = TRect(FLeft, FTop, FLeft+FWidth, FTop+FHeight);
	}

	return rtThis;
}
//---------------------------------------------------------------------------
BTNLST* __fastcall TStateBtn::GetSub()
{
	return &m_LstSub;
}
//---------------------------------------------------------------------------
TStateBtn* __fastcall TStateBtn::GetSubCusor(TPoint pt)
{
	TStateBtn *pCur = this;
	TRect rt = TRect( Left, Top, Left+Width, Top+Height);

	if( PtInRect( rt, pt ) ) {

		TStateBtn *pSub = 0;
		if( m_LstSub.size() ) {

			TPoint ptTmp = pt;
			BTNLST::iterator lst_FBTNLST;
			for(lst_FBTNLST  = m_LstSub.begin();
				lst_FBTNLST != m_LstSub.end();
				lst_FBTNLST++ )
			{
				ptTmp = TPoint(pt.X-Left, pt.Y-Top);
				pSub = (*lst_FBTNLST)->GetSubCusor(ptTmp);
				if( pSub ) return pSub;
			}
		}

		if( 0==pSub ) return pCur;
	}

	return 0;
}
//---------------------------------------------------------------------------
void __fastcall TStateBtn::SetSub(TStateBtn *pBtn)
{
	m_LstSub.push_back(pBtn);
	pBtn->m_PAR = this;
}
//---------------------------------------------------------------------------
void __fastcall TStateBtn::SetBounds(int nLeft, int nTop, int nWidth, int nHeight)
{
	Left   = nLeft;
	Top    = nTop;
	Width  = nWidth;
	Height = nHeight;
}
//---------------------------------------------------------------------------
void __fastcall TStateBtn::SetData(void *pData)
{
	m_Data = pData;
}
//----------------------------------------------------------------------------
bool __fastcall TStateBtn::DoNotifyStateHandle()
{
	if( !FStateCus ) {

		int CurStateTmp = FState;
		if( false==FEnable ) {

			CurStateTmp = Bs_DISABLE;

		}else {

			if( FDown ) CurStateTmp = Bs_DOWN;
			else if( FEnter ) CurStateTmp = Bs_ENTER;
			else CurStateTmp = Bs_NORMAL;

			if( FStateCount<=CurStateTmp ) CurStateTmp = FStateCount-1;
		}

		FState = CurStateTmp;
	}
//	else if( OnNotifyState ) OnNotifyState(this);

	if( OnNotifyState ) OnNotifyState(this);

	return true;
}
//----------------------------------------------------------------------------
void __fastcall TStateBtn::Paint(TCanvas *Canvas)
{
	if( !DoNotifyStateHandle() ) return;

	TCanvas *CanvasDr = Canvas;

	TRect rtThis = GetBounds();

	if( NULL==CanvasDr ) CanvasDr = m_Panel->m_CanvasDr;

	if( FNotifyDraw )  //»æÖÆ
	{
		if( FNotifyDraw ) {

			if( !m_PAR && FState==Bs_NORMAL && m_Panel->m_BMPBkg ) {

				StretchBlt(CanvasDr->Handle,
						rtThis.left, rtThis.Top,
						rtThis.Width(), rtThis.Height(),
						m_Panel->m_BMPBkg->Canvas->Handle,
						rtThis.left, rtThis.Top,
						rtThis.Width(), rtThis.Height(),
						SRCCOPY);
			}
			FNotifyDraw(this, FState, rtThis, CanvasDr);
		}

		if( m_LstSub.size() ) {

			BTNLST::iterator lst_FBTNLST;
			for(lst_FBTNLST  = m_LstSub.begin();
				lst_FBTNLST != m_LstSub.end();
				lst_FBTNLST++ )
			{
				(*lst_FBTNLST)->Paint(CanvasDr);
			}
		}
	}

	if( NULL==Canvas ) {

		HDC hDC = GetDC(m_Panel->Handle);
		StretchBlt(hDC, rtThis.Left, rtThis.Top, rtThis.Width(), rtThis.Height(),
		   CanvasDr->Handle, rtThis.Left, rtThis.Top, rtThis.Width(), rtThis.Height(),
		   SRCCOPY);
		DeleteDC(hDC);
	}
}
//---------------------------------------------------------------------------





