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

#include "Controls\CTL_CtlE.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "Data\CTL_DatE.hpp"
#include "WControls\CTL_WCBx.hpp"
#include "WControls\CTL_WSta.hpp"
#include "Others\CTL_EnLi.hpp"
#include "WControls\CTL_WCkB.hpp"
#include "Controls\CTL_Cnst.hpp"

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

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

//**************************************************************************
//Constructor / Destructor
CTL_Editor_EnumControl::CTL_Editor_EnumControl(BOOL bReadOnly,
																							 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__ENUM,
																						bReadOnly,
																						CTL_SPACING_TYPE__SINGLE_LINE,
																						FALSE,
																						_pclParentList,
																						_csControlName,
																						 _bAcceptNameFromData, //ANNECY BBB
//Stefan Dumitrean 20-07-98 ( OAC buttons )
																						ucInitialCurrentPair
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																						 )
{
	m_pclEnumDescriptor = NULL;

	m_pclComboBox = NULL;
	m_pclStatic = NULL;

	m_pri_bMustSortNamesInComboBox = FALSE;

	m_td_pri_fn_vBuildEnumDesciptor_CallBack = NULL;
}

//**************************************************************************
CTL_Editor_EnumControl::~CTL_Editor_EnumControl()
{

}

//Member functions

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

	m_pclParentWnd = pclParentWnd;

	m_pro_fn_bCreateBaseControls(	C_ENUM_NAME_PERCENT_WIDTH,
																40,
																C_EDIT_HEIGHT);

	//Creates the static control (Read only)
	m_pclStatic = new CTL_Editor_Static("", CTL_STATIC_TYPE__FIELD_VALUE, 0, NULL, m_pclParentWnd);
#ifndef CTL_WITH_NO_TUT
	m_pclStatic->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Enum");
#endif //CTL_WITH_NO_TUT

	CTL_Editor_BaseControl *pclBC;
	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclStatic,
																																m_pclStatic,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																C_ENUM_BOX_PERCENT_WIDTH,
																																C_ENUM_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_pclComboBox = new CTL_Editor_ComboBox(CTL_COMBO_BOX_TYPE__STANDARD,
																						WS_TABSTOP | (m_pri_bMustSortNamesInComboBox ? CBS_SORT : 0),
																						this,
																						m_pclParentWnd);
		
		m_pclComboBox->m_pub_fn_vSetSelChanged_CallBack(s_m_fn_vComboBoxSelChanged);
		m_pclComboBox->m_pub_fn_vSetDropDown_CallBack(s_m_fn_vComboBoxDroppedDown);
#ifndef CTL_WITH_NO_TUT
		m_pclComboBox->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Enum");
