home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DOCTEMPL.CP_ / DOCTEMPL.CP
Encoding:
Text File  |  1993-02-08  |  7.2 KB  |  267 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. // CDocTemplate
  24.  
  25. IMPLEMENT_DYNAMIC(CDocTemplate, CCmdTarget)
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CDocTemplate construction/destruction
  29.  
  30. CDocTemplate::CDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
  31.     CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  32. {
  33.     ASSERT_VALID_IDR(nIDResource);
  34.     ASSERT(pDocClass != NULL);
  35.     ASSERT(pFrameClass != NULL);
  36.  
  37.     m_nIDResource = nIDResource;
  38.     m_pDocClass = pDocClass;
  39.     m_pFrameClass = pFrameClass;
  40.     m_pViewClass = pViewClass;
  41.     m_pAttachedServer = NULL;
  42.  
  43.     if (!m_strDocStrings.LoadString(m_nIDResource))
  44.         TRACE1("Warning: no document names in string for template #%d\n",
  45.             nIDResource);
  46. }
  47.  
  48. CDocTemplate::~CDocTemplate()
  49. {
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CDocTemplate attributes
  54.  
  55. BOOL CDocTemplate::GetDocString(CString& rString, enum DocStringIndex i) const
  56. {
  57.     return AfxExtractSubString(rString, m_strDocStrings, (int)i);
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // Document management
  62.  
  63. void CDocTemplate::AddDocument(CDocument* pDoc)
  64. {
  65.     ASSERT_VALID(pDoc);
  66.     ASSERT(pDoc->m_pDocTemplate == NULL);   // no template attached yet
  67.     pDoc->m_pDocTemplate = this;
  68. }
  69.  
  70. void CDocTemplate::RemoveDocument(CDocument* pDoc)
  71. {
  72.     ASSERT_VALID(pDoc);
  73.     ASSERT(pDoc->m_pDocTemplate == this);   // must be attached to us
  74.     pDoc->m_pDocTemplate = NULL;
  75. }
  76.  
  77. CDocTemplate::Confidence CDocTemplate::MatchDocType(const char* pszPathName,
  78.     CDocument*& rpDocMatch)
  79. {
  80.     ASSERT(pszPathName != NULL);
  81.     rpDocMatch = NULL;
  82.  
  83.     // go through all documents
  84.     POSITION pos = GetFirstDocPosition();
  85.     while (pos)
  86.     {
  87.         CDocument* pDoc = GetNextDoc(pos);
  88.         if (pDoc->GetPathName() == pszPathName)
  89.         {
  90.             // already open
  91.             rpDocMatch = pDoc;
  92.             return yesAlreadyOpen;
  93.         }
  94.     }
  95.  
  96.     // see if it matches our default suffix
  97.     CString strFilterExt;
  98.     if (GetDocString(strFilterExt, CDocTemplate::filterExt) &&
  99.       !strFilterExt.IsEmpty())
  100.     {
  101.         // see if extension matches
  102.         ASSERT(strFilterExt[0] == '.');
  103.         LPSTR lpszDot = _AfxStrChr(pszPathName, '.');
  104.         if (lpszDot != NULL && lstrcmpi(lpszDot, strFilterExt) == 0)
  105.             return yesAttemptNative; // extension matches, looks like ours
  106.     }
  107.  
  108.     // otherwise we will guess it may work
  109.     return yesAttemptForeign;
  110. }
  111.  
  112.  
  113. CDocument* CDocTemplate::CreateNewDocument()
  114. {
  115.     // default implementation constructs one from CRuntimeClass
  116.     if (m_pDocClass == NULL)
  117.     {
  118.         TRACE0("Error: you must override CDocTemplate::CreateNewDocument\n");
  119.         ASSERT(FALSE);
  120.         return NULL;
  121.     }
  122.     CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject();
  123.     if (pDocument == NULL)
  124.     {
  125.         TRACE1("Warning: Dynamic create of document type %Fs failed\n",
  126.             m_pDocClass->m_lpszClassName);
  127.         return NULL;
  128.     }
  129.     ASSERT(pDocument->IsKindOf(RUNTIME_CLASS(CDocument)));
  130.     AddDocument(pDocument);
  131.     return pDocument;
  132. }
  133.  
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // Default frame creation
  137.  
  138. CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)
  139. {
  140.     ASSERT_VALID(pDoc);
  141.     // create a frame wired to the specified document
  142.  
  143.     ASSERT(m_nIDResource != 0); // must have a resource ID to load from
  144.     CCreateContext context;
  145.     context.m_pCurrentFrame = pOther;
  146.     context.m_pCurrentDoc = pDoc;
  147.     context.m_pNewViewClass = m_pViewClass;
  148.     context.m_pNewDocTemplate = this;
  149.  
  150.     if (m_pFrameClass == NULL)
  151.     {
  152.         TRACE0("Error: you must override CDocTemplate::CreateNewFrame\n");
  153.         ASSERT(FALSE);
  154.         return NULL;
  155.     }
  156.     CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();
  157.     if (pFrame == NULL)
  158.     {
  159.         TRACE1("Warning: Dynamic create of frame %Fs failed\n",
  160.             m_pFrameClass->m_lpszClassName);
  161.         return NULL;
  162.     }
  163.     ASSERT(pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd)));
  164.  
  165.     if (context.m_pNewViewClass == NULL)
  166.         TRACE0("Warning: creating frame with no default view\n");
  167.  
  168.     // create new from resource
  169.     if (!pFrame->LoadFrame(m_nIDResource,
  170.             WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles
  171.             NULL, &context))
  172.     {
  173.         TRACE0("Warning: CDocTemplate couldn't create a frame\n");
  174.         // frame will be deleted in PostNcDestroy cleanup
  175.         return NULL;
  176.     }
  177.  
  178.     // it worked !
  179.     return pFrame;
  180. }
  181.  
  182. void CDocTemplate::InitialUpdateFrame(CFrameWnd* pFrame, CDocument* pDoc)
  183. {
  184.     // if the frame does not have an active view, set to first pane
  185.     if (pFrame->GetActiveView() == NULL)
  186.     {
  187.         CWnd* pWnd = pFrame->GetDescendantWindow(AFX_IDW_PANE_FIRST);
  188.         if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CView)))
  189.             pFrame->SetActiveView((CView*)pWnd);
  190.     }
  191.  
  192.     // send initial update to all views (and other controls) in the frame
  193.     pFrame->SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE);
  194.  
  195.     // finally, activate the frame
  196.     // (send the default show command unless the main desktop window)
  197.     int nCmdShow = -1;      // default
  198.     if (pFrame == AfxGetApp()->m_pMainWnd)
  199.         nCmdShow = AfxGetApp()->m_nCmdShow; // use the parameter from WinMain
  200.     pFrame->ActivateFrame(nCmdShow);
  201.  
  202.     // now that the frame is visible - update frame counts
  203.     pDoc->UpdateFrameCounts();
  204.     pFrame->OnUpdateFrameTitle(TRUE);
  205.  
  206.     // at this point the frame should be properly linked to the document
  207.     ASSERT(pFrame->GetActiveDocument() == pDoc);
  208. }
  209.  
  210. /////////////////////////////////////////////////////////////////////////////
  211. // CDocTemplate commands and command helpers
  212.  
  213. BOOL CDocTemplate::SaveAllModified()
  214. {
  215.     POSITION pos = GetFirstDocPosition();
  216.     while (pos)
  217.     {
  218.         CDocument* pDoc = GetNextDoc(pos);
  219.         if (pDoc->IsModified() && !pDoc->SaveModified())
  220.             return FALSE;
  221.     }
  222.     return TRUE;
  223. }
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CDocTemplate diagnostics
  227.  
  228. #ifdef _DEBUG
  229. void CDocTemplate::Dump(CDumpContext& dc) const
  230. {
  231.     CCmdTarget::Dump(dc);
  232.  
  233.     AFX_DUMP1(dc, "\nm_nIDResource = ", m_nIDResource);
  234.     AFX_DUMP1(dc, "\nm_strDocStrings: ", m_strDocStrings);
  235.     if (m_pDocClass)
  236.         AFX_DUMP1(dc, "\nm_pDocClass = ", m_pDocClass->m_lpszClassName);
  237.     else
  238.         AFX_DUMP0(dc, "\nm_pDocClass = NULL");
  239.  
  240.     if (dc.GetDepth() > 0)
  241.     {
  242.         AFX_DUMP0(dc, "\ndocument list = {");
  243.         POSITION pos = GetFirstDocPosition();
  244.         while (pos)
  245.         {
  246.             CDocument* pDoc = GetNextDoc(pos);
  247.             AFX_DUMP1(dc, "\ndocument ", pDoc);
  248.         }
  249.         AFX_DUMP0(dc, "\n}");
  250.     }
  251. }
  252.  
  253. void CDocTemplate::AssertValid() const
  254. {
  255.     CCmdTarget::AssertValid();
  256.  
  257.     POSITION pos = GetFirstDocPosition();
  258.     while (pos)
  259.     {
  260.         CDocument* pDoc = GetNextDoc(pos);
  261.         ASSERT_VALID(pDoc);
  262.     }
  263. }
  264. #endif //_DEBUG
  265.  
  266. /////////////////////////////////////////////////////////////////////////////
  267.