// LKSamplePrintDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LKSamplePrint.h"
#include "LKSamplePrintDlg.h"

#include "..\\include\\P30CPCL.h" 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLKSamplePrintDlg dialog

CLKSamplePrintDlg::CLKSamplePrintDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLKSamplePrintDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLKSamplePrintDlg)
	m_strIP = _T("");
	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLKSamplePrintDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLKSamplePrintDlg)
	DDX_Text(pDX, IDC_EDIT_IP, m_strIP);
	DDX_Check(pDX, IDC_CHECK1, m_bCheck1);
	DDX_Check(pDX, IDC_CHECK2, m_bCheck2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLKSamplePrintDlg, CDialog)
	//{{AFX_MSG_MAP(CLKSamplePrintDlg)
	ON_BN_CLICKED(IDC_PRINTTEXT, OnPrinttext)
	ON_BN_CLICKED(IDC_PRINT_BITMAP, OnPrintBitmap)
	ON_BN_CLICKED(IDC_PRINTBARCODE, OnPrintbarcode)
	ON_BN_CLICKED(IDC_PRINTSAMPLE, OnPrintsample)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
	ON_BN_CLICKED(IDC_PRINT_2DBARCODE, OnPrint2dbarcode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLKSamplePrintDlg message handlers

BOOL CLKSamplePrintDlg::OnInitDialog()
{
	CDialog::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
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRINT_BITMAP)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRINTBARCODE)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRINTSAMPLE)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRINTTEXT)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRINT_2DBARCODE)->EnableWindow(FALSE);
	
	m_strIP = L"192.168.10.200";
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLKSamplePrintDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
	long	opened;
	UpdateData(TRUE);
	CString strPort;

	opened = OpenPort(m_strIP, 9100);		

	if(opened == 0) {
		GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_PRINT_BITMAP)->EnableWindow(TRUE);
		GetDlgItem(IDC_PRINTBARCODE)->EnableWindow(TRUE);
		GetDlgItem(IDC_PRINTSAMPLE)->EnableWindow(TRUE);
		GetDlgItem(IDC_PRINTTEXT)->EnableWindow(TRUE);
		GetDlgItem(IDC_PRINT_2DBARCODE)->EnableWindow(TRUE);
	} else {
		MessageBox(L"Open Failed", L"Error", MB_OK);
	}
}

void CLKSamplePrintDlg::OnButtonDisconnect() 
{
	// TODO: Add your control notification handler code here
	long	opened;
	opened = ClosePort();
	if(opened == 0) {
		GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_PRINT_BITMAP)->EnableWindow(FALSE);
		GetDlgItem(IDC_PRINTBARCODE)->EnableWindow(FALSE);
		GetDlgItem(IDC_PRINTSAMPLE)->EnableWindow(FALSE);
		GetDlgItem(IDC_PRINTTEXT)->EnableWindow(FALSE);
		GetDlgItem(IDC_PRINT_2DBARCODE)->EnableWindow(FALSE);
	} else {
		MessageBox(L"Open Failed", L"Error", MB_OK);
	}

}

void CLKSamplePrintDlg::OnPrinttext() 
{
	// TODO: Add your control notification handler code here
	long lResult;

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_BUSY)	== LK_STS_BUSY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck2 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & 0x0f) != 0x00) {
		UpdateData(FALSE);
		return;
	}

	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	CPCLPrintText(0, 0, 0, L"1", L"1",   L"FONT-0-0", 0);
	CPCLPrintText(0, 0, 1, L"1", L"50",  L"FONT-0-1", 0);
	CPCLPrintText(0, 4, 0, L"1", L"100", L"FONT-4-0", 0);
	CPCLPrintText(0, 4, 1, L"1", L"150",  L"FONT-4-1", 0);
	CPCLPrintText(0, 4, 2, L"1", L"260",  L"4-2", 0);
	PrintForm();
}

void CLKSamplePrintDlg::OnPrintBitmap() 
{
	// TODO: Add your control notification handler code here
	long lResult;

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_BUSY)	== LK_STS_BUSY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck2 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & 0x0f) != 0x00) {
		UpdateData(FALSE);
		return;
	}

	CString strFile;
	CFileDialog FileDlg(TRUE, L"", strFile, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, L"Image File(*.bmp)|*.bmp|Image File(*.jpg)|*.jpg|Image File(*.gif)|*.gif||");
	
	if(FileDlg.DoModal() == IDOK)
	{
		strFile = FileDlg.GetPathName();
	} else return;

	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// Printing the saved in flash memory.
