home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / SCRIBBLE / STEP2 / SCRIBDOC.CP_ / SCRIBDOC.CP
Encoding:
Text File  |  1993-02-08  |  6.2 KB  |  249 lines

  1. // scribdoc.cpp : implementation of the CScribDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 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 Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "scribble.h"
  16.  
  17. #include "scribdoc.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CScribDoc
  26.  
  27. IMPLEMENT_DYNCREATE(CScribDoc, CDocument)
  28.  
  29. BEGIN_MESSAGE_MAP(CScribDoc, CDocument)
  30.     //{{AFX_MSG_MAP(CScribDoc)
  31.         // NOTE - the ClassWizard will add and remove mapping macros here.
  32.         //    DO NOT EDIT what you see in these blocks of generated code !
  33.     ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  35.     ON_COMMAND(ID_PEN_THICK_OR_THIN, OnPenThickOrThin)
  36.     ON_UPDATE_COMMAND_UI(ID_PEN_THICK_OR_THIN, OnUpdatePenThickOrThin)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CScribDoc construction/destruction
  42.  
  43. CScribDoc::CScribDoc()
  44. {
  45.     // TODO: add one-time construction code here
  46. }
  47.  
  48. CScribDoc::~CScribDoc()
  49. {
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. void CScribDoc::DeleteContents()
  54. {
  55.     while (!m_strokeList.IsEmpty())
  56.     {
  57.         delete m_strokeList.RemoveHead();
  58.     }
  59. }
  60.  
  61. void CScribDoc::InitDocument()
  62. {
  63.     m_bThickPen = FALSE;
  64.     m_nThinWidth = 2;   // default thin pen is 2 pixels wide
  65.     m_nThickWidth = 5;  // default thick pen is 5 pixels wide
  66.     ReplacePen();       // initialze pen according to current width
  67. }
  68.  
  69. BOOL CScribDoc::OnNewDocument()
  70. {
  71.     if (!CDocument::OnNewDocument())
  72.         return FALSE;
  73.     InitDocument();
  74.     return TRUE;
  75. }
  76.  
  77. BOOL CScribDoc::OnOpenDocument(const char* pszPathName)
  78. {
  79.     if (!CDocument::OnOpenDocument(pszPathName))
  80.         return FALSE;
  81.     InitDocument();
  82.     return TRUE;
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CScribDoc serialization
  87.  
  88. void CScribDoc::Serialize(CArchive& ar)
  89. {
  90.     if (ar.IsStoring())
  91.     {
  92.     }
  93.     else
  94.     {
  95.     }
  96.     m_strokeList.Serialize(ar);
  97. }
  98.  
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CScribDoc diagnostics
  102.  
  103. #ifdef _DEBUG
  104. void CScribDoc::AssertValid() const
  105. {
  106.     CDocument::AssertValid();
  107. }
  108.  
  109. void CScribDoc::Dump(CDumpContext& dc) const
  110. {
  111.     CDocument::Dump(dc);
  112. }
  113.  
  114. #endif //_DEBUG
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CScribDoc commands
  118.  
  119. void CScribDoc::OnEditClearAll()
  120. {
  121.     DeleteContents();
  122.     SetModifiedFlag();  // Mark the document as having been modified, for 
  123.                         // purposes of confirming File Close.
  124.     UpdateAllViews(NULL);
  125. }
  126.  
  127. void CScribDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
  128. {
  129.     // Enable the command user interface object (menu item or tool bar
  130.     // button) if the document is non-empty, i.e., has at least one stroke.
  131.     pCmdUI->Enable(!m_strokeList.IsEmpty());
  132. }
  133.  
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. void CScribDoc::OnPenThickOrThin()
  137. {
  138.     // Toggle the state of the pen between thin or thick.
  139.     m_bThickPen = !m_bThickPen;
  140.  
  141.     // Change the current pen to reflect the new user-specified width.
  142.     ReplacePen();
  143. }
  144.  
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147. void CScribDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI)
  148. {
  149.     // Add check mark to Draw Thick Line menu item, if the current
  150.     // pen width is "thick".
  151.     pCmdUI->SetCheck(m_bThickPen);
  152. }
  153.  
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. void CScribDoc::ReplacePen()
  157. {
  158.     m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
  159.  
  160.     // Change the current pen to reflect the new user-specified width.
  161.     m_penCur.DeleteObject();
  162.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
  163. }
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. CStroke* CScribDoc::NewStroke()
  167. {
  168.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  169.     m_strokeList.AddTail(pStrokeItem);
  170.     SetModifiedFlag();  // Mark the document as having been modified, for
  171.                         // purposes of confirming File Close.
  172.     return pStrokeItem;
  173. }
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. POSITION CScribDoc::GetFirstStrokePos()
  177. {
  178.     return m_strokeList.GetHeadPosition();
  179. }
  180.  
  181.  
  182. /////////////////////////////////////////////////////////////////////////////
  183. CStroke* CScribDoc::GetNextStroke(POSITION& pos)
  184. {
  185.     return (CStroke*)m_strokeList.GetNext(pos);
  186. }
  187.  
  188.  
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CStroke
  191.  
  192. // Each time we change what gets serialized, we change the
  193. // schema number.
  194. IMPLEMENT_SERIAL(CStroke, CObject, 1)
  195. /////////////////////////////////////////////////////////////////////////////
  196. CStroke::CStroke()
  197. {
  198.     // this empty constructor should be used by serialization only
  199. }
  200.  
  201. /////////////////////////////////////////////////////////////////////////////
  202. CStroke::CStroke(UINT nPenWidth)
  203. {
  204.     m_nPenWidth = nPenWidth;
  205. }
  206.  
  207. /////////////////////////////////////////////////////////////////////////////
  208. void CStroke::AddPoint(CPoint pt)
  209. {
  210.     m_pointArray.Add(MAKELONG(pt.x, pt.y));
  211. }
  212.  
  213. /////////////////////////////////////////////////////////////////////////////
  214. BOOL CStroke::DrawStroke(CDC* pDC)
  215. {
  216.     CPen penStroke;
  217.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  218.         return FALSE;
  219.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  220.     pDC->MoveTo(GetPoint(0));
  221.     for (int i=1; i < m_pointArray.GetSize(); i++)
  222.     {
  223.         pDC->LineTo(GetPoint(i));
  224.     }
  225.  
  226.     pDC->SelectObject(pOldPen);
  227.     return TRUE;
  228. }
  229.  
  230. /////////////////////////////////////////////////////////////////////////////
  231. void CStroke::Serialize(CArchive& ar)
  232. {
  233.     if (ar.IsStoring())
  234.     {
  235.         ar << (WORD)m_nPenWidth;
  236.         m_pointArray.Serialize(ar);
  237.     }
  238.     else
  239.     {
  240.         WORD w;
  241.         ar >> w;
  242.         m_nPenWidth = w;
  243.         m_pointArray.Serialize(ar);
  244.     }
  245. }
  246.  
  247.  
  248.  
  249.