Click here to Skip to main content
15,886,724 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.6K   2.4K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
// UndoRedoListBox.cpp : implementation file
//

#include "stdafx.h"
#include "UndoRedoListBox.h"
#include "WndController.h"
#include "WndMsgHandler.h"
#include "UndoRedoBar.h"

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



namespace SbjCore
{
	namespace Mvc
	{
		struct UndoRedoListBoxImpl : public SbjCore::Mvc::WndController
		{
			SbjCore::Mvc::UndoRedoBar* pTheBar;
			UINT nTheID;

			class LButtonUpHandler : public SbjCore::Mvc::WndMsgHandler
			{
				virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
				{
					wParam;
					CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
					*pResult = 0;
					LRESULT lRslt = 0; // let the default process after us

					SbjCore::Mvc::UndoRedoListBoxImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoListBoxImpl*>(GetController());

					CListBox* pListBox = dynamic_cast<CListBox*>(pCtrlr->GetWnd());

					BOOL bOutside;
					pListBox->ItemFromPoint(pt, bOutside);

					if (!bOutside)
					{
						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pListBox->GetParent());
						ASSERT_VALID(pBar);
						if (pBar != NULL)						
						{
							pBar->DoUndo();
						}
					}
					return lRslt;
				}
			} theLButtonUpHandler;
			friend LButtonUpHandler;


			class MouseMoveHandler : public SbjCore::Mvc::WndMsgHandler
			{

				CALL_DEFAULT_FIRST() // comment out to handle message before default

					virtual LRESULT OnHandleWndMsg(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
				{
					wParam;
					CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
					*pResult = 1;
					LRESULT lRslt = 1;

					SbjCore::Mvc::UndoRedoListBoxImpl* pCtrlr = dynamic_cast<SbjCore::Mvc::UndoRedoListBoxImpl*>(GetController());

					CListBox* pListBox = dynamic_cast<CListBox*>(pCtrlr->GetWnd());

					BOOL bOutside;
					int nItem = pListBox->ItemFromPoint(pt, bOutside);

					if (!bOutside)
					{
						pListBox->SelItemRange (FALSE, nItem + 1, pListBox->GetCount () - 1);

						if (nItem == 0)
						{
							pListBox->SetSel(0);
						}
						else
						{
							pListBox->SelItemRange(TRUE, 0, nItem);
						}

						SbjCore::Mvc::UndoRedoBar* pBar = dynamic_cast<SbjCore::Mvc::UndoRedoBar*>(pListBox->GetParent());
						ASSERT_VALID(pBar);
						if (pBar != NULL)
						{
							pCtrlr->pTheBar->SetLabelCount(nItem + 1);
						}
					}

					return lRslt;
				}

			} theMouseMoveHandler;
			friend MouseMoveHandler;
			
			UndoRedoListBoxImpl() :
				pTheBar(NULL),
				nTheID(0)
			{
				AddHandler(WM_LBUTTONUP, &theLButtonUpHandler);
				AddHandler(WM_MOUSEMOVE, &theMouseMoveHandler);
			}
			virtual ~UndoRedoListBoxImpl()
			{
			}
		};
		
		///////////////////////////////////////////////////////////////////		
		
		UndoRedoListBox::UndoRedoListBox() :
			m_pImpl(new UndoRedoListBoxImpl)
		{
			SetController(m_pImpl);
		}

		UndoRedoListBox::~UndoRedoListBox()
		{
			try
			{
				delete m_pImpl;
			}
			catch(...)
			{
				ASSERT(FALSE);
			}
		}
		
		BOOL UndoRedoListBox::PreTranslateMessage( MSG* pMsg )
		{
			if (pMsg->message == WM_LBUTTONDOWN)
			{
				return TRUE;
			}

			return CListBox::PreTranslateMessage(pMsg);
		}
	}
}


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