// Implementation file for the definition of a characteristic base control
/////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "Controls\CTL_BCtl.hpp"

#include "Controls\CTL_Ctl.hpp"

//External Modules
#include "CTL_ErO.hpp"
//End of External Modules

#define C_SPACE_BETWEEN_CONTROLS	2

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//Constructor / Destructor
////////////////////////////
//************************************************************************
CTL_Editor_BaseControl::CTL_Editor_BaseControl(CTL_BaseWindowsControl *_pclBaseWindowsControl,
																							 CWnd *_pclWnd,
																							 CTL_Editor_Control *_pclParentControl,
																							 CTL_tdeBaseControlDisplayType _eDisplayType,
																							 unsigned char _ucPercentWidth,
																							 unsigned short _uwFixedWidth,
																							 unsigned short _uwHeight,
																							 BOOL _bSamePlaceAsNext,
																							 BOOL _bGoToNextLineAfterMe /*= FALSE*/)
{
	m_pri_pclBaseWindowsControl = _pclBaseWindowsControl;
	m_pri_pclWnd = _pclWnd;
	m_pri_pclParentControl = _pclParentControl;
	m_pri_tdeDisplayType = _eDisplayType;

	m_pri_ucPercentWidth = _ucPercentWidth;
	m_pri_uwFixedWidth = _uwFixedWidth;

	m_pri_uwHeight = _uwHeight;

	m_pri_lComputedWidth = 0;
	m_pri_cComputedPercentage = CTL_C_BASE_CONTROL_INVALID_PERCENTAGE;

	m_pri_bSamePlaceAsNext = _bSamePlaceAsNext;
	m_pri_bBeginsOnNextLine = _bGoToNextLineAfterMe;
	
	m_crBaseZoneRect = CRect(0, 0, 0, m_pri_uwHeight);

	m_pri_td_p_fn_vCanBeDisplayedCallBack = NULL;
	m_pri_td_p_fn_vMustBeEnabledCallBack = NULL;

	m_pri_td_p_fn_lGetSize_CallBack = NULL;
}

//************************************************************************
CTL_Editor_BaseControl::~CTL_Editor_BaseControl()
{
	delete m_pri_pclWnd;
}

//Access functions
///////////////////////

//************************************************************************
CRect &CTL_Editor_BaseControl::m_fn_rcrGetZoneRect()
{
	return m_crBaseZoneRect;
}

//************************************************************************
CWnd *CTL_Editor_BaseControl::m_fn_pclGetWnd()
{
	return m_pri_pclWnd;
}

//Member functions
///////////////////////

//************************************************************************
void CTL_Editor_BaseControl::m_fn_vHideBaseControl()
{
	if ( m_pri_pclWnd != NULL )
		m_pri_pclWnd->ShowWindow(SW_HIDE);
}

//************************************************************************
void CTL_Editor_BaseControl::m_fn_vDisplayBaseControl()
{
	if ( m_pri_pclWnd != NULL )
	{
		if ( m_pub_fn_bCanBeDisplayed() )
		{
			m_pri_pclWnd->ShowWindow(SW_SHOW);

			//Enables/Disables
			m_pri_pclWnd->EnableWindow(m_pub_fn_bMustBeEnabled());
		}
		else
			m_pri_pclWnd->ShowWindow(SW_HIDE);
	}
}

//************************************************************************
void CTL_Editor_BaseControl::m_fn_vMoveBaseControl()
{
	if ( m_pri_pclWnd != NULL )
		m_pri_pclWnd->MoveWindow(m_crBaseZoneRect);
}

//************************************************************************
BOOL CTL_Editor_BaseControl::m_pub_fn_bCanBeDisplayed()
{
	BOOL bCanBeDisplayed = TRUE;

	if ( m_pri_td_p_fn_vCanBeDisplayedCallBack != NULL )
		bCanBeDisplayed = m_pri_td_p_fn_vCanBeDisplayedCallBack(this);

	return bCanBeDisplayed;
}

