1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
5 // SPDX-License-Identifier: GPL-2.0-or-later
6 /////////////////////////////////////////////////////////////////////////////
8 * @file HexMergeView.cpp
10 * @brief Implementation file for CHexMergeView
15 #include "HexMergeFrm.h"
18 #include "HexMergeView.h"
19 #include "OptionsDef.h"
20 #include "OptionsMgr.h"
21 #include "Environment.h"
27 /** @brief Location for hex compare specific help to open. */
28 static TCHAR HexMergeViewHelpLocation[] = _T("::/htmlhelp/Compare_bin.html");
31 * @brief Turn bool api result into success/error code
33 static HRESULT NTAPI SE(BOOL f)
37 HRESULT hr = (HRESULT)::GetLastError();
44 static UINT64 NTAPI GetLastWriteTime(HANDLE h)
47 return ::GetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft)) ? ft : 0;
50 static void NTAPI SetLastWriteTime(HANDLE h, UINT64 ft)
52 ::SetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft));
55 /////////////////////////////////////////////////////////////////////////////
58 IMPLEMENT_DYNCREATE(CHexMergeView, CView)
60 BEGIN_MESSAGE_MAP(CHexMergeView, CView)
61 //{{AFX_MSG_MAP(CHexMergeView)
62 ON_MESSAGE_VOID(WM_PAINT, CWnd::OnPaint)
68 ON_COMMAND(ID_HELP, OnHelp)
69 ON_COMMAND(ID_EDIT_FIND, OnEditFind)
70 ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
71 ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
72 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
73 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
74 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
75 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
76 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
77 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
78 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
79 ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
80 ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
81 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
82 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
83 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
84 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
86 // Test case to verify WM_COMMAND won't accidentally go through Default()
87 //ON_COMMAND(ID_APP_ABOUT, Default)
90 /////////////////////////////////////////////////////////////////////////////
91 // CHexMergeView construction/destruction
96 CHexMergeView::CHexMergeView()
103 * @brief Drawing is not supported
105 void CHexMergeView::OnDraw(CDC *)
111 * @brief returns true if heksedit.dll is loadable
113 bool CHexMergeView::IsLoadable()
115 static void *pv = nullptr;
118 pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
120 return pv != nullptr;
124 * @brief Load heksedit.dll and setup window class name
126 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
129 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
130 cs.lpszClass = _T("heksedit");
131 cs.style |= WS_HSCROLL | WS_VSCROLL;
136 * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
138 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
140 if (CView::OnCreate(lpCreateStruct) == -1)
142 m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
143 if (m_pif == nullptr || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
149 * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
151 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
156 * @brief Synchronize all involved scrollbars
158 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
161 if (pScrollBar && nSBCode == SB_THUMBTRACK)
163 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
164 si.nPos = si.nTrackPos;
165 SetScrollInfo(SB_HORZ, &si);
167 CView::OnHScroll(nSBCode, nPos, pScrollBar);
168 if (pScrollBar != nullptr)
170 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
171 if (nSBCode != SB_THUMBTRACK)
173 pScrollBar->SetScrollInfo(&si);
176 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
177 for (int pane = 0; pane < pSplitter->GetColumnCount(); ++pane)
179 if (pane != m_nThisPane)
181 CWnd *pWnd = pSplitter->GetDlgItem(pSplitter->IdFromRowCol(0, pane));
182 pWnd->SetScrollInfo(SB_HORZ, &si);
183 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
190 * @brief Synchronize all involved scrollbars
192 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
195 if (pScrollBar && nSBCode == SB_THUMBTRACK)
197 pScrollBar->GetScrollInfo(&si);
198 si.nPos = si.nTrackPos;
199 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
201 CView::OnVScroll(nSBCode, nPos, pScrollBar);
202 if (pScrollBar && nSBCode != SB_THUMBTRACK)
204 GetScrollInfo(SB_VERT, &si);
205 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
209 BOOL CHexMergeView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
211 if ((GetAsyncKeyState(VK_CONTROL) &0x8000) != 0) // if (nFlags & MK_CONTROL)
213 PostMessage(WM_COMMAND, zDelta < 0 ? ID_VIEW_ZOOMOUT : ID_VIEW_ZOOMIN);
220 * @brief Synchronize file path bar activation states
222 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
224 CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
225 CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
226 pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
230 * @brief Get pointer to control's content buffer
232 BYTE *CHexMergeView::GetBuffer(int length)
234 return m_pif->get_buffer(length);
238 * @brief Get length of control's content buffer
240 int CHexMergeView::GetLength()
242 return m_pif->get_length();
246 * @brief Checks if file has changed since last update
247 * @param [in] path File to check
248 * @return `true` if file is changed.
250 IMergeDoc::FileChange CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
253 if (!dfi.Update(path))
254 return IMergeDoc::FileChange::Removed;
256 if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME))
257 tolerance = SmallTimeDiff; // From MainFrm.h
258 int64_t timeDiff = dfi.mtime - m_fileInfo.mtime;
259 if (timeDiff < 0) timeDiff = -timeDiff;
260 if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size))
261 return IMergeDoc::FileChange::Changed;
262 return IMergeDoc::FileChange::NoChange;
268 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
270 HANDLE h = CreateFile(path, GENERIC_READ,
271 FILE_SHARE_READ | FILE_SHARE_WRITE,
272 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
273 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
274 if (h == INVALID_HANDLE_VALUE)
276 DWORD length = GetFileSize(h, 0);
277 hr = SE(length != INVALID_FILE_SIZE);
280 if (void *buffer = GetBuffer(length))
283 hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
287 else if (length != 0)
293 m_fileInfo.Update(path);
300 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
302 // Warn user in case file has been changed by someone else
303 if (IsFileChangedOnDisk(path) == IMergeDoc::FileChange::Changed)
305 String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
306 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
309 // Ask user what to do about FILE_ATTRIBUTE_READONLY
310 String strPath = path;
311 bool bApplyToAll = false;
312 if (theApp.HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
314 path = strPath.c_str();
315 // Take a chance to create a backup
316 if (!theApp.CreateBackup(false, path))
318 // Write data to an intermediate file
319 String tempPath = env::GetTemporaryPath();
320 String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
321 if (sIntermediateFilename.empty())
322 return E_FAIL; //Nothing to do if even tempfile name fails
323 HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
324 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
325 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
326 if (h == INVALID_HANDLE_VALUE)
328 DWORD length = GetLength();
329 void *buffer = GetBuffer(length);
336 hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
340 hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
343 m_fileInfo.Update(path);
345 hr = SE(DeleteFile(sIntermediateFilename.c_str()));
348 LogErrorString(strutils::format(_T("DeleteFile(%s) failed: %s"),
349 sIntermediateFilename, GetSysError(hr)));
355 * @brief Get modified flag
357 bool CHexMergeView::GetModified()
359 return m_pif->get_status()->iFileChanged != 0;
363 * @brief Set modified flag
365 void CHexMergeView::SetSavePoint()
367 m_pif->set_savepoint();
371 * @brief Clear undo records
373 void CHexMergeView::ClearUndoRecords()
375 m_pif->clear_undorecords();
379 * @brief Get readonly flag
381 bool CHexMergeView::GetReadOnly()
383 return m_pif->get_settings()->bReadOnly;
387 * @brief Set readonly flag
389 void CHexMergeView::SetReadOnly(bool bReadOnly)
391 m_pif->get_settings()->bReadOnly = bReadOnly;
395 * @brief Allow the control to update all kinds of things that need to be updated when
396 * the window or content buffer have been resized or certain settings have been changed.
398 void CHexMergeView::ResizeWindow()
400 m_pif->resize_window();
404 * @brief Find a sequence of bytes
406 void CHexMergeView::OnEditFind()
412 * @brief Find & replace a sequence of bytes
414 void CHexMergeView::OnEditReplace()
416 m_pif->CMD_replace();
420 * @brief Repeat last find in one or another direction
422 void CHexMergeView::OnEditRepeat()
424 if (GetKeyState(VK_SHIFT) < 0)
425 m_pif->CMD_findprev();
427 m_pif->CMD_findnext();
431 * @brief Called when "Undo" item is updated
433 void CHexMergeView::OnUpdateEditUndo(CCmdUI* pCmdUI)
435 pCmdUI->Enable(m_pif->can_undo());
439 * @brief Undo last action
441 void CHexMergeView::OnEditUndo()
443 m_pif->CMD_edit_undo();
447 * @brief Called when "Redo" item is updated
449 void CHexMergeView::OnUpdateEditRedo(CCmdUI* pCmdUI)
451 pCmdUI->Enable(m_pif->can_redo());
455 * @brief Redo last action
457 void CHexMergeView::OnEditRedo()
459 m_pif->CMD_edit_redo();
463 * @brief Cut selected content
465 void CHexMergeView::OnEditCut()
467 m_pif->CMD_edit_cut();
471 * @brief Copy selected content
473 void CHexMergeView::OnEditCopy()
475 m_pif->CMD_edit_copy();
479 * @brief Paste clipboard content over selected content
481 void CHexMergeView::OnEditPaste()
483 m_pif->CMD_edit_paste();
487 * @brief Select entire content
489 void CHexMergeView::OnEditSelectAll()
491 m_pif->CMD_select_all();
495 * @brief Clear selected content
497 void CHexMergeView::OnEditClear()
499 m_pif->CMD_edit_clear();
503 * @brief Check for keyboard commands
505 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
507 if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
509 if (pMsg->message == WM_KEYDOWN)
511 // Close window in response to VK_ESCAPE if user has allowed it from options
512 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetInt(OPT_CLOSE_WITH_ESC) != 0)
514 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
518 return m_pif->translate_accelerator(pMsg);
522 * @brief Go to first diff
524 void CHexMergeView::OnFirstdiff()
526 m_pif->select_next_diff(TRUE);
530 * @brief Go to last diff
532 void CHexMergeView::OnLastdiff()
534 m_pif->select_prev_diff(TRUE);
538 * @brief Go to next diff
540 void CHexMergeView::OnNextdiff()
542 m_pif->select_next_diff(FALSE);
546 * @brief Go to previous diff
548 void CHexMergeView::OnPrevdiff()
550 m_pif->select_prev_diff(FALSE);
553 /** @brief Open help from mainframe when user presses F1*/
554 void CHexMergeView::OnHelp()
556 theApp.ShowHelp(HexMergeViewHelpLocation);
559 void CHexMergeView::ZoomText(int amount)
561 m_pif->CMD_zoom(amount);
565 * @brief Copy selected bytes from source view to destination view
566 * @note Grows destination buffer as appropriate
568 void CHexMergeView::CopySel(const CHexMergeView *src, CHexMergeView *dst)
570 dst->m_pif->copy_sel_from(src->m_pif);
574 * @brief Copy all bytes from source view to destination view
575 * @note Grows destination buffer as appropriate
577 void CHexMergeView::CopyAll(const CHexMergeView *src, CHexMergeView *dst)
579 dst->m_pif->copy_all_from(src->m_pif);