Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

The SBJ MVC Framework - The Design View, Responding to Model Change

, 19 Mar 2009
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
SbjDevBin202.zip
SbjDevSrc202.zip
SbjCore
res
bigcat.ico
main.bmp
properties.bmp
properties_hc.bmp
properties_wnd_hc.ico
Shapes
Data
res
buttons.bmp
filelarge.bmp
filesmall.bmp
filesmall.png
main.bmp
Shapes.ico
ShapesDoc.ico
Toolbar.bmp
Toolbar256.bmp
treeimages.bmp
Write - Copy.png
Write.png
Write1.png
writelarge.bmp
writesmall - Copy.bmp
writesmall - Copy.png
writesmall.bmp
writesmall.png
XmlMvc
res
//------------------------------------------------------------------------------
// $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)

Share

About the Author

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by SBJ
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid