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