1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /////////////////////////////////////////////////////////////////////////////
22 * @file HexMergeView.cpp
24 * @brief Implementation file for CHexMergeDoc
27 // ID line follows -- this is updated by SVN
28 // $Id: HexMergeView.cpp 7165 2010-05-15 14:04:43Z jtuc $
31 #include "HexMergeFrm.h"
34 #include "HexMergeView.h"
35 #include "OptionsDef.h"
36 #include "OptionsMgr.h"
37 #include "Environment.h"
42 static char THIS_FILE[] = __FILE__;
46 * @brief Turn bool api result into success/error code
48 static HRESULT NTAPI SE(BOOL f)
52 HRESULT hr = (HRESULT)::GetLastError();
59 static UINT64 NTAPI GetLastWriteTime(HANDLE h)
62 return ::GetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft)) ? ft : 0;
65 static void NTAPI SetLastWriteTime(HANDLE h, UINT64 ft)
67 ::SetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft));
70 /////////////////////////////////////////////////////////////////////////////
73 IMPLEMENT_DYNCREATE(CHexMergeView, CView)
75 BEGIN_MESSAGE_MAP(CHexMergeView, CView)
76 //{{AFX_MSG_MAP(CHexMergeView)
77 ON_MESSAGE_VOID(WM_PAINT, CWnd::OnPaint)
82 ON_COMMAND(ID_EDIT_FIND, OnEditFind)
83 ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
84 ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
85 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
86 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
87 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
88 ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
89 ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
90 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
91 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
92 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
93 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
95 // Test case to verify WM_COMMAND won't accidentally go through Default()
96 //ON_COMMAND(ID_APP_ABOUT, Default)
99 /////////////////////////////////////////////////////////////////////////////
100 // CHexMergeView construction/destruction
103 * @brief Constructor.
105 CHexMergeView::CHexMergeView()
114 * @brief Drawing is not supported
116 void CHexMergeView::OnDraw(CDC *)
122 * @brief Load heksedit.dll and setup window class name
124 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
126 static void *pv = NULL;
129 static const CLSID clsid = { 0xBCA3CA6B, 0xCC6B, 0x4F79,
130 { 0xA2, 0xC2, 0xDD, 0xBE, 0x86, 0x4B, 0x1C, 0x90 } };
131 if (FAILED(::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, IID_IUnknown, &pv)))
133 pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
135 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
138 cs.lpszClass = _T("heksedit");
139 cs.style |= WS_HSCROLL | WS_VSCROLL;
144 * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
146 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
148 if (CView::OnCreate(lpCreateStruct) == -1)
150 m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
151 if (m_pif == 0 || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
157 * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
159 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
164 * @brief Synchronize all involved scrollbars
166 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
169 if (pScrollBar && nSBCode == SB_THUMBTRACK)
171 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
172 si.nPos = si.nTrackPos;
173 SetScrollInfo(SB_HORZ, &si);
175 CView::OnHScroll(nSBCode, nPos, pScrollBar);
178 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
179 if (nSBCode != SB_THUMBTRACK)
181 pScrollBar->SetScrollInfo(&si);
183 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
184 int nID = GetDlgCtrlID();
185 nID ^= pSplitter->IdFromRowCol(0, 0) ^ pSplitter->IdFromRowCol(0, 1);
186 CWnd *pWnd = pSplitter->GetDlgItem(nID);
187 pWnd->SetScrollInfo(SB_HORZ, &si);
188 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
193 * @brief Synchronize all involved scrollbars
195 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
198 if (pScrollBar && nSBCode == SB_THUMBTRACK)
200 pScrollBar->GetScrollInfo(&si);
201 si.nPos = si.nTrackPos;
202 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
204 CView::OnVScroll(nSBCode, nPos, pScrollBar);
205 if (pScrollBar && nSBCode != SB_THUMBTRACK)
207 GetScrollInfo(SB_VERT, &si);
208 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
213 * @brief Synchronize file path bar activation states
215 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
217 CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
218 CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
219 pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
223 * @brief Get pointer to control's content buffer
225 BYTE *CHexMergeView::GetBuffer(int length)
227 return m_pif->get_buffer(length);
231 * @brief Get length of control's content buffer
233 int CHexMergeView::GetLength()
235 return m_pif->get_length();
239 * @brief Checks if file has changed since last update
240 * @param [in] path File to check
241 * @return TRUE if file is changed.
243 BOOL CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
245 // NB: FileTimes are measured in 100 nanosecond intervals since 1601-01-01.
246 BOOL bChanged = FALSE;
247 HANDLE h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE,
248 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
249 if (h != INVALID_HANDLE_VALUE)
251 UINT64 mtime = GetLastWriteTime(h);
252 UINT64 lower = min(mtime, m_mtime);
253 UINT64 upper = max(mtime, m_mtime);
254 BOOL bIgnoreSmallDiff = GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME);
255 UINT64 tolerance = bIgnoreSmallDiff ? SmallTimeDiff * 10000000 : 0;
256 bChanged = upper - lower > tolerance || m_size != GetFileSize(h, 0);
265 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
267 HANDLE h = CreateFile(path, GENERIC_READ,
268 FILE_SHARE_READ | FILE_SHARE_WRITE,
269 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
270 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
273 m_mtime = GetLastWriteTime(h);
274 DWORD length = m_size = GetFileSize(h, 0);
275 hr = SE(length != INVALID_FILE_SIZE);
278 if (void *buffer = GetBuffer(length))
281 hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
285 else if (length != 0)
297 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
299 // Warn user in case file has been changed by someone else
300 if (IsFileChangedOnDisk(path))
302 String msg = string_format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
303 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
306 // Ask user what to do about FILE_ATTRIBUTE_READONLY
307 String strPath = path;
308 BOOL bApplyToAll = FALSE;
309 if (theApp.HandleReadonlySave(strPath, FALSE, bApplyToAll) == IDCANCEL)
311 path = strPath.c_str();
312 // Take a chance to create a backup
313 if (!theApp.CreateBackup(FALSE, path))
315 // Write data to an intermediate file
316 String tempPath = env_GetTempPath();
317 String sIntermediateFilename = env_GetTempFileName(tempPath, _T("MRG_"), 0);
318 if (sIntermediateFilename.empty())
319 return E_FAIL; //Nothing to do if even tempfile name fails
320 HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
321 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
322 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
325 DWORD length = GetLength();
326 void *buffer = GetBuffer(length);
330 hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
331 UINT64 mtime = GetLastWriteTime(h);
335 hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
340 hr = SE(DeleteFile(sIntermediateFilename.c_str()));
343 LogErrorString(string_format(_T("DeleteFile(%s) failed: %s"),
344 sIntermediateFilename.c_str(), GetSysError(hr).c_str()));
352 IHexEditorWindow::Status *CHexMergeView::GetStatus()
354 return m_pif->get_status();
358 * @brief Get modified flag
360 BOOL CHexMergeView::GetModified()
362 return m_pif->get_status()->iFileChanged;
366 * @brief Set modified flag
368 void CHexMergeView::SetModified(BOOL bModified)
370 m_pif->get_status()->iFileChanged = bModified;
374 * @brief Get readonly flag
376 BOOL CHexMergeView::GetReadOnly()
378 return m_pif->get_settings()->bReadOnly;
382 * @brief Set readonly flag
384 void CHexMergeView::SetReadOnly(BOOL bReadOnly)
386 m_pif->get_settings()->bReadOnly = bReadOnly;
390 * @brief Allow the control to update all kinds of things that need to be updated when
391 * the window or content buffer have been resized or certain settings have been changed.
393 void CHexMergeView::ResizeWindow()
395 m_pif->resize_window();
399 * @brief Repaint a range of bytes
401 void CHexMergeView::RepaintRange(int i, int j)
403 int iBytesPerLine = m_pif->get_settings()->iBytesPerLine;
404 m_pif->repaint(i / iBytesPerLine, j / iBytesPerLine);
408 * @brief Find a sequence of bytes
410 void CHexMergeView::OnEditFind()
416 * @brief Find & replace a sequence of bytes
418 void CHexMergeView::OnEditReplace()
420 m_pif->CMD_replace();
424 * @brief Repeat last find in one or another direction
426 void CHexMergeView::OnEditRepeat()
428 if (GetKeyState(VK_SHIFT) < 0)
429 m_pif->CMD_findprev();
431 m_pif->CMD_findnext();
435 * @brief Cut selected content
437 void CHexMergeView::OnEditCut()
439 m_pif->CMD_edit_cut();
443 * @brief Copy selected content
445 void CHexMergeView::OnEditCopy()
447 m_pif->CMD_edit_copy();
451 * @brief Paste clipboard content over selected content
453 void CHexMergeView::OnEditPaste()
455 m_pif->CMD_edit_paste();
459 * @brief Select entire content
461 void CHexMergeView::OnEditSelectAll()
463 m_pif->CMD_select_all();
467 * @brief Clear selected content
469 void CHexMergeView::OnEditClear()
471 m_pif->CMD_edit_clear();
475 * @brief Check for keyboard commands
477 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
479 if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
481 if (pMsg->message == WM_KEYDOWN)
483 // Close window in response to VK_ESCAPE if user has allowed it from options
484 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetBool(OPT_CLOSE_WITH_ESC))
486 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
490 return m_pif->translate_accelerator(pMsg);
494 * @brief Go to first diff
496 void CHexMergeView::OnFirstdiff()
498 m_pif->select_next_diff(TRUE);
502 * @brief Go to last diff
504 void CHexMergeView::OnLastdiff()
506 m_pif->select_prev_diff(TRUE);
510 * @brief Go to next diff
512 void CHexMergeView::OnNextdiff()
514 m_pif->select_next_diff(FALSE);
518 * @brief Go to previous diff
520 void CHexMergeView::OnPrevdiff()
522 m_pif->select_prev_diff(FALSE);
525 void CHexMergeView::ZoomText(int amount)
527 m_pif->CMD_zoom(amount);