// EdIRREd.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRREd.hpp"

#include "EdIRREdA.hpp"
#include "EdIRREdD.hpp"
#include "EdIRREdP.hpp"
#include "EdIRRBox.hpp"
#include "EdIRRVw.hpp"
#include "EdIRFrm.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRIRD.hpp"
#include "EdIRStrg.hpp"
#include "EdIRIAGl.hpp"
#include "EdIRRes.hm"
#include "EdIRReplce.hpp"
#include "Edirbis.hpp"
#include "EdIRDVVw.hpp"
#include "incAI.h"

#include "edirwaw.hpp"
#include "edirdat.hpp"

#include "EdIRBVw.hpp"
#include "EdIRBeha.hpp"

#include "EdIRIAWd.hpp"

#include "EdIRSele.hpp"

#include "ai_intf.hpp"

#include "../tempgrp/tid/inc/debugcom.h"

#include "EDACModl.hpp"

#include "x:\cpa\main\inc\_EditID.h"

#include "_Actors.hpp"

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

#define M_TRANSLATE_KEYWORD_SCRIPT_TO_EDITOR(sz)	fn_csGetKeyWordEditorNameFromId(fn_lGetKeyWordIdFromEditorName(sz))
#define M_TRANSLATE_ACTION_SCRIPT_TO_EDITOR(sz)		fn_csGetMetaActionEditorNameFromId(fn_lGetMetaActionIdFromEditorName(sz))

#define M_ISALNUM(c)													(isalnum(c) || (c)=='_')
												
#define C_KEYWORDCOLOR													RGB(255,0,0)
#define C_ACTIONWORDCOLOR												RGB(10,125,10)
#define C_CONDITIONWORDCOLOR											RGB(0,0,255)
#define C_CONSTANTWORDCOLOR												RGB(255,125,0)
#define C_DEFAULTCOLOR													RGB(0,0,0)
#define C_COMMENTCOLOR													RGB(150,70,150)
#define C_MACROWORDCOLOR												RGB(255,0,255)
#define	C_CONSTANTCOLOR													RGB(0,100,100)

#define C_PAGEUP														VK_PRIOR
#define C_PAGEDOWN														VK_NEXT
#define C_ENTER															VK_RETURN
#define C_TAB															VK_TAB
#define C_BACKSPACE														VK_BACK
#define C_INSER															VK_INSERT
#define C_SUPPR															VK_DELETE
#define C_CTRL															VK_CONTROL
#define C_SHIFT															VK_SHIFT
#define C_ALTGR															VK_MENU
#define C_F9															VK_F9
#define C_F																70

#define C_L																76

#define C_COMMENT														190
#define C_DECOMMENT														191

#define C_F2															VK_F2
#define C_F3															VK_F3
#define C_F4															VK_F4
#define C_W																87
#define C_P																80

#define C_CTRLC															3
#define C_CTRLV															22
#define C_CTRLX															24
#define C_AUTOCOMPLETECHARDOWN											222
#define C_AUTOCOMPLETECHAR												178
#define C_CTRLZ															26
#define C_CTRLA															1

#define C_RETURN														"\xD\xA"

//See help about MapVirtualKey
#define C_TRANSLATE_VK2CHAR												2
#define C_TRANSLATE_CHARMASK											0x0000FFFF
 
// #define DAVE  [kwn]

BOOL fn_bIsValidCharacter (UINT nChar, BOOL bShiftPressed)
{
	if (isprint(nChar))
		return TRUE;

	// special character
	if (bShiftPressed)
	{
		switch (nChar)
		{
		// 'à' => '0'
		case 224:
			return TRUE;
		// 'ç' => '9'
		case 231:
			return TRUE;
		// 'è' => '7'
		case 232:
			return TRUE;
		// 'é' => '2'
		case 233:
			return TRUE;
		}
	}
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_EditCtrl
/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CPA_EdIR_EditCtrl, CRichEditCtrl)
	//{{AFX_MSG_MAP(CPA_EdIR_EditCtrl)
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_KEYUP()
	ON_WM_HELPINFO()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_IR_RULES_ADDWATCH, OnIrRulesAddwatch)
	ON_COMMAND(ID_IR_RULES_FINDREPLACE, OnIrRulesFindreplace)
	ON_COMMAND(ID_IR_RULES_NEXT, OnIrRulesNext)
	ON_COMMAND(ID_IR_RULES_PREVIOUS, OnIrRulesPrevious)
	ON_COMMAND(ID_IR_RULES_CHANGEFONT, OnIrChangeFont)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CStringList			gm_clKeyWords;
CStringList			gm_clActionWords;
CStringList			gm_clConditionWords;
CStringList			gm_clConstantWords;
CStringList			gm_clKnownWords;
static BOOL			gb_Already = FALSE;

/**********************************************************************************/
CPA_EdIR_EditCtrl::CPA_EdIR_EditCtrl()
{
	if(gb_Already == FALSE)
	{
		// Inserts known dsg var types
		long	lNbDsgVars=fn_lGetNbDsgVarTypes();
		for(long lDsgVarIndex=0;lDsgVarIndex<lNbDsgVars;lDsgVarIndex++)
		{
			CString	csDsgVarType=fn_csGetDsgVarTypeEditorNameFromId(lDsgVarIndex);

			gm_clKeyWords.AddTail(csDsgVarType);
			ASSERT(!csDsgVarType.IsEmpty());
		}

		// Inserts known keywords
		long	lNbKeyWords=fn_lGetNbKeyWords();
		for(long lKeyWordIndex=0;lKeyWordIndex<lNbKeyWords;lKeyWordIndex++)
		{
			CString	csKeyWord=fn_csGetKeyWordEditorNameFromId(lKeyWordIndex);

			gm_clKeyWords.AddTail(csKeyWord);
			ASSERT(!csKeyWord.IsEmpty());
		}

	gm_clKeyWords.AddTail("#define");
	gm_clKeyWords.AddTail("private");
	gm_clKeyWords.AddTail("public");

		// Adds special words
		if(g_pclAIInterface->M_GetMainWorld()->GetInterface()->fn_csGetCurrentLanguage()=="French")
		{
			gm_clKeyWords.AddTail("Macro");
			gm_clKeyWords.AddTail("Subroutine");
			gm_clKeyWords.AddTail("Vrai");
			gm_clKeyWords.AddTail("Faux");
			gm_clKeyWords.AddTail("BloquantMoteur");
			gm_clKeyWords.AddTail("BloquantRegle");
			gm_clKeyWords.AddTail("NonBloquant");
		}
		else
		{
			gm_clKeyWords.AddTail("Macro");
			gm_clKeyWords.AddTail("Subroutine");
			gm_clKeyWords.AddTail("True");
			gm_clKeyWords.AddTail("False");
			gm_clKeyWords.AddTail("StopEngine");
			gm_clKeyWords.AddTail("StopRule");
			gm_clKeyWords.AddTail("NonStop");
		}

		// Inserts known conditions
		long	lNbConditions=fn_lGetNbConditions();
		for(long lCondIndex=0;lCondIndex<lNbConditions;lCondIndex++)
		{
			CString	csCondition=fn_csGetConditionEditorNameFromId(lCondIndex);

			gm_clConditionWords.AddTail(csCondition);
			ASSERT(!csCondition.IsEmpty());
		}

		// Inserts known metaactions
		long	lNbMetaActions=fn_lGetNbMetaActions();
		for(long lMetaActIndex=0;lMetaActIndex<lNbMetaActions;lMetaActIndex++)
		{
			CString	csMetaAction=fn_csGetMetaActionEditorNameFromId(lMetaActIndex);

			gm_clActionWords.AddTail(csMetaAction);
			ASSERT(!csMetaAction.IsEmpty());
		}

		// Inserts known procedures
		long	lNbProcedures=fn_lGetNbProcedures();
		for(long lProcIndex=0;lProcIndex<lNbProcedures;lProcIndex++)
		{
			CString	csProcedure=fn_csGetProcedureEditorNameFromId(lProcIndex);

			gm_clActionWords.AddTail(csProcedure);
			ASSERT(!csProcedure.IsEmpty());
		}

		// Inserts known functions
		long	lNbFunctions=fn_lGetNbFunctions();
		for(long lFuncIndex=0;lFuncIndex<lNbFunctions;lFuncIndex++)
		{
			CString	csFunction=fn_csGetFunctionEditorNameFromId(lFuncIndex);

			gm_clActionWords.AddTail(csFunction);
			ASSERT(!csFunction.IsEmpty());
		}

		// Inserts known buttons
		for(unsigned long ulKeyButtonIndex=0;ulKeyButtonIndex<IPT_fn_ulNumberOfEntryAction();ulKeyButtonIndex++)
		{
			CString	csButton=IPT_fn_szEntryActionNumber(ulKeyButtonIndex);

			gm_clConstantWords.AddTail(csButton);
			ASSERT(!csButton.IsEmpty());
		}

		// Inserts known fields
		long	lNbFields=fn_lGetNbFields();
		for(long lFieldIndex=0;lFieldIndex<lNbFields;lFieldIndex++)
		{
			CString	csField=fn_csGetFieldEditorNameFromId(lFieldIndex);

			gm_clConstantWords.AddTail(csField);
			ASSERT(!csField.IsEmpty());
		}


    // Inserts known texts: scan the FON link table and get all keys
		{
			SCR_tdst_Link_Value *p_stTextEntry;
			SCR_tdst_Link_Table *p_stTextLinkTable = FON_fn_p_stGetLinkTable();
			unsigned int uiTextPos = 0;
			SCR_M_DyAr_GetNextElement(
				SCR_tdst_Link_Value,
				uiTextPos,
				p_stTextEntry,
				p_stTextLinkTable->stLinkArray
			);
			/* while there are entries in the list */
			while ( p_stTextEntry )
			{
				gm_clConstantWords.AddTail(SCR_M_p_sz_Link_GetKey(p_stTextEntry));

				uiTextPos ++;
				SCR_M_DyAr_GetNextElement(
					SCR_tdst_Link_Value,
					uiTextPos,
					p_stTextEntry,
					p_stTextLinkTable->stLinkArray
				);
			}
		}

		//Makes the KnownWords List
		//KeyWords
		POSITION	pos=gm_clKeyWords.GetHeadPosition();
		while(pos!=NULL)
			gm_clKnownWords.AddTail(gm_clKeyWords.GetNext(pos));

		//ConditionWords
		pos=gm_clConditionWords.GetHeadPosition();
		while(pos!=NULL)
			gm_clKnownWords.AddTail(gm_clConditionWords.GetNext(pos));

		//ActionWords
		pos=gm_clActionWords.GetHeadPosition();
		while(pos!=NULL)
			gm_clKnownWords.AddTail(gm_clActionWords.GetNext(pos));

		//ConstantWords
		pos=gm_clConstantWords.GetHeadPosition();
		while(pos!=NULL)
			gm_clKnownWords.AddTail(gm_clConstantWords.GetNext(pos));

		gb_Already = TRUE;
	}

	m_lOldCurrentCharIndex=0;
	m_lOldScrollPosition=0;
	m_csOldSelectedText="";
	m_bSingleChar1=FALSE;
	m_bSingleChar2=FALSE;
	m_bShiftKeyIsPressed=FALSE;
	m_bHasBeenModified=FALSE;
	mb_CanDefault = TRUE;
	m_lFirstSelectIndex = 0;
	m_lLastSelectIndex = 0;
	m_bSingleChar1=FALSE;
	m_bSingleChar2=FALSE;
	m_bSearchIsFinished=FALSE;

	m_bShiftKeyIsPressed=FALSE;
	m_bHasBeenModified=FALSE;

	m_bParentsSeleIAObj = FALSE;

	m_bRepaintParent = FALSE;
	m_csLastSearched = "";

	m_csFontType = "";
	m_nFontSize  = 0;
}

