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

  1. // intlstvw.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 "intlstvw.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CIntListView
  25.  
  26. IMPLEMENT_DYNCREATE(CIntListView, CFormView)
  27.  
  28. CIntListView::CIntListView()
  29.     : CFormView(CIntListView::IDD)
  30. {
  31.     //{{AFX_DATA_INIT(CIntListView)
  32.     m_int = 0;
  33.     //}}AFX_DATA_INIT
  34. }
  35.  
  36. CIntListView::~CIntListView()
  37. {
  38. }
  39.  
  40. void CIntListView::OnInitialUpdate()
  41. {
  42.     CFormView::OnInitialUpdate();
  43.  
  44.     // Copy all of the integers from the document's CList<int,int>
  45.     // to the listbox.
  46.     m_ctlList.ResetContent();
  47.     CList<int,int>& intList = GetDocument()->m_intList;
  48.     POSITION pos = intList.GetHeadPosition();
  49.     while (pos != NULL)
  50.     {
  51.         int n = intList.GetNext(pos);
  52.         AddIntToListBox(n);
  53.     }
  54. }
  55.  
  56.  
  57. void CIntListView::DoDataExchange(CDataExchange* pDX)
  58. {
  59.     CFormView::DoDataExchange(pDX);
  60.     //{{AFX_DATA_MAP(CIntListView)
  61.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  62.     DDX_Text(pDX, IDC_ELEMENT, m_int);
  63.     //}}AFX_DATA_MAP
  64. }
  65.  
  66.  
  67. BEGIN_MESSAGE_MAP(CIntListView, CFormView)
  68.     //{{AFX_MSG_MAP(CIntListView)
  69.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  70.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  71.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  72.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  73.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  74.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  75.     //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77.  
  78.  
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CIntListView diagnostics
  82.  
  83. #ifdef _DEBUG
  84. void CIntListView::AssertValid() const
  85. {
  86.     CFormView::AssertValid();
  87. }
  88.  
  89. void CIntListView::Dump(CDumpContext& dc) const
  90. {
  91.     CFormView::Dump(dc);
  92. }
  93.  
  94. CCollectDoc* CIntListView::GetDocument() // non-debug version is inline
  95. {
  96.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  97. }
  98. #endif //_DEBUG
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CIntListView internal implementation
  102.  
  103. BOOL CIntListView::FindInt(int& nSel, POSITION& pos)
  104. {
  105.     nSel = m_ctlList.GetCurSel();
  106.     if (nSel == LB_ERR)
  107.     {
  108.         AfxMessageBox(IDS_SELECT_INTEGER);
  109.         return FALSE;
  110.     }
  111.  
  112.     int nValue = (int)m_ctlList.GetItemData(nSel);
  113.  
  114.     // Find the integer in the integer list.
  115.     pos = GetDocument()->m_intList.Find(nValue);
  116.  
  117.     // If the integer is in the listbox, it must also be in the integer list
  118.     ASSERT(pos != NULL);
  119.  
  120.     return TRUE;
  121. }
  122.  
  123. void CIntListView::AddIntToListBox(int n, int nSel)
  124. {
  125.     // Add new integer to the listbox.
  126.     CString str;
  127.     str.Format(_T("%i"),n);
  128.     if (nSel == -1)
  129.         nSel = m_ctlList.AddString(str);
  130.     else
  131.         m_ctlList.InsertString(nSel, str);
  132.  
  133.     // Save the integer as the listbox entry's "data item".
  134.     m_ctlList.SetItemData(nSel, (DWORD)n);
  135. }
  136.  
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CIntListView message handlers
  140.  
  141. void CIntListView::OnAdd()
  142. {
  143.     if (UpdateData() != TRUE)
  144.         return;
  145.  
  146.     // Add new integer to the CList<int,int>
  147.     GetDocument()->m_intList.AddTail(m_int);
  148.  
  149.     AddIntToListBox(m_int);
  150. }
  151.  
  152. void CIntListView::OnInsertBefore()
  153. {
  154.     if (UpdateData() != TRUE)
  155.         return;
  156.  
  157.     int nSel;
  158.     POSITION pos;
  159.     // Find the integer in both the CList<int,int> and the listbox.
  160.     if (FindInt(nSel, pos) != TRUE)
  161.         return;
  162.  
  163.  
  164.     // Insert in front of the integer found in the CList<int,int>
  165.     POSITION posTemp = GetDocument()->m_intList.InsertBefore(pos, m_int);
  166.  
  167.     // Insert new integer in the listbox
  168.     AddIntToListBox(m_int, nSel);
  169. }
  170.  
  171. void CIntListView::OnUpdate()
  172. {
  173.     if (UpdateData() != TRUE)
  174.         return;
  175.  
  176.     int nSel;
  177.     POSITION pos;
  178.     // Find the integer in both the CList<int,int> and the listbox.
  179.     if (FindInt(nSel, pos) != TRUE)
  180.         return;
  181.  
  182.     // Replace the integer in the CList<int,int>.
  183.     GetDocument()->m_intList.SetAt(pos, m_int);
  184.  
  185.     // Update the old integer in the listbox by removing
  186.     // the old entry and adding a new entry.
  187.     m_ctlList.DeleteString(nSel);
  188.     AddIntToListBox(m_int, nSel);
  189. }
  190.  
  191. void CIntListView::OnRemove()
  192. {
  193.     int nSel;
  194.     POSITION pos;
  195.     // Find the string in both the CList<int,int> and in the listbox.
  196.     if (FindInt(nSel, pos) != TRUE)
  197.         return;
  198.  
  199.  
  200.     // Remove the integer from the CList<int,int>.
  201.     GetDocument()->m_intList.RemoveAt(pos);
  202.  
  203.     // Remove the integer from the listbox.
  204.     m_ctlList.DeleteString(nSel);
  205. }
  206.  
  207. void CIntListView::OnRemoveAll()
  208. {
  209.     // Remove all of the integers from the CList<int,int>.
  210.     GetDocument()->m_intList.RemoveAll();
  211.  
  212.     // Remove all of the integers from the listbox.
  213.     m_ctlList.ResetContent();
  214. }
  215.  
  216. void CIntListView::OnSelChangeList()
  217. {
  218.     // Update the edit control to reflect the new selection
  219.     // in the listbox.
  220.     m_int = (int)m_ctlList.GetItemData(m_ctlList.GetCurSel());
  221.     UpdateData(FALSE);
  222. }
  223.