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 / mapssvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  5.8 KB  |  236 lines

  1. // mapssvw.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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.     BSTR bstrKey, bstrValue;
  48.     m_ctlList.ResetContent();
  49.     IStlMapStringToStringPtr& map = GetDocument()->m_mapStringToString;
  50.     try {
  51.     map->First();
  52.     while (map->Next(&bstrKey, &bstrValue))
  53.     {
  54.         CString strKey(bstrKey), strValue(bstrValue);
  55.         AddMapEntryToListBox(strKey, strValue);
  56.     }
  57.     } catch(_com_error& e) {
  58.         dump_com_error(e);
  59.     }
  60. }
  61.  
  62. void CMapStringToStringView::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CFormView::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CMapStringToStringView)
  66.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  67.     DDX_Text(pDX, IDC_KEY, m_strKey);
  68.     DDX_Text(pDX, IDC_VALUE, m_strValue);
  69.     //}}AFX_DATA_MAP
  70. }
  71.  
  72.  
  73. BEGIN_MESSAGE_MAP(CMapStringToStringView, CFormView)
  74.     //{{AFX_MSG_MAP(CMapStringToStringView)
  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. // CMapStringToStringView diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CMapStringToStringView::AssertValid() const
  90. {
  91.     CFormView::AssertValid();
  92. }
  93.  
  94. void CMapStringToStringView::Dump(CDumpContext& dc) const
  95. {
  96.     CFormView::Dump(dc);
  97. }
  98.  
  99. CCollectDoc* CMapStringToStringView::GetDocument() // non-debug version is inline
  100. {
  101.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  102. }
  103. #endif //_DEBUG
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMapStringToStringView internal implementation
  107.  
  108. int CMapStringToStringView::FindKeyInListBox(CString& strKey)
  109. {
  110.     CString strValue;
  111.     BSTR bstrValue;
  112.     try {
  113.     if (!GetDocument()->m_mapStringToString->Lookup((LPCTSTR) strKey, &bstrValue))
  114.         return LB_ERR;
  115.     } catch(_com_error& e) {
  116.         dump_com_error(e);
  117.     }
  118.     CString strListBoxEntry = strKey;
  119.     strListBoxEntry += _T(" >> ");
  120.     strListBoxEntry += strValue = bstrValue;
  121.     return m_ctlList.FindString(-1, strListBoxEntry);
  122. }
  123.  
  124. void CMapStringToStringView::AddMapEntryToListBox(CString& strKey, CString& strValue)
  125. {
  126.     CString str = strKey;
  127.     str += _T(" >> ");
  128.     str += strValue;
  129.     m_ctlList.AddString(str);
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CMapStringToStringView message handlers
  134.  
  135. void CMapStringToStringView::OnAddOrUpdate()
  136. {
  137.     if (UpdateData() != TRUE)
  138.         return;
  139.  
  140.     try {
  141.     // Add or replace entry in the listbox
  142.     int nSel = FindKeyInListBox(m_strKey);
  143.     if (nSel != LB_ERR)
  144.         m_ctlList.DeleteString(nSel);
  145.     AddMapEntryToListBox(m_strKey, m_strValue);
  146.  
  147.     // Add or update association in the CMapStringToString
  148.     GetDocument()->m_mapStringToString->Map[(LPCTSTR) m_strKey] = (LPCTSTR) m_strValue;
  149.     } catch(_com_error& e) {
  150.         dump_com_error(e);
  151.     }
  152. }
  153.  
  154. void CMapStringToStringView::OnFind()
  155. {
  156.     if (UpdateData() != TRUE)
  157.         return;
  158.  
  159.     try {
  160.     BSTR bstrValue;
  161.     if (!GetDocument()->m_mapStringToString->Lookup((LPCTSTR) m_strKey, &bstrValue))
  162.     {
  163.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  164.         return;
  165.     }
  166.  
  167.     m_strValue = bstrValue;
  168.     UpdateData(FALSE);  // update the value found in the map lookup
  169.     } catch(_com_error& e) {
  170.         dump_com_error(e);
  171.     }
  172. }
  173.  
  174. void CMapStringToStringView::OnRemove()
  175. {
  176.     if (UpdateData() != TRUE)
  177.         return;
  178.  
  179.     try {
  180.     BSTR bstrValue;
  181.     IStlMapStringToStringPtr& map = GetDocument()->m_mapStringToString;
  182.     if (!map->Lookup((LPCTSTR) m_strKey, &bstrValue))
  183.     {
  184.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  185.         return;
  186.     }
  187.  
  188.     if (bstrValue != m_strValue)
  189.     {
  190.         m_strValue = bstrValue;
  191.         UpdateData(FALSE);
  192.         AfxMessageBox(IDS_KEY_NOT_FOUND_CHOOSE_REMOVE_AGAIN);
  193.         return;
  194.     }
  195.  
  196.     // Remove assocation from the listbox
  197.     int nSel = FindKeyInListBox(m_strKey);
  198.     ASSERT(nSel != LB_ERR);
  199.     m_ctlList.DeleteString(nSel);
  200.  
  201.     // Remove the association from the CMapStringToString
  202.     map->Remove;
  203.     } catch(_com_error& e) {
  204.         dump_com_error(e);
  205.     }
  206. }
  207.  
  208. void CMapStringToStringView::OnRemoveAll()
  209. {
  210.     try {
  211.     GetDocument()->m_mapStringToString->RemoveAll();
  212.     } catch(_com_error& e) {
  213.         dump_com_error(e);
  214.     }
  215.     m_ctlList.ResetContent();
  216. }
  217.  
  218. void CMapStringToStringView::OnSelChangeList()
  219. {
  220.     // Update the "key" field to reflect the new selection in the listbox.
  221.     CString str;
  222.     m_ctlList.GetText(m_ctlList.GetCurSel(), str);
  223.     m_strKey.Empty();
  224.     LPCTSTR lpsz = str;
  225.     while (*lpsz != ' ' || *(lpsz+1) != '>' || *(lpsz+2) != '>')
  226.     {
  227.         m_strKey += *lpsz;
  228.         lpsz++;
  229.     }
  230.     UpdateData(FALSE);
  231.  
  232.     // Now that the key field has been updated to reflect the listbox selection,
  233.     // find the string in the map as though the user had hit the Find button.
  234.     OnFind();
  235. }
  236.