/**********************************************************************************/
CPA_EdIR_EditCtrl::~CPA_EdIR_EditCtrl()
{
}

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_EditCtrl message handlers

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::Create(DWORD dwStyle,const RECT& rect,CWnd* pParentWnd,UINT nID)
{
	static my_nID = 123456789;
	m_nID = my_nID ++;

	BOOL	bRes=CRichEditCtrl::Create(dwStyle,rect,pParentWnd,m_nID);

	if(bRes)
	{
		//Sets courrier font size 10
		SetFont(GetParent()->GetFont());

		//Gets font size
		CClientDC	cdc(GetParent());
		CFont		*p_clOldFont=cdc.SelectObject(GetParent()->GetFont());
		m_clFontSize=cdc.GetTextExtent("A");
		cdc.SelectObject(p_clOldFont);

		//Gets vertical scrollbar height
		m_lVScrollHeight=GetSystemMetrics(SM_CYVSCROLL);

		//Gets vertical scrollbar height
		m_lHScrollWidth=GetSystemMetrics(SM_CXHSCROLL);

		CHARFORMAT cfmCharFormat;
		GetSelectionCharFormat(cfmCharFormat);
		cfmCharFormat.crTextColor=C_DEFAULTCOLOR;
		cfmCharFormat.dwMask=CFM_COLOR;		
		cfmCharFormat.dwEffects=NULL;
		SetDefaultCharFormat(cfmCharFormat);
		//END
	}

	m_clToolTip.Create(this);

	GetWindowRect (& m_crRect);
	ScreenToClient(& m_crRect);
	m_clToolTip.AddTool(this, (LPCSTR) "", &m_crRect, m_nID);

	return bRes;
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::ShowWindow(int nCmdShow)
{
	if(nCmdShow==SW_SHOW)
	{
		m_fn_vResizeToFitParent();
		((CPA_EdIR_View*)GetParent())->m_fn_vSetEditControl(this);

		//Sets cursor position
		m_fn_vSetCursorPos(m_lOldCurrentCharIndex,m_lOldScrollPosition, -1, TRUE);

		m_clToolTip.Activate(TRUE);
		m_fn_vSetFontTypeAndSize( );
	}
	else
	if(nCmdShow==SW_HIDE)
	{
		//Gets current char pos
		m_lOldCurrentCharIndex=m_fn_lGetCurrentCharIndex();

		//Gets scroll position
		CPoint	clScrollPos  = GetScrollPosition();
		m_lOldScrollPosition = clScrollPos.y;

		m_clToolTip.Activate(FALSE);
	}

	return CRichEditCtrl::ShowWindow(nCmdShow);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vCopyIntoClipBoard(CString csText)
{
	if(OpenClipboard())
	{
		if(EmptyClipboard())
		{
			long		lSize=csText.GetLength();
			HANDLE	hText=GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,lSize+1);

			if(hText!=NULL)
			{
				LPVOID	pvText=GlobalLock(hText);

				if(pvText!=NULL)
				{
					CopyMemory(pvText,LPCTSTR(csText),lSize+1);
				
					HANDLE	hRes=SetClipboardData(CF_TEXT,hText);

					if(hRes==NULL)
						MessageBox("Can't set clipboard data");

					GlobalUnlock(hText);
				}
				else
					MessageBox("Can't lock memory for clipboard operation");
			}
			else
				MessageBox("Can't alloc memory for clipboard");
		}
		else
			MessageBox("Can't empty	clipboard");

		CloseClipboard();
	}
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetStringFromClipBoard()
{
	CString	csText="";

	if(OpenClipboard())
	{
		HANDLE	hText=GetClipboardData(CF_TEXT);

		if(hText!=NULL)
		{
			LPVOID	pvText=GlobalLock(hText);

			if(pvText!=NULL)
			{
				csText=(char *)pvText;

				GlobalUnlock(hText);
			}
			else
				MessageBox("Can't lock allocated memory for clipboard operation");
		}

		CloseClipboard();
	}

	return csText;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
#ifdef DAVE  
	if(isprint(nChar))
		m_fn_vOnChar(m_fn_lGetCurrentCharIndex(),nChar);
	else
		CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
#else
	if(isprint(nChar))
		m_fn_vOnChar(m_fn_lGetCurrentCharIndex(),nChar);
	else
		CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
/*kwn
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		//Undo
		if(nChar==C_CTRLZ)
		{
			if(m_clEditManager.CanUndo())
				m_clEditManager.Undo();
			else
			{
				MessageBox("No Undo Available");

				m_fn_vSetModified(FALSE);
			}
		}

		//Redo
		if(nChar==C_CTRLA)
		{
			if(m_clEditManager.CanRedo())
				m_clEditManager.Redo();
			else
				MessageBox("No Redo Available");
		}

		//Default input
		if(isprint(nChar))
			m_fn_vOnChar(m_fn_lGetCurrentCharIndex(),nChar);
		else
		//Return
		if(nChar==VK_RETURN)
		{
			CString	csText;
			GetWindowText(csText);
			LPCSTR	pcur = (LPCSTR) csText;
			long	lIndex = m_fn_lGetCurrentCharIndex(), cpttab = 0;
			long	lIndex1 = lIndex;
			CString	addtab;

			while (lIndex && pcur[lIndex] != '\r')
				lIndex--;
			if(pcur[lIndex] == '\r') 
			{
				lIndex++;
				while(pcur[lIndex] && (lIndex < lIndex1 ) && ((pcur[lIndex] == '\t') || (pcur[lIndex] == ' ')))
				{
					addtab += pcur[lIndex];
					lIndex++; cpttab++;
				}
			}
			m_fn_vOnAddString(m_fn_lGetCurrentCharIndex(),C_RETURN);
			if(cpttab)
				m_fn_vOnAddString(m_fn_lGetCurrentCharIndex(),addtab);
		}
	}*/
#endif
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	
#ifdef DAVE  
	CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);	
#else
	//In order to manage selection with page up and page down
	static long	s_lSelectionStartIndex=-1;

	// update display parameter
	m_bRepaintParent = FALSE;

	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		// intercept engine key
		if (nChar == C_F2)
		{
			M_GetMainWnd()->SendMessage(WM_COMMAND, CPA_IDCB_SWAPTOEDITORS, 0);

		}

		// move from one word to another (Ctrl+arrow)
		if(m_fn_bCtrlKeyIsPressed() && (nChar == VK_LEFT || nChar == VK_RIGHT))
		{
			CString	csText;
			int		iIndex;

			GetWindowText(csText);
			CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
			iIndex = (int) m_fn_lGetCurrentCharIndex();

			while(nChar == VK_LEFT && iIndex > 0 && csText[iIndex-1] == '_')
			{
				CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
				CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
				iIndex = (int) m_fn_lGetCurrentCharIndex();
			}
			while(nChar == VK_RIGHT && iIndex < csText.GetLength() && csText[iIndex] == '_')
			{
				CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
				CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
				iIndex = (int) m_fn_lGetCurrentCharIndex();
			}
			return;
		}

		// shift mode
		if(nChar==C_SHIFT)
		{
			if(m_bShiftKeyIsPressed==FALSE)
			{
				long	lEndSelection;
				GetSel(s_lSelectionStartIndex,lEndSelection);
			}

			m_bShiftKeyIsPressed=TRUE;
		}
		
		char	cErasedChar=0;

		// erase with Suppr Key
		if(nChar==C_SUPPR && m_fn_lGetCurrentCharIndex()<GetTextLength())
		{
			CString	csText;
			GetWindowText(csText);
			
			if ( m_fn_bCtrlKeyIsPressed() )
			{
				CString	csDeleteString;
				long	lStartSelection, lEndSelection;
				int		iIndex;
				
				iIndex = m_fn_lGetCurrentCharIndex();

				if(!M_ISALNUM(csText[iIndex]))
				{
 					cErasedChar = csText[iIndex];
					m_bSingleChar2 = TRUE;
				}
				else
				{
					m_bSingleChar2 = FALSE;
					csDeleteString = m_fn_csGetWordAfter((m_fn_lGetCurrentCharIndex()), lStartSelection, lEndSelection);
					m_fn_vOnDelString(lStartSelection,csDeleteString,nChar,TRUE);								
				}
			}
			else
			{
				cErasedChar=csText[int(m_fn_lGetCurrentCharIndex())];
			}
		}
		// erase with backspace Key
		else if(nChar==C_BACKSPACE && m_fn_lGetCurrentCharIndex()>0)
		{
			CString	csText;
			GetWindowText(csText);
			
			if ( m_fn_bCtrlKeyIsPressed() )
			{
				CString	csDeleteString;
				long	lStartSelection, lEndSelection;
				int		iIndex;

				iIndex = m_fn_lGetCurrentCharIndex()-1;

				if(!M_ISALNUM(csText[iIndex]))
				{
					cErasedChar=csText[iIndex];
					m_bSingleChar1 = TRUE;
				}
				else
				{
					m_bSingleChar1 = FALSE;
					csDeleteString = m_fn_csGetWordBefore((m_fn_lGetCurrentCharIndex()) ,lStartSelection ,lEndSelection);
					m_fn_vOnDelString(lStartSelection,csDeleteString,nChar,TRUE);								
				}
			}
			else
			{
				cErasedChar=csText[int(m_fn_lGetCurrentCharIndex()-1)];
			}
		}

		//Gets selected text
		CString	csSelectedText;
		long	lStartSelection;
		long	lEndSelection;
		BOOL	bNoSelectedText;


		GetSel(lStartSelection,lEndSelection);
		bNoSelectedText=(lStartSelection==lEndSelection);

		if(!bNoSelectedText)
		{
			GetWindowText(csSelectedText);
			csSelectedText=csSelectedText.Mid(lStartSelection,lEndSelection-lStartSelection);
			m_csOldSelectedText=csSelectedText;
		}
		else
			m_csOldSelectedText="";

		//Deletes
		UINT nStandardChar = MapVirtualKey(nChar,C_TRANSLATE_VK2CHAR);
		nStandardChar&=C_TRANSLATE_CHARMASK;
		
		// check if the selection must be deleted
		if( (!bNoSelectedText) 
		   &&
			((!m_fn_bCtrlKeyIsPressed()&&(nStandardChar!=0)&&(fn_bIsValidCharacter(nStandardChar, m_bShiftKeyIsPressed)))	||
			 (m_fn_bCtrlKeyIsPressed() && nChar=='V')	||
			 (!m_bShiftKeyIsPressed && nChar==C_SUPPR)	||
			 (m_bShiftKeyIsPressed && nChar==C_INSER)	||
			 (nChar==C_AUTOCOMPLETECHARDOWN)			||
			 (nChar==C_BACKSPACE)))
		{
			m_fn_vOnDelString(lStartSelection,m_csOldSelectedText,nChar,TRUE);
		}

		// check if something else must be deleted
		else if((bNoSelectedText) && ((!m_bShiftKeyIsPressed && nChar==C_SUPPR)||(nChar==C_BACKSPACE)))
		{
			CString csDeletedString;

			// end or begin of a line
			if((nChar==C_SUPPR && cErasedChar=='\xD') ||
			   (nChar==C_BACKSPACE && cErasedChar=='\xA'))
				csDeletedString=C_RETURN;
			// nothing
			else if(cErasedChar==0)
				csDeletedString="";
			// current character
			else
				csDeletedString=cErasedChar;

			if(!m_fn_bCtrlKeyIsPressed() || m_bSingleChar1 || m_bSingleChar2 )
				m_fn_vOnDelString(m_fn_lGetCurrentCharIndex(),csDeletedString,nChar,!bNoSelectedText);
		}

		//Processes input (default action)
		if(nChar!=C_AUTOCOMPLETECHARDOWN							&&
//		   nChar!=C_INSER											&&
		   nChar!=C_SUPPR											&&
		   nChar!=C_BACKSPACE										&&
//		   nChar!=C_TAB												&&
		   nChar!=C_PAGEUP											&&
		   nChar!=C_PAGEDOWN										&&
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='C')				&&		// Copy
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='X')				&&		// Cut
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='V')				&&		// Paste
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='Z')				&&		// Undo
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='A')				&&		// ??
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='T')				&&		// find by type
//		   !(m_fn_bCtrlKeyIsPressed() && nChar=='F')				&&		// find & replace
		   !(m_fn_bCtrlKeyIsPressed() && nChar=='P')				&&		// process AI
		   !(m_fn_bCtrlKeyIsPressed() && nChar=='W')				&&		// ??
		   !(m_fn_bCtrlKeyIsPressed() && nChar==C_COMMENT)			&&		// comment selection
		   !(m_fn_bCtrlKeyIsPressed() && nChar==C_DECOMMENT)		//&&		// decomment selection
//		   !(m_fn_bCtrlKeyIsPressed() && nChar==C_INSER)			&&
//		   !(m_bShiftKeyIsPressed && nChar==C_SUPPR)				&&
//		   !(m_bShiftKeyIsPressed && nChar==C_INSER)				
		   )	
			CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);

