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

#include "EMECVwMS.hpp"

#include "EMECFmMn.hpp"
#include "EMECDoc.hpp"
#include "EMECCnst.hpp"

#include "_Minterf.hpp"

#include "help_def.h"	//for Context Help

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

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_MiniStrucView

IMPLEMENT_DYNCREATE(CPA_Meca_MiniStrucView, CTL_Editor_BaseFormView)

BEGIN_MESSAGE_MAP(CPA_Meca_MiniStrucView, CTL_Editor_BaseFormView)
	//{{AFX_MSG_MAP(CPA_Meca_MiniStrucView)
	ON_WM_HELPINFO()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//***************************************************************************
CPA_Meca_MiniStrucView::CPA_Meca_MiniStrucView()
											: CTL_Editor_BaseFormView(CPA_Meca_MiniStrucView::IDD, TRUE)
{
	//{{AFX_DATA_INIT(CPA_Meca_MiniStrucView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pclOldCardType = NULL;
}

//***************************************************************************
CPA_Meca_MiniStrucView::~CPA_Meca_MiniStrucView()
{

}

//Member Functions
////////////////////////////

//***************************************************************************
void CPA_Meca_MiniStrucView::OnInitialUpdate() 
{
	CTL_Editor_BaseFormView::OnInitialUpdate();
	
	m_fn_pclGetDocument()->m_pclMiniStrucView = this;

	CRect crClientRect;
	GetClientRect(crClientRect);
	
	//Sets the edition rectangle
	MoveWindow(CRect(0,0,100,100));
	m_fn_vCreateAllMS();
}

//*********************************************************************************
// Creates the MS controls (and their data controls)
void CPA_Meca_MiniStrucView::m_fn_vCreateAllMS()
{
	m_fn_vCreateMS(&(m_fn_pclGetDocument()->m_clCardTypeList));
}

//*********************************************************************************
// Creates the MS controls (and their data controls)
void CPA_Meca_MiniStrucView::m_fn_vCreateMS(CPA_Meca_CardTypeList *pclList)
{
	//Creates the list of MS (non visible action) 
	CRect crCurrentRect(m_crEditionRect);
	crCurrentRect.bottom = crCurrentRect.top + C_MS_LINE_HEIGHT;

	CPA_Meca_CardType *pclCurrentCardType;

	POSITION MiniStrucPos = pclList->GetHeadPosition();
	while ( MiniStrucPos != NULL )
	{
		pclCurrentCardType = pclList->GetNext(MiniStrucPos);
 	
		pclCurrentCardType->m_fn_lCreateMSControls(this, crCurrentRect);	  
		
		crCurrentRect.OffsetRect(0, C_MS_SPACING + C_MS_LINE_HEIGHT);
	}
}

//*******************************************************************************
//Called (during motor for example) to update current data in editor
void CPA_Meca_MiniStrucView::m_fn_vUpdateAllDatas()
{
	//Updates all controls
	POSITION ControlPos = m_fn_pclGetDocument()->m_pclCurrentCardType->m_clControlList.GetHeadPosition();
	while ( ControlPos != NULL )
		m_fn_pclGetDocument()->m_pclCurrentCardType->m_clControlList.GetNext(ControlPos)->m_fn_vUpdate();
}

//*******************************************************************************
void CPA_Meca_MiniStrucView::m_fn_vEditCard(CPA_Meca_Card *pclNewCard)
{
	m_fn_vUpdateAllDatas();
}

//*******************************************************************************
//To display a Card Type (<--> the associated controls)
void CPA_Meca_MiniStrucView::m_fn_vDisplayCardType(CPA_Meca_CardType *pclNewCardType,
																									 BOOL bMustRepaint)
{
	if ( (m_pclOldCardType != NULL) && (m_pclOldCardType != pclNewCardType) )
		m_pclOldCardType->m_fn_vHideAllControls();						

	if ( (bMustRepaint) || (m_pclOldCardType != pclNewCardType) )
	{
		//If no Card has been selected yet, we chose the first one
		//(else names are not displayed)
		if ( pclNewCardType->m_fn_pclGetEditedCard() == NULL )
		{
			//To avoid a non-loaded Card to be selected
			CPA_Meca_Card *pclCurrentCard = pclNewCardType->m_fn_pclGetCardsList()->m_pub_fn_pclGetFirstAvailableCard();
				
			pclNewCardType->m_fn_vSetEditedCard(pclCurrentCard);
		}

		if ( pclNewCardType->m_fn_pclGetEditedCard() != NULL )
		{
			m_fn_pclGetDocument()->m_fn_vSetPointersForCard(pclNewCardType->m_fn_pclGetEditedCard());

			pclNewCardType->m_fn_lOpen(m_crEditionRect);		
			
			m_pclOldCardType = pclNewCardType;
		}
	}
}

//*******************************************************************************
CPA_Meca_MyDocument* CPA_Meca_MiniStrucView::m_fn_pclGetDocument()
{
	return g_pclInterface->m_fn_pclGetDocument();
}

//***************************************************************************
BOOL CPA_Meca_MiniStrucView::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	CString csFileAndWindow = g_pclInterface->m_clDocument.m_csHelpFileNameAndPath;
	::WinHelp(m_hWnd,
						LPCTSTR(csFileAndWindow),
						HELP_CONTEXT,
						IDH_GENERAL);
	
	//return CFormView::OnHelpInfo(pHelpInfo);
	return FALSE;
}

//***************************************************************************
void CPA_Meca_MiniStrucView::OnSize(UINT nType, int cx, int cy) 
{
	CTL_Editor_BaseFormView::OnSize(nType, cx, cy);
	
	CRect crClientRect;
	GetClientRect(crClientRect);
	
	//Sets the edition rectangle
	m_crEditionRect.top = C_MAIN_VIEW_SPACING;
	m_crEditionRect.bottom = crClientRect.bottom - C_MAIN_VIEW_SPACING;
	m_crEditionRect.left = crClientRect.left + C_MAIN_VIEW_SPACING;
	m_crEditionRect.right = crClientRect.right - C_MAIN_VIEW_SPACING - C_SCROLLBAR_WIDTH;

	CPoint	cpTopLeft(0,C_MAIN_VIEW_SPACING);
	CPoint	cpTopRight(crClientRect.Width(),C_MAIN_VIEW_SPACING);

	if
	(
		m_fn_pclGetDocument()
	&&	(m_fn_pclGetDocument()->m_pclCurrentCardType) 
	&&	!(m_fn_pclGetDocument()->m_pclCurrentCardType->m_clControlList.IsEmpty())
	)
		m_fn_pclGetDocument()->m_pclCurrentCardType->m_clControlList.m_pub_fn_vDisplayControlsInZone(cpTopLeft,cpTopRight);
}

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_MiniStrucView diagnostics
//***************************************************************************
//***************************************************************************
//***************************************************************************

#ifdef _DEBUG
void CPA_Meca_MiniStrucView::AssertValid() const
{
	CTL_Editor_BaseFormView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_MiniStrucView message handlers


