Click here to Skip to main content
15,892,517 members
Articles / Desktop Programming / MFC

Notifying the Document

Rate me:
Please Sign up or sign in to vote.
4.36/5 (11 votes)
12 Jul 2006CPOL6 min read 63.9K   1.2K   35  
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions