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

#include "Controls\CTL_CtlV.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "Data\CTL_DatV.hpp"
#include "Others\CTL_Pri.hpp"
#include "WControls\CTL_WSpn.hpp"
#include "WControls\CTL_WEdt.hpp"
#include "WControls\CTL_WSta.hpp"
#include "WControls\CTL_WCkB.hpp"
#include "Controls\CTL_Cnst.hpp"

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

//Constructor / Destructor
//************************************************************************************
CTL_Editor_VectorControl::CTL_Editor_VectorControl(long double ldMin,
																									 long double ldMax,
																									 char cDataLength,
																									 BOOL bReadOnly,
																									 CTL_Editor_ControlList *_pclParentList,
																									 CString _csControlName,
																									BOOL _bAcceptNameFromData, //ANNECY BBB
//Stefan Dumitrean 20-07-98 ( OAC buttons )
																									unsigned char ucInitialCurrentPair
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																									 )
												: CTL_Editor_Control(CTL_DATA_TYPE__VECTOR, 
																						 bReadOnly, 
																						 CTL_SPACING_TYPE__MULTI_LINE,
																						 FALSE,
																						 _pclParentList,
																						 _csControlName,
																						 _bAcceptNameFromData, //ANNECY BBB
//Stefan Dumitrean 20-07-98 ( OAC buttons )
																						 ucInitialCurrentPair
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																						 )
{
	m_ldMaxValue = ldMax;
	m_ldMinValue = ldMin;
	
	m_pclCurrentEditX = NULL;
	m_pclCurrentEditY = NULL;
	m_pclCurrentEditZ = NULL;
	m_pclCurrentStaticX = NULL;
	m_pclCurrentStaticY = NULL;
	m_pclCurrentStaticZ = NULL;
	m_pclSpinX = NULL;
	m_pclSpinY = NULL;
	m_pclSpinZ = NULL;
	m_pclNameStatic = NULL;
}

//************************************************************************************
CTL_Editor_VectorControl::~CTL_Editor_VectorControl()
{

}

//Member functions

//************************************************************************************
//Function called to create associated control
BOOL CTL_Editor_VectorControl::m_fn_bCreateControl(CWnd *pclParentWnd)
{
	BOOL bReturnValue = TRUE;
	CRect crBidonRect(0,0,10,10);

	m_pclParentWnd = pclParentWnd;

	m_pro_fn_bCreateBaseControls(100,
															 40,
															 C_EDIT_HEIGHT);

	//Creates static boxes for the current value of the charac. (Read Only mode)
	m_pclCurrentStaticX = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0, NULL, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
	m_pclCurrentStaticX->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : X");
#endif //CTL_WITH_NO_TUT

	CTL_Editor_BaseControl *pclBC;
	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentStaticX,
																																m_pclCurrentStaticX,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																33,
																																C_DECIMAL_CURRENT_VALUE_MIN_WIDTH,
																																C_EDIT_HEIGHT,
																																!m_fn_bIsAlwaysReadOnly(),
																																TRUE);
	pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bStaticMustBeDisplayed);
	
	if ( !m_fn_bIsAlwaysReadOnly() )
	{
		//Creates Edit boxes for the current value of the charac.
		m_pclCurrentEditX = new CTL_Editor_Edit(m_ldMinValue, m_ldMaxValue, WS_TABSTOP, this, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclCurrentEditX->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : X");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentEditX,
																																	m_pclCurrentEditX,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																	28,
																																	C_DECIMAL_CURRENT_VALUE_MIN_WIDTH,
																																	C_EDIT_HEIGHT,
																																	FALSE,
																																	TRUE); //Forces to go to the next line
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bEditMustBeDisplayed);

		//Creates a spin button for the current value of the charac.
//ANNECY CB
#if 0
		m_pclSpinX = new CTL_Editor_SpinButton(this, m_ldMinValue, m_ldMaxValue, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclSpinX->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : X");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(NULL,
																																	m_pclSpinX,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__ALWAYS_FIXED,
																																	0,
																																	C_SPIN_WIDTH,
																																	C_SPIN_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bSpinMustBeDisplayed);
#endif
//ENDANNECY CB
	}

	m_pclCurrentStaticY = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0, NULL, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
	m_pclCurrentStaticY->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Y");
