/*(c) Ubi Studios 1996*/
/* See Vincent Greco for any comment or question*/
 
#ifndef __LSTEDLST_HPP__
#define __LSTEDLST_HPP__
 
#include "LstEdMot.Hpp"
/*#include "ErrHie.h"*/
 
 
 
#define TemplateParam	\
template<class EditorClass, class tdxHandleToStruct, class tdxPointerToAnchorStruct>
 
 
#define ElementClass	\
CPA_EdElement<EditorClass, tdxHandleToStruct, tdxPointerToAnchorStruct>
 
#define ListClass	\
CPA_EdList<EditorClass,tdxHandleToStruct,tdxPointerToAnchorStruct>
 
#define ListBaseClass	\
CPA_List<ElementClass >
/*Be careful the space character must not be removed (class<class<param> > is OK,  class<class<param>> is Wrong) */
 
 
 
/****************************************/
/*assumes that tdxHandleToStruct has a tdxPointerToAnchorStruct * pAnchor member*/
/*assumes that tdxHandleToStruct has a tdxHandleToStruct * hNextBrother member*/
/*assumes that tdxHandleToStruct has a tdxHandleToStruct * hPrevBrother member*/
 
 
 
TemplateParam class CPA_EdList;
 
 
TemplateParam
class CPA_EdElement : virtual public CPA_EdMot<tdxHandleToStruct>
{
private:
	ListClass * m_pList;
 
public:
	CPA_EdElement(void){m_pList=NULL;}/*use SetList(NULL) ???*/
	virtual ~CPA_EdElement(void){}
 
public:
	/*accessors to private member*/
	ListClass *GetList(void){return m_pList;}
	void SetList(ListClass * _pList, BOOL ModifyEngine=TRUE)
	{
		m_pList=_pList;
		if(ModifyEngine)
		{
			if(_pList)
			{
				LST2_M_StaticGetFather(GetStruct())=_pList->GetEngineAnchor();
			}
			else
			{
				LST2_M_StaticGetFather(GetStruct())=NULL;
			}
 
		}
	}
 
public:
	tdxHandleToStruct GetPrevStruct(void);
	tdxHandleToStruct GetNextStruct(void);
	void Isolate(BOOL ModifyEngine=TRUE);
/*	void InsertBetween(tdxHandleToStruct pPrev, tdxHandleToStruct pNext);*/
 
};
 
 
TemplateParam
class CPA_EdList : public ListBaseClass 
{
private:
	tdxPointerToAnchorStruct m_pAnchor;
 
public:
    CPA_EdList() {}
    virtual ~CPA_EdList(void){}
 
public:
	/*accessors to private member*/
	tdxPointerToAnchorStruct GetEngineAnchor() {return m_pAnchor;}
	void SetEngineAnchor(tdxPointerToAnchorStruct  _pAnchor){m_pAnchor=_pAnchor;}
 
public:
	/*redefined functions*/
	POSITION InsertAfter(POSITION position, ElementClass* newElement, BOOL ModifyEngine=TRUE);
	POSITION InsertBefore(POSITION position, ElementClass* newElement, BOOL ModifyEngine=TRUE);
	POSITION AddHead(ElementClass* newElement, BOOL ModifyEngine=TRUE);
	POSITION AddTail(ElementClass* newElement, BOOL ModifyEngine=TRUE);
	void RemoveAt(POSITION position, BOOL ModifyEngine=TRUE);
	EditorClass* RemoveHead(BOOL ModifyEngine=TRUE);
	EditorClass* RemoveTail(BOOL ModifyEngine=TRUE);
	void RemoveAll(BOOL ModifyEngine=TRUE);
 
	/*simple type conversions*/
	EditorClass* GetAt(POSITION position) {return (EditorClass*)ListBaseClass::GetAt(position);}
	EditorClass* GetNext(POSITION& position) {return (EditorClass*)ListBaseClass::GetNext(position);}
	EditorClass* GetPrev(POSITION& position) {return (EditorClass*)ListBaseClass::GetPrev(position);}
	EditorClass* GetHead() {return (EditorClass*)ListBaseClass::GetHead();}
	EditorClass* GetTail() {return (EditorClass*)ListBaseClass::GetTail();}
	
	EditorClass* GetHeadElement(POSITION& rPosition) const {return (EditorClass*)ListBaseClass::GetHeadElement(rPosition);}
    EditorClass* GetTailElement(POSITION& rPosition) const {return (EditorClass*)ListBaseClass::GetTailElement(rPosition);}
	EditorClass* GetNextElement(POSITION& rPosition) const {return (EditorClass*)ListBaseClass::GetNextElement(rPosition);}
	EditorClass* GetPrevElement(POSITION& rPosition) const {return (EditorClass*)ListBaseClass::GetPrevElement(rPosition);}
 
