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

#include "ListViewBase.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TListViewBase::TListViewBase(TComponent* Owner)
		: TTableView( Owner )
{
	OnDrawBKG  = DoDrawBKG;
 	OnTitle    = DoTitle;
	OnBottom   = DoBottom;

	OnCellHgt  = DoCellHgt;
	OnClickL   = DoClickL;
	OnClickLP  = DoClickLP;
	OnClickR   = DoClickR;
	OnClickRP  = DoClickRP;

	OnMDCell   = DoMDCell;
	OnMDNum    = DoMDNum;
	OnMDTop    = DoMDTop;
	OnMDBottom = DoMDBottom;
	OnMDMOV    = DoMDMOV;

	FRightCellChange  = false;
	OnDrawCell    = DoCellDraw;
	OnCellData    = DoCellData;
	OnCellLClick  = DoCellLClick;
	OnCellRClick  = DoCellRClick;
	OnCellLDClick = DoCellDBClick;

	FRightIconChange = false;
	FIconChange   = false;
	OnDrawIcon    = DoIconDraw;
	OnIconData    = DoIconData;
	OnIconsNum    = DoIconsNum;
	OnIconsBounds = DoIconsBounds;
	OnIconLClick  = DoIconLClick;
	OnIconRClick  = DoIconRClick;
	OnIconLDClick = DoIconDBClick;

	m_MoveTimer = new TTimer(this);
	m_MoveTimer->Enabled = false;
	m_MoveTimer->Interval = 100;
	m_MoveTimer->OnTimer = MoveTimerProc;
}
//---------------------------------------------------------------------------
__fastcall TListViewBase::~TListViewBase()
{
   	SAFE_DELETE(m_MoveTimer)
}
//---------------------------------------------------------------------------
void __fastcall TListViewBase::BtnDown(TStateBtn *pCur, TPoint pt, bool bLeft)
{
	NULL_RETURN(pCur)

	TStateBtn *pCurTmp = pCur;

	if(bLeft || FRightCellChange ) {

		TStateBtn *pBtn = NULL;
		if(  pCur && pCur->m_PAR ) {
			bool bIconChange = true;
			if( !bLeft && !FRightIconChange) bIconChange = false;
			if( bIconChange ) {
				pCur->Down = true;
				pCur = pCur->m_PAR;
			}
		}

		BTNLST::iterator lst_FBTNLST;
		for( lst_FBTNLST = m_LstBtn.begin();
			 lst_FBTNLST!= m_LstBtn.end();
			 lst_FBTNLST++ )
		{
			pBtn = *lst_FBTNLST;
			if( pBtn==pCur ) {
				pBtn->Down = true;
				IndSel = pBtn->Index;
				pBtn->Paint();
			}
			else if( pBtn->Down ) {
				pBtn->Down = false;
				pBtn->Enter = false;
				pBtn->Paint();
			}
		}
	}

	if( bLeft && OnClickL ) {

		OnClickL(pCurTmp, pt);
	}
	else if( !bLeft && OnClickR ) {

		OnClickR(pCurTmp, pt);
    }
}
//---------------------------------------------------------------------------
void __fastcall TListViewBase::BtnUp(TStateBtn *pCur, TStateBtn *pSel, TPoint pt, bool bLeft)
{
	if( pSel && pSel->m_PAR ) {
		bool bIconChange = true;
		if( !bLeft && !FRightIconChange) bIconChange = false;
		if( bIconChange ) {
			pSel->Down = false;
			pSel->Enter = false;
			pSel->Paint();
		}
	}

	if( bLeft && OnClickLP ) {
		OnClickLP(pCur, pt);
	}
	else if( !bLeft && OnClickRP ) {
		OnClickRP(pCur, pt);
	}

	m_MoveTimer->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TListViewBase::BtnMove(TStateBtn *pCur, TPoint pt)
{
	TStateBtn *pCurTmp = pCur;

	TStateBtn *pBtn = NULL;
	if( !pCur ) IndFocus = -1;

	if(  pCur!=m_BtnFocus && m_BtnFocus && m_BtnFocus->m_PAR ) {
		m_BtnFocus->Enter = false;
		m_BtnFocus->Paint();
	}

	if( pCur && pCur->m_PAR ) {
		pCur->Enter = true;
		pCur = pCur->m_PAR;
	}

	if( pCur!=m_BtnFocus ) {

		BTNLST::iterator lst_FBTNLST;
		for( lst_FBTNLST = m_LstBtn.begin();
			 lst_FBTNLST!= m_LstBtn.end();
			 lst_FBTNLST++ )
		{
			pBtn = *lst_FBTNLST;
			if( pBtn==pCur ) {
				pBtn->Enter = true;
				IndFocus = pBtn->Index;
				pBtn->Paint();
			}
			else if( pBtn->State==Bs_ENTER ) {
				pBtn->Enter = false;
				pBtn->Paint();
			}
		}
	}

	if( m_bLeft && OnMDMOV ) {
		OnMDMOV(m_BtnSel, pCurTmp, pt);
	}

	m_ptTimer = pt;
	if( m_bLeft && m_BtnSel && NULL==m_BtnSel->m_PAR && (!PtInRect( m_RtContent, pt ) || pCurTmp==NULL) ) {
		if(!m_MoveTimer->Enabled)
			m_MoveTimer->Enabled = true;
	}
	else if( m_MoveTimer->Enabled ){
		m_MoveTimer->Enabled = false;
	}
}
//---------------------------------------------------------------------------
void __fastcall TListViewBase::MoveTimerProc(System::TObject* Sender)
{
	if( m_ptTimer.X<0||m_ptTimer.Y<0 ) {
		SetSBPos(SBS_POS, GetCellBeg()-1);
	}
	else {
      	SetSBPos(SB_LINEDOWN, GetCellBeg()+1);
	}
}
//---------------------------------------------------------------------------
int  __fastcall TListViewBase::DoMouseWheel(TObject *Sender)
{
	if(m_BtnFocus && m_BtnFocus->Enter) {
		m_BtnFocus->Enter = false;
		m_BtnFocus->Paint();
	}
}
//---------------------------------------------------------------------------
int __fastcall TListViewBase::DoMDCell(TTableView *Sender, int iCellIndex)
{
	if( iCellIndex<0 || iCellIndex>=GetCellNum() )
		return 0;

	TStateBtn *rowCell = (TStateBtn *)GetCellAds(iCellIndex);

	if(!rowCell) {
		rowCell               = new TStateBtn(this);
		rowCell->OnNotifyDraw = OnDrawCell;
		rowCell->OnClick      = OnCellLClick;
		rowCell->OnClickR     = OnCellRClick;
		rowCell->OnDBClick    = OnCellLDClick;
		rowCell->Index = iCellIndex;

		if(OnIconsNum) {
			int iconNum = OnIconsNum(rowCell);
			for(int i=0; i<iconNum; i++) {

				TStateBtn *pBtnCol    = new TStateBtn(this);
				pBtnCol->OnNotifyDraw = OnDrawIcon;
				pBtnCol->OnClick      = OnIconLClick;
				pBtnCol->OnClickR     = OnIconRClick;
				pBtnCol->OnDBClick    = OnIconLDClick;
				pBtnCol->Index        = i;
				rowCell->SetSub(pBtnCol);

				if(!FIconChange) {
					DoIconDataAndBounds(pBtnCol);
				}
			}
		}
	}

	rowCell->Index = iCellIndex;
	if( OnCellData )
		OnCellData(rowCell);

	if(FIconChange) {

		BTNLST *pLstSub = rowCell->GetSub();
		BTNLST::iterator lst_FBTNLST;
		for(lst_FBTNLST  = pLstSub->begin();
			lst_FBTNLST != pLstSub->end();
			lst_FBTNLST++ )
		{
			DoIconDataAndBounds(*lst_FBTNLST);
		}
	}

	if( IndSel==rowCell->Index )
		rowCell->Down = true;
	else if( rowCell->Down )
		rowCell->Down = false;

	if( IndFocus==rowCell->Index )
		rowCell->Enter = true;
	else if( rowCell->Enter )
		rowCell->Enter = false;

	return ((int)(rowCell));
}
//---------------------------------------------------------------------------
void __fastcall TListViewBase::DoIconDataAndBounds(TStateBtn *pBtnCol)
{
	if( OnIconData )
		OnIconData(pBtnCol);

	TRect rtBounds;
	if(OnIconsBounds) {
		OnIconsBounds(pBtnCol, rtBounds);
		pBtnCol->SetBounds(rtBounds.Left, rtBounds.Top, rtBounds.Width(), rtBounds.Height());
	}
}
//---------------------------------------------------------------------------

void __fastcall TListViewBase::SetTextFontSize(int sz)
{
	Canvas->Font->Size = sz;
	m_CanvasDr->Font->Size = sz;
}
//---------------------------------------------------------------------------

