// ScannerDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ScannerDemo.h"
#include "ScannerDemoDlg.h"

#include "../../../SDK/Include/DTScannerAPI.h"
#pragma comment(lib,"../../../SDK/lib/DTScannerAPI.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CScannerDemoDlg dialog
DtrIscpRsSymbologies* g_Symbologies=NULL;
void ReceiveScannerData(char *pBuf,long dataLen);

CScannerDemoDlg *g_pScannerWnd = NULL;
CScannerDemoDlg::CScannerDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScannerDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CScannerDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_port);
	DDX_Control(pDX, IDC_EDIT_INFO, m_ctrInfo);
	DDX_Control(pDX, IDC_EDIT_VERSION, m_version);
	DDX_Control(pDX, IDC_COMBO_TRIGGER_MODE, m_mode);
	DDX_Control(pDX, IDC_LIST_SYMBOL_ACTIVE, m_symbolActive);
	DDX_Control(pDX, IDC_LIST_SYMBOL_UNACTIVE, m_symbolUnactive);
	DDX_Control(pDX, IDC_CHECK_DISALE_ALL_SYMBOL, m_checkSymbol);
}

BEGIN_MESSAGE_MAP(CScannerDemoDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CONNECT_SCANNER, OnBnClickedButtonConnectScanner)
	ON_BN_CLICKED(IDC_BUTTONAPPLY, OnBnClickedButtonApply)
	ON_BN_CLICKED(IDC_BUTTON_FACTORY_DEFAULT, OnBnClickedButtonFactoryDefault)
	ON_BN_CLICKED(IDC_BUTTON_BEEP_ENABLE, OnBnClickedButtonBeepEnable)
	ON_BN_CLICKED(IDC_BUTTON_BEEP_DISABLE, OnBnClickedButtonBeepDisable)
	ON_BN_CLICKED(IDC_BUTTON_DO_TRIGGER, OnBnClickedButtonDoTrigger)
	ON_BN_CLICKED(IDC_BUTTON_UNDO_TRIGGER, OnBnClickedButtonUndoTrigger)
	ON_BN_CLICKED(IDC_BUTTON_ADD_SYMBOL, OnBnClickedButtonAddSymbol)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE_SYMBOL, OnBnClickedButtonRemoveSymbol)
	ON_BN_CLICKED(IDC_CHECK_DISALE_ALL_SYMBOL, OnBnClickedCheckDisaleAllSymbol)
	ON_WM_DESTROY()
	ON_WM_VSCROLL()
END_MESSAGE_MAP()

 

// CScannerDemoDlg message handlers
void CScannerDemoDlg::InitScrollInfo()   
{   	
	int heigh= GetSystemMetrics(SM_CYFULLSCREEN);
	SCROLLINFO si;   
	si.cbSize = sizeof(SCROLLINFO);   
	si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;   
	si.nMin = 0;   
	RECT minutesRect;   
	GetDlgItem(IDC_EDIT_INFO)->GetWindowRect(&minutesRect);   
	si.nMax = minutesRect.bottom;   
	si.nPage = heigh;
	si.nPos = 0;   
	SetScrollInfo(SB_VERT, &si, TRUE);   
} 
BOOL CScannerDemoDlg::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
	InitScrollInfo();
	int index=0;//  theApp.GetProfileInt(TEXT("ScannerAPI"), TEXT("DTScannerDemo"), 2);	
	m_port.SetCurSel(index);
	Scanner_SetDataReceiverCallback(&ReceiveScannerData);
	g_pScannerWnd = this;
	int count=0;
	Scanner_EnumSymbolIdentifierClass(NULL,&count);
	g_Symbologies = new DtrIscpRsSymbologies[count];
	m_nSymbolTotalCount = count;
	
	EnableCtrlWindow(FALSE);

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


void CScannerDemoDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	SCROLLINFO si = {   sizeof(SCROLLINFO),    
		SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS,    
		0, 0, 0, 0,    
		0   
	};   
	int imi = si.nPos;   
	GetScrollInfo(SB_VERT, &si);   
	switch (nSBCode)   
	{           
	case SB_TOP:   
		si.nPos = si.nMin;   
		break;   
	case SB_BOTTOM:   
		si.nPos = si.nMax; //- si.nPage;   
		break;   
	case SB_LINEDOWN:   
		if(si.nPos + si.nPage < si.nMax)   
		{   		
			SetScrollPos(SB_VERT, GetScrollPos(SB_VERT)+5);   
			::ScrollWindowEx(m_hWnd,0,-5,NULL, NULL,NULL, NULL,SW_ERASE|SW_SCROLLCHILDREN);       
		}   
		break;           
	case SB_LINEUP:    
		if(si.nPos > si.nMin)   
		{   
			SetScrollPos(SB_VERT, GetScrollPos(SB_VERT)-5);   
			::ScrollWindowEx(m_hWnd,0,5,NULL, NULL,NULL, NULL,SW_ERASE|SW_SCROLLCHILDREN);       
		}   
		if(si.nPos < si.nMin)   
			si.nPos = si.nMin;         
		break;        
	case SB_THUMBPOSITION:   
		SetScrollPos(SB_VERT, GetScrollPos(SB_VERT) + nPos - si.nPos);   
		::ScrollWindowEx(m_hWnd,0,si.nPos - nPos,NULL, NULL,NULL, NULL,SW_ERASE|SW_SCROLLCHILDREN);       
		break;   
		/*case SB_PAGEUP:   
		if(si.nPos > si.nMin)   
		{   
		SetScrollPos(SB_VERT, GetScrollPos(SB_VERT)-5);   
		::ScrollWindowEx(m_hWnd,0,5,NULL, NULL,NULL, NULL,SW_ERASE|SW_SCROLLCHILDREN);       
		}   
		if(si.nPos < si.nMin)   
		si.nPos = si.nMin;         
		break;        

		case SB_PAGEDOWN:   
		if(si.nPos + si.nPage < si.nMax)   
		{   
		SetScrollPos(SB_VERT, GetScrollPos(SB_VERT)+5);   
		::ScrollWindowEx(m_hWnd,0,-5,NULL, NULL,NULL, NULL,SW_ERASE|SW_SCROLLCHILDREN);       
		}   
		break;     */
	}   
	UpdateData(FALSE);   

	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}


void CScannerDemoDlg::OnDestroy()
{
	if (g_Symbologies)
	{
		delete [] g_Symbologies;
	}
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
}
void CScannerDemoDlg::EnableCtrlWindow(BOOL bEnable)
{

	m_mode.EnableWindow(bEnable);
	//m_port.EnableWindow(!bEnable);
	m_symbolActive.EnableWindow(bEnable);
	m_symbolUnactive.EnableWindow(bEnable);
	m_checkSymbol.EnableWindow(bEnable);

	GetDlgItem(IDC_BUTTON_REMOVE_SYMBOL)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_ADD_SYMBOL)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_DO_TRIGGER)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_UNDO_TRIGGER)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_BEEP_DISABLE)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_BEEP_ENABLE)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_FACTORY_DEFAULT)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTONAPPLY)->EnableWindow(bEnable);

	if (bEnable && m_checkSymbol.GetCheck()==BST_CHECKED)
	{
		GetDlgItem(IDC_BUTTON_REMOVE_SYMBOL)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_ADD_SYMBOL)->EnableWindow(FALSE);
		m_symbolActive.EnableWindow(FALSE);
		m_symbolUnactive.EnableWindow(FALSE);
	}

}

