// EventsHistoryPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "EventsHistoryPage.h"

//included for _chdir
#include <direct.h> 


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

/////////////////////////////////////////////////////////////////////////////
// CEventsHistoryPage property page

IMPLEMENT_DYNCREATE(CEventsHistoryPage, CPropertyPage)

CEventsHistoryPage::CEventsHistoryPage() : CPropertyPage(CEventsHistoryPage::IDD)
{
	//{{AFX_DATA_INIT(CEventsHistoryPage)
	m_bShowBankWithId = FALSE;
	m_bShowEventWithId = FALSE;
	m_lBankId = 0;
	m_lEventId = 0;
	m_bDebugBreak = FALSE;
	m_bFirstOccurence = FALSE;
	m_bWatchMode = FALSE;
	m_LCBPath = _T("");
	//}}AFX_DATA_INIT
}

CEventsHistoryPage::~CEventsHistoryPage()
{
}

void CEventsHistoryPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEventsHistoryPage)
	DDX_Control(pDX, IDC_WATCH_LIST, m_WatchList);
	DDX_Control(pDX, IDC_EVENTS_HISTORY_LIST, m_EventsList);
	DDX_Check(pDX, IDC_CHECK_BANK, m_bShowBankWithId);
	DDX_Check(pDX, IDC_CHECK_EVENT, m_bShowEventWithId);
	DDX_Text(pDX, IDC_EDIT_BANK_ID, m_lBankId);
	DDV_MinMaxLong(pDX, m_lBankId, 0, 256);
	DDX_Text(pDX, IDC_EDIT_EVENT_ID, m_lEventId);
	DDV_MinMaxLong(pDX, m_lEventId, 0, 1024);
	DDX_Check(pDX, IDC_CHECK_DEBUG_ON_EVENT, m_bDebugBreak);
	DDX_Check(pDX, IDC_CHECK_FIRST_OCCUR, m_bFirstOccurence);
	DDX_Check(pDX, IDC_CHECK_WATCH, m_bWatchMode);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEventsHistoryPage, CPropertyPage)
	//{{AFX_MSG_MAP(CEventsHistoryPage)
	ON_BN_CLICKED(IDC_RESET_BUTTON, OnResetButton)
	ON_BN_CLICKED(IDC_CHECK_FIRST_OCCUR, OnCheckFirstOccur)
	ON_BN_CLICKED(IDC_CHECK_BANK, OnCheckBank)
	ON_BN_CLICKED(IDC_CHECK_EVENT, OnCheckEvent)
	ON_EN_CHANGE(IDC_EDIT_BANK_ID, OnChangeEditBankId)
	ON_EN_CHANGE(IDC_EDIT_EVENT_ID, OnChangeEditEventId)
	ON_BN_CLICKED(IDC_HELP_WATCH, OnHelpWatch)
	ON_BN_CLICKED(IDC_CHECK_WATCH, OnCheckWatch)
	ON_BN_CLICKED(IDC_EMPTY_WATCH, OnEmptyWatch)
	ON_BN_CLICKED(IDC_PAUSE_BUTTON, OnPauseButton)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEventsHistoryPage message handlers

