/*=========================================================================
*
* CPAHieEd.cpp :  CPA_HierarchyEditor : Implementation file.
*
*
* Version			1.0
* Creation date
* Revision date
*
* Shaitan
*=======================================================================*/
#include "stdafx.h"

#include "itf/customid.h"
#ifdef ACTIVE_EDITOR
#include "acp_base.h"

#include "itf/CPAHieEd.hpp"
#include "itf/CPAInter.hpp"
#include "itf/CPAMWorl.hpp"

#include "itf/CPAFileO.hpp"

//#################################################################################
// INITS
//#################################################################################

/*===========================================================================
	Creation
=========================================================================*/
void CPA_HierarchyEditor::Create (void)
{
	CString types[] = {C_szSuperObjectTypeName, C_szFileTypeName};

	// reachable object list
	fn_vRegisterObjectsType(types,2);
}

//#################################################################################
//	FOCUS
//#################################################################################

/*===========================================================================
	Called just before engine loop
=========================================================================*/
void CPA_HierarchyEditor::fn_vBeforeEngine()
{
	// update menu
	GetMainWorld()->GetFRMBase()->fn_vUpdateCurrentEditor(NULL);
	// update state
	GetInterface()->SetInitialState(FALSE);
}


/*===========================================================================
	Update editor hierarchy after engine loop
=========================================================================*/
void CPA_HierarchyEditor::fn_vBeforeEditor()
{

}

//#################################################################################
// EDITOR
//#################################################################################

/*===========================================================================
	Activate default editor
=========================================================================*/
void CPA_HierarchyEditor::fn_vOnActivateEditor (CPA_List<CPA_BaseObject> *pParams, BOOL bBackActivated)
{
	CPA_SuperObject	*pEdObj;
	CPA_BaseObject	*pElem;
	POSITION		pos;
	
	// if there is a list, select corresponding super-objects
	if (pParams)
	{
		// cancel current selection
		GetInterface()->fn_vCancelCurrentSelection(FALSE);
		// select parameters
		for (pElem = pParams->GetHeadElement(pos); pElem;
			 pElem = pParams->GetNextElement(pos))
		{
			// check if this is a super-object
			if (pElem->GetType() == C_szSuperObjectTypeName)
			{
				// if possible, select it
				pEdObj = (CPA_SuperObject *) pElem;
				if (GetInterface()->fn_bCanSelect(pEdObj) == C_Accept)
					GetInterface()->fn_vAddSelectedObject(pEdObj, FALSE);
			}
		}
	}
}
	
/*===========================================================================
	Close default editor
=========================================================================*/
void CPA_HierarchyEditor::fn_vOnCloseEditor (void)
{
	fn_vCancelAllModes();
}


//#################################################################################
// SUPER-OBJECT
//#################################################################################

/*===========================================================================
	GetEditor SO from engine SO
=========================================================================*/
CPA_SuperObject	* CPA_HierarchyEditor::GetEditorObject	(HIE_tdxHandleToSuperObject hSupObj)
{
	CPA_SuperObject *pElem;
	POSITION		pos;

	// test the structure
	for (pElem = M_GetWorld()->GetListAllObjects()->GetHeadElement(pos); pElem;
		 pElem = M_GetWorld()->GetListAllObjects()->GetNextElement(pos))
	{
		if (pElem->GetStruct() == hSupObj)
			return pElem;
	}

	// object was not found
	return NULL;
}

/*===========================================================================
	Create new super-object (from engine struct)
=========================================================================*/
CPA_SuperObject * CPA_HierarchyEditor::GetNewSuperObject (HIE_tdxHandleToSuperObject hEngineSO, 
														  tdeSaveStatus eStatus, tdeTypeSO eTypeSO, 
														  CString csName, CString csEngineFileName)
{
	CPA_SuperObject *pSupObj;

	// create super-object
	pSupObj = new CPA_SuperObject(this, hEngineSO, eStatus, eTypeSO);
	// name
	if (pSupObj->fn_eRename(csName) != E_mc_None)
		pSupObj->SetDefaultUniqueName();

	return pSupObj;
}

/*===========================================================================
	Create new super-object 
=========================================================================*/
CPA_SuperObject * CPA_HierarchyEditor::GetNewSuperObject (tdeSaveStatus eStatus, tdeTypeSO eTypeSO, 
														  CString csName, CString csEngineFileName)
{
	CPA_SuperObject *pSupObj;

	// create super-object
	pSupObj = new CPA_SuperObject(this, eStatus, eTypeSO);
	// name
	if (pSupObj->fn_eRename(csName) != E_mc_None)
		pSupObj->SetDefaultUniqueName();

	return pSupObj;
}


/*===========================================================================
	Copy super-object
=========================================================================*/
CPA_SuperObject * CPA_HierarchyEditor::GetCopyOfSuperObject	(CPA_SuperObject *pModel)
{
	CPA_SuperObject *pSupObj;

	// copy super-object
	pSupObj = new CPA_SuperObject(*pModel);
	// set new name
	pSupObj->SetDefaultUniqueName();

	return pSupObj;
}

//#################################################################################
// FILE-OBJECT
//#################################################################################

/*===========================================================================
	Get or load file object
=========================================================================*/
CPA_FileObject * CPA_HierarchyEditor::GetFileObject (CString csFileName)
{
	CPA_FileObject	*pFileObject;

	// get corresponding prefixed name
	csFileName = GetInterface()->GetPrefixedName(csFileName);
	CPA_FileObject::sm_vUniformizeSlashes(csFileName);
	// check if file object already exists
	pFileObject = (CPA_FileObject *) GetBaseObject(csFileName, C_szFileTypeName, NO_CRITERIA, E_rc_AllObjects);
	// if necessary, create it
	if (!pFileObject)
		pFileObject =  new CPA_FileObject(this, csFileName);

	return pFileObject;
}


//ANNECY Shaitan EmptySectors 25/02/98
BOOL CPA_HierarchyEditor::fn_bCanBeParent (CPA_SuperObject *pParent, tdstPosition *p_stPosition, tdeTypeSO eTypeSO, tdeSaveStatus eStatus)
{
	return FALSE;
}
//ENDANNECY Shaitan EmptySectors

#endif // ACTIVE_EDITOR
