/*=========================================================================
* 
* EDTlType.cpp : Editor Lists - Implementation file
* 
*
* Version			1.0
* Creation date		
* Revision date
*
* Shaitan
*=======================================================================*/

#include "stdafx.h"

#ifdef ACTIVE_EDITOR

#include "acp_base.h"
#include "itf/CPAModel.hpp"
#include "itf/CPAProj.hpp"
#include "itf/CPAdIns.hpp"
#include "itf/CPAInter.hpp"

#include <direct.h>



//#################################################################################
// CLASS EDT_Model
//#################################################################################

/*===========================================================================
*		Description:	constructor 
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------

*		Revision date:                                                                                          Author:         
*=========================================================================*/
EDT_Model::EDT_Model (CPA_ObjectDLLBase *p_oDLL, CString csTypeName, CString csName, CString csBitmapName, CPA_Interface *pInterface)
{
	CString csBitmapLoad;
	
	// set DLL
	m_pObjectDLL = p_oDLL;
	// set type
	m_csTypeName = csTypeName;
	// set descriptor
	m_csName = csName;
	// set bitmap name
	m_csBitmapName = csBitmapName;
 
	// get complete file name
	csBitmapLoad = M_GetMainApp()->m_csEditorDataPath + "\\" + m_csBitmapName;
	// create bitmap and store it into the list
	m_iBitmapPos = pInterface->GetBitmapList()->m_fn_iAdd(csBitmapLoad);
	if (m_iBitmapPos == -1)
	{
		csBitmapLoad = M_GetMainApp()->m_csEditorDataPath + "\\Hierarchy\\Default.bmp";
		m_iBitmapPos = pInterface->GetBitmapList()->m_fn_iAdd(csBitmapLoad);
	}
}

EDT_Model::~EDT_Model (void)
{
}


/*----------------------------------------
			Bitmap
----------------------------------------*/
BOOL EDT_Model::fn_bChangeBitmap (CPA_Interface *pInterface, CString csNewBitmapName)
{
	CString csBitmapLoad;
	int		iBitmapPos;

	// get complete file name
	csBitmapLoad = M_GetMainApp()->m_csEditorDataPath + "\\" + csNewBitmapName;
	// create bitmap and store it into the list
	iBitmapPos = pInterface->GetBitmapList()->m_fn_iAdd(csBitmapLoad);
	if (iBitmapPos != -1)
	{
		m_csBitmapName = csNewBitmapName;
		m_iBitmapPos = iBitmapPos;
		return TRUE;
	}
	else
		return FALSE;
}

//#################################################################################
// EDT_ModelView 
//#################################################################################

/*----------------------------------------
			CONSTRUCTEUR
----------------------------------------*/
EDT_ModelView::EDT_ModelView () 
	: C3ListView()
{
	// update parameters
	m_fn_vSetBox(C_cDisplayBitmapAndText, 80, 52);
	m_fn_vSetBox(C_cDisplayBitmap, 34, 34);
	m_fn_vSetBox(C_cDisplayText, 80, 18);
	// set DD parameters
	m_fn_vSetDDTypeAndEditor( C_ucModel, "Evt_Editor");
}

EDT_ModelView::~EDT_ModelView()
{
}

/*-----------------------------------------------------------------------------
 *  Description : called when selected item change to the given case
 *---------------------------------------------------------------------------*/
void EDT_ModelView::OnSelectItem (short wCase)
{
	CPA_Interface	*pInterface;
	EDT_Model		*pNewModel;

	C3ListView::OnSelectItem(wCase);

	pInterface = ((CPA_DialogInsert *) GetParent())->GetInterface();

	// Get model corresponding to the choice
	tdstTSLItem *p_stCurItem = m_fn_p_stGetItem(wCase, m_cCurrentOrder);
	if (p_stCurItem)
		pNewModel = (EDT_Model *) p_stCurItem->m_lContent;
	else
		pNewModel = NULL;

	pInterface->SetCurrentModel(pNewModel);
}


//#################################################################################
// CLASS EDT_ModelsByType
//#################################################################################

