// LKSamplePrintDlg.cpp : implementation file
//

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

#include "..\\include\\SEWOOP30.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)
		// NOTE: the ClassWizard will add member initialization here
	//}}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_Control(pDX, IDC_COMBO1, m_cboPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLKSamplePrintDlg, CDialog)
	//{{AFX_MSG_MAP(CLKSamplePrintDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	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_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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	for(int i = 0; i < 10; i++)
	{
		CString strPort;
		strPort.Format(L"COM%d:",i);
		m_cboPort.AddString(strPort);
	}
	
	m_cboPort.SetCurSel(9); // COM9 Default



	// TODO: Add extra initialization here
	GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_CLOSE)->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);

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



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

	m_cboPort.GetLBText(m_cboPort.GetCurSel(),strPort);	

	opened = OpenPort(strPort, 115200); // Bluetooth SPP[Serial Port Profile]
	if(opened == 0) {
		GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_CLOSE)->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::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
	long	opened;
	opened = ClosePort();
	if(opened == 0) {
		GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_CLOSE)->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;
	CString	ErrMsg;

	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();
	if( lResult != LK_STS_NORMAL)
	{
		ErrMsg.Format(L"Printer Status get Error ErrorCode = %d", lResult);
		AfxMessageBox(ErrMsg);
		return;
	}

	PrintNormal(L"\x1b|cA\x1b|bC\x1b|2CReceipt\n");
	PrintNormal(L"\x1b|rATEL (123)-456-7890\n\n");
	PrintNormal(L"\x1b|cAThank you for coming to our shop\n");
	PrintNormal(L"\x1b|cA------------------------------------------------\n");
	PrintNormal(L"Chicken                                   $10.00\n");
	PrintNormal(L"Hamburger                                 $20.00\n");
	PrintNormal(L"Pizza                                     $30.00\n");
	PrintNormal(L"Lemons                                    $40.00\n");
	PrintNormal(L"Drink                                     $50.00\n");
	PrintNormal(L"Excluded tax                             $150.00\n");
	PrintNormal(L"\x1b|uCTax(5%)                                    $7.50\n");
	PrintNormal(L"\x1b|bC\x1b|2CTotal            $157.50\n\n");
	PrintNormal(L"Payment                                  $200.00\n");
	PrintNormal(L"Change                                    $42.50\n\n");
	PrintNormal(L"\x1b|fP");
}

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

	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();
	if( lResult != LK_STS_NORMAL)
	{
		ErrMsg.Format(L"Printer Status get Error ErrorCode = %d", lResult);
		AfxMessageBox(ErrMsg);
		return;
	}
	
	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;

	PrintBitmap(strFile, LK_ALIGNMENT_CENTER, LK_BITMAP_NO_DITHER);

	PrintNormal(L"\x1b|fP");
}

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

	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();
	if( lResult != LK_STS_NORMAL)
	{
		ErrMsg.Format(L"Printer Status get Error ErrorCode = %d", lResult);
		AfxMessageBox(ErrMsg);
		return;
	}

	PrintNormal(L"-----start of barcode-----\n");
	PrintNormal(L"3 of 9 barcode\n");
	PrintBarCode(L"1234567890", LK_BCS_Code39, 40, 576, 1, 2);
	PrintNormal(L"Code 93 barcode\n");
	PrintBarCode(L"0123498765", LK_BCS_Code93, 40, 576, 1, 2);
	PrintNormal(L"Interleaved 2 of 5 barcode\n");
	PrintBarCode(L"0987654321", LK_BCS_ITF, 40, 576, 1, 2);
	PrintNormal(L"Code 128 A barcode\n");
	PrintBarCode(L"{ACODE 128", LK_BCS_Code128, 40, 576, 1, 2);
	PrintNormal(L"Code 128 B barcode\n");
	PrintBarCode(L"{BCode 128", LK_BCS_Code128, 40, 576, 1, 2);
	PrintNormal(L"Code 128 C barcode\n");
	PrintBarCode(L"{C12345", LK_BCS_Code128, 40, 576, 1, 2);
	PrintNormal(L"Codabar barcode\n");
	PrintBarCode(L"A1029384756A", LK_BCS_Codabar, 40, 576, 1, 2);

	PrintNormal(L"\n-----end of barcode-----\n\n");

	PrintNormal(L"\x1b|fP");

}

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

	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();
	if( lResult != LK_STS_NORMAL)
	{
		ErrMsg.Format(L"Printer Status get Error ErrorCode = %d", lResult);
		AfxMessageBox(ErrMsg);
		return;
	}

	PrintNormal(L"----True-Type Font Printing.----\n");
	PrintNormal(L"Print Normal(24x24)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x00, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x05, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x06, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x08, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x88, 24, 2);
	PrintNormal(L"Print Italic(24x24)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x10, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x15, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x16, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x18, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x98, 24, 2);
	PrintNormal(L"Print Underline(24x24)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x20, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x25, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x26, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x28, 24, 2);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0xA8, 24, 2);

	PrintNormal(L"Print Normal(28x28)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x00, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x05, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x06, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x08, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x88, 28, 1);
	PrintNormal(L"Print Italic(28x28)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x10, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x15, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x16, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x18, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x98, 28, 1);
	PrintNormal(L"Print Underline(28x28)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x20, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x25, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x26, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x28, 28, 1);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0xA8, 28, 1);

	PrintNormal(L"Print Normal(32x32)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x00, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x05, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x06, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x08, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x88, 32, 0);
	PrintNormal(L"Print Italic(32x32)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x10, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x15, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x16, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x18, 32, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x98, 32, 0);
	PrintNormal(L"Print Underline(32x32)\n");
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x20, 28, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x25, 28, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x26, 28, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0x28, 28, 0);
	PrintText2Image(L"Tahoma Font", L"Tahoma", 0xA8, 28, 0);

	PrintNormal(L"--True-Type Font Printing End.--\n\n");

	PrintNormal(L"\x1b|fP");
}

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

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

	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();
	if( lResult != LK_STS_NORMAL)
	{
		ErrMsg.Format(L"Printer Status get Error ErrorCode = %d", lResult);
		AfxMessageBox(ErrMsg);
		return;
	}

	// PDF417 Print.
	PrintNormal(L"PDF417 Column=3, Cell Width = 2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 3, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=4, Cell Width = 2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 4, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=5, Cell Width = 2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 5, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=6, Cell Width = 2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 6, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=7, Cell Width=2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 7, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=8, Cell Width=2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 8, 2, LK_ALIGNMENT_CENTER);

	PrintNormal(L"PDF417 Column=4, Cell Width = 2\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 4, 2, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=4, Cell Width = 3\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 4, 3, LK_ALIGNMENT_CENTER);
	PrintNormal(L"PDF417 Column=3, Cell Width = 4\n");
	PrintPDF417(L"SEWOO MOBILEPRINTER", 0, 3, 4, LK_ALIGNMENT_CENTER);

	// QRCode Print.
	PrintNormal(L"QRCode Left Alignment\n");
	PrintQRCode(L"SEWOO MOBILEPRINTER", 0, 3, 0, LK_ALIGNMENT_LEFT);
	PrintNormal(L"QRCode Center Alignment\n");
	PrintQRCode(L"SEWOO MOBILEPRINTER", 0, 3, 0, LK_ALIGNMENT_CENTER);
	PrintNormal(L"QRCode Right Alignment\n");
	PrintQRCode(L"SEWOO MOBILEPRINTER", 0, 3, 0, LK_ALIGNMENT_RIGHT);

	PrintNormal(L"\x1b|fP");	
}