	EditorClass* GetHeadElement(void) const {return (EditorClass*)ListBaseClass::GetHeadElement();}
    EditorClass* GetTailElement(void) const {return (EditorClass*)ListBaseClass::GetTailElement();}
	EditorClass* GetNextElement(ElementClass * pElement) const {return (EditorClass*)ListBaseClass::GetNextElement(pElement);}
	EditorClass* GetPrevElement(ElementClass * pElement) const {return (EditorClass*)ListBaseClass::GetPrevElement(pElement);}
};
 
 
/*------------------------------------------------------------------*/
 
 
TemplateParam tdxHandleToStruct ElementClass::GetPrevStruct(void)
{
	tdxHandleToStruct hStruct=  GetStruct();
	return LST2_M_StaticGetPrevElement(hStruct);
}
 
TemplateParam tdxHandleToStruct ElementClass::GetNextStruct(void)
{
	tdxHandleToStruct hStruct=  GetStruct();
	return LST2_M_StaticGetNextElement(hStruct);
}
 
TemplateParam void ElementClass::Isolate(BOOL ModifyEngine)/*can be called as many times as you want.*/
{
	if(GetList())
	{
		POSITION P=GetList()->Find(this);
		GetList()->RemoveAt(P, ModifyEngine);
/*		SetList(NULL);//modifyEngine ???*/
       SetList(NULL,ModifyEngine);
	}
}
 
 
/*-----------------------------------------*/
 
 
TemplateParam POSITION ListClass::InsertAfter(POSITION position, ElementClass* newElement, BOOL ModifyEngine)
{
	if(!position) return AddHead(newElement, ModifyEngine);
	ElementClass* pPrevObj=GetAt(position);/*can't be NULL*/
	newElement->Isolate(ModifyEngine);
	if(ModifyEngine)
	{
		tdxHandleToStruct pPrev=pPrevObj->GetStruct();
		tdxHandleToStruct pNext=pPrevObj->GetNextStruct();
		LST2_M_StaticInsertBetween(newElement->GetStruct(),pPrev,pNext);
	}	
	newElement->SetList(this, ModifyEngine);
	return ListBaseClass::InsertAfter(position, newElement);
}
 
 
TemplateParam POSITION ListClass::InsertBefore(POSITION position, ElementClass* newElement, BOOL ModifyEngine)
{
	if(!position) return AddTail(newElement, ModifyEngine);
	ElementClass* pNextObj=GetAt(position);/*can't be NULL*/
	newElement->Isolate(ModifyEngine);
	if(ModifyEngine)
	{
		tdxHandleToStruct pNext=pNextObj->GetStruct();
		tdxHandleToStruct pPrev=pNextObj->GetPrevStruct();
		LST2_M_StaticInsertBetween(newElement->GetStruct(),pPrev,pNext);
    }
	newElement->SetList(this, ModifyEngine);
	return ListBaseClass::InsertBefore(position, newElement);
}
 
TemplateParam POSITION ListClass::AddHead(ElementClass* newElement, BOOL ModifyEngine )
{
	newElement->Isolate(ModifyEngine);
	if(ModifyEngine)
	{
		LST2_M_StaticAddHead(GetEngineAnchor(),newElement->GetStruct());	
    }
	newElement->SetList(this, ModifyEngine);
	return ListBaseClass::AddHead(newElement);
}
 
 
TemplateParam POSITION ListClass::AddTail(ElementClass* newElement, BOOL ModifyEngine )
{
	newElement->Isolate(ModifyEngine);
	if(ModifyEngine)
	{
		LST2_M_StaticAddTail(GetEngineAnchor(),newElement->GetStruct());	
    }
	newElement->SetList(this, ModifyEngine);
	return ListBaseClass::AddTail(newElement);
}
 
 
TemplateParam void ListClass::RemoveAt(POSITION position, BOOL ModifyEngine)
{
	ElementClass *pOldObj = GetAt(position);
	if(ModifyEngine)
	{
		LST2_M_StaticIsolate(pOldObj->GetStruct());
	}
	pOldObj->SetList(NULL, ModifyEngine);
/*	pOldObj->SetAnchor(NULL, ModifyEngine);*/
	ListBaseClass::RemoveAt(position);
}
 
TemplateParam EditorClass* ListClass::RemoveHead(BOOL ModifyEngine)
{
	ElementClass *pOldObj = ListBaseClass::RemoveHead();
	if(ModifyEngine)
	{
		LST2_M_StaticIsolate(pOldObj->GetStruct());
	}
	pOldObj->SetList(NULL, ModifyEngine);
	/*pOldObj->SetAnchor(NULL, ModifyEngine);*/
	return (EditorClass*)pOldObj;
}
 
 
TemplateParam EditorClass* ListClass::RemoveTail(BOOL ModifyEngine)
{
	ElementClass *pOldObj = ListBaseClass::RemoveTail();
	if(ModifyEngine)
	{
		LST2_M_StaticIsolate(pOldObj->GetStruct());
	}
	pOldObj->SetList(NULL, ModifyEngine);
	return (EditorClass*)pOldObj;
}
 
TemplateParam void ListClass::RemoveAll(BOOL ModifyEngine)/*not tested !!!*/
{
	if(ModifyEngine)
	{
		LST2_M_StaticInitAnchor(GetEngineAnchor());
	}
	ListBaseClass::RemoveAll();
}
 
/*-----------------------------------------*/
 
 
 
#endif /*__LSTEDLST_HPP__*/
