// DiagView.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRBVw.hpp"

#include "EdIRBVwA.hpp"
#include "EdIRBVwD.hpp"
#include "EdIRBVwX.hpp"
#include "EdIRBVwR.hpp"
#include "EdIRBeha.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRBELs.hpp"
#include "EdIRFrm.hpp"
#include "EdIRRVw.hpp"
#include "EdIRBtVw.hpp"
#include "EdIRBNam.hpp"
#include "EdIRPrtB.hpp"
#include "EdIRPrtF.hpp"
#include "EdIRStrg.hpp"
#include "EdIR2Eng.hpp"
#include "EdIRRes.hm"
#include "EdIRIRD.hpp"
#include "EdIRGlob.hpp"

#include "ai_intf.hpp"

#include "incTUT.h"

// BEGIN CPA2 Cristi Petrescu 98-03-
#include "EdIRMacL.hpp"
extern CPA_EdIR_MacroList							g_clMacroList;
// END CPA2 Cristi Petrescu 98-03-

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define C_WIDTH		2000
#define C_HEIGHT	4000

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_DiagView
/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CPA_EdIR_DiagView, CFormView)

BEGIN_MESSAGE_MAP(CPA_EdIR_DiagView, CFormView)
	//{{AFX_MSG_MAP(CPA_EdIR_DiagView)
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_RBUTTONUP()
	ON_WM_KEYUP()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_PAINT()
	ON_LBN_SELCHANGE(IDD_IR_BEHAVIOUR_LIST, OnSelChangeBehaviourList)
	ON_WM_HELPINFO()
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_IR_DIAGVIEW_ADD_BEHAVIOUR,m_fn_vOnAddBehaviour)
	ON_COMMAND(ID_IR_DIAGVIEW_DEL_ALL_BEHAVIOUR,m_fn_vOnDeleteAllBehaviours)
END_MESSAGE_MAP()

/**********************************************************************************/
CPA_EdIR_DiagView::CPA_EdIR_DiagView()
	: CFormView(CPA_EdIR_DiagView::IDD)
{
	//{{AFX_DATA_INIT(CPA_EdIR_DiagView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pclOldActiveBehaviour			=NULL;
	m_pclOldCopiedMainBehaviour	=NULL;
	m_pclListOfBehaviours				=NULL;
	m_tdeBehaviourModeView			=DiagView;
	m_bCtrlKeyIsPressed					=FALSE;
	m_bPrintCurrentBehaviour		=FALSE;
	m_bPrintAllBehaviours				=FALSE;
	m_bPrintAllIA								=FALSE;
	m_bPrintIsFinished					=TRUE;
	m_bHasBeenModified					=FALSE;
//ANNECY CB
	m_bSpecialCB = FALSE;
//END
}

