// CPACSpEd.cpp : implementation file
///////////////////////////////////////////
#include "StdAfx.h"

#include "WControls\CTL_WEdt.hpp"

#include "Controls\CTL_CtlI.hpp"
#include "Controls\CTL_CtlF.hpp"
#include "Controls\CTL_CtlV.hpp"
#include "Others\CTL_Pub.hpp"

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

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

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_Edit

BEGIN_MESSAGE_MAP(CTL_Editor_Edit, CEdit)
	//{{AFX_MSG_MAP(CTL_Editor_Edit)
	ON_WM_KEYUP()
	ON_WM_KILLFOCUS()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//************************************************************************************
CTL_Editor_Edit::CTL_Editor_Edit(long _lMin, 
																 long _lMax,
																 long _lAdditionnalStyles,
																 CTL_Editor_IntegerControl *_pclControl,
																 CWnd *_pclParentWnd,
																 long _lUserDefinedType /*= 0*/,
																 long _lUserDefinedCode /*= 0*/)

							: CTL_BaseWindowsControl(_pclControl,
																			 _pclParentWnd,
																			 _lUserDefinedType,
																			 _lUserDefinedCode)
{
	m_pri_lMin = _lMin;
	m_pri_lMax = _lMax;	
	m_pri_pclIntegerControl = _pclControl;
	m_pri_pclDecimalControl = NULL;
	m_pri_pclVectorControl  = NULL;	

	m_pri_tdeType = CTL_EDIT_TYPE__INTEGER;

	//Creates control
	CRect crBidonRect(0,0,0,0);
	DWORD lStyles = WS_CHILD | WS_BORDER | ES_CENTER | ES_MULTILINE | /*ES_NUMBER |*/ ES_WANTRETURN;
	lStyles |= _lAdditionnalStyles;

	if ( Create(lStyles, 
							crBidonRect, 
							_pclParentWnd, 
							CTL_fn_lGetNextAvailableIDForControl()) )
	{
		SetFont(_pclParentWnd->GetFont());
	}
}

//************************************************************************************
CTL_Editor_Edit::CTL_Editor_Edit(long double _ldMin, 
																 long double _ldMax,
																 long _lAdditionnalStyles,
																 CTL_Editor_DecimalControl *_pclControl,
																 CWnd *_pclParentWnd,
																 long _lUserDefinedType /*= 0*/,
																 long _lUserDefinedCode /*= 0*/)

							: CTL_BaseWindowsControl(_pclControl,
																			 _pclParentWnd,
																			 _lUserDefinedType,
																			 _lUserDefinedCode)
{
	m_pri_ldMin = _ldMin;
	m_pri_ldMax = _ldMax;
	m_pri_pclDecimalControl = _pclControl;
	m_pri_pclIntegerControl = NULL;
	m_pri_pclVectorControl  = NULL;	
	
	m_pri_tdeType = CTL_EDIT_TYPE__DECIMAL;

	//Creates control
	CRect crBidonRect(0,0,0,0);
	DWORD lStyles = WS_CHILD | WS_BORDER | ES_CENTER | ES_MULTILINE | ES_WANTRETURN;
	lStyles |= _lAdditionnalStyles;

	if ( Create(lStyles, 
							crBidonRect, 
							_pclParentWnd, 
							CTL_fn_lGetNextAvailableIDForControl()) )
	{
		SetFont(_pclParentWnd->GetFont());
	}
}

