Click here to Skip to main content
15,885,077 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.2K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: PropGridController.cpp $
// $Header: /SbjDev/SbjCore/PropGridController.cpp 9     11/12/08 2:22p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 9 $
//
//-----------------------------------------------------------------------------

#include "StdAfx.h"
#include "resource.h"
#include "PropGridController.h"
#include "PropMap.h"
#include "EventMgr.h"
#include "ModelEvents.h"
#include "ModelTypeDefs.h"
#include "ModelController.h"
#include "CmdMsgHandler.h"

namespace localNS
{
	typedef std::map<CString, SbjCore::Mvc::PropGrid::PropMap*> PropMaps;
	PropMaps theMaps;
	SbjCore::Mvc::PropGrid::PropMap* pTheCurMap = NULL;
	
	// Event Handlers /////////////////////////////////////////////////////////

	class SelChangedEventHandler : public SbjCore::EventMgr::EventHandler
	{
	public:
		SelChangedEventHandler() :
		  SbjCore::EventMgr::EventHandler(SbjCore::Mvc::Model::Events::EVID_SELITEM_CHANGED)
		  {
		  }
	private:		
		virtual void OnHandle(SbjCore::EventMgr::Event* pEvent)
		{
			SbjCore::Mvc::Model::Events::SelItemsChanged* pTheEvent = dynamic_cast<SbjCore::Mvc::Model::Events::SelItemsChanged*>(pEvent);
			const SbjCore::Mvc::Model::Controller* pModelCtrlr = pTheEvent->pCtrlr;
			SbjCore::Mvc::Model::ItemHandles selItems;
			pModelCtrlr->GetSelectedItems(selItems);
			int nCount = selItems.size();
			if (1 == nCount)
			{
				HANDLE hItem = selItems[0];

				CString sName(pModelCtrlr->GetItemTypeName(hItem));
				SbjCore::Mvc::PropGrid::PropMap* pPropMap = theMaps[sName];
				if (pPropMap != NULL)
				{
					if (pPropMap != pTheCurMap)
					{
						pPropMap->LoadPropGrid(hItem);
						pTheCurMap = pPropMap;
					}
					else
					{
						pPropMap->Update(hItem);
					}
				}
				else
				{
					if (pTheCurMap != NULL)
					{
						pTheCurMap->GetPropGridCtrl()->RemoveAll();
						pTheCurMap->GetPropGridCtrl()->Invalidate();
						pTheCurMap = NULL;
					}
				}
			}
			else // nCount == 0 || nCount > 1
			{
				if (pTheCurMap != NULL)
				{
					pTheCurMap->GetPropGridCtrl()->RemoveAll();
					pTheCurMap->GetPropGridCtrl()->Invalidate();
					pTheCurMap = NULL;
				}
			}

		}
	};


	class ModelChangedEventHandler : public SbjCore::EventMgr::EventHandler
	{
	public:
		ModelChangedEventHandler() :
		  SbjCore::EventMgr::EventHandler(SbjCore::Mvc::Model::Events::EVID_ITEM_CHANGED)
		  {
		  }

	private:		
		virtual void OnHandle(SbjCore::EventMgr::Event* pEvent)
		{
			if (pTheCurMap != NULL)
			{
				SbjCore::Mvc::Model::Events::ItemChange* pTheEvent = dynamic_cast<SbjCore::Mvc::Model::Events::ItemChange*>(pEvent);
				pTheCurMap->Update(pTheEvent->hItem);
			}
		}
	};

	// Message Handlers ///////////////////////////////////////////////////////

	class ExpandAllHandler : public SbjCore::Mvc::CmdMsgHandler
	{
		virtual bool OnHandleCmd(UINT nID)
		{
			nID;
			bool bRslt = true;
			SbjCore::Mvc::PropGrid::Controller* pCtrlr = dynamic_cast<SbjCore::Mvc::PropGrid::Controller*>(GetController());

			CMFCPropertyGridCtrl* pGrid = pCtrlr->GetPropGridCtrl();

			pGrid->ExpandAll();

			return bRslt;
		}

		virtual bool OnHandleCmdUI(CCmdUI* pCmdUI)
		{
			bool bRslt = true;

			// TODO: Modify for handler specific code
			pCmdUI->Enable(true);

			return bRslt;
		};

	};

