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 MergeEditView.cpp
24 * @brief Implementation of the CMergeEditView class
28 #include "MergeEditView.h"
32 #include "LocationView.h"
35 #include "OptionsMgr.h"
36 #include "OptionsDiffColors.h"
37 #include "FileTransform.h"
39 #include "WMGotoDlg.h"
40 #include "OptionsDef.h"
41 #include "SyntaxColors.h"
42 #include "MergeEditFrm.h"
43 #include "MergeLineFlags.h"
45 #include "DropHandler.h"
47 #include "ShellContextMenu.h"
54 using CrystalLineParser::TEXTBLOCK;
56 /** @brief Timer ID for delayed rescan. */
57 const UINT IDT_RESCAN = 2;
58 /** @brief Timer timeout for delayed rescan. */
59 const UINT RESCAN_TIMEOUT = 1000;
61 /** @brief Location for file compare specific help to open. */
62 static TCHAR MergeViewHelpLocation[] = _T("::/htmlhelp/Compare_files.html");
64 /////////////////////////////////////////////////////////////////////////////
67 IMPLEMENT_DYNCREATE(CMergeEditView, CCrystalEditViewEx)
69 CMergeEditView::CMergeEditView()
70 : m_bCurrentLineIsDiff(false)
73 , m_bDetailView(false)
74 , m_piMergeEditStatus(nullptr)
75 , m_bAutomaticRescan(false)
76 , fTimerWaitingForIdle(0)
79 , m_CurrentPredifferID(0)
81 SetParser(&m_xParser);
83 Options::DiffColors::Load(GetOptionsMgr(), m_cachedColors);
86 CMergeEditView::~CMergeEditView()
91 BEGIN_MESSAGE_MAP(CMergeEditView, CCrystalEditViewEx)
92 //{{AFX_MSG_MAP(CMergeEditView)
93 ON_COMMAND(ID_CURDIFF, OnCurdiff)
94 ON_UPDATE_COMMAND_UI(ID_CURDIFF, OnUpdateCurdiff)
95 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
96 ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
97 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
98 ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
99 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
100 ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
101 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
102 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
103 ON_UPDATE_COMMAND_UI(ID_FIRSTDIFF, OnUpdateFirstdiff)
104 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
105 ON_UPDATE_COMMAND_UI(ID_LASTDIFF, OnUpdateLastdiff)
106 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
107 ON_UPDATE_COMMAND_UI(ID_NEXTDIFF, OnUpdateNextdiff)
108 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
109 ON_UPDATE_COMMAND_UI(ID_PREVDIFF, OnUpdatePrevdiff)
110 ON_COMMAND(ID_NEXTCONFLICT, OnNextConflict)
111 ON_UPDATE_COMMAND_UI(ID_NEXTCONFLICT, OnUpdateNextConflict)
112 ON_COMMAND(ID_PREVCONFLICT, OnPrevConflict)
113 ON_UPDATE_COMMAND_UI(ID_PREVCONFLICT, OnUpdatePrevConflict)
114 ON_COMMAND(ID_NEXTDIFFLM, OnNextdiffLM)
115 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLM, OnUpdateNextdiffLM)
116 ON_COMMAND(ID_PREVDIFFLM, OnPrevdiffLM)
117 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLM, OnUpdatePrevdiffLM)
118 ON_COMMAND(ID_NEXTDIFFLR, OnNextdiffLR)
119 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLR, OnUpdateNextdiffLR)
120 ON_COMMAND(ID_PREVDIFFLR, OnPrevdiffLR)
121 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLR, OnUpdatePrevdiffLR)
122 ON_COMMAND(ID_NEXTDIFFMR, OnNextdiffMR)
123 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFMR, OnUpdateNextdiffMR)
124 ON_COMMAND(ID_PREVDIFFMR, OnPrevdiffMR)
125 ON_UPDATE_COMMAND_UI(ID_PREVDIFFMR, OnUpdatePrevdiffMR)
126 ON_COMMAND(ID_NEXTDIFFLO, OnNextdiffLO)
127 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLO, OnUpdateNextdiffLO)
128 ON_COMMAND(ID_PREVDIFFLO, OnPrevdiffLO)
129 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLO, OnUpdatePrevdiffLO)
130 ON_COMMAND(ID_NEXTDIFFMO, OnNextdiffMO)
131 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFMO, OnUpdateNextdiffMO)
132 ON_COMMAND(ID_PREVDIFFMO, OnPrevdiffMO)
133 ON_UPDATE_COMMAND_UI(ID_PREVDIFFMO, OnUpdatePrevdiffMO)
134 ON_COMMAND(ID_NEXTDIFFRO, OnNextdiffRO)
135 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFRO, OnUpdateNextdiffRO)
136 ON_COMMAND(ID_PREVDIFFRO, OnPrevdiffRO)
137 ON_UPDATE_COMMAND_UI(ID_PREVDIFFRO, OnUpdatePrevdiffRO)
138 ON_WM_LBUTTONDBLCLK()
140 ON_COMMAND(ID_ALL_LEFT, OnAllLeft)
141 ON_UPDATE_COMMAND_UI(ID_ALL_LEFT, OnUpdateAllLeft)
142 ON_COMMAND(ID_ALL_RIGHT, OnAllRight)
143 ON_UPDATE_COMMAND_UI(ID_ALL_RIGHT, OnUpdateAllRight)
144 ON_COMMAND(ID_AUTO_MERGE, OnAutoMerge)
145 ON_UPDATE_COMMAND_UI(ID_AUTO_MERGE, OnUpdateAutoMerge)
146 ON_COMMAND(ID_L2R, OnL2r)
147 ON_UPDATE_COMMAND_UI(ID_L2R, OnUpdateL2r)
148 ON_COMMAND(ID_R2L, OnR2l)
149 ON_UPDATE_COMMAND_UI(ID_R2L, OnUpdateR2l)
150 ON_COMMAND(ID_COPY_FROM_LEFT, OnCopyFromLeft)
151 ON_UPDATE_COMMAND_UI(ID_COPY_FROM_LEFT, OnUpdateCopyFromLeft)
152 ON_COMMAND(ID_COPY_FROM_RIGHT, OnCopyFromRight)
153 ON_UPDATE_COMMAND_UI(ID_COPY_FROM_RIGHT, OnUpdateCopyFromRight)
154 ON_COMMAND(ID_ADD_SYNCPOINT, OnAddSyncPoint)
155 ON_COMMAND(ID_CLEAR_SYNCPOINTS, OnClearSyncPoints)
156 ON_UPDATE_COMMAND_UI(ID_CLEAR_SYNCPOINTS, OnUpdateClearSyncPoints)
157 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
158 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
159 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
161 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_LEFT, OnUpdateFileSaveLeft)
162 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_MIDDLE, OnUpdateFileSaveMiddle)
163 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_RIGHT, OnUpdateFileSaveRight)
164 ON_COMMAND(ID_REFRESH, OnRefresh)
165 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
166 ON_COMMAND(ID_SELECTLINEDIFF, OnSelectLineDiff<false>)
167 ON_UPDATE_COMMAND_UI(ID_SELECTPREVLINEDIFF, OnUpdateSelectLineDiff)
168 ON_COMMAND(ID_SELECTPREVLINEDIFF, OnSelectLineDiff<true>)
169 ON_UPDATE_COMMAND_UI(ID_SELECTPREVLINEDIFF, OnUpdateSelectLineDiff)
171 ON_UPDATE_COMMAND_UI(ID_EDIT_REPLACE, OnUpdateEditReplace)
172 ON_COMMAND(ID_FILE_LEFT_READONLY, OnLeftReadOnly)
173 ON_UPDATE_COMMAND_UI(ID_FILE_LEFT_READONLY, OnUpdateLeftReadOnly)
174 ON_COMMAND(ID_FILE_MIDDLE_READONLY, OnMiddleReadOnly)
175 ON_UPDATE_COMMAND_UI(ID_FILE_MIDDLE_READONLY, OnUpdateMiddleReadOnly)
176 ON_COMMAND(ID_FILE_RIGHT_READONLY, OnRightReadOnly)
177 ON_UPDATE_COMMAND_UI(ID_FILE_RIGHT_READONLY, OnUpdateRightReadOnly)
178 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE0FILE_RO, OnUpdateStatusRO)
179 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE1FILE_RO, OnUpdateStatusRO)
180 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE2FILE_RO, OnUpdateStatusRO)
181 ON_COMMAND_RANGE(ID_EOL_TO_DOS, ID_EOL_TO_MAC, OnConvertEolTo)
182 ON_UPDATE_COMMAND_UI_RANGE(ID_EOL_TO_DOS, ID_EOL_TO_MAC, OnUpdateConvertEolTo)
183 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE0FILE_EOL, OnUpdateStatusEOL)
184 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE1FILE_EOL, OnUpdateStatusEOL)
185 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE2FILE_EOL, OnUpdateStatusEOL)
186 ON_COMMAND(ID_L2RNEXT, OnL2RNext)
187 ON_UPDATE_COMMAND_UI(ID_L2RNEXT, OnUpdateL2RNext)
188 ON_COMMAND(ID_R2LNEXT, OnR2LNext)
189 ON_UPDATE_COMMAND_UI(ID_R2LNEXT, OnUpdateR2LNext)
190 ON_COMMAND(ID_WINDOW_CHANGE_PANE, OnChangePane)
191 ON_COMMAND(ID_NEXT_PANE, OnChangePane)
192 ON_COMMAND(ID_EDIT_WMGOTO, OnWMGoto)
193 ON_COMMAND(ID_FILE_SHELLMENU, OnShellMenu)
194 ON_UPDATE_COMMAND_UI(ID_FILE_SHELLMENU, OnUpdateShellMenu)
195 ON_COMMAND_RANGE(ID_SCRIPT_FIRST, ID_SCRIPT_LAST, OnScripts)
196 ON_COMMAND(ID_NO_PREDIFFER, OnNoPrediffer)
197 ON_UPDATE_COMMAND_UI(ID_NO_PREDIFFER, OnUpdateNoPrediffer)
198 ON_COMMAND_RANGE(ID_PREDIFFERS_FIRST, ID_PREDIFFERS_LAST, OnPrediffer)
199 ON_UPDATE_COMMAND_UI_RANGE(ID_PREDIFFERS_FIRST, ID_PREDIFFERS_LAST, OnUpdatePrediffer)
202 ON_COMMAND(ID_EDIT_COPY_LINENUMBERS, OnEditCopyLineNumbers)
203 ON_UPDATE_COMMAND_UI(ID_EDIT_COPY_LINENUMBERS, OnUpdateEditCopyLinenumbers)
204 ON_COMMAND(ID_VIEW_LINEDIFFS, OnViewLineDiffs)
205 ON_UPDATE_COMMAND_UI(ID_VIEW_LINEDIFFS, OnUpdateViewLineDiffs)
206 ON_COMMAND(ID_VIEW_WORDWRAP, OnViewWordWrap)
207 ON_UPDATE_COMMAND_UI(ID_VIEW_WORDWRAP, OnUpdateViewWordWrap)
208 ON_COMMAND(ID_VIEW_LINENUMBERS, OnViewLineNumbers)
209 ON_UPDATE_COMMAND_UI(ID_VIEW_LINENUMBERS, OnUpdateViewLineNumbers)
210 ON_COMMAND(ID_VIEW_WHITESPACE, OnViewWhitespace)
211 ON_UPDATE_COMMAND_UI(ID_VIEW_WHITESPACE, OnUpdateViewWhitespace)
212 ON_COMMAND(ID_FILE_OPEN_REGISTERED, OnOpenFile)
213 ON_COMMAND(ID_FILE_OPEN_WITHEDITOR, OnOpenFileWithEditor)
214 ON_COMMAND(ID_FILE_OPEN_WITH, OnOpenFileWith)
215 ON_COMMAND(ID_VIEW_SWAPPANES, OnViewSwapPanes)
216 ON_UPDATE_COMMAND_UI(ID_NO_EDIT_SCRIPTS, OnUpdateNoEditScripts)
219 ON_COMMAND(ID_HELP, OnHelp)
220 ON_COMMAND(ID_VIEW_FILEMARGIN, OnViewMargin)
221 ON_UPDATE_COMMAND_UI(ID_VIEW_FILEMARGIN, OnUpdateViewMargin)
222 ON_UPDATE_COMMAND_UI(ID_VIEW_CHANGESCHEME, OnUpdateViewChangeScheme)
223 ON_COMMAND_RANGE(ID_COLORSCHEME_FIRST, ID_COLORSCHEME_LAST, OnChangeScheme)
224 ON_UPDATE_COMMAND_UI_RANGE(ID_COLORSCHEME_FIRST, ID_COLORSCHEME_LAST, OnUpdateChangeScheme)
226 ON_COMMAND(ID_VIEW_ZOOMIN, OnViewZoomIn)
227 ON_COMMAND(ID_VIEW_ZOOMOUT, OnViewZoomOut)
228 ON_COMMAND(ID_VIEW_ZOOMNORMAL, OnViewZoomNormal)
229 ON_COMMAND(ID_WINDOW_SPLIT, OnWindowSplit)
230 ON_UPDATE_COMMAND_UI(ID_WINDOW_SPLIT, OnUpdateWindowSplit)
235 /////////////////////////////////////////////////////////////////////////////
236 // CMergeEditView diagnostics
239 CMergeDoc* CMergeEditView::GetDocument() // non-debug version is inline
241 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMergeDoc)));
242 return (CMergeDoc*)m_pDocument;
247 /////////////////////////////////////////////////////////////////////////////
248 // CMergeEditView message handlers
251 * @brief Return text buffer for file in view
253 CCrystalTextBuffer *CMergeEditView::LocateTextBuffer()
255 return GetDocument()->m_ptBuf[m_nThisPane].get();
259 * @brief Update any resources necessary after a GUI language change
261 void CMergeEditView::UpdateResources()
265 CMergeEditView *CMergeEditView::GetGroupView(int nBuffer) const
267 return GetDocument()->GetView(m_nThisGroup, nBuffer);
270 void CMergeEditView::PrimeListWithFile()
272 // Set the tab size now, just in case the options change...
273 // We don't update it at the end of OnOptions,
274 // we can update it safely now
275 SetTabSize(GetOptionsMgr()->GetInt(OPT_TAB_SIZE));
278 * @brief Return text from line given
280 CString CMergeEditView::GetLineText(int idx)
282 return GetLineChars(idx);
286 * @brief Return text from selection
288 CString CMergeEditView::GetSelectedText()
290 CPoint ptStart, ptEnd;
292 GetSelection(ptStart, ptEnd);
293 if (ptStart != ptEnd)
294 GetTextWithoutEmptys(ptStart.y, ptStart.x, ptEnd.y, ptEnd.x, strText);
299 * @brief Get diffs inside selection.
300 * @param [out] firstDiff First diff inside selection
301 * @param [out] lastDiff Last diff inside selection
302 * @param [out] firstWordDiff First word level diff inside selection
303 * @param [out] lastWordDiff Last word level diff inside selection
304 * @note -1 is returned in parameters if diffs cannot be determined
305 * @todo This shouldn't be called when there is no diffs, so replace
306 * first 'if' with ASSERT()?
308 void CMergeEditView::GetFullySelectedDiffs(int & firstDiff, int & lastDiff, int & firstWordDiff, int & lastWordDiff, const CPoint *pptStart, const CPoint *pptEnd)
315 CMergeDoc *pd = GetDocument();
316 const int nDiffs = pd->m_diffList.GetSignificantDiffs();
320 int firstLine, lastLine;
321 CPoint ptStart, ptEnd;
322 GetSelection(ptStart, ptEnd);
323 if (pptStart != nullptr)
325 if (pptEnd != nullptr)
327 firstLine = ptStart.y;
330 firstDiff = pd->m_diffList.LineToDiff(firstLine);
331 bool firstLineIsNotInDiff = firstDiff == -1;
334 firstDiff = pd->m_diffList.NextSignificantDiffFromLine(firstLine);
339 lastDiff = pd->m_diffList.LineToDiff(lastLine);
340 bool lastLineIsNotInDiff = lastDiff == -1;
342 lastDiff = pd->m_diffList.PrevSignificantDiffFromLine(lastLine);
343 if (lastDiff < firstDiff)
350 if (firstDiff != -1 && lastDiff != -1)
354 if (ptStart != ptEnd)
356 VERIFY(pd->m_diffList.GetDiff(firstDiff, di));
357 if (lastLineIsNotInDiff && (firstLineIsNotInDiff || (di.dbegin == firstLine && ptStart.x == 0)))
363 if (firstWordDiff == -1)
365 vector<WordDiff> worddiffs = pd->GetWordDiffArrayInDiffBlock(firstDiff);
366 for (size_t i = 0; i < worddiffs.size(); ++i)
368 if (worddiffs[i].endline[m_nThisPane] > firstLine ||
369 (firstLine == worddiffs[i].endline[m_nThisPane] && worddiffs[i].end[m_nThisPane] - 1 >= ptStart.x))
371 firstWordDiff = static_cast<int>(i);
376 if (firstLine >= di.dbegin && firstWordDiff == -1)
383 VERIFY(pd->m_diffList.GetDiff(lastDiff, di));
384 vector<WordDiff> worddiffs = pd->GetWordDiffArrayInDiffBlock(lastDiff);
385 for (size_t i = worddiffs.size() - 1; i != (size_t)-1; --i)
387 if (worddiffs[i].beginline[m_nThisPane] < lastLine ||
388 (lastLine == worddiffs[i].beginline[m_nThisPane] && worddiffs[i].begin[m_nThisPane] + 1 <= ptEnd.x))
390 lastWordDiff = static_cast<int>(i);
395 if (lastLine <= di.dend && lastWordDiff == -1)
398 if (firstDiff == lastDiff && (lastWordDiff != -1 && lastWordDiff < firstWordDiff))
405 else if (lastDiff < firstDiff || (firstDiff == lastDiff && firstWordDiff == -1 && lastWordDiff == -1))
422 ASSERT(firstDiff == -1 ? (lastDiff == -1 && firstWordDiff == -1 && lastWordDiff == -1) : true);
423 ASSERT(lastDiff == -1 ? (firstDiff == -1 && firstWordDiff == -1 && lastWordDiff == -1) : true);
424 ASSERT(firstDiff != -1 ? firstWordDiff != -1 : true);
427 std::map<int, std::vector<int>> CMergeEditView::GetColumnSelectedWordDiffIndice()
429 CMergeDoc *pDoc = GetDocument();
430 std::map<int, std::vector<int>> ret;
431 std::map<int, std::vector<int> *> list;
432 CPoint ptStart, ptEnd;
433 GetSelection(ptStart, ptEnd);
434 for (int nLine = ptStart.y; nLine <= ptEnd.y; ++nLine)
436 if (pDoc->m_diffList.LineToDiff(nLine) != -1)
438 int firstDiff, lastDiff, firstWordDiff, lastWordDiff;
440 GetColumnSelection(nLine, nLeft, nRight);
441 CPoint ptStart2, ptEnd2;
444 ptStart2.y = ptEnd2.y = nLine;
445 GetFullySelectedDiffs(firstDiff, lastDiff, firstWordDiff, lastWordDiff, &ptStart2, &ptEnd2);
446 if (firstDiff != -1 && lastDiff != -1)
448 std::vector<int> *pWordDiffs;
449 if (list.find(firstDiff) == list.end())
450 list.insert(std::pair<int, std::vector<int> *>(firstDiff, new std::vector<int>()));
451 pWordDiffs = list[firstDiff];
452 for (int i = firstWordDiff; i <= lastWordDiff; ++i)
454 if (pWordDiffs->empty() || i != (*pWordDiffs)[pWordDiffs->size() - 1])
455 pWordDiffs->push_back(i);
460 for (auto& it : list)
461 ret.insert(std::pair<int, std::vector<int>>(it.first, *it.second));
465 void CMergeEditView::OnInitialUpdate()
468 CCrystalEditViewEx::OnInitialUpdate();
470 SetFont(dynamic_cast<CMainFrame*>(AfxGetMainWnd())->m_lfDiff);
471 SetAlternateDropTarget(new DropHandler(std::bind(&CMergeEditView::OnDropFiles, this, std::placeholders::_1)));
477 void CMergeEditView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
479 CCrystalEditViewEx::OnActivateView(bActivate, pActivateView, pDeactiveView);
481 CMergeDoc* pDoc = GetDocument();
482 pDoc->UpdateHeaderActivity(m_nThisPane, !!bActivate);
485 std::vector<TEXTBLOCK> CMergeEditView::GetAdditionalTextBlocks (int nLineIndex)
487 static const std::vector<TEXTBLOCK> emptyBlocks;
490 if (nLineIndex < m_lineBegin || nLineIndex > m_lineEnd)
494 DWORD dwLineFlags = GetLineFlags(nLineIndex);
495 if ((dwLineFlags & LF_SNP) == LF_SNP || (dwLineFlags & LF_DIFF) != LF_DIFF || (dwLineFlags & LF_MOVED) == LF_MOVED)
498 if (!GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT))
501 CMergeDoc *pDoc = GetDocument();
502 if (pDoc->IsEditedAfterRescan(m_nThisPane))
505 int nDiff = pDoc->m_diffList.LineToDiff(nLineIndex);
510 pDoc->m_diffList.GetDiff(nDiff, cd);
511 int unemptyLineCount = 0;
512 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
514 if (cd.begin[nPane] != cd.end[nPane] + 1)
517 if (unemptyLineCount < 2)
520 vector<WordDiff> worddiffs = pDoc->GetWordDiffArray(nLineIndex);
521 size_t nWordDiffs = worddiffs.size();
523 bool lineInCurrentDiff = IsLineInCurrentDiff(nLineIndex);
525 std::vector<TEXTBLOCK> blocks(nWordDiffs * 2 + 1);
526 blocks[0].m_nCharPos = 0;
527 blocks[0].m_nColorIndex = COLORINDEX_NONE;
528 blocks[0].m_nBgColorIndex = COLORINDEX_NONE;
530 for (i = 0, j = 1; i < nWordDiffs; i++)
532 if (worddiffs[i].beginline[m_nThisPane] > nLineIndex || worddiffs[i].endline[m_nThisPane] < nLineIndex )
534 if (pDoc->m_nBuffers > 2)
536 if (m_nThisPane == 0 && worddiffs[i].op == OP_3RDONLY)
538 else if (m_nThisPane == 2 && worddiffs[i].op == OP_1STONLY)
541 int begin[3], end[3];
542 bool deleted = false;
543 for (int pane = 0; pane < pDoc->m_nBuffers; pane++)
545 begin[pane] = (worddiffs[i].beginline[pane] < nLineIndex) ? 0 : worddiffs[i].begin[pane];
546 end[pane] = (worddiffs[i].endline[pane] > nLineIndex) ? GetGroupView(pane)->GetLineLength(nLineIndex) : worddiffs[i].end[pane];
547 if (worddiffs[i].beginline[pane] == worddiffs[i].endline[pane] &&
548 worddiffs[i].begin[pane] == worddiffs[i].end[pane])
551 blocks[j].m_nCharPos = begin[m_nThisPane];
552 if (lineInCurrentDiff)
554 if (m_cachedColors.clrSelDiffText != CLR_NONE)
555 blocks[j].m_nColorIndex = COLORINDEX_HIGHLIGHTTEXT1 | COLORINDEX_APPLYFORCE;
557 blocks[j].m_nColorIndex = COLORINDEX_NONE;
558 blocks[j].m_nBgColorIndex = COLORINDEX_APPLYFORCE |
559 (deleted ? COLORINDEX_HIGHLIGHTBKGND4 : COLORINDEX_HIGHLIGHTBKGND1);
563 if (m_cachedColors.clrDiffText != CLR_NONE)
564 blocks[j].m_nColorIndex = COLORINDEX_HIGHLIGHTTEXT2 | COLORINDEX_APPLYFORCE;
566 blocks[j].m_nColorIndex = COLORINDEX_NONE;
567 blocks[j].m_nBgColorIndex = COLORINDEX_APPLYFORCE |
568 (deleted ? COLORINDEX_HIGHLIGHTBKGND3 : COLORINDEX_HIGHLIGHTBKGND2);
571 blocks[j].m_nCharPos = end[m_nThisPane];
572 blocks[j].m_nColorIndex = COLORINDEX_NONE;
573 blocks[j].m_nBgColorIndex = COLORINDEX_NONE;
582 COLORREF CMergeEditView::GetColor(int nColorIndex)
584 switch (nColorIndex & ~COLORINDEX_APPLYFORCE)
586 case COLORINDEX_HIGHLIGHTBKGND1:
587 return m_cachedColors.clrSelWordDiff;
588 case COLORINDEX_HIGHLIGHTTEXT1:
589 return m_cachedColors.clrSelWordDiffText;
590 case COLORINDEX_HIGHLIGHTBKGND2:
591 return m_cachedColors.clrWordDiff;
592 case COLORINDEX_HIGHLIGHTTEXT2:
593 return m_cachedColors.clrWordDiffText;
594 case COLORINDEX_HIGHLIGHTBKGND3:
595 return m_cachedColors.clrWordDiffDeleted;
596 case COLORINDEX_HIGHLIGHTBKGND4:
597 return m_cachedColors.clrSelWordDiffDeleted;
600 return CCrystalTextView::GetColor(nColorIndex);
605 * @brief Determine text and background color for line
606 * @param [in] nLineIndex Index of line in view (NOT line in file)
607 * @param [out] crBkgnd Backround color for line
608 * @param [out] crText Text color for line
610 void CMergeEditView::GetLineColors(int nLineIndex, COLORREF & crBkgnd,
611 COLORREF & crText, bool & bDrawWhitespace)
613 DWORD ignoreFlags = 0;
614 GetLineColors2(nLineIndex, ignoreFlags, crBkgnd, crText, bDrawWhitespace);
618 * @brief Determine text and background color for line
619 * @param [in] nLineIndex Index of line in view (NOT line in file)
620 * @param [in] ignoreFlags Flags that caller wishes ignored
621 * @param [out] crBkgnd Backround color for line
622 * @param [out] crText Text color for line
624 * This version allows caller to suppress particular flags
626 void CMergeEditView::GetLineColors2(int nLineIndex, DWORD ignoreFlags, COLORREF & crBkgnd,
627 COLORREF & crText, bool & bDrawWhitespace)
629 if (GetLineCount() <= nLineIndex)
632 DWORD dwLineFlags = GetLineFlags(nLineIndex);
634 if (dwLineFlags & ignoreFlags)
635 dwLineFlags &= (~ignoreFlags);
639 // Line with WinMerge flag,
640 // Lines with only the LF_DIFF/LF_TRIVIAL flags are not colored with Winmerge colors
641 if (dwLineFlags & (LF_WINMERGE_FLAGS & ~LF_DIFF & ~LF_TRIVIAL & ~LF_MOVED & ~LF_SNP))
643 crText = m_cachedColors.clrDiffText;
644 bDrawWhitespace = true;
646 if (dwLineFlags & LF_GHOST)
648 crBkgnd = m_cachedColors.clrDiffDeleted;
653 // If no syntax hilighting
654 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
656 crBkgnd = GetColor (COLORINDEX_BKGND);
657 crText = GetColor (COLORINDEX_NORMALTEXT);
658 bDrawWhitespace = false;
661 // Line not inside diff, get colors from CrystalEditor
662 CCrystalEditViewEx::GetLineColors(nLineIndex, crBkgnd,
663 crText, bDrawWhitespace);
665 if (nLineIndex < m_lineBegin || nLineIndex > m_lineEnd)
667 crBkgnd = GetColor (COLORINDEX_WHITESPACE);
668 crText = GetColor (COLORINDEX_WHITESPACE);
669 bDrawWhitespace = false;
675 if (dwLineFlags & LF_WINMERGE_FLAGS)
677 crText = m_cachedColors.clrDiffText;
678 bDrawWhitespace = true;
679 bool lineInCurrentDiff = IsLineInCurrentDiff(nLineIndex);
681 if (dwLineFlags & LF_SNP)
683 if (lineInCurrentDiff)
685 if (dwLineFlags & LF_GHOST)
686 crBkgnd = m_cachedColors.clrSelSNPDeleted;
688 crBkgnd = m_cachedColors.clrSelSNP;
689 crText = m_cachedColors.clrSelSNPText;
693 if (dwLineFlags & LF_GHOST)
694 crBkgnd = m_cachedColors.clrSNPDeleted;
696 crBkgnd = m_cachedColors.clrSNP;
697 crText = m_cachedColors.clrSNPText;
701 else if (dwLineFlags & LF_DIFF)
703 if (lineInCurrentDiff)
705 if (dwLineFlags & LF_MOVED)
707 if (dwLineFlags & LF_GHOST)
708 crBkgnd = m_cachedColors.clrSelMovedDeleted;
710 crBkgnd = m_cachedColors.clrSelMoved;
711 crText = m_cachedColors.clrSelMovedText;
715 crBkgnd = m_cachedColors.clrSelDiff;
716 crText = m_cachedColors.clrSelDiffText;
722 if (dwLineFlags & LF_MOVED)
724 if (dwLineFlags & LF_GHOST)
725 crBkgnd = m_cachedColors.clrMovedDeleted;
727 crBkgnd = m_cachedColors.clrMoved;
728 crText = m_cachedColors.clrMovedText;
732 crBkgnd = m_cachedColors.clrDiff;
733 crText = m_cachedColors.clrDiffText;
738 else if (dwLineFlags & LF_TRIVIAL)
740 // trivial diff can not be selected
741 if (dwLineFlags & LF_GHOST)
742 // ghost lines in trivial diff has their own color
743 crBkgnd = m_cachedColors.clrTrivialDeleted;
745 crBkgnd = m_cachedColors.clrTrivial;
746 crText = m_cachedColors.clrTrivialText;
749 else if (dwLineFlags & LF_GHOST)
751 if (lineInCurrentDiff)
752 crBkgnd = m_cachedColors.clrSelDiffDeleted;
754 crBkgnd = m_cachedColors.clrDiffDeleted;
760 // Line not inside diff,
761 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
763 // If no syntax hilighting, get windows default colors
764 crBkgnd = GetColor (COLORINDEX_BKGND);
765 crText = GetColor (COLORINDEX_NORMALTEXT);
766 bDrawWhitespace = false;
769 // Syntax highlighting, get colors from CrystalEditor
770 CCrystalEditViewEx::GetLineColors(nLineIndex, crBkgnd,
771 crText, bDrawWhitespace);
776 * @brief Sync other pane position
778 void CMergeEditView::UpdateSiblingScrollPos (bool bHorz)
780 CSplitterWnd *pSplitterWnd = GetParentSplitter (this, false);
781 if (pSplitterWnd != nullptr)
783 // See CSplitterWnd::IdFromRowCol() implementation for details
784 int nCurrentRow = (GetDlgCtrlID () - AFX_IDW_PANE_FIRST) / 16;
785 int nCurrentCol = (GetDlgCtrlID () - AFX_IDW_PANE_FIRST) % 16;
786 ASSERT (nCurrentRow >= 0 && nCurrentRow < pSplitterWnd->GetRowCount ());
787 ASSERT (nCurrentCol >= 0 && nCurrentCol < pSplitterWnd->GetColumnCount ());
789 // limit the TopLine : must be smaller than GetLineCount for all the panels
790 int newTopSubLine = m_nTopSubLine;
791 int nRows = pSplitterWnd->GetRowCount ();
792 int nCols = pSplitterWnd->GetColumnCount ();
794 // for (nRow = 0; nRow < nRows; nRow++)
796 // for (int nCol = 0; nCol < nCols; nCol++)
798 // CMergeEditView *pSiblingView = static_cast<CMergeEditView*>(GetSiblingView (nRow, nCol));
799 // if (pSiblingView != nullptr)
800 // if (pSiblingView->GetSubLineCount() <= newTopSubLine)
801 // newTopSubLine = pSiblingView->GetSubLineCount()-1;
804 if (m_nTopSubLine != newTopSubLine)
805 ScrollToSubLine(newTopSubLine);
807 for (nRow = 0; nRow < nRows; nRow++)
809 for (int nCol = 0; nCol < nCols; nCol++)
811 if (!(nRow == nCurrentRow && nCol == nCurrentCol)) // We don't need to update ourselves
813 CMergeEditView *pSiblingView = static_cast<CMergeEditView*>(GetSiblingView (nRow, nCol));
814 if (pSiblingView != nullptr && pSiblingView->m_nThisGroup == m_nThisGroup)
815 pSiblingView->OnUpdateSibling (this, bHorz);
823 * @brief Update other panes
825 void CMergeEditView::OnUpdateSibling (CCrystalTextView * pUpdateSource, bool bHorz)
827 if (pUpdateSource != this)
829 ASSERT (pUpdateSource != nullptr);
830 ASSERT_KINDOF (CCrystalTextView, pUpdateSource);
831 CMergeEditView *pSrcView = static_cast<CMergeEditView*>(pUpdateSource);
832 if (!bHorz) // changed this so bHorz works right
834 ASSERT (pSrcView->m_nTopSubLine >= 0);
836 // This ASSERT is wrong: panes have different files and
837 // different linecounts
838 // ASSERT (pSrcView->m_nTopLine < GetLineCount ());
839 if (pSrcView->m_nTopSubLine != m_nTopSubLine)
841 ScrollToSubLine (pSrcView->m_nTopSubLine, true, false);
843 RecalcVertScrollBar(true);
844 RecalcHorzScrollBar();
849 ASSERT (pSrcView->m_nOffsetChar >= 0);
851 // This ASSERT is wrong: panes have different files and
852 // different linelengths
853 // ASSERT (pSrcView->m_nOffsetChar < GetMaxLineLength ());
854 if (pSrcView->m_nOffsetChar != m_nOffsetChar)
856 ScrollToChar (pSrcView->m_nOffsetChar, true, false);
858 RecalcHorzScrollBar(true);
859 RecalcHorzScrollBar();
865 void CMergeEditView::OnDisplayDiff(int nDiff /*=0*/)
867 int newlineBegin, newlineEnd;
868 CMergeDoc *pd = GetDocument();
869 if (nDiff < 0 || nDiff >= pd->m_diffList.GetSize())
877 VERIFY(pd->m_diffList.GetDiff(nDiff, curDiff));
879 newlineBegin = curDiff.dbegin;
880 ASSERT (newlineBegin >= 0);
881 newlineEnd = curDiff.dend;
884 if (newlineBegin == m_lineBegin && newlineEnd == m_lineEnd)
886 m_lineBegin = newlineBegin;
887 m_lineEnd = newlineEnd;
889 // scroll to the first line of the diff
890 ScrollToLine(m_lineBegin);
892 // tell the others views about this diff (no need to call UpdateSiblingScrollPos)
893 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
895 // pSplitterWnd is `nullptr` if WinMerge started minimized.
896 if (pSplitterWnd != nullptr)
898 int nRows = pSplitterWnd->GetRowCount ();
899 int nCols = pSplitterWnd->GetColumnCount ();
900 for (int nRow = 0; nRow < nRows; nRow++)
902 for (int nCol = 0; nCol < nCols; nCol++)
904 CMergeEditView *pSiblingView = static_cast<CMergeEditView*>(GetSiblingView (nRow, nCol));
905 if (pSiblingView != nullptr)
906 pSiblingView->OnDisplayDiff(nDiff);
911 // update the width of the horizontal scrollbar
912 RecalcHorzScrollBar();
916 * @brief Selects diff by number and syncs other file
917 * @param [in] nDiff Diff to select, must be >= 0
918 * @param [in] bScroll Scroll diff to view
919 * @param [in] bSelectText Select diff text
920 * @sa CMergeEditView::ShowDiff()
921 * @sa CMergeDoc::SetCurrentDiff()
922 * @todo Parameter bSelectText is never used?
924 void CMergeEditView::SelectDiff(int nDiff, bool bScroll /*= true*/, bool bSelectText /*= true*/)
926 CMergeDoc *pd = GetDocument();
928 // Check that nDiff is valid
930 _RPTF1(_CRT_ERROR, "Diffnumber negative (%d)", nDiff);
931 if (nDiff >= pd->m_diffList.GetSize())
932 _RPTF2(_CRT_ERROR, "Selected diff > diffcount (%d >= %d)",
933 nDiff, pd->m_diffList.GetSize());
936 pd->SetCurrentDiff(nDiff);
937 ShowDiff(bScroll, bSelectText);
938 pd->UpdateAllViews(this);
939 UpdateSiblingScrollPos(false);
941 // notify either side, as it will notify the other one
942 pd->ForEachView (0, [&](auto& pView) { if (pView->m_bDetailView) pView->OnDisplayDiff(nDiff); });
946 * @brief Called when user selects "Current Difference".
947 * Goes to active diff. If no active diff, selects diff under cursor
948 * @sa CMergeEditView::SelectDiff()
949 * @sa CMergeDoc::GetCurrentDiff()
950 * @sa CMergeDoc::LineToDiff()
952 void CMergeEditView::OnCurdiff()
954 CMergeDoc *pd = GetDocument();
956 // If no diffs, nothing to select
957 if (!pd->m_diffList.HasSignificantDiffs())
960 // GetCurrentDiff() returns -1 if no diff selected
961 int nDiff = pd->GetCurrentDiff();
964 // Scroll to the first line of the currently selected diff
965 SelectDiff(nDiff, true, false);
969 // If cursor is inside diff, select that diff
970 CPoint pos = GetCursorPos();
971 nDiff = pd->m_diffList.LineToDiff(pos.y);
972 if (nDiff != -1 && pd->m_diffList.IsDiffSignificant(nDiff))
973 SelectDiff(nDiff, true, false);
978 * @brief Called when "Current diff" item is updated
980 void CMergeEditView::OnUpdateCurdiff(CCmdUI* pCmdUI)
982 CMergeDoc *pd = GetDocument();
983 CPoint pos = GetCursorPos();
984 int nCurrentDiff = pd->GetCurrentDiff();
985 if (nCurrentDiff == -1)
987 int nNewDiff = pd->m_diffList.LineToDiff(pos.y);
988 pCmdUI->Enable(nNewDiff != -1 && pd->m_diffList.IsDiffSignificant(nNewDiff));
991 pCmdUI->Enable(true);
995 * @brief Copy selected text to clipboard
997 void CMergeEditView::OnEditCopy()
999 CMergeDoc * pDoc = GetDocument();
1000 CPoint ptSelStart, ptSelEnd;
1001 GetSelection(ptSelStart, ptSelEnd);
1004 if (ptSelStart == ptSelEnd)
1009 if (!m_bColumnSelection)
1011 CDiffTextBuffer * buffer = pDoc->m_ptBuf[m_nThisPane].get();
1013 buffer->GetTextWithoutEmptys(ptSelStart.y, ptSelStart.x,
1014 ptSelEnd.y, ptSelEnd.x, text);
1017 GetTextWithoutEmptysInColumnSelection(text);
1019 PutToClipboard(text, text.GetLength(), m_bColumnSelection);
1023 * @brief Called when "Copy" item is updated
1025 void CMergeEditView::OnUpdateEditCopy(CCmdUI* pCmdUI)
1027 CCrystalEditViewEx::OnUpdateEditCopy(pCmdUI);
1031 * @brief Cut current selection to clipboard
1033 void CMergeEditView::OnEditCut()
1035 if (IsReadOnly(m_nThisPane))
1038 CPoint ptSelStart, ptSelEnd;
1039 CMergeDoc * pDoc = GetDocument();
1040 GetSelection(ptSelStart, ptSelEnd);
1043 if (ptSelStart == ptSelEnd)
1047 if (!m_bColumnSelection)
1048 pDoc->m_ptBuf[m_nThisPane]->GetTextWithoutEmptys(ptSelStart.y, ptSelStart.x,
1049 ptSelEnd.y, ptSelEnd.x, text);
1051 GetTextWithoutEmptysInColumnSelection(text);
1053 PutToClipboard(text, text.GetLength(), m_bColumnSelection);
1055 if (!m_bColumnSelection)
1057 CPoint ptCursorPos = ptSelStart;
1058 ASSERT_VALIDTEXTPOS(ptCursorPos);
1059 SetAnchor(ptCursorPos);
1060 SetSelection(ptCursorPos, ptCursorPos);
1061 SetCursorPos(ptCursorPos);
1062 EnsureVisible(ptCursorPos);
1064 pDoc->m_ptBuf[m_nThisPane]->DeleteText(this, ptSelStart.y, ptSelStart.x, ptSelEnd.y,
1065 ptSelEnd.x, CE_ACTION_CUT);
1068 DeleteCurrentColumnSelection (CE_ACTION_CUT);
1070 m_pTextBuffer->SetModified(true);
1074 * @brief Called when "Cut" item is updated
1076 void CMergeEditView::OnUpdateEditCut(CCmdUI* pCmdUI)
1078 if (!IsReadOnly(m_nThisPane))
1079 CCrystalEditViewEx::OnUpdateEditCut(pCmdUI);
1081 pCmdUI->Enable(false);
1085 * @brief Paste text from clipboard
1087 void CMergeEditView::OnEditPaste()
1089 if (IsReadOnly(m_nThisPane))
1092 CCrystalEditViewEx::Paste();
1093 m_pTextBuffer->SetModified(true);
1097 * @brief Called when "Paste" item is updated
1099 void CMergeEditView::OnUpdateEditPaste(CCmdUI* pCmdUI)
1101 if (!IsReadOnly(m_nThisPane))
1102 CCrystalEditViewEx::OnUpdateEditPaste(pCmdUI);
1104 pCmdUI->Enable(false);
1108 * @brief Undo last action
1110 void CMergeEditView::OnEditUndo()
1112 CWaitCursor waitstatus;
1113 CMergeDoc* pDoc = GetDocument();
1114 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo-1));
1117 if (IsReadOnly(m_nThisPane))
1120 GetParentFrame()->SetActiveView(this, true);
1121 if(CCrystalEditViewEx::DoEditUndo())
1124 pDoc->UpdateHeaderPath(m_nThisPane);
1125 pDoc->FlushAndRescan();
1128 m_pTextBuffer->GetRedoActionCode(nAction);
1129 if (nAction == CE_ACTION_MERGE)
1130 // select the diff so we may just merge it again
1136 tgt->SendMessage(WM_COMMAND, ID_EDIT_UNDO);
1138 if (!pDoc->CanUndo())
1139 pDoc->SetAutoMerged(false);
1143 * @brief Called when "Undo" item is updated
1145 void CMergeEditView::OnUpdateEditUndo(CCmdUI* pCmdUI)
1147 CMergeDoc* pDoc = GetDocument();
1148 if (pDoc->curUndo!=pDoc->undoTgt.begin())
1150 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo-1));
1151 pCmdUI->Enable( !IsReadOnly(tgt->m_nThisPane));
1154 pCmdUI->Enable(false);
1158 * @brief Go to first diff
1160 * Called when user selects "First Difference"
1161 * @sa CMergeEditView::SelectDiff()
1163 void CMergeEditView::OnFirstdiff()
1165 CMergeDoc *pd = GetDocument();
1166 if (pd->m_diffList.HasSignificantDiffs())
1168 int nDiff = pd->m_diffList.FirstSignificantDiff();
1169 SelectDiff(nDiff, true, false);
1174 * @brief Update "First diff" UI items
1176 void CMergeEditView::OnUpdateFirstdiff(CCmdUI* pCmdUI)
1178 OnUpdatePrevdiff(pCmdUI);
1182 * @brief Go to last diff
1184 void CMergeEditView::OnLastdiff()
1186 CMergeDoc *pd = GetDocument();
1187 if (pd->m_diffList.HasSignificantDiffs())
1189 int nDiff = pd->m_diffList.LastSignificantDiff();
1190 SelectDiff(nDiff, true, false);
1195 * @brief Update "Last diff" UI items
1197 void CMergeEditView::OnUpdateLastdiff(CCmdUI* pCmdUI)
1199 OnUpdateNextdiff(pCmdUI);
1203 * @brief Go to next diff and select it.
1205 * Finds and selects next difference. There are several cases:
1206 * - if there is selected difference, and that difference is visible
1207 * on screen, next found difference is selected.
1208 * - if there is selected difference but it is not visible, next
1209 * difference from cursor position is selected. This is what user
1210 * expects to happen and is natural thing to do. Also reduces
1211 * needless scrolling.
1212 * - if there is no selected difference, next difference from cursor
1213 * position is selected.
1215 void CMergeEditView::OnNextdiff()
1217 CMergeDoc *pd = GetDocument();
1218 int cnt = pd->m_ptBuf[0]->GetLineCount();
1222 // Returns -1 if no diff selected
1224 int curDiff = pd->GetCurrentDiff();
1228 if (!IsDiffVisible(curDiff))
1230 // Selected difference not visible, select next from cursor
1231 int line = GetCursorPos().y;
1232 // Make sure we aren't in the first line of the diff
1234 if (!IsValidTextPosY(CPoint(0, line)))
1236 nextDiff = pd->m_diffList.NextSignificantDiffFromLine(line);
1240 // Find out if there is a following significant diff
1241 if (curDiff < pd->m_diffList.GetSize() - 1)
1243 nextDiff = pd->m_diffList.NextSignificantDiff(curDiff);
1249 // We don't have a selected difference,
1250 // but cursor can be inside inactive diff
1251 int line = GetCursorPos().y;
1252 if (!IsValidTextPosY(CPoint(0, line)))
1254 nextDiff = pd->m_diffList.NextSignificantDiffFromLine(line);
1257 int lastDiff = pd->m_diffList.LastSignificantDiff();
1258 if (nextDiff >= 0 && nextDiff <= lastDiff)
1259 SelectDiff(nextDiff, true, false);
1260 else if (CDirDoc *pDirDoc = pd->GetDirDoc())
1262 if (pDirDoc->MoveableToNextDiff())
1263 pDirDoc->MoveToNextDiff(pd);
1268 * @brief Update "Next diff" UI items
1270 void CMergeEditView::OnUpdateNextdiff(CCmdUI* pCmdUI)
1272 CMergeDoc *pd = GetDocument();
1273 const DIFFRANGE * dfi = pd->m_diffList.LastSignificantDiffRange();
1278 // There aren't any significant differences
1283 // Enable if the beginning of the last significant difference is after caret
1284 enabled = (GetCursorPos().y < (long)dfi->dbegin);
1287 if (!enabled && pd->GetDirDoc())
1288 enabled = pd->GetDirDoc()->MoveableToNextDiff();
1290 pCmdUI->Enable(enabled);
1294 * @brief Go to previous diff and select it.
1296 * Finds and selects previous difference. There are several cases:
1297 * - if there is selected difference, and that difference is visible
1298 * on screen, previous found difference is selected.
1299 * - if there is selected difference but it is not visible, previous
1300 * difference from cursor position is selected. This is what user
1301 * expects to happen and is natural thing to do. Also reduces
1302 * needless scrolling.
1303 * - if there is no selected difference, previous difference from cursor
1304 * position is selected.
1306 void CMergeEditView::OnPrevdiff()
1308 CMergeDoc *pd = GetDocument();
1309 int cnt = pd->m_ptBuf[0]->GetLineCount();
1313 // GetCurrentDiff() returns -1 if no diff selected
1315 int curDiff = pd->GetCurrentDiff();
1319 if (!IsDiffVisible(curDiff))
1321 // Selected difference not visible, select previous from cursor
1322 int line = GetCursorPos().y;
1323 // Make sure we aren't in the last line of the diff
1325 if (!IsValidTextPosY(CPoint(0, line)))
1327 prevDiff = pd->m_diffList.PrevSignificantDiffFromLine(line);
1331 // Find out if there is a preceding significant diff
1334 prevDiff = pd->m_diffList.PrevSignificantDiff(curDiff);
1340 // We don't have a selected difference,
1341 // but cursor can be inside inactive diff
1342 int line = GetCursorPos().y;
1343 if (!IsValidTextPosY(CPoint(0, line)))
1345 prevDiff = pd->m_diffList.PrevSignificantDiffFromLine(line);
1348 int firstDiff = pd->m_diffList.FirstSignificantDiff();
1349 if (prevDiff >= 0 && prevDiff >= firstDiff)
1350 SelectDiff(prevDiff, true, false);
1351 else if (CDirDoc *pDirDoc = pd->GetDirDoc())
1353 if (pDirDoc->MoveableToPrevDiff())
1354 pDirDoc->MoveToPrevDiff(pd);
1359 * @brief Update "Previous diff" UI items
1361 void CMergeEditView::OnUpdatePrevdiff(CCmdUI* pCmdUI)
1363 CMergeDoc *pd = GetDocument();
1364 const DIFFRANGE * dfi = pd->m_diffList.FirstSignificantDiffRange();
1369 // There aren't any significant differences
1374 // Enable if the end of the first significant difference is before caret
1375 enabled = (GetCursorPos().y > (long)dfi->dend);
1378 if (!enabled && pd->GetDirDoc())
1379 enabled = pd->GetDirDoc()->MoveableToPrevDiff();
1381 pCmdUI->Enable(enabled);
1384 void CMergeEditView::OnNextConflict()
1386 OnNext3wayDiff(THREEWAYDIFFTYPE_CONFLICT);
1390 * @brief Update "Next Conflict" UI items
1392 void CMergeEditView::OnUpdateNextConflict(CCmdUI* pCmdUI)
1394 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_CONFLICT);
1397 void CMergeEditView::OnPrevConflict()
1399 OnPrev3wayDiff(THREEWAYDIFFTYPE_CONFLICT);
1403 * @brief Update "Prev Conflict" UI items
1405 void CMergeEditView::OnUpdatePrevConflict(CCmdUI* pCmdUI)
1407 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_CONFLICT);
1411 * @brief Go to next 3-way diff and select it.
1413 void CMergeEditView::OnNext3wayDiff(int nDiffType)
1415 CMergeDoc *pd = GetDocument();
1416 int cnt = pd->m_ptBuf[0]->GetLineCount();
1420 // Returns -1 if no diff selected
1421 int curDiff = pd->GetCurrentDiff();
1425 int nextDiff = curDiff;
1426 if (!IsDiffVisible(curDiff))
1428 // Selected difference not visible, select next from cursor
1429 int line = GetCursorPos().y;
1430 // Make sure we aren't in the first line of the diff
1432 if (!IsValidTextPosY(CPoint(0, line)))
1434 nextDiff = pd->m_diffList.NextSignificant3wayDiffFromLine(line, nDiffType);
1438 // Find out if there is a following significant diff
1439 if (curDiff < pd->m_diffList.GetSize() - 1)
1441 nextDiff = pd->m_diffList.NextSignificant3wayDiff(curDiff, nDiffType);
1447 // nextDiff is the next one if there is one, else it is the one we're on
1448 SelectDiff(nextDiff, true, false);
1452 // We don't have a selected difference,
1453 // but cursor can be inside inactive diff
1454 int line = GetCursorPos().y;
1455 if (!IsValidTextPosY(CPoint(0, line)))
1457 curDiff = pd->m_diffList.NextSignificant3wayDiffFromLine(line, nDiffType);
1459 SelectDiff(curDiff, true, false);
1464 * @brief Update "Next 3-way diff" UI items
1466 void CMergeEditView::OnUpdateNext3wayDiff(CCmdUI* pCmdUI, int nDiffType)
1468 CMergeDoc *pd = GetDocument();
1470 if (pd->m_nBuffers < 3)
1472 pCmdUI->Enable(false);
1476 const DIFFRANGE * dfi = pd->m_diffList.LastSignificant3wayDiffRange(nDiffType);
1480 // There aren't any significant differences
1481 pCmdUI->Enable(false);
1485 // Enable if the beginning of the last significant difference is after caret
1486 CPoint pos = GetCursorPos();
1487 pCmdUI->Enable(pos.y < (long)dfi->dbegin);
1492 * @brief Go to previous 3-way diff and select it.
1494 void CMergeEditView::OnPrev3wayDiff(int nDiffType)
1496 CMergeDoc *pd = GetDocument();
1498 int cnt = pd->m_ptBuf[0]->GetLineCount();
1502 // GetCurrentDiff() returns -1 if no diff selected
1503 int curDiff = pd->GetCurrentDiff();
1507 int prevDiff = curDiff;
1508 if (!IsDiffVisible(curDiff))
1510 // Selected difference not visible, select previous from cursor
1511 int line = GetCursorPos().y;
1512 // Make sure we aren't in the last line of the diff
1514 if (!IsValidTextPosY(CPoint(0, line)))
1516 prevDiff = pd->m_diffList.PrevSignificant3wayDiffFromLine(line, nDiffType);
1520 // Find out if there is a preceding significant diff
1523 prevDiff = pd->m_diffList.PrevSignificant3wayDiff(curDiff, nDiffType);
1529 // prevDiff is the preceding one if there is one, else it is the one we're on
1530 SelectDiff(prevDiff, true, false);
1534 // We don't have a selected difference,
1535 // but cursor can be inside inactive diff
1536 int line = GetCursorPos().y;
1537 if (!IsValidTextPosY(CPoint(0, line)))
1539 curDiff = pd->m_diffList.PrevSignificant3wayDiffFromLine(line, nDiffType);
1541 SelectDiff(curDiff, true, false);
1546 * @brief Update "Previous diff X and Y" UI items
1548 void CMergeEditView::OnUpdatePrev3wayDiff(CCmdUI* pCmdUI, int nDiffType)
1550 CMergeDoc *pd = GetDocument();
1552 if (pd->m_nBuffers < 3)
1554 pCmdUI->Enable(false);
1558 const DIFFRANGE * dfi = pd->m_diffList.FirstSignificant3wayDiffRange(nDiffType);
1562 // There aren't any significant differences
1563 pCmdUI->Enable(false);
1567 // Enable if the end of the first significant difference is before caret
1568 CPoint pos = GetCursorPos();
1569 pCmdUI->Enable(pos.y > (long)dfi->dend);
1573 void CMergeEditView::OnNextdiffLM()
1575 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTMIDDLE);
1578 void CMergeEditView::OnUpdateNextdiffLM(CCmdUI* pCmdUI)
1580 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTMIDDLE);
1583 void CMergeEditView::OnNextdiffLR()
1585 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTRIGHT);
1588 void CMergeEditView::OnUpdateNextdiffLR(CCmdUI* pCmdUI)
1590 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTRIGHT);
1593 void CMergeEditView::OnNextdiffMR()
1595 OnNext3wayDiff(THREEWAYDIFFTYPE_MIDDLERIGHT);
1598 void CMergeEditView::OnUpdateNextdiffMR(CCmdUI* pCmdUI)
1600 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLERIGHT);
1603 void CMergeEditView::OnNextdiffLO()
1605 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTONLY);
1608 void CMergeEditView::OnUpdateNextdiffLO(CCmdUI* pCmdUI)
1610 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTONLY);
1613 void CMergeEditView::OnNextdiffMO()
1615 OnNext3wayDiff(THREEWAYDIFFTYPE_MIDDLEONLY);
1618 void CMergeEditView::OnUpdateNextdiffMO(CCmdUI* pCmdUI)
1620 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLEONLY);
1623 void CMergeEditView::OnNextdiffRO()
1625 OnNext3wayDiff(THREEWAYDIFFTYPE_RIGHTONLY);
1628 void CMergeEditView::OnUpdateNextdiffRO(CCmdUI* pCmdUI)
1630 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_RIGHTONLY);
1633 void CMergeEditView::OnPrevdiffLM()
1635 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTMIDDLE);
1638 void CMergeEditView::OnUpdatePrevdiffLM(CCmdUI* pCmdUI)
1640 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTMIDDLE);
1643 void CMergeEditView::OnPrevdiffLR()
1645 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTRIGHT);
1648 void CMergeEditView::OnUpdatePrevdiffLR(CCmdUI* pCmdUI)
1650 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTRIGHT);
1653 void CMergeEditView::OnPrevdiffMR()
1655 OnPrev3wayDiff(THREEWAYDIFFTYPE_MIDDLERIGHT);
1658 void CMergeEditView::OnUpdatePrevdiffMR(CCmdUI* pCmdUI)
1660 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLERIGHT);
1663 void CMergeEditView::OnPrevdiffLO()
1665 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTONLY);
1668 void CMergeEditView::OnUpdatePrevdiffLO(CCmdUI* pCmdUI)
1670 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTONLY);
1673 void CMergeEditView::OnPrevdiffMO()
1675 OnPrev3wayDiff(THREEWAYDIFFTYPE_MIDDLEONLY);
1678 void CMergeEditView::OnUpdatePrevdiffMO(CCmdUI* pCmdUI)
1680 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLEONLY);
1683 void CMergeEditView::OnPrevdiffRO()
1685 OnPrev3wayDiff(THREEWAYDIFFTYPE_RIGHTONLY);
1688 void CMergeEditView::OnUpdatePrevdiffRO(CCmdUI* pCmdUI)
1690 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_RIGHTONLY);
1694 * @brief Clear selection
1696 void CMergeEditView::SelectNone()
1698 SetSelection (GetCursorPos(), GetCursorPos());
1703 * @brief Check if line is inside currently selected diff
1704 * @param [in] nLine 0-based linenumber in view
1705 * @sa CMergeDoc::GetCurrentDiff()
1706 * @sa CMergeDoc::LineInDiff()
1708 bool CMergeEditView::IsLineInCurrentDiff(int nLine) const
1710 // Check validity of nLine
1713 _RPTF1(_CRT_ERROR, "Linenumber is negative (%d)!", nLine);
1714 int nLineCount = LocateTextBuffer()->GetLineCount();
1715 if (nLine >= nLineCount)
1716 _RPTF2(_CRT_ERROR, "Linenumber > linecount (%d>%d)!", nLine, nLineCount);
1719 const CMergeDoc *pd = GetDocument();
1720 int curDiff = pd->GetCurrentDiff();
1723 return pd->m_diffList.LineInDiff(nLine, curDiff);
1727 * @brief Called when mouse left-button double-clicked
1729 * Double-clicking mouse inside diff selects that diff
1731 void CMergeEditView::OnLButtonDblClk(UINT nFlags, CPoint point)
1733 CMergeDoc *pd = GetDocument();
1734 CPoint pos = GetCursorPos();
1736 int diff = pd->m_diffList.LineToDiff(pos.y);
1737 if (diff != -1 && pd->m_diffList.IsDiffSignificant(diff))
1738 SelectDiff(diff, false, false);
1740 CCrystalEditViewEx::OnLButtonDblClk(nFlags, point);
1744 * @brief Called when mouse left button is released.
1746 * If button is released outside diffs, current diff
1749 void CMergeEditView::OnLButtonUp(UINT nFlags, CPoint point)
1751 CMergeDoc *pd = GetDocument();
1752 CCrystalEditViewEx::OnLButtonUp(nFlags, point);
1754 // If we have a selected diff, deselect it
1755 int nCurrentDiff = pd->GetCurrentDiff();
1756 if (nCurrentDiff != -1)
1758 CPoint pos = GetCursorPos();
1759 if (!IsLineInCurrentDiff(pos.y))
1761 pd->SetCurrentDiff(-1);
1763 pd->UpdateAllViews(this);
1768 void CMergeEditView::OnX2Y(int srcPane, int dstPane)
1770 // Check that right side is not readonly
1771 if (IsReadOnly(dstPane))
1774 CMergeDoc *pDoc = GetDocument();
1775 int currentDiff = pDoc->GetCurrentDiff();
1777 if (currentDiff == -1)
1780 // If cursor is inside diff get number of that diff
1781 if (m_bCurrentLineIsDiff)
1783 CPoint pt = GetCursorPos();
1784 currentDiff = pDoc->m_diffList.LineToDiff(pt.y);
1790 if (!m_bColumnSelection)
1792 int firstDiff, lastDiff, firstWordDiff, lastWordDiff;
1793 GetFullySelectedDiffs(firstDiff, lastDiff, firstWordDiff, lastWordDiff);
1794 if (firstDiff != -1 && lastDiff != -1)
1796 CWaitCursor waitstatus;
1797 pDoc->CopyMultipleList(srcPane, dstPane, firstDiff, lastDiff, firstWordDiff, lastWordDiff);
1802 CWaitCursor waitstatus;
1803 auto wordDiffs = GetColumnSelectedWordDiffIndice();
1805 std::for_each(wordDiffs.rbegin(), wordDiffs.rend(), [&](auto& it) {
1806 pDoc->WordListCopy(srcPane, dstPane, it.first, it.second[0], it.second[it.second.size() - 1], &it.second, i != 0, i == 0);
1811 else if (currentDiff != -1 && pDoc->m_diffList.IsDiffSignificant(currentDiff))
1813 CWaitCursor waitstatus;
1814 pDoc->ListCopy(srcPane, dstPane, currentDiff);
1818 void CMergeEditView::OnUpdateX2Y(int dstPane, CCmdUI* pCmdUI)
1820 // Check that right side is not readonly
1821 if (!IsReadOnly(dstPane))
1823 // If one or more diffs inside selection OR
1824 // there is an active diff OR
1825 // cursor is inside diff
1828 int firstDiff, lastDiff, firstWordDiff, lastWordDiff;
1829 GetFullySelectedDiffs(firstDiff, lastDiff, firstWordDiff, lastWordDiff);
1831 pCmdUI->Enable(firstDiff != -1 && lastDiff != -1);
1835 const int currDiff = GetDocument()->GetCurrentDiff();
1836 if (currDiff != -1 && GetDocument()->m_diffList.IsDiffSignificant(currDiff))
1837 pCmdUI->Enable(true);
1839 pCmdUI->Enable(m_bCurrentLineIsDiff);
1843 pCmdUI->Enable(false);
1847 * @brief Copy diff from left pane to right pane
1849 * Difference is copied from left to right when
1850 * - difference is selected
1851 * - difference is inside selection (allows merging multiple differences).
1852 * - cursor is inside diff
1854 * If there is selected diff outside selection, we copy selected
1857 void CMergeEditView::OnL2r()
1859 int dstPane = (m_nThisPane < GetDocument()->m_nBuffers - 1) ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
1860 int srcPane = dstPane - 1;
1861 OnX2Y(srcPane, dstPane);
1865 * @brief Called when "Copy to left" item is updated
1867 void CMergeEditView::OnUpdateL2r(CCmdUI* pCmdUI)
1869 OnUpdateX2Y(m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1, pCmdUI);
1873 * @brief Copy diff from right pane to left pane
1875 * Difference is copied from left to right when
1876 * - difference is selected
1877 * - difference is inside selection (allows merging multiple differences).
1878 * - cursor is inside diff
1880 * If there is selected diff outside selection, we copy selected
1883 void CMergeEditView::OnR2l()
1885 int dstPane = (m_nThisPane > 0) ? m_nThisPane - 1 : 0;
1886 int srcPane = dstPane + 1;
1887 OnX2Y(srcPane, dstPane);
1891 * @brief Called when "Copy to right" item is updated
1893 void CMergeEditView::OnUpdateR2l(CCmdUI* pCmdUI)
1895 OnUpdateX2Y(m_nThisPane > 0 ? m_nThisPane - 1 : 0, pCmdUI);
1898 void CMergeEditView::OnCopyFromLeft()
1900 int dstPane = m_nThisPane;
1901 int srcPane = dstPane - 1;
1904 OnX2Y(srcPane, dstPane);
1907 void CMergeEditView::OnUpdateCopyFromLeft(CCmdUI* pCmdUI)
1909 int dstPane = m_nThisPane;
1910 int srcPane = dstPane - 1;
1912 pCmdUI->Enable(false);
1914 OnUpdateX2Y(dstPane, pCmdUI);
1917 void CMergeEditView::OnCopyFromRight()
1919 int dstPane = m_nThisPane;
1920 int srcPane = dstPane + 1;
1921 if (srcPane >= GetDocument()->m_nBuffers)
1923 OnX2Y(srcPane, dstPane);
1926 void CMergeEditView::OnUpdateCopyFromRight(CCmdUI* pCmdUI)
1928 int dstPane = m_nThisPane;
1929 int srcPane = dstPane + 1;
1930 if (srcPane >= GetDocument()->m_nBuffers)
1931 pCmdUI->Enable(false);
1933 OnUpdateX2Y(dstPane, pCmdUI);
1937 * @brief Copy all diffs from right pane to left pane
1939 void CMergeEditView::OnAllLeft()
1941 // Check that left side is not readonly
1942 int srcPane = m_nThisPane > 0 ? m_nThisPane : 1;
1943 int dstPane = m_nThisPane > 0 ? m_nThisPane - 1 : 0;
1944 if (IsReadOnly(dstPane))
1946 CWaitCursor waitstatus;
1948 GetDocument()->CopyAllList(srcPane, dstPane);
1952 * @brief Called when "Copy all to left" item is updated
1954 void CMergeEditView::OnUpdateAllLeft(CCmdUI* pCmdUI)
1956 // Check that left side is not readonly
1957 int dstPane = m_nThisPane > 0 ? m_nThisPane - 1 : 0;
1958 if (!IsReadOnly(dstPane))
1959 pCmdUI->Enable(GetDocument()->m_diffList.HasSignificantDiffs());
1961 pCmdUI->Enable(false);
1965 * @brief Copy all diffs from left pane to right pane
1967 void CMergeEditView::OnAllRight()
1969 // Check that right side is not readonly
1970 int srcPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane : m_nThisPane - 1;
1971 int dstPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
1972 if (IsReadOnly(dstPane))
1975 CWaitCursor waitstatus;
1977 GetDocument()->CopyAllList(srcPane, dstPane);
1981 * @brief Called when "Copy all to right" item is updated
1983 void CMergeEditView::OnUpdateAllRight(CCmdUI* pCmdUI)
1985 // Check that right side is not readonly
1986 int dstPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
1987 if (!IsReadOnly(dstPane))
1988 pCmdUI->Enable(GetDocument()->m_diffList.HasSignificantDiffs());
1990 pCmdUI->Enable(false);
1994 * @brief Do Auto merge
1996 void CMergeEditView::OnAutoMerge()
1998 // Check current pane is not readonly
1999 if (GetDocument()->IsModified() || GetDocument()->GetAutoMerged() || IsReadOnly(m_nThisPane))
2002 CWaitCursor waitstatus;
2004 GetDocument()->DoAutoMerge(m_nThisPane);
2008 * @brief Called when "Auto Merge" item is updated
2010 void CMergeEditView::OnUpdateAutoMerge(CCmdUI* pCmdUI)
2012 pCmdUI->Enable(GetDocument()->m_nBuffers == 3 &&
2013 !GetDocument()->IsModified() &&
2014 !GetDocument()->GetAutoMerged() &&
2015 !IsReadOnly(m_nThisPane));
2019 * @brief Add synchronization point
2021 void CMergeEditView::OnAddSyncPoint()
2023 GetDocument()->AddSyncPoint();
2027 * @brief Clear synchronization points
2029 void CMergeEditView::OnClearSyncPoints()
2031 GetDocument()->ClearSyncPoints();
2035 * @brief Called when "Clear Synchronization Points" item is updated
2037 void CMergeEditView::OnUpdateClearSyncPoints(CCmdUI* pCmdUI)
2039 pCmdUI->Enable(GetDocument()->HasSyncPoints());
2043 * @brief This function is called before other edit events.
2044 * @param [in] nAction Edit operation to do
2045 * @param [in] pszText Text to insert, delete etc
2046 * @sa CCrystalEditView::OnEditOperation()
2047 * @todo More edit-events for rescan delaying?
2049 void CMergeEditView::OnEditOperation(int nAction, LPCTSTR pszText, size_t cchText)
2051 if (IsReadOnly(m_nThisPane))
2053 // We must not arrive here, and assert helps detect troubles
2058 CMergeDoc* pDoc = GetDocument();
2059 pDoc->SetEditedAfterRescan(m_nThisPane);
2061 // simple hook for multiplex undo operations
2062 // deleted by jtuc 2003-06-28
2063 // now AddUndoRecords does it (so we don't create entry for OnEditOperation with no Undo data in m_pTextBuffer)
2064 /*if(dynamic_cast<CMergeDoc::CDiffTextBuffer*>(m_pTextBuffer)->curUndoGroup())
2066 pDoc->undoTgt.erase(pDoc->curUndo, pDoc->undoTgt.end());
2067 pDoc->undoTgt.push_back(this);
2068 pDoc->curUndo = pDoc->undoTgt.end();
2071 // perform original function
2072 CCrystalEditViewEx::OnEditOperation(nAction, pszText, cchText);
2074 // augment with additional operations
2076 // Change header to inform about changed doc
2077 pDoc->UpdateHeaderPath(m_nThisPane);
2079 // If automatic rescan enabled, rescan after edit events
2080 if (m_bAutomaticRescan)
2082 // keep document up to date
2083 // (Re)start timer to rescan only when user edits text
2084 // If timer starting fails, rescan immediately
2085 if (nAction == CE_ACTION_TYPING ||
2086 nAction == CE_ACTION_REPLACE ||
2087 nAction == CE_ACTION_BACKSPACE ||
2088 nAction == CE_ACTION_INDENT ||
2089 nAction == CE_ACTION_PASTE ||
2090 nAction == CE_ACTION_DELSEL ||
2091 nAction == CE_ACTION_DELETE ||
2092 nAction == CE_ACTION_CUT)
2094 if (!SetTimer(IDT_RESCAN, RESCAN_TIMEOUT, nullptr))
2095 pDoc->FlushAndRescan();
2098 pDoc->FlushAndRescan();
2104 // Update other pane for sync line.
2105 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
2107 if (nPane == m_nThisPane)
2109 CCrystalEditView *pView = GetGroupView(nPane);
2110 if (pView != nullptr)
2111 pView->Invalidate();
2118 * @brief Redo last action
2120 void CMergeEditView::OnEditRedo()
2122 CWaitCursor waitstatus;
2123 CMergeDoc* pDoc = GetDocument();
2124 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo));
2127 if (IsReadOnly(m_nThisPane))
2130 GetParentFrame()->SetActiveView(this, true);
2131 if(CCrystalEditViewEx::DoEditRedo())
2134 pDoc->UpdateHeaderPath(m_nThisPane);
2135 pDoc->FlushAndRescan();
2140 tgt->SendMessage(WM_COMMAND, ID_EDIT_REDO);
2145 * @brief Called when "Redo" item is updated
2147 void CMergeEditView::OnUpdateEditRedo(CCmdUI* pCmdUI)
2149 CMergeDoc* pDoc = GetDocument();
2150 if (pDoc->curUndo!=pDoc->undoTgt.end())
2152 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo));
2153 pCmdUI->Enable( !IsReadOnly(tgt->m_nThisPane));
2156 pCmdUI->Enable(false);
2159 void CMergeEditView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
2161 CCrystalEditViewEx::OnUpdate(pSender, lHint, pHint);
2165 * @brief Scrolls to current diff and/or selects diff text
2166 * @param [in] bScroll If true scroll diff to view
2167 * @param [in] bSelectText If true select diff text
2168 * @note If bScroll and bSelectText are false, this does nothing!
2169 * @todo This shouldn't be called when no diff is selected, so
2170 * somebody could try to ASSERT(nDiff > -1)...
2172 void CMergeEditView::ShowDiff(bool bScroll, bool bSelectText)
2174 CMergeDoc *pd = GetDocument();
2175 const int nDiff = pd->GetCurrentDiff();
2177 // Try to trap some errors
2178 if (nDiff >= pd->m_diffList.GetSize())
2179 _RPTF2(_CRT_ERROR, "Selected diff > diffcount (%d > %d)!",
2180 nDiff, pd->m_diffList.GetSize());
2182 if (nDiff >= 0 && nDiff < pd->m_diffList.GetSize())
2184 CPoint ptStart, ptEnd;
2186 pd->m_diffList.GetDiff(nDiff, curDiff);
2189 ptStart.y = curDiff.dbegin;
2191 ptEnd.y = curDiff.dend;
2195 if (!IsDiffVisible(curDiff, CONTEXT_LINES_BELOW))
2197 // Difference is not visible, scroll it so that max amount of
2198 // scrolling is done while keeping the diff in screen. So if
2199 // scrolling is downwards, scroll the diff to as up in screen
2200 // as possible. This usually brings next diff to the screen
2201 // and we don't need to scroll into it.
2202 int nLine = GetSubLineIndex(ptStart.y);
2203 if (nLine > CONTEXT_LINES_ABOVE)
2205 nLine -= CONTEXT_LINES_ABOVE;
2207 GetGroupView(m_nThisPane)->ScrollToSubLine(nLine);
2208 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2210 if (nPane != m_nThisPane)
2211 GetGroupView(nPane)->ScrollToSubLine(nLine);
2214 GetGroupView(m_nThisPane)->SetCursorPos(ptStart);
2215 GetGroupView(m_nThisPane)->SetAnchor(ptStart);
2216 GetGroupView(m_nThisPane)->SetSelection(ptStart, ptStart);
2217 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2219 if (nPane != m_nThisPane)
2221 GetGroupView(nPane)->SetCursorPos(ptStart);
2222 GetGroupView(nPane)->SetAnchor(ptStart);
2223 GetGroupView(nPane)->SetSelection(ptStart, ptStart);
2230 ptEnd.x = GetLineLength(ptEnd.y);
2231 SetSelection(ptStart, ptEnd);
2240 void CMergeEditView::OnTimer(UINT_PTR nIDEvent)
2242 // Maybe we want theApp::OnIdle to proceed before processing a timer message
2243 // ...but for this the queue must be empty
2244 // The timer message is a low priority message but the queue is maybe not yet empty
2245 // So we set a flag, wait for OnIdle to proceed, then come back here...
2246 // We come back here with a IDLE_TIMER OnTimer message (send with SendMessage
2247 // not with SetTimer so there is no delay)
2249 // IDT_RESCAN was posted because the app wanted to do a flushAndRescan with some delay
2251 // IDLE_TIMER is the false timer used to come back here after OnIdle
2252 // fTimerWaitingForIdle is a bool to store the commands waiting for idle
2253 // (one normal timer = one flag = one command)
2255 if (nIDEvent == IDT_RESCAN)
2257 KillTimer(IDT_RESCAN);
2258 fTimerWaitingForIdle |= FLAG_RESCAN_WAITS_FOR_IDLE;
2259 // notify the app to come back after OnIdle
2260 theApp.SetNeedIdleTimer();
2263 if (nIDEvent == IDLE_TIMER)
2265 // not a real timer, just come back after OnIdle
2266 // look to flags to know what to do
2267 if (fTimerWaitingForIdle & FLAG_RESCAN_WAITS_FOR_IDLE)
2268 GetDocument()->RescanIfNeeded(RESCAN_TIMEOUT/1000);
2269 fTimerWaitingForIdle = 0;
2272 CCrystalEditViewEx::OnTimer(nIDEvent);
2276 * @brief Returns if buffer is read-only
2277 * @note This has no any relation to file being read-only!
2279 bool CMergeEditView::IsReadOnly(int pane) const
2281 return GetDocument()->m_ptBuf[pane]->GetReadOnly() != false;
2285 * @brief Called when "Save left (as...)" item is updated
2287 void CMergeEditView::OnUpdateFileSaveLeft(CCmdUI* pCmdUI)
2289 CMergeDoc *pd = GetDocument();
2290 pCmdUI->Enable(!IsReadOnly(0) && pd->m_ptBuf[0]->IsModified());
2294 * @brief Called when "Save middle (as...)" item is updated
2296 void CMergeEditView::OnUpdateFileSaveMiddle(CCmdUI* pCmdUI)
2298 CMergeDoc *pd = GetDocument();
2299 pCmdUI->Enable(pd->m_nBuffers == 3 && !IsReadOnly(1) && pd->m_ptBuf[1]->IsModified());
2303 * @brief Called when "Save right (as...)" item is updated
2305 void CMergeEditView::OnUpdateFileSaveRight(CCmdUI* pCmdUI)
2307 CMergeDoc *pd = GetDocument();
2308 pCmdUI->Enable(!IsReadOnly(pd->m_nBuffers - 1) && pd->m_ptBuf[pd->m_nBuffers - 1]->IsModified());
2312 * @brief Refresh display using text-buffers
2313 * @note This DOES NOT reload files!
2315 void CMergeEditView::OnRefresh()
2317 CMergeDoc *pd = GetDocument();
2318 ASSERT(pd != nullptr);
2319 pd->FlushAndRescan(true);
2323 * @brief Enable/Disable automatic rescanning
2325 bool CMergeEditView::EnableRescan(bool bEnable)
2327 bool bOldValue = m_bAutomaticRescan;
2328 m_bAutomaticRescan = bEnable;
2333 * @brief Handle some keys when in merging mode
2335 bool CMergeEditView::MergeModeKeyDown(MSG* pMsg)
2337 bool bHandled = false;
2339 // Allow default text selection when SHIFT pressed
2340 if (::GetAsyncKeyState(VK_SHIFT))
2343 // Allow default editor functions when CTRL pressed
2344 if (::GetAsyncKeyState(VK_CONTROL))
2347 // If we are in merging mode (merge with cursor keys)
2348 // handle some keys here
2349 switch (pMsg->wParam)
2376 * @brief Called before messages are translated.
2378 * Checks if ESC key was pressed, saves and closes doc.
2379 * Also if in merge mode traps cursor keys.
2381 BOOL CMergeEditView::PreTranslateMessage(MSG* pMsg)
2383 if (pMsg->message == WM_KEYDOWN)
2385 // If we are in merging mode (merge with cursor keys)
2386 // handle some keys here
2387 if (theApp.GetMergingMode())
2389 bool bHandled = MergeModeKeyDown(pMsg);
2394 // Close window if user has allowed it from options
2395 if (pMsg->wParam == VK_ESCAPE)
2397 bool bCloseWithEsc = GetOptionsMgr()->GetBool(OPT_CLOSE_WITH_ESC);
2399 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
2404 return CCrystalEditViewEx::PreTranslateMessage(pMsg);
2408 * @brief Called when "Save" item is updated
2410 void CMergeEditView::OnUpdateFileSave(CCmdUI* pCmdUI)
2412 CMergeDoc *pd = GetDocument();
2414 bool bModified = false;
2415 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2417 if (pd->m_ptBuf[nPane]->IsModified())
2420 pCmdUI->Enable(bModified);
2424 * @brief Enable/disable left buffer read-only
2426 void CMergeEditView::OnLeftReadOnly()
2428 CMergeDoc *pd = GetDocument();
2429 bool bReadOnly = pd->m_ptBuf[0]->GetReadOnly();
2430 pd->m_ptBuf[0]->SetReadOnly(!bReadOnly);
2434 * @brief Called when "Left read-only" item is updated
2436 void CMergeEditView::OnUpdateLeftReadOnly(CCmdUI* pCmdUI)
2438 CMergeDoc *pd = GetDocument();
2439 bool bReadOnly = pd->m_ptBuf[0]->GetReadOnly();
2440 pCmdUI->Enable(true);
2441 pCmdUI->SetCheck(bReadOnly);
2445 * @brief Enable/disable middle buffer read-only
2447 void CMergeEditView::OnMiddleReadOnly()
2449 CMergeDoc *pd = GetDocument();
2450 if (pd->m_nBuffers == 3)
2452 bool bReadOnly = pd->m_ptBuf[1]->GetReadOnly();
2453 pd->m_ptBuf[1]->SetReadOnly(!bReadOnly);
2458 * @brief Called when "Middle read-only" item is updated
2460 void CMergeEditView::OnUpdateMiddleReadOnly(CCmdUI* pCmdUI)
2462 CMergeDoc *pd = GetDocument();
2463 if (pd->m_nBuffers < 3)
2465 pCmdUI->Enable(false);
2469 bool bReadOnly = pd->m_ptBuf[1]->GetReadOnly();
2470 pCmdUI->Enable(true);
2471 pCmdUI->SetCheck(bReadOnly);
2476 * @brief Enable/disable right buffer read-only
2478 void CMergeEditView::OnRightReadOnly()
2480 CMergeDoc *pd = GetDocument();
2481 bool bReadOnly = pd->m_ptBuf[pd->m_nBuffers - 1]->GetReadOnly();
2482 pd->m_ptBuf[pd->m_nBuffers - 1]->SetReadOnly(!bReadOnly);
2486 * @brief Called when "Left read-only" item is updated
2488 void CMergeEditView::OnUpdateRightReadOnly(CCmdUI* pCmdUI)
2490 CMergeDoc *pd = GetDocument();
2491 bool bReadOnly = pd->m_ptBuf[pd->m_nBuffers - 1]->GetReadOnly();
2492 pCmdUI->Enable(true);
2493 pCmdUI->SetCheck(bReadOnly);
2496 /// Store interface we use to display status line info
2497 void CMergeEditView::SetStatusInterface(IMergeEditStatus * piMergeEditStatus)
2499 ASSERT(m_piMergeEditStatus == nullptr);
2500 m_piMergeEditStatus = piMergeEditStatus;
2504 * @brief Update status bar contents.
2506 void CMergeEditView::UpdateStatusbar()
2512 * @brief Update statusbar info, Override from CCrystalTextView
2513 * @note we tab-expand column, but we don't tab-expand char count,
2514 * since we want to show how many chars there are and tab is just one
2515 * character although it expands to several spaces.
2517 void CMergeEditView::OnUpdateCaret()
2519 if (m_piMergeEditStatus == nullptr || !IsTextBufferInitialized())
2522 CPoint cursorPos = GetCursorPos();
2523 int nScreenLine = cursorPos.y;
2524 const int nRealLine = ComputeRealLine(nScreenLine);
2531 DWORD dwLineFlags = 0;
2533 dwLineFlags = m_pTextBuffer->GetLineFlags(nScreenLine);
2534 // Is this a ghost line ?
2535 if (dwLineFlags & LF_GHOST)
2537 // Ghost lines display eg "Line 12-13"
2538 sLine.Format(_T("%d-%d"), nRealLine, nRealLine+1);
2539 sEol = _T("hidden");
2543 // Regular lines display eg "Line 13 Characters: 25 EOL: CRLF"
2544 sLine.Format(_T("%d"), nRealLine+1);
2545 curChar = cursorPos.x + 1;
2546 chars = GetLineLength(nScreenLine);
2547 column = CalculateActualOffset(nScreenLine, cursorPos.x, true) + 1;
2548 columns = CalculateActualOffset(nScreenLine, chars, true) + 1;
2550 if (GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
2551 GetDocument()->IsMixedEOL(m_nThisPane))
2553 sEol = GetTextBufferEol(nScreenLine);
2556 sEol = _T("hidden");
2558 m_piMergeEditStatus->SetLineInfo(sLine, column, columns,
2559 curChar, chars, sEol, GetDocument()->m_ptBuf[m_nThisPane]->getCodepage(), GetDocument()->m_ptBuf[m_nThisPane]->getHasBom());
2561 // Is cursor inside difference?
2562 if (dwLineFlags & LF_NONTRIVIAL_DIFF)
2563 m_bCurrentLineIsDiff = true;
2565 m_bCurrentLineIsDiff = false;
2567 UpdateLocationViewPosition(m_nTopSubLine, m_nTopSubLine + GetScreenLines());
2570 * @brief Select linedifference in the current line.
2572 * Select line difference in current line. Selection type
2573 * is choosed by highlight type.
2575 template<bool reversed>
2576 void CMergeEditView::OnSelectLineDiff()
2578 // Pass this to the document, to compare this file to other
2579 GetDocument()->Showlinediff(this, reversed);
2582 /// Enable select difference menuitem if current line is inside difference.
2583 void CMergeEditView::OnUpdateSelectLineDiff(CCmdUI* pCmdUI)
2585 int line = GetCursorPos().y;
2586 bool enable = ((GetLineFlags(line) & (LF_DIFF | LF_GHOST)) != 0);
2587 if (GetDocument()->IsEditedAfterRescan(m_nThisPane))
2589 pCmdUI->Enable(enable);
2593 * @brief Enable/disable Replace-menuitem
2595 void CMergeEditView::OnUpdateEditReplace(CCmdUI* pCmdUI)
2597 CMergeDoc *pd = GetDocument();
2598 bool bReadOnly = pd->m_ptBuf[m_nThisPane]->GetReadOnly();
2600 pCmdUI->Enable(!bReadOnly);
2604 * @brief Update readonly statusbaritem
2606 void CMergeEditView::OnUpdateStatusRO(CCmdUI* pCmdUI)
2608 bool bRO = GetDocument()->m_ptBuf[pCmdUI->m_nID - ID_STATUS_PANE0FILE_RO]->GetReadOnly();
2609 pCmdUI->Enable(bRO);
2613 * @brief Create the dynamic submenu for scripts
2615 HMENU CMergeEditView::createScriptsSubmenu(HMENU hMenu)
2618 std::vector<String> functionNamesList = FileTransform::GetFreeFunctionsInScripts(L"EDITOR_SCRIPT");
2621 size_t i = GetMenuItemCount(hMenu);
2623 DeleteMenu(hMenu, 0, MF_BYPOSITION);
2625 if (functionNamesList.size() == 0)
2627 // no script : create a <empty> entry
2628 AppendMenu(hMenu, MF_STRING, ID_NO_EDIT_SCRIPTS, _("< Empty >").c_str());
2632 // or fill in the submenu with the scripts names
2633 int ID = ID_SCRIPT_FIRST; // first ID in menu
2634 for (i = 0 ; i < functionNamesList.size() ; i++, ID++)
2635 AppendMenu(hMenu, MF_STRING, ID, functionNamesList[i].c_str());
2637 functionNamesList.clear();
2640 if (!plugin::IsWindowsScriptThere())
2641 AppendMenu(hMenu, MF_STRING, ID_NO_SCT_SCRIPTS, _("WSH not found - .sct scripts disabled").c_str());
2647 * @brief Create the dynamic submenu for prediffers
2649 * @note The plugins are grouped in (suggested) and (not suggested)
2650 * The IDs follow the order of GetAvailableScripts
2652 * suggested 0 ID_1ST + 0
2653 * suggested 1 ID_1ST + 2
2654 * suggested 2 ID_1ST + 5
2655 * not suggested 0 ID_1ST + 1
2656 * not suggested 1 ID_1ST + 3
2657 * not suggested 2 ID_1ST + 4
2659 HMENU CMergeEditView::createPrediffersSubmenu(HMENU hMenu)
2662 int i = GetMenuItemCount(hMenu);
2664 DeleteMenu(hMenu, 0, MF_BYPOSITION);
2666 CMergeDoc *pd = GetDocument();
2667 ASSERT(pd != nullptr);
2670 AppendMenu(hMenu, MF_STRING, ID_NO_PREDIFFER, _("No prediffer (normal)").c_str());
2672 // get the scriptlet files
2673 PluginArray * piScriptArray =
2674 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
2675 PluginArray * piScriptArray2 =
2676 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
2678 // build the menu : first part, suggested plugins
2680 AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);
2681 AppendMenu(hMenu, MF_STRING, ID_SUGGESTED_PLUGINS, _("Suggested plugins").c_str());
2683 int ID = ID_PREDIFFERS_FIRST; // first ID in menu
2685 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
2687 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
2688 if (plugin->m_disabled || !plugin->TestAgainstRegList(pd->m_strBothFilenames))
2691 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2693 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
2695 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
2696 if (plugin->m_disabled || !plugin->TestAgainstRegList(pd->m_strBothFilenames))
2699 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2702 // build the menu : second part, others plugins
2704 AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);
2705 AppendMenu(hMenu, MF_STRING, ID_NOT_SUGGESTED_PLUGINS, _("Other plugins").c_str());
2707 ID = ID_PREDIFFERS_FIRST; // first ID in menu
2708 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
2710 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
2711 if (plugin->m_disabled || plugin->TestAgainstRegList(pd->m_strBothFilenames) != false)
2714 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2716 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
2718 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
2719 if (plugin->m_disabled || plugin->TestAgainstRegList(pd->m_strBothFilenames) != false)
2722 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2725 // compute the m_CurrentPredifferID (to set the radio button)
2726 PrediffingInfo prediffer;
2727 pd->GetPrediffer(&prediffer);
2729 if (prediffer.m_PluginOrPredifferMode != PLUGIN_MANUAL)
2730 m_CurrentPredifferID = 0;
2731 else if (prediffer.m_PluginName.empty())
2732 m_CurrentPredifferID = ID_NO_PREDIFFER;
2735 ID = ID_PREDIFFERS_FIRST; // first ID in menu
2736 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
2738 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
2739 if (prediffer.m_PluginName == plugin->m_name)
2740 m_CurrentPredifferID = ID;
2743 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
2745 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
2746 if (prediffer.m_PluginName == plugin->m_name)
2747 m_CurrentPredifferID = ID;
2755 * @brief Offer a context menu built with scriptlet/ActiveX functions
2757 void CMergeEditView::OnContextMenu(CWnd* pWnd, CPoint point)
2759 // Create the menu and populate it with the available functions
2761 VERIFY(menu.LoadMenu(IDR_POPUP_MERGEVIEW));
2763 // Remove copying item copying from active side
2764 if (m_nThisPane == 0) // left?
2766 menu.RemoveMenu(ID_COPY_FROM_RIGHT, MF_BYCOMMAND);
2767 menu.RemoveMenu(ID_COPY_FROM_LEFT, MF_BYCOMMAND);
2769 if (m_nThisPane == GetDocument()->m_nBuffers - 1)
2771 menu.RemoveMenu(ID_COPY_FROM_LEFT, MF_BYCOMMAND);
2772 menu.RemoveMenu(ID_COPY_FROM_RIGHT, MF_BYCOMMAND);
2775 VERIFY(menu.LoadToolbar(IDR_MAINFRAME));
2776 theApp.TranslateMenu(menu.m_hMenu);
2778 BCMenu *pSub = static_cast<BCMenu *>(menu.GetSubMenu(0));
2779 ASSERT(pSub != nullptr);
2781 // Context menu opened using keyboard has no coordinates
2782 if (point.x == -1 && point.y == -1)
2785 GetClientRect(rect);
2786 ClientToScreen(rect);
2788 point = rect.TopLeft();
2792 pSub->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
2793 point.x, point.y, AfxGetMainWnd());
2798 * @brief Update EOL mode in status bar
2800 void CMergeEditView::OnUpdateStatusEOL(CCmdUI* pCmdUI)
2802 GetGroupView(pCmdUI->m_nID - ID_STATUS_PANE0FILE_EOL)->OnUpdateIndicatorCRLF(pCmdUI);
2806 * @brief Change EOL mode and unify all the lines EOL to this new mode
2808 void CMergeEditView::OnConvertEolTo(UINT nID )
2810 CRLFSTYLE nStyle = CRLF_STYLE_AUTOMATIC;;
2814 nStyle = CRLF_STYLE_DOS;
2816 case ID_EOL_TO_UNIX:
2817 nStyle = CRLF_STYLE_UNIX;
2820 nStyle = CRLF_STYLE_MAC;
2824 _RPTF0(_CRT_ERROR, "Unhandled EOL type conversion!");
2827 m_pTextBuffer->SetCRLFMode(nStyle);
2829 // we don't need a derived applyEOLMode for ghost lines as they have no EOL char
2830 if (m_pTextBuffer->applyEOLMode())
2832 CMergeDoc *pd = GetDocument();
2833 ASSERT(pd != nullptr);
2834 pd->UpdateHeaderPath(m_nThisPane);
2835 pd->FlushAndRescan(true);
2840 * @brief allow convert to entries in file submenu
2842 void CMergeEditView::OnUpdateConvertEolTo(CCmdUI* pCmdUI)
2844 int nStyle = CRLF_STYLE_AUTOMATIC;
2845 switch (pCmdUI->m_nID)
2848 nStyle = CRLF_STYLE_DOS;
2850 case ID_EOL_TO_UNIX:
2851 nStyle = CRLF_STYLE_UNIX;
2854 nStyle = CRLF_STYLE_MAC;
2858 _RPTF0(_CRT_ERROR, "Missing menuitem handler for EOL convert menu!");
2862 if (GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
2863 GetDocument()->IsMixedEOL(m_nThisPane) ||
2864 nStyle != m_pTextBuffer->GetCRLFMode())
2866 pCmdUI->SetRadio(false);
2868 // Don't allow selecting other EOL style for protected pane
2869 if (IsReadOnly(m_nThisPane))
2870 pCmdUI->Enable(false);
2873 pCmdUI->SetRadio(true);
2877 * @brief Copy diff from left to right and advance to next diff
2879 void CMergeEditView::OnL2RNext()
2882 if (IsCursorInDiff()) // for 3-way file compare
2888 * @brief Update "Copy right and advance" UI item
2890 void CMergeEditView::OnUpdateL2RNext(CCmdUI* pCmdUI)
2892 OnUpdateL2r(pCmdUI);
2896 * @brief Copy diff from right to left and advance to next diff
2898 void CMergeEditView::OnR2LNext()
2901 if (IsCursorInDiff()) // for 3-way file compare
2907 * @brief Update "Copy left and advance" UI item
2909 void CMergeEditView::OnUpdateR2LNext(CCmdUI* pCmdUI)
2911 OnUpdateR2l(pCmdUI);
2915 * @brief Change active pane in MergeView.
2916 * Changes active pane and makes sure cursor position is kept in
2917 * screen. Currently we put cursor in same line than in original
2918 * active pane but we could be smarter too? Maybe update cursor
2919 * only when it is not visible in new pane?
2921 void CMergeEditView::OnChangePane()
2923 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
2924 CMergeEditView *pWnd = static_cast<CMergeEditView*>(pSplitterWnd->GetActivePane());
2925 CMergeDoc *pDoc = GetDocument();
2926 bool bFound = false;
2927 CMergeEditView *pNextActiveView = nullptr;
2928 std::vector<CMergeEditView *> list = pDoc->GetViewList();
2929 list.insert(list.end(), list.begin(), list.end());
2930 for (auto& pView : list)
2932 if (bFound && pView->m_bDetailView == pWnd->m_bDetailView)
2934 pNextActiveView = pView;
2940 GetParentFrame()->SetActiveView(pNextActiveView);
2941 CPoint ptCursor = pWnd->GetCursorPos();
2943 if (ptCursor.y >= pNextActiveView->GetLineCount())
2944 ptCursor.y = pNextActiveView->GetLineCount() - 1;
2945 pNextActiveView->SetCursorPos(ptCursor);
2946 pNextActiveView->SetAnchor(ptCursor);
2947 pNextActiveView->SetSelection(ptCursor, ptCursor);
2951 * @brief Show "Go To" dialog and scroll views to line or diff.
2953 * Before dialog is opened, current line and file is determined
2955 * @note Conversions needed between apparent and real lines
2957 void CMergeEditView::OnWMGoto()
2960 CMergeDoc *pDoc = GetDocument();
2961 CPoint pos = GetCursorPos();
2965 nRealLine = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(pos.y);
2966 int nLineCount = pDoc->m_ptBuf[m_nThisPane]->GetLineCount();
2967 nLastLine = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(nLineCount - 1);
2969 // Set active file and current line selected in dialog
2970 dlg.m_strParam = strutils::to_str(nRealLine + 1);
2971 dlg.m_nFile = (pDoc->m_nBuffers < 3) ? (m_nThisPane == 1 ? 2 : 0) : m_nThisPane;
2972 dlg.m_nGotoWhat = 0;
2974 if (dlg.DoModal() == IDOK)
2976 CMergeDoc * pDoc1 = GetDocument();
2977 CMergeEditView * pCurrentView = nullptr;
2980 pCurrentView = GetGroupView(m_nThisPane);
2983 try { num = std::stoi(dlg.m_strParam) - 1; } catch(...) {}
2985 if (dlg.m_nGotoWhat == 0)
2987 int nRealLine1 = num;
2990 if (nRealLine1 > nLastLine)
2991 nRealLine1 = nLastLine;
2993 GotoLine(nRealLine1, true, (pDoc1->m_nBuffers < 3) ? (dlg.m_nFile == 2 ? 1 : 0) : dlg.m_nFile);
3000 if (diff >= pDoc1->m_diffList.GetSize())
3001 diff = pDoc1->m_diffList.GetSize();
3003 pCurrentView->SelectDiff(diff, true, false);
3008 void CMergeEditView::OnShellMenu()
3010 CFrameWnd *pFrame = GetTopLevelFrame();
3011 ASSERT(pFrame != nullptr);
3012 BOOL bAutoMenuEnableOld = pFrame->m_bAutoMenuEnable;
3013 pFrame->m_bAutoMenuEnable = FALSE;
3015 String path = GetDocument()->m_filePaths[m_nThisPane];
3016 std::unique_ptr<CShellContextMenu> pContextMenu(new CShellContextMenu(0x9000, 0x9FFF));
3017 pContextMenu->Initialize();
3018 pContextMenu->AddItem(paths::GetParentPath(path), paths::FindFileName(path));
3019 pContextMenu->RequeryShellContextMenu();
3021 ::GetCursorPos(&point);
3022 HWND hWnd = GetSafeHwnd();
3023 BOOL nCmd = TrackPopupMenu(pContextMenu->GetHMENU(), TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, 0, hWnd, nullptr);
3025 pContextMenu->InvokeCommand(nCmd, hWnd);
3026 pContextMenu->ReleaseShellContextMenu();
3028 pFrame->m_bAutoMenuEnable = bAutoMenuEnableOld;
3031 void CMergeEditView::OnUpdateShellMenu(CCmdUI* pCmdUI)
3033 pCmdUI->Enable(!GetDocument()->m_filePaths[m_nThisPane].empty());
3037 * @brief Reload options.
3039 void CMergeEditView::RefreshOptions()
3041 m_bAutomaticRescan = GetOptionsMgr()->GetBool(OPT_AUTOMATIC_RESCAN);
3043 if (GetOptionsMgr()->GetInt(OPT_TAB_TYPE) == 0)
3044 SetInsertTabs(true);
3046 SetInsertTabs(false);
3048 SetSelectionMargin(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
3050 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
3051 SetTextType(CCrystalTextView::SRC_PLAIN);
3053 SetWordWrapping(GetOptionsMgr()->GetBool(OPT_WORDWRAP));
3054 SetViewLineNumbers(GetOptionsMgr()->GetBool(OPT_VIEW_LINENUMBERS));
3056 SetViewTabs(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE));
3057 SetViewEols(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE),
3058 GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
3059 GetDocument()->IsMixedEOL(m_nThisPane));
3061 Options::DiffColors::Load(GetOptionsMgr(), m_cachedColors);
3064 void CMergeEditView::OnScripts(UINT nID )
3066 // text is CHAR if compiled without UNICODE, WCHAR with UNICODE
3067 String text = GetSelectedText();
3069 // transform the text with a script/ActiveX function, event=EDITOR_SCRIPT
3070 bool bChanged = FileTransform::Interactive(text, L"EDITOR_SCRIPT", nID - ID_SCRIPT_FIRST);
3072 // now replace the text
3073 ReplaceSelection(text.c_str(), text.length(), 0);
3077 * @brief Called when an editor script item is updated
3079 void CMergeEditView::OnUpdateNoEditScripts(CCmdUI* pCmdUI)
3081 // append the scripts submenu
3082 HMENU scriptsSubmenu = dynamic_cast<CMainFrame*>(AfxGetMainWnd())->GetScriptsSubmenu(AfxGetMainWnd()->GetMenu()->m_hMenu);
3083 if (scriptsSubmenu != nullptr)
3084 createScriptsSubmenu(scriptsSubmenu);
3086 pCmdUI->Enable(true);
3090 * @brief Called when an editor script item is updated
3092 void CMergeEditView::OnUpdatePrediffer(CCmdUI* pCmdUI)
3094 pCmdUI->Enable(true);
3096 CMergeDoc *pd = GetDocument();
3097 ASSERT(pd != nullptr);
3098 PrediffingInfo prediffer;
3099 pd->GetPrediffer(&prediffer);
3101 if (prediffer.m_PluginOrPredifferMode != PLUGIN_MANUAL)
3103 pCmdUI->SetRadio(false);
3107 // Detect when CDiffWrapper::RunFileDiff has canceled a buggy prediffer
3108 if (prediffer.m_PluginName.empty())
3109 m_CurrentPredifferID = ID_NO_PREDIFFER;
3111 pCmdUI->SetRadio(pCmdUI->m_nID == static_cast<UINT>(m_CurrentPredifferID));
3115 * @brief Update "Prediffer" menuitem
3117 void CMergeEditView::OnUpdateNoPrediffer(CCmdUI* pCmdUI)
3119 // recreate the sub menu (to fill the "selected prediffers")
3120 GetMainFrame()->UpdatePrediffersMenu();
3124 void CMergeEditView::OnNoPrediffer()
3126 OnPrediffer(ID_NO_PREDIFFER);
3129 * @brief Handler for all prediffer choices, including ID_PREDIFF_MANUAL, ID_PREDIFF_AUTO, ID_NO_PREDIFFER, & specific prediffers
3131 void CMergeEditView::OnPrediffer(UINT nID )
3133 CMergeDoc *pd = GetDocument();
3134 ASSERT(pd != nullptr);
3136 SetPredifferByMenu(nID);
3137 pd->FlushAndRescan(true);
3141 * @brief Handler for all prediffer choices.
3142 * Prediffer choises include ID_PREDIFF_MANUAL, ID_PREDIFF_AUTO,
3143 * ID_NO_PREDIFFER, & specific prediffers.
3145 void CMergeEditView::SetPredifferByMenu(UINT nID )
3147 CMergeDoc *pd = GetDocument();
3148 ASSERT(pd != nullptr);
3150 if (nID == ID_NO_PREDIFFER)
3152 m_CurrentPredifferID = nID;
3153 // All flags are set correctly during the construction
3154 PrediffingInfo *infoPrediffer = new PrediffingInfo;
3155 infoPrediffer->m_PluginOrPredifferMode = PLUGIN_MANUAL;
3156 infoPrediffer->m_PluginName.clear();
3157 pd->SetPrediffer(infoPrediffer);
3158 pd->FlushAndRescan(true);
3162 // get the scriptlet files
3163 PluginArray * piScriptArray =
3164 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
3165 PluginArray * piScriptArray2 =
3166 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
3168 // build a PrediffingInfo structure fom the ID
3169 PrediffingInfo prediffer;
3170 prediffer.m_PluginOrPredifferMode = PLUGIN_MANUAL;
3172 size_t pluginNumber = nID - ID_PREDIFFERS_FIRST;
3173 if (pluginNumber < piScriptArray->size())
3175 prediffer.m_bWithFile = true;
3176 const PluginInfoPtr & plugin = piScriptArray->at(pluginNumber);
3177 prediffer.m_PluginName = plugin->m_name;
3181 pluginNumber -= piScriptArray->size();
3182 if (pluginNumber >= piScriptArray2->size())
3184 prediffer.m_bWithFile = false;
3185 const PluginInfoPtr & plugin = piScriptArray2->at(pluginNumber);
3186 prediffer.m_PluginName = plugin->m_name;
3189 // update data for the radio button
3190 m_CurrentPredifferID = nID;
3192 // update the prediffer and rescan
3193 pd->SetPrediffer(&prediffer);
3197 * @brief Look through available prediffers, and return ID of requested one, if found
3199 int CMergeEditView::FindPrediffer(LPCTSTR prediffer) const
3202 int ID = ID_PREDIFFERS_FIRST;
3204 // Search file prediffers
3205 PluginArray * piScriptArray =
3206 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
3207 for (i=0; i<piScriptArray->size(); ++i, ++ID)
3209 const PluginInfoPtr & plugin = piScriptArray->at(i);
3210 if (plugin->m_name == prediffer)
3214 // Search buffer prediffers
3215 PluginArray * piScriptArray2 =
3216 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
3217 for (i=0; i<piScriptArray2->size(); ++i, ++ID)
3219 const PluginInfoPtr & plugin = piScriptArray2->at(i);
3220 if (plugin->m_name == prediffer)
3228 * @brief Look through available prediffers, and return ID of requested one, if found
3230 bool CMergeEditView::SetPredifferByName(const CString & prediffer)
3232 int id = FindPrediffer(prediffer);
3233 if (id<0) return false;
3234 SetPredifferByMenu(id);
3239 * @brief Goto given line.
3240 * @param [in] nLine Destination linenumber
3241 * @param [in] bRealLine if true linenumber is real line, otherwise
3242 * it is apparent line (including deleted lines)
3243 * @param [in] pane Pane index of goto target pane (0 = left, 1 = right).
3245 void CMergeEditView::GotoLine(UINT nLine, bool bRealLine, int pane)
3247 CMergeDoc *pDoc = GetDocument();
3248 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3249 CMergeEditView *pCurrentView = nullptr;
3250 if (pSplitterWnd != nullptr)
3251 pCurrentView = static_cast<CMergeEditView*>
3252 (pSplitterWnd->GetActivePane());
3254 int nRealLine = nLine;
3255 int nApparentLine = nLine;
3257 // Compute apparent (shown linenumber) line
3260 if (nRealLine > pDoc->m_ptBuf[pane]->GetLineCount() - 1)
3261 nRealLine = pDoc->m_ptBuf[pane]->GetLineCount() - 1;
3263 nApparentLine = pDoc->m_ptBuf[pane]->ComputeApparentLine(nRealLine);
3267 ptPos.y = nApparentLine;
3269 // Scroll line to center of view
3270 int nScrollLine = GetSubLineIndex(nApparentLine);
3271 nScrollLine -= GetScreenLines() / 2;
3272 if (nScrollLine < 0)
3275 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3277 CMergeEditView *pView = GetGroupView(nPane);
3278 pView->ScrollToSubLine(nScrollLine);
3279 if (ptPos.y < pView->GetLineCount())
3281 pView->SetCursorPos(ptPos);
3282 pView->SetAnchor(ptPos);
3286 CPoint ptPos1(0, pView->GetLineCount() - 1);
3287 pView->SetCursorPos(ptPos1);
3288 pView->SetAnchor(ptPos1);
3292 // If goto target is another view - activate another view.
3293 // This is done for user convenience as user probably wants to
3294 // work with goto target file.
3295 if (GetGroupView(pane) != pCurrentView)
3297 if (pSplitterWnd != nullptr)
3299 if (pSplitterWnd->GetColumnCount() > 1)
3300 pSplitterWnd->SetActivePane(0, pane);
3302 pSplitterWnd->SetActivePane(pane, 0);
3308 * @brief Check for horizontal scroll. Re-route to CSplitterEx if not from
3311 void CMergeEditView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
3313 if (pScrollBar == nullptr)
3315 // Scroll did not come frome a scroll bar
3316 // Find the appropriate scroll bar
3317 // and send the message to the splitter window instead
3318 // The event should eventually come back here but with a valid scrollbar
3319 // Along the way it will be propagated to other windows that need it
3320 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3321 CScrollBar* curBar = this->GetScrollBarCtrl(SB_HORZ);
3322 pSplitterWnd->SendMessage(WM_HSCROLL,
3323 MAKELONG(nSBCode, nPos), (LPARAM)curBar->m_hWnd);
3326 CCrystalTextView::OnHScroll (nSBCode, nPos, pScrollBar);
3330 * @brief When view is scrolled using scrollbars update location pane.
3332 void CMergeEditView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
3334 if (pScrollBar == nullptr)
3336 // Scroll did not come frome a scroll bar
3337 // Find the appropriate scroll bar
3338 // and send the message to the splitter window instead
3339 // The event should eventually come back here but with a valid scrollbar
3340 // Along the way it will be propagated to other windows that need it
3341 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3342 CScrollBar* curBar = this->GetScrollBarCtrl(SB_VERT);
3343 pSplitterWnd->SendMessage(WM_VSCROLL,
3344 MAKELONG(nSBCode, nPos), (LPARAM)curBar->m_hWnd);
3347 CCrystalTextView::OnVScroll (nSBCode, nPos, pScrollBar);
3349 if (nSBCode == SB_ENDSCROLL)
3352 // Note we cannot use nPos because of its 16-bit nature
3353 SCROLLINFO si = {0};
3354 si.cbSize = sizeof (si);
3355 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
3356 VERIFY (GetScrollInfo (SB_VERT, &si));
3358 // Get the current position of scroll box.
3359 int nCurPos = si.nPos;
3361 UpdateLocationViewPosition(nCurPos, nCurPos + GetScreenLines());
3365 * @brief Copy selected lines adding linenumbers.
3367 void CMergeEditView::OnEditCopyLineNumbers()
3375 CMergeDoc *pDoc = GetDocument();
3376 GetSelection(ptStart, ptEnd);
3378 // Get last selected line (having widest linenumber)
3379 int line = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(ptEnd.y);
3380 size_t nNumWidth = strutils::to_str(line + 1).length();
3382 for (int i = ptStart.y; i <= ptEnd.y; i++)
3384 if (GetLineFlags(i) & LF_GHOST || (GetEnableHideLines() && (GetLineFlags(i) & LF_INVISIBLE)))
3387 // We need to convert to real linenumbers
3388 line = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(i);
3390 // Insert spaces to align different width linenumbers (99, 100)
3391 strLine = GetLineText(i);
3392 CString sSpaces(' ', static_cast<int>(nNumWidth - strutils::to_str(line + 1).length()));
3395 strNumLine.Format(_T("%d: %s"), line + 1, (LPCTSTR)strLine);
3396 strText += strNumLine;
3398 PutToClipboard(strText, strText.GetLength(), m_bColumnSelection);
3401 void CMergeEditView::OnUpdateEditCopyLinenumbers(CCmdUI* pCmdUI)
3403 CCrystalEditViewEx::OnUpdateEditCopy(pCmdUI);
3407 * @brief Open active file with associated application.
3409 * First tries to open file using shell 'Edit' action, since that
3410 * action open scripts etc. to editor instead of running them. If
3411 * edit-action is not registered, 'Open' action is used.
3413 void CMergeEditView::OnOpenFile()
3415 CMergeDoc * pDoc = GetDocument();
3416 ASSERT(pDoc != nullptr);
3418 String sFileName = pDoc->m_filePaths[m_nThisPane];
3419 if (sFileName.empty())
3421 HINSTANCE rtn = ShellExecute(::GetDesktopWindow(), _T("edit"), sFileName.c_str(),
3422 0, 0, SW_SHOWNORMAL);
3423 if (reinterpret_cast<uintptr_t>(rtn) == SE_ERR_NOASSOC)
3424 rtn = ShellExecute(::GetDesktopWindow(), _T("open"), sFileName.c_str(),
3425 0, 0, SW_SHOWNORMAL);
3426 if (reinterpret_cast<uintptr_t>(rtn) == SE_ERR_NOASSOC)
3431 * @brief Open active file with app selection dialog
3433 void CMergeEditView::OnOpenFileWith()
3435 CMergeDoc * pDoc = GetDocument();
3436 ASSERT(pDoc != nullptr);
3438 String sFileName = pDoc->m_filePaths[m_nThisPane];
3439 if (sFileName.empty())
3443 if (!GetSystemDirectory(sysdir.GetBuffer(MAX_PATH), MAX_PATH))
3445 sysdir.ReleaseBuffer();
3446 CString arg = (CString)_T("shell32.dll,OpenAs_RunDLL ") + sFileName.c_str();
3447 ShellExecute(::GetDesktopWindow(), 0, _T("RUNDLL32.EXE"), arg,
3448 sysdir, SW_SHOWNORMAL);
3452 * @brief Open active file with external editor
3454 void CMergeEditView::OnOpenFileWithEditor()
3456 CMergeDoc * pDoc = GetDocument();
3457 ASSERT(pDoc != nullptr);
3459 String sFileName = pDoc->m_filePaths[m_nThisPane];
3460 if (sFileName.empty())
3463 int nRealLine = ComputeRealLine(GetCursorPos().y) + 1;
3464 theApp.OpenFileToExternalEditor(sFileName, nRealLine);
3468 * @brief Force repaint of the location pane.
3470 void CMergeEditView::RepaintLocationPane()
3472 // Must force recalculation due to caching of data in location pane.
3473 CLocationView *pLocationView = GetDocument()->GetLocationView();
3474 if (pLocationView != nullptr)
3475 pLocationView->ForceRecalculate();
3479 * @brief Enables/disables linediff (different color for diffs)
3481 void CMergeEditView::OnViewLineDiffs()
3483 bool bWordDiffHighlight = GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT);
3484 GetOptionsMgr()->SaveOption(OPT_WORDDIFF_HIGHLIGHT, !bWordDiffHighlight);
3486 // Call CMergeDoc RefreshOptions() to refresh *both* views
3487 CMergeDoc *pDoc = GetDocument();
3488 pDoc->RefreshOptions();
3489 pDoc->FlushAndRescan(true);
3492 void CMergeEditView::OnUpdateViewLineDiffs(CCmdUI* pCmdUI)
3494 pCmdUI->Enable(true);
3495 pCmdUI->SetCheck(GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT));
3499 * @brief Enables/disables line number
3501 void CMergeEditView::OnViewLineNumbers()
3503 GetOptionsMgr()->SaveOption(OPT_VIEW_LINENUMBERS, !GetViewLineNumbers());
3505 // Call CMergeDoc RefreshOptions() to refresh *both* views
3506 CMergeDoc *pDoc = GetDocument();
3507 pDoc->RefreshOptions();
3510 void CMergeEditView::OnUpdateViewLineNumbers(CCmdUI* pCmdUI)
3512 pCmdUI->Enable(true);
3513 pCmdUI->SetCheck(GetViewLineNumbers());
3517 * @brief Enables/disables word wrap
3519 void CMergeEditView::OnViewWordWrap()
3521 GetOptionsMgr()->SaveOption(OPT_WORDWRAP, !m_bWordWrap);
3523 // Call CMergeDoc RefreshOptions() to refresh *both* views
3524 CMergeDoc *pDoc = GetDocument();
3525 pDoc->RefreshOptions();
3526 pDoc->UpdateAllViews(this);
3531 void CMergeEditView::OnUpdateViewWordWrap(CCmdUI* pCmdUI)
3533 pCmdUI->Enable(true);
3534 pCmdUI->SetCheck(m_bWordWrap);
3537 void CMergeEditView::OnViewWhitespace()
3539 GetOptionsMgr()->SaveOption(OPT_VIEW_WHITESPACE, !GetViewTabs());
3541 // Call CMergeDoc RefreshOptions() to refresh *both* views
3542 CMergeDoc *pDoc = GetDocument();
3543 pDoc->RefreshOptions();
3546 void CMergeEditView::OnUpdateViewWhitespace(CCmdUI* pCmdUI)
3548 pCmdUI->SetCheck(GetViewTabs());
3551 void CMergeEditView::OnSize(UINT nType, int cx, int cy)
3553 if (!IsInitialized())
3556 CMergeDoc * pDoc = GetDocument();
3557 if (m_nThisPane < pDoc->m_nBuffers - 1)
3559 // To calculate subline index correctly
3560 // we have to invalidate line cache in all pane before calling the function related the subline.
3561 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3563 CMergeEditView *pView = GetGroupView(nPane);
3564 if (pView != nullptr)
3565 pView->InvalidateScreenRect(false);
3570 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3572 CMergeEditView *pView = GetGroupView(nPane);
3573 if (pView != nullptr)
3574 pView->Invalidate();
3577 // recalculate m_nTopSubLine
3578 m_nTopSubLine = GetSubLineIndex(m_nTopLine);
3582 RecalcVertScrollBar (false, false);
3583 RecalcHorzScrollBar (false, false);
3587 * @brief allocates GDI resources for printing
3588 * @param pDC [in] points to the printer device context
3589 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
3591 void CMergeEditView::OnBeginPrinting(CDC * pDC, CPrintInfo * pInfo)
3593 GetParentFrame()->PostMessage(WM_TIMER);
3595 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
3597 CMergeEditView *pView = GetDocument()->GetView(m_nThisGroup, pane);
3598 pView->m_bPrintHeader = true;
3599 pView->m_bPrintFooter = true;
3600 pView->CGhostTextView::OnBeginPrinting(pDC, pInfo);
3605 * @brief frees GDI resources for printing
3606 * @param pDC [in] points to the printer device context
3607 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
3609 void CMergeEditView::OnEndPrinting(CDC * pDC, CPrintInfo * pInfo)
3611 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
3612 GetDocument()->GetView(m_nThisGroup, pane)->CGhostTextView::OnEndPrinting(pDC, pInfo);
3614 GetParentFrame()->PostMessage(WM_TIMER);
3618 * @brief Gets header text to print
3619 * @param [in] nPageNum the page number to print
3620 * @param [out] header text to print
3622 void CMergeEditView::GetPrintHeaderText(int nPageNum, CString & text)
3624 text = GetDocument()->GetTitle();
3628 * @brief Prints header
3629 * @param [in] nPageNum the page number to print
3631 void CMergeEditView::PrintHeader(CDC * pdc, int nPageNum)
3633 if (m_nThisPane > 0)
3635 int oldRight = m_rcPrintArea.right;
3636 m_rcPrintArea.right += m_rcPrintArea.Width() * (GetDocument()->m_nBuffers - 1);
3637 CGhostTextView::PrintHeader(pdc, nPageNum);
3638 m_rcPrintArea.right = oldRight;
3642 * @brief Prints footer
3643 * @param [in] nPageNum the page number to print
3645 void CMergeEditView::PrintFooter(CDC * pdc, int nPageNum)
3647 if (m_nThisPane > 0)
3649 int oldRight = m_rcPrintArea.right;
3650 m_rcPrintArea.right += m_rcPrintArea.Width() * (GetDocument()->m_nBuffers - 1);
3651 CGhostTextView::PrintFooter(pdc, nPageNum);
3652 m_rcPrintArea.right = oldRight;
3655 void CMergeEditView::RecalcPageLayouts (CDC * pDC, CPrintInfo * pInfo)
3657 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
3658 GetDocument()->GetView(m_nThisGroup, pane)->CGhostTextView::RecalcPageLayouts(pDC, pInfo);
3662 * @brief Prints or previews both panes.
3663 * @param pDC [in] points to the printer device context
3664 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
3666 void CMergeEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
3668 CRect rDraw = pInfo->m_rectDraw;
3669 CSize sz = rDraw.Size();
3670 CMergeDoc *pDoc = GetDocument();
3672 SIZE szLeftTop, szRightBottom;
3673 GetPrintMargins(szLeftTop.cx, szLeftTop.cy, szRightBottom.cx, szRightBottom.cy);
3674 pDC->HIMETRICtoLP(&szLeftTop);
3675 pDC->HIMETRICtoLP(&szRightBottom);
3677 int midX = (sz.cx - szLeftTop.cx - szRightBottom.cx) / pDoc->m_nBuffers;
3680 for (int pane = 0; pane < pDoc->m_nBuffers; pane++)
3682 pInfo->m_rectDraw.left = rDraw.left + midX * pane;
3683 pInfo->m_rectDraw.right = pInfo->m_rectDraw.left + midX + szLeftTop.cx + szRightBottom.cx;
3684 CMergeEditView* pPane = pDoc->GetView(m_nThisGroup, pane);
3685 pPane->CGhostTextView::OnPrint(pDC, pInfo);
3689 bool CMergeEditView::IsInitialized() const
3691 CMergeEditView * pThis = const_cast<CMergeEditView *>(this);
3692 CDiffTextBuffer * pBuffer = dynamic_cast<CDiffTextBuffer *>(pThis->LocateTextBuffer());
3693 return pBuffer->IsInitialized();
3697 * @brief returns the number of empty lines which are added for synchronizing the line in two/three panes.
3699 int CMergeEditView::GetEmptySubLines( int nLineIndex )
3701 int nBreaks[3] = {0};
3702 int nMaxBreaks = -1;
3703 CMergeDoc * pDoc = GetDocument();
3704 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3706 CMergeEditView *pView = GetGroupView(nPane);
3707 if (pView != nullptr)
3709 if (nLineIndex >= pView->GetLineCount())
3711 pView->WrapLineCached( nLineIndex, pView->GetScreenChars(), nullptr, nBreaks[nPane] );
3713 nMaxBreaks = max(nMaxBreaks, nBreaks[nPane]);
3716 if (nBreaks[m_nThisPane] < nMaxBreaks)
3717 return nMaxBreaks - nBreaks[m_nThisPane];
3723 * @brief Invalidate sub line index cache from the specified index to the end of file.
3724 * @param [in] nLineIndex Index of the first line to invalidate
3726 void CMergeEditView::InvalidateSubLineIndexCache( int nLineIndex )
3728 CMergeDoc * pDoc = GetDocument();
3729 ASSERT(pDoc != nullptr);
3731 // We have to invalidate sub line index cache on both panes.
3732 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3734 CMergeEditView *pView = GetGroupView(nPane);
3735 if (pView != nullptr)
3736 pView->CCrystalTextView::InvalidateSubLineIndexCache( nLineIndex );
3740 void CMergeEditView::SetWordWrapping( bool bWordWrap )
3742 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
3743 GetGroupView(pane)->m_bWordWrap = bWordWrap;
3744 CCrystalTextView::SetWordWrapping(bWordWrap);
3748 * @brief Swap the positions of the two panes
3750 void CMergeEditView::OnViewSwapPanes()
3752 GetDocument()->SwapFiles();
3756 * @brief Check if cursor is inside difference.
3757 * @return true if cursor is inside difference.
3759 bool CMergeEditView::IsCursorInDiff() const
3761 return m_bCurrentLineIsDiff;
3765 * @brief Determine if difference is visible on screen.
3766 * @param [in] nDiff Number of diff to check.
3767 * @return true if difference is visible.
3769 bool CMergeEditView::IsDiffVisible(int nDiff)
3771 const CMergeDoc *pd = GetDocument();
3774 pd->m_diffList.GetDiff(nDiff, diff);
3776 return IsDiffVisible(diff);
3780 * @brief Determine if difference is visible on screen.
3781 * @param [in] diff diff to check.
3782 * @param [in] nLinesBelow Allow "minimizing" the number of visible lines.
3783 * @return true if difference is visible, false otherwise.
3785 bool CMergeEditView::IsDiffVisible(const DIFFRANGE& diff, int nLinesBelow /*=0*/)
3787 const int nDiffStart = GetSubLineIndex(diff.dbegin);
3788 const int nDiffEnd = GetSubLineIndex(diff.dend);
3789 // Diff's height is last line - first line + last line's line count
3790 const int nDiffHeight = nDiffEnd - nDiffStart + GetSubLines(diff.dend) + 1;
3792 // If diff first line outside current view - context OR
3793 // if diff last line outside current view - context OR
3794 // if diff is bigger than screen
3795 if ((nDiffStart < m_nTopSubLine) ||
3796 (nDiffEnd >= m_nTopSubLine + GetScreenLines() - nLinesBelow) ||
3797 (nDiffHeight >= GetScreenLines()))
3807 /** @brief Open help from mainframe when user presses F1*/
3808 void CMergeEditView::OnHelp()
3810 theApp.ShowHelp(MergeViewHelpLocation);
3814 * @brief Called after document is loaded.
3815 * This function is called from CMergeDoc::OpenDocs() after documents are
3816 * loaded. So this is good place to set View's options etc.
3818 void CMergeEditView::DocumentsLoaded()
3820 // Enable/disable automatic rescan (rescanning after edit)
3821 EnableRescan(GetOptionsMgr()->GetBool(OPT_AUTOMATIC_RESCAN));
3823 // SetTextType will revert to language dependent defaults for tab
3824 SetTabSize(GetOptionsMgr()->GetInt(OPT_TAB_SIZE));
3825 SetViewTabs(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE));
3826 const bool mixedEOLs = GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
3827 GetDocument()->IsMixedEOL(m_nThisPane);
3828 SetViewEols(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE), mixedEOLs);
3829 SetWordWrapping(GetOptionsMgr()->GetBool(OPT_WORDWRAP));
3830 SetViewLineNumbers(GetOptionsMgr()->GetBool(OPT_VIEW_LINENUMBERS));
3831 SetSelectionMargin(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
3833 // Enable Backspace at beginning of line
3834 SetDisableBSAtSOL(false);
3836 // Set tab type (tabs/spaces)
3837 bool bInsertTabs = (GetOptionsMgr()->GetInt(OPT_TAB_TYPE) == 0);
3838 SetInsertTabs(bInsertTabs);
3840 // Sometimes WinMerge doesn't update scrollbars correctly (they remain
3841 // disabled) after docs are open in screen. So lets make sure they are
3842 // really updated, even though this is unnecessary in most cases.
3843 RecalcHorzScrollBar();
3844 RecalcVertScrollBar();
3848 * @brief Update LocationView position.
3849 * This function updates LocationView position to given lines.
3850 * Usually we want to lines in file compare view and area in
3851 * LocationView to match. Be extra carefull to not call non-existing
3853 * @param [in] nTopLine Top line of current view.
3854 * @param [in] nBottomLine Bottom line of current view.
3856 void CMergeEditView::UpdateLocationViewPosition(int nTopLine /*=-1*/,
3857 int nBottomLine /*= -1*/)
3859 CMergeDoc *pDoc = GetDocument();
3860 if (pDoc == nullptr)
3863 CLocationView *pLocationView = pDoc->GetLocationView();
3865 if (pLocationView != nullptr && IsWindow(pLocationView->GetSafeHwnd()))
3867 pLocationView->UpdateVisiblePos(nTopLine, nBottomLine);
3872 * @brief Enable/Disable view's selection margins.
3873 * Selection margins show bookmarks and word-wrap symbols, so they are pretty
3874 * useful. But it appears many users don't use/need those features and for them
3875 * selection margins are just wasted screen estate.
3877 void CMergeEditView::OnViewMargin()
3879 bool bViewMargin = GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN);
3880 GetOptionsMgr()->SaveOption(OPT_VIEW_FILEMARGIN, !bViewMargin);
3882 SetSelectionMargin(!bViewMargin);
3883 CMergeDoc *pDoc = GetDocument();
3884 pDoc->RefreshOptions();
3885 pDoc->UpdateAllViews(this);
3889 * @brief Update GUI for Enable/Disable view's selection margin.
3890 * @param [in] pCmdUI Pointer to UI item to update.
3892 void CMergeEditView::OnUpdateViewMargin(CCmdUI* pCmdUI)
3894 pCmdUI->Enable(true);
3895 pCmdUI->SetCheck(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
3899 * @brief Create the "Change Scheme" sub menu.
3900 * @param [in] pCmdUI Pointer to UI item to update.
3902 void CMergeEditView::OnUpdateViewChangeScheme(CCmdUI *pCmdUI)
3904 // Delete the place holder menu.
3905 pCmdUI->m_pSubMenu->DeleteMenu(0, MF_BYPOSITION);
3907 const HMENU hSubMenu = pCmdUI->m_pSubMenu->m_hMenu;
3909 String name = theApp.LoadString(ID_COLORSCHEME_FIRST);
3910 AppendMenu(hSubMenu, MF_STRING, ID_COLORSCHEME_FIRST, name.c_str());
3911 AppendMenu(hSubMenu, MF_SEPARATOR, 0, nullptr);
3913 for (int i = ID_COLORSCHEME_FIRST + 1; i <= ID_COLORSCHEME_LAST; ++i)
3915 name = theApp.LoadString(i);
3916 AppendMenu(hSubMenu, MF_STRING, i, name.c_str());
3919 pCmdUI->Enable(true);
3923 * @brief Change the editor's syntax highlighting scheme.
3924 * @param [in] nID Selected color scheme sub menu id.
3926 void CMergeEditView::OnChangeScheme(UINT nID)
3928 CMergeDoc *pDoc = GetDocument();
3929 ASSERT(pDoc != nullptr);
3931 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3933 CMergeEditView *pView = GetGroupView(nPane);
3934 ASSERT(pView != nullptr);
3936 if (pView != nullptr)
3938 pView->SetTextType(CCrystalTextView::TextType(nID - ID_COLORSCHEME_FIRST));
3939 pView->SetDisableBSAtSOL(false);
3943 pDoc->UpdateAllViews(nullptr);
3947 * @brief Enable all color schemes sub menu items.
3948 * @param [in] pCmdUI Pointer to UI item to update.
3950 void CMergeEditView::OnUpdateChangeScheme(CCmdUI* pCmdUI)
3952 const bool bIsCurrentScheme = (static_cast<UINT>(m_CurSourceDef->type) == (pCmdUI->m_nID - ID_COLORSCHEME_FIRST));
3953 pCmdUI->SetRadio(bIsCurrentScheme);
3954 pCmdUI->Enable(GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT));
3958 * @brief Called when mouse's wheel is scrolled.
3960 BOOL CMergeEditView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
3962 if ( nFlags == MK_CONTROL )
3964 short amount = zDelta < 0 ? -1: 1;
3967 // no default CCrystalTextView
3968 return CView::OnMouseWheel(nFlags, zDelta, pt);
3971 if (nFlags == MK_SHIFT)
3973 SCROLLINFO si = { sizeof SCROLLINFO };
3974 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
3976 VERIFY(GetScrollInfo(SB_HORZ, &si));
3979 si.nPos -= zDelta / 40;
3980 if (si.nPos > si.nMax) si.nPos = si.nMax;
3981 if (si.nPos < si.nMin) si.nPos = si.nMin;
3983 SetScrollInfo(SB_HORZ, &si);
3986 SendMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, si.nPos) , NULL );
3988 // no default CCrystalTextView
3989 return CView::OnMouseWheel(nFlags, zDelta, pt);
3992 return CGhostTextView::OnMouseWheel(nFlags, zDelta, pt);
3996 * @brief Change font size (zoom) in views.
3997 * @param [in] amount Amount of change/zoom, negative number makes
3998 * font smaller, positive number bigger and 0 reset the font size.
4000 void CMergeEditView::ZoomText(short amount)
4005 const int nLogPixelsY = CClientDC(this).GetDeviceCaps(LOGPIXELSY);
4006 int nPointSize = -MulDiv(lf.lfHeight, 72, nLogPixelsY);
4010 nPointSize = -MulDiv(GetOptionsMgr()->GetInt(OPT_FONT_FILECMP + OPT_FONT_HEIGHT), 72, nLogPixelsY);
4013 nPointSize += amount;
4017 lf.lfHeight = -MulDiv(nPointSize, nLogPixelsY, 72);
4019 CMergeDoc *pDoc = GetDocument();
4020 ASSERT(pDoc != nullptr);
4022 if (pDoc != nullptr)
4024 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4026 CMergeEditView *pView = GetGroupView(nPane);
4027 ASSERT(pView != nullptr);
4029 if (pView != nullptr)
4038 * @brief Called when user selects View/Zoom In from menu.
4040 void CMergeEditView::OnViewZoomIn()
4046 * @brief Called when user selects View/Zoom Out from menu.
4048 void CMergeEditView::OnViewZoomOut()
4054 * @brief Called when user selects View/Zoom Normal from menu.
4056 void CMergeEditView::OnViewZoomNormal()
4061 void CMergeEditView::OnDropFiles(const std::vector<String>& tFiles)
4063 if (tFiles.size() > 1 || paths::IsDirectory(tFiles[0]))
4065 GetMainFrame()->GetDropHandler()->GetCallback()(tFiles);
4069 GetDocument()->ChangeFile(m_nThisPane, tFiles[0]);
4072 void CMergeEditView::OnWindowSplit()
4075 auto& wndSplitter = dynamic_cast<CMergeEditFrame *>(GetParentFrame())->GetSplitter();
4076 CMergeDoc *pDoc = GetDocument();
4077 CMergeEditView *pView = pDoc->GetView(0, m_nThisPane);
4078 auto* pwndSplitterChild = pView->GetParentSplitter(pView, false);
4079 int nBuffer = m_nThisPane;
4080 if (pDoc->m_nGroups <= 2)
4082 wndSplitter.SplitRow(1);
4083 wndSplitter.EqualizeRows();
4087 wndSplitter.SetActivePane(0, 0);
4088 wndSplitter.DeleteRow(1);
4089 if (pwndSplitterChild->GetColumnCount() > 1)
4090 pwndSplitterChild->SetActivePane(0, nBuffer);
4092 pwndSplitterChild->SetActivePane(nBuffer, 0);
4096 void CMergeEditView::OnUpdateWindowSplit(CCmdUI* pCmdUI)
4098 pCmdUI->Enable(TRUE);
4099 pCmdUI->SetCheck(GetDocument()->m_nGroups > 2);