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 / typlstvw.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  7KB  |  265 lines

  1. // typlstvw.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 "typlstvw.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. // CTypedPtrListView
  26.  
  27. IMPLEMENT_DYNCREATE(CTypedPtrListView, CFormView)
  28.  
  29. CTypedPtrListView::CTypedPtrListView()
  30.     : CFormView(CTypedPtrListView::IDD)
  31. {
  32.     //{{AFX_DATA_INIT(CTypedPtrListView)
  33.     m_float = 0.0f;
  34.     m_int = 0;
  35.     m_str = "";
  36.     //}}AFX_DATA_INIT
  37. }
  38.  
  39. CTypedPtrListView::~CTypedPtrListView()
  40. {
  41. }
  42.  
  43. void CTypedPtrListView::OnInitialUpdate()
  44. {
  45.     CFormView::OnInitialUpdate();
  46.  
  47.     // Copy all of the strings from the document's CTypedPtrList
  48.     // to the listbox.
  49.     m_ctlList.ResetContent();
  50.     CMyStructList& mystructList = GetDocument()->m_mystructList;
  51.     POSITION pos = mystructList.GetHeadPosition();
  52.     while (pos != NULL)
  53.     {
  54.         CMyStruct* pMyStruct = mystructList.GetNext(pos);
  55.         AddMyStructToListBox(pMyStruct);
  56.     }
  57. }
  58.  
  59. void CTypedPtrListView::DoDataExchange(CDataExchange* pDX)
  60. {
  61.     CFormView::DoDataExchange(pDX);
  62.     //{{AFX_DATA_MAP(CTypedPtrListView)
  63.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  64.     DDX_Text(pDX, IDC_FLOAT, m_float);
  65.     DDX_Text(pDX, IDC_INT, m_int);
  66.     DDX_Text(pDX, IDC_STRING, m_str);
  67.     //}}AFX_DATA_MAP
  68.     if (pDX->m_bSaveAndValidate && _tcschr(m_str, ' ') != NULL)
  69.     {
  70.         AfxMessageBox(IDS_BLANK_CHARS_NOT_ALLOWED);
  71.         pDX->Fail();
  72.     }
  73. }
  74.  
  75.  
  76. BEGIN_MESSAGE_MAP(CTypedPtrListView, CFormView)
  77.     //{{AFX_MSG_MAP(CTypedPtrListView)
  78.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  79.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  80.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  81.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  82.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  83.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  84.     //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86.  
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CTypedPtrListView diagnostics
  90.  
  91. #ifdef _DEBUG
  92. void CTypedPtrListView::AssertValid() const
  93. {
  94.     CFormView::AssertValid();
  95. }
  96.  
  97. void CTypedPtrListView::Dump(CDumpContext& dc) const
  98. {
  99.     CFormView::Dump(dc);
  100. }
  101.  
  102. CCollectDoc* CTypedPtrListView::GetDocument() // non-debug version is inline
  103. {
  104.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  105. }
  106.  
  107. #endif //_DEBUG
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CmystructListView internal implementation
  111.  
  112. CMyStruct* CTypedPtrListView::FindMyStruct(int& nSel, POSITION& pos)
  113. {
  114.     nSel = m_ctlList.GetCurSel();
  115.     if (nSel == LB_ERR)
  116.     {
  117.         AfxMessageBox(IDS_SELECT_ENTRY);
  118.         return NULL;
  119.     }
  120.  
  121.     // The CMyStruct pointer was saved as the listbox entry's data item.
  122.     CMyStruct* pMyStruct = (CMyStruct*)m_ctlList.GetItemDataPtr(nSel);
  123.  
  124.     // Find the CMyStruct pointer in the CTypedPtrList.
  125.     pos = GetDocument()->m_mystructList.Find(pMyStruct);
  126.  
  127.     // If the CMyStruct is displayed in the listbox, it should also be
  128.     // found in the CTypedPtrList.
  129.     ASSERT(pos != NULL);
  130.  
  131.     return pMyStruct;
  132. }
  133.  
  134. void CTypedPtrListView::AddMyStructToListBox(CMyStruct* pMyStruct, int nSel)
  135. {
  136.     // Add new CMyStruct to the listbox.
  137.     CString str;
  138.     pMyStruct->FormatMyStruct(str);
  139.     if (nSel == -1)
  140.         nSel = m_ctlList.AddString(str);
  141.     else
  142.         m_ctlList.InsertString(nSel, str);
  143.  
  144.     // Save the CMyStruct pointer as the listbox entry's "data item".
  145.     m_ctlList.SetItemDataPtr(nSel, pMyStruct);
  146. }
  147.  
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CTypedPtrListView message handlers
  151.  
  152. void CTypedPtrListView::OnAdd()
  153. {
  154.     if (UpdateData() != TRUE)
  155.         return;
  156.  
  157.     // Add new CMyStruct to the CTypedPtrList
  158.     CMyStruct* pMyStruct = new CMyStruct;
  159.     pMyStruct->m_int = m_int;
  160.     pMyStruct->m_float = m_float;
  161.     pMyStruct->m_str = m_str;
  162.     GetDocument()->m_mystructList.AddTail(pMyStruct);
  163.  
  164.     AddMyStructToListBox(pMyStruct);
  165. }
  166.  
  167. void CTypedPtrListView::OnInsertBefore()
  168. {
  169.     if (UpdateData() != TRUE)
  170.         return;
  171.  
  172.     int nSel;
  173.     POSITION pos;
  174.     // Find the CMyStruct in the CTypedPtrList and listbox.
  175.     if (FindMyStruct(nSel, pos) == NULL)
  176.         return;
  177.  
  178.     // Insert new CMyStruct in the CTypedPtrList
  179.     CMyStruct* pMyStruct = new CMyStruct;
  180.     pMyStruct->m_int = m_int;
  181.     pMyStruct->m_float = m_float;
  182.     pMyStruct->m_str = m_str;
  183.     GetDocument()->m_mystructList.InsertBefore(pos, pMyStruct);
  184.  
  185.     AddMyStructToListBox(pMyStruct, nSel);
  186. }
  187.  
  188. void CTypedPtrListView::OnUpdate()
  189. {
  190.     if (UpdateData() != TRUE)
  191.         return;
  192.  
  193.     int nSel;
  194.     POSITION pos;
  195.     // Find the CMyStruct in the CTypedPtrList and listbox.
  196.     CMyStruct* pMyStruct = FindMyStruct(nSel, pos);
  197.     if (pMyStruct == NULL)
  198.         return;
  199.  
  200.     // Replace the value of the CMyStruct in the CTypedPtrList.
  201.     pMyStruct->m_int = m_int;
  202.     pMyStruct->m_float = m_float;
  203.     pMyStruct->m_str = m_str;
  204.  
  205.     // Replace the displayed CMyStruct in the listbox by removing
  206.     // the old listbox entry and adding a new entry.
  207.     m_ctlList.DeleteString(nSel);
  208.     AddMyStructToListBox(pMyStruct, nSel);
  209. }
  210.  
  211. void CTypedPtrListView::OnRemove()
  212. {
  213.     int nSel;
  214.     POSITION pos;
  215.     // Find the CMyStruct in the CTypedPtrList and listbox.
  216.     CMyStruct* pMyStruct = FindMyStruct(nSel, pos);
  217.     if (pMyStruct == NULL)
  218.         return;
  219.  
  220.     // Remove the CMyStruct ptr from the CTypedPtrList.
  221.     GetDocument()->m_mystructList.RemoveAt(pos);
  222.  
  223.     // Delete the CMyStruct object. (The CTypePtrList only holds the ptr.)
  224.     delete pMyStruct;
  225.  
  226.     // Remove the corresponding entry from the listbox.
  227.     m_ctlList.DeleteString(nSel);
  228. }
  229.  
  230. void CTypedPtrListView::OnRemoveAll()
  231. {
  232.     CMyStructList& mystructList = GetDocument()->m_mystructList;
  233.  
  234.     // Delete all of the CMyStruct objects pointed to
  235.     // by the CTypedPtrList. Then remove all of the
  236.     // CMyStruct pointers from the CTypedPtrList, which
  237.     // is faster than removing each individually.
  238.     POSITION pos = mystructList.GetHeadPosition();
  239.     while (pos != NULL)
  240.     {
  241.         delete mystructList.GetNext(pos);
  242.     }
  243.     mystructList.RemoveAll();
  244.  
  245.     // Remove all of the corresponding formatted strings from the listbox.
  246.     m_ctlList.ResetContent();
  247. }
  248.  
  249. void CTypedPtrListView::OnSelChangeList()
  250. {
  251.     // Update the edit control to reflect the new selection
  252.     // in the listbox.
  253.  
  254.     int nSel;
  255.     POSITION pos;
  256.     // Find the CMyStruct in the CTypedPtrList and listbox.
  257.     CMyStruct* pMyStruct = FindMyStruct(nSel, pos);
  258.  
  259.     m_int = pMyStruct->m_int;
  260.     m_float = pMyStruct->m_float;
  261.     m_str = pMyStruct->m_str;
  262.  
  263.     UpdateData(FALSE);
  264. }
  265.