// Implementation file for the definition of a group of controls
//	used to edit a Characteristic
/////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "Controls\CTL_Ctl.hpp"

#include "Controls\CTL_BCtl.hpp"
#include "WControls\CTL_WSta.hpp"
#include "WControls\CTL_WCkB.hpp"
#include "Others\CTL_Pri.hpp"
#include "Others\CTL_Pub.hpp"

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

#define C_TIMER_DELAY	150	//in milliseconds

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

//Constructor(s) / Destructor
////////////////////////////////////
//************************************************************************
CTL_Editor_Control::CTL_Editor_Control(CTL_tdeEditorDataType tdeType,
                                       BOOL bIsAlwaysReadOnly,
                                       CTL_tdeControlSpacingType tdeSpacingType,
                                       BOOL bUserCanChangeAspect,
																			 CTL_Editor_ControlList *_pclParentList,
																			 CString _csControlName,
																			 //ANNECY BBB {
																			 BOOL _bAcceptNameFromData /*= TRUE*/,
																			 //ANNECY BBB }
//Stefan Dumitrean 20-07-98 ( OAC buttons )
									   										 unsigned char ucInitialCurrentPair /* = 0*/
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

																			 )
{
	//ANNECY BBB {
	m_bAcceptNameFromData = _bAcceptNameFromData;
	//ANNECY BBB }
  m_tdeDataType = tdeType;
  m_bIsAlwaysReadOnly = bIsAlwaysReadOnly;  
  m_bIsReadOnly = m_bIsAlwaysReadOnly;
  m_tdeSpacingType = tdeSpacingType;
  m_bUserCanChangeAspect = bUserCanChangeAspect;
	m_pro_csControlName = _csControlName;
//ANNECY CB
#if 0
 	ERROR_PREPARE_M(CTL_g_csModuleNameForErO,
									"Trying to create a control",
									"CTL_Editor_Control::CTL_Editor_Control(...)",
									E_ERROR_GRAVITY_WARNING,
									"No name was given to the control... TUT Module probably won't accept this !");
	ERROR_ASSERT( !m_pro_csControlName.IsEmpty() );
#endif
//END
	
	m_pri_pclParentList = _pclParentList;

  m_bControlCreated = FALSE;
  m_bHidden = TRUE;
  
  m_pclNameStatic = NULL;
  m_pclData = NULL;

  m_crZoneRect = CRect(0,0,0,0);
  
  m_pclListOfBaseControls = new CTL_Editor_BaseControlList;
  
	m_pri_p_fn_bControlCanBeDisplayedCallBack = CTL_g_p_fnDefaultCallBack_ControlCanBeDisplayed;
//Stefan Dumitrean 20-07-98 ( OAC buttons )
  m_ucInitialCurrentPair = ucInitialCurrentPair;
//End Stefan Dumitrean 20-07-98 ( OAC buttons )

}

//************************************************************************
CTL_Editor_Control::~CTL_Editor_Control()
{
	delete m_pclListOfBaseControls;
}

//Access functions
//////////////////////////////////
//************************************************************************
BOOL CTL_Editor_Control::m_fn_bIsReadOnly()
{
	if ( m_bIsAlwaysReadOnly )
		return TRUE;
	else
		return m_bIsReadOnly;
}

//************************************************************************
BOOL CTL_Editor_Control::m_fn_bIsAlwaysReadOnly()
{
	return m_bIsAlwaysReadOnly;
}

//************************************************************************
BOOL CTL_Editor_Control::m_pub_fn_bHasBeenCreated()
{
	return m_bControlCreated;
}

//************************************************************************
BOOL CTL_Editor_Control::m_fn_bCanUserChangeAspect()
{
	return m_bUserCanChangeAspect;
}

//************************************************************************
CRect CTL_Editor_Control::m_fn_crGetZoneRect()
{
	return m_crZoneRect;
}

//************************************************************************
CTL_Editor_Data *CTL_Editor_Control::m_fn_pclGetEditedData()
{
	return m_pclData;
}

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

