home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c07 / treelist / treedoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  2.9 KB  |  123 lines

  1. // treeDoc.cpp : implementation of the CTreeDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "AnmlData.h"
  6.  
  7. #include "tree.h"
  8.  
  9. #include "treeview.h"
  10. #include "treeDoc.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CTreeDoc
  20.  
  21. IMPLEMENT_DYNCREATE(CTreeDoc, CDocument)
  22.  
  23. BEGIN_MESSAGE_MAP(CTreeDoc, CDocument)
  24.     //{{AFX_MSG_MAP(CTreeDoc)
  25.         // NOTE - the ClassWizard will add and remove mapping macros here.
  26.         //    DO NOT EDIT what you see in these blocks of generated code!
  27.     //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CTreeDoc construction/destruction
  32.  
  33. // The document must initialize its 2 view pointers to 0.
  34. CTreeDoc::CTreeDoc()
  35.     : m_pTreeView(0), m_pListView(0)
  36. {
  37. }
  38.  
  39. CTreeDoc::~CTreeDoc()
  40. {
  41. }
  42.  
  43. POSITION CTreeDoc::InsertData (CAnimalInfo &animal)
  44. {
  45.     // No attempt is made here to keep the list ordered.
  46.     // The tree view will take care of that.
  47.     POSITION pos = m_AnimalList.AddTail(animal);
  48.  
  49.     SetModifiedFlag();
  50.  
  51.     return pos;
  52. }
  53.  
  54. BOOL CTreeDoc::OnNewDocument()
  55. {
  56.     if (!CDocument::OnNewDocument())
  57.         return FALSE;
  58.     
  59.     // Even an empty document (in this app) has
  60.     // something to display in the tree view.
  61.     m_pTreeView->PopulateTree();
  62.  
  63.     return TRUE;
  64. }
  65.  
  66. void CTreeDoc::DeleteContents() 
  67. {
  68.     // Clear out everything from the document object's CList.
  69.     m_AnimalList.RemoveAll();
  70.     
  71.     CDocument::DeleteContents();
  72. }
  73.  
  74. BOOL CTreeDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  75. {
  76.     if (!CDocument::OnOpenDocument(lpszPathName))
  77.         return FALSE;
  78.     
  79.     // Now that the document is open and its CList object
  80.     // has been deserialized, display the data in the tree view.
  81.     m_pTreeView->PopulateTree();
  82.     
  83.     return TRUE;
  84. }
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CTreeDoc serialization
  87.  
  88. // This helper function, which is not a member of any class,
  89. // is needed by the CList object in the document class. Without it,
  90. // CAnimalInfo's Serialize function won't be called. When called
  91. // for a CList, nCount is always 1. 
  92.  
  93. // The default SerializeElements called by CList does a bit-wise
  94. // read/write, which won't work for CAnimalInfo's CString members.
  95. void AFXAPI SerializeElements(CArchive& ar, CAnimalInfo * pMyData, int nCount)
  96. {
  97.     pMyData->Serialize(ar);
  98. }
  99.  
  100. void CTreeDoc::Serialize(CArchive& ar)
  101. {
  102.     m_AnimalList.Serialize(ar);
  103. }
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CTreeDoc diagnostics
  107.  
  108. #ifdef _DEBUG
  109. void CTreeDoc::AssertValid() const
  110. {
  111.     CDocument::AssertValid();
  112. }
  113.  
  114. void CTreeDoc::Dump(CDumpContext& dc) const
  115. {
  116.     CDocument::Dump(dc);
  117. }
  118. #endif //_DEBUG
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CTreeDoc commands
  122.  
  123.