home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / docmulti.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  6KB  |  236 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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.         pDocument->SetPathName(lpszPathName);
  170.     }
  171.  
  172.     InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
  173.     return pDocument;
  174. }
  175.  
  176. void CMultiDocTemplate::SetDefaultTitle(CDocument* pDocument)
  177. {
  178.     CString strDocName;
  179.     if (GetDocString(strDocName, CDocTemplate::docName) &&
  180.         !strDocName.IsEmpty())
  181.     {
  182.         TCHAR szNum[8];
  183.         wsprintf(szNum, _T("%d"), m_nUntitledCount+1);
  184.         strDocName += szNum;
  185.     }
  186.     else
  187.     {
  188.         // use generic 'untitled' - ignore untitled count
  189.         VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  190.     }
  191.     pDocument->SetTitle(strDocName);
  192. }
  193.  
  194. /////////////////////////////////////////////////////////////////////////////
  195. // CMultiDocTemplate diagnostics
  196.  
  197. #ifdef _DEBUG
  198. void CMultiDocTemplate::Dump(CDumpContext& dc) const
  199. {
  200.     CDocTemplate::Dump(dc);
  201.  
  202.     dc << "m_hMenuShared = " << (UINT)m_hMenuShared;
  203.     dc << "\nm_hAccelTable = " << (UINT)m_hAccelTable;
  204.     dc << "\nm_nUntitledCount = " << m_nUntitledCount;
  205.     dc << "\nwith " << m_docList.GetCount() << " open documents";
  206.     POSITION pos = GetFirstDocPosition();
  207.     while (pos != NULL)
  208.     {
  209.         CDocument* pDoc = GetNextDoc(pos);
  210.         dc << "\nwith document " << (void*)pDoc;
  211.     }
  212.  
  213.     dc << "\n";
  214. }
  215.  
  216. void CMultiDocTemplate::AssertValid() const
  217. {
  218.     CDocTemplate::AssertValid();
  219.  
  220.     POSITION pos = GetFirstDocPosition();
  221.     while (pos != NULL)
  222.     {
  223.         CDocument* pDoc = GetNextDoc(pos);
  224.         ASSERT_VALID(pDoc);
  225.     }
  226. }
  227. #endif //_DEBUG
  228.  
  229. #ifdef AFX_INIT_SEG
  230. #pragma code_seg(AFX_INIT_SEG)
  231. #endif
  232.  
  233. IMPLEMENT_DYNAMIC(CMultiDocTemplate, CDocTemplate)
  234.  
  235. /////////////////////////////////////////////////////////////////////////////
  236.