// Implementation file for the definition of an Action / States characteristic
///////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

//ANNECY CHRISTOPHE MODIFICATIONS
#define D_ZdxStuff_StructureDefine
//ANNECY END CHRISTOPHE MODIFICATIONS

#include "CPA_CtlS.hpp"

#include "CPA_DatS.hpp"
#include "EDACCnst.hpp"

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

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

//**************************************************************************
//Constructor / Destructor
CPA_Editor_StateControl::CPA_Editor_StateControl(BOOL _bReadOnly,
																								 CTL_Editor_ControlList *_pclParentList,
																								 CString _csControlName,
											 													BOOL _bAcceptsToTakeNameOfData, //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__USE_USER_DEFINED_TYPE,
																						_bReadOnly, 
																						CTL_SPACING_TYPE__MULTI_LINE,
																						FALSE,
																						_pclParentList,
																						_csControlName,
																						_bAcceptsToTakeNameOfData, //ANNECY BBB
//Stefan Dumitrean 20-07-98 ( OAC buttons )
																						ucInitialCurrentPair
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																						)
{
	m_pclEnumDescriptorForActions = NULL;
	m_pclEnumDescriptorForStates = NULL;

	m_pclComboBoxForActions = NULL;
	m_pclStaticForActions = NULL;
	m_pclStaticForStatesName = NULL;
	m_pclStaticForStates = NULL;
	m_pclCheckBoxForRandom = NULL;
}

//**************************************************************************
CPA_Editor_StateControl::~CPA_Editor_StateControl()
{
	delete m_pclComboBoxForActions;
	delete m_pclStaticForActions;
	delete m_pclStaticForStatesName;
	delete m_pclStaticForStates;
	delete m_pclCheckBoxForRandom;
}

//Member functions

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

	m_pclParentWnd = pclParentWnd;

	m_pro_fn_bCreateBaseControls(	C_STATE_NAME_PERCENT_WIDTH,
																C_STATE_NAME_MIN_WIDTH,
																C_EDIT_HEIGHT);

	//Creates the static control (Read only)
	m_pclStaticForActions = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0, NULL, m_pclParentWnd);
	m_pclStaticForActions->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Action Value");

	CTL_Editor_BaseControl *pclBC;
	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclStaticForActions,
																																m_pclStaticForActions,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																C_STATE_COMBO_BOX_PERCENT_WIDTH,
																																C_STATE_COMBO_BOX_MIN_WIDTH,
																																C_EDIT_HEIGHT,
																																!m_fn_bIsAlwaysReadOnly());
	pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bStaticCanBeDisplayed);

	if ( !m_fn_bIsAlwaysReadOnly() )
	{
		//Creates the combo box
		m_pclComboBoxForActions = new CTL_Editor_ComboBox(CTL_COMBO_BOX_TYPE__STANDARD,
																											WS_TABSTOP,
																											this,
																											m_pclParentWnd);
		m_pclComboBoxForActions->m_pub_fn_vSetSelChanged_CallBack(s_m_fn_vComboBoxSelChanged);
		m_pclComboBoxForActions->m_pub_fn_vSetDropDown_CallBack(s_m_fn_vComboBoxDroppedDown);
		m_pclComboBoxForActions->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Action");

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclComboBoxForActions,
																																	m_pclComboBoxForActions,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																	C_STATE_COMBO_BOX_PERCENT_WIDTH,
																																	C_STATE_COMBO_BOX_MIN_WIDTH,
																																	C_STATE_COMBO_BOX_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bComboBoxCanBeDisplayed);
	}

	//Creates a static control for the State declaration
	m_pclStaticForStatesName = new CTL_Editor_Static("State", CTL_STATIC_TYPE__NEUTRAL_FIELD, 0, NULL, m_pclParentWnd);
	m_pclStaticForStatesName->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - State Header");

	m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclStaticForStatesName,
																												m_pclStaticForStatesName,
																												this,
																												CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																												C_STATE_STATIC_NAME_PERCENT_WIDTH,
																												0,		//no min. size for static name
																												C_EDIT_HEIGHT,
																												FALSE,
																												TRUE);

	//Creates a static control for the State
	m_pclStaticForStates = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0 , NULL, m_pclParentWnd);
	m_pclStaticForStates->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - State Value");

	m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclStaticForStates,
																												m_pclStaticForStates,
																												this,
																												CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																												C_STATE_STATIC_PERCENT_WIDTH,
																												C_STATE_STATIC_MIN_WIDTH,
																												C_EDIT_HEIGHT);

	//Creates a Check Box for Random initial States
	m_pclCheckBoxForRandom = new CTL_Editor_CheckBox(CTL_CHECK_BOX_TYPE__NORMAL, 
																									 WS_TABSTOP,
																									 this,
																									 m_pclParentWnd,
																									 0, 0, 
																									 "Normal", "Random");
	m_pclCheckBoxForRandom->m_pub_fn_vSetHasBeenClicked_CallBack(s_m_fn_vCheckBoxHasBeenClicked);
	m_pclCheckBoxForRandom->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Random State");

	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclCheckBoxForRandom,
																																m_pclCheckBoxForRandom,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																C_STATE_BUTTON_PERCENT_WIDTH,
																																C_STATE_BUTTON_MIN_WIDTH,
																																C_STATE_BUTTON_HEIGHT);
	pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bButtonCanBeDisplayed);

	m_bControlCreated = bReturnValue;

	return bReturnValue;
}

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

