// PA_EdIR_DiagReplace.cpp : implementation file
//

#include "stdafx.h"
//#include "edir.h"
#include "EdIRReplce.hpp"

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

#include "ITF/FrmGest.hpp"

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DiagReplace dialog


CPA_EdIR_DiagReplace::CPA_EdIR_DiagReplace(CPA_EdIR_EditCtrl *EditCtrl ,CWnd* pParent /*=NULL*/)
	: CDialog(CPA_EdIR_DiagReplace::IDD, &g_oBaseFrame)
{
	m_EditCtrl = EditCtrl;
	m_EditCtrl->m_fnSetSelectAString(FALSE);
	m_bFirst = TRUE;
	//{{AFX_DATA_INIT(CPA_EdIR_DiagReplace)
	m_bCaseSensitive = FALSE;
	m_bAllAi = FALSE;
	//}}AFX_DATA_INIT
	m_DiagView = ((CPA_EdIR_Frame*)(m_EditCtrl->GetParentFrame()))->m_fn_pclGetDiagrammeView();
}


void CPA_EdIR_DiagReplace::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_DiagReplace)
	DDX_Control(pDX, IDC_COMBO_REPLACE, m_replace);
	DDX_Control(pDX, IDC_COMBO_FIND, m_find);
	DDX_Check(pDX, IDC_CHECK_CASE_SENSITIVE, m_bCaseSensitive);
	DDX_Check(pDX, IDC_CHECK_ALL_AI, m_bAllAi);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPA_EdIR_DiagReplace, CDialog)
	//{{AFX_MSG_MAP(CPA_EdIR_DiagReplace)
	ON_BN_CLICKED(IDC_BUTTON_FIND_NEXT, OnButtonFindNext)
	ON_BN_CLICKED(IDC_BUTTON_REPLACE, OnButtonReplace)
	ON_BN_CLICKED(IDC_BUTTON_REPLACE_ALL, OnButtonReplaceAll)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_DiagReplace message handlers


void CPA_EdIR_DiagReplace::OnButtonFindNext() 
{
		UpdateData(TRUE);
		CString	csText1;
		m_find.GetWindowText(csText1);
		CString	csText2;
		m_replace.GetWindowText(csText2);
		m_EditCtrl->m_bSearchIsFinished=FALSE;
		long l_SizeFind =  csText1.GetLength();
		long l_SizeReplace =  csText2.GetLength();
		long l_find;
		long iCount;
		long iActive;

		if (!fn_ExistFind(csText1)) m_find.InsertString(0,csText1);
		if (!fn_ExistReplace(csText2)) m_replace.InsertString(0,csText2);

//Stefan Dumitrean 7-07-98 ( Find-Replace scroll )
//		long index = m_EditCtrl->m_fn_lGetCurrentCharIndex();
		long lStart, index;
		m_EditCtrl->GetSel( lStart, index );
//End Stefan Dumitrean 7-07-98 ( Find-Replace scroll )
		if (m_bFirst)
		{
			l_find = m_EditCtrl->m_fn_lFindString(m_bCaseSensitive ,(index- l_SizeFind)>=0 ?index- l_SizeFind:0,csText1);
			 m_bFirst=FALSE;
		}
		else 
			 l_find = m_EditCtrl->m_fn_lFindString(m_bCaseSensitive ,index ,csText1);


		if (l_find == -1)
		{
			if (m_EditCtrl->m_fnGetSelectAString())
			{
				m_EditCtrl->m_fn_vColorText( m_EditCtrl->m_lFirstSelectIndex,m_EditCtrl->m_lLastSelectIndex,m_EditCtrl->col_last);
			}
 			if (m_bAllAi)
			{
		 		iCount = m_DiagView->m_clBehaviourListBox.GetCount();
				iActive = m_DiagView->m_clBehaviourListBox.GetCurSel();
				if (iActive < iCount-1)
				{
					m_DiagView->m_clBehaviourListBox.SetCurSel(iActive+1);
					CPA_EdIR_Behaviour	*pclBehaviour;
					pclBehaviour=(CPA_EdIR_Behaviour *)m_DiagView->m_clBehaviourListBox.GetItemDataPtr((m_DiagView->m_clBehaviourListBox).GetCurSel());
					m_DiagView->m_fn_vOldActiveBehaviourHasChanged(pclBehaviour,FALSE);
					m_EditCtrl = &(m_DiagView->m_fn_pclGetActiveBehaviour())->m_clEditCtrl;
					m_EditCtrl->ShowWindow(TRUE);
					m_bFirst = TRUE;
					m_EditCtrl->m_fnSetSelectAString(FALSE);
				}
			}
		}
		else
		{
//Stefan Dumitrean 6-07-98 ( Find-Replace scroll )
			m_EditCtrl->HideSelection( FALSE, TRUE );
			m_EditCtrl->SetSel( l_find, l_find + l_SizeFind );
//			m_EditCtrl->m_fn_vSetCursorPos(l_find+l_SizeFind+1);
//End Stefan Dumitrean 6-07-98 ( Find-Replace scroll )
		}
		if (m_EditCtrl->m_bSearchIsFinished == TRUE)
		{
			AfxMessageBox("End of file" );
			SetFocus();
		}
}

