// MixerPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "MixerPage.h"

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

//ACP
#include "snddll.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMixerPage property page

IMPLEMENT_DYNCREATE(CMixerPage, CPropertyPage)

CMixerPage::CMixerPage() : CPropertyPage(CMixerPage::IDD)
{
	//{{AFX_DATA_INIT(CMixerPage)
	m_cSelectedRes = _T("");
	m_bDebugBreak = FALSE;
	m_bFirstOccurence = FALSE;
	m_bShowBankWithId = FALSE;
	m_bShowResWithId = FALSE;
	m_lBankId = 0;
	m_lResId = 0;
	m_ulSelectedResVol = 0;
	m_cSelectedResName = _T("");
	//}}AFX_DATA_INIT
}

CMixerPage::~CMixerPage()
{
}

void CMixerPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMixerPage)
	DDX_Control(pDX, IDC_VOLUME_SLIDER, m_VolumeSlider);
	DDX_Control(pDX, IDC_RES_LIST, m_ResList);
	DDX_Check(pDX, IDC_CHECK_DEBUG_ON_RES, m_bDebugBreak);
	DDX_Check(pDX, IDC_CHECK_FIRST_RES_OCCUR, m_bFirstOccurence);
	DDX_Check(pDX, IDC_CHECK_RES_BANK, m_bShowBankWithId);
	DDX_Check(pDX, IDC_CHECK_RES, m_bShowResWithId);
	DDX_Text(pDX, IDC_EDIT_RES_BANK_ID, m_lBankId);
	DDV_MinMaxLong(pDX, m_lBankId, 0, 256);
	DDX_Text(pDX, IDC_EDIT_RES_ID, m_lResId);
	DDV_MinMaxLong(pDX, m_lResId, 0, 1024);
	DDX_Text(pDX, IDC_EDIT_RES_VOL, m_ulSelectedResVol);
	DDV_MinMaxLong(pDX, m_ulSelectedResVol, 0, 127);
	DDX_Text(pDX, IDC_SEL_RES_NAME, m_cSelectedResName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMixerPage, CPropertyPage)
	//{{AFX_MSG_MAP(CMixerPage)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_PAUSE_RES_BUTTON, OnPauseResButton)
	ON_BN_CLICKED(IDC_RESET_RESLIST_BUTTON, OnResetReslistButton)
	ON_BN_CLICKED(IDC_CHECK_DEBUG_ON_RES, OnCheckDebugOnRes)
	ON_BN_CLICKED(IDC_CHECK_FIRST_RES_OCCUR, OnCheckFirstResOccur)
	ON_BN_CLICKED(IDC_CHECK_RES, OnCheckRes)
	ON_BN_CLICKED(IDC_CHECK_RES_BANK, OnCheckResBank)
	ON_EN_CHANGE(IDC_EDIT_RES_BANK_ID, OnChangeEditResBankId)
	ON_EN_CHANGE(IDC_EDIT_RES_ID, OnChangeEditResId)
	ON_BN_CLICKED(IDC_VOL_MOINS, OnVolMoins)
	ON_BN_CLICKED(IDC_VOL_PLUS, OnVolPlus)
	ON_WM_VSCROLL()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMixerPage message handlers



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


	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Initialise the ClistCtrl containing ressources 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_MixerPageRegKeys.iColTime;							// width of the column, in pixels
    lvc->pszText = "Time";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_ResList.InsertColumn(0, lvc);


	lvc->pszText = "Resource Name";		// column heading 
	lvc->cx = pSuperVisApp->m_MixerPageRegKeys.iColResName;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_ResList.InsertColumn(1, lvc);

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

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


	lvc->cx = pSuperVisApp->m_MixerPageRegKeys.iColVol;		// width of the column, in pixels
	lvc->pszText = "Volume";								// column heading 
    lvc->iSubItem = 6;										// index of subitem associated with column
	m_ResList.InsertColumn(6, lvc);

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

	free( lvc );

	
	//
	//Initialisation ds controles du DialogBox
	//
	m_VolumeSlider.SetRange( 0, 127, FALSE);
	m_VolumeSlider.SetPos(127);


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

	//Set LCB Edit box
	((CEdit *)(GetDlgItem(IDC_EDIT_RES_VOL)))->SetReadOnly( TRUE );
	((CEdit *)(GetDlgItem(IDC_SEL_RES_NAME)))->SetReadOnly( TRUE );

	m_cSelectedResName = "No Selected Resource";


	//Disable the + and - button and the slider.  The user wont be able to save nothing
	((CEdit *)(GetDlgItem(IDC_VOL_MOINS)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_VOL_PLUS)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_VOLUME_SLIDER)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_SAVE)))->EnableWindow( FALSE );
	((CEdit *)(GetDlgItem(IDC_PLAY)))->EnableWindow( FALSE );


	//Init du "pause" mode
	m_bPauseProcess = FALSE;


	m_lBankId = pSuperVisApp->m_MixerPageRegKeys.iCheckBankId;
	m_lResId = pSuperVisApp->m_MixerPageRegKeys.iCheckResId;
	m_bFirstOccurence = pSuperVisApp->m_MixerPageRegKeys.bCheckFirstOccur;



	
	UpdateData(FALSE);

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

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

	if(pMsg->message == WM_USER_UPDATE_DATA_FALSE)
	{
		//We send the updatedata(TRUE) here
		//We cannot pass the CWnd across thread and this cause problem while using updatedata
		UpdateData(FALSE);
	}
	
	return CPropertyPage::PreTranslateMessage(pMsg);
}



