oledoccl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. /////////////////////////////////////////////////////////////////////////////
  17. // COleDocObjectItem
  18. IMPLEMENT_DYNAMIC(COleDocObjectItem, COleClientItem)
  19. BEGIN_INTERFACE_MAP(COleDocObjectItem, COleClientItem)
  20. INTERFACE_PART(COleDocObjectItem, IID_IOleDocumentSite, OleDocumentSite)
  21. END_INTERFACE_MAP()
  22. COleDocObjectItem::COleDocObjectItem(COleDocument* pContainerDoc)
  23. : COleClientItem(pContainerDoc)
  24. {
  25. m_pHelpPopupMenu = NULL;
  26. m_pActiveView = NULL;
  27. m_pIPrint = NULL;
  28. m_bInHelpMenu = FALSE;
  29. }
  30. COleDocObjectItem::~COleDocObjectItem()
  31. {
  32. if (m_pHelpPopupMenu != NULL)
  33. m_pHelpPopupMenu->RemoveMenu(0, MF_BYPOSITION);
  34. delete m_pHelpPopupMenu;
  35. }
  36. /////////////////////////////////////////////////////////////////////////////
  37. // IOleDocumentSite interface
  38. STDMETHODIMP_(ULONG) COleDocObjectItem::XOleDocumentSite::AddRef()
  39. {
  40. METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  41. return pThis->ExternalAddRef();
  42. }
  43. STDMETHODIMP_(ULONG) COleDocObjectItem::XOleDocumentSite::Release()
  44. {
  45. METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  46. return pThis->ExternalRelease();
  47. }
  48. STDMETHODIMP COleDocObjectItem::XOleDocumentSite::QueryInterface(
  49. REFIID iid, LPVOID* ppvObj)
  50. {
  51. METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  52. return pThis->ExternalQueryInterface(&iid, ppvObj);
  53. }
  54. STDMETHODIMP COleDocObjectItem::XOleDocumentSite::ActivateMe(
  55. LPOLEDOCUMENTVIEW pViewToActivate)
  56. {
  57. METHOD_PROLOGUE_EX(COleDocObjectItem, OleDocumentSite)
  58. LPOLEDOCUMENT lpDocument;
  59. LPOLECLIENTSITE lpClientSite = pThis->GetClientSite();
  60. LPOLEINPLACESITE lpInPlaceSite =
  61. (LPOLEINPLACESITE) pThis->GetInterface(&IID_IOleInPlaceSite);
  62. if (lpClientSite == NULL || lpInPlaceSite == NULL)
  63. return E_FAIL;
  64. // if we've gotten a NULL view, we're to create one ourselves
  65. if (pViewToActivate == NULL)
  66. {
  67. // if we already have a view, we can simply activate it
  68. if (pThis->m_pActiveView != NULL && pThis->m_pView != NULL)
  69. {
  70. pThis->ActivateAndShow();
  71. return NOERROR;
  72. }
  73. ASSERT(pThis->m_lpObject != NULL);
  74. if (pThis->m_lpObject == NULL)
  75. return E_FAIL;
  76. lpDocument = QUERYINTERFACE(pThis->m_lpObject, IOleDocument);
  77. if (lpDocument == NULL)
  78. return E_FAIL;
  79. if (FAILED(
  80. lpDocument->CreateView(lpInPlaceSite, NULL, 0, &pViewToActivate)))
  81. {
  82. lpDocument->Release();
  83. return E_OUTOFMEMORY;
  84. }
  85. // we're done with the document pointer
  86. lpDocument->Release();
  87. }
  88. else if (pThis->m_pActiveView != NULL && pThis->m_pActiveView == pViewToActivate)
  89. {
  90. // we already own this view, so no need to addref
  91. // simply make it visible and resize it
  92. pThis->ActivateAndShow();
  93. return NOERROR;
  94. }
  95. else
  96. {
  97. // set the in-place site
  98. pViewToActivate->SetInPlaceSite(lpInPlaceSite);
  99. pViewToActivate->AddRef();
  100. }
  101. // it must've created
  102. ASSERT(pThis->m_pView != NULL);
  103. // if we had an old one, release it
  104. if (pThis->m_pActiveView != NULL)
  105. {
  106. pThis->m_pActiveView->Show(FALSE);
  107. pThis->m_pActiveView->UIActivate(FALSE);
  108. pThis->m_pActiveView->Release();
  109. if (pThis->m_pIPrint != (IPrint*) -1 && pThis->m_pIPrint != NULL)
  110. pThis->m_pIPrint->Release();
  111. pThis->m_pIPrint = NULL;
  112. }
  113. // remember it for later
  114. pThis->m_pActiveView = pViewToActivate;
  115. // activate and position it
  116. pThis->ActivateAndShow();
  117. return NOERROR;
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // IOleDocumentSite implementation helper
  121. void COleDocObjectItem::ActivateAndShow()
  122. {
  123. // set up toolbars and menus for the object
  124. m_pActiveView->UIActivate(TRUE);
  125. // set the window size, avoiding new toolbars
  126. RECT rc;
  127. m_pView->GetClientRect(&rc);
  128. m_pActiveView->SetRect(&rc);
  129. // make everything visible
  130. m_pActiveView->Show(TRUE);
  131. return;
  132. }
  133. void COleDocObjectItem::OnGetItemPosition(CRect& rPosition)
  134. {
  135. ASSERT_VALID(this);
  136. ASSERT(AfxIsValidAddress(&rPosition, sizeof(RECT)));
  137. // doc objects [almost] always in the exact rect of the view
  138. ASSERT_VALID(m_pView);
  139. m_pView->GetClientRect(&rPosition);
  140. }
  141. LPOLEDOCUMENTVIEW COleDocObjectItem::GetActiveView() const
  142. {
  143. return m_pActiveView;
  144. }
  145. void COleDocObjectItem::Release(OLECLOSE dwCloseOption)
  146. {
  147. RELEASE(m_pActiveView);
  148. if (m_pIPrint != (IPrint*) -1)
  149. RELEASE(m_pIPrint);
  150. COleClientItem::Release(dwCloseOption);
  151. }
  152. HRESULT COleDocObjectItem::ExecCommand(DWORD nCmdID,
  153. DWORD nCmdExecOpt /* = OLECMDEXECOPT_DONTPROMPTUSER */,
  154. const GUID* pguidCmdGroup /* = NULL */)
  155. {
  156. LPOLECOMMANDTARGET lpCt = QUERYINTERFACE(m_lpObject, IOleCommandTarget);
  157. HRESULT hr = E_NOTIMPL;
  158. if (lpCt != NULL)
  159. hr = lpCt->Exec(pguidCmdGroup, nCmdID, nCmdExecOpt, NULL, NULL);
  160. RELEASE(lpCt);
  161. return hr;
  162. }
  163. BOOL COleDocObjectItem::SupportsIPrint()
  164. {
  165. // did someone already ask? -1 means we know it doesn't,
  166. // non-NULL means we know it does (and we point at it)
  167. // NULL means we don't know
  168. if (m_pIPrint == NULL)
  169. {
  170. // QI for it
  171. m_pIPrint = QUERYINTERFACE(m_lpObject, IPrint);
  172. if (m_pIPrint == NULL)
  173. {
  174. // if the server isn't running, we'll need to
  175. // start it in order to print
  176. if (FAILED(::OleRun(m_lpObject)))
  177. m_pIPrint = (IPrint*) -1;
  178. else
  179. m_pIPrint = QUERYINTERFACE(m_lpObject, IPrint);
  180. }
  181. }
  182. return (m_pIPrint != NULL && m_pIPrint != (IPrint*) -1);
  183. }
  184. BOOL COleDocObjectItem::GetPageCount(LPLONG pnFirstPage, LPLONG pcPages)
  185. {
  186. if (!SupportsIPrint())
  187. return FALSE;
  188. //WINBUG: The proxy in DOCOBJ.DLL is broken; it doesn't allow
  189. // NULL parameters to IPrint::GetPageInfo(), even though the spec
  190. // says it should.
  191. LONG lPages;
  192. LONG lFirstPage;
  193. HRESULT hr = m_pIPrint->GetPageInfo(&lFirstPage, &lPages);
  194. if (pnFirstPage != NULL)
  195. *pnFirstPage = lFirstPage;
  196. if (pcPages != NULL)
  197. *pcPages = lPages;
  198. if (SUCCEEDED(hr))
  199. return TRUE;
  200. else
  201. return FALSE;
  202. }
  203. CMenu* COleDocObjectItem::GetHelpMenu(UINT& nPosition)
  204. {
  205. CFrameWnd* pFrame = m_pView->GetTopLevelFrame();
  206. CMenu* pMenuFrame = CMenu::FromHandle(pFrame->m_hMenuDefault);
  207. if (pMenuFrame != NULL)
  208. nPosition = pMenuFrame->GetMenuItemCount() -1;
  209. return pMenuFrame;
  210. }
  211. void COleDocObjectItem::OnInsertMenus(CMenu* pMenuShared,
  212. LPOLEMENUGROUPWIDTHS lpMenuWidths)
  213. {
  214. ASSERT_VALID(this);
  215. ASSERT_VALID(pMenuShared);
  216. ASSERT(AfxIsValidAddress(lpMenuWidths, sizeof(OLEMENUGROUPWIDTHS)));
  217. // initialize the group widths array
  218. lpMenuWidths->width[0] = 1;
  219. lpMenuWidths->width[2] = 0;
  220. lpMenuWidths->width[4] = 0;
  221. // get menu from document template
  222. CDocTemplate* pTemplate = GetDocument()->GetDocTemplate();
  223. HMENU hMenuOLE = pTemplate->m_hMenuInPlace;
  224. // only copy the popups if there is a menu loaded
  225. if (hMenuOLE == NULL)
  226. return;
  227. UINT nItem;
  228. CMenu* pMenuFrame = GetHelpMenu(nItem);
  229. if (pMenuFrame != NULL)
  230. {
  231. CString strHelpMenuName;
  232. int nSeparator = pMenuFrame->GetMenuString(nItem,
  233. strHelpMenuName, MF_BYPOSITION);
  234. if (nSeparator == 0)
  235. {
  236. TRACE0("Error: COleDocObjectItem::OnInsertMenus() found no help menu!\n");
  237. return;
  238. }
  239. CString strTearOffName;
  240. strTearOffName.Format(_T("%s %s"), AfxGetAppName(), strHelpMenuName);
  241. strTearOffName.Remove('&');
  242. // get the normal frame menu
  243. int nCount = pMenuFrame->GetMenuItemCount();
  244. HMENU hMenu = GetSubMenu(pMenuFrame->m_hMenu, nCount-1);
  245. // clean up old menu and allocate a new one
  246. if (m_pHelpPopupMenu == NULL)
  247. {
  248. m_pHelpPopupMenu = new CMenu;
  249. // create new sub-popup menu and add container's Help tearoff
  250. // then add help menu from main window
  251. m_pHelpPopupMenu->CreateMenu();
  252. m_pHelpPopupMenu->InsertMenu((UINT) -1, MF_BYPOSITION | MF_POPUP,
  253. (UINT) hMenu, strTearOffName);
  254. }
  255. pMenuShared->InsertMenu(1, MF_BYPOSITION | MF_POPUP,
  256. (UINT) m_pHelpPopupMenu->m_hMenu, strHelpMenuName);
  257. // tell the object we added our Help menu
  258. lpMenuWidths->width[5] = 1;
  259. }
  260. // insert our menu items and adjust group widths array
  261. AfxMergeMenus(pMenuShared->GetSafeHmenu(), hMenuOLE,
  262. &lpMenuWidths->width[0], 0);
  263. }
  264. void COleDocObjectItem::OnRemoveMenus(CMenu *pMenuShared)
  265. {
  266. int cItemsShared = pMenuShared->GetMenuItemCount();
  267. if (cItemsShared != 0)
  268. {
  269. CMenu *pMenuHelp = pMenuShared->GetSubMenu(cItemsShared - 1);
  270. int cItemsHelp = pMenuHelp->GetMenuItemCount();
  271. int nItem;
  272. for (nItem = cItemsHelp-1; nItem > 0; nItem--)
  273. pMenuHelp->DeleteMenu(nItem, MF_BYPOSITION);
  274. pMenuShared->RemoveMenu(cItemsShared - 1, MF_BYPOSITION);
  275. }
  276. COleClientItem::OnRemoveMenus(pMenuShared);
  277. }
  278. BOOL COleDocObjectItem::OnPreparePrinting(CView* pCaller,
  279. CPrintInfo* pInfo, BOOL bPrintAll)
  280. {
  281. LONG lDocObjectPages = 0;
  282. CDocument* pDoc = pCaller->GetDocument();
  283. COleDocument* pOleDoc = DYNAMIC_DOWNCAST(COleDocument, pDoc);
  284. if (pOleDoc == NULL)
  285. return FALSE;
  286. POSITION pos = pOleDoc->GetStartPosition();
  287. while (pos != NULL)
  288. {
  289. COleClientItem* pItem = pOleDoc->GetNextClientItem(pos);
  290. COleDocObjectItem* pDocItem =
  291. DYNAMIC_DOWNCAST(COleDocObjectItem, pItem);
  292. if (pDocItem == NULL)
  293. continue;
  294. // if this isn't the view, continue
  295. if (!bPrintAll)
  296. {
  297. if (pItem->m_pView == NULL ||
  298. pItem->m_pView->m_hWnd != pCaller->m_hWnd)
  299. continue;
  300. }
  301. if (pDocItem->SupportsIPrint())
  302. {
  303. LONG lThisObjectPages;
  304. if (pDocItem->GetPageCount(NULL, &lThisObjectPages))
  305. lDocObjectPages += lThisObjectPages;
  306. pInfo->m_bDocObject = TRUE;
  307. }
  308. else
  309. lDocObjectPages++;
  310. if (!bPrintAll)
  311. break;
  312. }
  313. if (lDocObjectPages > 0)
  314. {
  315. UINT nMaxPage = pInfo->GetMaxPage();
  316. // set the page count; increment it if previously set
  317. if (nMaxPage == 0xFFFF)
  318. pInfo->SetMaxPage(lDocObjectPages);
  319. else
  320. pInfo->SetMaxPage(nMaxPage + lDocObjectPages);
  321. pInfo->m_bDocObject = TRUE;
  322. }
  323. if (pInfo->m_bDocObject)
  324. {
  325. // we can't show the "selection" button for DocObjects
  326. pInfo->m_pPD->m_pd.Flags |= PD_NOSELECTION;
  327. // if it's a doc object, and we're printing all, then we
  328. // shouldn't show the selection
  329. if (bPrintAll)
  330. pInfo->m_pPD->m_pd.Flags |= PD_NOPAGENUMS;
  331. }
  332. return TRUE;
  333. }
  334. void COleDocObjectItem::OnPrint(CView* pCaller, CPrintInfo* pInfo,
  335. BOOL bPrintAll)
  336. {
  337. // Note that this function ignores pInfo->m_nCurPage, and will always
  338. // print the whole range of pages in the CPrintInfo object. That's
  339. // because DocObjects don't support any mechanism to _continue_
  340. // printing to an existing print job.
  341. CDocument* pDoc = pCaller->GetDocument();
  342. COleDocument* pOleDoc = DYNAMIC_DOWNCAST(COleDocument, pDoc);
  343. if (pOleDoc == NULL)
  344. return;
  345. POSITION pos = pOleDoc->GetStartPosition();
  346. while (pos != NULL)
  347. {
  348. COleClientItem* pItem = pOleDoc->GetNextClientItem(pos);
  349. COleDocObjectItem* pDocItem = DYNAMIC_DOWNCAST(COleDocObjectItem, pItem);
  350. if (pDocItem == NULL)
  351. continue;
  352. // if this isn't the view, continue
  353. if (!bPrintAll)
  354. {
  355. if (pItem->m_pView == NULL || pItem->m_pView->m_hWnd != pCaller->m_hWnd)
  356. continue;
  357. }
  358. HRESULT hrThisPage = E_UNEXPECTED;
  359. if (pDocItem->SupportsIPrint())
  360. {
  361. DVTARGETDEVICE* pTargetDevice = NULL;
  362. LPDEVNAMES lpDevNames = NULL;
  363. LPDEVMODE lpDevMode = NULL;
  364. lpDevNames = (LPDEVNAMES) GlobalLock(pInfo->m_pPD->m_pd.hDevNames);
  365. if (lpDevNames != NULL)
  366. {
  367. lpDevMode = (LPDEVMODE) GlobalLock(pInfo->m_pPD->m_pd.hDevMode);
  368. if (lpDevMode != NULL)
  369. {
  370. pTargetDevice = _AfxOleCreateTargetDevice(lpDevNames, lpDevMode);
  371. if (pTargetDevice != NULL)
  372. {
  373. PAGESET* pps = (PAGESET*) CoTaskMemAlloc(sizeof(PAGESET));
  374. if (pps != NULL)
  375. {
  376. pps->cbStruct = sizeof(PAGESET);
  377. ASSERT((pps->cbStruct % 4) == 0);
  378. pps->fOddPages = TRUE;
  379. pps->fEvenPages = TRUE;
  380. pps->cPageRange = 1;
  381. pps->rgPages[0].nFromPage = pInfo->GetFromPage();
  382. pps->rgPages[0].nToPage = pInfo->GetToPage();
  383. LONG lLastPage = pps->rgPages[0].nFromPage;
  384. LONG lPagesPrinted;
  385. DWORD dwFlags = PRINTFLAG_RECOMPOSETODEVICE;
  386. if (pInfo->m_pPD->m_pd.Flags & PD_PRINTTOFILE)
  387. dwFlags |= PRINTFLAG_PRINTTOFILE;
  388. hrThisPage = pDocItem->m_pIPrint->Print(dwFlags,
  389. &pTargetDevice, &pps, NULL, NULL,
  390. pInfo->m_nCurPage, &lPagesPrinted,
  391. &lLastPage);
  392. if (!SUCCEEDED(hrThisPage))
  393. TRACE1("IPrint::Print() returned %8.8X\n", hrThisPage);
  394. CoTaskMemFree(pps);
  395. }
  396. CoTaskMemFree(pTargetDevice);
  397. }
  398. GlobalUnlock(pInfo->m_pPD->m_pd.hDevMode);
  399. }
  400. GlobalUnlock(pInfo->m_pPD->m_pd.hDevNames);
  401. }
  402. }
  403. else
  404. {
  405. // try through IOleCommandTarget
  406. hrThisPage = pDocItem->ExecCommand(OLECMDID_PRINT);
  407. if (!SUCCEEDED(hrThisPage))
  408. TRACE1("IOleCommandTarget::Exec() returned %8.8X\n", hrThisPage);
  409. }
  410. }
  411. return;
  412. }