home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / SCRIBBLE / STEP5 / SCRIBDOC.CP_ / SCRIBDOC.CP
Encoding:
Text File  |  1993-02-08  |  8.1 KB  |  314 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.     // default document size is 8 x 9 inches, measures in MM_LOENGLISH
  71.     // (0.01 inches)
  72.     m_sizeDoc = CSize(800,900);
  73.  
  74. }
  75.  
  76. BOOL CScribDoc::OnNewDocument()
  77. {
  78.     if (!CDocument::OnNewDocument())
  79.         return FALSE;
  80.     InitDocument();
  81.     return TRUE;
  82. }
  83.  
  84. BOOL CScribDoc::OnOpenDocument(const char* pszPathName)
  85. {
  86.     if (!CDocument::OnOpenDocument(pszPathName))
  87.         return FALSE;
  88.     InitDocument();
  89.     return TRUE;
  90. }
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CScribDoc serialization
  94.  
  95. void CScribDoc::Serialize(CArchive& ar)
  96. {
  97.     if (ar.IsStoring())
  98.     {
  99.         ar << m_sizeDoc;
  100.     }
  101.     else
  102.     {
  103.         ar >> m_sizeDoc;
  104.     }
  105.     m_strokeList.Serialize(ar);
  106. }
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CScribDoc diagnostics
  111.  
  112. #ifdef _DEBUG
  113. void CScribDoc::AssertValid() const
  114. {
  115.     CDocument::AssertValid();
  116. }
  117.  
  118. void CScribDoc::Dump(CDumpContext& dc) const
  119. {
  120.     CDocument::Dump(dc);
  121. }
  122.  
  123. #endif //_DEBUG
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CScribDoc commands
  127.  
  128. void CScribDoc::OnEditClearAll()
  129. {
  130.     DeleteContents();
  131.     SetModifiedFlag();  // Mark the document as having been modified, for 
  132.                         // purposes of confirming File Close.
  133.     UpdateAllViews(NULL);
  134. }
  135.  
  136. void CScribDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
  137. {
  138.     // Enable the command user interface object (menu item or tool bar
  139.     // button) if the document is non-empty, i.e., has at least one stroke.
  140.     pCmdUI->Enable(!m_strokeList.IsEmpty());
  141. }
  142.  
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. void CScribDoc::OnPenThickOrThin()
  146. {
  147.     // Toggle the state of the pen between thin or thick.
  148.     m_bThickPen = !m_bThickPen;
  149.  
  150.     // Change the current pen to reflect the new user-specified width.
  151.     ReplacePen();
  152. }
  153.  
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. void CScribDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI)
  157. {
  158.     // Add check mark to Draw Thick Line menu item, if the current
  159.     // pen width is "thick".
  160.     pCmdUI->SetCheck(m_bThickPen);
  161. }
  162.  
  163.  
  164. /////////////////////////////////////////////////////////////////////////////
  165. void CScribDoc::OnPenWidths()
  166. {
  167.     CPenWidthsDlg dlg;
  168.     // Initialize dialog data
  169.     dlg.m_nThinWidth = m_nThinWidth;
  170.     dlg.m_nThickWidth = m_nThickWidth;
  171.  
  172.     // Invoke the dialog box
  173.     if (dlg.DoModal() == IDOK)
  174.     {
  175.         // retrieve the dialog data
  176.         m_nThinWidth = dlg.m_nThinWidth;
  177.         m_nThickWidth = dlg.m_nThickWidth;
  178.  
  179.         // Update the pen that is used by views when drawing new strokes,
  180.         // to reflect the new pen width definitions for "thick" and "thin".
  181.         ReplacePen();
  182.     }
  183. }
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186. void CScribDoc::ReplacePen()
  187. {
  188.     m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
  189.  
  190.     // Change the current pen to reflect the new user-specified width.
  191.     m_penCur.DeleteObject();
  192.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
  193. }
  194.  
  195. /////////////////////////////////////////////////////////////////////////////
  196. CStroke* CScribDoc::NewStroke()
  197. {
  198.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  199.     m_strokeList.AddTail(pStrokeItem);
  200.     SetModifiedFlag();  // Mark the document as having been modified, for
  201.                         // purposes of confirming File Close.
  202.     return pStrokeItem;
  203. }
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206. POSITION CScribDoc::GetFirstStrokePos()
  207. {
  208.     return m_strokeList.GetHeadPosition();
  209. }
  210.  
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213. CStroke* CScribDoc::GetNextStroke(POSITION& pos)
  214. {
  215.     return (CStroke*)m_strokeList.GetNext(pos);
  216. }
  217.  
  218.  
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CStroke
  221.  
  222. // Each time we change what gets serialized, we change the
  223. // schema number.
  224. IMPLEMENT_SERIAL(CStroke, CObject, 2)
  225.  
  226. /////////////////////////////////////////////////////////////////////////////
  227. CStroke::CStroke()
  228. {
  229.     // this empty constructor should be used by serialization only
  230. }
  231.  
  232. /////////////////////////////////////////////////////////////////////////////
  233. CStroke::CStroke(UINT nPenWidth)
  234. {
  235.     m_nPenWidth = nPenWidth;
  236.     m_rectBounding.SetRectEmpty();
  237. }
  238.  
  239. /////////////////////////////////////////////////////////////////////////////
  240. void CStroke::AddPoint(CPoint pt)
  241. {
  242.     m_pointArray.Add(MAKELONG(pt.x, pt.y));
  243. }
  244.  
  245. /////////////////////////////////////////////////////////////////////////////
  246. BOOL CStroke::DrawStroke(CDC* pDC)
  247. {
  248.     CPen penStroke;
  249.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  250.         return FALSE;
  251.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  252.     pDC->MoveTo(GetPoint(0));
  253.     for (int i=1; i < m_pointArray.GetSize(); i++)
  254.     {
  255.         pDC->LineTo(GetPoint(i));
  256.     }
  257.  
  258.     pDC->SelectObject(pOldPen);
  259.     return TRUE;
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. void CStroke::Serialize(CArchive& ar)
  264. {
  265.     if (ar.IsStoring())
  266.     {
  267.         ar << m_rectBounding;
  268.         ar << (WORD)m_nPenWidth;
  269.         m_pointArray.Serialize(ar);
  270.     }
  271.     else
  272.     {
  273.         ar >> m_rectBounding;
  274.         WORD w;
  275.         ar >> w;
  276.         m_nPenWidth = w;
  277.         m_pointArray.Serialize(ar);
  278.     }
  279. }
  280.  
  281. /////////////////////////////////////////////////////////////////////////////
  282. void CStroke::FinishStroke()
  283. {
  284.     // Calculate the bounding rectangle.  It's needed for smart
  285.     // repainting.
  286.  
  287.     if (m_pointArray.GetSize()==0)
  288.     {
  289.         m_rectBounding.SetRectEmpty();
  290.         return;
  291.     }
  292.     CPoint pt = GetPoint(0);
  293.     m_rectBounding = CRect(pt.x, pt.y, pt.x, pt.y);
  294.  
  295.     for (int i=1; i < m_pointArray.GetSize(); i++)
  296.     {
  297.         // If the point lies outside of the accumulated bounding
  298.         // rectangle, then inflate the bounding rect to include it.
  299.         pt = GetPoint(i);
  300.         m_rectBounding.left     = min(m_rectBounding.left, pt.x);
  301.         m_rectBounding.right    = max(m_rectBounding.right, pt.x);
  302.         m_rectBounding.top      = max(m_rectBounding.top, pt.y);
  303.         m_rectBounding.bottom   = min(m_rectBounding.bottom, pt.y);
  304.     }
  305.  
  306.     // Add the pen width to the bounding rectangle.  This is necessary
  307.     // to account for the width of the stroke when invalidating
  308.     // the screen.
  309.     m_rectBounding.InflateRect(CSize(m_nPenWidth, -(int)m_nPenWidth));
  310.     return;
  311. }
  312.  
  313.  
  314.