home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / oclient / maindoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.8 KB  |  189 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-1998 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.  
  14. #include "stdafx.h"
  15. #include "oclient.h"
  16.  
  17. #include "maindoc.h"
  18. #include "rectitem.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. // private clipboard format
  26. CLIPFORMAT CMainDoc::m_cfPrivate = NULL;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMainDoc
  30.  
  31. IMPLEMENT_DYNCREATE(CMainDoc, COleLinkingDoc)
  32.  
  33. BEGIN_MESSAGE_MAP(CMainDoc, COleLinkingDoc)
  34.     //{{AFX_MSG_MAP(CMainDoc)
  35.     ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  36.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  37.     //}}AFX_MSG_MAP
  38.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdatePasteMenu)
  39.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, OnUpdatePasteLinkMenu)
  40.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, OnUpdateEditLinksMenu)
  41.     ON_COMMAND(ID_OLE_EDIT_LINKS, OnEditLinks)
  42.     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, OnUpdateObjectVerbMenu)
  43.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CHANGE_ICON, OnUpdateEditChangeIcon)
  44.     ON_COMMAND(ID_OLE_EDIT_CHANGE_ICON, OnEditChangeIcon)
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CMainDoc construction/destruction
  49.  
  50. CMainDoc::CMainDoc() : m_sizeDoc(800,1050) // document size is 8x10.5
  51. {
  52.     EnableCompoundFile();
  53.  
  54.     m_bNeedUpdate = TRUE;
  55.     if (m_cfPrivate == NULL)
  56.         m_cfPrivate = (CLIPFORMAT)
  57.             ::RegisterClipboardFormat(_T("MFC OClient Sample"));
  58. }
  59.  
  60. CMainDoc::~CMainDoc()
  61. {
  62. }
  63.  
  64. CSize &CMainDoc::GetDocumentSize()
  65. {
  66.     return m_sizeDoc;
  67. }
  68.  
  69. void CMainDoc::OnShowViews(BOOL bVisible)
  70. {
  71.     COleLinkingDoc::OnShowViews(bVisible);
  72.  
  73.     if (bVisible && m_bNeedUpdate)
  74.     {
  75.         // update embedded links in this document before showing the window
  76.         COleUpdateDialog dlg(this);
  77.         dlg.DoModal();
  78.         m_bNeedUpdate = FALSE;
  79.     }
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CMainDoc item management
  84.  
  85. CRectItem* CMainDoc::CreateItem()
  86. {
  87.     return new CRectItem(this); // does 'AddItem' automatically
  88. }
  89.  
  90. // safe delete that notifies views
  91. void CMainDoc::DeleteItem(CRectItem* pItem)
  92. {
  93.     ASSERT(pItem->GetDocument() == this);
  94.  
  95.     SetModifiedFlag();
  96.     UpdateAllViews(NULL, 1, pItem); // pItem will be deleted
  97.     pItem->Delete();    // does a 'RemoveItem' & 'delete this' automatically
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CMainDoc serialization
  102.  
  103. void CMainDoc::Serialize(CArchive& ar)
  104. {
  105. // NOTE: New easier to use serialization model -- even for OLE objects!
  106.     WORD wMagic = 0x0DAF;
  107.     if (ar.IsStoring())
  108.     {
  109.         if (HasBlankItems() &&
  110.             AfxMessageBox(IDP_SAVEINCOMPLETE, MB_YESNO|MB_ICONQUESTION) != IDYES)
  111.         {
  112.             TRACE0("Aborting save -- incomplete items found!\n");
  113.             AfxThrowArchiveException(CArchiveException::generic);
  114.         }
  115.         ar << wMagic;
  116.     }
  117.     else
  118.     {
  119.         WORD w;
  120.         ar >> w;
  121.  
  122.         if (w != wMagic)
  123.         {
  124.             TRACE0("invalid magic number at start of file\n");
  125.             AfxThrowArchiveException(CArchiveException::generic);
  126.         }
  127.     }
  128.  
  129.     // serialize the rest of the document (OLE items)
  130.     COleLinkingDoc::Serialize(ar);
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CMainDoc commands
  135.  
  136. void CMainDoc::OnEditClearAll()
  137. {
  138.     // delete all items in the document (also removes sub-storages)
  139.     POSITION pos = GetStartPosition();
  140.     while (pos != NULL)
  141.     {
  142.         CRectItem* pItem = (CRectItem*)GetNextItem(pos);
  143.         ASSERT_KINDOF(CRectItem, pItem);
  144.         pItem->Delete();
  145.     }
  146.  
  147.     // everything is gone now!
  148.     SetModifiedFlag();
  149.     UpdateAllViews(NULL);
  150. }
  151.  
  152.  
  153. void CMainDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
  154. {
  155.     // Enable ClearAll if there is anything to clear
  156.     pCmdUI->Enable(GetStartPosition() != NULL);
  157. }
  158.  
  159. void CMainDoc::AdjustItemPosition(CRectItem* pItem)
  160. {
  161.     POSITION pos = GetStartPosition();
  162.     while (pos != NULL)
  163.     {
  164.         CRectItem* pRectItem = (CRectItem*)GetNextItem(pos);
  165.         ASSERT_KINDOF(CRectItem, pItem);
  166.         if (pRectItem != pItem && pRectItem->GetRect() == pItem->GetRect())
  167.         {
  168.             pItem->m_ptPos.x += 10;
  169.             pItem->m_ptPos.y -= 10;
  170.             pos = GetStartPosition();
  171.         }
  172.     }
  173. }
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // CMainDoc diagnostics
  177.  
  178. #ifdef _DEBUG
  179. void CMainDoc::AssertValid() const
  180. {
  181.     COleLinkingDoc::AssertValid();
  182. }
  183.  
  184. void CMainDoc::Dump(CDumpContext& dc) const
  185. {
  186.     COleLinkingDoc::Dump(dc);
  187. }
  188. #endif //_DEBUG
  189.