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

//ANNECY CB
#if 0
#include "WControls\CTL_WSpn.hpp"

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

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

short CTL_g_uwScreenHeight = 0;

UDACCEL g_a_struc_Accel[C_ACCEL_NB] = 
{
	{ 1, 1 },
	{ 1, 2 },
	{ 1, 3 },
	{ 1, 4 },
	{ 1, 5 },
};

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

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_SpinButton

BEGIN_MESSAGE_MAP(CTL_Editor_SpinButton, CSpinButtonCtrl)
	//{{AFX_MSG_MAP(CTL_Editor_SpinButton)
	ON_NOTIFY_REFLECT(UDN_DELTAPOS, OnDeltapos)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//***************************************************************************
CTL_Editor_SpinButton::CTL_Editor_SpinButton(CTL_Editor_IntegerControl *_pclControl,
																						 long _lLower,	
																						 long _lUpper, 
																						 CWnd *_pclParentWnd)
									
									: CTL_BaseWindowsControl(_pclControl,
																					 _pclParentWnd)
{
	m_pclIntegerControl = _pclControl;
	m_pclDecimalControl = NULL;
	m_pclVectorControl = NULL;
	
	ERROR_ASSERT(_lLower < _lUpper);
	m_lLower = _lLower;
	m_lUpper = _lUpper;

	m_tdeType = EDCAR_SPIN_TYPE_INTEGER;

	//Creates control
	CRect crBidonRect(0,0,10,10);
	DWORD lStyles = WS_CHILD | UDS_ARROWKEYS | UDS_AUTOBUDDY;
	if ( Create(lStyles, 
				crBidonRect, 
				_pclParentWnd, 
				CTL_fn_lGetNextAvailableIDForControl()) )
	{
		SetAccel(C_ACCEL_NB, g_a_struc_Accel);
		SetRange(0, 100);
	}

	m_uwManualIntegerDelta = 1;
	m_bManualPosIsOn = FALSE;

	if ( CTL_g_uwScreenHeight == 0 )
	{
		//Gets screen's height
		HDC	  hdc;
		hdc = ::GetWindowDC(NULL);
		CTL_g_uwScreenHeight = GetDeviceCaps(hdc, VERTRES);
		::ReleaseDC(NULL,hdc);
	}
}

//***************************************************************************
CTL_Editor_SpinButton::CTL_Editor_SpinButton(CTL_Editor_DecimalControl *_pclControl,
																						 long double _ldLower,
																						 long double _ldUpper, 
																						 CWnd *_pclParentWnd)
									
									: CTL_BaseWindowsControl(_pclControl,
																					 _pclParentWnd)
{
	m_pclIntegerControl = NULL;
	m_pclDecimalControl = _pclControl;
	m_pclVectorControl = NULL;
	m_ldLower = _ldLower;
	m_ldUpper = _ldUpper;

	m_tdeType = EDCAR_SPIN_TYPE_DECIMAL;

	//Creates control
	CRect crBidonRect(0,0,10,10);
	DWORD lStyles = WS_CHILD | UDS_ARROWKEYS | UDS_AUTOBUDDY;
	if ( Create(lStyles, 
				crBidonRect, 
				_pclParentWnd, 
				CTL_fn_lGetNextAvailableIDForControl()) )
	{
		SetAccel(C_ACCEL_NB, g_a_struc_Accel);
		SetRange(0, 100);
	}
	//Computes divisor value
	char cCounter = C_SPIN_PRECISION;
	m_ldDivisor = 1.0;
	while (	cCounter != 0 )
	{
		m_ldDivisor *= 10.0;
		cCounter --;
	}

	m_fManualDecimalDelta = (float)0.1;
	m_bManualPosIsOn = FALSE;

	if ( CTL_g_uwScreenHeight == 0 )
	{
		//Gets screen's height
		HDC	  hdc;
		hdc = ::GetWindowDC(NULL);
		CTL_g_uwScreenHeight = GetDeviceCaps(hdc, VERTRES);
		::ReleaseDC(NULL,hdc);
	}
}

//***************************************************************************
CTL_Editor_SpinButton::CTL_Editor_SpinButton(CTL_Editor_VectorControl *_pclControl,
																						 long double _ldLower,
																						 long double _ldUpper, 
																						 CWnd *_pclParentWnd)
									
									: CTL_BaseWindowsControl(_pclControl,
																					 _pclParentWnd)
{
	m_pclIntegerControl = NULL;
	m_pclDecimalControl = NULL;
	m_pclVectorControl = _pclControl;

	m_ldLower = _ldLower;
	m_ldUpper = _ldUpper;

	m_tdeType = EDCAR_SPIN_TYPE_VECTOR;

	//Creates control
	CRect crBidonRect(0,0,10,10);
	DWORD lStyles = WS_CHILD | UDS_ARROWKEYS | UDS_AUTOBUDDY;
	if	( Create(	lStyles, 
								crBidonRect, 
								_pclParentWnd, 
								CTL_fn_lGetNextAvailableIDForControl())
			)
	{
		SetAccel(C_ACCEL_NB, g_a_struc_Accel);
		SetRange(0, 100);
	}

	//Computes divisor value
	char cCounter = C_SPIN_PRECISION;
	m_ldDivisor = 1.0;
	while (	cCounter != 0 )
	{
		m_ldDivisor *= 10.0;
		cCounter --;
	}

	m_fManualDecimalDelta = (float)0.1;
	m_bManualPosIsOn = FALSE;

	if ( CTL_g_uwScreenHeight == 0 )
	{
		//Gets screen's height
		HDC	  hdc;
		hdc = ::GetWindowDC(NULL);
		CTL_g_uwScreenHeight = GetDeviceCaps(hdc, VERTRES);
		::ReleaseDC(NULL,hdc);
	}
}