//************************************************************************
CTL_tdeEditorDataType CTL_Editor_Control::m_pub_fn_tdeGetDataType()
{
	return m_tdeDataType;
}

//************************************************************************
CTL_tdeControlSpacingType CTL_Editor_Control::m_fn_tdeGetSpacingType()
{
	return m_tdeSpacingType;
}

//************************************************************************
void CTL_Editor_Control::m_pub_fn_vDisplayInZone(	CRect &_r_crSameLineDestinationZone,
																									CRect &_r_crNextLineDestinationZone)
{
	if	(	m_pub_fn_bMustBeDisplayed() )
	{
		//Security !!
		_r_crSameLineDestinationZone.bottom = _r_crSameLineDestinationZone.top;
		_r_crNextLineDestinationZone.bottom = _r_crNextLineDestinationZone.top;

		switch( m_fn_tdeGetSpacingType() )
		{											    
			//#################################################################
			case CTL_SPACING_TYPE__MULTI_LINE:
			case CTL_SPACING_TYPE__SINGLE_LINE:
				//Forces this control to start on next line
				_r_crSameLineDestinationZone = _r_crNextLineDestinationZone;

				m_pri_fn_vDisplaySubControlsInZone(	TRUE,
																						_r_crSameLineDestinationZone,
																						_r_crNextLineDestinationZone);

				//Forces the next control to start on next line
				_r_crSameLineDestinationZone = _r_crNextLineDestinationZone;

				break;

			//#################################################################
			case CTL_SPACING_TYPE__AUTO:
			case CTL_SPACING_TYPE__FIXED_SIZE:
				//If control lies in the left X space
				if ( m_pri_fn_bCanDisplayInWidth(_r_crSameLineDestinationZone.Width()) )
				{
					m_pri_fn_vDisplaySubControlsInZone(	FALSE,
																							_r_crSameLineDestinationZone,
																							_r_crNextLineDestinationZone);
				}
				//else changes for the next line
				else
				{
					//Forces this control to start on next line
					_r_crSameLineDestinationZone = _r_crNextLineDestinationZone;

					m_pri_fn_vDisplaySubControlsInZone(	TRUE,
																							_r_crSameLineDestinationZone,
																							_r_crNextLineDestinationZone);
				}
 
				break;
		}
	}//End of test for non display
	else
	{
		m_fn_vHide();
		//Security !!
		m_bHidden = TRUE;
	}
}

//************************************************************************
BOOL CTL_Editor_Control::m_pri_fn_bCanDisplayInWidth(unsigned short uwLeftWidth)
{
//ANNECY CB
#if 0
	ERROR_ASSERT(		(m_fn_tdeGetSpacingType() == CTL_SPACING_TYPE__AUTO)
							||	(m_fn_tdeGetSpacingType() == CTL_SPACING_TYPE__FIXED_SIZE)
							);
#endif
//END
	return ( uwLeftWidth >= m_pri_fn_uwComputeTotalWidth(0) );	//Parameter is only used for Percentage calculations
}

//************************************************************************
unsigned short CTL_Editor_Control::m_pri_fn_uwComputeTotalWidth(unsigned short uwAllowedTotalWidth)
{	
	unsigned short uwTotalWidth = 0;

	m_pclListOfBaseControls->m_pub_fn_vComputeZoneOfAllBaseControls(uwAllowedTotalWidth,
																																	m_fn_tdeGetSpacingType());

	CTL_Editor_BaseControl *pclBaseCtrl;
	POSITION pos = m_pclListOfBaseControls->GetHeadPosition();
	while ( pos != NULL )
	{
		pclBaseCtrl = m_pclListOfBaseControls->GetNext(pos);

		if ( !pclBaseCtrl->m_pri_bSamePlaceAsNext )
			uwTotalWidth += (pclBaseCtrl->m_pri_lComputedWidth + CTL_C_SPACE_BETWEEN_CONTROLS);
	}

	return uwTotalWidth;
}

//************************************************************************
unsigned char CTL_Editor_Control::m_pri_fn_ucGetBaseControlNumber()
{
	return m_pclListOfBaseControls->GetCount();
}

