// SuperVisSettingsPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "SuperVisSettingsPage.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSuperVisSettingsPage property page

IMPLEMENT_DYNCREATE(CSuperVisSettingsPage, CPropertyPage)

CSuperVisSettingsPage::CSuperVisSettingsPage() : CPropertyPage(CSuperVisSettingsPage::IDD)
{
	//{{AFX_DATA_INIT(CSuperVisSettingsPage)
	m_uiRefreshRate = 0;
	m_bSetWindowOnTop = FALSE;
	m_BinaryLCBPath = _T("");
	//}}AFX_DATA_INIT
}

CSuperVisSettingsPage::~CSuperVisSettingsPage()
{
}

void CSuperVisSettingsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSuperVisSettingsPage)
	DDX_Text(pDX, IDC_REFRESH_RATE, m_uiRefreshRate);
	DDV_MinMaxUInt(pDX, m_uiRefreshRate, 250, 10000);
	DDX_Check(pDX, IDC_WINDOW_ON_TOP, m_bSetWindowOnTop);
	DDX_Text(pDX, IDC_EDIT_LCB_PATH, m_BinaryLCBPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSuperVisSettingsPage, CPropertyPage)
	//{{AFX_MSG_MAP(CSuperVisSettingsPage)
	ON_BN_CLICKED(IDC_PLUS, OnPlus)
	ON_BN_CLICKED(IDC_MOINS, OnMoins)
	ON_EN_SETFOCUS(IDC_REFRESH_RATE, OnSetfocusRefreshRate)
	ON_BN_CLICKED(IDC_WINDOW_ON_TOP, OnWindowOnTop)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BROWSE_FOR_LCB, OnBrowseForLcb)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSuperVisSettingsPage message handlers

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

	//Refresh rate default
	m_uiRefreshRate = (unsigned int)pSuperVisApp->m_iRefreshRate;

	//Always on top check box
	m_bSetWindowOnTop =  pSuperVisApp->m_bWindowOnTop;

	//Set the selected
	((CEdit *)(GetDlgItem(IDC_REFRESH_RATE)))->LimitText(5);

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


	m_BinaryLCBPath = pSuperVisApp->m_cBinaryLCBPath;
		

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



void CSuperVisSettingsPage::OnPlus() 
{
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
		
	m_uiRefreshRate += 250;
	if(m_uiRefreshRate > 10000)
		m_uiRefreshRate = 10000;
	pSuperVisApp->m_SuperVisThread->SuperVisSheet->SetTimer( WM_USER_TIMER, m_uiRefreshRate, NULL );
	UpdateData(FALSE);
	
}

void CSuperVisSettingsPage::OnMoins() 
{
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	m_uiRefreshRate -= 250;
	if(m_uiRefreshRate <= 250)
		m_uiRefreshRate = 250;
	pSuperVisApp->m_SuperVisThread->SuperVisSheet->SetTimer( WM_USER_TIMER, m_uiRefreshRate, NULL );
	UpdateData(FALSE);
	
}


void CSuperVisSettingsPage::OnSetfocusRefreshRate() 
{
	//Set the selected
	((CEdit *)(GetDlgItem(IDC_REFRESH_RATE)))->SetSel( 0, -1, FALSE );
	
}

void CSuperVisSettingsPage::OnWindowOnTop() 
{
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Update the variable
	UpdateData(TRUE);
	
	if(m_bSetWindowOnTop)
	{
		pSuperVisApp->m_SuperVisThread->SuperVisSheet->SetWindowPos(&wndTopMost ,0 ,0, 0, 0, SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOMOVE);
	}
	else
	{
		pSuperVisApp->m_SuperVisThread->SuperVisSheet->SetWindowPos(&wndNoTopMost  ,0 ,0, 0, 0, SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOMOVE);
	}

	
	
	
	pSuperVisApp->m_bWindowOnTop = m_bSetWindowOnTop;

}

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

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	pSuperVisApp->m_iRefreshRate = (int)m_uiRefreshRate;
}


void CSuperVisSettingsPage::OnBrowseForLcb() 
{

	CString strFileName;

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

	//Leave if we already had intiated SoundEditor Automation 
	if(!pSuperVisApp->m_SuperVisThread->bAutomationActivated)
	{
		CFileDialog dlg( TRUE,"lcb","*.lcb",
						 OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR,
						 _T("SoundEditor|LCB |"));



		int nResponse = dlg.DoModal();
		
		if (nResponse == IDOK)
		{
			
			//We have to extract the path, GetpathName include the filename
			m_BinaryLCBPath = dlg.GetPathName();
			UpdateData(FALSE);

			//Update for later registry saving
			pSuperVisApp->m_cBinaryLCBPath = m_BinaryLCBPath;


			pSuperVisApp->m_SuperVisThread->Automation(m_BinaryLCBPath);


		}
		else if (nResponse == IDCANCEL)
		{
			return;
		
		}

	}

	return;	
	
}
