Click here to Skip to main content
15,886,067 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.3K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: TreeController.h $
// $Header: /SbjDev/SbjCore/TreeController.h 10    11/12/08 2:22p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 10 $
//
//-----------------------------------------------------------------------------

#pragma once

#include "WndController.h"
#include "MenuUtils.h"


DECLARE_REGISTERED_CLIPFORMAT(CFHTREEITEM, {E0446724-23E0-4667-927F-98FC098010DD})

#define DEFAULT_TREE_STYLES (WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS)

namespace SbjCore
{
	namespace DragNDrop
	{ 
		class DataHandler;
	};

	namespace Mvc
	{
		namespace Model
		{
			class Controller;
		}
	
		namespace TreeView
		{
			class ItemController;
			class ItemDisplayModel;

			struct ControllerImpl;

			class AFX_EXT_CLASS Controller : public WndController
			{
				DECLARE_DYNCREATE(Controller)
			public:
				Controller();

				Controller(const UINT nImageListID, const int nExpandLevels, const DWORD dwStyle = DEFAULT_TREE_STYLES);

				virtual ~Controller(void);

			public:
				void MapItemRTCToModelTypeName(LPCTSTR lpszName, CRuntimeClass* pRTC);

				ItemController* CreateItem(const SbjCore::Mvc::Model::Controller* pModelCtrlr, HANDLE hModelItem);

				void LoadTree(const SbjCore::Mvc::Model::Controller* pModelCtrlr, HANDLE hItem, HTREEITEM htiAfter = TVI_LAST);

				void InsertTree(const SbjCore::Mvc::Model::Controller* pModelCtrlr, HANDLE hItem, HTREEITEM htiParent, HTREEITEM htiAfter = TVI_LAST);

				ItemController* GetItemController(HANDLE hItem);

			public: // accessors

				// returns the controlled CTreeCtrl
				CTreeCtrl* GetTreeCtrl() const;

				void SetStyle(const DWORD dwStyle);
				DWORD GetStyle() const;

				void SetImageListID(const UINT n);
				UINT GetImageListID() const;

				bool UsingResIDs() const;
				void SetUsingResIDs(const bool b);

				void SetExpandLevels(const int n);
				int GetExpandLevels() const;

			public: // virtual

				virtual HTREEITEM InsertItem(ItemController* pItem, HTREEITEM htiParent = TVI_ROOT, 
					HTREEITEM htiAfter = TVI_LAST, bool bExpandParent = true);

				virtual int ImageIndexFromResID(UINT nResID) const;

			public: 		
				// select item reference by ItemController
				void SelectItem(ItemController* pItem);
				// delete item reference by ItemController
				void DeleteItem(ItemController* pItem);
				// delete item's children (recursive)
				void DeleteChildren(ItemController* pItem);
				// delete all items and their controllers
				void DeleteAllItems();

				// sort the children of the htiParent item
				bool SortChildren(HTREEITEM htiParent, PFNTVCOMPARE lpfnCompare = NULL);

				// adds a DropTargetHandler for a given clip format
				void AddDropTargetHandler(CLIPFORMAT cf, DragNDrop::DataHandler* p);

				// called to block/unblock notification messages sent by tree classes during nested operations.
				void BlockNotify(const bool bBlock = true);

				// Returns notify blocked state
				bool IsBlocked() const;

				// return the ItemController associated with the HTREEITEM
				ItemController* GetItem(HTREEITEM hti) const;

				// return the selected ItemController if any
				ItemController* GetSelectedItem();
				// return the ItemController if any located under the point
				ItemController* GetItemFromPt(CPoint pt, UINT *pFlags);
				// return the parent ItemController of a ItemController
				ItemController* GetParentItem(const ItemController* p) const;

				// return the ItemController if it can accept children, otherwise return first ancestor ItemController that can
				ItemController* AssureItemCanAcceptChildren(ItemController* pItem);


				// return true if a child of the parent already has this name
				bool NameExists(LPCTSTR lpszName, HTREEITEM htiParent = TVI_ROOT) const;
				// assure a unique name by appending (x) where x is an ascending number
				CString AssureNewName(LPCTSTR lpszName, HTREEITEM htiParent = TVI_ROOT);

				// base class for user defined search criteria
				struct SearchCriteria
				{
					SearchCriteria() {}
					virtual ~SearchCriteria() {}

					virtual bool ItemMatches(ItemController* pItemCtrlr) = 0;
				};

				// returns ItemController based on user defined SearchCriteria::ItemMatches
				ItemController* FindItem(SearchCriteria* pCriteria);

				void TreeLoaded();

			protected:
				virtual void OnInitialize();
				virtual void OnTreeLoaded();
				virtual SbjCore::Utils::Menu::ItemRange OnPrepareCtxMenu(CMenu& ctxMenu);


			private:
				struct ControllerImpl* const m_pImpl;
			};
		}
	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/TreeController.h $
// 
// 10    11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 9     10/14/08 1:12p Steve
// Implemented Deletes
// 
// 8     9/23/08 11:30a 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