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