home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / allinone / collect / typaryvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  5.6 KB  |  231 lines

  1. // typaryvw.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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 "collect.h"
  15. #include "colledoc.h"
  16. #include "typaryvw.h"
  17. #include "resource.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CTypedPtrArrayView
  26.  
  27. IMPLEMENT_DYNCREATE(CTypedPtrArrayView, CFormView)
  28.  
  29. CTypedPtrArrayView::CTypedPtrArrayView()
  30.     : CFormView(CTypedPtrArrayView::IDD)
  31. {
  32.     //{{AFX_DATA_INIT(CTypedPtrArrayView)
  33.     m_int = 0;
  34.     m_float = 0.0f;
  35.     m_str = "";
  36.     //}}AFX_DATA_INIT
  37. }
  38.  
  39. CTypedPtrArrayView::~CTypedPtrArrayView()
  40. {
  41. }
  42.  
  43. void CTypedPtrArrayView::OnInitialUpdate()
  44. {
  45.     CFormView::OnInitialUpdate();
  46.  
  47.     try {
  48.     // Copy all of the entries from the document's CTypedPtrArray
  49.     // to the listbox.
  50.     m_ctlList.ResetContent();
  51.     IStlMyObjectArrayPtr& myobArray = GetDocument()->m_myobArray;
  52.     for (int nIndex = 0; nIndex < (int) myobArray->Count; nIndex++)
  53.     {
  54.         CMyObject* pMyObject = (CMyObject*) myobArray->Array[nIndex];
  55.         AddMyObjectToListBox(pMyObject);
  56.     }
  57.     } catch(_com_error& e) {
  58.         dump_com_error(e);
  59.     }
  60. }
  61.  
  62. void CTypedPtrArrayView::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CFormView::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CTypedPtrArrayView)
  66.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  67.     DDX_Text(pDX, IDC_INT, m_int);
  68.     DDX_Text(pDX, IDC_FLOAT, m_float);
  69.     DDX_Text(pDX, IDC_STRING, m_str);
  70.     //}}AFX_DATA_MAP
  71. }
  72.  
  73.  
  74. BEGIN_MESSAGE_MAP(CTypedPtrArrayView, CFormView)
  75.     //{{AFX_MSG_MAP(CTypedPtrArrayView)
  76.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  77.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  78.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  79.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  80.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  81.     //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83.  
  84.  
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CTypedPtrArrayView diagnostics
  88.  
  89. #ifdef _DEBUG
  90. void CTypedPtrArrayView::AssertValid() const
  91. {
  92.     CFormView::AssertValid();
  93. }
  94.  
  95. void CTypedPtrArrayView::Dump(CDumpContext& dc) const
  96. {
  97.     CFormView::Dump(dc);
  98. }
  99.  
  100. CCollectDoc* CTypedPtrArrayView::GetDocument() // non-debug version is inline
  101. {
  102.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  103. }
  104. #endif //_DEBUG
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CmyobArrayView internal implementation
  108.  
  109. void CTypedPtrArrayView::AddMyObjectToListBox(CMyObject* pMyObject,
  110.     int nSel)
  111. {
  112.     // Add new CMyObject to the listbox.
  113.     CString str;
  114.     pMyObject->Format(str);
  115.     if (nSel == -1)
  116.         nSel = m_ctlList.AddString(str);
  117.     else
  118.         m_ctlList.InsertString(nSel, str);
  119. }
  120.  
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CTypedPtrArrayView message handlers
  124.  
  125. void CTypedPtrArrayView::OnAdd()
  126. {
  127.     if (UpdateData() != TRUE)
  128.         return;
  129.  
  130.     try {
  131.     // Add new CMyObject to the CTypedPtrArray
  132.     CMyObject* pMyObject = new CMyObject;
  133.     pMyObject->m_int = m_int;
  134.     pMyObject->m_float = m_float;
  135.     pMyObject->m_str = m_str.AllocSysString();
  136.     GetDocument()->m_myobArray->Add = pMyObject;
  137.  
  138.     AddMyObjectToListBox(pMyObject);
  139.     } catch(_com_error& e) {
  140.         dump_com_error(e);
  141.     }
  142. }
  143.  
  144. void CTypedPtrArrayView::OnUpdate()
  145. {
  146.     if (UpdateData() != TRUE)
  147.         return;
  148.  
  149.     int nSel = m_ctlList.GetCurSel();
  150.     if (nSel == LB_ERR)
  151.     {
  152.         AfxMessageBox(IDS_SELECT_ENTRY_TO_BE_UPDATED);
  153.         return;
  154.     }
  155.  
  156.     try {
  157.     CMyObject* pMyObject = (CMyObject*) GetDocument()->m_myobArray->Array[nSel];
  158.  
  159.     // Replace the value of the CMyObject in the CTypedPtrArray.
  160.     pMyObject->m_int = m_int;
  161.     pMyObject->m_float = m_float;
  162.     pMyObject->m_str = m_str.AllocSysString();
  163.  
  164.     // Replace the displayed CMyObject in the listbox by removing
  165.     // the old listbox entry and adding a new entry.
  166.     m_ctlList.DeleteString(nSel);
  167.     AddMyObjectToListBox(pMyObject, nSel);
  168.     } catch(_com_error& e) {
  169.         dump_com_error(e);
  170.     }
  171. }
  172.  
  173. void CTypedPtrArrayView::OnRemove()
  174. {
  175.     int nSel = m_ctlList.GetCurSel();
  176.     if (nSel == LB_ERR)
  177.     {
  178.         AfxMessageBox(IDS_SELECT_ENTRY_TO_BE_REMOVED);
  179.         return;
  180.     }
  181.  
  182.     try {
  183.     CMyObject* pMyObject = (CMyObject*) GetDocument()->m_myobArray->Array[nSel];
  184.  
  185.     // Remove the CMyObject ptr from the CTypedPtrArray.
  186.     GetDocument()->m_myobArray->Remove[nSel];
  187.  
  188.     // Delete the CMyObject object. (The CTypedPtrArray only holds the ptr.)
  189.     delete pMyObject;
  190.     } catch(_com_error& e) {
  191.         dump_com_error(e);
  192.     }
  193.  
  194.     // Remove the corresponding entry from the listbox.
  195.     m_ctlList.DeleteString(nSel);
  196. }
  197.  
  198. void CTypedPtrArrayView::OnRemoveAll()
  199. {
  200.     CCollectDoc* pDoc = GetDocument();
  201.     try {
  202.     for (int n = 0; n < (int) pDoc->m_myobArray->Count; n++)
  203.         delete (CMyObject*) pDoc->m_myobArray->Array[n];
  204.  
  205.     pDoc->m_myobArray->RemoveAll();
  206.     } catch(_com_error& e) {
  207.         dump_com_error(e);
  208.     }
  209.  
  210.     // Remove all of the corresponding formatted strings from the listbox.
  211.     m_ctlList.ResetContent();
  212. }
  213.  
  214. void CTypedPtrArrayView::OnSelChangeList()
  215. {
  216.     // Update the edit control to reflect the new selection
  217.     // in the listbox.
  218.     int nSel = m_ctlList.GetCurSel();
  219.     try {
  220.     CMyObject* pMyObject = (CMyObject*) GetDocument()->m_myobArray->Array[nSel];
  221.  
  222.     m_int = pMyObject->m_int;
  223.     m_float = pMyObject->m_float;
  224.     m_str = pMyObject->m_str;
  225.     } catch(_com_error& e) {
  226.         dump_com_error(e);
  227.     }
  228.  
  229.     UpdateData(FALSE);
  230. }
  231.