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

#include "stdafx.h"
#include "Printer.h"
#include "PrinterDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CPrinterDlg 대화 상자
CPrinterDlg *pDlg;



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

	pDlg = this;
}

CPrinterDlg::~CPrinterDlg()
{
	m_Printer.closeService(1000);
}

void CPrinterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT2, m_strPort);
}

BEGIN_MESSAGE_MAP(CPrinterDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_RADIO3, &CPrinterDlg::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &CPrinterDlg::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO5, &CPrinterDlg::OnBnClickedRadio5)
	ON_BN_CLICKED(IDC_BUTTON1, &CPrinterDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CPrinterDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CPrinterDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_RADIO6, &CPrinterDlg::OnBnClickedRadio6)
	ON_BN_CLICKED(IDC_RADIO7, &CPrinterDlg::OnBnClickedRadio7)
END_MESSAGE_MAP()


// CPrinterDlg 메시지 처리기

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

	//set default command mode
#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 = "";
#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 CPrinterDlg::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 CPrinterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CPrinterDlg::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 CPrinterDlg::OnBnClickedRadio4()
{
	m_nInterface = MPOS_INTERFACE_BLUETOOTH;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = "COM3";
	UpdateData(FALSE);
}

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

void CPrinterDlg::OnBnClickedButton1()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) 
		GetDlgItemText(IDC_IPADDRESS1, m_strPort);
		
	m_Printer.selectInterface(m_nInterface, m_strPort);
	m_Printer.selectCommandMode(m_nConnectionType);
	long lResult = m_Printer.openService();
	if(lResult != RES_SUCCESS && lResult != RES_ALREADY_OPEN)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);
		return;
	}
	
	m_Printer.printText(_T("B-Gate SDK Sample (font A)\r\n"), 0, 0, 0, 0, 0, 0, POS_ALIGNMENT_LEFT);
	m_Printer.printText(_T("B-Gate SDK Sample (font B)\r\n"), 1, 0, 0, 0, 0, 0, POS_ALIGNMENT_LEFT);
	m_Printer.printText(_T("B-Gate SDK Sample (font C)\r\n"), 2, 0, 0, 0, 0, 0, POS_ALIGNMENT_LEFT);

	m_Printer.printText(_T("right align\r\n"), 0, 1, 1, 0, 0, 0, POS_ALIGNMENT_RIGHT);
	m_Printer.printText(_T("center align\r\n"), 0, 1, 1, 0, 0, 0, POS_ALIGNMENT_CENTER);
	m_Printer.printText(_T("Left align\r\n"), 0, 1, 1, 0, 0, 0, POS_ALIGNMENT_LEFT);

	m_Printer.print1DBarcode(_T("1234567890123456"), POS_BARCODE_CODE128, 3, 100, POS_ALIGNMENT_RIGHT, POS_BARCODE_TEXT_NONE);
	m_Printer.print1DBarcode(_T("1234567890123456"), POS_BARCODE_CODE128, 3, 100, POS_ALIGNMENT_CENTER, POS_BARCODE_TEXT_ABOVE);
	m_Printer.print1DBarcode(_T("1234567890123456"), POS_BARCODE_CODE128, 3, 100, POS_ALIGNMENT_LEFT, POS_BARCODE_TEXT_BELOW);

	m_Printer.printBitmapFile(_T("Logo.bmp"), 300, POS_ALIGNMENT_RIGHT, 0, FALSE, TRUE);
	m_Printer.printBitmapFile(_T("Logo.bmp"), 300, POS_ALIGNMENT_CENTER, 0, FALSE, TRUE);
	m_Printer.printBitmapFile(_T("Logo.bmp"), 300, POS_ALIGNMENT_LEFT, 0, FALSE, TRUE);

	m_Printer.cutPaper(POS_FEED_PARTIAL_CUT);
}

void __stdcall CPrinterDlg::StatusUpdateEvent(int status)
{
	CString str = pDlg->getPrinterStatusString(status);
	pDlg->SetDlgItemText(IDC_STATIC_DATA, str);
}

void CPrinterDlg::OnBnClickedButton2()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);
	m_Printer.selectInterface(m_nInterface, m_strPort);
	m_Printer.selectCommandMode(m_nConnectionType);
	long lResult = m_Printer.openService();
	if(lResult != RES_SUCCESS && lResult != RES_ALREADY_OPEN)
	{
		CString strdata;
		strdata.Format(_T("Error Code = %d"), lResult);
		AfxMessageBox(strdata);
		return;
	}
		
	m_Printer.asbEnable(TRUE);
	m_Printer.setStatusUpdateEvent(CPrinterDlg::StatusUpdateEvent);
	m_Printer.openDrawer(POS_DRAWER_PIN2, 100, 100);
}

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

	m_Printer.setPagemode(POS_PAGEMODE_IN);
	m_Printer.setPagemodePrintArea(0, 0, 500, 1000);
	m_Printer.setPagemodeDirection(POS_DIRECTION_LEFT90);
	m_Printer.setPagemodePosition(100, 190);	
	m_Printer.printText(_T("MPos print test"), 0, 0, 0, 0, 0, 0, 0);
	m_Printer.setPagemodePosition(100, 210);
	m_Printer.printText(_T("pagemode test"), 0, 0, 0, 0, 0, 0, 0);
	m_Printer.setPagemodePosition(100, 240);
	m_Printer.printText(_T("rotate left 90"), 0, 0, 0, 0, 0, 0, 0);

	m_Printer.setPagemodePosition(100, 350);
	m_Printer.print1DBarcode(_T("1234567890123456"), POS_BARCODE_CODE128, 3, 100, POS_ALIGNMENT_LEFT, POS_BARCODE_TEXT_BELOW);

	m_Printer.setPagemodePosition(480, 110);
	m_Printer.printQRCode(_T("http://www.bixolon.com"), POS_QRCODE_MODEL1, POS_ALIGNMENT_CENTER, 4, POS_QRCODE_ECC_H);
	
	m_Printer.setPagemode(POS_PAGEMODE_OUT);
	
	m_Printer.cutPaper(POS_FEED_PARTIAL_CUT);
}

CString CPrinterDlg::getPrinterStatusString(int status)
{
	if(POS_STATUS_COVEROPEN == status)
		return _T("Cover open");
	else if(POS_STATUS_PAPEREMPTY == status)
		return _T("Paper empty");
	else if(POS_STATUS_PAPER_NEAREND == status)
		return _T("Nearend Paper");
	else if(POS_STATUS_ERROR == status)
		return _T("Error");
	else if (POS_STATUS_DRAWER_HIGH == status)
		return _T("Drawer High");
	else if (POS_STATUS_DRAWER_LOW == status)
		return _T("Drawer Low");
	else// if(MPOS_STATUS_NORMAL == status)
		return _T("Normal");

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

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