home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLEDOCTG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-12  |  6.1 KB  |  240 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. #define new DEBUG_NEW
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // COleCmdUI class
  22.  
  23. COleCmdUI::COleCmdUI(OLECMD* rgCmds, ULONG cCmds, const GUID* pGroup)
  24. {
  25.     m_rgCmds = rgCmds;
  26.     m_nIndexMax = cCmds;
  27.     m_pguidCmdGroup = pGroup;
  28. }
  29.  
  30. void COleCmdUI::Enable(BOOL bOn)
  31. {
  32.    if (m_rgCmds != NULL)
  33.    {
  34.       ASSERT(m_nIndex < m_nIndexMax);
  35.  
  36.       if (bOn)
  37.          m_rgCmds[m_nIndex].cmdf |= OLECMDF_ENABLED;
  38.       else
  39.          m_rgCmds[m_nIndex].cmdf &= ~OLECMDF_ENABLED;
  40.       m_bEnableChanged = TRUE;
  41.    }
  42. }
  43.  
  44. void COleCmdUI::SetCheck(int nCheck)
  45. {
  46.    if (m_rgCmds != NULL)
  47.    {
  48.       ASSERT(m_nIndex < m_nIndexMax);
  49.  
  50.       m_rgCmds[m_nIndex].cmdf &= ~(OLECMDF_LATCHED|OLECMDF_NINCHED);
  51.       if (nCheck == 1)
  52.          m_rgCmds[m_nIndex].cmdf |= OLECMDF_LATCHED;
  53.       else if (nCheck == 2)
  54.          m_rgCmds[m_nIndex].cmdf |= OLECMDF_NINCHED;
  55.    }
  56. }
  57.  
  58. void COleCmdUI::SetText(LPCTSTR lpszText)
  59. {
  60.     m_strText = lpszText;
  61. }
  62.  
  63. BOOL COleCmdUI::DoUpdate(CCmdTarget* pTarget, BOOL bDisableIfNoHandler)
  64. {
  65.    BOOL bResult;
  66.    ASSERT_VALID(pTarget);
  67.  
  68.     // fire off an OLECOMMNAD message to translate the OLECMD ID
  69.     // to a real WM_COMMAND ID via the message maps
  70.     // if we find a translation, fire a UPDATE_COMMAND_UI request to
  71.     // see if the command should be enabled or not
  72.  
  73.    m_bEnableChanged = FALSE;
  74.    bResult = pTarget->OnCmdMsg(m_nID, CN_OLECOMMAND, this, NULL);
  75.    if (!bResult)
  76.       ASSERT(!m_bEnableChanged);
  77.    else
  78.       bResult = pTarget->OnCmdMsg(m_nID, CN_UPDATE_COMMAND_UI, this, NULL);
  79.  
  80.    if (bDisableIfNoHandler && !m_bEnableChanged)
  81.    {
  82.       AFX_CMDHANDLERINFO info;
  83.       info.pTarget = NULL;
  84.       bResult = pTarget->OnCmdMsg(m_nID, CN_COMMAND, this, &info);
  85.  
  86.       Enable(bResult);
  87.       if (bResult || m_bEnableChanged)
  88.          m_rgCmds[m_nIndex].cmdf |= OLECMDF_SUPPORTED;
  89.       else
  90.          m_rgCmds[m_nIndex].cmdf &= ~OLECMDF_SUPPORTED;
  91.    }
  92.    else
  93.    {
  94.       if (m_bEnableChanged)
  95.          m_rgCmds[m_nIndex].cmdf |= OLECMDF_SUPPORTED;
  96.       else
  97.          m_rgCmds[m_nIndex].cmdf &= ~OLECMDF_SUPPORTED;
  98.    }
  99.    return bResult;
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // IOleCommandTarget implementation
  104.  
  105. STDMETHODIMP_(ULONG) CDocObjectServer::XOleCommandTarget::AddRef()
  106. {
  107.     METHOD_PROLOGUE_EX(CDocObjectServer, OleCommandTarget)
  108.     return pThis->m_pOwner->ExternalAddRef();
  109. }
  110.  
  111. STDMETHODIMP_(ULONG) CDocObjectServer::XOleCommandTarget::Release()
  112. {
  113.     METHOD_PROLOGUE_EX(CDocObjectServer, OleCommandTarget)
  114.     return pThis->m_pOwner->ExternalRelease();
  115. }
  116.  
  117. STDMETHODIMP CDocObjectServer::XOleCommandTarget::QueryInterface(
  118.     REFIID iid, LPVOID* ppvObj)
  119. {
  120.     METHOD_PROLOGUE_EX(CDocObjectServer, OleCommandTarget)
  121.     return pThis->m_pOwner->ExternalQueryInterface(&iid, ppvObj);
  122. }
  123.  
  124. STDMETHODIMP CDocObjectServer::XOleCommandTarget::QueryStatus(
  125.    const GUID* pguidCmdGroup, ULONG cCmds, OLECMD rgCmds[],
  126.    OLECMDTEXT* pcmdtext)
  127. {
  128.     METHOD_PROLOGUE_EX(CDocObjectServer, OleCommandTarget)
  129.     ASSERT_VALID(pThis);
  130.     HRESULT hr = NOERROR;
  131.  
  132.     if (rgCmds == NULL)
  133.         hr = E_POINTER;
  134.     else
  135.     {
  136.         COleDocIPFrameWnd* pFrame = pThis->GetControllingFrame();
  137.         if (pFrame == NULL)
  138.         {
  139.             ULONG nIndex;
  140.             for (nIndex = 0; nIndex < cCmds; nIndex++)
  141.                 rgCmds[nIndex].cmdf = 0;
  142.         }
  143.         else
  144.         {
  145.             COleCmdUI state(rgCmds, cCmds, pguidCmdGroup);
  146.             if (pcmdtext == NULL)
  147.                 state.m_nCmdTextFlag = 0;
  148.             else
  149.                 state.m_nCmdTextFlag = pcmdtext->cmdtextf;
  150.             for (state.m_nIndex = 0; state.m_nIndex < cCmds; state.m_nIndex++)
  151.             {
  152.                 state.m_nID = rgCmds[state.m_nIndex].cmdID;
  153.                 state.DoUpdate(pFrame, TRUE);
  154.             }
  155.  
  156.             if (pcmdtext != NULL && pcmdtext->rgwz != NULL &&
  157.                 (pcmdtext->cmdtextf != OLECMDTEXTF_NONE))
  158.             {
  159.                 USES_CONVERSION;
  160.                 ASSERT(cCmds == 1);
  161.                 state.m_strText = state.m_strText.Right(pcmdtext->cwBuf-1);
  162.                 pcmdtext->cwActual = state.m_strText.GetLength();
  163.  
  164. #ifdef _UNICODE
  165.                 lstrcpyW(pcmdtext->rgwz, (LPCTSTR) state.m_strText);
  166. #elif defined(OLE2ANSI)
  167.                 lstrcpy(pcmdtext->rgwz, state.m_strText);
  168. #else
  169.                 lstrcpyW(pcmdtext->rgwz, T2W((LPCTSTR) state.m_strText));
  170. #endif
  171.             }
  172.         }
  173.     }
  174.  
  175.     return hr;
  176. }
  177.  
  178. STDMETHODIMP CDocObjectServer::XOleCommandTarget::Exec(
  179.    const GUID* pguidCmdGroup, DWORD nCmdID, DWORD nCmdExecOpt,
  180.    VARIANTARG* pvarargIn, VARIANTARG* pvarargOut)
  181. {
  182.     METHOD_PROLOGUE_EX(CDocObjectServer, OleCommandTarget)
  183.     ASSERT_VALID(pThis);
  184.  
  185.     // Offer the command to the document, first
  186.  
  187.     HRESULT hr = pThis->OnExecOleCmd(pguidCmdGroup, nCmdID,
  188.             nCmdExecOpt, pvarargIn, pvarargOut);
  189.     if (hr != E_NOTIMPL)
  190.         return hr;
  191.  
  192. #ifdef _DEBUG
  193.     // MFC doesn't support commands with arguments
  194.     // You must handle argument commands by overriding OnExecOleCmd()
  195.  
  196.     if (pvarargIn != NULL || pvarargOut != NULL)
  197.         TRACE1("Warning: IOleCommandTarget::Exec() received parameterized command #%d\n", nCmdID);
  198. #endif
  199.  
  200.     COleDocIPFrameWnd* pFrame = pThis->GetControllingFrame();
  201.     if (pFrame == NULL)
  202.         hr = OLECMDERR_E_NOHELP;
  203.     else
  204.     {
  205.         OLECMD cmd;
  206.  
  207.         COleCmdUI state(&cmd, 1, pguidCmdGroup);
  208.         state.m_nIndex = 0;
  209.         cmd.cmdf = 0;
  210.         cmd.cmdID = nCmdID;
  211.         state.m_nID = nCmdID;
  212.  
  213.         // help via Doc Object targeting is not supported
  214.  
  215.         if (nCmdExecOpt == OLECMDEXECOPT_SHOWHELP)
  216.             hr = OLECMDERR_E_DISABLED;
  217.         else
  218.         {
  219.             // is the command supported?
  220.  
  221.             if (!state.DoUpdate(pFrame, TRUE))
  222.                 hr = OLECMDERR_E_NOTSUPPORTED;
  223.             else
  224.             {
  225.                 if (cmd.cmdf & OLECMDF_ENABLED)
  226.                 {
  227.                     if (pFrame->OnCmdMsg(state.m_nID, CN_COMMAND, NULL, NULL))
  228.                         hr = S_OK;
  229.                     else
  230.                         hr = E_FAIL;
  231.                 }
  232.                 else
  233.                     hr = OLECMDERR_E_DISABLED;
  234.             }
  235.         }
  236.     }
  237.  
  238.     return hr;
  239. }
  240.