Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Notifying the Document

, 12 Jul 2006
An article on delivering objects to the document in a document/view architecture, using the WM_NOTIFY message.
// NotifierAppView.cpp : implementation of the CNotifierAppView class
//

#include "stdafx.h"
#include "NotifierApp.h"

#include "NotifyObject.h"
#include "NotifyObjects.h"

#include "NotifierAppDoc.h"
#include "NotifierAppView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CNotifierAppView

IMPLEMENT_DYNCREATE(CNotifierAppView, CFormView)

BEGIN_MESSAGE_MAP(CNotifierAppView, CFormView)
	ON_BN_CLICKED(IDC_BUTTON_RUNSTOP, OnBnClickedButtonRunstop)
	ON_BN_CLICKED(IDC_BUTTON_ERROR, OnBnClickedButtonError)
	ON_WM_TIMER()
END_MESSAGE_MAP()

// CNotifierAppView construction/destruction

CNotifierAppView::CNotifierAppView()
	: CFormView(CNotifierAppView::IDD)
{
	m_Timer = 100 ; 
	m_bRunStop = FALSE ; 
}

CNotifierAppView::~CNotifierAppView()
{
}

void CNotifierAppView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST, m_List);
}

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

	return CFormView::PreCreateWindow(cs);
}

void CNotifierAppView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	m_List.InsertColumn( 0, _T("Name") ) ;
	m_List.InsertColumn( 1, _T("Data") ) ;
	CRect rList ;
	m_List.GetClientRect( rList ) ;
	m_List.SetColumnWidth( 0, rList.Width()/4 ) ;
	m_List.SetColumnWidth( 1, rList.Width() - ( rList.Width()/4 ) ) ;
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
}


// CNotifierAppView diagnostics

#ifdef _DEBUG
void CNotifierAppView::AssertValid() const
{
	CFormView::AssertValid();
}

void CNotifierAppView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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


// CNotifierAppView message handlers

void CNotifierAppView::OnBnClickedButtonRunstop()
{
	if ( m_bRunStop == FALSE )
	{
		// enable timer
		GetDlgItem(IDC_BUTTON_RUNSTOP)->SetWindowText( _T("Stop") ) ;
		m_Timer = SetTimer( m_Timer, 1000, NULL ) ;
		m_bRunStop = TRUE ;
	}
	else
	{
		// kill timer
		GetDlgItem(IDC_BUTTON_RUNSTOP)->SetWindowText( _T("Run") ) ;
		KillTimer( m_Timer ) ;
		m_bRunStop = FALSE ;
	}
}

void CNotifierAppView::OnBnClickedButtonError()
{
	GetDocument()->RequestError() ;
}


void CNotifierAppView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	if ( pHint != NULL )
	{
		if ( pHint->IsKindOf( RUNTIME_CLASS( CNotifyObject ) ) != FALSE )
		{
			CNotifyObject * pNotify = (CNotifyObject*)pHint ; 
			switch ( (long)(NotifyObjectType(pNotify)) )
			{
			case NOTIFY_ERROR_OBJECT:
				{
					CErrorObject * Error = (CErrorObject*)pNotify ; // use a reference to the object
					if ( m_List.GetItemCount() >= 20 )
					{
						m_List.DeleteItem(0) ;
					}
					int item = m_List.InsertItem( m_List.GetItemCount(), _T("CErrorObject") ) ;
					m_List.SetItemText( item, 1, Error->get_ErrorMessage() ) ; 
					break ;
				}
			case NOTIFY_INFORMATION_OBJECT:
				{
					CInformationObject Info = *(CInformationObject*)pNotify ; // copy the contents to a local variable
					if ( m_List.GetItemCount() >= 10 )
					{
						m_List.DeleteItem(0) ;
					}
					int item = m_List.InsertItem( m_List.GetItemCount(), _T("CInformationObject") ) ;
					m_List.SetItemText( item, 1, Info.get_InformationMessage() ) ; 
					break ;
				}
			default:
				;
			}
		}
	}
}

void CNotifierAppView::OnTimer(UINT nIDEvent)
{
	if ( nIDEvent == m_Timer ) 
	{
		GetDocument()->RequestInformation() ;
	}

	CFormView::OnTimer(nIDEvent);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Bergman
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 12 Jul 2006
Article Copyright 2006 by Michael Bergman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid