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"
28 * @brief Turn bool api result into success/error code
30 static HRESULT NTAPI SE(BOOL f)
34 HRESULT hr = (HRESULT)::GetLastError();
41 static UINT64 NTAPI GetLastWriteTime(HANDLE h)
44 return ::GetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft)) ? ft : 0;
47 static void NTAPI SetLastWriteTime(HANDLE h, UINT64 ft)
49 ::SetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft));
52 /////////////////////////////////////////////////////////////////////////////
55 IMPLEMENT_DYNCREATE(CHexMergeView, CView)
57 BEGIN_MESSAGE_MAP(CHexMergeView, CView)
58 //{{AFX_MSG_MAP(CHexMergeView)
59 ON_MESSAGE_VOID(WM_PAINT, CWnd::OnPaint)
64 ON_COMMAND(ID_EDIT_FIND, OnEditFind)
65 ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
66 ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
67 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
68 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
69 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
70 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
71 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
72 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
73 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
74 ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
75 ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
76 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
77 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
78 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
79 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
81 // Test case to verify WM_COMMAND won't accidentally go through Default()
82 //ON_COMMAND(ID_APP_ABOUT, Default)
85 /////////////////////////////////////////////////////////////////////////////
86 // CHexMergeView construction/destruction
91 CHexMergeView::CHexMergeView()
98 * @brief Drawing is not supported
100 void CHexMergeView::OnDraw(CDC *)
106 * @brief returns true if heksedit.dll is loadable
108 bool CHexMergeView::IsLoadable()
110 static void *pv = nullptr;
113 pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
115 return pv != nullptr;
119 * @brief Load heksedit.dll and setup window class name
121 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
124 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
125 cs.lpszClass = _T("heksedit");
126 cs.style |= WS_HSCROLL | WS_VSCROLL;
131 * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
133 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
135 if (CView::OnCreate(lpCreateStruct) == -1)
137 m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
138 if (m_pif == nullptr || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
144 * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
146 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
151 * @brief Synchronize all involved scrollbars
153 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
156 if (pScrollBar && nSBCode == SB_THUMBTRACK)
158 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
159 si.nPos = si.nTrackPos;
160 SetScrollInfo(SB_HORZ, &si);
162 CView::OnHScroll(nSBCode, nPos, pScrollBar);
163 if (pScrollBar != nullptr)
165 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
166 if (nSBCode != SB_THUMBTRACK)
168 pScrollBar->SetScrollInfo(&si);
171 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
172 for (int pane = 0; pane < pSplitter->GetColumnCount(); ++pane)
174 if (pane != m_nThisPane)
176 CWnd *pWnd = pSplitter->GetDlgItem(pSplitter->IdFromRowCol(0, pane));
177 pWnd->SetScrollInfo(SB_HORZ, &si);
178 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
185 * @brief Synchronize all involved scrollbars
187 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
190 if (pScrollBar && nSBCode == SB_THUMBTRACK)
192 pScrollBar->GetScrollInfo(&si);
193 si.nPos = si.nTrackPos;
194 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
196 CView::OnVScroll(nSBCode, nPos, pScrollBar);
197 if (pScrollBar && nSBCode != SB_THUMBTRACK)
199 GetScrollInfo(SB_VERT, &si);
200 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
205 * @brief Synchronize file path bar activation states
207 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
209 CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
210 CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
211 pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
215 * @brief Get pointer to control's content buffer
217 BYTE *CHexMergeView::GetBuffer(int length)
219 return m_pif->get_buffer(length);
223 * @brief Get length of control's content buffer
225 int CHexMergeView::GetLength()
227 return m_pif->get_length();
231 * @brief Checks if file has changed since last update
232 * @param [in] path File to check
233 * @return `true` if file is changed.
235 IMergeDoc::FileChange CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
238 if (!dfi.Update(path))
239 return IMergeDoc::FileRemoved;
241 if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME))
242 tolerance = SmallTimeDiff; // From MainFrm.h
243 int64_t timeDiff = dfi.mtime - m_fileInfo.mtime;
244 if (timeDiff < 0) timeDiff = -timeDiff;
245 if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size))
246 return IMergeDoc::FileChanged;
247 return IMergeDoc::FileNoChange;
253 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
255 HANDLE h = CreateFile(path, GENERIC_READ,
256 FILE_SHARE_READ | FILE_SHARE_WRITE,
257 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
258 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
259 if (h == INVALID_HANDLE_VALUE)
261 DWORD length = GetFileSize(h, 0);
262 hr = SE(length != INVALID_FILE_SIZE);
265 if (void *buffer = GetBuffer(length))
268 hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
272 else if (length != 0)
278 m_fileInfo.Update(path);
285 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
287 // Warn user in case file has been changed by someone else
288 if (IsFileChangedOnDisk(path) == IMergeDoc::FileChanged)
290 String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
291 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
294 // Ask user what to do about FILE_ATTRIBUTE_READONLY
295 String strPath = path;
296 bool bApplyToAll = false;
297 if (theApp.HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL)
299 path = strPath.c_str();
300 // Take a chance to create a backup
301 if (!theApp.CreateBackup(false, path))
303 // Write data to an intermediate file
304 String tempPath = env::GetTemporaryPath();
305 String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
306 if (sIntermediateFilename.empty())
307 return E_FAIL; //Nothing to do if even tempfile name fails
308 HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
309 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
310 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
311 if (h == INVALID_HANDLE_VALUE)
313 DWORD length = GetLength();
314 void *buffer = GetBuffer(length);
321 hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
325 hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
328 m_fileInfo.Update(path);
330 hr = SE(DeleteFile(sIntermediateFilename.c_str()));
333 LogErrorString(strutils::format(_T("DeleteFile(%s) failed: %s"),
334 sIntermediateFilename, GetSysError(hr)));
340 * @brief Get modified flag
342 bool CHexMergeView::GetModified()
344 return m_pif->get_status()->iFileChanged != 0;
348 * @brief Set modified flag
350 void CHexMergeView::SetSavePoint()
352 m_pif->set_savepoint();
356 * @brief Clear undo records
358 void CHexMergeView::ClearUndoRecords()
360 m_pif->clear_undorecords();
364 * @brief Get readonly flag
366 bool CHexMergeView::GetReadOnly()
368 return m_pif->get_settings()->bReadOnly;
372 * @brief Set readonly flag
374 void CHexMergeView::SetReadOnly(bool bReadOnly)
376 m_pif->get_settings()->bReadOnly = bReadOnly;
380 * @brief Allow the control to update all kinds of things that need to be updated when
381 * the window or content buffer have been resized or certain settings have been changed.
383 void CHexMergeView::ResizeWindow()
385 m_pif->resize_window();
389 * @brief Find a sequence of bytes
391 void CHexMergeView::OnEditFind()
397 * @brief Find & replace a sequence of bytes
399 void CHexMergeView::OnEditReplace()
401 m_pif->CMD_replace();
405 * @brief Repeat last find in one or another direction
407 void CHexMergeView::OnEditRepeat()
409 if (GetKeyState(VK_SHIFT) < 0)
410 m_pif->CMD_findprev();
412 m_pif->CMD_findnext();
416 * @brief Called when "Undo" item is updated
418 void CHexMergeView::OnUpdateEditUndo(CCmdUI* pCmdUI)
420 pCmdUI->Enable(m_pif->can_undo());
424 * @brief Undo last action
426 void CHexMergeView::OnEditUndo()
428 m_pif->CMD_edit_undo();
432 * @brief Called when "Redo" item is updated
434 void CHexMergeView::OnUpdateEditRedo(CCmdUI* pCmdUI)
436 pCmdUI->Enable(m_pif->can_redo());
440 * @brief Redo last action
442 void CHexMergeView::OnEditRedo()
444 m_pif->CMD_edit_redo();
448 * @brief Cut selected content
450 void CHexMergeView::OnEditCut()
452 m_pif->CMD_edit_cut();
456 * @brief Copy selected content
458 void CHexMergeView::OnEditCopy()
460 m_pif->CMD_edit_copy();
464 * @brief Paste clipboard content over selected content
466 void CHexMergeView::OnEditPaste()
468 m_pif->CMD_edit_paste();
472 * @brief Select entire content
474 void CHexMergeView::OnEditSelectAll()
476 m_pif->CMD_select_all();
480 * @brief Clear selected content
482 void CHexMergeView::OnEditClear()
484 m_pif->CMD_edit_clear();
488 * @brief Check for keyboard commands
490 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
492 if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
494 if (pMsg->message == WM_KEYDOWN)
496 // Close window in response to VK_ESCAPE if user has allowed it from options
497 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetInt(OPT_CLOSE_WITH_ESC) != 0)
499 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
503 return m_pif->translate_accelerator(pMsg);
507 * @brief Go to first diff
509 void CHexMergeView::OnFirstdiff()
511 m_pif->select_next_diff(TRUE);
515 * @brief Go to last diff
517 void CHexMergeView::OnLastdiff()
519 m_pif->select_prev_diff(TRUE);
523 * @brief Go to next diff
525 void CHexMergeView::OnNextdiff()
527 m_pif->select_next_diff(FALSE);
531 * @brief Go to previous diff
533 void CHexMergeView::OnPrevdiff()
535 m_pif->select_prev_diff(FALSE);
538 void CHexMergeView::ZoomText(int amount)
540 m_pif->CMD_zoom(amount);
544 * @brief Copy selected bytes from source view to destination view
545 * @note Grows destination buffer as appropriate
547 void CHexMergeView::CopySel(const CHexMergeView *src, CHexMergeView *dst)
549 dst->m_pif->copy_sel_from(src->m_pif);
553 * @brief Copy all bytes from source view to destination view
554 * @note Grows destination buffer as appropriate
556 void CHexMergeView::CopyAll(const CHexMergeView *src, CHexMergeView *dst)
558 dst->m_pif->copy_all_from(src->m_pif);