home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DOCMULTI.CP_ / DOCMULTI.CP
Encoding:
Text File  |  1993-02-08  |  5.7 KB  |  210 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. // CMultiDocTemplate
  24.  
  25. IMPLEMENT_DYNAMIC(CMultiDocTemplate, CDocTemplate)
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMultiDocTemplate construction/destruction
  29.  
  30. CMultiDocTemplate::CMultiDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
  31.     CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  32.         : CDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  33. {
  34.     ASSERT(m_docList.IsEmpty());
  35.     ASSERT(pDocClass != NULL);
  36.  
  37. #ifndef _AFXDLL
  38.     HINSTANCE hInst = AfxGetResourceHandle();
  39. #else
  40.     HINSTANCE hInst = AfxFindResourceHandle(
  41.                 MAKEINTRESOURCE(nIDResource), RT_MENU);
  42. #endif
  43.     m_hMenuShared = ::LoadMenu(hInst, MAKEINTRESOURCE(nIDResource));
  44.     m_hAccelTable = ::LoadAccelerators(hInst, MAKEINTRESOURCE(nIDResource));
  45.     m_nUntitledCount = 0;   // start at 1
  46.  
  47. #ifdef _DEBUG
  48.     // warnings about missing components (don't bother with accelerators)
  49.     if (m_hMenuShared == NULL)
  50.         TRACE1("Warning: no shared menu for document template #%d\n", nIDResource);
  51. #endif //_DEBUG
  52. }
  53.  
  54. CMultiDocTemplate::~CMultiDocTemplate()
  55. {
  56. #ifdef _DEBUG
  57.     if (!m_docList.IsEmpty())
  58.         TRACE1("Warning: destroying CMultiDocTemplate with %d documents alive\n",
  59.             m_docList.GetCount());
  60. #endif
  61.     // delete shared components
  62.     if (m_hMenuShared != NULL)
  63.         ::DestroyMenu(m_hMenuShared);
  64.     if (m_hAccelTable != NULL)
  65.         ::FreeResource((HGLOBAL)m_hAccelTable);
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMultiDocTemplate attributes
  70.  
  71. POSITION CMultiDocTemplate::GetFirstDocPosition() const
  72. {
  73.     return m_docList.GetHeadPosition();
  74. }
  75.  
  76. CDocument* CMultiDocTemplate::GetNextDoc(POSITION& rPos) const
  77. {
  78.     return (CDocument*)m_docList.GetNext(rPos);
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CMultiDocTemplate document management (a list of currently open documents)
  83.  
  84. void CMultiDocTemplate::AddDocument(CDocument* pDoc)
  85. {
  86.     ASSERT_VALID(pDoc);
  87.  
  88.     CDocTemplate::AddDocument(pDoc);
  89.     ASSERT(m_docList.Find(pDoc, NULL) == NULL); // must not be in list
  90.     m_docList.AddTail(pDoc);
  91. }
  92.  
  93.  
  94. void CMultiDocTemplate::RemoveDocument(CDocument* pDoc)
  95. {
  96.     ASSERT_VALID(pDoc);
  97.  
  98.     CDocTemplate::RemoveDocument(pDoc);
  99.     m_docList.RemoveAt(m_docList.Find(pDoc));
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CMultiDocTemplate commands
  104.  
  105.  
  106. CDocument* CMultiDocTemplate::OpenDocumentFile(const char* pszPathName)
  107. {
  108.     CDocument* pDocument = CreateNewDocument();
  109.     if (pDocument == NULL)
  110.     {
  111.         TRACE0("CDocTemplate::CreateNewDocument returned NULL\n");
  112.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  113.         return NULL;
  114.     }
  115.     ASSERT_VALID(pDocument);
  116.  
  117.     BOOL bOldAuto = pDocument->m_bAutoDelete;
  118.     pDocument->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong
  119.     CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);
  120.     pDocument->m_bAutoDelete = bOldAuto;
  121.     if (pFrame == NULL)
  122.     {
  123.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  124.         delete pDocument;       // explicit delete on error
  125.         return NULL;
  126.     }
  127.     ASSERT_VALID(pFrame);
  128.  
  129.     if (pszPathName == NULL)
  130.     {
  131.         // create a new document - with default document name
  132.         UINT nUntitled = m_nUntitledCount + 1;
  133.  
  134.         CString strDocName;
  135.         if (GetDocString(strDocName, CDocTemplate::docName) &&
  136.             !strDocName.IsEmpty())
  137.         {
  138.             char szNum[8];
  139.             wsprintf(szNum, "%d", nUntitled);
  140.             strDocName += szNum;
  141.         }
  142.         else
  143.         {
  144.             // use generic 'untitled' - ignore untitled count
  145.             VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  146.         }
  147.         pDocument->SetTitle(strDocName);
  148.  
  149.         if (!pDocument->OnNewDocument())
  150.         {
  151.             // user has be alerted to what failed in OnNewDocument
  152.             TRACE0("CDocument::OnNewDocument returned FALSE\n");
  153.             pFrame->DestroyWindow();
  154.             return NULL;
  155.         }
  156.  
  157.         // it worked, now bump untitled count
  158.         m_nUntitledCount++;
  159.     }
  160.     else
  161.     {
  162.         // open an existing document
  163.         if (!pDocument->OnOpenDocument(pszPathName))
  164.         {
  165.             // user has be alerted to what failed in OnOpenDocument
  166.             TRACE0("CDocument::OnOpenDocument returned FALSE\n");
  167.             pFrame->DestroyWindow();
  168.             return NULL;
  169.         }
  170.     }
  171.  
  172.     InitialUpdateFrame(pFrame, pDocument);
  173.     return pDocument;
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CMultiDocTemplate diagnostics
  178.  
  179. #ifdef _DEBUG
  180. void CMultiDocTemplate::Dump(CDumpContext& dc) const
  181. {
  182.     CDocTemplate::Dump(dc);
  183.     AFX_DUMP1(dc, "\nm_hMenuShared = ", (UINT)m_hMenuShared);
  184.     AFX_DUMP1(dc, "\nm_hAccelTable = ", (UINT)m_hAccelTable);
  185.     AFX_DUMP1(dc, "\nm_nUntitledCount = ", m_nUntitledCount);
  186.     AFX_DUMP1(dc, "\nwith ", m_docList.GetCount());
  187.     AFX_DUMP0(dc, " open documents");
  188.     POSITION pos = GetFirstDocPosition();
  189.     while (pos)
  190.     {
  191.         CDocument* pDoc = GetNextDoc(pos);
  192.         AFX_DUMP1(dc, "\nwith document ", (void*)pDoc);
  193.     }
  194. }
  195.  
  196. void CMultiDocTemplate::AssertValid() const
  197. {
  198.     CDocTemplate::AssertValid();
  199.  
  200.     POSITION pos = GetFirstDocPosition();
  201.     while (pos)
  202.     {
  203.         CDocument* pDoc = GetNextDoc(pos);
  204.         ASSERT_VALID(pDoc);
  205.     }
  206. }
  207. #endif //_DEBUG
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210.