home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch14 / phn / phnview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-21  |  7.1 KB  |  323 lines

  1. // phnview.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "phn.h"
  6.  
  7. //////////////////////
  8. // MY CODE STARTS HERE
  9. //////////////////////
  10.  
  11. #include "phone.h"
  12. #include "phndoc.h"
  13.  
  14. ////////////////////
  15. // MY CODE ENDS HERE
  16. ////////////////////
  17.  
  18.  
  19.  
  20. #include "phnview.h"
  21.  
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CPhnView
  29.  
  30. IMPLEMENT_DYNCREATE(CPhnView, CFormView)
  31.  
  32. CPhnView::CPhnView()
  33.     : CFormView(CPhnView::IDD)
  34. {
  35.     //{{AFX_DATA_INIT(CPhnView)
  36.     m_Name = "";
  37.     m_Phone = "";
  38.     //}}AFX_DATA_INIT
  39. }
  40.  
  41. CPhnView::~CPhnView()
  42. {
  43. }
  44.  
  45. void CPhnView::DoDataExchange(CDataExchange* pDX)
  46. {
  47.     CFormView::DoDataExchange(pDX);
  48.     //{{AFX_DATA_MAP(CPhnView)
  49.     DDX_Text(pDX, IDC_NAME, m_Name);
  50.     DDX_Text(pDX, IDC_PHONE, m_Phone);
  51.     //}}AFX_DATA_MAP
  52. }
  53.  
  54.  
  55. BEGIN_MESSAGE_MAP(CPhnView, CFormView)
  56.     //{{AFX_MSG_MAP(CPhnView)
  57.     ON_EN_CHANGE(IDC_NAME, OnChangeName)
  58.     ON_EN_CHANGE(IDC_PHONE, OnChangePhone)
  59.     ON_BN_CLICKED(IDC_PREVIOUS_BUTTON, OnPreviousButton)
  60.     ON_BN_CLICKED(IDC_NEXT_BUTTON, OnNextButton)
  61.     ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
  62.     ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton)
  63.     //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65.  
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CPhnView message handlers
  69.  
  70.  
  71. //////////////////////
  72. // MY CODE STARTS HERE
  73. //////////////////////
  74.  
  75. void CPhnView::OnInitialUpdate()
  76. {
  77.  
  78.   // Get a pointer to the document.
  79.   CPhnDoc* pDoc = (CPhnDoc*) GetDocument();
  80.  
  81.   // Get the address of m_PhoneList of the document class.
  82.   m_pList = &(pDoc->m_PhoneList);
  83.  
  84.   // Get head position.
  85.   m_position = m_pList->GetHeadPosition();
  86.  
  87.  
  88.   // Update m_Name and m_Phone with values from the list.
  89.   CPhone* pPhone = (CPhone*)m_pList->GetAt(m_position);
  90.   m_Name  = pPhone->m_Name;
  91.   m_Phone = pPhone->m_Phone;
  92.  
  93.   // Update the screen with the new values of the variables.
  94.   UpdateData(FALSE);
  95.  
  96.   // Place the cursor inside the IDC_NAME edit box.
  97.   ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
  98.  
  99. }
  100.  
  101. ////////////////////
  102. // MY CODE ENDS HERE
  103. ////////////////////
  104.  
  105.  
  106.  
  107. void CPhnView::OnChangeName()
  108. {
  109.     // TODO: Add your control notification handler code here
  110.  
  111.  
  112.     //////////////////////
  113.     // MY CODE STARTS HERE
  114.     //////////////////////
  115.  
  116.     // Update controls variables with screen contents.
  117.     UpdateData(TRUE);
  118.  
  119.     // Get a pointer to the document.
  120.     CPhnDoc* pDoc = (CPhnDoc*) GetDocument();
  121.  
  122.     // Update the document.
  123.     CPhone* pPhone = (CPhone*)m_pList->GetAt(m_position);
  124.     pPhone->m_Name = m_Name;
  125.  
  126.     // Set the Modified flag to TRUE.
  127.     pDoc->SetModifiedFlag();
  128.  
  129.     ////////////////////
  130.     // MY CODE ENDS HERE
  131.     ////////////////////
  132.     
  133. }
  134.  
  135. void CPhnView::OnChangePhone()
  136. {
  137.     // TODO: Add your control notification handler code here
  138.     
  139.  
  140.     //////////////////////
  141.     // MY CODE STARTS HERE
  142.     //////////////////////
  143.  
  144.     // Update controls variables with screen contents.
  145.     UpdateData(TRUE);
  146.  
  147.     // Get a pointer to the document.
  148.     CPhnDoc* pDoc = (CPhnDoc*) GetDocument();
  149.  
  150.     // Update the document.
  151.     CPhone* pPhone = (CPhone*)m_pList->GetAt(m_position);
  152.     pPhone->m_Phone = m_Phone;
  153.  
  154.     // Set the Modified flag to TRUE.
  155.     pDoc->SetModifiedFlag();
  156.  
  157.     ////////////////////
  158.     // MY CODE ENDS HERE
  159.     ////////////////////
  160.  
  161.  
  162. }
  163.  
  164. void CPhnView::OnPreviousButton()
  165. {
  166.     // TODO: Add your control notification handler code here
  167.  
  168.   //////////////////////
  169.   // MY CODE STARTS HERE
  170.   //////////////////////
  171.  
  172.   // Declare a Temporary POSITION variable.
  173.   POSITION temp_pos;
  174.  
  175.   // Update temp_pos with the current position of the list.
  176.   temp_pos = m_position;
  177.  
  178.   // Update temp_pos with the previous position.
  179.   m_pList->GetPrev(temp_pos);
  180.  
  181.   if (temp_pos == NULL)
  182.      {
  183.      // No previous element.
  184.      MessageBox("Bottom of file encountered!",
  185.                 "Phone for Windows");
  186.      }
  187.   else
  188.      {
  189.      // Update m_position, m_Name, and m_Phone.
  190.      m_position = temp_pos;
  191.      CPhone* pPhone = (CPhone*)m_pList->GetAt(m_position);
  192.      m_Name  = pPhone->m_Name;
  193.      m_Phone = pPhone->m_Phone;
  194.      UpdateData (FALSE);
  195.      }
  196.  
  197.   // Place the cursor inside the IDC_NAME edit box.
  198.   ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
  199.  
  200.   ////////////////////
  201.   // MY CODE ENDS HERE
  202.   ////////////////////
  203.  
  204. }
  205.  
  206. void CPhnView::OnNextButton()
  207. {
  208.     // TODO: Add your control notification handler code here
  209.     
  210.   //////////////////////
  211.   // MY CODE STARTS HERE
  212.   //////////////////////
  213.  
  214.   // Declare a temporary POSITION variable.
  215.   POSITION temp_pos;
  216.  
  217.   // Update temp_pos with the current position of the list.
  218.   temp_pos = m_position;
  219.  
  220.   // Update temp_pos with the next position.
  221.   m_pList->GetNext(temp_pos);
  222.  
  223.   if (temp_pos == NULL)
  224.      {
  225.      // No next element.
  226.      MessageBox("End of file encountered!",
  227.                 "Phone for Windows");
  228.      }
  229.   else
  230.      {
  231.      // Update m_position, m_Name, and m_Phone.
  232.      m_position = temp_pos;
  233.      CPhone* pPhone = (CPhone*)m_pList->GetAt(m_position);
  234.      m_Name  = pPhone->m_Name;
  235.      m_Phone = pPhone->m_Phone;
  236.      UpdateData (FALSE);
  237.      }
  238.  
  239.   // Place the cursor inside the IDC_NAME edit box.
  240.   ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
  241.  
  242.   ////////////////////
  243.   // MY CODE ENDS HERE
  244.   ////////////////////
  245.  
  246.  
  247. }
  248.  
  249. void CPhnView::OnAddButton()
  250. {
  251.     // TODO: Add your control notification handler code here
  252.  
  253.   //////////////////////
  254.   // MY CODE STARTS HERE
  255.   //////////////////////
  256.  
  257.   // Update m_Name, m_Phone and the screen with blanks.
  258.   m_Name  = "";
  259.   m_Phone = "";
  260.   UpdateData (FALSE);
  261.  
  262.   // Create a new object of class CPhone.
  263.   CPhone* pPhone  = new CPhone();
  264.   pPhone->m_Name  = m_Name;
  265.   pPhone->m_Phone = m_Phone;
  266.  
  267.   // Add the new object to the tail of the list, and
  268.   // update m_position with the new position.
  269.   m_position = m_pList->AddTail(pPhone);
  270.  
  271.   // Get a pointer to the document.
  272.   CPhnDoc* pDoc = (CPhnDoc*) GetDocument();
  273.  
  274.   // Set the Modified flag to TRUE.
  275.   pDoc->SetModifiedFlag();
  276.  
  277.   // Place the cursor inside the IDC_NAME edit box.
  278.   ((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
  279.  
  280.   ////////////////////
  281.   // MY CODE ENDS HERE
  282.   ////////////////////
  283.  
  284.     
  285. }
  286.  
  287. void CPhnView::OnDeleteButton()
  288. {
  289.     // TODO: Add your control notification handler code here
  290.  
  291.   ///////////////////////
  292.   // MY CODE STARTS HERE
  293.   //////////////////////
  294.  
  295.   // Save the old pointer for deletion.
  296.   CObject* pOld;
  297.   pOld = m_pList->GetAt( m_position );
  298.  
  299.   // Remove the element from the list.
  300.   m_pList->RemoveAt( m_position );
  301.  
  302.   // Delete the object from memory.
  303.   delete pOld;
  304.  
  305.   // If the list is now completely empty, add a blank item.
  306.   if ( m_pList->IsEmpty() )
  307.      OnAddButton();
  308.  
  309.   // Get a pointer to the document.
  310.   CPhnDoc* pDoc = (CPhnDoc*) GetDocument();
  311.  
  312.   // Set the Modified flag to TRUE.
  313.   pDoc->SetModifiedFlag();
  314.  
  315.   // Display the first item of the list.
  316.   OnInitialUpdate();
  317.  
  318.   ////////////////////
  319.   // MY CODE ENDS HERE
  320.   ////////////////////
  321.     
  322. }
  323.