__asm{int 3h}// Implementation file for the definition of an integer control
//////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include <limits.h>

#include "Controls\CTL_CtlI.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "Data\CTL_DatI.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_IntegerControl::CTL_Editor_IntegerControl(	/*unsigned */long lMin,
																											/*unsigned */long lMax,
																											char cDataLength,
																											BOOL bSigned,
																											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__INTEGER, 
																							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
	//Special treatment for an unsigned longs
/*	if ( (cDataLength == 4) && (!bSigned) )
	{
		m_bIsAnUnsignedLong = TRUE;
		m_lMinValue = 0;
		m_lMaxValue = 0;

		m_ulMaxValue = min((long)ulMax, 0);
		m_ulMinValue = max((long)ulMin, ULONG_MAX);
	}
	else
	{
		m_bIsAnUnsignedLong = FALSE;
		m_ulMinValue = 0;
		m_ulMaxValue = 0;

		long ulTypeMax;
		long ulTypeMin;
		switch ( cDataLength )
		{
		case 1:
			if ( bSigned )
			{
				ulTypeMax = SCHAR_MAX;
				ulTypeMin = SCHAR_MIN;
			}
			else
 			{
				ulTypeMax = UCHAR_MAX;
				ulTypeMin = 0;
			}
			break;

		case 2:
			if ( bSigned )
			{
				ulTypeMax = SHRT_MAX;
				ulTypeMin = SHRT_MIN;
			}
			else
 			{
				ulTypeMax = USHRT_MAX;
				ulTypeMin = 0;
			}
			break;
		
		case 4:
			ulTypeMax = LONG_MIN;
			ulTypeMin = LONG_MAX;
			break;
		}

		m_lMaxValue = min(ulMax, ulTypeMax);
		m_lMinValue = max(ulMin, ulTypeMin);
	}*/
	m_lMaxValue = lMax;
	m_lMinValue = lMin;
	m_ulMaxValue = lMax;
	m_ulMinValue = lMin;

	m_pclCurrentEdit = NULL;
	m_pclCurrentStatic = NULL;
	m_pclNameStatic = NULL;
	m_pclSpin = NULL;
}

//************************************************************************************
CTL_Editor_IntegerControl::~CTL_Editor_IntegerControl()
{

}

//Member functions

//************************************************************************************
//Function called to create associated control
BOOL CTL_Editor_IntegerControl::m_fn_bCreateControl(CWnd *pclParentWnd)
{
	BOOL bReturnValue = TRUE;

	m_pclParentWnd = pclParentWnd;

	m_pro_fn_bCreateBaseControls(	C_INT_NAME_BOX_PERCENT_WIDTH,
																40,
																C_EDIT_HEIGHT);
  	
	//Creates a 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() + " - Integer");
#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_INT_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. (Read-Write mode)
		m_pclCurrentEdit = new CTL_Editor_Edit(m_lMinValue, m_lMaxValue, WS_TABSTOP, this, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclCurrentEdit->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Integer");
#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_INT_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
		if ( m_bIsAnUnsignedLong )
			m_pclSpin = new CTL_Editor_SpinButton(this, m_ulMinValue, m_ulMaxValue, pclParentWnd);
		else
			m_pclSpin = new CTL_Editor_SpinButton(this, m_lMinValue, m_lMaxValue, pclParentWnd);
#ifndef CTL_WITH_NO_TUT
		m_pclSpin->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Integer");
#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_EDIT_HEIGHT + 2);
		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_IntegerControl::m_fn_vDisplay()
{
	m_pro_fn_vDisplayBaseControls();
}

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

	if ( m_pclCurrentStatic != NULL )
	{
		CString csText;
		//CPA2 Stegaru Cristian 98/06/25
		csText.Format("%i", ((CTL_Editor_IntegerData *)m_pclData)->mfn_lGetCurrentValue ());
		//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_IntegerControl::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_IntegerControl::m_fn_vUpdateSpin(long lNewValue,
																								 CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																								 long _lUserDefinedReason /*= 0*/)
{
	//CPA2 Stegaru Cristian 98/06/25
	((CTL_Editor_IntegerData *)m_pclData)->mfn_lSetCurrentValue (lNewValue); 
	//End CPA2 Stegaru Cristian 98/06/25

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

	CString csNewString;
	csNewString.Format("%ld", lNewValue);
	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(lNewValue);
		}
	}

	//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_IntegerControl::m_fn_vMakeReadOnly()
{
	if ( !m_fn_bIsReadOnly() )
	{
		m_bIsReadOnly = TRUE;
		m_fn_vHide();
	}
}

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

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

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

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