
// FileOperationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileOperation.h"
#include "FileOperationDlg.h"
#include "afxdialogex.h"
#include "cgen_api.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFileOperationDlg dialog



CFileOperationDlg::CFileOperationDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_FILEOPERATION_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileOperationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICENAME,	m_edit_device_name);
	DDX_Control(pDX, IDC_EDIT_RETURNNUM,	m_edit_returnnum);
	DDX_Control(pDX, IDC_EDIT_STATUS,		m_edit_applicationstatus);
	DDX_Control(pDX, IDC_MFCEDITBROWSE_SETTINGFILE,			m_edit_settingfile);
	DDX_Control(pDX, IDC_MFCEDITBROWSE_ARBITRATYDATAFILE,	m_edit_arbitrarydatafile);
	DDX_Control(pDX, IDC_COMBO_OPENMODE,	m_combo_openmode);
	DDX_Control(pDX, IDC_COMBO_AOCHANNEL,	m_combo_aochannel);
	DDX_Control(pDX, IDC_COMBO_WAVETYPE,	m_combo_wavetype);
	DDX_Control(pDX, IDC_COMBO_PARAM,		m_combo_param);
}

BEGIN_MESSAGE_MAP(CFileOperationDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GENOPENAPPLICATION,		&CFileOperationDlg::OnBnClickedButtonGenopenapplication)
	ON_BN_CLICKED(IDC_BUTTON_GENSETDEVICENAME,			&CFileOperationDlg::OnBnClickedButtonGensetdevicename)
	ON_BN_CLICKED(IDC_BUTTON_GENEXPORTSETTINGFILE,		&CFileOperationDlg::OnBnClickedButtonGenexportsettingfile)
	ON_BN_CLICKED(IDC_BUTTON_GENIMPORTSETTINGFILE,		&CFileOperationDlg::OnBnClickedButtonGenimportsettingfile)
	ON_BN_CLICKED(IDC_BUTTON_GENSETWAVETYPE,			&CFileOperationDlg::OnBnClickedButtonGensetwavetype)
	ON_BN_CLICKED(IDC_BUTTON_GENSETARBITRARYDATAFILE,	&CFileOperationDlg::OnBnClickedButtonGensetarbitrarydatafile)
	ON_BN_CLICKED(IDC_BUTTON_GENGETARBITRARYDATAFILE,	&CFileOperationDlg::OnBnClickedButtonGengetarbitrarydatafile)
	ON_BN_CLICKED(IDC_BUTTON_GENSAVEDATAFILE,			&CFileOperationDlg::OnBnClickedButtonGensavedatafile)
	ON_BN_CLICKED(IDC_BUTTON_GENSTARTOPERATION,			&CFileOperationDlg::OnBnClickedButtonGenstartoperation)
	ON_BN_CLICKED(IDC_BUTTON_GENGETAPPLICATIONSTATUS,	&CFileOperationDlg::OnBnClickedButtonGengetapplicationstatus)
	ON_BN_CLICKED(IDC_BUTTON_GENSTOPOPERATION,			&CFileOperationDlg::OnBnClickedButtonGenstopopereation)
	ON_BN_CLICKED(IDC_BUTTON_GENCLOSE,					&CFileOperationDlg::OnBnClickedButtonGenclose)
	ON_BN_CLICKED(IDC_BUTTON_EXIT,						&CFileOperationDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


//----------------------------------------
// Macro definition
//----------------------------------------
#define CWAVEFORMGENERATOR_CHANNELS			16		// Max channels
//----------------------------------------
// Global variable declaration
//----------------------------------------
short	g_appid;									// Application accessID


// CFileOperationDlg message handlers
//================================================================================
// Initial settings
//================================================================================
BOOL CFileOperationDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CString		text_temp;			// For temporary storage of text

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	//----------------------------------------
	// Update the initial display of device name
	//----------------------------------------
	m_edit_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Create the combobox for screen size
	//----------------------------------------
	m_combo_openmode.AddString(_T("Minimize"));
	m_combo_openmode.AddString(_T("Normal"));
	m_combo_openmode.AddString(_T("Maximize"));
	//----------------------------------------
	// Change the selection of screen size combobox to 0
	//----------------------------------------
	m_combo_openmode.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for channel
	//----------------------------------------
	for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++) {
		text_temp.Format(_T("%d"), i);
		m_combo_aochannel.AddString(text_temp);
	}
	//----------------------------------------
	// Change the selection of channel combobox to 0
	//----------------------------------------
	m_combo_aochannel.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for wave type
	//----------------------------------------
	m_combo_wavetype.AddString(_T("Sinewave"));
	m_combo_wavetype.AddString(_T("Constant"));
	m_combo_wavetype.AddString(_T("Triangular"));
	m_combo_wavetype.AddString(_T("Ramp"));
	m_combo_wavetype.AddString(_T("Square"));
	m_combo_wavetype.AddString(_T("Noise"));
	m_combo_wavetype.AddString(_T("Arbitrary"));
	//----------------------------------------
	// Change the selection of wave type combobox to 0
	//----------------------------------------
	m_combo_wavetype.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for data save
	//----------------------------------------
	m_combo_param.AddString(_T("Single channel"));
	m_combo_param.AddString(_T("Multiple channels"));
	//----------------------------------------
	// Change the selection of data save combobox to 0
	//----------------------------------------
	m_combo_param.SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFileOperationDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFileOperationDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the GenOpenApplication button is clicked
