home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / OCLIENT / MAINDOC.CP_ / MAINDOC.CP
Encoding:
Text File  |  1993-02-08  |  4.6 KB  |  205 lines

  1. // maindoc.cpp : implementation of the CMainDoc 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.  
  15. #include "stdafx.h"
  16. #include "oclient.h"
  17.  
  18. #include "maindoc.h"
  19. #include "rectitem.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMainDoc
  28.  
  29. IMPLEMENT_DYNCREATE(CMainDoc, COleClientDoc)
  30.  
  31. BEGIN_MESSAGE_MAP(CMainDoc, COleClientDoc)
  32.     //{{AFX_MSG_MAP(CMainDoc)
  33.     ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CMainDoc construction/destruction
  40.  
  41. CMainDoc::CMainDoc()
  42. {
  43. }
  44.  
  45. CMainDoc::~CMainDoc()
  46. {
  47. }
  48.  
  49. BOOL CMainDoc::OnNewDocument()
  50. {
  51.     if (!COleClientDoc::OnNewDocument())
  52.         return FALSE;
  53.     return TRUE;
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CMainDoc item management
  58.  
  59. CRectItem* CMainDoc::CreateItem()
  60. {
  61.     CRectItem* pItem = new CRectItem(this); // does 'AddItem' automatically
  62.     ASSERT(!pItem->IsVisible());
  63.     // item is invisible - no need to update views or dirty document
  64.     return pItem;
  65. }
  66.  
  67. // safe delete that notifies views
  68. void CMainDoc::DeleteItem(CRectItem* pItem)
  69. {
  70.     if (pItem->IsVisible())
  71.     {
  72.         // only visible items really count
  73.         SetModifiedFlag();
  74.         pItem->Invalidate();
  75.     }
  76.     else
  77.     {
  78.         TRACE("Warning deleting an invisible RectItem\n");
  79.     }
  80.     ASSERT(pItem->GetDocument() == this);
  81.     delete pItem;       // does a 'RemoveItem' automatically
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CMainDoc serialization
  86.  
  87. void CMainDoc::Serialize(CArchive& ar)
  88. {
  89.     WORD wMagic = 0x0DAF;
  90.  
  91.     if (ar.IsStoring())
  92.     {
  93.         ar << wMagic;
  94.         // First count up number of OLE Objects to write
  95.         WORD cCompleteObjectsToWrite = 0;
  96.         WORD cIncompleteObjects = 0;
  97.         POSITION pos;
  98.  
  99.         pos = GetStartPosition();
  100.         while (pos)
  101.         {
  102.             CRectItem* pItem = (CRectItem*)GetNextItem(pos);
  103.             if (pItem->IsKindOf(RUNTIME_CLASS(CRectItem)))
  104.             {
  105.                 if (pItem->IsVisible())
  106.                     cCompleteObjectsToWrite++;
  107.                 else
  108.                     cIncompleteObjects++;
  109.             }
  110.         }
  111.  
  112.         TRACE("%d complete objects to write, %d incomplete ones to ignore\n",
  113.             cCompleteObjectsToWrite, cIncompleteObjects);
  114.  
  115.         if (cIncompleteObjects > 0 &&
  116.             AfxMessageBox(IDP_SAVEINCOMPLETE,
  117.               MB_YESNO | MB_ICONQUESTION) != IDYES)
  118.         {
  119.             TRACE("Aborting save\n");
  120.             AfxThrowArchiveException(CArchiveException::generic);
  121.         }
  122.  
  123.         ar << cCompleteObjectsToWrite;
  124.  
  125. #ifdef _DEBUG
  126.         WORD    cObjectsWritten = 0;
  127. #endif
  128.         // save items in correct order
  129.         pos = GetStartPosition();
  130.         while (pos)
  131.         {
  132.             CRectItem* pItem = (CRectItem*)GetNextItem(pos);
  133.             if (pItem->IsKindOf(RUNTIME_CLASS(CRectItem)) &&
  134.                pItem->IsVisible())
  135.             {
  136.                 pItem->Serialize(ar);
  137.                 // NOTE: don't use WriteObject since we know the actual
  138.                 //  class for pItem
  139. #ifdef _DEBUG
  140.                 cObjectsWritten++;
  141. #endif
  142.             }
  143.         }
  144. #ifdef _DEBUG
  145.         ASSERT(cObjectsWritten == cCompleteObjectsToWrite);
  146. #endif
  147.     }
  148.     else // loading
  149.     {
  150.         WORD w;
  151.         ar >> w;
  152.  
  153.         if (w != wMagic)
  154.         {
  155.             TRACE("invalid magic number at start of file\n");
  156.             AfxThrowArchiveException(CArchiveException::generic);
  157.         }
  158.  
  159.         WORD cObjectsToRead;
  160.         ar >> cObjectsToRead;
  161.  
  162.         for (UINT iObj = 0; iObj < cObjectsToRead; iObj++)
  163.         {
  164.             CRectItem* pItem = CreateItem();
  165.             pItem->Serialize(ar);
  166.         }
  167.     }
  168. }
  169.  
  170. /////////////////////////////////////////////////////////////////////////////
  171. // CMainDoc diagnostics
  172.  
  173. #ifdef _DEBUG
  174. void CMainDoc::AssertValid() const
  175. {
  176.     COleClientDoc::AssertValid();
  177. }
  178.  
  179. void CMainDoc::Dump(CDumpContext& dc) const
  180. {
  181.     COleClientDoc::Dump(dc);
  182. }
  183.  
  184. #endif //_DEBUG
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187. // CMainDoc commands
  188.  
  189.  
  190. void CMainDoc::OnEditClearAll()
  191. {
  192.     DeleteContents();
  193.     // everything is gone now !
  194.     SetModifiedFlag();
  195.     UpdateAllViews(NULL);
  196. }
  197.  
  198.  
  199. void CMainDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
  200. {
  201.     // Enable ClearAll if there is anything to clear
  202.     pCmdUI->Enable(GetStartPosition() != NULL);
  203. }
  204.  
  205.