void CMixerPage::OnSave() 
{

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

	//Save the SoundEditor Document
	if(pSuperVisApp->m_SuperVisThread->bAutomationActivated)
	{	
		if (!pSuperVisApp->m_SuperVisThread->m_IEditor->SaveDocument(pSuperVisApp->m_cLCBPath))	
		{
			AfxMessageBox("Could not save the sound project");
			return;
		}
	}
		
}


void CMixerPage::OnPauseResButton() 
{
	if(m_bPauseProcess)
	{
		m_bPauseProcess = FALSE;

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

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

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

void CMixerPage::OnResetReslistButton() 
{
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	pSuperVisApp->m_SuperVisThread->SuperVisSheet->PostMessage(WM_RESETLIST, NULL, NULL);

}
	
void CMixerPage::ResetReslistButton()
{ 	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	long i;
	ResourceElement* temp;

	CSingleLock sLock( pSuperVisApp->SuperVisMutex );
	//Wait for Mutex Object
	sLock.Lock( INFINITE );
	
	//Free the Resources array
	for (i= 0;  i < pSuperVisApp->m_apResources.GetSize() ;i++)
	{
		temp = (ResourceElement *)(pSuperVisApp->m_apResources.GetAt(i));
		delete temp;
	}

	pSuperVisApp->m_apResources.RemoveAll();


	//Free the First occurence Resources array
	int iuij= pSuperVisApp->m_apFirstOccurResources.GetSize();
	for (i= 0;  i < pSuperVisApp->m_apFirstOccurResources.GetSize() ;i++)
	{
		temp = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(i));
		delete temp;
	}

	pSuperVisApp->m_apFirstOccurResources.RemoveAll();


	//Empty the CListCtrl
	m_ResList.DeleteAllItems();	


	//Release
	sLock.Unlock();
	
}

void CMixerPage::RefreshResList()
{

	long i;

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


	//Clear the list
	m_ResList.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_apFirstOccurResources.GetSize(); i ++ )
		{
			FilterAndDisplayFirstOccurRes(i);

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

		}	
	}






}

