home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oleui1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  4.6 KB  |  185 lines

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