Click here to Skip to main content
15,885,216 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: GDIUtils.h $
// $Header: /DevNet/SbjCore/SbjCore/Utils/GDIUtils.h 4     2/24/08 11:47a Steve $
//
//	Copyright � 2006 Tartus, Inc.
// All rights reserved.
//
// The information contained herein is confidential, proprietary to
// Tartus, Inc., and considered a trade secret as defined
// in section 499C of the penal code of the State of California.  Use
// of this information by anyone other than authorized employees of
// Tartus, Inc. is granted only under a written non-disclosure
// agreement, which expressly prescribes the scope and manner of such
// use.
//
// *** Authors ***
//	 Neal O'Hara
//	 John La Porta
//	 Zhimin Lin
//	 Andreas Christmann
//	 Steve Johnson
//	 Frankie Jefferson
//	 Tartus, Inc.
//
// $Revision: 4 $
//
//-----------------------------------------------------------------------------

#pragma once 

namespace SbjCore
{
	namespace Utils
	{
		namespace GDI 
		{

			class DC
			{
			public:
				// Destructor
				virtual ~DC()
				{
					m_pDC = NULL;
				}

			protected:
				explicit DC(CDC* pDC) :
					m_pDC(pDC)
				{
				}

				CDC* GetDC() const
				{
					return m_pDC;
				}

			private:
				DC(const DC&); // not defined
				const DC& operator=(const DC&); // not defined
				
				CDC* m_pDC;
			};
			
			/////////////////////////////////////////////////////////////////////
			
			class StockObject : public DC
			{
			public:
				StockObject(CDC* pDC, int nIndex) : 
					DC(pDC), 
					m_pLastObject(NULL)
				{
					if (GetDC()->GetSafeHdc())
					{
						m_pLastObject = pDC->SelectStockObject(nIndex);
					}
				}

				virtual ~StockObject()
				{
					if (m_pLastObject)
					{
						(void)GetDC()->SelectObject(m_pLastObject);
						m_pLastObject = NULL;
					}
				}

			private:
				StockObject(const StockObject&);                  // not defined
				const StockObject& operator=(const StockObject&); // not defined

				CGdiObject* m_pLastObject;
			};
			
			///////////////////////////////////////////////////////////////////////
			
			template <class T>
			class Object : public DC
			{
			public:
				Object(CDC* pDC, T* pObject);

				virtual ~Object();

			private:
				Object(const Object&); // not defined
				const Object& operator=(const Object&); // not defined

				T* m_pLast;
			};

			template <class T>
			Object<T>::Object(CDC* pDC, T* pObject) : 
				DC(pDC),
				m_pLast(NULL)
			{
				if ((pObject != NULL) && GetDC()->GetSafeHdc())
				{
					m_pLast = pDC->SelectObject(pObject);
				}
			}


			template <class T>
			Object<T>::~Object()
			{
				if (m_pLast)
				{
					(void) GetDC()->SelectObject(m_pLast);
					m_pLast = NULL;
				}
			}
			
			/////////////////////////////////////////////////////////////////////
			
			// just to provide a straight init constructor
			LOGFONT AFX_EXT_API InitLogFont(int nHeight, LPCTSTR lpszFaceName, 
					int nWeight = FW_REGULAR, bool bItalic = false, bool bUnderline = false, bool bStrikeOut = false);
			
			LOGFONT AFX_EXT_API CopyLogFont(const LOGFONT& lf);

			// creates a lfHeight in negative points using CreatePointFont
			LOGFONT AFX_EXT_API InitPointLogFont(int n100thsPoints, LPCTSTR lpszFaceName, 
				int nWeight = FW_REGULAR, bool bItalic = false, bool bUnderline = false, bool bStrikeOut = false);
				
			//////////////////////////////////////////////////
			// CMemDC - memory DC
			//
			// Author: Keith Rule
			// Email:  keithr@europa.com
			// Copyright 1996-2002, Keith Rule
			//
			// You may freely use or modify this code provided this
			// Copyright is included in all derived versions.
			//
			// History - 10/3/97 Fixed scrolling bug.
			//               Added print support. - KR
			//
			//       11/3/99 Fixed most common complaint. Added
			//            background color fill. - KR
			//
			//       11/3/99 Added support for mapping modes other than
			//            MM_TEXT as suggested by Lee Sang Hun. - KR
			//
			//       02/11/02 Added support for CScrollView as supplied
			//             by Gary Kirkham. - KR
			//
			// This class implements a memory Device Context which allows
			// flicker free drawing.

			class CMemDC : public CDC 
			{
			private:       
				CBitmap    m_bitmap;        // Offscreen bitmap
				CBitmap*       m_oldBitmap; // bitmap originally found in CMemDC
				CDC*       m_pDC;           // Saves CDC passed in constructor
				CRect      m_rect;          // Rectangle of drawing area.
				BOOL       m_bMemDC;        // TRUE if CDC really is a Memory DC.
			public:

				CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
				{
					ASSERT(pDC != NULL); 

					// Some initialization
					m_pDC = pDC;
					m_oldBitmap = NULL;
					m_bMemDC = !pDC->IsPrinting();

					// Get the rectangle to draw
					if (pRect == NULL) {
						pDC->GetClipBox(&m_rect);
					} else {
						m_rect = *pRect;
					}

					if (m_bMemDC) {
						// Create a Memory DC
						CreateCompatibleDC(pDC);
						pDC->LPtoDP(&m_rect);

						m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), 
							m_rect.Height());
						m_oldBitmap = SelectObject(&m_bitmap);

						SetMapMode(pDC->GetMapMode());

						SetWindowExt(pDC->GetWindowExt());
						SetViewportExt(pDC->GetViewportExt());

						pDC->DPtoLP(&m_rect);
						SetWindowOrg(m_rect.left, m_rect.top);
					} else {
						// Make a copy of the relevent parts of the current 
						// DC for printing
						m_bPrinting = pDC->m_bPrinting;
						m_hDC       = pDC->m_hDC;
						m_hAttribDC = pDC->m_hAttribDC;
					}

					// Fill background 
					FillSolidRect(m_rect, pDC->GetBkColor());
				}

				~CMemDC()      
				{          
					if (m_bMemDC) {
						// Copy the offscreen bitmap onto the screen.
						m_pDC->BitBlt(m_rect.left, m_rect.top, 
							m_rect.Width(),  m_rect.Height(),
							this, m_rect.left, m_rect.top, SRCCOPY);            

						//Swap back the original bitmap.
						SelectObject(m_oldBitmap);        
					} else {
						// All we need to do is replace the DC with an illegal
						// value, this keeps us from accidentally deleting the 
						// handles associated with the CDC that was passed to 
						// the constructor.              
						m_hDC = m_hAttribDC = NULL;
					}       
				}

				// Allow usage as a pointer    
				CMemDC* operator->() 
				{
					return this;
				}       

				// Allow usage as a pointer    
				operator CMemDC*() 
				{
					return this;
				}
			};

		}
	}
}
//*** Modification History ***
// $Log: /DevNet/SbjCore/SbjCore/Utils/GDIUtils.h $
// 
// 4     2/24/08 11:47a Steve
// 
// 3     2/13/08 10:27a Steve
// Added Named Colors to ColorProp, Bold and Italics to Fonts and error
// checking to ProfiledFileProperty
// 
// 2     10/11/07 11:48a Steve
// Added InitLogFont
// 
// 1     4/02/07 4:57p Steve
// 
// 1     6/09/06 5:16p 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