BOOL CEventsHistoryPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Initialise the ClistCtrl containing Events descriptions
	LV_COLUMN* lvc;

	lvc = (LV_COLUMN*)malloc(sizeof(LV_COLUMN));

	
	lvc->mask = LVCF_FMT |LVCF_SUBITEM 
		| LVCF_TEXT | LVCF_WIDTH;			// members contain valid information
    lvc->fmt = LVCFMT_LEFT;					// Alignment of the column
    lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColTime;							// width of the column, in pixels
    lvc->pszText = "Time";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_EventsList.InsertColumn(0, lvc);


	lvc->pszText = "Event Name";			// column heading 
	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColEvtName;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_EventsList.InsertColumn(1, lvc);

	lvc->pszText = "Event Id";				// column heading 
	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColEvtId;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_EventsList.InsertColumn(2, lvc);
	
	lvc->pszText = "Bank Name";				// column heading 
	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColBankName;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_EventsList.InsertColumn(3, lvc);
	
	lvc->pszText = "Bank Id";				// column heading 
	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColBankId;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_EventsList.InsertColumn(4, lvc);

	
	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColEvtType;							// width of the column, in pixels
	lvc->pszText = "Event Type";					// column heading 
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_EventsList.InsertColumn(5, lvc);

	lvc->cx = pSuperVisApp->m_EventPageRegKeys.iColInfo;							// width of the column, in pixels
	lvc->pszText = "Info";					// column heading 
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_EventsList.InsertColumn(6, lvc);

	free( lvc );

	
	//Set the limit length of the edit box
	((CEdit *)(GetDlgItem(IDC_EDIT_BANK_ID)))->LimitText(3);
	((CEdit *)(GetDlgItem(IDC_EDIT_EVENT_ID)))->LimitText(3);


	//Init du "pause" mode
	m_bPauseProcess = FALSE;
	
	UpdateData(FALSE);

	m_lBankId = pSuperVisApp->m_EventPageRegKeys.iCheckBankId;
	m_lEventId = pSuperVisApp->m_EventPageRegKeys.iCheckEventId;
	m_bFirstOccurence = pSuperVisApp->m_EventPageRegKeys.bCheckFirstOccur;

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CEventsHistoryPage::RefreshEventsHistoList()
{


	long i;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();


	//Clear the list
	m_EventsList.DeleteAllItems();
	
	
	//We choose the good array to use for display
	if(m_bFirstOccurence)
	{
		//Cycle through the pointer Array to filter
		for (i = 0; i < pSuperVisApp->m_apFirstOccurEventHistoArray.GetSize(); i ++ )
		{
			FilterAndDisplayFirstOccurEvent(i);

		}	
		
	
	}
	else
	{
		//Cycle through the pointer Array to filter
		for (i = 0; i < pSuperVisApp->m_apEventHistoArray.GetSize(); i ++ )
		{
			FilterAndDisplayEvent(i);

		}	
	}
	
}

void CEventsHistoryPage::FilterAndDisplayEvent(long lIndex)
{	

	char buffer[25];
	
	unsigned long ulBankId;
	unsigned long ulEventId;
	

	EventsHistElement *evtHisto;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	evtHisto = (EventsHistElement *)(pSuperVisApp->m_apEventHistoArray.GetAt(lIndex));

	ulBankId = (0xFFFF0000 & evtHisto->evt_Id) >> 16;
	ulEventId= (0x0000FFFF & evtHisto->evt_Id);

	//
	//Check if we have have to filter the events
	//******************************************

	//Retrieve data from the control
	PostMessage(WM_USER_UPDATE_DATA_TRUE, 0, 0);

	//If the watch checkBox is checked we need to filter
	if(m_bWatchMode)
	{

		BOOL bIsInWatchList = FALSE;
		int i;
		int iCount = m_WatchList.GetCount();
		
		//if list is empty with dont display anything
		if (iCount == 0)
			return;
		
		
		for (i = 0; i < iCount; i++)
		{
			//Look through the watch list for the same Event ID
			if (evtHisto->evt_Id == (tdxId)m_WatchList.GetItemData(i))
				bIsInWatchList = TRUE;
			
		}

		//if the event wasnt in the watch list we leave
		if(!bIsInWatchList)
			return;


	} 
	else
	{

		if (m_bShowEventWithId)
		{
			if (m_lEventId != (long)ulEventId)
				return;

		}

		if (m_bShowBankWithId)
		{
			if (m_lBankId != (long)ulBankId)
				return;
		}
	}

	
	
	//Check if we have to debugbreak before displaying the events
	if(m_bDebugBreak)
	{
		DebugBreak();
	}



	//Display time
	sprintf(buffer,"%02d:%02d:%02d:%03d",
		evtHisto->time.wHour,
		evtHisto->time.wMinute,
		evtHisto->time.wSecond,
		evtHisto->time.wMilliseconds);
	m_EventsList.InsertItem( 0, buffer );


	//Set the item data with the eventsArray index, to be used later for drag&drop, pop-memnu, etc.
	m_EventsList.SetItemData( 0, (DWORD)lIndex );


	//Event Name
	m_EventsList.SetItem(0, 1, LVIF_TEXT, (LPCSTR)evtHisto->cEvtName, 0, 0, 0, NULL );


	//Event ID 
	sprintf(buffer, "%lu", ulEventId);
	m_EventsList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Bank Name
	m_EventsList.SetItem(0, 3, LVIF_TEXT, (LPCSTR)evtHisto->cEvtBankName, 0, 0, 0, NULL );
	
	//Bank ID 
	sprintf(buffer, "%lu", ulBankId);
	m_EventsList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	
	
	//Type
	switch(evtHisto->evt_eType){

	case EVT_SON_STOP_ALL:
		sprintf(buffer, "EVT_SON_STOP_ALL");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_STOP_N_GO:
		sprintf(buffer, "EVT_SON_STOP_N_GO");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_EFFET:
		sprintf(buffer, "EVT_SON_EFFET");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PITCH:
		sprintf(buffer, "EVT_SON_PITCH");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_VOLUME:
		sprintf(buffer, "EVT_SON_VOLUME");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PAN:
		sprintf(buffer, "EVT_SON_PAN");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PLAY:
		sprintf(buffer, "EVT_SON_PLAY");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_STOP:
		sprintf(buffer, "EVT_SON_STOP");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;

	}

	//Info supp.
	m_EventsList.SetItem(0, 6, LVIF_TEXT, (LPCSTR)evtHisto->cInfo, 0, 0, 0, NULL );

/*
	//Sort items
	m_EventsList.SortTextItems( m_EventsList.nSortedCol, m_EventsList.bSortAscending, 0 ,-1);
*/}



