// SigPlusLCD15DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SigPlusLCD15Demo.h"
#include "SigPlusLCD15DemoDlg.h"

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

CString strSig="";
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSigPlusLCD15DemoDlg dialog

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

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

/////////////////////////////////////////////////////////////////////////////
// CSigPlusLCD15DemoDlg message handlers

BOOL CSigPlusLCD15DemoDlg::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 LCD1X5 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, 64, 100, 0, 1900, 700); //Sets Map up for LCD 1X5 tablet


	// The following parameters are set in case the user's INI file is not correctly set up for an LCD 1X5 tablet
	// Otherwise, if the INI is correctly set up, these parameters do not need to be set
	m_SigPlus.SetTabletXStart(400);
	m_SigPlus.SetTabletXStop(2400);
	m_SigPlus.SetTabletYStart(350);
	m_SigPlus.SetTabletYStop(1050);
	m_SigPlus.SetTabletLogicalXSize( 2000);
	m_SigPlus.SetTabletLogicalYSize( 700);



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

	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 1X5 screen
	m_SigPlus.LCDWriteFile(1, 2, 0, 0, 240, 53, 0, "C:\\thankyou.bmp"); //This image is
	//written out to the background

	//These two BMPs are written out to the foreground
	m_SigPlus.LCDWriteFile(0, 2, 0, 20, 240, 44, 0, "C:\\sign.bmp");
	m_SigPlus.LCDWriteFile(0, 2, 207, 4, 21, 11, 0, "C:\\ok.bmp");
	m_SigPlus.LCDWriteFile(0, 2, 15, 4, 37, 11, 0, "C:\\clear.bmp");

	//Create the hot spot for the OK BMP
	m_SigPlus.KeyPadAddHotSpot (0, 1, 197, 5, 19, 17); //For OK button
	m_SigPlus.KeyPadAddHotSpot (1, 1, 10, 5, 53, 17); //For CLEAR button


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

	//The following sets the LCD's signature window up to display ink only in the
	//certain spot on the LCD specified--in this case, the Signature BMP area
	m_SigPlus.LCDSetWindow (0, 22, 240, 40);
	m_SigPlus.SetSigWindow (1, 0, 22, 240, 40); //Sets the area where ink is permitted in the SigPlus object

	//blnClearTab = False
	m_SigPlus.SetEventEnableMask (1);

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

void CSigPlusLCD15DemoDlg::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 CSigPlusLCD15DemoDlg::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 CSigPlusLCD15DemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

