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

  1. // typlstvw.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 "typlstvw.h"
  17. #include "resource.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CTypedPtrListView
  26.  
  27. IMPLEMENT_DYNCREATE(CTypedPtrListView, CFormView)
  28.  
  29. CTypedPtrListView::CTypedPtrListView()
  30.     : CFormView(CTypedPtrListView::IDD)
  31. {
  32.     //{{AFX_DATA_INIT(CTypedPtrListView)
  33.     m_float = 0.0f;
  34.     m_int = 0;
  35.     m_str = "";
  36.     //}}AFX_DATA_INIT
  37. }
  38.  
  39. CTypedPtrListView::~CTypedPtrListView()
  40. {
  41. }
  42.  
  43. void CTypedPtrListView::OnInitialUpdate()
  44. {
  45.     CFormView::OnInitialUpdate();
  46.  
  47.     try {
  48.     // Copy all of the strings from the document's CTypedPtrList
  49.     // to the listbox.
  50.     m_ctlList.ResetContent();
  51.     IStlMyStructListPtr& mystructList = GetDocument()->m_mystructList;
  52.     mystructList->First();
  53.     IMyStruct* pMyStruct = NULL;
  54.     while (mystructList->Next(&pMyStruct))
  55.         AddMyStructToListBox((CMyStruct*) pMyStruct);
  56.     } catch(_com_error& e) {
  57.         dump_com_error(e);
  58.     }
  59. }
  60.  
  61. void CTypedPtrListView::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CFormView::DoDataExchange(pDX);
  64.     //{{AFX_DATA_MAP(CTypedPtrListView)
  65.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  66.     DDX_Text(pDX, IDC_FLOAT, m_float);
  67.     DDX_Text(pDX, IDC_INT, m_int);
  68.     DDX_Text(pDX, IDC_STRING, m_str);
  69.     //}}AFX_DATA_MAP
  70.     if (pDX->m_bSaveAndValidate && _tcschr(m_str, ' ') != NULL)
  71.     {
  72.         AfxMessageBox(IDS_BLANK_CHARS_NOT_ALLOWED);
  73.         pDX->Fail();
  74.     }
  75. }
  76.  
  77.  
  78. BEGIN_MESSAGE_MAP(CTypedPtrListView, CFormView)
  79.     //{{AFX_MSG_MAP(CTypedPtrListView)
  80.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  81.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  82.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  83.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  84.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  85.     ON_BN_CLICKED(IDC_INSERT_BEFORE, OnInsertBefore)
  86.     //}}AFX_MSG_MAP
  87. END_MESSAGE_MAP()
  88.  
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CTypedPtrListView diagnostics
  92.  
  93. #ifdef _DEBUG
  94. void CTypedPtrListView::AssertValid() const
  95. {
  96.     CFormView::AssertValid();
  97. }
  98.  
  99. void CTypedPtrListView::Dump(CDumpContext& dc) const
  100. {
  101.     CFormView::Dump(dc);
  102. }
  103.  
  104. CCollectDoc* CTypedPtrListView::GetDocument() // non-debug version is inline
  105. {
  106.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  107. }
  108.  
  109. #endif //_DEBUG
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CmystructListView internal implementation
  113.  
  114. CMyStruct* CTypedPtrListView::FindMyStruct(int& nSel)
  115. {
  116.     try {
  117.     nSel = m_ctlList.GetCurSel();
  118.     if (nSel == LB_ERR)
  119.     {
  120.         AfxMessageBox(IDS_SELECT_ENTRY);
  121.         return NULL;
  122.     }
  123.  
  124.     // The CMyStruct pointer was saved as the listbox entry's data item.
  125.     CMyStruct* pMyStruct = (CMyStruct*)m_ctlList.GetItemDataPtr(nSel);
  126.  
  127.     // Find the CMyStruct pointer in the CTypedPtrList.
  128.     return GetDocument()->m_mystructList->Find(pMyStruct) ? pMyStruct : NULL;
  129.     } catch(_com_error& e) {
  130.         dump_com_error(e);
  131.     }
  132.     return NULL;
  133. }
  134.  
  135. void CTypedPtrListView::AddMyStructToListBox(CMyStruct* pMyStruct, int nSel)
  136. {
  137.     try {
  138.     // Add new CMyStruct to the listbox.
  139.     CString str;
  140.     pMyStruct->Format(str);
  141.     if (nSel == -1)
  142.         nSel = m_ctlList.AddString(str);
  143.     else
  144.         m_ctlList.InsertString(nSel, str);
  145.     } catch(_com_error& e) {
  146.         dump_com_error(e);
  147.     }
  148.  
  149.     // Save the CMyStruct pointer as the listbox entry's "data item".
  150.     m_ctlList.SetItemDataPtr(nSel, pMyStruct);
  151. }
  152.  
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // CTypedPtrListView message handlers
  156.  
  157. void CTypedPtrListView::OnAdd()
  158. {
  159.     if (UpdateData() != TRUE)
  160.         return;
  161.  
  162.     try {
  163.     // Add new CMyStruct to the CTypedPtrList
  164.     CMyStruct* pMyStruct = new CMyStruct;
  165.     pMyStruct->m_int = m_int;
  166.     pMyStruct->m_float = m_float;
  167.     pMyStruct->m_str = m_str.AllocSysString();
  168.     GetDocument()->m_mystructList->Add = pMyStruct;
  169.  
  170.     AddMyStructToListBox(pMyStruct);
  171.     } catch(_com_error& e) {
  172.         dump_com_error(e);
  173.     }
  174. }
  175.  
  176. void CTypedPtrListView::OnInsertBefore()
  177. {
  178.     if (UpdateData() != TRUE)
  179.         return;
  180.  
  181.     int nSel;
  182.     // Find the CMyStruct in the CTypedPtrList and listbox.
  183.     if (!FindMyStruct(nSel))
  184.         return;
  185.  
  186.     try {
  187.     // Insert new CMyStruct in the CTypedPtrList
  188.     CMyStruct* pMyStruct = new CMyStruct;
  189.     pMyStruct->m_int = m_int;
  190.     pMyStruct->m_float = m_float;
  191.     pMyStruct->m_str = m_str.AllocSysString();
  192.     GetDocument()->m_mystructList->InsertBefore = pMyStruct;
  193.  
  194.     AddMyStructToListBox(pMyStruct, nSel);
  195.     } catch(_com_error& e) {
  196.         dump_com_error(e);
  197.     }
  198. }
  199.  
  200. void CTypedPtrListView::OnUpdate()
  201. {
  202.     if (UpdateData() != TRUE)
  203.         return;
  204.  
  205.     try {
  206.     int nSel;
  207.     // Find the CMyStruct in the CTypedPtrList and listbox.
  208.     CMyStruct* pMyStruct = FindMyStruct(nSel);
  209.     if (pMyStruct == NULL)
  210.         return;
  211.  
  212.     // Replace the value of the CMyStruct in the CTypedPtrList.
  213.     pMyStruct->m_int = m_int;
  214.     pMyStruct->m_float = m_float;
  215.     pMyStruct->m_str = m_str.AllocSysString();
  216.  
  217.     // Replace the displayed CMyStruct in the listbox by removing
  218.     // the old listbox entry and adding a new entry.
  219.     AddMyStructToListBox(pMyStruct, nSel);
  220.     m_ctlList.DeleteString(nSel);
  221.     } catch(_com_error& e) {
  222.         dump_com_error(e);
  223.     }
  224. }
  225.  
  226. void CTypedPtrListView::OnRemove()
  227. {
  228.     try {
  229.     int nSel;
  230.     // Find the CMyStruct in the CTypedPtrList and listbox.
  231.     CMyStruct* pMyStruct = FindMyStruct(nSel);
  232.     if (pMyStruct == NULL)
  233.         return;
  234.  
  235.     // Remove the CMyStruct ptr from the CTypedPtrList.
  236.     GetDocument()->m_mystructList->Remove;
  237.  
  238.     // Delete the CMyStruct object. (The CTypePtrList only holds the ptr.)
  239.     delete (CMyStruct*) pMyStruct;
  240.  
  241.     // Remove the corresponding entry from the listbox.
  242.     m_ctlList.DeleteString(nSel);
  243.     } catch(_com_error& e) {
  244.         dump_com_error(e);
  245.     }
  246. }
  247.  
  248. void CTypedPtrListView::OnRemoveAll()
  249. {
  250.     try {
  251.     IStlMyStructListPtr& mystructList = GetDocument()->m_mystructList;
  252.  
  253.     // Delete all of the CMyStruct objects pointed to
  254.     // by the CTypedPtrList. Then remove all of the
  255.     // CMyStruct pointers from the CTypedPtrList, which
  256.     // is faster than removing each individually.
  257.     mystructList->First();
  258.     IMyStruct* pstr = NULL;
  259.     while (mystructList->Next(&pstr))
  260.         delete (CMyStruct*) pstr;
  261.     mystructList->RemoveAll();
  262.  
  263.     // Remove all of the corresponding formatted strings from the listbox.
  264.     m_ctlList.ResetContent();
  265.     } catch(_com_error& e) {
  266.         dump_com_error(e);
  267.     }
  268. }
  269.  
  270. void CTypedPtrListView::OnSelChangeList()
  271. {
  272.     // Update the edit control to reflect the new selection
  273.     // in the listbox.
  274.  
  275.     int nSel;
  276.     // Find the CMyStruct in the CTypedPtrList and listbox.
  277.     CMyStruct* pMyStruct = FindMyStruct(nSel);
  278.  
  279.     m_int = pMyStruct->m_int;
  280.     m_float = pMyStruct->m_float;
  281.     m_str = pMyStruct->m_str;
  282.  
  283.     UpdateData(FALSE);
  284. }
  285.