
// MltLineDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MltLine.h"
#include "MltLineDlg.h"
#include "afxdialogex.h"

// Including Files ////////////////////////////////////////
#include "Gpibac.h"
#include "SubFunc.h"
///////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Static //////////////////////////////////////////////////////////
#define GP_GTL		0x01
#define GP_SDC		0x04
#define GP_PPC		0x05
#define GP_GET		0x08
#define GP_TCT		0x09
#define GP_LLO		0x11
#define GP_DCL		0x14
#define GP_PPU		0x15
#define GP_SPE		0x18
#define GP_SPD		0x19
#define GP_MLA		0x20
#define GP_UNL		0x3F
#define GP_MTA		0x40
#define GP_UNT		0x5F
// Setting /////////////////////////////////////////////////////////////
DWORD	Myadr, Yradr;			// MyAddress,Address of device
DWORD	Cmd[50];				// Command Array(1)
CString	csBuf;					// Character string
BOOL	bRet;					// BOOL value in AddComand function 
long	CmdNum, CmdNumPos;		// Number of command,Position of command 
long	Ret;					// Return code
long	CmdNumArray[50];		// Command Array(2)
char	DelMyadr[8];			// For delete MyAddress in listbox
/////////////////////////////////////////////////////////////////////////////

// CMltLineDlg dialog



CMltLineDlg::CMltLineDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMltLineDlg::IDD, pParent)
{
	m_TextMsg = _T("");
	m_TextRet = _T("");
	m_YrAdrBox = _T("");

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMltLineDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_YRADR_BOX, m_DelYrAdr);
	DDX_Text(pDX, IDC_TEXT_MSG, m_TextMsg);
	DDX_Text(pDX, IDC_TEXT_RET, m_TextRet);
	DDX_CBString(pDX, IDC_YRADR_BOX, m_YrAdrBox);
}

BEGIN_MESSAGE_MAP(CMltLineDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INIT, &CMltLineDlg::OnBnClickedInit)
	ON_BN_CLICKED(IDC_SEND, &CMltLineDlg::OnBnClickedSend)
	ON_BN_CLICKED(IDC_DCL, &CMltLineDlg::OnBnClickedDcl)
	ON_BN_CLICKED(IDC_GET, &CMltLineDlg::OnBnClickedGet)
	ON_BN_CLICKED(IDC_GTL, &CMltLineDlg::OnBnClickedGtl)
	ON_BN_CLICKED(IDC_LLO, &CMltLineDlg::OnBnClickedLlo)
	ON_BN_CLICKED(IDC_SDC, &CMltLineDlg::OnBnClickedSdc)
	ON_BN_CLICKED(IDC_UNL, &CMltLineDlg::OnBnClickedUnl)
	ON_BN_CLICKED(IDC_UNT, &CMltLineDlg::OnBnClickedUnt)
	ON_CBN_SELCHANGE(IDC_YRADR_BOX, &CMltLineDlg::OnCbnSelchangeYradrBox)
	ON_BN_CLICKED(IDC_ALL_DEL, &CMltLineDlg::OnBnClickedAllDel)
	ON_BN_CLICKED(IDC_DEL, &CMltLineDlg::OnBnClickedDel)
	ON_BN_CLICKED(IDC_EXT, &CMltLineDlg::OnBnClickedExt)
END_MESSAGE_MAP()


// CMltLineDlg message handlers

BOOL CMltLineDlg::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

	// ****************************************************** Setting *** 
	CmdNum = 1;
	CmdNumPos = 0;
	m_YrAdrBox = _T("1");
	UpdateData(FALSE);
	// ******************************************************************

	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 CMltLineDlg::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 CMltLineDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

/////////////////////////////////////////////////////////////////////
// This Sample Code is here.
/////////////////////////////////////////////////////////////////////

//////////////////////////// [DispCommandList] function ///
CString DispCommandList()
{
	long	TxtRet;
	long	Count;								// For Text	of Return code
	CString Text = _T(""), TmpString;			// Making message,All message

	for (Count = 1; Count < CmdNum; Count++){
		TxtRet = Cmd[Count];
		TmpString.Format(_T(" [%xH]"), TxtRet);
		Text = Text + TmpString;
	}
	return	Text;
}

