home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / DRAWCL.PAK / DRAWDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.0 KB  |  305 lines

  1. // drawdoc.cpp : implementation of the CDrawDoc 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 "drawcli.h"
  15.  
  16. #include "drawdoc.h"
  17. #include "drawvw.h"
  18. #include "drawobj.h"
  19. #include "cntritem.h"
  20. #if !defined(_MAC)
  21. #include "summpage.h"
  22. #include "statpage.h"
  23. #endif
  24.  
  25. #ifdef _DEBUG
  26. #undef THIS_FILE
  27. static char BASED_CODE THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CDrawDoc
  32.  
  33. IMPLEMENT_DYNCREATE(CDrawDoc, COleDocument)
  34.  
  35. BEGIN_MESSAGE_MAP(CDrawDoc, COleDocument)
  36.     //{{AFX_MSG_MAP(CDrawDoc)
  37.     ON_COMMAND(ID_VIEW_PAPERCOLOR, OnViewPaperColor)
  38.     ON_COMMAND(ID_FILE_SUMMARYINFO, OnFileSummaryInfo)
  39.     //}}AFX_MSG_MAP
  40.     // Enable default OLE container implementation
  41.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
  42.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
  43.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
  44.     ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
  45.     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, COleDocument::OnUpdateObjectVerbMenu)
  46. #if !defined(_MAC)
  47.         // MAPI support
  48.     ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
  49.     ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
  50. #endif
  51. END_MESSAGE_MAP()
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CDrawDoc construction/destruction
  55.  
  56. CDrawDoc::CDrawDoc()
  57. {
  58.     EnableCompoundFile();
  59.  
  60.     m_nMapMode = MM_ANISOTROPIC;
  61.     m_paperColor = RGB(255, 255, 255);
  62. #if !defined(_MAC)
  63.     m_pSummInfo = NULL;
  64. #endif
  65.     ComputePageSize();
  66. }
  67.  
  68. CDrawDoc::~CDrawDoc()
  69. {
  70.     POSITION pos = m_objects.GetHeadPosition();
  71.     while (pos != NULL)
  72.         delete m_objects.GetNext(pos);
  73. #if !defined(_MAC)
  74.     delete m_pSummInfo;
  75. #endif
  76. }
  77.  
  78. BOOL CDrawDoc::OnNewDocument()
  79. {
  80.     if (!COleDocument::OnNewDocument())
  81.         return FALSE;
  82.  
  83.     // reinitialization code 
  84.     // (SDI documents will reuse this document)
  85. #if !defined(_MAC)
  86.     if(m_pSummInfo != NULL)
  87.         delete m_pSummInfo;
  88.     m_pSummInfo = new CSummInfo;
  89.     // Title, Subject, Author, Keywords default to empty string
  90.     // Comments, Template, SavedBy default to empty string
  91.     // LastSave, LastPrint, EditTime, RevNum default to 0
  92.     m_pSummInfo->StartEditTimeCount();
  93.     m_pSummInfo->RecordCreateDate();
  94.     m_pSummInfo->SetNumPages(1);
  95.     // NumWords, NumChars default to 0
  96.     m_pSummInfo->SetAppname( _T("DrawCli") );
  97.     // Security defaults to 0 
  98. #endif
  99.     return TRUE;
  100. }
  101.  
  102. BOOL CDrawDoc::OnOpenDocument(LPCTSTR lpszPathName)
  103. {
  104. #if !defined(_MAC)
  105.     if( m_pSummInfo != NULL)
  106.         delete m_pSummInfo;    
  107.     m_pSummInfo = new CSummInfo;
  108.     m_pSummInfo->StartEditTimeCount();
  109. #endif
  110.     return COleDocument::OnOpenDocument(lpszPathName);
  111. }
  112.  
  113. BOOL CDrawDoc::OnSaveDocument(LPCTSTR lpszPathName)
  114. {
  115. #if !defined(_MAC)
  116.     m_pSummInfo->RecordSaveDate();
  117.     m_pSummInfo->IncrRevNum();
  118.     m_pSummInfo->SetLastAuthor(m_pSummInfo->GetAuthor());
  119.     m_pSummInfo->AddCountToEditTime();
  120.     m_pSummInfo->StartEditTimeCount();
  121. #endif
  122.     return COleDocument::OnSaveDocument(lpszPathName);
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CDrawDoc serialization
  127.  
  128. void CDrawDoc::Serialize(CArchive& ar)
  129. {
  130.     if (ar.IsStoring())
  131.     {
  132.         ar << m_paperColor;
  133.         m_objects.Serialize(ar);
  134. #if !defined(_MAC)
  135.         m_pSummInfo->WriteToStorage(m_lpRootStg);
  136. #endif
  137.     }
  138.     else
  139.     {
  140.         ar >> m_paperColor;
  141.         m_objects.Serialize(ar);
  142. #if !defined(_MAC)
  143.         m_pSummInfo->ReadFromStorage(m_lpRootStg);
  144. #endif
  145.     }
  146.  
  147.     // By calling the base class COleDocument, we enable serialization
  148.     //  of the container document's COleClientItem objects automatically.
  149.     COleDocument::Serialize(ar);
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CDrawDoc implementation
  155.  
  156. void CDrawDoc::Draw(CDC* pDC, CDrawView* pView)
  157. {
  158.     POSITION pos = m_objects.GetHeadPosition();
  159.     while (pos != NULL)
  160.     {
  161.         CDrawObj* pObj = m_objects.GetNext(pos);
  162.         pObj->Draw(pDC);
  163.         if (pView->m_bActive && !pDC->IsPrinting() && pView->IsSelected(pObj))
  164.             pObj->DrawTracker(pDC, CDrawObj::selected);
  165.     }
  166. }
  167.  
  168. void CDrawDoc::Add(CDrawObj* pObj)
  169. {
  170.     m_objects.AddTail(pObj);
  171.     pObj->m_pDocument = this;
  172.     SetModifiedFlag();
  173. }
  174.  
  175. void CDrawDoc::Remove(CDrawObj* pObj)
  176. {
  177.     // Find and remove from document
  178.     POSITION pos = m_objects.Find(pObj);
  179.     if (pos != NULL)
  180.         m_objects.RemoveAt(pos);
  181.     // set document modified flag
  182.     SetModifiedFlag();
  183.  
  184.     // call remove for each view so that the view can remove from m_selection
  185.     pos = GetFirstViewPosition();
  186.     while (pos != NULL)
  187.         ((CDrawView*)GetNextView(pos))->Remove(pObj);
  188. }
  189.  
  190. // point is in logical coordinates
  191. CDrawObj* CDrawDoc::ObjectAt(const CPoint& point)
  192. {
  193.     CRect rect(point, CSize(1, 1));
  194.     POSITION pos = m_objects.GetTailPosition();
  195.     while (pos != NULL)
  196.     {
  197.         CDrawObj* pObj = m_objects.GetPrev(pos);
  198.         if (pObj->Intersects(rect))
  199.             return pObj;
  200.     }
  201.  
  202.     return NULL;
  203. }
  204.  
  205. void CDrawDoc::ComputePageSize()
  206. {
  207.     CSize new_size(850, 1100);  // 8.5" x 11" default
  208.  
  209.     CPrintDialog dlg(FALSE);
  210.     if (AfxGetApp()->GetPrinterDeviceDefaults(&dlg.m_pd))
  211.     {
  212.         // GetPrinterDC returns a HDC so attach it
  213.         CDC dc;
  214.         HDC hDC= dlg.CreatePrinterDC();
  215.         ASSERT(hDC != NULL);
  216.         dc.Attach(hDC);
  217.  
  218.         // Get the size of the page in loenglish
  219.         new_size.cx = MulDiv(dc.GetDeviceCaps(HORZSIZE), 1000, 254);
  220.         new_size.cy = MulDiv(dc.GetDeviceCaps(VERTSIZE), 1000, 254);
  221.     }
  222.  
  223.     // if size changed then iterate over views and reset
  224.     if (new_size != m_size)
  225.     {
  226.         m_size = new_size;
  227.         POSITION pos = GetFirstViewPosition();
  228.         while (pos != NULL)
  229.             ((CDrawView*)GetNextView(pos))->SetPageSize(m_size);
  230.     }
  231. }
  232.  
  233. void CDrawDoc::OnViewPaperColor()
  234. {
  235.     CColorDialog dlg;
  236.     if (dlg.DoModal() != IDOK)
  237.         return;
  238.  
  239.     m_paperColor = dlg.GetColor();
  240.     SetModifiedFlag();
  241.     UpdateAllViews(NULL);
  242. }
  243.  
  244. /////////////////////////////////////////////////////////////////////////////
  245. // CDrawDoc diagnostics
  246.  
  247. #ifdef _DEBUG
  248. void CDrawDoc::AssertValid() const
  249. {
  250.     COleDocument::AssertValid();
  251. }
  252.  
  253. void CDrawDoc::Dump(CDumpContext& dc) const
  254. {
  255.     COleDocument::Dump(dc);
  256. }
  257. #endif //_DEBUG
  258.  
  259. /////////////////////////////////////////////////////////////////////////////
  260. // CDrawDoc commands
  261.  
  262. void CDrawDoc::OnFileSummaryInfo() 
  263. {
  264. #if !defined(_MAC)
  265.     ASSERT_VALID(this);
  266.  
  267.     CPropertySheet sheet( _T("Document Properties") );
  268.     CSummPage summ;
  269.     CStatPage stat;
  270.     sheet.AddPage( &summ );
  271.     sheet.AddPage( &stat );
  272.  
  273.     summ.m_strAppname = m_pSummInfo->GetAppname();
  274.     summ.m_strTitle   = m_pSummInfo->GetTitle();
  275.     summ.m_strSubj    = m_pSummInfo->GetSubject();
  276.     summ.m_strAuthor  = m_pSummInfo->GetAuthor();
  277.     summ.m_strKeywd   = m_pSummInfo->GetKeywords();
  278.     summ.m_strCmt     = m_pSummInfo->GetComments();
  279.     summ.m_strTempl   = m_pSummInfo->GetTemplate();
  280.  
  281.     stat.m_strSavedBy    = m_pSummInfo->GetLastAuthor();
  282.     stat.m_strRevNum     = m_pSummInfo->GetRevNum();
  283.     stat.m_strEditTime   = m_pSummInfo->GetEditTime();
  284.     stat.m_strLastPrint  = m_pSummInfo->GetLastPrintDate();
  285.     stat.m_strCreateDate = m_pSummInfo->GetCreateDate();
  286.     stat.m_strLastSave   = m_pSummInfo->GetLastSaveDate();
  287.     stat.m_strNumPages   = m_pSummInfo->GetNumPages();
  288.     stat.m_strNumWords   = m_pSummInfo->GetNumWords();
  289.     stat.m_strNumChars   = m_pSummInfo->GetNumChars();
  290.     stat.m_strSecurity   = m_pSummInfo->GetSecurity();
  291.  
  292.     if (sheet.DoModal() != IDOK)
  293.         return;
  294.  
  295.     m_pSummInfo->SetAuthor(summ.m_strAuthor);
  296.     m_pSummInfo->SetKeywords(summ.m_strKeywd);
  297.     m_pSummInfo->SetSubject(summ.m_strSubj);
  298.     m_pSummInfo->SetComments(summ.m_strCmt);
  299.     m_pSummInfo->SetTemplate(summ.m_strTempl);
  300.     m_pSummInfo->SetTitle(summ.m_strTitle);
  301.     
  302.     SetModifiedFlag();        
  303. #endif
  304. }
  305.