// EdIAView.cpp : implementation of the CEdIAView class
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRRVw.hpp"

#include "EdIRBeha.hpp"
#include "EdIRBeEn.hpp"
#include "EdIRIRD.hpp"
#include "EdIRBVw.hpp"
#include "EdIRFrm.hpp"
#include "EdIRRes.hm"

#include "ai_intf.hpp"

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

//Stefan Dumitrean 22-06-98 ( choose font )
#define C_FONT_SETTINGS_FILE_NAME "\\cpa\\exe\\main\\edt_data\\fontsize.dat"
//End Stefan Dumitrean 22-06-98 ( choose font )

/////////////////////////////////////////////////////////////////////////////
// class CPA_EdIR_View
/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CPA_EdIR_View, CFormView)

BEGIN_MESSAGE_MAP(CPA_EdIR_View, CFormView)
	//{{AFX_MSG_MAP(CPA_EdIR_View)
	ON_WM_HELPINFO()
	ON_WM_PAINT()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
	ON_WM_NCHITTEST()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_View construction/destruction

/**********************************************************************************/
CPA_EdIR_View::CPA_EdIR_View()
	: CFormView(CPA_EdIR_View::IDD)
{
	//{{AFX_DATA_INIT(CPA_EdIR_View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	m_bIsReadOnly=FALSE;
	m_pclBreakPointList=NULL;
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_pclDebugInfoList = NULL;
	//END ROMTEAM Cristi Petrescu 98-05-
//Stefan Dumitrean 8-06-98 (new scroll )
	m_p_clEditControl = NULL;
//End Stefan Dumitrean 8-06-98

}

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

/**********************************************************************************/
void CPA_EdIR_View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPA_EdIR_View)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/**********************************************************************************/
BOOL CPA_EdIR_View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

/**********************************************************************************/
BOOL CPA_EdIR_View::Create(LPCTSTR lpszClassName,LPCTSTR lpszWindowName,DWORD dwStyle,const RECT &rect,CWnd *pParentWnd,UINT nID,CCreateContext *pContext)
{
	BOOL bRes=CFormView::Create(lpszClassName,lpszWindowName,dwStyle,rect,pParentWnd,nID,pContext);
//Stefan Dumitrean 19-06-98 ( choose font )
	if( bRes && !m_fn_bLoadFontSettings( C_FONT_SETTINGS_FILE_NAME ) ) 
	{
		GetFont()->GetLogFont( &m_stCurrentFont );
		m_nFontSize = 100; //don't know how to get the size from the LOGFONT structure
	}
//End Stefan Dumitrean 19-06-98 ( choose font )
	return bRes;
}

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_View diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CPA_EdIR_View message handlers

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

	CFormView::OnInitialUpdate();

	AfxSetResourceHandle(hOldInstance);
}

/**********************************************************************************/
UINT CPA_EdIR_View::OnNcHitTest(CPoint point)
{
	UINT	uiHitTest=CWnd::OnNcHitTest(point);

	if(uiHitTest!=HTBOTTOM &&
	   uiHitTest!=HTBOTTOMLEFT &&
	   uiHitTest!=HTBOTTOMRIGHT &&
	   uiHitTest!=HTCAPTION &&
	   uiHitTest!=HTGROWBOX &&
	   uiHitTest!=HTLEFT &&
	   uiHitTest!=HTMENU &&
	   uiHitTest!=HTRIGHT &&
	   uiHitTest!=HTSIZE &&
	   uiHitTest!=HTTOPLEFT &&
	   uiHitTest!=HTTOPRIGHT)
	   return uiHitTest;
	else
		return 0;
}

