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 / intlstvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  5.3 KB  |  241 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.     IStlintListPtr& intList = GetDocument()->m_intList;
  48.     try {
  49.     intList->First();
  50.     long n = 0;
  51.     while (intList->Next(&n))
  52.         AddIntToListBox(n);
  53.     } catch(_com_error& e) {
  54.         dump_com_error(e);
  55.     }
  56. }
  57.  
  58.  
  59. void CIntListView::DoDataExchange(CDataExchange* pDX)
  60. {
  61.     CFormView::DoDataExchange(pDX);
  62.     //{{AFX_DATA_MAP(CIntListView)
  63.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  64.     DDX_Text(pDX, IDC_ELEMENT, m_int);
  65.     //}}AFX_DATA_MAP
  66. }
  67.  
  68.  
  69. BEGIN_MESSAGE_MAP(CIntListView, CFormView)
  70.     //{{AFX_MSG_MAP(CIntListView)
  71.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  72.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  73.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  74.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  75.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  76.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  77.     //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79.  
  80.  
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CIntListView diagnostics
  84.  
  85. #ifdef _DEBUG
  86. void CIntListView::AssertValid() const
  87. {
  88.     CFormView::AssertValid();
  89. }
  90.  
  91. void CIntListView::Dump(CDumpContext& dc) const
  92. {
  93.     CFormView::Dump(dc);
  94. }
  95.  
  96. CCollectDoc* CIntListView::GetDocument() // non-debug version is inline
  97. {
  98.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  99. }
  100. #endif //_DEBUG
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CIntListView internal implementation
  104.  
  105. BOOL CIntListView::FindInt(int& nSel)
  106. {
  107.     nSel = m_ctlList.GetCurSel();
  108.     if (nSel == LB_ERR)
  109.     {
  110.         AfxMessageBox(IDS_SELECT_INTEGER);
  111.         return FALSE;
  112.     }
  113.  
  114.     int nValue = (int)m_ctlList.GetItemData(nSel);
  115.  
  116.     try {
  117.     // Find the integer in the integer list.
  118.     return GetDocument()->m_intList->Find(nValue);
  119.     } catch(_com_error& e) {
  120.         dump_com_error(e);
  121.     }
  122.     return FALSE;
  123. }
  124.  
  125. void CIntListView::AddIntToListBox(int n, int nSel)
  126. {
  127.     // Add new integer to the listbox.
  128.     CString str;
  129.     str.Format(_T("%i"),n);
  130.     if (nSel == -1)
  131.         nSel = m_ctlList.AddString(str);
  132.     else
  133.         m_ctlList.InsertString(nSel, str);
  134.  
  135.     // Save the integer as the listbox entry's "data item".
  136.     m_ctlList.SetItemData(nSel, (DWORD)n);
  137. }
  138.  
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CIntListView message handlers
  142.  
  143. void CIntListView::OnAdd()
  144. {
  145.     if (UpdateData() != TRUE)
  146.         return;
  147.  
  148.     try {
  149.     // Add new integer to the CList<int,int>
  150.     GetDocument()->m_intList->Add = m_int;
  151.     } catch(_com_error& e) {
  152.         dump_com_error(e);
  153.     }
  154.  
  155.     AddIntToListBox(m_int);
  156. }
  157.  
  158. void CIntListView::OnInsertBefore()
  159. {
  160.     if (UpdateData() != TRUE)
  161.         return;
  162.  
  163.     int nSel;
  164.     // Find the integer in both the CList<int,int> and the listbox.
  165.     if (!FindInt(nSel))
  166.         return;
  167.  
  168.  
  169.     try {
  170.     // Insert in front of the integer found in the CList<int,int>
  171.     GetDocument()->m_intList->InsertBefore = m_int;
  172.     } catch(_com_error& e) {
  173.         dump_com_error(e);
  174.     }
  175.  
  176.     // Insert new integer in the listbox
  177.     AddIntToListBox(m_int, nSel);
  178. }
  179.  
  180. void CIntListView::OnUpdate()
  181. {
  182.     if (UpdateData() != TRUE)
  183.         return;
  184.  
  185.     int nSel;
  186.     // Find the integer in both the CList<int,int> and the listbox.
  187.     if (!FindInt(nSel))
  188.         return;
  189.  
  190.     try {
  191.     // Replace the integer in the CList<int,int>.
  192.     GetDocument()->m_intList->SetAt = m_int;
  193.     } catch(_com_error& e) {
  194.         dump_com_error(e);
  195.     }
  196.  
  197.     // Update the old integer in the listbox by removing
  198.     // the old entry and adding a new entry.
  199.     m_ctlList.DeleteString(nSel);
  200.     AddIntToListBox(m_int, nSel);
  201. }
  202.  
  203. void CIntListView::OnRemove()
  204. {
  205.     int nSel;
  206.     // Find the string in both the CList<int,int> and in the listbox.
  207.     if (!FindInt(nSel))
  208.         return;
  209.  
  210.     try {
  211.     // Remove the integer from the CList<int,int>.
  212.     GetDocument()->m_intList->Remove;
  213.     } catch(_com_error& e) {
  214.         dump_com_error(e);
  215.     }
  216.  
  217.     // Remove the integer from the listbox.
  218.     m_ctlList.DeleteString(nSel);
  219. }
  220.  
  221. void CIntListView::OnRemoveAll()
  222. {
  223.     try {
  224.     // Remove all of the integers from the CList<int,int>.
  225.     GetDocument()->m_intList->RemoveAll();
  226.     } catch(_com_error& e) {
  227.         dump_com_error(e);
  228.     }
  229.  
  230.     // Remove all of the integers from the listbox.
  231.     m_ctlList.ResetContent();
  232. }
  233.  
  234. void CIntListView::OnSelChangeList()
  235. {
  236.     // Update the edit control to reflect the new selection
  237.     // in the listbox.
  238.     m_int = (int)m_ctlList.GetItemData(m_ctlList.GetCurSel());
  239.     UpdateData(FALSE);
  240. }
  241.