home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / activedoc / oledocument.h < prev    next >
C/C++ Source or Header  |  1998-04-03  |  8KB  |  314 lines

  1. // OleDocument.h : IOleDocument implementation
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include <docobj.h>
  14.  
  15. //////////////////////////////////////////////////////////////////////////////
  16. // IOleDocumentImpl
  17. template <class T>
  18. class ATL_NO_VTABLE IOleDocumentImpl : public IOleDocument
  19. {
  20. public:
  21.     STDMETHOD(CreateView)(IOleInPlaceSite *pIPSite, IStream *pstm,
  22.         DWORD /* dwReserved */, IOleDocumentView **ppView)
  23.     {
  24.         ATLTRACE(_T("IOleDocument::CreateView\n"));
  25.         T* pT = static_cast<T*>(this);
  26.  
  27.         if (ppView == NULL)
  28.             return E_POINTER;
  29.  
  30.         // If we've already created a view then we can't create another as we
  31.         // currently only support the ability to create one view
  32.         if (pT->m_spInPlaceSite != NULL)
  33.             return E_FAIL;
  34.  
  35.         IOleDocumentView* pView;
  36.         pT->GetUnknown()->QueryInterface(IID_IOleDocumentView, (void**)&pView);
  37.         // If we support IOleDocument we should support IOleDocumentView
  38.         ATLASSERT(pView != NULL);
  39.  
  40.         // If they've given us a site then use it
  41.         if (pIPSite != NULL)
  42.             pView->SetInPlaceSite(pIPSite);
  43.  
  44.         // If they have given us an IStream pointer then use it to
  45.         // initialize the view
  46.         if (pstm != NULL)
  47.         {
  48.             pView->ApplyViewState(pstm);
  49.         }
  50.  
  51.         // Return the view
  52.         *ppView = pView;
  53.  
  54.         return S_OK;
  55.     }
  56.  
  57.     STDMETHOD(GetDocMiscStatus)(DWORD *pdwStatus)
  58.     {
  59.         ATLTRACE(_T("IOleDocument::GetDocMiscStatus\n"));
  60.         *pdwStatus = DOCMISC_NOFILESUPPORT;
  61.         return S_OK;
  62.     }
  63.  
  64.     STDMETHOD(EnumViews)(IEnumOleDocumentViews** /*ppEnum*/,
  65.         IOleDocumentView **ppView)
  66.     {
  67.         ATLTRACE(_T("IOleDocument::EnumViews\n"));
  68.         T* pT = static_cast<T*>(this);
  69.  
  70.         if (ppView == NULL)
  71.             return E_POINTER;
  72.  
  73.         // We only support one view
  74.         pT->_InternalQueryInterface(IID_IOleDocumentView, (void**)ppView);
  75.         return S_OK;
  76.     }
  77. };
  78.  
  79. //////////////////////////////////////////////////////////////////////////////
  80. // IOleDocumentViewImpl
  81.  
  82. template <class T>
  83. class ATL_NO_VTABLE IOleDocumentViewImpl : public IOleDocumentView
  84. {
  85. public:
  86.     STDMETHOD(SetInPlaceSite)(IOleInPlaceSite *pIPSite)
  87.     {
  88.         ATLTRACE(_T("IOleDocumentView::SetInPlaceSite\n"));
  89.         T* pT = static_cast<T*>(this);
  90.         HRESULT hr;
  91.  
  92.         if (pT->m_spInPlaceSite)
  93.         {
  94.             // If we already have a site get rid of it
  95.             UIActivate(FALSE);
  96.             hr = pT->InPlaceDeactivate();
  97.             if (FAILED(hr))
  98.                 return hr;
  99.             ATLASSERT(!pT->m_bInPlaceActive);
  100.         }
  101.         if (pIPSite != NULL)
  102.             pT->m_spInPlaceSite = (IOleInPlaceSiteWindowless*)pIPSite;
  103.  
  104.         return S_OK;
  105.     }
  106.  
  107.    STDMETHOD(GetInPlaceSite)(IOleInPlaceSite **ppIPSite)
  108.     {
  109.         ATLTRACE(_T("IOleDocumentView::GetInPlaceSite\n"));
  110.         T* pT = static_cast<T*>(this);
  111.  
  112.         ATLASSERT(ppIPSite);
  113.         if (ppIPSite == NULL)
  114.             return E_FAIL;
  115.  
  116.         *ppIPSite = pT->m_spInPlaceSite;
  117.         pT->m_spInPlaceSite.p->AddRef();
  118.         return S_OK;
  119.     }
  120.  
  121.     STDMETHOD(GetDocument)(IUnknown **ppunk)
  122.     {
  123.         ATLTRACE(_T("IOleDocumentView::GetDocument\n"));
  124.         T* pT = static_cast<T*>(this);
  125.  
  126.         ATLASSERT(ppunk);
  127.         if (ppunk == NULL)
  128.             return E_FAIL;
  129.  
  130.         *ppunk = pT->GetUnknown();
  131.         (*ppunk)->AddRef();
  132.         return S_OK;
  133.     }
  134.  
  135.     STDMETHOD(SetRect)(LPRECT prcView)
  136.     {
  137.         static bool bResizing;
  138.         if (bResizing)
  139.             return S_OK;
  140.         bResizing = true;
  141.  
  142.         ATLTRACE("IOleDocumentView::SetObjectRects(%d, %d,  %d, %d)\n", prcView->left, prcView->top, prcView->right, prcView->bottom);
  143.         T* pT = static_cast<T*>(this);
  144.         pT->SetObjectRects(prcView, prcView);
  145.  
  146.         bResizing = false;
  147.         return S_OK;
  148.     }
  149.  
  150.     STDMETHOD(GetRect)(LPRECT prcView)
  151.     {
  152.         ATLTRACE(_T("IOleDocumentView::GetRect\n"));
  153.         T* pT = static_cast<T*>(this);
  154.  
  155.         ATLASSERT(prcView);
  156.         if (prcView == NULL)
  157.             return E_POINTER;
  158.  
  159.         *prcView = pT->m_rcPos;
  160.         return S_OK;
  161.     }
  162.  
  163.     STDMETHOD(SetRectComplex)(LPRECT /* prcView */, LPRECT /* prcHScroll */,
  164.         LPRECT /* prcVScroll */, LPRECT /* prcSizeBox */)
  165.     {
  166.         ATLTRACENOTIMPL(_T("IOleDocumentView::SetRectComplex"));
  167.     }
  168.  
  169.     STDMETHOD(Show)(BOOL fShow)
  170.     {
  171.         ATLTRACE(_T("IOleDocumentView::Show\n"));
  172.         T* pT = static_cast<T*>(this);
  173.         HRESULT hr = S_OK;
  174.  
  175.         if (fShow)
  176.         {
  177.             if (!pT->m_bUIActive)
  178.                 hr = pT->ActiveXDocActivate(OLEIVERB_INPLACEACTIVATE);
  179.         }
  180.         else
  181.         {
  182.             hr = pT->UIActivate(FALSE);
  183.             ::ShowWindow(pT->m_hWnd, SW_HIDE);
  184.         }
  185.         return hr;
  186.     }
  187.  
  188.     STDMETHOD(UIActivate)(BOOL fUIActivate)
  189.     {
  190.         ATLTRACE(_T("IOleDocumentView::UIActivate\n"));
  191.         T* pT = static_cast<T*>(this);
  192.         HRESULT hr = S_OK;
  193.  
  194.         if (fUIActivate)
  195.         {
  196.             // We must know the client site first
  197.             if (pT->m_spInPlaceSite == NULL)
  198.                 return E_UNEXPECTED;
  199.             if (!pT->m_bUIActive)
  200.                 hr = pT->ActiveXDocActivate(OLEIVERB_UIACTIVATE);
  201.         }
  202.         else
  203.         {
  204.             pT->InPlaceMenuDestroy();
  205.             hr = pT->UIDeactivate();
  206.         }
  207.  
  208.         return hr;
  209.     }
  210.  
  211.     STDMETHOD(Open)()
  212.     {
  213.         ATLTRACENOTIMPL(_T("IOleDocumentView::Open"));
  214.     }
  215.  
  216.     STDMETHOD(CloseView)(DWORD /* dwReserved */)
  217.     {
  218.         ATLTRACE(_T("IOleDocumentView::CloseView\n"));
  219.         T* pT = static_cast<T*>(this);
  220.         pT->Show(FALSE);
  221.         pT->SetInPlaceSite(NULL);
  222.         return S_OK;
  223.     }
  224.  
  225.     STDMETHOD(SaveViewState)(LPSTREAM /* pstm */)
  226.     {
  227.         ATLTRACENOTIMPL(_T("IOleDocumentView::SaveViewState"));
  228.     }
  229.  
  230.     STDMETHOD(ApplyViewState)(LPSTREAM /* pstm */)
  231.     {
  232.         ATLTRACENOTIMPL(_T("IOleDocumentView::ApplyViewState"));
  233.     }
  234.  
  235.     STDMETHOD(Clone)(IOleInPlaceSite* /* pIPSiteNew */,
  236.         IOleDocumentView** /* ppViewNew */)
  237.     {
  238.         ATLTRACENOTIMPL(_T("IOleDocumentView::Clone"));
  239.     }
  240.  
  241.     HRESULT ActiveXDocActivate(LONG iVerb)
  242.     {
  243.         HRESULT hr;
  244.         T* pT = static_cast<T*>(this);
  245.  
  246.         pT->m_bNegotiatedWnd = TRUE;
  247.  
  248.         if (!pT->m_bInPlaceActive)
  249.         {
  250.             hr = pT->m_spInPlaceSite->CanInPlaceActivate();
  251.             if (FAILED(hr))
  252.                 return hr;
  253.             pT->m_spInPlaceSite->OnInPlaceActivate();
  254.         }
  255.         pT->m_bInPlaceActive = TRUE;
  256.  
  257.         // get location in the parent window,
  258.         // as well as some information about the parent
  259.         RECT rcPos, rcClip;
  260.         CComPtr<IOleInPlaceUIWindow> spInPlaceUIWindow;
  261.         pT->m_frameInfo.cb = sizeof(OLEINPLACEFRAMEINFO);
  262.  
  263.         HWND hwndParent;
  264.         if (pT->m_spInPlaceSite->GetWindow(&hwndParent) == S_OK)
  265.         {
  266.             pT->m_spInPlaceFrame.Release();
  267.             pT->m_spInPlaceSite->GetWindowContext(&pT->m_spInPlaceFrame,
  268.                 &spInPlaceUIWindow, &rcPos, &rcClip, &pT->m_frameInfo);
  269.  
  270.             if (!pT->m_bWndLess)
  271.             {
  272.                 if (pT->m_hWnd)
  273.                 {
  274.                     ::ShowWindow(pT->m_hWnd, SW_SHOW);
  275.                     pT->SetFocus();
  276.                 }
  277.                 else
  278.                     pT->m_hWnd = pT->Create(hwndParent, rcPos);
  279.             }
  280.             pT->SetObjectRects(&rcPos, &rcClip);
  281.         }
  282.  
  283.         CComPtr<IOleInPlaceActiveObject> spActiveObject;
  284.         QueryInterface(IID_IOleInPlaceActiveObject, (void**)&spActiveObject);
  285.  
  286.         // Gone active by now, take care of UIACTIVATE
  287.         if (pT->DoesVerbUIActivate(iVerb))
  288.         {
  289.             if (!pT->m_bUIActive)
  290.             {
  291.                 pT->m_bUIActive = TRUE;
  292.                 hr = pT->m_spInPlaceSite->OnUIActivate();
  293.                 if (FAILED(hr))
  294.                     return hr;
  295.  
  296.                 // set ourselves up in the host
  297.                 if (spActiveObject != NULL)
  298.                 {
  299.                     if (pT->m_spInPlaceFrame != NULL)
  300.                         pT->m_spInPlaceFrame->SetActiveObject(spActiveObject, NULL);
  301.                     if (spInPlaceUIWindow != NULL)
  302.                         spInPlaceUIWindow->SetActiveObject(spActiveObject, NULL);
  303.                 }
  304.             }
  305.         }
  306.  
  307.         // Merge the menus
  308.         pT->InPlaceMenuCreate();
  309.         pT->SetupToolbar(spInPlaceUIWindow);
  310.         pT->m_spClientSite->ShowObject();
  311.         return S_OK;
  312.     }
  313. };
  314.