
// IntervalDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Interval.h"
#include "IntervalDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

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

// CIntervalDlg dialog



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

void CIntervalDlg::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_EDIT_INTERVAL, m_edit_interval);
	DDX_Control(pDX, IDC_STATIC_MSG_COUNT, m_static_msg_count);
	DDX_Control(pDX, IDC_EDIT_RET, m_edit_ret);
	DDX_Control(pDX, IDC_BUTTON_TIM_START_TIMER, m_btn_tim_start_timer);
	DDX_Control(pDX, IDC_BUTTON_TIM_STOP_TIMER, m_btn_tim_stop_timer);
}

BEGIN_MESSAGE_MAP(CIntervalDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TIM_INIT, &CIntervalDlg::OnBnClickedButtonTimInit)
	ON_BN_CLICKED(IDC_BUTTON_TIM_START_TIMER, &CIntervalDlg::OnBnClickedButtonTimStartTimer)
	ON_BN_CLICKED(IDC_BUTTON_TIM_STOP_TIMER, &CIntervalDlg::OnBnClickedButtonTimStopTimer)
	ON_BN_CLICKED(IDC_BUTTON_TIM_EXIT, &CIntervalDlg::OnBnClickedButtonTimExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CIntervalDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CIntervalDlg message handlers

BOOL CIntervalDlg::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
	m_edit_interval.SetWindowText(_T("1000"));
	m_btn_tim_stop_timer.EnableWindow(FALSE);

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


//==============================================================================
// TimInit
//==============================================================================
void CIntervalDlg::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);
	}
}

//==============================================================================
// TimStartTimer
//==============================================================================
void CIntervalDlg::OnBnClickedButtonTimStartTimer()
{
	char			ErrorString[256];
	TCHAR			szBuf[256];
	unsigned long	Microseconds;
	long			ret;

	//-----------------------------------
	// Interval Count Initialization
	//-----------------------------------
	interval_count = 0;
	//-----------------------------------
	// Get the specified value of Interval
	//-----------------------------------
	m_edit_interval.GetWindowText(szBuf, 256);
	Microseconds = _ttoi(szBuf);
	//-----------------------------------
	// Start Interval Timer 
	//-----------------------------------
	ret = TimStartTimer(&TimerId, Microseconds, m_hWnd);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimStartTimer ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		//-----------------------------------
		// Enable the TimStopTimer button 
		//-----------------------------------
		m_btn_tim_stop_timer.EnableWindow(TRUE);
		//-----------------------------------
		// Disable the TimStartTimer button
		//-----------------------------------
		m_btn_tim_start_timer.EnableWindow(FALSE);
	}
}

//==============================================================================
// TimStopTimer
//==============================================================================
void CIntervalDlg::OnBnClickedButtonTimStopTimer()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// Stop Interval Timer 
	//-----------------------------------
	ret = TimStopTimer(TimerId);
	//-----------------------------------
	// Error handle
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimStopTimer ret = %d %S"), ret, ErrorString);	
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		//-----------------------------------
		// Disable the TimStopTimer button 
		//-----------------------------------
		m_btn_tim_stop_timer.EnableWindow(FALSE);
		//-----------------------------------
		// Enable the TimStartTimer button 
		//-----------------------------------
		m_btn_tim_start_timer.EnableWindow(TRUE);
	}
}

//==============================================================================
// TimExit
//==============================================================================
void CIntervalDlg::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);
	//-----------------------------------
	// Disable the TimStopTimer button 
	//-----------------------------------
	m_btn_tim_stop_timer.EnableWindow(FALSE);
	//-----------------------------------
	// Enable the TimStartTimer button 
	//-----------------------------------
	m_btn_tim_start_timer.EnableWindow(TRUE);
}

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

//===================================================
// Event notification handle
//===================================================
LRESULT CIntervalDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	TCHAR	szBuf[256];

	switch (message){
		//-----------------------------------
		// Interval Timer message handle
		//-----------------------------------
	case TIMM_INTERVAL:
		interval_count++;
		wsprintf(szBuf, _T("%d"), interval_count);
		m_static_msg_count.SetWindowText(szBuf);
		break;
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}