/*
 =======================================================================================
  Name        : ChkDataDlg.cpp
 
  Author      : vincent lhullier            Date :08/09/97
 
  Description : implementation of main dialog of ChkData application
 =======================================================================================
 */

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

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

/*
 =======================================================================================
 =======================================================================================
	CAboutDlg
 =======================================================================================
 =======================================================================================
 */
class CAboutDlg : public CDialog
{
  public:
	CAboutDlg();

  // Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	//}}AFX_VIRTUAL

  // Implementation
  protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*
 =======================================================================================
 =======================================================================================
	CChkDataDlg dialog
 =======================================================================================
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : constructor
 ----------------------------------------------------------------------------------------
 */
CChkDataDlg::CChkDataDlg(CWnd* pParent /*=NULL*/) : CDialog(CChkDataDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChkDataDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_hBtRougeBmp.LoadBitmap( IDB_BITMAP_BTROUGE );
	m_hBtVertBmp.LoadBitmap( IDB_BITMAP_BTVERT );

	m_lNumberOfFiles = 0;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : destructor
 ----------------------------------------------------------------------------------------
 */
CChkDataDlg::~CChkDataDlg()
{
	m_hBtRougeBmp.DeleteObject();
	m_hBtVertBmp.DeleteObject();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Message map
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CChkDataDlg, CDialog)
	//{{AFX_MSG_MAP(CChkDataDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DRAWITEM()
	ON_WM_MEASUREITEM()
	ON_LBN_DBLCLK(IDC_LIST_FILETYPE, OnDblclkListFiletype)
	ON_BN_CLICKED(IDC_BUTTON_PROCESS, OnButtonProcess)
	ON_BN_CLICKED(IDC_BUTTON_BUILDFILELIST, OnButtonBuildfilelist)
	ON_LBN_SELCHANGE(IDC_LIST_SECTIONS, OnSelchangeListSections)
	ON_LBN_SELCHANGE(IDC_LIST_ENTRIES, OnSelchangeListEntries)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_SECTIONANDREFS, OnButtonSectionandrefs)
	ON_BN_CLICKED(IDC_BUTTON_STAT, OnButtonStat)
	ON_LBN_SELCHANGE(IDC_LIST_BADREF, OnSelchangeListBadref)
	ON_LBN_DBLCLK(IDC_LIST_SECTIONWITHBADREF, OnDblclkListSectionwithbadref)
	ON_BN_CLICKED(IDC_BUTTON_MODSTAT, OnButtonModstat)
	//}}AFX_MSG_MAP
	ON_CONTROL_RANGE( BN_CLICKED, IDC_CHECK_PARAM0, IDC_CHECK_PARAM19, OnButtonParam )
END_MESSAGE_MAP()

/*
 =======================================================================================
	specific functions
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : init progress bar
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vInitProgessBar( int _iStart, int _iEnd )
{
	m_oProgress.m_fn_vSetRange( _iStart, _iEnd );
	m_oProgress.m_fn_vReinit();
	m_oProgress.m_fn_vInitDrawing();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : fill section type list box
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vFillSectionTypeList( void )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	SCR_tdst_DyAr_Description	*p_stDyAr;
	SCR_tdst_Link_Value			*p_stValue;
	unsigned int				uiSection;
	int							iIndex;
	char						szText[512];
	long						lNumberOfSections = 0;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	

	m_p_oLBSectionType->ResetContent();

	p_stDyAr = &g_stSectionTypeLinkTable.stLinkArray;
	for (uiSection = 0; uiSection < p_stDyAr->uiNumValues; uiSection ++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiSection, p_stValue, g_stSectionTypeLinkTable.stLinkArray );

		sprintf( szText, "%s", p_stValue->p_szKey );
		if (p_stValue->stInfos.ulLong1)
			sprintf( szText + strlen( szText ), " (%d)", p_stValue->stInfos.ulLong1 );

		iIndex = m_p_oLBSectionType->AddString( szText );
		//m_p_oLBSectionType->SetItemData( iIndex, p_stValue->ulValue );
		m_p_oLBSectionType->SetItemData( iIndex, (DWORD) p_stValue );
		lNumberOfSections += p_stValue->stInfos.ulLong1;
	}

	sprintf( szText, " Section types : %d", p_stDyAr->uiNumValues );
	if (lNumberOfSections)
		sprintf(szText + strlen( szText ), " (%d)", lNumberOfSections );

	GetDlgItem( IDC_STATIC_NUMBEROFSECTIONTYPES )->SetWindowText( szText );

	if (p_stDyAr->uiNumValues)
	{
		m_p_oLBSectionType->SetCurSel( 0 );
		OnSelchangeListSections();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : fill section type list box
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vFillListEntry( void )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	SCR_tdst_DyAr_Description	*p_stDyAr;
	tdstEntry					*p_stEntry;
	unsigned int				uiEntry;
	int							iIndex;
	char						szText[20];
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	
	m_p_oLBEntry->ResetContent();

	if (m_p_stCurrentSectionType == NULL)
		return;

	p_stDyAr = (SCR_tdst_DyAr_Description *) m_p_stCurrentSectionType->ulValue;
	for (uiEntry = 0; uiEntry < p_stDyAr->uiNumValues; uiEntry ++)
	{
		SCR_M_DyAr_GetElement( tdstEntry, uiEntry, p_stEntry, (*p_stDyAr) );

		iIndex = m_p_oLBEntry->AddString( p_stEntry->szName );
		m_p_oLBEntry->SetItemData( iIndex, (DWORD) p_stEntry );
	}

	sprintf( szText, " Entries : %d", p_stDyAr->uiNumValues );
	GetDlgItem( IDC_STATIC_NUMBEROFENTRIES )->SetWindowText( szText );

	if (p_stDyAr->uiNumValues)
	{
		m_p_oLBEntry->SetCurSel( 0 );
	}
	OnSelchangeListEntries();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : fill section type list box
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vShowCurrentEntry( void )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	char	szSample[1] = { 0 };
	char	*p_szSample;
	char	cParam, cNumberOfParams;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	if (m_p_stCurrentEntry == NULL)
	{
		p_szSample = szSample;
		cNumberOfParams = 0;
	}
	else
	{
		p_szSample = m_p_stCurrentEntry->szSample;
		cNumberOfParams = m_p_stCurrentEntry->cNumberOfParams;
	}

	GetDlgItem( IDC_STATIC_ENTRYSAMPLE )->SetWindowText( p_szSample );

	for (cParam = 0; cParam < cNumberOfParams; cParam++)
	{
		m_a_poBtParam[ cParam ]->ShowWindow( TRUE );
		m_a_poBtParam[ cParam ]->SetCheck( m_p_stCurrentEntry->a_cIsRef[ cParam ] );
	}

	for (; cParam < 20; cParam++)
		m_a_poBtParam[ cParam ]->ShowWindow( FALSE );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : fill section type list box
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vDumpSectionAndRefsListInFile( char *_szFileName )
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	FILE						*hpFile;
	SCR_tdst_DyAr_Description	*p_stDyAr;
	SCR_tdst_DyAr_Description   *p_stRefDyAr;
	SCR_tdst_Link_Value			*p_stValue;
	unsigned int				uiSection;
	tdstReference				*p_stRef;
	unsigned int				uiRef;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	if ((hpFile = fopen( _szFileName, "wt") ) == NULL)
		return;

	fprintf( hpFile, "\n********************\n* Sections & Refs   *\n********************\n" );

	p_stDyAr = &g_stSectionLinkTable.stLinkArray;
	for (uiSection = 0; uiSection < p_stDyAr->uiNumValues; uiSection ++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiSection, p_stValue, (*p_stDyAr) );
		fprintf( hpFile, "(%4d) %s\n", p_stValue->stInfos.ulLong1, p_stValue->p_szKey );

		/*
		 * dump references
		 */
		p_stRefDyAr = (SCR_tdst_DyAr_Description *) p_stValue->ulValue;
		if (p_stRefDyAr)
		{
			for (uiRef = 0; uiRef < p_stRefDyAr->uiNumValues; uiRef ++)
			{
				SCR_M_DyAr_GetElement( tdstReference, uiRef, p_stRef, (*p_stRefDyAr) );
				fprintf( hpFile, "\t(%04d) -> %s\n", p_stRef->uiLine, p_stRef->p_stRef->p_szKey );
			}
		}
	}

	fprintf( hpFile, "\n********************\n* References        *\n********************\n" );
	p_stDyAr = &g_stRefLinkTable.stLinkArray;
	for (uiRef = 0; uiRef < p_stDyAr->uiNumValues; uiRef++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiRef, p_stValue, (*p_stDyAr) );
		fprintf( hpFile, "%c [%6d] %s\n", p_stValue->stInfos.ulLong2 ? ' ' : 'X', p_stValue->stInfos.ulLong1, p_stValue->p_szKey );
	}

	fclose( hpFile );
}


