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

  1. // dwarryvw.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 "dwarryvw.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDWordArrayView
  25.  
  26. IMPLEMENT_DYNCREATE(CDWordArrayView, CFormView)
  27.  
  28. CDWordArrayView::CDWordArrayView()
  29.     : CFormView(CDWordArrayView::IDD)
  30. {
  31.     //{{AFX_DATA_INIT(CDWordArrayView)
  32.     m_dw = 0;
  33.     //}}AFX_DATA_INIT
  34. }
  35.  
  36. CDWordArrayView::~CDWordArrayView()
  37. {
  38. }
  39.  
  40. void CDWordArrayView::OnInitialUpdate()
  41. {
  42.     CFormView::OnInitialUpdate();
  43.  
  44.     // Copy all of the DWORDs from the document's CDWordArray
  45.     // to the listbox.
  46.     m_ctlList.ResetContent();
  47.     CDWordArray& dwArray = GetDocument()->m_dwArray;
  48.     for (int nIndex = 0; nIndex < dwArray.GetSize(); nIndex++)
  49.     {
  50.         DWORD dw = dwArray[nIndex];
  51.         CString str;
  52.         str.Format(_T("%d"),dw);
  53.         int nSel = m_ctlList.AddString(str);
  54.     }
  55. }
  56.  
  57. void CDWordArrayView::DoDataExchange(CDataExchange* pDX)
  58. {
  59.     CFormView::DoDataExchange(pDX);
  60.     //{{AFX_DATA_MAP(CDWordArrayView)
  61.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  62.     DDX_Text(pDX, IDC_DWORD, m_dw);
  63.     //}}AFX_DATA_MAP
  64. }
  65.  
  66.  
  67. BEGIN_MESSAGE_MAP(CDWordArrayView, CFormView)
  68.     //{{AFX_MSG_MAP(CDWordArrayView)
  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.     //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76.  
  77.  
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CDWordArrayView diagnostics
  81.  
  82. #ifdef _DEBUG
  83. void CDWordArrayView::AssertValid() const
  84. {
  85.     CFormView::AssertValid();
  86. }
  87.  
  88. void CDWordArrayView::Dump(CDumpContext& dc) const
  89. {
  90.     CFormView::Dump(dc);
  91. }
  92.  
  93. CCollectDoc* CDWordArrayView::GetDocument() // non-debug version is inline
  94. {
  95.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  96. }
  97. #endif //_DEBUG
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CDWordArrayView message handlers
  102.  
  103. void CDWordArrayView::OnAdd()
  104. {
  105.     if (UpdateData() != TRUE)
  106.         return;
  107.  
  108.     // Add new DWORD to the CDWordArray
  109.     int nIndex = GetDocument()->m_dwArray.Add(m_dw);
  110.  
  111.     // Add new DWORD to the listbox.
  112.     CString str;
  113.     str.Format(_T("%d"),m_dw);
  114.     int nSel = m_ctlList.AddString(str);
  115.  
  116.     // The index of the new DWORD in the array should be the
  117.     // same as the index of the new DWORD in the listbox.
  118.     ASSERT(nIndex == nSel);
  119. }
  120.  
  121. void CDWordArrayView::OnUpdate()
  122. {
  123.     if (UpdateData() != TRUE)
  124.         return;
  125.  
  126.     int nSel = m_ctlList.GetCurSel();
  127.     if (nSel == LB_ERR)
  128.     {
  129.         AfxMessageBox(IDS_SELECT_DWORD_TO_UPDATE);
  130.         return;
  131.     }
  132.  
  133.     // Replace the DWORD in the CDWordArray.
  134.     GetDocument()->m_dwArray[nSel] = m_dw;
  135.  
  136.     // Update the old DWORD in the listbox by removing
  137.     // the old entry and adding a new entry.
  138.     m_ctlList.DeleteString(nSel);
  139.     CString str;
  140.     str.Format(_T("%d"),m_dw);
  141.     m_ctlList.InsertString(nSel, str);
  142. }
  143.  
  144. void CDWordArrayView::OnRemove()
  145. {
  146.     if (UpdateData() != TRUE)
  147.         return;
  148.  
  149.     int nSel = m_ctlList.GetCurSel();
  150.     if (nSel == LB_ERR)
  151.     {
  152.         AfxMessageBox(IDS_SELECT_DWORD_TO_REMOVE);
  153.         return;
  154.     }
  155.  
  156.     // Remove the DWORD from the CDWordArray.
  157.     GetDocument()->m_dwArray.RemoveAt(nSel);
  158.  
  159.     // Remove the DWORD from the listbox.
  160.     m_ctlList.DeleteString(nSel);
  161. }
  162.  
  163. void CDWordArrayView::OnRemoveAll()
  164. {
  165.     // Remove all of the DWORDs from the CDWordArray.
  166.     GetDocument()->m_dwArray.RemoveAll();
  167.  
  168.     // Remove all of the DWORDs from the listbox.
  169.     m_ctlList.ResetContent();
  170. }
  171.  
  172. void CDWordArrayView::OnSelChangeList()
  173. {
  174.     // Update the edit control to reflect the new selection
  175.     // in the listbox.
  176.     m_dw = GetDocument()->m_dwArray[m_ctlList.GetCurSel()];
  177.     UpdateData(FALSE);
  178. }
  179.