// Base Windows Control for CTL
//
// YB
////////////////////////////////////
#include "StdAfx.h"

#include "WControls\CTL_BWC.hpp"

#include "Others\CTL_Pri.hpp"
#include "Others\CTL_Pub.hpp"
#include "Controls\CTL_Ctl.hpp"

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

//***************************************************************************
CTL_BaseWindowsControl::CTL_BaseWindowsControl(CTL_Editor_Control *_pclParentControl,
																							 CWnd *_pclParentWnd,
																							 long _lUserDefinedType /*= 0*/,
																							 long _lUserDefinedCode /*= 0*/)
{
	m_pri_pclParentControl = _pclParentControl;
	m_pri_pclParentWnd = _pclParentWnd;
	m_pri_lUserDefinedType = _lUserDefinedType;
	m_pri_lUserDefinedCode = _lUserDefinedCode;
	
	m_pri_pclWnd = NULL;
}

//***************************************************************************
CTL_BaseWindowsControl::~CTL_BaseWindowsControl()
{
#ifndef CTL_WITH_NO_TUT
	if ( CTL_p_fn_bUnregisterControl != NULL )
	{
		if ( m_pri_pclWnd != NULL )
			CTL_p_fn_bUnregisterControl(m_pri_pclWnd->m_hWnd);
	}
/*	else
	{
		ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
										"Trying to unregister a control from TUT Module",
										"CTL_BaseWindowsControl::~CTL_BaseWindowsControl()",
										E_ERROR_GRAVITY_INSTABLE,
										"Function not found : the TUT DLL is probably not present in ""Edt_Data\\Tools"" directory");
		ERROR_ASSERT( FALSE );
	}*/
#endif //CTL_WITH_NO_TUT
}

//***************************************************************************
long CTL_BaseWindowsControl::m_pub_fn_lGetUserDefinedType()
{
	return m_pri_lUserDefinedType;
}

//***************************************************************************
long CTL_BaseWindowsControl::m_pub_fn_lGetUserDefinedCode()
{
	return m_pri_lUserDefinedCode;
}

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

//***************************************************************************
CWnd *CTL_BaseWindowsControl::m_pub_fn_pclGetParentWnd()
{
	return m_pri_pclParentWnd;
}

#ifndef CTL_WITH_NO_TUT
//***************************************************************************
//For TUT registration
void CTL_BaseWindowsControl::m_pro_fn_vRegisterWindowsControl(CString _csAdditionnalInfo,
																															enum TUT_eControlType_ _eType,
																															CString _csControlString,
																															CWnd *_pclWnd)
{
	ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
									"Trying to register a control for TUT Module",
									"CTL_BaseWindowsControl::m_pub_fn_vRegisterWindowsControl(...)",
									E_ERROR_GRAVITY_INSTABLE,
									"An additonnal information should be stored in TUT's control name...");
	ERROR_ASSERT( !_csAdditionnalInfo.IsEmpty() );

	m_pri_pclWnd = _pclWnd;
	m_pri_fn_vBuildNameForTUT(_csAdditionnalInfo, _eType, _csControlString, _pclWnd);
}
#endif //CTL_WITH_NO_TUT

//***************************************************************************
// Private Functions
//***************************************************************************

#ifndef CTL_WITH_NO_TUT
//***************************************************************************
void CTL_BaseWindowsControl::m_pri_fn_vBuildNameForTUT(CString _csAdditionnalInfo,
																											 enum TUT_eControlType_ _eType,
																											 CString _csControlString,
																											 CWnd *_pclWnd)
{
/*	ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
									"Trying to register a control for TUT Module",
									"CTL_BaseWindowsControl::m_pri_fn_vBuildNameForTUT()",
									E_ERROR_GRAVITY_INSTABLE,
									"Function not found : the TUT DLL is probably not present in ""Edt_Data\\Tools"" directory");
	ERROR_CHECK( CTL_fn_bGetTUT_DLLFunctions() );*/

	if ( CTL_p_fn_vRegisterControl == NULL )
		CTL_fn_bGetTUT_DLLFunctions();

	if ( CTL_p_fn_vRegisterControl != NULL )
	{
		const CString c_csSeparator = " - ";
		
		CString csTUTName;
		
		//Adds TAG
		if ( CTL_g_csCurrentTAG.IsEmpty() )
			csTUTName = CString("CTL alone");
		else
			csTUTName = CString("CTL > ") + CTL_g_csCurrentTAG;

		//Adds Secondary Info
		if ( !CTL_g_csCurrentSecondaryInfo.IsEmpty() )
			csTUTName += c_csSeparator + CTL_g_csCurrentSecondaryInfo;

		//Adds additionnal Info
		if ( !_csAdditionnalInfo.IsEmpty() )
			csTUTName += c_csSeparator + _csAdditionnalInfo;

		//Adds control type
		if ( !_csControlString.IsEmpty() )
			csTUTName += c_csSeparator + _csControlString;

		//To ensure name's unicity : adds the ID
		//CString csID;
		//csID.Format("%i", CTL_fn_lGetCurrentIDForControl());
		//csTUTName += (c_csSeparator + csID);

		//Registers in TUT
		CTL_p_fn_vRegisterControl(_pclWnd->m_hWnd,
															csTUTName,
															_eType);
	}
}
#endif //CTL_WITH_NO_TUT

