home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c05 / lab02 / baseline / scribdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.9 KB  |  191 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.     ON_COMMAND(ID_PEN_CHANGETOTHICKORTHIN, OnPenChangetothickorthin)
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CScribbleDoc construction/destruction
  37.  
  38. CScribbleDoc::CScribbleDoc()
  39. {
  40.     // TODO: add one-time construction code here
  41.  
  42. }
  43.  
  44. CScribbleDoc::~CScribbleDoc()
  45. {
  46. }
  47.  
  48. BOOL CScribbleDoc::OnNewDocument()
  49. {
  50.     if (!CDocument::OnNewDocument())
  51.         return FALSE;
  52.     InitDocument();
  53.     return TRUE;
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CScribbleDoc serialization
  58.  
  59. void CScribbleDoc::Serialize(CArchive& ar)
  60. {
  61.     if (ar.IsStoring())
  62.     {
  63.     }
  64.     else
  65.     {
  66.     }
  67.     m_strokeList.Serialize(ar);
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CScribbleDoc diagnostics
  72.  
  73. #ifdef _DEBUG
  74. void CScribbleDoc::AssertValid() const
  75. {
  76.     CDocument::AssertValid();
  77. }
  78.  
  79. void CScribbleDoc::Dump(CDumpContext& dc) const
  80. {
  81.     CDocument::Dump(dc);
  82. }
  83. #endif //_DEBUG
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CScribbleDoc commands
  87.  
  88. BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  89. {
  90.     if (!CDocument::OnOpenDocument(lpszPathName))
  91.         return FALSE;
  92.     InitDocument(); 
  93.     return TRUE;
  94. }
  95.  
  96. void CScribbleDoc::DeleteContents() 
  97. {
  98.     while (!m_strokeList.IsEmpty())
  99.     {
  100.         delete m_strokeList.RemoveHead();
  101.     }
  102.     CDocument::DeleteContents();
  103. }
  104.  
  105. void CScribbleDoc::InitDocument()
  106. {
  107.     m_nPenWidth = THIN; // default 2 pixel pen width
  108.     // solid, black pen
  109.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0));
  110. }
  111.  
  112. CStroke* CScribbleDoc::NewStroke()
  113. {
  114.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  115.     m_strokeList.AddTail(pStrokeItem);
  116.     SetModifiedFlag();  // Mark the document as having been modified, for
  117.                         // purposes of confirming File Close.
  118.     return pStrokeItem;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CStroke
  126.  
  127. IMPLEMENT_SERIAL(CStroke, CObject, 1)
  128. CStroke::CStroke()
  129. {
  130.     // This empty constructor should be used by serialization only
  131. }
  132.  
  133. CStroke::CStroke(UINT nPenWidth)
  134. {
  135.     m_nPenWidth = nPenWidth;
  136. }
  137.  
  138. void CStroke::Serialize(CArchive& ar)
  139. {
  140.     if (ar.IsStoring())
  141.     {
  142.         ar << (WORD)m_nPenWidth;
  143.         m_pointArray.Serialize(ar);
  144.     }
  145.     else
  146.     {
  147.         WORD w;
  148.         ar >> w;
  149.         m_nPenWidth = w;
  150.         m_pointArray.Serialize(ar);
  151.     }
  152. }
  153.  
  154. BOOL CStroke::DrawStroke(CDC* pDC)
  155. {
  156.     CPen penStroke;
  157.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  158.         return FALSE;
  159.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  160.     pDC->MoveTo(m_pointArray[0]);
  161.     for (int i=1; i < m_pointArray.GetSize(); i++)
  162.     {
  163.         pDC->LineTo(m_pointArray[i]);
  164.     }
  165.  
  166.     pDC->SelectObject(pOldPen);
  167.     return TRUE;
  168. }
  169.  
  170. void CScribbleDoc::OnPenChangetothickorthin() 
  171. {
  172.     if (GetPenWidth() == THIN)
  173.     {
  174.         ChangePen(THICK);
  175.     }
  176.     else
  177.     {
  178.         ChangePen(THIN);
  179.     }
  180. }
  181.  
  182.  
  183. void CScribbleDoc::ChangePen(PENWIDTH penWidth)
  184. {
  185.     m_nPenWidth = penWidth;
  186.     m_penCur.DeleteObject();
  187.     m_penCur.CreatePen( PS_SOLID,    
  188.                          m_nPenWidth,    
  189.                          RGB(0,0,0));    
  190. }
  191.