// TimerCtl.cpp : Implementation of the CTimerCtrl OLE control class.

#include "stdafx.h"
#include "timer.h"
#include "TimerCtl.h"
#include "TimerPpg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CTimerCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CTimerCtrl, COleControl)
	//{{AFX_MSG_MAP(CTimerCtrl)
	ON_WM_DESTROY()
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CTimerCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CTimerCtrl)
	DISP_PROPERTY_NOTIFY(CTimerCtrl, "ShowSeconds", m_showSeconds, OnShowSecondsChanged, VT_BOOL)
	DISP_FUNCTION(CTimerCtrl, "SetAlarm", SetAlarm, VT_EMPTY, VTS_I4)
	DISP_STOCKPROP_BACKCOLOR()
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CTimerCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CTimerCtrl, COleControl)
	//{{AFX_EVENT_MAP(CTimerCtrl)
	EVENT_CUSTOM("AlarmExpired", FireAlarmExpired, VTS_I4)
	EVENT_STOCK_DBLCLICK()
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CTimerCtrl, 2)
	PROPPAGEID(CTimerPropPage::guid)
	PROPPAGEID(CLSID_CColorPropPage)
END_PROPPAGEIDS(CTimerCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CTimerCtrl, "TIMER.TimerCtrl.1",
	0x69999373, 0xbd40, 0x11cf, 0x8a, 0x6c, 0, 0xaa, 0, 0xa5, 0x80, 0x97)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CTimerCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DTimer =
		{ 0x69999371, 0xbd40, 0x11cf, { 0x8a, 0x6c, 0, 0xaa, 0, 0xa5, 0x80, 0x97 } };
const IID BASED_CODE IID_DTimerEvents =
		{ 0x69999372, 0xbd40, 0x11cf, { 0x8a, 0x6c, 0, 0xaa, 0, 0xa5, 0x80, 0x97 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwTimerOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CTimerCtrl, IDS_TIMER, _dwTimerOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::CTimerCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CTimerCtrl

BOOL CTimerCtrl::CTimerCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_TIMER,
			IDB_TIMER,
			afxRegApartmentThreading,
			_dwTimerOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::CTimerCtrl - Constructor

CTimerCtrl::CTimerCtrl()
{
	InitializeIIDs(&IID_DTimer, &IID_DTimerEvents);

	m_showSeconds = TRUE ;
	SetBackColor (RGB(255,255,255)) ;
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::~CTimerCtrl - Destructor

CTimerCtrl::~CTimerCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::OnDraw - Drawing function

void CTimerCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{

/*
Get current time.
*/
	SYSTEMTIME st ;
	GetLocalTime (&st) ;

/*
Convert into colon-separated string. If ShowSeconds property is set,
then compose string showing time in seconds.  Otherwise, compose 
string showing time to nearest minute.
*/
	char out [128] ;

	if (m_showSeconds)
	{
		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) ;
	}

/*
Having subscribed to be notified of it, use the stock property 
"BackColor" to draw the background of the control.
*/
	OLE_COLOR oc = GetBackColor ( ) ;
	COLORREF cr = TranslateColor (oc) ; 

	CBrush BackBrush (cr) ;
	pdc->FillRect(rcBounds, &BackBrush);

	pdc->SetBkColor (cr) ;

/*
Use ambient foreground color for text
*/

	oc = AmbientForeColor ( ) ;
	cr = TranslateColor (oc) ;

	pdc->SetTextColor (cr) ;

/*
Draw within bounding rectangle presented by framework.
*/
	pdc->DrawText (out, -1, (LPRECT)&rcBounds, 
		DT_CENTER | DT_VCENTER | DT_SINGLELINE) ;

}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::DoPropExchange - Persistence support

void CTimerCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	PX_Bool (pPX, "ShowSeconds", m_showSeconds) ;
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::OnResetState - Reset control to default state

void CTimerCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl::AboutBox - Display an "About" box to the user

void CTimerCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_TIMER);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CTimerCtrl message handlers

/*
When window is created, start a one-second timer which will be used to refresh the display.
*/

int CTimerCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_TimerID = SetTimer (0, 1000, NULL) ;	
	
	return 0;
}

/*
When the timer signals, invalidate the window to force a repaint.
*/

void CTimerCtrl::OnTimer(UINT nIDEvent) 
{
	Invalidate ( ) ;

/*
If system time has passed alarm time, fire the event signalling the 
container that the alarm has expired.
*/
	if (m_Expiration != 0 && m_Expiration <= GetTickCount( ))
	{
		FireAlarmExpired (m_Expiration) ;
		m_Expiration = 0 ;
	}

}


/*
When the window is destroyed, kill the timer too.
*/

void CTimerCtrl::OnDestroy() 
{
	COleControl::OnDestroy();
	
	KillTimer (m_TimerID) ;	
}


void CTimerCtrl::OnShowSecondsChanged() 
{
	Invalidate( ) ;
	SetModifiedFlag();
}

void CTimerCtrl::SetAlarm(long Delay) 
{
	m_Expiration = GetTickCount( ) + Delay * 1000 ;
}