//************************************************************************
void CPA_Editor_StateControl::m_fn_vUpdateCurrentValue()
{
	//Gets the new current value
	if ( m_pclComboBoxForActions != NULL )
	{
		short wIndex = m_pclComboBoxForActions->GetCurSel();	
		if ( wIndex != CB_ERR )
		{
			CTL_Editor_EnumElement * pSelect = (CTL_Editor_EnumElement *) m_pclComboBoxForActions->GetItemDataPtr(wIndex);
			
			if ((pSelect->m_pub_fn_lGetValue() !=  0) || (m_pclComboBoxForActions->GetCount() == 1))
				((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForActions = pSelect;
		}

		//Updates State edited value
		//Sets state to Action's default state
		((CPA_Editor_StateData *)m_pclData)->m_fn_bSetCurrentValueForStateAccordingToAction();
	}

	m_fn_vUpdateParent(CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
}

//************************************************************************
void CPA_Editor_StateControl::m_fn_vUpdate(CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																					 long _lUserDefinedReason /*= 0*/)
{
	//Re-constructs the list of entries in ComboBox
	m_pclEnumDescriptorForActions = ((CPA_Editor_StateData *)m_pclData)->m_fn_pclGetEnumDescriptorForActions();
	m_pclEnumDescriptorForStates = ((CPA_Editor_StateData *)m_pclData)->m_fn_pclGetEnumDescriptorForStates();

	if ( !m_fn_bIsAlwaysReadOnly() )
	{
		//Empties the ComboBox
		m_pclComboBoxForActions->ResetContent();
		
		//Fills the combo box
		short wIndex;
		POSITION pos = m_pclEnumDescriptorForActions->GetHeadPosition();
		CTL_Editor_EnumElement *pclElement;
		while ( pos != NULL )
		{
			pclElement = m_pclEnumDescriptorForActions->GetNext(pos);
			wIndex = m_pclComboBoxForActions->AddString(pclElement->m_pub_fn_csGetElementName());
			m_pclComboBoxForActions->SetItemDataPtr(wIndex, (void *)(pclElement));
		}
	}

	//Read/Write
	//Updates the combo box (Action)
	CTL_Editor_EnumElement *pclSearchedElement = ((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForActions;
	CTL_Editor_EnumElement *pclCurrentComboValue;
	BOOL bFound = FALSE;
	if ( m_pclComboBoxForActions != NULL )
	{
		short wIndex = m_pclComboBoxForActions->GetCurSel();	
		for ( wIndex = 0; (wIndex < m_pclComboBoxForActions->GetCount()) && (!bFound); wIndex ++ )
		{
			pclCurrentComboValue = (CTL_Editor_EnumElement *)(m_pclComboBoxForActions->GetItemDataPtr(wIndex));
			bFound = ( pclSearchedElement == pclCurrentComboValue );
		}

		if ( bFound )
			m_pclComboBoxForActions->SetCurSel(--wIndex);
	}
	
	//Read Only
	if	(		( m_pclStaticForActions != NULL )
			&&	( ((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForActions != NULL )
			)
		m_pclStaticForActions->m_fn_vSetTextToDisplay(((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForActions->m_pub_fn_csGetElementName(), TRUE);

	//Sets text to Display in State static
	if	(		( m_pclStaticForStates != NULL )
			&&	( ((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForStates != NULL )
			)
		m_pclStaticForStates->m_fn_vSetTextToDisplay(((CPA_Editor_StateData *)m_pclData)->m_pclCurrentValueForStates->m_pub_fn_csGetElementName(), TRUE);

	//Button (Random initial State)
	m_pclCheckBoxForRandom->SetCheck(FALSE/*TEMPORAIRE*/);

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

//************************************************************************
//Function called to update parent window in case of values changes.
void CPA_Editor_StateControl::m_fn_vUpdateParent(CTL_tdeUpdateReason _eReason,
																								 long _lUserDefinedReason /*= 0*/)
{
	//Updates motor's data
	m_pclData->m_fn_vUpdateMotorData(_eReason, _lUserDefinedReason);
	m_fn_vUpdate(_eReason, _lUserDefinedReason);

	//Updates parent if necessary (Watch a.s.o.)
	CTL_Editor_Control::m_fn_vUpdateParent(_eReason, _lUserDefinedReason);
}

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

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

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

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

//************************************************************************
BOOL CPA_Editor_StateControl::s_m_fn_bButtonCanBeDisplayed(class CTL_Editor_BaseControl *_pclSenderBaseControl)
{
	CPA_Editor_StateData *pclData = (CPA_Editor_StateData *)_pclSenderBaseControl->m_pub_fn_pclGetParentControl()->m_fn_pclGetEditedData();
	ERROR_ASSERT( pclData != NULL );
	
	return ( pclData->m_pub_fn_bIsTheInitialState() );
}

//************************************************************************
void CPA_Editor_StateControl::s_m_fn_vCheckBoxHasBeenClicked(class CTL_Editor_CheckBox *_pclSenderCheckBox,
																														 class CTL_Editor_Control *_pclParentControl,
																														 enum CTL_eCarCheckBoxType _tdeType,
																														 long _lUserDefinedType,
																														 long _lUserDefinedCode)
{
	ERROR_ASSERT( _pclParentControl != NULL );
	CPA_Editor_StateData *pclData = (CPA_Editor_StateData *)_pclParentControl->m_fn_pclGetEditedData();
	ERROR_ASSERT( pclData != NULL );

	//Reads buttons state
	BOOL bCheck = _pclSenderCheckBox->GetCheck();

	//Updates motor field

	//Calls Motor function

}

//************************************************************************
//When selection has been changed in Actions Combo box
void CPA_Editor_StateControl:: s_m_fn_vComboBoxSelChanged(class CTL_Editor_ComboBox *_pclSenderComboBox,
																													class CTL_Editor_Control *_pclParentControl,
																													enum CTL_eComboBoxType _tdeType,
																													long _lUserDefinedType,
																													long _lUserDefinedCode)
{
	ERROR_ASSERT( _pclParentControl != NULL );

	//Re-builds the Enum Descriptor
	CPA_Editor_StateData *pclData = (CPA_Editor_StateData *)_pclParentControl->m_fn_pclGetEditedData();
	ERROR_ASSERT( pclData != NULL );
	if ( pclData->m_pub_fn_bIsTheInitialState() )
	{

	}
	else
	{

	}

	((CPA_Editor_StateControl *)_pclParentControl)->m_fn_vUpdateCurrentValue();
}

//************************************************************************
//When selection has been changed in Actions Combo box
void CPA_Editor_StateControl:: s_m_fn_vComboBoxDroppedDown(class CTL_Editor_ComboBox *_pclSenderComboBox,
																													 class CTL_Editor_Control *_pclParentControl,
																													 enum CTL_eComboBoxType _tdeType,
																													 long _lUserDefinedType,
																													 long _lUserDefinedCode)
{
/*	ERROR_ASSERT( _pclParentControl != NULL );

	//Re-builds the Enum Descriptor
	CPA_Editor_StateData *pclData = (CPA_Editor_StateData *)_pclParentControl->m_fn_pclGetEditedData();
	ERROR_ASSERT( pclData != NULL );
	pclData->m_fn_vConstructEnumDescriptors();
*/
}


