home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DOCSINGL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.5 KB  |  256 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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 related
  7. // electronic 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 THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CSingleDocTemplate construction/destruction
  26.  
  27. CSingleDocTemplate::CSingleDocTemplate(UINT nIDResource,
  28.     CRuntimeClass* pDocClass, CRuntimeClass* pFrameClass,
  29.     CRuntimeClass* pViewClass)
  30.         : CDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  31. {
  32.     m_pOnlyDoc = NULL;
  33. }
  34.  
  35. CSingleDocTemplate::~CSingleDocTemplate()
  36. {
  37. #ifdef _DEBUG
  38.     if (m_pOnlyDoc != NULL)
  39.         TRACE0("Warning: destroying CSingleDocTemplate with live document.\n");
  40. #endif
  41. }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSingleDocTemplate attributes
  45.  
  46. POSITION CSingleDocTemplate::GetFirstDocPosition() const
  47. {
  48.     return (m_pOnlyDoc == NULL) ? NULL : BEFORE_START_POSITION;
  49. }
  50.  
  51. CDocument* CSingleDocTemplate::GetNextDoc(POSITION& rPos) const
  52. {
  53.     CDocument* pDoc = NULL;
  54.     if (rPos == BEFORE_START_POSITION)
  55.     {
  56.         // first time through, return a real document
  57.         ASSERT(m_pOnlyDoc != NULL);
  58.         pDoc = m_pOnlyDoc;
  59.     }
  60.     rPos = NULL;        // no more
  61.     return pDoc;
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CSingleDocTemplate document management (a list of currently open documents)
  66.  
  67. void CSingleDocTemplate::AddDocument(CDocument* pDoc)
  68. {
  69.     ASSERT(m_pOnlyDoc == NULL);     // one at a time please
  70.     ASSERT_VALID(pDoc);
  71.  
  72.     CDocTemplate::AddDocument(pDoc);
  73.     m_pOnlyDoc = pDoc;
  74. }
  75.  
  76. void CSingleDocTemplate::RemoveDocument(CDocument* pDoc)
  77. {
  78.     ASSERT(m_pOnlyDoc == pDoc);     // must be this one
  79.     ASSERT_VALID(pDoc);
  80.  
  81.     CDocTemplate::RemoveDocument(pDoc);
  82.     m_pOnlyDoc = NULL;
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CSingleDocTemplate commands
  87.  
  88. CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
  89.     BOOL bMakeVisible)
  90.     // if lpszPathName == NULL => create new file of this type
  91. {
  92.     CDocument* pDocument = NULL;
  93.     CFrameWnd* pFrame = NULL;
  94.     BOOL bCreated = FALSE;      // => doc and frame created
  95.     BOOL bWasModified = FALSE;
  96.  
  97.     if (m_pOnlyDoc != NULL)
  98.     {
  99.         // already have a document - reinit it
  100.         pDocument = m_pOnlyDoc;
  101.         if (!pDocument->SaveModified())
  102.             return NULL;        // leave the original one
  103.  
  104.         pFrame = (CFrameWnd*)AfxGetMainWnd();
  105.         ASSERT(pFrame != NULL);
  106.         ASSERT_KINDOF(CFrameWnd, pFrame);
  107.         ASSERT_VALID(pFrame);
  108.     }
  109.     else
  110.     {
  111.         // create a new document
  112.         pDocument = CreateNewDocument();
  113.         ASSERT(pFrame == NULL);     // will be created below
  114.         bCreated = TRUE;
  115.     }
  116.  
  117.     if (pDocument == NULL)
  118.     {
  119.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  120.         return NULL;
  121.     }
  122.     ASSERT(pDocument == m_pOnlyDoc);
  123.  
  124.     if (pFrame == NULL)
  125.     {
  126.         ASSERT(bCreated);
  127.  
  128.         // create frame - set as main document frame
  129.         BOOL bAutoDelete = pDocument->m_bAutoDelete;
  130.         pDocument->m_bAutoDelete = FALSE;
  131.                     // don't destroy if something goes wrong
  132.         pFrame = CreateNewFrame(pDocument, NULL);
  133.         pDocument->m_bAutoDelete = bAutoDelete;
  134.         if (pFrame == NULL)
  135.         {
  136.             AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  137.             delete pDocument;       // explicit delete on error
  138.             return NULL;
  139.         }
  140.     }
  141.  
  142.     if (lpszPathName == NULL)
  143.     {
  144.         // create a new document
  145.         SetDefaultTitle(pDocument);
  146.  
  147.         // avoid creating temporary compound file when starting up invisible
  148.         if (!bMakeVisible)
  149.             pDocument->m_bEmbedded = TRUE;
  150.  
  151.         if (!pDocument->OnNewDocument())
  152.         {
  153.             // user has been alerted to what failed in OnNewDocument
  154.             TRACE0("CDocument::OnNewDocument returned FALSE.\n");
  155.             if (bCreated)
  156.                 pFrame->DestroyWindow();    // will destroy document
  157.             return NULL;
  158.         }
  159.     }
  160.     else
  161.     {
  162.         CWaitCursor wait;
  163.  
  164.         // open an existing document
  165.         bWasModified = pDocument->IsModified();
  166.         pDocument->SetModifiedFlag(FALSE);  // not dirty for open
  167.  
  168.         if (!pDocument->OnOpenDocument(lpszPathName))
  169.         {
  170.             // user has been alerted to what failed in OnOpenDocument
  171.             TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
  172.             if (bCreated)
  173.             {
  174.                 pFrame->DestroyWindow();    // will destroy document
  175.             }
  176.             else if (!pDocument->IsModified())
  177.             {
  178.                 // original document is untouched
  179.                 pDocument->SetModifiedFlag(bWasModified);
  180.             }
  181.             else
  182.             {
  183.                 // we corrupted the original document
  184.                 SetDefaultTitle(pDocument);
  185.  
  186.                 if (!pDocument->OnNewDocument())
  187.                 {
  188.                     TRACE0("Error: OnNewDocument failed after trying to open a document - trying to continue.\n");
  189.                     // assume we can continue
  190.                 }
  191.             }
  192.             return NULL;        // open failed
  193.         }
  194. #ifdef _MAC
  195.         // if the document is dirty, we must have opened a stationery pad - don't
  196.         // change the pathname because we want to treat the document as untitled
  197.         if (!pDocument->IsModified())
  198. #endif
  199.             pDocument->SetPathName(lpszPathName);
  200.     }
  201.  
  202.     CWinThread* pThread = AfxGetThread();
  203.     if (bCreated && pThread->m_pMainWnd == NULL)
  204.     {
  205.         // set as main frame (InitialUpdateFrame will show the window)
  206.         pThread->m_pMainWnd = pFrame;
  207.     }
  208.     InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
  209.  
  210.     return pDocument;
  211. }
  212.  
  213. void CSingleDocTemplate::SetDefaultTitle(CDocument* pDocument)
  214. {
  215.     CString strDocName;
  216.     if (!GetDocString(strDocName, CDocTemplate::docName) ||
  217.         strDocName.IsEmpty())
  218.     {
  219.         // use generic 'untitled'
  220.         VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  221.     }
  222.     pDocument->SetTitle(strDocName);
  223. }
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CSingleDocTemplate diagnostics
  227.  
  228. #ifdef _DEBUG
  229. void CSingleDocTemplate::Dump(CDumpContext& dc) const
  230. {
  231.     CDocTemplate::Dump(dc);
  232.  
  233.     if (m_pOnlyDoc)
  234.         dc << "with document: " << (void*)m_pOnlyDoc;
  235.     else
  236.         dc << "with no document";
  237.  
  238.     dc << "\n";
  239. }
  240.  
  241. void CSingleDocTemplate::AssertValid() const
  242. {
  243.     CDocTemplate::AssertValid();
  244.     if (m_pOnlyDoc)
  245.         ASSERT_VALID(m_pOnlyDoc);
  246. }
  247. #endif //_DEBUG
  248.  
  249. #ifdef AFX_INIT_SEG
  250. #pragma code_seg(AFX_INIT_SEG)
  251. #endif
  252.  
  253. IMPLEMENT_DYNAMIC(CSingleDocTemplate, CDocTemplate)
  254.  
  255. /////////////////////////////////////////////////////////////////////////////
  256.