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

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