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

  1. // strlstvw.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 "strlstvw.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CStringListView
  25.  
  26. IMPLEMENT_DYNCREATE(CStringListView, CFormView)
  27.  
  28. CStringListView::CStringListView()
  29.     : CFormView(CStringListView::IDD)
  30. {
  31.     //{{AFX_DATA_INIT(CStringListView)
  32.     m_strElement = "";
  33.     //}}AFX_DATA_INIT
  34. }
  35.  
  36. CStringListView::~CStringListView()
  37. {
  38. }
  39.  
  40. void CStringListView::OnInitialUpdate()
  41. {
  42.     CFormView::OnInitialUpdate();
  43.  
  44.     // Copy all of the strings from the document's CStringList
  45.     // to the listbox.
  46.     m_ctlList.ResetContent();
  47.     CStringList& stringList = GetDocument()->m_stringList;
  48.     POSITION pos = stringList.GetHeadPosition();
  49.     while (pos != NULL)
  50.     {
  51.         CString str = stringList.GetNext(pos);
  52.         m_ctlList.AddString(str);
  53.     }
  54. }
  55.  
  56. void CStringListView::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CFormView::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CStringListView)
  60.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  61.     DDX_Text(pDX, IDC_ELEMENT, m_strElement);
  62.     //}}AFX_DATA_MAP
  63. }
  64.  
  65.  
  66. BEGIN_MESSAGE_MAP(CStringListView, CFormView)
  67.     //{{AFX_MSG_MAP(CStringListView)
  68.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  69.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  70.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  71.     ON_BN_CLICKED(IDC_UPDATE, OnUpdateElement)
  72.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  73.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  74.     //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76.  
  77.  
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CStringListView diagnostics
  81.  
  82. #ifdef _DEBUG
  83. void CStringListView::AssertValid() const
  84. {
  85.     CFormView::AssertValid();
  86. }
  87.  
  88. void CStringListView::Dump(CDumpContext& dc) const
  89. {
  90.     CFormView::Dump(dc);
  91. }
  92.  
  93. CCollectDoc* CStringListView::GetDocument() // non-debug version is inline
  94. {
  95.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  96. }
  97. #endif //_DEBUG
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CStringListView internal implementation
  101.  
  102. BOOL CStringListView::FindString(int& nSel, POSITION& pos)
  103. {
  104.     nSel = m_ctlList.GetCurSel();
  105.     if (nSel == LB_ERR)
  106.     {
  107.         AfxMessageBox(IDS_SELECT_STRING_FIRST);
  108.         return FALSE;
  109.     }
  110.  
  111.     // The currently selected string in the listbox is the string
  112.     // to be updated or removed .
  113.     CString strOld;
  114.     m_ctlList.GetText(nSel, strOld);
  115.  
  116.     // Find the string to be updated or replaced in the CStringList.
  117.     CStringList& stringList = GetDocument()->m_stringList;
  118.     pos = stringList.Find(strOld);
  119.  
  120.     // If the string is in the listbox, it should also be in the
  121.     // CStringList.
  122.     ASSERT(pos != NULL);
  123.  
  124.     return TRUE;
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CStringListView message handlers
  129.  
  130. void CStringListView::OnAdd()
  131. {
  132.     if (UpdateData() != TRUE)
  133.         return;
  134.  
  135.     // Add new string to the CStringList
  136.     GetDocument()->m_stringList.AddTail(m_strElement);
  137.  
  138.     // Add new string to the listbox.
  139.     m_ctlList.AddString(m_strElement);
  140. }
  141.  
  142. void CStringListView::OnInsertBefore()
  143. {
  144.     if (UpdateData() != TRUE)
  145.         return;
  146.  
  147.     int nSel;
  148.     POSITION pos;
  149.     // Find the string in both the CStringList and in the listbox.
  150.     if (FindString(nSel, pos) != TRUE)
  151.         return;
  152.  
  153.     // Insert in front of the string found in the CStringList
  154.     GetDocument()->m_stringList.InsertBefore(pos, m_strElement);
  155.  
  156.     // Insert new string in the listbox
  157.     m_ctlList.InsertString(nSel, m_strElement);
  158. }
  159.  
  160. void CStringListView::OnUpdateElement()
  161. {
  162.     if (UpdateData() != TRUE)
  163.         return;
  164.  
  165.     int nSel;
  166.     POSITION pos;
  167.     // Find the string in both the CStringList and in the listbox.
  168.     if (FindString(nSel, pos) != TRUE)
  169.         return;
  170.  
  171.     // Replace the value of the string in the CStringList.
  172.     GetDocument()->m_stringList.SetAt(pos, m_strElement);
  173.  
  174.     // Replace the value of the string in the listbox by
  175.     // removing the old string and adding the new string.
  176.     m_ctlList.DeleteString(nSel);
  177.     m_ctlList.InsertString(nSel, m_strElement);
  178. }
  179.  
  180. void CStringListView::OnRemove()
  181. {
  182.     int nSel;
  183.     POSITION pos;
  184.     // FInd the string in both the CStringList and in the listbox.
  185.     if (FindString(nSel, pos) != TRUE)
  186.         return;
  187.  
  188.     // Remove the string from the CStringList.
  189.     GetDocument()->m_stringList.RemoveAt(pos);
  190.  
  191.     // Remove the string from the listbox.
  192.     m_ctlList.DeleteString(nSel);
  193. }
  194.  
  195. void CStringListView::OnRemoveAll()
  196. {
  197.     // Remove all of the strings from the CStringList.
  198.     GetDocument()->m_stringList.RemoveAll();
  199.  
  200.     // Remove all of the strings from the listbox.
  201.     m_ctlList.ResetContent();
  202. }
  203.  
  204.  
  205. void CStringListView::OnSelChangeList()
  206. {
  207.     // Update the edit control to reflect the new selection
  208.     // in the listbox.
  209.     m_ctlList.GetText(m_ctlList.GetCurSel(), m_strElement);
  210.     UpdateData(FALSE);
  211. }
  212.