1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
5 // SPDX-License-Identifier: GPL-2.0-or-later
6 /////////////////////////////////////////////////////////////////////////////
8 * @file MergeEditView.cpp
10 * @brief Implementation of the CMergeEditView class
14 #include "MergeEditView.h"
18 #include "LocationView.h"
21 #include "OptionsMgr.h"
22 #include "OptionsDiffColors.h"
23 #include "FileTransform.h"
25 #include "WMGotoDlg.h"
26 #include "OptionsDef.h"
27 #include "SyntaxColors.h"
28 #include "MergeEditFrm.h"
29 #include "MergeLineFlags.h"
31 #include "DropHandler.h"
33 #include "ShellContextMenu.h"
40 #ifndef WM_MOUSEHWHEEL
41 # define WM_MOUSEHWHEEL 0x20e
45 using CrystalLineParser::TEXTBLOCK;
47 /** @brief Timer ID for delayed rescan. */
48 const UINT IDT_RESCAN = 2;
49 /** @brief Timer timeout for delayed rescan. */
50 const UINT RESCAN_TIMEOUT = 1000;
52 /** @brief Location for file compare specific help to open. */
53 static TCHAR MergeViewHelpLocation[] = _T("::/htmlhelp/Compare_files.html");
55 /////////////////////////////////////////////////////////////////////////////
58 IMPLEMENT_DYNCREATE(CMergeEditView, CCrystalEditViewEx)
60 CMergeEditView::CMergeEditView()
61 : m_bCurrentLineIsDiff(false)
64 , m_bDetailView(false)
65 , m_piMergeEditStatus(nullptr)
66 , m_bAutomaticRescan(false)
67 , fTimerWaitingForIdle(0)
70 , m_CurrentPredifferID(0)
71 , m_bChangedSchemeManually(false)
73 SetParser(&m_xParser);
75 Options::DiffColors::Load(GetOptionsMgr(), m_cachedColors);
78 CMergeEditView::~CMergeEditView()
83 BEGIN_MESSAGE_MAP(CMergeEditView, CCrystalEditViewEx)
84 //{{AFX_MSG_MAP(CMergeEditView)
85 ON_COMMAND(ID_CURDIFF, OnCurdiff)
86 ON_UPDATE_COMMAND_UI(ID_CURDIFF, OnUpdateCurdiff)
87 ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
88 ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
89 ON_COMMAND(ID_EDIT_CUT, OnEditCut)
90 ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
91 ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
92 ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
93 ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
94 ON_COMMAND(ID_FIRSTDIFF, OnFirstdiff)
95 ON_UPDATE_COMMAND_UI(ID_FIRSTDIFF, OnUpdateFirstdiff)
96 ON_COMMAND(ID_LASTDIFF, OnLastdiff)
97 ON_UPDATE_COMMAND_UI(ID_LASTDIFF, OnUpdateLastdiff)
98 ON_COMMAND(ID_NEXTDIFF, OnNextdiff)
99 ON_UPDATE_COMMAND_UI(ID_NEXTDIFF, OnUpdateNextdiff)
100 ON_COMMAND(ID_PREVDIFF, OnPrevdiff)
101 ON_UPDATE_COMMAND_UI(ID_PREVDIFF, OnUpdatePrevdiff)
102 ON_COMMAND(ID_NEXTCONFLICT, OnNextConflict)
103 ON_UPDATE_COMMAND_UI(ID_NEXTCONFLICT, OnUpdateNextConflict)
104 ON_COMMAND(ID_PREVCONFLICT, OnPrevConflict)
105 ON_UPDATE_COMMAND_UI(ID_PREVCONFLICT, OnUpdatePrevConflict)
106 ON_COMMAND(ID_NEXTDIFFLM, OnNextdiffLM)
107 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLM, OnUpdateNextdiffLM)
108 ON_COMMAND(ID_PREVDIFFLM, OnPrevdiffLM)
109 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLM, OnUpdatePrevdiffLM)
110 ON_COMMAND(ID_NEXTDIFFLR, OnNextdiffLR)
111 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLR, OnUpdateNextdiffLR)
112 ON_COMMAND(ID_PREVDIFFLR, OnPrevdiffLR)
113 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLR, OnUpdatePrevdiffLR)
114 ON_COMMAND(ID_NEXTDIFFMR, OnNextdiffMR)
115 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFMR, OnUpdateNextdiffMR)
116 ON_COMMAND(ID_PREVDIFFMR, OnPrevdiffMR)
117 ON_UPDATE_COMMAND_UI(ID_PREVDIFFMR, OnUpdatePrevdiffMR)
118 ON_COMMAND(ID_NEXTDIFFLO, OnNextdiffLO)
119 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFLO, OnUpdateNextdiffLO)
120 ON_COMMAND(ID_PREVDIFFLO, OnPrevdiffLO)
121 ON_UPDATE_COMMAND_UI(ID_PREVDIFFLO, OnUpdatePrevdiffLO)
122 ON_COMMAND(ID_NEXTDIFFMO, OnNextdiffMO)
123 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFMO, OnUpdateNextdiffMO)
124 ON_COMMAND(ID_PREVDIFFMO, OnPrevdiffMO)
125 ON_UPDATE_COMMAND_UI(ID_PREVDIFFMO, OnUpdatePrevdiffMO)
126 ON_COMMAND(ID_NEXTDIFFRO, OnNextdiffRO)
127 ON_UPDATE_COMMAND_UI(ID_NEXTDIFFRO, OnUpdateNextdiffRO)
128 ON_COMMAND(ID_PREVDIFFRO, OnPrevdiffRO)
129 ON_UPDATE_COMMAND_UI(ID_PREVDIFFRO, OnUpdatePrevdiffRO)
130 ON_WM_LBUTTONDBLCLK()
133 ON_COMMAND(ID_ALL_LEFT, OnAllLeft)
134 ON_UPDATE_COMMAND_UI(ID_ALL_LEFT, OnUpdateAllLeft)
135 ON_COMMAND(ID_ALL_RIGHT, OnAllRight)
136 ON_UPDATE_COMMAND_UI(ID_ALL_RIGHT, OnUpdateAllRight)
137 ON_COMMAND(ID_AUTO_MERGE, OnAutoMerge)
138 ON_UPDATE_COMMAND_UI(ID_AUTO_MERGE, OnUpdateAutoMerge)
139 ON_COMMAND(ID_L2R, OnL2r)
140 ON_UPDATE_COMMAND_UI(ID_L2R, OnUpdateL2r)
141 ON_COMMAND(ID_LINES_L2R, OnLinesL2r)
142 ON_UPDATE_COMMAND_UI(ID_LINES_L2R, OnUpdateLinesL2r)
143 ON_COMMAND(ID_R2L, OnR2l)
144 ON_UPDATE_COMMAND_UI(ID_R2L, OnUpdateR2l)
145 ON_COMMAND(ID_LINES_R2L, OnLinesR2l)
146 ON_UPDATE_COMMAND_UI(ID_LINES_R2L, OnUpdateLinesR2l)
147 ON_COMMAND(ID_COPY_FROM_LEFT, OnCopyFromLeft)
148 ON_UPDATE_COMMAND_UI(ID_COPY_FROM_LEFT, OnUpdateCopyFromLeft)
149 ON_COMMAND(ID_COPY_LINES_FROM_LEFT, OnCopyLinesFromLeft)
150 ON_UPDATE_COMMAND_UI(ID_COPY_LINES_FROM_LEFT, OnUpdateCopyLinesFromLeft)
151 ON_COMMAND(ID_COPY_FROM_RIGHT, OnCopyFromRight)
152 ON_UPDATE_COMMAND_UI(ID_COPY_FROM_RIGHT, OnUpdateCopyFromRight)
153 ON_COMMAND(ID_COPY_LINES_FROM_RIGHT, OnCopyLinesFromRight)
154 ON_UPDATE_COMMAND_UI(ID_COPY_LINES_FROM_RIGHT, OnUpdateCopyLinesFromRight)
155 ON_COMMAND(ID_ADD_SYNCPOINT, OnAddSyncPoint)
156 ON_COMMAND(ID_CLEAR_SYNCPOINTS, OnClearSyncPoints)
157 ON_UPDATE_COMMAND_UI(ID_CLEAR_SYNCPOINTS, OnUpdateClearSyncPoints)
158 ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
159 ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
160 ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
162 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_LEFT, OnUpdateFileSaveLeft)
163 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_MIDDLE, OnUpdateFileSaveMiddle)
164 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_RIGHT, OnUpdateFileSaveRight)
165 ON_COMMAND(ID_REFRESH, OnRefresh)
166 ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
167 ON_COMMAND(ID_SELECTLINEDIFF, OnSelectLineDiff<false>)
168 ON_UPDATE_COMMAND_UI(ID_SELECTLINEDIFF, OnUpdateSelectLineDiff)
169 ON_COMMAND(ID_SELECTPREVLINEDIFF, OnSelectLineDiff<true>)
170 ON_UPDATE_COMMAND_UI(ID_SELECTPREVLINEDIFF, OnUpdateSelectLineDiff)
171 ON_COMMAND(ID_ADD_TO_IGNORED_SUBSTITUTIONS, OnAddToSubstitutionFilters)
172 ON_UPDATE_COMMAND_UI(ID_ADD_TO_IGNORED_SUBSTITUTIONS, OnUpdateAddToSubstitutionFilters)
174 ON_UPDATE_COMMAND_UI(ID_EDIT_REPLACE, OnUpdateEditReplace)
175 ON_COMMAND(ID_FILE_LEFT_READONLY, OnLeftReadOnly)
176 ON_UPDATE_COMMAND_UI(ID_FILE_LEFT_READONLY, OnUpdateLeftReadOnly)
177 ON_COMMAND(ID_FILE_MIDDLE_READONLY, OnMiddleReadOnly)
178 ON_UPDATE_COMMAND_UI(ID_FILE_MIDDLE_READONLY, OnUpdateMiddleReadOnly)
179 ON_COMMAND(ID_FILE_RIGHT_READONLY, OnRightReadOnly)
180 ON_UPDATE_COMMAND_UI(ID_FILE_RIGHT_READONLY, OnUpdateRightReadOnly)
181 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE0FILE_RO, OnUpdateStatusRO)
182 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE1FILE_RO, OnUpdateStatusRO)
183 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE2FILE_RO, OnUpdateStatusRO)
184 ON_COMMAND_RANGE(ID_EOL_TO_DOS, ID_EOL_TO_MAC, OnConvertEolTo)
185 ON_UPDATE_COMMAND_UI_RANGE(ID_EOL_TO_DOS, ID_EOL_TO_MAC, OnUpdateConvertEolTo)
186 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE0FILE_EOL, OnUpdateStatusEOL)
187 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE1FILE_EOL, OnUpdateStatusEOL)
188 ON_UPDATE_COMMAND_UI(ID_STATUS_PANE2FILE_EOL, OnUpdateStatusEOL)
189 ON_COMMAND(ID_L2RNEXT, OnL2RNext)
190 ON_UPDATE_COMMAND_UI(ID_L2RNEXT, OnUpdateL2RNext)
191 ON_COMMAND(ID_R2LNEXT, OnR2LNext)
192 ON_UPDATE_COMMAND_UI(ID_R2LNEXT, OnUpdateR2LNext)
193 ON_COMMAND(ID_WINDOW_CHANGE_PANE, OnChangePane)
194 ON_COMMAND(ID_NEXT_PANE, OnChangePane)
195 ON_COMMAND(ID_EDIT_WMGOTO, OnWMGoto)
196 ON_COMMAND(ID_GOTO_MOVED_LINE_LM, OnGotoMovedLineLM)
197 ON_UPDATE_COMMAND_UI(ID_GOTO_MOVED_LINE_LM, OnUpdateGotoMovedLineLM)
198 ON_COMMAND(ID_GOTO_MOVED_LINE_MR, OnGotoMovedLineMR)
199 ON_UPDATE_COMMAND_UI(ID_GOTO_MOVED_LINE_MR, OnUpdateGotoMovedLineMR)
200 ON_COMMAND(ID_FILE_SHELLMENU, OnShellMenu)
201 ON_UPDATE_COMMAND_UI(ID_FILE_SHELLMENU, OnUpdateShellMenu)
202 ON_COMMAND_RANGE(ID_SCRIPT_FIRST, ID_SCRIPT_LAST, OnScripts)
203 ON_COMMAND(ID_NO_PREDIFFER, OnNoPrediffer)
204 ON_UPDATE_COMMAND_UI(ID_NO_PREDIFFER, OnUpdateNoPrediffer)
205 ON_COMMAND_RANGE(ID_PREDIFFERS_FIRST, ID_PREDIFFERS_LAST, OnPrediffer)
206 ON_UPDATE_COMMAND_UI_RANGE(ID_PREDIFFERS_FIRST, ID_PREDIFFERS_LAST, OnUpdatePrediffer)
209 ON_COMMAND(ID_EDIT_COPY_LINENUMBERS, OnEditCopyLineNumbers)
210 ON_UPDATE_COMMAND_UI(ID_EDIT_COPY_LINENUMBERS, OnUpdateEditCopyLinenumbers)
211 ON_COMMAND(ID_VIEW_LINEDIFFS, OnViewLineDiffs)
212 ON_UPDATE_COMMAND_UI(ID_VIEW_LINEDIFFS, OnUpdateViewLineDiffs)
213 ON_COMMAND(ID_VIEW_WORDWRAP, OnViewWordWrap)
214 ON_UPDATE_COMMAND_UI(ID_VIEW_WORDWRAP, OnUpdateViewWordWrap)
215 ON_COMMAND(ID_VIEW_LINENUMBERS, OnViewLineNumbers)
216 ON_UPDATE_COMMAND_UI(ID_VIEW_LINENUMBERS, OnUpdateViewLineNumbers)
217 ON_COMMAND(ID_VIEW_WHITESPACE, OnViewWhitespace)
218 ON_UPDATE_COMMAND_UI(ID_VIEW_WHITESPACE, OnUpdateViewWhitespace)
219 ON_COMMAND(ID_VIEW_EOL, OnViewEOL)
220 ON_UPDATE_COMMAND_UI(ID_VIEW_EOL, OnUpdateViewEOL)
221 ON_COMMAND(ID_FILE_OPEN_REGISTERED, OnOpenFile)
222 ON_COMMAND(ID_FILE_OPEN_WITHEDITOR, OnOpenFileWithEditor)
223 ON_COMMAND(ID_FILE_OPEN_WITH, OnOpenFileWith)
224 ON_COMMAND(ID_SWAPPANES_SWAP12, OnViewSwapPanes12)
225 ON_COMMAND(ID_SWAPPANES_SWAP23, OnViewSwapPanes23)
226 ON_COMMAND(ID_SWAPPANES_SWAP13, OnViewSwapPanes13)
227 ON_UPDATE_COMMAND_UI(ID_NO_EDIT_SCRIPTS, OnUpdateNoEditScripts)
230 ON_COMMAND(ID_HELP, OnHelp)
231 ON_COMMAND(ID_VIEW_SELMARGIN, OnViewMargin)
232 ON_UPDATE_COMMAND_UI(ID_VIEW_SELMARGIN, OnUpdateViewMargin)
233 ON_UPDATE_COMMAND_UI(ID_VIEW_CHANGESCHEME, OnUpdateViewChangeScheme)
234 ON_COMMAND_RANGE(ID_COLORSCHEME_FIRST, ID_COLORSCHEME_LAST, OnChangeScheme)
235 ON_UPDATE_COMMAND_UI_RANGE(ID_COLORSCHEME_FIRST, ID_COLORSCHEME_LAST, OnUpdateChangeScheme)
238 ON_COMMAND(ID_VIEW_ZOOMIN, OnViewZoomIn)
239 ON_COMMAND(ID_VIEW_ZOOMOUT, OnViewZoomOut)
240 ON_COMMAND(ID_VIEW_ZOOMNORMAL, OnViewZoomNormal)
241 ON_COMMAND(ID_WINDOW_SPLIT, OnWindowSplit)
242 ON_UPDATE_COMMAND_UI(ID_WINDOW_SPLIT, OnUpdateWindowSplit)
243 ON_NOTIFY(NM_DBLCLK, AFX_IDW_STATUS_BAR, OnStatusBarDblClick)
248 /////////////////////////////////////////////////////////////////////////////
249 // CMergeEditView diagnostics
252 CMergeDoc* CMergeEditView::GetDocument() // non-debug version is inline
254 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMergeDoc)));
255 return (CMergeDoc*)m_pDocument;
260 /////////////////////////////////////////////////////////////////////////////
261 // CMergeEditView message handlers
264 * @brief Return text buffer for file in view
266 CCrystalTextBuffer *CMergeEditView::LocateTextBuffer()
268 return GetDocument()->m_ptBuf[m_nThisPane].get();
272 * @brief Update any resources necessary after a GUI language change
274 void CMergeEditView::UpdateResources()
278 CMergeEditView *CMergeEditView::GetGroupView(int nBuffer) const
280 return GetDocument()->GetView(m_nThisGroup, nBuffer);
283 void CMergeEditView::PrimeListWithFile()
285 // Set the tab size now, just in case the options change...
286 // We don't update it at the end of OnOptions,
287 // we can update it safely now
288 SetTabSize(GetOptionsMgr()->GetInt(OPT_TAB_SIZE));
291 * @brief Return text from line given
293 CString CMergeEditView::GetLineText(int idx)
295 return GetLineChars(idx);
299 * @brief Return text from selection
301 CString CMergeEditView::GetSelectedText()
304 auto [ptStart, ptEnd] = GetSelection();
305 if (ptStart != ptEnd)
306 GetTextWithoutEmptys(ptStart.y, ptStart.x, ptEnd.y, ptEnd.x, strText);
311 * @brief Return number of selected characters
313 std::pair<int, int> CMergeEditView::GetSelectedLineAndCharacterCount()
315 auto [ptStart, ptEnd] = GetSelection();
316 int nCharsOrColumns =0;
317 int nSelectedLines = 0;
318 for (int nLine = ptStart.y; nLine <= ptEnd.y; ++nLine)
320 if ((GetLineFlags(nLine) & (LF_GHOST | LF_INVISIBLE)) == 0)
322 int nLineLength = GetLineLength(nLine) + (m_pTextBuffer->GetLineEol(nLine)[0] ? 1 : 0);
323 nCharsOrColumns += (nLine == ptEnd.y) ? ptEnd.x : nLineLength;
324 if (nLine == ptStart.y)
325 nCharsOrColumns -= ptStart.x;
326 if (nLine < ptEnd.y || (ptStart != ptEnd && ptEnd.x > 0))
330 if (m_bRectangularSelection)
332 int nStartLeft, nStartRight, nEndLeft, nEndRight;
333 GetColumnSelection(ptStart.y, nStartLeft, nStartRight);
334 GetColumnSelection(ptEnd.y, nEndLeft, nEndRight);
335 nCharsOrColumns = (std::max)(nStartRight, nEndRight) - (std::min)(nStartLeft, nEndLeft);
337 return { nSelectedLines, nCharsOrColumns };
341 * @brief Get diffs inside selection.
342 * @param [out] firstDiff First diff inside selection
343 * @param [out] lastDiff Last diff inside selection
344 * @note -1 is returned in parameters if diffs cannot be determined
345 * @todo This shouldn't be called when there is no diffs, so replace
346 * first 'if' with ASSERT()?
348 void CMergeEditView::GetFullySelectedDiffs(int & firstDiff, int & lastDiff)
353 CMergeDoc *pd = GetDocument();
354 const int nDiffs = pd->m_diffList.GetSignificantDiffs();
358 int firstLine, lastLine;
359 GetFullySelectedLines(firstLine, lastLine);
360 if (lastLine < firstLine)
363 firstDiff = pd->m_diffList.NextSignificantDiffFromLine(firstLine);
364 lastDiff = pd->m_diffList.PrevSignificantDiffFromLine(lastLine);
365 if (firstDiff != -1 && lastDiff != -1)
369 // Check that first selected line is first diff's first line or above it
370 VERIFY(pd->m_diffList.GetDiff(firstDiff, di));
371 if ((int)di.dbegin < firstLine)
373 if (firstDiff < lastDiff)
377 // Check that last selected line is last diff's last line or below it
378 VERIFY(pd->m_diffList.GetDiff(lastDiff, di));
379 if ((int)di.dend > lastLine)
381 if (firstDiff < lastDiff)
385 // Special case: one-line diff is not selected if cursor is in it
386 if (firstLine == lastLine)
395 * @brief Get diffs inside selection.
396 * @param [out] firstDiff First diff inside selection
397 * @param [out] lastDiff Last diff inside selection
398 * @param [out] firstWordDiff First word level diff inside selection
399 * @param [out] lastWordDiff Last word level diff inside selection
400 * @note -1 is returned in parameters if diffs cannot be determined
401 * @todo This shouldn't be called when there is no diffs, so replace
402 * first 'if' with ASSERT()?
404 void CMergeEditView::GetFullySelectedDiffs(int & firstDiff, int & lastDiff, int & firstWordDiff, int & lastWordDiff, const CPoint *pptStart, const CPoint *pptEnd)
411 CMergeDoc *pd = GetDocument();
412 const int nDiffs = pd->m_diffList.GetSignificantDiffs();
416 int firstLine, lastLine;
417 auto [ptStart, ptEnd] = GetSelection();
418 if (pptStart != nullptr)
420 if (pptEnd != nullptr)
422 firstLine = ptStart.y;
425 firstDiff = pd->m_diffList.LineToDiff(firstLine);
426 bool firstLineIsNotInDiff = firstDiff == -1;
429 firstDiff = pd->m_diffList.NextSignificantDiffFromLine(firstLine);
434 lastDiff = pd->m_diffList.LineToDiff(lastLine);
435 bool lastLineIsNotInDiff = lastDiff == -1;
437 lastDiff = pd->m_diffList.PrevSignificantDiffFromLine(lastLine);
438 if (lastDiff < firstDiff)
445 if (firstDiff != -1 && lastDiff != -1)
449 if (pd->EqualCurrentWordDiff(m_nThisPane, ptStart, ptEnd))
451 firstWordDiff = lastWordDiff = static_cast<int>(pd->GetCurrentWordDiff().nWordDiff);
453 else if (ptStart != ptEnd)
455 VERIFY(pd->m_diffList.GetDiff(firstDiff, di));
456 if (lastLineIsNotInDiff && (firstLineIsNotInDiff || (di.dbegin == firstLine && ptStart.x == 0)))
462 if (firstWordDiff == -1)
464 vector<WordDiff> worddiffs = pd->GetWordDiffArrayInDiffBlock(firstDiff);
465 for (size_t i = 0; i < worddiffs.size(); ++i)
467 int worddiffLen = worddiffs[i].end[m_nThisPane] - worddiffs[i].begin[m_nThisPane];
468 if (worddiffs[i].endline[m_nThisPane] > firstLine ||
469 (firstLine == worddiffs[i].endline[m_nThisPane] &&
470 worddiffs[i].end[m_nThisPane] - (worddiffLen == 0 ? 0 : 1) >= ptStart.x))
472 firstWordDiff = static_cast<int>(i);
477 if (firstLine >= di.dbegin && firstWordDiff == -1)
484 VERIFY(pd->m_diffList.GetDiff(lastDiff, di));
485 vector<WordDiff> worddiffs = pd->GetWordDiffArrayInDiffBlock(lastDiff);
486 for (size_t i = worddiffs.size() - 1; i != (size_t)-1; --i)
488 if (worddiffs[i].beginline[m_nThisPane] < lastLine ||
489 (lastLine == worddiffs[i].beginline[m_nThisPane] && worddiffs[i].begin[m_nThisPane] + 1 <= ptEnd.x))
491 lastWordDiff = static_cast<int>(i);
496 if (lastLine <= di.dend && lastWordDiff == -1)
499 if (firstDiff == lastDiff && (lastWordDiff != -1 && lastWordDiff < firstWordDiff))
506 else if (lastDiff < firstDiff || (firstDiff == lastDiff && firstWordDiff == -1 && lastWordDiff == -1))
523 ASSERT(firstDiff == -1 ? (lastDiff == -1 && firstWordDiff == -1 && lastWordDiff == -1) : true);
524 ASSERT(lastDiff == -1 ? (firstDiff == -1 && firstWordDiff == -1 && lastWordDiff == -1) : true);
525 ASSERT(firstDiff != -1 ? firstWordDiff != -1 : true);
528 void CMergeEditView::GetSelectedDiffs(int & firstDiff, int & lastDiff)
533 CMergeDoc *pd = GetDocument();
534 const int nDiffs = pd->m_diffList.GetSignificantDiffs();
538 int firstLine, lastLine;
539 auto [ptStart, ptEnd] = GetSelection();
540 firstLine = ptStart.y;
543 firstDiff = pd->m_diffList.LineToDiff(firstLine);
546 firstDiff = pd->m_diffList.NextSignificantDiffFromLine(firstLine);
550 lastDiff = pd->m_diffList.LineToDiff(lastLine);
552 lastDiff = pd->m_diffList.PrevSignificantDiffFromLine(lastLine);
553 if (lastDiff < firstDiff)
559 ASSERT(firstDiff == -1 ? (lastDiff == -1) : true);
560 ASSERT(lastDiff == -1 ? (firstDiff == -1) : true);
563 std::map<int, std::vector<int>> CMergeEditView::GetColumnSelectedWordDiffIndice()
565 CMergeDoc *pDoc = GetDocument();
566 std::map<int, std::vector<int>> ret;
567 std::map<int, std::vector<int> *> list;
568 auto [ptStart, ptEnd] = GetSelection();
569 for (int nLine = ptStart.y; nLine <= ptEnd.y; ++nLine)
571 if (pDoc->m_diffList.LineToDiff(nLine) != -1)
573 int firstDiff, lastDiff, firstWordDiff, lastWordDiff;
575 GetColumnSelection(nLine, nLeft, nRight);
576 CPoint ptStart2, ptEnd2;
579 ptStart2.y = ptEnd2.y = nLine;
580 GetFullySelectedDiffs(firstDiff, lastDiff, firstWordDiff, lastWordDiff, &ptStart2, &ptEnd2);
581 if (firstDiff != -1 && lastDiff != -1)
583 std::vector<int> *pWordDiffs;
584 if (list.find(firstDiff) == list.end())
585 list.insert(std::pair<int, std::vector<int> *>(firstDiff, new std::vector<int>()));
586 pWordDiffs = list[firstDiff];
587 for (int i = firstWordDiff; i <= lastWordDiff; ++i)
589 if (pWordDiffs->empty() || i != (*pWordDiffs)[pWordDiffs->size() - 1])
590 pWordDiffs->push_back(i);
595 for (auto& it : list)
596 ret.insert(std::pair<int, std::vector<int>>(it.first, *it.second));
600 void CMergeEditView::OnInitialUpdate()
603 CCrystalEditViewEx::OnInitialUpdate();
605 SetFont(dynamic_cast<CMainFrame*>(AfxGetMainWnd())->m_lfDiff);
606 SetAlternateDropTarget(new DropHandler(std::bind(&CMergeEditView::OnDropFiles, this, std::placeholders::_1)));
612 void CMergeEditView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
614 CCrystalEditViewEx::OnActivateView(bActivate, pActivateView, pDeactiveView);
616 CMergeDoc* pDoc = GetDocument();
617 pDoc->UpdateHeaderActivity(m_nThisPane, !!bActivate);
620 std::vector<CrystalLineParser::TEXTBLOCK> CMergeEditView::GetMarkerTextBlocks(int nLineIndex) const
624 if (nLineIndex < m_lineBegin || nLineIndex > m_lineEnd)
625 return std::vector<CrystalLineParser::TEXTBLOCK>();
627 return CCrystalTextView::GetMarkerTextBlocks(nLineIndex);
630 std::vector<TEXTBLOCK> CMergeEditView::GetAdditionalTextBlocks (int nLineIndex)
632 static const std::vector<TEXTBLOCK> emptyBlocks;
635 if (nLineIndex < m_lineBegin || nLineIndex > m_lineEnd)
639 DWORD dwLineFlags = GetLineFlags(nLineIndex);
640 if ((dwLineFlags & LF_SNP) == LF_SNP || (dwLineFlags & LF_DIFF) != LF_DIFF || (dwLineFlags & LF_MOVED) == LF_MOVED)
643 if (!GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT))
646 CMergeDoc *pDoc = GetDocument();
647 if (pDoc->IsEditedAfterRescan(m_nThisPane))
650 int nDiff = pDoc->m_diffList.LineToDiff(nLineIndex);
655 pDoc->m_diffList.GetDiff(nDiff, cd);
656 int unemptyLineCount = 0;
657 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
659 if (cd.begin[nPane] != cd.end[nPane] + 1)
662 if (unemptyLineCount < 2)
665 vector<WordDiff> worddiffs = pDoc->GetWordDiffArray(nLineIndex);
666 size_t nWordDiffs = worddiffs.size();
668 bool lineInCurrentDiff = IsLineInCurrentDiff(nLineIndex);
670 std::vector<TEXTBLOCK> blocks(nWordDiffs * 2 + 1);
671 blocks[0].m_nCharPos = 0;
672 blocks[0].m_nColorIndex = COLORINDEX_NONE;
673 blocks[0].m_nBgColorIndex = COLORINDEX_NONE;
675 for (i = 0, j = 1; i < nWordDiffs; i++)
677 if (worddiffs[i].beginline[m_nThisPane] > nLineIndex || worddiffs[i].endline[m_nThisPane] < nLineIndex )
679 if (pDoc->m_nBuffers > 2)
681 if (m_nThisPane == 0 && worddiffs[i].op == OP_3RDONLY)
683 else if (m_nThisPane == 2 && worddiffs[i].op == OP_1STONLY)
686 int begin[3], end[3];
687 bool deleted = false;
688 for (int pane = 0; pane < pDoc->m_nBuffers; pane++)
690 begin[pane] = (worddiffs[i].beginline[pane] < nLineIndex) ? 0 : worddiffs[i].begin[pane];
691 end[pane] = (worddiffs[i].endline[pane] > nLineIndex) ? GetGroupView(pane)->GetLineLength(nLineIndex) : worddiffs[i].end[pane];
692 if (worddiffs[i].beginline[pane] == worddiffs[i].endline[pane] &&
693 worddiffs[i].begin[pane] == worddiffs[i].end[pane])
696 blocks[j].m_nCharPos = begin[m_nThisPane];
697 if (lineInCurrentDiff)
699 if (m_cachedColors.clrSelDiffText != CLR_NONE)
700 blocks[j].m_nColorIndex = COLORINDEX_HIGHLIGHTTEXT1 | COLORINDEX_APPLYFORCE;
702 blocks[j].m_nColorIndex = COLORINDEX_NONE;
703 blocks[j].m_nBgColorIndex = COLORINDEX_APPLYFORCE |
704 (deleted ? COLORINDEX_HIGHLIGHTBKGND4 : COLORINDEX_HIGHLIGHTBKGND1);
708 if (m_cachedColors.clrDiffText != CLR_NONE)
709 blocks[j].m_nColorIndex = COLORINDEX_HIGHLIGHTTEXT2 | COLORINDEX_APPLYFORCE;
711 blocks[j].m_nColorIndex = COLORINDEX_NONE;
712 blocks[j].m_nBgColorIndex = COLORINDEX_APPLYFORCE |
713 (deleted ? COLORINDEX_HIGHLIGHTBKGND3 : COLORINDEX_HIGHLIGHTBKGND2);
716 blocks[j].m_nCharPos = end[m_nThisPane];
717 blocks[j].m_nColorIndex = COLORINDEX_NONE;
718 blocks[j].m_nBgColorIndex = COLORINDEX_NONE;
727 COLORREF CMergeEditView::GetColor(int nColorIndex) const
729 switch (nColorIndex & ~COLORINDEX_MASK)
731 case COLORINDEX_HIGHLIGHTBKGND1:
732 return m_cachedColors.clrSelWordDiff;
733 case COLORINDEX_HIGHLIGHTTEXT1:
734 return m_cachedColors.clrSelWordDiffText;
735 case COLORINDEX_HIGHLIGHTBKGND2:
736 return m_cachedColors.clrWordDiff;
737 case COLORINDEX_HIGHLIGHTTEXT2:
738 return m_cachedColors.clrWordDiffText;
739 case COLORINDEX_HIGHLIGHTBKGND3:
740 return m_cachedColors.clrWordDiffDeleted;
741 case COLORINDEX_HIGHLIGHTBKGND4:
742 return m_cachedColors.clrSelWordDiffDeleted;
745 return CCrystalTextView::GetColor(nColorIndex);
750 * @brief Determine text and background color for line
751 * @param [in] nLineIndex Index of line in view (NOT line in file)
752 * @param [out] crBkgnd Backround color for line
753 * @param [out] crText Text color for line
755 void CMergeEditView::GetLineColors(int nLineIndex, COLORREF & crBkgnd,
756 COLORREF & crText, bool & bDrawWhitespace)
758 DWORD ignoreFlags = 0;
759 GetLineColors2(nLineIndex, ignoreFlags, crBkgnd, crText, bDrawWhitespace);
763 * @brief Determine text and background color for line
764 * @param [in] nLineIndex Index of line in view (NOT line in file)
765 * @param [in] ignoreFlags Flags that caller wishes ignored
766 * @param [out] crBkgnd Backround color for line
767 * @param [out] crText Text color for line
769 * This version allows caller to suppress particular flags
771 void CMergeEditView::GetLineColors2(int nLineIndex, DWORD ignoreFlags, COLORREF & crBkgnd,
772 COLORREF & crText, bool & bDrawWhitespace)
774 if (GetLineCount() <= nLineIndex)
777 DWORD dwLineFlags = GetLineFlags(nLineIndex);
779 if (dwLineFlags & ignoreFlags)
780 dwLineFlags &= (~ignoreFlags);
784 // Line with WinMerge flag,
785 // Lines with only the LF_DIFF/LF_TRIVIAL flags are not colored with Winmerge colors
786 if (dwLineFlags & (LF_WINMERGE_FLAGS & ~LF_DIFF & ~LF_TRIVIAL & ~LF_MOVED & ~LF_SNP))
788 crText = m_cachedColors.clrDiffText;
789 bDrawWhitespace = true;
791 if (dwLineFlags & LF_GHOST)
793 crBkgnd = m_cachedColors.clrDiffDeleted;
798 // If no syntax hilighting
799 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
801 crBkgnd = GetColor (COLORINDEX_BKGND);
802 crText = GetColor (COLORINDEX_NORMALTEXT);
803 bDrawWhitespace = false;
806 // Line not inside diff, get colors from CrystalEditor
807 CCrystalEditViewEx::GetLineColors(nLineIndex, crBkgnd,
808 crText, bDrawWhitespace);
810 if (nLineIndex < m_lineBegin || nLineIndex > m_lineEnd)
812 crBkgnd = GetColor (COLORINDEX_WHITESPACE);
813 crText = GetColor (COLORINDEX_WHITESPACE);
814 bDrawWhitespace = false;
820 if (dwLineFlags & LF_WINMERGE_FLAGS)
822 crText = m_cachedColors.clrDiffText;
823 bDrawWhitespace = true;
824 bool lineInCurrentDiff = IsLineInCurrentDiff(nLineIndex);
826 if (dwLineFlags & LF_SNP)
828 if (lineInCurrentDiff)
830 if (dwLineFlags & LF_GHOST)
831 crBkgnd = m_cachedColors.clrSelSNPDeleted;
833 crBkgnd = m_cachedColors.clrSelSNP;
834 crText = m_cachedColors.clrSelSNPText;
838 if (dwLineFlags & LF_GHOST)
839 crBkgnd = m_cachedColors.clrSNPDeleted;
841 crBkgnd = m_cachedColors.clrSNP;
842 crText = m_cachedColors.clrSNPText;
846 else if (dwLineFlags & LF_DIFF)
848 if (lineInCurrentDiff)
850 if (dwLineFlags & LF_MOVED)
852 crBkgnd = m_cachedColors.clrSelMoved;
853 crText = m_cachedColors.clrSelMovedText;
857 crBkgnd = m_cachedColors.clrSelDiff;
858 crText = m_cachedColors.clrSelDiffText;
864 if (dwLineFlags & LF_MOVED)
866 crBkgnd = m_cachedColors.clrMoved;
867 crText = m_cachedColors.clrMovedText;
871 crBkgnd = m_cachedColors.clrDiff;
872 crText = m_cachedColors.clrDiffText;
877 else if (dwLineFlags & LF_TRIVIAL)
879 // trivial diff can not be selected
880 if (dwLineFlags & LF_GHOST)
881 // ghost lines in trivial diff has their own color
882 crBkgnd = m_cachedColors.clrTrivialDeleted;
884 crBkgnd = m_cachedColors.clrTrivial;
885 crText = m_cachedColors.clrTrivialText;
888 else if (dwLineFlags & LF_GHOST)
890 if (lineInCurrentDiff)
892 if (dwLineFlags & LF_MOVED)
893 crBkgnd = m_cachedColors.clrSelMovedDeleted;
895 crBkgnd = m_cachedColors.clrSelDiffDeleted;
899 if (dwLineFlags & LF_MOVED)
900 crBkgnd = m_cachedColors.clrMovedDeleted;
902 crBkgnd = m_cachedColors.clrDiffDeleted;
909 // Line not inside diff,
910 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
912 // If no syntax hilighting, get windows default colors
913 crBkgnd = GetColor (COLORINDEX_BKGND);
914 crText = GetColor (COLORINDEX_NORMALTEXT);
915 bDrawWhitespace = false;
918 // Syntax highlighting, get colors from CrystalEditor
919 CCrystalEditViewEx::GetLineColors(nLineIndex, crBkgnd,
920 crText, bDrawWhitespace);
925 * @brief Sync other pane position
927 void CMergeEditView::UpdateSiblingScrollPos (bool bHorz)
929 CSplitterWnd *pSplitterWnd = GetParentSplitter (this, false);
930 if (pSplitterWnd != nullptr)
932 // See CSplitterWnd::IdFromRowCol() implementation for details
933 int nCurrentRow = (GetDlgCtrlID () - AFX_IDW_PANE_FIRST) / 16;
934 int nCurrentCol = (GetDlgCtrlID () - AFX_IDW_PANE_FIRST) % 16;
935 ASSERT (nCurrentRow >= 0 && nCurrentRow < pSplitterWnd->GetRowCount ());
936 ASSERT (nCurrentCol >= 0 && nCurrentCol < pSplitterWnd->GetColumnCount ());
938 // limit the TopLine : must be smaller than GetLineCount for all the panels
939 int newTopSubLine = m_nTopSubLine;
940 int nRows = pSplitterWnd->GetRowCount ();
941 int nCols = pSplitterWnd->GetColumnCount ();
943 // for (nRow = 0; nRow < nRows; nRow++)
945 // for (int nCol = 0; nCol < nCols; nCol++)
947 // CMergeEditView *pSiblingView = static_cast<CMergeEditView*>(GetSiblingView (nRow, nCol));
948 // if (pSiblingView != nullptr)
949 // if (pSiblingView->GetSubLineCount() <= newTopSubLine)
950 // newTopSubLine = pSiblingView->GetSubLineCount()-1;
953 if (m_nTopSubLine != newTopSubLine)
954 ScrollToSubLine(newTopSubLine);
956 for (nRow = 0; nRow < nRows; nRow++)
958 for (int nCol = 0; nCol < nCols; nCol++)
960 if (!(nRow == nCurrentRow && nCol == nCurrentCol)) // We don't need to update ourselves
962 CMergeEditView *pSiblingView = static_cast<CMergeEditView*>(GetSiblingView (nRow, nCol));
963 if (pSiblingView != nullptr && pSiblingView->m_nThisGroup == m_nThisGroup)
964 pSiblingView->OnUpdateSibling (this, bHorz);
972 * @brief Update other panes
974 void CMergeEditView::OnUpdateSibling (CCrystalTextView * pUpdateSource, bool bHorz)
976 if (pUpdateSource != this)
978 ASSERT (pUpdateSource != nullptr);
979 ASSERT_KINDOF (CCrystalTextView, pUpdateSource);
980 CMergeEditView *pSrcView = static_cast<CMergeEditView*>(pUpdateSource);
981 if (!bHorz) // changed this so bHorz works right
983 ASSERT (pSrcView->m_nTopSubLine >= 0);
985 // This ASSERT is wrong: panes have different files and
986 // different linecounts
987 // ASSERT (pSrcView->m_nTopLine < GetLineCount ());
988 if (pSrcView->m_nTopSubLine != m_nTopSubLine)
990 ScrollToSubLine (pSrcView->m_nTopSubLine, true, false);
992 RecalcVertScrollBar(true);
993 RecalcHorzScrollBar();
998 ASSERT (pSrcView->m_nOffsetChar >= 0);
1000 // This ASSERT is wrong: panes have different files and
1001 // different linelengths
1002 // ASSERT (pSrcView->m_nOffsetChar < GetMaxLineLength ());
1003 if (pSrcView->m_nOffsetChar != m_nOffsetChar)
1005 ScrollToChar (pSrcView->m_nOffsetChar, true, false);
1007 RecalcHorzScrollBar(true);
1008 RecalcHorzScrollBar();
1014 void CMergeEditView::OnDisplayDiff(int nDiff /*=0*/)
1016 int newlineBegin, newlineEnd;
1017 CMergeDoc *pd = GetDocument();
1018 if (nDiff < 0 || nDiff >= pd->m_diffList.GetSize())
1026 VERIFY(pd->m_diffList.GetDiff(nDiff, curDiff));
1028 newlineBegin = curDiff.dbegin;
1029 ASSERT (newlineBegin >= 0);
1030 newlineEnd = curDiff.dend;
1033 m_lineBegin = newlineBegin;
1034 m_lineEnd = newlineEnd;
1036 int nLineCount = GetLineCount();
1037 if (m_lineBegin > nLineCount)
1038 m_lineBegin = nLineCount - 1;
1039 if (m_lineEnd > nLineCount)
1040 m_lineEnd = nLineCount - 1;
1042 if (m_nTopLine == newlineBegin)
1045 // scroll to the first line of the diff
1046 vector<WordDiff> worddiffs;
1047 if (GetOptionsMgr()->GetBool(OPT_SCROLL_TO_FIRST_INLINE_DIFF))
1048 worddiffs = pd->GetWordDiffArrayInDiffBlock(nDiff);
1049 CPoint pt = worddiffs.size() > 0 ?
1050 CPoint{ worddiffs[0].begin[m_nThisPane], worddiffs[0].beginline[m_nThisPane] } :
1051 CPoint{ 0, m_lineBegin };
1052 ScrollToLine(m_lineBegin);
1056 // update the width of the horizontal scrollbar
1057 RecalcHorzScrollBar();
1061 * @brief Selects diff by number and syncs other file
1062 * @param [in] nDiff Diff to select, must be >= 0
1063 * @param [in] bScroll Scroll diff to view
1064 * @param [in] bSelectText Select diff text
1065 * @sa CMergeEditView::ShowDiff()
1066 * @sa CMergeDoc::SetCurrentDiff()
1067 * @todo Parameter bSelectText is never used?
1069 void CMergeEditView::SelectDiff(int nDiff, bool bScroll /*= true*/, bool bSelectText /*= true*/)
1071 CMergeDoc *pd = GetDocument();
1073 // Check that nDiff is valid
1075 _RPTF1(_CRT_ERROR, "Diffnumber negative (%d)", nDiff);
1076 if (nDiff >= pd->m_diffList.GetSize())
1077 _RPTF2(_CRT_ERROR, "Selected diff > diffcount (%d >= %d)",
1078 nDiff, pd->m_diffList.GetSize());
1081 pd->SetCurrentDiff(nDiff);
1082 ShowDiff(bScroll, bSelectText);
1083 pd->UpdateAllViews(this);
1084 UpdateSiblingScrollPos(false);
1086 // notify either side, as it will notify the other one
1087 pd->ForEachView ([&](auto& pView) { if (pView->m_bDetailView) pView->OnDisplayDiff(nDiff); });
1090 void CMergeEditView::DeselectDiffIfCursorNotInCurrentDiff()
1092 CMergeDoc *pd = GetDocument();
1093 // If we have a selected diff, deselect it
1094 int nCurrentDiff = pd->GetCurrentDiff();
1095 if (nCurrentDiff != -1)
1097 CPoint pos = GetCursorPos();
1098 if (!IsLineInCurrentDiff(pos.y))
1100 pd->SetCurrentDiff(-1);
1102 pd->UpdateAllViews(this);
1108 * @brief Called when user selects "Current Difference".
1109 * Goes to active diff. If no active diff, selects diff under cursor
1110 * @sa CMergeEditView::SelectDiff()
1111 * @sa CMergeDoc::GetCurrentDiff()
1112 * @sa CMergeDoc::LineToDiff()
1114 void CMergeEditView::OnCurdiff()
1116 CMergeDoc *pd = GetDocument();
1118 // If no diffs, nothing to select
1119 if (!pd->m_diffList.HasSignificantDiffs())
1122 // GetCurrentDiff() returns -1 if no diff selected
1123 int nDiff = pd->GetCurrentDiff();
1126 // Scroll to the first line of the currently selected diff
1127 SelectDiff(nDiff, true, false);
1131 // If cursor is inside diff, select that diff
1132 CPoint pos = GetCursorPos();
1133 nDiff = pd->m_diffList.LineToDiff(pos.y);
1134 if (nDiff != -1 && pd->m_diffList.IsDiffSignificant(nDiff))
1135 SelectDiff(nDiff, true, false);
1140 * @brief Called when "Current diff" item is updated
1142 void CMergeEditView::OnUpdateCurdiff(CCmdUI* pCmdUI)
1144 CMergeDoc *pd = GetDocument();
1145 CPoint pos = GetCursorPos();
1146 int nCurrentDiff = pd->GetCurrentDiff();
1147 if (nCurrentDiff == -1)
1149 int nNewDiff = pd->m_diffList.LineToDiff(pos.y);
1150 pCmdUI->Enable(nNewDiff != -1 && pd->m_diffList.IsDiffSignificant(nNewDiff));
1153 pCmdUI->Enable(true);
1157 * @brief Copy selected text to clipboard
1159 void CMergeEditView::OnEditCopy()
1161 CMergeDoc * pDoc = GetDocument();
1162 auto [ptSelStart, ptSelEnd] = GetSelection();
1165 if (ptSelStart == ptSelEnd)
1170 if (!m_bRectangularSelection)
1172 CDiffTextBuffer * buffer = pDoc->m_ptBuf[m_nThisPane].get();
1174 buffer->GetTextWithoutEmptys(ptSelStart.y, ptSelStart.x,
1175 ptSelEnd.y, ptSelEnd.x, text);
1178 GetTextWithoutEmptysInColumnSelection(text);
1180 PutToClipboard(text, text.GetLength(), m_bRectangularSelection);
1184 * @brief Called when "Copy" item is updated
1186 void CMergeEditView::OnUpdateEditCopy(CCmdUI* pCmdUI)
1188 CCrystalEditViewEx::OnUpdateEditCopy(pCmdUI);
1192 * @brief Cut current selection to clipboard
1194 void CMergeEditView::OnEditCut()
1196 if (!QueryEditable())
1199 CMergeDoc * pDoc = GetDocument();
1200 auto [ptSelStart, ptSelEnd] = GetSelection();
1203 if (ptSelStart == ptSelEnd)
1207 if (!m_bRectangularSelection)
1208 pDoc->m_ptBuf[m_nThisPane]->GetTextWithoutEmptys(ptSelStart.y, ptSelStart.x,
1209 ptSelEnd.y, ptSelEnd.x, text);
1211 GetTextWithoutEmptysInColumnSelection(text);
1213 PutToClipboard(text, text.GetLength(), m_bRectangularSelection);
1215 if (!m_bRectangularSelection)
1217 CPoint ptCursorPos = ptSelStart;
1218 ASSERT_VALIDTEXTPOS(ptCursorPos);
1219 SetAnchor(ptCursorPos);
1220 SetSelection(ptCursorPos, ptCursorPos);
1221 SetCursorPos(ptCursorPos);
1222 EnsureVisible(ptCursorPos);
1224 pDoc->m_ptBuf[m_nThisPane]->DeleteText(this, ptSelStart.y, ptSelStart.x, ptSelEnd.y,
1225 ptSelEnd.x, CE_ACTION_CUT);
1228 DeleteCurrentColumnSelection (CE_ACTION_CUT);
1230 m_pTextBuffer->SetModified(true);
1234 * @brief Called when "Cut" item is updated
1236 void CMergeEditView::OnUpdateEditCut(CCmdUI* pCmdUI)
1238 if (QueryEditable())
1239 CCrystalEditViewEx::OnUpdateEditCut(pCmdUI);
1241 pCmdUI->Enable(false);
1245 * @brief Paste text from clipboard
1247 void CMergeEditView::OnEditPaste()
1249 if (!QueryEditable())
1252 CCrystalEditViewEx::Paste();
1253 m_pTextBuffer->SetModified(true);
1257 * @brief Called when "Paste" item is updated
1259 void CMergeEditView::OnUpdateEditPaste(CCmdUI* pCmdUI)
1261 if (QueryEditable())
1262 CCrystalEditViewEx::OnUpdateEditPaste(pCmdUI);
1264 pCmdUI->Enable(false);
1268 * @brief Undo last action
1270 void CMergeEditView::OnEditUndo()
1272 CWaitCursor waitstatus;
1273 CMergeDoc* pDoc = GetDocument();
1274 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo-1));
1277 if (!QueryEditable())
1280 GetParentFrame()->SetActiveView(this, true);
1281 if(CCrystalEditViewEx::DoEditUndo())
1284 pDoc->UpdateHeaderPath(m_nThisPane);
1285 pDoc->FlushAndRescan();
1288 m_pTextBuffer->GetRedoActionCode(nAction);
1289 if (nAction == CE_ACTION_MERGE)
1290 // select the diff so we may just merge it again
1296 tgt->SendMessage(WM_COMMAND, ID_EDIT_UNDO);
1298 if (!pDoc->CanUndo())
1299 pDoc->SetAutoMerged(false);
1303 * @brief Called when "Undo" item is updated
1305 void CMergeEditView::OnUpdateEditUndo(CCmdUI* pCmdUI)
1307 CMergeDoc* pDoc = GetDocument();
1308 if (pDoc->curUndo!=pDoc->undoTgt.begin())
1310 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo-1));
1311 pCmdUI->Enable( !IsReadOnly(tgt->m_nThisPane));
1314 pCmdUI->Enable(false);
1318 * @brief Go to first diff
1320 * Called when user selects "First Difference"
1321 * @sa CMergeEditView::SelectDiff()
1323 void CMergeEditView::OnFirstdiff()
1325 CMergeDoc *pd = GetDocument();
1326 if (pd->m_diffList.HasSignificantDiffs())
1328 int nDiff = pd->m_diffList.FirstSignificantDiff();
1329 SelectDiff(nDiff, true, false);
1334 * @brief Update "First diff" UI items
1336 void CMergeEditView::OnUpdateFirstdiff(CCmdUI* pCmdUI)
1338 OnUpdatePrevdiff(pCmdUI);
1342 * @brief Go to last diff
1344 void CMergeEditView::OnLastdiff()
1346 CMergeDoc *pd = GetDocument();
1347 if (pd->m_diffList.HasSignificantDiffs())
1349 int nDiff = pd->m_diffList.LastSignificantDiff();
1350 SelectDiff(nDiff, true, false);
1355 * @brief Update "Last diff" UI items
1357 void CMergeEditView::OnUpdateLastdiff(CCmdUI* pCmdUI)
1359 OnUpdateNextdiff(pCmdUI);
1363 * @brief Go to next diff and select it.
1365 * Finds and selects next difference. There are several cases:
1366 * - if there is selected difference, and that difference is visible
1367 * on screen, next found difference is selected.
1368 * - if there is selected difference but it is not visible, next
1369 * difference from cursor position is selected. This is what user
1370 * expects to happen and is natural thing to do. Also reduces
1371 * needless scrolling.
1372 * - if there is no selected difference, next difference from cursor
1373 * position is selected.
1375 void CMergeEditView::OnNextdiff()
1377 CMergeDoc *pd = GetDocument();
1378 int cnt = pd->m_ptBuf[0]->GetLineCount();
1382 // Returns -1 if no diff selected
1384 int curDiff = pd->GetCurrentDiff();
1388 if (!IsDiffVisible(curDiff))
1390 // Selected difference not visible, select next from cursor
1391 int line = GetCursorPos().y;
1392 // Make sure we aren't in the first line of the diff
1394 if (!IsValidTextPosY(CPoint(0, line)))
1396 nextDiff = pd->m_diffList.NextSignificantDiffFromLine(line);
1400 // Find out if there is a following significant diff
1401 if (curDiff < pd->m_diffList.GetSize() - 1)
1403 nextDiff = pd->m_diffList.NextSignificantDiff(curDiff);
1409 // We don't have a selected difference,
1410 // but cursor can be inside inactive diff
1411 int line = GetCursorPos().y;
1412 if (!IsValidTextPosY(CPoint(0, line)))
1414 nextDiff = pd->m_diffList.NextSignificantDiffFromLine(line);
1417 int lastDiff = pd->m_diffList.LastSignificantDiff();
1418 if (nextDiff >= 0 && nextDiff <= lastDiff)
1419 SelectDiff(nextDiff, true, false);
1420 else if (CDirDoc *pDirDoc = pd->GetDirDoc())
1422 if (pDirDoc->MoveableToNextDiff())
1423 pDirDoc->MoveToNextDiff(pd);
1428 * @brief Update "Next diff" UI items
1430 void CMergeEditView::OnUpdateNextdiff(CCmdUI* pCmdUI)
1432 CMergeDoc *pd = GetDocument();
1433 const DIFFRANGE * dfi = pd->m_diffList.LastSignificantDiffRange();
1438 // There aren't any significant differences
1443 // Enable if the beginning of the last significant difference is after caret
1444 enabled = (GetCursorPos().y < (long)dfi->dbegin);
1447 if (!enabled && pd->GetDirDoc())
1448 enabled = pd->GetDirDoc()->MoveableToNextDiff();
1450 pCmdUI->Enable(enabled);
1454 * @brief Go to previous diff and select it.
1456 * Finds and selects previous difference. There are several cases:
1457 * - if there is selected difference, and that difference is visible
1458 * on screen, previous found difference is selected.
1459 * - if there is selected difference but it is not visible, previous
1460 * difference from cursor position is selected. This is what user
1461 * expects to happen and is natural thing to do. Also reduces
1462 * needless scrolling.
1463 * - if there is no selected difference, previous difference from cursor
1464 * position is selected.
1466 void CMergeEditView::OnPrevdiff()
1468 CMergeDoc *pd = GetDocument();
1469 int cnt = pd->m_ptBuf[0]->GetLineCount();
1473 // GetCurrentDiff() returns -1 if no diff selected
1475 int curDiff = pd->GetCurrentDiff();
1479 if (!IsDiffVisible(curDiff))
1481 // Selected difference not visible, select previous from cursor
1482 int line = GetCursorPos().y;
1483 // Make sure we aren't in the last line of the diff
1485 if (!IsValidTextPosY(CPoint(0, line)))
1487 prevDiff = pd->m_diffList.PrevSignificantDiffFromLine(line);
1491 // Find out if there is a preceding significant diff
1494 prevDiff = pd->m_diffList.PrevSignificantDiff(curDiff);
1500 // We don't have a selected difference,
1501 // but cursor can be inside inactive diff
1502 int line = GetCursorPos().y;
1503 if (!IsValidTextPosY(CPoint(0, line)))
1505 prevDiff = pd->m_diffList.PrevSignificantDiffFromLine(line);
1508 int firstDiff = pd->m_diffList.FirstSignificantDiff();
1509 if (prevDiff >= 0 && prevDiff >= firstDiff)
1510 SelectDiff(prevDiff, true, false);
1511 else if (CDirDoc *pDirDoc = pd->GetDirDoc())
1513 if (pDirDoc->MoveableToPrevDiff())
1514 pDirDoc->MoveToPrevDiff(pd);
1519 * @brief Update "Previous diff" UI items
1521 void CMergeEditView::OnUpdatePrevdiff(CCmdUI* pCmdUI)
1523 CMergeDoc *pd = GetDocument();
1524 const DIFFRANGE * dfi = pd->m_diffList.FirstSignificantDiffRange();
1529 // There aren't any significant differences
1534 // Enable if the end of the first significant difference is before caret
1535 enabled = (GetCursorPos().y > (long)dfi->dend);
1538 if (!enabled && pd->GetDirDoc())
1539 enabled = pd->GetDirDoc()->MoveableToPrevDiff();
1541 pCmdUI->Enable(enabled);
1544 void CMergeEditView::OnNextConflict()
1546 OnNext3wayDiff(THREEWAYDIFFTYPE_CONFLICT);
1550 * @brief Update "Next Conflict" UI items
1552 void CMergeEditView::OnUpdateNextConflict(CCmdUI* pCmdUI)
1554 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_CONFLICT);
1557 void CMergeEditView::OnPrevConflict()
1559 OnPrev3wayDiff(THREEWAYDIFFTYPE_CONFLICT);
1563 * @brief Update "Prev Conflict" UI items
1565 void CMergeEditView::OnUpdatePrevConflict(CCmdUI* pCmdUI)
1567 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_CONFLICT);
1571 * @brief Go to next 3-way diff and select it.
1573 void CMergeEditView::OnNext3wayDiff(int nDiffType)
1575 CMergeDoc *pd = GetDocument();
1576 int cnt = pd->m_ptBuf[0]->GetLineCount();
1580 // Returns -1 if no diff selected
1581 int curDiff = pd->GetCurrentDiff();
1585 int nextDiff = curDiff;
1586 if (!IsDiffVisible(curDiff))
1588 // Selected difference not visible, select next from cursor
1589 int line = GetCursorPos().y;
1590 // Make sure we aren't in the first line of the diff
1592 if (!IsValidTextPosY(CPoint(0, line)))
1594 nextDiff = pd->m_diffList.NextSignificant3wayDiffFromLine(line, nDiffType);
1598 // Find out if there is a following significant diff
1599 if (curDiff < pd->m_diffList.GetSize() - 1)
1601 nextDiff = pd->m_diffList.NextSignificant3wayDiff(curDiff, nDiffType);
1607 // nextDiff is the next one if there is one, else it is the one we're on
1608 SelectDiff(nextDiff, true, false);
1612 // We don't have a selected difference,
1613 // but cursor can be inside inactive diff
1614 int line = GetCursorPos().y;
1615 if (!IsValidTextPosY(CPoint(0, line)))
1617 curDiff = pd->m_diffList.NextSignificant3wayDiffFromLine(line, nDiffType);
1619 SelectDiff(curDiff, true, false);
1624 * @brief Update "Next 3-way diff" UI items
1626 void CMergeEditView::OnUpdateNext3wayDiff(CCmdUI* pCmdUI, int nDiffType)
1628 CMergeDoc *pd = GetDocument();
1630 if (pd->m_nBuffers < 3)
1632 pCmdUI->Enable(false);
1636 const DIFFRANGE * dfi = pd->m_diffList.LastSignificant3wayDiffRange(nDiffType);
1640 // There aren't any significant differences
1641 pCmdUI->Enable(false);
1645 // Enable if the beginning of the last significant difference is after caret
1646 CPoint pos = GetCursorPos();
1647 pCmdUI->Enable(pos.y < (long)dfi->dbegin);
1652 * @brief Go to previous 3-way diff and select it.
1654 void CMergeEditView::OnPrev3wayDiff(int nDiffType)
1656 CMergeDoc *pd = GetDocument();
1658 int cnt = pd->m_ptBuf[0]->GetLineCount();
1662 // GetCurrentDiff() returns -1 if no diff selected
1663 int curDiff = pd->GetCurrentDiff();
1667 int prevDiff = curDiff;
1668 if (!IsDiffVisible(curDiff))
1670 // Selected difference not visible, select previous from cursor
1671 int line = GetCursorPos().y;
1672 // Make sure we aren't in the last line of the diff
1674 if (!IsValidTextPosY(CPoint(0, line)))
1676 prevDiff = pd->m_diffList.PrevSignificant3wayDiffFromLine(line, nDiffType);
1680 // Find out if there is a preceding significant diff
1683 prevDiff = pd->m_diffList.PrevSignificant3wayDiff(curDiff, nDiffType);
1689 // prevDiff is the preceding one if there is one, else it is the one we're on
1690 SelectDiff(prevDiff, true, false);
1694 // We don't have a selected difference,
1695 // but cursor can be inside inactive diff
1696 int line = GetCursorPos().y;
1697 if (!IsValidTextPosY(CPoint(0, line)))
1699 curDiff = pd->m_diffList.PrevSignificant3wayDiffFromLine(line, nDiffType);
1701 SelectDiff(curDiff, true, false);
1706 * @brief Update "Previous diff X and Y" UI items
1708 void CMergeEditView::OnUpdatePrev3wayDiff(CCmdUI* pCmdUI, int nDiffType)
1710 CMergeDoc *pd = GetDocument();
1712 if (pd->m_nBuffers < 3)
1714 pCmdUI->Enable(false);
1718 const DIFFRANGE * dfi = pd->m_diffList.FirstSignificant3wayDiffRange(nDiffType);
1722 // There aren't any significant differences
1723 pCmdUI->Enable(false);
1727 // Enable if the end of the first significant difference is before caret
1728 CPoint pos = GetCursorPos();
1729 pCmdUI->Enable(pos.y > (long)dfi->dend);
1733 void CMergeEditView::OnNextdiffLM()
1735 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTMIDDLE);
1738 void CMergeEditView::OnUpdateNextdiffLM(CCmdUI* pCmdUI)
1740 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTMIDDLE);
1743 void CMergeEditView::OnNextdiffLR()
1745 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTRIGHT);
1748 void CMergeEditView::OnUpdateNextdiffLR(CCmdUI* pCmdUI)
1750 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTRIGHT);
1753 void CMergeEditView::OnNextdiffMR()
1755 OnNext3wayDiff(THREEWAYDIFFTYPE_MIDDLERIGHT);
1758 void CMergeEditView::OnUpdateNextdiffMR(CCmdUI* pCmdUI)
1760 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLERIGHT);
1763 void CMergeEditView::OnNextdiffLO()
1765 OnNext3wayDiff(THREEWAYDIFFTYPE_LEFTONLY);
1768 void CMergeEditView::OnUpdateNextdiffLO(CCmdUI* pCmdUI)
1770 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTONLY);
1773 void CMergeEditView::OnNextdiffMO()
1775 OnNext3wayDiff(THREEWAYDIFFTYPE_MIDDLEONLY);
1778 void CMergeEditView::OnUpdateNextdiffMO(CCmdUI* pCmdUI)
1780 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLEONLY);
1783 void CMergeEditView::OnNextdiffRO()
1785 OnNext3wayDiff(THREEWAYDIFFTYPE_RIGHTONLY);
1788 void CMergeEditView::OnUpdateNextdiffRO(CCmdUI* pCmdUI)
1790 OnUpdateNext3wayDiff(pCmdUI, THREEWAYDIFFTYPE_RIGHTONLY);
1793 void CMergeEditView::OnPrevdiffLM()
1795 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTMIDDLE);
1798 void CMergeEditView::OnUpdatePrevdiffLM(CCmdUI* pCmdUI)
1800 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTMIDDLE);
1803 void CMergeEditView::OnPrevdiffLR()
1805 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTRIGHT);
1808 void CMergeEditView::OnUpdatePrevdiffLR(CCmdUI* pCmdUI)
1810 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTRIGHT);
1813 void CMergeEditView::OnPrevdiffMR()
1815 OnPrev3wayDiff(THREEWAYDIFFTYPE_MIDDLERIGHT);
1818 void CMergeEditView::OnUpdatePrevdiffMR(CCmdUI* pCmdUI)
1820 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLERIGHT);
1823 void CMergeEditView::OnPrevdiffLO()
1825 OnPrev3wayDiff(THREEWAYDIFFTYPE_LEFTONLY);
1828 void CMergeEditView::OnUpdatePrevdiffLO(CCmdUI* pCmdUI)
1830 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_LEFTONLY);
1833 void CMergeEditView::OnPrevdiffMO()
1835 OnPrev3wayDiff(THREEWAYDIFFTYPE_MIDDLEONLY);
1838 void CMergeEditView::OnUpdatePrevdiffMO(CCmdUI* pCmdUI)
1840 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_MIDDLEONLY);
1843 void CMergeEditView::OnPrevdiffRO()
1845 OnPrev3wayDiff(THREEWAYDIFFTYPE_RIGHTONLY);
1848 void CMergeEditView::OnUpdatePrevdiffRO(CCmdUI* pCmdUI)
1850 OnUpdatePrev3wayDiff(pCmdUI, THREEWAYDIFFTYPE_RIGHTONLY);
1854 * @brief Clear selection
1856 void CMergeEditView::SelectNone()
1858 SetSelection (GetCursorPos(), GetCursorPos());
1863 * @brief Check if line is inside currently selected diff
1864 * @param [in] nLine 0-based linenumber in view
1865 * @sa CMergeDoc::GetCurrentDiff()
1866 * @sa CMergeDoc::LineInDiff()
1868 bool CMergeEditView::IsLineInCurrentDiff(int nLine) const
1870 // Check validity of nLine
1873 _RPTF1(_CRT_ERROR, "Linenumber is negative (%d)!", nLine);
1874 int nLineCount = LocateTextBuffer()->GetLineCount();
1875 if (nLine >= nLineCount)
1876 _RPTF2(_CRT_ERROR, "Linenumber > linecount (%d>%d)!", nLine, nLineCount);
1879 const CMergeDoc *pd = GetDocument();
1880 int curDiff = pd->GetCurrentDiff();
1883 return pd->m_diffList.LineInDiff(nLine, curDiff);
1887 * @brief Called when mouse left-button double-clicked
1889 * Double-clicking mouse inside diff selects that diff
1891 void CMergeEditView::OnLButtonDblClk(UINT nFlags, CPoint point)
1893 CMergeDoc *pd = GetDocument();
1894 CPoint pos = GetCursorPos();
1896 int diff = pd->m_diffList.LineToDiff(pos.y);
1897 if (diff != -1 && pd->m_diffList.IsDiffSignificant(diff))
1898 SelectDiff(diff, false, false);
1900 CCrystalEditViewEx::OnLButtonDblClk(nFlags, point);
1904 * @brief Called when mouse left button is released.
1906 * If button is released outside diffs, current diff
1909 void CMergeEditView::OnLButtonUp(UINT nFlags, CPoint point)
1911 CCrystalEditViewEx::OnLButtonUp(nFlags, point);
1912 DeselectDiffIfCursorNotInCurrentDiff();
1916 * @brief Called when mouse right button is pressed.
1918 * If right button is pressed outside diffs, current diff
1921 void CMergeEditView::OnRButtonDown(UINT nFlags, CPoint point)
1923 CCrystalEditViewEx::OnRButtonDown(nFlags, point);
1924 DeselectDiffIfCursorNotInCurrentDiff();
1927 void CMergeEditView::OnX2Y(int srcPane, int dstPane, bool selectedLineOnly)
1929 // Check that right side is not readonly
1930 if (IsReadOnly(dstPane))
1933 CMergeDoc *pDoc = GetDocument();
1934 int currentDiff = pDoc->GetCurrentDiff();
1936 if (currentDiff == -1)
1939 // If cursor is inside diff get number of that diff
1940 if (m_bCurrentLineIsDiff)
1942 CPoint pt = GetCursorPos();
1943 currentDiff = pDoc->m_diffList.LineToDiff(pt.y);
1947 auto [ptStart, ptEnd] = GetSelection();
1948 if (IsSelection() || pDoc->EqualCurrentWordDiff(srcPane, ptStart, ptEnd))
1950 if (!m_bRectangularSelection)
1952 if (selectedLineOnly)
1954 int firstDiff, lastDiff;
1955 GetSelectedDiffs(firstDiff, lastDiff);
1956 if (firstDiff != -1 && lastDiff != -1)
1958 CWaitCursor waitstatus;
1959 pDoc->CopyMultiplePartialList(srcPane, dstPane, firstDiff, lastDiff, ptStart.y, ptEnd.y);
1964 int firstDiff, lastDiff, firstWordDiff, lastWordDiff;
1965 GetFullySelectedDiffs(firstDiff, lastDiff, firstWordDiff, lastWordDiff);
1966 if (firstDiff != -1 && lastDiff != -1)
1968 CWaitCursor waitstatus;
1970 // Setting CopyFullLine (OPT_COPY_FULL_LINE)
1971 // restore old copy behaviour (always copy "full line" instead of "selected text only"), with a hidden option
1972 if (GetOptionsMgr()->GetBool(OPT_COPY_FULL_LINE))
1974 // old behaviour: copy full line
1975 pDoc->CopyMultipleList(srcPane, dstPane, firstDiff, lastDiff);
1979 // new behaviour: copy selected text only
1980 pDoc->CopyMultipleList(srcPane, dstPane, firstDiff, lastDiff, firstWordDiff, lastWordDiff);
1987 CWaitCursor waitstatus;
1988 auto wordDiffs = GetColumnSelectedWordDiffIndice();
1990 std::for_each(wordDiffs.rbegin(), wordDiffs.rend(), [&](auto& it) {
1991 pDoc->WordListCopy(srcPane, dstPane, it.first, it.second[0], it.second[it.second.size() - 1], &it.second, i != 0, i == 0);
1996 else if (currentDiff != -1 && pDoc->m_diffList.IsDiffSignificant(currentDiff))
1998 if (selectedLineOnly)
2000 CWaitCursor waitstatus;
2001 pDoc->PartialListCopy(srcPane, dstPane, currentDiff, ptStart.y, ptEnd.y);
2005 CWaitCursor waitstatus;
2006 pDoc->ListCopy(srcPane, dstPane, currentDiff);
2011 void CMergeEditView::OnUpdateX2Y(int dstPane, CCmdUI* pCmdUI)
2013 // Check that right side is not readonly
2014 if (!IsReadOnly(dstPane))
2016 // If one or more diffs inside selection OR
2017 // there is an active diff OR
2018 // cursor is inside diff
2019 auto [ptStart, ptEnd] = GetSelection();
2020 if (IsSelection() || GetDocument()->EqualCurrentWordDiff(m_nThisPane, ptStart, ptEnd))
2022 if (m_bCurrentLineIsDiff || (m_pTextBuffer->GetLineFlags(m_ptSelStart.y) & LF_NONTRIVIAL_DIFF) != 0)
2024 pCmdUI->Enable(true);
2028 int firstDiff, lastDiff;
2029 GetFullySelectedDiffs(firstDiff, lastDiff);
2031 pCmdUI->Enable(firstDiff != -1 && lastDiff != -1 && (lastDiff >= firstDiff));
2036 const int currDiff = GetDocument()->GetCurrentDiff();
2037 pCmdUI->Enable(m_bCurrentLineIsDiff || (currDiff != -1 && GetDocument()->m_diffList.IsDiffSignificant(currDiff)));
2041 pCmdUI->Enable(false);
2045 * @brief Copy diff from left pane to right pane
2047 * Difference is copied from left to right when
2048 * - difference is selected
2049 * - difference is inside selection (allows merging multiple differences).
2050 * - cursor is inside diff
2052 * If there is selected diff outside selection, we copy selected
2055 void CMergeEditView::OnL2r()
2057 int dstPane = (m_nThisPane < GetDocument()->m_nBuffers - 1) ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
2058 int srcPane = dstPane - 1;
2059 OnX2Y(srcPane, dstPane);
2063 * @brief Called when "Copy to left" item is updated
2065 void CMergeEditView::OnUpdateL2r(CCmdUI* pCmdUI)
2067 OnUpdateX2Y(m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1, pCmdUI);
2070 void CMergeEditView::OnLinesL2r()
2072 int dstPane = (m_nThisPane < GetDocument()->m_nBuffers - 1) ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
2073 int srcPane = dstPane - 1;
2074 OnX2Y(srcPane, dstPane, true);
2077 void CMergeEditView::OnUpdateLinesL2r(CCmdUI* pCmdUI)
2079 OnUpdateX2Y(m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1, pCmdUI);
2083 * @brief Copy diff from right pane to left pane
2085 * Difference is copied from left to right when
2086 * - difference is selected
2087 * - difference is inside selection (allows merging multiple differences).
2088 * - cursor is inside diff
2090 * If there is selected diff outside selection, we copy selected
2093 void CMergeEditView::OnR2l()
2095 int dstPane = (m_nThisPane > 0) ? m_nThisPane - 1 : 0;
2096 int srcPane = dstPane + 1;
2097 OnX2Y(srcPane, dstPane);
2101 * @brief Called when "Copy to right" item is updated
2103 void CMergeEditView::OnUpdateR2l(CCmdUI* pCmdUI)
2105 OnUpdateX2Y(m_nThisPane > 0 ? m_nThisPane - 1 : 0, pCmdUI);
2108 void CMergeEditView::OnLinesR2l()
2110 int dstPane = (m_nThisPane > 0) ? m_nThisPane - 1 : 0;
2111 int srcPane = dstPane + 1;
2112 OnX2Y(srcPane, dstPane, true);
2115 void CMergeEditView::OnUpdateLinesR2l(CCmdUI* pCmdUI)
2117 OnUpdateX2Y(m_nThisPane > 0 ? m_nThisPane - 1 : 0, pCmdUI);
2120 void CMergeEditView::OnCopyFromLeft()
2122 int dstPane = m_nThisPane;
2123 int srcPane = dstPane - 1;
2126 OnX2Y(srcPane, dstPane);
2129 void CMergeEditView::OnUpdateCopyFromLeft(CCmdUI* pCmdUI)
2131 int dstPane = m_nThisPane;
2132 int srcPane = dstPane - 1;
2134 pCmdUI->Enable(false);
2136 OnUpdateX2Y(dstPane, pCmdUI);
2139 void CMergeEditView::OnCopyLinesFromLeft()
2141 int dstPane = m_nThisPane;
2142 int srcPane = dstPane - 1;
2145 OnX2Y(srcPane, dstPane, true);
2148 void CMergeEditView::OnUpdateCopyLinesFromLeft(CCmdUI* pCmdUI)
2150 int dstPane = m_nThisPane;
2151 int srcPane = dstPane - 1;
2153 pCmdUI->Enable(false);
2155 OnUpdateX2Y(dstPane, pCmdUI);
2158 void CMergeEditView::OnCopyFromRight()
2160 int dstPane = m_nThisPane;
2161 int srcPane = dstPane + 1;
2162 if (srcPane >= GetDocument()->m_nBuffers)
2164 OnX2Y(srcPane, dstPane);
2167 void CMergeEditView::OnUpdateCopyFromRight(CCmdUI* pCmdUI)
2169 int dstPane = m_nThisPane;
2170 int srcPane = dstPane + 1;
2171 if (srcPane >= GetDocument()->m_nBuffers)
2172 pCmdUI->Enable(false);
2174 OnUpdateX2Y(dstPane, pCmdUI);
2177 void CMergeEditView::OnCopyLinesFromRight()
2179 int dstPane = m_nThisPane;
2180 int srcPane = dstPane + 1;
2181 if (srcPane >= GetDocument()->m_nBuffers)
2183 OnX2Y(srcPane, dstPane, true);
2186 void CMergeEditView::OnUpdateCopyLinesFromRight(CCmdUI* pCmdUI)
2188 int dstPane = m_nThisPane;
2189 int srcPane = dstPane + 1;
2190 if (srcPane >= GetDocument()->m_nBuffers)
2191 pCmdUI->Enable(false);
2193 OnUpdateX2Y(dstPane, pCmdUI);
2197 * @brief Copy all diffs from right pane to left pane
2199 void CMergeEditView::OnAllLeft()
2201 // Check that left side is not readonly
2202 int srcPane = m_nThisPane > 0 ? m_nThisPane : 1;
2203 int dstPane = m_nThisPane > 0 ? m_nThisPane - 1 : 0;
2204 if (IsReadOnly(dstPane))
2206 CWaitCursor waitstatus;
2208 GetDocument()->CopyAllList(srcPane, dstPane);
2212 * @brief Called when "Copy all to left" item is updated
2214 void CMergeEditView::OnUpdateAllLeft(CCmdUI* pCmdUI)
2216 // Check that left side is not readonly
2217 int dstPane = m_nThisPane > 0 ? m_nThisPane - 1 : 0;
2218 if (!IsReadOnly(dstPane))
2219 pCmdUI->Enable(GetDocument()->m_diffList.HasSignificantDiffs());
2221 pCmdUI->Enable(false);
2225 * @brief Copy all diffs from left pane to right pane
2227 void CMergeEditView::OnAllRight()
2229 // Check that right side is not readonly
2230 int srcPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane : m_nThisPane - 1;
2231 int dstPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
2232 if (IsReadOnly(dstPane))
2235 CWaitCursor waitstatus;
2237 GetDocument()->CopyAllList(srcPane, dstPane);
2241 * @brief Called when "Copy all to right" item is updated
2243 void CMergeEditView::OnUpdateAllRight(CCmdUI* pCmdUI)
2245 // Check that right side is not readonly
2246 int dstPane = m_nThisPane < GetDocument()->m_nBuffers - 1 ? m_nThisPane + 1 : GetDocument()->m_nBuffers - 1;
2247 if (!IsReadOnly(dstPane))
2248 pCmdUI->Enable(GetDocument()->m_diffList.HasSignificantDiffs());
2250 pCmdUI->Enable(false);
2254 * @brief Do Auto merge
2256 void CMergeEditView::OnAutoMerge()
2258 // Check current pane is not readonly
2259 if (GetDocument()->IsModified() || GetDocument()->GetAutoMerged() || !QueryEditable())
2262 CWaitCursor waitstatus;
2264 GetDocument()->DoAutoMerge(m_nThisPane);
2268 * @brief Called when "Auto Merge" item is updated
2270 void CMergeEditView::OnUpdateAutoMerge(CCmdUI* pCmdUI)
2272 pCmdUI->Enable(GetDocument()->m_nBuffers == 3 &&
2273 !GetDocument()->IsModified() &&
2274 !GetDocument()->GetAutoMerged() &&
2279 * @brief Add synchronization point
2281 void CMergeEditView::OnAddSyncPoint()
2283 GetDocument()->AddSyncPoint();
2287 * @brief Clear synchronization points
2289 void CMergeEditView::OnClearSyncPoints()
2291 GetDocument()->ClearSyncPoints();
2295 * @brief Called when "Clear Synchronization Points" item is updated
2297 void CMergeEditView::OnUpdateClearSyncPoints(CCmdUI* pCmdUI)
2299 pCmdUI->Enable(GetDocument()->HasSyncPoints());
2303 * @brief This function is called before other edit events.
2304 * @param [in] nAction Edit operation to do
2305 * @param [in] pszText Text to insert, delete etc
2306 * @sa CCrystalEditView::OnEditOperation()
2307 * @todo More edit-events for rescan delaying?
2309 void CMergeEditView::OnEditOperation(int nAction, LPCTSTR pszText, size_t cchText)
2311 if (!QueryEditable())
2313 // We must not arrive here, and assert helps detect troubles
2318 CMergeDoc* pDoc = GetDocument();
2319 pDoc->SetEditedAfterRescan(m_nThisPane);
2321 // simple hook for multiplex undo operations
2322 // deleted by jtuc 2003-06-28
2323 // now AddUndoRecords does it (so we don't create entry for OnEditOperation with no Undo data in m_pTextBuffer)
2324 /*if(dynamic_cast<CMergeDoc::CDiffTextBuffer*>(m_pTextBuffer)->curUndoGroup())
2326 pDoc->undoTgt.erase(pDoc->curUndo, pDoc->undoTgt.end());
2327 pDoc->undoTgt.push_back(this);
2328 pDoc->curUndo = pDoc->undoTgt.end();
2331 // perform original function
2332 CCrystalEditViewEx::OnEditOperation(nAction, pszText, cchText);
2334 // augment with additional operations
2336 // Change header to inform about changed doc
2337 pDoc->UpdateHeaderPath(m_nThisPane);
2339 // If automatic rescan enabled, rescan after edit events
2340 if (m_bAutomaticRescan)
2342 // keep document up to date
2343 // (Re)start timer to rescan only when user edits text
2344 // If timer starting fails, rescan immediately
2345 if (nAction == CE_ACTION_TYPING ||
2346 nAction == CE_ACTION_REPLACE ||
2347 nAction == CE_ACTION_BACKSPACE ||
2348 nAction == CE_ACTION_INDENT ||
2349 nAction == CE_ACTION_PASTE ||
2350 nAction == CE_ACTION_DELSEL ||
2351 nAction == CE_ACTION_DELETE ||
2352 nAction == CE_ACTION_CUT)
2354 if (!SetTimer(IDT_RESCAN, RESCAN_TIMEOUT, nullptr))
2355 pDoc->FlushAndRescan();
2358 pDoc->FlushAndRescan();
2364 // Update other pane for sync line.
2365 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
2367 if (nPane == m_nThisPane)
2369 CCrystalEditView *pView = GetGroupView(nPane);
2370 if (pView != nullptr)
2371 pView->Invalidate();
2378 * @brief Redo last action
2380 void CMergeEditView::OnEditRedo()
2382 CWaitCursor waitstatus;
2383 CMergeDoc* pDoc = GetDocument();
2384 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo));
2387 if (!QueryEditable())
2390 GetParentFrame()->SetActiveView(this, true);
2391 if(CCrystalEditViewEx::DoEditRedo())
2394 pDoc->UpdateHeaderPath(m_nThisPane);
2395 pDoc->FlushAndRescan();
2400 tgt->SendMessage(WM_COMMAND, ID_EDIT_REDO);
2405 * @brief Called when "Redo" item is updated
2407 void CMergeEditView::OnUpdateEditRedo(CCmdUI* pCmdUI)
2409 CMergeDoc* pDoc = GetDocument();
2410 if (pDoc->curUndo!=pDoc->undoTgt.end())
2412 CMergeEditView *tgt = pDoc->GetView(m_nThisGroup, *(pDoc->curUndo));
2413 pCmdUI->Enable( !IsReadOnly(tgt->m_nThisPane));
2416 pCmdUI->Enable(false);
2419 void CMergeEditView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
2421 CCrystalEditViewEx::OnUpdate(pSender, lHint, pHint);
2425 * @brief Scrolls to current diff and/or selects diff text
2426 * @param [in] bScroll If true scroll diff to view
2427 * @param [in] bSelectText If true select diff text
2428 * @note If bScroll and bSelectText are false, this does nothing!
2429 * @todo This shouldn't be called when no diff is selected, so
2430 * somebody could try to ASSERT(nDiff > -1)...
2432 void CMergeEditView::ShowDiff(bool bScroll, bool bSelectText)
2434 CMergeDoc *pd = GetDocument();
2435 const int nDiff = pd->GetCurrentDiff();
2437 // Try to trap some errors
2438 if (nDiff >= pd->m_diffList.GetSize())
2439 _RPTF2(_CRT_ERROR, "Selected diff > diffcount (%d > %d)!",
2440 nDiff, pd->m_diffList.GetSize());
2442 if (nDiff >= 0 && nDiff < pd->m_diffList.GetSize())
2444 CPoint ptStart, ptEnd;
2446 pd->m_diffList.GetDiff(nDiff, curDiff);
2449 ptStart.y = curDiff.dbegin;
2451 ptEnd.y = curDiff.dend;
2453 if (bScroll && !m_bDetailView)
2455 if (!IsDiffVisible(curDiff, CONTEXT_LINES_BELOW))
2457 // Difference is not visible, scroll it so that max amount of
2458 // scrolling is done while keeping the diff in screen. So if
2459 // scrolling is downwards, scroll the diff to as up in screen
2460 // as possible. This usually brings next diff to the screen
2461 // and we don't need to scroll into it.
2462 int nLine = GetSubLineIndex(ptStart.y);
2463 if (nLine > CONTEXT_LINES_ABOVE)
2465 nLine -= CONTEXT_LINES_ABOVE;
2467 GetGroupView(m_nThisPane)->ScrollToSubLine(nLine);
2468 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2470 if (nPane != m_nThisPane)
2471 GetGroupView(nPane)->ScrollToSubLine(nLine);
2475 vector<WordDiff> worddiffs;
2476 if (GetOptionsMgr()->GetBool(OPT_SCROLL_TO_FIRST_INLINE_DIFF))
2477 worddiffs = pd->GetWordDiffArrayInDiffBlock(nDiff);
2478 CPoint pt = worddiffs.size() > 0 ?
2479 CPoint{ worddiffs[0].begin[m_nThisPane], worddiffs[0].beginline[m_nThisPane] } :
2481 GetGroupView(m_nThisPane)->SetCursorPos(pt);
2482 GetGroupView(m_nThisPane)->SetAnchor(pt);
2483 GetGroupView(m_nThisPane)->SetSelection(pt, pt);
2484 GetGroupView(m_nThisPane)->EnsureVisible(pt);
2485 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2487 if (nPane != m_nThisPane)
2489 if (worddiffs.size() > 0)
2491 pt.x = worddiffs[0].begin[nPane];
2492 pt.y = worddiffs[0].beginline[nPane];
2494 GetGroupView(nPane)->SetCursorPos(pt);
2495 GetGroupView(nPane)->SetAnchor(pt);
2496 GetGroupView(nPane)->SetSelection(pt, pt);
2503 ptEnd.x = GetLineLength(ptEnd.y);
2504 SetSelection(ptStart, ptEnd);
2513 void CMergeEditView::OnTimer(UINT_PTR nIDEvent)
2515 // Maybe we want theApp::OnIdle to proceed before processing a timer message
2516 // ...but for this the queue must be empty
2517 // The timer message is a low priority message but the queue is maybe not yet empty
2518 // So we set a flag, wait for OnIdle to proceed, then come back here...
2519 // We come back here with a IDLE_TIMER OnTimer message (send with SendMessage
2520 // not with SetTimer so there is no delay)
2522 // IDT_RESCAN was posted because the app wanted to do a flushAndRescan with some delay
2524 // IDLE_TIMER is the false timer used to come back here after OnIdle
2525 // fTimerWaitingForIdle is a bool to store the commands waiting for idle
2526 // (one normal timer = one flag = one command)
2528 if (nIDEvent == IDT_RESCAN)
2530 KillTimer(IDT_RESCAN);
2531 fTimerWaitingForIdle |= FLAG_RESCAN_WAITS_FOR_IDLE;
2532 // notify the app to come back after OnIdle
2533 theApp.SetNeedIdleTimer();
2536 if (nIDEvent == IDLE_TIMER)
2538 // not a real timer, just come back after OnIdle
2539 // look to flags to know what to do
2540 if (fTimerWaitingForIdle & FLAG_RESCAN_WAITS_FOR_IDLE)
2541 GetDocument()->RescanIfNeeded(RESCAN_TIMEOUT/1000);
2542 fTimerWaitingForIdle = 0;
2545 CCrystalEditViewEx::OnTimer(nIDEvent);
2549 * @brief Returns if buffer is read-only
2550 * @note This has no any relation to file being read-only!
2552 bool CMergeEditView::IsReadOnly(int pane) const
2554 return m_bDetailView ? true : (GetDocument()->m_ptBuf[pane]->GetReadOnly() != false);
2558 * @brief Called when "Save left (as...)" item is updated
2560 void CMergeEditView::OnUpdateFileSaveLeft(CCmdUI* pCmdUI)
2562 CMergeDoc *pd = GetDocument();
2563 pCmdUI->Enable(!IsReadOnly(0) && pd->m_ptBuf[0]->IsModified());
2567 * @brief Called when "Save middle (as...)" item is updated
2569 void CMergeEditView::OnUpdateFileSaveMiddle(CCmdUI* pCmdUI)
2571 CMergeDoc *pd = GetDocument();
2572 pCmdUI->Enable(pd->m_nBuffers == 3 && !IsReadOnly(1) && pd->m_ptBuf[1]->IsModified());
2576 * @brief Called when "Save right (as...)" item is updated
2578 void CMergeEditView::OnUpdateFileSaveRight(CCmdUI* pCmdUI)
2580 CMergeDoc *pd = GetDocument();
2581 pCmdUI->Enable(!IsReadOnly(pd->m_nBuffers - 1) && pd->m_ptBuf[pd->m_nBuffers - 1]->IsModified());
2585 * @brief Refresh display using text-buffers
2586 * @note This DOES NOT reload files!
2588 void CMergeEditView::OnRefresh()
2590 CMergeDoc *pd = GetDocument();
2591 ASSERT(pd != nullptr);
2592 pd->FlushAndRescan(true);
2596 * @brief Handle some keys when in merging mode
2598 bool CMergeEditView::MergeModeKeyDown(MSG* pMsg)
2600 bool bHandled = false;
2602 // Allow default text selection when SHIFT pressed
2603 if (::GetAsyncKeyState(VK_SHIFT))
2606 // Allow default editor functions when CTRL pressed
2607 if (::GetAsyncKeyState(VK_CONTROL))
2610 // If we are in merging mode (merge with cursor keys)
2611 // handle some keys here
2612 switch (pMsg->wParam)
2639 * @brief Called before messages are translated.
2641 * Checks if ESC key was pressed, saves and closes doc.
2642 * Also if in merge mode traps cursor keys.
2644 BOOL CMergeEditView::PreTranslateMessage(MSG* pMsg)
2646 if (pMsg->message == WM_KEYDOWN)
2648 // If we are in merging mode (merge with cursor keys)
2649 // handle some keys here
2650 if (theApp.GetMergingMode())
2652 bool bHandled = MergeModeKeyDown(pMsg);
2657 // Close window if user has allowed it from options
2658 if (pMsg->wParam == VK_ESCAPE)
2660 int nCloseWithEsc = GetOptionsMgr()->GetInt(OPT_CLOSE_WITH_ESC);
2661 if (nCloseWithEsc != 0)
2662 GetParentFrame()->PostMessage(WM_CLOSE, 0, 0);
2667 return CCrystalEditViewEx::PreTranslateMessage(pMsg);
2671 * @brief Called when "Save" item is updated
2673 void CMergeEditView::OnUpdateFileSave(CCmdUI* pCmdUI)
2675 CMergeDoc *pd = GetDocument();
2677 bool bModified = false;
2678 for (int nPane = 0; nPane < pd->m_nBuffers; nPane++)
2680 if (pd->m_ptBuf[nPane]->IsModified())
2683 pCmdUI->Enable(bModified);
2687 * @brief Enable/disable left buffer read-only
2689 void CMergeEditView::OnLeftReadOnly()
2691 CMergeDoc *pd = GetDocument();
2692 bool bReadOnly = pd->m_ptBuf[0]->GetReadOnly();
2693 pd->m_ptBuf[0]->SetReadOnly(!bReadOnly);
2697 * @brief Called when "Left read-only" item is updated
2699 void CMergeEditView::OnUpdateLeftReadOnly(CCmdUI* pCmdUI)
2701 CMergeDoc *pd = GetDocument();
2702 bool bReadOnly = pd->m_ptBuf[0]->GetReadOnly();
2703 pCmdUI->Enable(true);
2704 pCmdUI->SetCheck(bReadOnly);
2708 * @brief Enable/disable middle buffer read-only
2710 void CMergeEditView::OnMiddleReadOnly()
2712 CMergeDoc *pd = GetDocument();
2713 if (pd->m_nBuffers == 3)
2715 bool bReadOnly = pd->m_ptBuf[1]->GetReadOnly();
2716 pd->m_ptBuf[1]->SetReadOnly(!bReadOnly);
2721 * @brief Called when "Middle read-only" item is updated
2723 void CMergeEditView::OnUpdateMiddleReadOnly(CCmdUI* pCmdUI)
2725 CMergeDoc *pd = GetDocument();
2726 if (pd->m_nBuffers < 3)
2728 pCmdUI->Enable(false);
2732 bool bReadOnly = pd->m_ptBuf[1]->GetReadOnly();
2733 pCmdUI->Enable(true);
2734 pCmdUI->SetCheck(bReadOnly);
2739 * @brief Enable/disable right buffer read-only
2741 void CMergeEditView::OnRightReadOnly()
2743 CMergeDoc *pd = GetDocument();
2744 bool bReadOnly = pd->m_ptBuf[pd->m_nBuffers - 1]->GetReadOnly();
2745 pd->m_ptBuf[pd->m_nBuffers - 1]->SetReadOnly(!bReadOnly);
2749 * @brief Called when "Left read-only" item is updated
2751 void CMergeEditView::OnUpdateRightReadOnly(CCmdUI* pCmdUI)
2753 CMergeDoc *pd = GetDocument();
2754 bool bReadOnly = pd->m_ptBuf[pd->m_nBuffers - 1]->GetReadOnly();
2755 pCmdUI->Enable(true);
2756 pCmdUI->SetCheck(bReadOnly);
2759 /// Store interface we use to display status line info
2760 void CMergeEditView::SetStatusInterface(IMergeEditStatus * piMergeEditStatus)
2762 ASSERT(m_piMergeEditStatus == nullptr);
2763 m_piMergeEditStatus = piMergeEditStatus;
2767 * @brief Update status bar contents.
2769 void CMergeEditView::UpdateStatusbar()
2775 * @brief Update statusbar info, Override from CCrystalTextView
2776 * @note we tab-expand column, but we don't tab-expand char count,
2777 * since we want to show how many chars there are and tab is just one
2778 * character although it expands to several spaces.
2780 void CMergeEditView::OnUpdateCaret()
2782 if (m_piMergeEditStatus == nullptr || !IsTextBufferInitialized())
2785 CPoint cursorPos = GetCursorPos();
2786 int nScreenLine = cursorPos.y;
2787 const int nRealLine = ComputeRealLine(nScreenLine);
2794 auto [selectedLines, selectedChars] = GetSelectedLineAndCharacterCount();
2795 DWORD dwLineFlags = 0;
2797 dwLineFlags = m_pTextBuffer->GetLineFlags(nScreenLine);
2798 // Is this a ghost line ?
2799 if (dwLineFlags & LF_GHOST)
2801 // Ghost lines display eg "Line 12-13"
2802 sLine.Format(_T("%d-%d"), nRealLine, nRealLine+1);
2803 sEol = _T("hidden");
2807 // Regular lines display eg "Line 13 Characters: 25 EOL: CRLF"
2808 sLine.Format(_T("%d"), nRealLine+1);
2809 curChar = cursorPos.x + 1;
2810 chars = GetLineLength(nScreenLine);
2811 column = CalculateActualOffset(nScreenLine, cursorPos.x, true) + 1;
2812 columns = CalculateActualOffset(nScreenLine, chars, true) + 1;
2814 if (GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
2815 GetDocument()->IsMixedEOL(m_nThisPane))
2817 sEol = GetTextBufferEol(nScreenLine);
2820 sEol = _T("hidden");
2822 m_piMergeEditStatus->SetLineInfo(sLine, column, columns,
2823 curChar, chars, selectedLines, selectedChars,
2824 sEol, GetDocument()->m_ptBuf[m_nThisPane]->getCodepage(), GetDocument()->m_ptBuf[m_nThisPane]->getHasBom());
2826 // Is cursor inside difference?
2827 if (dwLineFlags & LF_NONTRIVIAL_DIFF)
2828 m_bCurrentLineIsDiff = true;
2830 m_bCurrentLineIsDiff = false;
2832 CWnd* pWnd = GetFocus();
2833 if (!m_bDetailView || (pWnd && pWnd->m_hWnd == this->m_hWnd))
2834 UpdateLocationViewPosition(m_nTopSubLine, m_nTopSubLine + GetScreenLines());
2837 * @brief Select linedifference in the current line.
2839 * Select line difference in current line. Selection type
2840 * is choosed by highlight type.
2842 template<bool reversed>
2843 void CMergeEditView::OnSelectLineDiff()
2845 // Pass this to the document, to compare this file to other
2846 GetDocument()->Showlinediff(this, reversed);
2849 /// Enable select difference menuitem if current line is inside difference.
2850 void CMergeEditView::OnUpdateSelectLineDiff(CCmdUI* pCmdUI)
2852 pCmdUI->Enable(!GetDocument()->IsEditedAfterRescan());
2855 void CMergeEditView::OnAddToSubstitutionFilters()
2857 // Pass this to the document, to compare this file to other
2858 GetDocument()->AddToSubstitutionFilters(this, false);
2861 void CMergeEditView::OnUpdateAddToSubstitutionFilters(CCmdUI* pCmdUI)
2863 pCmdUI->Enable(GetDocument()->m_nBuffers == 2 && !GetDocument()->IsEditedAfterRescan());
2867 * @brief Enable/disable Replace-menuitem
2869 void CMergeEditView::OnUpdateEditReplace(CCmdUI* pCmdUI)
2871 CMergeDoc *pd = GetDocument();
2872 bool bReadOnly = pd->m_ptBuf[m_nThisPane]->GetReadOnly();
2874 pCmdUI->Enable(!bReadOnly);
2878 * @brief Update readonly statusbaritem
2880 void CMergeEditView::OnUpdateStatusRO(CCmdUI* pCmdUI)
2882 bool bRO = GetDocument()->m_ptBuf[pCmdUI->m_nID - ID_STATUS_PANE0FILE_RO]->GetReadOnly();
2883 pCmdUI->Enable(bRO);
2887 * @brief Create the dynamic submenu for scripts
2889 HMENU CMergeEditView::createScriptsSubmenu(HMENU hMenu)
2892 std::vector<String> functionNamesList = FileTransform::GetFreeFunctionsInScripts(L"EDITOR_SCRIPT");
2895 size_t i = GetMenuItemCount(hMenu);
2897 DeleteMenu(hMenu, 0, MF_BYPOSITION);
2899 if (functionNamesList.size() == 0)
2901 // no script : create a <empty> entry
2902 AppendMenu(hMenu, MF_STRING, ID_NO_EDIT_SCRIPTS, _("< Empty >").c_str());
2906 // or fill in the submenu with the scripts names
2907 int ID = ID_SCRIPT_FIRST; // first ID in menu
2908 for (i = 0 ; i < functionNamesList.size() ; i++, ID++)
2909 AppendMenu(hMenu, MF_STRING, ID, functionNamesList[i].c_str());
2911 functionNamesList.clear();
2914 if (!plugin::IsWindowsScriptThere())
2915 AppendMenu(hMenu, MF_STRING, ID_NO_SCT_SCRIPTS, _("WSH not found - .sct scripts disabled").c_str());
2921 * @brief Create the dynamic submenu for prediffers
2923 * @note The plugins are grouped in (suggested) and (not suggested)
2924 * The IDs follow the order of GetAvailableScripts
2926 * suggested 0 ID_1ST + 0
2927 * suggested 1 ID_1ST + 2
2928 * suggested 2 ID_1ST + 5
2929 * not suggested 0 ID_1ST + 1
2930 * not suggested 1 ID_1ST + 3
2931 * not suggested 2 ID_1ST + 4
2933 HMENU CMergeEditView::createPrediffersSubmenu(HMENU hMenu)
2936 int i = GetMenuItemCount(hMenu);
2938 DeleteMenu(hMenu, 0, MF_BYPOSITION);
2940 CMergeDoc *pd = GetDocument();
2941 ASSERT(pd != nullptr);
2944 AppendMenu(hMenu, MF_STRING, ID_NO_PREDIFFER, _("No prediffer (normal)").c_str());
2946 // get the scriptlet files
2947 PluginArray * piScriptArray =
2948 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
2949 PluginArray * piScriptArray2 =
2950 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
2952 // build the menu : first part, suggested plugins
2954 AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);
2955 AppendMenu(hMenu, MF_STRING, ID_SUGGESTED_PLUGINS, _("Suggested plugins").c_str());
2957 int ID = ID_PREDIFFERS_FIRST; // first ID in menu
2959 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
2961 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
2962 if (plugin->m_disabled || !plugin->TestAgainstRegList(pd->m_strBothFilenames))
2965 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2967 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
2969 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
2970 if (plugin->m_disabled || !plugin->TestAgainstRegList(pd->m_strBothFilenames))
2973 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2976 // build the menu : second part, others plugins
2978 AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);
2979 AppendMenu(hMenu, MF_STRING, ID_NOT_SUGGESTED_PLUGINS, _("Other plugins").c_str());
2981 ID = ID_PREDIFFERS_FIRST; // first ID in menu
2982 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
2984 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
2985 if (plugin->m_disabled || plugin->TestAgainstRegList(pd->m_strBothFilenames) != false)
2988 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2990 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
2992 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
2993 if (plugin->m_disabled || plugin->TestAgainstRegList(pd->m_strBothFilenames) != false)
2996 AppendMenu(hMenu, MF_STRING, ID, plugin->m_name.c_str());
2999 // compute the m_CurrentPredifferID (to set the radio button)
3000 PrediffingInfo prediffer;
3001 pd->GetPrediffer(&prediffer);
3003 if (prediffer.m_PluginOrPredifferMode != PLUGIN_MODE::PLUGIN_MANUAL)
3004 m_CurrentPredifferID = 0;
3005 else if (prediffer.m_PluginName.empty())
3006 m_CurrentPredifferID = ID_NO_PREDIFFER;
3009 ID = ID_PREDIFFERS_FIRST; // first ID in menu
3010 for (iScript = 0 ; iScript < piScriptArray->size() ; iScript++, ID ++)
3012 const PluginInfoPtr & plugin = piScriptArray->at(iScript);
3013 if (prediffer.m_PluginName == plugin->m_name)
3014 m_CurrentPredifferID = ID;
3017 for (iScript = 0 ; iScript < piScriptArray2->size() ; iScript++, ID ++)
3019 const PluginInfoPtr & plugin = piScriptArray2->at(iScript);
3020 if (prediffer.m_PluginName == plugin->m_name)
3021 m_CurrentPredifferID = ID;
3029 * @brief Offer a context menu built with scriptlet/ActiveX functions
3031 void CMergeEditView::OnContextMenu(CWnd* pWnd, CPoint point)
3033 // Create the menu and populate it with the available functions
3035 VERIFY(menu.LoadMenu(IDR_POPUP_MERGEVIEW));
3037 // Remove copying item copying from active side
3038 if (m_nThisPane == 0) // left?
3040 menu.RemoveMenu(ID_COPY_FROM_RIGHT, MF_BYCOMMAND);
3041 menu.RemoveMenu(ID_COPY_FROM_LEFT, MF_BYCOMMAND);
3042 menu.RemoveMenu(ID_COPY_LINES_FROM_RIGHT, MF_BYCOMMAND);
3043 menu.RemoveMenu(ID_COPY_LINES_FROM_LEFT, MF_BYCOMMAND);
3045 if (m_nThisPane == GetDocument()->m_nBuffers - 1)
3047 menu.RemoveMenu(ID_COPY_FROM_LEFT, MF_BYCOMMAND);
3048 menu.RemoveMenu(ID_COPY_FROM_RIGHT, MF_BYCOMMAND);
3049 menu.RemoveMenu(ID_COPY_LINES_FROM_RIGHT, MF_BYCOMMAND);
3050 menu.RemoveMenu(ID_COPY_LINES_FROM_LEFT, MF_BYCOMMAND);
3053 // Remove "Go to Moved Line Between Middle and Right" if in 2-way file comparison.
3054 // Remove "Go to Moved Line Between Middle and Right" if the right pane is active in 3-way file comparison.
3055 // Remove "Go to Moved Line Between Left and Middle" if the right pane is active in 3-way file comparison.
3056 int nBuffers = GetDocument()->m_nBuffers;
3057 if (nBuffers == 2 || (nBuffers == 3 && m_nThisPane == 0))
3058 menu.RemoveMenu(ID_GOTO_MOVED_LINE_MR, MF_BYCOMMAND);
3059 else if (nBuffers == 3 && m_nThisPane == 2)
3060 menu.RemoveMenu(ID_GOTO_MOVED_LINE_LM, MF_BYCOMMAND);
3062 VERIFY(menu.LoadToolbar(IDR_MAINFRAME));
3063 theApp.TranslateMenu(menu.m_hMenu);
3065 BCMenu *pSub = static_cast<BCMenu *>(menu.GetSubMenu(0));
3066 ASSERT(pSub != nullptr);
3068 // Context menu opened using keyboard has no coordinates
3069 if (point.x == -1 && point.y == -1)
3072 GetClientRect(rect);
3073 ClientToScreen(rect);
3075 point = rect.TopLeft();
3079 pSub->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
3080 point.x, point.y, AfxGetMainWnd());
3085 * @brief Update EOL mode in status bar
3087 void CMergeEditView::OnUpdateStatusEOL(CCmdUI* pCmdUI)
3089 GetGroupView(pCmdUI->m_nID - ID_STATUS_PANE0FILE_EOL)->OnUpdateIndicatorCRLF(pCmdUI);
3093 * @brief Change EOL mode and unify all the lines EOL to this new mode
3095 void CMergeEditView::OnConvertEolTo(UINT nID )
3097 CRLFSTYLE nStyle = CRLFSTYLE::AUTOMATIC;;
3101 nStyle = CRLFSTYLE::DOS;
3103 case ID_EOL_TO_UNIX:
3104 nStyle = CRLFSTYLE::UNIX;
3107 nStyle = CRLFSTYLE::MAC;
3111 _RPTF0(_CRT_ERROR, "Unhandled EOL type conversion!");
3114 m_pTextBuffer->SetCRLFMode(nStyle);
3116 // we don't need a derived applyEOLMode for ghost lines as they have no EOL char
3117 if (m_pTextBuffer->applyEOLMode())
3119 CMergeDoc *pd = GetDocument();
3120 ASSERT(pd != nullptr);
3121 pd->UpdateHeaderPath(m_nThisPane);
3122 pd->FlushAndRescan(true);
3127 * @brief allow convert to entries in file submenu
3129 void CMergeEditView::OnUpdateConvertEolTo(CCmdUI* pCmdUI)
3131 CRLFSTYLE nStyle = CRLFSTYLE::AUTOMATIC;
3132 switch (pCmdUI->m_nID)
3135 nStyle = CRLFSTYLE::DOS;
3137 case ID_EOL_TO_UNIX:
3138 nStyle = CRLFSTYLE::UNIX;
3141 nStyle = CRLFSTYLE::MAC;
3145 _RPTF0(_CRT_ERROR, "Missing menuitem handler for EOL convert menu!");
3149 if (GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
3150 GetDocument()->IsMixedEOL(m_nThisPane) ||
3151 nStyle != m_pTextBuffer->GetCRLFMode())
3153 pCmdUI->SetRadio(false);
3155 // Don't allow selecting other EOL style for protected pane
3156 if (!QueryEditable())
3157 pCmdUI->Enable(false);
3160 pCmdUI->SetRadio(true);
3164 * @brief Copy diff from left to right and advance to next diff
3166 void CMergeEditView::OnL2RNext()
3169 if (IsCursorInDiff()) // for 3-way file compare
3175 * @brief Update "Copy right and advance" UI item
3177 void CMergeEditView::OnUpdateL2RNext(CCmdUI* pCmdUI)
3179 OnUpdateL2r(pCmdUI);
3183 * @brief Copy diff from right to left and advance to next diff
3185 void CMergeEditView::OnR2LNext()
3188 if (IsCursorInDiff()) // for 3-way file compare
3194 * @brief Update "Copy left and advance" UI item
3196 void CMergeEditView::OnUpdateR2LNext(CCmdUI* pCmdUI)
3198 OnUpdateR2l(pCmdUI);
3202 * @brief Change active pane in MergeView.
3203 * Changes active pane and makes sure cursor position is kept in
3204 * screen. Currently we put cursor in same line than in original
3205 * active pane but we could be smarter too? Maybe update cursor
3206 * only when it is not visible in new pane?
3208 void CMergeEditView::OnChangePane()
3210 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3211 CMergeEditView *pWnd = static_cast<CMergeEditView*>(pSplitterWnd->GetActivePane());
3212 CMergeDoc *pDoc = GetDocument();
3213 bool bFound = false;
3214 CMergeEditView *pNextActiveView = nullptr;
3215 std::vector<CMergeEditView *> list = pDoc->GetViewList();
3216 list.insert(list.end(), list.begin(), list.end());
3217 for (auto& pView : list)
3219 if (bFound && pView->m_bDetailView == pWnd->m_bDetailView)
3221 pNextActiveView = pView;
3227 GetParentFrame()->SetActiveView(pNextActiveView);
3228 CPoint ptCursor = pWnd->GetCursorPos();
3230 if (ptCursor.y >= pNextActiveView->GetLineCount())
3231 ptCursor.y = pNextActiveView->GetLineCount() - 1;
3232 pNextActiveView->SetCursorPos(ptCursor);
3233 pNextActiveView->SetAnchor(ptCursor);
3234 pNextActiveView->SetSelection(ptCursor, ptCursor);
3238 * @brief Show "Go To" dialog and scroll views to line or diff.
3240 * Before dialog is opened, current line and file is determined
3242 * @note Conversions needed between apparent and real lines
3244 void CMergeEditView::OnWMGoto()
3247 CMergeDoc *pDoc = GetDocument();
3248 CPoint pos = GetCursorPos();
3252 nRealLine = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(pos.y);
3253 int nLineCount = pDoc->m_ptBuf[m_nThisPane]->GetLineCount();
3254 nLastLine = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(nLineCount - 1);
3256 // Set active file and current line selected in dialog
3257 dlg.m_strParam = strutils::to_str(nRealLine + 1);
3258 dlg.m_nFile = (pDoc->m_nBuffers < 3) ? (m_nThisPane == 1 ? 2 : 0) : m_nThisPane;
3259 dlg.m_nGotoWhat = 0;
3261 if (dlg.DoModal() == IDOK)
3263 CMergeDoc * pDoc1 = GetDocument();
3264 CMergeEditView * pCurrentView = nullptr;
3267 pCurrentView = GetGroupView(m_nThisPane);
3270 try { num = std::stoi(dlg.m_strParam) - 1; } catch(...) {}
3272 if (dlg.m_nGotoWhat == 0)
3274 int nRealLine1 = num;
3277 if (nRealLine1 > nLastLine)
3278 nRealLine1 = nLastLine;
3280 bool bShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
3281 GotoLine(nRealLine1, true, (pDoc1->m_nBuffers < 3) ? (dlg.m_nFile == 2 ? 1 : 0) : dlg.m_nFile, !bShift);
3288 if (diff >= pDoc1->m_diffList.GetSize())
3289 diff = pDoc1->m_diffList.GetSize();
3291 pCurrentView->SelectDiff(diff, true, false);
3297 * @brief Called when "Go to Moved Line Between Left and Middle" item is selected.
3298 * Go to moved line between the left and right panes when in 2-way file comparison.
3299 * Go to moved line between the left and middle panes when in 3-way file comparison.
3301 void CMergeEditView::OnGotoMovedLineLM()
3303 if (!GetOptionsMgr()->GetBool(OPT_CMP_MOVED_BLOCKS))
3306 CMergeDoc* pDoc = GetDocument();
3307 CPoint pos = GetCursorPos();
3309 ASSERT(m_nThisPane >= 0 && m_nThisPane < 3);
3310 ASSERT(pDoc != nullptr);
3311 ASSERT(pDoc->m_nBuffers == 2 || pDoc->m_nBuffers == 3);
3314 if (m_nThisPane == 0)
3316 int line = pDoc->RightLineInMovedBlock(m_nThisPane, pos.y);
3318 GotoLine(line, false, 1);
3320 else if (m_nThisPane == 1)
3322 int line = pDoc->LeftLineInMovedBlock(m_nThisPane, pos.y);
3324 GotoLine(line, false, 0);
3329 * @brief Called when "Go to Moved Line Between Left and Middle" item is updated.
3330 * @param [in] pCmdUI UI component to update.
3331 * @note The item label is changed to "Go to Moved Line" when 2-way file comparison.
3333 void CMergeEditView::OnUpdateGotoMovedLineLM(CCmdUI* pCmdUI)
3335 CMergeDoc* pDoc = GetDocument();
3336 CPoint pos = GetCursorPos();
3338 ASSERT(m_nThisPane >= 0 && m_nThisPane < 3);
3339 ASSERT(pCmdUI != nullptr);
3340 ASSERT(pDoc != nullptr);
3341 ASSERT(pDoc->m_nBuffers == 2 || pDoc->m_nBuffers == 3);
3344 if (pDoc->m_nBuffers == 2)
3345 pCmdUI->SetText(_("Go to Moved Line\tCtrl+Shift+G").c_str());
3347 if (!GetOptionsMgr()->GetBool(OPT_CMP_MOVED_BLOCKS) || m_nThisPane == 2)
3349 pCmdUI->Enable(false);
3353 if (m_nThisPane == 0)
3355 bool bOn = (pDoc->RightLineInMovedBlock(m_nThisPane, pos.y) >= 0);
3356 pCmdUI->Enable(bOn);
3358 else if (m_nThisPane == 1)
3360 bool bOn = (pDoc->LeftLineInMovedBlock(m_nThisPane, pos.y) >= 0);
3361 pCmdUI->Enable(bOn);
3366 * @brief Called when "Go to Moved Line Between Middle and Right" item is selected.
3367 * Go to moved line between the middle and right panes when in 3-way file comparison.
3369 void CMergeEditView::OnGotoMovedLineMR()
3371 if (!GetOptionsMgr()->GetBool(OPT_CMP_MOVED_BLOCKS))
3374 CMergeDoc* pDoc = GetDocument();
3375 CPoint pos = GetCursorPos();
3377 ASSERT(m_nThisPane >= 0 && m_nThisPane < 3);
3378 ASSERT(pDoc != nullptr);
3379 ASSERT(pDoc->m_nBuffers == 2 || pDoc->m_nBuffers == 3);
3382 if (m_nThisPane == 1)
3384 int line = pDoc->RightLineInMovedBlock(m_nThisPane, pos.y);
3386 GotoLine(line, false, 2);
3388 else if (m_nThisPane == 2)
3390 int line = pDoc->LeftLineInMovedBlock(m_nThisPane, pos.y);
3392 GotoLine(line, false, 1);
3397 * @brief Called when "Go to Moved Line Between Middle and Right" item is updated.
3398 * @param [in] pCmdUI UI component to update.
3400 void CMergeEditView::OnUpdateGotoMovedLineMR(CCmdUI* pCmdUI)
3402 CMergeDoc* pDoc = GetDocument();
3403 CPoint pos = GetCursorPos();
3405 ASSERT(m_nThisPane >= 0 && m_nThisPane < 3);
3406 ASSERT(pCmdUI != nullptr);
3407 ASSERT(pDoc != nullptr);
3408 ASSERT(pDoc->m_nBuffers == 2 || pDoc->m_nBuffers == 3);
3411 if (!GetOptionsMgr()->GetBool(OPT_CMP_MOVED_BLOCKS) || pDoc->m_nBuffers == 2 || m_nThisPane == 0)
3413 pCmdUI->Enable(false);
3417 if (m_nThisPane == 1)
3419 bool bOn = (pDoc->RightLineInMovedBlock(m_nThisPane, pos.y) >= 0);
3420 pCmdUI->Enable(bOn);
3422 else if (m_nThisPane == 2)
3424 bool bOn = (pDoc->LeftLineInMovedBlock(m_nThisPane, pos.y) >= 0);
3425 pCmdUI->Enable(bOn);
3429 void CMergeEditView::OnShellMenu()
3431 CFrameWnd *pFrame = GetTopLevelFrame();
3432 ASSERT(pFrame != nullptr);
3433 BOOL bAutoMenuEnableOld = pFrame->m_bAutoMenuEnable;
3434 pFrame->m_bAutoMenuEnable = FALSE;
3436 String path = GetDocument()->m_filePaths[m_nThisPane];
3437 std::unique_ptr<CShellContextMenu> pContextMenu(new CShellContextMenu(0x9000, 0x9FFF));
3438 pContextMenu->Initialize();
3439 pContextMenu->AddItem(paths::GetParentPath(path), paths::FindFileName(path));
3440 pContextMenu->RequeryShellContextMenu();
3442 ::GetCursorPos(&point);
3443 HWND hWnd = GetSafeHwnd();
3444 BOOL nCmd = TrackPopupMenu(pContextMenu->GetHMENU(), TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, 0, hWnd, nullptr);
3446 pContextMenu->InvokeCommand(nCmd, hWnd);
3447 pContextMenu->ReleaseShellContextMenu();
3449 pFrame->m_bAutoMenuEnable = bAutoMenuEnableOld;
3452 void CMergeEditView::OnUpdateShellMenu(CCmdUI* pCmdUI)
3454 pCmdUI->Enable(!GetDocument()->m_filePaths[m_nThisPane].empty());
3458 * @brief Reload options.
3460 void CMergeEditView::RefreshOptions()
3462 RENDERING_MODE nRenderingMode = static_cast<RENDERING_MODE>(GetOptionsMgr()->GetInt(OPT_RENDERING_MODE));
3463 SetRenderingMode(nRenderingMode);
3465 m_bAutomaticRescan = GetOptionsMgr()->GetBool(OPT_AUTOMATIC_RESCAN);
3467 if (GetOptionsMgr()->GetInt(OPT_TAB_TYPE) == 0)
3468 SetInsertTabs(true);
3470 SetInsertTabs(false);
3472 SetSelectionMargin(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
3474 if (!GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT))
3475 SetTextType(CrystalLineParser::SRC_PLAIN);
3476 else if (!m_bChangedSchemeManually)
3478 // The syntax highlighting scheme should only be applied if it has not been manually changed.
3479 String fileName = GetDocument()->m_filePaths[m_nThisPane];
3481 paths::SplitFilename(fileName, nullptr, nullptr, &sExt);
3482 CrystalLineParser::TextDefinition* def = CrystalLineParser::GetTextType(sExt.c_str());
3484 SetTextType(def->type);
3486 SetTextType(CrystalLineParser::SRC_PLAIN);
3489 SetWordWrapping(GetOptionsMgr()->GetBool(OPT_WORDWRAP));
3490 SetViewLineNumbers(GetOptionsMgr()->GetBool(OPT_VIEW_LINENUMBERS));
3492 SetViewTabs(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE));
3493 SetViewEols(GetOptionsMgr()->GetBool(OPT_VIEW_EOL),
3494 GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
3495 GetDocument()->IsMixedEOL(m_nThisPane));
3497 Options::DiffColors::Load(GetOptionsMgr(), m_cachedColors);
3500 void CMergeEditView::OnScripts(UINT nID )
3502 // text is CHAR if compiled without UNICODE, WCHAR with UNICODE
3503 String text = GetSelectedText();
3505 // transform the text with a script/ActiveX function, event=EDITOR_SCRIPT
3506 bool bChanged = FileTransform::Interactive(text, L"EDITOR_SCRIPT", nID - ID_SCRIPT_FIRST);
3508 // now replace the text
3509 ReplaceSelection(text.c_str(), text.length(), 0);
3513 * @brief Called when an editor script item is updated
3515 void CMergeEditView::OnUpdateNoEditScripts(CCmdUI* pCmdUI)
3517 // append the scripts submenu
3518 HMENU scriptsSubmenu = pCmdUI->m_pSubMenu ? pCmdUI->m_pSubMenu->m_hMenu : nullptr;
3519 if (scriptsSubmenu != nullptr)
3520 createScriptsSubmenu(scriptsSubmenu);
3522 pCmdUI->Enable(true);
3526 * @brief Called when an editor script item is updated
3528 void CMergeEditView::OnUpdatePrediffer(CCmdUI* pCmdUI)
3530 pCmdUI->Enable(true);
3532 CMergeDoc *pd = GetDocument();
3533 ASSERT(pd != nullptr);
3534 PrediffingInfo prediffer;
3535 pd->GetPrediffer(&prediffer);
3537 if (prediffer.m_PluginOrPredifferMode != PLUGIN_MODE::PLUGIN_MANUAL)
3539 pCmdUI->SetRadio(false);
3543 // Detect when CDiffWrapper::RunFileDiff has canceled a buggy prediffer
3544 if (prediffer.m_PluginName.empty())
3545 m_CurrentPredifferID = ID_NO_PREDIFFER;
3547 pCmdUI->SetRadio(pCmdUI->m_nID == static_cast<UINT>(m_CurrentPredifferID));
3551 * @brief Update "Prediffer" menuitem
3553 void CMergeEditView::OnUpdateNoPrediffer(CCmdUI* pCmdUI)
3555 // recreate the sub menu (to fill the "selected prediffers")
3556 GetMainFrame()->UpdatePrediffersMenu();
3560 void CMergeEditView::OnNoPrediffer()
3562 OnPrediffer(ID_NO_PREDIFFER);
3565 * @brief Handler for all prediffer choices, including ID_PREDIFF_MANUAL, ID_PREDIFF_AUTO, ID_NO_PREDIFFER, & specific prediffers
3567 void CMergeEditView::OnPrediffer(UINT nID )
3569 CMergeDoc *pd = GetDocument();
3570 ASSERT(pd != nullptr);
3572 SetPredifferByMenu(nID);
3573 pd->FlushAndRescan(true);
3577 * @brief Handler for all prediffer choices.
3578 * Prediffer choises include ID_PREDIFF_MANUAL, ID_PREDIFF_AUTO,
3579 * ID_NO_PREDIFFER, & specific prediffers.
3581 void CMergeEditView::SetPredifferByMenu(UINT nID )
3583 CMergeDoc *pd = GetDocument();
3584 ASSERT(pd != nullptr);
3586 if (nID == ID_NO_PREDIFFER)
3588 m_CurrentPredifferID = nID;
3589 // All flags are set correctly during the construction
3590 PrediffingInfo *infoPrediffer = new PrediffingInfo;
3591 infoPrediffer->m_PluginOrPredifferMode = PLUGIN_MODE::PLUGIN_MANUAL;
3592 infoPrediffer->m_PluginName.clear();
3593 pd->SetPrediffer(infoPrediffer);
3594 pd->FlushAndRescan(true);
3598 // get the scriptlet files
3599 PluginArray * piScriptArray =
3600 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
3601 PluginArray * piScriptArray2 =
3602 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
3604 // build a PrediffingInfo structure fom the ID
3605 PrediffingInfo prediffer;
3606 prediffer.m_PluginOrPredifferMode = PLUGIN_MODE::PLUGIN_MANUAL;
3608 size_t pluginNumber = nID - ID_PREDIFFERS_FIRST;
3609 if (pluginNumber < piScriptArray->size())
3611 const PluginInfoPtr & plugin = piScriptArray->at(pluginNumber);
3612 prediffer.m_PluginName = plugin->m_name;
3616 pluginNumber -= piScriptArray->size();
3617 if (pluginNumber >= piScriptArray2->size())
3619 const PluginInfoPtr & plugin = piScriptArray2->at(pluginNumber);
3620 prediffer.m_PluginName = plugin->m_name;
3623 // update data for the radio button
3624 m_CurrentPredifferID = nID;
3626 // update the prediffer and rescan
3627 pd->SetPrediffer(&prediffer);
3631 * @brief Look through available prediffers, and return ID of requested one, if found
3633 int CMergeEditView::FindPrediffer(LPCTSTR prediffer) const
3636 int ID = ID_PREDIFFERS_FIRST;
3638 // Search file prediffers
3639 PluginArray * piScriptArray =
3640 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"FILE_PREDIFF");
3641 for (i=0; i<piScriptArray->size(); ++i, ++ID)
3643 const PluginInfoPtr & plugin = piScriptArray->at(i);
3644 if (plugin->m_name == prediffer)
3648 // Search buffer prediffers
3649 PluginArray * piScriptArray2 =
3650 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(L"BUFFER_PREDIFF");
3651 for (i=0; i<piScriptArray2->size(); ++i, ++ID)
3653 const PluginInfoPtr & plugin = piScriptArray2->at(i);
3654 if (plugin->m_name == prediffer)
3662 * @brief Look through available prediffers, and return ID of requested one, if found
3664 bool CMergeEditView::SetPredifferByName(const CString & prediffer)
3666 int id = FindPrediffer(prediffer);
3667 if (id<0) return false;
3668 SetPredifferByMenu(id);
3673 * @brief Goto given line.
3674 * @param [in] nLine Destination linenumber
3675 * @param [in] bRealLine if true linenumber is real line, otherwise
3676 * it is apparent line (including deleted lines)
3677 * @param [in] pane Pane index of goto target pane (0 = left, 1 = right).
3678 * @param [in] bMoveAnchor if true the anchor is moved to nLine
3680 void CMergeEditView::GotoLine(UINT nLine, bool bRealLine, int pane, bool bMoveAnchor)
3682 CMergeDoc *pDoc = GetDocument();
3683 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3684 CMergeEditView *pCurrentView = nullptr;
3685 if (pSplitterWnd != nullptr)
3686 pCurrentView = static_cast<CMergeEditView*>
3687 (pSplitterWnd->GetActivePane());
3689 int nRealLine = nLine;
3690 int nApparentLine = nLine;
3692 // Compute apparent (shown linenumber) line
3695 if (nRealLine > pDoc->m_ptBuf[pane]->GetLineCount() - 1)
3696 nRealLine = pDoc->m_ptBuf[pane]->GetLineCount() - 1;
3698 nApparentLine = pDoc->m_ptBuf[pane]->ComputeApparentLine(nRealLine);
3702 ptPos.y = nApparentLine;
3704 // Scroll line to center of view
3705 int nScrollLine = GetSubLineIndex(nApparentLine);
3706 nScrollLine -= GetScreenLines() / 2;
3707 if (nScrollLine < 0)
3710 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
3712 int nGroup = m_bDetailView ? 0 : m_nThisGroup;
3713 CMergeEditView* pView = GetDocument()->GetView(nGroup, nPane);
3714 pView->ScrollToSubLine(nScrollLine);
3715 if (ptPos.y < pView->GetLineCount())
3717 pView->SetCursorPos(ptPos);
3719 pView->SetAnchor(ptPos);
3720 pView->SetSelection(pView->GetAnchor(), ptPos);
3724 CPoint ptPos1(0, pView->GetLineCount() - 1);
3725 pView->SetCursorPos(ptPos1);
3727 pView->SetAnchor(ptPos1);
3728 pView->SetSelection(pView->GetAnchor(), ptPos1);
3732 // If goto target is another view - activate another view.
3733 // This is done for user convenience as user probably wants to
3734 // work with goto target file.
3736 GetDocument()->GetView(0, pane)->SetActivePane();
3737 else if (GetGroupView(pane) != pCurrentView)
3738 GetGroupView(pane)->SetActivePane();
3742 * @brief Check for horizontal scroll. Re-route to CSplitterEx if not from
3745 void CMergeEditView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
3747 if (pScrollBar == nullptr)
3749 // Scroll did not come frome a scroll bar
3750 // Find the appropriate scroll bar
3751 // and send the message to the splitter window instead
3752 // The event should eventually come back here but with a valid scrollbar
3753 // Along the way it will be propagated to other windows that need it
3754 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3755 CScrollBar* curBar = this->GetScrollBarCtrl(SB_HORZ);
3756 pSplitterWnd->SendMessage(WM_HSCROLL,
3757 MAKELONG(nSBCode, nPos), (LPARAM)curBar->m_hWnd);
3760 CCrystalTextView::OnHScroll (nSBCode, nPos, pScrollBar);
3764 * @brief When view is scrolled using scrollbars update location pane.
3766 void CMergeEditView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
3768 if (pScrollBar == nullptr)
3770 // Scroll did not come frome a scroll bar
3771 // Find the appropriate scroll bar
3772 // and send the message to the splitter window instead
3773 // The event should eventually come back here but with a valid scrollbar
3774 // Along the way it will be propagated to other windows that need it
3775 CSplitterWnd *pSplitterWnd = GetParentSplitter(this, false);
3776 CScrollBar* curBar = this->GetScrollBarCtrl(SB_VERT);
3777 pSplitterWnd->SendMessage(WM_VSCROLL,
3778 MAKELONG(nSBCode, nPos), (LPARAM)curBar->m_hWnd);
3781 CCrystalTextView::OnVScroll (nSBCode, nPos, pScrollBar);
3783 if (nSBCode == SB_ENDSCROLL)
3786 // Note we cannot use nPos because of its 16-bit nature
3787 SCROLLINFO si = {0};
3788 si.cbSize = sizeof (si);
3789 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
3790 VERIFY (GetScrollInfo (SB_VERT, &si));
3792 // Get the current position of scroll box.
3793 int nCurPos = si.nPos;
3795 UpdateLocationViewPosition(nCurPos, nCurPos + GetScreenLines());
3799 * @brief Copy selected lines adding linenumbers.
3801 void CMergeEditView::OnEditCopyLineNumbers()
3807 CMergeDoc *pDoc = GetDocument();
3808 auto [ptStart, ptEnd] = GetSelection();
3810 // Get last selected line (having widest linenumber)
3811 int line = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(ptEnd.y);
3812 size_t nNumWidth = strutils::to_str(line + 1).length();
3814 for (int i = ptStart.y; i <= ptEnd.y; i++)
3816 if (GetLineFlags(i) & LF_GHOST || (GetEnableHideLines() && (GetLineFlags(i) & LF_INVISIBLE)))
3819 // We need to convert to real linenumbers
3820 line = pDoc->m_ptBuf[m_nThisPane]->ComputeRealLine(i);
3822 // Insert spaces to align different width linenumbers (99, 100)
3823 strLine = GetLineText(i);
3824 CString sSpaces(' ', static_cast<int>(nNumWidth - strutils::to_str(line + 1).length()));
3827 strNumLine.Format(_T("%d: %s"), line + 1, (LPCTSTR)strLine);
3828 strText += strNumLine;
3830 PutToClipboard(strText, strText.GetLength(), m_bRectangularSelection);
3833 void CMergeEditView::OnUpdateEditCopyLinenumbers(CCmdUI* pCmdUI)
3835 CCrystalEditViewEx::OnUpdateEditCopy(pCmdUI);
3839 * @brief Open active file with associated application.
3841 * First tries to open file using shell 'Edit' action, since that
3842 * action open scripts etc. to editor instead of running them. If
3843 * edit-action is not registered, 'Open' action is used.
3845 void CMergeEditView::OnOpenFile()
3847 CMergeDoc * pDoc = GetDocument();
3848 ASSERT(pDoc != nullptr);
3850 String sFileName = pDoc->m_filePaths[m_nThisPane];
3851 if (sFileName.empty())
3853 HINSTANCE rtn = ShellExecute(::GetDesktopWindow(), _T("edit"), sFileName.c_str(),
3854 0, 0, SW_SHOWNORMAL);
3855 if (reinterpret_cast<uintptr_t>(rtn) == SE_ERR_NOASSOC)
3856 rtn = ShellExecute(::GetDesktopWindow(), _T("open"), sFileName.c_str(),
3857 0, 0, SW_SHOWNORMAL);
3858 if (reinterpret_cast<uintptr_t>(rtn) == SE_ERR_NOASSOC)
3863 * @brief Open active file with app selection dialog
3865 void CMergeEditView::OnOpenFileWith()
3867 CMergeDoc * pDoc = GetDocument();
3868 ASSERT(pDoc != nullptr);
3870 String sFileName = pDoc->m_filePaths[m_nThisPane];
3871 if (sFileName.empty())
3875 if (!GetSystemDirectory(sysdir.GetBuffer(MAX_PATH), MAX_PATH))
3877 sysdir.ReleaseBuffer();
3878 CString arg = (CString)_T("shell32.dll,OpenAs_RunDLL ") + sFileName.c_str();
3879 ShellExecute(::GetDesktopWindow(), 0, _T("RUNDLL32.EXE"), arg,
3880 sysdir, SW_SHOWNORMAL);
3884 * @brief Open active file with external editor
3886 void CMergeEditView::OnOpenFileWithEditor()
3888 CMergeDoc * pDoc = GetDocument();
3889 ASSERT(pDoc != nullptr);
3891 String sFileName = pDoc->m_filePaths[m_nThisPane];
3892 if (sFileName.empty())
3895 int nRealLine = ComputeRealLine(GetCursorPos().y) + 1;
3896 theApp.OpenFileToExternalEditor(sFileName, nRealLine);
3900 * @brief Force repaint of the location pane.
3902 void CMergeEditView::RepaintLocationPane()
3904 // Must force recalculation due to caching of data in location pane.
3905 CLocationView *pLocationView = GetDocument()->GetLocationView();
3906 if (pLocationView != nullptr)
3907 pLocationView->ForceRecalculate();
3911 * @brief Enables/disables linediff (different color for diffs)
3913 void CMergeEditView::OnViewLineDiffs()
3915 bool bWordDiffHighlight = GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT);
3916 GetOptionsMgr()->SaveOption(OPT_WORDDIFF_HIGHLIGHT, !bWordDiffHighlight);
3918 // Call CMergeDoc RefreshOptions() to refresh *both* views
3919 CMergeDoc *pDoc = GetDocument();
3920 pDoc->RefreshOptions();
3921 pDoc->FlushAndRescan(true);
3924 void CMergeEditView::OnUpdateViewLineDiffs(CCmdUI* pCmdUI)
3926 pCmdUI->Enable(true);
3927 pCmdUI->SetCheck(GetOptionsMgr()->GetBool(OPT_WORDDIFF_HIGHLIGHT));
3931 * @brief Enables/disables line number
3933 void CMergeEditView::OnViewLineNumbers()
3935 GetOptionsMgr()->SaveOption(OPT_VIEW_LINENUMBERS, !GetViewLineNumbers());
3937 // Call CMergeDoc RefreshOptions() to refresh *both* views
3938 CMergeDoc *pDoc = GetDocument();
3939 pDoc->RefreshOptions();
3942 void CMergeEditView::OnUpdateViewLineNumbers(CCmdUI* pCmdUI)
3944 pCmdUI->Enable(true);
3945 pCmdUI->SetCheck(GetViewLineNumbers());
3949 * @brief Enables/disables word wrap
3951 void CMergeEditView::OnViewWordWrap()
3953 GetOptionsMgr()->SaveOption(OPT_WORDWRAP, !m_bWordWrap);
3955 // Call CMergeDoc RefreshOptions() to refresh *both* views
3956 CMergeDoc *pDoc = GetDocument();
3957 pDoc->RefreshOptions();
3958 pDoc->UpdateAllViews(this);
3963 void CMergeEditView::OnUpdateViewWordWrap(CCmdUI* pCmdUI)
3965 pCmdUI->Enable(true);
3966 pCmdUI->SetCheck(m_bWordWrap);
3969 void CMergeEditView::OnViewWhitespace()
3971 GetOptionsMgr()->SaveOption(OPT_VIEW_WHITESPACE, !GetViewTabs());
3973 // Call CMergeDoc RefreshOptions() to refresh *both* views
3974 CMergeDoc *pDoc = GetDocument();
3975 pDoc->RefreshOptions();
3978 void CMergeEditView::OnUpdateViewWhitespace(CCmdUI* pCmdUI)
3980 pCmdUI->SetCheck(GetViewTabs());
3983 void CMergeEditView::OnViewEOL()
3985 GetOptionsMgr()->SaveOption(OPT_VIEW_EOL, !GetViewEols());
3986 GetDocument()->RefreshOptions();
3989 void CMergeEditView::OnUpdateViewEOL(CCmdUI* pCmdUI)
3991 pCmdUI->SetCheck(GetViewEols());
3994 void CMergeEditView::OnSize(UINT nType, int cx, int cy)
3996 if (!IsInitialized())
3999 CMergeDoc * pDoc = GetDocument();
4000 if (m_nThisPane < pDoc->m_nBuffers - 1)
4002 // To calculate subline index correctly
4003 // we have to invalidate line cache in all pane before calling the function related the subline.
4004 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4006 CMergeEditView *pView = GetGroupView(nPane);
4007 if (pView != nullptr)
4008 pView->InvalidateScreenRect(false);
4013 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4015 CMergeEditView *pView = GetGroupView(nPane);
4016 if (pView != nullptr)
4017 pView->Invalidate();
4020 // recalculate m_nTopSubLine
4021 m_nTopSubLine = GetSubLineIndex(m_nTopLine);
4025 RecalcVertScrollBar (false, false);
4026 RecalcHorzScrollBar (false, false);
4030 * @brief allocates GDI resources for printing
4031 * @param pDC [in] points to the printer device context
4032 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
4034 void CMergeEditView::OnBeginPrinting(CDC * pDC, CPrintInfo * pInfo)
4036 GetParentFrame()->PostMessage(WM_TIMER);
4038 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
4040 CMergeEditView *pView = GetDocument()->GetView(m_nThisGroup, pane);
4041 pView->m_bPrintHeader = true;
4042 pView->m_bPrintFooter = true;
4043 pView->CGhostTextView::OnBeginPrinting(pDC, pInfo);
4048 * @brief frees GDI resources for printing
4049 * @param pDC [in] points to the printer device context
4050 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
4052 void CMergeEditView::OnEndPrinting(CDC * pDC, CPrintInfo * pInfo)
4054 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
4055 GetDocument()->GetView(m_nThisGroup, pane)->CGhostTextView::OnEndPrinting(pDC, pInfo);
4057 GetParentFrame()->PostMessage(WM_TIMER);
4061 * @brief Gets header text to print
4062 * @param [in] nPageNum the page number to print
4063 * @param [out] header text to print
4065 void CMergeEditView::GetPrintHeaderText(int nPageNum, CString & text)
4067 text = GetDocument()->GetTitle();
4071 * @brief Prints header
4072 * @param [in] nPageNum the page number to print
4074 void CMergeEditView::PrintHeader(CDC * pdc, int nPageNum)
4076 if (m_nThisPane > 0)
4078 int oldRight = m_rcPrintArea.right;
4079 m_rcPrintArea.right += m_rcPrintArea.Width() * (GetDocument()->m_nBuffers - 1);
4080 CGhostTextView::PrintHeader(pdc, nPageNum);
4081 m_rcPrintArea.right = oldRight;
4085 * @brief Prints footer
4086 * @param [in] nPageNum the page number to print
4088 void CMergeEditView::PrintFooter(CDC * pdc, int nPageNum)
4090 if (m_nThisPane > 0)
4092 int oldRight = m_rcPrintArea.right;
4093 m_rcPrintArea.right += m_rcPrintArea.Width() * (GetDocument()->m_nBuffers - 1);
4094 CGhostTextView::PrintFooter(pdc, nPageNum);
4095 m_rcPrintArea.right = oldRight;
4098 void CMergeEditView::RecalcPageLayouts (CDC * pDC, CPrintInfo * pInfo)
4100 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
4101 GetDocument()->GetView(m_nThisGroup, pane)->CGhostTextView::RecalcPageLayouts(pDC, pInfo);
4105 * @brief Prints or previews both panes.
4106 * @param pDC [in] points to the printer device context
4107 * @param pInfo [in] points to a CPrintInfo structure that describes the current print job
4109 void CMergeEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
4111 CRect rDraw = pInfo->m_rectDraw;
4112 CSize sz = rDraw.Size();
4113 CMergeDoc *pDoc = GetDocument();
4115 SIZE szLeftTop, szRightBottom;
4116 GetPrintMargins(szLeftTop.cx, szLeftTop.cy, szRightBottom.cx, szRightBottom.cy);
4117 pDC->HIMETRICtoLP(&szLeftTop);
4118 pDC->HIMETRICtoLP(&szRightBottom);
4120 int midX = (sz.cx - szLeftTop.cx - szRightBottom.cx) / pDoc->m_nBuffers;
4123 for (int pane = 0; pane < pDoc->m_nBuffers; pane++)
4125 pInfo->m_rectDraw.left = rDraw.left + midX * pane;
4126 pInfo->m_rectDraw.right = pInfo->m_rectDraw.left + midX + szLeftTop.cx + szRightBottom.cx;
4127 CMergeEditView* pPane = pDoc->GetView(m_nThisGroup, pane);
4128 pPane->CGhostTextView::OnPrint(pDC, pInfo);
4132 bool CMergeEditView::IsInitialized() const
4134 CMergeEditView * pThis = const_cast<CMergeEditView *>(this);
4135 CDiffTextBuffer * pBuffer = dynamic_cast<CDiffTextBuffer *>(pThis->LocateTextBuffer());
4136 return pBuffer->IsInitialized();
4140 * @brief returns the number of empty lines which are added for synchronizing the line in two/three panes.
4142 int CMergeEditView::GetEmptySubLines( int nLineIndex )
4144 int nBreaks[3] = {0};
4145 int nMaxBreaks = -1;
4146 CMergeDoc * pDoc = GetDocument();
4147 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4149 CMergeEditView *pView = GetGroupView(nPane);
4150 if (pView != nullptr)
4152 if (nLineIndex >= pView->GetLineCount())
4154 pView->WrapLineCached( nLineIndex, pView->GetScreenChars(), nullptr, nBreaks[nPane] );
4156 nMaxBreaks = max(nMaxBreaks, nBreaks[nPane]);
4159 if (nBreaks[m_nThisPane] < nMaxBreaks)
4160 return nMaxBreaks - nBreaks[m_nThisPane];
4166 * @brief Invalidate sub line index cache from the specified index to the end of file.
4167 * @param [in] nLineIndex Index of the first line to invalidate
4169 void CMergeEditView::InvalidateSubLineIndexCache( int nLineIndex )
4171 CMergeDoc * pDoc = GetDocument();
4172 ASSERT(pDoc != nullptr);
4174 // We have to invalidate sub line index cache on both panes.
4175 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4177 CMergeEditView *pView = GetGroupView(nPane);
4178 if (pView != nullptr)
4179 pView->CCrystalTextView::InvalidateSubLineIndexCache( nLineIndex );
4183 void CMergeEditView::SetWordWrapping( bool bWordWrap )
4185 for (int pane = 0; pane < GetDocument()->m_nBuffers; pane++)
4186 GetGroupView(pane)->m_bWordWrap = bWordWrap;
4187 CCrystalTextView::SetWordWrapping(bWordWrap);
4191 * @brief Swap the positions of the two panes
4193 void CMergeEditView::OnViewSwapPanes12()
4195 GetDocument()->SwapFiles(0, 1);
4199 * @brief Swap the positions of the two panes
4201 void CMergeEditView::OnViewSwapPanes23()
4203 GetDocument()->SwapFiles(1, 2);
4207 * @brief Swap the positions of the two panes
4209 void CMergeEditView::OnViewSwapPanes13()
4211 GetDocument()->SwapFiles(0, 2);
4215 * @brief Determine if difference is visible on screen.
4216 * @param [in] nDiff Number of diff to check.
4217 * @return true if difference is visible.
4219 bool CMergeEditView::IsDiffVisible(int nDiff)
4221 const CMergeDoc *pd = GetDocument();
4224 pd->m_diffList.GetDiff(nDiff, diff);
4226 return IsDiffVisible(diff);
4230 * @brief Determine if difference is visible on screen.
4231 * @param [in] diff diff to check.
4232 * @param [in] nLinesBelow Allow "minimizing" the number of visible lines.
4233 * @return true if difference is visible, false otherwise.
4235 bool CMergeEditView::IsDiffVisible(const DIFFRANGE& diff, int nLinesBelow /*=0*/)
4237 const int nDiffStart = GetSubLineIndex(diff.dbegin);
4238 const int nDiffEnd = GetSubLineIndex(diff.dend);
4239 // Diff's height is last line - first line + last line's line count
4240 const int nDiffHeight = nDiffEnd - nDiffStart + GetSubLines(diff.dend) + 1;
4242 // If diff first line outside current view - context OR
4243 // if diff last line outside current view - context OR
4244 // if diff is bigger than screen
4245 if ((nDiffStart < m_nTopSubLine) ||
4246 (nDiffEnd >= m_nTopSubLine + GetScreenLines() - nLinesBelow) ||
4247 (nDiffHeight >= GetScreenLines()))
4257 /** @brief Open help from mainframe when user presses F1*/
4258 void CMergeEditView::OnHelp()
4260 theApp.ShowHelp(MergeViewHelpLocation);
4264 * @brief Called after document is loaded.
4265 * This function is called from CMergeDoc::OpenDocs() after documents are
4266 * loaded. So this is good place to set View's options etc.
4268 void CMergeEditView::DocumentsLoaded()
4270 if (GetDocument()->m_ptBuf[m_nThisPane]->GetTableEditing())
4273 if (m_nThisPane == GetDocument()->m_nBuffers - 1 && !m_bDetailView)
4278 SetTopMargin(false);
4281 // Enable/disable automatic rescan (rescanning after edit)
4282 EnableRescan(GetOptionsMgr()->GetBool(OPT_AUTOMATIC_RESCAN));
4284 // SetTextType will revert to language dependent defaults for tab
4285 SetTabSize(GetOptionsMgr()->GetInt(OPT_TAB_SIZE));
4286 SetViewTabs(GetOptionsMgr()->GetBool(OPT_VIEW_WHITESPACE));
4287 const bool mixedEOLs = GetOptionsMgr()->GetBool(OPT_ALLOW_MIXED_EOL) ||
4288 GetDocument()->IsMixedEOL(m_nThisPane);
4289 SetViewEols(GetOptionsMgr()->GetBool(OPT_VIEW_EOL), mixedEOLs);
4290 SetWordWrapping(GetOptionsMgr()->GetBool(OPT_WORDWRAP));
4291 SetViewLineNumbers(GetOptionsMgr()->GetBool(OPT_VIEW_LINENUMBERS));
4292 SetSelectionMargin(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
4294 // Enable Backspace at beginning of line
4295 SetDisableBSAtSOL(false);
4297 // Set tab type (tabs/spaces)
4298 bool bInsertTabs = (GetOptionsMgr()->GetInt(OPT_TAB_TYPE) == 0);
4299 SetInsertTabs(bInsertTabs);
4301 // Sometimes WinMerge doesn't update scrollbars correctly (they remain
4302 // disabled) after docs are open in screen. So lets make sure they are
4303 // really updated, even though this is unnecessary in most cases.
4304 RecalcHorzScrollBar();
4305 RecalcVertScrollBar();
4309 * @brief Update LocationView position.
4310 * This function updates LocationView position to given lines.
4311 * Usually we want to lines in file compare view and area in
4312 * LocationView to match. Be extra carefull to not call non-existing
4314 * @param [in] nTopLine Top line of current view.
4315 * @param [in] nBottomLine Bottom line of current view.
4317 void CMergeEditView::UpdateLocationViewPosition(int nTopLine /*=-1*/,
4318 int nBottomLine /*= -1*/)
4320 CMergeDoc *pDoc = GetDocument();
4321 if (pDoc == nullptr)
4324 CLocationView *pLocationView = pDoc->GetLocationView();
4326 if (pLocationView != nullptr && IsWindow(pLocationView->GetSafeHwnd()))
4328 pLocationView->UpdateVisiblePos(nTopLine, nBottomLine);
4333 * @brief Enable/Disable view's selection margins.
4334 * Selection margins show bookmarks and word-wrap symbols, so they are pretty
4335 * useful. But it appears many users don't use/need those features and for them
4336 * selection margins are just wasted screen estate.
4338 void CMergeEditView::OnViewMargin()
4340 bool bViewMargin = GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN);
4341 GetOptionsMgr()->SaveOption(OPT_VIEW_FILEMARGIN, !bViewMargin);
4343 SetSelectionMargin(!bViewMargin);
4344 CMergeDoc *pDoc = GetDocument();
4345 pDoc->RefreshOptions();
4346 pDoc->UpdateAllViews(this);
4350 * @brief Update GUI for Enable/Disable view's selection margin.
4351 * @param [in] pCmdUI Pointer to UI item to update.
4353 void CMergeEditView::OnUpdateViewMargin(CCmdUI* pCmdUI)
4355 pCmdUI->Enable(true);
4356 pCmdUI->SetCheck(GetOptionsMgr()->GetBool(OPT_VIEW_FILEMARGIN));
4360 * @brief Create the "Change Scheme" sub menu.
4361 * @param [in] pCmdUI Pointer to UI item to update.
4363 void CMergeEditView::OnUpdateViewChangeScheme(CCmdUI *pCmdUI)
4365 // Delete the place holder menu.
4366 pCmdUI->m_pSubMenu->DeleteMenu(0, MF_BYPOSITION);
4368 const HMENU hSubMenu = pCmdUI->m_pSubMenu->m_hMenu;
4370 String name = theApp.LoadString(ID_COLORSCHEME_FIRST);
4371 AppendMenu(hSubMenu, MF_STRING, ID_COLORSCHEME_FIRST, name.c_str());
4372 AppendMenu(hSubMenu, MF_SEPARATOR, 0, nullptr);
4374 for (int i = ID_COLORSCHEME_FIRST + 1; i <= ID_COLORSCHEME_LAST; ++i)
4376 name = theApp.LoadString(i);
4377 AppendMenu(hSubMenu, MF_STRING, i, name.c_str());
4380 pCmdUI->Enable(true);
4384 * @brief Change the editor's syntax highlighting scheme.
4385 * @param [in] nID Selected color scheme sub menu id.
4387 void CMergeEditView::OnChangeScheme(UINT nID)
4389 CMergeDoc *pDoc = GetDocument();
4390 ASSERT(pDoc != nullptr);
4392 for (int nGroup = 0; nGroup < pDoc->m_nGroups; nGroup++)
4393 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4395 CMergeEditView *pView = pDoc->GetView(nGroup, nPane);
4396 ASSERT(pView != nullptr);
4398 if (pView != nullptr)
4400 pView->SetTextType(CrystalLineParser::TextType(nID - ID_COLORSCHEME_FIRST));
4401 pView->SetDisableBSAtSOL(false);
4402 pView->m_bChangedSchemeManually = true;
4410 * @brief Enable all color schemes sub menu items.
4411 * @param [in] pCmdUI Pointer to UI item to update.
4413 void CMergeEditView::OnUpdateChangeScheme(CCmdUI* pCmdUI)
4415 const bool bIsCurrentScheme = (static_cast<UINT>(m_CurSourceDef->type) == (pCmdUI->m_nID - ID_COLORSCHEME_FIRST));
4416 pCmdUI->SetRadio(bIsCurrentScheme);
4417 pCmdUI->Enable(GetOptionsMgr()->GetBool(OPT_SYNTAX_HIGHLIGHT));
4421 * @brief Called when mouse's wheel is scrolled.
4423 BOOL CMergeEditView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
4425 if ( nFlags == MK_CONTROL )
4427 short amount = zDelta < 0 ? -1: 1;
4430 // no default CCrystalTextView
4431 return CView::OnMouseWheel(nFlags, zDelta, pt);
4434 if (nFlags == MK_SHIFT)
4436 SCROLLINFO si = { sizeof SCROLLINFO };
4437 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
4439 VERIFY(GetScrollInfo(SB_HORZ, &si));
4442 si.nPos -= zDelta / 40;
4443 if (si.nPos > si.nMax) si.nPos = si.nMax;
4444 if (si.nPos < si.nMin) si.nPos = si.nMin;
4446 SetScrollInfo(SB_HORZ, &si);
4449 SendMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, si.nPos) , NULL );
4451 // no default CCrystalTextView
4452 return CView::OnMouseWheel(nFlags, zDelta, pt);
4455 return CGhostTextView::OnMouseWheel(nFlags, zDelta, pt);
4459 * @brief Called when mouse's horizontal wheel is scrolled.
4461 void CMergeEditView::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
4463 SCROLLINFO si = { sizeof SCROLLINFO };
4464 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
4466 VERIFY(GetScrollInfo(SB_HORZ, &si));
4469 si.nPos += zDelta / 40;
4470 if (si.nPos > si.nMax) si.nPos = si.nMax;
4471 if (si.nPos < si.nMin) si.nPos = si.nMin;
4473 SetScrollInfo(SB_HORZ, &si);
4476 SendMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, si.nPos) , NULL );
4478 // no default CCrystalTextView
4479 CView::OnMouseHWheel(nFlags, zDelta, pt);
4483 * @brief Change font size (zoom) in views.
4484 * @param [in] amount Amount of change/zoom, negative number makes
4485 * font smaller, positive number bigger and 0 reset the font size.
4487 void CMergeEditView::ZoomText(short amount)
4492 const int nLogPixelsY = CClientDC(this).GetDeviceCaps(LOGPIXELSY);
4493 int nPointSize = -MulDiv(lf.lfHeight, 72, nLogPixelsY);
4497 nPointSize = -MulDiv(GetOptionsMgr()->GetInt(OPT_FONT_FILECMP + OPT_FONT_HEIGHT), 72, nLogPixelsY);
4500 nPointSize += amount;
4504 lf.lfHeight = -MulDiv(nPointSize, nLogPixelsY, 72);
4506 CMergeDoc *pDoc = GetDocument();
4507 ASSERT(pDoc != nullptr);
4509 if (pDoc != nullptr)
4511 for (int nPane = 0; nPane < pDoc->m_nBuffers; nPane++)
4513 CMergeEditView *pView = GetGroupView(nPane);
4514 ASSERT(pView != nullptr);
4516 if (pView != nullptr)
4524 bool CMergeEditView::QueryEditable()
4526 return m_bDetailView ? false : !GetDocument()->m_ptBuf[m_nThisPane]->GetReadOnly();
4530 * @brief Adjust the point to remain in the displayed diff
4532 * @return Tells if the point has been changed
4534 bool CMergeEditView::EnsureInDiff(CPoint& pt)
4536 int nLineCount = GetLineCount();
4537 if (m_lineBegin >= nLineCount)
4538 m_lineBegin = nLineCount - 1;
4539 if (m_lineEnd >= nLineCount)
4540 m_lineEnd = nLineCount - 1;
4542 int diffLength = m_lineEnd - m_lineBegin + 1;
4543 // first get the degenerate case out of the way
4545 if (diffLength == 0)
4547 if (pt.y == m_lineBegin && pt.x == 0)
4555 if (pt.y < m_lineBegin)
4561 // diff is defined and not below diff
4562 if (m_lineEnd > -1 && pt.y > m_lineEnd)
4565 pt.x = GetLineLength(pt.y);
4571 void CMergeEditView::EnsureVisible(CPoint pt)
4576 // ensure we remain in diff
4577 if (EnsureInDiff(ptNew))
4578 SetCursorPos(ptNew);
4580 CCrystalTextView::EnsureVisible(ptNew);
4583 void CMergeEditView::EnsureVisible(CPoint ptStart, CPoint ptEnd)
4585 CCrystalTextView::EnsureVisible(ptStart, ptEnd);
4588 void CMergeEditView::SetSelection(const CPoint& ptStart, const CPoint& ptEnd, bool bUpdateView)
4590 CPoint ptStartNew = ptStart;
4591 CPoint ptEndNew = ptEnd;
4594 // ensure we remain in diff
4595 EnsureInDiff(ptStartNew);
4596 EnsureInDiff(ptEndNew);
4598 CCrystalTextView::SetSelection(ptStartNew, ptEndNew, bUpdateView);
4601 void CMergeEditView::ScrollToSubLine(int nNewTopLine, bool bNoSmoothScroll /*= FALSE*/, bool bTrackScrollBar /*= TRUE*/)
4605 int nLineCount = GetLineCount();
4606 if (m_lineBegin >= nLineCount)
4607 m_lineBegin = nLineCount - 1;
4608 if (m_lineEnd >= nLineCount)
4609 m_lineEnd = nLineCount - 1;
4611 // ensure we remain in diff
4612 int sublineBegin = GetSubLineIndex(m_lineBegin);
4613 int sublineEnd = m_lineEnd < 0 ? -1 : GetSubLineIndex(m_lineEnd) + GetSubLines(m_lineEnd) - 1;
4614 int diffLength = sublineEnd - sublineBegin + 1;
4615 int displayLength = GetScreenLines();
4616 if (diffLength <= displayLength)
4617 nNewTopLine = sublineBegin;
4620 if (nNewTopLine < sublineBegin)
4621 nNewTopLine = sublineBegin;
4622 if (nNewTopLine + displayLength - 1 > sublineEnd)
4623 nNewTopLine = GetSubLineIndex(sublineEnd - displayLength + 1);
4626 CPoint pt = GetCursorPos();
4627 if (EnsureInDiff(pt))
4630 auto [ptSelStart, ptSelEnd] = GetSelection();
4631 if (EnsureInDiff(ptSelStart) || EnsureInDiff(ptSelEnd))
4632 SetSelection(ptSelStart, ptSelEnd);
4634 CCrystalTextView::ScrollToSubLine(nNewTopLine, bNoSmoothScroll, bTrackScrollBar);
4637 void CMergeEditView::SetActivePane()
4639 auto* pwndSplitterChild = GetParentSplitter(this, false);
4640 if (!pwndSplitterChild)
4642 if (pwndSplitterChild->GetColumnCount() > 1)
4643 pwndSplitterChild->SetActivePane(0, m_nThisPane);
4645 pwndSplitterChild->SetActivePane(m_nThisPane, 0);
4649 * @brief Called when user selects View/Zoom In from menu.
4651 void CMergeEditView::OnViewZoomIn()
4657 * @brief Called when user selects View/Zoom Out from menu.
4659 void CMergeEditView::OnViewZoomOut()
4665 * @brief Called when user selects View/Zoom Normal from menu.
4667 void CMergeEditView::OnViewZoomNormal()
4672 void CMergeEditView::OnDropFiles(const std::vector<String>& tFiles)
4674 if (tFiles.size() > 1 || paths::IsDirectory(tFiles[0]))
4676 GetMainFrame()->GetDropHandler()->GetCallback()(tFiles);
4680 GetDocument()->ChangeFile(m_nThisPane, tFiles[0]);
4683 void CMergeEditView::OnWindowSplit()
4686 auto& wndSplitter = dynamic_cast<CMergeEditFrame *>(GetParentFrame())->GetSplitter();
4687 CMergeDoc *pDoc = GetDocument();
4688 CMergeEditView *pView = pDoc->GetView(0, m_nThisPane);
4689 auto* pwndSplitterChild = pView->GetParentSplitter(pView, false);
4690 int nBuffer = m_nThisPane;
4691 if (pDoc->m_nGroups <= 2)
4693 wndSplitter.SplitRow(1);
4694 wndSplitter.EqualizeRows();
4698 wndSplitter.SetActivePane(0, 0);
4699 wndSplitter.DeleteRow(1);
4700 pDoc->GetView(0, nBuffer)->SetActivePane();
4704 void CMergeEditView::OnUpdateWindowSplit(CCmdUI* pCmdUI)
4706 pCmdUI->Enable(!m_bDetailView);
4707 pCmdUI->SetCheck(GetDocument()->m_nGroups > 2);
4710 void CMergeEditView::OnStatusBarDblClick(NMHDR* pNMHDR, LRESULT* pResult)
4713 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
4714 const int pane = pNMItemActivate->iItem / 4;
4715 CMergeDoc* pDoc = GetDocument();
4716 if (pane >= pDoc->m_nBuffers || !GetParentFrame()->IsChild(CWnd::FromHandle(pNMItemActivate->hdr.hwndFrom)))
4719 switch (pNMItemActivate->iItem % 4)
4722 pDoc->GetView(0, pane)->PostMessage(WM_COMMAND, ID_EDIT_WMGOTO);
4725 pDoc->GetView(0, pane)->PostMessage(WM_COMMAND, ID_FILE_ENCODING);
4730 ::GetCursorPos(&point);
4733 VERIFY(menu.LoadMenu(IDR_POPUP_MERGEEDITFRAME_STATUSBAR_EOL));
4734 theApp.TranslateMenu(menu.m_hMenu);
4735 menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, GetDocument()->GetView(0, pane));
4739 pDoc->m_ptBuf[pane]->SetReadOnly(!GetDocument()->m_ptBuf[pane]->GetReadOnly());