void CScannerDemoDlg::ShowErrMsg(int error)
{
	CString str;
	switch(error)
	{
	case 0:
		break;
	case 1:
		str = "Interface is invalid!";
		break;
	case 2:
		str = "No device found!";
		break;
	case 3:
		str = "Time out";
		break;
	case 4:
		str = "Not connected";
		break;
	case 5:
		str = "Dll internal error";
		break;
	case 6:
		str = "Not support";
		break;
	case 10:
		str = "Invalid input buffer";
		break;
	case 11:
		str = "Input buffer length too small";
		break;
	default:
		str = "UnKnown error";
		break;
	}
	if (str.IsEmpty()==FALSE)
	{
		MessageBox(str);
	}
}
void CScannerDemoDlg::UpdateSymbolIdentifier()
{

	m_symbolActive.ResetContent();
	m_symbolUnactive.ResetContent();
	int count = m_nSymbolTotalCount;

	Scanner_EnumSymbolIdentifierClass(g_Symbologies,&count);

	BOOL bActive=FALSE;
	unsigned short result=0;
	TCHAR symbolstr[256]={0};
	for(int i=0;i<count;i++)
	{
		bActive = g_Symbologies[i].bAction;

		if (bActive)
		{			
			m_symbolActive.AddString(g_Symbologies[i].string);			
			//RETAILMSG(1,(_T("activestring=%s\n"),g_Symbologies[i].string));
		}
		else
		{
			m_symbolUnactive.AddString(g_Symbologies[i].string);
			//RETAILMSG(1,(_T("Unactivestring=%s\n"),g_Symbologies[i].string));
		}

	}
}
void CScannerDemoDlg::OnBnClickedButtonConnectScanner()
{
	CString str;
	unsigned short result = 0;
	GetDlgItemText(IDC_BUTTON_CONNECT_SCANNER,str);

	CWaitCursor wait;

	if (str.Compare(_T("Connect"))==0)
	{
		result= Scanner_Connect(1);
		
		if (result==0)
		{
			SetDlgItemText(IDC_BUTTON_CONNECT_SCANNER,_T("Disconnect"));
			int mode=0;
			unsigned short retmode;
			retmode = Scanner_GetTriggerMode(&mode);
			
			if (retmode==0)
			{
				m_mode.SetCurSel(mode);
			}

			TCHAR version[256]={0};
			retmode = Scanner_GetFirmwareInfo(version,256);
			if (retmode==0)
			{
				m_version.SetWindowText(version);
			}


			UpdateSymbolIdentifier();
			EnableCtrlWindow(TRUE);


		}
		/*else
		{
			RETAILMSG(1,(TEXT("Demo fail Connect!\n")));
		}*/

	}
	else
	{
		result = Scanner_Disconnect();
		if (result==0)
		{
			SetDlgItemText(IDC_BUTTON_CONNECT_SCANNER,_T("Connect"));
			m_version.SetWindowText(_T(""));

			m_symbolActive.ResetContent();
			m_symbolUnactive.ResetContent();

			EnableCtrlWindow(FALSE);
		}



	}

	wait.Restore(); 

	ShowErrMsg(result);
}
void ReceiveScannerData(char *pBuf,long dataLen)
{

	CString str;
	str.Format(_T("%s"), pBuf);
	g_pScannerWnd->AddOutputInfo(str);

}
void CScannerDemoDlg::AddOutputInfo(CString info)
{
	TCHAR buf[28] = {0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,0x0008,0x0009,0x000b,0x000C,0x000e,0x000f,0x0010,0x0011,0x0013,0x0014,0x0015,0x0016,0x0017,0x0018,0x0019,0x001a,0x001b,0x001c,0x001d,0x001e,0x001f};
	int i = -1;
	//CString a;
	CString text;
	m_ctrInfo.GetWindowText( text );
	while(true)
	{
		i = info.FindOneOf(buf);
		//a.Format(_T("%x"),i);
		//MessageBox(a);
		if(i == -1)
			break;
		info.Delete(i);
	}
	//RETAILMSG(1,(_T("----%s---\r\n"),info));
	text = text + info;

	//if( m_ctrInfo.GetLineCount() >=50 )
	{
		//int pos = text.Find( _T("\n") , 0);
		//text = text.Mid( pos+1 );
	//	if(text.Find( _T("\n") , 0))
		text.Replace(_T("\r\n"),_T("\r"));
		text.Replace(_T("\r"),_T("\r\n"));
		text.Remove(0x1d);
	}
	text+=L"\r\n";
	//RETAILMSG(1,(_T("text = %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n"),text[0],text[1],text[2],text[3],text[4],text[5],text[6],text[7],text[8],text[9],text[10],text[11],text[12],text[13],text[14],text[15],text[16],text[17],text[18],text[19]));
	m_ctrInfo.SetWindowText( text );
	
	m_ctrInfo.LineScroll(m_ctrInfo.GetLineCount());	
}
void CScannerDemoDlg::OnBnClickedButtonApply()
{
	int index = m_mode.GetCurSel();
	unsigned short result=0;
	if (index>=0)
	{
		result = Scanner_SetTriggerMode(index);
	}
	ShowErrMsg(result);
}