void CMixerPage::FilterAndDisplayRes(long lIndex)
{

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

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

	res = (ResourceElement *)(pSuperVisApp->m_apResources.GetAt(lIndex));


	ulBankId = ((res->ulId >> 16) & 0x0000FFFF);
	ulResId = res->ulId & 0x0000FFFF;


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

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


	if (m_bShowResWithId)
	{
		if (m_lResId != (long)ulResId)
			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",
		res->time.wHour,
		res->time.wMinute,
		res->time.wSecond,
		res->time.wMilliseconds);
	m_ResList.InsertItem( 0, buffer );


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


	//resource Name
	m_ResList.SetItem(0, 1, LVIF_TEXT, (LPCSTR)res->cResName, 0, 0, 0, NULL );

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

	//Bank Name
	m_ResList.SetItem(0, 3, LVIF_TEXT, (LPCSTR)res->cResBankName, 0, 0, 0, NULL );
	
	//Bank ID 
	sprintf(buffer, "%lu", ulBankId);
	m_ResList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	
	//Type
	switch(res->tdeType){

	case TYPE_INVALID:
		sprintf(buffer, "TYPE_INVALID");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SAMPLE:
		sprintf(buffer, "TYPE_SAMPLE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_MIDI:
		sprintf(buffer, "TYPE_MIDI");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_CDAUDIO:
		sprintf(buffer, "TYPE_CDAUDIO");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SEQUENCE:
		sprintf(buffer, "TYPE_SEQUENCE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SWITCH_OLD:
		sprintf(buffer, "TYPE_SWITCH_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SPLIT:
		sprintf(buffer, "TYPE_SPLIT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD:
		sprintf(buffer, "TYPE_THEME_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	case TYPE_SWITCH:
		sprintf(buffer, "TYPE_SWITCH");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD2:
		sprintf(buffer, "TYPE_THEME_OLD2");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_RANDOM:
		sprintf(buffer, "TYPE_RANDOM");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME:
		sprintf(buffer, "TYPE_THEME");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_FOR_ALIGNEMENT:
		sprintf(buffer, "TYPE_FOR_ALIGNEMENT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;

	}


	//Volume 
	sprintf(buffer, "%u", res->ucVolume);
	m_ResList.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//perso related to res
	m_ResList.SetItem(0, 7, LVIF_TEXT, (LPCSTR)res->cInfo, 0, 0, 0, NULL );


}




void CMixerPage::FilterAndDisplayFirstOccurRes(long lIndex)
{

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

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

	res = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(lIndex));


	ulBankId = ((res->ulId >> 16) & 0x0000FFFF);
	ulResId = res->ulId & 0x0000FFFF;


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

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


	if (m_bShowResWithId)
	{
		if (m_lResId != (long)ulResId)
			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",
		res->time.wHour,
		res->time.wMinute,
		res->time.wSecond,
		res->time.wMilliseconds);
	m_ResList.InsertItem( 0, buffer );


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


	//resource Name
	m_ResList.SetItem(0, 1, LVIF_TEXT, (LPCSTR)res->cResName, 0, 0, 0, NULL );

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

	//Bank Name
	m_ResList.SetItem(0, 3, LVIF_TEXT, (LPCSTR)res->cResBankName, 0, 0, 0, NULL );
	
	//Bank ID 
	sprintf(buffer, "%lu", ulBankId);
	m_ResList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	
	//Type
	switch(res->tdeType){

	case TYPE_INVALID:
		sprintf(buffer, "TYPE_INVALID");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SAMPLE:
		sprintf(buffer, "TYPE_SAMPLE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_MIDI:
		sprintf(buffer, "TYPE_MIDI");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_CDAUDIO:
		sprintf(buffer, "TYPE_CDAUDIO");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SEQUENCE:
		sprintf(buffer, "TYPE_SEQUENCE");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SWITCH_OLD:
		sprintf(buffer, "TYPE_SWITCH_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_SPLIT:
		sprintf(buffer, "TYPE_SPLIT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD:
		sprintf(buffer, "TYPE_THEME_OLD");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	case TYPE_SWITCH:
		sprintf(buffer, "TYPE_SWITCH");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME_OLD2:
		sprintf(buffer, "TYPE_THEME_OLD2");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_RANDOM:
		sprintf(buffer, "TYPE_RANDOM");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_THEME:
		sprintf(buffer, "TYPE_THEME");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;
	case TYPE_FOR_ALIGNEMENT:
		sprintf(buffer, "TYPE_FOR_ALIGNEMENT");
		m_ResList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );
		break;

	}


	//Volume 
	sprintf(buffer, "%u", res->ucVolume);
	m_ResList.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//perso related to res
	m_ResList.SetItem(0, 7, LVIF_TEXT, (LPCSTR)res->cInfo, 0, 0, 0, NULL );


}







void CMixerPage::OnCheckDebugOnRes() 
{
	//Update the variable
	UpdateData(TRUE);
	
}

void CMixerPage::OnCheckFirstResOccur() 
{
	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	
}

void CMixerPage::OnCheckRes() 
{

	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	

}

void CMixerPage::OnCheckResBank() 
{
	//Update the variable
	UpdateData(TRUE);
	RefreshResList();
	
}

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

	}

	RefreshResList();

	
}

void CMixerPage::OnChangeEditResId() 
{
	//Keep the Event Id in the 0 - 1024 range
	UpdateData(TRUE);
	if (m_lResId > 1024)
	{
		m_lResId = 1024;
		UpdateData(FALSE);

	}

	RefreshResList();
	
}


void CMixerPage::SetEditBoxVolume(unsigned long _ulIndex)
{

	LPDISPATCH lpDispatch;

	unsigned long ulBankId;
	unsigned long ulResId;
			
	ResourceElement* temp;	

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

	//We do all this only if we have a valid path, hence a correct inti, to work with
	if(pSuperVisApp->m_SuperVisThread->bAutomationActivated)
	{

		//Enable the + and - buttons.
		((CEdit *)(GetDlgItem(IDC_VOL_MOINS)))->EnableWindow( TRUE );
		((CEdit *)(GetDlgItem(IDC_VOL_PLUS)))->EnableWindow( TRUE );
		((CEdit *)(GetDlgItem(IDC_VOLUME_SLIDER)))->EnableWindow( TRUE );
		((CEdit *)(GetDlgItem(IDC_SAVE)))->EnableWindow( TRUE );
		((CEdit *)(GetDlgItem(IDC_PLAY)))->EnableWindow( TRUE );
		

		//Save the Array Index to be used by the OnVolMoins() and OnVolPlus() messages
		m_ulSelectedResourceArrayIndex = _ulIndex;	
		
		//Select resource in the approriate array
		if(pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.m_bFirstOccurence)
		{

			temp = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(_ulIndex));
		}
		else
		{
			temp = (ResourceElement *)(pSuperVisApp->m_apResources.GetAt(_ulIndex));
		}

		
		
		//Save the Resource Element to be used later even if the List has been emptied
		m_SelectedResElement.time = temp->time;
		m_SelectedResElement.ulId = temp->ulId;
		m_SelectedResElement.tdeType = temp->tdeType;
		m_SelectedResElement.ucVolume = temp->ucVolume;
		m_SelectedResElement.cResBankName = temp->cResBankName;
		m_SelectedResElement.cResName = temp->cResName;
		m_SelectedResElement.res = temp->res;
		m_SelectedResElement.cInfo = temp->cInfo;
		m_SelectedResElement.evt = temp->evt;
				
		ulBankId = ((temp->ulId >> 16) & 0x0000FFFF);
		ulResId = temp->ulId & 0x0000FFFF;

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

		}
		END_TRY
		if(lpDispatch != NULL)
		{
			Group.AttachDispatch(lpDispatch);

		}
		
		

		//Get the resources list
		IResources Resources;
		Resources.AttachDispatch(Group.GetResources());

		//Get the Resource
		//Use full EdiD 0x0000|0000
		TRY
		{
			lpDispatch = Resources.FindItemByID(temp->ulId);

		}
		END_TRY
		if(lpDispatch != NULL)
		{
			pSuperVisApp->m_SuperVisThread->m_Resource.AttachDispatch(lpDispatch);
			m_cSelectedResName = temp->cResName;
			m_ulSelectedResVol = pSuperVisApp->m_SuperVisThread->m_Resource.GetVolume();
			m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);

		}


		UpdateData(FALSE);

	}

}

void CMixerPage::OnVolMoins() 
{
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	m_ulSelectedResVol--;
	m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);
	
	//Set the volume in SounEditor using OLE
	pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);

	//Edit the Sound in realtime....			
	dbgSND_fn_bSetResourceStaticVolume(m_SelectedResElement.ulId,(unsigned char)m_ulSelectedResVol);

	UpdateData(FALSE);
	
}