//***************************************************************************
CTL_Editor_SpinButton::~CTL_Editor_SpinButton()
{
}

/////////////////////////////////////////////////////////////////////////////
// CTL_Editor_SpinButton message handlers

//***************************************************************************
void CTL_Editor_SpinButton::OnDeltapos(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	switch ( m_tdeType )
	{
		//---------------------------------------------------------
		case EDCAR_SPIN_TYPE_INTEGER:
		{
			m_lCurrent += pNMUpDown->iDelta;

			if ( m_lCurrent > m_lUpper	)
				m_lCurrent = m_lUpper;
			else if ( m_lCurrent < m_lLower	)
				m_lCurrent = m_lLower;

			m_pclIntegerControl->m_fn_vUpdateEdit(m_lCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}
		break;

		//---------------------------------------------------------
		case EDCAR_SPIN_TYPE_DECIMAL:
		{
			m_ldCurrent += ((long double)(pNMUpDown->iDelta) / m_ldDivisor);

			if ( m_ldCurrent > m_ldUpper )
				m_ldCurrent = m_ldUpper;
			else if ( m_ldCurrent < m_ldLower )
				m_ldCurrent = m_ldLower;

			m_pclDecimalControl->m_fn_vUpdateEdit(m_ldCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}
		break;
		
		//---------------------------------------------------------
		case EDCAR_SPIN_TYPE_VECTOR:
		{
			m_ldCurrent += ((long double)(pNMUpDown->iDelta) / m_ldDivisor);

			if ( m_ldCurrent > m_ldUpper )
				m_ldCurrent = m_ldUpper;
			else if ( m_ldCurrent < m_ldLower )
				m_ldCurrent = m_ldLower;

			m_pclVectorControl->m_fn_vUpdateEdit(m_ldCurrent, this, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
		}
		break;
		
	}

	*pResult = 0;
}

//***************************************************************************
void CTL_Editor_SpinButton::m_fn_vSetCurrentPos(long lNewValue)
{
	m_lCurrent = lNewValue;
}

//***************************************************************************
void CTL_Editor_SpinButton::m_fn_vSetCurrentPos(long double ldNewValue)
{
	m_ldCurrent = ldNewValue;
}

//***************************************************************************
void CTL_Editor_SpinButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SetCapture();
	m_bManualPosIsOn = TRUE;

	ClientToScreen(&point);
	m_uwOldPosition = (unsigned short)point.y;
	
	CSpinButtonCtrl::OnRButtonDown(nFlags, point);
}

//***************************************************************************
void CTL_Editor_SpinButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	if ( m_bManualPosIsOn )
	{
		CPoint cpScreenPos = point;
		ClientToScreen(&cpScreenPos);
														 
		if ( cpScreenPos.y == 0 )
		{
			SetCursorPos(cpScreenPos.x, CTL_g_uwScreenHeight-2);
			cpScreenPos.y = m_uwOldPosition = CTL_g_uwScreenHeight-2;
			
		}

		if ( cpScreenPos.y == (CTL_g_uwScreenHeight-1) )
		{
			SetCursorPos(cpScreenPos.x, 1);
 			cpScreenPos.y = m_uwOldPosition = 1;
		}

		switch ( m_tdeType )
		{
			case EDCAR_SPIN_TYPE_INTEGER:
			{
				long lNewValue = (long)(m_lCurrent + (m_uwOldPosition - cpScreenPos.y) * m_uwManualIntegerDelta);
				if ( lNewValue > m_lUpper )
					lNewValue = m_lUpper;
				else if (  lNewValue < m_lLower )
					lNewValue = m_lLower;
				m_fn_vSetCurrentPos(lNewValue);
				m_pclIntegerControl->m_fn_vUpdateEdit(m_lCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
			}
			break;

			case EDCAR_SPIN_TYPE_DECIMAL:
			{
				long double ldNewValue = (long double)(m_ldCurrent + (m_uwOldPosition - cpScreenPos.y) * m_fManualDecimalDelta);
				if ( ldNewValue > m_ldUpper )
					ldNewValue = m_ldUpper;
				else if ( ldNewValue < m_ldLower )
					ldNewValue = m_ldLower;											
				m_fn_vSetCurrentPos(ldNewValue);
				m_pclDecimalControl->m_fn_vUpdateEdit(m_ldCurrent, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
			}
			break;

			case EDCAR_SPIN_TYPE_VECTOR:
			{
				long double ldNewValue = (long double)(m_ldCurrent + (m_uwOldPosition - cpScreenPos.y) * m_fManualDecimalDelta);
				if ( ldNewValue > m_ldUpper )
					ldNewValue = m_ldUpper;
				else if ( ldNewValue < m_ldLower )
					ldNewValue = m_ldLower;											
				m_fn_vSetCurrentPos(ldNewValue);
				m_pclVectorControl->m_fn_vUpdateEdit(m_ldCurrent, this, CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
			}
			break;
		}

		m_uwOldPosition = (unsigned short)cpScreenPos.y ;
	}

	CSpinButtonCtrl::OnMouseMove(nFlags, point);
}

//***************************************************************************
void CTL_Editor_SpinButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	ReleaseCapture();
	m_bManualPosIsOn = FALSE;

	CSpinButtonCtrl::OnRButtonUp(nFlags, point);
}

//**********************************************************************************
//For TUT registration
void CTL_Editor_SpinButton::m_pub_fn_vRegisterWindowsControl(CString _csAdditionnalInfo)
{
#ifndef CTL_WITH_NO_TUT
	m_pro_fn_vRegisterWindowsControl(_csAdditionnalInfo, TUT_e_Spin, "Spin Control", this);
#endif //CTL_WITH_NO_TUT
}

#endif
//ENDANNECY CB