// SigPlusLCD43Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "SigPlusLCD43.h"
#include "SigPlusLCD43Dlg.h"

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

CString strSignature="";
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSigPlusLCD43Dlg dialog

CSigPlusLCD43Dlg::CSigPlusLCD43Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSigPlusLCD43Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSigPlusLCD43Dlg)
		// 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 CSigPlusLCD43Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSigPlusLCD43Dlg)
	DDX_Control(pDX, IDC_SIGPLUSCTRL1, m_SigPlus);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSigPlusLCD43Dlg, CDialog)
	//{{AFX_MSG_MAP(CSigPlusLCD43Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSigPlusLCD43Dlg message handlers

BOOL CSigPlusLCD43Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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



    //*********************************************************************
	//PLEASE NOTE: THIS DEMO LOADS SEVERAL BMPs TO THE LCD4X3 SCREEN      *
	//THE DEMO CURRENTLY LOOKS FOR THESE IMAGES IN THE LOCAL C: DIRECTORY *
	//CHANGE AS DESIRED...FIND THESE IMAGES IN THE IMAGES DIRECTORY       *
	//IN THIS DEMO                                                        *
	//*********************************************************************

	m_SigPlus.LCDSetTabletMap(0, 240, 128, 200, 250, 2000, 1300); //Sets Map up for LCD 4X3 tablet


	// The following parameters are set in case the user's INI file is not correctly set up for an LCD 4X3 tablet
	// Otherwise, if the INI is correctly set up, these parameters do not need to be set
	m_SigPlus.SetTabletXStart(500);
	m_SigPlus.SetTabletXStop(2650);
	m_SigPlus.SetTabletYStart(400);
	m_SigPlus.SetTabletYStop(2100);
	m_SigPlus.SetTabletLogicalXSize( 2150);
	m_SigPlus.SetTabletLogicalYSize( 1700);


	m_SigPlus.LCDRefresh(0, 0, 0, 240, 128);

	m_SigPlus.SetLCDCaptureMode (2); //Sets mode so ink will not disappear after a few seconds


	//The following code will write BMP images out to the LCD 4X3 screen
	m_SigPlus.LCDWriteFile(1, 2, 0, 0, 240, 128, 0, "C:\\screen2.bmp"); //This image is
	//written out to the background

	//These BMPs are written out to the foreground
	m_SigPlus.LCDWriteFile(0, 2, 10, 0, 100, 10, 0, "C:\\prescription.bmp");
	m_SigPlus.LCDWriteFile(0, 2, 10, 64, 130, 40, 0, "C:\\checkboxes.bmp");
	m_SigPlus.LCDWriteFile(0, 2, 70, 110, 100, 10, 0, "C:\\next_clear.bmp");

	//Create the hot spot for the OK BMP
	m_SigPlus.KeyPadAddHotSpot (0, 1, 11, 55, 15, 25); //Page 1 hotspots
	m_SigPlus.KeyPadAddHotSpot (1, 1, 11, 75, 15, 25); 
	m_SigPlus.KeyPadAddHotSpot (2, 1, 70, 95, 30, 30); 
	m_SigPlus.KeyPadAddHotSpot (3, 1, 132, 95, 40, 30); 

	m_SigPlus.ClearTablet();
	m_SigPlus.SetTabletState (1); //Turns tablet on to collect signature

	m_SigPlus.SetSigWindow(1, 0, 0, 1, 1);
    m_SigPlus.LCDSetWindow (0, 0, 0, 0); //Prohibit inking on entire LCD

	m_SigPlus.SetEventEnableMask (1);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSigPlusLCD43Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CSigPlusLCD43Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSigPlusLCD43Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BEGIN_EVENTSINK_MAP(CSigPlusLCD43Dlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CSigPlusLCD43Dlg)
	ON_EVENT(CSigPlusLCD43Dlg, IDC_SIGPLUSCTRL1, 2 /* PenDown */, OnPenDownSigplusctrl1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CSigPlusLCD43Dlg::OnPenDownSigplusctrl1() 
{
	// TODO: Add your control notification handler code here
	if(m_SigPlus.KeyPadQueryHotSpot(0) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);
       m_SigPlus.LCDWriteFile (0, 3, 12, 65, 14, 13, 0, "C:\\check.bmp");
	}

	if(m_SigPlus.KeyPadQueryHotSpot(1) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);
       m_SigPlus.LCDWriteFile (0, 3, 12, 88, 14, 13, 0, "C:\\check.bmp");
	}

		if(m_SigPlus.KeyPadQueryHotSpot(2) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);
       m_SigPlus.KeyPadClearHotSpotList(); //clear out page 1 hotspots
    
       //Add page 2 hotspots
       m_SigPlus.KeyPadAddHotSpot (4, 1, 17, 53, 60, 15);
       m_SigPlus.KeyPadAddHotSpot (5, 1, 106, 51, 36, 16);
       m_SigPlus.KeyPadAddHotSpot (6, 1, 187, 52, 30, 15);
    
       m_SigPlus.LCDRefresh (1, 70, 110, 30, 15); //Inverts (makes black) only
       //the secion of the LCD specified...typically used to indicate to
       //the user that their pen tap has been recognized
    
       m_SigPlus.LCDRefresh (2, 0, 0, 240, 128); //sets the BMP in the background
       //to the foreground
    
       m_SigPlus.LCDSetWindow (0, 74, 240, 54); //Permits only the section
       m_SigPlus.SetSigWindow (1, 0, 68, 240, 60);
       //specified to draw ink on the LCD screen
    
	}

	if(m_SigPlus.KeyPadQueryHotSpot(3) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);
       m_SigPlus.LCDRefresh (1, 132, 110, 40, 15); //Inverts only the section of the
       //LCD specified
    
       m_SigPlus.LCDWriteFile (0, 2, 10, 64, 130, 40, 0, "C:\\checkboxes.bmp");
       m_SigPlus.LCDRefresh (1, 132, 110, 40, 15); //Reverses inversion to
       //return that section of the LCD to normal
	}

	if(m_SigPlus.KeyPadQueryHotSpot(4) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);

	   m_SigPlus.KeyPadClearHotSpotList(); //clear out page 2 hotspots
    
	   //Add page 1 hotspots
	   m_SigPlus.KeyPadAddHotSpot (0, 1, 11, 55, 15, 25); //set up hot spots for page 1
	   m_SigPlus.KeyPadAddHotSpot (1, 1, 11, 75, 15, 25);
	   m_SigPlus.KeyPadAddHotSpot (2, 1, 70, 95, 30, 30);
	   m_SigPlus.KeyPadAddHotSpot (3, 1, 132, 95, 40, 30);
    
	   m_SigPlus.LCDRefresh (1, 18, 53, 58, 15); //Inverts that section of LCD
	   m_SigPlus.LCDRefresh (0, 0, 0, 240, 128);
    
	   //Redraw all of the BMPs, user goes back to page 1
	   m_SigPlus.LCDWriteFile(0, 2, 10, 0, 100, 10, 0, "C:\\prescription.bmp");
	   m_SigPlus.LCDWriteFile(0, 2, 10, 64, 130, 40, 0, "C:\\checkboxes.bmp");
	   m_SigPlus.LCDWriteFile(0, 2, 70, 110, 100, 10, 0, "C:\\next_clear.bmp");
	   m_SigPlus.LCDSetWindow (0, 0, 0, 0); //Prohibit ink from being drawn on entire LCD
	   m_SigPlus.SetSigWindow(1, 0, 0, 1, 1);
	}

	if(m_SigPlus.KeyPadQueryHotSpot(5) > 0)  //if the OK hotspot is tapped, then...
	{
       m_SigPlus.ClearTablet(); //Clear SigPlus
	   m_SigPlus.ClearSigWindow(1);
	   m_SigPlus.LCDRefresh (1, 106, 52, 40, 16);
       m_SigPlus.LCDRefresh (2, 0, 0, 240, 128);
	}

    if(m_SigPlus.KeyPadQueryHotSpot(6) > 0)
	{
    //m_SigPlus.ClearTablet();
	m_SigPlus.ClearSigWindow(1);
    m_SigPlus.LCDRefresh (1, 186, 53, 35, 15);
    Sleep (500);
    m_SigPlus.LCDRefresh (0, 0, 0, 240, 128);

		if( m_SigPlus.NumberOfTabletPoints() > 0)
		{
			m_SigPlus.SetTabletState (0);
			m_SigPlus.SetJustifyMode (5);

			///////////////////////////////////////////////////////////////////////
			//before extracting signature, it can be compressed and encrypted
			//in fact, this step is important in keeping with eSign laws
			//for details, see commented code in short section below
			//***********************************************************
			//m_SigPlus.AutoKeyStart();
			//m_SigPlus.SetAutoKeyData("this is sample encryption data");
			//m_SigPlus.AutoKeyFinish();
			//m_SigPlus.SetSigCompressionMode(2);
			//m_SigPlus.SetEncryptionMode(2);
			strSignature = m_SigPlus.GetSigString(); //strSignature now holds SigString
			//if you use the 5 commented-out lines above, before the signature can
			//be successfully reset in a new instance of SigPlus, the lines must be
			//called again
			//////////////////////////////////////////////////////////////////////

			m_SigPlus.LCDSetFont (18, 8, 1, 0, 0, 0, "Arial");
			m_SigPlus.LCDWriteString (0, 2, 23, 49, 0, 0, 0, "Thank You For Signing!");
			m_SigPlus.LCDWriteFile (0, 2, 58, 90, 105, 30, 0, "c:\\topazlogo.bmp");
			Sleep (3000);
			m_SigPlus.ClearTablet();
			//m_SigPlus.LCDRefresh 1, 186, 53, 35, 15
			m_SigPlus.SetTabletState (1);
			m_SigPlus.LCDRefresh (0, 0, 0, 240, 128); //Clears entire LCD screen
			m_SigPlus.SetLCDCaptureMode (1); //Resets regular auto-clear inking
			m_SigPlus.SetTabletState (0);
			exit(0);
		}
		else
		{
			m_SigPlus.LCDSetFont (16, 9, 1, 0, 0, 0, "Arial");
			m_SigPlus.LCDWriteString (0, 2, 65, 38, 0, 0, 0, "Please Sign");
			m_SigPlus.LCDWriteString (0, 2, 40, 63, 0, 0, 0, "Before Continuing...");
			Sleep (2500);
			m_SigPlus.ClearTablet();
			m_SigPlus.LCDRefresh (2, 0, 0, 240, 128);
		}
    
	}


m_SigPlus.ClearSigWindow(1);
m_SigPlus.SetEventEnableMask(1);
}

void CSigPlusLCD43Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	m_SigPlus.SetTabletState (1);
    m_SigPlus.SetLCDCaptureMode (1);
    m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
    m_SigPlus.SetTabletState (0);
	CDialog::OnCancel();
}

void CSigPlusLCD43Dlg::OnOK() 
{
	// TODO: Add extra validation here
	m_SigPlus.SetTabletState (1);
    m_SigPlus.SetLCDCaptureMode (1);
    m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
    m_SigPlus.SetTabletState (0);
	CDialog::OnOK();
}
