oleui1.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 AFX_OLE2_SEG
  12. #pragma code_seg(AFX_OLE2_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // User interface for COleClientItem
  21. BOOL COleClientItem::ReportError(SCODE sc) const
  22. // return TRUE if error or warning reported
  23. {
  24. ASSERT_VALID(this);
  25. UINT nIDPrompt = 0;
  26. switch (sc)
  27. {
  28. case OLE_E_STATIC:
  29. nIDPrompt = AFX_IDP_STATIC_OBJECT;
  30. break;
  31. case E_NOINTERFACE:
  32. case E_NOTIMPL:
  33. case E_FAIL:
  34. nIDPrompt = AFX_IDP_FAILED_TO_CONNECT;
  35. break;
  36. case E_OUTOFMEMORY:
  37. nIDPrompt = AFX_IDP_FAILED_MEMORY_ALLOC;
  38. break;
  39. default:
  40. return FALSE; // nothing sensible to report
  41. }
  42. ASSERT(nIDPrompt != 0);
  43. AfxMessageBox(nIDPrompt);
  44. return TRUE;
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. // Item activation
  48. BOOL COleClientItem::DoVerb(LONG nVerb, CView* pView, LPMSG lpMsg)
  49. {
  50. ASSERT_VALID(this);
  51. if (pView != NULL)
  52. ASSERT_VALID(pView);
  53. if (lpMsg != NULL)
  54. ASSERT(AfxIsValidAddress(lpMsg, sizeof(MSG), FALSE));
  55. TRY
  56. {
  57. Activate(nVerb, pView, lpMsg);
  58. }
  59. CATCH(COleException, e)
  60. {
  61. // catch OLE errors and report them as such
  62. if (!ReportError(e->m_sc))
  63. AfxMessageBox(AFX_IDP_FAILED_TO_LAUNCH);
  64. DELETE_EXCEPTION(e);
  65. return FALSE;
  66. }
  67. AND_CATCH_ALL(e)
  68. {
  69. // otherwise, show generic error
  70. AfxMessageBox(AFX_IDP_FAILED_TO_LAUNCH);
  71. DELETE_EXCEPTION(e);
  72. return FALSE;
  73. }
  74. END_CATCH_ALL
  75. return TRUE;
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // COleClientDoc - user interface implementation
  79. // (functions reside in COleDocument to enable them on the server as well)
  80. BOOL COleDocument::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  81. AFX_CMDHANDLERINFO* pHandlerInfo)
  82. {
  83. ASSERT_VALID(this);
  84. if (nCode == CN_COMMAND && nID >= ID_OLE_VERB_FIRST && nID <= ID_OLE_VERB_LAST)
  85. {
  86. CView* pRoutingView = GetRoutingView_();
  87. COleClientItem* pSel = GetPrimarySelectedItem(pRoutingView);
  88. if (pSel != NULL)
  89. {
  90. if (pHandlerInfo != NULL) // routing test
  91. {
  92. pHandlerInfo->pTarget = this;
  93. return TRUE; // would be handled here
  94. }
  95. // activate the current selection with the appropriate verb
  96. CWaitCursor wait;
  97. pSel->DoVerb(nID - ID_OLE_VERB_FIRST, pRoutingView);
  98. return TRUE; // handled
  99. }
  100. }
  101. return CDocument::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  102. }
  103. COleClientItem* COleDocument::GetPrimarySelectedItem(CView* pView)
  104. {
  105. ASSERT_VALID(this);
  106. ASSERT(pView != NULL);
  107. ASSERT_VALID(pView);
  108. COleClientItem* pSelectedItem = NULL;
  109. // walk all items in the document - return one if there
  110. // is only one client item selected
  111. // (note: non OLE client items are ignored)
  112. POSITION pos = GetStartPosition();
  113. COleClientItem* pItem;
  114. while ((pItem = GetNextClientItem(pos)) != NULL)
  115. {
  116. if (pView->IsSelected(pItem))
  117. {
  118. // client item selected in
  119. if (pSelectedItem != NULL)
  120. return NULL; // more than one - no primary selection
  121. pSelectedItem = pItem;
  122. }
  123. }
  124. return pSelectedItem;
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // In-place item handling
  128. COleClientItem* COleDocument::GetInPlaceActiveItem(CWnd* pWnd)
  129. {
  130. ASSERT_VALID(this);
  131. ASSERT(pWnd != NULL);
  132. ASSERT_VALID(pWnd);
  133. // check for any item active on the immediate frame of pWndContainer
  134. // (two active objects on same frame are not supported)
  135. if (!pWnd->IsFrameWnd())
  136. {
  137. CFrameWnd* pFrameWnd = pWnd->GetParentFrame();
  138. if (pFrameWnd != NULL)
  139. pWnd = pFrameWnd;
  140. }
  141. POSITION pos = GetStartPosition();
  142. COleClientItem* pItem;
  143. while ((pItem = GetNextClientItem(pos)) != NULL)
  144. {
  145. if (pItem->m_pView != NULL && pItem->IsInPlaceActive() &&
  146. (pItem->m_pView == pWnd ||
  147. pItem->m_pView->GetParentFrame() == pWnd))
  148. {
  149. // that item is active on pWndContainer
  150. return pItem;
  151. }
  152. }
  153. // no item active on that window
  154. return NULL;
  155. }
  156. /////////////////////////////////////////////////////////////////////////////