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

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