// EdIRDVVw.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRDVVw.hpp"

#include "EdIRDV.hpp"
#include "EdIRIRD.hpp"
#include "EdIRBeEn.hpp"
//BEGIN ROMTEAM Cristi Petrescu 98-06-
#include "edirfrm.hpp"
//END ROMTEAM Cristi Petrescu 98-06-

#include "EdIRCtl.hpp"
#include "EdIRQuer.hpp"

#include "ai_intf.hpp"

#include "Ctl.h"

#include "EDACDoc.hpp"
#include "EDACModl.hpp"

#include "incAI.h"

//ANNECY CB
#include "x:\cpa\main\inc\_EditID.h"
#include "_Ainterf.hpp"
#include "_Actors.hpp"
#include "EDIRGlob.hpp"
//END

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

//BEGIN ROMTEAM Cristi Petrescu 98-06-
#define UP_SPACING 70
//END ROMTEAM Cristi Petrescu 98-06-

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_DesignerVariablesView
/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CPA_EdIR_DesignerVariablesView, CFormView)

BEGIN_MESSAGE_MAP(CPA_EdIR_DesignerVariablesView, CTL_Editor_BaseFormView)
	//{{AFX_MSG_MAP(CPA_EdIR_DesignerVariablesView)
	ON_WM_SIZE()
//	ON_BN_CLICKED(IDC_CHECK1, OnCheckClicked)
	ON_BN_CLICKED(IDC_CHECK_SHOW_PRIVATE, OnCheckShowPrivate)
	//}}AFX_MSG_MAP
	//CPA2 Stegaru Cristian 98-06
	ON_BN_CLICKED (IDC_CHECK_HIDE_VAR_FOR_MODEL, OnCheckHideVariableForModel)
	//End CPA2 Stegaru Cristian 98-06
END_MESSAGE_MAP()

/*
//ANNECY CB
void CPA_EdIR_DesignerVariablesView::OnCheckClicked(void)
{
	CButton *pbut = (CButton *) GetDlgItem(IDC_CHECK1);
	AfxGetApp()->DoWaitCursor(1);
	SetCapture();
	if(pbut->GetCheck())
	{
		m_bHideVars = TRUE;
		m_fn_vUpdateDisplay();
	}
	else
	{
		m_bHideVars = FALSE;
		m_fn_vUpdateControls(m_pclListOfDesignerVariableControl);

		CPA_Actor	*pclSelectedActor;
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CTL_Editor_DataList	*pclListOfDesignerVariableType;

		pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
		if(pclSelectedActor)
		{
			pclListOfDesignerVariableType=pclSelectedActor->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			m_fn_vUpdateDisplayWithValues(pclListOfDesignerVariableType);
		}
	}
	AfxGetApp()->DoWaitCursor(-1);
	ReleaseCapture();
}
//END
*/

//BEGIN ROMTEAM Cristi Petrescu 98-06-
//**********************************************************************************
void CPA_EdIR_DesignerVariablesView::OnCheckShowPrivate() 
{
	CButton *pbut = (CButton *) GetDlgItem(IDC_CHECK_SHOW_PRIVATE);
	AfxGetApp()->DoWaitCursor(1);
	SetCapture();

	CPA_EdIR_Frame	*pclIRMainFrame = g_pclAIInterface->m_pclIRMainFrame;
	if (pclIRMainFrame)
	{
		CPA_EdIR_Brain *pclBrain = pclIRMainFrame -> m_pclBrain;
		if (pclBrain)
		{
			pclBrain -> m_bShowPrivateVariable = ! pclBrain -> m_bShowPrivateVariable;
			pbut -> SetCheck (pclBrain -> m_bShowPrivateVariable);

			// for saving... should notify the model actor somehow	
			pclBrain -> m_fn_pclGetModel () -> fn_vNotifySave ();

			m_fn_vUpdateDisplay();
		}
	}

	AfxGetApp()->DoWaitCursor(-1);
	ReleaseCapture();
}
//END ROMTEAM Cristi Petrescu 98-06-

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method	: OnCheckHideVariableForModel
// Date		: 98-06
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description	:
// Author		: Stegaru Cristian - CPA
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification	: 
// Date			:
// By			:
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CPA_EdIR_DesignerVariablesView::OnCheckHideVariableForModel ()
{
	CButton *pbut = (CButton *) GetDlgItem(IDC_CHECK_HIDE_VAR_FOR_MODEL);
	AfxGetApp()->DoWaitCursor(1);
	SetCapture();

	CPA_Actor	*pclSelectedActor;
	CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CTL_Editor_DataList	*pclListOfDesignerVariableType;

	pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
	if(pclSelectedActor)
	{
		if(pbut->GetCheck())
		{
			pclSelectedActor->mfn_vCheckHideVar (TRUE);
			m_fn_vUpdateDisplay();
		}
		else
		{
			pclSelectedActor->mfn_vCheckHideVar (FALSE);
			m_fn_vUpdateControls(m_pclListOfDesignerVariableControl);
			pclListOfDesignerVariableType=pclSelectedActor->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			m_fn_vUpdateDisplayWithValues(pclListOfDesignerVariableType);
		}
	}
	AfxGetApp()->DoWaitCursor(-1);
	ReleaseCapture();
}

