Click here to Skip to main content
15,886,806 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.3K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: DockPaneController.cpp $
// $Header: /SbjDev/SbjCore/DockPaneController.cpp 15    10/14/08 1:12p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 15 $
//
//-----------------------------------------------------------------------------
						 
#include "stdafx.h"
#include "DockPaneController.h"
#include "WndMsgHandler.h"
#include "SetResourceModule.h"

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

namespace localNS
{
	class ToolBar : public CMFCToolBar
	{
	public:	
		virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)	
		{
			pTarget;
			CMFCToolBar::OnUpdateCmdUI((CFrameWnd*) GetOwner (), bDisableIfNoHndler);
		}

		virtual BOOL AllowShowOnList() const
		{	
			return FALSE;	
		}
	};

	class OnCreateHandler : public SbjCore::Mvc::WndMsgHandler
	{
		CALL_DEFAULT_FIRST()

			virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
		{
			wParam;
			LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
			CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;
			*pResult = 0;
			LRESULT lRslt = -1;

			SbjCore::Mvc::DockPaneController* pBar = (SbjCore::Mvc::DockPaneController*)GetController();


			if ((pBar != NULL) && pBar->CreateClient(lpcs, pContext))
			{
				pBar->AdjustLayout();
				lRslt = 0;
			}			 

			return lRslt;
		}
	};
	
	class OnSizeHandler : public SbjCore::Mvc::WndMsgHandler
	{
		CALL_DEFAULT_FIRST()
			virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
		{
			wParam;
			lParam;
			*pResult = 0;
			LRESULT lRslt = 1;

			SbjCore::Mvc::DockPaneController* pBar = (SbjCore::Mvc::DockPaneController*)GetController();

			pBar->AdjustLayout();

			return lRslt;
		}

	};

	class OnEraseBkgndHandler : public SbjCore::Mvc::WndMsgHandler
	{
		virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
		{
			wParam;
			lParam;
			*pResult = 0;
			LRESULT lRslt = 1;

			SbjCore::Mvc::WndController* pCtrlr = GetController();

			CWnd* pWnd = pCtrlr->GetWnd();

			CPaintDC* pDC = (CPaintDC*)pWnd->GetDC();

			CRect rect;
			pWnd->GetClientRect(rect);

			pDC->FillSolidRect(&rect, ::GetSysColor (COLOR_3DSHADOW));

			return lRslt;
		}

	};

	class SetFocusHandler : public SbjCore::Mvc::WndMsgHandler
	{
		//CALL_DEFAULT_FIRST() // comment out to handle message before default

			virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
		{
			wParam;
			lParam;
			*pResult = 0;
			LRESULT lRslt = 1;

			SbjCore::Mvc::WndController* pCtrlr = dynamic_cast<SbjCore::Mvc::WndController*>(GetController());
			if (pCtrlr != NULL)
			{
				CWnd* pChildWnd = pCtrlr->GetWnd()->GetDlgItem(AFX_IDW_PANE_FIRST);
				if (pChildWnd != NULL)
				{
					pChildWnd->SetFocus();
				}
			}
			return lRslt;
		}

	};
}

namespace SbjCore
{
	namespace Mvc
	{
		struct DockPaneControllerImpl
		{
			localNS::ToolBar theToolBar;
			localNS::OnCreateHandler theOnCreateHandler;
			localNS::OnSizeHandler theOnSizeHandler;
			localNS::OnEraseBkgndHandler theOnEraseBkgndHandler;
			localNS::SetFocusHandler theSetFocusHandler;
		
			DockPaneControllerImpl()
			{
			}

			virtual ~DockPaneControllerImpl()
			{
			}
		};

		/////////////////////////////////////////////////////
		
		IMPLEMENT_DYNCREATE(DockPaneController, WndController)

		DockPaneController::DockPaneController() :
			m_pImpl(new DockPaneControllerImpl)
		{
			AddHandler(WM_CREATE, &m_pImpl->theOnCreateHandler);
			AddHandler(WM_SIZE, &m_pImpl->theOnSizeHandler);
			AddHandler(WM_ERASEBKGND, &m_pImpl->theOnEraseBkgndHandler);
			AddHandler(WM_SETFOCUS, &m_pImpl->theSetFocusHandler);
		}