// Initialize the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenopenapplication()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			device_name[256]	= {};	// Device name
	char			error_string[256]	= {};	// Error code string
	unsigned short	openmode;					// Screen size
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowText(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Get screen size from combobox
	//----------------------------------------
	openmode = m_combo_openmode.GetCurSel();
	//----------------------------------------
	// Perform application initialization process
	//----------------------------------------
	ret = GenOpenApplication(device_name, &g_appid, openmode);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenOpenApplication = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenSetDeviceName button is clicked
// Set the device name
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGensetdevicename()
{
	TCHAR	text_temp[256]		= {};	// For temporary storage of text
	char	device_name[256]	= {};	// Device name
	char	error_string[256]	= {};	// Error code string
	long	ret;						// For return code
	CString	text_string;				// For error code display

	//----------------------------------------
	// Get setting file name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowText(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Set device name
	//----------------------------------------
	ret = GenSetDeviceName(g_appid, device_name);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);	
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenSetDeviceName = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenExportSettingFile button is clicked
// Save the settings to ini file
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenexportsettingfile()
{
	TCHAR	text_temp[256]		= {};	// For temporary storage of text
	char	filepath[256]		= {};	// File path
	char	error_string[256]	= {};	// Error code string
	long	ret;						// For return code
	CString	text_string;				// For error code display

	//----------------------------------------
	// Get setting file path from text box
	//----------------------------------------
	m_edit_settingfile.GetWindowText(text_temp, _countof(text_temp));
	WideCharToMultiByte(CP_ACP, 0, &text_temp[0], -1, &filepath[0], 256, NULL, NULL);
	//----------------------------------------
	// Save the settings to ini file
	//----------------------------------------
	ret = GenExportSettingFile(g_appid, filepath);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenExportSettingFile = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenImportSettingFile button is clicked
// Load the settings from ini file.
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenimportsettingfile()
{
	TCHAR	text_temp[256]		= {};	// For temporary storage of text
	char	filepath[256]		= {};	// File path
	char	error_string[256]	= {};	// Error code string
	long	ret;						// For return code
	CString	text_string;				// For error code display

	//----------------------------------------
	// Get setting file path from text box
	//----------------------------------------
	m_edit_settingfile.GetWindowText(text_temp, _countof(text_temp));
	WideCharToMultiByte(CP_ACP, 0, &text_temp[0], -1, &filepath[0], 256, NULL, NULL);
	//----------------------------------------
	// Load the settings from ini file.
	//----------------------------------------
	ret = GenImportSettingFile(g_appid, filepath);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenImportSettingFile = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenSetWaveType button is clicked
// Set the waveform type
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGensetwavetype()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	unsigned short	wavetype;					// Waveform type
	unsigned short	aochannel;					// Channel
	char			error_string[256]	= {};	// Error code string
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Get the wave type from combobox
	//----------------------------------------
	wavetype	= m_combo_wavetype.GetCurSel();
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Set the wave type
	//----------------------------------------
	ret = GenSetWaveType(g_appid, aochannel, wavetype);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenSetWaveType = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenSetArbitraryDataFile button is clicked
// Set the data file (csv file) path
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGensetarbitrarydatafile()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			filepath[256]		= {};	// File path
	char			error_string[256]	= {};	// Error code string
	unsigned short	aochannel;					// Channel
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Get data file path from text box
	//----------------------------------------
	m_edit_arbitrarydatafile.GetWindowText(text_temp, _countof(text_temp));
	WideCharToMultiByte(CP_ACP, 0, &text_temp[0], -1, &filepath[0], 256, NULL, NULL);
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Set the data file path
	//----------------------------------------
	ret = GenSetArbitraryDataFile(g_appid, aochannel, filepath);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenSetArbitraryDataFile = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenGetArbitraryDataFile button is clicked
// Get the setting data file (csv file) path
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGengetarbitrarydatafile()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			filepath[256]		= {};	// File path
	char			error_string[256]	= {};	// Error code string
	unsigned short	aochannel;					// Channel
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Get the data file path
	//----------------------------------------
	ret = GenGetArbitraryDataFile(g_appid, aochannel, filepath);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenGetArbitraryDataFile = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the data file path
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), filepath);
	text_string.Format(_T("%s"), text_temp);
	m_edit_arbitrarydatafile.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenSaveDataFile button is clicked
// Save the specified channel data to a csv file
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGensavedatafile()
{
	TCHAR		text_temp[256]		= {};	// For temporary storage of text
	char		filepath[256]		= {};	// File path
	char		error_string[256]	= {};	// Error code string
	short		aochannel;					// Channel
	short		param;						// Single or Multiple
	long		ret;						// For return code
	CString		text_string;				// For error code display

	//----------------------------------------
	// Get the data file name from text box
	//----------------------------------------
	m_edit_arbitrarydatafile.GetWindowText(text_temp, _countof(text_temp));
	WideCharToMultiByte(CP_ACP, 0, &text_temp[0], -1, &filepath[0], 256, NULL, NULL);
	//----------------------------------------
	// Get the channel parameters (single or multiple) from combo box
	//----------------------------------------
	param		= m_combo_param.GetCurSel();
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Data file settings
	//----------------------------------------
	//-------------------
	// For the single channel, set the fourth argument to the channel
	//-------------------
	if (param == 0) {
		ret = GenSaveDataFile(g_appid, filepath, param, aochannel);
	}
	//-------------------
	// For the multiple channels, set the fourth argument to 0
	//-------------------
	else {
		ret = GenSaveDataFile(g_appid, filepath, param, 0);
	}
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenSaveDataFile = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenStartOperation button is clicked
// Start the output of the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenstartoperation()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			error_string[256]	= {};	// Error code string
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Start the output of the application
	//----------------------------------------
	ret = GenStartOperation(g_appid);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenStartOperation = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenGetApplicationStatus button is clicked
// Get the operating status
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGengetapplicationstatus()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			error_string[256]	= {};	// Error code string
	unsigned long	status				= 0;	// Status
	long			ret;						// For return code
	CString			text_string;				// For error code display

	//----------------------------------------
	// Get the operating status
	//----------------------------------------
	ret = GenGetApplicationStatus(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenGetApplicationStatus = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the status
	//----------------------------------------
	if (status == 0) {
		text_string.Format(_T("00H(Stopped)"));
	}
	else if (status == 1) {
		text_string.Format(_T("01H(Running)"));
	}
	else if (status == 2) {
		text_string.Format(_T("02H(Waiting for start trigger)"));
	}
	else if (status == 3) {
		text_string.Format(_T("03H(Running, Waiting for start trigger)"));
	}
	else {
		text_string.Format(_T("%d "), status);
	}
	m_edit_applicationstatus.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenStopOperation button is clicked
// Stop the output of the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenstopopereation()
{
	TCHAR	text_temp[256]		= {};	// For temporary storage of text
	char	error_string[256]	= {};	// Error code string
	long	ret;						// For return code
	CString	text_string;				// For error code display

	//----------------------------------------
	// Stop the output of the application
	//----------------------------------------
	ret = GenStopOperation(g_appid);	
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenStopOperation = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenCloseApplication button is clicked
// Close the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGenclose()
{
	TCHAR			text_temp[256]		= {};	// For temporary storage of text
	char			error_string[256]	= {};	// Error code string
	long			ret;						// For return code
	unsigned long	status;						// Status
	CString			text_string;				// For error code display
	
	status = 0;
	//----------------------------------------
	// Perform application termination process
	//----------------------------------------
	ret = GenCloseApplication(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenCloseApplication = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowTextW(text_string);
	//----------------------------------------
	// Display the status
	//----------------------------------------
	if (status == 0) {
		text_string.Format(_T("0(Normal completed)"));
	}
	else if (status == 1) {
		text_string.Format(_T("1(C-WaveformGenerator completed error)"));
	}
	else {
		text_string.Format(_T("%d "), status);
	}
	m_edit_applicationstatus.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Exit button is clicked
// Terminate the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonExit()
{
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
