// compprop.cpp : implementation file
//

#include "stdafx.h"
#include "acp_base.h"
#include "camresrc.h"
#include "compprop.hpp"
#include "pagetarget.hpp"
#include "pagerotcenter.hpp"
#include "camera.hpp"
#include "caminter.hpp"

#define M_GetDLL(Camera) ((Camera_Interface*)((Camera)->GetEditor()))
#define M_GetAxisCombo() ((CComboBox*)GetDlgItem(IDC_COMBO_AXIS))
/////////////////////////////////////////////////////////////////////////////
// CompleteProperties dialog


//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
CompleteProperties::CompleteProperties(Camera* p_oCamera,CWnd* pParent /*=NULL*/)
	: CDialog(CompleteProperties::IDD, pParent),
	m_oTargetPage(p_oCamera),m_oRotCenterPage(p_oCamera),m_oGeneralPage(p_oCamera)
{

	//{{AFX_DATA_INIT(CompleteProperties)
	m_csTypeName = _T("");
	m_csCameraName = _T("");
	//}}AFX_DATA_INIT
	m_p_oCamera = p_oCamera;
}


//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void CompleteProperties::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CompleteProperties)
	DDX_Control(pDX, IDC_TAB_PROPERTIES, m_oTabControl);
	DDX_Text(pDX, IDC_EDIT_TYPE, m_csTypeName);
	DDX_Text(pDX, IDC_EDIT_NAME, m_csCameraName);
	//}}AFX_DATA_MAP

}