void CEventsHistoryPage::FilterAndDisplayFirstOccurEvent(long lIndex)
{	

	char buffer[25];
	
	unsigned long ulBankId;
	unsigned long ulEventId;
	

	EventsHistElement *evtHisto;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	evtHisto = (EventsHistElement *)(pSuperVisApp->m_apFirstOccurEventHistoArray.GetAt(lIndex));

	ulBankId = (0xFFFF0000 & evtHisto->evt_Id) >> 16;
	ulEventId= (0x0000FFFF & evtHisto->evt_Id);

	//
	//Check if we have have to filter the events
	//******************************************

	//Retrieve data from the control
	PostMessage(WM_USER_UPDATE_DATA_TRUE, 0, 0);

	//If the watch checkBox is checked we need to filter
	if(m_bWatchMode)
	{

		BOOL bIsInWatchList = FALSE;
		int i;
		int iCount = m_WatchList.GetCount();
		
		//if list is empty with dont display anything
		if (iCount == 0)
			return;
		
		
		for (i = 0; i < iCount; i++)
		{
			//Look through the watch list for the same Event ID
			if (evtHisto->evt_Id == (tdxId)m_WatchList.GetItemData(i))
				bIsInWatchList = TRUE;
			
		}

		//if the event wasnt in the watch list we leave
		if(!bIsInWatchList)
			return;


	} 
	else
	{

		if (m_bShowEventWithId)
		{
			if (m_lEventId != (long)ulEventId)
				return;

		}

		if (m_bShowBankWithId)
		{
			if (m_lBankId != (long)ulBankId)
				return;
		}
	}

	
	
	//Check if we have to debugbreak before displaying the events
	if(m_bDebugBreak)
	{
		DebugBreak();
	}



	//Display time
	sprintf(buffer,"%02d:%02d:%02d:%03d",
		evtHisto->time.wHour,
		evtHisto->time.wMinute,
		evtHisto->time.wSecond,
		evtHisto->time.wMilliseconds);
	m_EventsList.InsertItem( 0, buffer );


	//Set the item data with the eventsArray index, to be used later for drag&drop, pop-memnu, etc.
	m_EventsList.SetItemData( 0, (DWORD)lIndex );


	//Event Name
	m_EventsList.SetItem(0, 1, LVIF_TEXT, (LPCSTR)evtHisto->cEvtName, 0, 0, 0, NULL );


	//Event ID 
	sprintf(buffer, "%lu", ulEventId);
	m_EventsList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Bank Name
	m_EventsList.SetItem(0, 3, LVIF_TEXT, (LPCSTR)evtHisto->cEvtBankName, 0, 0, 0, NULL );
	
	//Bank ID 
	sprintf(buffer, "%lu", ulBankId);
	m_EventsList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	
	
	//Type
	switch(evtHisto->evt_eType){

	case EVT_SON_STOP_ALL:
		sprintf(buffer, "EVT_SON_STOP_ALL");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_STOP_N_GO:
		sprintf(buffer, "EVT_SON_STOP_N_GO");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_EFFET:
		sprintf(buffer, "EVT_SON_EFFET");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PITCH:
		sprintf(buffer, "EVT_SON_PITCH");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_VOLUME:
		sprintf(buffer, "EVT_SON_VOLUME");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PAN:
		sprintf(buffer, "EVT_SON_PAN");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_PLAY:
		sprintf(buffer, "EVT_SON_PLAY");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case EVT_SON_STOP:
		sprintf(buffer, "EVT_SON_STOP");
		m_EventsList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;

	}


	//Info supp.
	m_EventsList.SetItem(0, 6, LVIF_TEXT, (LPCSTR)evtHisto->cInfo, 0, 0, 0, NULL );

/*
	//Sort items
	m_EventsList.SortTextItems( m_EventsList.nSortedCol, m_EventsList.bSortAscending, 0 ,-1);
*/
}



