home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DOCMULTI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.5 KB  |  246 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. // CMultiDocTemplate construction/destruction
  26.  
  27. CMultiDocTemplate::CMultiDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
  28.     CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  29.         : CDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  30. {
  31.     ASSERT(m_docList.IsEmpty());
  32.  
  33.     m_hMenuShared = NULL;
  34.     m_hAccelTable = NULL;
  35.     m_nUntitledCount = 0;   // start at 1
  36.  
  37.     // load resources in constructor if not statically allocated
  38.     if (!CDocManager::bStaticInit)
  39.         LoadTemplate();
  40. }
  41.  
  42. void CMultiDocTemplate::LoadTemplate()
  43. {
  44.     CDocTemplate::LoadTemplate();
  45.  
  46.     if (m_nIDResource != 0 && m_hMenuShared == NULL)
  47.     {
  48.         HINSTANCE hInst = AfxFindResourceHandle(
  49.             MAKEINTRESOURCE(m_nIDResource), RT_MENU);
  50.         m_hMenuShared = ::LoadMenu(hInst, MAKEINTRESOURCE(m_nIDResource));
  51.         m_hAccelTable =
  52.             ::LoadAccelerators(hInst, MAKEINTRESOURCE(m_nIDResource));
  53.     }
  54.  
  55. #ifdef _DEBUG
  56.     // warnings about missing components (don't bother with accelerators)
  57.     if (m_hMenuShared == NULL)
  58.         TRACE1("Warning: no shared menu for document template #%d.\n",
  59.             m_nIDResource);
  60. #endif //_DEBUG
  61. }
  62.  
  63. CMultiDocTemplate::~CMultiDocTemplate()
  64. {
  65. #ifdef _DEBUG
  66.     if (!m_docList.IsEmpty())
  67.         TRACE1("Warning: destroying CMultiDocTemplate with %d documents alive.\n",
  68.             m_docList.GetCount());
  69. #endif
  70.     // delete shared components
  71.     if (m_hMenuShared != NULL)
  72.         ::DestroyMenu(m_hMenuShared);
  73.     if (m_hAccelTable != NULL)
  74.         ::FreeResource((HGLOBAL)m_hAccelTable);
  75. }
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CMultiDocTemplate attributes
  79.  
  80. POSITION CMultiDocTemplate::GetFirstDocPosition() const
  81. {
  82.     return m_docList.GetHeadPosition();
  83. }
  84.  
  85. CDocument* CMultiDocTemplate::GetNextDoc(POSITION& rPos) const
  86. {
  87.     return (CDocument*)m_docList.GetNext(rPos);
  88. }
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CMultiDocTemplate document management (a list of currently open documents)
  92.  
  93. void CMultiDocTemplate::AddDocument(CDocument* pDoc)
  94. {
  95.     ASSERT_VALID(pDoc);
  96.  
  97.     CDocTemplate::AddDocument(pDoc);
  98.     ASSERT(m_docList.Find(pDoc, NULL) == NULL); // must not be in list
  99.     m_docList.AddTail(pDoc);
  100. }
  101.  
  102.  
  103. void CMultiDocTemplate::RemoveDocument(CDocument* pDoc)
  104. {
  105.     ASSERT_VALID(pDoc);
  106.  
  107.     CDocTemplate::RemoveDocument(pDoc);
  108.     m_docList.RemoveAt(m_docList.Find(pDoc));
  109. }
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CMultiDocTemplate commands
  113.  
  114. CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
  115.     BOOL bMakeVisible)
  116. {
  117.     CDocument* pDocument = CreateNewDocument();
  118.     if (pDocument == NULL)
  119.     {
  120.         TRACE0("CDocTemplate::CreateNewDocument returned NULL.\n");
  121.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  122.         return NULL;
  123.     }
  124.     ASSERT_VALID(pDocument);
  125.  
  126.     BOOL bAutoDelete = pDocument->m_bAutoDelete;
  127.     pDocument->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong
  128.     CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);
  129.     pDocument->m_bAutoDelete = bAutoDelete;
  130.     if (pFrame == NULL)
  131.     {
  132.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  133.         delete pDocument;       // explicit delete on error
  134.         return NULL;
  135.     }
  136.     ASSERT_VALID(pFrame);
  137.  
  138.     if (lpszPathName == NULL)
  139.     {
  140.         // create a new document - with default document name
  141.         SetDefaultTitle(pDocument);
  142.  
  143.         // avoid creating temporary compound file when starting up invisible
  144.         if (!bMakeVisible)
  145.             pDocument->m_bEmbedded = TRUE;
  146.  
  147.         if (!pDocument->OnNewDocument())
  148.         {
  149.             // user has be alerted to what failed in OnNewDocument
  150.             TRACE0("CDocument::OnNewDocument returned FALSE.\n");
  151.             pFrame->DestroyWindow();
  152.             return NULL;
  153.         }
  154.  
  155.         // it worked, now bump untitled count
  156.         m_nUntitledCount++;
  157.     }
  158.     else
  159.     {
  160.         // open an existing document
  161.         CWaitCursor wait;
  162.         if (!pDocument->OnOpenDocument(lpszPathName))
  163.         {
  164.             // user has be alerted to what failed in OnOpenDocument
  165.             TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
  166.             pFrame->DestroyWindow();
  167.             return NULL;
  168.         }
  169. #ifdef _MAC
  170.         // if the document is dirty, we must have opened a stationery pad
  171.         //  - don't change the pathname because we want to treat the document
  172.         //  as untitled
  173.         if (!pDocument->IsModified())
  174. #endif
  175.             pDocument->SetPathName(lpszPathName);
  176.     }
  177.  
  178.     InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
  179.     return pDocument;
  180. }
  181.  
  182. void CMultiDocTemplate::SetDefaultTitle(CDocument* pDocument)
  183. {
  184.     CString strDocName;
  185.     if (GetDocString(strDocName, CDocTemplate::docName) &&
  186.         !strDocName.IsEmpty())
  187.     {
  188.         TCHAR szNum[8];
  189. #ifndef _MAC
  190.         wsprintf(szNum, _T("%d"), m_nUntitledCount+1);
  191. #else
  192.         wsprintf(szNum, _T(" %d"), m_nUntitledCount+1);
  193. #endif
  194.         strDocName += szNum;
  195.     }
  196.     else
  197.     {
  198.         // use generic 'untitled' - ignore untitled count
  199.         VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  200.     }
  201.     pDocument->SetTitle(strDocName);
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // CMultiDocTemplate diagnostics
  206.  
  207. #ifdef _DEBUG
  208. void CMultiDocTemplate::Dump(CDumpContext& dc) const
  209. {
  210.     CDocTemplate::Dump(dc);
  211.  
  212.     dc << "m_hMenuShared = " << (UINT)m_hMenuShared;
  213.     dc << "\nm_hAccelTable = " << (UINT)m_hAccelTable;
  214.     dc << "\nm_nUntitledCount = " << m_nUntitledCount;
  215.     dc << "\nwith " << m_docList.GetCount() << " open documents";
  216.     POSITION pos = GetFirstDocPosition();
  217.     while (pos != NULL)
  218.     {
  219.         CDocument* pDoc = GetNextDoc(pos);
  220.         dc << "\nwith document " << (void*)pDoc;
  221.     }
  222.  
  223.     dc << "\n";
  224. }
  225.  
  226. void CMultiDocTemplate::AssertValid() const
  227. {
  228.     CDocTemplate::AssertValid();
  229.  
  230.     POSITION pos = GetFirstDocPosition();
  231.     while (pos != NULL)
  232.     {
  233.         CDocument* pDoc = GetNextDoc(pos);
  234.         ASSERT_VALID(pDoc);
  235.     }
  236. }
  237. #endif //_DEBUG
  238.  
  239. #ifdef AFX_INIT_SEG
  240. #pragma code_seg(AFX_INIT_SEG)
  241. #endif
  242.  
  243. IMPLEMENT_DYNAMIC(CMultiDocTemplate, CDocTemplate)
  244.  
  245. /////////////////////////////////////////////////////////////////////////////
  246.