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