home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap24 / patron / ambients.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  236 lines

  1. /*
  2.  * AMBIENTS.CPP
  3.  * Patron Chapter 24
  4.  *
  5.  * Implementation of the ambient properties IDispatch on a
  6.  * tenant control site.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19. /*
  20.  * CImpIDispatch::CImpIDispatch
  21.  * CImpIDispatch::~CImpIDispatch
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pTen            PCTenant of the tenant we're in.
  25.  *  pUnkOuter       LPUNKNOWN to which we delegate.
  26.  */
  27.  
  28. CImpIDispatch::CImpIDispatch(PCTenant pTen, LPUNKNOWN pUnkOuter)
  29.     {
  30.     m_cRef=0;
  31.     m_pTen=pTen;
  32.     m_pUnkOuter=pUnkOuter;
  33.     return;
  34.     }
  35.  
  36. CImpIDispatch::~CImpIDispatch(void)
  37.     {
  38.     return;
  39.     }
  40.  
  41.  
  42.  
  43.  
  44. /*
  45.  * CImpIDispatch::QueryInterface
  46.  * CImpIDispatch::AddRef
  47.  * CImpIDispatch::Release
  48.  */
  49.  
  50. STDMETHODIMP CImpIDispatch::QueryInterface(REFIID riid, PPVOID ppv)
  51.     {
  52.     return m_pUnkOuter->QueryInterface(riid, ppv);
  53.     }
  54.  
  55. STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
  56.     {
  57.     ++m_cRef;
  58.     return m_pUnkOuter->AddRef();
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
  62.     {
  63.     m_cRef--;
  64.     return m_pUnkOuter->Release();
  65.     }
  66.  
  67.  
  68.  
  69.  
  70. /*
  71.  * CImpIDispatch::GetTypeInfoCount
  72.  * CImpIDispatch::GetTypeInfo
  73.  * CImpIDispatch::GetIDsOfNames
  74.  *
  75.  * Unimplemented members, not needed for ambient properties.
  76.  */
  77.  
  78. STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT *pctInfo)
  79.     {
  80.     *pctInfo=0;
  81.     return NOERROR;
  82.     }
  83.  
  84. STDMETHODIMP CImpIDispatch::GetTypeInfo(UINT itinfo
  85.     , LCID lcid, ITypeInfo **pptInfo)
  86.     {
  87.     *pptInfo=NULL;
  88.     return ResultFromScode(E_NOTIMPL);
  89.     }
  90.  
  91. STDMETHODIMP CImpIDispatch::GetIDsOfNames(REFIID riid
  92.     , OLECHAR **rgszNames, UINT cNames, LCID lcid, DISPID *rgDispID)
  93.     {
  94.     *rgszNames=NULL;
  95.     *rgDispID=NULL;
  96.     return ResultFromScode(E_NOTIMPL);
  97.     }
  98.  
  99.  
  100.  
  101.  
  102. /*
  103.  * CImpIDispatch::Invoke
  104.  *
  105.  * Purpose:
  106.  *  Calls a method in the dispatch interface or manipulates a
  107.  *  property.
  108.  *
  109.  * Parameters:
  110.  *  dispIDMember    DISPID of the method or property of interest.
  111.  *  riid            REFIID reserved, must be NULL.
  112.  *  lcid            LCID of the locale.
  113.  *  wFlags          USHORT describing the context of the invocation.
  114.  *  pDispParams     DISPPARAMS * to the array of arguments.
  115.  *  pVarResult      VARIANT * in which to store the result.  Is
  116.  *                  NULL if the caller is not interested.
  117.  *  pExcepInfo      EXCEPINFO * to exception information.
  118.  *  puArgErr        UINT * in which to store the index of an
  119.  *                  invalid parameter if DISP_E_TYPEMISMATCH
  120.  *                  is returned.
  121.  *
  122.  * Return Value:
  123.  *  HRESULT         NOERROR or a general error code.
  124.  */
  125.  
  126.  
  127. STDMETHODIMP CImpIDispatch::Invoke(DISPID dispIDMember, REFIID riid
  128.     , LCID lcid, unsigned short wFlags, DISPPARAMS *pDispParams
  129.     , VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  130.     {
  131.     HRESULT     hr;
  132.     VARIANT     varResult;
  133.  
  134.     if (IID_NULL!=riid)
  135.         return ResultFromScode(E_INVALIDARG);
  136.  
  137.     /*
  138.      * We ignore lcid in this function.  A multilingual application
  139.      * might use it to determine the meaning of certain parameters
  140.      * or perhaps as an indication of how to format data like
  141.      * time, date, and currency or any other language or locale-
  142.      * sensitive data.
  143.      */
  144.  
  145.     /*
  146.      * Variable handling:  since ambient properties is just a
  147.      * collection of singular read-only values, we don't have to
  148.      * worry about input parameters.
  149.      */
  150.  
  151.     /*
  152.      * If the caller is not interested in the return value, then
  153.      * pVarResult is NULL.  But since we're dealing with ambient
  154.      * properties, there should always be an interest.  In any case,
  155.      * if we're given a NULL, we'll point it to a dummy structure so
  156.      * the rest of the code can assume that pVarResult is non-NULL.
  157.      */
  158.     if(NULL==pVarResult)
  159.       pVarResult=&varResult;
  160.  
  161.     VariantInit(pVarResult);
  162.  
  163.     //The most common case is boolean, use as an initial type
  164.     V_VT(pVarResult)=VT_BOOL;
  165.  
  166.     /*
  167.      * Process the requested ambient property.  Anything but a
  168.      * request for a property is invalid, so we can check that
  169.      * before looking at the specific ID.  We can only get away
  170.      * with this because all properties are read-only.
  171.      */
  172.  
  173.     if (!(DISPATCH_PROPERTYGET & wFlags))
  174.         return ResultFromScode(DISP_E_MEMBERNOTFOUND);
  175.  
  176.     hr=NOERROR;
  177.  
  178.     switch (dispIDMember)
  179.         {
  180.         case DISPID_AMBIENT_BACKCOLOR:
  181.             V_I4(pVarResult)=m_pTen->m_clrBack;
  182.             V_VT(pVarResult)=VT_I4;
  183.             break;
  184.  
  185.         case DISPID_AMBIENT_FORECOLOR:
  186.             V_I4(pVarResult)=m_pTen->m_clrFore;
  187.             V_VT(pVarResult)=VT_I4;
  188.             break;
  189.  
  190.         case DISPID_AMBIENT_FONT:
  191.             /*
  192.              * If we failed to create the font, act like we
  193.              * don't support it.
  194.              */
  195.             if (NULL==m_pTen->m_pIFont)
  196.                 return ResultFromScode(DISP_E_MEMBERNOTFOUND);
  197.  
  198.             //The correct type is an IFontDisp pointer
  199.             V_DISPATCH(pVarResult)=m_pTen->m_pIFont;
  200.             m_pTen->m_pIFont->AddRef();
  201.             V_VT(pVarResult)=VT_FONT;
  202.             break;
  203.  
  204.         case DISPID_AMBIENT_LOCALEID:
  205.             V_I4(pVarResult)=m_pTen->m_lcid;
  206.             V_VT(pVarResult)=VT_I4;
  207.             break;
  208.  
  209.         case DISPID_AMBIENT_USERMODE:
  210.             V_BOOL(pVarResult)=!m_pTen->m_fDesignMode;
  211.             break;
  212.  
  213.         case DISPID_AMBIENT_UIDEAD:
  214.             V_BOOL(pVarResult)=m_pTen->m_fUIDead;
  215.             break;
  216.  
  217.         case DISPID_AMBIENT_SUPPORTSMNEMONICS:
  218.             V_BOOL(pVarResult)=TRUE;
  219.             break;
  220.  
  221.         case DISPID_AMBIENT_SHOWGRABHANDLES:
  222.             V_BOOL(pVarResult)=m_pTen->m_fHatchHandles;
  223.             break;
  224.  
  225.         case DISPID_AMBIENT_SHOWHATCHING:
  226.             V_BOOL(pVarResult)=m_pTen->m_fHatchHandles;
  227.             break;
  228.  
  229.         default:
  230.             hr=ResultFromScode(DISP_E_MEMBERNOTFOUND);
  231.             break;
  232.         }
  233.  
  234.     return hr;
  235.     }
  236.