void CPA_EdIR_DiagReplace::OnButtonReplace() 
{
		UpdateData(TRUE);
		CString	csText1;
		m_find.GetWindowText(csText1);
		CString	csText2;
		m_replace.GetWindowText(csText2);
		long lFind = csText1.GetLength();
		long l_find ;
		long iCount;
		long iActive;

		m_EditCtrl->m_bSearchIsFinished = FALSE;
		if (!m_EditCtrl->m_fnGetSelectAString())
		{
			long lCharIndex = m_EditCtrl->m_fn_lGetCurrentCharIndex();
			long lFirst,lLast;
			m_EditCtrl->m_fn_csGetWordIn(lCharIndex  , lFirst, lLast);
			l_find = m_EditCtrl->m_fn_lFindString(m_bCaseSensitive , lFirst ,csText1);
			if (l_find ==-1 && m_bAllAi)
			{
		 		iCount = m_DiagView->m_clBehaviourListBox.GetCount();
				iActive = m_DiagView->m_clBehaviourListBox.GetCurSel();
				if (iActive < iCount-1)
				{
					m_DiagView->m_clBehaviourListBox.SetCurSel(iActive+1);
					CPA_EdIR_Behaviour	*pclBehaviour;
					pclBehaviour=(CPA_EdIR_Behaviour *)m_DiagView->m_clBehaviourListBox.GetItemDataPtr((m_DiagView->m_clBehaviourListBox).GetCurSel());
					m_DiagView->m_fn_vOldActiveBehaviourHasChanged(pclBehaviour,FALSE);
					m_EditCtrl = &(m_DiagView->m_fn_pclGetActiveBehaviour())->m_clEditCtrl;
					m_EditCtrl->ShowWindow(TRUE);
					m_bFirst = TRUE;
					m_EditCtrl->m_fnSetSelectAString(FALSE);
				}
			}
//Stefan Dumitrean 6-07-98 ( Find-Replace scroll )
			else
			{
				m_EditCtrl->HideSelection( FALSE, TRUE );
				m_EditCtrl->SetSel( l_find, l_find + lFind );
			}
//End Stefan Dumitrean 6-07-98 ( Find-Replace scroll )

		}
		else
		{
			CString csText;
			m_EditCtrl->GetWindowText(csText);
			long l1 = m_EditCtrl->m_lFirstSelectIndex;
			long l2 = lFind;
			CString csReplaceString = csText.Mid(l1, l2);
			m_EditCtrl->m_fn_ReplaceString(m_EditCtrl->m_lLastSelectIndex,  csReplaceString , csText2);
			l_find =m_EditCtrl->m_fn_lFindString(m_bCaseSensitive ,m_EditCtrl->m_lLastSelectIndex,  csText1 );
//Stefan Dumitrean 6-07-98 ( Find-Replace scroll )
			m_EditCtrl->HideSelection( FALSE, TRUE );
			m_EditCtrl->SetSel( l_find, l_find + lFind );

//			m_EditCtrl->m_fn_vSetCursorPos(l_find+lFind +1);
//End Stefan Dumitrean 6-07-98 ( Find-Replace scroll )
		}
		if (m_EditCtrl->m_bSearchIsFinished == TRUE)
		{
			AfxMessageBox("End of file" );
			SetFocus();
		}
}