//Member functions
///////////////////////////
//************************************************************************
//Display of sub_controls in list
void CTL_Editor_Control::m_pri_fn_vDisplaySubControlsInZone(BOOL _bUseNextLineZone,
																														CRect &_r_crSameLineDestinationZone,
																														CRect &_r_crNextLineDestinationZone)
{
	ERROR_ASSERT( m_pclData != NULL );

	if ( m_fn_bAcceptsToTakeNameFromData() && (m_pub_fn_csGetControlName().CompareNoCase(m_pclData->m_pub_fn_csGetDataName()) != 0) ) //ANNECY BBB
		m_pub_fn_csSetControlName(m_pclData->m_pub_fn_csGetDataName());

	//Computes exact width for all base controls in list
	short wTotalWidth = _bUseNextLineZone ?	_r_crNextLineDestinationZone.Width(): _r_crSameLineDestinationZone.Width();
	
	m_pclListOfBaseControls->m_pub_fn_vComputeZoneOfAllBaseControls(wTotalWidth,
																																	m_fn_tdeGetSpacingType());

	//Then displays them
	CTL_Editor_BaseControl *pclCurrentBaseControl;
	POSITION pos = m_pclListOfBaseControls->GetHeadPosition();

	while ( pos != NULL )
	{
		pclCurrentBaseControl = m_pclListOfBaseControls->GetNext(pos);
		
		if ( pclCurrentBaseControl->m_pub_fn_bCanBeDisplayed() )
			pclCurrentBaseControl->m_pub_fn_vMoveInZone(_r_crSameLineDestinationZone,
																									_r_crNextLineDestinationZone);
	}

	//Displays (only if necessary, i.e. if hidden)
	m_bHidden = TRUE;	//TEMPORAIRE : to force display
	m_fn_vDisplay();
	//Security !!
	m_bHidden = FALSE;
}

//************************************************************************
//Function called to update parent window in case of values changes.
void CTL_Editor_Control::m_fn_vUpdateParent(CTL_tdeUpdateReason eReason,
																						long _lUserDefinedReason /*= 0*/)
{

}

//************************************************************************
void CTL_Editor_Control::m_pub_fn_vSetControlCanBeDisplayedCallBack(CTL_td_p_fn_bControlCanBeDisplayed _p_fn_CallBalck)
{
	ERROR_ASSERT( m_pri_p_fn_bControlCanBeDisplayedCallBack == NULL );

	if ( m_pri_p_fn_bControlCanBeDisplayedCallBack == NULL )
		m_pri_p_fn_bControlCanBeDisplayedCallBack = _p_fn_CallBalck;
}

//************************************************************************
BOOL CTL_Editor_Control::m_pub_fn_bMustBeDisplayed()
{
	//Calls CallBack if it exists
	if ( m_pri_p_fn_bControlCanBeDisplayedCallBack != NULL )
		return m_pri_p_fn_bControlCanBeDisplayedCallBack(this);
	else
		return TRUE;
}

//************************************************************************
//Function called to edit complex data (which can not be directly
//edited)
void CTL_Editor_Control::m_fn_vEditData()
{
	ERROR_ASSERT( FALSE )	;
}

//************************************************************************
//Function called to hide group of controls
void CTL_Editor_Control::m_fn_vHide()
{
	m_pro_fn_vHideBaseControls();

	m_crZoneRect = CRect(0,0,0,0);
}

//************************************************************************
void CTL_Editor_Control::m_fn_vAskForNewAspect(unsigned short uwAllowedTotalWidth)
{
//	EdActors_ControlDefinitionDialog dial(this, uwAllowedTotalWidth);
//	dial.DoModal();
}

//************************************************************************
//Function called to develop complex data in tree
void CTL_Editor_Control::m_fn_vDevelopData()
{

}

//The list of base controls
//************************************************************************
CTL_Editor_BaseControlList *CTL_Editor_Control::m_pub_fn_pclGetListOfBaseControls()
{
	return m_pclListOfBaseControls;
}

//************************************************************************
//The parent list of controls
CTL_Editor_ControlList *CTL_Editor_Control::m_pub_fn_pclGetParentList()
{
	return m_pri_pclParentList;
}