//************************************************************************************
CTL_Editor_Edit::CTL_Editor_Edit(long double _ldMin, 
																 long double _ldMax,
																 long _lAdditionnalStyles,
																 CTL_Editor_VectorControl *_pclControl,
																 CWnd *_pclParentWnd,
																 long _lUserDefinedType /*= 0*/,
																 long _lUserDefinedCode /*= 0*/)

							: CTL_BaseWindowsControl(_pclControl,
																			 _pclParentWnd,
																			 _lUserDefinedType,
																			 _lUserDefinedCode)
{
	m_pri_ldMin = _ldMin;
	m_pri_ldMax = _ldMax;
	m_pri_pclDecimalControl = NULL;
	m_pri_pclIntegerControl = NULL;
	m_pri_pclVectorControl  = _pclControl;	

	m_pri_tdeType = CTL_EDIT_TYPE__VECTOR;

	//Creates control
	CRect crBidonRect(0,0,0,0);
	DWORD lStyles = WS_CHILD | WS_BORDER | ES_CENTER | ES_MULTILINE | ES_WANTRETURN;
	lStyles |= _lAdditionnalStyles;

	if ( Create(lStyles, 
							crBidonRect, 
							_pclParentWnd, 
							CTL_fn_lGetNextAvailableIDForControl()) )
	{
		SetFont(_pclParentWnd->GetFont());
	}
}

//************************************************************************************
CTL_Editor_Edit::~CTL_Editor_Edit()
{

}

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_Edit message handlers

//************************************************************************************
void CTL_Editor_Edit::m_fn_vSetCurrentValue(long lNewValue)
{
	m_pri_lCurrent = lNewValue;
}

//************************************************************************************
void CTL_Editor_Edit::m_fn_vSetCurrentValue(long double ldNewValue)
{
	m_pri_ldCurrent = ldNewValue;
}

