home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / collect / mapdwvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.5 KB  |  250 lines

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