home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLEUI1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  4.5 KB  |  184 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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.         COleClientItem* pSel = GetPrimarySelectedItem(GetRoutingView());
  105.         if (pSel != NULL)
  106.         {
  107.             if (pHandlerInfo != NULL)       // routing test
  108.             {
  109.                 pHandlerInfo->pTarget = this;
  110.                 return TRUE;        // would be handled here
  111.             }
  112.  
  113.             // activate the current selection with the appropriate verb
  114.             CWaitCursor wait;
  115.             pSel->DoVerb(nID - ID_OLE_VERB_FIRST, GetRoutingView());
  116.             return TRUE;    // handled
  117.         }
  118.     }
  119.     return CDocument::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  120. }
  121.  
  122. COleClientItem* COleDocument::GetPrimarySelectedItem(CView* pView)
  123. {
  124.     ASSERT_VALID(this);
  125.     ASSERT(pView != NULL);
  126.     ASSERT_VALID(pView);
  127.  
  128.     COleClientItem* pSelectedItem = NULL;
  129.  
  130.     // walk all items in the document - return one if there
  131.     //   is only one client item selected
  132.     // (note: non OLE client items are ignored)
  133.     POSITION pos = GetStartPosition();
  134.     COleClientItem* pItem;
  135.     while ((pItem = GetNextClientItem(pos)) != NULL)
  136.     {
  137.         if (pView->IsSelected(pItem))
  138.         {
  139.             // client item selected in
  140.             if (pSelectedItem != NULL)
  141.                 return NULL;        // more than one - no primary selection
  142.             pSelectedItem = pItem;
  143.         }
  144.     }
  145.     return pSelectedItem;
  146. }
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149. // In-place item handling
  150.  
  151. COleClientItem* COleDocument::GetInPlaceActiveItem(CWnd* pWnd)
  152. {
  153.     ASSERT_VALID(this);
  154.     ASSERT(pWnd != NULL);
  155.     ASSERT_VALID(pWnd);
  156.  
  157.     // check for any item active on the immediate frame of pWndContainer
  158.     //  (two active objects on same frame are not supported)
  159.     if (!pWnd->IsFrameWnd())
  160.     {
  161.         CFrameWnd* pFrameWnd = pWnd->GetParentFrame();
  162.         if (pFrameWnd != NULL)
  163.             pWnd = pFrameWnd;
  164.     }
  165.  
  166.     POSITION pos = GetStartPosition();
  167.     COleClientItem* pItem;
  168.     while ((pItem = GetNextClientItem(pos)) != NULL)
  169.     {
  170.         if (pItem->m_pView != NULL && pItem->IsInPlaceActive() &&
  171.             (pItem->m_pView == pWnd ||
  172.              pItem->m_pView->GetParentFrame() == pWnd))
  173.         {
  174.             // that item is active on pWndContainer
  175.             return pItem;
  176.         }
  177.     }
  178.  
  179.     // no item active on that window
  180.     return NULL;
  181. }
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184.