Click here to Skip to main content
15,891,905 members
Articles / Desktop Programming / MFC

The SBJ MVC Framework - The Model, from Abstraction to Realization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
20 Mar 2009CPOL19 min read 109.9K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: WndController.cpp $
// $Header: /SbjDev/SbjCore/WndController.cpp 5     10/16/08 2:41a Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 5 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "WndController.h"
#include "WndMsgHandler.h"

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

namespace localNS
{
	class ContextMenuHandler : public SbjCore::Mvc::WndMsgHandler
	{
		virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
		{
			wParam;
			*pResult = 0;
			LRESULT lRslt = 0;
	
			SbjCore::Mvc::WndController* pCtrlr = GetController();
	
			CWnd* pWnd = pCtrlr->GetWnd();
			
			CMenu theMenu;

			theMenu.CreatePopupMenu();
			
			

			pCtrlr->PrepareCtxMenu(theMenu);

			if (theMenu.GetMenuItemCount() > 0)
			{
				CWinAppEx* pApp = dynamic_cast<CWinAppEx*>(AfxGetApp());
				ASSERT(pApp != NULL);
				
				if (pApp != NULL)
				{
					CContextMenuManager* pMgr = pApp->GetContextMenuManager();
					
					if (pMgr != NULL)
					{
						pMgr->ShowPopupMenu(theMenu.GetSafeHmenu(), LOWORD(lParam), HIWORD(lParam), pWnd, TRUE); 
					//	*pResult = 1;
						lRslt = 1;
					}
				}
			}

			return lRslt;
		}
	
	};

}

namespace SbjCore
{
	namespace Mvc
	{
		struct WndControllerImpl
		{
			localNS::ContextMenuHandler theContextMenuHandler;
			
			WndControllerImpl()
			{
			}
			virtual ~WndControllerImpl()
			{
			}
		};
		
		/////////////////////////////////////////////////////////////////////
		
		IMPLEMENT_DYNCREATE(WndController, CmdTargetController)
		
		WndController::WndController() :
			m_pImpl(new WndControllerImpl)
		{
			AddHandler(WM_CONTEXTMENU, &m_pImpl->theContextMenuHandler);
		}
		
		WndController::~WndController()
		{
			try
			{
				delete m_pImpl;
			}
			catch(...)
			{
				ASSERT(FALSE);
			}
		
		}
		
		
		void WndController::SetWnd(CWnd* p)
		{
			SetCmdTarget(static_cast<CCmdTarget*>(p));
		}

		CWnd* WndController::GetWnd() const
		{
			return static_cast<CWnd*>(GetCmdTarget());	
		}

		/** HandleWndMsgPreDefault queries the handler map and routes message if handler is found.
		
			This method is called by ControlledWndT<>::OnWndMsgPreDefault 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 WndController::HandleWndMsg(UINT message,		///< the Windows message ID
			WPARAM wParam,									///< wparam value for the message
			LPARAM lParam,									///< lparam value for the message
			LRESULT * pResult)								///< if not NULL, the return of the message
		{
			BOOL bRslt = FALSE;
			
			CWnd* pWnd = GetWnd();

			if ((pWnd != NULL) && (pWnd->GetSafeHwnd() != NULL))
			{
				try
				{
					WndMsgHandler *pHandler = dynamic_cast<WndMsgHandler *>(Controller::GetHandler(message));

					if (pHandler != NULL)
					{
						bRslt = (BOOL)pHandler->HandleWndMsg(wParam, lParam, pResult);
					}
				}
				catch (...)
				{
					ASSERT(FALSE);
				}
			}

			return bRslt;
		}

		bool WndController::CallDefaultFirst(UINT message)
		{
			BOOL bRslt = false;

			CWnd* pWnd = GetWnd();

			if ((pWnd != NULL) && (pWnd->GetSafeHwnd() != NULL))
			{
				try
				{
					WndMsgHandler *pHandler = dynamic_cast<WndMsgHandler *>(Controller::GetHandler(message));

					if (pHandler != NULL)
					{
						bRslt = (BOOL)pHandler->CallDefaultFirst();
					}
				}
				catch (...)
				{
					ASSERT(FALSE);
				}
			}

			return bRslt;
		}

		/// calls virtual OnPrepareCtxMenu(ctxMenu)
		SbjCore::Utils::Menu::ItemRange WndController::PrepareCtxMenu(CMenu& ctxMenu)
		{
			return OnPrepareCtxMenu(ctxMenu);
		}

		SbjCore::Utils::Menu::ItemRange WndController::OnPrepareCtxMenu(CMenu& ctxMenu)
		{
			ctxMenu;
			return SbjCore::Utils::Menu::ItemRange();
		}

		bool WndController::OnRoutCmdMsg( UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo )
		{
			bool bRslt = false;
			CFrameWnd* pFrame = (CFrameWnd*)AfxGetMainWnd();
			if (pFrame != NULL)
			{
				CDocument* pDoc = pFrame->GetActiveDocument();
				if (pDoc != NULL)
				{
					bRslt = pDoc->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
				}
			}
			return bRslt;
		}
		
	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/WndController.cpp $
// 
// 5     10/16/08 2:41a Steve
// Ready for publishing
// 
// 4     10/14/08 1:12p Steve
// Implemented Deletes

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