// WavVoicesPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "WavVoicesPage.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWavVoicesPage property page

IMPLEMENT_DYNCREATE(CWavVoicesPage, CPropertyPage)

CWavVoicesPage::CWavVoicesPage() : CPropertyPage(CWavVoicesPage::IDD)
{
	//{{AFX_DATA_INIT(CWavVoicesPage)
	m_lProcessTime = 0;
	//}}AFX_DATA_INIT
}

CWavVoicesPage::~CWavVoicesPage()
{
}

void CWavVoicesPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWavVoicesPage)
	DDX_Control(pDX, IDC_WAV_VOICES_LIST, m_WavVoicesListCtrl);
	DDX_Text(pDX, IDC_PROCESS_TIME, m_lProcessTime);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CWavVoicesPage message handlers

BOOL CWavVoicesPage::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_RegKeyWavVoicePage.iColWavVoiceId;							// width of the column, in pixels
    lvc->pszText = "Id";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(0, lvc);


	lvc->pszText = "Resource Pointer";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColResPtr;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(1, lvc);

	lvc->pszText = "Resource Id";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColResId;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(2, lvc);
	
	lvc->pszText = "Paused?";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColPaused;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(3, lvc);
	
	lvc->pszText = "Volume";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColVol;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(4, lvc);

	
	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColPan;							// width of the column, in pixels
	lvc->pszText = "Pan";					// column heading 
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(5, lvc);

	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColSpace;							// width of the column, in pixels
	lvc->pszText = "Space";					// column heading 
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(5, lvc);


	lvc->cx = pSuperVisApp->m_RegKeyWavVoicePage.iColFreq;							// width of the column, in pixels
	lvc->pszText = "Frequency";					// column heading 
    lvc->iSubItem = 7;						// index of subitem associated with column
	m_WavVoicesListCtrl.InsertColumn(5, lvc);


	free( lvc );

	//Init the process time control
	m_lProcessTime = 0;

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

void CWavVoicesPage::RefreshVoicesList()
{

	long i;

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


	//Clear the list
	m_WavVoicesListCtrl.DeleteAllItems();
	
	
	//Cycle through the pointer Array to filter
	for (i = 0; i < pSuperVisApp->m_apWavVoices.GetSize(); i ++ )
	{
		AddWavVoices(i);

	}	

	//Add processTime
	m_lProcessTime = pSuperVisApp->m_lProcessTime;
	UpdateData(FALSE);

}


void CWavVoicesPage::AddWavVoices(long lIndex)
{

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

	pVoice = (WavVoiceArrayElement *)(pSuperVisApp->m_apWavVoices.GetAt(lIndex));

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

	char buffer[128];

	//Display item
	sprintf(buffer,"0x%x", pVoice->lId);
	m_WavVoicesListCtrl.InsertItem( 0, buffer );


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


	//Resource pointer
	sprintf(buffer,"0x%x", pVoice->res_pstPtr);
	m_WavVoicesListCtrl.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Resource ID
	sprintf(buffer,"0x%x", pVoice->res_Id);
	m_WavVoicesListCtrl.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Paused?
	if(pVoice->bPaused)
		sprintf(buffer, "YES");
	else
		sprintf(buffer, "NO");
	m_WavVoicesListCtrl.SetItem(0, 3, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	//Volume
	sprintf(buffer, "%d", pVoice->ucVolume);
	m_WavVoicesListCtrl.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Pan
	sprintf(buffer, "%d", pVoice->ucPan);
	m_WavVoicesListCtrl.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Space
	sprintf(buffer, "%d", pVoice->ucSpace);
	m_WavVoicesListCtrl.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//Frequency
	sprintf(buffer, "%6.3f", pVoice->dFreq);
	m_WavVoicesListCtrl.SetItem(0, 7, LVIF_TEXT, buffer, 0, 0, 0, NULL );


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

}

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

	int ColWidth = m_WavVoicesListCtrl.GetColumnWidth(0);
	pSuperVisApp->m_RegKeyWavVoicePage.iColWavVoiceId = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(1);
	pSuperVisApp->m_RegKeyWavVoicePage.iColResPtr = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(2);
	pSuperVisApp->m_RegKeyWavVoicePage.iColResId = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(3);
	pSuperVisApp->m_RegKeyWavVoicePage.iColPaused = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(4);
	pSuperVisApp->m_RegKeyWavVoicePage.iColVol = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(5);
	pSuperVisApp->m_RegKeyWavVoicePage.iColPan = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(6);
	pSuperVisApp->m_RegKeyWavVoicePage.iColSpace = ColWidth;
	ColWidth = m_WavVoicesListCtrl.GetColumnWidth(7);
	pSuperVisApp->m_RegKeyWavVoicePage.iColFreq = ColWidth;	


}
