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 / chap18 / cosmo / figure.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  7KB  |  331 lines

  1. /*
  2.  * FIGURE.CPP
  3.  * Cosmo Chapter 18
  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.     return;
  65.     }
  66.  
  67.  
  68. CFigure::~CFigure(void)
  69.     {
  70.     ReleaseInterface(m_pIOleClientSite);
  71.     ReleaseInterface(m_pIDataAdviseHolder);
  72.     ReleaseInterface(m_pIOleAdviseHolder);
  73.     ReleaseInterface(m_pIStorage)
  74.     ReleaseInterface(m_pIStream)
  75.  
  76.     DeleteInterfaceImp(m_pImpIOleObject)
  77.     DeleteInterfaceImp(m_pImpIDataObject)
  78.     DeleteInterfaceImp(m_pImpIPersistStorage);
  79.  
  80.     //Free strings.
  81.     if (NULL!=m_pST)
  82.         delete m_pST;
  83.  
  84.     return;
  85.     }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. /*
  92.  * CFigure::QueryInterface
  93.  * CFigure::AddRef
  94.  * CFigure::Release
  95.  *
  96.  * Purpose:
  97.  *  IUnknown members for CFigure object.
  98.  */
  99.  
  100. STDMETHODIMP CFigure::QueryInterface(REFIID riid, PPVOID ppv)
  101.     {
  102.     *ppv=NULL;
  103.  
  104.     if (IID_IUnknown==riid)
  105.         *ppv=this;
  106.  
  107.     if (IID_IPersist==riid || IID_IPersistStorage==riid)
  108.         *ppv=m_pImpIPersistStorage;
  109.  
  110.     if (IID_IDataObject==riid)
  111.         *ppv=m_pImpIDataObject;
  112.  
  113.     if (IID_IOleObject==riid)
  114.         *ppv=m_pImpIOleObject;
  115.  
  116.     if (NULL!=*ppv)
  117.         {
  118.         ((LPUNKNOWN)*ppv)->AddRef();
  119.         return NOERROR;
  120.         }
  121.  
  122.     return ResultFromScode(E_NOINTERFACE);
  123.     }
  124.  
  125.  
  126. STDMETHODIMP_(ULONG) CFigure::AddRef(void)
  127.     {
  128.     return ++m_cRef;
  129.     }
  130.  
  131.  
  132. STDMETHODIMP_(ULONG) CFigure::Release(void)
  133.     {
  134.     if (0!=--m_cRef)
  135.         return m_cRef;
  136.  
  137.     if (NULL!=m_pfnDestroy)
  138.         (*m_pfnDestroy)();
  139.  
  140.     //Document deletes us
  141.     return 0;
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. /*
  150.  * CFigure::Init
  151.  *
  152.  * Purpose:
  153.  *  Performs any initialization of a CFigure that's prone to failure
  154.  *  that we also use internally before exposing the object outside.
  155.  *
  156.  * Parameters:
  157.  *  None
  158.  *
  159.  * Return Value:
  160.  *  BOOL            TRUE if the function is successful,
  161.  *                  FALSE otherwise.
  162.  */
  163.  
  164. BOOL CFigure::Init(void)
  165.     {
  166.     m_pST=new CStringTable(m_pDoc->m_hInst);
  167.  
  168.     if (NULL==m_pST)
  169.         return FALSE;
  170.  
  171.     if (!m_pST->Init(IDS_FIGUREMIN, IDS_FIGUREMAX))
  172.         return FALSE;
  173.  
  174.     //Allocate contained interfaces.
  175.     m_pImpIPersistStorage=new CImpIPersistStorage(this, this);
  176.  
  177.     if (NULL==m_pImpIPersistStorage)
  178.         return FALSE;
  179.  
  180.     m_pImpIDataObject=new CImpIDataObject(this, this);
  181.  
  182.     if (NULL==m_pImpIDataObject)
  183.         return FALSE;
  184.  
  185.     m_pImpIOleObject=new CImpIOleObject(this, this);
  186.  
  187.     if (NULL==m_pImpIOleObject)
  188.         return FALSE;
  189.  
  190.     return TRUE;
  191.     }
  192.  
  193.  
  194.  
  195. /*
  196.  * CFigure::FrameSet
  197.  *
  198.  * Purpose:
  199.  *  Provides the compound document object with access to the frame
  200.  *  of this application for UI purposes.
  201.  *
  202.  * Parameters:
  203.  *  pFR             PCCosmoFrame of the frame window.
  204.  *
  205.  * Return Value:
  206.  *  None
  207.  */
  208.  
  209. void CFigure::FrameSet(PCCosmoFrame pFR)
  210.     {
  211.     m_pFR=pFR;
  212.     return;
  213.     }
  214.  
  215.  
  216.  
  217.  
  218. /*
  219.  * CFigure::FIsDirty
  220.  *
  221.  * Purpose:
  222.  *  Checks if the document is dirty.  This can be called from
  223.  *  IPersistStorage::IsDirty which doesn't have access to CCosmoDoc.
  224.  *
  225.  * Parameters:
  226.  *  None
  227.  *
  228.  * Return Value:
  229.  *  BOOL            TRUE if dirty, FALSE if clean.
  230.  */
  231.  
  232. BOOL CFigure::FIsDirty(void)
  233.     {
  234.     return m_pDoc->m_fDirty;
  235.     }
  236.  
  237.  
  238.  
  239.  
  240. /*
  241.  * CFigure::FIsEmbedded
  242.  *
  243.  * Purpose:
  244.  *  Answers if the object is embedded or not.
  245.  *
  246.  * Parameters:
  247.  *  None
  248.  *
  249.  * Return Value:
  250.  *  BOOL            TRUE if the object is embedded, FALSE otherwise.
  251.  */
  252.  
  253. BOOL CFigure::FIsEmbedded(void)
  254.     {
  255.     return m_fEmbedded;
  256.     }
  257.  
  258.  
  259.  
  260.  
  261. /*
  262.  * CFigure::SendAdvise
  263.  *
  264.  * Purpose:
  265.  *  Calls the appropriate IOleClientSite or IAdviseSink member
  266.  *  function for various events such as closure, saving, etc.
  267.  *
  268.  * Parameters:
  269.  *  uCode           UINT OBJECTCODE_* identifying the notification.
  270.  *
  271.  * Return Value:
  272.  *  None
  273.  */
  274.  
  275. void CFigure::SendAdvise(UINT uCode)
  276.     {
  277.     switch (uCode)
  278.         {
  279.         case OBJECTCODE_SAVED:
  280.             if (NULL!=m_pIOleAdviseHolder)
  281.                 m_pIOleAdviseHolder->SendOnSave();
  282.  
  283.             break;
  284.  
  285.         case OBJECTCODE_CLOSED:
  286.             if (NULL!=m_pIOleAdviseHolder)
  287.                 m_pIOleAdviseHolder->SendOnClose();
  288.  
  289.             break;
  290.  
  291.         case OBJECTCODE_RENAMED:
  292.             //Call IOleAdviseHolder::SendOnRename (later)
  293.             break;
  294.  
  295.         case OBJECTCODE_SAVEOBJECT:
  296.             if (FIsDirty() && NULL!=m_pIOleClientSite)
  297.                 m_pIOleClientSite->SaveObject();
  298.  
  299.             break;
  300.  
  301.         case OBJECTCODE_DATACHANGED:
  302.             //No flags are necessary here.
  303.             if (NULL!=m_pIDataAdviseHolder)
  304.                 {
  305.                 m_pIDataAdviseHolder->SendOnDataChange
  306.                     (m_pImpIDataObject, 0, 0);
  307.                 }
  308.             break;
  309.  
  310.         case OBJECTCODE_SHOWWINDOW:
  311.             if (NULL!=m_pIOleClientSite)
  312.                 m_pIOleClientSite->OnShowWindow(TRUE);
  313.  
  314.             break;
  315.  
  316.         case OBJECTCODE_HIDEWINDOW:
  317.             if (NULL!=m_pIOleClientSite)
  318.                 m_pIOleClientSite->OnShowWindow(FALSE);
  319.  
  320.             break;
  321.  
  322.         case OBJECTCODE_SHOWOBJECT:
  323.             if (NULL!=m_pIOleClientSite)
  324.                 m_pIOleClientSite->ShowObject();
  325.  
  326.             break;
  327.         }
  328.  
  329.     return;
  330.     }
  331.