home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oledocvw.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  12KB  |  455 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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. void CDocObjectServer::OnSaveViewState(CArchive& /* ar */)
  21. {
  22.     // user can override to save some state or do something neat
  23. }
  24.  
  25. void CDocObjectServer::OnApplyViewState(CArchive& /* ar */)
  26. {
  27.     // user can override to restore state or do something neat
  28. }
  29.  
  30.  
  31. HRESULT CDocObjectServer::OnActivateView()
  32. {
  33.     USES_CONVERSION;
  34.     ASSERT_VALID(this);
  35.  
  36.     HRESULT hr = E_FAIL;
  37.  
  38.     // Can't in-place activate without a client site
  39.     if (m_pOwner->m_lpClientSite == NULL)
  40.         return NOERROR;
  41.  
  42.     // build object title/name (the container may use this in its caption)
  43.     CString strFileType, strTitle;
  44.     if (!m_pOwner->GetFileTypeString(strFileType))
  45.         return E_FAIL;
  46.     AfxFormatString2(strTitle, AFX_IDS_OBJ_TITLE_INPLACE,
  47.         AfxGetAppName(), strFileType);
  48.  
  49.     // Find our view site
  50.     LPOLEINPLACESITE lpInPlaceSite = NULL;
  51.     lpInPlaceSite = m_pViewSite;
  52.     if (lpInPlaceSite == NULL)
  53.         return E_FAIL;
  54.     lpInPlaceSite->AddRef();
  55.  
  56.     // start activation sequence...
  57.     if ((hr = lpInPlaceSite->OnInPlaceActivate()) != NOERROR)
  58.         goto ReleaseAndFail;
  59.  
  60.     // we'll need the parent window to create the CDocObjectIPFrameWnd
  61.     HWND hWnd;
  62.     VERIFY(lpInPlaceSite->GetWindow(&hWnd) == NOERROR);
  63.     CWnd* pParentWnd;
  64.     pParentWnd = CWnd::FromHandle(hWnd);
  65.  
  66.     // create the inplace frame window
  67.     COleIPFrameWnd* pFrameWnd;
  68.  
  69.     // if we've not been activate before, we'll need
  70.     // to create our frame at this time
  71.  
  72.     if (m_pOwner->m_pInPlaceFrame != NULL)
  73.         pFrameWnd = m_pOwner->m_pInPlaceFrame;
  74.     else
  75.     {
  76.         pFrameWnd = (COleIPFrameWnd*) m_pOwner->CreateInPlaceFrame(pParentWnd);
  77.         if (pFrameWnd == NULL)
  78.         {
  79.             ASSERT(lpInPlaceSite != NULL);
  80.             lpInPlaceSite->OnInPlaceDeactivate();
  81.             goto ReleaseAndFail;
  82.         }
  83.  
  84.         ASSERT(pFrameWnd->GetParent() == pParentWnd);
  85.         m_pOwner->m_pInPlaceFrame = pFrameWnd;
  86.  
  87.         // need to get frame & doc window interfaces as well as other info
  88.         RECT rcPosRect, rcClipRect;
  89.         if ((hr = lpInPlaceSite->GetWindowContext(
  90.             &pFrameWnd->m_lpFrame, &pFrameWnd->m_lpDocFrame,
  91.             &rcPosRect, &rcClipRect, &pFrameWnd->m_frameInfo)) != NOERROR)
  92.         {
  93.             goto DestroyFrameAndFail;
  94.         }
  95.         ASSERT(pFrameWnd->m_lpFrame != NULL);
  96.  
  97.         // send activate notification
  98.         if ((hr = lpInPlaceSite->OnUIActivate()) != NOERROR)
  99.             goto DestroyFrameAndFail;
  100.  
  101.         // setup the shared menu
  102.         if (!pFrameWnd->BuildSharedMenu())
  103.             goto DeactivateUIAndFail;
  104.  
  105.         // allow server to install frame controls in container
  106.  
  107.         VERIFY(pFrameWnd->m_lpFrame->GetWindow(&hWnd) == NOERROR);
  108.  
  109.         pFrameWnd->m_pMainFrame = new COleCntrFrameWnd(pFrameWnd);
  110.         pFrameWnd->m_pMainFrame->Attach(hWnd);
  111.  
  112.         if (pFrameWnd->m_lpDocFrame != NULL)
  113.         {
  114.             VERIFY(pFrameWnd->m_lpDocFrame->GetWindow(&hWnd) == NOERROR);
  115.             pFrameWnd->m_pDocFrame = new COleCntrFrameWnd(pFrameWnd);
  116.             pFrameWnd->m_pDocFrame->Attach(hWnd);
  117.         }
  118.         // update zoom factor information before creating control bars
  119.         pFrameWnd->m_rectPos.CopyRect(&rcPosRect);
  120.         pFrameWnd->m_rectClip.CopyRect(&rcClipRect);
  121.         if (!pFrameWnd->OnCreateControlBars(pFrameWnd->m_pMainFrame,
  122.             pFrameWnd->m_pDocFrame))
  123.         {
  124.             goto DeactivateUIAndFail;
  125.         }
  126.     }
  127.  
  128.     // set the active object
  129.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  130.     LPOLEINPLACEACTIVEOBJECT lpActiveObject;
  131.     lpActiveObject = (LPOLEINPLACEACTIVEOBJECT)
  132.         m_pOwner->GetInterface(&IID_IOleInPlaceActiveObject);
  133.  
  134.     pFrameWnd->m_lpFrame->SetActiveObject(lpActiveObject,
  135.         T2OLE((LPTSTR) (LPCTSTR) strTitle));
  136.  
  137.     if (pFrameWnd->m_lpDocFrame != NULL)
  138.     {
  139.         pFrameWnd->m_lpDocFrame->SetActiveObject(lpActiveObject,
  140.             T2OLE((LPTSTR) (LPCTSTR) strTitle));
  141.     }
  142.  
  143.     // add frame & document level frame controls
  144.     ASSERT(m_pOwner->m_pInPlaceFrame == pFrameWnd);
  145.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  146.     m_pOwner->OnShowControlBars(pFrameWnd->m_pMainFrame, TRUE);
  147.     if (pFrameWnd->m_lpDocFrame != NULL)
  148.         m_pOwner->OnShowControlBars(pFrameWnd->m_pDocFrame, TRUE);
  149.  
  150.     // show any hidden modeless dialogs as well...
  151.     pFrameWnd->ShowOwnedWindows(TRUE);
  152.  
  153.     // attempt toolbar negotiation
  154.     m_pOwner->OnResizeBorder(NULL, pFrameWnd->m_lpFrame, TRUE);
  155.     if (pFrameWnd->m_lpDocFrame != NULL)
  156.         m_pOwner->OnResizeBorder(NULL, pFrameWnd->m_lpDocFrame, FALSE);
  157.  
  158.     // install the menu (also installs a hook which forwards messages from
  159.     //  the menu to the inplace frame window)
  160.     pFrameWnd->m_lpFrame->SetMenu(pFrameWnd->m_hSharedMenu,
  161.         pFrameWnd->m_hOleMenu, pFrameWnd->m_hWnd);
  162.  
  163.     // finally -- show the inplace frame window and set focus
  164.     pFrameWnd->ShowWindow(SW_SHOW);
  165.     pFrameWnd->SetFocus();
  166.     pFrameWnd->UpdateWindow();
  167.  
  168.     // allow the main window to be set
  169.     m_pOwner->OnFrameWindowActivate(TRUE);
  170.     pFrameWnd->m_bUIActive = TRUE;
  171.  
  172.     // cleanup and return
  173.     lpInPlaceSite->Release();
  174.     return hr;
  175.  
  176. DeactivateUIAndFail:
  177.     ASSERT(lpInPlaceSite != NULL);
  178.     lpInPlaceSite->OnUIDeactivate(FALSE);
  179.  
  180. DestroyFrameAndFail:
  181.     if (m_pOwner->m_pInPlaceFrame != NULL)
  182.     {
  183.         ASSERT(pFrameWnd != NULL);
  184.         m_pOwner->DestroyInPlaceFrame(pFrameWnd);
  185.         m_pOwner->m_pInPlaceFrame = NULL;
  186.  
  187.         // also need to send OnInPlaceDeactivate notification
  188.         ASSERT(lpInPlaceSite != NULL);
  189.         lpInPlaceSite->OnInPlaceDeactivate();
  190.     }
  191. ReleaseAndFail:
  192.     ASSERT(lpInPlaceSite != NULL);
  193.     lpInPlaceSite->Release();
  194.  
  195.     return hr;
  196. }
  197.  
  198. STDMETHODIMP_(ULONG) CDocObjectServer::XOleDocumentView::AddRef()
  199. {
  200.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  201.     return pThis->m_pOwner->ExternalAddRef();
  202. }
  203.  
  204. STDMETHODIMP_(ULONG) CDocObjectServer::XOleDocumentView::Release()
  205. {
  206.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  207.     return pThis->m_pOwner->ExternalRelease();
  208. }
  209.  
  210. STDMETHODIMP CDocObjectServer::XOleDocumentView::QueryInterface(REFIID iid, LPVOID* ppvObj)
  211. {
  212.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  213.     return pThis->m_pOwner->ExternalQueryInterface(&iid, ppvObj);
  214. }
  215.  
  216. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetInPlaceSite(
  217.     LPOLEINPLACESITE pIPSite)
  218. {
  219.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  220.     ASSERT_VALID(pThis);
  221.  
  222.     // if currently inplace active, then do normal inplace deactivation
  223.     if (pThis->m_pOwner->IsInPlaceActive())
  224.         pThis->m_pOwner->m_xOleInPlaceObject.InPlaceDeactivate();
  225.  
  226.     // release the view site pointer
  227.     if (pThis->m_pViewSite)
  228.         pThis->m_pViewSite->Release();
  229.  
  230.     // remember the new view site pointer and addref it, if it is non-NULL
  231.     pThis->m_pViewSite = pIPSite;
  232.     if (pThis->m_pViewSite != NULL)
  233.         pThis->m_pViewSite->AddRef();
  234.  
  235.     return NOERROR;
  236. }
  237.  
  238. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetInPlaceSite(LPOLEINPLACESITE* ppIPSite)
  239. {
  240.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  241.     ASSERT_VALID(pThis);
  242.     ASSERT(ppIPSite != NULL);
  243.  
  244.     if (pThis->m_pViewSite)
  245.         pThis->m_pViewSite->AddRef();
  246.     *ppIPSite = pThis->m_pViewSite;
  247.  
  248.     return NOERROR;
  249. }
  250.  
  251. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetDocument(LPUNKNOWN* ppUnk)
  252. {
  253.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  254.     ASSERT_VALID(pThis);
  255.     ASSERT(ppUnk != NULL);
  256.  
  257.     HRESULT hr = pThis->m_xOleDocument.QueryInterface(IID_IUnknown,
  258.         (LPVOID*)ppUnk);
  259.     ASSERT(*ppUnk != NULL);
  260.  
  261.     return hr;
  262. }
  263.  
  264. void CDocObjectServer::OnSetItemRects(LPRECT lprcPosRect, LPRECT lprcClipRect)
  265. {
  266.     m_pOwner->OnSetItemRects(lprcPosRect, lprcClipRect);
  267. }
  268.  
  269. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetRect(LPRECT lprcView)
  270. {
  271.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  272.     ASSERT_VALID(pThis);
  273.     ASSERT(lprcView != NULL);
  274.  
  275.     HRESULT hr = E_UNEXPECTED;
  276.     TRY
  277.     {
  278.         pThis->OnSetItemRects(lprcView, lprcView);
  279.         hr = NOERROR;
  280.     }
  281.     END_TRY
  282.  
  283.     return hr;
  284. }
  285.  
  286. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetRect(LPRECT lprcView)
  287. {
  288.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  289.     ASSERT_VALID(pThis);
  290.     ASSERT(lprcView != NULL);
  291.  
  292.     pThis->m_pOwner->GetItemPosition(lprcView);
  293.     return NOERROR;
  294. }
  295.  
  296. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetRectComplex(
  297.     LPRECT lprcView, LPRECT lprcHScroll,
  298.     LPRECT lprcVScroll, LPRECT lprcSizeBox)
  299. {
  300.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  301.     ASSERT_VALID(pThis);
  302.  
  303.     UNUSED_ALWAYS(lprcView);
  304.     UNUSED_ALWAYS(lprcHScroll);
  305.     UNUSED_ALWAYS(lprcVScroll);
  306.     UNUSED_ALWAYS(lprcSizeBox);
  307.  
  308.     // We don't support complex rectangles, so return error
  309.     return E_NOTIMPL;
  310. }
  311.  
  312. STDMETHODIMP CDocObjectServer::XOleDocumentView::Show(BOOL bShow)
  313. {
  314.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  315.     ASSERT_VALID(pThis);
  316.  
  317.     HRESULT hr = NOERROR;
  318.  
  319.     if (bShow)
  320.     {
  321.         // in-place but don't UI activate; give the view focus
  322.         hr = pThis->m_pOwner->ActivateInPlace();
  323.     }
  324.     else
  325.     {
  326.         // Call IOleInPlaceObject::InPlaceDeactivate on this view
  327.         hr = pThis->m_pOwner->m_xOleInPlaceObject.InPlaceDeactivate();
  328.     }
  329.  
  330.     return hr;
  331. }
  332.  
  333. STDMETHODIMP CDocObjectServer::XOleDocumentView::UIActivate(BOOL bUIActivate)
  334. {
  335.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  336.     ASSERT_VALID(pThis);
  337.  
  338.     HRESULT hr = NOERROR;
  339.  
  340.     if (bUIActivate)
  341.     {
  342.         // UI Activate the view then take focus and bring the view forward
  343.         hr = pThis->OnActivateView();
  344.     }
  345.     else
  346.     {
  347.         // Call IOleInPlaceObject::UIDeactivate on this view
  348.         hr = pThis->m_pOwner->m_xOleInPlaceObject.UIDeactivate();
  349.     }
  350.     return hr;
  351. }
  352.  
  353. STDMETHODIMP CDocObjectServer::XOleDocumentView::Open()
  354. {
  355.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  356.     ASSERT_VALID(pThis);
  357.  
  358.     return pThis->m_pOwner->m_xOleObject.DoVerb(OLEIVERB_OPEN, NULL,
  359.         NULL, 0, NULL, NULL);
  360. }
  361.  
  362. STDMETHODIMP CDocObjectServer::XOleDocumentView::CloseView(DWORD /* dwReserved */)
  363. {
  364.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  365.     ASSERT_VALID(pThis);
  366.  
  367.     // Call IOleDocumentView::Show(FALSE) to hide the view
  368.     Show(FALSE);
  369.  
  370.     // Call IOleDocumentView::SetInPlaceSite(NULL) to deactivate the object
  371.     HRESULT hr = SetInPlaceSite(NULL);
  372.  
  373.     return hr;
  374. }
  375.  
  376. STDMETHODIMP CDocObjectServer::XOleDocumentView::SaveViewState(LPSTREAM pstm)
  377. {
  378.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  379.     ASSERT_VALID(pThis);
  380.  
  381.     HRESULT hr = NOERROR;
  382.  
  383.     // Attach the stream to an MFC file object
  384.     COleStreamFile file;
  385.     file.Attach(pstm);
  386.     CFileException fe;
  387.  
  388.     // save it via a CArchive
  389.     CArchive saveArchive(&file, CArchive::store | CArchive::bNoFlushOnDelete);
  390.     TRY
  391.     {
  392.         pThis->OnSaveViewState(saveArchive);
  393.         saveArchive.Close();
  394.         file.Detach();
  395.     }
  396.     CATCH(COleException, pOE)
  397.     {
  398.         hr = pOE->m_sc;
  399.     }
  400.     AND_CATCH_ALL(e)
  401.     {
  402.         hr = E_UNEXPECTED;
  403.     }
  404.     END_CATCH_ALL
  405.  
  406.     return hr;
  407. }
  408.  
  409. STDMETHODIMP CDocObjectServer::XOleDocumentView::ApplyViewState(LPSTREAM pstm)
  410. {
  411.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  412.     ASSERT_VALID(pThis);
  413.  
  414.     HRESULT hr = NOERROR;
  415.  
  416.     // Attach the stream to an MFC file object
  417.     COleStreamFile file;
  418.     file.Attach(pstm);
  419.     CFileException fe;
  420.  
  421.     // load it with CArchive
  422.     CArchive loadArchive(&file, CArchive::load | CArchive::bNoFlushOnDelete);
  423.     TRY
  424.     {
  425.         pThis->OnApplyViewState(loadArchive);
  426.         loadArchive.Close();
  427.         file.Detach();
  428.     }
  429.     CATCH(COleException, pOE)
  430.     {
  431.         hr = pOE->m_sc;
  432.     }
  433.     AND_CATCH_ALL(e)
  434.     {
  435.         hr = E_UNEXPECTED;
  436.     }
  437.     END_CATCH_ALL
  438.  
  439.     return hr;
  440. }
  441.  
  442. STDMETHODIMP CDocObjectServer::XOleDocumentView::Clone(
  443.     LPOLEINPLACESITE pipsiteNew, LPOLEDOCUMENTVIEW* ppviewNew)
  444. {
  445.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  446.     ASSERT_VALID(pThis);
  447.  
  448.     UNUSED_ALWAYS(pipsiteNew);
  449.     UNUSED_ALWAYS(ppviewNew);
  450.  
  451.     // In order to support this, we would need to support multiple views,
  452.     // which we do not.  So we will return an error.
  453.     return E_NOTIMPL;
  454. }
  455.