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

  1. // ptarryvw.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 "ptarryvw.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. // CPointArrayView
  26.  
  27. IMPLEMENT_DYNCREATE(CPointArrayView, CFormView)
  28.  
  29. CPointArrayView::CPointArrayView()
  30.     : CFormView(CPointArrayView::IDD)
  31. {
  32.     //{{AFX_DATA_INIT(CPointArrayView)
  33.     m_x = 0;
  34.     m_y = 0;
  35.     //}}AFX_DATA_INIT
  36. }
  37.  
  38. CPointArrayView::~CPointArrayView()
  39. {
  40. }
  41.  
  42. void CPointArrayView::OnInitialUpdate()
  43. {
  44.     CFormView::OnInitialUpdate();
  45.  
  46.     // Copy all of the points from the document's CArray of CPoints
  47.     // to the listbox.
  48.     m_ctlList.ResetContent();
  49.     IStlPointArrayPtr& ptArray = GetDocument()->m_ptArray;
  50.     try {
  51.     for (int nIndex = 0; nIndex < (int) ptArray->Count; nIndex++)
  52.     {
  53.         CPoint pt = ptArray->Array[nIndex];
  54.         AddPointToListBox(pt);
  55.     }
  56.     } catch(_com_error& e) {
  57.         dump_com_error(e);
  58.     }
  59. }
  60.  
  61.  
  62. void CPointArrayView::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CFormView::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CPointArrayView)
  66.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  67.     DDX_Text(pDX, IDC_X, m_x);
  68.     DDX_Text(pDX, IDC_Y, m_y);
  69.     //}}AFX_DATA_MAP
  70. }
  71.  
  72.  
  73. BEGIN_MESSAGE_MAP(CPointArrayView, CFormView)
  74.     //{{AFX_MSG_MAP(CPointArrayView)
  75.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  76.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  77.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  78.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  79.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  80.     //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CPointArrayView diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CPointArrayView::AssertValid() const
  90. {
  91.     CFormView::AssertValid();
  92. }
  93.  
  94. void CPointArrayView::Dump(CDumpContext& dc) const
  95. {
  96.     CFormView::Dump(dc);
  97. }
  98.  
  99. CCollectDoc* CPointArrayView::GetDocument() // non-debug version is inline
  100. {
  101.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  102. }
  103. #endif //_DEBUG
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CPointArrayView internal implementation
  108.  
  109. void CPointArrayView::AddPointToListBox(CPoint pt, int nSel)
  110. {
  111.     // Add new point to the listbox.
  112.     CString str;
  113.     str.Format(_T("(%i,%i)"), pt.x, pt.y);
  114.     if (nSel == -1)
  115.         nSel = m_ctlList.AddString(str);
  116.     else
  117.         m_ctlList.InsertString(nSel, str);
  118. }
  119.  
  120.  
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CPointArrayView message handlers
  123.  
  124. void CPointArrayView::OnAdd()
  125. {
  126.     if (UpdateData() != TRUE)
  127.         return;
  128.  
  129.     // Add new point to the CList<int,int>
  130.     CPoint pt(m_x, m_y);
  131.     try {
  132.     GetDocument()->m_ptArray->Add = pt;
  133.     } catch(_com_error& e) {
  134.         dump_com_error(e);
  135.     }
  136.  
  137.     AddPointToListBox(pt);
  138. }
  139.  
  140. void CPointArrayView::OnUpdate()
  141. {
  142.     if (UpdateData() != TRUE)
  143.         return;
  144.  
  145.     int nSel = m_ctlList.GetCurSel();
  146.     if (nSel == LB_ERR)
  147.     {
  148.         AfxMessageBox(IDS_SELECT_POINT_TO_BE_UPDATED);
  149.         return;
  150.     }
  151.  
  152.     // Replace the point in the CArray of CPoints.
  153.     CPoint pt(m_x, m_y);
  154.     try {
  155.     GetDocument()->m_ptArray->Array[nSel] = pt;
  156.     } catch(_com_error& e) {
  157.         dump_com_error(e);
  158.     }
  159.  
  160.     // Update the old point in the listbox by removing
  161.     // the old entry and adding a new entry.
  162.     m_ctlList.DeleteString(nSel);
  163.     AddPointToListBox(pt, nSel);
  164. }
  165.  
  166. void CPointArrayView::OnRemove()
  167. {
  168.     if (UpdateData() != TRUE)
  169.         return;
  170.  
  171.     int nSel = m_ctlList.GetCurSel();
  172.     if (nSel == LB_ERR)
  173.     {
  174.         AfxMessageBox(IDS_SELECT_POINT_TO_BE_REMOVED);
  175.         return;
  176.     }
  177.  
  178.     try {
  179.     // Remove the point from the CArray of CPoints..
  180.     GetDocument()->m_ptArray->Remove[nSel];
  181.     } catch(_com_error& e) {
  182.         dump_com_error(e);
  183.     }
  184.  
  185.     // Remove the point from the listbox.
  186.     m_ctlList.DeleteString(nSel);
  187. }
  188.  
  189. void CPointArrayView::OnRemoveAll()
  190. {
  191.     try {
  192.     // Remove all of the points from the CArray of CPoints.
  193.     GetDocument()->m_ptArray->RemoveAll();
  194.     } catch(_com_error& e) {
  195.         dump_com_error(e);
  196.     }
  197.  
  198.     // Remove all of the points from the listbox.
  199.     m_ctlList.ResetContent();
  200. }
  201.  
  202. void CPointArrayView::OnSelChangeList()
  203. {
  204.     try {
  205.     // Update the edit control to reflect the new selection
  206.     // in the listbox.
  207.     CPoint pt = GetDocument()->m_ptArray->Array[m_ctlList.GetCurSel()];
  208.  
  209.     m_x = pt.x;
  210.     m_y = pt.y;
  211.     UpdateData(FALSE);
  212.     } catch(_com_error& e) {
  213.         dump_com_error(e);
  214.     }
  215. }
  216.