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 / dwarryvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  4.6 KB  |  205 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.     IStlDWordArrayPtr& dwArray = GetDocument()->m_dwArray;
  48.     try {
  49.     for (int nIndex = 0; nIndex < (int) dwArray->Count; nIndex++)
  50.     {
  51.         DWORD dw = dwArray->Array[nIndex];
  52.         CString str;
  53.         str.Format(_T("%d"),dw);
  54.         int nSel = m_ctlList.AddString(str);
  55.     }
  56.     } catch(_com_error& e) {
  57.         dump_com_error(e);
  58.     }
  59. }
  60.  
  61. void CDWordArrayView::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CFormView::DoDataExchange(pDX);
  64.     //{{AFX_DATA_MAP(CDWordArrayView)
  65.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  66.     DDX_Text(pDX, IDC_DWORD, m_dw);
  67.     //}}AFX_DATA_MAP
  68. }
  69.  
  70.  
  71. BEGIN_MESSAGE_MAP(CDWordArrayView, CFormView)
  72.     //{{AFX_MSG_MAP(CDWordArrayView)
  73.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  74.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  75.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  76.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  77.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  78.     //}}AFX_MSG_MAP
  79. END_MESSAGE_MAP()
  80.  
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CDWordArrayView diagnostics
  85.  
  86. #ifdef _DEBUG
  87. void CDWordArrayView::AssertValid() const
  88. {
  89.     CFormView::AssertValid();
  90. }
  91.  
  92. void CDWordArrayView::Dump(CDumpContext& dc) const
  93. {
  94.     CFormView::Dump(dc);
  95. }
  96.  
  97. CCollectDoc* CDWordArrayView::GetDocument() // non-debug version is inline
  98. {
  99.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  100. }
  101. #endif //_DEBUG
  102.  
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CDWordArrayView message handlers
  106.  
  107. void CDWordArrayView::OnAdd()
  108. {
  109.     if (UpdateData() != TRUE)
  110.         return;
  111.  
  112.     try {
  113.     // Add new DWORD to the CDWordArray
  114.     GetDocument()->m_dwArray->Add = m_dw;
  115.  
  116.     // Add new DWORD to the listbox.
  117.     CString str;
  118.     str.Format(_T("%d"),m_dw);
  119.     UINT nSel = m_ctlList.AddString(str) + 1;
  120.  
  121.     // The index of the new DWORD in the array should be the
  122.     // same as the index of the new DWORD in the listbox.
  123.  
  124.     ASSERT(GetDocument()->m_dwArray->Count == nSel);
  125.     } catch(_com_error& e) {
  126.         dump_com_error(e);
  127.     }
  128. }
  129.  
  130. void CDWordArrayView::OnUpdate()
  131. {
  132.     if (UpdateData() != TRUE)
  133.         return;
  134.  
  135.     int nSel = m_ctlList.GetCurSel();
  136.     if (nSel == LB_ERR)
  137.     {
  138.         AfxMessageBox(IDS_SELECT_DWORD_TO_UPDATE);
  139.         return;
  140.     }
  141.  
  142.     try {
  143.     // Replace the DWORD in the CDWordArray.
  144.     GetDocument()->m_dwArray->Array[nSel] = m_dw;
  145.     } catch(_com_error& e) {
  146.         dump_com_error(e);
  147.     }
  148.  
  149.     // Update the old DWORD in the listbox by removing
  150.     // the old entry and adding a new entry.
  151.     m_ctlList.DeleteString(nSel);
  152.     CString str;
  153.     str.Format(_T("%d"),m_dw);
  154.     m_ctlList.InsertString(nSel, str);
  155. }
  156.  
  157. void CDWordArrayView::OnRemove()
  158. {
  159.     if (UpdateData() != TRUE)
  160.         return;
  161.  
  162.     int nSel = m_ctlList.GetCurSel();
  163.     if (nSel == LB_ERR)
  164.     {
  165.         AfxMessageBox(IDS_SELECT_DWORD_TO_REMOVE);
  166.         return;
  167.     }
  168.  
  169.     try {
  170.     // Remove the DWORD from the CDWordArray.
  171.     GetDocument()->m_dwArray->Remove[nSel];
  172.     } catch(_com_error& e) {
  173.         dump_com_error(e);
  174.     }
  175.  
  176.     // Remove the DWORD from the listbox.
  177.     m_ctlList.DeleteString(nSel);
  178. }
  179.  
  180. void CDWordArrayView::OnRemoveAll()
  181. {
  182.     try {
  183.     // Remove all of the DWORDs from the CDWordArray.
  184.     GetDocument()->m_dwArray->RemoveAll();
  185.     } catch(_com_error& e) {
  186.         dump_com_error(e);
  187.     }
  188.  
  189.     // Remove all of the DWORDs from the listbox.
  190.     m_ctlList.ResetContent();
  191. }
  192.  
  193. void CDWordArrayView::OnSelChangeList()
  194. {
  195.     try {
  196.     // Update the edit control to reflect the new selection
  197.     // in the listbox.
  198.     m_dw = GetDocument()->m_dwArray->Array[m_ctlList.GetCurSel()];
  199.     } catch(_com_error& e) {
  200.         dump_com_error(e);
  201.     }
  202.  
  203.     UpdateData(FALSE);
  204. }
  205.