void CScannerDemoDlg::OnBnClickedButtonFactoryDefault()
{
	CWaitCursor wait;

	unsigned short result=0;
	result = Scanner_SetFactoryDefault();
	if (result==0)
	{
		CString str;

		GetDlgItemText(IDC_BUTTON_CONNECT_SCANNER,str);	

		if (str.Compare(_T("Connect"))!=0)
		{
			OnBnClickedButtonConnectScanner();
		}

		if (m_checkSymbol.GetCheck()==BST_CHECKED)
		{
			m_checkSymbol.SetCheck(BST_CHECKED);
		}

		GetDlgItemText(IDC_BUTTON_CONNECT_SCANNER,str);	
		if (str.Compare(_T("Connect"))==0)
		{
			OnBnClickedButtonConnectScanner();
		}
	}

	wait.Restore();

	ShowErrMsg(result);
}

void CScannerDemoDlg::OnBnClickedButtonBeepEnable()
{
	unsigned short result=0;
	result = Scanner_SetBeepStatus(TRUE);
	ShowErrMsg(result);
}

void CScannerDemoDlg::OnBnClickedButtonBeepDisable()
{
	unsigned short result=0;
	result = Scanner_SetBeepStatus(FALSE);
	ShowErrMsg(result);
}

void CScannerDemoDlg::OnBnClickedButtonDoTrigger()
{
	unsigned short result=0;
	result = Scanner_Trigger();
	ShowErrMsg(result);
}

void CScannerDemoDlg::OnBnClickedButtonUndoTrigger()
{
	unsigned short result=0;
	result = Scanner_UnTrigger();
	ShowErrMsg(result);
}
void CScannerDemoDlg::OnBnClickedButtonAddSymbol()
{
	int nCount = m_symbolUnactive.GetSelCount();
	if (nCount<0)
	{
		return;
	}

	if (m_checkSymbol.GetCheck()==BST_CHECKED)
	{
		return;
	}

	CWaitCursor wait;

	int selIndex=0;
	CString str;

	USHORT err=0;

	while (nCount--)
	{
		m_symbolUnactive.GetSelItems(1, &selIndex);
		m_symbolUnactive.GetText(selIndex,str);
		for (int j=0;j<m_nSymbolTotalCount;j++)
		{
			if (str.Compare(g_Symbologies[j].string)==0)
			{
				if(err = Scanner_SetSymbolIdentifierStatus(g_Symbologies[j].index,TRUE)==0)
				{
					m_symbolActive.AddString(str);
					m_symbolUnactive.DeleteString(selIndex);
					g_Symbologies[j].bAction = TRUE;
				}
				break;
			}
		}
	}

	wait.Restore(); 
}

void CScannerDemoDlg::OnBnClickedButtonRemoveSymbol()
{
	int nCount = m_symbolActive.GetSelCount();
	if (nCount<0)
	{
		return;
	}

	if (m_checkSymbol.GetCheck()==BST_CHECKED)
	{
		return;
	}

	CWaitCursor wait;

	int selIndex = 0;

	CString str;
	USHORT err=0;

	while (nCount--)
	{
		m_symbolActive.GetSelItems(1, &selIndex);
		m_symbolActive.GetText(selIndex,str);

		for (int j=0;j<m_nSymbolTotalCount;j++)
		{
			if (str.Compare(g_Symbologies[j].string)==0)
			{
				if(err = Scanner_SetSymbolIdentifierStatus(g_Symbologies[j].index,FALSE)==0)
				{
					m_symbolUnactive.AddString(str);
					m_symbolActive.DeleteString(selIndex);
					g_Symbologies[j].bAction = FALSE;
				}
				break;
			}
		}
	}

	wait.Restore(); 

}

void CScannerDemoDlg::OnBnClickedCheckDisaleAllSymbol()
{
	unsigned short result;

	int check = m_checkSymbol.GetCheck();
	if (check==BST_CHECKED)
	{
		result = Scanner_SetSymbolIdentifierStatus(-1,FALSE);
		if (result==0)
		{
			m_symbolActive.EnableWindow(FALSE);
			m_symbolUnactive.EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_ADD_SYMBOL)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON_REMOVE_SYMBOL)->EnableWindow(FALSE);
		}
	}
	else
	{
		m_symbolActive.EnableWindow(TRUE);
		m_symbolUnactive.EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_ADD_SYMBOL)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_REMOVE_SYMBOL)->EnableWindow(TRUE);

		for (int i=0;i<m_nSymbolTotalCount;i++)
		{
			if (g_Symbologies[i].bAction)
			{
				Scanner_SetSymbolIdentifierStatus(g_Symbologies[i].index,TRUE);
			}
		}


	}
}


#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CScannerDemoDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_SCANNERDEMO_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_SCANNERDEMO_DIALOG));
	}
}
#endif

