
// DemoVCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DemoVC.h"
#include "DemoVCDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDemoVCDlg dialog



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

void CDemoVCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_VIEW, m_View);
	DDX_Control(pDX, IDC_COMBO2, m_cboCouple);
	DDX_Control(pDX, IDC_COMBO3, m_cboVoltDiv);
	DDX_Control(pDX, IDC_EDIT1, m_editVPos);
	DDX_Control(pDX, IDC_EDIT2, m_editBufferlen);
	DDX_Control(pDX, IDC_EDIT3, m_editReadLen);
	DDX_Control(pDX, IDC_EDIT4, m_editDisplayLen);
	DDX_Control(pDX, IDC_COMBO1, m_cboCH);
	DDX_Control(pDX, IDC_EDIT5, m_editSampleRate);
	DDX_Control(pDX, IDC_CHECK1, m_chkOnOff);
}

BEGIN_MESSAGE_MAP(CDemoVCDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_STN_CLICKED(IDC_STATIC_VIEW, &CDemoVCDlg::OnStnClickedStaticView)
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_COMBO1, &CDemoVCDlg::OnCbnSelchangeCombo1)
	ON_EN_CHANGE(IDC_EDIT1, &CDemoVCDlg::OnEnChangeEdit1)
	ON_EN_KILLFOCUS(IDC_EDIT1, &CDemoVCDlg::OnEnKillfocusEdit1)
	ON_EN_KILLFOCUS(IDC_EDIT2, &CDemoVCDlg::OnEnKillfocusEdit2)
	ON_EN_KILLFOCUS(IDC_EDIT3, &CDemoVCDlg::OnEnKillfocusEdit3)
	ON_EN_KILLFOCUS(IDC_EDIT4, &CDemoVCDlg::OnEnKillfocusEdit4)
	ON_CBN_SELCHANGE(IDC_COMBO2, &CDemoVCDlg::OnCbnSelchangeCombo2)
	ON_CBN_SELCHANGE(IDC_COMBO3, &CDemoVCDlg::OnCbnSelchangeCombo3)
	ON_EN_KILLFOCUS(IDC_EDIT5, &CDemoVCDlg::OnEnKillfocusEdit5)
	ON_BN_CLICKED(IDC_CHECK1, &CDemoVCDlg::OnBnClickedCheck1)
END_MESSAGE_MAP()


// CDemoVCDlg message handlers

BOOL CDemoVCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 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

	// TODO: Add extra initialization here
	if (m_Hard.m_bValidDevice)
	{
		SetTimer(0, 100, NULL);
	}
	m_cboVoltDiv.AddString(_T("500uV"));
	m_cboVoltDiv.AddString(_T("1mV"));
	m_cboVoltDiv.AddString(_T("2mV"));
	m_cboVoltDiv.AddString(_T("5mV"));
	m_cboVoltDiv.AddString(_T("10mV"));
	m_cboVoltDiv.AddString(_T("20mV"));
	m_cboVoltDiv.AddString(_T("50mV"));
	m_cboVoltDiv.AddString(_T("100mV"));
	m_cboVoltDiv.AddString(_T("200mV"));
	m_cboVoltDiv.AddString(_T("500mV"));
	m_cboVoltDiv.AddString(_T("1V"));
	m_cboVoltDiv.AddString(_T("2V"));
	m_cboVoltDiv.AddString(_T("5V"));
	m_cboVoltDiv.AddString(_T("10V"));
	m_cboCH.SetCurSel(0);
	updateControls();
	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 CDemoVCDlg::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 CDemoVCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CDemoVCDlg::OnStnClickedStaticView()
{
	// TODO: Add your control notification handler code here
}


void CDemoVCDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	if (SIZE_MINIMIZED == nType)
		return;
	//if()
	if (m_View.m_hWnd == NULL)
		return;
	CRect rect;
	GetWindowRect(&rect);
	ScreenToClient(&rect);
	//rect
	int nBottomMargin = 180;
	rect.bottom -= nBottomMargin;
	if (rect.bottom < rect.top + 30)
		return;
	m_View.MoveWindow(rect);
	//m_View.Invalidate();
	Invalidate();
	// TODO: Add your message handler code here
}


void CDemoVCDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == 0)
	{
		if (!m_Hard.m_bValidDevice)
		{
			KillTimer(nIDEvent);
		}
		if (m_Hard.ReadData())
		{
			m_View.Invalidate();
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}


void CDemoVCDlg::OnCbnSelchangeCombo1()
{
	// TODO: Add your control notification handler code here
	updateControls();
}


void CDemoVCDlg::OnEnChangeEdit1()
{

}
void CDemoVCDlg::updateControls()
{
	int n=m_cboCH.GetCurSel();
	if (n < 0)
		return;
	CH *pCH = &m_Hard.m_ch[n];
	m_cboCouple.SetCurSel(pCH->nCHCoupling == DC ? 0 : 1);
	m_cboVoltDiv.SetCurSel(pCH->nCHVoltDIV);
	CString str;
	m_chkOnOff.SetCheck(m_Hard.m_ch[n].bCHEnable ? 1 : 0);
	str.Format(_T("%d"), pCH->m_nLeverPos);	m_editVPos.SetWindowText(str);
	str.Format(_T("%d"), m_Hard.m_nReadLen);	m_editReadLen.SetWindowText(str);
	str.Format(_T("%d"), m_Hard.m_nBufferLen);	m_editBufferlen.SetWindowText(str);

	str.Format(_T("%d"), m_View.m_nDisPlayLen);	m_editDisplayLen.SetWindowText(str);

	
}

void CDemoVCDlg::OnEnKillfocusEdit1()
{
	// TODO: Add your control notification handler code here
	int n = m_cboCH.GetCurSel();
	if (n < 0)
		return;
	CString str;
	CEdit *p = &m_editVPos;
	p->GetWindowText(str);
	int x = _ttoi(str);
	str.Format(_T("%d"), x);
	p->SetWindowText(str);
	m_Hard.setCHVpos(n, x);



	
}


void CDemoVCDlg::OnEnKillfocusEdit2()
{
	// TODO: Add your control notification handler code here

	CString str;
	m_editBufferlen.GetWindowText(str);
	int x = _ttoi(str);
	str.Format(_T("%d"), x);
	m_editBufferlen.SetWindowText(str);
	m_Hard.SetBufferLen(x);
}


void CDemoVCDlg::OnEnKillfocusEdit3()
{
	// TODO: Add your control notification handler code here
	CString str;
	m_editReadLen.GetWindowText(str);
	int x = _ttoi(str);
	str.Format(_T("%d"), x);
	m_editReadLen.SetWindowText(str);
	m_Hard.SetReadLen(x);
}


void CDemoVCDlg::OnEnKillfocusEdit4()
{
	CString str;
	m_editDisplayLen.GetWindowText(str);
	int x = _ttoi(str);
	str.Format(_T("%d"), x);
	m_editDisplayLen.SetWindowText(str);
	m_View.m_nDisPlayLen = x;
	// TODO: Add your control notification handler code here
}


void CDemoVCDlg::OnCbnSelchangeCombo2()
{
	int n = m_cboCH.GetCurSel();
	if (n < 0)
		return;
	int m = m_cboCouple.GetCurSel();
	if (m < 0)
		return;
	m_Hard.setCHCouple(n, m);
	// TODO: Add your control notification handler code here
	//m_Hard.setCHCouple()
}


void CDemoVCDlg::OnCbnSelchangeCombo3()
{
	int n = m_cboCH.GetCurSel();
	if (n < 0)
		return;
	int m = m_cboVoltDiv.GetCurSel();
	if (m < 0)
		return;
	m_Hard.setCHVoltDiv(n, m);
	// TODO: Add your control notification handler code here
}


void CDemoVCDlg::OnEnKillfocusEdit5()
{
	// TODO: Add your control notification handler code here
	CString str;
	m_editSampleRate.GetWindowText(str);
	double x = _ttof(str);
	
	
	m_Hard.SetSampleRate(x);
	str.Format(_T("%G"), m_Hard.m_fSampleRate);
	m_editSampleRate.SetWindowText(str);
}


void CDemoVCDlg::OnBnClickedCheck1()
{
	// TODO: Add your control notification handler code here
	int n = m_cboCH.GetCurSel();
	if (n < 0)
		return;
	//BST_UNCHECKED
	bool bEnable = m_chkOnOff.GetCheck();
	m_Hard.SetCHOnOff(n, bEnable);
}
