//---------------------------------
// Name:			ProgressDialog.cpp
// Purpose:			Progress dialog
// Toolchain(s):	Microsoft Visual C++
// Creation date:	05-Jun-06
// Created by:		JJo
//---------------------------------

//---------------------------------
// Includes

#include "ProgressDialog.h"

//---------------------------------
// Message map

BEGIN_MESSAGE_MAP(ProgressDialog, CDialog)
	ON_BN_CLICKED(IDC_CANCEL, OnCancelClicked)
	ON_MESSAGE(MSG_FINISHED, OnFinished)
END_MESSAGE_MAP()

//---------------------------------
// Class

/**
 * Name:		ProgressDialog
 * Purpose:		Constructor for the ProgressDialog class
 */
ProgressDialog::ProgressDialog(CWnd * pParent, CWinThread * pThread):
CDialog(IDD_PROGRESS, pParent),
m_pThread(pThread),
m_closeThread(false) {}

/**
 * Name:		~ProgressDialog
 * Purpose:		Destructor for the ProgressDialog class
 */
ProgressDialog::~ProgressDialog() {}

/**
 * Name:		SetTitle
 * Purpose:		Sets the title for the progress window
 * Input(s):
 *  - Title for the progress window
 */
void ProgressDialog::SetTitle(const char * title) {

	this->SetWindowText(title);
}

/**
 * Name:		SetRange
 * Purpose:		Sets the range for the progress bar
 * Input(s):
 *  - Start value
 *  - End value
 */
void ProgressDialog::SetRange(int start, int end) {

	this->m_pProgress = (CProgressCtrl *) this->GetDlgItem(IDC_PROGRESSBAR);
	this->m_pProgress->SetRange(start, end);
	this->m_pProgress->SetPos(start);
}

/**
 * Name:		SetStep
 * Purpose:		Sets the step increment for the progress bar
 * Input(s):
 *  - Step increment
 */
void ProgressDialog::SetStep(int step) {

	this->m_pProgress = (CProgressCtrl *) this->GetDlgItem(IDC_PROGRESSBAR);
	this->m_pProgress->SetStep(step);
}

/**
 * Name:		StepIt
 * Purpose:		Increments the progress bar
 */
void ProgressDialog::StepIt() {

	this->m_pProgress = (CProgressCtrl *) this->GetDlgItem(IDC_PROGRESSBAR);
	this->m_pProgress->StepIt();
}

/**
 * Name:		Finish
 * Purpose:		Terminates the window
 */
void ProgressDialog::Finish() {

	this->PostMessage(MSG_FINISHED);
}

/**
 * Name:		OnFinished
 * Purpose:		MSG_FINISHED message handler
 */
void ProgressDialog::OnFinished() {

	this->EndDialog(0);
	MessageBeep(MB_OK);
}

/**
 * Name:		OnCancelClicked
 * Purpose:		Handles clicks on the cancel button
 */
void ProgressDialog::OnCancelClicked() {

	MessageBeep(MB_OK);
	this->m_closeThread = TRUE;
	WaitForSingleObject(this->m_pThread->m_hThread, INFINITE);
	this->EndDialog(IDCANCEL);
}

/**
 * Name:		ThreadShouldClose
 * Purpose:		Returns true if the worker thread should be closed
 */
bool ProgressDialog::ThreadShouldClose() const {

	return this->m_closeThread;
}

/**
 * Name:		DoModal
 * Purpose:		Displays the progress bar in a modal way
 */
int ProgressDialog::DoModal() {

	this->m_pThread->ResumeThread();
	return CDialog::DoModal();
}
