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