
// StopWatchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StopWatch.h"
#include "StopWatchDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// TIMER Header File
//---------------------------------------------------------
#include "CTimerw.h"

// CStopWatchDlg dialog



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

void CStopWatchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_edit_device_name);
	DDX_Control(pDX, IDC_STATIC_PRODUCT_NAME, m_static_product_name);
	DDX_Control(pDX, IDC_STATIC_TIME, m_static_time);
	DDX_Control(pDX, IDC_EDIT_RET, m_edit_ret);
}

BEGIN_MESSAGE_MAP(CStopWatchDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TIM_INIT, &CStopWatchDlg::OnBnClickedButtonTimInit)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_START, &CStopWatchDlg::OnBnClickedButtonCountStart)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_LAP, &CStopWatchDlg::OnBnClickedButtonCountLap)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_RESET, &CStopWatchDlg::OnBnClickedButtonCountReset)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_STOP, &CStopWatchDlg::OnBnClickedButtonCountStop)
	ON_BN_CLICKED(IDC_BUTTON_TIM_EXIT, &CStopWatchDlg::OnBnClickedButtonTimExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CStopWatchDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CStopWatchDlg message handlers

BOOL CStopWatchDlg::OnInitDialog()
{
	CDialogEx::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

	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 CStopWatchDlg::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
	{
		CDialogEx::OnPaint();
	}
}

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


//==============================================================================
// TimInit
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonTimInit()
{
	char	DeviceName[256], ProductName[256], ErrorString[256];
	TCHAR	szDeviceName[256], szProductName[256], szBuf[256];
	long	ret;

	//-----------------------------------
	// Get Device Name
	//-----------------------------------
	m_edit_device_name.GetWindowText(szDeviceName, 256);
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------------
	// Initialization
	//-----------------------------------
	ret = TimInit(DeviceName, ProductName);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimInit ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szDeviceName, _T("%S"), DeviceName);
		m_edit_device_name.SetWindowText(szDeviceName);
		wsprintf(szProductName, _T("%S"), ProductName);
		m_static_product_name.SetWindowText(szProductName);
	}
}

//==============================================================================
// Start count
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountStart()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// Start count
	//-----------------------------------
	ret = TimCountStart(&CountId);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountStart ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// Get elapsed time
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountLap()
{
	unsigned long	Microseconds;
	char			ErrorString[256];
	TCHAR			szBuf[256];
	long			ret;

	//-----------------------------------
	// Get elapsed time
	//-----------------------------------
	ret = TimCountLap(CountId, &Microseconds);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountLap ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szBuf, _T("%d"), Microseconds);
		m_static_time.SetWindowText(szBuf);
	}
}

//==============================================================================
// Reset count
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountReset()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// Reset count
	//-----------------------------------
	ret = TimCountReset(CountId);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountReset ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// Stop count
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountStop()
{
	unsigned long	Microseconds;
	char			ErrorString[256];
	TCHAR			szBuf[256];
	long			ret;

	//-----------------------------------
	// Stop count
	//-----------------------------------
	ret = TimCountStop(CountId, &Microseconds);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountStop ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szBuf, _T("%d"), Microseconds);
		m_static_time.SetWindowText(szBuf);
	}
}

//==============================================================================
// TimExit
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonTimExit()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// Exit handle
	//-----------------------------------
	ret = TimExit();
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimExit ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// Exit
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonExit()
{
	OnOK();
}