void CEventsHistoryPage::OnResetButton() 
{

	int i;
	EventsHistElement* temp;

	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	
	//Free the events history array
	for (i= 0;  i < pSuperVisApp->m_apEventHistoArray.GetSize() ;i++)
	{
		temp = (EventsHistElement *)(pSuperVisApp->m_apEventHistoArray.GetAt(i));
		delete temp;
	}

	pSuperVisApp->m_apEventHistoArray.RemoveAll();


	//Free the events history array
	for (i= 0;  i < pSuperVisApp->m_apFirstOccurEventHistoArray.GetSize() ;i++)
	{
		temp = (EventsHistElement *)(pSuperVisApp->m_apFirstOccurEventHistoArray.GetAt(i));
		delete temp;
	}

	pSuperVisApp->m_apFirstOccurEventHistoArray.RemoveAll();


	
	//Empty the CListCtrl
	m_EventsList.DeleteAllItems();
	
}

void CEventsHistoryPage::OnCheckFirstOccur() 
{
	
	//Update the variable
	UpdateData(TRUE);
	RefreshEventsHistoList();
	
}

void CEventsHistoryPage::OnCheckBank() 
{

	//Update the variable
	UpdateData(TRUE);

	if (m_bShowBankWithId)
	{
		GetDlgItem(IDC_CHECK_WATCH)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_CHECK_WATCH)->EnableWindow(TRUE);
	}

	RefreshEventsHistoList();
	
}

void CEventsHistoryPage::OnCheckEvent() 
{
	
	//Update the variable
	UpdateData(TRUE);
	
	if (m_bShowEventWithId)
	{
		GetDlgItem(IDC_CHECK_WATCH)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_CHECK_WATCH)->EnableWindow(TRUE);
	}
	
	RefreshEventsHistoList();
	
}


void CEventsHistoryPage::OnChangeEditBankId() 
{

	//Keep the bank Id in the 0 - 256 range
	UpdateData(TRUE);
	if (m_lBankId > 256)
	{
		m_lBankId = 256;
		UpdateData(FALSE);

	}

	RefreshEventsHistoList();

}

void CEventsHistoryPage::OnChangeEditEventId() 
{

	//Keep the Event Id in the 0 - 1024 range
	UpdateData(TRUE);
	if (m_lEventId > 1024)
	{
		m_lEventId = 1024;
		UpdateData(FALSE);

	}


	RefreshEventsHistoList();
	
}

void CEventsHistoryPage::OnHelpWatch() 
{

	MessageBox("The Watch List allow to show a selected group of events. Right-click on the Events List Historic to add a new events	","Watch List Help",MB_OK);
	
	
}

void CEventsHistoryPage::OnCheckWatch() 
{
	
	UpdateData(TRUE);

	
	if(m_bWatchMode)
	{
		//If we are in Watch List mode we disable the normal mode
		GetDlgItem(IDC_CHECK_BANK)->EnableWindow(FALSE);	
		GetDlgItem(IDC_EDIT_BANK_ID)->EnableWindow(FALSE);	
		GetDlgItem(IDC_CHECK_EVENT)->EnableWindow(FALSE);	
		GetDlgItem(IDC_EDIT_EVENT_ID)->EnableWindow(FALSE);	
	}
	else
	{
		
		GetDlgItem(IDC_CHECK_BANK)->EnableWindow(TRUE);	
		GetDlgItem(IDC_EDIT_BANK_ID)->EnableWindow(TRUE);	
		GetDlgItem(IDC_CHECK_EVENT)->EnableWindow(TRUE);	
		GetDlgItem(IDC_EDIT_EVENT_ID)->EnableWindow(TRUE);	
	}

	RefreshEventsHistoList();

}


void CEventsHistoryPage::SendEventInWatchList()
{
	unsigned long ulBankId;
	unsigned long ulEventId;
	int iArrayIndex;
	EventsHistElement *evtHisto;

	char buffer[50];

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	// The index of the selected item
	int i = m_EventsList.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);


	//retrieve the array index 
	iArrayIndex = m_EventsList.GetItemData(i);
	
	
	//Retrieve the event
	evtHisto = (EventsHistElement *)(pSuperVisApp->m_apEventHistoArray.GetAt(iArrayIndex));

	ulBankId = (0xFFFF0000 & evtHisto->evt_Id) >> 16;
	ulEventId= (0x0000FFFF & evtHisto->evt_Id);

	
	sprintf(buffer, "%s   E%lu:B%lu",evtHisto->cEvtName, ulEventId, ulBankId);
	int iWatchListIndex = m_WatchList.AddString( buffer );


	//We associate the event ID with the itemdata of the listbox.
	//Will be use to compare when a new event will be add in the 
	//events Historic list.
	m_WatchList.SetItemData(iWatchListIndex, (DWORD)evtHisto->evt_Id);


}