/**********************************************************************************/
void CPA_EdIR_View::OnPaint()
{
	CPA_EdIR_Behaviour	*pclActiveBehaviour=((CPA_EdIR_Frame *)GetParentFrame())->m_fn_pclGetDiagrammeView()->m_fn_pclGetActiveBehaviour();
//Stefan Dumitrean 9-06-98 (new scroll)
//	CPoint	cpOffset=GetScrollPosition();
	CPoint	cpOffset=0;
//End Stefan Dumitrean 9-06-98
	CClientDC	cDc(this);

	CFormView::OnPaint();

	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	//Mark executed lines
	CPA_EdIR_Frame	*pclIRMainFrame=g_pclAIInterface->m_pclIRMainFrame;
	tdeEditState tdeState=pclIRMainFrame->m_fn_pclGetButtonView()->m_fn_tdeGetEditState();
	if (tdeState == EditIntell || tdeState == EditReflex)
	{
		CPA_DLLBase	*pclDebugDLL = (CPA_DLLBase*) (g_pclAIInterface -> GetMainWorld () -> GetToolDLLWithName ("IA_Debugger"));
		if (pclDebugDLL -> fn_bIsCurrentEditor () &&
			g_pclAIInterface -> m_pclIRMainFrame -> m_bMarkExecutedLines)
		{
			if(m_pclDebugInfoList!=NULL)
			{
				HICON hDebugInfoIcon=AfxGetApp()->LoadIcon(IDI_ICON_IR_MARKEXECUTED);

				//Draws mark
				if(pclActiveBehaviour!=NULL)
				{
					POSITION	pos=m_pclDebugInfoList->GetHeadPosition();
					while(pos!=NULL)
					{
						CPA_EdIR_DebugInfo	*pclDebugInfo=m_pclDebugInfoList->GetNext(pos);

						if(pclDebugInfo -> m_fn_bGetExecuted ())
						{
							long	lLineIndex=pclActiveBehaviour->m_fn_lGetLineNumberInPixel(pclDebugInfo->m_fn_lGetIndex());
						
							CPoint	p(0,lLineIndex);
							p-=cpOffset;
							cDc.DrawIcon(p,hDebugInfoIcon);
						}
					}
				}
			}
		}
		//END ROMTEAM Cristi Petrescu 98-05-

		// BEGIN ROMTEAM Cristi Petrescu 98-08-
		// Draws Executed line
		if (pclDebugDLL -> fn_bIsCurrentEditor ())
		{
			HICON hDebugInfoIcon=AfxGetApp()->LoadIcon(IDI_ICON_EXECUTED_LINE);

			if (m_lExecutedLine >=0)
			{
				long	lLineIndex=pclActiveBehaviour->m_fn_lGetLineNumberInPixel(m_lExecutedLine);
				CPoint	p(0,lLineIndex);
				p-=cpOffset;
				cDc.DrawIcon(p,hDebugInfoIcon);
			}
		}
		// END ROMTEAM Cristi Petrescu 98-08-
	}

	//Draws BreakPointList
	if(m_pclBreakPointList!=NULL)
	{
		HICON			hBreakPointIcon=AfxGetApp()->LoadIcon(IDI_ICON_IR_BREAKPOINT);

		//Draws BreakPoint
		if(pclActiveBehaviour!=NULL)
		{
			CString		csActiveBehaviourName=pclActiveBehaviour->m_fn_pclGetMainBehaviour()->GetName();
			POSITION	pos=m_pclBreakPointList->GetHeadPosition();
			while(pos!=NULL)
			{
				CPA_EdIR_BreakPoint	*pclBreakPoint=m_pclBreakPointList->GetNext(pos);

				if(pclBreakPoint->m_fn_csGetBehaviourName()==csActiveBehaviourName)
				{
					long	lLineIndex=pclActiveBehaviour->m_fn_lGetLineNumberInPixel(pclBreakPoint->m_fn_lGetIndex());
				
					CPoint	p(0,lLineIndex);
					p-=cpOffset;
					cDc.DrawIcon(p,hBreakPointIcon);
				}
			}
		}
	}

	//ANNECY CB
	if(pclActiveBehaviour && pclActiveBehaviour->m_lErrorIndex != -1)
	{
		HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON_IR_ERROR);
		long	lLineIndex=pclActiveBehaviour->m_fn_lGetLineNumberInPixel(pclActiveBehaviour->m_lErrorIndex);

		CPoint	p(0,lLineIndex);
		p-=cpOffset;
		cDc.DrawIcon(p,hIcon);
	}
	//END ANNECY
}

/**********************************************************************************/
BOOL CPA_EdIR_View::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	::WinHelp(m_hWnd,g_pclAIInterface->m_csBrainHelpFileNameAndPath,HELP_CONTEXT,HID_RULES_ZONE);
	
	return CFormView::OnHelpInfo(pHelpInfo);
}

/**********************************************************************************/
void CPA_EdIR_View::m_fn_vSetReadOnly(BOOL bReadOnly)
{
	m_bIsReadOnly=bReadOnly;
}

/**********************************************************************************/
void CPA_EdIR_View::m_fn_vSetBreakPointList(CPA_EdIR_BreakPointList *pclBreakPointList)
{
	m_pclBreakPointList=pclBreakPointList;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
/**********************************************************************************/
void CPA_EdIR_View::m_fn_vSetDebugInfoList(CPA_EdIR_DebugInfoList *pclDebugInfoList)
{
	m_pclDebugInfoList=pclDebugInfoList;
}
//END ROMTEAM Cristi Petrescu 98-05-

//Stefan Dumitrean 8-06-98 ( new scroll )
void CPA_EdIR_View::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	if( m_p_clEditControl )
		m_p_clEditControl->m_fn_vResizeToFitParent();
}


//End Stefan Dumitrean 8-06-98

//Stefan Dumitrean 19-06-98 ( choose font )
void CPA_EdIR_View::m_fn_vChooseFont()
{
	CFontDialog clFDlg( &m_stCurrentFont, CF_NOSTYLESEL | CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_FIXEDPITCHONLY, NULL, this );
	if( clFDlg.DoModal() == IDOK )
	{
		
		clFDlg.GetCurrentFont( &m_stCurrentFont );
		m_stCurrentFont.lfWeight = 0;
		m_stCurrentFont.lfItalic = 0;
		m_stCurrentFont.lfUnderline = 0;

		m_nFontSize = clFDlg.GetSize();

		m_fn_vSaveFontSettings( C_FONT_SETTINGS_FILE_NAME );

		if( m_p_clEditControl )
			m_p_clEditControl->m_fn_vSetFontTypeAndSize( );
	}
}

