home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLEDOCVW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  11.8 KB  |  455 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. #include <afxpriv.h>     // for COleCntrFrameWnd
  13.  
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char BASED_CODE THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. #define new DEBUG_NEW
  20.  
  21. void CDocObjectServer::OnSaveViewState(CArchive& /* ar */)
  22. {
  23.     // user can override to save some state or do something neat
  24. }
  25.  
  26. void CDocObjectServer::OnApplyViewState(CArchive& /* ar */)
  27. {
  28.     // user can override to restore state or do something neat
  29. }
  30.  
  31.  
  32. HRESULT CDocObjectServer::OnActivateView()
  33. {
  34.     USES_CONVERSION;
  35.     ASSERT_VALID(this);
  36.  
  37.     HRESULT hr = E_FAIL;
  38.  
  39.     // Can't in-place activate without a client site
  40.     if (m_pOwner->m_lpClientSite == NULL)
  41.         return NOERROR;
  42.  
  43.     // build object title/name (the container may use this in its caption)
  44.     CString strFileType, strTitle;
  45.     if (!m_pOwner->GetFileTypeString(strFileType))
  46.         return E_FAIL;
  47.     AfxFormatString2(strTitle, AFX_IDS_OBJ_TITLE_INPLACE,
  48.         AfxGetAppName(), strFileType);
  49.  
  50.     // Find our view site
  51.     LPOLEINPLACESITE lpInPlaceSite = NULL;
  52.     lpInPlaceSite = m_pViewSite;
  53.     if (lpInPlaceSite == NULL)
  54.         return E_FAIL;
  55.     lpInPlaceSite->AddRef();
  56.  
  57.     // start activation sequence...
  58.     if ((hr = lpInPlaceSite->OnInPlaceActivate()) != NOERROR)
  59.         goto ReleaseAndFail;
  60.  
  61.     // we'll need the parent window to create the CDocObjectIPFrameWnd
  62.     HWND hWnd;
  63.     VERIFY(lpInPlaceSite->GetWindow(&hWnd) == NOERROR);
  64.     CWnd* pParentWnd;
  65.     pParentWnd = CWnd::FromHandle(hWnd);
  66.  
  67.     // create the inplace frame window
  68.     COleIPFrameWnd* pFrameWnd;
  69.  
  70.     // if we've not been activate before, we'll need
  71.     // to create our frame at this time
  72.  
  73.     if (m_pOwner->m_pInPlaceFrame != NULL)
  74.         pFrameWnd = m_pOwner->m_pInPlaceFrame;
  75.     else
  76.     {
  77.         pFrameWnd = (COleIPFrameWnd*) m_pOwner->CreateInPlaceFrame(pParentWnd);
  78.         if (pFrameWnd == NULL)
  79.         {
  80.             ASSERT(lpInPlaceSite != NULL);
  81.             lpInPlaceSite->OnInPlaceDeactivate();
  82.             goto ReleaseAndFail;
  83.         }
  84.  
  85.         ASSERT(pFrameWnd->GetParent() == pParentWnd);
  86.         m_pOwner->m_pInPlaceFrame = pFrameWnd;
  87.  
  88.         // need to get frame & doc window interfaces as well as other info
  89.         RECT rcPosRect, rcClipRect;
  90.         if ((hr = lpInPlaceSite->GetWindowContext(
  91.             &pFrameWnd->m_lpFrame, &pFrameWnd->m_lpDocFrame,
  92.             &rcPosRect, &rcClipRect, &pFrameWnd->m_frameInfo)) != NOERROR)
  93.         {
  94.             goto DestroyFrameAndFail;
  95.         }
  96.         ASSERT(pFrameWnd->m_lpFrame != NULL);
  97.  
  98.         // send activate notification
  99.         if ((hr = lpInPlaceSite->OnUIActivate()) != NOERROR)
  100.             goto DestroyFrameAndFail;
  101.  
  102.         // setup the shared menu
  103.         if (!pFrameWnd->BuildSharedMenu())
  104.             goto DeactivateUIAndFail;
  105.  
  106.         // allow server to install frame controls in container
  107.  
  108.         VERIFY(pFrameWnd->m_lpFrame->GetWindow(&hWnd) == NOERROR);
  109.  
  110.         pFrameWnd->m_pMainFrame = new COleCntrFrameWnd(pFrameWnd);
  111.         pFrameWnd->m_pMainFrame->Attach(hWnd);
  112.  
  113.         if (pFrameWnd->m_lpDocFrame != NULL)
  114.         {
  115.             VERIFY(pFrameWnd->m_lpDocFrame->GetWindow(&hWnd) == NOERROR);
  116.             pFrameWnd->m_pDocFrame = new COleCntrFrameWnd(pFrameWnd);
  117.             pFrameWnd->m_pDocFrame->Attach(hWnd);
  118.         }
  119.         // update zoom factor information before creating control bars
  120.         pFrameWnd->m_rectPos.CopyRect(&rcPosRect);
  121.         pFrameWnd->m_rectClip.CopyRect(&rcClipRect);
  122.         if (!pFrameWnd->OnCreateControlBars(pFrameWnd->m_pMainFrame,
  123.             pFrameWnd->m_pDocFrame))
  124.         {
  125.             goto DeactivateUIAndFail;
  126.         }
  127.     }
  128.  
  129.     // set the active object
  130.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  131.     LPOLEINPLACEACTIVEOBJECT lpActiveObject;
  132.     lpActiveObject = (LPOLEINPLACEACTIVEOBJECT)
  133.         m_pOwner->GetInterface(&IID_IOleInPlaceActiveObject);
  134.  
  135.     pFrameWnd->m_lpFrame->SetActiveObject(lpActiveObject,
  136.         T2OLE((LPTSTR) (LPCTSTR) strTitle));
  137.  
  138.     if (pFrameWnd->m_lpDocFrame != NULL)
  139.     {
  140.         pFrameWnd->m_lpDocFrame->SetActiveObject(lpActiveObject,
  141.             T2OLE((LPTSTR) (LPCTSTR) strTitle));
  142.     }
  143.  
  144.     // add frame & document level frame controls
  145.     ASSERT(m_pOwner->m_pInPlaceFrame == pFrameWnd);
  146.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  147.     m_pOwner->OnShowControlBars(pFrameWnd->m_pMainFrame, TRUE);
  148.     if (pFrameWnd->m_lpDocFrame != NULL)
  149.         m_pOwner->OnShowControlBars(pFrameWnd->m_pDocFrame, TRUE);
  150.  
  151.     // show any hidden modeless dialogs as well...
  152.     pFrameWnd->ShowOwnedWindows(TRUE);
  153.  
  154.     // attempt toolbar negotiation
  155.     m_pOwner->OnResizeBorder(NULL, pFrameWnd->m_lpFrame, TRUE);
  156.     if (pFrameWnd->m_lpDocFrame != NULL)
  157.         m_pOwner->OnResizeBorder(NULL, pFrameWnd->m_lpDocFrame, FALSE);
  158.  
  159.     // install the menu (also installs a hook which forwards messages from
  160.     //  the menu to the inplace frame window)
  161.     pFrameWnd->m_lpFrame->SetMenu(pFrameWnd->m_hSharedMenu,
  162.         pFrameWnd->m_hOleMenu, pFrameWnd->m_hWnd);
  163.  
  164.     // finally -- show the inplace frame window and set focus
  165.     pFrameWnd->ShowWindow(SW_SHOW);
  166.     pFrameWnd->SetFocus();
  167.     pFrameWnd->UpdateWindow();
  168.  
  169.     // allow the main window to be set
  170.     m_pOwner->OnFrameWindowActivate(TRUE);
  171.     pFrameWnd->m_bUIActive = TRUE;
  172.  
  173.     // cleanup and return
  174.     lpInPlaceSite->Release();
  175.     return hr;
  176.  
  177. DeactivateUIAndFail:
  178.     ASSERT(lpInPlaceSite != NULL);
  179.     lpInPlaceSite->OnUIDeactivate(FALSE);
  180.  
  181. DestroyFrameAndFail:
  182.     if (m_pOwner->m_pInPlaceFrame != NULL)
  183.     {
  184.         ASSERT(pFrameWnd != NULL);
  185.         m_pOwner->DestroyInPlaceFrame(pFrameWnd);
  186.         m_pOwner->m_pInPlaceFrame = NULL;
  187.  
  188.         // also need to send OnInPlaceDeactivate notification
  189.         ASSERT(lpInPlaceSite != NULL);
  190.         lpInPlaceSite->OnInPlaceDeactivate();
  191.     }
  192. ReleaseAndFail:
  193.     ASSERT(lpInPlaceSite != NULL);
  194.     lpInPlaceSite->Release();
  195.  
  196.     return hr;
  197. }
  198.  
  199. STDMETHODIMP_(ULONG) CDocObjectServer::XOleDocumentView::AddRef()
  200. {
  201.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  202.     return pThis->m_pOwner->ExternalAddRef();
  203. }
  204.  
  205. STDMETHODIMP_(ULONG) CDocObjectServer::XOleDocumentView::Release()
  206. {
  207.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  208.     return pThis->m_pOwner->ExternalRelease();
  209. }
  210.  
  211. STDMETHODIMP CDocObjectServer::XOleDocumentView::QueryInterface(REFIID iid, LPVOID* ppvObj)
  212. {
  213.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  214.     return pThis->m_pOwner->ExternalQueryInterface(&iid, ppvObj);
  215. }
  216.  
  217. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetInPlaceSite(
  218.     LPOLEINPLACESITE pIPSite)
  219. {
  220.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  221.     ASSERT_VALID(pThis);
  222.  
  223.     // if currently inplace active, then do normal inplace deactivation
  224.     if (pThis->m_pOwner->IsInPlaceActive())
  225.         pThis->m_pOwner->m_xOleInPlaceObject.InPlaceDeactivate();
  226.  
  227.     // release the view site pointer
  228.     if (pThis->m_pViewSite)
  229.         pThis->m_pViewSite->Release();
  230.  
  231.     // remember the new view site pointer and addref it, if it is non-NULL
  232.     pThis->m_pViewSite = pIPSite;
  233.     if (pThis->m_pViewSite != NULL)
  234.         pThis->m_pViewSite->AddRef();
  235.  
  236.     return NOERROR;
  237. }
  238.  
  239. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetInPlaceSite(LPOLEINPLACESITE* ppIPSite)
  240. {
  241.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  242.     ASSERT_VALID(pThis);
  243.     ASSERT(ppIPSite != NULL);
  244.  
  245.     if (pThis->m_pViewSite)
  246.         pThis->m_pViewSite->AddRef();
  247.     *ppIPSite = pThis->m_pViewSite;
  248.  
  249.     return NOERROR;
  250. }
  251.  
  252. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetDocument(LPUNKNOWN* ppUnk)
  253. {
  254.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  255.     ASSERT_VALID(pThis);
  256.     ASSERT(ppUnk != NULL);
  257.  
  258.     HRESULT hr = pThis->m_xOleDocument.QueryInterface(IID_IUnknown,
  259.         (LPVOID*)ppUnk);
  260.     ASSERT(*ppUnk != NULL);
  261.  
  262.     return hr;
  263. }
  264.  
  265. void CDocObjectServer::OnSetItemRects(LPRECT lprcPosRect, LPRECT lprcClipRect)
  266. {
  267.     m_pOwner->OnSetItemRects(lprcPosRect, lprcClipRect);
  268. }
  269.  
  270. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetRect(LPRECT lprcView)
  271. {
  272.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  273.     ASSERT_VALID(pThis);
  274.     ASSERT(lprcView != NULL);
  275.  
  276.     HRESULT hr = E_UNEXPECTED;
  277.     TRY
  278.     {
  279.         pThis->OnSetItemRects(lprcView, lprcView);
  280.         hr = NOERROR;
  281.     }
  282.     END_TRY
  283.  
  284.     return hr;
  285. }
  286.  
  287. STDMETHODIMP CDocObjectServer::XOleDocumentView::GetRect(LPRECT lprcView)
  288. {
  289.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  290.     ASSERT_VALID(pThis);
  291.     ASSERT(lprcView != NULL);
  292.  
  293.     pThis->m_pOwner->GetItemPosition(lprcView);
  294.     return NOERROR;
  295. }
  296.  
  297. STDMETHODIMP CDocObjectServer::XOleDocumentView::SetRectComplex(
  298.     LPRECT lprcView, LPRECT lprcHScroll,
  299.     LPRECT lprcVScroll, LPRECT lprcSizeBox)
  300. {
  301.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  302.     ASSERT_VALID(pThis);
  303.  
  304.     UNUSED_ALWAYS(lprcView);
  305.     UNUSED_ALWAYS(lprcHScroll);
  306.     UNUSED_ALWAYS(lprcVScroll);
  307.     UNUSED_ALWAYS(lprcSizeBox);
  308.  
  309.     // We don't support complex rectangles, so return error
  310.     return E_NOTIMPL;
  311. }
  312.  
  313. STDMETHODIMP CDocObjectServer::XOleDocumentView::Show(BOOL bShow)
  314. {
  315.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  316.     ASSERT_VALID(pThis);
  317.  
  318.     HRESULT hr = NOERROR;
  319.  
  320.     if (bShow)
  321.     {
  322.         // in-place but don't UI activate; give the view focus
  323.         hr = pThis->m_pOwner->ActivateInPlace();
  324.     }
  325.     else
  326.     {
  327.         // Call IOleInPlaceObject::InPlaceDeactivate on this view
  328.         hr = pThis->m_pOwner->m_xOleInPlaceObject.InPlaceDeactivate();
  329.     }
  330.  
  331.     return hr;
  332. }
  333.  
  334. STDMETHODIMP CDocObjectServer::XOleDocumentView::UIActivate(BOOL bUIActivate)
  335. {
  336.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  337.     ASSERT_VALID(pThis);
  338.  
  339.     HRESULT hr = NOERROR;
  340.  
  341.     if (bUIActivate)
  342.     {
  343.         // UI Activate the view then take focus and bring the view forward
  344.         hr = pThis->OnActivateView();
  345.     }
  346.     else
  347.     {
  348.         // Call IOleInPlaceObject::UIDeactivate on this view
  349.         hr = pThis->m_pOwner->m_xOleInPlaceObject.UIDeactivate();
  350.     }
  351.     return hr;
  352. }
  353.  
  354. STDMETHODIMP CDocObjectServer::XOleDocumentView::Open()
  355. {
  356.     METHOD_PROLOGUE_EX(CDocObjectServer, OleDocumentView)
  357.     ASSERT_VALID(pThis);
  358.  
  359.     return E_NOTIMPL;
  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.