		DockPaneController::~DockPaneController()
		{
			try
			{
				delete m_pImpl;
			}
			catch (...)
			{
				ASSERT(FALSE);
			}
		}
		
		BOOL DockPaneController::CreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
		{
			bool bRslt = false;
			UINT nToolBarImageID = 0;
			HINSTANCE hResInst = GetResourceHandle();
			UINT nToolBarID = GetToolBarID(nToolBarImageID);
			if (nToolBarID > 0)
			{
				if (hResInst == 0)
				{
					hResInst = ::AfxGetResourceHandle();
				}
				
				Utils::SetResourceModule theResModule(hResInst);
				
				m_pImpl->theToolBar.Create (GetWnd(), AFX_DEFAULT_TOOLBAR_STYLE, nToolBarID);
				bRslt = m_pImpl->theToolBar.LoadToolBar (nToolBarID, 0, 0, TRUE );

				m_pImpl->theToolBar.CleanUpLockedImages ();
				bRslt = m_pImpl->theToolBar.LoadBitmap (nToolBarImageID, 0, 0, TRUE );

				m_pImpl->theToolBar.SetPaneStyle(m_pImpl->theToolBar.GetPaneStyle() |
					CBRS_TOOLTIPS | CBRS_FLYBY);

				m_pImpl->theToolBar.SetPaneStyle (
					m_pImpl->theToolBar.GetPaneStyle () & 
					~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));

				m_pImpl->theToolBar.SetOwner (GetWnd());

				// All commands will be routed via this control , not via the parent frame:
				m_pImpl->theToolBar.SetRouteCommandsViaFrame (FALSE);

			}
			return OnCreateClient(lpcs, pContext);
		}

		void DockPaneController::AdjustLayout()
		{
			(void)OnAdjustLayout();
		}


		UINT DockPaneController::GetToolBarID(UINT& nToolBarImageID) const
		{
			return OnGetToolBarID(nToolBarImageID);
		}

		HINSTANCE DockPaneController::GetResourceHandle() const
		{
			return OnGetResourceHandle();
		}
		
		
		int DockPaneController::OnAdjustLayout(int nTop /*= 0*/)
		{
			int nTopOffset = 0;
			
			CWnd* pWnd = GetWnd();
			
			if ((pWnd != NULL) && (pWnd->GetSafeHwnd() != NULL))
			{
				CRect rectClient;
				pWnd->GetClientRect(rectClient);
				
				if (m_pImpl->theToolBar.GetSafeHwnd() != NULL)
				{
					nTopOffset = m_pImpl->theToolBar.CalcFixedLayout (FALSE, TRUE).cy;

					m_pImpl->theToolBar.SetWindowPos (NULL,
						rectClient.left, 
						rectClient.top + nTop, 
						rectClient.Width(),
						nTopOffset,
						SWP_NOACTIVATE | SWP_NOZORDER);
				}
			}								  
			return nTopOffset + nTop;
		}
		
		UINT DockPaneController::OnGetToolBarID(UINT& nToolBarImageID) const
		{
			nToolBarImageID = 0;
			return 0;
		}

		HINSTANCE DockPaneController::OnGetResourceHandle() const
		{
			return 0;
		}

		bool DockPaneController::OnRoutCmdMsg( UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo )
		{
			bool bRslt = false;
			
			CWnd* pChildWnd = GetWnd()->GetDlgItem(AFX_IDW_PANE_FIRST);
			if (pChildWnd != NULL)
			{
				bRslt = pChildWnd->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
			}
			return bRslt;
		}

		BOOL DockPaneController::OnCreateClient( LPCREATESTRUCT lpcs, CCreateContext* pContext )
		{
			lpcs;
			pContext;
			ASSERT(FALSE);
			return false;
		}
	}				 
}
		  
		    
//*** Modification History ***
// $Log: /SbjDev/SbjCore/DockPaneController.cpp $
// 
// 15    10/14/08 1:12p Steve
// Implemented Deletes
// 
// 14    9/23/08 11:23a Steve
// 
// 13    9/23/08 10:37a Steve
// Keyword expansion works and comment format changed to "// "

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