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 / chap19 / polyline / ioleobj.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  11KB  |  488 lines

  1. /*
  2.  * IOLEOBJ.CPP
  3.  * Polyline Component Chapter 19
  4.  *
  5.  * Implementation of the IOleObject interface for Polyline.  Some of
  6.  * these just pass through to the default handler which does default
  7.  * implementations.
  8.  *
  9.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Microsoft
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "polyline.h"
  18.  
  19.  
  20. /*
  21.  * CImpIOleObject::CImpIOleObject
  22.  * CImpIOleObject::~CImpIOleObject
  23.  *
  24.  * Parameters (Constructor):
  25.  *  pObj            PCPolyline of the object we're in.
  26.  *  pUnkOuter       LPUNKNOWN to which we delegate.
  27.  */
  28.  
  29. CImpIOleObject::CImpIOleObject(PCPolyline pObj
  30.     , LPUNKNOWN pUnkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_pUnkOuter=pUnkOuter;
  35.     return;
  36.     }
  37.  
  38. CImpIOleObject::~CImpIOleObject(void)
  39.     {
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45. /*
  46.  * CImpIOleObject::QueryInterface
  47.  * CImpIOleObject::AddRef
  48.  * CImpIOleObject::Release
  49.  *
  50.  * Purpose:
  51.  *  IUnknown members for CImpIOleObject object.
  52.  */
  53.  
  54. STDMETHODIMP CImpIOleObject::QueryInterface(REFIID riid, PPVOID ppv)
  55.     {
  56.     return m_pUnkOuter->QueryInterface(riid, ppv);
  57.     }
  58.  
  59.  
  60. STDMETHODIMP_(ULONG) CImpIOleObject::AddRef(void)
  61.     {
  62.     ++m_cRef;
  63.     return m_pUnkOuter->AddRef();
  64.     }
  65.  
  66. STDMETHODIMP_(ULONG) CImpIOleObject::Release(void)
  67.     {
  68.     --m_cRef;
  69.     return m_pUnkOuter->Release();
  70.     }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CImpIOleObject::SetClientSite
  78.  * CImpIOleObject::GetClientSite
  79.  *
  80.  * Purpose:
  81.  *  Manages the IOleClientSite pointer of our container.
  82.  */
  83.  
  84. STDMETHODIMP CImpIOleObject::SetClientSite
  85.     (LPOLECLIENTSITE pIOleClientSite)
  86.     {
  87.     if (NULL!=m_pObj->m_pIOleClientSite)
  88.         m_pObj->m_pIOleClientSite->Release();
  89.  
  90.     m_pObj->m_pIOleClientSite=pIOleClientSite;
  91.  
  92.     if (NULL!=m_pObj->m_pIOleClientSite)
  93.         m_pObj->m_pIOleClientSite->AddRef();
  94.  
  95.     return NOERROR;
  96.     }
  97.  
  98. STDMETHODIMP CImpIOleObject::GetClientSite(LPOLECLIENTSITE *ppSite)
  99.     {
  100.     //Be sure to AddRef the new pointer you are giving away.
  101.     *ppSite=m_pObj->m_pIOleClientSite;
  102.     m_pObj->m_pIOleClientSite->AddRef();
  103.  
  104.     return NOERROR;
  105.     }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. /*
  112.  * CImpIOleObject::SetHostNames
  113.  *
  114.  * Purpose:
  115.  *  Provides the object with names of the container application and
  116.  *  the object in the container to use in object user interface.
  117.  *
  118.  * Parameters:
  119.  *  pszApp          LPCOLESTR of the container application.
  120.  *  pszObj          LPCOLESTR of some name that is useful in window
  121.  *                  titles.
  122.  *
  123.  * Return Value:
  124.  *  HRESULT         NOERROR
  125.  */
  126.  
  127. STDMETHODIMP CImpIOleObject::SetHostNames(LPCOLESTR pszApp
  128.     , LPCOLESTR pszObj)
  129.     {
  130.     if (NULL!=m_pObj->m_hDlg)
  131.         {
  132.         TCHAR       szTemp[128];
  133.  
  134.        #ifdef WIN32ANSI
  135.         char        szObj[80];
  136.         WideCharToMultiByte(CP_ACP, 0, pszObj, -1, szObj, 80
  137.             , NULL, NULL);
  138.         wsprintf(szTemp, SZPOLYFRAMETITLE, szObj);
  139.        #else
  140.         wsprintf(szTemp, SZPOLYFRAMETITLE, pszObj);
  141.        #endif
  142.         SetWindowText(m_pObj->m_hDlg, szTemp);
  143.         }
  144.  
  145.     return NOERROR;
  146.     }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. /*
  153.  * CImpIOleObject::Close
  154.  *
  155.  * Purpose:
  156.  *  Forces the object to close down its user interface and unload.
  157.  *
  158.  * Parameters:
  159.  *  dwSaveOption    DWORD describing the circumstances under which
  160.  *                  the object is being saved and closed.
  161.  *
  162.  * Return Value:
  163.  *  HRESULT         NOERROR or a general error value.
  164.  */
  165.  
  166. STDMETHODIMP CImpIOleObject::Close(DWORD dwSaveOption)
  167.     {
  168.     HWND        hWnd;
  169.     BOOL        fSave=FALSE;
  170.  
  171.     hWnd=m_pObj->m_hDlg;
  172.  
  173.     //If object is dirty and we're asked to save, save it and close.
  174.     if (OLECLOSE_SAVEIFDIRTY==dwSaveOption && m_pObj->m_fDirty)
  175.         fSave=TRUE;
  176.  
  177.     /*
  178.      * If asked to prompt, only do so if dirty, then if we get a
  179.      * YES, save as usual and close.  On NO, just close.  On
  180.      * CANCEL return OLE_E_PROMPTSAVECANCELLED.
  181.      */
  182.     if (OLECLOSE_PROMPTSAVE==dwSaveOption && m_pObj->m_fDirty)
  183.         {
  184.         TCHAR       szTitle[20];
  185.         TCHAR       szTemp[80];
  186.         UINT        uRet;
  187.  
  188.         lstrcpy(szTitle, m_pObj->String(IDS_CLOSECAPTION));
  189.         lstrcpy(szTemp, m_pObj->String(IDS_CLOSEPROMPT));
  190.  
  191.         uRet=MessageBox(hWnd, szTemp, szTitle, MB_YESNOCANCEL);
  192.  
  193.         if (IDCANCEL==uRet)
  194.             return ResultFromScode(OLE_E_PROMPTSAVECANCELLED);
  195.  
  196.         if (IDYES==uRet)
  197.             fSave=TRUE;
  198.         }
  199.  
  200.     if (fSave)
  201.         {
  202.         m_pObj->SendAdvise(OBJECTCODE_SAVEOBJECT);
  203.         m_pObj->SendAdvise(OBJECTCODE_SAVED);
  204.         }
  205.  
  206.     //We get directly here on OLECLOSE_NOSAVE.
  207.     if (NULL!=hWnd)
  208.         {
  209.         //This hides the window and sends the appropriate notify.
  210.         DoVerb(OLEIVERB_HIDE, NULL, NULL, -1, NULL, NULL);
  211.  
  212.         m_pObj->SendAdvise(OBJECTCODE_CLOSED);
  213.         PostMessage(hWnd, POLYM_CLOSE, 0, 0L);
  214.         }
  215.  
  216.     return NOERROR;
  217.     }
  218.  
  219.  
  220.  
  221.  
  222. /*
  223.  * CImpIOleObject::DoVerb
  224.  *
  225.  * Purpose:
  226.  *  Executes an object-defined action.
  227.  *
  228.  * Parameters:
  229.  *  iVerb           LONG index of the verb to execute.
  230.  *  pMSG            LPMSG describing the event causing the
  231.  *                  activation.
  232.  *  pActiveSite     LPOLECLIENTSITE to the site involved.
  233.  *  lIndex          LONG the piece on which execution is happening.
  234.  *  hWndParent      HWND of the window in which the object can play
  235.  *                  in-place.
  236.  *  pRectPos        LPRECT of the object in hWndParent where the
  237.  *                  object can play in-place if desired.
  238.  *
  239.  * Return Value:
  240.  *  HRESULT         NOERROR or a general error value.
  241.  */
  242.  
  243. STDMETHODIMP CImpIOleObject::DoVerb(LONG iVerb, LPMSG pMSG
  244.     , LPOLECLIENTSITE pActiveSite, LONG lIndex, HWND hWndParent
  245.     , LPCRECT pRectPos)
  246.     {
  247.     HRESULT     hr;
  248.  
  249.     switch (iVerb)
  250.         {
  251.         case OLEIVERB_HIDE:
  252.             if (NULL!=m_pObj->m_hDlg)
  253.                 {
  254.                 ShowWindow(m_pObj->m_hDlg, SW_HIDE);
  255.                 m_pObj->SendAdvise(OBJECTCODE_HIDEWINDOW);
  256.                 }
  257.  
  258.             break;
  259.  
  260.         case OLEIVERB_PRIMARY:
  261.         case OLEIVERB_OPEN:
  262.         case OLEIVERB_SHOW:
  263.             /*
  264.              * If we're not running, make sure we are.  In any
  265.              * case, make the dialog visible and insure it has
  266.              * the right parent now.
  267.              */
  268.             hr=NOERROR;
  269.             if (NULL==m_pObj->m_hDlg)
  270.                 hr=m_pObj->m_pImpIRunnableObject->Run(NULL);
  271.  
  272.             if (FAILED(hr) || NULL==m_pObj->m_hDlg)
  273.                 return ResultFromScode(E_OUTOFMEMORY);
  274.  
  275.             ShowWindow(m_pObj->m_hDlg, SW_SHOW);
  276.             SetFocus(m_pObj->m_hDlg);
  277.             m_pObj->SendAdvise(OBJECTCODE_SHOWOBJECT);
  278.             m_pObj->SendAdvise(OBJECTCODE_SHOWWINDOW);
  279.  
  280.             break;
  281.  
  282.         default:
  283.             return ResultFromScode(OLEOBJ_S_INVALIDVERB);
  284.         }
  285.  
  286.     return NOERROR;
  287.     }
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294. /*
  295.  * CImpIOleObject::GetUserClassID
  296.  *
  297.  * Purpose:
  298.  *  Used for linked objects, this returns the class ID of what end
  299.  *  users think they are editing.
  300.  *
  301.  * Parameters:
  302.  *  pClsID          LPCLSID in which to store the CLSID.
  303.  *
  304.  * Return Value:
  305.  *  HRESULT         NOERROR or a general error value.
  306.  */
  307.  
  308. STDMETHODIMP CImpIOleObject::GetUserClassID(LPCLSID pClsID)
  309.     {
  310.     /*
  311.      * If you are not registered to handle data other than yourself,
  312.      * then you can just return your class ID here.  If you are
  313.      * registered as usable from Treat-As dialogs, then you need to
  314.      * return the CLSID of what you are really editing.
  315.      */
  316.  
  317.     *pClsID=CLSID_Polyline19;
  318.     return NOERROR;
  319.     }
  320.  
  321.  
  322.  
  323.  
  324.  
  325. /*
  326.  * CImpIOleObject::SetExtent
  327.  *
  328.  * Purpose:
  329.  *  Sets the size of the object in HIMETRIC units.  Since we're in
  330.  *  a dialog, the size of the object in us is fixed, so we ignore
  331.  *  this call.
  332.  *
  333.  * Parameters:
  334.  *  dwAspect        DWORD of the aspect affected.
  335.  *  pszl            LPSIZEL containing the new size.
  336.  *
  337.  * Return Value:
  338.  *  HRESULT         NOERROR or a general error value.
  339.  */
  340.  
  341. STDMETHODIMP CImpIOleObject::SetExtent(DWORD dwAspect
  342.     , LPSIZEL pszl)
  343.     {
  344.     //Ignored:  no size change in the dialog.
  345.     return NOERROR;
  346.     }
  347.  
  348.  
  349.  
  350.  
  351.  
  352. /*
  353.  * CImpIOleObject::GetExtent
  354.  *
  355.  * Purpose:
  356.  *  Retrieves the size of the object in HIMETRIC units.
  357.  *
  358.  * Parameters:
  359.  *  dwAspect        DWORD of the aspect requested
  360.  *  pszl            LPSIZEL into which to store the size.
  361.  *
  362.  * Return Value:
  363.  *  HRESULT         NOERROR or a general error value.
  364.  */
  365.  
  366. STDMETHODIMP CImpIOleObject::GetExtent(DWORD dwAspect, LPSIZEL pszl)
  367.     {
  368.     //Delegate directly to IViewObject2::GetExtent
  369.     return m_pObj->m_pImpIViewObject->GetExtent(dwAspect, -1
  370.         , NULL, pszl);
  371.     }
  372.  
  373.  
  374.  
  375.  
  376.  
  377. /*
  378.  * CImpIOleObject::Advise
  379.  * CImpIOleObject::Unadvise
  380.  * CImpIOleObject::EnumAdvise
  381.  *
  382.  * Purpose:
  383.  *  Advisory connection functions.
  384.  */
  385.  
  386. STDMETHODIMP CImpIOleObject::Advise(LPADVISESINK pIAdviseSink
  387.     , LPDWORD pdwConn)
  388.     {
  389.     if (NULL==m_pObj->m_pIOleAdviseHolder)
  390.         {
  391.         HRESULT     hr;
  392.  
  393.         hr=CreateOleAdviseHolder(&m_pObj->m_pIOleAdviseHolder);
  394.  
  395.         if (FAILED(hr))
  396.             return hr;
  397.         }
  398.  
  399.     return m_pObj->m_pIOleAdviseHolder->Advise(pIAdviseSink
  400.         , pdwConn);
  401.     }
  402.  
  403.  
  404. STDMETHODIMP CImpIOleObject::Unadvise(DWORD dwConn)
  405.     {
  406.     if (NULL!=m_pObj->m_pIOleAdviseHolder)
  407.         return m_pObj->m_pIOleAdviseHolder->Unadvise(dwConn);
  408.  
  409.     return ResultFromScode(E_FAIL);
  410.     }
  411.  
  412.  
  413. STDMETHODIMP CImpIOleObject::EnumAdvise(LPENUMSTATDATA *ppEnum)
  414.     {
  415.     if (NULL!=m_pObj->m_pIOleAdviseHolder)
  416.         return m_pObj->m_pIOleAdviseHolder->EnumAdvise(ppEnum);
  417.  
  418.     return ResultFromScode(E_FAIL);
  419.     }
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426. //Methods not implemented or trivial
  427.  
  428. STDMETHODIMP CImpIOleObject::SetMoniker(DWORD dwWhich
  429.     , LPMONIKER pmk)
  430.     {
  431.     return ResultFromScode(E_NOTIMPL);
  432.     }
  433.  
  434. STDMETHODIMP CImpIOleObject::GetMoniker(DWORD dwAssign
  435.     , DWORD dwWhich, LPMONIKER *ppmk)
  436.     {
  437.     return ResultFromScode(E_NOTIMPL);
  438.     }
  439.  
  440. STDMETHODIMP CImpIOleObject::InitFromData(LPDATAOBJECT pIDataObject
  441.     , BOOL fCreation, DWORD dw)
  442.     {
  443.     return ResultFromScode(E_NOTIMPL);
  444.     }
  445.  
  446. STDMETHODIMP CImpIOleObject::GetClipboardData(DWORD dwReserved
  447.     , LPDATAOBJECT *ppIDataObj)
  448.     {
  449.     return ResultFromScode(E_NOTIMPL);
  450.     }
  451.  
  452. STDMETHODIMP CImpIOleObject::Update(void)
  453.     {
  454.     return NOERROR;
  455.     }
  456.  
  457. STDMETHODIMP CImpIOleObject::IsUpToDate(void)
  458.     {
  459.     return NOERROR;
  460.     }
  461.  
  462. STDMETHODIMP CImpIOleObject::SetColorScheme(LPLOGPALETTE pLP)
  463.     {
  464.     return ResultFromScode(E_NOTIMPL);
  465.     }
  466.  
  467.  
  468.  
  469. //Methods implemented using registry helper functions in OLE.
  470.  
  471. STDMETHODIMP CImpIOleObject::EnumVerbs(LPENUMOLEVERB *ppEnum)
  472.     {
  473.     return OleRegEnumVerbs(m_pObj->m_clsID, ppEnum);
  474.     }
  475.  
  476. STDMETHODIMP CImpIOleObject::GetUserType(DWORD dwForm
  477.     , LPOLESTR *ppszType)
  478.     {
  479.     return OleRegGetUserType(m_pObj->m_clsID, dwForm, ppszType);
  480.     }
  481.  
  482. STDMETHODIMP CImpIOleObject::GetMiscStatus(DWORD dwAspect
  483.     , LPDWORD pdwStatus)
  484.     {
  485.     return OleRegGetMiscStatus(m_pObj->m_clsID, dwAspect
  486.         , pdwStatus);
  487.     }
  488.