Do not add empty lines at the end
[TortoiseGit.git] / src / TortoiseMerge / BottomView.cpp
blob566bd10f62099bbee3b14b1bc31abd7df8fe8fb2
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2013 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "resource.h"
21 #include "AppUtils.h"
23 #include "bottomview.h"
25 IMPLEMENT_DYNCREATE(CBottomView, CBaseView)
27 CBottomView::CBottomView(void)
29 m_pwndBottom = this;
30 m_pState = &m_AllState.bottom;
31 m_nStatusBarID = ID_INDICATOR_BOTTOMVIEW;
34 CBottomView::~CBottomView(void)
39 void CBottomView::AddContextItems(CIconMenu& popup, DiffStates state)
41 const bool bShow = HasSelection() && (state != DIFFSTATE_UNKNOWN);
42 if (!bShow)
43 return;
45 popup.AppendMenuIcon(POPUPCOMMAND_USETHEIRBLOCK, IDS_VIEWCONTEXTMENU_USETHEIRBLOCK);
46 popup.AppendMenuIcon(POPUPCOMMAND_USEYOURBLOCK, IDS_VIEWCONTEXTMENU_USEYOURBLOCK);
47 popup.AppendMenuIcon(POPUPCOMMAND_USEYOURANDTHEIRBLOCK, IDS_VIEWCONTEXTMENU_USEYOURANDTHEIRBLOCK);
48 popup.AppendMenuIcon(POPUPCOMMAND_USETHEIRANDYOURBLOCK, IDS_VIEWCONTEXTMENU_USETHEIRANDYOURBLOCK);
50 CBaseView::AddContextItems(popup, state);
54 void CBottomView::UseBlock(CBaseView * pwndView, int nFirstViewLine, int nLastViewLine)
56 if (!IsViewGood(pwndView))
57 return;
58 CUndo::GetInstance().BeginGrouping(); // start group undo
60 for (int viewLine = nFirstViewLine; viewLine <= nLastViewLine; viewLine++)
62 viewdata lineData = pwndView->GetViewData(viewLine);
63 if ((lineData.ending != EOL_NOENDING)||(viewLine < (GetViewCount()-1)))
64 lineData.ending = lineendings;
65 lineData.state = ResolveState(lineData.state);
66 SetViewData(viewLine, lineData);
69 int nRemovedLines = CleanEmptyLines();
70 SaveUndoStep();
71 UpdateViewLineNumbers();
72 SaveUndoStep();
74 CUndo::GetInstance().EndGrouping();
76 // final clean up
77 ClearSelection();
78 SetupAllViewSelection(nFirstViewLine, nLastViewLine - nRemovedLines);
79 BuildAllScreen2ViewVector();
80 SetModified();
81 RefreshViews();
84 void CBottomView::UseBothBlocks(CBaseView * pwndFirst, CBaseView * pwndLast)
86 if (!IsViewGood(pwndFirst) || !IsViewGood(pwndLast))
87 return;
88 int nFirstViewLine = 0; // first view line in selection
89 int nLastViewLine = 0; // last view line in selection
91 if (!GetViewSelection(nFirstViewLine, nLastViewLine))
92 return;
94 int nNextViewLine = nLastViewLine + 1; // first view line after selected block
96 CUndo::GetInstance().BeginGrouping(); // start group undo
98 // use (copy) first block
99 for (int viewLine = nFirstViewLine; viewLine <= nLastViewLine; viewLine++)
101 viewdata lineData = pwndFirst->GetViewData(viewLine);
102 if ((lineData.ending != EOL_NOENDING)||(viewLine < (GetViewCount()-1)))
103 lineData.ending = lineendings;
104 lineData.state = ResolveState(lineData.state);
105 SetViewData(viewLine, lineData);
106 if (!IsStateEmpty(pwndFirst->GetViewState(viewLine)))
108 pwndFirst->SetViewState(viewLine, DIFFSTATE_YOURSADDED); // this is improper (may be DIFFSTATE_THEIRSADDED) but seems not to produce any visible bug
111 SaveUndoStep();
113 // use (insert) last block
114 int nViewIndex = nNextViewLine;
115 for (int viewLine = nFirstViewLine; viewLine <= nLastViewLine; viewLine++, nViewIndex++)
117 viewdata lineData = pwndLast->GetViewData(viewLine);
118 lineData.state = ResolveState(lineData.state);
119 InsertViewData(nViewIndex, lineData);
120 if (!IsStateEmpty(pwndLast->GetViewState(viewLine)))
122 pwndLast->SetViewState(viewLine, DIFFSTATE_THEIRSADDED); // this is improper but seems not to produce any visible bug
125 SaveUndoStep();
127 // adjust line numbers in target
128 // we fix all line numbers to handle exotic cases
129 UpdateViewLineNumbers();
130 SaveUndoStep();
132 // now insert an empty block in both first and last
133 int nCount = nLastViewLine - nFirstViewLine + 1;
134 pwndLast->InsertViewEmptyLines(nFirstViewLine, nCount);
135 pwndFirst->InsertViewEmptyLines(nNextViewLine, nCount);
136 SaveUndoStep();
138 int nRemovedLines = CleanEmptyLines();
139 SaveUndoStep();
141 CUndo::GetInstance().EndGrouping();
143 // final clean up
144 ClearSelection();
145 SetupAllViewSelection(nFirstViewLine, 2*nLastViewLine - nFirstViewLine - nRemovedLines + 1);
146 BuildAllScreen2ViewVector();
147 SetModified();
148 pwndLast->SetModified();
149 pwndFirst->SetModified();
150 RefreshViews();
153 void CBottomView::UseViewBlock(CBaseView * pwndView)
155 if (!IsViewGood(pwndView))
156 return;
157 int nFirstViewLine = 0; // first view line in selection
158 int nLastViewLine = 0; // last view line in selection
160 if (!GetViewSelection(nFirstViewLine, nLastViewLine))
161 return;
163 return UseBlock(pwndView, nFirstViewLine, nLastViewLine);
166 void CBottomView::UseViewFile(CBaseView * pwndView)
168 if (!IsViewGood(pwndView))
169 return;
170 int nFirstViewLine = 0;
171 int nLastViewLine = GetViewCount()-1;
173 return UseBlock(pwndView, nFirstViewLine, nLastViewLine);