#endif //CTL_WITH_NO_TUT

	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentStaticY,
																																m_pclCurrentStaticY,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																33,
																																C_DECIMAL_CURRENT_VALUE_MIN_WIDTH,
																																C_EDIT_HEIGHT,
																																!m_fn_bIsAlwaysReadOnly());
	pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bStaticMustBeDisplayed);
	
	if ( !m_fn_bIsAlwaysReadOnly() )
	{
		//Creates Edit boxes for the current value of the charac.
		m_pclCurrentEditY = new CTL_Editor_Edit(m_ldMinValue, m_ldMaxValue, WS_TABSTOP, this, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclCurrentEditY->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Y");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentEditY,
																																	m_pclCurrentEditY,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																	28,
																																	C_DECIMAL_CURRENT_VALUE_MIN_WIDTH, 
																																	C_EDIT_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bEditMustBeDisplayed);

		//Creates a spin button for the current value of the charac.
//ANNECY CB
#if 0
		m_pclSpinY = new CTL_Editor_SpinButton(this, m_ldMinValue, m_ldMaxValue, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclSpinY->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Y");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(NULL,
																																	m_pclSpinY,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__ALWAYS_FIXED,
																																	0,
																																	C_SPIN_WIDTH,
																																	C_SPIN_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bSpinMustBeDisplayed);
#endif
//ENDANNECY CB
	}

	m_pclCurrentStaticZ = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0 , NULL, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
	m_pclCurrentStaticZ->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Z");
#endif //CTL_WITH_NO_TUT

	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentStaticZ,
																																m_pclCurrentStaticZ,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																33,
																																C_DECIMAL_CURRENT_VALUE_MIN_WIDTH,
																																C_EDIT_HEIGHT,
																																!m_fn_bIsAlwaysReadOnly());
	pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bStaticMustBeDisplayed);
	
	if ( !m_fn_bIsAlwaysReadOnly() )
	{
		//Creates Edit boxes for the current value of the charac.
		m_pclCurrentEditZ = new CTL_Editor_Edit(m_ldMinValue, m_ldMaxValue, WS_TABSTOP, this, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclCurrentEditZ->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Z");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentEditZ,
																																	m_pclCurrentEditZ,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																	28,
																																	C_DECIMAL_CURRENT_VALUE_MIN_WIDTH, 
																																	C_EDIT_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bEditMustBeDisplayed);

		//Creates a spin button for the current value of the charac.
//ANNECY CB
#if 0
		m_pclSpinZ = new CTL_Editor_SpinButton(this, m_ldMinValue, m_ldMaxValue, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclSpinZ->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Vector : Z");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(NULL,
																																	m_pclSpinZ,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__ALWAYS_FIXED,
																																	0,
																																	C_SPIN_WIDTH, 
																																	C_SPIN_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bSpinMustBeDisplayed);
#endif
//ENDANNECY CB
	}

	m_bControlCreated = bReturnValue;

	return bReturnValue;
}

