Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC

The SBJ MVC Framework - The Design View, Responding to Model Change

Rate me:
Please Sign up or sign in to vote.
4.87/5 (22 votes)
19 Mar 2009CPOL13 min read 80.4K   2.4K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
//------------------------------------------------------------------------------
// $Workfile: CmdTargetController.cpp $
// $Header: /SbjDev/SbjCore/CmdTargetController.cpp 3     10/24/08 9:03a Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 3 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "CmdTargetController.h"
#include "CmdMsgHandler.h"
#include "NotifyMsgHandler.h"

#include <stack>

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


namespace SbjCore
{
	namespace Mvc
	{
		/// private implementation struct for the CmdTargetController class
		struct CmdTargetControllerImpl
		{
			CCmdTarget* pCmdTarget;


			/// ctor
			CmdTargetControllerImpl() :
				pCmdTarget(NULL)
			{
			}

			/// dtor
			virtual ~CmdTargetControllerImpl()
			{
			}
			
		};
		
		
		////////////////////////////////////////////////////////		
		
		IMPLEMENT_DYNCREATE(CmdTargetController, Controller)

		
		CmdTargetController::CmdTargetController() :
			m_pImpl(new CmdTargetControllerImpl)
		{
		}

		
		CmdTargetController::~CmdTargetController()
		{
			try 
			{
				delete m_pImpl;
			}
			catch (...)
			{
				ASSERT(false);
			}

		}

		
		void CmdTargetController::SetCmdTarget(CCmdTarget* p)
		{
			m_pImpl->pCmdTarget = p;
		}

		
		CCmdTarget* CmdTargetController::GetCmdTarget() const
		{
			return m_pImpl->pCmdTarget;
		}

		bool CmdTargetController::RoutCmdMsg(UINT nID,			
			int nCode,						
			void* pExtra,					
			AFX_CMDHANDLERINFO* pHandlerInfo)
		{
			return OnRoutCmdMsg(nID, nCode, pExtra, pHandlerInfo);
		}			
		
		
		
		////////////////////////////////////////////////////////////////////////////////////////////////

		/** HandleCmdMsg queries the handler map and routes message if handler is found
		
			This method is called by ControlledCmdTargetT<>::OnCmdMsg before any possible default
			processing by the controlled _baseClass.  It should never be called directly.
			
			\return true if message is handled and no further action should be taken
		*/
		bool CmdTargetController::HandleCmdMsg(UINT nID)
		{
			bool bRslt = false;

			if (m_pImpl->pCmdTarget != NULL)
			{
				try
				{
					CmdMsgHandler* pHandler = dynamic_cast<CmdMsgHandler*>(GetHandler(nID));

					if (pHandler != NULL)
					{
						bRslt = pHandler->HandleCmd(nID);
					}
				}
				catch (...)
				{
					ASSERT(false);
				}
			}
			return bRslt;
		}

		bool CmdTargetController::HandleCmdUIMsg(UINT nID, CCmdUI* pCmdUI)
		{
			bool bRslt = false;
			if (m_pImpl->pCmdTarget != NULL)
			{
				try
				{
						CmdMsgHandler *pHandler = dynamic_cast<CmdMsgHandler*>(GetHandler(nID));

						if (pHandler != NULL)
						{
							bRslt = pHandler->HandleCmdUI(pCmdUI);
						}
				}
				catch (...)
				{
					ASSERT(false);
				}
			}
				
			return bRslt;
		}
		

		bool CmdTargetController::HandleNotifyMsg(NMHDR* pNMHdr, LRESULT* pResult)
		{
			bool bRslt = false;
			if (m_pImpl->pCmdTarget != NULL)
			{
				try
				{
					NotifyMsgHandler *pHandler = dynamic_cast<NotifyMsgHandler*>(GetHandler(pNMHdr->code));

					if (pHandler != NULL)
					{
						bRslt = pHandler->HandleNotify(pNMHdr, pResult);
					}
				}
				catch (...)
				{
					ASSERT(false);
				}
			}

			return bRslt;
		}
		
		bool CmdTargetController::OnRoutCmdMsg(UINT nID,			///< command ID.
			int nCode,							///< command notification code.
			void* pExtra,						///< see CCmdTarget::OnCmdMsg doc
			AFX_CMDHANDLERINFO* pHandlerInfo)	///< see CCmdTarget::OnCmdMsg doc);
		{
			nID;
			nCode;
			pExtra;
			pHandlerInfo;
			
			return false;
		}

	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/CmdTargetController.cpp $
// 
// 3     10/24/08 9:03a Steve

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
SBJ
United States United States
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.

Comments and Discussions