// QBitView.cpp : implementation of the CQBitView class
//

#include "stdafx.h"
#include "QBit.h"

#include "QBitDoc.h"
#include "QBitView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CQBitView

IMPLEMENT_DYNCREATE(CQBitView, CFormView)

BEGIN_MESSAGE_MAP(CQBitView, CFormView)
	//{{AFX_MSG_MAP(CQBitView)
	ON_BN_CLICKED(IDC_FACTORISE_RADIO, OnFactoriseRadio)
	ON_BN_CLICKED(IDC_CIRCUIT_RADIO, OnCircuitRadio)
	ON_BN_CLICKED(IDC_AMPLITUDE_RADIO, OnAmplitudeRadio)
	ON_BN_CLICKED(IDC_PROBALITY_RADIO, OnProbalityRadio)
	ON_BN_CLICKED(IDC_BAR_GRAPH_CHECK, OnBarGraphCheck)
	ON_BN_CLICKED(IDC_CIRCUIT_BROWSE_BUTTON, OnCircuitBrowseButton)
	ON_BN_CLICKED(IDC_FILE_OUT_BROWSE, OnFileOutBrowse)
	ON_BN_CLICKED(IDC_VALIDATE_BUTTON, OnValidateButton)
	ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
	ON_BN_CLICKED(IDC_STOP_BUTTON, OnStopButton)
	ON_BN_CLICKED(IDC_RESET_BUTTON, OnResetButton)
	ON_BN_CLICKED(IDC_FILE_OUT_CHECK, OnFileOutCheck)
	ON_BN_CLICKED(IDC_CIRCUIT_INPUT_VALUE_RADIO, OnCircuitInputValueRadio)
	ON_BN_CLICKED(IDC_CIRCUIT_INPUT_SELECT_RADIO, OnCircuitInputSelectRadio)
	ON_BN_CLICKED(IDC_CIRCUIT_INPUT_ALL_RADIO, OnCircuitInputAllRadio)
	ON_BN_CLICKED(IDC_VIEW_OUTPUT_BUTTON, OnViewOutputButton)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQBitView construction/destruction

CQBitView::CQBitView()
	: CFormView(CQBitView::IDD)
{
	//{{AFX_DATA_INIT(CQBitView)
	m_num_to_fac = 15;
	m_circuit_file_name = _T("full_adder.txt");
	m_out_file_name = _T("out.txt");
	m_circuit_input_select = _T("");
	m_circuit_input_value = 0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	results = new RESULTS;

	results->activity_string = "";
	results->progress = 0;
}

CQBitView::~CQBitView()
{
	delete results; 
}

void CQBitView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQBitView)
	DDX_Text(pDX, IDC_FACTORISE_EDIT, m_num_to_fac);
	DDX_Text(pDX, IDC_CIRCUIT_EDIT, m_circuit_file_name);
	DDX_Text(pDX, IDC_FILE_OUT_EDIT, m_out_file_name);
	DDX_Text(pDX, IDC_CIRCUIT_INPUT_SELECT_EDIT, m_circuit_input_select);
	DDX_Text(pDX, IDC_CIRCUIT_INPUT_VALUE_EDIT, m_circuit_input_value);
	//}}AFX_DATA_MAP
}

BOOL CQBitView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CQBitView printing

BOOL CQBitView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CQBitView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CQBitView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CQBitView::OnPrint(CDC* pDC, CPrintInfo*)
{
	// TODO: add code to print the controls
}

/////////////////////////////////////////////////////////////////////////////
// CQBitView diagnostics

#ifdef _DEBUG
void CQBitView::AssertValid() const
{
	CFormView::AssertValid();
}

void CQBitView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CQBitDoc* CQBitView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQBitDoc)));
	return (CQBitDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CQBitView message handlers

// ******* initial application settings *******
void CQBitView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
	// setting initial application states
	
	// factorise mode (default automatic)
	CButton *facr = (CButton*) GetDlgItem(IDC_FACTORISE_RADIO);
	facr->SetCheck(BST_CHECKED);
	GetDlgItem(IDC_FACTORISE_EDIT)->EnableWindow(TRUE);
	CButton *autor = (CButton*) GetDlgItem(IDC_FACTORISE_ITERATION_AUTO_RADIO);
	autor->SetCheck(BST_CHECKED);
	
	// deactivate circuit mode controls (default value)
	GetDlgItem(IDC_CIRCUIT_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_BROWSE_BUTTON)->EnableWindow(FALSE);
	CButton *cvalr = (CButton*) GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO);
	cvalr->SetCheck(BST_CHECKED);
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_RADIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_ALL_RADIO)->EnableWindow(FALSE);
	

	// amplitudes
	CButton *ampr = (CButton*) GetDlgItem(IDC_AMPLITUDE_RADIO);
	ampr->SetCheck(BST_CHECKED);

	GetDlgItem(IDC_FILE_OUT_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_FILE_OUT_BROWSE)->EnableWindow(FALSE);

	// start and stop buttons
	GetDlgItem(IDC_START_BUTTON)->EnableWindow(TRUE);
	GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(FALSE);

}

// ******* main switch buttons *******
void CQBitView::OnFactoriseRadio() 
{
	// TODO: Add your control notification handler code here

	// activate factorise mode controls
	GetDlgItem(IDC_FACTORISE_EDIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_FACTORISE_ITERATION_AUTO_RADIO)->EnableWindow(TRUE);
	GetDlgItem(IDC_FACTORISE_ITERATION_PER_RADIO)->EnableWindow(TRUE);
	
	// deactivate circuit mode controls
	GetDlgItem(IDC_CIRCUIT_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_BROWSE_BUTTON)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_RADIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_CIRCUIT_INPUT_ALL_RADIO)->EnableWindow(FALSE);
}

void CQBitView::OnCircuitRadio() 
{
	// TODO: Add your control notification handler code here
	
	// activate circuit mode controls
	GetDlgItem(IDC_CIRCUIT_EDIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_CIRCUIT_BROWSE_BUTTON)->EnableWindow(TRUE);
	CButton *cinval = (CButton*) GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO);
	CButton *cselval = (CButton*) GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_RADIO);
	if(cinval->GetCheck() == BST_CHECKED)
		GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(TRUE);
	else if(cselval->GetCheck() == BST_CHECKED)
		GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO)->EnableWindow(TRUE);
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_RADIO)->EnableWindow(TRUE);
	GetDlgItem(IDC_CIRCUIT_INPUT_ALL_RADIO)->EnableWindow(TRUE);
	
	// deactivate factorise mode controls
	GetDlgItem(IDC_FACTORISE_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_FACTORISE_ITERATION_AUTO_RADIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_FACTORISE_ITERATION_PER_RADIO)->EnableWindow(FALSE);
}

// ******* circuit browse and input radios *******
void CQBitView::OnCircuitBrowseButton() 
{
	// TODO: Add your control notification handler code here	

	CFileDialog circuitFileDialog(TRUE, "txt", NULL, 0, "txt file (*.txt)|*.txt|All files (*.*)|*.*||", this);
	circuitFileDialog.m_ofn.lpstrTitle = "Browse";
	circuitFileDialog.DoModal();
	m_circuit_file_name = circuitFileDialog.m_ofn.lpstrFile;
	UpdateData(FALSE);

}

void CQBitView::OnCircuitInputValueRadio() 
{
	// TODO: Add your control notification handler code here

	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(FALSE);	
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(TRUE);
}

void CQBitView::OnCircuitInputSelectRadio() 
{
	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(FALSE);	
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(TRUE);
}

void CQBitView::OnCircuitInputAllRadio() 
{
	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_EDIT)->EnableWindow(FALSE);	
	GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_EDIT)->EnableWindow(FALSE);
}

// ******* output radios *******
void CQBitView::OnAmplitudeRadio() 
{
	// TODO: Add your control notification handler code here
	TRACE(_T("OnAmp called\n"));	
}

void CQBitView::OnProbalityRadio() 
{
	// TODO: Add your control notification handler code here
	TRACE(_T("OnProb called\n"));
}

