OSDN Git Service

Binary Compare: Fix the problem that the file cannot be saved after creating a new one
[winmerge-jp/winmerge-jp.git] / Src / HexMergeDoc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997-2000  Thingamahoochie Software
4 //    Author: Dean Grimm
5 //    SPDX-License-Identifier: GPL-2.0-or-later
6 /////////////////////////////////////////////////////////////////////////////
7 /** 
8  * @file  HexMergeDoc.cpp
9  *
10  * @brief Implementation file for CHexMergeDoc
11  *
12  */
13
14 #include "stdafx.h"
15 #include "HexMergeDoc.h"
16 #include <afxinet.h>
17 #include "UnicodeString.h"
18 #include "HexMergeFrm.h"
19 #include "HexMergeView.h"
20 #include "DiffItem.h"
21 #include "FolderCmp.h"
22 #include "DiffContext.h"        // FILE_SAME
23 #include "DirDoc.h"
24 #include "DirActions.h"
25 #include "OptionsDef.h"
26 #include "DiffFileInfo.h"
27 #include "SaveClosingDlg.h"
28 #include "DiffList.h"
29 #include "paths.h"
30 #include "OptionsMgr.h"
31 #include "FileOrFolderSelect.h"
32 #include "DiffWrapper.h"
33 #include "SyntaxColors.h"
34 #include "Merge.h"
35 #include "Constants.h"
36 #include "MainFrm.h"
37
38 #ifdef _DEBUG
39 #define new DEBUG_NEW
40 #endif
41
42 int CHexMergeDoc::m_nBuffersTemp = 2;
43
44 static void UpdateDiffItem(int nBuffers, DIFFITEM &di, CDiffContext *pCtxt);
45 static int Try(HRESULT hr, UINT type = MB_OKCANCEL|MB_ICONSTOP);
46
47 /**
48  * @brief Update diff item
49  */
50 static void UpdateDiffItem(int nBuffers, DIFFITEM &di, CDiffContext *pCtxt)
51 {
52         di.diffcode.setSideNone();
53         for (int nBuffer = 0; nBuffer < nBuffers; nBuffer++)
54         {
55                 di.diffFileInfo[nBuffer].ClearPartial();
56                 if (pCtxt->UpdateInfoFromDiskHalf(di, nBuffer))
57                         di.diffcode.diffcode |= DIFFCODE::FIRST << nBuffer;
58         }
59         // Clear flags
60         di.diffcode.diffcode &= ~(DIFFCODE::TEXTFLAGS | DIFFCODE::COMPAREFLAGS | DIFFCODE::COMPAREFLAGS3WAY);
61         // Really compare
62         FolderCmp folderCmp(pCtxt);
63         di.diffcode.diffcode |= folderCmp.prepAndCompareFiles(di);
64 }
65
66 /**
67  * @brief Issue an error popup if passed in HRESULT is nonzero
68  */
69 static int Try(HRESULT hr, UINT type)
70 {
71         return hr ? CInternetException(hr).ReportError(type) : 0;
72 }
73
74 /////////////////////////////////////////////////////////////////////////////
75 // CHexMergeDoc
76
77 IMPLEMENT_DYNCREATE(CHexMergeDoc, CDocument)
78
79 BEGIN_MESSAGE_MAP(CHexMergeDoc, CDocument)
80         //{{AFX_MSG_MAP(CHexMergeDoc)
81         ON_COMMAND(ID_FILE_SAVE, OnFileSave)
82         ON_COMMAND(ID_FILE_SAVE_LEFT, OnFileSaveLeft)
83         ON_COMMAND(ID_FILE_SAVE_RIGHT, OnFileSaveRight)
84         ON_COMMAND(ID_FILE_SAVEAS_LEFT, OnFileSaveAsLeft)
85         ON_COMMAND(ID_FILE_SAVEAS_RIGHT, OnFileSaveAsRight)
86         ON_UPDATE_COMMAND_UI(ID_STATUS_DIFFNUM, OnUpdateStatusNum)
87         ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_LEFT, OnUpdateFileSaveLeft)
88         ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_RIGHT, OnUpdateFileSaveRight)
89         ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
90         ON_COMMAND(ID_RESCAN, OnFileReload)
91         ON_COMMAND(ID_L2R, OnL2r)
92         ON_COMMAND(ID_R2L, OnR2l)
93         ON_COMMAND(ID_COPY_FROM_LEFT, OnCopyFromLeft)
94         ON_COMMAND(ID_COPY_FROM_RIGHT, OnCopyFromRight)
95         ON_COMMAND(ID_ALL_LEFT, OnAllLeft)
96         ON_COMMAND(ID_ALL_RIGHT, OnAllRight)
97         ON_COMMAND(ID_VIEW_ZOOMIN, OnViewZoomIn)
98         ON_COMMAND(ID_VIEW_ZOOMOUT, OnViewZoomOut)
99         ON_COMMAND(ID_VIEW_ZOOMNORMAL, OnViewZoomNormal)
100         ON_COMMAND(ID_REFRESH, OnRefresh)
101         ON_COMMAND_RANGE(ID_MERGE_COMPARE_TEXT, ID_MERGE_COMPARE_IMAGE, OnFileRecompareAs)
102         ON_UPDATE_COMMAND_UI_RANGE(ID_MERGE_COMPARE_TEXT, ID_MERGE_COMPARE_IMAGE, OnUpdateFileRecompareAs)
103         //}}AFX_MSG_MAP
104 END_MESSAGE_MAP()
105
106 /////////////////////////////////////////////////////////////////////////////
107 // CHexMergeDoc construction/destruction
108
109 /**
110  * @brief Constructor.
111  */
112 CHexMergeDoc::CHexMergeDoc()
113 : m_pDirDoc(nullptr)
114 , m_nBuffers(m_nBuffersTemp)
115 , m_pView{}
116 , m_nBufferType{BUFFERTYPE::NORMAL, BUFFERTYPE::NORMAL, BUFFERTYPE::NORMAL}
117 {
118         m_filePaths.SetSize(m_nBuffers);
119 }
120
121 /**
122  * @brief Destructor.
123  *
124  * Informs associated dirdoc that mergedoc is closing.
125  */
126 CHexMergeDoc::~CHexMergeDoc()
127 {       
128         if (m_pDirDoc != nullptr)
129                 m_pDirDoc->MergeDocClosing(this);
130 }
131
132 /**
133  * @brief Return active merge edit view (or left one if neither active)
134  */
135 CHexMergeView * CHexMergeDoc::GetActiveMergeView() const
136 {
137         CView * pActiveView = GetParentFrame()->GetActiveView();
138         CHexMergeView * pHexMergeView = dynamic_cast<CHexMergeView *>(pActiveView);
139         if (pHexMergeView == nullptr)
140                 pHexMergeView = m_pView[0]; // default to left view (in case some location or detail view active)
141         return pHexMergeView;
142 }
143
144 /**
145  * @brief Update associated diff item
146  */
147 int CHexMergeDoc::UpdateDiffItem(CDirDoc *pDirDoc)
148 {
149         // If directory compare has results
150         if (pDirDoc != nullptr && pDirDoc->HasDiffs())
151         {
152                 CDiffContext &ctxt = pDirDoc->GetDiffContext();
153                 if (DIFFITEM *pos = FindItemFromPaths(ctxt, m_filePaths))
154                 {
155                         DIFFITEM &di = ctxt.GetDiffRefAt(pos);
156                         ::UpdateDiffItem(m_nBuffers, di, &ctxt);
157                 }
158         }
159         bool bDiff = false;
160         int lengthFirst = m_pView[0]->GetLength();
161         void *bufferFirst = m_pView[0]->GetBuffer(lengthFirst);
162         for (int nBuffer = 1; nBuffer < m_nBuffers; nBuffer++)
163         {
164                 int length = m_pView[nBuffer]->GetLength();
165                 if (lengthFirst != length)
166                         bDiff = true;
167                 else
168                 {
169                         void *buffer = m_pView[nBuffer]->GetBuffer(length);
170                         bDiff = (memcmp(bufferFirst, buffer, lengthFirst) != 0);
171                 }
172                 if (bDiff)
173                         break;
174         }
175         GetParentFrame()->SetLastCompareResult(bDiff);
176         return bDiff ? 1 : 0;
177 }
178
179 /**
180  * @brief Asks and then saves modified files
181  */
182 bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
183 {
184         bool bLModified = false, bMModified = false, bRModified = false;
185
186         if (m_nBuffers == 3)
187         {
188                 bLModified = m_pView[0]->GetModified();
189                 bMModified = m_pView[1]->GetModified();
190                 bRModified = m_pView[2]->GetModified();
191         }
192         else
193         {
194                 bLModified = m_pView[0]->GetModified();
195                 bRModified = m_pView[1]->GetModified();
196         }
197         if (!bLModified && !bMModified && !bRModified)
198                  return true;
199
200         const String &pathLeft = m_filePaths.GetLeft();
201         const String &pathMiddle = m_filePaths.GetMiddle();
202         const String &pathRight = m_filePaths.GetRight();
203
204         bool result = true;
205         bool bLSaveSuccess = false, bMSaveSuccess = false, bRSaveSuccess = false;
206
207         SaveClosingDlg dlg;
208         dlg.DoAskFor(bLModified, bMModified, bRModified);
209         if (!bAllowCancel)
210                 dlg.m_bDisableCancel = true;
211         if (!pathLeft.empty())
212                 dlg.m_sLeftFile = pathLeft;
213         else
214                 dlg.m_sLeftFile = m_strDesc[0];
215         if (m_nBuffers == 3)
216         {
217                 if (!pathMiddle.empty())
218                         dlg.m_sMiddleFile = pathMiddle;
219                 else
220                         dlg.m_sMiddleFile = m_strDesc[1];
221         }
222         if (!pathRight.empty())
223                 dlg.m_sRightFile = pathRight;
224         else
225                 dlg.m_sRightFile = m_strDesc[1];
226
227         if (dlg.DoModal() == IDOK)
228         {
229                 if (bLModified)
230                 {
231                         if (dlg.m_leftSave == SaveClosingDlg::SAVECLOSING_SAVE)
232                         {
233                                 bLSaveSuccess = DoFileSave(0);
234                                 if (!bLSaveSuccess)
235                                         result = false;
236                         }
237                         else
238                         {
239                                 m_pView[0]->SetSavePoint();
240                         }
241                 }
242                 if (bMModified)
243                 {
244                         if (dlg.m_middleSave == SaveClosingDlg::SAVECLOSING_SAVE)
245                         {
246                                 bMSaveSuccess = DoFileSave(1);
247                                 if (!bMSaveSuccess)
248                                         result = false;
249                         }
250                         else
251                         {
252                                 m_pView[1]->SetSavePoint();
253                         }
254                 }
255                 if (bRModified)
256                 {
257                         if (dlg.m_rightSave == SaveClosingDlg::SAVECLOSING_SAVE)
258                         {
259                                 bRSaveSuccess = DoFileSave(m_nBuffers - 1);
260                                 if (!bRSaveSuccess)
261                                         result = false;
262                         }
263                         else
264                         {
265                                 m_pView[m_nBuffers - 1]->SetSavePoint();
266                         }
267                 }
268         }
269         else
270         {       
271                 result = false;
272         }
273
274         // If file were modified and saving was successfull,
275         // update status on dir view
276         if (bLSaveSuccess || bMSaveSuccess || bRSaveSuccess)
277         {
278                 UpdateDiffItem(m_pDirDoc);
279         }
280
281         return result;
282 }
283
284 /**
285  * @brief Save modified documents
286  */
287 BOOL CHexMergeDoc::SaveModified()
288 {
289         return PromptAndSaveIfNeeded(true);
290 }
291
292 /**
293  * @brief Saves both files
294  */
295 void CHexMergeDoc::OnFileSave() 
296 {
297         for (int nBuffer = 0; nBuffer < m_nBuffers; nBuffer++)
298                 DoFileSave(nBuffer);
299 }
300
301 bool CHexMergeDoc::DoFileSave(int nBuffer)
302 {
303         bool result = false;
304         if (m_pView[nBuffer]->GetModified())
305         {
306                 if (m_nBufferType[nBuffer] == BUFFERTYPE::UNNAMED)
307                         result = DoFileSaveAs(nBuffer);
308                 else
309                 {
310                         const String &path = m_filePaths.GetPath(nBuffer);
311                         HRESULT hr = m_pView[nBuffer]->SaveFile(path.c_str());
312                         if (Try(hr) == IDCANCEL)
313                                 return false;
314                         if (FAILED(hr))
315                                 return DoFileSaveAs(nBuffer);
316                         result = true;
317                         if (result)
318                                 UpdateDiffItem(m_pDirDoc);
319                 }
320         }
321         return result;
322 }
323
324 bool CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
325 {
326         const String &path = m_filePaths.GetPath(nBuffer);
327         String strPath;
328         String title;
329         if (nBuffer == 0)
330                 title = _("Save Left File As");
331         else if (nBuffer == m_nBuffers - 1)
332                 title = _("Save Right File As");
333         else
334                 title = _("Save Middle File As");
335         if (SelectFile(AfxGetMainWnd()->GetSafeHwnd(), strPath, false, path.c_str(), title))
336         {
337                 HRESULT hr = m_pView[nBuffer]->SaveFile(strPath.c_str());
338                 if (Try(hr) == IDCANCEL)
339                         return false;
340                 if (FAILED(hr))
341                         return false;
342                 if (path.empty())
343                 {
344                         // We are saving scratchpad (unnamed file)
345                         m_nBufferType[nBuffer] = BUFFERTYPE::UNNAMED_SAVED;
346                         m_strDesc[nBuffer].erase();
347                 }
348
349                 m_filePaths.SetPath(nBuffer, strPath);
350                 UpdateDiffItem(m_pDirDoc);
351                 UpdateHeaderPath(nBuffer);
352                 return true;
353         }
354         return false;
355 }
356
357 /**
358  * @brief Saves left-side file
359  */
360 void CHexMergeDoc::OnFileSaveLeft()
361 {
362         DoFileSave(0);
363 }
364
365 /**
366  * @brief Saves middle-side file
367  */
368 void CHexMergeDoc::OnFileSaveMiddle()
369 {
370         DoFileSave(1);
371 }
372
373 /**
374  * @brief Saves right-side file
375  */
376 void CHexMergeDoc::OnFileSaveRight()
377 {
378         DoFileSave(m_nBuffers - 1);
379 }
380
381 /**
382  * @brief Saves left-side file with name asked
383  */
384 void CHexMergeDoc::OnFileSaveAsLeft()
385 {
386         DoFileSaveAs(0);
387 }
388
389 /**
390  * @brief Saves right-side file with name asked
391  */
392 void CHexMergeDoc::OnFileSaveAsMiddle()
393 {
394         DoFileSaveAs(1);
395 }
396
397 /**
398  * @brief Saves right-side file with name asked
399  */
400 void CHexMergeDoc::OnFileSaveAsRight()
401 {
402         DoFileSaveAs(m_nBuffers - 1);
403 }
404
405 /**
406  * @brief Update diff-number pane text
407  */
408 void CHexMergeDoc::OnUpdateStatusNum(CCmdUI* pCmdUI) 
409 {
410         String s;
411         pCmdUI->SetText(s.c_str());
412 }
413
414 /**
415  * @brief DirDoc gives us its identity just after it creates us
416  */
417 void CHexMergeDoc::SetDirDoc(CDirDoc * pDirDoc)
418 {
419         ASSERT(pDirDoc != nullptr && m_pDirDoc == nullptr);
420         m_pDirDoc = pDirDoc;
421 }
422
423 /**
424  * @brief Return pointer to parent frame
425  */
426 CHexMergeFrame * CHexMergeDoc::GetParentFrame() const
427 {
428         return static_cast<CHexMergeFrame *>(m_pView[0]->GetParentFrame()); 
429 }
430
431 /**
432  * @brief DirDoc is closing
433  */
434 void CHexMergeDoc::DirDocClosing(CDirDoc * pDirDoc)
435 {
436         ASSERT(m_pDirDoc == pDirDoc);
437         m_pDirDoc = nullptr;
438 }
439
440 /**
441  * @brief DirDoc commanding us to close
442  */
443 bool CHexMergeDoc::CloseNow()
444 {
445         // Allow user to cancel closing
446         if (!PromptAndSaveIfNeeded(true))
447                 return false;
448
449         GetParentFrame()->CloseNow();
450         return true;
451 }
452
453 /**
454 * @brief Load one file
455 */
456 HRESULT CHexMergeDoc::LoadOneFile(int index, LPCTSTR filename, bool readOnly, const String& strDesc)
457 {
458         if (filename[0])
459         {
460                 if (Try(m_pView[index]->LoadFile(filename), MB_ICONSTOP) != 0)
461                         return E_FAIL;
462                 m_pView[index]->SetReadOnly(readOnly);
463                 m_filePaths.SetPath(index, filename);
464                 ASSERT(m_nBufferType[index] == BUFFERTYPE::NORMAL); // should have been initialized to BUFFERTYPE::NORMAL in constructor
465                 if (!strDesc.empty())
466                 {
467                         m_strDesc[index] = strDesc;
468                         m_nBufferType[index] = BUFFERTYPE::NORMAL_NAMED;
469                 }
470         }
471         else
472         {
473                 m_nBufferType[index] = BUFFERTYPE::UNNAMED;
474                 m_strDesc[index] = strDesc;
475         }
476         UpdateHeaderPath(index);
477         m_pView[index]->ResizeWindow();
478         return S_OK;
479 }
480
481 /**
482  * @brief Load files and initialize frame's compare result icon
483  */
484 bool CHexMergeDoc::OpenDocs(int nFiles, const FileLocation fileloc[], const bool bRO[], const String strDesc[])
485 {
486         CHexMergeFrame *pf = GetParentFrame();
487         ASSERT(pf != nullptr);
488         bool bSucceeded = true;
489         int nNormalBuffer = 0;
490         int nBuffer;
491         for (nBuffer = 0; nBuffer < nFiles; nBuffer++)
492         {
493                 if (FAILED(LoadOneFile(nBuffer, fileloc[nBuffer].filepath.c_str(), bRO[nBuffer], strDesc ? strDesc[nBuffer] : _T(""))))
494                 {
495                         bSucceeded = false;
496                         break;
497                 }
498                 if (m_nBufferType[nBuffer] == BUFFERTYPE::NORMAL || m_nBufferType[nBuffer] == BUFFERTYPE::NORMAL_NAMED)
499                         ++nNormalBuffer;
500         }
501         if (nBuffer == nFiles)
502         {
503                 // An extra ResizeWindow() on the left view aligns scroll ranges, and
504                 // also triggers initial diff coloring by invalidating the client area.
505                 m_pView[0]->ResizeWindow();
506
507                 if (nNormalBuffer > 0)
508                         OnRefresh();
509                 else
510                         UpdateDiffItem(m_pDirDoc);
511         }
512         else
513         {
514                 // Use verify macro to trap possible error in debug.
515                 VERIFY(pf->DestroyWindow());
516         }
517         return bSucceeded;
518 }
519
520 void CHexMergeDoc::MoveOnLoad(int nPane, int)
521 {
522         if (nPane < 0)
523         {
524                 nPane = GetOptionsMgr()->GetInt(OPT_ACTIVE_PANE);
525                 if (nPane < 0 || nPane >= m_nBuffers)
526                         nPane = 0;
527         }
528
529         GetParentFrame()->SetActivePane(nPane);
530
531         if (GetOptionsMgr()->GetBool(OPT_SCROLL_TO_FIRST))
532                 m_pView[0]->SendMessage(WM_COMMAND, ID_FIRSTDIFF);
533 }
534
535 void CHexMergeDoc::CheckFileChanged(void)
536 {
537         for (int pane = 0; pane < m_nBuffers; ++pane)
538         {
539                 if (m_pView[pane]->IsFileChangedOnDisk(m_filePaths[pane].c_str()) == FileChange::Changed)
540                 {
541                         String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge scanned it last time.\n\nDo you want to reload the file?"), m_filePaths[pane]);
542                         if (AfxMessageBox(msg.c_str(), MB_YESNO | MB_ICONWARNING) == IDYES)
543                         {
544                                 OnFileReload();
545                         }
546                         break;
547                 }
548         }
549 }
550
551 /**
552  * @brief Write path and filename to headerbar
553  * @note SetText() does not repaint unchanged text
554  */
555 void CHexMergeDoc::UpdateHeaderPath(int pane)
556 {
557         CHexMergeFrame *pf = GetParentFrame();
558         ASSERT(pf != nullptr);
559         String sText;
560
561         if (m_nBufferType[pane] == BUFFERTYPE::UNNAMED ||
562                 m_nBufferType[pane] == BUFFERTYPE::NORMAL_NAMED)
563         {
564                 sText = m_strDesc[pane];
565         }
566         else
567         {
568                 sText = m_filePaths.GetPath(pane);
569                 if (m_pDirDoc != nullptr)
570                         m_pDirDoc->ApplyDisplayRoot(pane, sText);
571         }
572         if (m_pView[pane]->GetModified())
573                 sText.insert(0, _T("* "));
574         pf->GetHeaderInterface()->SetText(pane, sText);
575
576         SetTitle(nullptr);
577 }
578
579
580 /**
581  * @brief Customize a heksedit control's settings
582  */
583 static void Customize(IHexEditorWindow::Settings *settings)
584 {
585         settings->bSaveIni = false;
586         //settings->iAutomaticBPL = false;
587         //settings->iBytesPerLine = 16;
588         //settings->iFontSize = 8;
589 }
590
591 /**
592  * @brief Customize a heksedit control's colors
593  */
594 static void Customize(IHexEditorWindow::Colors *colors)
595 {
596         COptionsMgr *pOptionsMgr = GetOptionsMgr();
597         colors->iSelBkColorValue = RGB(224, 224, 224);
598         colors->iDiffBkColorValue = pOptionsMgr->GetInt(OPT_CLR_DIFF);
599         colors->iSelDiffBkColorValue = pOptionsMgr->GetInt(OPT_CLR_SELECTED_DIFF);
600         colors->iDiffTextColorValue = pOptionsMgr->GetInt(OPT_CLR_DIFF_TEXT);
601         if (colors->iDiffTextColorValue == 0xFFFFFFFF)
602                 colors->iDiffTextColorValue = 0;
603         colors->iSelDiffTextColorValue = pOptionsMgr->GetInt(OPT_CLR_SELECTED_DIFF_TEXT);
604         if (colors->iSelDiffTextColorValue == 0xFFFFFFFF)
605                 colors->iSelDiffTextColorValue = 0;
606         SyntaxColors *pSyntaxColors = theApp.GetMainSyntaxColors();
607         colors->iTextColorValue = pSyntaxColors->GetColor(COLORINDEX_NORMALTEXT);
608         colors->iBkColorValue = pSyntaxColors->GetColor(COLORINDEX_BKGND);
609         colors->iSelTextColorValue = pSyntaxColors->GetColor(COLORINDEX_SELTEXT);
610         colors->iSelBkColorValue = pSyntaxColors->GetColor(COLORINDEX_SELBKGND);
611 }
612
613 /**
614  * @brief Customize a heksedit control's settings and colors
615  */
616 static void Customize(IHexEditorWindow *pif)
617 {
618         Customize(pif->get_settings());
619         Customize(pif->get_colors());
620         //LANGID wLangID = (LANGID)GetThreadLocale();
621         //pif->load_lang(wLangID);
622 }
623
624 void CHexMergeDoc::RefreshOptions()
625 {
626         for (int nBuffer = 0; nBuffer < m_nBuffers; nBuffer++)
627         {
628                 IHexEditorWindow *pif = m_pView[nBuffer]->GetInterface();
629                 pif->read_ini_data();
630                 Customize(pif);
631                 pif->resize_window();
632         }
633 }
634
635 /**
636  * @brief Update document filenames to title
637  */
638 void CHexMergeDoc::SetTitle(LPCTSTR lpszTitle)
639 {
640         String sTitle = (lpszTitle != nullptr) ? lpszTitle : CMergeFrameCommon::GetTitleString(m_filePaths, m_strDesc);
641         CDocument::SetTitle(sTitle.c_str());
642 }
643
644 /**
645  * @brief We have two child views (left & right), so we keep pointers directly
646  * at them (the MFC view list doesn't have them both)
647  */
648 void CHexMergeDoc::SetMergeViews(CHexMergeView *pView[])
649 {
650         for (int nBuffer = 0; nBuffer < m_nBuffers; nBuffer++)
651         {
652                 ASSERT(pView[nBuffer] != nullptr && m_pView[nBuffer] == nullptr);
653                 m_pView[nBuffer] = pView[nBuffer];
654                 m_pView[nBuffer]->m_nThisPane = nBuffer;
655         }
656 }
657
658 /**
659  * @brief Called when "Save left" item is updated
660  */
661 void CHexMergeDoc::OnUpdateFileSaveLeft(CCmdUI* pCmdUI)
662 {
663         pCmdUI->Enable(m_pView[0]->GetModified());
664 }
665
666 /**
667  * @brief Called when "Save middle" item is updated
668  */
669 void CHexMergeDoc::OnUpdateFileSaveMiddle(CCmdUI* pCmdUI)
670 {
671         pCmdUI->Enable(m_nBuffers == 3 && m_pView[1]->GetModified());
672 }
673
674 /**
675  * @brief Called when "Save right" item is updated
676  */
677 void CHexMergeDoc::OnUpdateFileSaveRight(CCmdUI* pCmdUI)
678 {
679         pCmdUI->Enable(m_pView[m_nBuffers - 1]->GetModified());
680 }
681
682 /**
683  * @brief Called when "Save" item is updated
684  */
685 void CHexMergeDoc::OnUpdateFileSave(CCmdUI* pCmdUI)
686 {
687         bool bModified = false;
688         for (int nBuffer = 0; nBuffer < m_nBuffers; nBuffer++)
689                 bModified |= m_pView[nBuffer]->GetModified();
690         pCmdUI->Enable(bModified);
691 }
692
693 /**
694  * @brief Reloads the opened files
695  */
696 void CHexMergeDoc::OnFileReload()
697 {
698         if (!PromptAndSaveIfNeeded(true))
699                 return;
700         
701         FileLocation fileloc[3];
702         bool bRO[3];
703         for (int pane = 0; pane < m_nBuffers; pane++)
704         {
705                 fileloc[pane].setPath(m_filePaths[pane]);
706                 bRO[pane] = m_pView[pane]->GetReadOnly();
707         }
708         if (!OpenDocs(m_nBuffers, fileloc, bRO, m_strDesc))
709                 return;
710         MoveOnLoad(GetActiveMergeView()->m_nThisPane);
711 }
712
713 /**
714  * @brief Copy selected bytes from left to right
715  */
716 void CHexMergeDoc::OnL2r()
717 {
718         int dstPane = (GetActiveMergeView()->m_nThisPane < m_nBuffers - 1) ? GetActiveMergeView()->m_nThisPane + 1 : m_nBuffers - 1;
719         int srcPane = dstPane - 1;
720         CHexMergeView::CopySel(m_pView[srcPane], m_pView[dstPane]);
721 }
722
723 /**
724  * @brief Copy selected bytes from right to left
725  */
726 void CHexMergeDoc::OnR2l()
727 {
728         int dstPane = (GetActiveMergeView()->m_nThisPane > 0) ? GetActiveMergeView()->m_nThisPane - 1 : 0;
729         int srcPane = dstPane + 1;
730         CHexMergeView::CopySel(m_pView[srcPane], m_pView[dstPane]);
731 }
732
733 /**
734  * @brief Copy selected bytes from left to active pane
735  */
736 void CHexMergeDoc::OnCopyFromLeft()
737 {
738         int dstPane = GetActiveMergeView()->m_nThisPane;
739         int srcPane = (dstPane - 1 < 0) ? 0 : dstPane - 1;
740         CHexMergeView::CopySel(m_pView[srcPane], m_pView[dstPane]);
741 }
742
743 /**
744  * @brief Copy selected bytes from right to active pane
745  */
746 void CHexMergeDoc::OnCopyFromRight()
747 {
748         int dstPane = GetActiveMergeView()->m_nThisPane;
749         int srcPane = (dstPane + 1 > m_nBuffers - 1) ? m_nBuffers - 1 : dstPane + 1;
750         CHexMergeView::CopySel(m_pView[srcPane], m_pView[dstPane]);
751 }
752
753 /**
754  * @brief Copy all bytes from left to right
755  */
756 void CHexMergeDoc::OnAllRight()
757 {
758         int dstPane = (GetActiveMergeView()->m_nThisPane < m_nBuffers - 1) ? GetActiveMergeView()->m_nThisPane + 1 : m_nBuffers - 1;
759         int srcPane = dstPane - 1;
760         CHexMergeView::CopyAll(m_pView[srcPane], m_pView[dstPane]);
761 }
762
763 /**
764  * @brief Copy all bytes from right to left
765  */
766 void CHexMergeDoc::OnAllLeft()
767 {
768         int dstPane = (GetActiveMergeView()->m_nThisPane > 0) ? GetActiveMergeView()->m_nThisPane - 1 : 0;
769         int srcPane = dstPane + 1;
770         CHexMergeView::CopyAll(m_pView[srcPane], m_pView[dstPane]);
771 }
772
773 /**
774  * @brief Called when user selects View/Zoom In from menu.
775  */
776 void CHexMergeDoc::OnViewZoomIn()
777 {
778         for (int pane = 0; pane < m_nBuffers; pane++)
779                 m_pView[pane]->ZoomText(1);
780 }
781
782 /**
783  * @brief Called when user selects View/Zoom Out from menu.
784  */
785 void CHexMergeDoc::OnViewZoomOut()
786 {
787         for (int pane = 0; pane < m_nBuffers; pane++)
788                 m_pView[pane]->ZoomText(-1);
789 }
790
791 /**
792  * @brief Called when user selects View/Zoom Normal from menu.
793  */
794 void CHexMergeDoc::OnViewZoomNormal()
795 {
796         for (int pane = 0; pane < m_nBuffers; pane++)
797                 m_pView[pane]->ZoomText(0);
798 }
799
800 void CHexMergeDoc::OnRefresh()
801 {
802         if (UpdateDiffItem(m_pDirDoc) == 0)
803         {
804                 CMergeFrameCommon::ShowIdenticalMessage(m_filePaths, true,
805                         [](LPCTSTR msg, UINT flags, UINT id) -> int { return AfxMessageBox(msg, flags, id); });
806         }
807 }
808
809 void CHexMergeDoc::OnFileRecompareAs(UINT nID)
810 {
811         FileLocation fileloc[3];
812         DWORD dwFlags[3];
813         String strDesc[3];
814         int nBuffers = m_nBuffers;
815         CDirDoc *pDirDoc = m_pDirDoc->GetMainView() ? m_pDirDoc : 
816                 static_cast<CDirDoc*>(theApp.m_pDirTemplate->CreateNewDocument());
817         for (int nBuffer = 0; nBuffer < nBuffers; ++nBuffer)
818         {
819                 fileloc[nBuffer].setPath(m_filePaths[nBuffer]);
820                 dwFlags[nBuffer] = m_pView[nBuffer]->GetReadOnly() ? FFILEOPEN_READONLY : 0;
821                 strDesc[nBuffer] = m_strDesc[nBuffer];
822         }
823         CloseNow();
824         GetMainFrame()->ShowMergeDoc(nID, pDirDoc, nBuffers, fileloc, dwFlags, strDesc);
825 }
826
827 void CHexMergeDoc::OnUpdateFileRecompareAs(CCmdUI* pCmdUI)
828 {
829         pCmdUI->Enable(pCmdUI->m_nID != ID_MERGE_COMPARE_XML);
830 }
831