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

  1. // typtrmap.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 "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.     try {
  48.     // Copy all of the assocations from the document's CTypedPtrMap
  49.     // to the listbox.
  50.     BSTR bstrKey;
  51.     IMyStruct* pMyStruct = NULL;
  52.     m_ctlList.ResetContent();
  53.     IStlMapStringToMyObjectPtr& map = GetDocument()->m_mapStringToMyObject;
  54.     map->First();
  55.     while (map->Next(&bstrKey, &pMyStruct))
  56.     {
  57.         CString strKey(bstrKey);
  58.         ::SysFreeString(bstrKey);
  59.         CMyObject* pMyObject = (CMyObject*) pMyStruct;
  60.         AddMapEntryToListBox(strKey, pMyObject);
  61.     }
  62.     } catch(_com_error& e) {
  63.         dump_com_error(e);
  64.     }
  65. }
  66.  
  67. void CTypedPtrMapView::DoDataExchange(CDataExchange* pDX)
  68. {
  69.     CFormView::DoDataExchange(pDX);
  70.     //{{AFX_DATA_MAP(CTypedPtrMapView)
  71.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  72.     DDX_Text(pDX, IDC_KEY, m_strKey);
  73.     DDX_Text(pDX, IDC_INT, m_int);
  74.     DDX_Text(pDX, IDC_FLOAT, m_float);
  75.     DDX_Text(pDX, IDC_STRING, m_str);
  76.     //}}AFX_DATA_MAP
  77. }
  78.  
  79.  
  80. BEGIN_MESSAGE_MAP(CTypedPtrMapView, CFormView)
  81.     //{{AFX_MSG_MAP(CTypedPtrMapView)
  82.     ON_BN_CLICKED(IDC_ADD_OR_UPDATE, OnAddOrUpdate)
  83.     ON_BN_CLICKED(IDC_FIND, OnFind)
  84.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  85.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  86.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  87.     //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89.  
  90.  
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CTypedPtrMapView diagnostics
  94.  
  95. #ifdef _DEBUG
  96. void CTypedPtrMapView::AssertValid() const
  97. {
  98.     CFormView::AssertValid();
  99. }
  100.  
  101. void CTypedPtrMapView::Dump(CDumpContext& dc) const
  102. {
  103.     CFormView::Dump(dc);
  104. }
  105.  
  106. CCollectDoc* CTypedPtrMapView::GetDocument() // non-debug version is inline
  107. {
  108.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  109. }
  110. #endif //_DEBUG
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CTypedPtrMapView internal implementation
  114.  
  115. CString CTypedPtrMapView::FormatListBoxEntry(CString& strKey, CMyObject* pMyObject)
  116. {
  117.     CString strListBoxEntry = strKey;
  118.     strListBoxEntry += _T(" >> ");
  119.     CString strMyObject;
  120.     pMyObject->Format(strMyObject);
  121.     strListBoxEntry += strMyObject;
  122.     return strListBoxEntry;
  123. }
  124.  
  125. int CTypedPtrMapView::FindKeyInListBox(CString& strKey)
  126. {
  127.     try {
  128.     IMyStruct* pMyStruct = NULL;
  129.     if (!GetDocument()->m_mapStringToMyObject->Lookup((LPCTSTR) strKey, &pMyStruct))
  130.         return LB_ERR;
  131.     CMyObject* pMyObject = (CMyObject*) pMyStruct;
  132.     CString strListBoxEntry = FormatListBoxEntry(strKey, pMyObject);
  133.  
  134.     return m_ctlList.FindString(-1, strListBoxEntry);
  135.     } catch(_com_error& e) {
  136.         dump_com_error(e);
  137.     }
  138.     return LB_ERR;
  139. }
  140.  
  141. void CTypedPtrMapView::AddMapEntryToListBox(CString& strKey, CMyObject* pMyObject)
  142. {
  143.     CString strListBoxEntry = FormatListBoxEntry(strKey, pMyObject);
  144.     m_ctlList.AddString(strListBoxEntry);
  145. }
  146.  
  147. CMyObject* CTypedPtrMapView::ConstructMyObjectFromView()
  148. {
  149.     CMyObject* pMyObject = new CMyObject;
  150.     pMyObject->m_int = m_int;
  151.     pMyObject->m_float = m_float;
  152.     pMyObject->m_str = m_str.AllocSysString();
  153.     return pMyObject;
  154. }
  155.  
  156. void CTypedPtrMapView::UpdateViewFromMyObject(CMyObject* pMyObject)
  157. {
  158.     m_int = pMyObject->m_int;
  159.     m_float = pMyObject->m_float;
  160.     m_str = pMyObject->m_str;
  161.     UpdateData(FALSE);  // update the value found in the map lookup
  162. }
  163.  
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CTypedPtrMapView message handlers
  167.  
  168. void CTypedPtrMapView::OnAddOrUpdate()
  169. {
  170.     if (UpdateData() != TRUE)
  171.         return;
  172.  
  173.     try {
  174.     // Add or replace entry in the listbox
  175.     int nSel = FindKeyInListBox(m_strKey);
  176.     if (nSel != LB_ERR)
  177.         m_ctlList.DeleteString(nSel);
  178.     CMyObject* pMyObject = ConstructMyObjectFromView();
  179.     AddMapEntryToListBox(m_strKey, pMyObject);
  180.  
  181.     // Add or update association in the CTypedPtrMap
  182.     delete (CMyObject*) GetDocument()->m_mapStringToMyObject->Map[(LPCTSTR) m_strKey];
  183.     GetDocument()->m_mapStringToMyObject->Map[(LPCTSTR) m_strKey] = pMyObject;
  184.     } catch(_com_error& e) {
  185.         dump_com_error(e);
  186.     }
  187. }
  188.  
  189. void CTypedPtrMapView::OnFind()
  190. {
  191.     if (UpdateData() != TRUE)
  192.         return;
  193.  
  194.     try {
  195.     IMyStruct* pMyStruct = NULL;
  196.     if (!GetDocument()->m_mapStringToMyObject->Lookup((LPCTSTR) m_strKey, &pMyStruct))
  197.     {
  198.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  199.         return;
  200.     }
  201.  
  202.     CMyObject* pMyObject = (CMyObject*) pMyStruct;
  203.     UpdateViewFromMyObject(pMyObject);
  204.     } catch(_com_error& e) {
  205.         dump_com_error(e);
  206.     }
  207. }
  208.  
  209. void CTypedPtrMapView::OnRemove()
  210. {
  211.     if (UpdateData() != TRUE)
  212.         return;
  213.  
  214.     try {
  215.     IMyStruct* pMyStruct = NULL;
  216.     IStlMapStringToMyObjectPtr& map = GetDocument()->m_mapStringToMyObject;
  217.     if (!map->Lookup((LPCTSTR) m_strKey, &pMyStruct))
  218.     {
  219.         AfxMessageBox(IDS_KEY_NOT_FOUND);
  220.         return;
  221.     }
  222.  
  223.     CMyObject* pMyObject = (CMyObject*) pMyStruct;
  224.     if (m_int != pMyObject->m_int
  225.         || m_float != pMyObject->m_float
  226.         || m_str != pMyObject->m_str)
  227.     {
  228.         UpdateViewFromMyObject(pMyObject);
  229.         AfxMessageBox(IDS_KEY_NOT_FOUND_CHOOSE_REMOVE_AGAIN);
  230.         return;
  231.     }
  232.  
  233.     // Remove assocation from the listbox
  234.     int nSel = FindKeyInListBox(m_strKey);
  235.     ASSERT(nSel != LB_ERR);
  236.     m_ctlList.DeleteString(nSel);
  237.  
  238.     // Remove the association from the CTypedPtrMap
  239.     map->Remove;
  240.  
  241.     // Delete the CMyObject
  242.     delete pMyObject;
  243.     } catch(_com_error& e) {
  244.         dump_com_error(e);
  245.     }
  246. }
  247.  
  248. void CTypedPtrMapView::OnRemoveAll()
  249. {
  250.     try {
  251.     CCollectDoc* pDoc = GetDocument();
  252.     BSTR bstr;
  253.     IMyStruct* pMyStruct = NULL;
  254.     pDoc->m_mapStringToMyObject->First();
  255.     while (pDoc->m_mapStringToMyObject->Next(&bstr, &pMyStruct))
  256.     {
  257.         ::SysFreeString(bstr);
  258.         delete (CMyObject*) pMyStruct;
  259.     }
  260.     pDoc->m_mapStringToMyObject->RemoveAll();
  261.  
  262.     m_ctlList.ResetContent();
  263.     } catch(_com_error& e) {
  264.         dump_com_error(e);
  265.     }
  266. }
  267.  
  268. void CTypedPtrMapView::OnSelChangeList()
  269. {
  270.     // Update the "key" field to reflect the new selection in the listbox.
  271.     CString str;
  272.     m_ctlList.GetText(m_ctlList.GetCurSel(), str);
  273.     m_strKey.Empty();
  274.     LPCTSTR lpsz = str;
  275.     while (*lpsz != ' ' || *(lpsz+1) != '>' || *(lpsz+2) != '>')
  276.     {
  277.         m_strKey += *lpsz;
  278.         lpsz++;
  279.     }
  280.     UpdateData(FALSE);
  281.  
  282.     // Now that the key field has been updated to reflect the listbox selection,
  283.     // find the string in the map as though the user had hit the Find button.
  284.     OnFind();
  285. }
  286.