home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / minidrw2 / miniddoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  3.4 KB  |  156 lines

  1. // MiniDDoc.cpp : implementation of the CMiniDrawDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MiniDraw.h"
  6.  
  7. #include "MiniDDoc.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMiniDrawDoc
  17.  
  18. IMPLEMENT_DYNCREATE(CMiniDrawDoc, CDocument)
  19.  
  20. BEGIN_MESSAGE_MAP(CMiniDrawDoc, CDocument)
  21.    //{{AFX_MSG_MAP(CMiniDrawDoc)
  22.    ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  23.    ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  24.    ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  25.    ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
  26.    //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CMiniDrawDoc construction/destruction
  31.  
  32. CMiniDrawDoc::CMiniDrawDoc()
  33. {
  34.    // TODO: add one-time construction code here
  35.  
  36. }
  37.  
  38. CMiniDrawDoc::~CMiniDrawDoc()
  39. {
  40. }
  41.  
  42. BOOL CMiniDrawDoc::OnNewDocument()
  43. {
  44.    if (!CDocument::OnNewDocument())
  45.       return FALSE;
  46.  
  47.    // TODO: add reinitialization code here
  48.    // (SDI documents will reuse this document)
  49.  
  50.    return TRUE;
  51. }
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CMiniDrawDoc serialization
  55.  
  56. void CMiniDrawDoc::Serialize(CArchive& ar)
  57. {
  58.    if (ar.IsStoring())
  59.    {
  60.       // TODO: add storing code here
  61.    }
  62.    else
  63.    {
  64.       // TODO: add loading code here
  65.    }
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMiniDrawDoc diagnostics
  70.  
  71. #ifdef _DEBUG
  72. void CMiniDrawDoc::AssertValid() const
  73. {
  74.    CDocument::AssertValid();
  75. }
  76.  
  77. void CMiniDrawDoc::Dump(CDumpContext& dc) const
  78. {
  79.    CDocument::Dump(dc);
  80. }
  81. #endif //_DEBUG
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CMiniDrawDoc commands
  85.  
  86. void CLine::Draw (CDC *PDC)
  87.    {
  88.    PDC->MoveTo (m_X1, m_Y1);
  89.    PDC->LineTo (m_X2, m_Y2);
  90.    }
  91.  
  92. void CMiniDrawDoc::AddLine (int X1, int Y1, int X2, int Y2)
  93.    {            
  94.    CLine *PLine = new CLine (X1, Y1, X2, Y2);
  95.    m_LineArray.Add (PLine);
  96.    }
  97.  
  98. CLine *CMiniDrawDoc::GetLine (int Index)
  99.    {
  100.    if (Index < 0 || Index > m_LineArray.GetUpperBound ())
  101.       return 0;
  102.    return m_LineArray.GetAt (Index);
  103.    }    
  104.    
  105. int CMiniDrawDoc::GetNumLines ()
  106.    {
  107.    return m_LineArray.GetSize ();
  108.    }
  109.  
  110. void CMiniDrawDoc::DeleteContents() 
  111. {
  112.    // TODO: Add your specialized code here and/or call the base class
  113.    
  114.    int Index = m_LineArray.GetSize ();
  115.    while (Index--)
  116.       delete m_LineArray.GetAt (Index);      
  117.    m_LineArray.RemoveAll ();      
  118.  
  119.    CDocument::DeleteContents();
  120. }
  121.  
  122. void CMiniDrawDoc::OnEditClearAll() 
  123. {
  124.    // TODO: Add your command handler code here
  125.  
  126.    DeleteContents ();
  127.    UpdateAllViews (0);
  128. }
  129.  
  130. void CMiniDrawDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI) 
  131. {
  132.    // TODO: Add your command update UI handler code here
  133.  
  134.    pCmdUI->Enable (m_LineArray.GetSize ());  
  135. }
  136.  
  137. void CMiniDrawDoc::OnEditUndo() 
  138. {
  139.    // TODO: Add your command handler code here
  140.  
  141.    int Index = m_LineArray.GetUpperBound ();
  142.    if (Index > -1)
  143.       {
  144.       delete m_LineArray.GetAt (Index);
  145.       m_LineArray.RemoveAt (Index);
  146.       }
  147.    UpdateAllViews (0);                                    
  148. }
  149.  
  150. void CMiniDrawDoc::OnUpdateEditUndo(CCmdUI* pCmdUI) 
  151. {
  152.    // TODO: Add your command update UI handler code here
  153.    
  154.    pCmdUI->Enable (m_LineArray.GetSize ());
  155. }
  156.