/**********************************************************************************/
CPA_EdIR_DiagView::~CPA_EdIR_DiagView()
{
	//In order to not delete the objects in the list because they will be deleted later
	m_clOldCopiedMainBehaviourList.RemoveAll();
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vAModelHasBeenRemoved()
{
	//List View Mode
	CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
	if(tdeMode==ListView)
	{
		long	lNbBehaviours=m_clBehaviourListBox.GetCount();

		for(long lCounter=0;lCounter<lNbBehaviours;lCounter++)
			m_clBehaviourListBox.DeleteString(0);
	}

	m_pclOldActiveBehaviour=NULL;
	m_pclListOfBehaviours=NULL;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vUpdateListOfBehaviour(CPA_EdIR_MainBehaviourList *pclListOfBehaviours, BOOL Disp)
{
	CPA_EdIR_MainBehaviourList *rememb;

	//Hides old behaviour
	if(m_pclListOfBehaviours && Disp)
	{
		POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();

		while(pos!=NULL)
		{
			CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

			//pclMainBehaviour->m_pclBehaviour should be !=NULL
			if(pclMainBehaviour->m_pclBehaviour!=NULL)
			{
				pclMainBehaviour->m_pclBehaviour->m_fn_vActivate(FALSE);
				pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_HIDE);
			}

			if(pclMainBehaviour->m_pclBehaviour==m_pclOldActiveBehaviour)
				m_pclListOfBehaviours->m_pclOldActiveMainBehaviour=pclMainBehaviour;
		}

		if(m_pclListOfBehaviours->IsEmpty())
			m_pclListOfBehaviours->m_pclOldActiveMainBehaviour=NULL;
	}

	if(Disp == FALSE)
		rememb = m_pclListOfBehaviours;

	m_pclListOfBehaviours=pclListOfBehaviours;

	//Adds declarations' behaviour in Declaration mode (if no one is present)
	CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	BOOL								bDeclMode=(p_clBtView->m_fn_tdeGetEditState()==EditDecl);
	if(bDeclMode &&
	   m_pclListOfBehaviours->IsEmpty())
	{
		BOOL bIsLoadingWorld = g_pclAIInterface->GetInterface()->fn_bIsLoadingWorld();
		g_pclAIInterface->GetInterface()->SetLoadingWorld(TRUE);
		
		//Adds DesignerVariables declaration
		CPA_EdIR_Behaviour			*pclBehaviour=m_fn_pclAddOneBehaviour(g_c_csDsgVarName,CRect(15,15,85,45),"",bDeclMode,0);
		CPA_EdIR_MainBehaviour	*pclMainBehaviour=new CPA_EdIR_MainBehaviour(m_fn_pclGetActorModel(),g_c_csDsgVarName,CRect(15,15,85,45),"",pclBehaviour);
		pclBehaviour->m_fn_vSetMainBehaviour(pclMainBehaviour);

		m_pclListOfBehaviours->AddTail(pclMainBehaviour);

		//Adds Macros declaration
		pclBehaviour=m_fn_pclAddOneBehaviour(g_c_csMacroName,CRect(15,60,85,90),"",bDeclMode,0);
		pclMainBehaviour=new CPA_EdIR_MainBehaviour(m_fn_pclGetActorModel(),g_c_csMacroName,CRect(15,60,85,90),"",pclBehaviour);
		pclBehaviour->m_fn_vSetMainBehaviour(pclMainBehaviour);

		m_pclListOfBehaviours->AddTail(pclMainBehaviour);
	
		g_pclAIInterface->GetInterface()->SetLoadingWorld(bIsLoadingWorld);
	}

	if(Disp == FALSE)
		m_pclListOfBehaviours = rememb;

	//Shows new behaviour
	if(Disp)
	{
/*		POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
		while(pos!=NULL)
			m_pclListOfBehaviours->GetNext(pos)->m_pclBehaviour->ShowWindow(SW_SHOW);*/

		if(m_pclListOfBehaviours->m_pclOldActiveMainBehaviour)
			m_fn_vOldActiveBehaviourHasChanged(m_pclListOfBehaviours->m_pclOldActiveMainBehaviour->m_pclBehaviour);
		else
			m_fn_vOldActiveBehaviourHasChanged(NULL);

		InvalidateRect(NULL);
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vChangeViewModeOfBehaviours(tdeBehaviourModeView tdeMode)
{
	m_tdeBehaviourModeView=tdeMode;

	switch(tdeMode)
	{
		case ListView:
			{
				//Sets scroll size
				SetScrollSizes(MM_TEXT,CSize(0,0));

				//Resets the listbox
				if(m_clBehaviourListBox.GetCount()!=0)
					while(m_clBehaviourListBox.DeleteString(0)!=0);

				//Initializes the listbox
				POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
				long		lCounter=0;
				long		lSelectedIndex=-1;
				while(pos!=NULL)
				{
					CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

					//Hides buttons
					pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_HIDE);

					//Adds behaviour in listbox
					m_clBehaviourListBox.AddString(pclMainBehaviour->m_pclBehaviour->m_fn_csGetName());
					int	iIndex=m_clBehaviourListBox.FindStringExact(0,pclMainBehaviour->m_pclBehaviour->m_fn_csGetName());
					m_clBehaviourListBox.SetItemDataPtr(iIndex,pclMainBehaviour->m_pclBehaviour);

					if(pclMainBehaviour->m_pclBehaviour==m_pclOldActiveBehaviour)
						lSelectedIndex=lCounter;

					lCounter++;
				}

				//Gets view size
				CRect	cr;
				GetClientRect(cr);

				//Shows list
				m_clBehaviourListBox.ShowWindow(SW_SHOW);
				m_clBehaviourListBox.MoveWindow(0,0,cr.Width(),cr.Height());
				m_clBehaviourListBox.EnableWindow(TRUE);

				//Selects behaviour in listbox
				if(lSelectedIndex!=-1)
				{
					m_clBehaviourListBox.SetCurSel(lSelectedIndex);
					m_fn_vOldActiveBehaviourHasChanged(m_pclOldActiveBehaviour);
				}
			}
			break;

		case DiagView:
			{
				//Hides list
				m_clBehaviourListBox.ShowWindow(SW_HIDE);
				m_clBehaviourListBox.EnableWindow(FALSE);

				//Shows buttons
				POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
				while(pos!=NULL)
					m_pclListOfBehaviours->GetNext(pos)->m_pclBehaviour->ShowWindow(SW_SHOW);

				//Sets scroll size
				SetScrollSizes(MM_TEXT,CSize(C_WIDTH,C_HEIGHT));

				//Centers the view on the selected behaviour
				if(m_pclOldActiveBehaviour)
				{
					CRect	cr;
					m_pclOldActiveBehaviour->GetWindowRect(cr);
					ScreenToClient(cr);

					CRect	crViewSize;
					GetClientRect(crViewSize);

					CPoint	cpPosition=GetScrollPosition();
					cr.OffsetRect(cpPosition);

					ScrollToPosition(CPoint(cr.CenterPoint()-crViewSize.CenterPoint()));
				}
			}
			break;

		default:
			break;
	}

	InvalidateRect(NULL);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vCreateNewBehaviour(tdeBehaviourModeView tdeMode)
{
	if(m_pclListOfBehaviours)
	{
		CPA_EdIR_DlgNewBehaviourName	clNewName("");

		if(clNewName.DoModal()==IDOK)
		{
			//Checks if the name doesn't exist
			if(!m_fn_bNameAlreadyExists(clNewName.m_csNewName))
			{
				CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
				BOOL								bDeclMode=(p_clBtView->m_fn_tdeGetEditState()==EditDecl);

				//Debug
				if(bDeclMode==TRUE)
				{
					AfxMessageBox("[DEV] : CPA_EdIR_DiagView::m_fn_vCreateNewBehaviour : bDeclMode=TRUE");

					return;
				}
				//End Debug

				//Computes the new position
				POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
				long		lMaxHeight=0;
				while(pos!=NULL)
				{
					CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

					CRect	cr;
					pclMainBehaviour->m_pclBehaviour->GetWindowRect(cr);
					ScreenToClient(cr);

					if(lMaxHeight<cr.bottom)
						lMaxHeight=cr.bottom;
				}

				CRect		crRect=CRect(15,lMaxHeight+15,60,lMaxHeight+45);

				CPA_EdIR_Behaviour	*pclBehaviour;
				
				if(tdeMode==ListView)
					pclBehaviour=m_fn_pclAddOneBehaviour(clNewName.m_csNewName,crRect,"",bDeclMode,0);
				else
					pclBehaviour=m_fn_pclAddOneBehaviour(clNewName.m_csNewName,crRect,"",bDeclMode,WS_VISIBLE);

				CPA_EdIR_MainBehaviour	*pclMainBehaviour=new CPA_EdIR_MainBehaviour(m_fn_pclGetActorModel(),clNewName.m_csNewName,crRect,"",pclBehaviour);
				pclBehaviour->m_fn_vSetMainBehaviour(pclMainBehaviour);

				CPA_EdIR_UndoCreateBehaviour	*pclUndoCreateBehaviour;

				pclUndoCreateBehaviour=new CPA_EdIR_UndoCreateBehaviour(this,pclMainBehaviour,m_pclListOfBehaviours);

				m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoCreateBehaviour);
			}
			else
				MessageBox("This name already exists !");
		}
		else
			MessageBox("Invalid name !");
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vCopyBehaviour(CPA_EdIR_MainBehaviour *pclMainBehaviourSrc,CString csName)
{
	if(m_pclListOfBehaviours)
	{
		if(csName=="")
		{
			CPA_EdIR_DlgNewBehaviourName	clNewName("");

			if(clNewName.DoModal()==IDOK)
				csName=clNewName.m_csNewName;
			else
				MessageBox("Invalid name !");
		}

		//Checks if the name doesn't exist
		if(!m_fn_bNameAlreadyExists(csName))
		{
			CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
			BOOL				bDeclMode=(p_clBtView->m_fn_tdeGetEditState()==EditDecl);

			//Computes the new position
			CRect		crRect=pclMainBehaviourSrc->m_pclBehaviour->m_fn_crGetBoundingBox();;
			crRect.OffsetRect(20,20);

			//Gets src rules
			CString	csRules=pclMainBehaviourSrc->m_pclBehaviour->m_fn_csGetRules();

			CPA_EdIR_Behaviour	*pclBehaviour;
				
			if(p_clBtView->m_fn_tdeGetViewMode()==ListView)
				pclBehaviour=m_fn_pclAddOneBehaviour(csName,crRect,csRules,bDeclMode,0);
			else
				pclBehaviour=m_fn_pclAddOneBehaviour(csName,crRect,csRules,bDeclMode,WS_VISIBLE);

			CPA_EdIR_MainBehaviour	*pclMainBehaviour=new CPA_EdIR_MainBehaviour(m_fn_pclGetActorModel(),csName,crRect,csRules,pclBehaviour);
			pclBehaviour->m_fn_vSetMainBehaviour(pclMainBehaviour);

			CPA_EdIR_UndoCreateBehaviour	*pclUndoCreateBehaviour;

			pclUndoCreateBehaviour=new CPA_EdIR_UndoCreateBehaviour(this,pclMainBehaviour,m_pclListOfBehaviours);

			m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoCreateBehaviour);
		}
		else
			MessageBox("This name already exists !");
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_DiagView)
	DDX_Control(pDX, IDD_IR_BEHAVIOUR_LIST, m_clBehaviourListBox);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DiagView diagnostics

#ifdef _DEBUG
/**********************************************************************************/
void CPA_EdIR_DiagView::AssertValid() const
{
	CFormView::AssertValid();
}

/**********************************************************************************/
void CPA_EdIR_DiagView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DiagView message handlers

/**********************************************************************************/
void CPA_EdIR_DiagView::OnInitialUpdate() 
{
	HINSTANCE hOldInstance=AfxGetResourceHandle();
	AfxSetResourceHandle(g_stAIIdentity.hModule);

	CFormView::OnInitialUpdate();

	SetScrollSizes(MM_TEXT,CSize(C_WIDTH,C_HEIGHT));

	//////////////
	//////////////
	//Registers for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vRegisterControlID(IDD_IR_BEHAVIOUR_LIST,"EdIR_BehaviourView_ListBox",TUT_e_ListBox);
	//End of Register for TUT Module
	//////////////

	AfxSetResourceHandle(hOldInstance);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnDestroy()
{
	//////////////
	//////////////
	//UnRegisters for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vUnregisterControlID(IDD_IR_BEHAVIOUR_LIST);
	//End of UnRegister for TUT Module
	//////////////

	CFormView::OnDestroy();
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnPaint() 
{
	CFormView::OnPaint();

	if(m_pclListOfBehaviours)
	{
		//Only in diagview mode
		CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
		tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
		if(tdeMode==DiagView)
		{
			POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();

			while(pos!=NULL)
			{
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

				//pclMainBehaviour->m_pclBehaviour should be !=NULL
				if(pclMainBehaviour->m_pclBehaviour!=NULL)
					pclMainBehaviour->m_pclBehaviour->m_fn_vDrawDependencies();
			}
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnSize(UINT nType,int cx,int cy) 
{
	CFormView::OnSize(nType,cx,cy);

	if(m_tdeBehaviourModeView==ListView)
	{
		//Gets view size
		CRect	cr;
		GetClientRect(cr);

		//Resizes list
		m_clBehaviourListBox.MoveWindow(0,0,cr.Width(),cr.Height());
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	if(m_pclListOfBehaviours && p_clBtView->m_fn_tdeGetEditState()!=EditDecl)
	{
		CRect	r;
		GetWindowRect(&r);
		
		CMenu	Menu;
		Menu.LoadMenu(IDR_IR_MENU_POPUP_DIAGVIEW);
		
		CMenu	*SousMenu=Menu.GetSubMenu(0);

		if(m_pclListOfBehaviours->IsEmpty())
			SousMenu->EnableMenuItem(ID_IR_DIAGVIEW_DEL_ALL_BEHAVIOUR,MF_GRAYED|MF_BYCOMMAND);

		//////////////
		//////////////
		//Registers for TUT Module
		TUT_M_vGetTutDll();
		
		TUT_M_vRegisterMenu(m_hWnd,SousMenu->m_hMenu,r.left+point.x,r.top+point.y);
		//End of Register for TUT Module
		//////////////

		SousMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,r.left+point.x,r.top+point.y,this);
		SousMenu->DestroyMenu();
		Menu.DestroyMenu();
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CFormView::OnMouseMove(nFlags, point);

	m_clOldMousePos=point;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if(nChar==VK_CONTROL)
		m_bCtrlKeyIsPressed=TRUE;
//ROMTEAM F2Key(Ionut Grozea 25/03/98)
	if(nChar==113)
		M_GetMainWnd()->SendMessage(WM_COMMAND, CPA_IDCB_SWAPTOEDITORS, 0);
//ENDROMTEAM F2Key(Ionut Grozea 25/03/98)
	
	CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnKeyUp(UINT nChar,UINT nRepCnt,UINT nFlags) 
{
	if(m_pclListOfBehaviours)
	{
		//Undo Redo
		if(m_bCtrlKeyIsPressed && nChar=='Z')
		{
			if(m_pclListOfBehaviours->m_clEditManager.CanUndo())
				m_pclListOfBehaviours->m_clEditManager.Undo();
			else
			{
				MessageBox("No Undo Available");

				m_fn_vSetModified(FALSE);
			}
		}

		if(m_bCtrlKeyIsPressed && nChar=='A')
		{
			if(m_pclListOfBehaviours->m_clEditManager.CanRedo())
				m_pclListOfBehaviours->m_clEditManager.Redo();
			else
				MessageBox("No Redo Available");
		}

		//Copy Paste
		if(m_bCtrlKeyIsPressed && nChar=='C')
		{
			LPINT	piIndex=new int[m_pclListOfBehaviours->GetCount()];
			int		iNbItems=m_clBehaviourListBox.GetSelItems(m_pclListOfBehaviours->GetCount(),piIndex);

			m_clOldCopiedMainBehaviourList.RemoveAll();

			if(iNbItems>1)
			{
				for(int iIndex=0;iIndex<iNbItems;iIndex++)
				{
					CPA_EdIR_Behaviour			*pclBehaviour=(CPA_EdIR_Behaviour *)m_clBehaviourListBox.GetItemDataPtr(piIndex[iIndex]);
					CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_fn_pclGetMainBehaviour(pclBehaviour);

					m_clOldCopiedMainBehaviourList.AddTail(pclMainBehaviour);
				}
			}
			else
				m_pclOldCopiedMainBehaviour=m_fn_pclGetMainBehaviour(m_pclOldActiveBehaviour);
		}

		if(m_bCtrlKeyIsPressed && nChar=='V')
		{
			CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
			if(p_clBtView->m_fn_tdeGetEditState()!=EditDecl)
			{
				if(m_clOldCopiedMainBehaviourList.GetCount()!=0)
				{
					POSITION	pos=m_clOldCopiedMainBehaviourList.GetHeadPosition();
					while(pos!=NULL)
					{
						CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_clOldCopiedMainBehaviourList.GetNext(pos);
						m_fn_vCopyBehaviour(pclMainBehaviour,pclMainBehaviour->GetName()+"_Copy");
					}
				}
				else
				if(m_pclOldCopiedMainBehaviour!=NULL)
				{
					m_fn_vCopyBehaviour(m_pclOldCopiedMainBehaviour);
				}
			}
			else
				AfxMessageBox("You can't paste behaviour(s) in Declaration Mode !!");
		}

		if(nChar==VK_INSERT)
		{
			CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
			if(p_clBtView->m_fn_tdeGetEditState()!=EditDecl)
			{
				CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
				m_fn_vCreateNewBehaviour(p_clBtView->m_fn_tdeGetViewMode());
			}
			else
				AfxMessageBox("You can't create behaviour in Declaration Mode !!");
		}

		if(nChar==VK_DELETE)
		{
			CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
			if(p_clBtView->m_fn_tdeGetEditState()!=EditDecl)
				m_fn_vDeleteBehaviour(NULL);
			else
				AfxMessageBox("You can't delete behaviour in Declaration Mode !!");
		}
	}

	if(nChar==VK_CONTROL)
		m_bCtrlKeyIsPressed=FALSE;

	CFormView::OnKeyUp(nChar, nRepCnt, nFlags);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SetFocus();
	
	CFormView::OnLButtonDown(nFlags, point);
}


/**********************************************************************************/
void CPA_EdIR_DiagView::OnSelChangeBehaviourList() 
{
	CPA_EdIR_Behaviour	*pclBehaviour;

	pclBehaviour=(CPA_EdIR_Behaviour *)m_clBehaviourListBox.GetItemDataPtr(m_clBehaviourListBox.GetCurSel());

	m_fn_vOldActiveBehaviourHasChanged(pclBehaviour,FALSE);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vOnAddBehaviour()
{
	if(m_pclListOfBehaviours)
	{
		CPA_EdIR_DlgNewBehaviourName	clNewName("");

		if(clNewName.DoModal()==IDOK)
		{
			//Checks if the name doesn't exist
			if(!m_fn_bNameAlreadyExists(clNewName.m_csNewName))
			{
				//Adds one behaviour
				CPA_EdIR_ButtonView	*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
				BOOL				bDeclMode=(p_clBtView->m_fn_tdeGetEditState()==EditDecl);
				CRect				crRect=CRect(m_clOldMousePos.x,m_clOldMousePos.y,m_clOldMousePos.x+45,m_clOldMousePos.y+30);

				CPA_EdIR_Behaviour			*pclBehaviour=m_fn_pclAddOneBehaviour(clNewName.m_csNewName,crRect,"",bDeclMode,WS_VISIBLE);
				CPA_EdIR_MainBehaviour	*pclMainBehaviour=new CPA_EdIR_MainBehaviour(m_fn_pclGetActorModel(),clNewName.m_csNewName,crRect,"",pclBehaviour);
				pclBehaviour->m_fn_vSetMainBehaviour(pclMainBehaviour);

				CPA_EdIR_UndoCreateBehaviour	*pclUndoCreateBehaviour;

				pclUndoCreateBehaviour=new CPA_EdIR_UndoCreateBehaviour(this,pclMainBehaviour,m_pclListOfBehaviours);

				m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoCreateBehaviour);
			}
			else
				MessageBox("This name already exists !");
		}
		else
			MessageBox("Invalid name !");
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vOnDeleteAllBehaviours()
{
	m_fn_vDeleteAllBehaviours();
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vOldActiveBehaviourHasChanged(CPA_EdIR_Behaviour *p_clNewActiveBehaviour,BOOL bDeselect)
{
	if(m_pclOldActiveBehaviour)
		m_pclOldActiveBehaviour->m_fn_vActivate(FALSE);

	m_pclOldActiveBehaviour=p_clNewActiveBehaviour;
	if(m_pclOldActiveBehaviour)
	{
		m_pclOldActiveBehaviour->m_fn_vActivate(TRUE);
		//In ListView Mode
		CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
		tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
		if(tdeMode==ListView)
		{
			//Looks for behaviour entry in the list
			long	lIndex=m_fn_lGetIndex(m_pclOldActiveBehaviour);

			if(lIndex!=-1)
				m_clBehaviourListBox.SetCurSel(lIndex,bDeselect);
		}
	}

	//Draws BreakPoint
	((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetEdRulesView()->InvalidateRect(NULL);
// Shaitan => Macros to subroutines
	((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView()->m_fn_vEnableButtons(TRUE);
// End Shaitan => Macros to subroutines
}

/**********************************************************************************/
CPA_EdIR_Behaviour *CPA_EdIR_DiagView::m_fn_pclGetActiveBehaviour()
{
	return m_pclOldActiveBehaviour;
}

/**********************************************************************************/
CPA_EdIR_Behaviour *CPA_EdIR_DiagView::m_fn_pclAddOneBehaviour(CString csName,
																															 CRect crRect,
																															 CString csText,
																															 BOOL bDeclarationMode,
																															 DWORD dwVisibleStyle)
{
	CPA_EdIR_View				*p_clEdIAView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetEdRulesView();
	CPA_EdIR_Behaviour	*p_clBehaviour=new CPA_EdIR_Behaviour(p_clEdIAView,bDeclarationMode);

	p_clBehaviour->Create(csName,WS_CHILD|dwVisibleStyle,crRect,this,1);
//ANNECY CB
	if(m_bSpecialCB == FALSE)
//END
	m_fn_vOldActiveBehaviourHasChanged(p_clBehaviour);

	p_clBehaviour->m_fn_vSetRules(csText);

	return p_clBehaviour;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vDeleteBehaviour(CPA_EdIR_Behaviour *p_clBehaviour)
{
	if(m_pclListOfBehaviours)
	{
		if(p_clBehaviour==NULL)
			p_clBehaviour=m_pclOldActiveBehaviour;

		if(p_clBehaviour!=NULL)
		{
			CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_fn_pclGetMainBehaviour(p_clBehaviour);

			if(pclMainBehaviour)
			{
				// BEGIN CPA2 Cristi Petrescu 98-03-
				// if it is macro, delete it from the global macro list
				/*
				CPA_EdIR_Frame *pclIRMainFrame = g_pclAIInterface -> m_pclIRMainFrame;
				if (pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_tdeGetEditState () == EditMacro)
				{
					// is a macro
					POSITION pos = g_clMacroList . GetHeadPosition();
					POSITION oldpos = pos;
					while (pos != NULL)
					{
						CPA_EdIR_Macro *pclMacro = g_clMacroList . GetNext(pos);
						CString csMacroName = pclMainBehaviour->GetName ();
						csMacroName . MakeLower ();
						if (csMacroName == pclMacro -> m_fn_csGetMacroName ())
						{
							g_clMacroList . RemoveAt (oldpos);
							break;
						}
						oldpos = pos;
					}
				}
				*/
				// END CPA2 Cristi Petrescu 98-03-
				if(m_pclOldCopiedMainBehaviour==pclMainBehaviour)
					m_pclOldCopiedMainBehaviour=NULL;

				CPA_EdIR_UndoDeleteBehaviour	*pclUndoDeleteBehaviour;

				pclUndoDeleteBehaviour=new CPA_EdIR_UndoDeleteBehaviour(this,pclMainBehaviour,m_pclListOfBehaviours);

				m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoDeleteBehaviour);
			}
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vDeleteAllBehaviours()
{
	if(m_pclListOfBehaviours)
	{
		CPA_EdIR_UndoDeleteAllBehaviours	*pclUndoDeleteAllBehaviours;

		pclUndoDeleteAllBehaviours=new CPA_EdIR_UndoDeleteAllBehaviours(this,m_pclListOfBehaviours);

		m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoDeleteAllBehaviours);
	}
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bGetFirstBehaviour(CString &csName,CRect &crRect,CString &csRules)
{
	m_posCurrent=m_pclListOfBehaviours->GetHeadPosition();

	if(m_posCurrent!=NULL)
	{
		CPA_EdIR_Behaviour	*p_clBehaviour=m_pclListOfBehaviours->GetAt(m_posCurrent)->m_pclBehaviour;

		csName=p_clBehaviour->m_fn_csGetName();
		crRect=p_clBehaviour->m_fn_crGetBoundingBox();
		csRules=p_clBehaviour->m_fn_csGetRules();
	}

	return (m_posCurrent!=NULL);
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bGetNextBehaviour(CString &csName,CRect &crRect,CString &csRules)
{
	m_pclListOfBehaviours->GetNext(m_posCurrent);

	if(m_posCurrent!=NULL)
	{
		CPA_EdIR_Behaviour	*p_clBehaviour=m_pclListOfBehaviours->GetAt(m_posCurrent)->m_pclBehaviour;

		csName=p_clBehaviour->m_fn_csGetName();
		crRect=p_clBehaviour->m_fn_crGetBoundingBox();
		csRules=p_clBehaviour->m_fn_csGetRules();
	}

	return (m_posCurrent!=NULL);
}

/**********************************************************************************/
CPA_EdIR_Behaviour *CPA_EdIR_DiagView::m_fn_pclGetBehaviour(CString csName)
{
	POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();

	while(pos!=NULL)
	{
		CPA_EdIR_Behaviour	*p_clBehaviour=m_pclListOfBehaviours->GetAt(pos)->m_pclBehaviour;

		if(p_clBehaviour->m_fn_csGetName()==csName)
			return p_clBehaviour;

		m_pclListOfBehaviours->GetNext(pos);
	}

	return NULL;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vClipControls(CClientDC *cdc)
{
	CRect	cr;
	GetClientRect(cr);

	cdc->SetBoundsRect(cr,DCB_DISABLE);

	POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();

	while(pos!=NULL)
	{
		CPA_EdIR_Behaviour	*p_clBehaviour=m_pclListOfBehaviours->GetAt(pos)->m_pclBehaviour;

		CRect	cr;
		p_clBehaviour->GetWindowRect(cr);
		ScreenToClient(cr);
		cdc->ExcludeClipRect(cr);

		m_pclListOfBehaviours->GetNext(pos);
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vUpdateDependencies()
{
	//Deletes old dependencies
	POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();

	while(pos!=NULL)
		m_pclListOfBehaviours->GetNext(pos)->m_pclBehaviour->m_clListOfDependentBehaviours.RemoveAll();

	//Updates dependencies
	pos=m_pclListOfBehaviours->GetHeadPosition();

	while(pos!=NULL)
		m_pclListOfBehaviours->GetNext(pos)->m_pclBehaviour->m_fn_vUpdateDependencies();
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bNameAlreadyExists(CString csName)
{
	CPA_EdIR_MainBehaviour	clDummyBehaviour(m_fn_pclGetActorModel());

	return (clDummyBehaviour.fn_eCheckUnicity(csName)!=E_mc_None);
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bIsInCurrentList(CPA_EdIR_MainBehaviour *pclMainBehaviour)
{
	if(m_pclListOfBehaviours)
		return (m_pclListOfBehaviours->Find(pclMainBehaviour)!=NULL);
	else
		return FALSE;
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	::WinHelp(m_hWnd,g_pclAIInterface->m_csBrainHelpFileNameAndPath,HELP_CONTEXT,HID_BEHAVIOURS_ZONE);

	return CFormView::OnHelpInfo(pHelpInfo);
}

/**********************************************************************************/
CPA_EdIR_MainBehaviour	*CPA_EdIR_DiagView::m_fn_pclGetMainBehaviour(CPA_EdIR_Behaviour *pclBehaviour)
{
	return pclBehaviour->m_fn_pclGetMainBehaviour();
}

/**********************************************************************************/
long CPA_EdIR_DiagView::m_fn_lGetIndex(CPA_EdIR_Behaviour *pclBehaviour)
{
	//Looks for behaviour entry in the list
	long	lIndex=-1;

	for(long lCounter=0;lIndex==-1 && lCounter<m_clBehaviourListBox.GetCount();lCounter++)
	{
		if(m_clBehaviourListBox.GetItemDataPtr(lCounter)==pclBehaviour)
			lIndex=lCounter;
	}

	return lIndex;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vAddOneBehaviourInList(CPA_EdIR_MainBehaviour *pclMainBehaviour)
{
	m_pclListOfBehaviours->AddTail(pclMainBehaviour);
	m_fn_vOldActiveBehaviourHasChanged(pclMainBehaviour->m_pclBehaviour);

	//Sets name
	pclMainBehaviour->m_pclBehaviour->SetWindowText(pclMainBehaviour->GetName());

	//Updates button width
	pclMainBehaviour->m_pclBehaviour->m_fn_vUpdateButtonWidth();

	//List View Mode
	CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
	if(tdeMode==ListView)
	{
		//Adds behaviour in listbox
		m_clBehaviourListBox.AddString(pclMainBehaviour->GetName());
		int	iIndex=m_clBehaviourListBox.FindStringExact(0,pclMainBehaviour->GetName());
		m_clBehaviourListBox.SetItemDataPtr(iIndex,pclMainBehaviour->m_pclBehaviour);

		//Selects the new behaviour
		m_clBehaviourListBox.SetCurSel(iIndex);
	}
	else
	{
		//Shows it
		pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_SHOW);

		//Centers on the new behaviour
		CRect	cr;
		pclMainBehaviour->m_pclBehaviour->GetWindowRect(cr);
		ScreenToClient(cr);

		CRect	crViewSize;
		GetClientRect(crViewSize);

		CPoint	cpPosition=GetScrollPosition();
		cr.OffsetRect(cpPosition);

		ScrollToPosition(CPoint(cr.CenterPoint()-crViewSize.CenterPoint()));
	}
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bDeleteBehaviourFromList(CPA_EdIR_MainBehaviour *pclMainBehaviour)
{
	CPA_EdIR_Behaviour *p_clBehaviour=pclMainBehaviour->m_pclBehaviour;

	if(p_clBehaviour==NULL)
		p_clBehaviour=m_pclOldActiveBehaviour;

	if(p_clBehaviour)
	{
		if(g_oCoherenceManager.m_fn_bIsChild(pclMainBehaviour)==FALSE)
		{
			POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
			BOOL		bFound=FALSE;

			while(pos!=NULL && !bFound)
			{
				CPA_EdIR_Behaviour *p_clBehav=m_pclListOfBehaviours->GetAt(pos)->m_pclBehaviour;

				if(p_clBehav==p_clBehaviour)
				{
					m_pclListOfBehaviours->RemoveAt(pos);

					bFound=TRUE;
				}
				else
					m_pclListOfBehaviours->GetNext(pos);
			}

			BOOL	bSelectedIsDeleted=(m_pclOldActiveBehaviour==p_clBehaviour);
			if(bSelectedIsDeleted)
			{
				if(!m_pclListOfBehaviours->IsEmpty())
					m_fn_vOldActiveBehaviourHasChanged(m_pclListOfBehaviours->GetHead()->m_pclBehaviour);
				else
					m_fn_vOldActiveBehaviourHasChanged(NULL);
			}

			pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_HIDE);

			//In ListView Mode
			CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
			tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
			if(tdeMode==ListView)
			{
				//Looks for behaviour entry in the list
				long	lIndex=m_fn_lGetIndex(pclMainBehaviour->m_pclBehaviour);

				//Delete entry for this behaviour
				if(lIndex!=-1)
					m_clBehaviourListBox.DeleteString(lIndex);

				//Selects another behaviour
				if(bSelectedIsDeleted &&
				   m_clBehaviourListBox.GetCount()>0)
				{
					ASSERT(m_pclOldActiveBehaviour!=NULL);

					long	lIndex=m_fn_lGetIndex(m_pclOldActiveBehaviour);

					ASSERT(lIndex!=-1);

					m_clBehaviourListBox.SetCurSel(lIndex);
				}
			}
			else
			{
				if(m_pclOldActiveBehaviour)
				{
					//Centers on the new selected behaviour
					CRect	cr;
					m_pclOldActiveBehaviour->GetWindowRect(cr);
					ScreenToClient(cr);

					CRect	crViewSize;
					GetClientRect(crViewSize);

					CPoint	cpPosition=GetScrollPosition();
					cr.OffsetRect(cpPosition);

					ScrollToPosition(CPoint(cr.CenterPoint()-crViewSize.CenterPoint()));
				}
			}

			m_fn_vUpdateDependencies();

			return TRUE;
		}
		else
		{
			AfxMessageBox("Can't delete this behaviour, it is used !!");

			return FALSE;
		}
	}

	return FALSE;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vAddAllBehavioursInList(CPA_EdIR_MainBehaviourList *pclMainBehaviourList)
{
	CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();

	//Adds all behaviours
	POSITION	pos=pclMainBehaviourList->GetHeadPosition();
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour *pclMainBehaviour=pclMainBehaviourList->GetNext(pos);

		m_pclListOfBehaviours->AddTail(pclMainBehaviour);

		//List View Mode
		if(tdeMode==ListView)
		{
			//Adds behaviour in listbox
			m_clBehaviourListBox.AddString(pclMainBehaviour->GetName());
			int	iIndex=m_clBehaviourListBox.FindStringExact(0,pclMainBehaviour->GetName());
			m_clBehaviourListBox.SetItemDataPtr(iIndex,pclMainBehaviour->m_pclBehaviour);
		}
		else
			pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_SHOW);
	}

	if(m_pclListOfBehaviours->GetCount()>0)
	{
		//Selects first behaviour
		m_fn_vOldActiveBehaviourHasChanged(m_pclListOfBehaviours->GetHead()->m_pclBehaviour);
	}

	//List View Mode
	if(tdeMode==ListView)
	{
		if(m_clBehaviourListBox.GetCount()>0)
		{
			//Selects the first behaviour
			ASSERT(m_pclOldActiveBehaviour!=NULL);

			long	lIndex=m_fn_lGetIndex(m_pclOldActiveBehaviour);

			ASSERT(lIndex!=-1);

			m_clBehaviourListBox.SetCurSel(lIndex);
		}
	}
	else
	{
		if(m_pclListOfBehaviours->GetCount()>0)
		{
			//Centers on the first behaviour
			CPA_EdIR_MainBehaviour *pclMainBehaviour=m_pclListOfBehaviours->GetHead();

			CRect	cr;
			pclMainBehaviour->m_pclBehaviour->GetWindowRect(cr);
			ScreenToClient(cr);

			CRect	crViewSize;
			GetClientRect(crViewSize);

			CPoint	cpPosition=GetScrollPosition();
			cr.OffsetRect(cpPosition);

			ScrollToPosition(CPoint(cr.CenterPoint()-crViewSize.CenterPoint()));
		}
	}
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bDeleteAllBehavioursFromList()
{
	//First check if we can delete all
	POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
	BOOL		bDeleteAllIsOk=TRUE;
	while(pos!=NULL)
	{
		CPA_EdIR_MainBehaviour *p_clMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

		if(g_oCoherenceManager.m_fn_bIsChild(p_clMainBehaviour))
		{
			bDeleteAllIsOk=FALSE;

			CString	csMsg;
			csMsg="Can't delete all behaviours\n";
			csMsg+="The behaviour called "+p_clMainBehaviour->GetName()+" is used";
			AfxMessageBox(csMsg);
		}
	}

	if(bDeleteAllIsOk)
	{
		//Hides all behaviours
		POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
		while(pos!=NULL)
		{
			CPA_EdIR_MainBehaviour *pclMainBehaviour=m_pclListOfBehaviours->GetNext(pos);

			pclMainBehaviour->m_pclBehaviour->ShowWindow(SW_HIDE);
		}

		//Deletes all behaviours
		m_pclListOfBehaviours->RemoveAll();

		//List View Mode
		CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
		tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
		if(tdeMode==ListView)
		{
			long	lNbBehaviours=m_clBehaviourListBox.GetCount();

			for(long lCounter=0;lCounter<lNbBehaviours;lCounter++)
				m_clBehaviourListBox.DeleteString(0);
		}
			
		m_fn_vOldActiveBehaviourHasChanged(NULL);

		return TRUE;
	}
	else
		return FALSE;
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bRenameBehaviour(CPA_EdIR_MainBehaviour *pclMainBehaviour,
											  CString csName)
{
	// BEGIN CPA2 Cristi Petrescu 98-03-
	// CString csMacroName = pclMainBehaviour->GetName ();
	// END CPA2 Cristi Petrescu 98-03-

	//Changes name
	if(pclMainBehaviour->fn_eRename(csName)!=E_mc_None)
	{
		AfxMessageBox("Can't rename this behaviour, this name is already used !!");

		return FALSE;
	}

	CPA_EdIR_Behaviour	*pclBehaviour=pclMainBehaviour->m_pclBehaviour;
	csName=pclMainBehaviour->GetName();
	
	pclBehaviour->SetWindowText(csName);

	// BEGIN CPA2 Cristi Petrescu 98-03-
	// if it is macro, rename it in the global macro list
	/*
	CPA_EdIR_Frame *pclIRMainFrame = g_pclAIInterface -> m_pclIRMainFrame;
	if (pclIRMainFrame -> m_fn_pclGetButtonView () -> m_fn_tdeGetEditState () == EditMacro)
	{
		// is a macro
		POSITION pos = g_clMacroList . GetHeadPosition();
		while (pos != NULL)
		{
			CPA_EdIR_Macro *pclMacro = g_clMacroList . GetNext(pos);
			csMacroName . MakeLower ();
			if (csMacroName == pclMacro -> m_fn_csGetMacroName ())
			{
				pclMacro -> m_fn_vRenameMacro (csName);
				break;
			}
		}
	}
	*/
	// END CPA2 Cristi Petrescu 98-03-

	//Updates button width
	pclBehaviour->m_fn_vUpdateButtonWidth();

	//ListView Mode
	CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
	tdeBehaviourModeView	tdeMode=p_clBtView->m_fn_tdeGetViewMode();
	if(tdeMode==ListView)
	{
		//Looks for behaviour entry in the list
		long	lIndex=m_fn_lGetIndex(pclBehaviour);

		if(lIndex!=-1)
		{
			m_clBehaviourListBox.DeleteString(lIndex);
			m_clBehaviourListBox.InsertString(lIndex,csName);
			int	iIndex=m_clBehaviourListBox.FindStringExact(0,csName);
			m_clBehaviourListBox.SetItemDataPtr(iIndex,pclBehaviour);
			m_clBehaviourListBox.SetCurSel(iIndex);
		}
	}

	return TRUE;
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vAskForRenamingBehaviour(CPA_EdIR_Behaviour *pclBehaviour,
													  CString csOldName,
													  CString csNewName)
{
	//Looks for main behaviour
	CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_fn_pclGetMainBehaviour(pclBehaviour);

	if(pclMainBehaviour)
	{
		//Ask for renaming behaviour
		CPA_EdIR_UndoRenameBehaviour	*pclUndoRenameBehaviour;

		pclUndoRenameBehaviour=new CPA_EdIR_UndoRenameBehaviour(this,pclMainBehaviour,m_pclListOfBehaviours,csOldName,csNewName);

		m_pclListOfBehaviours->m_clEditManager.AskFor(pclUndoRenameBehaviour);
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vCheckBehaviour(enum tdeTypeText_ eTypeText)
{
	LPINT	piIndex=new int[m_pclListOfBehaviours->GetCount()];
	int		iNbItems=m_clBehaviourListBox.GetSelItems(m_pclListOfBehaviours->GetCount(),piIndex);

	if(iNbItems>1)
	{
		for(int iIndex=0;iIndex<iNbItems;iIndex++)
		{
			CPA_EdIR_Behaviour			*pclBehaviour=(CPA_EdIR_Behaviour *)m_clBehaviourListBox.GetItemDataPtr(piIndex[iIndex]);
			CPA_EdIR_MainBehaviour	*pclMainBehaviour=m_fn_pclGetMainBehaviour(pclBehaviour);

			CString	csMsgBase="Checking "+pclMainBehaviour->GetName()+" ... ";
			fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);

			CString	csMsg=csMsgBase;
			if(pclBehaviour->m_fn_bCheckIA(eTypeText))
			{
				csMsg+="OK :-)";
				fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
			}
			else
			{
				csMsg+="KO :-(";
				fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
			}
		}
	}
	else
	{
		if(m_pclOldActiveBehaviour!=NULL)
		{
			CString	csMsgBase="Checking "+m_pclOldActiveBehaviour->m_fn_pclGetMainBehaviour()->GetName()+" ... ";
			fn_vDisplayInfoInStatusBar(csMsgBase,C_STATUS_NORMAL);

			CString	csMsg=csMsgBase;
			if(m_pclOldActiveBehaviour->m_fn_bCheckIA(eTypeText))
			{
				csMsg+="OK :-)";
				fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_NORMAL);
			}
			else
			{
				csMsg+="KO :-(";
				fn_vDisplayInfoInStatusBar(csMsg,C_STATUS_ERROR);
			}
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vPrint()
{
	if(m_bPrintIsFinished)
	{
		CPA_EdIR_ButtonView		*p_clBtView=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetButtonView();
		tdeEditState					tdeState=p_clBtView->m_fn_tdeGetEditState();
		CString								csBehaviourListName;
		switch(tdeState)
		{
			case EditIntell:
				csBehaviourListName="All intelligence behaviours";
				break;

			case EditReflex:
				csBehaviourListName="All reflex behaviours";
				break;


			case EditDecl:
				csBehaviourListName="All declarations";
				break;

			// BEGIN CPA2 Cristi Petrescu 98-03-
			case EditMacro:
				csBehaviourListName="All macros";
				break;
			// END CPA2 Cristi Petrescu 98-03-
		}

		CPA_EdIR_PrintDialog	clPrintDialog(csBehaviourListName,"Times New Roman",50);

		if(clPrintDialog.DoModal()==IDOK)
		{
			m_csFontName=clPrintDialog.m_csFontName;
			m_lFontSize=clPrintDialog.m_lFontSize;

			//Computes line height
			CFont	clPrintFont;
			clPrintFont.CreateFont(m_lFontSize,0,0,0,FW_NORMAL,
															FALSE,FALSE,FALSE,DEFAULT_CHARSET,
															OUT_STRING_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
															DEFAULT_PITCH,LPCTSTR(m_csFontName));

			CClientDC	cdc(this);
			
			cdc.SelectObject(&clPrintFont);
			m_lLineHeight=cdc.GetTextExtent("A").cy;

			//
			m_bPrintCurrentBehaviour=clPrintDialog.m_bCurrentBehaviour;
			m_bPrintAllBehaviours=clPrintDialog.m_bAllBehaviours;
			m_bPrintAllIA=clPrintDialog.m_bAllIA;
			m_bOneBehaviourOnAPage=clPrintDialog.m_bOneBehaviourOnAPage;

			m_bPrintIsFinished=FALSE;

			if(clPrintDialog.m_bIsPreview)
			{
				CPA_EdIR_PrintFrame *pclPrintFrame=new CPA_EdIR_PrintFrame;

				pclPrintFrame->m_fn_bCreate(this,"Print Preview",CRect(50,50,500,500));
			}
			else
				CFormView::OnFilePrint();
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vPrint(CDC* pDC, CPrintInfo* pInfo)
{
	OnPrint(pDC,pInfo);
}

/**********************************************************************************/
BOOL CPA_EdIR_DiagView::OnPreparePrinting(CPrintInfo* pInfo) 
{
	pInfo->m_bDirect=TRUE;
	BOOL	bRes=DoPreparePrinting(pInfo);
	
	return (CFormView::OnPreparePrinting(pInfo) && bRes);
}


/**********************************************************************************/
void CPA_EdIR_DiagView::OnBeginPrinting(CDC* pDC,CPrintInfo* pInfo) 
{
	pInfo->m_bDocObject=FALSE;

	UINT												nMinPage=1;
	UINT												nMaxPage=65535;
	CPA_EdIR_MainBehaviourList	clBehaviourListToPrint;
	if(m_bPrintCurrentBehaviour)
	{
		if(m_pclOldActiveBehaviour)
			clBehaviourListToPrint.AddTail(m_fn_pclGetMainBehaviour(m_pclOldActiveBehaviour));
	}
	else
	if(m_bPrintAllBehaviours)
	{
		POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(m_pclListOfBehaviours->GetNext(pos));
	}
	else
	if(m_bPrintAllIA)
	{
		//Adds Intelligence
		CPA_EdIR_Frame	*pclFrame=(CPA_EdIR_Frame *)GetParentFrame();
		CPA_EdIR_MainBehaviourList	*pclListOfMainBehaviours=pclFrame->m_fn_pclGetIntelligenceBehaviours();
		POSITION	pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		//Adds Reflex
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetReflexBehaviours();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		//Adds Declarations
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetDeclarations();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		// BEGIN CPA2 Cristi Petrescu 98-03-
		//Adds Macros
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetMacros();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));
		// END CPA2 Cristi Petrescu 98-03-

		// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
		//Adds Subr
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetSubrs();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));
		// END ROMTEAM CPA2 Cristi Petrescu 98-04-
	}

	//Substracts 5 mm to increase printer's margins
	long	lDPI=pDC->GetDeviceCaps(LOGPIXELSY);
	long	lFiveMmInDPI=long(double(5*lDPI)/25.4);
	nMaxPage=clBehaviourListToPrint.m_fn_lGetNbPages(m_lLineHeight,
																									 pDC->GetDeviceCaps(VERTRES)-lFiveMmInDPI,
																									 m_bOneBehaviourOnAPage);
	pInfo->SetMinPage(nMinPage);
	pInfo->SetMaxPage(nMaxPage);

	
	CFormView::OnBeginPrinting(pDC, pInfo);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vBeginPrinting(CDC *pDC,CPrintInfo *pInfo)
{
	OnBeginPrinting(pDC,pInfo);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	CPA_EdIR_MainBehaviourList	clBehaviourListToPrint;
	if(m_bPrintCurrentBehaviour)
	{
		if(m_pclOldActiveBehaviour)
			clBehaviourListToPrint.AddTail(m_fn_pclGetMainBehaviour(m_pclOldActiveBehaviour));
	}
	else
	if(m_bPrintAllBehaviours)
	{
		POSITION	pos=m_pclListOfBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(m_pclListOfBehaviours->GetNext(pos));
	}
	else
	if(m_bPrintAllIA)
	{
		//Adds Intelligence
		CPA_EdIR_Frame	*pclFrame=(CPA_EdIR_Frame *)GetParentFrame();
		CPA_EdIR_MainBehaviourList	*pclListOfMainBehaviours=pclFrame->m_fn_pclGetIntelligenceBehaviours();
		POSITION	pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		//Adds Reflex
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetReflexBehaviours();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		//Adds Declarations
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetDeclarations();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));

		// BEGIN CPA2 Cristi Petrescu 98-03-
		//Adds Macros
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetMacros();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));
		// END CPA2 Cristi Petrescu 98-03-

		// BEGIN ROMTEAM CPA2 Cristi Petrescu 98-04-
		//Adds Subrs
		pclListOfMainBehaviours=pclFrame->m_fn_pclGetSubrs();
		pos=pclListOfMainBehaviours->GetHeadPosition();
		while(pos!=NULL)
			clBehaviourListToPrint.AddTail(pclListOfMainBehaviours->GetNext(pos));
		// END ROMTEAM CPA2 Cristi Petrescu 98-04-
	}

	CFont	clPrintFont;
	clPrintFont.CreateFont(m_lFontSize,0,0,0,FW_NORMAL,
													FALSE,FALSE,FALSE,DEFAULT_CHARSET,
													OUT_STRING_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,
													DEFAULT_PITCH,LPCTSTR(m_csFontName));

	pDC->SelectObject(&clPrintFont);

	//Substracts 5 mm to increase printer's margins
	long	lDPI=pDC->GetDeviceCaps(LOGPIXELSY);
	long	lFiveMmInDPI=long(double(5*lDPI)/25.4);
	clBehaviourListToPrint.m_fn_vPrint(m_lLineHeight,
																		 pDC->GetDeviceCaps(VERTRES)-lFiveMmInDPI,
																		 m_bOneBehaviourOnAPage,
																		 pInfo->m_nCurPage,
																		 pDC,pInfo);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) 
{
	m_bPrintIsFinished=TRUE;

	CFormView::OnEndPrinting(pDC, pInfo);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView) 
{
	m_bPrintIsFinished=TRUE;

	CFormView::OnEndPrintPreview(pDC, pInfo, point, pView);
}

/**********************************************************************************/
void CPA_EdIR_DiagView::m_fn_vEndPrintPreview()
{
	m_bPrintIsFinished=TRUE;
}

/****************************************************************************/
BOOL CPA_EdIR_DiagView::m_fn_bHasBeenModified()
{
	BOOL	bHasBeenModified=FALSE;

	if(m_pclListOfBehaviours)
		bHasBeenModified=m_pclListOfBehaviours->m_fn_bHasBeenModified();
		
	return (m_bHasBeenModified||bHasBeenModified);
}

/****************************************************************************/
void CPA_EdIR_DiagView::m_fn_vSetModified(BOOL bIsModified)
{
	if(m_pclListOfBehaviours)
		m_pclListOfBehaviours->m_fn_vSetModified(bIsModified);

	m_bHasBeenModified=bIsModified;
}

/****************************************************************************/
CPA_Actor *CPA_EdIR_DiagView::m_fn_pclGetActorModel()
{
	return ((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetModel();
}

#endif //D_ED_IR_ACTIVE
