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

#include "Controls\CTL_CtlF.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "Data\CTL_DatF.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_DecimalControl::CTL_Editor_DecimalControl(long double ldMin,
																										 long double ldMax,
																										 char cDataLength,
																										 BOOL bReadOnly,
																										 CTL_tdeControlSpacingType tdeSpacingType,
																										 BOOL bUserCanChangeAspect,
																										 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__DECIMAL, 
																						 bReadOnly, 
																						 tdeSpacingType,
																						 bUserCanChangeAspect,
																						 _pclParentList,
																						 _csControlName,
																						 _bAcceptNameFromData, //ANNECY BBB
//Stefan Dumitrean 20-07-98 ( OAC buttons )
																						 ucInitialCurrentPair
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																						 )
{
	//Computes min and max
/*	unsigned long lTypeMax;
	switch ( cDataLength )
	{
	case 1:
		lTypeMax = 255;
		break;
	case 2:
		lTypeMax = 65535;
		break;
	case 4:
		lTypeMax = 4294967295;
		break;
	}

	long lTypeMin = bSigned ? (-lTypeMax/2)-1 :  0;	 
	lTypeMax = bSigned ? lTypeMax/2 : lTypeMax;
								   
	m_lMaxValue = min(lMax, lTypeMax);
	m_lMinValue = max(lMin, lTypeMin);*/
	m_ldMaxValue = ldMax;
	m_ldMinValue = ldMin;
	
	m_pclCurrentEdit = NULL;
	m_pclCurrentStatic = NULL;
	m_pclNameStatic = NULL;
	m_pclSpin = NULL;
}

//************************************************************************************
CTL_Editor_DecimalControl::~CTL_Editor_DecimalControl()
{

}

//Member functions

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

	m_pclParentWnd = pclParentWnd;

	m_pro_fn_bCreateBaseControls(	C_INT_NAME_BOX_PERCENT_WIDTH,
																40,
																C_EDIT_HEIGHT);
  	
	//Creates an static box for the current value of the charac. (Read Only mode)
	m_pclCurrentStatic = new CTL_Editor_Static("",
																						 CTL_STATIC_TYPE__FIELD_VALUE,
																						 0,
																						 NULL,
																						 pclParentWnd);
#ifndef CTL_WITH_NO_TUT
	m_pclCurrentStatic->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Decimal");
#endif //CTL_WITH_NO_TUT

	CTL_Editor_BaseControl *pclBC;
	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentStatic,
																												m_pclCurrentStatic,
																												this,
																												CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																												C_INT_CURRENT_VALUE_PERCENT_WIDTH,
																												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 an edit box for the current value of the charac.
		m_pclCurrentEdit = new CTL_Editor_Edit(m_ldMinValue, m_ldMaxValue, WS_TABSTOP, this, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclCurrentEdit->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Decimal");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCurrentEdit,
																													m_pclCurrentEdit,
																													this,
																													CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																													C_INT_CURRENT_VALUE_PERCENT_WIDTH,
																													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_pclSpin = new CTL_Editor_SpinButton(this, m_ldMinValue, m_ldMaxValue, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclSpin->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Decimal");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(NULL,
																													m_pclSpin,
																													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
	}

	m_bControlCreated = bReturnValue;

	return bReturnValue;
}

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

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_DecimalControl::m_fn_vUpdate(CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																						 long _lUserDefinedReason /*= 0*/)
{
	//CPA2 Stegaru Cristian 98/06/25
	m_fn_vUpdateSpin(((CTL_Editor_DecimalData *)m_pclData)->mfn_ldGetCurrentValue ());
	m_fn_vUpdateEdit(((CTL_Editor_DecimalData *)m_pclData)->mfn_ldGetCurrentValue ());
	//End CPA2 Stegaru Cristian 98/06/25

	if ( m_pclCurrentStatic != NULL )
	{
		CString csText;
		//CPA2 Stegaru Cristian 98/06/25
		csText.Format("%g", ((CTL_Editor_DecimalData *)m_pclData)->mfn_ldGetCurrentValue ());
		//End CPA2 Stegaru Cristian 98/06/25
		m_pclCurrentStatic->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_DecimalControl::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_DecimalControl::m_fn_vUpdateSpin(long double ldNewValue,
																								 CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								 long _lUserDefinedReason /*= 0*/)
{
	//CPA2 Stegaru Cristian 98/06/25
	((CTL_Editor_DecimalData *)m_pclData)->mfn_ldSetCurrentValue (ldNewValue); 
	//End CPA2 Stegaru Cristian 98/06/25
	
//	if ( m_td_p_fn_vDataHasChangedCallBack != NULL )
//		m_td_p_fn_vDataHasChangedCallBack(m_pclData);

	//Updates the spin if it exists
//ANNECY CB
#if 0
	if ( !m_fn_bIsReadOnly() )
		if ( m_pclSpin )
			m_pclSpin->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_DecimalControl::m_fn_vUpdateEdit(long double ldNewValue,
																								 CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								 long _lUserDefinedReason /*= 0*/)
{
	//CPA2 Stegaru Cristian 98/06/25
	((CTL_Editor_DecimalData *)m_pclData)->mfn_ldSetCurrentValue (ldNewValue); 
	//End CPA2 Stegaru Cristian 98/06/25

//	if ( m_td_p_fn_vDataHasChangedCallBack != NULL )
//		m_td_p_fn_vDataHasChangedCallBack(m_pclData);

	CString csNewString;
	csNewString.Format("%g", ldNewValue);
	if ( m_fn_bIsReadOnly() )
	{
  		if ( m_pclCurrentStatic )
			m_pclCurrentStatic->m_fn_vSetTextToDisplay(csNewString);
	}
	else
	{
		if ( m_pclCurrentEdit )
		{
			m_pclCurrentEdit->SetWindowText(csNewString);
			m_pclCurrentEdit->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_DecimalControl::m_fn_vMakeReadOnly()
{
	if ( !m_fn_bIsReadOnly() )
	{
		m_bIsReadOnly = TRUE;
		m_fn_vHide();
	}
}

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

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

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

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