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

  1. // strlstvw.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 "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.     IStlStringListPtr& stringList = GetDocument()->m_stringList;
  48.     try {
  49.     stringList->First();
  50.     BSTR bstr;
  51.     while (stringList->Next(&bstr))
  52.     {
  53.         CString str = bstr;
  54.         ::SysFreeString(bstr);
  55.         m_ctlList.AddString(str);
  56.     }
  57.     } catch(_com_error& e) {
  58.         dump_com_error(e);
  59.     }
  60. }
  61.  
  62. void CStringListView::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CFormView::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CStringListView)
  66.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  67.     DDX_Text(pDX, IDC_ELEMENT, m_strElement);
  68.     //}}AFX_DATA_MAP
  69. }
  70.  
  71.  
  72. BEGIN_MESSAGE_MAP(CStringListView, CFormView)
  73.     //{{AFX_MSG_MAP(CStringListView)
  74.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  75.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  76.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  77.     ON_BN_CLICKED(IDC_UPDATE, OnUpdateElement)
  78.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  79.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  80.     //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CStringListView diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CStringListView::AssertValid() const
  90. {
  91.     CFormView::AssertValid();
  92. }
  93.  
  94. void CStringListView::Dump(CDumpContext& dc) const
  95. {
  96.     CFormView::Dump(dc);
  97. }
  98.  
  99. CCollectDoc* CStringListView::GetDocument() // non-debug version is inline
  100. {
  101.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  102. }
  103. #endif //_DEBUG
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CStringListView internal implementation
  107.  
  108. BOOL CStringListView::FindString(int& nSel)
  109. {
  110.     nSel = m_ctlList.GetCurSel();
  111.     if (nSel == LB_ERR)
  112.     {
  113.         AfxMessageBox(IDS_SELECT_STRING_FIRST);
  114.         return FALSE;
  115.     }
  116.  
  117.     // The currently selected string in the listbox is the string
  118.     // to be updated or removed .
  119.     CString strOld;
  120.     m_ctlList.GetText(nSel, strOld);
  121.  
  122.     try {
  123.     // Find the string to be updated or replaced in the CStringList.
  124.     return GetDocument()->m_stringList->Find((LPCTSTR) strOld);
  125.     } catch(_com_error& e) {
  126.         dump_com_error(e);
  127.     }
  128.     return FALSE;
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CStringListView message handlers
  133.  
  134. void CStringListView::OnAdd()
  135. {
  136.     if (UpdateData() != TRUE)
  137.         return;
  138.  
  139.     try {
  140.     // Add new string to the CStringList
  141.     GetDocument()->m_stringList->Add = (LPCTSTR) m_strElement;
  142.     } catch(_com_error& e) {
  143.         dump_com_error(e);
  144.     }
  145.  
  146.     // Add new string to the listbox.
  147.     m_ctlList.AddString(m_strElement);
  148. }
  149.  
  150. void CStringListView::OnInsertBefore()
  151. {
  152.     if (UpdateData() != TRUE)
  153.         return;
  154.  
  155.     int nSel;
  156.     // Find the string in both the CStringList and in the listbox.
  157.     if (!FindString(nSel))
  158.         return;
  159.  
  160.     try {
  161.     // Insert in front of the string found in the CStringList
  162.     GetDocument()->m_stringList->InsertBefore = (LPCTSTR) m_strElement;
  163.     } catch(_com_error& e) {
  164.         dump_com_error(e);
  165.     }
  166.  
  167.     // Insert new string in the listbox
  168.     m_ctlList.InsertString(nSel, m_strElement);
  169. }
  170.  
  171. void CStringListView::OnUpdateElement()
  172. {
  173.     if (UpdateData() != TRUE)
  174.         return;
  175.  
  176.     int nSel;
  177.     // Find the string in both the CStringList and in the listbox.
  178.     if (!FindString(nSel))
  179.         return;
  180.  
  181.     try {
  182.     // Replace the value of the string in the CStringList.
  183.     GetDocument()->m_stringList->SetAt = (LPCTSTR) m_strElement;
  184.     } catch(_com_error& e) {
  185.         dump_com_error(e);
  186.     }
  187.  
  188.     // Replace the value of the string in the listbox by
  189.     // removing the old string and adding the new string.
  190.     m_ctlList.DeleteString(nSel);
  191.     m_ctlList.InsertString(nSel, m_strElement);
  192. }
  193.  
  194. void CStringListView::OnRemove()
  195. {
  196.     int nSel;
  197.     // FInd the string in both the CStringList and in the listbox.
  198.     if (!FindString(nSel))
  199.         return;
  200.  
  201.     try {
  202.     // Remove the string from the CStringList.
  203.     GetDocument()->m_stringList->Remove;
  204.     } catch(_com_error& e) {
  205.         dump_com_error(e);
  206.     }
  207.  
  208.     // Remove the string from the listbox.
  209.     m_ctlList.DeleteString(nSel);
  210. }
  211.  
  212. void CStringListView::OnRemoveAll()
  213. {
  214.     try {
  215.     // Remove all of the strings from the CStringList.
  216.     GetDocument()->m_stringList->RemoveAll();
  217.     } catch(_com_error& e) {
  218.         dump_com_error(e);
  219.     }
  220.  
  221.     // Remove all of the strings from the listbox.
  222.     m_ctlList.ResetContent();
  223. }
  224.  
  225.  
  226. void CStringListView::OnSelChangeList()
  227. {
  228.     // Update the edit control to reflect the new selection
  229.     // in the listbox.
  230.     m_ctlList.GetText(m_ctlList.GetCurSel(), m_strElement);
  231.     UpdateData(FALSE);
  232. }
  233.