oledocvw.cpp 12 KB

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