// ImageProcTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ImageProcTest.h"
#include "ImageProcTestDlg.h"
#include "ChipmonkCoordinator.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CChipMonkCoordinator chipMonkCoordinator;
// CImageProcTestDlg dialog


class CWinXPOS :public COSBase {
	CImageProcTestDlg *m_pDlg;
public:
	CWinXPOS() : m_pDlg (NULL) { }
	BOOL Init(CImageProcTestDlg *pDlg) {
		m_pDlg = pDlg;
		return TRUE;
	}

	void DrawText(const WCHAR *pText) {
		if(m_pDlg) {
			m_pDlg->DrawText(pText);
		}
	}
	void DisplayPageInfo(const WCHAR *pText) {
		if(m_pDlg) {
			m_pDlg->DisplayPageInfo(pText);
		}
	}

	void Speak(const WCHAR *pText) {
		if(m_pDlg) {
			m_pDlg->Speak(pText);
		}
	}

	void DingSound() {
		if(m_pDlg) {
			m_pDlg->DingSound();
		}
	}
};

CWinXPOS g_winXPOS;

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

void CImageProcTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_SPEECH_ENBLED, m_bSpeechOutputEnabled);
	DDX_Control(pDX, IDC_EDIT_OCR_TEXT, m_editOcrText);
}

BEGIN_MESSAGE_MAP(CImageProcTestDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_OPEN_IMAGE, &CImageProcTestDlg::OnBnClickedButtonOpenImage)
	ON_BN_CLICKED(IDC_CHECK_SPEECH_ENBLED, &CImageProcTestDlg::OnBnClickedCheckSpeechEnbled)
END_MESSAGE_MAP()


// CImageProcTestDlg message handlers

BOOL CImageProcTestDlg::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

	HRESULT hr = m_cpVoice.CoCreateInstance( CLSID_SpVoice );
	g_winXPOS.Init(this);
	BOOL bInitialized = chipMonkCoordinator.Init(&g_winXPOS);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImageProcTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CImageProcTestDlg::OnBnClickedButtonOpenImage()
{
	CFileDialog fd(TRUE, CString(_T("jpg")), NULL, OFN_OVERWRITEPROMPT|OFN_EXPLORER, 
		_T("JPEG files (*.jpg)|*.jpg||"));
	if(fd.DoModal() != IDOK) {
		return;
	}
	CString sFilePath = fd.GetPathName();
	char sAsciiFN[MAX_PATH];
	WideCharToMultiByte(CP_ACP, 0, sFilePath, -1, sAsciiFN, MAX_PATH, NULL, NULL);
	chipMonkCoordinator.OnOpenImage(sAsciiFN);
}

void CImageProcTestDlg::OnBnClickedCheckSpeechEnbled()
{
	UpdateData();
}

void CImageProcTestDlg::DrawText(const WCHAR *pText) {
	m_editOcrText.SetWindowText(pText);
}

void CImageProcTestDlg::DisplayPageInfo(const WCHAR *pText) {
	SetDlgItemText(IDC_PAGE_INFO, pText);
}

void CImageProcTestDlg::Speak(const WCHAR *pText) {
	if(m_bSpeechOutputEnabled) {
		DWORD nCurrentStreamID;
		HRESULT hRes = m_cpVoice->Speak(pText, SPF_ASYNC, &nCurrentStreamID);
		if(hRes) {
			TRACE(L"Can't Speak!!!");
		}
	}
}

void CImageProcTestDlg::DingSound() {
	PlaySound(_T("IDR_WAVE_DING"), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
}
