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 CHexMergeView
29 #include "HexMergeFrm.h"
32 #include "HexMergeView.h"
33 #include "OptionsDef.h"
34 #include "OptionsMgr.h"
35 #include "Environment.h"
42 * @brief Turn bool api result into success/error code
44 static HRESULT NTAPI SE(BOOL f)
48 HRESULT hr = (HRESULT)::GetLastError();
55 static UINT64 NTAPI GetLastWriteTime(HANDLE h)
58 return ::GetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft)) ? ft : 0;
61 static void NTAPI SetLastWriteTime(HANDLE h, UINT64 ft)
63 ::SetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft));
66 /////////////////////////////////////////////////////////////////////////////
69 IMPLEMENT_DYNCREATE(CHexMergeView, CView)
71 BEGIN_MESSAGE_MAP(CHexMergeView, CView)
72 //{{AFX_MSG_MAP(CHexMergeView)
73 ON_MESSAGE_VOID(WM_PAINT, CWnd::OnPaint)
78 ON_COMMAND(ID_EDIT_FIND, OnEditFind)
79 ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
80 ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
81 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
82 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
83 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
84 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
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()
112 * @brief Drawing is not supported
114 void CHexMergeView::OnDraw(CDC *)
120 * @brief Load heksedit.dll and setup window class name
122 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
124 static void *pv = NULL;
127 static const CLSID clsid = { 0xBCA3CA6B, 0xCC6B, 0x4F79,
128 { 0xA2, 0xC2, 0xDD, 0xBE, 0x86, 0x4B, 0x1C, 0x90 } };
129 if (FAILED(::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, IID_IUnknown, &pv)))
131 pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
133 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
136 cs.lpszClass = _T("heksedit");
137 cs.style |= WS_HSCROLL | WS_VSCROLL;
142 * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
144 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
146 if (CView::OnCreate(lpCreateStruct) == -1)
148 m_pif = reinterpret_cast<IHexEditorWindow *>(::GetWindowLongPtr(m_hWnd, GWLP_USERDATA));
149 if (m_pif == 0 || m_pif->get_interface_version() < HEKSEDIT_INTERFACE_VERSION)
155 * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
157 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
162 * @brief Synchronize all involved scrollbars
164 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
167 if (pScrollBar && nSBCode == SB_THUMBTRACK)
169 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
170 si.nPos = si.nTrackPos;
171 SetScrollInfo(SB_HORZ, &si);
173 CView::OnHScroll(nSBCode, nPos, pScrollBar);
176 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
177 if (nSBCode != SB_THUMBTRACK)
179 pScrollBar->SetScrollInfo(&si);
182 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
183 for (int pane = 0; pane < pSplitter->GetColumnCount(); ++pane)
185 if (pane != m_nThisPane)
187 CWnd *pWnd = pSplitter->GetDlgItem(pSplitter->IdFromRowCol(0, pane));
188 pWnd->SetScrollInfo(SB_HORZ, &si);
189 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
196 * @brief Synchronize all involved scrollbars
198 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
201 if (pScrollBar && nSBCode == SB_THUMBTRACK)
203 pScrollBar->GetScrollInfo(&si);
204 si.nPos = si.nTrackPos;
205 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
207 CView::OnVScroll(nSBCode, nPos, pScrollBar);
208 if (pScrollBar && nSBCode != SB_THUMBTRACK)
210 GetScrollInfo(SB_VERT, &si);
211 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
216 * @brief Synchronize file path bar activation states
218 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
220 CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
221 CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
222 pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
226 * @brief Get pointer to control's content buffer
228 BYTE *CHexMergeView::GetBuffer(int length)
230 return m_pif->get_buffer(length);
234 * @brief Get length of control's content buffer
236 int CHexMergeView::GetLength()
238 return m_pif->get_length();
242 * @brief Checks if file has changed since last update
243 * @param [in] path File to check
244 * @return TRUE if file is changed.
246 BOOL CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
251 if (GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME))
252 tolerance = SmallTimeDiff; // From MainFrm.h
253 int64_t timeDiff = dfi.mtime - m_fileInfo.mtime;
254 if (timeDiff < 0) timeDiff = -timeDiff;
255 if ((timeDiff > tolerance * Poco::Timestamp::resolution()) || (dfi.size != m_fileInfo.size))
263 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
265 HANDLE h = CreateFile(path, GENERIC_READ,
266 FILE_SHARE_READ | FILE_SHARE_WRITE,
267 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
268 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
269 if (h == INVALID_HANDLE_VALUE)
271 DWORD length = GetFileSize(h, 0);
272 hr = SE(length != INVALID_FILE_SIZE);
275 if (void *buffer = GetBuffer(length))
278 hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
282 else if (length != 0)
288 m_fileInfo.Update(path);
295 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
297 // Warn user in case file has been changed by someone else
298 if (IsFileChangedOnDisk(path))
300 String msg = string_format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
301 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
304 // Ask user what to do about FILE_ATTRIBUTE_READONLY
305 String strPath = path;
306 BOOL bApplyToAll = FALSE;
307 if (theApp.HandleReadonlySave(strPath, FALSE, bApplyToAll) == IDCANCEL)
309 path = strPath.c_str();
310 // Take a chance to create a backup
311 if (!theApp.CreateBackup(FALSE, path))
313 // Write data to an intermediate file
314 String tempPath = env_GetTempPath();
315 String sIntermediateFilename = env_GetTempFileName(tempPath, _T("MRG_"), 0);
316 if (sIntermediateFilename.empty())
317 return E_FAIL; //Nothing to do if even tempfile name fails
318 HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
319 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
320 HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
321 if (h == INVALID_HANDLE_VALUE)
323 DWORD length = GetLength();
324 void *buffer = GetBuffer(length);
331 hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
335 hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
338 m_fileInfo.Update(path);
340 hr = SE(DeleteFile(sIntermediateFilename.c_str()));
343 LogErrorString(string_format(_T("DeleteFile(%s) failed: %s"),
344 sIntermediateFilename.c_str(), GetSysError(hr).c_str()));
350 * @brief Get modified flag
352 BOOL CHexMergeView::GetModified()
354 return m_pif->get_modified();
358 * @brief Set modified flag
360 void CHexMergeView::SetSavePoint()
362 m_pif->set_savepoint();
366 * @brief Clear undo records
368 void CHexMergeView::ClearUndoRecords()
370 m_pif->clear_undorecords();
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 Find a sequence of bytes
401 void CHexMergeView::OnEditFind()
407 * @brief Find & replace a sequence of bytes
409 void CHexMergeView::OnEditReplace()
411 m_pif->CMD_replace();
415 * @brief Repeat last find in one or another direction
417 void CHexMergeView::OnEditRepeat()
419 if (GetKeyState(VK_SHIFT) < 0)
420 m_pif->CMD_findprev();
422 m_pif->CMD_findnext();
426 * @brief Called when "Undo" item is updated
428 void CHexMergeView::OnUpdateEditUndo(CCmdUI* pCmdUI)
430 pCmdUI->Enable(m_pif->can_undo());
434 * @brief Undo last action
436 void CHexMergeView::OnEditUndo()
438 m_pif->CMD_edit_undo();
442 * @brief Called when "Redo" item is updated
444 void CHexMergeView::OnUpdateEditRedo(CCmdUI* pCmdUI)
446 pCmdUI->Enable(m_pif->can_redo());
450 * @brief Redo last action
452 void CHexMergeView::OnEditRedo()
454 m_pif->CMD_edit_redo();
458 * @brief Cut selected content
460 void CHexMergeView::OnEditCut()
462 m_pif->CMD_edit_cut();
466 * @brief Copy selected content
468 void CHexMergeView::OnEditCopy()
470 m_pif->CMD_edit_copy();
474 * @brief Paste clipboard content over selected content
476 void CHexMergeView::OnEditPaste()
478 m_pif->CMD_edit_paste();
482 * @brief Select entire content
484 void CHexMergeView::OnEditSelectAll()
486 m_pif->CMD_select_all();
490 * @brief Clear selected content
492 void CHexMergeView::OnEditClear()
494 m_pif->CMD_edit_clear();
498 * @brief Check for keyboard commands
500 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
502 if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
504 if (pMsg->message == WM_KEYDOWN)
506 // Close window in response to VK_ESCAPE if user has allowed it from options
507 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetBool(OPT_CLOSE_WITH_ESC))
509 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
513 return m_pif->translate_accelerator(pMsg);
517 * @brief Go to first diff
519 void CHexMergeView::OnFirstdiff()
521 m_pif->select_next_diff(TRUE);
525 * @brief Go to last diff
527 void CHexMergeView::OnLastdiff()
529 m_pif->select_prev_diff(TRUE);
533 * @brief Go to next diff
535 void CHexMergeView::OnNextdiff()
537 m_pif->select_next_diff(FALSE);
541 * @brief Go to previous diff
543 void CHexMergeView::OnPrevdiff()
545 m_pif->select_prev_diff(FALSE);
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);