/*		//Copies into clipboard
		if(!m_csOldSelectedText.IsEmpty() && (m_fn_bCtrlKeyIsPressed() && ((nChar=='C')||(nChar==C_INSER))))
		{
			m_fn_vCopyIntoClipBoard(m_csOldSelectedText);
		}

		//Pastes from clipboard
		if((m_fn_bCtrlKeyIsPressed() && nChar=='V')||(m_bShiftKeyIsPressed && nChar==C_INSER))
		{
			CString	csText=m_fn_csGetStringFromClipBoard();

			if(!csText.IsEmpty())
				m_fn_vOnAddString(m_fn_lGetCurrentCharIndex(),csText);
		}

		//Cuts into clipboard
		if(!m_csOldSelectedText.IsEmpty() && ((m_fn_bCtrlKeyIsPressed() && nChar=='X')||(m_bShiftKeyIsPressed && nChar==C_SUPPR)))
		{
			m_fn_vOnDelString(lStartSelection,m_csOldSelectedText,nChar,TRUE);
			m_fn_vCopyIntoClipBoard(m_csOldSelectedText);
		}

		//Tabulation
		if(nChar==C_TAB)
		{
			if( m_bShiftKeyIsPressed )
				m_fn_vDeleteBlockTabulation();
			else
				m_fn_vAddBlockTabulation();
		}
*/
		//AutoCompletion
		if(nChar==C_AUTOCOMPLETECHARDOWN)
		{
			long	lCurrentCharLine=m_fn_lGetCurrentCharLine();
			long	lCurrentCharCol=m_fn_lGetCurrentCharCol();

			m_fn_vCompleteWord(lCurrentCharLine,lCurrentCharCol);
		}

		//PageUp/Down capabilities
		if((nChar==C_PAGEUP)||(nChar==C_PAGEDOWN))
		{
			CRect	crClientRect;
			long	lWindowHeigth;
			long	lLineNumber;

			GetParent()->GetClientRect(crClientRect);

			lWindowHeigth=crClientRect.bottom-m_lVScrollHeight;
			lWindowHeigth/=m_clFontSize.cy;
			if(nChar==C_PAGEUP)
				lWindowHeigth=-lWindowHeigth;

			lLineNumber=m_fn_lGetCurrentCharLine();
			lLineNumber+=lWindowHeigth;
			if(lLineNumber<0)
				lLineNumber=0;

			if(m_bShiftKeyIsPressed==TRUE)
				SetSel(s_lSelectionStartIndex,LineIndex(lLineNumber));
			else
				SetSel(LineIndex(lLineNumber),LineIndex(lLineNumber));
		}

		//Displays IA Objects
		if(m_fn_bCtrlKeyIsPressed() && nChar=='T')
		{
			long	lCurrentCharLine=m_fn_lGetCurrentCharLine();
			long	lCurrentCharCol=m_fn_lGetCurrentCharCol();

			//Displays dialog box
			m_fn_vDisplayIAObjectsAndCompleteWordNew(lCurrentCharLine,lCurrentCharCol);
		}
/*
		//Find&Replace
		if(nChar==C_F && m_fn_bCtrlKeyIsPressed())
		{
			HINSTANCE hOldInstance=AfxGetResourceHandle();
			AfxSetResourceHandle(g_stAIIdentity.hModule);
			CPA_EdIR_DiagReplace m_DReplace(this);
			m_DReplace.DoModal();
			AfxSetResourceHandle(hOldInstance);
			SetFocus();
		}

		//Find last searched word
		if( nChar == C_L && m_fn_bCtrlKeyIsPressed() )
		{
			long lStart, lEnd;
			GetSel( lStart, lEnd );
			m_fn_vFindAndSelTextNext( lEnd, m_csLastSearched );
		}
*/
		//Add to watch
		if( nChar == C_W && m_fn_bCtrlKeyIsPressed() )
		{
			OnIrRulesAddwatch();
		}

		//Process AI
		if( nChar == C_P && m_fn_bCtrlKeyIsPressed() )
		{
			if (g_pclAIInterface && g_pclAIInterface->m_pclIRMainFrame && 
				g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView())
				g_pclAIInterface->m_pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_vProcessIA();
		}
/*
		//Find next 
		if( nChar == C_F3 )
		{
			long		first, last;
			CString		word;

			GetSel( first, last );
			if( first != last )
				word = m_fn_csGetWordIn(first+1, first, last);
			else
				word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);

			m_csLastSearched = word;

			m_fn_vFindAndSelTextNext( last, word );
		}
		// Find previous
		if( nChar == C_F4 )
		{
			long		first, last;
			CString		word;

			GetSel( first, last );
			if( first != last )
				word = m_fn_csGetWordIn(first+1, first, last);
			else
				word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);

			m_csLastSearched = word;

			m_fn_vFindAndSelTextPrevious( first, word );
		}
*/
		//Sets/Removes BreakPoint
		if(nChar==C_F9)
		{
			long	lLineIndex=m_fn_lGetCurrentCharLine();
			long	lStartLineIndex=LineIndex(lLineIndex);

			((CPA_EdIR_Frame *)GetParentFrame())->m_fn_vSetOrRemoveOneBreakPoint(lStartLineIndex,lLineIndex*m_clFontSize.cy);
		}

	}

	// comment current selected lines
	if (m_fn_bCtrlKeyIsPressed() && nChar==C_COMMENT)
	{
		long lStartSelection, lEndSelection;
		long lStartLine, lEndLine, lCurLine;
		long lAddEnd = 0;

		// get the selected lines
		GetSel (lStartSelection, lEndSelection);
		lStartLine = LineFromChar (lStartSelection);
		lEndLine = LineFromChar (lEndSelection-1);

		// for each selected line
		for (lCurLine = lStartLine; lCurLine <= lEndLine; lCurLine ++)
		{
			m_fn_vOnChar (LineIndex (lCurLine), ';');
			lAddEnd ++;
		}
		// restore selection
		SetSel (lStartSelection, lEndSelection + lAddEnd);
	}
	// decomment selected lines
	if (m_fn_bCtrlKeyIsPressed() && nChar==C_DECOMMENT) 
	{
		long lStartSelection, lEndSelection;
		long lStartLine, lEndLine, lCurLine;
		long lAddEnd = 0;

		// get the selected lines
		GetSel (lStartSelection, lEndSelection);
		lStartLine = LineFromChar (lStartSelection);
		lEndLine = LineFromChar (lEndSelection-1);

		// for each selected line
		for (lCurLine = lStartLine; lCurLine <= lEndLine; lCurLine ++)
		{
			long lLineIndex = LineIndex (lCurLine);
			long lLength = LineLength (lLineIndex);

			// get the current line in the buffer
			CString csLine = m_fn_csGetLine(lCurLine);

			// check if the line is commented
			CString csCommentLine = csLine;
			csCommentLine.TrimLeft();

			// if necessary, decomment it
			if ((csCommentLine != "") && (csCommentLine[0] == ';'))
			{
				long lIndex = csLine.Find(';') + lLineIndex;
				m_fn_vOnDelString (lIndex, ";", C_SUPPR, FALSE);
				lAddEnd --;
			}
		}
		// restore selection
		SetSel (lStartSelection, lEndSelection + lAddEnd);
	}
#endif
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
#ifdef DAVE
	CRichEditCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
#else
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		CRichEditCtrl::OnKeyUp(nChar, nRepCnt, nFlags);

		if((nChar==C_CTRL) || (nChar==C_ALTGR))
		{
			m_bSingleChar1 = FALSE;
			m_bSingleChar2 = FALSE;
		}

		if(nChar==C_SHIFT)
			m_bShiftKeyIsPressed=FALSE;
	}