void CPA_EdIR_DiagReplace::OnButtonReplaceAll() 
{
	 	int iCount;
		int iActiveBehaviour;
		HCURSOR     hWaitCursor;
		HCURSOR     hOldCursor;

		UpdateData(TRUE);
		CString	csText1;
		m_find.GetWindowText(csText1);
		CString	csText2;
		m_replace.GetWindowText(csText2);
		m_EditCtrl->m_bSearchIsFinished = FALSE;
			if(!m_bAllAi)
			{
				m_EditCtrl->m_fn_ReplaceAllString(m_bCaseSensitive ,0 , csText1 , csText2);
			}
			else
			{
				hWaitCursor = AfxGetApp()->LoadStandardCursor( IDC_WAIT );
				hOldCursor = SetCursor( hWaitCursor );
 				iCount = m_DiagView->m_clBehaviourListBox.GetCount();
				for (iActiveBehaviour=0;iActiveBehaviour<=iCount-1;iActiveBehaviour++)
					{
						m_DiagView->m_clBehaviourListBox.SetCurSel(iActiveBehaviour);
						CPA_EdIR_Behaviour	*pclBehaviour;
						pclBehaviour=(CPA_EdIR_Behaviour *)m_DiagView->m_clBehaviourListBox.GetItemDataPtr((m_DiagView->m_clBehaviourListBox).GetCurSel());
						m_DiagView->m_fn_vOldActiveBehaviourHasChanged(pclBehaviour,FALSE);
						m_EditCtrl = &(m_DiagView->m_fn_pclGetActiveBehaviour())->m_clEditCtrl;
						m_EditCtrl->m_fn_ReplaceAllString(m_bCaseSensitive ,0 , csText1 , csText2);
					}
			}
			SetCursor( hOldCursor );
			if (m_EditCtrl->m_bSearchIsFinished == TRUE)
			AfxMessageBox("End of file" );
			SetFocus();
}

BOOL CPA_EdIR_DiagReplace::OnInitDialog() 
{

	CDialog::OnInitDialog();
	GetDlgItem(IDC_COMBO_FIND)->SetFocus();
    FILE *File1;
    FILE *File2;
	char text[512];
	File1=fopen("\\cpa\\tempgrp\\tia\\ListAIFind.lst","r") ;
    if(File1)
	{	
		fscanf(File1, "%s" , text);
		if (feof(File1))
		{
		}
		else
		{
			while(!feof(File1))
			{
				if (text !="\n")
				m_find.AddString(text);
				fscanf(File1, "%s" , text);
			}
		}
		fclose(File1);

	}

	File2=fopen("\\cpa\\tempgrp\\tia\\ListAIReplace.lst","r") ;
    if(File2)
	{	
		fscanf(File2, "%s" , text);
		if (feof(File2))
		{
		}
		else
		{
			while(!feof(File2))
			{
				if (text !="\n")
				m_replace.AddString(text);
				fscanf(File2, "%s" , text);
			}
		}

		fclose(File2);
	}
	long first , last;
	CString cs_actual;
	cs_actual = m_EditCtrl->m_fn_csGetWordIn(m_EditCtrl->m_fn_lGetCurrentCharIndex(),first , last);
	m_find.InsertString(-1 , cs_actual);
	m_find.SelectString(-1,cs_actual);

	return TRUE;   
}

BOOL CPA_EdIR_DiagReplace::DestroyWindow() 
{
	FILE *File1;
	FILE *File2;
	char text[512];
	int i;
	m_EditCtrl->m_lFirstSelectIndex=0;
	m_EditCtrl->m_lLastSelectIndex=0;

    if(File1=fopen("\\cpa\\tempgrp\\tia\\ListAIFind.lst","w"))
	{
		for (i=0;i<m_find.GetCount();i++)
		{
			m_find.GetLBText(i , text);
			fprintf(File1, "%s\n" , text); 
		}
		fclose(File1); 	
	}
    if(File2=fopen("\\cpa\\tempgrp\\tia\\ListAIReplace.lst","w"))
	{
		for (i=0;i<m_replace.GetCount();i++)
		{
			m_replace.GetLBText(i , text);
			fprintf(File2, "%s\n" , text); 
		}
		fclose(File2);
	}

		m_EditCtrl->RestoreCol();
	return CDialog::DestroyWindow();
}

BOOL CPA_EdIR_DiagReplace::fn_ExistFind(CString search)
{
	UpdateData(TRUE);
	int i;
	char text[300];

	for (i=0;i<m_find.GetCount();i++)
		{
			m_find.GetLBText(i , text);
		if(text == search )
			return TRUE;
		}

	return FALSE;
}

BOOL CPA_EdIR_DiagReplace::fn_ExistReplace(CString search)
{
	UpdateData(TRUE);
	int i;
	char text[30];
	for (i=0;i<m_replace.GetCount();i++)
		{
			m_replace.GetLBText(i , text);
		if(text == search )
			return TRUE;
		}

	return FALSE;
}

void CPA_EdIR_DiagReplace::OnCancel() 
{
		m_EditCtrl->RestoreCol();
	
	CDialog::OnCancel();
}

