/*=========================================================================
*
* GraphInter.cpp	- main implementation file for the graphic interface
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "stdafx.h"

#include "GraphInter.h"

#include "print.h"

#include "DlgMain.h"
#include "IniData.h"
#include "Help.h"


//--- Global statics --------------------------------------------------------

// The one and only CGraphInterApp object
CGraphInterApp theApp;

// CGraphInterApp initialization

CRichEditCtrl*	p_oRichEditRes;
CHARFORMAT*		p_stCharFormatRes;
CRichEditCtrl*	p_oRichEditCom;
CHARFORMAT*		p_stCharFormatCom;
CWinThread*		p_ThMain;


//--------------------------------------------------------------------


/****************************************************************************
*		Description:	message map
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BEGIN_MESSAGE_MAP(CGraphInterApp, CWinApp)
	//{{AFX_MSG_MAP(CGraphInterApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/****************************************************************************
*		Description:	constructor
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
CGraphInterApp::CGraphInterApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


/****************************************************************************
*		Description:	init the application instance
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL CGraphInterApp::InitInstance()
{
	char szHelpFile[256];
	
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// compute the name of the context help file
	GetCurrentDirectory(256, szHelpFile);
	strcat(szHelpFile, "\\");
	strcat(szHelpFile, C_szCxtHelpFile);
	// update the current context help file
	fn_vChangeContextHelpFile(szHelpFile);


	// create the dialog for the application
	CDlgMain dlg;
	
	m_pMainWnd = &dlg;
	p_oRichEditCom = &dlg.m_oRichEditCom;
	p_stCharFormatCom = &dlg.m_stCharFormatCom;
	p_oRichEditRes = &dlg.m_oRichEditRes;
	p_stCharFormatRes = &dlg.m_stCharFormatRes;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


/****************************************************************************
*		Description:	update the name of the context help file
*
*		Parameters:		_szString	: string to display
*						_Line		: type of the line (normal, warning, error)
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CGraphInterApp::fn_vChangeContextHelpFile (char *szNewName)
{
	free((void*)m_pszHelpFilePath);
	m_pszHelpFilePath=_tcsdup(_T(szNewName));
}

/****************************************************************************
*		Description:	display a string in the result window
*
*		Parameters:		_szString	: string to display
*						_Line		: type of the line (normal, warning, error)
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_vAfxOutputStringRes( char* _szString, char _Line)
{
	fn_ComRes_vWriteLine( _szString, _Line ,p_oRichEditRes, p_stCharFormatRes);
}

/****************************************************************************
*		Description:	display a string in the command window
*
*		Parameters:		_szString	: string to display
*						_Line		: type of the line (normal, warning, error)
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_vAfxOutputStringCom( char* _szString, char _Line)
{
	char szSingleLine[256];
	char *p_szNextCom, *p_szCurrentCom;
	char *p_szNextLine, *p_szCurrentLine;
	BOOL bTitle, bLastLine, bLastCom = FALSE;
	int	 iLineIndex = 0, iCommandIndex = 0;

	p_szCurrentCom = _szString;

	while (p_szCurrentCom)
	{
		// update pointers
		p_szNextCom = strstr(p_szCurrentCom+2, "##");
		p_szCurrentLine = p_szCurrentCom+2;
		// update flags
		bTitle = TRUE;
		bLastCom = (p_szNextCom == NULL);
		bLastLine = FALSE;

		while (p_szCurrentLine)
		{
			// update command index
			g_iCommandIndex[iLineIndex] = iCommandIndex;

			p_szNextLine = strchr(p_szCurrentLine+1, '-');
			
			if (!p_szNextLine)
			{
				bLastLine = TRUE;
				if (!p_szNextCom)
					p_szNextLine = p_szCurrentLine + strlen(p_szCurrentLine);
				else
					p_szNextLine = p_szNextCom;
			}
			
			else if (!bLastCom && (p_szNextLine > p_szNextCom))
			{
				bLastLine = TRUE;
				p_szNextLine = p_szNextCom;
			}
			
			int nLenght = p_szNextLine - p_szCurrentLine;

			memset(szSingleLine, 0, 256);

			strcpy(szSingleLine, (bTitle ? " " : "          "));
			strncat(szSingleLine, p_szCurrentLine, nLenght);
			if (szSingleLine[strlen(szSingleLine) - 1] != '\n')
				strcat(szSingleLine+nLenght, "\n");

			if (bTitle)
				fn_ComRes_vWriteLine( szSingleLine, C_ComRes_cTitleLine ,p_oRichEditCom, p_stCharFormatCom);
			else
				fn_ComRes_vWriteLine( szSingleLine, C_ComRes_cNormalLine ,p_oRichEditCom, p_stCharFormatCom);

			p_szCurrentLine = bLastLine ? NULL : p_szNextLine;
			iLineIndex++;
			
			bTitle = FALSE;
		}
		g_iCommandIndex[iLineIndex] = iCommandIndex;
		p_szCurrentCom = p_szNextCom;
		iCommandIndex++;
	}


//	fn_ComRes_vWriteLine( _szString, _Line ,p_oRichEditCom, p_stCharFormatCom);
}


