// VoicesPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "VoicesPage.h"

//ACP Virtual Voice
#include "sndvirt.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVoicesPage property page

IMPLEMENT_DYNCREATE(CVoicesPage, CPropertyPage)

CVoicesPage::CVoicesPage() : CPropertyPage(CVoicesPage::IDD)
{
	//{{AFX_DATA_INIT(CVoicesPage)
	m_iMaxVoices = 0;
	m_iUsedVoices = 0;
	//}}AFX_DATA_INIT
}

CVoicesPage::~CVoicesPage()
{
}

void CVoicesPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVoicesPage)
	DDX_Control(pDX, IDC_VOICES_COMBO, m_VoicesCombo);
	DDX_Control(pDX, IDC_VOICES_LIST, m_VoicesListCtrl);
	DDX_Text(pDX, IDC_MAX_VOICES, m_iMaxVoices);
	DDX_Text(pDX, IDC_USED_VOICES, m_iUsedVoices);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CVoicesPage, CPropertyPage)
	//{{AFX_MSG_MAP(CVoicesPage)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVoicesPage message handlers

BOOL CVoicesPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	//Initialise the ClistCtrl containing virtual voices 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_RegKeyVoicePage.iColVoiceId;							// width of the column, in pixels
    lvc->pszText = "Voices Id";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(0, lvc);


	lvc->pszText = "Paused?";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColPaused;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(1, lvc);


	lvc->pszText = "Res Ptr";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColResPtr;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(2, lvc);


	lvc->pszText = "Res Id";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColResId;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(3, lvc);


	lvc->pszText = "Loop";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColLoop;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(4, lvc);


	lvc->pszText = "Norm";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColNorm;							// width of the column, in pixels
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(5, lvc);


	lvc->pszText = "Instance";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColInstance;							// width of the column, in pixels
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(6, lvc);


	lvc->pszText = "Audible Voice";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColAudibleV;							// width of the column, in pixels
    lvc->iSubItem = 7;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(7, lvc);

	lvc->pszText = "Hard Voice";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyVoicePage.iColHardV;							// width of the column, in pixels
    lvc->iSubItem = 8;						// index of subitem associated with column
	m_VoicesListCtrl.InsertColumn(8, lvc);

	free( lvc );

	//init module ComboBox so GetCurSel() wont return a CB_ERR
	m_VoicesCombo.SetCurSel(0);


	UpdateData(FALSE);	

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

void CVoicesPage::RefreshVoiceList()
{

	int i;

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


	//Get the module currently selected...
	int iSelectedModule = m_VoicesCombo.GetCurSel();
	
	//Clear the list
	m_VoicesListCtrl.DeleteAllItems();

	//Clear the comboBox
	m_VoicesCombo.ResetContent();

	
	//If there is nothing in the array we leave
	int iNbModule = pSuperVisApp->m_apVirtualVoiceModule.GetSize();
	if (iNbModule == 0)
		return;


	VirtualVoiceModule  *pModule;
	
	//Add module desciption in the ComboBox
	for (i = 0; i < iNbModule; i++ )
	{
		pModule  = (VirtualVoiceModule *)pSuperVisApp->m_apVirtualVoiceModule.GetAt(i);
		m_VoicesCombo.AddString(pModule->cDescription);
	}


	//Set to the module currently 
	//No item is selected
	if (iSelectedModule == CB_ERR)
		iSelectedModule = 0;
	
	//Set the combobox to the selected module
	m_VoicesCombo.SetCurSel(iSelectedModule);
	
	pModule  = (VirtualVoiceModule *)pSuperVisApp->m_apVirtualVoiceModule.GetAt(iSelectedModule);


	//Clean the virtual voices array
	int iArraySize = pModule->apVirtualVoicesArray.GetSize();
	VirtualVoice *pVoice;
	for (i = 0; i < iArraySize; i ++ )
	{
		//Retrieve the current Voice 
		pVoice = (VirtualVoice *)(pModule->apVirtualVoicesArray.GetAt(i));

		//Is the voice still active
		if(!pVoice->bStillActive){

			//Erase
			pModule->apVirtualVoicesArray.RemoveAt(i);
			delete pVoice;
			i --;
			iArraySize--;
		}
	}	
	
	//Cycle through the Current Virtual voices module.
	iArraySize = pModule->apVirtualVoicesArray.GetSize();
	for (i = 0; i < iArraySize; i ++ )
	{
		//We have to pass the index to the current module 
		AddVoiceToList(i, iSelectedModule);

	}	


	//We display here the number of voices
	SND_tdhVirtualModule pVirtualModule = (SND_tdhVirtualModule)pModule->lIdModule;
//	m_iUsedVoices = SNDLST2_M_DynamicGetNumberOfElements(&pVirtualModule->virtual_voices.pList);
	m_iUsedVoices = pModule->apVirtualVoicesArray.GetSize();
	m_iMaxVoices = pVirtualModule->g_lNbSxdAudible;

	UpdateData(FALSE);



}

