OSDN Git Service

Binary Compare: Fix the problem that the file cannot be saved after creating a new one
[winmerge-jp/winmerge-jp.git] / Src / HexMergeDoc.cpp
index f934397..824a81e 100644 (file)
@@ -230,15 +230,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_leftSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[0]->SaveFile(pathLeft.c_str())))
-                               {
-                               case 0:
-                                       bLSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bLSaveSuccess = DoFileSave(0);
+                               if (!bLSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -249,15 +243,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_middleSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[1]->SaveFile(pathMiddle.c_str())))
-                               {
-                               case 0:
-                                       bMSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bMSaveSuccess = DoFileSave(1);
+                               if (!bMSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -268,15 +256,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_rightSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[m_nBuffers - 1]->SaveFile(pathRight.c_str())))
-                               {
-                               case 0:
-                                       bRSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bRSaveSuccess = DoFileSave(m_nBuffers - 1);
+                               if (!bRSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -316,23 +298,30 @@ void CHexMergeDoc::OnFileSave()
                DoFileSave(nBuffer);
 }
 
-void CHexMergeDoc::DoFileSave(int nBuffer)
+bool CHexMergeDoc::DoFileSave(int nBuffer)
 {
+       bool result = false;
        if (m_pView[nBuffer]->GetModified())
        {
                if (m_nBufferType[nBuffer] == BUFFERTYPE::UNNAMED)
-                       DoFileSaveAs(nBuffer);
+                       result = DoFileSaveAs(nBuffer);
                else
                {
                        const String &path = m_filePaths.GetPath(nBuffer);
-                       if (Try(m_pView[nBuffer]->SaveFile(path.c_str())) == IDCANCEL)
-                               return;
+                       HRESULT hr = m_pView[nBuffer]->SaveFile(path.c_str());
+                       if (Try(hr) == IDCANCEL)
+                               return false;
+                       if (FAILED(hr))
+                               return DoFileSaveAs(nBuffer);
+                       result = true;
+                       if (result)
+                               UpdateDiffItem(m_pDirDoc);
                }
-               UpdateDiffItem(m_pDirDoc);
        }
+       return result;
 }
 
-void CHexMergeDoc::DoFileSaveAs(int nBuffer)
+bool CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
 {
        const String &path = m_filePaths.GetPath(nBuffer);
        String strPath;
@@ -345,8 +334,11 @@ void CHexMergeDoc::DoFileSaveAs(int nBuffer)
                title = _("Save Middle File As");
        if (SelectFile(AfxGetMainWnd()->GetSafeHwnd(), strPath, false, path.c_str(), title))
        {
-               if (Try(m_pView[nBuffer]->SaveFile(strPath.c_str())) == IDCANCEL)
-                       return;
+               HRESULT hr = m_pView[nBuffer]->SaveFile(strPath.c_str());
+               if (Try(hr) == IDCANCEL)
+                       return false;
+               if (FAILED(hr))
+                       return false;
                if (path.empty())
                {
                        // We are saving scratchpad (unnamed file)
@@ -357,7 +349,9 @@ void CHexMergeDoc::DoFileSaveAs(int nBuffer)
                m_filePaths.SetPath(nBuffer, strPath);
                UpdateDiffItem(m_pDirDoc);
                UpdateHeaderPath(nBuffer);
+               return true;
        }
+       return false;
 }
 
 /**
@@ -643,20 +637,7 @@ void CHexMergeDoc::RefreshOptions()
  */
 void CHexMergeDoc::SetTitle(LPCTSTR lpszTitle)
 {
-       String sTitle;
-       String sFileName[3];
-
-       if (lpszTitle != nullptr)
-               sTitle = lpszTitle;
-       else
-       {
-               for (int nBuffer = 0; nBuffer < m_filePaths.GetSize(); nBuffer++)
-                       sFileName[nBuffer] = !m_strDesc[nBuffer].empty() ? m_strDesc[nBuffer] : paths::FindFileName(m_filePaths[nBuffer]);
-               if (std::count(&sFileName[0], &sFileName[0] + m_nBuffers, sFileName[0]) == m_nBuffers)
-                       sTitle = sFileName[0] + strutils::format(_T(" x %d"), m_nBuffers);
-               else
-                       sTitle = strutils::join(&sFileName[0], &sFileName[0] + m_nBuffers, _T(" - "));
-       }
+       String sTitle = (lpszTitle != nullptr) ? lpszTitle : CMergeFrameCommon::GetTitleString(m_filePaths, m_strDesc);
        CDocument::SetTitle(sTitle.c_str());
 }
 
@@ -820,7 +801,7 @@ void CHexMergeDoc::OnRefresh()
 {
        if (UpdateDiffItem(m_pDirDoc) == 0)
        {
-               static_cast<CMergeFrameCommon*>(GetParentFrame())->ShowIdenticalMessage(m_filePaths, true,
+               CMergeFrameCommon::ShowIdenticalMessage(m_filePaths, true,
                        [](LPCTSTR msg, UINT flags, UINT id) -> int { return AfxMessageBox(msg, flags, id); });
        }
 }
@@ -840,10 +821,7 @@ void CHexMergeDoc::OnFileRecompareAs(UINT nID)
                strDesc[nBuffer] = m_strDesc[nBuffer];
        }
        CloseNow();
-       if (nID == ID_MERGE_COMPARE_TEXT)
-               GetMainFrame()->ShowMergeDoc(pDirDoc, nBuffers, fileloc, dwFlags, strDesc);
-       else
-               GetMainFrame()->ShowImgMergeDoc(pDirDoc, nBuffers, fileloc, dwFlags, strDesc);
+       GetMainFrame()->ShowMergeDoc(nID, pDirDoc, nBuffers, fileloc, dwFlags, strDesc);
 }
 
 void CHexMergeDoc::OnUpdateFileRecompareAs(CCmdUI* pCmdUI)