home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / mfcbind / cntritem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.0 KB  |  207 lines

  1. // CntrItem.cpp : implementation of the CMFCBindCntrItem class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "MFCBind.h"
  15.  
  16. #include "BindDoc.h"
  17. #include "BindView.h"
  18. #include "CntrItem.h"
  19. #include "mainfrm.h"
  20. #include "ObjView.h"
  21.  
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CMFCBindCntrItem implementation
  31.  
  32. int CMFCBindCntrItem::m_nDocID = 1;
  33.  
  34. IMPLEMENT_SERIAL(CMFCBindCntrItem, COleDocObjectItem, 0)
  35.  
  36. CMFCBindCntrItem::CMFCBindCntrItem(CMFCBindDoc* pContainer)
  37.     : COleDocObjectItem(pContainer)
  38. {
  39. }
  40.  
  41. CMFCBindCntrItem::~CMFCBindCntrItem()
  42. {
  43.     // TODO: add cleanup code here
  44.  
  45. }
  46.  
  47. void CMFCBindCntrItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
  48. {
  49.     ASSERT_VALID(this);
  50.  
  51.     COleDocObjectItem::OnChange(nCode, dwParam);
  52.     GetDocument()->UpdateAllViews(NULL);
  53. }
  54.  
  55. BOOL CMFCBindCntrItem::OnChangeItemPosition(const CRect& rectPos)
  56. {
  57.     ASSERT_VALID(this);
  58.  
  59.     if (!COleDocObjectItem::OnChangeItemPosition(rectPos))
  60.         return FALSE;
  61.     return TRUE;
  62. }
  63.  
  64. void CMFCBindCntrItem::OnGetItemPosition(CRect& rPosition)
  65. {
  66.     ASSERT_VALID(this);
  67.     m_pView->GetClientRect(rPosition);
  68. }
  69.  
  70. void CMFCBindCntrItem::OnActivate()
  71. {
  72.    CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd, AfxGetApp()->m_pMainWnd);
  73.    ASSERT(pFrame != NULL);
  74.  
  75.    CWnd* pView = pFrame->GetActiveView();
  76.    ASSERT(pView != NULL);
  77.  
  78.    COleDocObjectItem* pItem =
  79.       (COleDocObjectItem*) GetDocument()->GetInPlaceActiveItem(pView);
  80.  
  81.    // if this isn't the item being activated, deactivate it.
  82.    if (pItem != NULL && pItem != this)
  83.       pItem->Deactivate();
  84.  
  85.    COleDocObjectItem::OnActivate();
  86. }
  87.  
  88. void CMFCBindCntrItem::OnDeactivateUI(BOOL bUndoable)
  89. {
  90.     COleDocObjectItem::OnDeactivateUI(bUndoable);
  91. }
  92.  
  93. void CMFCBindCntrItem::Serialize(CArchive& ar)
  94. {
  95.     ASSERT_VALID(this);
  96.  
  97.     // Call base class first to read in COleDocObjectItem data.
  98.     // Since this sets up the m_pDocument pointer returned from
  99.     //  CMFCBindCntrItem::GetDocument, it is a good idea to call
  100.     //  the base class Serialize first.
  101.     COleDocObjectItem::Serialize(ar);
  102.  
  103.     // now store/retrieve data specific to CMFCBindCntrItem
  104.     if (ar.IsStoring())
  105.     {
  106.         // TODO: add storing code here
  107.     }
  108.     else
  109.     {
  110.         // TODO: add loading code here
  111.     }
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CMFCBindCntrItem diagnostics
  116.  
  117. #ifdef _DEBUG
  118. void CMFCBindCntrItem::AssertValid() const
  119. {
  120.     COleDocObjectItem::AssertValid();
  121. }
  122.  
  123. void CMFCBindCntrItem::Dump(CDumpContext& dc) const
  124. {
  125.     COleDocObjectItem::Dump(dc);
  126. }
  127. #endif
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130. //
  131.  
  132. void CMFCBindCntrItem::ProcessNewObject()
  133. {
  134.     // This function get's an HICON for the server from the registry by
  135.     // using our GetObjectIconFromRegistry function.  We also generate a
  136.     // name and tell the object list view to add this information to
  137.     // the list box.
  138.  
  139.     if (!m_lpObject)
  140.         return; //no object, something must've gone wrong
  141.  
  142.     CString strName;
  143.     strName.Format(_T("Section %d "), m_nDocID);
  144.     m_nDocID++;
  145.  
  146.     HICON hIcon = GetIconFromRegistry();
  147.     if (hIcon != NULL)
  148.     {
  149.         CMainFrame* pFrame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetApp()->m_pMainWnd);
  150.         ASSERT(pFrame != NULL);
  151.         CObjListView *pListView = pFrame->GetObjListView();
  152.         if (pListView == NULL)
  153.             return;
  154.  
  155.         pListView->AddItem((COleDocObjectItem*) this, strName, hIcon);
  156.     }
  157. }
  158.  
  159. BOOL CMFCBindCntrItem::FinishCreate(SCODE sc)
  160. {
  161.     // FinishCreate is an undocumented virtual function that we
  162.     // override here to do some extra processing after the object
  163.     // has been created.
  164.  
  165.     if (FAILED(sc))
  166.     {
  167.         AfxMessageBox(_T("Dang, OleCreate failed to create this item!!"));
  168.         return FALSE;
  169.     }
  170.  
  171.     BOOL bRet = COleDocObjectItem::FinishCreate(sc);
  172.     if (bRet)
  173.         ProcessNewObject();
  174.     return bRet;
  175.  
  176. }
  177.  
  178. void CMFCBindCntrItem::LoadFromMoniker(LPUNKNOWN pUnk, OLECHAR* szwName)
  179. {
  180.     HRESULT hr;
  181.     // Ask the system for a URL Moniker
  182.     IMoniker* pIMoniker;
  183.     hr = CreateURLMoniker(NULL, (LPWSTR)szwName, &pIMoniker);
  184.     if ( SUCCEEDED(hr) )
  185.     {
  186.         // Get the IPersistMoniker interface
  187.         IPersistMoniker* pPMk;
  188.         hr = pUnk->QueryInterface(
  189.                                 IID_IPersistMoniker,
  190.                                 (void **)&pPMk);
  191.         if ( SUCCEEDED(hr) )
  192.         {
  193.                 // note: do not register our BSC when calling IPM::Load directly
  194.             IBindCtx *pBCtx;
  195.             hr = CreateBindCtx(0, &pBCtx);
  196.             if ( SUCCEEDED(hr) )
  197.             {
  198.                 // Call Load on the IPersistMoniker
  199.                 hr = pPMk->Load(FALSE, pIMoniker, pBCtx, STGM_READ);
  200.                     pBCtx->Release();
  201.             }
  202.             pPMk->Release();
  203.         }
  204.         pIMoniker->Release( );
  205.     }
  206. }
  207.