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)
67 ON_COMMAND(ID_HELP, OnHelp)
68 ON_COMMAND(ID_EDIT_FIND, OnEditFind)
69 ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
70 ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
71 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
72 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
73 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
74 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
75 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
76 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
77 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
78 ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
79 ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
80 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
81 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
82 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
83 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
85 // Test case to verify WM_COMMAND won't accidentally go through Default()
86 //ON_COMMAND(ID_APP_ABOUT, Default)
89 /////////////////////////////////////////////////////////////////////////////
90 // CHexMergeView construction/destruction
95 CHexMergeView::CHexMergeView()
102 * @brief Drawing is not supported
104 void CHexMergeView::OnDraw(CDC *)
110 * @brief returns true if heksedit.dll is loadable
112 bool CHexMergeView::IsLoadable()
114 static void *pv = nullptr;
117 pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
119 return pv != nullptr;
123 * @brief Load heksedit.dll and setup window class name
125 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
128 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
129 cs.lpszClass = _T("heksedit");
130 cs.style |= WS_HSCROLL | WS_VSCROLL;
135 * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
137 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
139 if (CView::OnCreate(lpCreateStruct) == -1)
141 m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
142 if (m_pif == nullptr || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
148 * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
150 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
155 * @brief Synchronize all involved scrollbars
157 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
160 if (pScrollBar && nSBCode == SB_THUMBTRACK)
162 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
163 si.nPos = si.nTrackPos;
164 SetScrollInfo(SB_HORZ, &si);
166 CView::OnHScroll(nSBCode, nPos, pScrollBar);
167 if (pScrollBar != nullptr)
169 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
170 if (nSBCode != SB_THUMBTRACK)
172 pScrollBar->SetScrollInfo(&si);
175 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
176 for (int pane = 0; pane < pSplitter->GetColumnCount(); ++pane)
178 if (pane != m_nThisPane)
180 CWnd *pWnd = pSplitter->GetDlgItem(pSplitter->IdFromRowCol(0, pane));
181 pWnd->SetScrollInfo(SB_HORZ, &si);
182 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
189 * @brief Synchronize all involved scrollbars
191 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
194 if (pScrollBar && nSBCode == SB_THUMBTRACK)
196 pScrollBar->GetScrollInfo(&si);
197 si.nPos = si.nTrackPos;
198 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
200 CView::OnVScroll(nSBCode, nPos, pScrollBar);
201 if (pScrollBar && nSBCode != SB_THUMBTRACK)
203 GetScrollInfo(SB_VERT, &si);
204 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
209 * @brief Synchronize file path bar activation states
211 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
213 CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
214 CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
215 pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
219 * @brief Get pointer to control's content buffer
221 BYTE *CHexMergeView::GetBuffer(int length)
223 return m_pif->get_buffer(length);
227 * @brief Get length of control's content buffer
229 int CHexMergeView::GetLength()
231 return m_pif->get_length();
235 * @brief Checks if file has changed since last update
236 * @param [in] path File to check
237 * @return `true` if file is changed.
239 IMergeDoc::FileChange CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
242 if (!dfi.Update(path))
243 return IMergeDoc::FileRemoved;
245 if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME))
246 tolerance = SmallTimeDiff; // From MainFrm.h
247 int64_t timeDiff = dfi.mtime - m_fileInfo.mtime;
248 if (timeDiff < 0) timeDiff = -timeDiff;
249 if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size))
250 return IMergeDoc::FileChanged;
251 return IMergeDoc::FileNoChange;
257 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
259 HANDLE h = CreateFile(path, GENERIC_READ,
260 FILE_SHARE_READ | FILE_SHARE_WRITE,
261 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
262 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
263 if (h == INVALID_HANDLE_VALUE)
265 DWORD length = GetFileSize(h, 0);
266 hr = SE(length != INVALID_FILE_SIZE);
269 if (void *buffer = GetBuffer(length))
272 hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
276 else if (length != 0)
282 m_fileInfo.Update(path);
289 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
291 // Warn user in case file has been changed by someone else
292 if (IsFileChangedOnDisk(path) == IMergeDoc::FileChanged)
294 String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
295 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
298 // Ask user what to do about FILE_ATTRIBUTE_READONLY
299 String strPath = path;
300 bool bApplyToAll = false;
301 if (theApp.HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
303 path = strPath.c_str();
304 // Take a chance to create a backup
305 if (!theApp.CreateBackup(false, path))
307 // Write data to an intermediate file
308 String tempPath = env::GetTemporaryPath();
309 String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
310 if (sIntermediateFilename.empty())
311 return E_FAIL; //Nothing to do if even tempfile name fails
312 HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
313 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
314 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
315 if (h == INVALID_HANDLE_VALUE)
317 DWORD length = GetLength();
318 void *buffer = GetBuffer(length);
325 hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
329 hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
332 m_fileInfo.Update(path);
334 hr = SE(DeleteFile(sIntermediateFilename.c_str()));
337 LogErrorString(strutils::format(_T("DeleteFile(%s) failed: %s"),
338 sIntermediateFilename, GetSysError(hr)));
344 * @brief Get modified flag
346 bool CHexMergeView::GetModified()
348 return m_pif->get_status()->iFileChanged != 0;
352 * @brief Set modified flag
354 void CHexMergeView::SetSavePoint()
356 m_pif->set_savepoint();
360 * @brief Clear undo records
362 void CHexMergeView::ClearUndoRecords()
364 m_pif->clear_undorecords();
368 * @brief Get readonly flag
370 bool CHexMergeView::GetReadOnly()
372 return m_pif->get_settings()->bReadOnly;
376 * @brief Set readonly flag
378 void CHexMergeView::SetReadOnly(bool bReadOnly)
380 m_pif->get_settings()->bReadOnly = bReadOnly;
384 * @brief Allow the control to update all kinds of things that need to be updated when
385 * the window or content buffer have been resized or certain settings have been changed.
387 void CHexMergeView::ResizeWindow()
389 m_pif->resize_window();
393 * @brief Find a sequence of bytes
395 void CHexMergeView::OnEditFind()
401 * @brief Find & replace a sequence of bytes
403 void CHexMergeView::OnEditReplace()
405 m_pif->CMD_replace();
409 * @brief Repeat last find in one or another direction
411 void CHexMergeView::OnEditRepeat()
413 if (GetKeyState(VK_SHIFT) < 0)
414 m_pif->CMD_findprev();
416 m_pif->CMD_findnext();
420 * @brief Called when "Undo" item is updated
422 void CHexMergeView::OnUpdateEditUndo(CCmdUI* pCmdUI)
424 pCmdUI->Enable(m_pif->can_undo());
428 * @brief Undo last action
430 void CHexMergeView::OnEditUndo()
432 m_pif->CMD_edit_undo();
436 * @brief Called when "Redo" item is updated
438 void CHexMergeView::OnUpdateEditRedo(CCmdUI* pCmdUI)
440 pCmdUI->Enable(m_pif->can_redo());
444 * @brief Redo last action
446 void CHexMergeView::OnEditRedo()
448 m_pif->CMD_edit_redo();
452 * @brief Cut selected content
454 void CHexMergeView::OnEditCut()
456 m_pif->CMD_edit_cut();
460 * @brief Copy selected content
462 void CHexMergeView::OnEditCopy()
464 m_pif->CMD_edit_copy();
468 * @brief Paste clipboard content over selected content
470 void CHexMergeView::OnEditPaste()
472 m_pif->CMD_edit_paste();
476 * @brief Select entire content
478 void CHexMergeView::OnEditSelectAll()
480 m_pif->CMD_select_all();
484 * @brief Clear selected content
486 void CHexMergeView::OnEditClear()
488 m_pif->CMD_edit_clear();
492 * @brief Check for keyboard commands
494 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
496 if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
498 if (pMsg->message == WM_KEYDOWN)
500 // Close window in response to VK_ESCAPE if user has allowed it from options
501 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetInt(OPT_CLOSE_WITH_ESC) != 0)
503 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
507 return m_pif->translate_accelerator(pMsg);
511 * @brief Go to first diff
513 void CHexMergeView::OnFirstdiff()
515 m_pif->select_next_diff(TRUE);
519 * @brief Go to last diff
521 void CHexMergeView::OnLastdiff()
523 m_pif->select_prev_diff(TRUE);
527 * @brief Go to next diff
529 void CHexMergeView::OnNextdiff()
531 m_pif->select_next_diff(FALSE);
535 * @brief Go to previous diff
537 void CHexMergeView::OnPrevdiff()
539 m_pif->select_prev_diff(FALSE);
542 /** @brief Open help from mainframe when user presses F1*/
543 void CHexMergeView::OnHelp()
545 theApp.ShowHelp(HexMergeViewHelpLocation);
548 void CHexMergeView::ZoomText(int amount)
550 m_pif->CMD_zoom(amount);
554 * @brief Copy selected bytes from source view to destination view
555 * @note Grows destination buffer as appropriate
557 void CHexMergeView::CopySel(const CHexMergeView *src, CHexMergeView *dst)
559 dst->m_pif->copy_sel_from(src->m_pif);
563 * @brief Copy all bytes from source view to destination view
564 * @note Grows destination buffer as appropriate
566 void CHexMergeView::CopyAll(const CHexMergeView *src, CHexMergeView *dst)
568 dst->m_pif->copy_all_from(src->m_pif);