OneDocTemplate.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 5k
Category:

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "OneDocTemplate.h"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. IMPLEMENT_DYNAMIC(COneDocTemplate, CEGMultiDocTemplate)
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. COneDocTemplate::COneDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
  13. CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  14. : CEGMultiDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  15. {
  16. }
  17. COneDocTemplate::~COneDocTemplate()
  18. {
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. // COneDocTemplate document management (a list of currently open documents)
  22. void 
  23. COneDocTemplate::AddDocument(CDocument* pDoc)
  24. {
  25. ASSERT( m_docList.GetCount() == 0 ); // one at a time please
  26. CEGMultiDocTemplate::AddDocument(pDoc);
  27. }
  28. /////////////////////////////////////////////////////////////////////////////
  29. // COneDocTemplate commands
  30. CDocument* 
  31. COneDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible)
  32. {
  33. CDocument* pDocument = NULL;
  34. CFrameWnd* pFrame = NULL;
  35. BOOL bCreated = FALSE;      // => doc and frame created
  36. BOOL bWasModified = FALSE;
  37. if( m_docList.GetCount() != 0 )
  38. {
  39. // already have a document - reinit it
  40. POSITION pos = GetFirstDocPosition();
  41. pDocument = GetNextDoc(pos);
  42. if (!pDocument->SaveModified())
  43. return NULL;        // leave the original one
  44. // assume 1 frame for the document
  45. pos = pDocument->GetFirstViewPosition();
  46. ASSERT( pos ); // at least 1 view must be exists
  47. CView* pView = pDocument->GetNextView(pos);
  48. ASSERT_VALID(pView);
  49. ASSERT(::IsWindow(pView->m_hWnd));
  50. pFrame = pView->GetParentFrame();
  51. ASSERT(pFrame != NULL);
  52. ASSERT_KINDOF(CFrameWnd, pFrame);
  53. ASSERT_VALID(pFrame);
  54. }
  55. else
  56. {
  57. // create a new document
  58. pDocument = CreateNewDocument();
  59. ASSERT(pFrame == NULL);     // will be created below
  60. bCreated = TRUE;
  61. };
  62. if (pDocument == NULL)
  63. {
  64. TRACE0("CDocTemplate::CreateNewDocument returned NULL.n");
  65. AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  66. return NULL;
  67. }
  68. ASSERT_VALID(pDocument);
  69. if (pFrame == NULL)
  70. {
  71. ASSERT(bCreated);
  72. // create frame - set as main document frame
  73. BOOL bAutoDelete = pDocument->m_bAutoDelete;
  74. pDocument->m_bAutoDelete = FALSE;
  75. // don't destroy if something goes wrong
  76. pFrame = CreateNewFrame(pDocument, NULL);
  77. pDocument->m_bAutoDelete = bAutoDelete;
  78. if (pFrame == NULL)
  79. {
  80. AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  81. delete pDocument;       // explicit delete on error
  82. return NULL;
  83. }
  84. ASSERT_VALID(pFrame);
  85. };
  86. if (lpszPathName == NULL)
  87. {
  88. // create a new document - with default document name
  89. SetDefaultTitle(pDocument);
  90. // avoid creating temporary compound file when starting up invisible
  91. if (!bMakeVisible)
  92. pDocument->m_bEmbedded = TRUE;
  93. if (!pDocument->OnNewDocument())
  94. {
  95. // user has been alerted to what failed in OnNewDocument
  96. TRACE0("CDocument::OnNewDocument returned FALSE.n");
  97. if (bCreated)
  98. {
  99. pFrame->DestroyWindow(); // will destroy document
  100. };
  101. return NULL;
  102. }
  103. // it worked, now bump untitled count
  104. //m_nUntitledCount++; // no need to count untitled docs!
  105. }
  106. else
  107. {
  108. // open an existing document
  109. CWaitCursor wait;
  110. bWasModified = pDocument->IsModified();
  111. pDocument->SetModifiedFlag(FALSE);  // not dirty for open
  112. if (!pDocument->OnOpenDocument(lpszPathName))
  113. {
  114. // user has be alerted to what failed in OnOpenDocument
  115. TRACE0("CDocument::OnOpenDocument returned FALSE.n");
  116. if (bCreated)
  117. {
  118. pFrame->DestroyWindow();    // will destroy document
  119. }
  120. else if (!pDocument->IsModified())
  121. {
  122. // original document is untouched
  123. pDocument->SetModifiedFlag(bWasModified);
  124. }
  125. else
  126. {
  127. // we corrupted the original document
  128. SetDefaultTitle(pDocument);
  129. if (!pDocument->OnNewDocument())
  130. {
  131. TRACE0("Error: OnNewDocument failed after trying to open a document - trying to continue.n");
  132. // assume we can continue
  133. }
  134. }
  135. return NULL;        // open failed
  136. }
  137. pDocument->SetPathName(lpszPathName);
  138. }
  139. InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
  140. return pDocument;
  141. }
  142. void 
  143. COneDocTemplate::SetDefaultTitle(CDocument* pDocument)
  144. {
  145. CString strDocName;
  146. if (!GetDocString(strDocName, CDocTemplate::docName) ||
  147. strDocName.IsEmpty())
  148. {
  149. // use generic 'untitled'
  150. VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  151. }
  152. pDocument->SetTitle(strDocName);
  153. }
  154. /////////////////////////////////////////////////////////////////////////////
  155. // COneDocTemplate diagnostics
  156. #ifdef _DEBUG
  157. void 
  158. COneDocTemplate::AssertValid() const
  159. {
  160. CEGMultiDocTemplate::AssertValid();
  161. ASSERT( m_docList.GetCount() <= 1 );
  162. }
  163. #endif //_DEBUG