#endif //CTL_WITH_NO_TUT

		pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclComboBox,
																																	m_pclComboBox,
																																	this,
																																	CTL_BASE_CONTROL_DISPLAY_TYPE__NORMAL,
																																	C_ENUM_BOX_PERCENT_WIDTH,
																																	C_ENUM_BOX_MIN_WIDTH,
																																	C_ENUM_COMBO_BOX_HEIGHT);
		pclBC->m_pub_fn_vSetCanBeDisplayedCallBack(s_m_fn_bComboBoxCanBeDisplayed);
	}

	m_bControlCreated = bReturnValue;

	return bReturnValue;
}

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

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_EnumControl::m_fn_vUpdateCurrentValue()
{
	//Gets the new current value
	if ( m_pclComboBox != NULL )
	{
		short wIndex = m_pclComboBox->GetCurSel();
		if ( wIndex != CB_ERR )
		{
			CTL_Editor_EnumElement *pclNewSelectedElement = (CTL_Editor_EnumElement *)(m_pclComboBox->GetItemDataPtr(wIndex));

			//CPA2 Stegaru Cristian 98/06/26
			if ( ((CTL_Editor_EnumData *)m_pclData)->mfn_pGetCurrentValue () != pclNewSelectedElement )
			//End CPA2 Stegaru Cristian 98/06/26
			{
				//CPA2 Stegaru Cristian 98/06/26
				((CTL_Editor_EnumData *)m_pclData)->mfn_pSetCurrentValue (pclNewSelectedElement);
				//End CPA2 Stegaru Cristian 98/06/26
			}
		}
	}

	m_fn_vUpdateParent(CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER);
}

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_EnumControl::m_fn_vUpdate(CTL_tdeUpdateReason _eReason /*= E_ur_NoReasonGiven*/,
																					long _lUserDefinedReason /*= 0*/)
{
	//Read/Write
	//Updates the combo box
	//CPA2 Stegaru Cristian 98/06/26
	CTL_Editor_EnumElement *pclSearchedElement = ((CTL_Editor_EnumData *)m_pclData)->mfn_pGetCurrentValue (); 
	//End CPA2 Stegaru Cristian 98/06/26
	CTL_Editor_EnumElement *pclCurrentComboValue;
	BOOL bFound = FALSE;
	if ( m_pclComboBox )
	{
		short wIndex = m_pclComboBox->GetCurSel();
		for ( wIndex = 0; (wIndex < m_pclComboBox->GetCount()) && (!bFound); wIndex ++ )
		{
			pclCurrentComboValue = (CTL_Editor_EnumElement *)(m_pclComboBox->GetItemDataPtr(wIndex));
			bFound = ( pclSearchedElement == pclCurrentComboValue );
		}

		if ( bFound )
			m_pclComboBox->SetCurSel(--wIndex);
	}

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

	//Read Only
	//CPA2 Stegaru Cristian 98/06/26
	if ( (m_pclStatic != NULL) && (((CTL_Editor_EnumData *)m_pclData)->mfn_pGetCurrentValue () != NULL) )
		m_pclStatic->m_fn_vSetTextToDisplay(((CTL_Editor_EnumData *)m_pclData)->mfn_pGetCurrentValue ()->m_pub_fn_csGetElementName());
	//End CPA2 Stegaru Cristian 98/06/26
}

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_EnumControl::m_fn_vUpdateParent(CTL_tdeUpdateReason eReason,
																								long _lUserDefinedReason /*= 0*/)
{
	//Updates motor's data
	m_pclData->m_fn_vUpdateMotorData(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 CTL_Editor_EnumControl::m_fn_vMakeReadOnly()
{
	if ( !m_fn_bIsReadOnly() )
	{
		m_bIsReadOnly = TRUE;
		m_fn_vHide();
	}
}

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

//************************************************************************
void CTL_Editor_EnumControl::m_fn_vGetNewEnumDescriptor()
{
	//Gets the EnumDescriptor of the Data
	m_pclEnumDescriptor = ((CTL_Editor_EnumData *)m_fn_pclGetEditedData())->m_fn_pclGetEnumDescriptor();

	ERROR_ASSERT( m_pclEnumDescriptor != NULL );

	//Fills the combo box
	if ( m_pclComboBox != NULL )
	{
		m_pclComboBox->ResetContent();

		CTL_Editor_EnumElement *pclElement;
		POSITION pos = m_pclEnumDescriptor->GetHeadPosition();
		while ( pos != NULL )
		{
			pclElement = m_pclEnumDescriptor->GetNext(pos);
			short wIndex = m_pclComboBox->AddString(pclElement->m_pub_fn_csGetElementName());
			m_pclComboBox->SetItemDataPtr(wIndex, (void *)(pclElement));
		}
	}
}

//************************************************************************
void CTL_Editor_EnumControl::m_pub_fn_vBuildEnumDescriptor()
{
	if(		( m_pclData != NULL )
		&&	( m_pclComboBox != NULL )
		)
	{
		CTL_Editor_EnumDescriptor	*pclEnumDescriptor = ((CTL_Editor_EnumData *)m_pclData)->m_fn_pclGetEnumDescriptor();
		ERROR_ASSERT( pclEnumDescriptor != NULL );

		//Remembers old value
		CString	csOldValue;
		int iCurSel = m_pclComboBox->GetCurSel();
		if ( iCurSel != CB_ERR )
			m_pclComboBox->GetLBText(iCurSel, csOldValue);

		//Constructs the new list, by CallBack if it exists
		if ( m_td_pri_fn_vBuildEnumDesciptor_CallBack != NULL )
			m_td_pri_fn_vBuildEnumDesciptor_CallBack(this, pclEnumDescriptor);

		//Clears the ComboBox
		m_pclComboBox->ResetContent();

		//Updates ComboBox's content
		BOOL bOldSelectionFound = FALSE;
		short wIndex;
		CTL_Editor_EnumElement *pclCurrentElement;
		POSITION pos = m_pclEnumDescriptor->GetHeadPosition();
		while ( pos != NULL )
		{
			pclCurrentElement = m_pclEnumDescriptor->GetNext(pos);
			
			wIndex = m_pclComboBox->AddString(pclCurrentElement->m_pub_fn_csGetElementName());
			m_pclComboBox->SetItemDataPtr(wIndex, (void *)(pclCurrentElement));

			if( pclCurrentElement->m_pub_fn_csGetElementName().CompareNoCase(csOldValue) == 0 )
			{
				m_pclComboBox->SetCurSel(wIndex);
				bOldSelectionFound = TRUE;
			}
		}

		//If selection has changed, selects the first entry
		if ( !bOldSelectionFound )
			m_pclComboBox->SetCurSel(0);
	}
}

//************************************************************************
void CTL_Editor_EnumControl::m_pub_fn_vSetBuildEnumDesciptor_CallBack(CTL_td_p_fn_vBuildEnumDescriptor _p_fn_vCallBack)
{
	ERROR_ASSERT( m_td_pri_fn_vBuildEnumDesciptor_CallBack == NULL );

	m_td_pri_fn_vBuildEnumDesciptor_CallBack = _p_fn_vCallBack;
}

//************************************************************************
void CTL_Editor_EnumControl::m_pub_fn_vMustSortNamesInComboBox(BOOL _bSortOrder /*= TRUE*/)
{
	m_pri_bMustSortNamesInComboBox = _bSortOrder;	
}

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

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

//************************************************************************
//When selection has been changed in a Combo box
void CTL_Editor_EnumControl::s_m_fn_vComboBoxSelChanged(class CTL_Editor_ComboBox *_pclSenderComboBox,
																												class CTL_Editor_Control *_pclParentControl,
																												enum CTL_eComboBoxType _tdeType,
																												long _lUserDefinedType,
																												long _lUserDefinedCode)
{
	((CTL_Editor_EnumControl *)_pclParentControl)->m_fn_vUpdateCurrentValue();
}

//************************************************************************
//When selection has been changed in a Combo box
void CTL_Editor_EnumControl::s_m_fn_vComboBoxDroppedDown(class CTL_Editor_ComboBox *_pclSenderComboBox,
																												 class CTL_Editor_Control *_pclParentControl,
																												 enum CTL_eComboBoxType _tdeType,
																												 long _lUserDefinedType,
																												 long _lUserDefinedCode)
{
	((CTL_Editor_EnumControl *)_pclParentControl)->m_pub_fn_vBuildEnumDescriptor();
}

//************************************************************************
void CTL_Editor_EnumControl::m_fn_vSetEditedData(CTL_Editor_Data *pclData)
{
	m_pclData = pclData;
	m_pclData->m_pub_fn_vSetParentControl(this);

	if ( m_pub_fn_tdeGetDataType() == CTL_DATA_TYPE__ENUM )
		((CTL_Editor_EnumControl *)this)->m_fn_vGetNewEnumDescriptor();
}

//ROMTEAM Selection (Cristian Stegaru 24/03/98)
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : m_fn_iSetCanISelect_CallBack 
// Date   : 98-03
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian - CPA2
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
int CTL_Editor_EnumControl::m_fn_iSetCanISelect_CallBack (CTL_tpf_iCheckString pfCheckString)
{
	if (NULL == m_pclComboBox)
		return 0;

	m_pclComboBox->m_fn_vSetCanISelect_CallBack (pfCheckString);
	return 1;
}
//ENDROMTEAM Selection (Cristian Stegaru)