void CEventsHistoryPage::OnEmptyWatch() 
{
	m_WatchList.ResetContent();
	RefreshEventsHistoList();
	
}


BOOL CEventsHistoryPage::PreTranslateMessage(MSG* pMsg) 
{
	
	//We send the updatedata(TRUE) here
	//We cannot pass the CWnd across thread and this cause problem while using updatedata
	if (pMsg->message == WM_USER_UPDATE_DATA_TRUE){
		UpdateData(TRUE);
	}

	return CPropertyPage::PreTranslateMessage(pMsg);
}

void CEventsHistoryPage::OnPauseButton() 
{
	if(m_bPauseProcess)
	{
		m_bPauseProcess = FALSE;

		GetDlgItem(IDC_PAUSE_BUTTON)->SetWindowText("&Pause Process");

		//We refresh the list to add the event thatwe missed during pause.
		RefreshEventsHistoList();
	} 
	else
	{
		m_bPauseProcess = TRUE;

		GetDlgItem(IDC_PAUSE_BUTTON)->SetWindowText("&Unpause Process");
	}
	
}




void CEventsHistoryPage::GetInitWindowSize() 
{
/*
	//Retrieve all windows size
	((CWnd *)(GetDlgItem(IDC_EVENTS_HISTORY_LIST)))->GetWindowRect(m_arInitWindowSize[0]);
	((CWnd *)(GetDlgItem(IDC_EVENTS_HISTORY_LIST)))->ScreenToClient(&m_arInitWindowSize[0]);

	((CWnd *)(GetDlgItem(IDC_CHECK_EVENT)))->GetWindowRect(m_arInitWindowSize[1]);
	((CWnd *)(GetDlgItem(IDC_CHECK_EVENT)))->ScreenToClient(&m_arInitWindowSize[1]);

	((CWnd *)(GetDlgItem(IDC_CHECK_DEBUG_ON_EVENT)))->GetWindowRect(m_arInitWindowSize[2]);
	((CWnd *)(GetDlgItem(IDC_CHECK_DEBUG_ON_EVENT)))->ScreenToClient(&m_arInitWindowSize[2]);

	((CWnd *)(GetDlgItem(IDC_CHECK_FIRST_OCCUR)))->GetWindowRect(m_arInitWindowSize[3]);
	((CWnd *)(GetDlgItem(IDC_CHECK_FIRST_OCCUR)))->ScreenToClient(&m_arInitWindowSize[3]);

	((CWnd *)(GetDlgItem(IDC_EDIT_EVENT_ID)))->GetWindowRect(m_arInitWindowSize[4]);
	((CWnd *)(GetDlgItem(IDC_EDIT_EVENT_ID)))->ScreenToClient(&m_arInitWindowSize[4]);

	((CWnd *)(GetDlgItem(IDC_CHECK_BANK)))->GetWindowRect(m_arInitWindowSize[5]);
	((CWnd *)(GetDlgItem(IDC_CHECK_BANK)))->ScreenToClient(&m_arInitWindowSize[5]);

	((CWnd *)(GetDlgItem(IDC_EDIT_BANK_ID)))->GetWindowRect(m_arInitWindowSize[6]);
	((CWnd *)(GetDlgItem(IDC_EDIT_BANK_ID)))->ScreenToClient(&m_arInitWindowSize[6]);

	((CWnd *)(GetDlgItem(IDC_RESET_BUTTON)))->GetWindowRect(m_arInitWindowSize[7]);
	((CWnd *)(GetDlgItem(IDC_RESET_BUTTON)))->ScreenToClient(&m_arInitWindowSize[7]);

	((CWnd *)(GetDlgItem(IDC_WATCH_LIST)))->GetWindowRect(m_arInitWindowSize[8]);
	((CWnd *)(GetDlgItem(IDC_WATCH_LIST)))->ScreenToClient(&m_arInitWindowSize[8]);

	((CWnd *)(GetDlgItem(IDC_EMPTY_WATCH)))->GetWindowRect(m_arInitWindowSize[9]);
	((CWnd *)(GetDlgItem(IDC_EMPTY_WATCH)))->ScreenToClient(&m_arInitWindowSize[9]);

	((CWnd *)(GetDlgItem(IDC_CHECK_WATCH)))->GetWindowRect(m_arInitWindowSize[10]);
	((CWnd *)(GetDlgItem(IDC_CHECK_WATCH)))->ScreenToClient(&m_arInitWindowSize[10]);


	((CWnd *)(GetDlgItem(IDC_EDIT_LCB_PATH)))->GetWindowRect(m_arInitWindowSize[11]);
	((CWnd *)(GetDlgItem(IDC_EDIT_LCB_PATH)))->ScreenToClient(&m_arInitWindowSize[11]);

	((CWnd *)(GetDlgItem(IDC_BROWSE_FOR_LCB)))->GetWindowRect(m_arInitWindowSize[12]);
	((CWnd *)(GetDlgItem(IDC_BROWSE_FOR_LCB)))->ScreenToClient(&m_arInitWindowSize[12]);

	((CWnd *)(GetDlgItem(IDC_PAUSE_BUTTON)))->GetWindowRect(m_arInitWindowSize[13]);
	((CWnd *)(GetDlgItem(IDC_PAUSE_BUTTON)))->ScreenToClient(&m_arInitWindowSize[13]);

	GetWindowRect(m_arInitWindowSize[14]);
	ScreenToClient(&m_arInitWindowSize[14]);

  */
	
	  /*	
	
	
	CONTROL         "List1",IDC_EVENTS_HISTORY_LIST,"SysListView32",
                    LVS_REPORT | LVS_SINGLESEL | WS_BORDER | WS_TABSTOP,21,
                    87,397,187
    GROUPBOX        "Events Historic List Settings",IDC_STATIC,7,305,212,103
    CONTROL         "Only display events with Event Id = ",IDC_CHECK_EVENT,
                    "Button",BS_AUTOCHECKBOX | WS_TABSTOP,27,342,130,10
    CONTROL         "Debug Break on Next Event (need Visual C++ installed)",
                    IDC_CHECK_DEBUG_ON_EVENT,"Button",BS_AUTOCHECKBOX | 
                    WS_TABSTOP,27,382,190,9
    CONTROL         "Display only first occurence of Events",
                    IDC_CHECK_FIRST_OCCUR,"Button",BS_AUTOCHECKBOX | 
                    WS_TABSTOP,27,363,134,11
    EDITTEXT        IDC_EDIT_EVENT_ID,161,340,22,12,ES_AUTOHSCROLL | 
                    ES_NUMBER
    CONTROL         "Only display events with Bank Id = ",IDC_CHECK_BANK,
                    "Button",BS_AUTOCHECKBOX | WS_TABSTOP,27,321,126,12
    EDITTEXT        IDC_EDIT_BANK_ID,161,321,22,12,ES_AUTOHSCROLL | 
                    ES_NUMBER
    PUSHBUTTON      "&Reset List",IDC_RESET_BUTTON,348,66,70,15
    GROUPBOX        "Events Historic List",IDC_STATIC,7,38,425,252
    LTEXT           "Show all events that occured since the engine started. Press the ""Reset List"" button to empty the list",
                    IDC_STATIC,19,52,399,11,SS_SUNKEN | WS_BORDER
    GROUPBOX        "Events Watch List",IDC_STATIC,231,307,201,112
    LISTBOX         IDC_WATCH_LIST,298,334,124,75,LBS_SORT | 
                    LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
    PUSHBUTTON      "Empty  List",IDC_EMPTY_WATCH,240,363,48,14
    CONTROL         "Only Show Events In Watch List",IDC_CHECK_WATCH,"Button",
                    BS_AUTOCHECKBOX | WS_TABSTOP,240,318,130,8
    PUSHBUTTON      "?",IDC_HELP_WATCH,403,318,19,12
    EDITTEXT        IDC_EDIT_LCB_PATH,16,17,288,12,ES_AUTOHSCROLL
    PUSHBUTTON      "&Browse for LCB file....",IDC_BROWSE_FOR_LCB,340,15,80,
                    15
    GROUPBOX        "LCB filename:",IDC_STATIC,7,7,425,28
    PUSHBUTTON      "&Pause Process",IDC_PAUSE_BUTTON,261,66,70,15

 */
}


