OSDN Git Service

theApp is unnecessary because these methods are a static methods
[winmerge-jp/winmerge-jp.git] / Src / HexMergeView.cpp
index 5c1f19d..f5a3114 100644 (file)
@@ -2,21 +2,7 @@
 //    WinMerge:  an interactive diff/merge utility
 //    Copyright (C) 1997-2000  Thingamahoochie Software
 //    Author: Dean Grimm
-//
-//    This program is free software; you can redistribute it and/or modify
-//    it under the terms of the GNU General Public License as published by
-//    the Free Software Foundation; either version 2 of the License, or
-//    (at your option) any later version.
-//
-//    This program is distributed in the hope that it will be useful,
-//    but WITHOUT ANY WARRANTY; without even the implied warranty of
-//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//    GNU General Public License for more details.
-//
-//    You should have received a copy of the GNU General Public License
-//    along with this program; if not, write to the Free Software
-//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-//
+//    SPDX-License-Identifier: GPL-2.0-or-later
 /////////////////////////////////////////////////////////////////////////////
 /** 
  * @file  HexMergeView.cpp
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
-#undef THIS_FILE
-static char THIS_FILE[] = __FILE__;
 #endif
 
+/** @brief Location for hex compare specific help to open. */
+static TCHAR HexMergeViewHelpLocation[] = _T("::/htmlhelp/Compare_bin.html");
+
 /**
  * @brief Turn bool api result into success/error code
  */
@@ -48,8 +35,8 @@ static HRESULT NTAPI SE(BOOL f)
        if (f)
                return S_OK;
        HRESULT hr = (HRESULT)::GetLastError();
-       ASSERT(hr);
-       if (hr == 0)
+       ASSERT(hr != NULL);
+       if (hr == NULL)
                hr = E_UNEXPECTED;
        return hr;
 }
@@ -76,7 +63,9 @@ BEGIN_MESSAGE_MAP(CHexMergeView, CView)
        ON_WM_CREATE()
        ON_WM_HSCROLL()
        ON_WM_VSCROLL()
+       ON_WM_MOUSEWHEEL()
        ON_WM_NCCALCSIZE()
+       ON_COMMAND(ID_HELP, OnHelp)
        ON_COMMAND(ID_EDIT_FIND, OnEditFind)
        ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
        ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
@@ -105,7 +94,7 @@ END_MESSAGE_MAP()
  * @brief Constructor.
  */
 CHexMergeView::CHexMergeView()
-: m_pif(0)
+: m_pif(nullptr)
 , m_nThisPane(0)
 {
 }
@@ -115,26 +104,29 @@ CHexMergeView::CHexMergeView()
  */
 void CHexMergeView::OnDraw(CDC *)
 {
-       ASSERT(FALSE);
+       ASSERT(false);
 }
 
 /**
- * @brief Load heksedit.dll and setup window class name
+ * @brief returns true if heksedit.dll is loadable
  */
-BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
+bool CHexMergeView::IsLoadable()
 {
-       static void *pv = NULL;
-       if (pv == NULL)
+       static void *pv = nullptr;
+       if (pv == nullptr)
        {
-               static const CLSID clsid = { 0xBCA3CA6B, 0xCC6B, 0x4F79,
-                       { 0xA2, 0xC2, 0xDD, 0xBE, 0x86, 0x4B, 0x1C, 0x90 } };
-               if (FAILED(::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, IID_IUnknown, &pv)))
-               {
-                       pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
-                       if (!pv)
-                               LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
-               }
+               pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
        }
+       return pv != nullptr;
+}
+
+/**
+ * @brief Load heksedit.dll and setup window class name
+ */
+BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
+{
+       if (!IsLoadable())
+               LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
        cs.lpszClass = _T("heksedit");
        cs.style |= WS_HSCROLL | WS_VSCROLL;
        return TRUE;
@@ -148,7 +140,7 @@ int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
-       if (m_pif == 0 || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
+       if (m_pif == nullptr || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
                return -1;
        return 0;
 }
@@ -173,7 +165,7 @@ void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
                SetScrollInfo(SB_HORZ, &si);
        }
        CView::OnHScroll(nSBCode, nPos, pScrollBar);
-       if (pScrollBar)
+       if (pScrollBar != nullptr)
        {
                GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
                if (nSBCode != SB_THUMBTRACK)
@@ -214,6 +206,16 @@ void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
        }
 }
 
+BOOL CHexMergeView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
+{
+       if ((GetAsyncKeyState(VK_CONTROL) &0x8000) != 0) // if (nFlags & MK_CONTROL)
+       {
+               PostMessage(WM_COMMAND, zDelta < 0 ? ID_VIEW_ZOOMOUT : ID_VIEW_ZOOMIN);
+               return 1;
+       }
+       return 0;
+}
+
 /**
  * @brief Synchronize file path bar activation states
  */
@@ -243,20 +245,21 @@ int CHexMergeView::GetLength()
 /**
  * @brief Checks if file has changed since last update
  * @param [in] path File to check
- * @return TRUE if file is changed.
+ * @return `true` if file is changed.
  */
