Click here to Skip to main content
15,885,985 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
#include "stdafx.h"
#include "UndoRedoBar.h"
#include "UndoRedoMenu.h"
#include "UndoRedoButton.h"
#include "UndoRedoListBox.h"
#include "WndController.h"
#include "WndMsgHandler.h"

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


namespace SbjCore
{
 	namespace Mvc
	{
		struct UndoRedoBarImpl : public WndController
		{
			UndoRedoListBox	theListBox;
			CRect			rectLabel;
			int				nLabelHeight;
			CString			sLabel;
			
			class UndoRedoBarCreateHandler : 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::UndoRedoBarImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoBarImpl*>(GetController());

					if (pCtrlr != NULL)
					{
						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pCtrlr->GetWnd());	

						if (pBar != NULL)
						{
							CFont* pMenuFont = (CFont*)&CMFCMenuBar::GetMenuFont();

							CRect rectDummy (0, 0, 0, 0);

							pCtrlr->theListBox.Create (WS_VISIBLE | WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY | WS_VSCROLL | LBS_MULTIPLESEL, 
								rectDummy, pBar, 1);

							pCtrlr->theListBox.ModifyStyle(WS_BORDER, 0);

							pCtrlr->theListBox.SetFont(pMenuFont);

							CStringList& list = UndoRedoButton::GetUndoRedoList();

							for (POSITION pos = list.GetHeadPosition(); pos != NULL;)
							{
								pCtrlr->theListBox.AddString(list.GetNext(pos));
							}
						}
					}

					return lRslt;
				}

			} theUndoRedoBarCreateHandler;
			friend UndoRedoBarCreateHandler;
			
			class UndoRedoBarSizeHandler : 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::UndoRedoBarImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoBarImpl*>(GetController());

					if (pCtrlr != NULL)
					{
						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pCtrlr->GetWnd());	

						if (pBar != NULL)
						{
							CRect rectClient;
							pBar->GetClientRect(rectClient);

							CFont* pMenuFont = (CFont*) &CMFCMenuBar::GetMenuFont();

							CClientDC dc(pBar);
							CFont* pOldFont = dc.SelectObject(pMenuFont);

							TEXTMETRIC tm;
							dc.GetTextMetrics (&tm);

							pCtrlr->nLabelHeight = tm.tmHeight + 2;

							dc.SelectObject (pOldFont);


							pCtrlr->rectLabel = rectClient;
							pCtrlr->rectLabel.top = (pCtrlr->rectLabel.bottom - pCtrlr->nLabelHeight);
							pCtrlr->rectLabel.top -= (pCtrlr->rectLabel.top % tm.tmHeight);

							CRect rectList = rectClient;
							rectList.bottom = pCtrlr->rectLabel.top;

							pCtrlr->theListBox.MoveWindow(rectList);
						}
					}
					return lRslt;
				}

			} theUndoRedoBarSizeHandler;
			friend UndoRedoBarSizeHandler;
			
			UndoRedoBarImpl()
			{
				AddHandler(WM_CREATE, &theUndoRedoBarCreateHandler);
				AddHandler(WM_SIZE, &theUndoRedoBarSizeHandler);
			}
			
			virtual ~UndoRedoBarImpl()
			{
			}
		};
		
		/////////////////////////////////////////////////////////////////////////////

		IMPLEMENT_SERIAL(UndoRedoBar, CMFCPopupMenuBar, 1)

		UndoRedoBar::UndoRedoBar() :
			m_pImpl(new UndoRedoBarImpl)
		{
			SetController(m_pImpl);
		}

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

		/////////////////////////////////////////////////////////////////////////////
		// UndoRedoBar message handlers

		void UndoRedoBar::OnFillBackground (CDC* pDC)
		{
			ASSERT_VALID (pDC);

			CRect rectClient;
			GetClientRect (rectClient);

			::FillRect (pDC->GetSafeHdc (), &rectClient, ::GetSysColorBrush (COLOR_WINDOW));

			CBrush br;
			br.CreateSolidBrush(RGB(0xF8, 0xF8, 0xF8));
			// Draw label:
			::FillRect(pDC->GetSafeHdc (), &m_pImpl->rectLabel, br);

			CFont* pOldFont = pDC->SelectObject ((CFont*) &CMFCMenuBar::GetMenuFont ());
			pDC->SetBkMode (TRANSPARENT);
			pDC->SetTextColor (::GetSysColor (COLOR_BTNTEXT));

			CRect rectText = m_pImpl->rectLabel;
			rectText.OffsetRect(CPoint(0,4));
			pDC->DrawText (m_pImpl->sLabel, rectText, DT_CENTER | DT_VCENTER);

			pDC->SelectObject (pOldFont);
		}

		CSize UndoRedoBar::CalcSize (BOOL /*bVertDock*/)
		{
			return CSize (160, 115);
		}

		UndoRedoButton* UndoRedoBar::GetUndoRedoButton () const
		{
			// Get parent button:
			UndoRedoButton* pUndoRedoButton = NULL;

			UndoRedoMenu* pParentMenu = DYNAMIC_DOWNCAST(UndoRedoMenu, GetParent());
			if (pParentMenu != NULL)
			{
				pUndoRedoButton = DYNAMIC_DOWNCAST(UndoRedoButton, pParentMenu->GetParentButton());
			}

			return pUndoRedoButton;
		}

		void UndoRedoBar::DoUndo ()
		{
			UndoRedoButton* pUndoRedoButton = GetUndoRedoButton();

			pUndoRedoButton->SetCount(m_pImpl->theListBox.GetSelCount());
			GetOwner()->PostMessage (WM_COMMAND, pUndoRedoButton->m_nID);

			CMFCPopupMenu* pMenu = (CMFCPopupMenu*)GetParent();

			pMenu->CloseMenu ();
		}

		void UndoRedoBar::SetLabelCount(const int nCount)
		{
			UndoRedoButton* pUndoRedoButton = GetUndoRedoButton();
			
			if (ID_EDIT_UNDO == pUndoRedoButton->m_nID)
			{
				m_pImpl->sLabel.Format(_T("Undo %d Actions"), nCount);
			}

			if (ID_EDIT_REDO == pUndoRedoButton->m_nID)
			{
				m_pImpl->sLabel.Format(_T("Redo %d Actions"), nCount);
			}

			if (GetSafeHwnd () != NULL)
			{
				InvalidateRect (m_pImpl->rectLabel);
				UpdateWindow ();
			}
		}

	}
}


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