void CEventsHistoryPage::SetWindowSize(int iDeltaX, int iDeltaY) 
{
/*
	//Retrieve all windows size

	CRect rect = m_arInitWindowSize[0];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[0].right - m_arInitWindowSize[0].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_EVENTS_HISTORY_LIST)))->MoveWindow(rect);

	rect = m_arInitWindowSize[1];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[1].right - m_arInitWindowSize[1].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_CHECK_EVENT)))->MoveWindow(m_arInitWindowSize[1]);

	rect = m_arInitWindowSize[2];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[2].right - m_arInitWindowSize[2].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_CHECK_DEBUG_ON_EVENT)))->MoveWindow(m_arInitWindowSize[2]);

	rect = m_arInitWindowSize[3];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[3].right - m_arInitWindowSize[3].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_CHECK_FIRST_OCCUR)))->MoveWindow(m_arInitWindowSize[3]);


	rect = m_arInitWindowSize[4];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[4].right - m_arInitWindowSize[4].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_EDIT_EVENT_ID)))->MoveWindow(m_arInitWindowSize[4]);

	rect = m_arInitWindowSize[5];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[5].right - m_arInitWindowSize[5].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_CHECK_BANK)))->MoveWindow(m_arInitWindowSize[5]);


	rect = m_arInitWindowSize[6];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[6].right - m_arInitWindowSize[6].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_EDIT_BANK_ID)))->MoveWindow(m_arInitWindowSize[6]);


	rect = m_arInitWindowSize[7];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[7].right - m_arInitWindowSize[7].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_RESET_BUTTON)))->MoveWindow(m_arInitWindowSize[7]);


	rect = m_arInitWindowSize[8];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[8].right - m_arInitWindowSize[8].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_WATCH_LIST)))->MoveWindow(m_arInitWindowSize[8]);


	rect = m_arInitWindowSize[9];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[9].right - m_arInitWindowSize[9].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_EMPTY_WATCH)))->MoveWindow(m_arInitWindowSize[9]);


	rect = m_arInitWindowSize[10];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[10].right - m_arInitWindowSize[10].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_CHECK_WATCH)))->MoveWindow(m_arInitWindowSize[10]);

	rect = m_arInitWindowSize[11];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[11].right - m_arInitWindowSize[11].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_EDIT_LCB_PATH)))->MoveWindow(m_arInitWindowSize[11]);


	rect = m_arInitWindowSize[12];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[12].right - m_arInitWindowSize[12].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_BROWSE_FOR_LCB)))->MoveWindow(m_arInitWindowSize[12]);


	rect = m_arInitWindowSize[13];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[13].right - m_arInitWindowSize[13].left);
	rect.bottom += iDeltaY;
	((CWnd *)(GetDlgItem(IDC_PAUSE_BUTTON)))->MoveWindow(m_arInitWindowSize[13]);


	rect = m_arInitWindowSize[14];
	rect.right += iDeltaX;
	rect.left = rect.right - (m_arInitWindowSize[14].right - m_arInitWindowSize[14].left);
	rect.bottom += iDeltaY;
	MoveWindow(rect);

*/




}