/**********************************************************************************/
CPA_EdIR_DesignerVariablesView::CPA_EdIR_DesignerVariablesView()
	:CTL_Editor_BaseFormView(IDD, TRUE)
{
	//{{AFX_DATA_INIT(CPA_EdIR_DesignerVariablesView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pclListOfDesignerVariableControl=NULL;
	m_ulOldHeight=0;
//ANNECY CB
	m_bCanUpdateControls = TRUE;
//ANNECY Shaitan Correction (16/04/98) {
	m_bHideVars = TRUE;
//ENDANNECY Shaitan Correction }
//END
}

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

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::DoDataExchange(CDataExchange* pDX)
{
	CTL_Editor_BaseFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_DesignerVariablesView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DesignerVariablesView diagnostics

#ifdef _DEBUG
/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::AssertValid() const
{
	CTL_Editor_BaseFormView::AssertValid();
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::Dump(CDumpContext& dc) const
{
	CTL_Editor_BaseFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DesignerVariablesView message handlers

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::OnInitialUpdate() 
{
	HINSTANCE hOldInstance=AfxGetResourceHandle();
	AfxSetResourceHandle(g_stAIIdentity.hModule);

	CTL_Editor_BaseFormView::OnInitialUpdate();

//ANNECY Shaitan Correction (16/04/98) {
//	((CButton *)GetDlgItem(IDC_CHECK1))->SetCheck(m_bHideVars);
//ENDANNECY Shaitan Correction }

	fn_pclGetEdActorsDocument()->m_pclDesignerVariablesView=this;

	//CPA2 Stegaru Cristian 98-06
	if (FALSE == g_pclAIInterface->m_bIsOpened)
		EnableWindow (FALSE);
	else //opened
		EnableWindow ();
	//End CPA2 Stegaru Cristian 98-06

	AfxSetResourceHandle(hOldInstance);
}

//*******************************************************************************
void CPA_EdIR_DesignerVariablesView::OnSize(UINT nType, int cx, int cy) 
{
	CTL_Editor_BaseFormView::OnSize(nType, cx, cy);
//ANNECY CB
	if(m_pclListOfDesignerVariableControl!=NULL)
	{
		CRect		cr;
		GetClientRect(cr);
		CPoint	cpOffset=GetScrollPosition();
		//BEGIN ROMTEAM Cristi Petrescu 98-06-
		cpOffset.y -= UP_SPACING;
		//END ROMTEAM Cristi Petrescu 98-06-
		CPoint	cpTopLeft(0,-cpOffset.y);
		CPoint	cpTopRight(cr.Width(),-cpOffset.y);

		m_pclListOfDesignerVariableControl->m_pub_fn_vDisplayControlsInZone(cpTopLeft,cpTopRight);
	}
//	m_fn_vUpdateDisplay();
//END
}


/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vUpdateControls(CTL_Editor_ControlList *pclListOfDesignerVariableControl)
{
//ANNECY CB
	if(m_bCanUpdateControls == FALSE)
		return;
//	m_fn_vSetViewHeight(0);
//END

//ANNECY CB
	//CPA2 Stegaru Cristian 98-06
	if (m_pclListOfDesignerVariableControl != pclListOfDesignerVariableControl)
	//End CPA2 Stegaru Cristian 98-06
//END
	if(m_pclListOfDesignerVariableControl!=NULL)
	{
		POSITION	pos=m_pclListOfDesignerVariableControl->GetHeadPosition();
		while(pos!=NULL)
			m_pclListOfDesignerVariableControl->GetNext(pos)->m_fn_vHide();
	}

	m_pclListOfDesignerVariableControl=pclListOfDesignerVariableControl;

//ANNECY CB
	//CPA2 Stegaru Cristian 98-06
	if((mfn_bHideVariables ())||(m_pclListOfDesignerVariableControl==NULL))
	//End CPA2 Stegaru Cristian 98-06
		return;
	CString	csMsgBase="Updating vars controls ... ";
	fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);
//END

	CTL_fn_vSetDefaultCallBackFunctionWhenStaticIsClicked(fn_vOnButtonClick);
	CTL_fn_vSetDefaultCallBackFunctionToGetStaticColor(fn_vGetStaticColor);

	// BEGIN ROMTEAM Cristi Petrescu 98-11-
	CTL_fn_vSetModuleInfo("TIA","Designer Variables");
	POSITION	pos=m_pclListOfDesignerVariableControl->GetHeadPosition();
	while(pos!=NULL)
	{
		CTL_Editor_Control	*pclEditor_Control=m_pclListOfDesignerVariableControl->GetNext(pos);

		if(!pclEditor_Control->m_pub_fn_bHasBeenCreated())
		{
//			CTL_fn_vSetModuleInfo("TIA","Designer Variables");

			pclEditor_Control->m_fn_bCreateControl(this);

		}
	}
	CTL_fn_vResetModuleInfo();
	// END ROMTEAM Cristi Petrescu 98-11-

	CTL_fn_vSetDefaultCallBackFunctionWhenStaticIsClicked(NULL);
	CTL_fn_vSetDefaultCallBackFunctionToGetStaticColor(NULL);
//ANNECY CB
	CString csMsg=csMsgBase+"OK";
	fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
//END
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vUpdateValues(CTL_Editor_DataList	*pclListOfDesignerVariableType)
{
	if(m_pclListOfDesignerVariableControl == NULL)
		return;

	if(pclListOfDesignerVariableType->GetCount()!=m_pclListOfDesignerVariableControl->GetCount())
	{
		MessageBox("Pb with designer variable");

		return;
	}

	POSITION	posCtl=m_pclListOfDesignerVariableControl->GetHeadPosition();
	POSITION	posVal=pclListOfDesignerVariableType->GetHeadPosition();
	while(posCtl!=NULL)
	{
		CTL_Editor_Control	*pclEditor_Control=m_pclListOfDesignerVariableControl->GetNext(posCtl);
		CTL_Editor_Data			*pclEditor_BaseData=pclListOfDesignerVariableType->GetNext(posVal);

		pclEditor_Control->m_fn_vSetEditedData(pclEditor_BaseData);
		pclEditor_BaseData->m_fn_vGetMotorData();
	}
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vUpdateDisplayWithValues(CTL_Editor_DataList *pclListOfDesignerVariableType)
{
	m_fn_vUpdateValues(pclListOfDesignerVariableType);
//ANNECY CB
	//CPA2 Stegaru Cristian 98-06
	if(!mfn_bHideVariables ())
	//End CPA2 Stegaru Cristian 98-06
//END
	m_fn_vUpdateDisplay();
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vUpdateDisplay()
{
	//CPA2 Stegaru Cristian 98-06
	if (FALSE == g_pclAIInterface->m_bIsOpened)
		EnableWindow (FALSE);
	else //opened
		EnableWindow ();
	//End CPA2 Stegaru Cristian 98-06

	if(m_pclListOfDesignerVariableControl!=NULL)
	{
		CRect		cr;
		GetClientRect(cr);
		CPoint	cpOffset=GetScrollPosition();
//ANNECY CB
		//BEGIN ROMTEAM Cristi Petrescu 98-06-
		cpOffset.y -= UP_SPACING;
		//END ROMTEAM Cristi Petrescu 98-06-
//END
		CPoint	cpTopLeft(0,-cpOffset.y);
		CPoint	cpTopRight(cr.Width(),-cpOffset.y);
//ANNECY CB
		ShowWindow(FALSE);
//END
		m_pclListOfDesignerVariableControl->m_pub_fn_vDisplayControlsInZone(cpTopLeft,cpTopRight);

		POSITION	posCtl=m_pclListOfDesignerVariableControl->GetHeadPosition();
		while(posCtl!=NULL)
		{
			CTL_Editor_Control	*pclEditor_Control=m_pclListOfDesignerVariableControl->GetNext(posCtl);
			pclEditor_Control->m_fn_vUpdate();
		}

		m_fn_vSetViewHeight((unsigned short)(cpTopLeft.y+GetScrollPosition().y));

//ANNECY CB
		ShowWindow(TRUE);
//END
	}
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vAModelHasBeenRemoved(CTL_Editor_ControlList *pclListOfDesignerVariableControl)
{
	if(m_pclListOfDesignerVariableControl==pclListOfDesignerVariableControl)
		m_pclListOfDesignerVariableControl=NULL;
}

/**********************************************************************************/
void CPA_EdIR_DesignerVariablesView::m_fn_vSetOldHeight(unsigned long ulHeight)
{
	m_ulOldHeight=ulHeight;
}

/**********************************************************************************/
unsigned long CPA_EdIR_DesignerVariablesView::m_fn_ulGetOldHeight()
{
	return m_ulOldHeight;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
//**********************************************************************************
CTL_Editor_Data *CPA_EdIR_DesignerVariablesView::m_fn_pclGetVariable (CString csVariableName)
{
	POSITION pos = m_pclListOfDesignerVariableControl -> GetHeadPosition ();
	CTL_Editor_Data *pclEditor_Data;
	csVariableName . MakeLower ();

	while (pos)
	{
		pclEditor_Data = m_pclListOfDesignerVariableControl -> GetNext (pos) -> m_fn_pclGetEditedData ();		

		if (pclEditor_Data -> m_pub_fn_csGetDataName () == csVariableName )
			return pclEditor_Data;
	}

	return NULL;
}
//END ROMTEAM Cristi Petrescu 98-05-

//BEGIN ROMTEAM Cristi Petrescu 98-06-
void CPA_EdIR_DesignerVariablesView::m_fn_vSynchronizeWithBrain (CPA_EdIR_Brain *pclBrain)
{
	//CPA2 Stegaru Cristian 98-06
	if (!pclBrain)
		return;
	//End CPA2 Stegaru Cristian 98-06

	((CButton *) GetDlgItem (IDC_CHECK_SHOW_PRIVATE)) -> SetCheck (pclBrain -> m_bShowPrivateVariable);
	//CPA2 Stegaru Cristian 98-06
	CPA_Actor	*pModel = pclBrain->m_fn_pclGetModel();
	if (pModel)
		((CButton *)GetDlgItem (IDC_CHECK_HIDE_VAR_FOR_MODEL))->SetCheck (pModel->mfn_bIsHideVarChecked ());
	//End CPA2 Stegaru Cristian 98-06
}
//END ROMTEAM Cristi Petrescu 98-06-

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method	: mfn_bHideVariables 
// Date		: 98-06
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description	:
// Author		: Stegaru Cristian - CPA
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification	: 
// Date			:
// By			:
//////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CPA_EdIR_DesignerVariablesView::mfn_bHideVariables ()
{
	if (m_bHideVars)
		return m_bHideVars;

	CPA_EdIR_Frame	*pclIRMainFrame = g_pclAIInterface->m_pclIRMainFrame;
	if (pclIRMainFrame)
	{
		CPA_EdIR_Brain *pclBrain = pclIRMainFrame -> m_pclBrain;
		CPA_Actor	*pModel = NULL;
		if (pclBrain)
			pModel = pclBrain->m_fn_pclGetModel();

		if (pModel)
			return pModel->mfn_bIsHideVarChecked ();
	}

	return m_bHideVars;
}

void CPA_EdIR_DesignerVariablesView::mfn_vHideVariables (BOOL bHide)
{
	if (bHide)
	{
		m_bHideVars = TRUE;
		m_fn_vUpdateDisplay();
	}
	else
	{
		m_bHideVars = FALSE;
		m_fn_vUpdateControls(m_pclListOfDesignerVariableControl);

		CPA_Actor	*pclSelectedActor;
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CTL_Editor_DataList	*pclListOfDesignerVariableType;

		pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
		if(pclSelectedActor)
		{
			pclListOfDesignerVariableType=pclSelectedActor->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
			m_fn_vUpdateDisplayWithValues(pclListOfDesignerVariableType);
		}
	}
}

#endif //D_ED_IR_ACTIVE
