// SuperVisSheet.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "SuperVisSheet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSuperVisSheet

IMPLEMENT_DYNAMIC(CSuperVisSheet, CPropertySheet)

CSuperVisSheet::CSuperVisSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
	AddControlPages();
}

CSuperVisSheet::CSuperVisSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
	AddControlPages();
}


CSuperVisSheet::CSuperVisSheet()
	:CPropertySheet("SuperVisater99 By Pascal Lalancette")
{
	AddControlPages();
}

CSuperVisSheet::~CSuperVisSheet()
{
}


BEGIN_MESSAGE_MAP(CSuperVisSheet, CPropertySheet)
	//{{AFX_MSG_MAP(CSuperVisSheet)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_COMMAND(IDC_QUIT, OnQuit)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSuperVisSheet message handlers



void CSuperVisSheet::AddControlPages()
{

	AddPage(&m_VoicesPage);
	AddPage(&m_ThemesPage);
	AddPage(&m_SuperVisSettingPage);
//	AddPage(&m_RessourcesHistoricPage);
	AddPage(&m_MixerPage);
	AddPage(&m_MicroAndObjectsPage);
	AddPage(&m_EventsHistoryPage);

}

BOOL CSuperVisSheet::OnInitDialog() 
{

	BOOL bResult = CPropertySheet::OnInitDialog();

	 ((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_bIsPSheettActive = TRUE;

	//
	//Little trick to add a button in a modeless CPropertySheet. A modeless
	//propertysheet doesnt have ok, cancel and apply buttons.
	//
	RECT rc;
	GetWindowRect (&rc);
	// Increase the height of the CPropertySheet by 30
	rc.bottom += 35;          
	// Resize the CPropertySheet

	MoveWindow (rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);

	// Convert to client coordinates
	ScreenToClient (&rc);
	// m_Button is of type CButton and is a member of CMySheet
	m_Button.Create ("&Quit", WS_CHILD | WS_VISIBLE | WS_TABSTOP,
		CRect (rc.right - 85, rc.bottom-35, rc.right - 10, rc.bottom-10),
	this, IDC_QUIT);

	m_Button.SetFont( GetFont() );

	
	return bResult;

  
	
}


// Handler for button click of IDC_QUIT
void CSuperVisSheet::OnQuit ()
{ 
    
	//Kill the propertysheet
	DestroyWindow();
}

void CSuperVisSheet::PostNcDestroy() 
{

	//Set the flag so we dont close the window a second time when the engine quit
	((CSupervisApp*)AfxGetApp())->m_SuperVisThread->m_bIsPSheettActive = FALSE;

	//Set the flag for the engine know we are inactive
	((CSupervisApp*)AfxGetApp())->m_bSuperVisaterActive = FALSE;
	
	//We erase everything
	delete this;
	
	CPropertySheet::PostNcDestroy();
}

BOOL CSuperVisSheet::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	// If pHandlerInfo is NULL, then handle the message
	if (pHandlerInfo == NULL)
	{
		if (nID == ID_WATCH)
		{
			if (nCode == CN_COMMAND)
			{
				
				m_EventsHistoryPage.SendEventInWatchList();
			

			}
			
			
			return TRUE;
		}
		
	}	
	
	return CPropertySheet::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

void CSuperVisSheet::SendDestroyMsg()
{

	PostMessage(WM_USER_DESTROY_WINDOW, 0, 0);

}

BOOL CSuperVisSheet::PreTranslateMessage(MSG* pMsg) 
{
	//We destroy the supervisheet here
	//We cannot pass the CWnd across thread and this cause problem while using updatedata
	if (pMsg->message == WM_USER_DESTROY_WINDOW){
		DestroyWindow();
	}

	return CPropertySheet::PreTranslateMessage(pMsg);
}
