home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DOCSINGL.CP_ / DOCSINGL.CP
Encoding:
Text File  |  1993-02-08  |  6.3 KB  |  240 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 Microsoft Corporation 
  3. // All rights reserved. 
  4. //  
  5. // This source code is only intended as a supplement to the 
  6. // Microsoft Foundation Classes Reference and Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE2_SEG
  14. #pragma code_seg(AFX_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CSingleDocTemplate
  24.  
  25. IMPLEMENT_DYNAMIC(CSingleDocTemplate, CDocTemplate)
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CSingleDocTemplate construction/destruction
  29.  
  30. CSingleDocTemplate::CSingleDocTemplate(UINT nIDResource,
  31.     CRuntimeClass* pDocClass,
  32.     CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  33.         : CDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  34. {
  35.     m_pOnlyDoc = NULL;
  36. }
  37.  
  38. CSingleDocTemplate::~CSingleDocTemplate()
  39. {
  40. #ifdef _DEBUG
  41.     if (m_pOnlyDoc != NULL)
  42.         TRACE0("Warning: destroying CSingleDocTemplate with live document\n");
  43. #endif
  44. }
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CSingleDocTemplate attributes
  48.  
  49. POSITION CSingleDocTemplate::GetFirstDocPosition() const
  50. {
  51.     return (m_pOnlyDoc == NULL) ? NULL : BEFORE_START_POSITION;
  52. }
  53.  
  54. CDocument* CSingleDocTemplate::GetNextDoc(POSITION& rPos) const
  55. {
  56.     CDocument* pDoc = NULL;
  57.     if (rPos == BEFORE_START_POSITION)
  58.     {
  59.         // first time through, return a real document
  60.         ASSERT(m_pOnlyDoc != NULL);
  61.         pDoc = m_pOnlyDoc;
  62.     }
  63.     rPos = NULL;        // no more
  64.     return pDoc;
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CSingleDocTemplate document management (a list of currently open documents)
  69.  
  70. void CSingleDocTemplate::AddDocument(CDocument* pDoc)
  71. {
  72.     ASSERT(m_pOnlyDoc == NULL);     // one at a time please
  73.     ASSERT_VALID(pDoc);
  74.  
  75.     CDocTemplate::AddDocument(pDoc);
  76.     m_pOnlyDoc = pDoc;
  77. }
  78.  
  79. void CSingleDocTemplate::RemoveDocument(CDocument* pDoc)
  80. {
  81.     ASSERT(m_pOnlyDoc == pDoc);     // must be this one
  82.     ASSERT_VALID(pDoc);
  83.  
  84.     CDocTemplate::RemoveDocument(pDoc);
  85.     m_pOnlyDoc = NULL;
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CSingleDocTemplate commands
  90.  
  91. CDocument* CSingleDocTemplate::OpenDocumentFile(const char* pszPathName)
  92.     // if pszPathName == NULL => create new file of this type
  93. {
  94.     CDocument* pDocument = NULL;
  95.     CFrameWnd* pFrame = NULL;
  96.     BOOL bCreated = FALSE;      // => doc and frame created
  97.     BOOL bWasModified = FALSE;
  98.  
  99.     if (m_pOnlyDoc != NULL) 
  100.     {
  101.         // already have a document - reinit it
  102.         pDocument = m_pOnlyDoc;
  103.         if (!pDocument->SaveModified())
  104.             return NULL;        // leave the original one
  105.  
  106.         pFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd;
  107.         ASSERT(pFrame != NULL);
  108.         ASSERT(pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd)));
  109.         ASSERT_VALID(pFrame);
  110.     }
  111.     else
  112.     {
  113.         // create a new document
  114.         pDocument = CreateNewDocument();
  115.         ASSERT(pFrame == NULL);     // will be created below
  116.         bCreated = TRUE;
  117.     }
  118.  
  119.     if (pDocument == NULL)
  120.     {
  121.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  122.         return NULL;
  123.     }
  124.     ASSERT(pDocument == m_pOnlyDoc);
  125.  
  126.     if (pFrame == NULL)
  127.     {
  128.         ASSERT(bCreated);
  129.  
  130.         // create frame - set as main document frame
  131.         BOOL bOldAuto = pDocument->m_bAutoDelete;
  132.         pDocument->m_bAutoDelete = FALSE;
  133.                     // don't destroy if something goes wrong
  134.         pFrame = CreateNewFrame(pDocument, NULL);
  135.         pDocument->m_bAutoDelete = bOldAuto;
  136.         if (pFrame == NULL)
  137.         {
  138.             AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  139.             delete pDocument;       // explicit delete on error
  140.             return NULL;
  141.         }
  142.     }
  143.  
  144.     if (pszPathName == NULL)
  145.     {
  146.         // create a new document
  147.         CString strDocName;
  148.         if (!GetDocString(strDocName, CDocTemplate::docName) ||
  149.             strDocName.IsEmpty())
  150.         {
  151.             // use generic 'untitled'
  152.             VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  153.         }
  154.         pDocument->SetTitle(strDocName);
  155.  
  156.         if (!pDocument->OnNewDocument())
  157.         {
  158.             // user has been alerted to what failed in OnNewDocument
  159.             TRACE0("CDocument::OnNewDocument returned FALSE\n");
  160.             if (bCreated)
  161.                 pFrame->DestroyWindow();    // will destroy document
  162.             return NULL;
  163.         }
  164.     }
  165.     else
  166.     {
  167.         // open an existing document
  168.         bWasModified = pDocument->IsModified();
  169.         pDocument->SetModifiedFlag(FALSE);  // not dirty for open
  170.  
  171.         if (!pDocument->OnOpenDocument(pszPathName))
  172.         {
  173.             // user has been alerted to what failed in OnOpenDocument
  174.             TRACE0("CDocument::OnOpenDocument returned FALSE\n");
  175.             if (bCreated)
  176.             {
  177.                 pFrame->DestroyWindow();    // will destroy document
  178.             }
  179.             else if (!pDocument->IsModified())
  180.             {
  181.                 // original document is untouched
  182.                 pDocument->SetModifiedFlag(bWasModified);
  183.             }
  184.             else
  185.             {
  186.                 // we corrupted the original document
  187.                 CString strDocName;
  188.                 if (!GetDocString(strDocName, CDocTemplate::docName) ||
  189.                     strDocName.IsEmpty())
  190.                 {
  191.                     // use generic 'untitled'
  192.                     VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  193.                 }
  194.                 pDocument->SetTitle(strDocName);
  195.  
  196.                 if (!pDocument->OnNewDocument())
  197.                 {
  198.                     TRACE0("Error: OnNewDocument failed after failing to"
  199.                         " open a document - trying to continue\n");
  200.                     // assume we can continue
  201.                 }
  202.             }
  203.             return NULL;        // open failed
  204.         }
  205.     }
  206.  
  207.     if (bCreated && AfxGetApp()->m_pMainWnd == NULL)
  208.     {
  209.         // set as main frame
  210.         AfxGetApp()->m_pMainWnd = pFrame;
  211.         // InitialUpdateFrame will show the window in the correct mode
  212.     }
  213.  
  214.     InitialUpdateFrame(pFrame, pDocument);
  215.     return pDocument;
  216. }
  217.  
  218. /////////////////////////////////////////////////////////////////////////////
  219. // CSingleDocTemplate diagnostics
  220.  
  221. #ifdef _DEBUG
  222. void CSingleDocTemplate::Dump(CDumpContext& dc) const
  223. {
  224.     CDocTemplate::Dump(dc);
  225.     if (m_pOnlyDoc)
  226.         AFX_DUMP1(dc, "\nwith document: ", (void*)m_pOnlyDoc);
  227.     else
  228.         AFX_DUMP0(dc, "\nwith no document");
  229. }
  230.  
  231. void CSingleDocTemplate::AssertValid() const
  232. {
  233.     CDocTemplate::AssertValid();
  234.     if (m_pOnlyDoc)
  235.         ASSERT_VALID(m_pOnlyDoc);
  236. }
  237. #endif //_DEBUG
  238.  
  239. /////////////////////////////////////////////////////////////////////////////
  240.