home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c05 / lab03 / baseline / scribdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.7 KB  |  176 lines

  1. // ScribDoc.cpp : implementation of the CScribbleDoc class
  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 "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CScribbleDoc
  26.  
  27. IMPLEMENT_DYNCREATE(CScribbleDoc, CDocument)
  28.  
  29. BEGIN_MESSAGE_MAP(CScribbleDoc, CDocument)
  30.     //{{AFX_MSG_MAP(CScribbleDoc)
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CScribbleDoc construction/destruction
  36.  
  37. CScribbleDoc::CScribbleDoc()
  38. {
  39.     // TODO: add one-time construction code here
  40.  
  41. }
  42.  
  43. CScribbleDoc::~CScribbleDoc()
  44. {
  45. }
  46.  
  47. BOOL CScribbleDoc::OnNewDocument()
  48. {
  49.     if (!CDocument::OnNewDocument())
  50.         return FALSE;
  51.     InitDocument();
  52.     return TRUE;
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CScribbleDoc serialization
  57.  
  58. void CScribbleDoc::Serialize(CArchive& ar)
  59. {
  60.     if (ar.IsStoring())
  61.     {
  62.     }
  63.     else
  64.     {
  65.     }
  66.     m_strokeList.Serialize(ar);
  67. }
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CScribbleDoc diagnostics
  71.  
  72. #ifdef _DEBUG
  73. void CScribbleDoc::AssertValid() const
  74. {
  75.     CDocument::AssertValid();
  76. }
  77.  
  78. void CScribbleDoc::Dump(CDumpContext& dc) const
  79. {
  80.     CDocument::Dump(dc);
  81. }
  82. #endif //_DEBUG
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CScribbleDoc commands
  86.  
  87. BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  88. {
  89.     if (!CDocument::OnOpenDocument(lpszPathName))
  90.         return FALSE;
  91.     InitDocument(); 
  92.     return TRUE;
  93. }
  94.  
  95. void CScribbleDoc::DeleteContents() 
  96. {
  97.     while (!m_strokeList.IsEmpty())
  98.     {
  99.         delete m_strokeList.RemoveHead();
  100.     }
  101.     CDocument::DeleteContents();
  102. }
  103.  
  104. void CScribbleDoc::InitDocument()
  105. {
  106.     m_nPenWidth = THIN; // default 2 pixel pen width
  107.     // solid, black pen
  108.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0));
  109. }
  110.  
  111. CStroke* CScribbleDoc::NewStroke()
  112. {
  113.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  114.     m_strokeList.AddTail(pStrokeItem);
  115.     SetModifiedFlag();  // Mark the document as having been modified, for
  116.                         // purposes of confirming File Close.
  117.     return pStrokeItem;
  118. }
  119.  
  120. void CScribbleDoc::ChangePen(PENWIDTH penWidth)
  121. {
  122.     m_nPenWidth = penWidth;
  123.  
  124.     //delete the current pen before creating the new one
  125.     m_penCur.DeleteObject();
  126.     m_penCur.CreatePen(    PS_SOLID,
  127.                         m_nPenWidth,
  128.                         RGB (0,0,0));
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CStroke
  133.  
  134. IMPLEMENT_SERIAL(CStroke, CObject, 1)
  135. CStroke::CStroke()
  136. {
  137.     // This empty constructor should be used by serialization only
  138. }
  139.  
  140. CStroke::CStroke(UINT nPenWidth)
  141. {
  142.     m_nPenWidth = nPenWidth;
  143. }
  144.  
  145. void CStroke::Serialize(CArchive& ar)
  146. {
  147.     if (ar.IsStoring())
  148.     {
  149.         ar << (WORD)m_nPenWidth;
  150.         m_pointArray.Serialize(ar);
  151.     }
  152.     else
  153.     {
  154.         WORD w;
  155.         ar >> w;
  156.         m_nPenWidth = w;
  157.         m_pointArray.Serialize(ar);
  158.     }
  159. }
  160.  
  161. BOOL CStroke::DrawStroke(CDC* pDC)
  162. {
  163.     CPen penStroke;
  164.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  165.         return FALSE;
  166.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  167.     pDC->MoveTo(m_pointArray[0]);
  168.     for (int i=1; i < m_pointArray.GetSize(); i++)
  169.     {
  170.         pDC->LineTo(m_pointArray[i]);
  171.     }
  172.  
  173.     pDC->SelectObject(pOldPen);
  174.     return TRUE;
  175. }
  176.