/*
 ----------------------------------------------------------------------------------------
  Description   : fill list with bad references
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::fn_vFillBadReferenceList()
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	unsigned int			uiRef;
	SCR_tdst_Link_Value		*p_stValue;
	int						iIndex;
	char					szText[ SCR_CV_ui_Cfg_MaxLenName ];
	long					lBadRef = 0;

	/*
	unsigned long			ulMaxRefBy = 0;
	unsigned long			ulNbRefs;
	unsigned long			ulMaxRef = 0;
	*/
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


	m_p_oLBBadRef->ResetContent();

	for (uiRef = 0; uiRef < g_stRefLinkTable.stLinkArray.uiNumValues; uiRef++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiRef, p_stValue, g_stRefLinkTable.stLinkArray );
		if (p_stValue->stInfos.ulLong2 == 0)
		{
			sprintf( szText, "[%4d] %s", p_stValue->stInfos.ulLong1, p_stValue->p_szKey );
			iIndex = m_p_oLBBadRef->AddString( szText );
			m_p_oLBBadRef->SetItemData( iIndex, (DWORD) p_stValue );
			lBadRef += p_stValue->stInfos.ulLong1;
		}
		/*
		if (p_stValue->stInfos.ulLong1 > ulMaxRefBy)
			ulMaxRefBy = p_stValue->stInfos.ulLong1;
		*/
	}

	// test
	/*
	for (uiRef = 0; uiRef < g_stSectionLinkTable.stLinkArray.uiNumValues; uiRef++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiRef, p_stValue, g_stSectionLinkTable.stLinkArray );
		ulNbRefs = ((SCR_tdst_DyAr_Description *) p_stValue->ulValue == NULL) ? 0 : ((SCR_tdst_DyAr_Description *) p_stValue->ulValue)->uiNumValues;
		if (ulNbRefs > ulMaxRef)
			ulMaxRef = ulNbRefs;
	}
	*/


	sprintf( szText, " Bad references : %d [%d]", m_p_oLBBadRef->GetCount(), lBadRef );
	GetDlgItem( IDC_STATIC_BADREF )->SetWindowText( szText );

	m_p_stCurrentBadRef = NULL;
	fn_vFillSectionWithBadReferenceList();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : fill list of section that own current selected references
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::fn_vFillSectionWithBadReferenceList()
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	SCR_tdst_DyAr_Description	*p_stDyAr;
	SCR_tdst_Link_Value			*p_stValue;
	tdstReference				*p_stRef;
	int							iIndex;
	unsigned int				uiSection, uiRef;
	char						szText[ SCR_CV_ui_Cfg_MaxLenName ];
	unsigned int				uiNbRef = 0;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	

	m_p_oLBSectionWithBadRef->ResetContent();

	if (m_p_stCurrentBadRef == NULL)
		return;

	for (uiSection = 0; uiSection < g_stSectionLinkTable.stLinkArray.uiNumValues; uiSection++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiSection, p_stValue, g_stSectionLinkTable.stLinkArray );
		p_stDyAr = (SCR_tdst_DyAr_Description *) p_stValue->ulValue;
		if (p_stDyAr)
		{
			for (uiRef = 0; uiRef < p_stDyAr->uiNumValues; uiRef++)
			{
				SCR_M_DyAr_GetElement( tdstReference, uiRef, p_stRef, (*p_stDyAr) );
				if (p_stRef->p_stRef == m_p_stCurrentBadRef)
				{
					sprintf( szText, "[%04d] %s", p_stRef->uiLine, p_stValue->p_szKey );
					iIndex = m_p_oLBSectionWithBadRef->AddString( szText );
					m_p_oLBSectionWithBadRef->SetItemData( iIndex, (DWORD) p_stValue );
					if (uiNbRef++ == m_p_stCurrentBadRef->stInfos.ulLong1)
						return;
				}
			}
		}
	}

}


