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

#pragma once

#include "CmdTargetController.h"
#include "Resource.h"

namespace SbjCore
{
  namespace Mvc
  {
    /**
      template class wraps a CCmdTarget or derivative to add a 
      link to a CmdTargetController class for processing CmdMsg 
      messages.
                                  
      ControlledCmdTargetT overrides OnCmdMsg method of its template 
      base class so messages can be forwarded to an assigned 
      CmdTargetController before the default MFC message processing 
      can occur.  The ControlledCmdTargetT will set the CmdTargetController 
      in its constructor with a call to ControlledCmdTargetT::SetController. 
      
      \param _baseClass Any CCmdTarget or derived class. 
      
      NOTE: A ControlledWndT derivation of ControlledCmdTargetT is provided 
      for CWnd classes that provides support for all other Windows messages.
    */
    template<class _baseClass>
    class ControlledCmdTargetT : public _baseClass
    {
    public:
      ControlledCmdTargetT() :
        pTheController(NULL)
      {
      }

      virtual ~ControlledCmdTargetT()
      {
        pTheController = NULL;
      }

      /// sets the CmdTargetController for this target
      void SetController(CmdTargetController *pController)
      {
        ASSERT((pController == NULL) || (pTheController == NULL));
        pTheController = pController;
        if (pTheController != NULL)
        {
          pTheController->SetCmdTarget(this);
        }
        
      }
      
      CmdTargetController* GetController() const
      {
        return pTheController;
      }

      _baseClass* GetControlledTarget()
      {
        return (_baseClass*)this;
      }
      
    public:

      /** routes WM_COMMAND messages to the CmdTargetController.  
      
        A call to CmdTargetController::HandleCmdMsg is made. 
        If the return is false the _baseclass OnCmdMsg is called. 
        
        \return true if the message was handled, otherwise false.
      */
      virtual BOOL OnCmdMsg(UINT nID,			
        int nCode,							
        void* pExtra,						
        AFX_CMDHANDLERINFO* pHandlerInfo)	
      {
        BOOL bRslt = FALSE;
      
        // if we have a controller make sure it gets the message first
        if (pTheController != NULL)
        {
          // mfc message map is checking to disable messages with no handler
          if (pHandlerInfo != NULL)
          {
            // check to see if our controller has a handler
            if (pTheController->GetHandler(nID) != NULL)
            {
              pHandlerInfo->pTarget = this;
              pHandlerInfo->pmf = NULL; // we are't a message map function
              bRslt = true;  // still we let mfc know we have one
            }
          }
          else if ((nCode != CN_EVENT) && (nCode != CN_OLECOMMAND))  // let _baseClass handle ole
          {
#ifdef _DEBUG					
            // debug trap, just change to the id you want
            if (ID_SBJCORE_CTX_DELETE == nID)
            {
              nID = ID_SBJCORE_CTX_DELETE;
            }
#endif
            // check all non-cmdUI first
            if (nCode != CN_UPDATE_COMMAND_UI)
            {
              // check for a Notify message
              UINT nTempCode = LOWORD(nCode);

              if (nTempCode > 0)
              {
                AFX_NOTIFY* pNotify = static_cast<AFX_NOTIFY*>(pExtra);

                if (pNotify != NULL)
                {
                  // Notify message
                  bRslt = pTheController->HandleNotifyMsg(pNotify->pNMHDR, pNotify->pResult);
                }
              }
              else
              {
                // standard message
                bRslt = pTheController->HandleCmdMsg(nID);
              }
            }
            else
            {
              // cmdUI message
              bRslt = pTheController->HandleCmdUIMsg(nID, static_cast<CCmdUI*>(pExtra));
            }
          }
          // if still not handled allow controller to rout the message
          if (!bRslt)
          {
            bRslt = pTheController->RoutCmdMsg(nID, nCode, pExtra, pHandlerInfo);
          }
        }

        // still not handled				
        if (!bRslt)
        {
          bRslt = _baseClass::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
        }

        return bRslt;
      }
    protected:
      /// a pointer to the attached CmdTargetController class
      CmdTargetController* pTheController;

    };

  }
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/ControlledCmdTargetT.h $
// 
// 3     10/14/08 1:12p Steve
// Implemented Deletes

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