OSDN Git Service

Merge with stable
[winmerge-jp/winmerge-jp.git] / Src / HexMergeView.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997-2000  Thingamahoochie Software
4 //    Author: Dean Grimm
5 //
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.
10 //
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.
15 //
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.
19 //
20 /////////////////////////////////////////////////////////////////////////////
21 /** 
22  * @file  HexMergeView.cpp
23  *
24  * @brief Implementation file for CHexMergeView
25  *
26  */
27 // ID line follows -- this is updated by SVN
28 // $Id: HexMergeView.cpp 7165 2010-05-15 14:04:43Z jtuc $
29
30 #include "stdafx.h"
31 #include "HexMergeFrm.h"
32 #include "Merge.h"
33 #include "MainFrm.h"
34 #include "HexMergeView.h"
35 #include "OptionsDef.h"
36 #include "OptionsMgr.h"
37 #include "Environment.h"
38
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #undef THIS_FILE
42 static char THIS_FILE[] = __FILE__;
43 #endif
44
45 /**
46  * @brief Turn bool api result into success/error code
47  */
48 static HRESULT NTAPI SE(BOOL f)
49 {
50         if (f)
51                 return S_OK;
52         HRESULT hr = (HRESULT)::GetLastError();
53         ASSERT(hr);
54         if (hr == 0)
55                 hr = E_UNEXPECTED;
56         return hr;
57 }
58
59 static UINT64 NTAPI GetLastWriteTime(HANDLE h)
60 {
61         UINT64 ft;
62         return ::GetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft)) ? ft : 0;
63 }
64
65 static void NTAPI SetLastWriteTime(HANDLE h, UINT64 ft)
66 {
67         ::SetFileTime(h, 0, 0, reinterpret_cast<FILETIME *>(&ft));
68 }
69
70 /////////////////////////////////////////////////////////////////////////////
71 // CHexMergeView
72
73 IMPLEMENT_DYNCREATE(CHexMergeView, CView)
74
75 BEGIN_MESSAGE_MAP(CHexMergeView, CView)
76         //{{AFX_MSG_MAP(CHexMergeView)
77         ON_MESSAGE_VOID(WM_PAINT, CWnd::OnPaint)
78         ON_WM_CREATE()
79         ON_WM_HSCROLL()
80         ON_WM_VSCROLL()
81         ON_WM_NCCALCSIZE()
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)
94         //}}AFX_MSG_MAP
95         // Test case to verify WM_COMMAND won't accidentally go through Default()
96         //ON_COMMAND(ID_APP_ABOUT, Default)
97 END_MESSAGE_MAP()
98
99 /////////////////////////////////////////////////////////////////////////////
100 // CHexMergeView construction/destruction
101
102 /**
103  * @brief Constructor.
104  */
105 CHexMergeView::CHexMergeView()
106 : m_pif(0)
107 , m_nThisPane(0)
108 , m_mtime(0)
109 , m_size(0)
110 {
111 }
112
113 /**
114  * @brief Drawing is not supported
115  */
116 void CHexMergeView::OnDraw(CDC *)
117 {
118         ASSERT(FALSE);
119 }
120
121 /**
122  * @brief Load heksedit.dll and setup window class name
123  */
124 BOOL CHexMergeView::PreCreateWindow(CREATESTRUCT& cs)
125 {
126         static void *pv = NULL;
127         if (pv == NULL)
128         {
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)))
132                 {
133                         pv = LoadLibrary(_T("Frhed\\hekseditU.dll"));
134                         if (!pv)
135                                 LangMessageBox(IDS_FRHED_NOTINSTALLED, MB_OK);
136                 }
137         }
138         cs.lpszClass = _T("heksedit");
139         cs.style |= WS_HSCROLL | WS_VSCROLL;
140         return TRUE;
141 }
142
143 /**
144  * @brief Grab the control's IHexEditorWindow interface pointer upon window creation
145  */
146 int CHexMergeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
147 {
148         if (CView::OnCreate(lpCreateStruct) == -1)
149                 return -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)
152                 return -1;
153         return 0;
154 }
155
156 /**
157  * @brief Skip default WM_NCCALCSIZE processing so as to prevent scrollbars from showing up
158  */
159 void CHexMergeView::OnNcCalcSize(BOOL, NCCALCSIZE_PARAMS *)
160 {
161 }
162
163 /**
164  * @brief Synchronize all involved scrollbars
165  */
166 void CHexMergeView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
167 {
168         SCROLLINFO si;
169         if (pScrollBar && nSBCode == SB_THUMBTRACK)
170         {
171                 pScrollBar->GetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
172                 si.nPos = si.nTrackPos;
173                 SetScrollInfo(SB_HORZ, &si);
174         }
175         CView::OnHScroll(nSBCode, nPos, pScrollBar);
176         if (pScrollBar)
177         {
178                 GetScrollInfo(SB_HORZ, &si, SIF_ALL | SIF_DISABLENOSCROLL);
179                 if (nSBCode != SB_THUMBTRACK)
180                 {
181                         pScrollBar->SetScrollInfo(&si);
182                 }
183                 
184                 CSplitterWndEx *pSplitter = static_cast<CSplitterWndEx *>(GetParentSplitter(this, TRUE));
185                 for (int pane = 0; pane < pSplitter->GetColumnCount(); ++pane)
186                 {
187                         if (pane != m_nThisPane)
188                         {
189                                 CWnd *pWnd = pSplitter->GetDlgItem(pSplitter->IdFromRowCol(0, pane));
190                                 pWnd->SetScrollInfo(SB_HORZ, &si);
191                                 pWnd->SendMessage(WM_HSCROLL, MAKEWPARAM(nSBCode, nPos));
192                         }
193                 }
194         }
195 }
196
197 /**
198  * @brief Synchronize all involved scrollbars
199  */
200 void CHexMergeView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar * pScrollBar)
201 {
202         SCROLLINFO si;
203         if (pScrollBar && nSBCode == SB_THUMBTRACK)
204         {
205                 pScrollBar->GetScrollInfo(&si);
206                 si.nPos = si.nTrackPos;
207                 SetScrollInfo(SB_VERT, &si, SIF_ALL | SIF_DISABLENOSCROLL);
208         }
209         CView::OnVScroll(nSBCode, nPos, pScrollBar);
210         if (pScrollBar && nSBCode != SB_THUMBTRACK)
211         {
212                 GetScrollInfo(SB_VERT, &si);
213                 pScrollBar->SetScrollInfo(&si, SIF_ALL | SIF_DISABLENOSCROLL);
214         }
215 }
216
217 /**
218  * @brief Synchronize file path bar activation states
219  */
220 void CHexMergeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
221 {
222         CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
223         CHexMergeFrame *pFrameWnd = static_cast<CHexMergeFrame *>(GetParentFrame());
224         pFrameWnd->GetHeaderInterface()->SetActive(m_nThisPane, !!bActivate);
225 }
226
227 /**
228  * @brief Get pointer to control's content buffer
229  */
230 BYTE *CHexMergeView::GetBuffer(int length)
231 {
232         return m_pif->get_buffer(length);
233 }
234
235 /**
236  * @brief Get length of control's content buffer
237  */
238 int CHexMergeView::GetLength()
239 {
240         return m_pif->get_length();
241 }
242
243 /**
244  * @brief Checks if file has changed since last update
245  * @param [in] path File to check
246  * @return TRUE if file is changed.
247  */
248 BOOL CHexMergeView::IsFileChangedOnDisk(LPCTSTR path)
249 {
250         // NB: FileTimes are measured in 100 nanosecond intervals since 1601-01-01.
251         BOOL bChanged = FALSE;
252         HANDLE h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE,
253                 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
254         if (h != INVALID_HANDLE_VALUE)
255         {
256                 UINT64 mtime = GetLastWriteTime(h);
257                 UINT64 lower = min(mtime, m_mtime);
258                 UINT64 upper = max(mtime, m_mtime);
259                 BOOL bIgnoreSmallDiff = GetOptionsMgr()->GetBool(OPT_IGNORE_SMALL_FILETIME);
260                 UINT64 tolerance = bIgnoreSmallDiff ? SmallTimeDiff * 10000000 : 0;
261                 bChanged = upper - lower > tolerance || m_size != GetFileSize(h, 0);
262                 CloseHandle(h);
263         }
264         return bChanged;
265 }
266
267 /**
268  * @brief Load file
269  */
270 HRESULT CHexMergeView::LoadFile(LPCTSTR path)
271 {
272         HANDLE h = CreateFile(path, GENERIC_READ,
273                 FILE_SHARE_READ | FILE_SHARE_WRITE,
274                 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
275         HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
276         if (hr != S_OK)
277                 return hr;
278         m_mtime = GetLastWriteTime(h);
279         DWORD length = m_size = GetFileSize(h, 0);
280         hr = SE(length != INVALID_FILE_SIZE);
281         if (hr == S_OK)
282         {
283                 if (void *buffer = GetBuffer(length))
284                 {
285                         DWORD cb = 0;
286                         hr = SE(ReadFile(h, buffer, length, &cb, 0) && cb == length);
287                         if (hr != S_OK)
288                                 GetBuffer(0);
289                 }
290                 else if (length != 0)
291                 {
292                         hr = E_OUTOFMEMORY;
293                 }
294         }
295         CloseHandle(h);
296         return hr;
297 }
298
299 /**
300  * @brief Save file
301  */
302 HRESULT CHexMergeView::SaveFile(LPCTSTR path)
303 {
304         // Warn user in case file has been changed by someone else
305         if (IsFileChangedOnDisk(path))
306         {
307                 String msg = string_format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path);
308                 if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO)
309                         return S_OK;
310         }
311         // Ask user what to do about FILE_ATTRIBUTE_READONLY
312         String strPath = path;
313         BOOL bApplyToAll = FALSE;
314         if (theApp.HandleReadonlySave(strPath, FALSE, bApplyToAll) == IDCANCEL)
315                 return S_OK;
316         path = strPath.c_str();
317         // Take a chance to create a backup
318         if (!theApp.CreateBackup(FALSE, path))
319                 return S_OK;
320         // Write data to an intermediate file
321         String tempPath = env_GetTempPath();
322         String sIntermediateFilename = env_GetTempFileName(tempPath, _T("MRG_"), 0);
323         if (sIntermediateFilename.empty())
324                 return E_FAIL; //Nothing to do if even tempfile name fails
325         HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
326                 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
327         HRESULT hr = SE(h != INVALID_HANDLE_VALUE);
328         if (hr != S_OK)
329                 return hr;
330         DWORD length = GetLength();
331         void *buffer = GetBuffer(length);
332         if (buffer == 0)
333                 return E_POINTER;
334         DWORD cb = 0;
335         hr = SE(WriteFile(h, buffer, length, &cb, 0) && cb == length);
336         UINT64 mtime = GetLastWriteTime(h);
337         CloseHandle(h);
338         if (hr != S_OK)
339                 return hr;
340         hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE));
341         if (hr != S_OK)
342                 return hr;
343         m_mtime = mtime;
344         SetModified(FALSE);
345         hr = SE(DeleteFile(sIntermediateFilename.c_str()));
346         if (hr != S_OK)
347         {
348                 LogErrorString(string_format(_T("DeleteFile(%s) failed: %s"),
349                         sIntermediateFilename.c_str(), GetSysError(hr).c_str()));
350         }
351         return S_OK;
352 }
353
354 /**
355  * @brief Get status
356  */
357 IHexEditorWindow::Status *CHexMergeView::GetStatus()
358 {
359         return m_pif->get_status();
360 }
361
362 /**
363  * @brief Get modified flag
364  */
365 BOOL CHexMergeView::GetModified()
366 {
367         return m_pif->get_status()->iFileChanged;
368 }
369
370 /**
371  * @brief Set modified flag
372  */
373 void CHexMergeView::SetModified(BOOL bModified)
374 {
375         m_pif->get_status()->iFileChanged = bModified;
376 }
377
378 /**
379  * @brief Get readonly flag
380  */
381 BOOL CHexMergeView::GetReadOnly()
382 {
383         return m_pif->get_settings()->bReadOnly;
384 }
385
386 /**
387  * @brief Set readonly flag
388  */
389 void CHexMergeView::SetReadOnly(BOOL bReadOnly)
390 {
391         m_pif->get_settings()->bReadOnly = bReadOnly;
392 }
393
394 /**
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.
397  */
398 void CHexMergeView::ResizeWindow()
399 {
400         m_pif->resize_window();
401 }
402
403 /**
404  * @brief Repaint a range of bytes
405  */
406 void CHexMergeView::RepaintRange(int i, int j)
407 {
408         int iBytesPerLine = m_pif->get_settings()->iBytesPerLine;
409         m_pif->repaint(i / iBytesPerLine, j / iBytesPerLine);
410 }
411
412 /**
413  * @brief Find a sequence of bytes
414  */
415 void CHexMergeView::OnEditFind()
416 {
417         m_pif->CMD_find();
418 }
419
420 /**
421  * @brief Find & replace a sequence of bytes
422  */
423 void CHexMergeView::OnEditReplace()
424 {
425         m_pif->CMD_replace();
426 }
427
428 /**
429  * @brief Repeat last find in one or another direction
430  */
431 void CHexMergeView::OnEditRepeat()
432 {
433         if (GetKeyState(VK_SHIFT) < 0)
434                 m_pif->CMD_findprev();
435         else
436                 m_pif->CMD_findnext();
437 }
438
439 /**
440  * @brief Cut selected content
441  */
442 void CHexMergeView::OnEditCut()
443 {
444         m_pif->CMD_edit_cut();
445 }
446
447 /**
448  * @brief Copy selected content
449  */
450 void CHexMergeView::OnEditCopy()
451 {
452         m_pif->CMD_edit_copy();
453 }
454
455 /**
456  * @brief Paste clipboard content over selected content
457  */
458 void CHexMergeView::OnEditPaste()
459 {
460         m_pif->CMD_edit_paste();
461 }
462
463 /**
464  * @brief Select entire content
465  */
466 void CHexMergeView::OnEditSelectAll()
467 {
468         m_pif->CMD_select_all();
469 }
470
471 /**
472  * @brief Clear selected content
473  */
474 void CHexMergeView::OnEditClear()
475 {
476         m_pif->CMD_edit_clear();
477 }
478
479 /**
480  * @brief Check for keyboard commands
481  */
482 BOOL CHexMergeView::PreTranslateMessage(MSG* pMsg)
483 {
484         if (GetTopLevelFrame()->PreTranslateMessage(pMsg))
485                 return TRUE;
486         if (pMsg->message == WM_KEYDOWN)
487         {
488                 // Close window in response to VK_ESCAPE if user has allowed it from options
489                 if (pMsg->wParam == VK_ESCAPE && GetOptionsMgr()->GetBool(OPT_CLOSE_WITH_ESC))
490                 {
491                         GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
492                         return TRUE;
493                 }
494         }
495         return m_pif->translate_accelerator(pMsg);
496 }
497
498 /**
499  * @brief Go to first diff
500  */
501 void CHexMergeView::OnFirstdiff()
502 {
503         m_pif->select_next_diff(TRUE);
504 }
505
506 /**
507  * @brief Go to last diff
508  */
509 void CHexMergeView::OnLastdiff()
510 {
511         m_pif->select_prev_diff(TRUE);
512 }
513
514 /**
515  * @brief Go to next diff
516  */
517 void CHexMergeView::OnNextdiff()
518 {
519         m_pif->select_next_diff(FALSE);
520 }
521
522 /**
523  * @brief Go to previous diff
524  */
525 void CHexMergeView::OnPrevdiff()
526 {
527         m_pif->select_prev_diff(FALSE);
528 }
529
530 void CHexMergeView::ZoomText(int amount)
531 {
532         m_pif->CMD_zoom(amount);
533 }