// Global private functions
//
// YB
//////////////////////////////
#include "StdAfx.h"

#include "Others\CTL_Pri.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "Controls\CTL_Ctl.hpp"

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

//------------------------------------------------------------------------
// Global vars
CString CTL_g_csModuleNameForErO;

//Module
HMODULE CTL_g_hModule = 0;

//Cursors for BaseFormView class
HCURSOR CTL_g_hcursor_CloseHand;
HCURSOR CTL_g_hcursor_UpDownHand;

#ifndef CTL_WITH_NO_TUT
//The TUT Dll	Functions
CTL_td_p_fn_vRegisterControl CTL_p_fn_vRegisterControl = NULL;
CTL_td_p_fn_bUnregisterControl CTL_p_fn_bUnregisterControl = NULL;

//For TUT Module
CString CTL_g_csCurrentTAG;
CString CTL_g_csCurrentSecondaryInfo;
#endif //CTL_WITH_NO_TUT

// End of Global vars
//------------------------------------------------------------------------


//******************************************************************************
void CTL_fn_vInternalInitModule()
{
	//Computes version String
	CTL_g_csVersion = "2.0.3 [" + CString(__DATE__) + " (using TUT)]";

	//Registers Module for ErO
	CTL_g_csModuleNameForErO = "Data / Controls DLL";

	ERROR_g_fn_vAddAModuleDescriptor(	CTL_g_csModuleNameForErO,
																		CTL_g_csVersion,
																		"Yves BABITCH",
																		"01 48 18 53 32 (Montreuil)",
																		"ybabitch@ubisoft.fr",
																		"Daniel PALIX",
																		"04 50 51 26 63 (Annecy)",
																		"dpalix@ubisoft.fr",
																		"","",""
																	);
}

//******************************************************************************
long CTL_fn_lComputeSizeOfStaticForName(class CTL_Editor_BaseControl *_pclSenderBaseControl)
{
	CTL_Editor_Control *pclControl = _pclSenderBaseControl->m_pub_fn_pclGetParentControl();

	ERROR_ASSERT( pclControl != NULL );

	CString csTextToDisplay = pclControl->m_pub_fn_csGetControlName();

	CClientDC dc(_pclSenderBaseControl->m_fn_pclGetWnd());
	dc.SelectObject(_pclSenderBaseControl->m_fn_pclGetWnd()->GetFont());

	return ( dc.GetTextExtent(csTextToDisplay).cx + 6 );
}

#ifndef CTL_WITH_NO_TUT
//******************************************************************************
BOOL CTL_fn_bGetTUT_DLLFunctions()
{
	if ( CTL_p_fn_vRegisterControl == NULL )
	{
		//Gets Module
#ifdef _DEBUG
		HMODULE hTUTModule = (HMODULE)LoadLibrary("Edt_Data\\Tools\\TUTDFEvd.dll");
#else
		HMODULE hTUTModule = (HMODULE)LoadLibrary("Edt_Data\\Tools\\TUTDFEvr.dll");
#endif //_DEBUG_
	
		if ( hTUTModule != NULL )
		{
			//Gets functions
			FARPROC Function = GetProcAddress(hTUTModule, "fn_vRegisterControl");

			CTL_p_fn_vRegisterControl = (CTL_td_p_fn_vRegisterControl)Function;

			if ( CTL_p_fn_bUnregisterControl == NULL )
			{
				Function = GetProcAddress(hTUTModule, "fn_bUnregisterControl");
				CTL_p_fn_bUnregisterControl = (CTL_td_p_fn_bUnregisterControl)Function;
			}
		}
	}

	return ( (CTL_p_fn_vRegisterControl != NULL) && (CTL_p_fn_bUnregisterControl != NULL) );
}
#endif //CTL_WITH_NO_TUT

//**************************************************************************
void CTL_fn_vSetMemoryForFloat(void *pvDest, 
															 long double ldfSourceValue, 
															 char cDataLength)
{
//ANNECY CB
#if 0
	ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
									"Seting memory to update a motor data",
									"CTL_fn_vSetMemoryForFloat(...)",
									E_ERROR_GRAVITY_INSTABLE,
									"The destination is not a valid pointer : memory has not been updated");
	ERROR_ASSERT( pvDest != NULL );
#endif
//END
	if ( pvDest != NULL )
	{
		switch ( cDataLength )
		{
			case sizeof(float)/*FLOAT*/ :
			{
				float fTempValue = (float)ldfSourceValue;
				memcpy(pvDest, &fTempValue, sizeof(float));
			}
				break;

	/*		case DOUBLE :
			{
				double dfTempValue = (double)ldfSourceValue;
				memcpy(pvDest, &dfTempValue, sizeof(double));
			}
				break;

			case LONG_DOUBLE :
			{
				long double ldfTempValue = (long double)ldfSourceValue;
				memcpy(pvDest, &ldfTempValue, sizeof(long double));
			}
				break;
	*/	
		}
	}
}

//**************************************************************************
void CTL_fn_vGetMemoryForFloat(void *pvSource, 
															 long double &r_ldfEditorValue,
															 char cDataLength)
{
//ANNECY CB
#if 0
	ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
									"Seting memory to update a motor data",
									"CTL_fn_vGetMemoryForFloat(...)",
									E_ERROR_GRAVITY_INSTABLE,
									"The source is not a valid pointer : memory has not been updated");
	ERROR_ASSERT( pvSource != NULL );
#endif
//END

	if ( pvSource != NULL )
	{
		switch ( cDataLength )
		{
			case sizeof(float) :
			{
				float fTempValue;
				memcpy(&fTempValue, pvSource, sizeof(float));	 
				r_ldfEditorValue = fTempValue;
			}
				break;

	/*		case DOUBLE :
			{
				double dfTempValue;
				memcpy(&dfTempValue, pvSource, sizeof(double));
				r_ldfEditorValue = dfTempValue;
			}
				break;

			case LONG_DOUBLE :
			{
				long double ldfTempValue;
				memcpy(&ldfTempValue, pvSource, sizeof(long double));
				r_ldfEditorValue = ldfTempValue;
			}
				break;
	*/	
			default:
				ERROR_ASSERT( FALSE );
		}
	}
}