long CEventsHistoryPage::AddToFirstEvtOccurArray(EventsHistElement* evt)
{

	long i, lSize;
	EventsHistElement * evtHistoFirstOccurTest;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	lSize = pSuperVisApp->m_apFirstOccurEventHistoArray.GetSize();
	 
	 //look in the Events array from 0 to the current event we are filtering.
	//We dont  want to compare it with himself , hence the " -1 " in the loop.
	for(i = 0; i < lSize ; i++)
	{
		evtHistoFirstOccurTest = (EventsHistElement *)(pSuperVisApp->m_apFirstOccurEventHistoArray.GetAt(i));
		if (evt->evt_Id == evtHistoFirstOccurTest->evt_Id)
			return -1;

	}


	//Create new array element
	EventsHistElement * newElement = new EventsHistElement;

	newElement->time = evt->time;
	newElement->evt_pstPtr = evt->evt_pstPtr;
	newElement->evt_Id = evt->evt_Id;
	newElement->evt_eType = evt->evt_eType;
	newElement->res_pstPtr = evt->res_pstPtr;
	newElement->asso = evt->asso;
	newElement->cEvtName = evt->cEvtName;
	newElement->cEvtBankName = evt->cEvtBankName;
	newElement->cInfo = evt->cInfo;

			
	return pSuperVisApp->m_apFirstOccurEventHistoArray.Add(newElement);

}



