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

#pragma once

#include "stdafx.h"
#include "ModelController.h"
#include "ModelTypeDefs.h"
#include "ModelEvents.h"
#include "DocEvents.h"
#include "UndoRedoMgr.h"
#include "UndoRedoHandler.h"

namespace localNS
{
	LPCTSTR lpszLeft(_T("left"));
	LPCTSTR lpszTop(_T("top"));
	LPCTSTR lpszRight(_T("right"));
	LPCTSTR lpszBottom(_T("bottom"));
}

namespace SbjCore
{
	namespace Mvc
	{
		namespace Model
		{
			namespace Rect
			{
				void SetItemValue(Controller* pModelCtrlr, const HANDLE hItem, const CRect& r, bool bAddToUndoRedo /*= false*/, bool bFireEvents /*= false*/)
				{
					CRect rAfter = r;
					CRect rBefore(0,0,0,0);
					if (bAddToUndoRedo)
					{
						rBefore = GetItemValue(pModelCtrlr, hItem);
					}
					
					

					pModelCtrlr->SetItemAttrValue(hItem, localNS::lpszLeft, r.left);
					pModelCtrlr->SetItemAttrValue(hItem, localNS::lpszTop, r.top);
					pModelCtrlr->SetItemAttrValue(hItem, localNS::lpszRight, r.right);
					pModelCtrlr->SetItemAttrValue(hItem, localNS::lpszBottom, r.bottom);

					if (bAddToUndoRedo)
					{
						class UndoRedoHandler : public SbjCore::UndoRedo::Handler
						{
							CString sActionName;
							Controller* pModelCtrlr;
							HANDLE hItem;
							CRect rBefore;
							CRect rAfter;
						public:
							UndoRedoHandler(Controller* p, const HANDLE h, const CRect& rB, const CRect& rA) :
							  sActionName(_T("Move")),
								  pModelCtrlr(p),
								  hItem(h),
								  rBefore(rB),
								  rAfter(rA)
							  {
								  if (rBefore.TopLeft() == rAfter.TopLeft())
								  {
									  if (rBefore.BottomRight() != rAfter.BottomRight())
									  {
										  sActionName = _T("Resize");
									  }
								  }
								  else if (rBefore.BottomRight() == rAfter.BottomRight())
								  {
									  sActionName = _T("Resize");
								  }
							  }

							  virtual bool OnHandleUndo()
							  {
								  bool bRslt = true;
								  SetItemValue(pModelCtrlr, hItem, rBefore, false, true);
								  return bRslt;
							  }

							  virtual bool OnHandleRedo()
							  {
								  bool bRslt = true;
								  SetItemValue(pModelCtrlr, hItem, rAfter, false, true);
								  return bRslt;
							  }

							  virtual LPCTSTR OnGetHandlerName() const
							  {
								  return sActionName;
							  }
						};
						UndoRedoHandler* pUndoRedo = new UndoRedoHandler(pModelCtrlr, hItem, rBefore, rAfter);
						pModelCtrlr->GetUndoRedoMgr()->Push(pUndoRedo);
					}

					if (bFireEvents)
					{
						SbjCore::Mvc::Model::Events::ItemChange eventItemChanged(SbjCore::Mvc::Model::Events::EVID_ITEM_CHANGED, pModelCtrlr, hItem, _T("Rectangle"));
						SbjCore::Mvc::Doc::Events::DocModified eventDocModified(true);
					}

				}

				CRect GetItemValue(Controller* pModelCtrlr, const HANDLE hItem)
				{
					CRect r(0,0,0,0);
					if (pModelCtrlr != NULL)
					{
						r.left = pModelCtrlr->GetItemAttrValue(hItem, localNS::lpszLeft);
						r.top = pModelCtrlr->GetItemAttrValue(hItem, localNS::lpszTop);
						r.right = pModelCtrlr->GetItemAttrValue(hItem, localNS::lpszRight);
						r.bottom = pModelCtrlr->GetItemAttrValue(hItem, localNS::lpszBottom);
					}
					return r;
				}
			}
		}
	}	
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/ModelDataTypes.cpp $
// 
// 2     11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 1     11/02/08 12:48a Steve
// 
// 1     10/28/08 4:53p Steve
// 
// 3     10/28/08 11:13a Steve
// 
// 2     10/26/08 9:19a Steve
// 
// 1     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