//************************************************************************************
void CTL_Editor_Edit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if ( nChar == 13 )	//'Enter' was pressed
	{
		//Gets the new value string
		CString csNewValue;
		GetWindowText(csNewValue);

		switch ( m_pri_tdeType )
		{
		//-------------------------------------------------
		case CTL_EDIT_TYPE__INTEGER:
		{
  		m_pri_lCurrent = atol(LPCTSTR(csNewValue));	

			if ( m_pri_lCurrent < m_pri_lMin )
				m_pri_lCurrent = m_pri_lMin;
			else if ( m_pri_lCurrent > m_pri_lMax )
			{
				if ((m_pri_lMax == ULONG_MAX) && (m_pri_lMin >=0))
				{
					if ((unsigned long)m_pri_lCurrent > (unsigned long) m_pri_lMax)
						m_pri_lCurrent = m_pri_lMax;
				}
				else
					m_pri_lCurrent = m_pri_lMax;
			}
			
 			csNewValue.Format("%ld", m_pri_lCurrent);
			SetWindowText(csNewValue);

			//Updates parent's slider and tree view
			m_pri_pclIntegerControl->m_fn_vUpdateSpin(m_pri_lCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}											  
		break;

		//-------------------------------------------------
		case CTL_EDIT_TYPE__DECIMAL:
		{
  		m_pri_ldCurrent = atof(LPCTSTR(csNewValue));	

			if ( m_pri_ldCurrent < m_pri_ldMin )
				m_pri_ldCurrent = m_pri_ldMin;
			else if ( m_pri_ldCurrent > m_pri_ldMax )
				m_pri_ldCurrent = m_pri_ldMax;
			
 			csNewValue.Format("%.3f", m_pri_ldCurrent);
			SetWindowText(csNewValue);

			//Updates parent's slider and tree view
			m_pri_pclDecimalControl->m_fn_vUpdateSpin(m_pri_ldCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}
		break;

		//-------------------------------------------------
		case CTL_EDIT_TYPE__VECTOR:
		{
  		m_pri_ldCurrent = atof(LPCTSTR(csNewValue));	

			if ( m_pri_ldCurrent < m_pri_ldMin )
				m_pri_ldCurrent = m_pri_ldMin;
			else if ( m_pri_ldCurrent > m_pri_ldMax )
				m_pri_ldCurrent = m_pri_ldMax;
			
 			csNewValue.Format("%.3f", m_pri_ldCurrent);
			SetWindowText(csNewValue);

			//Updates parent's slider and tree view
			m_pri_pclVectorControl->m_fn_vUpdateSpin(m_pri_ldCurrent, this, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}
		break;

		}
	}
	
	CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

//************************************************************************************
//To avoid default 'right clic' result in an Edit field
void CTL_Editor_Edit::OnRButtonUp(UINT nFlags, CPoint point) 
{	
	//CEdit::OnRButtonUp(nFlags, point);
}

//**********************************************************************************
// To update contents when the edit loses focus
void CTL_Editor_Edit::OnKillFocus( CWnd* pNewWnd )
{
	OnKeyUp(13,1,0);
	CEdit::OnKillFocus(pNewWnd);
//ROMTEAM Selection (Cristian Stegaru 24/03/98)
	mfn_vRefreshMotorData ();
//ENDROMTEAM Selection (Cristian Stegaru)
}

//**********************************************************************************
//For TUT registration
void CTL_Editor_Edit::m_pub_fn_vRegisterWindowsControl(CString _csAdditionnalInfo)
{
#ifndef CTL_WITH_NO_TUT
	m_pro_fn_vRegisterWindowsControl(_csAdditionnalInfo, TUT_e_TextEdit, "Edit", this);
#endif //CTL_WITH_NO_TUT
}

//ROMTEAM Selection (Cristian Stegaru 24/03/98)
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : mfn_vRefreshMotorData 
// Date   : 98-03
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description : I put here all the stuff from OnKeyUp, excepting the key check
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CTL_Editor_Edit::mfn_vRefreshMotorData ()
{
	//Gets the new value string
	CString csNewValue;
	GetWindowText(csNewValue);

	switch ( m_pri_tdeType )
	{
	//-------------------------------------------------
	case CTL_EDIT_TYPE__INTEGER:
	{
  	m_pri_lCurrent = atol(LPCTSTR(csNewValue));	

		if ( m_pri_lCurrent < m_pri_lMin )
			m_pri_lCurrent = m_pri_lMin;
		else if ( m_pri_lCurrent > m_pri_lMax )
		{
			if ((m_pri_lMax == ULONG_MAX) && (m_pri_lMin >=0))
			{
				if ((unsigned long)m_pri_lCurrent > (unsigned long) m_pri_lMax)
					m_pri_lCurrent = m_pri_lMax;
			}
			else
				m_pri_lCurrent = m_pri_lMax;
		}
		
 		csNewValue.Format("%ld", m_pri_lCurrent);
		SetWindowText(csNewValue);

		//Updates parent's slider and tree view
		m_pri_pclIntegerControl->m_fn_vUpdateSpin(m_pri_lCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
	}											  
	break;

	//-------------------------------------------------
	case CTL_EDIT_TYPE__DECIMAL:
	{
  	m_pri_ldCurrent = atof(LPCTSTR(csNewValue));	

		if ( m_pri_ldCurrent < m_pri_ldMin )
			m_pri_ldCurrent = m_pri_ldMin;
		else if ( m_pri_ldCurrent > m_pri_ldMax )
			m_pri_ldCurrent = m_pri_ldMax;
		
 		csNewValue.Format("%.3f", m_pri_ldCurrent);
		SetWindowText(csNewValue);

		//Updates parent's slider and tree view
		m_pri_pclDecimalControl->m_fn_vUpdateSpin(m_pri_ldCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
	}
	break;

	//-------------------------------------------------
	case CTL_EDIT_TYPE__VECTOR:
	{
  	m_pri_ldCurrent = atof(LPCTSTR(csNewValue));	

		if ( m_pri_ldCurrent < m_pri_ldMin )
			m_pri_ldCurrent = m_pri_ldMin;
		else if ( m_pri_ldCurrent > m_pri_ldMax )
			m_pri_ldCurrent = m_pri_ldMax;
		
 		csNewValue.Format("%.3f", m_pri_ldCurrent);
		SetWindowText(csNewValue);

		//Updates parent's slider and tree view
		m_pri_pclVectorControl->m_fn_vUpdateSpin(m_pri_ldCurrent, this, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
	}
	break;

	}
}
//ENDROMTEAM Selection (Cristian Stegaru)