BEGIN_MESSAGE_MAP(CompleteProperties, CDialog)
	//{{AFX_MSG_MAP(CompleteProperties)
	ON_NOTIFY(TCN_SELCHANGING, IDC_TAB_PROPERTIES, OnSelchangingTabProperties)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_PROPERTIES, OnSelchangeTabProperties)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CompleteProperties message handlers

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
BOOL CompleteProperties::OnInitDialog() 
{
	TC_ITEM	stTabItem;
	CRect	oRect;
	tdeDisplayReferential eDisplayMode;

	CDialog::OnInitDialog();

	// get the display rect
	GetDlgItem(IDC_STATIC_PAGEPOS)->GetWindowRect(&oRect);
	ScreenToClient(&oRect);

	// create the two pages
	m_oTargetPage.Create(IDD_PROPPAGE_TARGET,this);
	m_oRotCenterPage.Create(IDD_PROPPAGE_ROTATIONCENTER,this);
	m_oGeneralPage.Create(IDD_PROPPAGE_GENERAL,this);

	// create the tabs

	stTabItem.mask = TCIF_TEXT|TCIF_PARAM ;
	stTabItem.pszText = "General";
	stTabItem.lParam = (LPARAM) &m_oGeneralPage;
	m_oTabControl.InsertItem(0,&stTabItem);

	stTabItem.mask = TCIF_TEXT|TCIF_PARAM ;
	stTabItem.pszText = "Target";
	stTabItem.lParam = (LPARAM) &m_oTargetPage;
	m_oTabControl.InsertItem(1,&stTabItem);

	stTabItem.mask = TCIF_TEXT|TCIF_PARAM ;
	stTabItem.pszText = "Rotation Center";
	stTabItem.lParam = (LPARAM) &m_oRotCenterPage;
	m_oTabControl.InsertItem(2,&stTabItem);

	// move the pages at the right place
	m_oTargetPage.SetWindowPos(&wndTop,oRect.left,oRect.top,0,0,SWP_NOSIZE);
	m_oRotCenterPage.SetWindowPos(&wndTop,oRect.left,oRect.top,0,0,SWP_NOSIZE);
	m_oGeneralPage.SetWindowPos(&wndTop,oRect.left,oRect.top,0,0,SWP_NOSIZE);	

	// activate the first tab
	m_oTabControl.SetCurSel(0);
	m_oGeneralPage.ShowWindow(SW_SHOWNORMAL);

	// put the type name of the camera
	m_csTypeName = (m_p_oCamera->GetCameraType() == InertCamera)? "Editor Camera" : "Actor Camera" ;
	// put the name of the camera
	m_csCameraName = m_p_oCamera->GetName();
	// update drawing
	UpdateData(FALSE);

	eDisplayMode = M_GetDLL(m_p_oCamera) -> GetReferentialDisplayMode();

	if( eDisplayMode == DisplayNone )
		M_GetAxisCombo() -> SelectString(-1,"No");
	else if( eDisplayMode == DisplayDownLeft )
		M_GetAxisCombo() -> SelectString(-1,"Down Left");
	else if( eDisplayMode == DisplayDownRight)
		M_GetAxisCombo() -> SelectString(-1,"Down Right");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void CompleteProperties::OnSelchangingTabProperties(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int			index;
	TC_ITEM		stTabItem;
	PageBase	*p_oPage;

	stTabItem.mask = TCIF_PARAM ;
	index = TabCtrl_GetCurSel(pNMHDR->hwndFrom);
	m_oTabControl.GetItem(index,&stTabItem);
	p_oPage = (PageBase*)stTabItem.lParam;

	if (p_oPage->IsPageValid())
	{
		p_oPage->ShowWindow(SW_HIDE);
		p_oPage->MakeChangesAvailable();
		*pResult = 0;
	}
	else
	{
		*pResult = TRUE;
	}
}

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void CompleteProperties::OnSelchangeTabProperties(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int			index;
	TC_ITEM		stTabItem;
	PageBase	*p_oPage;

	stTabItem.mask = TCIF_PARAM ;
	index = TabCtrl_GetCurSel(pNMHDR->hwndFrom);
	m_oTabControl.GetItem(index,&stTabItem);
	p_oPage = (PageBase*)stTabItem.lParam;
	
	p_oPage->ShowWindow(SW_SHOWNORMAL);

	*pResult = 0;
}


//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void CompleteProperties::OnOK() 
{
	int			index,i;
	TC_ITEM		stTabItem;
	PageBase	*p_oPage;

	UpdateData(TRUE);
	stTabItem.mask = TCIF_PARAM ;

	// look if all tabs are valid
	//beginning with the active one
	index = m_oTabControl.GetCurSel();
	m_oTabControl.GetItem(index,&stTabItem);
	p_oPage = (PageBase*)stTabItem.lParam;
	if (!p_oPage->IsPageValid())
		return;

	// then all other ones
	for (i=0; i<m_oTabControl.GetItemCount(); i++)
	{
		if (i != index)
		{
			m_oTabControl.GetItem(i,&stTabItem);
			p_oPage = (PageBase*)stTabItem.lParam;
			if (!p_oPage->IsPageValid())
			{
				m_oTabControl.SetCurSel(i);
				return;
			}
		}
	}

	// ok, everything is valid
	// so let's make changes
	for (i=0; i<m_oTabControl.GetItemCount(); i++)
	{
		m_oTabControl.GetItem(i,&stTabItem);
		p_oPage = (PageBase*)stTabItem.lParam;

		p_oPage->MakeChangesAvailable();
		p_oPage->DoUpdate();
	}
	
	// Axis Display
	int iIndex = M_GetAxisCombo() -> GetCurSel();
	if( iIndex != CB_ERR )
	{
		CString csText;

		M_GetAxisCombo() -> GetLBText( iIndex, csText );
		if( csText == "No" )
			M_GetDLL(m_p_oCamera) -> SetReferentialDisplayMode( DisplayNone );
		else if( csText == "Down Left" )
			M_GetDLL(m_p_oCamera) -> SetReferentialDisplayMode( DisplayDownLeft );
		else if( csText == "Down Right" )
			M_GetDLL(m_p_oCamera) -> SetReferentialDisplayMode( DisplayDownRight );
	}

	CDialog::OnOK();
}
