TXT

OnFileNew

By Derrick Wright,2014-04-15 22:57
9 views 0
OnFileNew

void CWinApp::OnFileNew()

    {

     if (m_pDocManager != NULL) //维护文档管理器

     m_pDocManager->OnFileNew();

    }

********************************************************************************

    ************************

void CDocManager::OnFileNew()

    {

     if (m_templateList.IsEmpty())

     {

     TRACE0("Error: no document templates registered with CWinApp.\n");

     AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

     return;

     }

     CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();

     if (m_templateList.GetCount() > 1)

     {

     // more than one document template to choose from

     // bring up dialog prompting user

     CNewTypeDlg dlg(&m_templateList);

     int nID = dlg.DoModal();

     if (nID == IDOK)

     pTemplate = dlg.m_pSelectedTemplate;

     else

     return; // none - cancel operation

     }

     ASSERT(pTemplate != NULL);

     ASSERT_KINDOF(CDocTemplate, pTemplate);

     pTemplate->OpenDocumentFile(NULL);

     // if returns NULL, the user has already been alerted }

********************************************************************************

    *********************************

CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,

     BOOL bMakeVisible)

     // if lpszPathName == NULL => create new file of this type

    {

     CDocument* pDocument = NULL;

     CFrameWnd* pFrame = NULL;

     BOOL bCreated = FALSE; // => doc and frame created

     BOOL bWasModified = FALSE;

     if (m_pOnlyDoc != NULL)

     {

     // already have a document - reinit it

     pDocument = m_pOnlyDoc;

     if (!pDocument->SaveModified())

     return NULL; // leave the original one

     pFrame = (CFrameWnd*)AfxGetMainWnd();

     ASSERT(pFrame != NULL);

     ASSERT_KINDOF(CFrameWnd, pFrame);

     ASSERT_VALID(pFrame);

     }

     else

     {

     // create a new document

     pDocument = CreateNewDocument(); //创建文档

     ASSERT(pFrame == NULL); // will be created below

     bCreated = TRUE;

     }

     if (pDocument == NULL)

     {

     AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

     return NULL;

     }

     ASSERT(pDocument == m_pOnlyDoc);

     if (pFrame == NULL)

     {

     ASSERT(bCreated);

     // create frame - set as main document frame

     BOOL bAutoDelete = pDocument->m_bAutoDelete;

     pDocument->m_bAutoDelete = FALSE;

     // don't destroy if something goes wrong

     pFrame = CreateNewFrame(pDocument, NULL); //创建框架和视图

     pDocument->m_bAutoDelete = bAutoDelete;

     if (pFrame == NULL)

     {

     AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

     delete pDocument; // explicit delete on error

     return NULL;

     }

     }

     if (lpszPathName == NULL)

     {

     // create a new document

     SetDefaultTitle(pDocument);

     // avoid creating temporary compound file when starting up invisible

     if (!bMakeVisible)

     pDocument->m_bEmbedded = TRUE;

     if (!pDocument->OnNewDocument())

     {

     // user has been alerted to what failed in OnNewDocument

     TRACE0("CDocument::OnNewDocument returned FALSE.\n");

     if (bCreated)

     pFrame->DestroyWindow(); // will destroy document

     return NULL;

     }

     }

     else

     {

     CWaitCursor wait;

     // open an existing document

     bWasModified = pDocument->IsModified();

     pDocument->SetModifiedFlag(FALSE); // not dirty for open

     if (!pDocument->OnOpenDocument(lpszPathName))

     {

     // user has been alerted to what failed in OnOpenDocument

     TRACE0("CDocument::OnOpenDocument returned FALSE.\n");

     if (bCreated)

     {

     pFrame->DestroyWindow(); // will destroy document

     }

     else if (!pDocument->IsModified())

     {

     // original document is untouched

     pDocument->SetModifiedFlag(bWasModified);

     }

     else

     {

     // we corrupted the original document

     SetDefaultTitle(pDocument);

     if (!pDocument->OnNewDocument())

     {

     TRACE0("Error: OnNewDocument failed after trying to open a

    document - trying to continue.\n");

     // assume we can continue

     }

     }

     return NULL; // open failed

     }

     pDocument->SetPathName(lpszPathName);

     }

     CWinThread* pThread = AfxGetThread();

     if (bCreated && pThread->m_pMainWnd == NULL)

     {

     // set as main frame (InitialUpdateFrame will show the window)

     pThread->m_pMainWnd = pFrame;

     }

     InitialUpdateFrame(pFrame, pDocument, bMakeVisible);

     return pDocument;

    }

********************************************************************************

    *************************************

Report this document

For any questions or suggestions please email
cust-service@docsford.com