diff --git a/Src/MainFrm.cpp b/Src/MainFrm.cpp index e03c2d22793..52999a1d0a4 100644 --- a/Src/MainFrm.cpp +++ b/Src/MainFrm.cpp @@ -893,9 +893,10 @@ bool CMainFrame::ShowTextOrTableMergeDoc(std::optional table, CDirDoc * pD const fileopenflags_t dwFlags[], const String strDesc[], const String& sReportFile /*= _T("")*/, const PackingInfo * infoUnpacker /*= nullptr*/, const OpenTextFileParams* pOpenParams /*= nullptr*/) { + CMultiDocTemplate* pDiffTemplate = theApp.GetDiffTemplate(); if (m_pMenus[MENU_MERGEVIEW] == nullptr) - theApp.m_pDiffTemplate->m_hMenuShared = NewMergeViewMenu(); - CMergeDoc * pMergeDoc = GetMergeDocForDiff(theApp.m_pDiffTemplate, pDirDoc, nFiles, false); + pDiffTemplate->m_hMenuShared = NewMergeViewMenu(); + CMergeDoc * pMergeDoc = GetMergeDocForDiff(pDiffTemplate, pDirDoc, nFiles, false); // Make local copies, so we can change encoding if we guess it below FileLocation fileloc[3]; @@ -1005,9 +1006,10 @@ bool CMainFrame::ShowHexMergeDoc(CDirDoc * pDirDoc, int nFiles, const FileLocati const fileopenflags_t dwFlags[], const String strDesc[], const String& sReportFile /*= _T("")*/, const PackingInfo * infoUnpacker /*= nullptr*/, const OpenBinaryFileParams* pOpenParams /*= nullptr*/) { + CMultiDocTemplate* pHexMergeTemplate = theApp.GetHexMergeTemplate(); if (m_pMenus[MENU_HEXMERGEVIEW] == nullptr) - theApp.m_pHexMergeTemplate->m_hMenuShared = NewHexMergeViewMenu(); - CHexMergeDoc *pHexMergeDoc = GetMergeDocForDiff(theApp.m_pHexMergeTemplate, pDirDoc, nFiles); + pHexMergeTemplate->m_hMenuShared = NewHexMergeViewMenu(); + CHexMergeDoc *pHexMergeDoc = GetMergeDocForDiff(pHexMergeTemplate, pDirDoc, nFiles); if (pHexMergeDoc == nullptr) return false; @@ -1096,7 +1098,7 @@ bool CMainFrame::ShowTextMergeDoc(CDirDoc* pDirDoc, int nBuffers, const String t FileLocation fileloc[3]; fileopenflags_t dwFlags[3] = {}; CDirDoc* pDirDoc2 = pDirDoc->GetMainView() ? pDirDoc : - static_cast(theApp.m_pDirTemplate->CreateNewDocument()); + static_cast(theApp.GetDirTemplate()->CreateNewDocument()); for (int nBuffer = 0; nBuffer < nBuffers; ++nBuffer) { auto wTemp = std::make_shared(TempFile()); @@ -1321,9 +1323,10 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/, if (tFiles.GetSize() < 2 || pathsType == paths::DOES_NOT_EXIST && !std::any_of(tFiles.begin(), tFiles.end(), [](const auto& path) { return path.empty() || paths::IsURL(path); })) { + CMultiDocTemplate* pOpenTemplate = theApp.GetOpenTemplate(); if (m_pMenus[MENU_OPENVIEW] == nullptr) - theApp.m_pOpenTemplate->m_hMenuShared = NewOpenViewMenu(); - COpenDoc *pOpenDoc = static_cast(theApp.m_pOpenTemplate->CreateNewDocument()); + pOpenTemplate->m_hMenuShared = NewOpenViewMenu(); + COpenDoc *pOpenDoc = static_cast(pOpenTemplate->CreateNewDocument()); if (dwFlags) { pOpenDoc->m_dwFlags[0] = dwFlags[0]; @@ -1334,8 +1337,8 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/, pOpenDoc->m_bRecurse = bRecurse2; if (infoUnpacker) pOpenDoc->m_strUnpackerPipeline = infoUnpacker->GetPluginPipeline(); - CFrameWnd *pFrame = theApp.m_pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr); - theApp.m_pOpenTemplate->InitialUpdateFrame(pFrame, pOpenDoc); + CFrameWnd *pFrame = pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr); + pOpenTemplate->InitialUpdateFrame(pFrame, pOpenDoc); return true; } @@ -1387,16 +1390,17 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/, // an archive. Don't open a new dirview if we are comparing files. if (pDirDoc == nullptr) { + CMultiDocTemplate* pDirTemplate = theApp.GetDirTemplate(); if (allowFolderCompare && pathsType == paths::IS_EXISTING_DIR) { CDirDoc::m_nDirsTemp = tFiles.GetSize(); if (m_pMenus[MENU_DIRVIEW] == nullptr) - theApp.m_pDirTemplate->m_hMenuShared = NewDirViewMenu(); - pDirDoc = static_cast(theApp.m_pDirTemplate->OpenDocumentFile(nullptr)); + pDirTemplate->m_hMenuShared = NewDirViewMenu(); + pDirDoc = static_cast(pDirTemplate->OpenDocumentFile(nullptr)); } else { - pDirDoc = static_cast(theApp.m_pDirTemplate->CreateNewDocument()); + pDirDoc = static_cast(pDirTemplate->CreateNewDocument()); } } @@ -1455,7 +1459,7 @@ bool CMainFrame::DoFileOpen(UINT nID, const PathContext* pFiles, const OpenFileParams *pOpenParams /*= nullptr*/) { ASSERT(pFiles != nullptr); - CDirDoc* pDirDoc = static_cast(theApp.m_pDirTemplate->CreateNewDocument()); + CDirDoc* pDirDoc = static_cast(theApp.GetDirTemplate()->CreateNewDocument()); FileLocation fileloc[3]; for (int pane = 0; pane < pFiles->GetSize(); pane++) fileloc[pane].setPath((*pFiles)[pane]); @@ -1724,25 +1728,25 @@ void CMainFrame::ApplyDiffOptions() /// Get list of OpenDocs (documents underlying edit sessions) OpenDocList &CMainFrame::GetAllOpenDocs() { - return static_cast(GetDocList(theApp.m_pOpenTemplate)); + return static_cast(GetDocList(theApp.GetOpenTemplate())); } /// Get list of MergeDocs (documents underlying edit sessions) MergeDocList &CMainFrame::GetAllMergeDocs() { - return static_cast(GetDocList(theApp.m_pDiffTemplate)); + return static_cast(GetDocList(theApp.GetDiffTemplate())); } /// Get list of DirDocs (documents underlying a scan) DirDocList &CMainFrame::GetAllDirDocs() { - return static_cast(GetDocList(theApp.m_pDirTemplate)); + return static_cast(GetDocList(theApp.GetDirTemplate())); } /// Get list of HexMergeDocs (documents underlying edit sessions) HexMergeDocList &CMainFrame::GetAllHexMergeDocs() { - return static_cast(GetDocList(theApp.m_pHexMergeTemplate)); + return static_cast(GetDocList(theApp.GetHexMergeTemplate())); } std::vector CMainFrame::GetAllImgMergeFrames() @@ -2000,7 +2004,7 @@ bool CMainFrame::DoFileNew(UINT nID, int nPanes, const PrediffingInfo *infoPrediffer /*= nullptr*/, const OpenFileParams *pOpenParams) { - CDirDoc *pDirDoc = static_cast(theApp.m_pDirTemplate->CreateNewDocument()); + CDirDoc *pDirDoc = static_cast(theApp.GetDirTemplate()->CreateNewDocument()); // Load emptyfile descriptors and open empty docs // Use default codepage @@ -2357,9 +2361,10 @@ CMainFrame * GetMainFrame() */ void CMainFrame::OnSaveProject() { + CMultiDocTemplate* pOpenTemplate = theApp.GetOpenTemplate(); if (m_pMenus[MENU_OPENVIEW] == nullptr) - theApp.m_pOpenTemplate->m_hMenuShared = NewOpenViewMenu(); - COpenDoc *pOpenDoc = static_cast(theApp.m_pOpenTemplate->CreateNewDocument()); + pOpenTemplate->m_hMenuShared = NewOpenViewMenu(); + COpenDoc *pOpenDoc = static_cast(pOpenTemplate->CreateNewDocument()); CFrameWnd * pFrame = GetActiveFrame(); FRAMETYPE frame = pFrame ? GetFrameType(pFrame) : FRAME_OTHER; @@ -2430,8 +2435,8 @@ void CMainFrame::OnSaveProject() } } - CFrameWnd *pOpenFrame = theApp.m_pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr); - theApp.m_pOpenTemplate->InitialUpdateFrame(pOpenFrame, pOpenDoc); + CFrameWnd *pOpenFrame = pOpenTemplate->CreateNewFrame(pOpenDoc, nullptr); + pOpenTemplate->InitialUpdateFrame(pOpenFrame, pOpenDoc); } /** @@ -3289,9 +3294,9 @@ void CMainFrame::ReloadMenu() // m_hMenuDefault is used to redraw the main menu when we close a child frame // if this child frame had a different menu pMainFrame->m_hMenuDefault = hNewDefaultMenu; - pApp->m_pOpenTemplate->m_hMenuShared = hNewDefaultMenu; - pApp->m_pDiffTemplate->m_hMenuShared = hNewMergeMenu; - pApp->m_pDirTemplate->m_hMenuShared = hNewDirMenu; + pApp->GetOpenTemplate()->m_hMenuShared = hNewDefaultMenu; + pApp->GetDiffTemplate()->m_hMenuShared = hNewMergeMenu; + pApp->GetDirTemplate()->m_hMenuShared = hNewDirMenu; // force redrawing the menu bar pMainFrame->DrawMenuBar(); diff --git a/Src/Merge.cpp b/Src/Merge.cpp index 8a8afe5e969..4c2cbf6148e 100644 --- a/Src/Merge.cpp +++ b/Src/Merge.cpp @@ -419,41 +419,6 @@ BOOL CMergeApp::InitInstance() return FALSE; } - // Register the application's document templates. Document templates - // serve as the connection between documents, frame windows and views. - - // Open view - m_pOpenTemplate = new CMultiDocTemplate( - IDR_MAINFRAME, - RUNTIME_CLASS(COpenDoc), - RUNTIME_CLASS(COpenFrame), // custom MDI child frame - RUNTIME_CLASS(COpenView)); - AddDocTemplate(m_pOpenTemplate); - - // Merge Edit view - m_pDiffTemplate = new CMultiDocTemplate( - IDR_MERGEDOCTYPE, - RUNTIME_CLASS(CMergeDoc), - RUNTIME_CLASS(CMergeEditFrame), // custom MDI child frame - RUNTIME_CLASS(CMergeEditSplitterView)); - AddDocTemplate(m_pDiffTemplate); - - // Merge Edit view - m_pHexMergeTemplate = new CMultiDocTemplate( - IDR_MERGEDOCTYPE, - RUNTIME_CLASS(CHexMergeDoc), - RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame - RUNTIME_CLASS(CHexMergeView)); - AddDocTemplate(m_pHexMergeTemplate); - - // Directory view - m_pDirTemplate = new CMultiDocTemplate( - IDR_DIRDOCTYPE, - RUNTIME_CLASS(CDirDoc), - RUNTIME_CLASS(CDirFrame), // custom MDI child frame - RUNTIME_CLASS(CDirView)); - AddDocTemplate(m_pDirTemplate); - // create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) @@ -500,6 +465,66 @@ BOOL CMergeApp::InitInstance() return bContinue; } +CMultiDocTemplate* CMergeApp::GetOpenTemplate() +{ + // Open view + if (!m_pOpenTemplate) + { + m_pOpenTemplate = new CMultiDocTemplate( + IDR_MAINFRAME, + RUNTIME_CLASS(COpenDoc), + RUNTIME_CLASS(COpenFrame), // custom MDI child frame + RUNTIME_CLASS(COpenView)); + AddDocTemplate(m_pOpenTemplate); + } + return m_pOpenTemplate; +} + +CMultiDocTemplate* CMergeApp::GetDiffTemplate() +{ + // Merge Edit view + if (!m_pDiffTemplate) + { + m_pDiffTemplate = new CMultiDocTemplate( + IDR_MERGEDOCTYPE, + RUNTIME_CLASS(CMergeDoc), + RUNTIME_CLASS(CMergeEditFrame), // custom MDI child frame + RUNTIME_CLASS(CMergeEditSplitterView)); + AddDocTemplate(m_pDiffTemplate); + } + return m_pDiffTemplate; +} + +CMultiDocTemplate* CMergeApp::GetHexMergeTemplate() +{ + if (!m_pHexMergeTemplate) + { + // Hex Merge view + m_pHexMergeTemplate = new CMultiDocTemplate( + IDR_MERGEDOCTYPE, + RUNTIME_CLASS(CHexMergeDoc), + RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame + RUNTIME_CLASS(CHexMergeView)); + AddDocTemplate(m_pHexMergeTemplate); + } + return m_pHexMergeTemplate; +} + +CMultiDocTemplate* CMergeApp::GetDirTemplate() +{ + if (!m_pDirTemplate) + { + // Directory view + m_pDirTemplate = new CMultiDocTemplate( + IDR_DIRDOCTYPE, + RUNTIME_CLASS(CDirDoc), + RUNTIME_CLASS(CDirFrame), // custom MDI child frame + RUNTIME_CLASS(CDirView)); + AddDocTemplate(m_pDirTemplate); + } + return m_pDirTemplate; +} + static void OpenContributersFile(int&) { CMergeApp::OpenFileToExternalEditor(paths::ConcatPath(env::GetProgPath(), ContributorsPath)); @@ -609,6 +634,8 @@ int CMergeApp::DoMessageBox(const tchar_t* lpszPrompt, UINT nType, UINT nIDPromp bool CMergeApp::IsReallyIdle() const { + if (!m_pDirTemplate) + return true; bool idle = true; POSITION pos = m_pDirTemplate->GetFirstDocPosition(); while (pos != nullptr) diff --git a/Src/Merge.h b/Src/Merge.h index 2e32de3e195..fed2ef4aea9 100644 --- a/Src/Merge.h +++ b/Src/Merge.h @@ -101,6 +101,10 @@ class CMergeApp : public CWinApp static void SetupTempPath(); bool IsReallyIdle() const; void RegisterIdleFunc(std::function func) { m_idleFuncs.push_back(func); }; + CMultiDocTemplate* GetOpenTemplate(); + CMultiDocTemplate* GetDiffTemplate(); + CMultiDocTemplate* GetHexMergeTemplate(); + CMultiDocTemplate* GetDirTemplate(); virtual UINT GetProfileInt(const tchar_t* lpszSection, const tchar_t* lpszEntry, int nDefault) override; virtual BOOL WriteProfileInt(const tchar_t* lpszSection, const tchar_t* lpszEntry, int nValue) override;