-BOOL CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
+IMergeDoc::FileChange CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
 {
        DiffFileInfo dfi;
-       dfi.Update(path);
+       if (!dfi.Update(path))
+               return IMergeDoc::FileChange::Removed;
        int tolerance = 0;
        if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME))
                tolerance = SmallTimeDiff; // From MainFrm.h
        int64_t timeDiff = dfi.mtime - m_fileInfo.mtime;
        if (timeDiff < 0) timeDiff = -timeDiff;
        if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size))
-               return true;
-       return false;
+               return IMergeDoc::FileChange::Changed;
+       return IMergeDoc::FileChange::NoChange;
 }
 
 /**
@@ -268,7 +271,7 @@ HRESULT CHexMergeView::LoadFile(LPCTSTR path)
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
-       if (hr != S_OK)
+       if (h == INVALID_HANDLE_VALUE)
                return hr;
        DWORD length = GetFileSize(h, 0);
        hr = SE(length != INVALID_FILE_SIZE);
@@ -297,35 +300,38 @@ HRESULT CHexMergeView::LoadFile(LPCTSTR path)
 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
 {
        // Warn user in case file has been changed by someone else
-       if (IsFileChangedOnDisk(path))
+       if (IsFileChangedOnDisk(path) == IMergeDoc::FileChange::Changed)
        {
-               String msg = string_format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
+               String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
                if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
                        return S_OK;
        }
        // Ask user what to do about FILE_ATTRIBUTE_READONLY
        String strPath = path;
-       BOOL bApplyToAll = FALSE;
-       if (theApp.HandleReadonlySave(strPath, FALSE, bApplyToAll) == IDCANCEL)
+       bool bApplyToAll = false;
+       if (CMergeApp::HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
                return S_OK;
        path = strPath.c_str();
        // Take a chance to create a backup
-       if (!theApp.CreateBackup(FALSE, path))
+       if (!CMergeApp::CreateBackup(false, path))
                return S_OK;
        // Write data to an intermediate file
-       String tempPath = env_GetTempPath();
-       String sIntermediateFilename = env_GetTempFileName(tempPath, _T("MRG_"), 0);
+       String tempPath = env::GetTemporaryPath();
+       String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
        if (sIntermediateFilename.empty())
                return E_FAIL; //Nothing to do if even tempfile name fails
        HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
                0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
        HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
-       if (hr != S_OK)
+       if (h == INVALID_HANDLE_VALUE)
                return hr;
        DWORD length = GetLength();
        void *buffer = GetBuffer(length);
        if (buffer == 0)
+       {
+               CloseHandle(h);
                return E_POINTER;
+       }
        DWORD cb = 0;
        hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
        CloseHandle(h);
@@ -339,8 +345,8 @@ HRESULT CHexMergeView::SaveFile(LPCTSTR path)
        hr = SE(DeleteFile(sIntermediateFilename.c_str()));
        if (hr != S_OK)
        {
-               LogErrorString(string_format(_T("DeleteFile(%s) failed: %s"),
-                       sIntermediateFilename.c_str(), GetSysError(hr).c_str()));
+               LogErrorString(strutils::format(_T("DeleteFile(%s) failed: %s"),
+                       sIntermediateFilename, GetSysError(hr)));
        }
        return S_OK;
 }
@@ -348,9 +354,9 @@ HRESULT CHexMergeView::SaveFile(LPCTSTR path)
 /**
  * @brief Get modified flag
  */
-BOOL CHexMergeView::GetModified()
+bool CHexMergeView::GetModified()
 {
-       return m_pif->get_modified();
+       return m_pif->get_status()->iFileChanged != 0;
 }
 
 /**
@@ -372,7 +378,7 @@ void CHexMergeView::ClearUndoRecords()
 /**
  * @brief Get readonly flag
  */
-BOOL CHexMergeView::GetReadOnly()
+bool CHexMergeView::GetReadOnly()
 {
        return m_pif->get_settings()->bReadOnly;
 }
@@ -380,7 +386,7 @@ BOOL CHexMergeView::GetReadOnly()
 /**
  * @brief Set readonly flag
  */
-void CHexMergeView::SetReadOnly(BOOL bReadOnly)
+void CHexMergeView::SetReadOnly(bool bReadOnly)
 {
        m_pif->get_settings()->bReadOnly = bReadOnly;
 }
@@ -503,7 +509,7 @@ BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
        if (pMsg->message == WM_KEYDOWN)
        {
                // Close window in response to VK_ESCAPE if user has allowed it from options
-               if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetBool(OPT_CLOSE_WITH_ESC))
+               if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetInt(OPT_CLOSE_WITH_ESC) != 0)
                {
                        GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
                        return TRUE;
@@ -544,6 +550,12 @@ void CHexMergeView::OnPrevdiff()
        m_pif->select_prev_diff(FALSE);
 }
 
+/** @brief Open help from mainframe when user presses F1*/
+void CHexMergeView::OnHelp()
+{
+       theApp.ShowHelp(HexMergeViewHelpLocation);
+}
+
 void CHexMergeView::ZoomText(int amount)
 {
        m_pif->CMD_zoom(amount);