//	CPCLPrintImage(L"SAMPLE02.PCX", L"12", L"150");
	CPCLPrintImage(strFile, L"10", L"10");
	CPCLPrintText2Image(0, L"200", L"8", L"Arial", 0x06, 24, L"Test24", 0);
	CPCLPrintText2Image(0, L"200", L"80", L"Arial", 0x06, 32, L"Test32", 0);
	CPCLPrintText2Image(0, L"200", L"160", L"Arial", 0x06, 48, L"Test48", 0);
	PrintForm();
}

void CLKSamplePrintDlg::OnPrintbarcode() 
{
	// TODO: Add your control notification handler code here
	long lResult;

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_BUSY)	== LK_STS_BUSY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck2 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & 0x0f) != 0x00) {
		UpdateData(FALSE);
		return;
	}

	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// density.
	SetTone(50);

	// CODABAR
	CPCLSetBarCode(0, 0, L"0");
	CPCLPrintBarCode(0, 7, 2, 1, 30, L"19", L"45", L"A37859B", 0);
	CPCLPrintText(0, 7, 0, L"19", L"18", L"CODABAR", 0);
	// Code 39
	CPCLSetBarCode(0, 0, L"0");
//	CPCLPrintBarCode(0, 0, 2, 1, 30, L"19", L"130", L"0123456", 10);
//	CPCLPrintText(0, 7, 0, L"21", L"103", L"CODE 39", 0);
	CPCLPrintBarCode(0, LK_CPCL_BCS_93, 1, 1, 30, L"19", L"130", L"1234567890123456", 0);
	CPCLPrintText(0, 7, 0, L"21", L"103", L"CODE 93", 0);
	// PDF-417
	CPCL2DPrintBarCode(0, 0, L"162", L"234", 2, 10, 2, 1, L"SEWOO TECH\r\nLK-P30");

	PrintForm();
}

void CLKSamplePrintDlg::OnPrintsample() 
{
	// TODO: Add your control notification handler code here
	long lResult;

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_BUSY)	== LK_STS_BUSY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck2 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & 0x0f) != 0x00) {
		UpdateData(FALSE);
		return;
	}

	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// density.
	SetTone(50);

	CPCLPrintText2Image(0, L"0", L"0", L"Tahoma", 0x00, 24, L"Tahoma Font", 0);
	CPCLPrintText2Image(0, L"0", L"30", L"Tahoma", 0x05, 24, L"Tahoma Font", 0);
	CPCLPrintText2Image(0, L"0", L"60", L"Tahoma", 0x06, 24, L"Tahoma Font", 0);
	CPCLPrintText2Image(0, L"0", L"90", L"Tahoma", 0x08, 24, L"Tahoma Font", 0);
	CPCLPrintText2Image(0, L"0", L"120", L"Tahoma", 0x08, 24, L"Tahoma Font", 1);

	PrintForm();
}

void CLKSamplePrintDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}


void CLKSamplePrintDlg::OnPrint2dbarcode() 
{
	// TODO: Add your control notification handler code here
	long lResult;

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_BUSY)	== LK_STS_BUSY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck2 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & 0x0f) != 0x00) {
		UpdateData(FALSE);
		return;
	}


	// PDF-417
	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// density.
	SetTone(50);
	CPCLPrintText(0, 7, 0, L"0", L"0",   L"PDF417 TEST", 0);
	CPCL2DPrintBarCode(0, 0, L"0", L"36", 2, 10, 2, 1, L"SEWOO TECH\r\nLK-P30");
	PrintForm();

	// QRCode
	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// density.
	SetTone(50);
	CPCLPrintText(0, 7, 0, L"0", L"0",   L"QRCODE TEST", 0);
	CPCL2DPrintBarCode(0, 1, L"0", L"36", 4, 0, 1, 0, L"SEWOO TECH\r\nLK-P30");
	PrintForm();

	// DataMatrix
	SetForm(L"0", 200, 200, L"406", 1);
	SetFormMode();
	// density.
	SetTone(50);
	CPCLPrintText(0, 7, 0, L"0", L"0",   L"DataMatrix TEST", 0);
	CPCL2DPrintBarCode(0, 2, L"0", L"36", 6, 0, 100, 0, L"SEWOO TECH\r\nLK-P30");
	PrintForm();
}
