home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / source / chap18 / doserver / bindview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-02  |  11.0 KB  |  402 lines

  1. // bindview.cpp : implementation of the Binder-aware DocObject OLE
  2. //                server document class IOleDocumentView interface
  3. //
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1995 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13. //
  14.  
  15. //BINDER
  16. // The DocObjectServerDoc class implements the binder document interface.
  17. // The code here makes the binder aware of our document and helps the
  18. // binder create and initialize views associated with the document.
  19. // The most important aspect of the binder document, though, is properly
  20. // managing the activation and deactivation of the view associated with
  21. // the document.
  22. //END_BINDER
  23.  
  24. #include "stdafx.h"
  25. #include <afxpriv.h>     // for COleCntrFrameWnd
  26. #include "binddoc.h"
  27. #include "bindipfw.h"
  28.  
  29. #ifdef _DEBUG
  30. #undef THIS_FILE
  31. static char BASED_CODE THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. void CDocObjectServerDoc::OnApplyViewState(CArchive& ar)
  35. {
  36. }
  37.  
  38. HRESULT CDocObjectServerDoc::OnActivateView()
  39. {
  40.     USES_CONVERSION;
  41.     ASSERT_VALID(this);
  42.     ASSERT(IsDocObject());
  43.  
  44.     HRESULT hr = E_FAIL;
  45.  
  46.     // Can't in-place activate without a client site
  47.     if (m_lpClientSite == NULL)
  48.         return NOERROR;
  49.  
  50.     ASSERT(m_pInPlaceFrame == NULL);
  51.  
  52.     // build object title/name (the container may use this in its caption)
  53.     CString strFileType, strTitle;
  54.     if (!GetFileTypeString(strFileType))
  55.         return E_FAIL;
  56.     AfxFormatString2(strTitle, AFX_IDS_OBJ_TITLE_INPLACE,
  57.         AfxGetAppName(), strFileType);
  58.  
  59.     // Find our view site
  60.     LPOLEINPLACESITE lpInPlaceSite = NULL;
  61.     lpInPlaceSite = m_pViewSite;
  62.     if (lpInPlaceSite == NULL)
  63.         return E_FAIL;
  64.     lpInPlaceSite->AddRef();
  65.  
  66.     // start activation sequence...
  67.      if ((hr = lpInPlaceSite->OnInPlaceActivate()) != NOERROR)
  68.         goto ReleaseAndFail;
  69.  
  70.     // we'll need the parent window to create the CDocObjectIPFrameWnd
  71.     HWND hWnd;
  72.     VERIFY(lpInPlaceSite->GetWindow(&hWnd) == NOERROR);
  73.     CWnd* pParentWnd;
  74.     pParentWnd = CWnd::FromHandle(hWnd);
  75.  
  76.     // create the inplace frame window
  77.     CDocObjectIPFrameWnd* pFrameWnd;
  78.     pFrameWnd = (CDocObjectIPFrameWnd*)CreateInPlaceFrame(pParentWnd);
  79.     if (pFrameWnd == NULL)
  80.     {
  81.         ASSERT(lpInPlaceSite != NULL);
  82.         lpInPlaceSite->OnInPlaceDeactivate();
  83.         goto ReleaseAndFail;
  84.     }
  85.  
  86.     // DocObjects need some special work done in the in-place
  87.     // frame window.  It should be derived from CDocObjectIPFrameWnd
  88.     ASSERT(pFrameWnd->IsKindOf(RUNTIME_CLASS(CDocObjectIPFrameWnd)));
  89.  
  90.     ASSERT(pFrameWnd->GetParent() == pParentWnd);
  91.     m_pInPlaceFrame = pFrameWnd;
  92.  
  93.     // need to get frame & doc window interfaces as well as other info
  94.     RECT rcPosRect, rcClipRect;
  95.     if ((hr = lpInPlaceSite->GetWindowContext(
  96.         &pFrameWnd->m_lpFrame, &pFrameWnd->m_lpDocFrame,
  97.         &rcPosRect, &rcClipRect, &pFrameWnd->m_frameInfo)) != NOERROR)
  98.     {
  99.         goto DestroyFrameAndFail;
  100.     }
  101.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  102.  
  103.     // TODO: Retrieve pointer to IDocObjectCommandTarget from
  104.     //         pFrameWnd->m_lpFrame, and save it somewhere
  105.  
  106.     // send activate notification
  107.     if ((hr = lpInPlaceSite->OnUIActivate()) != NOERROR)
  108.         goto DestroyFrameAndFail;
  109.  
  110.     // setup the shared menu
  111.     if (!pFrameWnd->BuildSharedMenu())
  112.         goto DeactivateUIAndFail;
  113.  
  114.     // allow server to install frame controls in container
  115.     VERIFY(pFrameWnd->m_lpFrame->GetWindow(&hWnd) == NOERROR);
  116.     pFrameWnd->m_pMainFrame = new COleCntrFrameWnd(pFrameWnd);
  117.     pFrameWnd->m_pMainFrame->Attach(hWnd);
  118.     if (pFrameWnd->m_lpDocFrame != NULL)
  119.     {
  120.         VERIFY(pFrameWnd->m_lpDocFrame->GetWindow(&hWnd) == NOERROR);
  121.         pFrameWnd->m_pDocFrame = new COleCntrFrameWnd(pFrameWnd);
  122.         pFrameWnd->m_pDocFrame->Attach(hWnd);
  123.     }
  124.     // update zoom factor information before creating control bars
  125.     pFrameWnd->m_rectPos.CopyRect(&rcPosRect);
  126.     pFrameWnd->m_rectClip.CopyRect(&rcClipRect);
  127.     if (!pFrameWnd->OnCreateControlBars(pFrameWnd->m_pMainFrame,
  128.         pFrameWnd->m_pDocFrame))
  129.     {
  130.         goto DeactivateUIAndFail;
  131.     }
  132.  
  133.     // set the active object
  134.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  135.     LPOLEINPLACEACTIVEOBJECT lpActiveObject;
  136.     lpActiveObject = (LPOLEINPLACEACTIVEOBJECT)
  137.         GetInterface(&IID_IOleInPlaceActiveObject);
  138.     pFrameWnd->m_lpFrame->SetActiveObject(lpActiveObject, T2OLE(strTitle));
  139.     if (pFrameWnd->m_lpDocFrame != NULL)
  140.         pFrameWnd->m_lpDocFrame->SetActiveObject(lpActiveObject, T2OLE(strTitle));
  141.  
  142.     // add frame & document level frame controls
  143.     ASSERT(m_pInPlaceFrame == pFrameWnd);
  144.     ASSERT(pFrameWnd->m_lpFrame != NULL);
  145.     OnShowControlBars(pFrameWnd->m_pMainFrame, TRUE);
  146.     if (pFrameWnd->m_lpDocFrame != NULL)
  147.         OnShowControlBars(pFrameWnd->m_pDocFrame, TRUE);
  148.  
  149.     // show any hidden modeless dialogs as well...
  150.     pFrameWnd->ShowOwnedWindows(TRUE);
  151.  
  152.     // attempt toolbar negotiation
  153.     OnResizeBorder(NULL, pFrameWnd->m_lpFrame, TRUE);
  154.     if (pFrameWnd->m_lpDocFrame != NULL)
  155.         OnResizeBorder(NULL, pFrameWnd->m_lpDocFrame, FALSE);
  156.  
  157.     // install the menu (also installs a hook which forwards messages from
  158.     //  the menu to the inplace frame window)
  159.     pFrameWnd->m_lpFrame->SetMenu(pFrameWnd->m_hSharedMenu,
  160.         pFrameWnd->m_hOleMenu, pFrameWnd->m_hWnd);
  161.  
  162.     // finally -- show the inplace frame window and set focus
  163.     pFrameWnd->ShowWindow(SW_SHOW);
  164.     pFrameWnd->SetFocus();
  165.     pFrameWnd->UpdateWindow();
  166.  
  167.     // allow the main window to be set
  168.     OnFrameWindowActivate(TRUE);
  169.     pFrameWnd->m_bUIActive = TRUE;
  170.  
  171.     // cleanup and return
  172.     lpInPlaceSite->Release();
  173.     return hr;
  174.  
  175. DeactivateUIAndFail:
  176.     ASSERT(lpInPlaceSite != NULL);
  177.     lpInPlaceSite->OnUIDeactivate(FALSE);
  178.  
  179. DestroyFrameAndFail:
  180.     if (m_pInPlaceFrame != NULL)
  181.     {
  182.         ASSERT(pFrameWnd != NULL);
  183.         DestroyInPlaceFrame(pFrameWnd);
  184.         m_pInPlaceFrame = NULL;
  185.  
  186.         // also need to send OnInPlaceDeactivate notification
  187.         ASSERT(lpInPlaceSite != NULL);
  188.         lpInPlaceSite->OnInPlaceDeactivate();
  189.     }
  190. ReleaseAndFail:
  191.     ASSERT(lpInPlaceSite != NULL);
  192.     lpInPlaceSite->Release();
  193.  
  194.     return hr;
  195. }
  196.  
  197. STDMETHODIMP_(ULONG) CDocObjectServerDoc::XOleDocumentView::AddRef()
  198. {
  199.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  200.     return pThis->ExternalAddRef();
  201. }
  202.  
  203. STDMETHODIMP_(ULONG) CDocObjectServerDoc::XOleDocumentView::Release()
  204. {
  205.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  206.     return pThis->ExternalRelease();
  207. }
  208.  
  209. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::QueryInterface(REFIID iid, LPVOID* ppvObj)
  210. {
  211.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  212.     return pThis->ExternalQueryInterface(&iid, ppvObj);
  213. }
  214.  
  215. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::SetInPlaceSite(
  216.     LPOLEINPLACESITE pIPSite)
  217. {
  218.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  219.     ASSERT_VALID(pThis);
  220.  
  221.     // if currently inplace active, then do normal inplace deactivation
  222.     if (pThis->IsInPlaceActive())
  223.         pThis->m_xOleInPlaceObject.InPlaceDeactivate();
  224.  
  225.     // release the view site pointer
  226.     if (pThis->m_pViewSite)
  227.         pThis->m_pViewSite->Release();
  228.  
  229.     // remember the new view site pointer and addref it, if it is non-NULL
  230.     pThis->m_pViewSite = pIPSite;
  231.     if (pThis->m_pViewSite != NULL)
  232.         pThis->m_pViewSite->AddRef();
  233.  
  234.     return NOERROR;
  235. }
  236.  
  237. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::GetInPlaceSite(LPOLEINPLACESITE* ppIPSite)
  238. {
  239.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  240.     ASSERT_VALID(pThis);
  241.     ASSERT(ppIPSite != NULL);
  242.  
  243.     if (pThis->m_pViewSite)
  244.         pThis->m_pViewSite->AddRef();
  245.     *ppIPSite = pThis->m_pViewSite;
  246.  
  247.     return NOERROR;
  248. }
  249.  
  250. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::GetDocument(LPUNKNOWN* ppUnk)
  251. {
  252.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  253.     ASSERT_VALID(pThis);
  254.     ASSERT(ppUnk != NULL);
  255.  
  256.     HRESULT hr = pThis->m_xOleDocument.QueryInterface(IID_IUnknown, 
  257.         (LPVOID*)ppUnk);
  258.     ASSERT(*ppUnk != NULL);
  259.  
  260.     return hr;
  261. }
  262.  
  263. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::SetRect(LPRECT lprcView)
  264. {
  265.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  266.     ASSERT_VALID(pThis);
  267.     ASSERT(lprcView != NULL);
  268.  
  269.     HRESULT hr = E_UNEXPECTED;
  270.     TRY
  271.     {
  272.         pThis->OnSetItemRects(lprcView, lprcView);
  273.         hr = NOERROR;
  274.     }
  275.     END_TRY
  276.  
  277.     return hr;
  278. }
  279.  
  280. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::GetRect(LPRECT lprcView)
  281. {
  282.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  283.     ASSERT_VALID(pThis);
  284.     ASSERT(lprcView != NULL);
  285.  
  286.     pThis->GetItemPosition(lprcView);
  287.     return NOERROR;
  288. }
  289.  
  290. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::SetRectComplex(
  291.     LPRECT lprcView, LPRECT lprcHScroll, 
  292.     LPRECT lprcVScroll, LPRECT lprcSizeBox)
  293. {
  294.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  295.     ASSERT_VALID(pThis);
  296.  
  297.     // We don't support complex rectangles, so return error
  298.     return E_NOTIMPL;
  299. }
  300.  
  301. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::Show(BOOL bShow)
  302. {
  303.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  304.     ASSERT_VALID(pThis);
  305.  
  306.     return E_NOTIMPL;
  307. }
  308.  
  309. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::UIActivate(BOOL bUIActivate)
  310. {
  311.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  312.     ASSERT_VALID(pThis);
  313.  
  314.     HRESULT hr = NOERROR;
  315.  
  316.     if (bUIActivate)
  317.     {
  318.         // UI Activate the view then take focus and bring the view forward
  319.         hr = pThis->OnActivateView();
  320.     }
  321.     else
  322.     {
  323.         // Call IOleInPlaceObject::UIDeactivate on this view
  324.         hr = pThis->m_xOleInPlaceObject.UIDeactivate();
  325.     }
  326.     return hr;
  327. }
  328.  
  329. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::Open()
  330. {
  331.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  332.     ASSERT_VALID(pThis);
  333.  
  334.     return E_NOTIMPL;
  335. }
  336.  
  337. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::CloseView(DWORD dwReserved)
  338. {
  339.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  340.     ASSERT_VALID(pThis);
  341.  
  342.     // Call IOleDocumentView::Show(FALSE) to hide the view
  343.     Show(FALSE);
  344.  
  345.     // Call IOleDocumentView::SetInPlaceSite(NULL) to deactivate the object
  346.     HRESULT hr = SetInPlaceSite(NULL);
  347.  
  348.     return hr;
  349. }
  350.  
  351. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::SaveViewState(LPSTREAM pstm)
  352. {
  353.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  354.     ASSERT_VALID(pThis);
  355.     return E_NOTIMPL;
  356. }
  357.  
  358. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::ApplyViewState(LPSTREAM pstm)
  359. {
  360.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  361.     ASSERT_VALID(pThis);
  362.  
  363.     HRESULT hr = NOERROR;
  364.  
  365.     // Attach the stream to an MFC file object
  366.     COleStreamFile file;
  367.     file.Attach(pstm);
  368.     CFileException fe;
  369.  
  370.     // load it with CArchive
  371.     CArchive loadArchive(&file, CArchive::load | CArchive::bNoFlushOnDelete);
  372.     TRY
  373.     {
  374.         pThis->OnApplyViewState(loadArchive);
  375.         loadArchive.Close();
  376.         file.Detach();
  377.     }
  378.     CATCH(COleException, pOE)
  379.     {
  380.         hr = pOE->m_sc;
  381.     }
  382.     AND_CATCH_ALL(e)
  383.     {
  384.         hr = E_UNEXPECTED;
  385.     }
  386.     END_CATCH_ALL
  387.  
  388.     return hr;
  389. }
  390.  
  391. STDMETHODIMP CDocObjectServerDoc::XOleDocumentView::Clone(
  392.     LPOLEINPLACESITE pipsiteNew, LPOLEDOCUMENTVIEW* ppviewNew)
  393. {
  394.     METHOD_PROLOGUE_EX(CDocObjectServerDoc, OleDocumentView)
  395.     ASSERT_VALID(pThis);
  396.  
  397.     // In order to support this, we would need to support multiple views,
  398.     // which we do not.  So we will return an error.
  399.     return E_NOTIMPL;
  400. }
  401.  
  402.