	class SortHandler : public SbjCore::Mvc::CmdMsgHandler
	{
		virtual bool OnHandleCmd(UINT nID)
		{
			nID;
			bool bRslt = true;
			SbjCore::Mvc::PropGrid::Controller* pCtrlr = dynamic_cast<SbjCore::Mvc::PropGrid::Controller*>(GetController());

			CMFCPropertyGridCtrl* pGrid = pCtrlr->GetPropGridCtrl();

			pGrid->SetAlphabeticMode(!pGrid->IsAlphabeticMode());
	
			return bRslt;
		}
	
		virtual bool OnHandleCmdUI(CCmdUI* pCmdUI)
		{
			bool bRslt = true;
	
			SbjCore::Mvc::PropGrid::Controller* pCtrlr = dynamic_cast<SbjCore::Mvc::PropGrid::Controller*>(GetController());

			CMFCPropertyGridCtrl* pGrid = pCtrlr->GetPropGridCtrl();

			pCmdUI->SetCheck(pGrid->IsAlphabeticMode());
	
			return bRslt;
		};
	
	};
}

namespace SbjCore
{
	namespace Mvc
	{
		namespace PropGrid
		{
			struct ControllerImpl
			{
				DWORD dwStyle;
				
				localNS::SelChangedEventHandler theSelChangedEventHandler;
				localNS::ModelChangedEventHandler theModelChangedEventHandler;

				localNS::ExpandAllHandler theExpandAllHandler;			
				localNS::SortHandler theSortHandler;			

				ControllerImpl(DWORD dw) :
				dwStyle(dw)
				{
				}
				virtual ~ControllerImpl()
				{
				}
			};

			///////////////////////////////////////////////////////////////////////////

			IMPLEMENT_DYNCREATE(Controller, WndController)

			Controller::Controller(DWORD dwStyle /*= DEFAULT_PROPGRID_STYLES*/) :
				m_pImpl(new ControllerImpl(dwStyle))
			{
				AddHandler(ID_EXPAND_ALL, &m_pImpl->theExpandAllHandler);
				AddHandler(ID_SORTPROPERTIES, &m_pImpl->theSortHandler);
			}

			Controller::~Controller(void)
			{
				try
				{
					delete m_pImpl;
				}
				catch(...)
				{
					ASSERT(FALSE);
				}
			}

			CMFCPropertyGridCtrl* Controller::GetPropGridCtrl() const
			{
				return dynamic_cast<CMFCPropertyGridCtrl*>(CmdTargetController::GetCmdTarget());
			}

			void Controller::SetStyle( const DWORD dwStyle )
			{
				m_pImpl->dwStyle = dwStyle;
			}

			DWORD Controller::GetStyle() const
			{
				return m_pImpl->dwStyle;
			}

			void Controller::OnInitialize()
			{
				CMFCPropertyGridCtrl* pPropGrid = GetPropGridCtrl();
				pPropGrid->EnableHeaderCtrl(FALSE);
				//pPropGrid->EnableDescriptionArea();
				pPropGrid->SetVSDotNetLook();
				//pPropGrid->MarkModifiedProperties();
			}

			void Controller::PropertyChanged( CMFCPropertyGridProperty* pProp ) const
			{
				OnPropertyChanged(pProp);
			}

			void Controller::OnPropertyChanged( CMFCPropertyGridProperty* pProp ) const
			{
				SbjCore::Mvc::Model::Controller* pModelCtrlr = dynamic_cast<SbjCore::Mvc::Model::Controller*>(SbjCore::Mvc::Model::GetCurController());
				HANDLE hItem = localNS::pTheCurMap->GetModelItemHandle();
				int nAttrIndex = (int)pProp->GetData();
				CString sAttrName = pModelCtrlr->GetItemAttrName(hItem, nAttrIndex);
				pModelCtrlr->SetItemAttrValue(hItem, (LPCTSTR)sAttrName, pProp->GetValue(), true, true);
			}

			void Controller::AddPropMap( LPCTSTR lpszItemType, PropMap* pPropMap )
			{
				pPropMap->SetPropGridCtrl(GetPropGridCtrl());
				localNS::theMaps[lpszItemType] = pPropMap;

			}
		}
	}
}
//*** Modification History ***
// $Log: /SbjDev/SbjCore/PropGridController.cpp $
// 
// 9     11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 8     9/23/08 11:53a Steve
// disabled description

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