/*===========================================================================
*		Description:	constructor 
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------

*		Revision date:                                                                                          Author:         
*=========================================================================*/
EDT_ModelsByType::EDT_ModelsByType (CString csTypeName, CPA_ObjectDLLBase *p_oDLL, CWnd *pParentDialog, CBitmapList *pBitmapList)
{
	// parameters
	m_pDLLObject = p_oDLL;
	m_csTypeName = csTypeName;
	// init list models
	fn_vInitModelView(pParentDialog, pBitmapList);
}


EDT_ModelsByType::~EDT_ModelsByType (void)
{
	m_stListModels.RemoveAll();
}


/*===========================================================================
*		Description:	reinit 
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                                                                          Author:         
*=========================================================================*/
void EDT_ModelsByType::fn_vReinit (void)
{
	EDT_Model	*pModel;
	long		lIndex;

	// remove all models
	while (m_stListModels.GetCount() > 0)
	{
		pModel = m_stListModels.RemoveTail();
		// update model view
		lIndex = m_pModelView->m_fn_lFindName(-1, pModel->m_csName);
		m_pModelView->m_fn_bDeleteItem(lIndex);
	}
}

/*===========================================================================
*		Description:	create model view
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                                                                          Author:         
*=========================================================================*/
void EDT_ModelsByType::fn_vInitModelView (CWnd *pParentDialog, CBitmapList *pBitmapList)
{
	// create model view
	m_pModelView = new EDT_ModelView;
	m_pModelView->Create(NULL, "", WS_BORDER|WS_VISIBLE|WS_CHILD/*AFX_WS_DEFAULT_VIEW*/, CRect(0,0,0,0), pParentDialog, AFX_IDW_PANE_FIRST, NULL);
	// set display type in list of models
	m_pModelView->m_fn_vSetBitmapList(pBitmapList);	
	m_pModelView->m_fn_vChangeDisplayType(C_cDisplayBitmapAndText);
	// init items
	fn_vFillModelView();
}

/*===========================================================================
*		Description:	fill model view
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------

*		Revision date:                                                                                          Author:         
*=========================================================================*/
void EDT_ModelsByType::fn_vFillModelView (void)
{
	EDT_Model			*pElem;
	POSITION			pos;

	for (pElem = m_stListModels.GetHeadElement(pos); pElem;
		 pElem = m_stListModels.GetNextElement(pos))
		m_pModelView->m_fn_iAddItem(pElem->m_csName, pElem->m_iBitmapPos, (long) pElem);
}

/*===========================================================================
*		Description:	Get the model with the given name 
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		
*---------------------------------------------------------------------------
*		Revision date:                     Author:         
*=========================================================================*/
EDT_Model * EDT_ModelsByType::GetModelWithName (CString csName)
{
	EDT_Model *pModel;
	POSITION  pos;

	for (pModel = m_stListModels.GetHeadElement(pos); pModel;
		 pModel = m_stListModels.GetNextElement(pos))
	{
		if (pModel->m_csName == csName)
			return pModel;
	}

	// model was not found
	return NULL;
}

/*===========================================================================
*		Description:	Add model to the list
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		
*---------------------------------------------------------------------------
*		Revision date:                     Author:         
*=========================================================================*/
void EDT_ModelsByType::fn_vAddANewModel (EDT_Model *pNewModel)
{
	// add model to the list
	m_stListModels.AddTail(pNewModel);
	// update model view
	m_pModelView->m_fn_iAddItem(pNewModel->m_csName, pNewModel->m_iBitmapPos, (long) pNewModel);
}

/*===========================================================================
*		Description:	remove model from the list
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		
*---------------------------------------------------------------------------
*		Revision date:                     Author:         
*=========================================================================*/
void EDT_ModelsByType::fn_vRemoveAModel (EDT_Model *pOldModel)
{
	POSITION	pos;
	long		lIndex;
	
	// Remove it from the list
	pos = m_stListModels.Find(pOldModel);
	if (pos)
		m_stListModels.RemoveAt(pos);
	// update model view
	lIndex = m_pModelView->m_fn_lFindName(-1, pOldModel->m_csName);
	m_pModelView->m_fn_bDeleteItem(lIndex);
}

#endif // ACTIVE_EDITOR