void CEventsHistoryPage::OnDestroy() 
{
	CPropertyPage::OnDestroy();

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	int ColWidth = m_EventsList.GetColumnWidth(0);
	pSuperVisApp->m_EventPageRegKeys.iColTime= ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(1);
	pSuperVisApp->m_EventPageRegKeys.iColEvtName= ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(2);
	pSuperVisApp->m_EventPageRegKeys.iColEvtId = ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(3);
	pSuperVisApp->m_EventPageRegKeys.iColBankName= ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(4);
	pSuperVisApp->m_EventPageRegKeys.iColBankId= ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(5);
	pSuperVisApp->m_EventPageRegKeys.iColEvtType = ColWidth;
	ColWidth = m_EventsList.GetColumnWidth(6);
	pSuperVisApp->m_EventPageRegKeys.iColInfo = ColWidth;


	pSuperVisApp->m_EventPageRegKeys.iCheckBankId = m_lBankId;
	pSuperVisApp->m_EventPageRegKeys.iCheckEventId = m_lEventId;
	pSuperVisApp->m_EventPageRegKeys.bCheckFirstOccur = m_bFirstOccurence;

}

void CEventsHistoryPage::SetEventsName(long lIndex)
{
	
	LPDISPATCH lpDispatch;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	EventsHistElement *evtHisto = (EventsHistElement *)(pSuperVisApp->m_apEventHistoArray.GetAt(lIndex));

	unsigned long ulBankId = (0xFFFF0000 & evtHisto->evt_Id) >> 16;
	unsigned long ulEventId= (0x0000FFFF & evtHisto->evt_Id);

	//Get the group using the Bank Name 
	IEventGroup Group;
	TRY
	{
		lpDispatch = pSuperVisApp->m_SuperVisThread->m_IGroups->FindItemByID((long)ulBankId);

	}
	CATCH(COleException, pEx)
	{
		// Simply show an error message to the user.
//		pEx->ReportError();
		return;
	}
	END_CATCH

	if (lpDispatch != NULL)
	{
		Group.AttachDispatch(lpDispatch);
		evtHisto->cEvtBankName = Group.GetName();
	}
	else 
		return;

	//Get the Events list
	IEvents Events;
	lpDispatch = Group.GetEvents();
	if (lpDispatch != NULL)
	{
		Events.AttachDispatch(lpDispatch);

	}
	else
		return;

	//Get the Event
	IEvent TheEvent;
	TRY
	{
		lpDispatch = Events.FindItemByID((long)ulEventId);

	}
	CATCH(COleException, pEx)
	{
		// Simply show an error message to the user.
//		pEx->ReportError();
		return;
	}
	END_CATCH
		
	if (lpDispatch != NULL)
	{
		TheEvent.AttachDispatch(lpDispatch);
		evtHisto->cEvtName = TheEvent.GetName();
	}
	

}


void CEventsHistoryPage::SetFirstOccurEventsName(long lIndex)
{

	LPDISPATCH lpDispatch;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();


	EventsHistElement *evtFirstOccurHisto = (EventsHistElement *)(pSuperVisApp->m_apFirstOccurEventHistoArray.GetAt(lIndex));

	unsigned long ulBankId = (0xFFFF0000 & evtFirstOccurHisto->evt_Id) >> 16;
	unsigned long ulEventId= (0x0000FFFF & evtFirstOccurHisto->evt_Id);

	//Get the group using the Bank Name
	IEventGroup Group;
	TRY
	{
		lpDispatch = pSuperVisApp->m_SuperVisThread->m_IGroups->FindItemByID(ulBankId);

	}
	CATCH(COleException, pEx)
	{
		// Simply show an error message to the user.
//		pEx->ReportError();
		return;
	}
	END_CATCH
	if (lpDispatch != NULL)
	{
		Group.AttachDispatch(lpDispatch);
		evtFirstOccurHisto->cEvtBankName = Group.GetName();
	}
	else
		return;

	//Get the Events list
	IEvents Events;
	lpDispatch = Group.GetEvents();
	if (lpDispatch != NULL)
	{
		Events.AttachDispatch(lpDispatch);
	}
	else
		return;

	//Get the Event
	IEvent TheEvent;
	TRY
	{
		lpDispatch = Events.FindItemByID(ulEventId);

	}
	CATCH(COleException, pEx)
	{
		// Simply show an error message to the user.
//		pEx->ReportError();
		return;
	}
	END_CATCH
	if (lpDispatch != NULL)
	{
		TheEvent.AttachDispatch(lpDispatch);
		evtFirstOccurHisto->cEvtName = TheEvent.GetName();
	}

}

BOOL CEventsHistoryPage::OnSetActive() 
{
	RefreshEventsHistoList();
	
	return CPropertyPage::OnSetActive();
}