///////////////////////////////// [AddCommand] function ///
BOOL AddCommand(long CommandType, int CommandNum)
{
	CString	ErrMsg;								// For Error Message

	if (CmdNum > 30){							// In terms of a message in the case of 30 or more
		ErrMsg = _T("Message is over!!");
		AfxMessageBox(ErrMsg);
		return FALSE;
	}
	if (CommandNum == 2){						// In case of necessary two message
		Cmd[CmdNum] = (Yradr | 0x20);			// Add Address of device to 0x20
		CmdNum = CmdNum + 1;
	}
	Cmd[CmdNum] = CommandType;
	CmdNum = CmdNum + 1;

	return TRUE;								// A normal end
}

//////////////////////////////////////////// Initialize ///
void CMltLineDlg::OnBnClickedInit()
{
	TCHAR DelMyadr[8];

	UpdateData(TRUE);

	GpibInit(&csBuf);							// SubClass is called
	GpBoardsts(0x08, &Myadr);					// My Address is acquired
	m_TextRet = csBuf;
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);

	wsprintf(DelMyadr, _T("%d"), Myadr);		// Show MyAddress
	Ret = m_DelYrAdr.FindStringExact(-1, DelMyadr);
	if (Ret >= 0){								// Seaching MyAddress
		m_DelYrAdr.DeleteString(Ret);			// Delete MyAddress 
		if (Myadr == Yradr){
			m_DelYrAdr.SetCurSel(0);
		}
	}

	UpdateData(FALSE);

	Yradr = GetDlgItemInt(IDC_YRADR_BOX);
}

////////////////////////////////////////////////// Send ///
void CMltLineDlg::OnBnClickedSend()
{
	Cmd[0] = CmdNum - 1;

	if (CmdNum <= 1){
		csBuf = _T("There are not transmission data.");
	}
	else{
		Ret = GpComand(Cmd);					// Send command
		CheckRet(_T("GpComand"), Ret, &csBuf);	// Error check
	}
	m_TextRet = csBuf;
	UpdateData(FALSE);
}

////////////////////////////////////////////////// DCL ////
void CMltLineDlg::OnBnClickedDcl()
{
	bRet = AddCommand(GP_DCL, 1);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 1;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// GET ///
void CMltLineDlg::OnBnClickedGet()
{
	bRet = AddCommand(GP_GET, 2);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 2;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// GTL ///
void CMltLineDlg::OnBnClickedGtl()
{
	bRet = AddCommand(GP_GTL, 2);				// AddCommand(Static, Command number)
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 2;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// LLO ///
void CMltLineDlg::OnBnClickedLlo()
{
	bRet = AddCommand(GP_LLO, 1);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 1;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// SDC ///
void CMltLineDlg::OnBnClickedSdc()
{
	bRet = AddCommand(GP_SDC, 2);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 2;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// UNL ///
void CMltLineDlg::OnBnClickedUnl()
{
	bRet = AddCommand(GP_UNL, 1);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 1;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

/////////////////////////////////////////////////// UNT ///
void CMltLineDlg::OnBnClickedUnt()
{
	bRet = AddCommand(GP_UNT, 1);
	if (bRet == TRUE){
		CmdNumArray[CmdNumPos] = 1;
		CmdNumPos = CmdNumPos + 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

//////////////////////////////////////// Address of device Change ///
void CMltLineDlg::OnCbnSelchangeYradrBox()
{
	UpdateData(TRUE);
	Yradr = GetDlgItemInt(IDC_YRADR_BOX);
}

//////////////////////////////////////////// ALL DELETE ///
void CMltLineDlg::OnBnClickedAllDel()
{
	CmdNum = 1;
	CmdNumPos = 0;

	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

//////////////////////////////////////////// ONE DELETE ///
void CMltLineDlg::OnBnClickedDel()
{
	if (CmdNumPos == 0)	{						// In case of Command is no
		m_TextRet = _T("There is not a message.");
	}
	else {
		if (CmdNumArray[CmdNumPos - 1] == 1){
			Cmd[CmdNum] = 0;
			CmdNum = CmdNum - 1;
		}
		else {
			Cmd[CmdNum] = 0;
			Cmd[CmdNum - 1] = 0;
			CmdNum = CmdNum - 2;
		}
		CmdNumArray[CmdNumPos - 1] = 0;
		CmdNumPos = CmdNumPos - 1;
		m_TextRet = "";
	}
	m_TextMsg = DispCommandList();
	UpdateData(FALSE);
}

////////////////////////////////////////////////// Exit ///
void CMltLineDlg::OnBnClickedExt()
{
	GpibExit();									// SubFunc.h is called
	CDialog::OnCancel();						// End of program
}

BOOL CMltLineDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_ESCAPE: 						// ESC Key is disebled.
			return TRUE;
		default:
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}
