// TimerUserView.cpp : implementation of the CTimerUserView class
//

#include "stdafx.h"
#include "TimerUser.h"
#include "TimerDlg.h"
#include "TimerUserDoc.h"
#include "TimerUserView.h"
#include "PosSizeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView

IMPLEMENT_DYNCREATE(CTimerUserView, CView)

BEGIN_MESSAGE_MAP(CTimerUserView, CView)
	//{{AFX_MSG_MAP(CTimerUserView)
	ON_COMMAND(ID_FILE_TIMERDIALOGBOX, OnFileTimerdialogbox)
	ON_COMMAND(ID_CONTROLTEST_ADDTOVIEW, OnControltestAddtoview)
	ON_UPDATE_COMMAND_UI(ID_CONTROLTEST_ADDTOVIEW, OnUpdateControltestAddtoview)
	ON_COMMAND(ID_CONTROLTEST_REMOVEFROMVIEW, OnControltestRemovefromview)
	ON_UPDATE_COMMAND_UI(ID_CONTROLTEST_REMOVEFROMVIEW, OnUpdateControltestRemovefromview)
	ON_COMMAND(ID_CONTROLTEST_MOVESIZE, OnControltestMovesize)
	ON_UPDATE_COMMAND_UI(ID_CONTROLTEST_MOVESIZE, OnUpdateControltestMovesize)
	ON_COMMAND(ID_CONTROLTEST_WAIT5SECONDS, OnControltestWait5seconds)
	ON_UPDATE_COMMAND_UI(ID_CONTROLTEST_WAIT5SECONDS, OnUpdateControltestWait5seconds)
	ON_COMMAND(ID_CONTROLTEST_SHOWSECONDSPROPERTY, OnControltestShowsecondsproperty)
	ON_UPDATE_COMMAND_UI(ID_CONTROLTEST_SHOWSECONDSPROPERTY, OnUpdateControltestShowsecondsproperty)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView construction/destruction

CTimerUserView::CTimerUserView()
{
	m_pTimer = NULL ;
}

CTimerUserView::~CTimerUserView()
{
}

BOOL CTimerUserView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView drawing

void CTimerUserView::OnDraw(CDC* pDC)
{
	CTimerUserDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView printing

BOOL CTimerUserView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTimerUserView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTimerUserView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView diagnostics

#ifdef _DEBUG
void CTimerUserView::AssertValid() const
{
	CView::AssertValid();
}

void CTimerUserView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTimerUserDoc* CTimerUserView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTimerUserDoc)));
	return (CTimerUserDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTimerUserView message handlers

void CTimerUserView::OnFileTimerdialogbox() 
{
	CTimerDlg dlg ;

	dlg.DoModal ( ) ;
	
}

void CTimerUserView::OnControltestAddtoview() 
{
	m_pTimer = new CTimer ( ) ;
	
	RECT r = {10, 10, 100, 50} ;

	m_pTimer->Create ("", WS_CHILD | WS_VISIBLE | WS_BORDER, r, this, 1) ;

}

void CTimerUserView::OnUpdateControltestAddtoview(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pTimer == NULL) ;	
}

void CTimerUserView::OnControltestRemovefromview() 
{
	m_pTimer->DestroyWindow ( ) ;
	delete m_pTimer ;
	m_pTimer = NULL ;
}

void CTimerUserView::OnUpdateControltestRemovefromview(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pTimer != NULL) ;	
}


BEGIN_EVENTSINK_MAP(CTimerUserView, CView)
    //{{AFX_EVENTSINK_MAP(CTimerDlg)
	ON_EVENT(CTimerUserView, 1, 1 /* AlarmExpired */, OnAlarmExpiredTimerctrl1, VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CTimerUserView::OnAlarmExpiredTimerctrl1(long TicksNow) 
{
	MessageBeep (0) ;
}

void CTimerUserView::OnControltestMovesize() 
{
	CPosSizeDlg dlg ;

	RECT r ;
	m_pTimer->GetWindowRect (&r) ;
	ScreenToClient (&r) ;

	dlg.m_X = r.left ;
	dlg.m_Y = r.top ;
	dlg.m_Width = r.right - r.left ;
	dlg.m_Height = r.bottom - r.top ;

	if (dlg.DoModal( ) == IDOK) 
	{
		m_pTimer->MoveWindow (dlg.m_X, dlg.m_Y, dlg.m_Width, dlg.m_Height) ;
	}
	
}

void CTimerUserView::OnUpdateControltestMovesize(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pTimer != NULL) ;	
}

void CTimerUserView::OnControltestWait5seconds() 
{
	m_pTimer->SetAlarm (5) ;
}

void CTimerUserView::OnUpdateControltestWait5seconds(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable (m_pTimer != NULL) ;	
}

void CTimerUserView::OnControltestShowsecondsproperty() 
{
	if (m_pTimer->GetShowSeconds( ) == TRUE)
	{
		m_pTimer->SetShowSeconds (FALSE) ;
	}
	else
	{
		m_pTimer->SetShowSeconds (TRUE) ;
	}
}

void CTimerUserView::OnUpdateControltestShowsecondsproperty(CCmdUI* pCmdUI) 
{
	if (m_pTimer == NULL)
	{
		pCmdUI->Enable (FALSE) ;	
		pCmdUI->SetCheck (FALSE) ;
	}
	else
	{
		pCmdUI->Enable (TRUE) ;
		pCmdUI->SetCheck (m_pTimer->GetShowSeconds( )) ;
	}
}
