docsingl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include "stdafx.h"
  11. #ifdef AFX_CORE2_SEG
  12. #pragma code_seg(AFX_CORE2_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CSingleDocTemplate construction/destruction
  21. CSingleDocTemplate::CSingleDocTemplate(UINT nIDResource,
  22. CRuntimeClass* pDocClass, CRuntimeClass* pFrameClass,
  23. CRuntimeClass* pViewClass)
  24. : CDocTemplate(nIDResource, pDocClass, pFrameClass, pViewClass)
  25. {
  26. m_pOnlyDoc = NULL;
  27. }
  28. CSingleDocTemplate::~CSingleDocTemplate()
  29. {
  30. #ifdef _DEBUG
  31. if (m_pOnlyDoc != NULL)
  32. TRACE0("Warning: destroying CSingleDocTemplate with live document.\n");
  33. #endif
  34. }
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CSingleDocTemplate attributes
  37. POSITION CSingleDocTemplate::GetFirstDocPosition() const
  38. {
  39. return (m_pOnlyDoc == NULL) ? NULL : BEFORE_START_POSITION;
  40. }
  41. CDocument* CSingleDocTemplate::GetNextDoc(POSITION& rPos) const
  42. {
  43. CDocument* pDoc = NULL;
  44. if (rPos == BEFORE_START_POSITION)
  45. {
  46. // first time through, return a real document
  47. ASSERT(m_pOnlyDoc != NULL);
  48. pDoc = m_pOnlyDoc;
  49. }
  50. rPos = NULL; // no more
  51. return pDoc;
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CSingleDocTemplate document management (a list of currently open documents)
  55. void CSingleDocTemplate::AddDocument(CDocument* pDoc)
  56. {
  57. ASSERT(m_pOnlyDoc == NULL); // one at a time please
  58. ASSERT_VALID(pDoc);
  59. CDocTemplate::AddDocument(pDoc);
  60. m_pOnlyDoc = pDoc;
  61. }
  62. void CSingleDocTemplate::RemoveDocument(CDocument* pDoc)
  63. {
  64. ASSERT(m_pOnlyDoc == pDoc); // must be this one
  65. ASSERT_VALID(pDoc);
  66. CDocTemplate::RemoveDocument(pDoc);
  67. m_pOnlyDoc = NULL;
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CSingleDocTemplate commands
  71. CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
  72. BOOL bMakeVisible)
  73. // if lpszPathName == NULL => create new file of this type
  74. {
  75. CDocument* pDocument = NULL;
  76. CFrameWnd* pFrame = NULL;
  77. BOOL bCreated = FALSE; // => doc and frame created
  78. BOOL bWasModified = FALSE;
  79. if (m_pOnlyDoc != NULL)
  80. {
  81. // already have a document - reinit it
  82. pDocument = m_pOnlyDoc;
  83. if (!pDocument->SaveModified())
  84. return NULL; // leave the original one
  85. pFrame = (CFrameWnd*)AfxGetMainWnd();
  86. ASSERT(pFrame != NULL);
  87. ASSERT_KINDOF(CFrameWnd, pFrame);
  88. ASSERT_VALID(pFrame);
  89. }
  90. else
  91. {
  92. // create a new document
  93. pDocument = CreateNewDocument();
  94. ASSERT(pFrame == NULL); // will be created below
  95. bCreated = TRUE;
  96. }
  97. if (pDocument == NULL)
  98. {
  99. AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  100. return NULL;
  101. }
  102. ASSERT(pDocument == m_pOnlyDoc);
  103. if (pFrame == NULL)
  104. {
  105. ASSERT(bCreated);
  106. // create frame - set as main document frame
  107. BOOL bAutoDelete = pDocument->m_bAutoDelete;
  108. pDocument->m_bAutoDelete = FALSE;
  109. // don't destroy if something goes wrong
  110. pFrame = CreateNewFrame(pDocument, NULL);
  111. pDocument->m_bAutoDelete = bAutoDelete;
  112. if (pFrame == NULL)
  113. {
  114. AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  115. delete pDocument; // explicit delete on error
  116. return NULL;
  117. }
  118. }
  119. if (lpszPathName == NULL)
  120. {
  121. // create a new document
  122. SetDefaultTitle(pDocument);
  123. // avoid creating temporary compound file when starting up invisible
  124. if (!bMakeVisible)
  125. pDocument->m_bEmbedded = TRUE;
  126. if (!pDocument->OnNewDocument())
  127. {
  128. // user has been alerted to what failed in OnNewDocument
  129. TRACE0("CDocument::OnNewDocument returned FALSE.\n");
  130. if (bCreated)
  131. pFrame->DestroyWindow(); // will destroy document
  132. return NULL;
  133. }
  134. }
  135. else
  136. {
  137. CWaitCursor wait;
  138. // open an existing document
  139. bWasModified = pDocument->IsModified();
  140. pDocument->SetModifiedFlag(FALSE); // not dirty for open
  141. if (!pDocument->OnOpenDocument(lpszPathName))
  142. {
  143. // user has been alerted to what failed in OnOpenDocument
  144. TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
  145. if (bCreated)
  146. {
  147. pFrame->DestroyWindow(); // will destroy document
  148. }
  149. else if (!pDocument->IsModified())
  150. {
  151. // original document is untouched
  152. pDocument->SetModifiedFlag(bWasModified);
  153. }
  154. else
  155. {
  156. // we corrupted the original document
  157. SetDefaultTitle(pDocument);
  158. if (!pDocument->OnNewDocument())
  159. {
  160. TRACE0("Error: OnNewDocument failed after trying to open a document - trying to continue.\n");
  161. // assume we can continue
  162. }
  163. }
  164. return NULL; // open failed
  165. }
  166. pDocument->SetPathName(lpszPathName);
  167. }
  168. CWinThread* pThread = AfxGetThread();
  169. if (bCreated && pThread->m_pMainWnd == NULL)
  170. {
  171. // set as main frame (InitialUpdateFrame will show the window)
  172. pThread->m_pMainWnd = pFrame;
  173. }
  174. InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
  175. return pDocument;
  176. }
  177. void CSingleDocTemplate::SetDefaultTitle(CDocument* pDocument)
  178. {
  179. CString strDocName;
  180. if (!GetDocString(strDocName, CDocTemplate::docName) ||
  181. strDocName.IsEmpty())
  182. {
  183. // use generic 'untitled'
  184. VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
  185. }
  186. pDocument->SetTitle(strDocName);
  187. }
  188. /////////////////////////////////////////////////////////////////////////////
  189. // CSingleDocTemplate diagnostics
  190. #ifdef _DEBUG
  191. void CSingleDocTemplate::Dump(CDumpContext& dc) const
  192. {
  193. CDocTemplate::Dump(dc);
  194. if (m_pOnlyDoc)
  195. dc << "with document: " << (void*)m_pOnlyDoc;
  196. else
  197. dc << "with no document";
  198. dc << "\n";
  199. }
  200. void CSingleDocTemplate::AssertValid() const
  201. {
  202. CDocTemplate::AssertValid();
  203. if (m_pOnlyDoc)
  204. ASSERT_VALID(m_pOnlyDoc);
  205. }
  206. #endif //_DEBUG
  207. #ifdef AFX_INIT_SEG
  208. #pragma code_seg(AFX_INIT_SEG)
  209. #endif
  210. IMPLEMENT_DYNAMIC(CSingleDocTemplate, CDocTemplate)
  211. /////////////////////////////////////////////////////////////////////////////