Click here to Skip to main content
15,881,588 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 109K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
#include "stdafx.h"
#include "Shapes.h"

#include "ShapesDoc.h"
#include "ShapesView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace localNS
{
	// DesignViewItems //////////////////////////////////////
	
	class ShapeItem : public SbjCore::Mvc::DesignView::Item
	{
	public:
		ShapeItem() :
			r(0,0,0,0),
			nBorderWidth(1),
			clrBorder(RGB(0,0,0)),
			clrFill((COLORREF)-1)
		{
		}
		virtual ~ShapeItem()
		{
		}
	protected:
		CRect r;
		int nBorderWidth;		
		COLORREF clrBorder;
		COLORREF clrFill;
	private:
		DECLARE_DYNCREATE(ShapeItem)
		
		virtual void OnDraw(CDC* pDC)
		{
			pDC;
			HANDLE hItem = GetModelItemHandle();
			SbjCore::Mvc::Model::Controller* pModelCtrlr = SbjCore::Mvc::Model::GetCurController();
			r = SbjCore::Mvc::Model::Rect::GetItemValue(pModelCtrlr, hItem);
			nBorderWidth = pModelCtrlr->GetItemAttrValue(hItem, _T("borderWidth"));
			clrBorder = pModelCtrlr->GetItemAttrValue(hItem, _T("borderRGB"));
			clrFill = pModelCtrlr->GetItemAttrValue(hItem, _T("fillRGB"));

		}
		virtual bool OnIsTrackable() const
		{
			return true;
		}
		
	};
	IMPLEMENT_DYNCREATE(ShapeItem, SbjCore::Mvc::DesignView::Item)

	
	
	class RectangleItem : public ShapeItem
	{
		DECLARE_DYNCREATE(RectangleItem)
		virtual void OnDraw(CDC* pDC)
		{
			ShapeItem::OnDraw(pDC);
			
			CPen pen(PS_SOLID, nBorderWidth, clrBorder);
			SbjCore::Utils::GDI::Object<CPen> objPen(pDC, &pen);
			LOGBRUSH lb = {BS_HOLLOW, 0, 0};
			CBrush brHollow;
			brHollow.CreateBrushIndirect(&lb);
			SbjCore::Utils::GDI::Object<CBrush> objBrush(pDC, &brHollow);

			if (clrFill != (COLORREF)-1)
			{
				CBrush brush;
				brush.CreateSolidBrush(clrFill);
				pDC->FillRect(r, &brush);
			}
			pDC->Rectangle(r);
		}
	};
	IMPLEMENT_DYNCREATE(RectangleItem, ShapeItem)


	class EllipseItem : public ShapeItem
	{
		DECLARE_DYNCREATE(EllipseItem)
		virtual void OnDraw(CDC* pDC)
		{
			ShapeItem::OnDraw(pDC);
			CPen pen(PS_SOLID, nBorderWidth, clrBorder);
			SbjCore::Utils::GDI::Object<CPen> objPen(pDC, &pen);
			CBrush br;
			br.CreateSolidBrush(clrFill);
			SbjCore::Utils::GDI::Object<CBrush> objBrush(pDC, &br);

			pDC->Ellipse(r);
		}
	};
	IMPLEMENT_DYNCREATE(EllipseItem, ShapeItem)
}

struct ShapesViewImpl : public SbjCore::Mvc::DesignView::Controller
{
	ShapesViewImpl()
	{
	 // must have a root for the design but it isn't drawn
		MapItemRTCToModelTypeName(_T("Drawing"), RUNTIME_CLASS(SbjCore::Mvc::DesignView::Item));
		MapItemRTCToModelTypeName(_T("Rectangle"), RUNTIME_CLASS(localNS::RectangleItem));
		MapItemRTCToModelTypeName(_T("Ellipse"), RUNTIME_CLASS(localNS::EllipseItem));
	}
	
	virtual ~ShapesViewImpl()
	{
	}
	
	virtual SbjCore::Utils::Menu::ItemRange OnPrepareCtxMenu( CMenu& ctxMenu )
	{
		SbjCore::Utils::Menu::ItemRange menuItems;

		menuItems.nFirst = (int)ctxMenu.GetMenuItemCount()-1;
		menuItems.nLast = menuItems.nFirst; 

		if (SbjCore::Utils::Menu::InsertSeparator(ctxMenu, menuItems.nLast))
		{
			menuItems.nLast++;
		}

		(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWELLIPSE, _T("New &Ellipse"));
		(void)ctxMenu.InsertMenu(0, MF_BYPOSITION, ID_CMDS_NEWRECTANGLE, _T("New &Rectangle"));
		

		menuItems = SbjCore::Mvc::DesignView::Controller::OnPrepareCtxMenu(ctxMenu);

		return menuItems;
	}
	
	
};

// ShapesView ///////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(ShapesView, t_Base)

BEGIN_MESSAGE_MAP(ShapesView, t_Base)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &t_Base::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &t_Base::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &ShapesView::OnFilePrintPreview)
END_MESSAGE_MAP()

ShapesView::ShapesView() :
	m_pImpl(new ShapesViewImpl)
{
	SetController(m_pImpl);
}

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

BOOL ShapesView::PreCreateWindow(CREATESTRUCT& cs)
{
	return t_Base::PreCreateWindow(cs);
}

void ShapesView::OnDraw(CDC* pDC)
{
	ShapesDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
	{
		return;
	}
	m_pImpl->Draw(pDC);
}
												

void ShapesView::OnFilePrintPreview()
{
	AFXPrintPreview(this);
}

BOOL ShapesView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void ShapesView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	pDC;
}

void ShapesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

void ShapesView::OnRButtonUp(UINT nFlags, CPoint point)
{
	nFlags;
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void ShapesView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	pWnd;
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}


// ShapesView diagnostics

#ifdef _DEBUG
void ShapesView::AssertValid() const
{
	t_Base::AssertValid();
}

void ShapesView::Dump(CDumpContext& dc) const
{
	t_Base::Dump(dc);
}

ShapesDoc* ShapesView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ShapesDoc)));
	return (ShapesDoc*)m_pDocument;
}

#endif //_DEBUG


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