void CSigPlusLCD15DemoDlg::OnPenDownSigplusctrl1() 
{
	// TODO: Add your control notification handler code here



if(m_SigPlus.KeyPadQueryHotSpot(0) > 0)  //if the OK hotspot is tapped, then...
{
    m_SigPlus.ClearSigWindow(1);
    //*********************Two ways to save the signature*

    //Method 1--storing as an ASCII string value
    

	///////////////////////////////////////////////////////////////////////
	//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);
	strSig = m_SigPlus.GetSigString(); //strSig 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
	//////////////////////////////////////////////////////////////////////


    //the strSig String variable now holds the signature as a long ASCII string.
    //this can be stored as desired, in a database, etc.
    
    //Method 2--storing as a SIG file on the hard drive
    //SigPlus1.ExportSigFile "C:\SigFile1.sig"
    //The commented-out function above will export the signature to the SIG file
    //specified (in this case C:\SigFile1.sig, saving the signature as a file on your hardrive
    //*****************************************************************************************'
    m_SigPlus.LCDRefresh (1, 210, 3, 14, 14); //Refresh LCD at 'OK' to indicate to user that this option has been sucessfully chosen

    if( m_SigPlus.NumberOfTabletPoints() > 0)
	{
       m_SigPlus.LCDSetWindow (0, 0, 0, 0);
       m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
       m_SigPlus.LCDRefresh (2, 0, 0, 240, 52); //Brings the background image already loaded into foreground
       m_SigPlus.ClearTablet();
       m_SigPlus.ClearSigWindow(1);
       m_SigPlus.KeyPadClearHotSpotList();
       m_SigPlus.KeyPadAddHotSpot (2, 1, 29, 38, 44, 19); //For BACK button
       m_SigPlus.KeyPadAddHotSpot (3, 1, 172, 37, 40, 19); //For EXIT button
       m_SigPlus.SetEventEnableMask (1);
	}
    else
	{
       m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
       m_SigPlus.LCDWriteFile (0, 2, 4, 20, 234, 20, 0, "C:\\please.bmp");
       Sleep (750);
       m_SigPlus.ClearTablet();
       m_SigPlus.LCDWriteFile (0, 2, 0, 20, 240, 44, 0, "C:\\sign.bmp");
       m_SigPlus.LCDWriteFile (0, 2, 207, 4, 21, 11, 0, "C:\\ok.bmp");
       m_SigPlus.LCDWriteFile (0, 2, 15, 4, 37, 11, 0, "C:\\clear.bmp");
    }
}

if( m_SigPlus.KeyPadQueryHotSpot(1) > 0) //if the CLEAR hotspot is tapped, then...
{
	m_SigPlus.ClearSigWindow(1);
    m_SigPlus.LCDRefresh (1, 10, 0, 53, 17); //Refresh LCD at 'CLEAR' to indicate to user that this option has been sucessfully chosen
    //m_SigPlus.LCDRefresh 0, 0, 18, 240, 50 'Clear out both signature and signature box
    m_SigPlus.LCDWriteFile( 0, 2, 0, 20, 240, 44, 0, "C:\\sign.bmp"); //redraw sig box
    m_SigPlus.LCDRefresh (1, 10, 0, 53, 17); //Refresh LCD at 'CLEAR' to return CLEAR button to normal
    m_SigPlus.ClearTablet();
}
if( m_SigPlus.KeyPadQueryHotSpot(2) > 0) //if the BACK hotspot is tapped, then...
{
	m_SigPlus.ClearSigWindow(1);
    m_SigPlus.LCDRefresh (1, 29, 38, 44, 19); //Refresh (invert) LCD at 'BACK' to indicate to user that this option has been sucessfully chosen
    m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
    m_SigPlus.ClearTablet();
    m_SigPlus.ClearSigWindow (1);
    m_SigPlus.LCDSetWindow (0, 22, 240, 40);
    m_SigPlus.LCDWriteFile (0, 2, 0, 20, 240, 44, 0, "C:\\sign.bmp");
    m_SigPlus.LCDWriteFile (0, 2, 207, 4, 21, 11, 0, "C:\\ok.bmp");
    m_SigPlus.LCDWriteFile (0, 2, 15, 4, 37, 11, 0, "C:\\clear.bmp");
    m_SigPlus.ClearTablet();
    m_SigPlus.KeyPadClearHotSpotList();
    m_SigPlus.KeyPadAddHotSpot (0, 1, 197, 5, 19, 17); //For OK button
    m_SigPlus.KeyPadAddHotSpot (1, 1, 10, 5, 53, 17); //For CLEAR button
}

if (m_SigPlus.KeyPadQueryHotSpot(3) > 0) //if the BACK hotspot is tapped, then...
{
	m_SigPlus.ClearSigWindow(1);
    m_SigPlus.LCDRefresh (1, 182, 37, 30, 19); //Refresh (invert) LCD at 'OK' to indicate to user that this option has been sucessfully chosen
    m_SigPlus.SetLCDCaptureMode (1);
    m_SigPlus.LCDRefresh (0, 0, 0, 240, 64);
    m_SigPlus.SetTabletState (0);
    exit(0);
}
m_SigPlus.ClearSigWindow(1);
m_SigPlus.SetEventEnableMask (1);





	
}



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