/*=========================================================================
*
* CPAdSel.cpp :  Selection - Implementation file
*
*
* Version			1.0
* Creation date
* Revision date
*
* Shaitan
*=======================================================================*/

#include "stdafx.h"
#ifdef ACTIVE_EDITOR
#include "acp_base.h"
#include "itf/CPAdSel.hpp"
#include "itf/DEVMul3d.Hpp"
#include "itf/CPAInter.hpp"

//#################################################################################
// CPA_DialogSelect dialog
//#################################################################################

/*----------------------------------------
----------------------------------------*/
CPA_DialogSelect::CPA_DialogSelect(CWnd* pParent /*=NULL*/)
	: CDialog(CPA_DialogSelect::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPA_DialogSelect)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_DialogSelect)
	DDX_Control(pDX, CPA_IDC_LISTSELECT2, m_cListSelect2);
	DDX_Control(pDX, CPA_IDC_LISTSELECT, m_cListSelect);
	DDX_Control(pDX, CPA_IDC_HIERARCHY2, m_cHierarchy2);
	DDX_Control(pDX, CPA_IDC_HIERARCHY1, m_cHierarchy);
	//}}AFX_DATA_MAP
}


/*----------------------------------------
----------------------------------------*/
BEGIN_MESSAGE_MAP(CPA_DialogSelect, CDialog)
	//{{AFX_MSG_MAP(CPA_DialogSelect)
	ON_LBN_SELCHANGE(CPA_IDC_LISTSELECT, OnSelchangeListSelect)
	ON_LBN_DBLCLK(CPA_IDC_LISTSELECT, OnDblClkListSelect)
	ON_LBN_SELCHANGE(CPA_IDC_HIERARCHY1, OnSelchangeHierarchy)
	ON_LBN_DBLCLK(CPA_IDC_HIERARCHY1, OnDblClkHierarchy)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//#################################################################################
// CPA_DialogSelect message handlers
//#################################################################################

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::fn_vDoDialog (CPA_Interface *pInterface, tdeSelectMode eMode, CString csTypeName, CString a_csValidTypes[], long lNbTypes)
{
	long			lIndex;

	// init parameters
	m_pInterface = pInterface;
	m_eMode = eMode;
	m_lNbTypes = lNbTypes;
	// type name
	m_csTypeName = csTypeName;
	// valid types
	if (a_csValidTypes)
	{
		for (lIndex = 0; lIndex <lNbTypes; lIndex++)
			m_a_csValidTypes[lIndex] = a_csValidTypes[lIndex];
	}
	// this dialog is modal
	DoModal();
}


/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::fn_vInitDialog ()
{
	m_stListSelect.RemoveAll();
	m_stHierarchy.RemoveAll();
	m_pSelection = NULL;
	m_pObjectToSelect = NULL;
}


/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::fn_vCloseDialog (void)
{
	// close the dialog
	m_bDoDialog	= FALSE;
	// release capture
	SetCapture();
	m_p_oSurrenderCapture();
}

/*----------------------------------------
----------------------------------------*/
BOOL CPA_DialogSelect::OnInitDialog ()
{
	CPA_SuperObject *pElem;
	POSITION		pos;
	char			szName[256];
	char			szType[256];

	// Create the dialog
	CDialog::OnInitDialog();
	// set focus to the dialog
	m_p_oGrabCapture();
	// free capture (for controls)
	ReleaseCapture();

	// init list destroy
	for (pElem = m_stListSelect.GetHeadElement(pos); pElem;
		 pElem = m_stListSelect.GetNextElement(pos))
	{
		sprintf(szName, "%s", pElem->GetNameToDraw());
		sprintf(szType, " %.1s-%s", pElem->GetRealTypeName(), pElem->GetModelName());
		m_cListSelect.AddString(szName);
		m_cListSelect2.AddString(szType);
	}
	// set current selection
	m_cListSelect.SetCurSel(0);
	m_pSelection = m_stListSelect.FindElementFromIndex(0);
	// init hierarchy
	fn_vInitHierarchy();

	return TRUE;

}

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::fn_vInitHierarchy (void)
{
	CPA_SuperObject *pElem;
	POSITION		pos;
	char			szName[256];
	char			szType[256];
	long			lIndex;

	// RAZ
	m_stHierarchy.RemoveAll();
	m_cHierarchy.ResetContent();
	m_cHierarchy2.ResetContent();
	// fill hierarchy list
	if (m_lNbTypes)
	{
		for (lIndex = 0; lIndex <m_lNbTypes; lIndex++)
			GetInterface()->fn_lFindSelectionInHierarchy(m_eMode, m_pSelection, m_a_csValidTypes[lIndex]);
	}
	else
			GetInterface()->fn_lFindSelectionInHierarchy(m_eMode, m_pSelection, m_csTypeName);
	// init control
	for (pElem = m_stHierarchy.GetHeadElement(pos); pElem;
		 pElem = m_stHierarchy.GetNextElement(pos))
	{
		sprintf(szName, "%s", pElem->GetNameToDraw());
		sprintf(szType, " %.1s-%s", pElem->GetRealTypeName(), pElem->GetModelName());
		m_cHierarchy.AddString(szName);
		m_cHierarchy2.AddString(szType);
	}
	// set default selection
	m_cHierarchy.SetCurSel(0);
	m_pObjectToSelect = m_stHierarchy.FindElementFromIndex(0);
	// update drawing
	GetInterface()->fn_vUpdateSelection(m_pObjectToSelect, m_eMode);
}


/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnSelchangeListSelect()
{
	// set the current selection
	m_pSelection = m_stListSelect.FindElementFromIndex(m_cListSelect.GetCurSel());
	// update hierarchy list
	fn_vInitHierarchy();
}


/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnDblClkListSelect()
{
	// set the current selection
	m_pSelection = m_stListSelect.FindElementFromIndex(m_cListSelect.GetCurSel());
	// update hierarchy list
	fn_vInitHierarchy();
	if (m_pSelection)
	{
		// get object to select
		m_pObjectToSelect = m_pSelection;
		// close the dialog
		fn_vCloseDialog();
		CDialog::OnOK();
	}
}

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnSelchangeHierarchy()
{
	// get object to select
	m_pObjectToSelect = m_stHierarchy.FindElementFromIndex(m_cHierarchy.GetCurSel());
	// update drawing
	GetInterface()->fn_vUpdateSelection(m_pObjectToSelect, m_eMode);
}

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnDblClkHierarchy()
{
	// get object to select
	m_pObjectToSelect = m_stHierarchy.FindElementFromIndex(m_cHierarchy.GetCurSel());
	// update drawing
	GetInterface()->fn_vUpdateSelection(m_pObjectToSelect, m_eMode);
	// close the dialog
	if (m_pObjectToSelect)
	{
		fn_vCloseDialog();
		CDialog::OnOK();
	}
}

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnOK()
{
	// set the current selection
	m_pObjectToSelect = m_stHierarchy.FindElementFromIndex(m_cHierarchy.GetCurSel());
	// close the dialog
	fn_vCloseDialog();
	CDialog::OnOK();
}

/*----------------------------------------
----------------------------------------*/
void CPA_DialogSelect::OnCancel()
{
	// cancel the selection
	m_pObjectToSelect = NULL;
	// close the dialog
	fn_vCloseDialog();
	CDialog::OnCancel();
}



#endif // ACTIVE_EDITOR
