home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / collect / typaryvw.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  208 lines

  1. // typaryvw.cpp : implementation file
  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 "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.     // Copy all of the entries from the document's CTypedPtrArray
  48.     // to the listbox.
  49.     m_ctlList.ResetContent();
  50.     CMyObjectArray& myobArray = GetDocument()->m_myobArray;
  51.     for (int nIndex = 0; nIndex < myobArray.GetSize(); nIndex++)
  52.     {
  53.         CMyObject* pMyObject = myobArray[nIndex];
  54.         AddMyObjectToListBox(pMyObject);
  55.     }
  56. }
  57.  
  58. void CTypedPtrArrayView::DoDataExchange(CDataExchange* pDX)
  59. {
  60.     CFormView::DoDataExchange(pDX);
  61.     //{{AFX_DATA_MAP(CTypedPtrArrayView)
  62.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  63.     DDX_Text(pDX, IDC_INT, m_int);
  64.     DDX_Text(pDX, IDC_FLOAT, m_float);
  65.     DDX_Text(pDX, IDC_STRING, m_str);
  66.     //}}AFX_DATA_MAP
  67. }
  68.  
  69.  
  70. BEGIN_MESSAGE_MAP(CTypedPtrArrayView, CFormView)
  71.     //{{AFX_MSG_MAP(CTypedPtrArrayView)
  72.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  73.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  74.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  75.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  76.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  77.     //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79.  
  80.  
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CTypedPtrArrayView diagnostics
  84.  
  85. #ifdef _DEBUG
  86. void CTypedPtrArrayView::AssertValid() const
  87. {
  88.     CFormView::AssertValid();
  89. }
  90.  
  91. void CTypedPtrArrayView::Dump(CDumpContext& dc) const
  92. {
  93.     CFormView::Dump(dc);
  94. }
  95.  
  96. CCollectDoc* CTypedPtrArrayView::GetDocument() // non-debug version is inline
  97. {
  98.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  99. }
  100. #endif //_DEBUG
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CmyobArrayView internal implementation
  104.  
  105. void CTypedPtrArrayView::AddMyObjectToListBox(CMyObject* pMyObject,
  106.     int nSel)
  107. {
  108.     // Add new CMyObject to the listbox.
  109.     CString str;
  110.     pMyObject->FormatMyObject(str);
  111.     if (nSel == -1)
  112.         nSel = m_ctlList.AddString(str);
  113.     else
  114.         m_ctlList.InsertString(nSel, str);
  115. }
  116.  
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CTypedPtrArrayView message handlers
  120.  
  121. void CTypedPtrArrayView::OnAdd()
  122. {
  123.     if (UpdateData() != TRUE)
  124.         return;
  125.  
  126.     // Add new CMyObject to the CTypedPtrArray
  127.     CMyObject* pMyObject = new CMyObject;
  128.     pMyObject->m_int = m_int;
  129.     pMyObject->m_float = m_float;
  130.     pMyObject->m_str = m_str;
  131.     GetDocument()->m_myobArray.Add(pMyObject);
  132.  
  133.     AddMyObjectToListBox(pMyObject);
  134. }
  135.  
  136. void CTypedPtrArrayView::OnUpdate()
  137. {
  138.     if (UpdateData() != TRUE)
  139.         return;
  140.  
  141.     int nSel = m_ctlList.GetCurSel();
  142.     if (nSel == LB_ERR)
  143.     {
  144.         AfxMessageBox(IDS_SELECT_ENTRY_TO_BE_UPDATED);
  145.         return;
  146.     }
  147.  
  148.     CMyObject* pMyObject = GetDocument()->m_myobArray[nSel];
  149.  
  150.     // Replace the value of the CMyObject in the CTypedPtrArray.
  151.     pMyObject->m_int = m_int;
  152.     pMyObject->m_float = m_float;
  153.     pMyObject->m_str = m_str;
  154.  
  155.     // Replace the displayed CMyObject in the listbox by removing
  156.     // the old listbox entry and adding a new entry.
  157.     m_ctlList.DeleteString(nSel);
  158.     AddMyObjectToListBox(pMyObject, nSel);
  159. }
  160.  
  161. void CTypedPtrArrayView::OnRemove()
  162. {
  163.     int nSel = m_ctlList.GetCurSel();
  164.     if (nSel == LB_ERR)
  165.     {
  166.         AfxMessageBox(IDS_SELECT_ENTRY_TO_BE_REMOVED);
  167.         return;
  168.     }
  169.  
  170.     CMyObject* pMyObject = GetDocument()->m_myobArray[nSel];
  171.  
  172.     // Remove the CMyObject ptr from the CTypedPtrArray.
  173.     GetDocument()->m_myobArray.RemoveAt(nSel);
  174.  
  175.     // Delete the CMyObject object. (The CTypedPtrArray only holds the ptr.)
  176.     delete pMyObject;
  177.  
  178.     // Remove the corresponding entry from the listbox.
  179.     m_ctlList.DeleteString(nSel);
  180. }
  181.  
  182. void CTypedPtrArrayView::OnRemoveAll()
  183. {
  184.     CCollectDoc* pDoc = GetDocument();
  185.     for (int n = 0; n < pDoc->m_myobArray.GetSize(); n++)
  186.     {
  187.         delete pDoc->m_myobArray[n];
  188.     }
  189.     pDoc->m_myobArray.RemoveAll();
  190.  
  191.     // Remove all of the corresponding formatted strings from the listbox.
  192.     m_ctlList.ResetContent();
  193. }
  194.  
  195. void CTypedPtrArrayView::OnSelChangeList()
  196. {
  197.     // Update the edit control to reflect the new selection
  198.     // in the listbox.
  199.     int nSel = m_ctlList.GetCurSel();
  200.     CMyObject* pMyObject = GetDocument()->m_myobArray[nSel];
  201.  
  202.     m_int = pMyObject->m_int;
  203.     m_float = pMyObject->m_float;
  204.     m_str = pMyObject->m_str;
  205.  
  206.     UpdateData(FALSE);
  207. }
  208.