// AtlTimerCtrl.cpp : Implementation of CAtlTimerCtrl
#include "stdafx.h"
#include "atltimer.h"
#include "AtlTimerCtrl.h"

/////////////////////////////////////////////////////////////////////////////
// CAtlTimerCtrl


HRESULT CAtlTimerCtrl::OnDraw(ATL_DRAWINFO& di)
{
/*
Get current time.
*/
	SYSTEMTIME st ;
	GetLocalTime (&st) ;

/*
Convert into colon-separated string.
*/
	char out [128] ;

	if (m_bShowSeconds)
	{
		wsprintf (out, "%2.2d:%2.2d:%2.2d", st.wHour, st.wMinute, 
			st.wSecond) ;
	}
	else
	{
		wsprintf (out, "%2.2d:%2.2d", st.wHour, st.wMinute) ;
	}

/*
Get correct color.
*/
	COLORREF cr ;

	if (HIBYTE(HIWORD(m_clrBackColor)))
	{
		cr = GetSysColor(LOWORD(m_clrBackColor)) ;
	}
	else
	{
		cr = m_clrBackColor ;
	}

	HBRUSH hbr = CreateSolidBrush (cr) ;
	SetBkColor (di.hdcDraw, cr) ;

/*
Draw within bounding rectangle presented by framework.
*/
	FillRect (di.hdcDraw, (RECT *)di.prcBounds,hbr) ;

	DrawText(di.hdcDraw, out, -1, (RECT *)di.prcBounds, 
		DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	DeleteObject (hbr) ;
	return S_OK;
}

LRESULT CAtlTimerCtrl::OnCreate (UINT nMsg, WPARAM wParam, 
                   LPARAM lParam, BOOL& bHandled)
{
	m_TimerId = SetTimer (0, 1000, NULL) ;
	return 0;
}
 
LRESULT CAtlTimerCtrl::OnDestroy (UINT nMsg, WPARAM wParam, 
                   LPARAM lParam, BOOL& bHandled)
{
	KillTimer  (m_TimerId) ;
    return 0;
}

LRESULT CAtlTimerCtrl::OnTimer (UINT nMsg, WPARAM wParam, 
                   LPARAM lParam, BOOL& bHandled)
{
	FireViewChange( ) ;

	if (m_Expiration != 0 && m_Expiration < GetTickCount ( ) )
	{
		m_Expiration = 0 ;
		Fire_AlarmExpired ( ) ;
	}

	return 0;
}

STDMETHODIMP CAtlTimerCtrl::get_ShowSeconds(BOOL * pVal)
{
	*pVal = m_bShowSeconds ;
	return S_OK;
}

STDMETHODIMP CAtlTimerCtrl::put_ShowSeconds(BOOL newVal)
{
	m_bShowSeconds = newVal ;
	FireViewChange( ) ;

	return S_OK;
}


STDMETHODIMP CAtlTimerCtrl::SetAlarm(long SecondsToWait)
{
	m_Expiration = GetTickCount( ) + SecondsToWait * 1000 ;
	return S_OK;
}
