// NotifyDlg.cpp : t@C
//

#include "stdafx.h"
#include "Counter.h"
#include "CounterDlg.h"
#include "NotifyDlg.h"
#include "afxdialogex.h"

//---------------------------------------------------------
// JE^wb_[t@C
//---------------------------------------------------------
#include "CCnt.h"
//---------------------------------------------------------
// Oϐ
//---------------------------------------------------------
extern	short	ChannelNum;			// `l
//---------------------------------------------------------
// f[^
//---------------------------------------------------------
static	BOOL	bNotifyError = TRUE;
static	BOOL	bNotifyCarry = TRUE;
static	DWORD	CompData[CHANNEL_NUM][2] = {
	{ 100, 150 },
	{ 200, 250 },
	{ 300, 350 },
	{ 400, 450 },
};

// CNotifyDlg _CAO

IMPLEMENT_DYNAMIC(CNotifyDlg, CDialogEx)

CNotifyDlg::CNotifyDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CNotifyDlg::IDD, pParent)
{

}

CNotifyDlg::~CNotifyDlg()
{
}

void CNotifyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_ERROR0, m_Error);
	DDX_Control(pDX, IDC_RADIO_CARRY0, m_Carry);
	DDX_Control(pDX, IDC_EDIT_CH3_REG1, m_Ch3Reg1);
	DDX_Control(pDX, IDC_EDIT_CH3_REG0, m_Ch3Reg0);
	DDX_Control(pDX, IDC_EDIT_CH2_REG1, m_Ch2Reg1);
	DDX_Control(pDX, IDC_EDIT_CH2_REG0, m_Ch2Reg0);
	DDX_Control(pDX, IDC_EDIT_CH1_REG1, m_Ch1Reg1);
	DDX_Control(pDX, IDC_EDIT_CH1_REG0, m_Ch1Reg0);
	DDX_Control(pDX, IDC_EDIT_CH0_REG1, m_Ch0Reg1);
	DDX_Control(pDX, IDC_EDIT_CH0_REG0, m_Ch0Reg0);
}


BEGIN_MESSAGE_MAP(CNotifyDlg, CDialogEx)
END_MESSAGE_MAP()


// CNotifyDlg bZ[W nh[

//===================================================
//	_CAO
//===================================================
BOOL CNotifyDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	TCHAR	szBuf[100];
	short	i;

	//-----------------------------------
	// Rg[zݒ
	//-----------------------------------
	//------------------
	// rWX^0
	//------------------
	m_Reg0[0]	= &m_Ch0Reg0;
	m_Reg0[1]	= &m_Ch1Reg0;
	m_Reg0[2]	= &m_Ch2Reg0;
	m_Reg0[3]	= &m_Ch3Reg0;
	//------------------
	// rWX^1
	//------------------
	m_Reg1[0]	= &m_Ch0Reg1;
	m_Reg1[1]	= &m_Ch1Reg1;
	m_Reg1[2]	= &m_Ch2Reg1;
	m_Reg1[3]	= &m_Ch3Reg1;
	//-----------------------------------
	// f[^̃Zbg
	//-----------------------------------
	for(i = 0; i < CHANNEL_NUM; i++){
		wsprintf(szBuf, _T("%lu"), CompData[i][0]);
		m_Reg0[i]->SetWindowText(szBuf);
		wsprintf(szBuf, _T("%lu"), CompData[i][1]);
		m_Reg1[i]->SetWindowText(szBuf);
		//------------------
		// `l𒴂ꍇrWX^\𖳌
		//------------------
		if(i >= ChannelNum){
			m_Reg0[i]->EnableWindow(FALSE);
			m_Reg1[i]->EnableWindow(FALSE);
		//------------------
		// `lȉ̏ꍇ\L
		//------------------
		}else{
			m_Reg0[i]->EnableWindow(TRUE);
			m_Reg1[i]->EnableWindow(TRUE);
		}
	}
	
	if (bNotifyError == TRUE) {
		m_Error.SetCheck(TRUE);
	} else {
		SendDlgItemMessage(IDC_RADIO_ERROR1, BM_SETCHECK, 1, 0);
	}
	if (bNotifyCarry == TRUE) {
		m_Carry.SetCheck(TRUE);
	} else {
		SendDlgItemMessage(IDC_RADIO_CARRY1, BM_SETCHECK, 1, 0);
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// O : OCX vpeB y[W͕K FALSE Ԃ܂B
}

//===================================================
// ݒ芮
//===================================================
void CNotifyDlg::OnOK()
{
	TCHAR		szBuf[100];
	long		ret;
	short		ChNo, RegNo;
	CString		str;
	char		error_string[256];
	TCHAR		szError_string[256];
	//-------------------------------
	// ݒ̎擾
	//-------------------------------
	for(ChNo = 0; ChNo < ChannelNum; ChNo++){
		m_Reg0[ChNo]->GetWindowText(szBuf, sizeof(szBuf));
		_stscanf_s(szBuf, _T("%lu"), &CompData[ChNo][0]);
		m_Reg1[ChNo]->GetWindowText(szBuf, sizeof(szBuf));
		_stscanf_s(szBuf, _T("%lu"), &CompData[ChNo][1]);
	}
	bNotifyError = m_Error.GetCheck();
	bNotifyCarry = m_Carry.GetCheck();

	//-------------------------------
	// ݒfoCXփZbg
	//-------------------------------
	for(ChNo = 0; ChNo < ChannelNum; ChNo++){
		for(RegNo = 0; RegNo < 2; RegNo++){
			//---------------------------------------------
			// JEgv̒ʒm & rWX^ ݒ
			//---------------------------------------------
			ret = CntNotifyCountUp(Id, ChNo, RegNo, CompData[ChNo][RegNo], hMainWnd);
			if (ret != 0) {
				CntGetErrorString(ret, error_string);
				wsprintf(szError_string, _T("%S"), error_string);
				str.Format(_T("CntNotifyCountUp : ret = %d\n %s"), ret, szError_string);
				MessageBox(str, _T("Error"), MB_OK);
				return;
			}
		}
	}
	//-------------------------------
	// JE^G[ʒm̏ꍇ
	//-------------------------------
	if (bNotifyError == TRUE) {
		//---------------------------------------------
		// JE^G[ʒm ݒ
		//---------------------------------------------
		ret = CntNotifyCounterError(Id, hMainWnd);
		if (ret != 0) {
			CntGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			str.Format(_T("CntNotifyCounterError : ret = %d\n %s"), ret, szError_string);
			MessageBox(str, _T("Error"), MB_OK);
			return;
		}
	}
	//-------------------------------
	// L[/{[ʒm̏ꍇ
	//-------------------------------
	if (bNotifyCarry == TRUE) {
		//---------------------------------------------
		// L[/{[ʒm ݒ
		//---------------------------------------------
		ret = CntNotifyCarryBorrow(Id, hMainWnd);
		if (ret != 0) {
			CntGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			str.Format(_T("CntNotifyCarryBorrow : ret = %d\n %s"), ret, szError_string);
			MessageBox(str, _T("Error"), MB_OK);
		}
	}

	CDialogEx::OnOK();
}

//===================================================
// LZ
//===================================================
void CNotifyDlg::OnCancel()
{
	CDialogEx::OnCancel();
}