CString CPA_EdIR_View::m_fn_csGetFontType()
{ 
	CString csRet = m_stCurrentFont.lfFaceName;
	return csRet;
}

int	CPA_EdIR_View::m_fn_nGetFontSize()
{
	return m_nFontSize;
}

CSize CPA_EdIR_View::m_fn_clGetFontExtent()
{
	CFont clFont;
	clFont.CreateFontIndirect( &m_stCurrentFont );
	CClientDC	cdc(this);
	CFont		*p_clOldFont=cdc.SelectObject(&clFont);
	CSize clFontSize = cdc.GetTextExtent("A");
	cdc.SelectObject(p_clOldFont);
	return clFontSize;
}


void CPA_EdIR_View::m_fn_vSaveFontSettings( CString csString )
{
	FILE * stFile = fopen( LPCSTR( csString ), "wt" );
	if( !stFile )
		return;
	fprintf( stFile, "[LOGFONT structure]\n\n" );
	fprintf( stFile, "lfHeight         = %ld\n", m_stCurrentFont.lfHeight );
	fprintf( stFile, "lfWidth          = %ld\n", m_stCurrentFont.lfWidth );
	fprintf( stFile, "lfEscapement     = %ld\n", m_stCurrentFont.lfEscapement );
	fprintf( stFile, "lfOrientation    = %ld\n", m_stCurrentFont.lfOrientation );
	fprintf( stFile, "lfWeight         = %ld\n", m_stCurrentFont.lfWeight );
	fprintf( stFile, "lfItalic         = %d\n", m_stCurrentFont.lfItalic );
	fprintf( stFile, "lfUnderline      = %d\n", m_stCurrentFont.lfUnderline );
	fprintf( stFile, "lfStrikeOut      = %d\n", m_stCurrentFont.lfStrikeOut );
	fprintf( stFile, "lfCharSet        = %d\n", m_stCurrentFont.lfCharSet );
	fprintf( stFile, "lfOutPrecision   = %d\n", m_stCurrentFont.lfOutPrecision );
	fprintf( stFile, "lfClipPrecision  = %d\n", m_stCurrentFont.lfClipPrecision );
	fprintf( stFile, "lfQuality        = %d\n", m_stCurrentFont.lfQuality );
	fprintf( stFile, "lfPitchAndFamily = %d\n", m_stCurrentFont.lfPitchAndFamily );
	fprintf( stFile, "lfFaceName       = %s\n\n", m_stCurrentFont.lfFaceName );

	fprintf( stFile, "[Font Size]\n\n" );
	fprintf( stFile, "Size = %d\n", m_nFontSize );
	fclose( stFile );
}


BOOL CPA_EdIR_View::m_fn_bLoadFontSettings( CString csString )
{
	FILE * stFile = fopen( LPCSTR( csString ), "rt" );
	if( !stFile )
		return FALSE;
	fscanf( stFile, "[LOGFONT structure]\n\n" );
	fscanf( stFile, "lfHeight         = %ld\n", &m_stCurrentFont.lfHeight );
	fscanf( stFile, "lfWidth          = %ld\n", &m_stCurrentFont.lfWidth );
	fscanf( stFile, "lfEscapement     = %ld\n", &m_stCurrentFont.lfEscapement );
	fscanf( stFile, "lfOrientation    = %ld\n", &m_stCurrentFont.lfOrientation );
	fscanf( stFile, "lfWeight         = %ld\n", &m_stCurrentFont.lfWeight );
	fscanf( stFile, "lfItalic         = %d\n", &m_stCurrentFont.lfItalic );
	fscanf( stFile, "lfUnderline      = %d\n", &m_stCurrentFont.lfUnderline );
	fscanf( stFile, "lfStrikeOut      = %d\n", &m_stCurrentFont.lfStrikeOut );
	fscanf( stFile, "lfCharSet        = %d\n", &m_stCurrentFont.lfCharSet );
	fscanf( stFile, "lfOutPrecision   = %d\n", &m_stCurrentFont.lfOutPrecision );
	fscanf( stFile, "lfClipPrecision  = %d\n", &m_stCurrentFont.lfClipPrecision );
	fscanf( stFile, "lfQuality        = %d\n", &m_stCurrentFont.lfQuality );
	fscanf( stFile, "lfPitchAndFamily = %d\n", &m_stCurrentFont.lfPitchAndFamily );
	fscanf( stFile, "lfFaceName       = %s\n\n", m_stCurrentFont.lfFaceName );

	fscanf( stFile, "[Font Size]\n\n" );
	fscanf( stFile, "Size = %d\n", &m_nFontSize );

	fclose( stFile );
	return TRUE;
}

//End Stefan Dumitrean 19-06-98 ( choose font )

#endif //D_ED_IR_ACTIVE