#endif
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnMouseMove(UINT nFlags,CPoint point)
{
	CRichEditCtrl::OnMouseMove( nFlags, point );
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnLButtonDown(UINT nFlags,CPoint point)
{
	GetParentFrame()->SetActiveView((CView *)GetParent());
	m_bRepaintParent = TRUE;
	CRichEditCtrl::OnLButtonDown(nFlags,point);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::OnLButtonUp(UINT nFlags,CPoint point)
{
	CRichEditCtrl::OnLButtonUp(nFlags,point);
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lSkipLeftAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=0;

	if(lIndex>0)
	{
		lResultIndex=lIndex-1;
		while(lResultIndex>=0 && M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex--;
		lResultIndex++;
	}

	return lResultIndex;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lSkipRightAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=lIndex;
	long	lStringLen=csString.GetLength();

	if(lIndex<lStringLen)
	{
		while(lResultIndex<=lStringLen-1 && M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex++;
	}

	return lResultIndex;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lSkipLeftNonAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=0;

	if(lIndex>0)
	{
		lResultIndex=lIndex-1;
		while(lResultIndex>=0 && !M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex--;
		lResultIndex++;
	}

	return lResultIndex;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lSkipRightNonAlphaNumChar(CString &csString,long lIndex)
{
	long	lResultIndex=lIndex;
	long	lStringLen=csString.GetLength();

	if(lIndex<lStringLen)
	{
		while(lResultIndex<=lStringLen-1 && !M_ISALNUM(csString[int(lResultIndex)]))
			lResultIndex++;
	}

	return lResultIndex;
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsInTheListString(CString &csWord,CStringList *p_clListString)
{
	POSITION	pos=p_clListString->GetHeadPosition();
	BOOL			bFound=FALSE;
	while(pos!=NULL && !bFound)
	{
		bFound=(p_clListString->GetNext(pos).CompareNoCase(csWord)==0);
	}

	return bFound;
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAKnownWord(CString &csWord)
{
	return m_fn_bIsInTheListString(csWord,&gm_clKnownWords);
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAKeyWord(CString &csWord)
{
	return m_fn_bIsInTheListString(csWord,&gm_clKeyWords);
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAnActionWord(CString &csWord)
{
	return m_fn_bIsInTheListString(csWord,&gm_clActionWords);
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAConditionWord(CString &csWord)
{
	return m_fn_bIsInTheListString(csWord,&gm_clConditionWords);
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAConstantWord(CString &csWord)
{
	return m_fn_bIsInTheListString(csWord,&gm_clConstantWords);
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsAMacroWord(CString &csWord)
{
	POSITION pos;
	CString csString;
	void * p_Dummy;
	if( g_clMacroList.map )
	{	
		pos = g_clMacroList.map->GetStartPosition();
		while( pos )
		{ 
			g_clMacroList.map->GetNextAssoc( pos, csString, p_Dummy );
			if( csString.CompareNoCase( csWord ) == 0 )
				return TRUE;
		}
	}
	return FALSE;
}

/**********************************************************************************/
CStringList *CPA_EdIR_EditCtrl::m_fn_pclGetListMatchWord(CString csWord,CStringList *p_clListString)
{
	CString	csUpperWord=csWord;
	csUpperWord.MakeUpper();

	CStringList	*p_clListMatchWord=new CStringList;
	POSITION	pos=p_clListString->GetHeadPosition();

	while(pos!=NULL)
	{
		CString	csCurrentString=p_clListString->GetAt(pos);
		csCurrentString.MakeUpper();

		if(csCurrentString.Find(csUpperWord)==0)
		{
			long l_id = fn_lGetProcedureIdFromEditorName(p_clListString->GetAt(pos));
			CString cs_Params = fn_csGetProcedureParams(l_id);
			CString szTemp;
			int nStringLength=cs_Params.GetLength();
			if (nStringLength)
			{
				szTemp="(";
				for (int nLoop=0; nLoop<nStringLength; nLoop++)
				{
					switch (cs_Params[nLoop])
					{
					case 'A':
						szTemp+="Action";
						break;
					case 'B':
						szTemp+="Boolean";
						break;
					case 'C':
						szTemp+="GMT";
						break;
					case 'D':
						szTemp+="Array";
						break;
					case 'E':
						szTemp+="ZDE_PERSO";
						break;
					case 'F':
						szTemp+="Surface";
						break;
					case 'G':
						szTemp+="Family";
						break;
					case 'H':
						szTemp+="ENV";
						break;
					case 'I':
						szTemp+="List";
						break;
					case 'J':
						szTemp+="Objecttable";
						break;
					case 'K':
						szTemp+="Mask";
						break;
					case 'L':
						szTemp+="Long";
						break;
					case 'M':
						szTemp+="Channel";
						break;
					case 'N':
						szTemp+="Power";
						break;
					case 'O':
						szTemp+="LIPSYNCHRO";
						break;
					case '!':
						szTemp+="PERSO";
						break;
					case 'P':
						szTemp+="PERSO";
						break;
					case 'Q':
						szTemp+="Comport";
						break;
					case 'R':
						szTemp+="Sector";
						break;
					case 'S':
						szTemp+="String";
						break;
					case 'T':
						szTemp+="Text";
						break;
					case 'U':
						szTemp+="ZDM_PERSO";
						break;
					case 'V':
						szTemp+="Vector";
						break;
					case 'W':
						szTemp+="WayPoint";
						break;
					case 'X':
						szTemp+="Real";
						break;
					case 'Z':
						szTemp+="ZDD_PERSO";
						break;
					case '1':
						szTemp+="my_Reflex";
						break;
					case '2':
						szTemp+="my_Intell";
						break;
					case '3':
						szTemp+="Reflex";
						break;
					case '4':
						szTemp+="Intell";
						break;
					case '5':
						szTemp+="Font";
						break;
					case '6':
						szTemp+="Button";
						break;
					case '#':
						szTemp+="Caps";
						break;
					case '$':
						szTemp+="Graph";
						break;
					default:
						break;

					}
 					szTemp+=", ";
				}
				szTemp.SetAt(szTemp.GetLength()-2,')');
				szTemp=szTemp.Left(szTemp.GetLength()-1);
			}
			szTemp=p_clListString->GetAt(pos)+szTemp;

			POSITION pos1 = p_clListMatchWord->GetHeadPosition();
			BOOL bOK = TRUE;
			while(pos1)
			{
				if(p_clListMatchWord->GetNext(pos1).CompareNoCase(szTemp) == 0)
				{
					bOK = FALSE;
					break;
				}
			}
			if(bOK)
				p_clListMatchWord->AddTail(szTemp);
		}
		p_clListString->GetNext(pos);
	}

	return p_clListMatchWord;
}

/**********************************************************************************/
COLORREF CPA_EdIR_EditCtrl::m_fn_rgbGetColor(CString &csWord)
{
	if(m_fn_bIsAKeyWord(csWord))
		return C_KEYWORDCOLOR;

	if(m_fn_bIsAnActionWord(csWord))
		return C_ACTIONWORDCOLOR;

	if(m_fn_bIsAConditionWord(csWord))
		return C_CONDITIONWORDCOLOR;

	if(m_fn_bIsAConstantWord(csWord))
		return C_CONSTANTWORDCOLOR;

	if( m_fn_bIsAMacroWord( csWord ) )
		return C_MACROWORDCOLOR;

	CString csLower = csWord;
	csLower.MakeLower();
	if (g_pclAIInterface->m_pclIRMainFrame->m_clConstantList.m_fn_bIsAConstant(csLower))
		return C_CONSTANTCOLOR;

	return C_DEFAULTCOLOR;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vColorText(long lStartWordIndex,long lEndWordIndex,COLORREF rgbColor, COLORREF rgbBackgroundColor, BOOL bRememberSel )
{
	if((rgbColor == C_DEFAULTCOLOR) && (mb_CanDefault == FALSE))
		return;

	//First, remembers old selection
	long	lFirstSelectedCharIndex;
	long	lLastSelectedCharIndex;
	if( bRememberSel )
		GetSel(lFirstSelectedCharIndex,lLastSelectedCharIndex);

	//Selects the word
	SetSel(lStartWordIndex,lEndWordIndex);

	//Changes word's color
	CHARFORMAT cfmCharFormat;
	GetSelectionCharFormat(cfmCharFormat);
	cfmCharFormat.crTextColor=rgbColor;
	cfmCharFormat.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;		
	cfmCharFormat.dwEffects = NULL;
	strcpy( cfmCharFormat.szFaceName, LPCSTR( m_csFontType ) );
	cfmCharFormat.yHeight = m_nFontSize;
	SetSelectionCharFormat(cfmCharFormat);

	//Selects old selection
	if( bRememberSel )
		SetSel(lFirstSelectedCharIndex,lLastSelectedCharIndex);
}

/**********************************************************************************/

void CPA_EdIR_EditCtrl::m_fn_vColorLines(int iStartLine, int iNumLines)
{
#ifndef DAVE
	for (int iLineNum = iStartLine ; iLineNum < (iStartLine + iNumLines) ; iLineNum++)
	{
		int iLineIndex = LineIndex(iLineNum);
		CString strLine;
		int iLineLen = LineLength(iLineIndex);
		if (iLineLen == 0)
			continue;
		LPTSTR strBuffer = strLine.GetBuffer(iLineLen);
		(*(DWORD*)strBuffer) = iLineLen;
		GetLine(iLineNum, strBuffer);
		strLine.ReleaseBuffer(iLineLen);

		for (int iPos = 0 ; iPos < iLineLen ; )
		{
			int iNext = iPos;
			while ((iNext < iLineLen) && (!isalnum(strLine[iNext]) && (strLine[iNext] != ';')))
				iNext++;
			if ((iNext < iLineLen) && (strLine[iNext] == ';'))
			{
				m_fn_vColorText(iLineIndex + iPos, iLineIndex + iLineLen, C_COMMENTCOLOR, -1, FALSE);
				break;
			}
			iPos = iNext;
			if (iPos == iLineLen)
				continue;
			int iTemp = iPos;
			int iTemp1 = 0;
			char mot[500];
			while((iPos < iLineLen) && (isalnum(strLine[iPos]) || (strLine[iPos] == '_')))
				mot[iTemp1++] = strLine[iPos++];
			mot[iTemp1] = 0;
			CString szWord = mot;

			if (!szWord.CompareNoCase("define") && (strLine[iPos - 7] == '#'))
				szWord = "#define";

			m_fn_vColorText(iLineIndex + iTemp, iLineIndex + iPos, m_fn_rgbGetColor(szWord)/*, rgbBackgroundColor*/, -1, FALSE );
		}
	}
#endif
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vColorText(long lStartWordIndex, long lEndWordIndex/*, CPA_EdIR_DebugInfoList *pclDebugInfoList*/)
{
#ifndef DAVE 
	CString	csText;
	GetWindowText(csText);
	LPCSTR	lpsz = (LPCSTR) csText;
	long	lTemp, lTemp1;
	char	mot[500];
	CString	szWord;

	//First, remembers old selection
	long lFirstSelectedCharIndex, lLastSelectedCharIndex;
	GetSel(lFirstSelectedCharIndex, lLastSelectedCharIndex);

	m_fn_vColorText(0, -1, RGB(0,0,0), -1, FALSE);	

	lTemp = lStartWordIndex;
	while(lTemp && (lpsz[lTemp] != ';') && (lpsz[lTemp] != '\r'))
		lTemp--;

	if(lpsz[lTemp] == ';')
	{
com:
		lTemp = lStartWordIndex;
		while(lpsz[lStartWordIndex] && lpsz[lStartWordIndex] != '\r')
			lStartWordIndex++;
		m_fn_vColorText(lTemp, lStartWordIndex, C_COMMENTCOLOR, -1, FALSE);
	}

	while(lpsz[lStartWordIndex] && (lStartWordIndex <= lEndWordIndex))
	{
		// Find start of next word
		while(lpsz[lStartWordIndex] && (lStartWordIndex <= lEndWordIndex) && !isalnum(lpsz[lStartWordIndex]) && lpsz[lStartWordIndex] != ';')
			lStartWordIndex++;
		if(lpsz[lStartWordIndex] == ';')
			goto com;

		lTemp = lStartWordIndex;
		lTemp1 = 0;
		while(lpsz[lStartWordIndex] && (lStartWordIndex <= lEndWordIndex) && (isalnum(lpsz[lStartWordIndex])||(lpsz[lStartWordIndex]=='_')))
			mot[lTemp1++] = lpsz[lStartWordIndex++];
		mot[lTemp1] = 0;
		szWord = mot;

		if (!szWord.CompareNoCase("define") && (lpsz[lStartWordIndex - 7] == '#'))
			szWord = "#define";

		m_fn_vColorText(lTemp, lStartWordIndex, m_fn_rgbGetColor(szWord)/*, rgbBackgroundColor*/, -1, FALSE );
	}

	SetSel(lFirstSelectedCharIndex,lLastSelectedCharIndex);
#endif
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vHideNonExecutedLines (CPA_EdIR_DebugInfoList *pclDebugInfoList)
{
	CList <long, long> clLinesNumbersList;
	POSITION pos = pclDebugInfoList->GetHeadPosition ();
	CPA_EdIR_DebugInfo *pclDebugInfo;

	long lSelLine;
	// get the cursor position
	lSelLine = m_fn_lGetSelLine ();

	long lLastLineNumber = 0;
	long lLineNumber;
	//long lIndex;

	// show all
	clLinesNumbersList.RemoveAll ();
	m_fn_vHideLines (clLinesNumbersList);

	// nothing but show all
	if (! g_pclAIInterface->m_pclIRMainFrame->m_bMarkExecutedLines)
	{
		BOOL bShowTests = g_pclAIInterface->m_pclIRMainFrame->m_bShowTests;
		// assuming ascending line number info
		while (pos)
		{
			pclDebugInfo = pclDebugInfoList->GetNext (pos);
			//lIndex = pclDebugInfo->m_fn_lGetIndex ();
			//lLineNumber = LineFromChar (lIndex);
			lLineNumber = pclDebugInfo->m_fn_lGetLineNumber ();

			if (! bShowTests &&
				M_GetTypeInterpret (pclDebugInfo->m_fn_pstGetNode ()) == E_ti_KeyWord &&
				M_eKeyWordIdInterpret (pclDebugInfo->m_fn_pstGetNode ()) == eKeyWord_If)
			{
				// we should force the deletion of this line (is a test)
				while (lLastLineNumber <= lLineNumber)
				{
					clLinesNumbersList.AddTail (lLastLineNumber);
					lLastLineNumber ++;
				}
			}
			else
			{
				if (pclDebugInfo->m_fn_bGetExecuted () && 
					lLineNumber >= lLastLineNumber)
				{
					// hide all between the last line one and this
					while (lLastLineNumber < lLineNumber)
					{
						clLinesNumbersList.AddTail (lLastLineNumber);
						lLastLineNumber ++;
					}
					// skip the current line
					lLastLineNumber ++; 
				}
			}
		}

		// and hide last lines, of course
		long lLineCount = GetLineCount ();

		while (lLastLineNumber < lLineCount)
		{
			clLinesNumbersList.AddTail (lLastLineNumber);
			lLastLineNumber ++;
		}

		m_fn_vHideLines (clLinesNumbersList);

		clLinesNumbersList.RemoveAll ();
	}

	m_fn_vSetSelLine (lSelLine);
}

//**********************************************************************************
LRESULT CPA_EdIR_EditCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// We need to pass these messages to the tooltip for it to determine
	// the position of the mouse

	switch (message)
	{
	case WM_PAINT:
		{
			int nScrollPos = GetScrollPos( SB_VERT );
			if( nScrollPos != m_lOldScrollPosition || 
				( nScrollPos == GetScrollLimit( SB_VERT ) &&
				  m_bRepaintParent 
				)
			  )
			{
				m_lOldScrollPosition = nScrollPos;
				CRect clClientRect;
				GetParent()->GetClientRect( &clClientRect );
				CRect clRect( 0, 0, 12, clClientRect.Height() );
				GetParent()->InvalidateRect( &clRect );
				GetParent()->UpdateWindow();
			}
		}	
		break;

	case WM_VSCROLL:
		{
			CRect clClientRect;
			GetParent()->GetClientRect( &clClientRect );
			CRect clRect( 0, 0, 12, clClientRect.Height() );
			GetParent()->InvalidateRect( &clRect );
			GetParent()->UpdateWindow();
		}
		break;


	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
	case WM_MBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_MBUTTONUP:
	case WM_RBUTTONUP:
	case WM_MOUSEMOVE:

	case WM_LBUTTONDBLCLK:
		{
			// all those only in debugger !
			CPA_DLLBase	*pclDebugDLL = (CPA_DLLBase*) (g_pclAIInterface->GetMainWorld ()->GetToolDLLWithName ("IA_Debugger"));
			if (pclDebugDLL->fn_bIsCurrentEditor ())
			{
				long xPos = LOWORD(lParam);
				long yPos = HIWORD(lParam);

				CPoint cpScrollPos = /*((CScrollView *) GetParent ())->*/GetScrollPosition ();

				long lCharLine = /*yPos / m_clFontSize.cy;*/(cpScrollPos.y + yPos) / m_clFontSize.cy;
				long lPtCharCol = /*xPos;*/cpScrollPos.x + xPos;
				long lIndex = LineIndex (lCharLine);
				CString csLineText = m_fn_csGetLine (lCharLine);
				long lPtCurCol = 0;
				int iCurChar = 0;
				long lCurCol = 0;
				BOOL bInText = TRUE;
				//Gets font size
				CClientDC cdc (GetParent ());
				CFont *p_clOldFont = cdc.SelectObject (GetParent ()->GetFont ());
				CPoint cpCharSize;	
				while (lPtCurCol <= lPtCharCol)
				{
					if (iCurChar >= csLineText.GetLength ())
					{
						bInText = FALSE;
						break;
					}
					char chChar = csLineText [iCurChar];
					cpCharSize = cdc.GetTextExtent (chChar);
					if (chChar == '\t')
					{
						// get the next tab position. *6, if you believe it!
						long lFinalCol = (1 + lCurCol / 6) * 6;
						long lIncrementCol = lFinalCol - lCurCol;
						lCurCol = lFinalCol;
						lPtCurCol += lIncrementCol * cpCharSize.x;
						if (lPtCurCol > lPtCharCol)
						{
							bInText = FALSE;
							break;
						}
					}
					else
					{
						lPtCurCol += cpCharSize.x;
						lCurCol ++;
					}
					iCurChar ++;
				}
				cdc.SelectObject(p_clOldFont);

				CString csVariableName;
				if (! bInText)
				{
					csVariableName = "";
				}
				else
				{
					lIndex += iCurChar;
					long lStart, lEnd;
					csVariableName = m_fn_csGetWordIn (lIndex, lStart, lEnd);
				}

				// Get the value
				CString csVariableValue = "";
				long lVariableIndex;
				if (csVariableName != "")
				{
					// ok, ok, but let's ask the debugger for the real value
					lVariableIndex = -1;
					CString *pcsVariableValue = NULL;
					// get the brain. Darn, this IS tough
					CPA_EdIR_Brain *pclBrain = g_pclAIInterface->m_pclIRMainFrame->m_pclDebugBrain;
					if (pclBrain)
					{
						lVariableIndex = pclBrain->m_fn_lGetVariableIndex (csVariableName);
						if (lVariableIndex >= 0)
							pcsVariableValue = (CString *) pclDebugDLL->OnQueryAction (g_pclAIInterface, C_uiAI_GetVariableValue, lVariableIndex);
					}

					// if this not work - no node selected - then look for the current value
					if (pcsVariableValue)
					{
						// extract the value from the pointer
						csVariableValue = * pcsVariableValue;
					}
					else
					{
					CPA_DLLBase	*pclActorDLL = (CPA_DLLBase*) (g_pclAIInterface->GetMainWorld ()->GetObjectDLLWithName (C_szDLLActorName));
					CPA_EdIR_DesignerVariablesView *pclDsgVarView = (CPA_EdIR_DesignerVariablesView *) pclActorDLL->OnQueryAction (g_pclAIInterface, C_uiActor_GetDesignerVariablesView, 0);
					CTL_Editor_Data *pclEditor_Data = pclDsgVarView->m_fn_pclGetVariable (csVariableName);
					if (pclEditor_Data)
					{
						// what is shown by this control?
						// format is as text.
						CStringList *pcslVariableValues = pclEditor_Data->m_fn_pcslFormatDataValueString ();
						POSITION pos = pcslVariableValues->GetHeadPosition ();
						while (pos)
						{
							csVariableValue += pcslVariableValues->GetNext (pos);
							csVariableValue += ",";
						}
						// eliminate the trailing comma
						csVariableValue = csVariableValue.Left (csVariableValue.GetLength () - 1);
					}
				}
				}

				if (message == WM_LBUTTONDBLCLK)
				{
					if (csVariableName != "" && csVariableValue != "")
					{
						tdstDebugAICom stCom;
						stCom.csName = csVariableName;
						stCom.csValue = csVariableValue;
						stCom.lIndex = lVariableIndex;

						pclDebugDLL->OnQueryAction (g_pclAIInterface, C_uiAI_ViewVariable, (long) (& stCom));
					}

					break;
				}


				m_clToolTip.UpdateTipText ((LPCSTR) csVariableValue, this, m_nID);

				// update the rect just in case...
				GetWindowRect (& m_crRect);
				ScreenToClient(& m_crRect);
				
				MSG msg;
				msg.hwnd = m_hWnd;
				msg.message = message;
				msg.wParam = wParam;
				msg.lParam = lParam;

				m_clToolTip.RelayEvent(&msg);
			}
		}
		break;
	}

	return CRichEditCtrlWithLineManagement::WindowProc(message,wParam,lParam);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddText(long lIndex,CString csText,COLORREF rgbColor)
{
	//First, remembers old selection
	long	lFirstSelectedCharIndex;
	long	lLastSelectedCharIndex;
	GetSel(lFirstSelectedCharIndex,lLastSelectedCharIndex);

	//Moves cursor on lIndex position
	SetSel(lIndex,lIndex);

	//Changes text's color
	CHARFORMAT	cfmCharFormat;
	GetSelectionCharFormat(cfmCharFormat);
	cfmCharFormat.crTextColor=rgbColor;
	cfmCharFormat.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;		
	cfmCharFormat.dwEffects = NULL;
	strcpy( cfmCharFormat.szFaceName, LPCSTR( m_csFontType ) );
	cfmCharFormat.yHeight = m_nFontSize;
	SetSelectionCharFormat(cfmCharFormat);
	
	//Inserts the text
	ReplaceSel(csText,TRUE);

	//Moves cursor after the text
	SetSel(lFirstSelectedCharIndex+csText.GetLength(),lFirstSelectedCharIndex+csText.GetLength());
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vReplaceText(long lStartIndex,long lEndIndex,CString csText,COLORREF rgbColor)
{
	//Selects text between lStartIndex position and lEndIndex position
	SetSel(lStartIndex,lEndIndex);

	//Changes text's color
	CHARFORMAT	cfmCharFormat;
	GetSelectionCharFormat(cfmCharFormat);
	cfmCharFormat.crTextColor=rgbColor;
	cfmCharFormat.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;		
	cfmCharFormat.dwEffects = NULL;
	strcpy( cfmCharFormat.szFaceName, LPCSTR( m_csFontType ) );
	cfmCharFormat.yHeight = m_nFontSize;
	SetSelectionCharFormat(cfmCharFormat);
	
	//Replaces the text
	ReplaceSel(csText,TRUE);

	//Moves cursor after the text
	SetSel(lStartIndex+csText.GetLength(),lStartIndex+csText.GetLength());
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lGetCurrentCharIndex()
{
	static long	s_lOldCurrent=-1;
	static long	s_lOldStart=-1;
	static long	s_lOldEnd=-1;

	long	lStart;
	long	lEnd;
	GetSel(lStart,lEnd);

	if(s_lOldStart!=lStart || s_lOldEnd!=lEnd)
	{
		if(s_lOldStart==lStart)
			s_lOldCurrent=lEnd;
		else
			s_lOldCurrent=lStart;

		s_lOldStart=lStart;
		s_lOldEnd=lEnd;
	}

	if(s_lOldCurrent>=GetTextLength())
		return GetTextLength();
	else
		return s_lOldCurrent;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lGetCurrentCharLine()
{
	return LineFromChar(m_fn_lGetCurrentCharIndex());
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lGetCurrentCharCol()
{
	long	lCurrentCharIndex=m_fn_lGetCurrentCharIndex();

	return lCurrentCharIndex-LineIndex(LineFromChar(lCurrentCharIndex));
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lGetCurrentCharLineLength()
{
	return LineLength(m_fn_lGetCurrentCharIndex());
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetWordBefore(long lIndex,long &lWordStartIndex,long &lWordEndIndex)
{
	//Gets text
	CString	csText;
	GetWindowText(csText);

	//Gets word
	lWordEndIndex=m_fn_lSkipLeftNonAlphaNumChar(csText,lIndex);
	lWordStartIndex=m_fn_lSkipLeftAlphaNumChar(csText,lWordEndIndex);
	
	return csText.Mid(lWordStartIndex,lWordEndIndex-lWordStartIndex);
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetWordIn(long lIndex,long &lWordStartIndex,long &lWordEndIndex)
{
	//Gets text
	CString	csText;
	GetWindowText(csText);

	//Gets word
	lWordStartIndex=m_fn_lSkipLeftAlphaNumChar(csText,lIndex);
	lWordEndIndex=m_fn_lSkipRightAlphaNumChar(csText,lIndex);
	
	return csText.Mid(lWordStartIndex,lWordEndIndex-lWordStartIndex);
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetWordAfter(long lIndex,long &lWordStartIndex,long &lWordEndIndex)
{
	//Gets text
	CString	csText;
	GetWindowText(csText);

	//Gets word
	lWordStartIndex=m_fn_lSkipRightNonAlphaNumChar(csText,lIndex);
	lWordEndIndex=m_fn_lSkipRightAlphaNumChar(csText,lWordStartIndex);

	return csText.Mid(lWordStartIndex,lWordEndIndex-lWordStartIndex);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vUpdateColorText(long lLine,long lCol)
{
	//Gets cursor's position
	long	lCurrentCharLine=lLine;
	long	lCurrentCharCol=lCol;

	long	lFirstCharInCurrentLineIndex=LineIndex(lCurrentCharLine);
	long	lCurrentCharIndex=lCol+lFirstCharInCurrentLineIndex;

	//Gets text
	CString	csText;
	GetWindowText(csText);

	//Gets line length
	long	lCurrentLineLen=LineLength(lFirstCharInCurrentLineIndex);

	//Cursor is between two word or not?

	BOOL	bTwoWords=TRUE;
	if(lCurrentCharIndex>0 && lCurrentCharIndex<csText.GetLength())
	{
		if(M_ISALNUM(csText[int(lCurrentCharIndex-1)]) &&
		   M_ISALNUM(csText[int(lCurrentCharIndex)]))
		   bTwoWords=FALSE;
	}
	else
		bTwoWords=FALSE;

	if(bTwoWords)
	{
		//Gets left word
		long	lLeftWordStartIndex;
		long	lLeftWordEndIndex;
		CString	csLeftWord=m_fn_csGetWordBefore(lCurrentCharIndex,lLeftWordStartIndex,lLeftWordEndIndex);

		if (!csLeftWord.CompareNoCase("define") && (csText[int(lLeftWordStartIndex-1)] == '#'))
		{
			lLeftWordStartIndex--;
			csLeftWord = "#define";
		}

		//Colors left word
		if(!m_fn_bIsInLineCommentedText(csText,lLeftWordStartIndex))
			m_fn_vColorText(lLeftWordStartIndex,lLeftWordEndIndex,m_fn_rgbGetColor(csLeftWord));

		if(lCurrentCharCol!=lCurrentLineLen)
		{
			//Gets right word
			long	lRightWordStartIndex;
			long	lRightWordEndIndex;
			CString	csRightWord=m_fn_csGetWordAfter(lCurrentCharIndex,lRightWordStartIndex,lRightWordEndIndex);

			//Colors right word
			if(!m_fn_bIsInLineCommentedText(csText,lRightWordStartIndex))
				m_fn_vColorText(lRightWordStartIndex,lRightWordEndIndex,m_fn_rgbGetColor(csRightWord));
		}
	}
	else
	{
		//Gets word
		long	lWordStartIndex;
		long	lWordEndIndex;
		CString	csWord;
		if(lCurrentCharCol!=lCurrentLineLen)
			csWord=m_fn_csGetWordIn(lCurrentCharIndex,lWordStartIndex,lWordEndIndex);
		else
			csWord=m_fn_csGetWordBefore(lCurrentCharIndex,lWordStartIndex,lWordEndIndex);

		//Colors word
		if(!m_fn_bIsInLineCommentedText(csText,lWordStartIndex))
			m_fn_vColorText(lWordStartIndex,lWordEndIndex,m_fn_rgbGetColor(csWord));
	}
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetWordInAList(CStringList *p_clListString)
{
	CPA_EdIR_ActCondBoxDlg	dlg(p_clListString);
	CString									csString="";

	if(dlg.DoModal()==IDOK)
		csString=dlg.m_csSelectedString;

	SetFocus();

	return csString;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddAction(long lIndex,COLORREF rgbColor)
{
	CString	csWord=m_fn_csGetWordInAList(&gm_clActionWords);

	if(csWord!="")
		m_fn_vOnAddString(lIndex,csWord);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddCondition(long lIndex,COLORREF rgbColor)
{
	CString	csWord=m_fn_csGetWordInAList(&gm_clConditionWords);

	if(csWord!="")
		m_fn_vOnAddString(lIndex,csWord);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddDesignerAction(long lIndex,COLORREF rgbColor)
{
	CStringList	cslDesignerActionList;

	((CPA_EdIR_Frame *)GetParentFrame())->m_fn_vGetActionList(&cslDesignerActionList);

	CString	csWord=m_fn_csGetWordInAList(&cslDesignerActionList);

	if(csWord!="")
		m_fn_vOnAddString(lIndex,csWord);
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetCompletedWord(CString csWord,CStringList *pclStringList)
{
	CStringList	*pclListString=m_fn_pclGetListMatchWord(csWord,pclStringList);

	CString	csCompleteWord;

	switch(pclListString->GetCount())
	{
		case 1:
			csCompleteWord=pclListString->GetHead();
			break;

		case 0:
			csCompleteWord=csWord;
			break;

		default:
			csCompleteWord=m_fn_csGetWordInAList(pclListString);
			break;
	}

	delete pclListString;

	return csCompleteWord;
}

/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetCompletedWord(CString csWord)
{
	//Gets IA Objects
	CStringList	*pclIAObjects=fn_pclGetListIAObjects();

	//Adds knownwords
	POSITION	pos=gm_clKnownWords.GetHeadPosition();
	while(pos!=NULL)
		pclIAObjects->AddTail(gm_clKnownWords.GetNext(pos));

	//Gets complete word
	CString	csCompleteWord=m_fn_csGetCompletedWord(csWord,pclIAObjects);

	delete pclIAObjects;

	return csCompleteWord;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vCompleteWord(long lLine,long lCol)
{
	//Gets char index
	long	lFirstCharInLineIndex=LineIndex(lLine);
	long	lCharIndex=lCol+lFirstCharInLineIndex;

	//Gets text
	CString	csText;
	GetWindowText(csText);
	
	//Cursor is inside a word?
	BOOL	bInsideAWord=(lCharIndex!=0) &&
										 (lCharIndex!=csText.GetLength()) &&
										 (M_ISALNUM(csText[int(lCharIndex-1)])) &&
										 (M_ISALNUM(csText[int(lCharIndex)]));

	//Gets word
	long	lWordStartIndex;
	long	lWordEndIndex;
	CString	csWord;
	if(bInsideAWord)
		csWord=m_fn_csGetWordIn(lCharIndex,lWordStartIndex,lWordEndIndex);
	else
	{
		if(lCharIndex!=0)
			csWord=m_fn_csGetWordBefore(lCharIndex,lWordStartIndex,lWordEndIndex);
		else
			csWord=m_fn_csGetWordAfter(lCharIndex,lWordStartIndex,lWordEndIndex);
	}

	//Adds an action/condition word
	if(m_fn_bIsAKeyWord(csWord) && !bInsideAWord && lCharIndex!=0)
	{
		COLORREF	rgbWordColor=C_COMMENTCOLOR;

		if(csWord.CompareNoCase(M_TRANSLATE_KEYWORD_SCRIPT_TO_EDITOR("If"))==0)
		{
			if(!m_fn_bIsInLineCommentedText(csText,lCharIndex))
				rgbWordColor=C_CONDITIONWORDCOLOR;

			m_fn_vAddCondition(lCharIndex,rgbWordColor);
		}
		else
		if((csWord.CompareNoCase(M_TRANSLATE_KEYWORD_SCRIPT_TO_EDITOR("Then"))==0) ||
			 (csWord.CompareNoCase(M_TRANSLATE_KEYWORD_SCRIPT_TO_EDITOR("Else"))==0))
		{
			if(!m_fn_bIsInLineCommentedText(csText,lCharIndex))
				rgbWordColor=C_ACTIONWORDCOLOR;

			m_fn_vAddAction(lCharIndex,rgbWordColor);
		}
		else
		{
/* 
			//Cursor is not inside a word
			//Is there any space (or point comma .. non alnum char) between the previous word and the cursor
			if(lCharIndex>lWordEndIndex)
			{
				//Gets a new word
				CString csCompleteWord=m_fn_csGetCompletedWord("");

				m_fn_vOnAddString(lCharIndex,csCompleteWord);
			}
*/
			// don't try to complete an empty word
			;
			//else nothing !! (csWord is a KeyWord so it's a complete word)
		}
	}
	else
	{
		//Cursor is not at the beginnig
		if(lCharIndex!=0)
		{
/* 
			//Is there any space (or point comma .. non alnum char) between the previous word and the cursor
			if(lCharIndex>lWordEndIndex)
			{
				//Gets a new word
				CString csCompleteWord=m_fn_csGetCompletedWord("");

				m_fn_vOnAddString(lCharIndex,csCompleteWord);
			}
			else
*/
			// don't try to complete an empty word
			if(lCharIndex<=lWordEndIndex)
			{
				//Gets the complete word
				CString csCompleteWord=m_fn_csGetCompletedWord(csWord);

				if(csWord!=csCompleteWord)
				{
					CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
					m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
				}
			}
		}
		else
		//Cursor is at the beginning
		{
			//Gets the complete word
			CString csCompleteWord=m_fn_csGetCompletedWord(csWord);

			if(csWord!=csCompleteWord)
			{
				CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
				m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
			}
		}
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vDisplayIAObjectsAndCompleteWord(long lLine,long lCol)
{
	//Gets Type
	CStringList	*pclIAObjectsType=fn_pclGetListIATypeObjects();
	
	CString	csType=m_fn_csGetWordInAList(pclIAObjectsType);

	delete pclIAObjectsType;

	if(!csType.IsEmpty())
	{
		//Gets all objects with this type
		CStringList	*pclIAObjects=fn_pclGetListIAObjects(csType);
		
		//Gets char index
		long	lFirstCharInLineIndex=LineIndex(lLine);
		long	lCharIndex=lCol+lFirstCharInLineIndex;

		//Gets text
		CString	csText;
		GetWindowText(csText);
	
		//Cursor is inside a word?
		BOOL	bInsideAWord=(lCharIndex!=0) &&
											 (lCharIndex!=csText.GetLength()) &&
											 (M_ISALNUM(csText[int(lCharIndex-1)])) &&
											 (M_ISALNUM(csText[int(lCharIndex)]));

		//Gets word
		long	lWordStartIndex;
		long	lWordEndIndex;
		CString	csWord;
		if(bInsideAWord)
			csWord=m_fn_csGetWordIn(lCharIndex,lWordStartIndex,lWordEndIndex);
		else
		{
			if(lCharIndex!=0)
				csWord=m_fn_csGetWordBefore(lCharIndex,lWordStartIndex,lWordEndIndex);
			else
				csWord=m_fn_csGetWordAfter(lCharIndex,lWordStartIndex,lWordEndIndex);
		}

		//Cursor is not at the beginnig
		if(lCharIndex!=0)
		{
			//Is there any space (or point comma .. non alnum char) between the previous word and the cursor
			if(lCharIndex>lWordEndIndex)
			{
				//Gets a new word
				CString csCompleteWord=m_fn_csGetCompletedWord("",pclIAObjects);

				m_fn_vOnAddString(lCharIndex,csCompleteWord);
			}
			else
			{
				//Gets the complete word
				CString csCompleteWord=m_fn_csGetCompletedWord(csWord,pclIAObjects);

				if(csWord!=csCompleteWord)
				{
					CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
					m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
				}
			}
		}
		else
		//Cursor is at the beginning
		{
			//Gets the complete word
			CString csCompleteWord=m_fn_csGetCompletedWord(csWord,pclIAObjects);

			if(csWord!=csCompleteWord)
			{
				CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
				m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
			}
		}

		delete pclIAObjects;
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vSetText(CString csText)
{
	SetWindowText(csText);
	SetSel(0, 0);
}


/**********************************************************************************/
CString CPA_EdIR_EditCtrl::m_fn_csGetText()
{
	CString	csText;
	GetWindowText(csText);
	return csText;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vSetCursorPos(long lIndex)
{
	SetSel(lIndex,lIndex);
	m_lOldScrollPosition = GetScrollPosition().y;
}

/**********************************************************************************/

void CPA_EdIR_EditCtrl::m_fn_vSetCursorPos(long lIndex,long lScrollPosition, long lScrollPositionH, BOOL bScroll)
{
	//Sets the cursor position
	SetSel(lIndex,lIndex);

	CPoint	clActScroll = GetScrollPosition();

	if( bScroll )
		if( lScrollPositionH >= 0 )
			ScrollToPosition(CPoint(lScrollPositionH,lScrollPosition));
		else
			ScrollToPosition(CPoint(clActScroll.x,lScrollPosition));
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lGetLineNumberInPixel(long lIndex)
{
	return ( ( GetCharPos( LineIndex( LineFromChar(lIndex) ) ) ).y );
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bIsInLineCommentedText(long lIndex)
{
	CString	csText;
	GetWindowText(csText);
	return m_fn_bIsInLineCommentedText(csText, lIndex);
}

BOOL CPA_EdIR_EditCtrl::m_fn_bIsInLineCommentedText(CString &csText,long lIndex)
{
	long lSemiColonIndex = lIndex - 1;
	LPCSTR lpsz = (LPCSTR) csText;
	while((lSemiColonIndex >= 0) && (lpsz[lSemiColonIndex] != ';') && (lpsz[lSemiColonIndex] != '\r'))
		lSemiColonIndex--;
	if(lpsz[lSemiColonIndex] == ';')
		return TRUE;
	return FALSE;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vProcessLineCommentInsertion(long lInsertionIndex)
{
	if(!m_fn_bIsInLineCommentedText(lInsertionIndex))
	{
		long	lLineLen=LineLength(lInsertionIndex);
		long	lStartLineIndex=LineIndex(LineFromChar(lInsertionIndex));
		m_fn_vColorText(lInsertionIndex,lStartLineIndex+lLineLen,C_COMMENTCOLOR);
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vProcessLineCommentDeletion(long lDeletionIndex)
{
	if(!m_fn_bIsInLineCommentedText(lDeletionIndex))
	{
		long	lLineCommentIndex=m_fn_lFindCharInLine(lDeletionIndex,';');

		if(lLineCommentIndex!=-1)
			m_fn_vColorText(lDeletionIndex,lLineCommentIndex);
		else
		{
			long	lLineLen=LineLength(lDeletionIndex);
			long	lStartLineIndex=LineIndex(LineFromChar(lDeletionIndex));
			m_fn_vColorText(lDeletionIndex,lStartLineIndex+lLineLen);
		}
	}
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lFind(long lStartIndex,char cLookedFor)
{
	//Gets text
	CString	csText;
	GetWindowText(csText);

	CString	csSearchInText=csText.Right(csText.GetLength()-lStartIndex);

	//Looks for char
	long	lIndex=csSearchInText.Find(cLookedFor);

	if(lIndex!=-1)
		return lStartIndex+lIndex;
	else
		return -1;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lReverseFind(long lEndIndex,char cLookedFor)
{
	//Gets text
	CString	csText;
	GetWindowText(csText);

	CString	csSearchInText=csText.Left(lEndIndex);

	//Looks for char
	long	lIndex=csSearchInText.ReverseFind(cLookedFor);

	return lIndex;
}

/**********************************************************************************/
long CPA_EdIR_EditCtrl::m_fn_lFindCharInLine(long lStartIndex,char cLookedFor)
{
	//Gets text
	CString	csText;
	long	lColIndex=lStartIndex-LineIndex(LineFromChar(lStartIndex));
	GetWindowText(csText);

	CString	csSearchInText=csText.Mid(lStartIndex,LineLength(lStartIndex)-lColIndex+1);

	//Looks for char
	long	lIndex=csSearchInText.Find(cLookedFor);

	if(lIndex!=-1)
		return lStartIndex+lIndex;
	else
		return -1;
}														   

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	long	lStartIndex;
	long	lEndIndex;
	CString	csKeyWord;
	
	csKeyWord=m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),lStartIndex,lEndIndex);
	if(csKeyWord.IsEmpty())
	{
		csKeyWord=m_fn_csGetWordAfter(m_fn_lGetCurrentCharIndex(),lStartIndex,lEndIndex);
		if(csKeyWord.IsEmpty())
			csKeyWord=m_fn_csGetWordBefore(m_fn_lGetCurrentCharIndex(),lStartIndex,lEndIndex);
	}

	if(m_fn_bIsAKnownWord(csKeyWord))
		::WinHelp(m_hWnd,g_pclAIInterface->m_csBrainHelpFileNameAndPath,HELP_KEY,(unsigned long)LPCTSTR(csKeyWord));
	else
		::WinHelp(m_hWnd,g_pclAIInterface->m_csBrainHelpFileNameAndPath,HELP_CONTEXT,HID_RULES_ZONE);
	
	return FALSE;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vOnChar(long lIndex,UINT nChar)
{
	CPA_EdIR_UndoPressAKey	*pclUndoPressAKey;

	CPoint	clScrollPos=GetScrollPosition();

	pclUndoPressAKey=new CPA_EdIR_UndoPressAKey(this,nChar,lIndex,clScrollPos.y);

	m_clEditManager.AskFor(pclUndoPressAKey);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddChar(long lIndex,long lScrollPosition,UINT nChar)
{
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		m_fn_vReplaceText(lIndex,lIndex,CString((char)nChar),C_DEFAULTCOLOR);

		if(nChar==';')
			m_fn_vProcessLineCommentInsertion(lIndex);

		long lCharLine=LineFromChar(lIndex);
		long lCharCol=lIndex-LineIndex(lCharLine);
		if(m_fn_bIsInLineCommentedText(lIndex))
			m_fn_vColorText(lIndex,lIndex+1,C_COMMENTCOLOR);
		else
			m_fn_vColorText(lIndex,lIndex+1,C_DEFAULTCOLOR);

		m_fn_vUpdateColorText(lCharLine,lCharCol);

		//Sets cursor position
		m_fn_vSetCursorPos(lIndex+1,lScrollPosition);
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vDelChar(long lIndex,long lScrollPosition,UINT nChar)
{
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		m_fn_vReplaceText(lIndex,lIndex+1,"",C_DEFAULTCOLOR);

		if(nChar==';')
			m_fn_vProcessLineCommentDeletion(lIndex);

		long lCharLine=LineFromChar(lIndex);
		long lCharCol=lIndex-LineIndex(lCharLine);
		m_fn_vUpdateColorText(lCharLine,lCharCol);

		//Sets cursor position
		m_fn_vSetCursorPos(lIndex,lScrollPosition);
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vOnAddString(long lIndex,CString csString)
{
	CPA_EdIR_UndoPaste	*pclUndoPaste;

	CPoint	clScrollPos = GetScrollPosition();

	pclUndoPaste=new CPA_EdIR_UndoPaste(this,csString,lIndex,clScrollPos.y);

	m_clEditManager.AskFor(pclUndoPaste);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vOnDelString(long lIndex,CString csString,UINT nDeletionType,BOOL bSelected)
{
	CPA_EdIR_UndoDelete	*pclUndoDelete;

	CPoint	clScrollPos=GetScrollPosition();

	//Gets current position
	long lCurrentIndex=lIndex;

	if(nDeletionType==C_BACKSPACE && !bSelected)
		lCurrentIndex-=csString.GetLength();

	pclUndoDelete=new CPA_EdIR_UndoDelete(this,csString,lCurrentIndex,clScrollPos.y);

	m_clEditManager.AskFor(pclUndoDelete);
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vAddString(long lIndex,long lScrollPosition,CString csString)
{
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		m_fn_vReplaceText(lIndex,lIndex,csString,C_DEFAULTCOLOR);

		if(!m_fn_bIsInLineCommentedText(lIndex))
		{
			long	lLineLen=LineLength(lIndex+csString.GetLength());
			long	lStartLineIndex=LineIndex(LineFromChar(lIndex+csString.GetLength()));
			m_fn_vColorText(lIndex,lStartLineIndex+lLineLen);
		}
		else
		{
			m_fn_vColorText(lIndex,lIndex+csString.GetLength(),C_COMMENTCOLOR);

			if(csString.Find(C_RETURN)!=-1)
			{
				long	lFirstEndLineIndex=m_fn_lReverseFind(lIndex+csString.GetLength(),CString(C_RETURN)[1]);
				long	lSecondEndLineIndex=m_fn_lFind(lFirstEndLineIndex+1,CString(C_RETURN)[1]);
				m_fn_vColorText(lFirstEndLineIndex,lSecondEndLineIndex);
			}
		}

		long lCharLine=LineFromChar(lIndex);
		long lCharCol=lIndex-LineIndex(lCharLine);
		m_fn_vUpdateColorText(lCharLine,lCharCol);

		//Sets cursor position
		m_fn_vSetCursorPos(lIndex+csString.GetLength(),lScrollPosition);
	}
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vDelString(long lIndex,long lScrollPosition,CString csString)
{
	if(!((CPA_EdIR_View *)GetParent())->m_bIsReadOnly)
	{
		m_fn_vReplaceText(lIndex,lIndex+csString.GetLength(),"",C_DEFAULTCOLOR);

		if(csString.Find(';')!=-1)
			m_fn_vProcessLineCommentDeletion(lIndex);

		long lCharLine=LineFromChar(lIndex);
		long lCharCol=lIndex-LineIndex(lCharLine);
		m_fn_vUpdateColorText(lCharLine,lCharCol);

		if(csString.Find(C_RETURN)!=-1 && m_fn_bIsInLineCommentedText(lIndex))
		{
			long	lStartLineIndex=LineIndex(LineFromChar(lIndex));
			long	lEndLineIndex=m_fn_lFind(lIndex,CString(C_RETURN)[1]);

			m_fn_vColorText(lIndex,lEndLineIndex,C_COMMENTCOLOR);
		}

		//Sets cursor position
		m_fn_vSetCursorPos(lIndex,lScrollPosition);
	}
}

/**********************************************************************************/
BOOL CPA_EdIR_EditCtrl::m_fn_bHasBeenModified()
{
	return m_bHasBeenModified;
}

/**********************************************************************************/
void CPA_EdIR_EditCtrl::m_fn_vSetModified(BOOL bModified)
{
	if(bModified)
	{
		//Notifications
		CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
		CPA_Actor		*pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
		if(pclSelectedActor)
		{
			if(pclSelectedActor->m_fn_bIsAnInstance())
				pclSelectedActor=pclSelectedActor->m_fn_pclGetModel();
			CPA_EdIR_Brain	*pclBrain=((EdActors_EditorActorModel *)pclSelectedActor->m_fn_pclGetEditorActor())->m_pclBrain;
			pclBrain->m_fn_vNotify();
		}
	}

	m_bHasBeenModified=bModified;
}

long CPA_EdIR_EditCtrl::m_fn_lFindString(BOOL bCaseSensitive , long  lStartIndex,CString  scString)
{
	CString	csText;
	GetWindowText(csText);

	CString	csSearchInText=csText.Right(csText.GetLength()-lStartIndex);
	long l_Size =  scString.GetLength();

	//Looks for string
	long	lIndex=FindString(bCaseSensitive , csSearchInText ,scString);

	if(lIndex!=-1)
	{
		if (m_fnGetSelectAString())
		{
			m_fn_vColorText( m_lFirstSelectIndex,m_lLastSelectIndex,col_last);
			long lCharLine=LineFromChar(m_lFirstSelectIndex);
			long lCharCol=m_lFirstSelectIndex-LineIndex(lCharLine);

			m_fn_vUpdateColorText(lCharLine,lCharCol);
			RestoreCol();
		}
		col_last = m_fn_rgbGetColor(scString);
		m_lFirstSelectIndex = lStartIndex+lIndex ;
		m_lLastSelectIndex = lStartIndex+lIndex+l_Size;
		m_fnSetSelectAString(TRUE);

		COLORREF col_selected=RGB( 255, 0, 255); 		
		m_fn_vColorText( lStartIndex+lIndex ,lStartIndex+lIndex+l_Size,col_selected);

		long	lIndex1=FindString(bCaseSensitive ,csSearchInText , scString);
		if (lIndex1==-1)
			m_fnSetSelectAString(FALSE);

		return lStartIndex+lIndex;

	}
	else
	{
 		if (m_fnGetSelectAString())
		{
			m_fn_vColorText( m_lFirstSelectIndex,m_lLastSelectIndex,col_last);
			long lCharLine=LineFromChar(m_lFirstSelectIndex);
			long lCharCol=m_lFirstSelectIndex-LineIndex(lCharLine);

				m_fn_vUpdateColorText(lCharLine,lCharCol);
				RestoreCol();
				m_fnSetSelectAString(FALSE);
			return -1;	
		}
		else
		{
			m_bSearchIsFinished=TRUE;
			return -1;
		}
	}

}

void CPA_EdIR_EditCtrl::m_fn_ReplaceString(long lIndex, CString scString1, CString scString2)
{
	m_fn_vOnDelString(  lIndex, scString1, 8 , 0);
	m_fn_vOnAddString( lIndex-scString1.GetLength(), scString2);

}

void CPA_EdIR_EditCtrl::m_fn_ReplaceAllString(BOOL bCaseSensitive , long lIndex, CString scString1, CString scString2)
{
		long l_Size1 =  scString1.GetLength();
		long l_Size2 =  scString2.GetLength();
		CString	csText;
		GetWindowText(csText);
		long l_size = csText.GetLength();
		long lPos = lIndex;
		if (m_fn_lFindString(bCaseSensitive,lIndex,  scString1 )==-1)
		{
			m_bSearchIsFinished = TRUE;
		}
		else
		{
			long posModif = m_fn_lFindString(bCaseSensitive ,lPos , scString1);

			while (lPos<l_size)
			{

			if (posModif>=0 )
				{
 					m_fn_ReplaceString( m_lLastSelectIndex,  scString1 , scString2);
					posModif = m_fn_lFindString( bCaseSensitive , m_lLastSelectIndex,  scString1 );				
					lPos=posModif+1;
					l_size+=l_Size2-l_Size1;

				}
			else 
				lPos = l_size;
			}
		}
}

void CPA_EdIR_EditCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	long lWordStartIndex , lWordEndIndex;
	long index = m_fn_lGetCurrentCharIndex();
	m_fn_csGetWordIn( index, lWordStartIndex, lWordEndIndex);		
	SetSel(lWordStartIndex  , lWordEndIndex);
}

long CPA_EdIR_EditCtrl::FindString(BOOL bCaseSensitive , CString cs_StringSource , CString cs_StringToFind)
{
	long lIndex ;
	long lWordStartIndex,  lWordEndIndex;
	long lLength = cs_StringSource.GetLength();
	long lLengthToFind = cs_StringToFind.GetLength();
	lWordStartIndex=m_fn_lSkipRightNonAlphaNumChar(cs_StringSource,0);
	lWordEndIndex=m_fn_lSkipRightAlphaNumChar(cs_StringSource,lWordStartIndex+1);

 	if (!bCaseSensitive)
	{
		CString cs_StringSourceLow = cs_StringSource ;
		cs_StringSourceLow.MakeLower();
		CString cs_StringToFindLow = cs_StringToFind ;
		cs_StringToFindLow.MakeLower();
		lIndex = cs_StringSourceLow.Find(cs_StringToFindLow);
		return lIndex;
	}
	else
	{
		lIndex = cs_StringSource.Find(cs_StringToFind);
		return lIndex;
	}
}


void CPA_EdIR_EditCtrl::RestoreCol()
{
//			m_fn_vColorText( m_lFirstSelectIndex,m_lLastSelectIndex,col_last);
			long lCharLine=LineFromChar(m_lFirstSelectIndex);
			long lCharCol=m_lFirstSelectIndex-LineIndex(lCharLine);
			if(m_fn_bIsInLineCommentedText(m_lFirstSelectIndex))
				m_fn_vColorText(m_lFirstSelectIndex,m_lLastSelectIndex,C_COMMENTCOLOR);
			else
				m_fn_vColorText(m_lFirstSelectIndex,m_lLastSelectIndex,C_DEFAULTCOLOR);

				m_fn_vUpdateColorText(lCharLine,lCharCol);

				m_fnSetSelectAString(FALSE);

}

void CPA_EdIR_EditCtrl::OnRButtonDown(UINT nFlags,CPoint point)
{
	CString word;
	long first , last;

	SendMessage(WM_LBUTTONDOWN, nFlags, MAKELPARAM(point.x, point.y));
	word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first , last);

	CRect	clRect;
	GetWindowRect(&clRect);
	
	CMenu	Menu;
	Menu.LoadMenu(IDR_IR_MENU_POPUP_RULES);
		
	CMenu	*SousMenu=Menu.GetSubMenu(0);

	CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_Actor	*pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
	if(pclSelectedActor) 
	{	
		CTL_Editor_DataList	*m_pclListOfDesignerVariableType = pclSelectedActor->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
		CTL_Editor_Data	*pclData = (CTL_Editor_Data	*)m_pclListOfDesignerVariableType->m_fn_pclGetDataFromName(word);
		if(pclData /*&& MessageBox(word, "Add to Watch Window", MB_OK)*/)
		{
			SousMenu->EnableMenuItem(ID_IR_RULES_ADDWATCH, MF_ENABLED);
		}
	}

	long l_id = fn_lGetProcedureIdFromEditorName(word);
	CString cs_Params = fn_csGetProcedureParams(l_id);

	if( !cs_Params.IsEmpty() )
	{
		SousMenu->EnableMenuItem( 4, MF_BYPOSITION );
		CMenu	*SousSousMenu = SousMenu->GetSubMenu(4);

		SousSousMenu->RemoveMenu( 0, MF_BYPOSITION );
		for( int i = 0; i < cs_Params.GetLength(); i++ )
			SousSousMenu->AppendMenu( MF_STRING, 0, fn_csGetParamTypeInEnglish( cs_Params[i] ) );
	}

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


void CPA_EdIR_EditCtrl::m_fn_vDisplayIAObjectsAndCompleteWordNew( long lLine, long lCol )
{ 
    //Gets char index
    long	lFirstCharInLineIndex=LineIndex(lLine);
    long	lCharIndex=lCol+lFirstCharInLineIndex;

    //Gets text
    CString	csText;
    GetWindowText(csText);
	
    //Cursor is inside a word?
    BOOL	bInsideAWord=(lCharIndex!=0) && (lCharIndex!=csText.GetLength()) &&
									    (M_ISALNUM(csText[int(lCharIndex-1)])) &&
									    (M_ISALNUM(csText[int(lCharIndex)]));

	//Gets word
	long	lWordStartIndex;
	long	lWordEndIndex;
	CString	csWord;
	if(bInsideAWord)
		csWord=m_fn_csGetWordIn(lCharIndex,lWordStartIndex,lWordEndIndex);
	else
	{
		if(lCharIndex!=0)
			csWord=m_fn_csGetWordBefore(lCharIndex,lWordStartIndex,lWordEndIndex);
		else
			csWord=m_fn_csGetWordAfter(lCharIndex,lWordStartIndex,lWordEndIndex);
	}

	//Cursor is not at the beginnig
	if(lCharIndex!=0)
	{
		//Is there any space (or point comma .. non alnum char) between the previous word and the cursor
		if(lCharIndex>lWordEndIndex)
		{
			//Gets a new word
			CString csCompleteWord=m_fn_csGetCompletedWordNew("");
			m_fn_vOnAddString(lCharIndex,csCompleteWord);
		}
		else
		{
			//Gets the complete word
			CString csCompleteWord=m_fn_csGetCompletedWordNew(csWord);

			if(csWord!=csCompleteWord)
			{
				CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
				m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
			}
		}
	}
	else
	//Cursor is at the beginning
	{
		//Gets the complete word
		CString csCompleteWord=m_fn_csGetCompletedWordNew(csWord);
		if(csWord!=csCompleteWord)
		{
			CString	csWordToAdd=csCompleteWord.Right(csCompleteWord.GetLength()-csWord.GetLength());
			m_fn_vOnAddString(lWordStartIndex+csWord.GetLength(),csWordToAdd);
		}
	} 
}

CString CPA_EdIR_EditCtrl::m_fn_csGetCompletedWordNew(CString csWord)
{
   CPA_EdIR_Select_IAObj SelDiag( &g_oBaseFrame, m_bParentsSeleIAObj, csWord);
   CString csString = "";

   if( SelDiag.DoModal() == IDOK )
	   csString = SelDiag.m_csRetWord;
   
   m_bParentsSeleIAObj = SelDiag.m_bParentsShow;
   SetFocus();

   return csString;
}

void CPA_EdIR_EditCtrl::m_fn_vResizeToFitParent()
{ 
	CRect clRect;
	GetParent()->GetClientRect(&clRect);
	MoveWindow( 12, 0, clRect.Width() - 13, clRect.Height() - 1);
	UpdateWindow();
}


CPoint CPA_EdIR_EditCtrl::GetScrollPosition()
{
	CPoint clPoint;
	clPoint.x = GetScrollPos( SB_HORZ );
	clPoint.y = GetScrollPos( SB_VERT );
	return clPoint;
}

void CPA_EdIR_EditCtrl::ScrollToPosition(CPoint clPoint)
{
	LineScroll( - ( GetScrollPos( SB_VERT ) ) / m_clFontSize.cy, - ( GetScrollPos( SB_HORZ ) ) / m_clFontSize.cx );
	LineScroll( clPoint.y / m_clFontSize.cy, clPoint.x / m_clFontSize.cx );
	UpdateWindow();
}


void CPA_EdIR_EditCtrl::m_fn_vAddBlockTabulation( )
{
	long lStart, lEnd;
	CString csString;
	BOOL bSel = FALSE;

	GetSel( lStart, lEnd );
	if( lStart != lEnd )
		bSel = TRUE;

	GetWindowText( csString );

	long j = 1;
	SetSel( lStart,lStart );
	m_fn_vOnChar(lStart,'\t');	

	for( long i = lStart; i < lEnd - 2 ; i++ )
		if( LPCSTR(csString)[i] == '\r' )
		{
			m_fn_vOnChar( i + 2 + j, '\t');
			j++;
		}
	if( bSel )
		SetSel( lStart, lEnd + j );
}

void CPA_EdIR_EditCtrl::m_fn_vDeleteBlockTabulation( )
{

	long lStart, lEnd, lScrollPos = (GetScrollPosition()).y;
	CString csString;
	BOOL bSel = FALSE;

	GetSel( lStart, lEnd );
	if( lStart != lEnd )
		bSel = TRUE;

	GetWindowText( csString );

	while( lStart && ( LPCSTR(csString)[lStart - 1] == '\t' ) )
		lStart--;
	if( LPCSTR(csString)[lStart] != '\t' )
		return;
	for( long i = lStart; i < lEnd - 2 ; i++ )
		if( LPCSTR(csString)[i] == '\r' )
			if( LPCSTR(csString)[i + 2] != '\t' )
				return;
	
	long j = 1;
	SetSel( lStart,lStart );
	m_fn_vOnDelString(lStart, '\t', 0, 0);	

	for( i = lStart; i < lEnd - 2 ; i++ )
		if( LPCSTR(csString)[i] == '\r' )
		{
			m_fn_vOnDelString( i + 2 - j, '\t', 0, 0 );
			j++;
		}

	if( bSel )
		SetSel( lStart, lEnd - j );

}

void CPA_EdIR_EditCtrl::m_fn_vFindAndSelTextPrevious( long lEnd, CString csText )
{
	if( csText.IsEmpty() )
		return;
	long lIndex, lIndex1 = -1;
	FINDTEXTEX  stFind;
	stFind.chrg.cpMin = 0;
	stFind.chrg.cpMax = lEnd;
	stFind.lpstrText = (char*)LPCSTR( csText );
	while( ( lIndex = FindText( 0, &stFind ) ) >= 0 )
	{	lIndex1 = lIndex;
		stFind.chrg.cpMin = lIndex + 1;
	}
	if( lIndex1 >= 0 )
		SetSel( lIndex1, lIndex1 + csText.GetLength() );
}

void CPA_EdIR_EditCtrl::m_fn_vFindAndSelTextNext( long lStart, CString csText )
{
	if( csText.IsEmpty() )
		return;
	long lIndex;
	FINDTEXTEX  stFind;
	stFind.chrg.cpMin = lStart;
	stFind.chrg.cpMax = GetTextLength();
	stFind.lpstrText = (char*)LPCSTR( csText );
	lIndex = FindText( 0, &stFind );
	if( lIndex >= 0)
		SetSel( lIndex, lIndex + csText.GetLength() );
	
}

void CPA_EdIR_EditCtrl::OnIrRulesPrevious() 
{
	long		first, last;
	CString		word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);
	m_csLastSearched = word;

	m_fn_vFindAndSelTextPrevious( m_fn_lGetCurrentCharIndex(), word );

}

void CPA_EdIR_EditCtrl::OnIrRulesNext() 
{
	long		first, last;
	CString		word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);
	m_csLastSearched = word;

	m_fn_vFindAndSelTextNext( m_fn_lGetCurrentCharIndex(), word );
	
}

void CPA_EdIR_EditCtrl::OnIrRulesFindreplace() 
{
	long		first, last;
	m_csLastSearched = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);
	HINSTANCE hOldInstance=AfxGetResourceHandle();
	AfxSetResourceHandle(g_stAIIdentity.hModule);
	CPA_EdIR_DiagReplace m_DReplace(this);
	m_DReplace.DoModal();
	AfxSetResourceHandle(hOldInstance);	
	SetFocus();
}


void CPA_EdIR_EditCtrl::OnIrRulesAddwatch() 
{
	long		first, last;
	CString		word = m_fn_csGetWordIn(m_fn_lGetCurrentCharIndex(),first, last);
	CPA_DLLBase	*pclActorDLL=(CPA_DLLBase*)(g_pclAIInterface->GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName));
	CPA_Actor	*pclSelectedActor=(CPA_Actor *)pclActorDLL->OnQueryAction(g_pclAIInterface,C_uiActor_GetSelectedActor,0);
	if(pclSelectedActor) 
	{	
		CTL_Editor_DataList	*m_pclListOfDesignerVariableType = pclSelectedActor->m_fn_pclGetEditorActor()->m_pclListOfDesignerVariableType;
		CTL_Editor_Data	*pclData = (CTL_Editor_Data	*)m_pclListOfDesignerVariableType->m_fn_pclGetDataFromName(word);
		if(pclData /*&& MessageBox(word, "Add to Watch Window", MB_OK)*/)
		{
			fn_vAddValueInWatch(pclData);
			fn_vSetInWatch(pclData,TRUE);
		}
	}	
}

void CPA_EdIR_EditCtrl::m_fn_vSetFontTypeAndSize(  )
{
	CHARFORMAT stCharFormat;
	CString csString;

	CString csFontType = ((CPA_EdIR_View*)GetParent())->m_fn_csGetFontType();
	int     nSize      = ((CPA_EdIR_View*)GetParent())->m_fn_nGetFontSize();
	
	if( m_csFontType != csFontType || m_nFontSize != nSize )
	{
		m_csFontType = csFontType;
		m_nFontSize = nSize;

		stCharFormat.cbSize = sizeof( stCharFormat );
		stCharFormat.dwMask = CFM_FACE | CFM_SIZE;
		strcpy( stCharFormat.szFaceName, LPCSTR( m_csFontType ) );
		stCharFormat.yHeight = m_nFontSize;
		SetDefaultCharFormat( stCharFormat );

//to set the text with the new default char format				
		GetWindowText( csString );
		SetWindowText( csString );

#ifdef DAVE 
		m_fn_vColorLines(0, GetLineCount());
#else
		m_fn_vColorText( 0, GetTextLength() );
#endif

		m_clFontSize = ((CPA_EdIR_View*)GetParent())->m_fn_clGetFontExtent( );
	}
}

void CPA_EdIR_EditCtrl::OnIrChangeFont() 
{
	((CPA_EdIR_View*)GetParent())->m_fn_vChooseFont();
}

#endif //D_ED_IR_ACTIVE