void CMixerPage::OnVolPlus() 
{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	m_ulSelectedResVol++;
	m_VolumeSlider.SetPos(127 - m_ulSelectedResVol);
	
	//Set the volume in SounEditor using OLE
	pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);

	
	//Edit the Sound in realtime....			
	dbgSND_fn_bSetResourceStaticVolume(m_SelectedResElement.ulId,(unsigned char)m_ulSelectedResVol);

	UpdateData(FALSE);
	
}


void CMixerPage::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (nSBCode == SB_THUMBTRACK || SB_LINEDOWN || SB_LINEUP){

		if (pScrollBar == (CScrollBar *)&m_VolumeSlider){


			ResourceElement* temp;
			CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
			
			m_ulSelectedResVol = 127 - m_VolumeSlider.GetPos();

			//Set the volume in SounEditor using OLE
			pSuperVisApp->m_SuperVisThread->m_Resource.SetVolume((int)m_ulSelectedResVol);

	
			//Edit the Sound in realtime....			
			if(pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.m_bFirstOccurence)
			{

				temp = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(m_ulSelectedResourceArrayIndex));

			}
			else
			{
				temp = (ResourceElement *)(pSuperVisApp->m_apResources.GetAt(m_ulSelectedResourceArrayIndex));

			}

			dbgSND_fn_bSetResourceStaticVolume(temp->ulId,(unsigned char)m_ulSelectedResVol);

			UpdateData(FALSE);
	
		}


	}
	
	CPropertyPage::OnVScroll(nSBCode, nPos, pScrollBar);
}



