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

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