void CQBitView::OnBarGraphCheck() 
{
	// TODO: Add your control notification handler code here

	// get check box pointer
	CButton *bgc = (CButton*) GetDlgItem(IDC_BAR_GRAPH_CHECK);

	// not implimented warning if check box enabled
	if(bgc->GetCheck() == BST_CHECKED)
		MessageBox(_T("Not yet implimented!"), _T ("QBit warning"), MB_ICONEXCLAMATION | MB_OK);	

	bgc->SetCheck(BST_UNCHECKED);
}

void CQBitView::OnFileOutCheck() 
{
	// TODO: Add your control notification handler code here
	
	// get check box pointer
	CButton *foc = (CButton*) GetDlgItem(IDC_FILE_OUT_CHECK);

	// warning if check box enabled
	if(foc->GetCheck() == BST_CHECKED)
		MessageBox(_T("Eats lots of disk space!"), _T ("QBit warning"), MB_ICONEXCLAMATION | MB_OK);	
	
	// check for check box enabled, if not disable
	// other file output controls
	
	if(foc->GetCheck() != BST_CHECKED)
	{
		GetDlgItem(IDC_FILE_OUT_EDIT)->EnableWindow(FALSE);
		GetDlgItem(IDC_FILE_OUT_BROWSE)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_FILE_OUT_EDIT)->EnableWindow(TRUE);
		GetDlgItem(IDC_FILE_OUT_BROWSE)->EnableWindow(TRUE);
	}
}

void CQBitView::OnFileOutBrowse() 
{
	// TODO: Add your control notification handler code here

	CFileDialog circuitFileDialog(TRUE, "txt", NULL, 0, "txt file (*.txt)|*.txt|All files (*.*)|*.*||", this);
	circuitFileDialog.m_ofn.lpstrTitle = "Browse";
	circuitFileDialog.DoModal();
	m_out_file_name = circuitFileDialog.m_ofn.lpstrFile;
	UpdateData(FALSE);
}