long CMixerPage::AddToFirstResOccurArray(ResourceElement* res)
{

	long i, lSize;
	ResourceElement * resFirstOccurTest;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	lSize = pSuperVisApp->m_apFirstOccurResources.GetSize();
	 
	 //look in the Events array from 0 to the current event we are filtering.
	for(i = 0; i < lSize ; i++)
	{
		resFirstOccurTest = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(i));
		if (res->ulId == resFirstOccurTest->ulId)
			return -1;

	}


	//Create new array element
	ResourceElement *newResElement = new ResourceElement;

	newResElement->time = res->time;
	newResElement->ulId = res->ulId;
	newResElement->tdeType = res->tdeType;
	newResElement->ucVolume = res->ucVolume;
	newResElement->cResBankName = res->cResBankName;
	newResElement->cResName = res->cResName;
	newResElement->res = res->res;
	newResElement->cInfo = res->cInfo;
	newResElement->evt = res->evt;

	
	return pSuperVisApp->m_apFirstOccurResources.Add(newResElement);

}

void CMixerPage::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	int ColWidth = m_ResList.GetColumnWidth(0);
	pSuperVisApp->m_MixerPageRegKeys.iColTime= ColWidth;
	ColWidth = m_ResList.GetColumnWidth(1);
	pSuperVisApp->m_MixerPageRegKeys.iColResName= ColWidth;
	ColWidth = m_ResList.GetColumnWidth(2);
	pSuperVisApp->m_MixerPageRegKeys.iColResId= ColWidth;
	ColWidth = m_ResList.GetColumnWidth(3);
	pSuperVisApp->m_MixerPageRegKeys.iColBankName= ColWidth;
	ColWidth = m_ResList.GetColumnWidth(4);
	pSuperVisApp->m_MixerPageRegKeys.iColBankId= ColWidth;
	ColWidth = m_ResList.GetColumnWidth(5);
	pSuperVisApp->m_MixerPageRegKeys.iColType = ColWidth;
	ColWidth = m_ResList.GetColumnWidth(6);
	pSuperVisApp->m_MixerPageRegKeys.iColVol= ColWidth;	
	ColWidth = m_ResList.GetColumnWidth(7);
	pSuperVisApp->m_MixerPageRegKeys.iColInfo= ColWidth;	


	UpdateData(TRUE);

	pSuperVisApp->m_MixerPageRegKeys.iCheckBankId = m_lBankId;
	pSuperVisApp->m_MixerPageRegKeys.iCheckResId = m_lResId;
	pSuperVisApp->m_MixerPageRegKeys.bCheckFirstOccur = m_bFirstOccurence;


}




void CMixerPage::SetResName(long lIndex)
{
	LPDISPATCH lpDispatch;

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

	//We leave if the size is 0. "Temporary patch to correct synchomisation problem with the ResetList button"
	if(pSuperVisApp->m_apResources.GetSize()==0)
		return;
	
	
	ResourceElement *res = (ResourceElement *)(pSuperVisApp->m_apResources.GetAt(lIndex));

	unsigned long ulBankId = (0xFFFF0000 & res->ulId) >> 16;

	//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);
		res->cResBankName = Group.GetName();
	}
	else
		return;

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


	//Get the Event
	IResource TheResource;
	TRY
	{
		//Use full EdiD 0x0000|0000
		lpDispatch = Resources.FindItemByID(res->ulId);

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

	if (lpDispatch != NULL)
	{
		TheResource.AttachDispatch(lpDispatch);
		res->cResName = TheResource.GetName();
	}


}


void CMixerPage::SetFirstOccurResName(long lIndex)
{

	LPDISPATCH lpDispatch;

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

	ResourceElement *res = (ResourceElement *)(pSuperVisApp->m_apFirstOccurResources.GetAt(lIndex));

	unsigned long ulBankId = (0xFFFF0000 & res->ulId) >> 16;

	//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);
		res->cResBankName = Group.GetName();
	}
	else
		return;

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


	//Get the Event
	IResource TheResource;
	TRY
	{
		//Use full EdiD 0x0000|0000
		lpDispatch = Resources.FindItemByID(res->ulId);

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

	if(lpDispatch != NULL)
	{
		TheResource.AttachDispatch(lpDispatch);
		res->cResName = TheResource.GetName();
	}
	else
		return;

}

BOOL CMixerPage::OnSetActive() 
{
	RefreshResList();
	
	return CPropertyPage::OnSetActive();
}






UINT PlaySoundProc( LPVOID pParam )
{


	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	dbgSND_fn_vSendRequestSound(pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.m_SelectedResElement.evt);

	return 0;

}








void CMixerPage::OnPlay() 
{

	//Start playing sound in a separate Thread so we dont have a  deadlock.
	AfxBeginThread( PlaySoundProc, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL );


	
	
	


}