//************************************************************************************
//Function called to display the associated control(s) of the char.
void CTL_Editor_VectorControl::m_fn_vDisplay()
{
	m_pro_fn_vDisplayBaseControls();
}

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_VectorControl::m_fn_vUpdate(CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																						long _lUserDefinedReason /*= 0*/)
{
	//CPA2 Stegaru Cristian 98/06/26
	m_fn_vUpdateSpin(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueX (), eModifType_X);
	m_fn_vUpdateSpin(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueY (), eModifType_Y);
	m_fn_vUpdateSpin(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueZ (), eModifType_Z);
	m_fn_vUpdateEdit(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueX (), eModifType_X);
	m_fn_vUpdateEdit(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueY (), eModifType_Y);
	m_fn_vUpdateEdit(((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueZ (), eModifType_Z);
	//End CPA2 Stegaru Cristian 98/06/26

	if ( m_pclCurrentStaticX != NULL )
	{
		CString csText;
		//CPA2 Stegaru Cristian 98/06/26
		csText.Format("%g", ((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueX ());
		//End CPA2 Stegaru Cristian 98/06/26
		m_pclCurrentStaticX->m_fn_vSetTextToDisplay(csText);
	}
	if ( m_pclCurrentStaticY != NULL )
	{
		CString csText;
		//CPA2 Stegaru Cristian 98/06/26
		csText.Format("%g", ((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueY ());
		//End CPA2 Stegaru Cristian 98/06/26
		m_pclCurrentStaticY->m_fn_vSetTextToDisplay(csText);
	}
	if ( m_pclCurrentStaticZ != NULL )
	{
		CString csText;
		//CPA2 Stegaru Cristian 98/06/26
		csText.Format("%g", ((CTL_Editor_VectorData *)m_pclData)->mfn_ldGetCurrentValueZ ());
		//End CPA2 Stegaru Cristian 98/06/26
		m_pclCurrentStaticZ->m_fn_vSetTextToDisplay(csText);
	}

	//CallBack
	m_pclData->m_fn_vDataHasBeenChanged(_eReason, _lUserDefinedReason);
}									   

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_VectorControl::m_fn_vUpdateParent(CTL_tdeUpdateReason eReason,
																									long _lUserDefinedReason /*= 0*/)
{
	CTL_Editor_Control::m_fn_vUpdateParent(eReason, _lUserDefinedReason);
}

//************************************************************************************
//Function called to update the slider when the edit changes
void CTL_Editor_VectorControl::m_fn_vUpdateSpin(long double ldNewValue,
																								CTL_Editor_Edit *_pclEditSender,
																								CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								long _lUserDefinedReason /*= 0*/)
{
	if ( _pclEditSender == m_pclCurrentEditX )
		m_fn_vUpdateSpin(ldNewValue, eModifType_X, _eReason, _lUserDefinedReason);
	else if ( _pclEditSender == m_pclCurrentEditY )
		m_fn_vUpdateSpin(ldNewValue, eModifType_Y, _eReason, _lUserDefinedReason);
	else if ( _pclEditSender == m_pclCurrentEditZ )
		m_fn_vUpdateSpin(ldNewValue, eModifType_Z, _eReason, _lUserDefinedReason);
}

//************************************************************************************
//Function called to update the slider when the edit changes
void CTL_Editor_VectorControl::m_fn_vUpdateSpin(long double ldNewValue,
																								m_eModifType _eModifType,
																								CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								long _lUserDefinedReason /*= 0*/)
{
	if ( _eModifType == eModifType_X )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueX (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26
	else if ( _eModifType == eModifType_Y )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueY (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26
	else if ( _eModifType == eModifType_Z )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueZ (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26

	//Updates the spin if it exists
//ANNECY CB
#if 0
	if ( !m_fn_bIsReadOnly() )
	{
		if ( _eModifType == eModifType_X )
		{
			if ( m_pclSpinX != NULL )
				m_pclSpinX->m_fn_vSetCurrentPos(ldNewValue);
		}
		else if ( _eModifType == eModifType_Y )
		{
			if ( m_pclSpinY != NULL )
				m_pclSpinY->m_fn_vSetCurrentPos(ldNewValue);
		}
		else if ( _eModifType == eModifType_Z )
		{
			if ( m_pclSpinZ != NULL )
				m_pclSpinZ->m_fn_vSetCurrentPos(ldNewValue);
		}
	}
#endif
//ENDANNECY CB

	//Updates parent's datas if necessary
	m_fn_vUpdateParent(_eReason, _lUserDefinedReason);

	//Updates motor's data in case of change
	m_pclData->m_fn_vUpdateMotorData(_eReason, _lUserDefinedReason);
}

//************************************************************************************
//Function called to update the edit when the slider changes
void CTL_Editor_VectorControl::m_fn_vUpdateEdit(long double ldNewValue,
																								CTL_Editor_SpinButton *_pclSpinSender,
																								CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								long _lUserDefinedReason /*= 0*/)
{
	if ( _pclSpinSender == m_pclSpinX )
		m_fn_vUpdateEdit(ldNewValue, eModifType_X, _eReason, _lUserDefinedReason);
	else if ( _pclSpinSender == m_pclSpinY )
		m_fn_vUpdateEdit(ldNewValue, eModifType_Y, _eReason, _lUserDefinedReason);
	else if ( _pclSpinSender == m_pclSpinZ )
		m_fn_vUpdateEdit(ldNewValue, eModifType_Z, _eReason, _lUserDefinedReason);
}

//************************************************************************************
//Function called to update the edit when the slider changes
void CTL_Editor_VectorControl::m_fn_vUpdateEdit(long double ldNewValue,
																								m_eModifType _eModifType,
																								CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								long _lUserDefinedReason /*= 0*/)
{
	if ( _eModifType == eModifType_X )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueX (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26
	else if ( _eModifType == eModifType_Y )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueY (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26
	else if ( _eModifType == eModifType_Z )
		//CPA2 Stegaru Cristian 98/06/26
		((CTL_Editor_VectorData *)m_pclData)->mfn_ldSetCurrentValueZ (ldNewValue); 
		//End CPA2 Stegaru Cristian 98/06/26

	CString csNewString;
	csNewString.Format("%g", ldNewValue);
	if ( m_fn_bIsReadOnly() )
	{
		if ( _eModifType == eModifType_X )
		{
  		if ( m_pclCurrentStaticX != NULL )
				m_pclCurrentStaticX->m_fn_vSetTextToDisplay(csNewString);
		}
		else if ( _eModifType == eModifType_Y )
		{
  		if ( m_pclCurrentStaticY != NULL )
				m_pclCurrentStaticY->m_fn_vSetTextToDisplay(csNewString);
		}
		else if ( _eModifType == eModifType_Z )
		{
  		if ( m_pclCurrentStaticZ != NULL )
				m_pclCurrentStaticZ->m_fn_vSetTextToDisplay(csNewString);
		}
	}
	else
	{
		if ( _eModifType == eModifType_X )
		{
			if ( m_pclCurrentEditX != NULL )
			{
				m_pclCurrentEditX->SetWindowText(csNewString);
				m_pclCurrentEditX->m_fn_vSetCurrentValue(ldNewValue);
			}
		}
		else if ( _eModifType == eModifType_Y )
		{
			if ( m_pclCurrentEditY != NULL )
			{
				m_pclCurrentEditY->SetWindowText(csNewString);
				m_pclCurrentEditY->m_fn_vSetCurrentValue(ldNewValue);
			}
		}
		else if ( _eModifType == eModifType_Z )
		{
			if ( m_pclCurrentEditZ != NULL )
			{
				m_pclCurrentEditZ->SetWindowText(csNewString);
				m_pclCurrentEditZ->m_fn_vSetCurrentValue(ldNewValue);
			}
		}
	}

	//Updates parent's datas if necessary
	m_fn_vUpdateParent(_eReason, _lUserDefinedReason);

	//Updates motor's data in case of change
	m_pclData->m_fn_vUpdateMotorData(_eReason, _lUserDefinedReason);
}

//************************************************************************
//Function called to make control Read Only
void CTL_Editor_VectorControl::m_fn_vMakeReadOnly()
{
	if ( !m_fn_bIsReadOnly() )
	{
		m_bIsReadOnly = TRUE;
		m_fn_vHide();
	}
}

//************************************************************************
//Function called to make control Read-Write
void CTL_Editor_VectorControl::m_fn_vMakeReadWrite()
{
	if ( !m_fn_bIsAlwaysReadOnly() )
	{
  		if ( m_fn_bIsReadOnly() )
		{
			m_bIsReadOnly = FALSE;
			m_fn_vHide();
		}
	}
}


//************************************************************************
BOOL CTL_Editor_VectorControl::s_m_fn_bStaticMustBeDisplayed(class CTL_Editor_BaseControl *_pclSenderBaseControl)
{
	return (_pclSenderBaseControl->m_pub_fn_pclGetParentControl()->m_fn_bIsReadOnly());
}

//************************************************************************
BOOL CTL_Editor_VectorControl::s_m_fn_bEditMustBeDisplayed(class CTL_Editor_BaseControl *_pclSenderBaseControl)
{
	return (!_pclSenderBaseControl->m_pub_fn_pclGetParentControl()->m_fn_bIsReadOnly());
}

//************************************************************************
BOOL CTL_Editor_VectorControl::s_m_fn_bSpinMustBeDisplayed(class CTL_Editor_BaseControl *_pclSenderBaseControl)
{
	return (!_pclSenderBaseControl->m_pub_fn_pclGetParentControl()->m_fn_bIsReadOnly());
}