//************************************************************************
BOOL CTL_Editor_BaseControl::m_pub_fn_bMustBeEnabled()
{
	BOOL bMustBeEnabled = TRUE;

	if ( m_pri_td_p_fn_vMustBeEnabledCallBack != NULL )
		bMustBeEnabled = m_pri_td_p_fn_vMustBeEnabledCallBack(this);

	return bMustBeEnabled;
}

//************************************************************************
void CTL_Editor_BaseControl::m_pub_fn_vComputeBaseControlZone()
{
	m_crBaseZoneRect.left = 0;
	m_crBaseZoneRect.top = 0;
	m_crBaseZoneRect.right = m_pri_lComputedWidth;
	m_crBaseZoneRect.bottom = m_pri_uwHeight;
}

//************************************************************************
void CTL_Editor_BaseControl::m_pub_fn_vMoveInZone(CRect &_r_crNextRectOnSameLine,
																									CRect &_r_crNextRectOnNextLine)
{
	//Moves Base Control to the right position
	if ( m_pri_bBeginsOnNextLine )
		m_crBaseZoneRect.OffsetRect(_r_crNextRectOnNextLine.left, _r_crNextRectOnNextLine.top);
	else
		m_crBaseZoneRect.OffsetRect(_r_crNextRectOnSameLine.left, _r_crNextRectOnSameLine.top);
	m_fn_vMoveBaseControl();

	//Computes Zones for next Base Control
	//Adjusts bottom of current line (in case of a bigger control than previous ones)
	_r_crNextRectOnSameLine.bottom = _r_crNextRectOnSameLine.top 
																	+ max(_r_crNextRectOnSameLine.Height(), m_pri_uwHeight);

	short wOldHeight = _r_crNextRectOnNextLine.Height();
	_r_crNextRectOnNextLine.top = _r_crNextRectOnSameLine.bottom + C_SPACE_BETWEEN_CONTROLS;
	_r_crNextRectOnNextLine.bottom = _r_crNextRectOnNextLine.top + wOldHeight;

	if ( m_pri_bBeginsOnNextLine )
		_r_crNextRectOnSameLine = _r_crNextRectOnNextLine;

	if ( !m_pri_bSamePlaceAsNext )
		_r_crNextRectOnSameLine.left += (m_pri_lComputedWidth + C_SPACE_BETWEEN_CONTROLS);
}

//************************************************************************
void CTL_Editor_BaseControl::m_pub_fn_vSetCanBeDisplayedCallBack(CTL_td_p_fn_bBaseControlCanBeDisplayed _p_fn_vCallBack)
{
	ERROR_ASSERT( m_pri_td_p_fn_vCanBeDisplayedCallBack == NULL );

	m_pri_td_p_fn_vCanBeDisplayedCallBack = _p_fn_vCallBack;
}

//************************************************************************
void CTL_Editor_BaseControl::m_pub_fn_vSetMustBeEnabledCallBack(CTL_td_p_fn_bBaseControlCanBeDisplayed _p_fn_vCallBack)
{
	ERROR_ASSERT( m_pri_td_p_fn_vMustBeEnabledCallBack == NULL );

	m_pri_td_p_fn_vMustBeEnabledCallBack = _p_fn_vCallBack;
}

//************************************************************************
CTL_Editor_Control *CTL_Editor_BaseControl::m_pub_fn_pclGetParentControl()
{
	return m_pri_pclParentControl;
}

//************************************************************************
void CTL_Editor_BaseControl::m_pub_fn_vSetGetSize_CallBack(CTL_tdp_fn_lGetSizeOfWindow _p_fn_lCallBack)
{
	ERROR_ASSERT( m_pri_td_p_fn_lGetSize_CallBack  == NULL );

	m_pri_td_p_fn_lGetSize_CallBack = _p_fn_lCallBack;
}

//************************************************************************
CTL_tdeBaseControlDisplayType CTL_Editor_BaseControl::m_pub_fn_tdeGetDisplayType()
{
	return m_pri_tdeDisplayType;
}