/*
 ----------------------------------------------------------------------------------------
  Description   : display last op time
  _lTime              -> elapse time
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::m_fn_vDisplayLastOperationTime( long _lTime )
{
	div_t stDiv;
	char szTime[50], *p_szTime;

	p_szTime = szTime + sprintf( szTime, "Last op time ");
	stDiv = div( _lTime, 3600 );
	_lTime = stDiv.rem;
	p_szTime += sprintf( p_szTime, "%02d:", stDiv.quot );
	stDiv = div( _lTime, 60 );
	sprintf( p_szTime, "%02d:%02d", stDiv.quot, stDiv.rem );
	GetDlgItem( IDC_STATIC_LASTOPTIME )->SetWindowText( szTime );
}


/*
 =======================================================================================
	Message handlers 
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_INITDIALOG
 ----------------------------------------------------------------------------------------
 */
BOOL CChkDataDlg::OnInitDialog()
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	long			lType;
	tdstFileType	*p_stFT = g_d_stFileType;
	UINT			uiIndex;
	char			szText[50];
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	CDialog::OnInitDialog();

	/*
	 * Add "About..." menu item to system menu.
	 * IDM_ABOUTBOX must be in the system command range.
	 */
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	/*
	 * init progression bar
	 */
	m_oProgress.SubclassDlgItem( IDC_PROGRESS, this );
	m_oProgress.SetStep( 1 );
	
	/*
	 * get pointer on dialog control
	 */
	static s_a_uiBtParam[20] = 
		{
			IDC_CHECK_PARAM0, IDC_CHECK_PARAM1, IDC_CHECK_PARAM2, IDC_CHECK_PARAM3, IDC_CHECK_PARAM4,
			IDC_CHECK_PARAM5, IDC_CHECK_PARAM6, IDC_CHECK_PARAM7, IDC_CHECK_PARAM8, IDC_CHECK_PARAM9,
			IDC_CHECK_PARAM10,IDC_CHECK_PARAM11,IDC_CHECK_PARAM12,IDC_CHECK_PARAM13,IDC_CHECK_PARAM14,
			IDC_CHECK_PARAM15,IDC_CHECK_PARAM16,IDC_CHECK_PARAM17,IDC_CHECK_PARAM18,IDC_CHECK_PARAM19
		};

	m_p_oLBFiles				= (CListBox *) GetDlgItem( IDC_LIST_FILE );
	m_p_oLBFileType				= (CListBox *) GetDlgItem( IDC_LIST_FILETYPE );
	m_p_oLBSectionType			= (CListBox *) GetDlgItem( IDC_LIST_SECTIONS );
	m_p_oLBEntry				= (CListBox *) GetDlgItem( IDC_LIST_ENTRIES );
	m_p_oLBBadRef				= (CListBox *) GetDlgItem( IDC_LIST_BADREF );
	m_p_oLBSectionWithBadRef	= (CListBox *) GetDlgItem( IDC_LIST_SECTIONWITHBADREF );
	
	for (uiIndex = 0; uiIndex < 20; uiIndex++ )
	{
		m_a_poBtParam[ uiIndex ] = (CButton *) GetDlgItem( s_a_uiBtParam[ uiIndex ] );
		m_a_poBtParam[ uiIndex ]->ShowWindow( FALSE );
	}

	/*
	 * fill file type list
	 */
	m_p_oLBFileType->ResetContent();

	for (lType = 0; lType < g_lNumberOfFileTypes; lType++, p_stFT++)
	{
		uiIndex = m_p_oLBFileType->AddString( p_stFT->szExt );
		m_p_oLBFileType->SetItemData( uiIndex, (DWORD) p_stFT );
	}
	sprintf( szText, "File types : %d", g_lNumberOfFileTypes );
	GetDlgItem( IDC_STATIC_FILETYPE )->SetWindowText( szText );

	/*
	 * file section type list
	 */
	m_fn_vFillSectionTypeList();


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_SYSCOMMAND
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_PAINT
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_QUERYDRAGICON
 ----------------------------------------------------------------------------------------
 */
