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

  1. // mapdwvw.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 "mapdwvw.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMapDWordToMyStructView
  25.  
  26. IMPLEMENT_DYNCREATE(CMapDWordToMyStructView, CFormView)
  27.  
  28. CMapDWordToMyStructView::CMapDWordToMyStructView()
  29.     : CFormView(CMapDWordToMyStructView::IDD)
  30. {
  31.     //{{AFX_DATA_INIT(CMapDWordToMyStructView)
  32.     m_int = 0;
  33.     m_float = 0.0f;
  34.     m_str = "";
  35.     m_dwKey = 0;
  36.     //}}AFX_DATA_INIT
  37. }
  38.  
  39. CMapDWordToMyStructView::~CMapDWordToMyStructView()
  40. {
  41. }
  42.  
  43. void CMapDWordToMyStructView::OnInitialUpdate()
  44. {
  45.     CFormView::OnInitialUpdate();
  46.  
  47.     // Copy all of the assocations from the document's CMap to the listbox.
  48.     DWORD dwKey;
  49.     CMyStruct* pMyStruct;
  50.     m_ctlList.ResetContent();
  51.     CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct;
  52.     POSITION pos = map.GetStartPosition();
  53.     while (pos != NULL)
  54.     {
  55.         map.GetNextAssoc(pos, dwKey, pMyStruct);
  56.         AddMapEntryToListBox(dwKey, pMyStruct);
  57.     }
  58. }
  59.  
  60. void CMapDWordToMyStructView::DoDataExchange(CDataExchange* pDX)
  61. {
  62.     CFormView::DoDataExchange(pDX);
  63.     //{{AFX_DATA_MAP(CMapDWordToMyStructView)
  64.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  65.     DDX_Text(pDX, IDC_INT, m_int);
  66.     DDX_Text(pDX, IDC_FLOAT, m_float);
  67.     DDX_Text(pDX, IDC_STRING, m_str);
  68.     DDX_Text(pDX, IDC_KEY, m_dwKey);
  69.     //}}AFX_DATA_MAP
  70. }
  71.  
  72.  
  73. BEGIN_MESSAGE_MAP(CMapDWordToMyStructView, CFormView)
  74.     //{{AFX_MSG_MAP(CMapDWordToMyStructView)
  75.     ON_BN_CLICKED(IDC_ADD_OR_UPDATE, OnAddOrUpdate)
  76.     ON_BN_CLICKED(IDC_FIND, OnFind)
  77.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  78.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  79.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  80.     //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CMapDWordToMyStructView diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CMapDWordToMyStructView::AssertValid() const
  90. {
  91.     CFormView::AssertValid();
  92. }
  93.  
  94. void CMapDWordToMyStructView::Dump(CDumpContext& dc) const
  95. {
  96.     CFormView::Dump(dc);
  97. }
  98.  
  99. CCollectDoc* CMapDWordToMyStructView::GetDocument() // non-debug version is inline
  100. {
  101.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  102. }
  103. #endif //_DEBUG
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMapDWordToMyStructView internal implementation
  107.  
  108. CString CMapDWordToMyStructView::FormatListBoxEntry(DWORD dwKey, CMyStruct* pMyStruct)
  109. {
  110.     CString strListBoxEntry;
  111.     strListBoxEntry.Format(_T("%d"),dwKey);
  112.     strListBoxEntry += _T(" >> ");
  113.     CString strMyStruct;
  114.     pMyStruct->FormatMyStruct(strMyStruct);
  115.     strListBoxEntry += strMyStruct;
  116.     return strListBoxEntry;
  117. }
  118.  
  119. int CMapDWordToMyStructView::FindKeyInListBox(DWORD dwKey)
  120. {
  121.     for (int nSel = 0; nSel < m_ctlList.GetCount(); nSel++)
  122.     {
  123.         if (m_ctlList.GetItemData(nSel) == dwKey)
  124.             return nSel;
  125.     }
  126.     return LB_ERR;
  127. }
  128.  
  129. void CMapDWordToMyStructView::AddMapEntryToListBox(DWORD dwKey, CMyStruct* pMyStruct)
  130. {
  131.     CString strListBoxEntry = FormatListBoxEntry(dwKey, pMyStruct);
  132.     int nSel = m_ctlList.AddString(strListBoxEntry);
  133.     m_ctlList.SetItemData(nSel, dwKey);
  134. }
  135.  
  136. CMyStruct* CMapDWordToMyStructView::ConstructMyStructFromView()
  137. {
  138.     CMyStruct* pMyStruct = new CMyStruct;
  139.     pMyStruct->m_int = m_int;
  140.     pMyStruct->m_float = m_float;
  141.     pMyStruct->m_str = m_str;
  142.     return pMyStruct;
  143. }
  144.  
  145. void CMapDWordToMyStructView::UpdateViewFromMyStruct(CMyStruct* pMyStruct)
  146. {
  147.     m_int = pMyStruct->m_int;
  148.     m_float = pMyStruct->m_float;
  149.     m_str = pMyStruct->m_str;
  150.     UpdateData(FALSE);  // update the value found in the map lookup
  151. }
  152.  
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // CMapDWordToMyStructView message handlers
  156.  
  157. void CMapDWordToMyStructView::OnAddOrUpdate()
  158. {
  159.     if (UpdateData() != TRUE)
  160.         return;
  161.  
  162.     // Add or replace entry in the listbox
  163.     int nSel = FindKeyInListBox(m_dwKey);
  164.     if (nSel != LB_ERR)
  165.         m_ctlList.DeleteString(nSel);
  166.     CMyStruct* pMyStruct = ConstructMyStructFromView();
  167.     AddMapEntryToListBox(m_dwKey, pMyStruct);
  168.  
  169.     // Add or update association in the CMap
  170.     CMyStruct* pTemp = GetDocument()->m_mapDWordToMyStruct[m_dwKey];
  171.     delete pTemp; // delete old value
  172.     GetDocument()->m_mapDWordToMyStruct[m_dwKey] = pMyStruct;
  173. }
  174.  
  175. void CMapDWordToMyStructView::OnFind()
  176. {
  177.     if (UpdateData() != TRUE)
  178.         return;
  179.  
  180.     CMyStruct* pMyStruct;
  181.     if (GetDocument()->m_mapDWordToMyStruct.Lookup(m_dwKey, pMyStruct) != TRUE)
  182.     {
  183.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  184.         return;
  185.     }
  186.  
  187.     UpdateViewFromMyStruct(pMyStruct);
  188. }
  189.  
  190. void CMapDWordToMyStructView::OnRemove()
  191. {
  192.     if (UpdateData() != TRUE)
  193.         return;
  194.  
  195.     CMyStruct* pMyStruct;
  196.     CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct;
  197.     if (map.Lookup(m_dwKey, pMyStruct) != TRUE)
  198.     {
  199.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  200.         return;
  201.     }
  202.  
  203.     if (m_int != pMyStruct->m_int
  204.         || m_float != pMyStruct->m_float
  205.         || m_str != pMyStruct->m_str)
  206.     {
  207.         UpdateViewFromMyStruct(pMyStruct);
  208.         AfxMessageBox(IDS_KEY_NOT_FOUND_CHOOSE_REMOVE_AGAIN);
  209.         return;
  210.     }
  211.  
  212.     // Remove assocation from the listbox
  213.     int nSel = FindKeyInListBox(m_dwKey);
  214.     ASSERT(nSel != LB_ERR);
  215.     m_ctlList.DeleteString(nSel);
  216.  
  217.     // Remove the association from the CMap
  218.     map.RemoveKey(m_dwKey);
  219.  
  220.     // Delete CMyStruct
  221.     delete pMyStruct;
  222. }
  223.  
  224. void CMapDWordToMyStructView::OnRemoveAll()
  225. {
  226.     CCollectDoc* pDoc = GetDocument();
  227.     POSITION pos = GetDocument()->m_mapDWordToMyStruct.GetStartPosition();
  228.     while (pos != NULL)
  229.     {
  230.         DWORD dwKey;
  231.         CMyStruct* pMyStruct;
  232.         pDoc->m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct);
  233.         delete pMyStruct;
  234.     }
  235.     pDoc->m_mapDWordToMyStruct.RemoveAll();
  236.     m_ctlList.ResetContent();
  237. }
  238.  
  239. void CMapDWordToMyStructView::OnSelChangeList()
  240. {
  241.     // Update the "key" field to reflect the new selection in the listbox.
  242.     m_dwKey = m_ctlList.GetItemData(m_ctlList.GetCurSel());
  243.     UpdateData(FALSE);
  244.  
  245.     // Now that the key field has been updated to reflect the listbox selection,
  246.     // find the string in the map as though the user had hit the Find button.
  247.     OnFind();
  248. }
  249.