Click here to Skip to main content
15,881,803 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: ShapesDoc.cpp $
// $Header: /SbjDev/Shapes/ShapesDoc.cpp 5     11/24/08 10:33a Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 5 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "Shapes.h"
#include "ShapesDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace localNS
{

	// Message Handlers /////////////////////////////////////////////////////////////////
	class InsertShapeHandler : public SbjCore::Mvc::CmdMsgHandler
	{
		CString sShapeType;
	public:
		InsertShapeHandler(LPCTSTR lpszShapeType) :
			sShapeType(lpszShapeType)
		{
		
		}
	private:
		virtual bool OnHandleCmd(UINT nID)
		{
			nID;
			bool bRslt = false;
			SbjCore::Mvc::Model::Controller* pModelCtrlr = dynamic_cast<SbjCore::Mvc::Model::Controller*>(GetController());
			SbjCore::Mvc::Model::ItemHandles theItems;
			int nCount = pModelCtrlr->GetItemChildren(SbjCore::Mvc::Model::Controller::GetItemRoot(), theItems);
			if (1 == nCount)
			{
				HANDLE hDrawing = theItems[0];
				HANDLE hItem = pModelCtrlr->CreateItem(sShapeType);
				if (hItem != NULL)
				{
					CString sFmt;
					sFmt.Format(_T("New %s"), sShapeType);
					CString sLabel(pModelCtrlr->AssureUniqueItemAttrValue(hDrawing, _T("label"), (LPCTSTR)sFmt));
					(void)pModelCtrlr->SetItemAttrValue( hItem, _T("label"), (LPCTSTR)sLabel);
					
					DWORD dw = ::GetMessagePos();
					CPoint pt(GET_X_LPARAM((LPARAM)dw), GET_Y_LPARAM((LPARAM)dw));
					CWnd* pWnd = CWnd::FromHandle(GetFocus());
					pWnd->ScreenToClient(&pt);
					CRect r(pt.x, pt.y, pt.x + 350, pt.y + 200);
					SbjCore::Mvc::Model::Rect::SetItemValue(pModelCtrlr, hItem, r);
					
					(void)pModelCtrlr->SetItemAttrValue( hItem, _T("borderRGB"), RGB(0,0,0));
					(void)pModelCtrlr->SetItemAttrValue( hItem, _T("borderWidth"), 5);
					(void)pModelCtrlr->SetItemAttrValue( hItem, _T("fillRGB"), RGB(255,255,255));
					
					bRslt = pModelCtrlr->InsertChild(hItem, hDrawing, NULL);
				}
			}
			return bRslt;
		}
	
		virtual bool OnHandleCmdUI(CCmdUI* pCmdUI)
		{
			bool bRslt = true;
	
			pCmdUI->Enable(true);
	
			return bRslt;
		};
	
	};

}


struct ShapesDocImpl : public XmlMvc::XmlDoc::Controller
{
	HANDLE hDrawing;
	localNS::InsertShapeHandler theInsertRectangleHandler;
	localNS::InsertShapeHandler theInsertEllipseHandler;

	ShapesDocImpl()	:
		hDrawing(NULL),
		theInsertRectangleHandler(_T("Rectangle")),
		theInsertEllipseHandler(_T("Ellipse"))
	{
		SetDocElementName(_T("Shapes"));
		AddHandler(ID_CMDS_NEWRECTANGLE, &theInsertRectangleHandler);
		AddHandler(ID_CMDS_NEWELLIPSE, &theInsertEllipseHandler);
	}
	virtual ~ShapesDocImpl()
	{
	}
	
	virtual BOOL OnNewDocument()
	{
		XmlMvc::XmlDoc::Controller::OnNewDocument();
		HANDLE hItem = CreateItem(_T("Drawing"));
		SetItemAttrValue(hItem, _T("name"), _T("New Drawing"));
		BOOL bRslt = InsertChild(hItem, SbjCore::Mvc::Model::Controller::GetItemRoot(), NULL, false);
		SbjCore::Mvc::Doc::Events::DocModified event(false);
		return bRslt;
	}
	
};

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

IMPLEMENT_DYNCREATE(ShapesDoc, t_Base)

BEGIN_MESSAGE_MAP(ShapesDoc, t_Base)
END_MESSAGE_MAP()

ShapesDoc::ShapesDoc() :
	m_pImpl(new ShapesDocImpl)
{
	SetController(m_pImpl);
}

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

#ifdef _DEBUG
void ShapesDoc::AssertValid() const
{
	t_Base::AssertValid();
}
void ShapesDoc::Dump(CDumpContext& dc) const
{
	t_Base::Dump(dc);
}
#endif //_DEBUG


//*** Modification History ***
// $Log: /SbjDev/Shapes/ShapesDoc.cpp $
// 
// 5     11/24/08 10:33a Steve
// Final changes before MVCChapt1 v2.0.1

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