HCURSOR CChkDataDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_DRAWITEM
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS) 
{
	// TODO: Add your message handler code here and/or call default
	if ( (lpDIS->itemID != LB_ERR) && (nIDCtl == IDC_LIST_FILETYPE) )
	{
		CDC				*p_oDC = CDC::FromHandle(lpDIS->hDC);
		CBrush			oBrush;
		COLORREF		xTextColor;
		BOOL			bSelected = (lpDIS->itemState & ODS_SELECTED );
		tdstFileType	*p_stFT;
		CPoint			oPoint;
		char			szValue[10];
      

		oBrush.CreateSolidBrush( GetSysColor( bSelected ? COLOR_HIGHLIGHT : COLOR_WINDOW) );
		xTextColor = GetSysColor( bSelected ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);

		p_oDC->FillRect( &lpDIS->rcItem, &oBrush );
        p_stFT = (tdstFileType *) m_p_oLBFileType->GetItemData( lpDIS->itemID );

		p_oDC->SetBkMode( TRANSPARENT);
		p_oDC->SetTextColor( xTextColor );

		oPoint.x = lpDIS->rcItem.left;
		oPoint.y = lpDIS->rcItem.top;
		p_oDC->DrawState( oPoint, CSize(16,16), p_stFT->bConsider ? &m_hBtVertBmp : &m_hBtRougeBmp, DST_BITMAP );
        lpDIS->rcItem.left += 20;
		lpDIS->rcItem.right -= 5;
		p_oDC->DrawText( p_stFT->szExt, &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
		p_oDC->DrawText( itoa(p_stFT->lUsed, szValue, 10) , &lpDIS->rcItem, DT_SINGLELINE | DT_RIGHT | DT_VCENTER );

		oBrush.DeleteObject();
	}
	else
		CDialog::OnDrawItem(nIDCtl, lpDIS);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_MEASUREITEM
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	//return 16;
	
	CDialog::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : ON_LBN_DBLCLK on IDC_LIST_FILETYPE list box
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnDblclkListFiletype() 
{
	UINT			nIndex;
	tdstFileType	*p_stFT;

	nIndex = m_p_oLBFileType->GetCurSel();
	if (nIndex != LB_ERR )
	{
		p_stFT = (tdstFileType *) m_p_oLBFileType->GetItemData( nIndex );
		p_stFT->bConsider = !p_stFT->bConsider;
		fn_IniD_vSetFileTypeConsiderStatus( p_stFT->szExt, p_stFT->bConsider );
		m_p_oLBFileType->Invalidate();
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_PROCESS button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonProcess()
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	long		lFile;
	char		szFileName[ MAX_PATH ];
	char		*p_szFileName;
	time_t		lStartTime, lEndTime;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	/*
	 * init progression and display for working
	 */
	m_fn_vInitProgessBar( 0, m_lNumberOfFiles );
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( FALSE );
	m_oProgress.ShowWindow(TRUE);

	/*
	 * get start time
	 */
	time( &lStartTime );

	/*
	 * reset use counters
	 */
	fn_Scr_vResetUsedCounters();

	/*
	 * parse each file
	 */
	strcpy( szFileName, g_stIniData.szRootForSection );
	p_szFileName = szFileName + g_stIniData.lRootForSectionLength;

	for (lFile = 0; lFile < m_lNumberOfFiles; lFile++) 
	{
		m_p_oLBFiles->GetText( lFile, p_szFileName );
		m_p_oLBFiles->SetCurSel( lFile );
		fn_Scr_vParseFileToBuildListOfSectionTypes( szFileName );
		m_oProgress.StepIt();
		
	}

	/*
	 * refresh list of section type
	 */
	m_fn_vFillSectionTypeList();

	/*
	 * get end time and display elapse time
	 */
	time( &lEndTime );
	m_oProgress.ShowWindow(FALSE);
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( TRUE );
	
	m_fn_vDisplayLastOperationTime( lEndTime - lStartTime );
}


/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_PROCESS button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonBuildfilelist() 
{
	char	szText[50];
	long	lFile;
	long	lNbFiles;
	char	**d_szFile;

	/* build list of file */
	fn_Tree_vRefreshWithFileList( g_p_stRootProject, &g_stIniData.stCheckConfig );
	m_lNumberOfFiles = fn_Tree_lBuildListOfFilesToGet( g_p_stRootProject, &d_szFile );

	SCR_fn_v_Link_CloseTable( &g_stFileLinkTable );
	SCR_fn_v_Link_InitTable( &g_stFileLinkTable );

	for (lNbFiles = 0, lFile = 0; lFile < m_lNumberOfFiles ; lFile++)
		if ( fn_Tree_bConsiderFile( d_szFile[ lFile ] ) )
		{
			m_p_oLBFiles->AddString( d_szFile[ lFile ] );
			lNbFiles++;
			SCR_fnp_st_Link_AddEntry( &g_stFileLinkTable, d_szFile[ lFile ], 0, SCR_ELS_Link_Initialized );
		}

	sprintf( szText, " Number of files : %d", lNbFiles );
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->SetWindowText( szText );

	fn_Tree_vFreeListOfFilesToGet( m_lNumberOfFiles, d_szFile );
	m_lNumberOfFiles = lNbFiles;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_SELCHANGE on IDC_LIST_SECTIONS
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnSelchangeListSections() 
{
	int	iIndex = m_p_oLBSectionType->GetCurSel();

	if (iIndex != LB_ERR )
		m_p_stCurrentSectionType = (SCR_tdst_Link_Value *) m_p_oLBSectionType->GetItemData( iIndex );
	else
		m_p_stCurrentSectionType = NULL;

	m_fn_vFillListEntry();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_SELCHANGE on IDC_LIST_ENTRIES
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnSelchangeListEntries() 
{
	int	iIndex = m_p_oLBEntry->GetCurSel();

	if (iIndex != LB_ERR )
		m_p_stCurrentEntry = (tdstEntry *) m_p_oLBEntry->GetItemData( iIndex );
	else
		m_p_stCurrentEntry = NULL;

	m_fn_vShowCurrentEntry();
	
}


/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_SAVE
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonSave() 
{
	fn_Scr_vSaveSectionTypes();	
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_SECTIONANDREFS button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonSectionandrefs() 
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	long						lFile;
	char						szFileName[ MAX_PATH ];
	char						*p_szFileName;
	time_t						lStartTime, lEndTime;
	SCR_tdst_Link_Value			*p_stValue;
	SCR_tdst_Link_Value			*p_stRefValue;
	unsigned int				uiRef;
	long						lNbErrors = 0;
	char						szFullRef[ SCR_CV_ui_Cfg_MaxLenName ];
	char						cPath;
	BOOL						bFile;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	/*
	 * init progression and display for working
	 */
	m_fn_vInitProgessBar( 0, m_lNumberOfFiles );
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( FALSE );
	m_oProgress.ShowWindow(TRUE);

	fn_Scr_vFreeSectionList();
	SCR_fn_v_Link_CloseTable( &g_stSectionLinkTable );
	SCR_fn_v_Link_CloseTable( &g_stRefLinkTable );
	SCR_fn_v_Link_InitTable( &g_stSectionLinkTable );
	SCR_fn_v_Link_InitTable( &g_stRefLinkTable );

	fn_Scr_vComputeSectionTypeHasRef( );

	/*
	 * get start time
	 */
	time( &lStartTime );

	/*
	 * reset use counters
	 */
	fn_Scr_vResetUsedCounters();

	/*
	 * parse each file
	 */
	strcpy( szFileName, g_stIniData.szRootForSection );
	p_szFileName = szFileName + g_stIniData.lRootForSectionLength;
	
	for (lFile = 0; lFile < m_lNumberOfFiles; lFile++) 
	{
		//m_p_oLBFiles->GetText( lFile, p_szFileName );
		p_stValue = (SCR_tdst_Link_Value *) g_stFileLinkTable.stLinkArray.d_stDynArray[ lFile ].d_vElement;
		strcpy( p_szFileName, p_stValue->p_szKey );
		m_p_oLBFiles->SetCurSel( lFile );
		fn_Scr_vParseFileToBuildListOfSectionsAndRefs( szFileName );
		//fn_Scr_vNewParseFileToBuildListOfSectionsAndRefs( szFileName, p_stValue );
		m_oProgress.StepIt();
	}

	/*
	 * dump list in a file
	 */
	//m_fn_vDumpSectionAndRefsListInFile( "SctRefs.txt" );

	m_fn_vInitProgessBar( 0, g_stRefLinkTable.stLinkArray.uiNumValues );

	/*
	for (uiRef = 0; uiRef < g_stRefLinkTable.stLinkArray.uiNumValues; uiRef++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiRef, p_stRefValue, g_stRefLinkTable.stLinkArray );

		// get referenced file
		p_szEndFileName = strchr( p_stRefValue->p_szKey, '^');
		if (p_szEndFileName != NULL)
			*p_szEndFileName = 0;

		p_stLVFile = SCR_fnp_st_Link_SearchKey( &g_stFileLinkTable, p_stRefValue->p_szKey );
		if (p_stLVFile == NULL)
		{
			for (cPath = 0; cPath < g_stIniData.cNumberOfDataPaths; cPath++)
			{
				sprintf( szFullRef, "%s\\%s", g_stIniData.a_stDataPath[cPath].szPath, p_stRefValue->p_szKey );
				p_stLVFile = SCR_fnp_st_Link_SearchKey( &g_stFileLinkTable, szFullRef );
				if (p_stLVFile != 0)
					break;
			}
		}

		if (p_stLVFile != 0)
		{
			if (p_szEndFileName == NULL)
			{
				p_stRefValue->stInfos.ulLong2 = 1;
			}
			else
			{
				*p_szEndFileName++ = '^';
				if (p_stLVFile->ulValue)
					p_stLVSection = SCR_fnp_st_Link_SearchKey( (SCR_tdst_Link_Table *) p_stLVFile->ulValue, p_szEndFileName );
				else
					p_stRefValue->stInfos.ulLong2 = 0;
				p_stRefValue->stInfos.ulLong2 = (unsigned long) p_stLVSection;
			}
		}
		else
			p_stRefValue->stInfos.ulLong2 = 0;

		m_oProgress.StepIt();
	}
	*/


	for (uiRef = 0; uiRef < g_stRefLinkTable.stLinkArray.uiNumValues; uiRef++)
	{
		SCR_M_DyAr_GetElement( SCR_tdst_Link_Value, uiRef, p_stRefValue, g_stRefLinkTable.stLinkArray );

		p_stValue = SCR_fnp_st_Link_SearchKey( &g_stSectionLinkTable, p_stRefValue->p_szKey );
		if (p_stValue == NULL)
		{
			/*
			 * test with all directory
			 */
			for (cPath = 0; cPath < g_stIniData.cNumberOfDataPaths; cPath++)
			{
				sprintf( szFullRef, "%s\\%s", g_stIniData.a_stDataPath[cPath].szPath, p_stRefValue->p_szKey );
				p_stValue = SCR_fnp_st_Link_SearchKey( &g_stSectionLinkTable, szFullRef );
				if (p_stValue != 0)
					break;
			}
		}
		p_stRefValue->stInfos.ulLong2 =  (unsigned long) p_stValue;
		m_oProgress.StepIt();
	}


	/*
	 * get end time and display elapse time
	 */
	time( &lEndTime );
	m_oProgress.ShowWindow(FALSE);
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( TRUE );

	m_fn_vDisplayLastOperationTime( lEndTime - lStartTime );

	fn_vFillBadReferenceList();
}

/*
 ----------------------------------------------------------------------------------------
  Description   : LBN_SELCHANGE on IDC_LIST_BADREF
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnSelchangeListBadref() 
{
	int	iIndex = m_p_oLBBadRef->GetCurSel();

	if (iIndex != LB_ERR )
		m_p_stCurrentBadRef = (SCR_tdst_Link_Value *) m_p_oLBBadRef->GetItemData( iIndex );
	else
		m_p_stCurrentBadRef = NULL;

	fn_vFillSectionWithBadReferenceList();
}



/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on a Param button
  nID                 -> id of param button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonParam( UINT nID )
{
	char cParam = (char) (nID - IDC_CHECK_PARAM0 );
	int  iState;

	iState = m_a_poBtParam[ cParam ]->GetCheck();
	m_p_stCurrentEntry->a_cIsRef[ cParam ] = iState;

	m_p_stCurrentEntry->cNumberOfRefs = 0;
	for (cParam = 0; cParam < m_p_stCurrentEntry->cNumberOfParams; cParam++)
	{
		if (m_p_stCurrentEntry->a_cIsRef[ cParam ])
			m_p_stCurrentEntry->cNumberOfRefs++;
	}
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_DBLCLK on IDC_LIST_SECTIONWITHBADREF
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnDblclkListSectionwithbadref() 
{
	int	iIndex = m_p_oLBSectionWithBadRef->GetCurSel();
	char szText[1024], *p_szText = szText;
	char szCommand[1024];
	if (iIndex == LB_ERR)
		return;


	m_p_oLBSectionWithBadRef->GetText( iIndex, szText );

	if ( (p_szText = strchr( szText, '^')) != NULL)
		*p_szText = 0;

	p_szText = szText;
	while (*p_szText++ != ']');
	p_szText++;

	sprintf( szCommand, "Notepad %s%s", g_stIniData.szRootForSection, p_szText );
	WinExec(szCommand, SW_SHOWNORMAL);
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_STAT button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonStat()
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	long			lFile;
	char			szFileName[ MAX_PATH ];
	char			*p_szFileName;
	time_t			lStartTime, lEndTime;
	tdstScriptStat	stStat;
	char			*d_szStatText, *p_szStatText;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	/*
	 * init progression and display for working
	 */
	m_fn_vInitProgessBar( 0, m_lNumberOfFiles );
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( FALSE );
	m_oProgress.ShowWindow(TRUE);

	/*
	 * get start time
	 */
	fn_Scr_vInitStat( &stStat );
	time( &lStartTime );

	/*
	 * parse each file
	 */
	strcpy( szFileName, g_stIniData.szRootForSection );
	p_szFileName = szFileName + g_stIniData.lRootForSectionLength;

	for (lFile = 0; lFile < m_lNumberOfFiles; lFile++) 
	{
		m_p_oLBFiles->GetText( lFile, p_szFileName );
		m_p_oLBFiles->SetCurSel( lFile );
		fn_Scr_vParseFileForStat( szFileName, &stStat );
		m_oProgress.StepIt();
		
	}

	/*
	 * build stat text
	 */
	p_szStatText = d_szStatText = (char *) malloc ( 10000 );

	p_szStatText += sprintf( p_szStatText, "Number of files        -> %d\r\n", stStat.ulNbFiles );
	p_szStatText += sprintf( p_szStatText, "Number of characters   -> %d\r\n", stStat.ulNbChars );
	p_szStatText += sprintf( p_szStatText, "Biggest file           -> (%d) %s\r\n", stStat.ulMaxSize, stStat.szBiggestFile );
	p_szStatText += sprintf( p_szStatText, "Number of lines        -> %d\r\n", stStat.ulNbLines );
	p_szStatText += sprintf( p_szStatText, "    Directives         -> %d\r\n", stStat.ulNbDirectiveLines );
	p_szStatText += sprintf( p_szStatText, "    Begin section      -> %d\r\n", stStat.ulNbBeginSectionLines );
	p_szStatText += sprintf( p_szStatText, "    End section        -> %d\r\n", stStat.ulNbEndSectionLines );
	p_szStatText += sprintf( p_szStatText, "    Entries            -> %d\r\n", stStat.ulNbEntryLines );
	p_szStatText += sprintf( p_szStatText, "Max sections in a file -> %d (%s)\r\n", stStat.ulNbMaxSectionInAFile, stStat.szFileWithMaxSection);
	p_szStatText += sprintf( p_szStatText, "Section name first part\r\n" );
	p_szStatText += sprintf( p_szStatText, "    Min -> (%d) %s\r\n", stStat.ulSectionNameMinLength, stStat.szSectionNameMin );
	p_szStatText += sprintf( p_szStatText, "    Max -> (%d) %s\r\n", stStat.ulSectionNameMaxLength, stStat.szSectionNameMax );
	p_szStatText += sprintf( p_szStatText, "Section name second part\r\n" );
	p_szStatText += sprintf( p_szStatText, "    Max     -> (%d) %s\r\n", stStat.ulSectionExtMaxLength, stStat.szSectionExtMax );
	p_szStatText += sprintf( p_szStatText, "    Average -> %f\r\n", stStat.fSectionExtAverageLength );
	p_szStatText += sprintf( p_szStatText, "Entry name\r\n" );
	p_szStatText += sprintf( p_szStatText, "    Min -> (%d) %s\r\n", stStat.ulEntryMinLength, stStat.szEntryMin );
	p_szStatText += sprintf( p_szStatText, "    Max -> (%d) %s\r\n", stStat.ulEntryMaxLength, stStat.szEntryMax );
	p_szStatText += sprintf( p_szStatText, "Format max length      -> %d\r\n", stStat.ulFormatMaxLength );
	p_szStatText += sprintf( p_szStatText, "Params max number      -> %d\r\n", stStat.cMaxParams );
	p_szStatText += sprintf( p_szStatText, "Params max length      -> %d\r\n", stStat.ulParamsMaxLength );

	GetDlgItem( IDC_EDIT_STAT )->SetWindowText( d_szStatText );

	free ( d_szStatText );

	/*
	 * get end time and display elapse time
	 */
	time( &lEndTime );
	m_oProgress.ShowWindow(FALSE);
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( TRUE );
	
	m_fn_vDisplayLastOperationTime( lEndTime - lStartTime );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_PROCESS button
 ----------------------------------------------------------------------------------------
 */
void CChkDataDlg::OnButtonModstat() 
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	long		lFile;
	char		szFileName[ MAX_PATH ];
	char		*p_szFileName;
	time_t		lStartTime, lEndTime;
    tdstModStat stStat;
    char		*d_szStatText, *p_szStatText;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	/*
	 * init progression and display for working
	 */
	m_fn_vInitProgessBar( 0, m_lNumberOfFiles );
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( FALSE );
	m_oProgress.ShowWindow(TRUE);

    fn_Scr_vInitModStat( &stStat );

	/*
	 * get start time
	 */
	time( &lStartTime );

	/*
	 * parse each file
	 */
	strcpy( szFileName, g_stIniData.szRootForSection );
	p_szFileName = szFileName + g_stIniData.lRootForSectionLength;

	for (lFile = 0; lFile < m_lNumberOfFiles; lFile++) 
	{
		m_p_oLBFiles->GetText( lFile, p_szFileName );
		m_p_oLBFiles->SetCurSel( lFile );
        if ( strstr( szFileName, ".mod") )
		    fn_Scr_vParseFileToCountModData( szFileName, &stStat );
		m_oProgress.StepIt();
		
	}

    /*
	 * get end time and display elapse time
	 */
	time( &lEndTime );
	m_oProgress.ShowWindow(FALSE);
	GetDlgItem( IDC_STATIC_NUMBEROFFILES )->ShowWindow( TRUE );
	
	m_fn_vDisplayLastOperationTime( lEndTime - lStartTime );

  	/*
	 * build stat text
	 */
	p_szStatText = d_szStatText = (char *) malloc ( 10000 );

	p_szStatText += sprintf( p_szStatText, "Number of Object       -> %d\r\n", stStat.ulNumber );
	p_szStatText += sprintf( p_szStatText, "Number of Points       -> %d - %d\r\n", stStat.ulMinPoints, stStat.ulMaxPoints );
    p_szStatText += sprintf( p_szStatText, "Number of Edges        -> %d - %d\r\n", stStat.ulMinEdges, stStat.ulMaxEdges );
    p_szStatText += sprintf( p_szStatText, "Number of Element      -> %d - %d\r\n", stStat.ulMinElement, stStat.ulMaxElement );

	GetDlgItem( IDC_EDIT_STAT )->SetWindowText( d_szStatText );

	free ( d_szStatText );

}
