/*
 =======================================================================================
  Name        : ChkData.cpp
 
  Author      : vincent lhullier            Date :08/09/97
 
  Description : main Application
 =======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"

#include "SCR.h"

#include "ChkData.h"
#include "ChkDataDlg.h"
#include "IniData.h"
#include "Tree_fct.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


/*
 =======================================================================================
	The one and only CChkDataApp object
 =======================================================================================
 */
CChkDataApp theApp;

/*
 =======================================================================================
	CChkDataApp
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : message map
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CChkDataApp, CWinApp)
	//{{AFX_MSG_MAP(CChkDataApp)
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()


/*
 ----------------------------------------------------------------------------------------
  Description   : constructor
 ----------------------------------------------------------------------------------------
 */
CChkDataApp::CChkDataApp()
{
}

/*
 ---------------------------------------------------------------------------------------
  call back for wait dialog
 ---------------------------------------------------------------------------------------
 */
BOOL CALLBACK fn_bWaitDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	static UINT uiTimer;
	static char cInit;
	BOOL		bResult;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	switch (uMsg)
	{
		case WM_INITDIALOG:
			SetWindowText(hwndDlg, "Load files infos" );
			cInit = 1;
			return TRUE;

		case WM_SHOWWINDOW:
			if (cInit)
			{ 
				cInit = 0;
				uiTimer = SetTimer( hwndDlg, 1, 100, NULL );
			}
			break;

		case WM_TIMER:
			KillTimer( hwndDlg, uiTimer );
			bResult = fn_Tree_bBuildFromDisk( &g_p_stRootProject, g_stIniData.szLocalPath, GetDlgItem( hwndDlg, IDC_STATIC_PROJECT ) );
			EndDialog( hwndDlg, bResult?1:0 );
			break;
	}

	return FALSE;
}


/*
 ----------------------------------------------------------------------------------------
  Description   : CChkDataApp initialization
 ----------------------------------------------------------------------------------------
 */
BOOL CChkDataApp::InitInstance()
{
	// Standard initialization

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

	/*
	 * Init script and memory module
	 */
	Mmg_fn_vFirstInitMmgModule(1);
	SCR_fn_v_RdL0_Init();

	SCR_fn_v_Link_InitTable( &g_stSectionTypeLinkTable );
	SCR_fn_v_Link_InitTable( &g_stSectionLinkTable );
	SCR_fn_v_Link_InitTable( &g_stRefLinkTable );
	SCR_fn_v_Link_InitTable( &g_stFileLinkTable );

	fn_IniD_vGetAllFromIniFile( TRUE );
	fn_IniD_vGetWindowPreference();

	int iResult = DialogBox( AfxGetInstanceHandle(), MAKEINTRESOURCE( IDD_DIALOG_WAIT ), ::GetDesktopWindow(), fn_bWaitDialogProc );
	if ( iResult != 1)
	{
		AfxMessageBox("Can't read file and disk information", MB_OK, MB_ICONSTOP );
		return FALSE;
	}
	strcpy( g_p_stRootProject->szName, g_stIniData.szLocalPath + g_stIniData.lRootForSectionLength );

	if (_access( g_stIniData.szSectionFile, 0) == 0)
		fn_Scr_vLoadSectionTypes();

	CChkDataDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
	}
	else if (nResponse == IDCANCEL)
	{
	}

	fn_Tree_vFreeFileTypeList();
	fn_Tree_vFreeProjectTree( g_p_stRootProject, TRUE );
	fn_IniD_vFreeFileList( &g_stIniData.stCheckConfig );
	fn_Scr_vFreeSectionTypeList();
	SCR_fn_v_Link_CloseTable( &g_stSectionTypeLinkTable );
	fn_Scr_vFreeSectionList();
	SCR_fn_v_Link_CloseTable( &g_stSectionLinkTable );
	SCR_fn_v_Link_CloseTable( &g_stRefLinkTable );
	fn_Scr_vFreeLTFile();
	SCR_fn_v_Link_CloseTable( &g_stFileLinkTable );

	/*
	 * close memory and script module
	 */
	SCR_fn_v_RdL0_Close();
	Mmg_fn_vStopMmgModule();

	return FALSE;
}
