// ThemesPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "ThemesPage.h"


//CPA
#include "SNDinc.h"
#include "snddll.h"

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

/////////////////////////////////////////////////////////////////////////////
// CThemesPage property page

IMPLEMENT_DYNCREATE(CThemesPage, CPropertyPage)

CThemesPage::CThemesPage() : CPropertyPage(CThemesPage::IDD)
{
	//{{AFX_DATA_INIT(CThemesPage)
	m_cAskedTheme = _T("");
	m_cCurrentBridge = _T("");
	m_cCurrentPart = _T("");
	m_cCurrentTheme = _T("");
	m_cState = _T("");
	m_cAskedThemeBank = _T("");
	m_cAskedThemeId = _T("");
	m_cAskedThemePointer = _T("");
	m_cAskedThemeResId = _T("");
	m_cCurrentThemeBank = _T("");
	m_cCurrentThemeId = _T("");
	m_cCurrentThemeResId = _T("");
	//}}AFX_DATA_INIT
}

CThemesPage::~CThemesPage()
{
}

void CThemesPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CThemesPage)
	DDX_Control(pDX, IDC_VOLUME_LINE_LIST, m_VolListCtrl);
	DDX_Text(pDX, IDC_CURRENT_BRIDGE, m_cCurrentBridge);
	DDX_Text(pDX, IDC_CURRENT_PART, m_cCurrentPart);
	DDX_Text(pDX, IDC_CURRENT_THEME, m_cCurrentTheme);
	DDX_Text(pDX, IDC_STATE, m_cState);
	DDX_Text(pDX, IDC_ASKED_THEME_BANK, m_cAskedThemeBank);
	DDX_Text(pDX, IDC_ASKED_THEME_ID, m_cAskedThemeId);
	DDX_Text(pDX, IDC_ASKED_THEME_POINTER, m_cAskedThemePointer);
	DDX_Text(pDX, IDC_ASKED_THEME_RES_ID, m_cAskedThemeResId);
	DDX_Text(pDX, IDC_CURRENT_THEME_BANK_ID, m_cCurrentThemeBank);
	DDX_Text(pDX, IDC_CURRENT_THEME_ID, m_cCurrentThemeId);
	DDX_Text(pDX, IDC_CURRENT_THEME_RES_ID, m_cCurrentThemeResId);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CThemesPage message handlers

BOOL CThemesPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Initialise the ClistCtrl containing volume line 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_RegKeyThemePage.iColVolId;							// width of the column, in pixels
    lvc->pszText = "Volume Line Id";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_VolListCtrl.InsertColumn(0, lvc);


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

	free( lvc );


	//Init Theme edit box with nohing
	m_cAskedTheme = "None";
	m_cCurrentBridge = "0";
	m_cCurrentPart = "None";
	m_cCurrentTheme = "None";
	m_cState = "No State";

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

void CThemesPage::RefreshVolList()
{

	long i;

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


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

	}	

}

void CThemesPage::AddVolumeToList(long lIndex)
{

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

	pVol = (VolumeArrayElement *)(pSuperVisApp->m_apVolumeLine.GetAt(lIndex));

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

	char buffer[128];

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


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


	//Volume line volume....doh!
	sprintf(buffer, "%d", pVol->lVol);
	m_VolListCtrl.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );


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

}

void CThemesPage::RefreshTheme()
{
	
	tduRefRes CurrentTheme, AskedTheme;
	long lIdPart,lIdLoop;
	char buffer[256];
	SndBool bTransiting;

	unsigned long ulBankId, ulEventId;
	
	
	if (dbgSND_fn_bGetThemePosition(&CurrentTheme, &lIdPart, &lIdLoop))
	{
		sprintf(buffer,"0x%x",CurrentTheme.pstPtr);
		m_cCurrentTheme = buffer;
		
		sprintf(buffer,"0x%x",CurrentTheme.pstPtr->Id);
		m_cCurrentThemeId = buffer;

		ulBankId = (0xFFFF0000 & CurrentTheme.pstPtr->Id) >> 16;
		sprintf(buffer,"%d",ulBankId);
		m_cCurrentThemeBank = buffer;

		ulEventId= (0x0000FFFF & CurrentTheme.pstPtr->Id);
		sprintf(buffer,"%d",ulEventId);
		m_cCurrentThemeResId = buffer;

		sprintf(buffer,"%x",lIdPart);
		m_cCurrentPart = buffer;

		sprintf(buffer," %d",lIdLoop);
		m_cCurrentBridge = buffer;


		if (dbgSND_fn_bGetAskedTheme(&AskedTheme, &bTransiting))
		{
			
			sprintf(buffer,"0x%x",AskedTheme.pstPtr);
			m_cAskedTheme = buffer;

			sprintf(buffer,"0x%x",AskedTheme.pstPtr->Id);
			m_cAskedThemeId = buffer;


			ulBankId = (0xFFFF0000 & AskedTheme.pstPtr->Id) >> 16;
			sprintf(buffer,"%d",ulBankId);
			m_cAskedThemeBank = buffer;

			ulEventId= (0x0000FFFF & AskedTheme.pstPtr->Id);
			sprintf(buffer,"%d",ulEventId);
			m_cAskedThemeResId = buffer;
			
			if (bTransiting) 
				sprintf(buffer,"Transiting...");
			else
				sprintf(buffer,"Waiting Before Transition...");
			m_cState = buffer;
		}

	}
	else
	{

		sprintf(buffer,"None");

		m_cCurrentPart = buffer;
		m_cCurrentTheme = buffer;
		m_cAskedTheme = buffer;
		
	
		sprintf(buffer,"");
		m_cCurrentThemeId = buffer;
		m_cCurrentThemeBank = buffer;
		m_cCurrentThemeResId = buffer;
		m_cCurrentBridge = buffer;
		m_cAskedThemeId = buffer;
		m_cAskedThemeBank = buffer;
		m_cAskedThemeResId = buffer;
		m_cState = buffer;

	}

	UpdateData(FALSE);




}

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

	int ColWidth = m_VolListCtrl.GetColumnWidth(0);
	pSuperVisApp->m_RegKeyThemePage.iColVolId= ColWidth;
	ColWidth = m_VolListCtrl.GetColumnWidth(1);
	pSuperVisApp->m_RegKeyThemePage.iColVol= ColWidth;	
}