void CQBitView::OnViewOutputButton() 
{
	// TODO: Add your control notification handler code here
	
	STARTUPINFO si;
	::ZeroMemory(&si, sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	PROCESS_INFORMATION pi;

	CString string = "Notepad ";
	string += m_out_file_name;

	char* newstring = new char[string.GetLength()+1];
	strcpy(newstring, (LPCSTR) string);
	if(::CreateProcess(NULL, newstring, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
		NULL, NULL, &si, &pi))
	{
		::CloseHandle(pi.hThread);
		::CloseHandle(pi.hProcess);
	}
}

void CQBitView::LaunchApp()
{
	STARTUPINFO si;
	::ZeroMemory(&si, sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	PROCESS_INFORMATION pi;

	if(::CreateProcess(NULL, _T("mod1"), NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
		NULL, NULL, &si, &pi))
	{
		::CloseHandle(pi.hThread);
		::CloseHandle(pi.hProcess);
	}

	TRACE("After app");
}

// the next function is not currently used - all data updateing is done in OnStart
void CQBitView::OnValidateButton() 
{
// TODO: Add your control notification handler code here

	UpdateData(TRUE);

	TRACE(_T ("Num to fac: %d"), m_num_to_fac);
}

// ******* main thread controls ******* 
void CQBitView::OnStartButton() 
{
	// TODO: Add your control notification handler code here

	// update all data, this ensures that the current member variables are
	// those requested by the user
	UpdateData(TRUE);

	// change button states
	GetDlgItem(IDC_START_BUTTON)->EnableWindow(FALSE);
	GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(TRUE);

	// set up thread parameter structure
	THREADPARAMS* ptp = new THREADPARAMS;
	ptp->num_to_fac = m_num_to_fac;
	ptp->circuit_input_value = m_circuit_input_value;
	ptp->circuit_file_name = m_circuit_file_name;
	ptp->circuit_input_select = m_circuit_input_select;
	ptp->out_file_name = m_out_file_name;
	ptp->pWnd = AfxGetMainWnd();
	ptp->pView = (CQBitView*)this;
	ptp->res = results;

	CButton *facr = (CButton*) GetDlgItem(IDC_FACTORISE_RADIO);

	// major state if/else
	if(facr->GetCheck())
	{
		CString message = "Starting factorisation...";
		// get message box object
		CListBox *activity_list_ptr = (CListBox*) GetDlgItem(IDC_ACTIVITY_LIST);
		// add current results acivity string to window
		activity_list_ptr->AddString(message);
		int count = activity_list_ptr->GetCount();
		activity_list_ptr->SetTopIndex(count-1);		
		
		CWinThread* facThread = AfxBeginThread(FactorisationThreadFunc,
			ptp, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);

		facThread->ResumeThread();

	}
	else
	{
		CString message = "Starting circuit simulation...";
		// get message box object
		CListBox *activity_list_ptr = (CListBox*) GetDlgItem(IDC_ACTIVITY_LIST);
		// add current results acivity string to window
		activity_list_ptr->AddString(message);
		int count = activity_list_ptr->GetCount();
		activity_list_ptr->SetTopIndex(count-1);
		
		CWinThread* circThread = AfxBeginThread(CircuitThreadFunc,
		ptp, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);

		circThread->ResumeThread();
	}

}

void CQBitView::OnStopButton() 
{
	// TODO: Add your control notification handler code here

	// change button states
	GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(FALSE);
	GetDlgItem(IDC_START_BUTTON)->EnableWindow(TRUE);

	// need to add code here to terminate the worker thread
	// not possible - the MFC doesn't support thread termination
	// from antother thread
	
	// get list box ptr
	CListBox *sb = (CListBox*) GetDlgItem(IDC_ACTIVITY_LIST);
	// reset list box
	sb->ResetContent();

	// get progress ctrl object
	CProgressCtrl *progress_ctrl_ptr = (CProgressCtrl*) GetDlgItem(IDC_SIM_PROGRESS);

	progress_ctrl_ptr->SetPos(0);
}

// ******* activity box *******
void CQBitView::OnResetButton() 
{
	// TODO: Add your control notification handler code here

	// get list box ptr
	CListBox *sb = (CListBox*) GetDlgItem(IDC_ACTIVITY_LIST);
	// reset list box
	sb->ResetContent();
		
}

// mainfrm window messaging functions called from mainfrm
void CQBitView::ProgressUpdate()
{
	// get progress ctrl object
	CProgressCtrl *progress_ctrl_ptr = (CProgressCtrl*) GetDlgItem(IDC_SIM_PROGRESS);
	// reset it!
	progress_ctrl_ptr->SetPos(results->progress);
}

void CQBitView::BoxUpdate()
{
	// get message box object
	CListBox *activity_list_ptr = (CListBox*) GetDlgItem(IDC_ACTIVITY_LIST);

	// add current results acivity string to window
	activity_list_ptr->AddString(results->activity_string);
	int count = activity_list_ptr->GetCount();
	activity_list_ptr->SetTopIndex(count-1);
}

void CQBitView::FileError()
{
//	MessageBox(results->file_error_string);
	MessageBox(results->file_error_string, _T ("QBit warning"), MB_ICONEXCLAMATION | MB_OK);	
}
	
// the factorisation worker thread function
UINT FactorisationThreadFunc(LPVOID pParam)
{
	bool failed = false;

	// seed random number
	srand(time(0));

	// cast thread parameters
	THREADPARAMS* ptp = (THREADPARAMS*) pParam;

	// iteration step mode
	CButton* itper = (CButton*) ptp->pView->GetDlgItem(IDC_FACTORISE_ITERATION_PER_RADIO);

	// open output file if requested
	CButton* foc = (CButton*) ptp->pView->GetDlgItem(IDC_FILE_OUT_CHECK);
		
	if(foc->GetCheck())
	{
		ptp->fout.open(ptp->out_file_name);
		if(ptp->fout.fail()) 
		{
			ptp->res->file_error_string = "Could not open output file!";
			ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			failed = true;
		}
	}
		
	if(!failed)
	{
		if(itper->GetCheck())
			CQAlgos::shor(ptp->num_to_fac, ptp, foc->GetCheck());
		else
			while(!CQAlgos::shor(ptp->num_to_fac, ptp, foc->GetCheck()));
	}

	// ready for the restart

	CProgressCtrl *progress_ctrl_ptr = (CProgressCtrl*) ptp->pView->GetDlgItem(IDC_SIM_PROGRESS);
	progress_ctrl_ptr->SetPos(0);

	CProgressCtrl *progress_ctrl2_ptr = (CProgressCtrl*) ptp->pView->GetDlgItem(IDC_ACTIVITY_PROGRESS);
	progress_ctrl2_ptr->SetPos(0);

	ptp->pView->GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(FALSE);
	ptp->pView->GetDlgItem(IDC_START_BUTTON)->EnableWindow(TRUE);

	return 0;
}

// the circuit worker thread function
UINT CircuitThreadFunc(LPVOID pParam)
{
	bool failed = false;

	// seed random number
	srand(time(0));
	
	// error check codes
	int code = 0;
	int code2 = 0;

	// cast thread parameters
	THREADPARAMS* ptp = (THREADPARAMS*) pParam;

	// get circuit input select radio ptrs
	CButton* valp = (CButton*) ptp->pView->GetDlgItem(IDC_CIRCUIT_INPUT_VALUE_RADIO);
	CButton* selp = (CButton*) ptp->pView->GetDlgItem(IDC_CIRCUIT_INPUT_SELECT_RADIO);
	CButton* allp = (CButton*) ptp->pView->GetDlgItem(IDC_CIRCUIT_INPUT_ALL_RADIO);

	// open input file
	ptp->fin.open(ptp->circuit_file_name, ios::nocreate);
	if(ptp->fin.fail()) 
	{
		ptp->res->file_error_string = "Input file doesn't exist!";
		ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
		failed = true;
	}

	// open output file if requested
	CButton * foc = (CButton*) ptp->pView->GetDlgItem(IDC_FILE_OUT_CHECK);
		
	if(foc->GetCheck() && !failed)
	{
		ptp->fout.open(ptp->out_file_name);
		if(ptp->fout.fail()) 
		{
			ptp->res->file_error_string = "Could not open output file!";
			ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			failed = true;
		}
	}

	CCircuit* circ = new CCircuit();

	if(!failed)
	{
	
		if(valp->GetCheck())
			code = circ->loadcircuit(0, ptp);	
		else if(selp->GetCheck())
			code = circ->loadcircuit(1, ptp);
		else
			code = circ->loadcircuit(2, ptp);

		if(code == 0)
		{
			if(valp->GetCheck())
			{
				code2 = circ->solvecircuit(0, ptp->circuit_input_value, ptp, foc->GetCheck());
			}
			else if(selp->GetCheck())
			{
				code2 = circ->solvecircuit(1, ptp->circuit_input_value, ptp, foc->GetCheck());
			}
			else if(allp->GetCheck())
			{
				code2 = circ->solvecircuit(2, ptp->circuit_input_value, ptp, foc->GetCheck());
			}
		}
		else 
		{
			code2 = 2;

			if(code == 1)
			{	
				ptp->res->file_error_string = "Input file format error!";
				ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			}
			else if(code == 2)
			{
				ptp->res->file_error_string = "Invalid selection bits!";
				ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			}
			else if(code == 3)
			{
				ptp->res->file_error_string = "Invalid number of operations!";
				ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			}

		failed = true;

		}

		if(code2 == 0)
		{
			ptp->res->activity_string = "***** Circuit simulation successful *****";
			ptp->pWnd->SendMessage(WM_USER_THREAD_UPDATE_BOX);	
		}
		else if(code2 == 1)
		{
			ptp->res->file_error_string = "Invalid input value!";
			ptp->pWnd->SendMessage(WM_USER_THREAD_FILE_ERROR);
			failed = true;
		}
	}

	delete circ;

	// ready for next run

	CProgressCtrl *progress_ctrl_ptr = (CProgressCtrl*) ptp->pView->GetDlgItem(IDC_SIM_PROGRESS);
	progress_ctrl_ptr->SetPos(0);

	CProgressCtrl *progress_ctrl2_ptr = (CProgressCtrl*) ptp->pView->GetDlgItem(IDC_ACTIVITY_PROGRESS);
	progress_ctrl2_ptr->SetPos(0);

	ptp->pView->GetDlgItem(IDC_STOP_BUTTON)->EnableWindow(FALSE);
	ptp->pView->GetDlgItem(IDC_START_BUTTON)->EnableWindow(TRUE);

	return 0;
}

