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 / chap21 / cosmo / figure.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  7.7 KB  |  372 lines

  1. /*
  2.  * FIGURE.CPP
  3.  * Cosmo Chapter 21
  4.  *
  5.  * Implementation of the CFigure object for Cosmo.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "cosmo.h"
  16.  
  17.  
  18. /*
  19.  * CFigure::CFigure
  20.  * CFigure::~CFigure
  21.  *
  22.  * Parameters (Constructor):
  23.  *  pfnDestroy      PFNDESTROYED to call when object is destroyed.
  24.  *  pDoc            PCCosmoDoc we're associated with.
  25.  */
  26.  
  27. CFigure::CFigure(PFNDESTROYED pfnDestroy, PCCosmoDoc pDoc)
  28.     {
  29.     m_cRef=0;
  30.     m_pfnDestroy=pfnDestroy;
  31.  
  32.     m_pFR=NULL;     //We get this later through FrameSet.
  33.     m_pDoc=pDoc;
  34.     m_pPL=pDoc->m_pPL;
  35.  
  36.     m_fEmbedded=FALSE;
  37.  
  38.     //NULL any contained interfaces initially.
  39.     m_pImpIPersistStorage=NULL;
  40.     m_pIStorage=NULL;
  41.     m_pIStream=NULL;
  42.     m_pImpIDataObject=NULL;
  43.     m_pIDataAdviseHolder=NULL;
  44.     m_pImpIOleObject=NULL;
  45.     m_pIOleAdviseHolder=NULL;
  46.     m_pIOleClientSite=NULL;
  47.  
  48.     m_clsID=CLSID_CosmoFigure;
  49.     m_cf=pDoc->m_cf;
  50.  
  51.     //These are for IDataObject::QueryGetData
  52.     m_cfeGet=CFORMATETCGET;
  53.  
  54.     SETDefFormatEtc(m_rgfeGet[0], pDoc->m_cf, TYMED_HGLOBAL);
  55.     SETDefFormatEtc(m_rgfeGet[1], pDoc->m_cfEmbedSource
  56.         , TYMED_ISTORAGE);
  57.     SETDefFormatEtc(m_rgfeGet[2], pDoc->m_cfObjectDescriptor
  58.         , TYMED_HGLOBAL);
  59.     SETDefFormatEtc(m_rgfeGet[3], CF_METAFILEPICT, TYMED_MFPICT);
  60.     SETDefFormatEtc(m_rgfeGet[4], CF_BITMAP, TYMED_GDI);
  61.  
  62.     m_pST=NULL;
  63.  
  64.     //CHAPTER21MOD
  65.     m_pImpIPersistFile=NULL;
  66.  
  67.     //We live in the document's lifetime, so no need to AddRef here.
  68.     m_pMoniker=m_pDoc->m_pMoniker;
  69.     m_dwRegROT=0L;
  70.     //End CHAPTER21MOD
  71.  
  72.     return;
  73.     }
  74.  
  75.  
  76. CFigure::~CFigure(void)
  77.     {
  78.     //CHAPTER21MOD
  79.     //Make sure no one thinks we're still running
  80.     INOLE_RevokeAsRunning(&m_dwRegROT);
  81.     //End CHAPTER21MOD
  82.  
  83.     ReleaseInterface(m_pIOleClientSite);
  84.     ReleaseInterface(m_pIDataAdviseHolder);
  85.     ReleaseInterface(m_pIOleAdviseHolder);
  86.     ReleaseInterface(m_pIStorage)
  87.     ReleaseInterface(m_pIStream)
  88.  
  89.     //CHAPTER21MOD
  90.     DeleteInterfaceImp(m_pImpIPersistFile);
  91.     //End CHAPTER21MOD
  92.     DeleteInterfaceImp(m_pImpIOleObject)
  93.     DeleteInterfaceImp(m_pImpIDataObject)
  94.     DeleteInterfaceImp(m_pImpIPersistStorage);
  95.  
  96.     //Free strings.
  97.     if (NULL!=m_pST)
  98.         delete m_pST;
  99.  
  100.     return;
  101.     }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. /*
  108.  * CFigure::QueryInterface
  109.  * CFigure::AddRef
  110.  * CFigure::Release
  111.  *
  112.  * Purpose:
  113.  *  IUnknown members for CFigure object.
  114.  */
  115.  
  116. STDMETHODIMP CFigure::QueryInterface(REFIID riid, PPVOID ppv)
  117.     {
  118.     *ppv=NULL;
  119.  
  120.     if (IID_IUnknown==riid)
  121.         *ppv=this;
  122.  
  123.     if (IID_IPersist==riid || IID_IPersistStorage==riid)
  124.         *ppv=m_pImpIPersistStorage;
  125.  
  126.     if (IID_IDataObject==riid)
  127.         *ppv=m_pImpIDataObject;
  128.  
  129.     if (IID_IOleObject==riid)
  130.         *ppv=m_pImpIOleObject;
  131.  
  132.     //CHAPTER21MOD
  133.     if (IID_IPersistFile==riid)
  134.         *ppv=m_pImpIPersistFile;
  135.     //End CHAPTER21MOD
  136.  
  137.     if (NULL!=*ppv)
  138.         {
  139.         ((LPUNKNOWN)*ppv)->AddRef();
  140.         return NOERROR;
  141.         }
  142.  
  143.     return ResultFromScode(E_NOINTERFACE);
  144.     }
  145.  
  146.  
  147. STDMETHODIMP_(ULONG) CFigure::AddRef(void)
  148.     {
  149.     return ++m_cRef;
  150.     }
  151.  
  152.  
  153. STDMETHODIMP_(ULONG) CFigure::Release(void)
  154.     {
  155.     if (0!=--m_cRef)
  156.         return m_cRef;
  157.  
  158.     if (NULL!=m_pfnDestroy)
  159.         (*m_pfnDestroy)();
  160.  
  161.     //Document deletes us
  162.     return 0;
  163.     }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /*
  171.  * CFigure::Init
  172.  *
  173.  * Purpose:
  174.  *  Performs any initialization of a CFigure that's prone to failure
  175.  *  that we also use internally before exposing the object outside.
  176.  *
  177.  * Parameters:
  178.  *  None
  179.  *
  180.  * Return Value:
  181.  *  BOOL            TRUE if the function is successful,
  182.  *                  FALSE otherwise.
  183.  */
  184.  
  185. BOOL CFigure::Init(void)
  186.     {
  187.     m_pST=new CStringTable(m_pDoc->m_hInst);
  188.  
  189.     if (NULL==m_pST)
  190.         return FALSE;
  191.  
  192.     if (!m_pST->Init(IDS_FIGUREMIN, IDS_FIGUREMAX))
  193.         return FALSE;
  194.  
  195.     //Allocate contained interfaces.
  196.     m_pImpIPersistStorage=new CImpIPersistStorage(this, this);
  197.  
  198.     if (NULL==m_pImpIPersistStorage)
  199.         return FALSE;
  200.  
  201.     m_pImpIDataObject=new CImpIDataObject(this, this);
  202.  
  203.     if (NULL==m_pImpIDataObject)
  204.         return FALSE;
  205.  
  206.     m_pImpIOleObject=new CImpIOleObject(this, this);
  207.  
  208.     if (NULL==m_pImpIOleObject)
  209.         return FALSE;
  210.  
  211.     //CHAPTER21MOD
  212.     m_pImpIPersistFile=new CImpIPersistFile(this, this);
  213.  
  214.     if (NULL==m_pImpIPersistFile)
  215.         return FALSE;
  216.     //End CHAPTER21MOD
  217.  
  218.     return TRUE;
  219.     }
  220.  
  221.  
  222.  
  223. /*
  224.  * CFigure::FrameSet
  225.  *
  226.  * Purpose:
  227.  *  Provides the compound document object with access to the frame
  228.  *  of this application for UI purposes.
  229.  *
  230.  * Parameters:
  231.  *  pFR             PCCosmoFrame of the frame window.
  232.  *
  233.  * Return Value:
  234.  *  None
  235.  */
  236.  
  237. void CFigure::FrameSet(PCCosmoFrame pFR)
  238.     {
  239.     m_pFR=pFR;
  240.     return;
  241.     }
  242.  
  243.  
  244.  
  245.  
  246. /*
  247.  * CFigure::FIsDirty
  248.  *
  249.  * Purpose:
  250.  *  Checks if the document is dirty.  This can be called from
  251.  *  IPersistStorage::IsDirty which doesn't have access to CCosmoDoc.
  252.  *
  253.  * Parameters:
  254.  *  None
  255.  *
  256.  * Return Value:
  257.  *  BOOL            TRUE if dirty, FALSE if clean.
  258.  */
  259.  
  260. BOOL CFigure::FIsDirty(void)
  261.     {
  262.     return m_pDoc->m_fDirty;
  263.     }
  264.  
  265.  
  266.  
  267.  
  268. /*
  269.  * CFigure::FIsEmbedded
  270.  *
  271.  * Purpose:
  272.  *  Answers if the object is embedded or not.
  273.  *
  274.  * Parameters:
  275.  *  None
  276.  *
  277.  * Return Value:
  278.  *  BOOL            TRUE if the object is embedded, FALSE otherwise.
  279.  */
  280.  
  281. BOOL CFigure::FIsEmbedded(void)
  282.     {
  283.     return m_fEmbedded;
  284.     }
  285.  
  286.  
  287.  
  288.  
  289. /*
  290.  * CFigure::SendAdvise
  291.  *
  292.  * Purpose:
  293.  *  Calls the appropriate IOleClientSite or IAdviseSink member
  294.  *  function for various events such as closure, saving, etc.
  295.  *
  296.  * Parameters:
  297.  *  uCode           UINT OBJECTCODE_* identifying the notification.
  298.  *
  299.  * Return Value:
  300.  *  None
  301.  */
  302.  
  303. void CFigure::SendAdvise(UINT uCode)
  304.     {
  305.     switch (uCode)
  306.         {
  307.         case OBJECTCODE_SAVED:
  308.             if (NULL!=m_pIOleAdviseHolder)
  309.                 m_pIOleAdviseHolder->SendOnSave();
  310.  
  311.             break;
  312.  
  313.         case OBJECTCODE_CLOSED:
  314.             if (NULL!=m_pIOleAdviseHolder)
  315.                 m_pIOleAdviseHolder->SendOnClose();
  316.  
  317.             break;
  318.  
  319.         case OBJECTCODE_RENAMED:
  320.             //CHAPTER21MOD
  321.             m_pMoniker=m_pDoc->m_pMoniker;  //For IOleObject::GetMoniker
  322.             m_dwRegROT=m_pDoc->m_dwRegROT;
  323.  
  324.             if (NULL!=m_pIOleAdviseHolder)
  325.                 m_pIOleAdviseHolder->SendOnRename(m_pMoniker);
  326.             //End CHAPTER21MOD
  327.             break;
  328.  
  329.         case OBJECTCODE_SAVEOBJECT:
  330.             if (FIsDirty() && NULL!=m_pIOleClientSite)
  331.                 m_pIOleClientSite->SaveObject();
  332.  
  333.             break;
  334.  
  335.         case OBJECTCODE_DATACHANGED:
  336.             //No flags are necessary here.
  337.             if (NULL!=m_pIDataAdviseHolder)
  338.                 {
  339.                 m_pIDataAdviseHolder->SendOnDataChange
  340.                     (m_pImpIDataObject, 0, 0);
  341.                 }
  342.  
  343.             //CHAPTER21MOD
  344.             //Tell the running object table of the change
  345.             if (0!=m_dwRegROT)
  346.                 INOLE_NoteChangeTime(m_dwRegROT, NULL, NULL);
  347.             //End CHAPTER21MOD
  348.  
  349.             break;
  350.  
  351.         case OBJECTCODE_SHOWWINDOW:
  352.             if (NULL!=m_pIOleClientSite)
  353.                 m_pIOleClientSite->OnShowWindow(TRUE);
  354.  
  355.             break;
  356.  
  357.         case OBJECTCODE_HIDEWINDOW:
  358.             if (NULL!=m_pIOleClientSite)
  359.                 m_pIOleClientSite->OnShowWindow(FALSE);
  360.  
  361.             break;
  362.  
  363.         case OBJECTCODE_SHOWOBJECT:
  364.             if (NULL!=m_pIOleClientSite)
  365.                 m_pIOleClientSite->ShowObject();
  366.  
  367.             break;
  368.         }
  369.  
  370.     return;
  371.     }
  372.