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

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