//************************************************************************
//Returns the old name
CString CTL_Editor_Control::m_pub_fn_csSetControlName(CString _csNewName)
{
	CString csOldName(m_pro_csControlName);
	
	m_pro_csControlName = _csNewName;
	
	//Updates display
	m_pclNameStatic->m_fn_vSetTextToDisplay(m_pro_csControlName);

	return csOldName;
}

//************************************************************************
CString CTL_Editor_Control::m_pub_fn_csGetControlName()
{
	return m_pro_csControlName;
}

//************************************************************************
CTL_Editor_Static *CTL_Editor_Control::m_pub_fn_pclGetNameStatic()
{
	return m_pclNameStatic;
}

//*********************************************************************************
CTL_OwnerData *CTL_Editor_Control::m_pub_fn_pclAddOwnerData(void *_pvDataPtr,
																														CString _csName)
{
	return m_pri_clListOfOwnerData.m_pub_fn_pclAddOwnerData(_pvDataPtr, _csName);
}

//*********************************************************************************
CTL_OwnerData *CTL_Editor_Control::m_pub_fn_pclAddOwnerData(long _lData,
																														CString _csName)
{
	return m_pri_clListOfOwnerData.m_pub_fn_pclAddOwnerData(_lData, _csName);
}

//*********************************************************************************
CTL_OwnerData *CTL_Editor_Control::m_pub_fn_pclGetOwnerDataWithName(CString _csSearchedName)
{
	return m_pri_clListOfOwnerData.m_pub_fn_pclFindOwnerDataWithName(_csSearchedName);
}

//************************************************************************
// Constructs the Edit for the name and the CheckBox for the links
BOOL CTL_Editor_Control::m_pro_fn_bCreateBaseControls(unsigned char _ucNamePercentWidth,
																											unsigned short _uwNameMinWidth,
																											unsigned short _uwHeight,
																											BOOL _bGoToNextLineAfterName /*= FALSE*/)
{
	BOOL bCreationOK = TRUE;

	//Creates a static box for the name of the charac.
	////////////////////////////////////////////////////
	ERROR_ASSERT( m_pclNameStatic == NULL );

	m_pclNameStatic = new CTL_Editor_Static(m_pro_csControlName,
																					CTL_STATIC_TYPE__FIELD_NAME,
																					0,
																					this,
																					m_pclParentWnd
																					);
#ifndef CTL_WITH_NO_TUT
	m_pclNameStatic->m_pub_fn_vRegisterWindowsControl(m_pub_fn_csGetControlName() + " - Field Name");
#endif //CTL_WITH_NO_TUT

	CTL_Editor_BaseControl *pclBC;
	pclBC = m_pclListOfBaseControls->m_pub_fn_pclAddControlAtTail(m_pclNameStatic,
																																m_pclNameStatic,
																																this,
																																CTL_BASE_CONTROL_DISPLAY_TYPE__USE_CALLBACK,
																																_ucNamePercentWidth,
																																_uwNameMinWidth,
																																_uwHeight,
																																FALSE,
																																_bGoToNextLineAfterName);

	pclBC->m_pub_fn_vSetGetSize_CallBack(CTL_fn_lComputeSizeOfStaticForName);

	return bCreationOK;
}

//************************************************************************
void CTL_Editor_Control::m_pro_fn_vDisplayBaseControls()
{
	if ( m_bHidden )
	{
		POSITION pos = m_pclListOfBaseControls->GetHeadPosition();
		while ( pos != NULL )
			m_pclListOfBaseControls->GetNext(pos)->m_fn_vDisplayBaseControl();

		m_bHidden = FALSE;
	}
}

//************************************************************************
void CTL_Editor_Control::m_pro_fn_vHideBaseControls()
{
	if ( !m_bHidden )
	{
		POSITION pos = m_pclListOfBaseControls->GetHeadPosition();
		while ( pos != NULL )
			m_pclListOfBaseControls->GetNext(pos)->m_fn_vHideBaseControl();

		m_bHidden = TRUE;
	}
}





