
// SampleBCDDlg.cpp : 구현 파일
//

#include "stdafx.h"
#include "SampleBCD.h"
#include "SampleBCDDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSampleBCDDlg 대화 상자
CSampleBCDDlg *pDlg;

CSampleBCDDlg::CSampleBCDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSampleBCDDlg::IDD, pParent)
	, m_nInterface(0)
	, m_nConnectionType(0)
	, m_strPort(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	pDlg = this;
}

void CSampleBCDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CSampleBCDDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_RADIO3, &CSampleBCDDlg::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &CSampleBCDDlg::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO5, &CSampleBCDDlg::OnBnClickedRadio5)	
	ON_BN_CLICKED(IDC_RADIO6, &CSampleBCDDlg::OnBnClickedRadio6)
	ON_BN_CLICKED(IDC_RADIO7, &CSampleBCDDlg::OnBnClickedRadio7)	
	ON_BN_CLICKED(IDC_BTN_DISPLAY_STR, &CSampleBCDDlg::OnBnClickedBtnDisplayStr)
	ON_BN_CLICKED(IDC_BTN_CLEAR_SCREEN, &CSampleBCDDlg::OnBnClickedBtnClearScreen)
	ON_BN_CLICKED(IDC_BTN_STORE_IMG, &CSampleBCDDlg::OnBnClickedBtnStoreImg)
	ON_BN_CLICKED(IDC_BTN_DISPLAY_IMG, &CSampleBCDDlg::OnBnClickedBtnDisplayImg)
	ON_BN_CLICKED(IDC_BTN_CLEAR_IMG, &CSampleBCDDlg::OnBnClickedBtnClearImg)
	ON_BN_CLICKED(IDC_BTN_CURSOR, &CSampleBCDDlg::OnBnClickedBtnCursor)
	ON_BN_CLICKED(IDC_BTN_CURSOR2, &CSampleBCDDlg::OnBnClickedBtnCursor2)
END_MESSAGE_MAP()


// CSampleBCDDlg 메시지 처리기

BOOL CSampleBCDDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	SetIcon(m_hIcon, TRUE);			// 큰 아이콘을 설정합니다.
	SetIcon(m_hIcon, FALSE);		// 작은 아이콘을 설정합니다.

	pBCD    = new CMPosControllerBCD();

#if _DEBUG
	//set usb interface
 	CButton* pButton = (CButton*)GetDlgItem(IDC_RADIO5);
 	pButton->SetCheck(true); 

	pButton = (CButton*)GetDlgItem(IDC_RADIO6);
	pButton->SetCheck(true); 

	m_nInterface = MPOS_INTERFACE_USB;
	m_nConnectionType = MPOS_MODE_DEFAULT;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = _T("");
#else
	CButton* pButton = (CButton*)GetDlgItem(IDC_RADIO3);
	pButton->SetCheck(true); 

	m_nInterface = MPOS_INTERFACE_ETHERNET;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(TRUE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(FALSE);
#endif

	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("192.168.0.10"));

	return TRUE;  // 포커스를 컨트롤에 설정하지 않으면 TRUE를 반환합니다.
}

void CSampleBCDDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트

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

		// 클라이언트 사각형에서 아이콘을 가운데에 맞춥니다.
		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;

		// 아이콘을 그립니다.
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// 사용자가 최소화된 창을 끄는 동안에 커서가 표시되도록 시스템에서
//  이 함수를 호출합니다.
HCURSOR CSampleBCDDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CSampleBCDDlg::OnBnClickedRadio3()
{
	m_nInterface = MPOS_INTERFACE_ETHERNET;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(TRUE);
	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("192.168.0.10"));
	GetDlgItem(IDC_EDIT2)->ShowWindow(FALSE);
	UpdateData(FALSE);
}

void CSampleBCDDlg::OnBnClickedRadio4()
{
	m_nInterface = MPOS_INTERFACE_BLUETOOTH;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = "COM3";
	UpdateData(FALSE);
}

void CSampleBCDDlg::OnBnClickedRadio5()
{
	m_nInterface = MPOS_INTERFACE_USB;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = _T("");
	UpdateData(FALSE);
}

void CSampleBCDDlg::OnBnClickedRadio6()
{
	m_nConnectionType = MPOS_MODE_DEFAULT;
}

void CSampleBCDDlg::OnBnClickedRadio7()
{
	m_nConnectionType = MPOS_MODE_BYPASS;
}

void CSampleBCDDlg::OnBnClickedBtnDisplayStr()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}
	
	CString data = _T("BIXOLON");
	CString cp852 = _T("ĄąŽžĘę¬źČş«»");
	pBCD->displayString(data, BCD_CODEPAGE_CP437, POS_ICS_USA, 1);
	pBCD->displayString(cp852, BCD_CODEPAGE_CP852, POS_ICS_USA, 2);

	pBCD->closeService(1000);
}

void CSampleBCDDlg::OnBnClickedBtnClearScreen()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}

	pBCD->clearScreen();
	pBCD->closeService(1000);
}

void CSampleBCDDlg::OnBnClickedBtnStoreImg()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}
	TCHAR szFilters[] = _T("All Files(*.*)|*.*||");

	CFileDialog FileDlg(TRUE, _T("*.bmp"), _T(""), OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilters);

	if(FileDlg.DoModal() != IDOK)
		return;

	CString fileName;
	fileName = FileDlg.GetPathName();

	pBCD->storeImageFile(fileName, 100, 1);

	CString str;
	str.Format(_T("Image was stored."));
	AfxMessageBox(str);

	pBCD->closeService(1000);
}

void CSampleBCDDlg::OnBnClickedBtnDisplayImg()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}
	
	pBCD->displayImage(1, 0, 0);
	pBCD->closeService(1000);
}

void CSampleBCDDlg::OnBnClickedBtnClearImg()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}

	pBCD->clearImage(1);

	CString str;
	str.Format(_T("Image was cleared."));
	AfxMessageBox(str);

	pBCD->closeService(1000);
}

void CSampleBCDDlg::OnBnClickedBtnCursor()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}
	
	pBCD->showCursor(1);
	pBCD->closeService(1000);
	
}

void CSampleBCDDlg::OnBnClickedBtnCursor2()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	pBCD->selectInterface(m_nInterface, m_strPort);
	pBCD->selectCommandMode(m_nConnectionType);
	long lResult = pBCD->openService();
	if(lResult == RES_ALREADY_OPEN)
		return;
	if(lResult != RES_SUCCESS)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);		
		return ;
	}

	pBCD->showCursor(0);
	pBCD->closeService(1000);
}