void CVoicesPage::AddVoiceToList(long lVoiceIndex, long lModuleIndex)
{

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

	//Retrieve the current module
	VirtualVoiceModule  *pModule;
	pModule  = (VirtualVoiceModule *)pSuperVisApp->m_apVirtualVoiceModule.GetAt(lModuleIndex);


	//Retrieve the current Voice 
	VirtualVoice *pVoice;
	pVoice = (VirtualVoice *)(pModule->apVirtualVoicesArray.GetAt(lVoiceIndex));


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

	char buffer[128];



	//Display item, Voice Id
	sprintf(buffer,"0x%08x", pVoice->lId);
	m_VoicesListCtrl.InsertItem( 0, buffer );


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

	//Paused?
	if(pVoice->bPaused)
		sprintf(buffer, "Yes");
	else
		sprintf(buffer, "No");
	m_VoicesListCtrl.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	

	//Resource pointer
	sprintf(buffer,"0x%08x", pVoice->lResRef);
	m_VoicesListCtrl.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Resource ID
	sprintf(buffer,"0x%08x", pVoice->lResId);
	m_VoicesListCtrl.SetItem(0, 3, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//loop?
	if(pVoice->bLoop)
		sprintf(buffer, "Yes");
	else
		sprintf(buffer, "No");
	m_VoicesListCtrl.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	//Norm
	sprintf(buffer, "%6.3f", pVoice->dNorme);
	m_VoicesListCtrl.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Instance
	sprintf(buffer, "%d", pVoice->lInstance);
	m_VoicesListCtrl.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	if (pVoice->lAudible)
	{
		//Audible voice
		sprintf(buffer, "0x%08x", pVoice->lAudible);
		m_VoicesListCtrl.SetItem(0, 7, LVIF_TEXT, buffer, 0, 0, 0, NULL );


		//Hard voice
		sprintf(buffer, "0x%08x", pVoice->lVoice);
		m_VoicesListCtrl.SetItem(0, 8, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	}
	else
	{
		//Audible voice
		sprintf(buffer, "");
		m_VoicesListCtrl.SetItem(0, 7, LVIF_TEXT, buffer, 0, 0, 0, NULL );


		//Hard voice
		sprintf(buffer, "");
		m_VoicesListCtrl.SetItem(0, 8, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	}

	//Refresh data from the control
//	PostMessage(WM_USER_UPDATE_DATA_FALSE, 0, 0);

}

BOOL CVoicesPage::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 CVoicesPage::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	int ColWidth = m_VoicesListCtrl.GetColumnWidth(0);
	pSuperVisApp->m_RegKeyVoicePage.iColVoiceId = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(1);
	pSuperVisApp->m_RegKeyVoicePage.iColPaused = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(2);
	pSuperVisApp->m_RegKeyVoicePage.iColResPtr = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(3);
	pSuperVisApp->m_RegKeyVoicePage.iColResId = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(4);
	pSuperVisApp->m_RegKeyVoicePage.iColLoop= ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(5);
	pSuperVisApp->m_RegKeyVoicePage.iColNorm = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(6);
	pSuperVisApp->m_RegKeyVoicePage.iColInstance = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(7);
	pSuperVisApp->m_RegKeyVoicePage.iColAudibleV = ColWidth;
	ColWidth = m_VoicesListCtrl.GetColumnWidth(8);
	pSuperVisApp->m_RegKeyVoicePage.iColHardV = ColWidth;	
}
