Fix compilation warnings
[TortoiseGit.git] / src / TortoiseMerge / Undo.cpp
blob8fa5516108c5c58516b21ae0b81759cb80dbaf7d
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007, 2010-2011 - 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.
20 #include "stdafx.h"
21 #include "Undo.h"
23 #include "BaseView.h"
25 void viewstate::AddViewLineFromView(CBaseView *pView, int nViewLine, bool bAddEmptyLine)
27 // is undo good place for this ?
28 if (!pView || !pView->m_pViewData)
29 return;
30 replacedlines[nViewLine] = pView->m_pViewData->GetData(nViewLine);
31 if (bAddEmptyLine)
33 addedlines.push_back(nViewLine + 1);
34 pView->AddEmptyViewLine(nViewLine);
38 void viewstate::Clear()
40 difflines.clear();
41 linestates.clear();
42 linelines.clear();
43 linesEOL.clear();
44 addedlines.clear();
46 removedlines.clear();
47 replacedlines.clear();
50 CUndo& CUndo::GetInstance()
52 static CUndo instance;
53 return instance;
56 CUndo::CUndo()
58 m_originalstate = 0;
59 m_groupCount = 0;
62 CUndo::~CUndo()
66 void CUndo::AddState(const allviewstate& allstate, POINT pt)
68 m_viewstates.push_back(allstate);
69 m_caretpoints.push_back(pt);
72 bool CUndo::Undo(CBaseView * pLeft, CBaseView * pRight, CBaseView * pBottom)
74 if (!CanUndo())
75 return false;
77 if (m_groups.size() && m_groups.back() == m_caretpoints.size())
79 m_groups.pop_back();
80 std::list<int>::size_type b = m_groups.back();
81 m_groups.pop_back();
82 while (b < m_caretpoints.size())
83 UndoOne(pLeft, pRight, pBottom);
85 else
86 UndoOne(pLeft, pRight, pBottom);
88 CBaseView * pActiveView = NULL;
90 if (pBottom && pBottom->IsTarget())
92 pActiveView = pBottom;
94 else
95 if (pRight && pRight->IsTarget())
97 pActiveView = pRight;
99 else
100 //if (pLeft && pLeft->IsTarget())
102 pActiveView = pLeft;
106 if (pActiveView) {
107 pActiveView->ClearSelection();
108 pActiveView->BuildAllScreen2ViewVector();
109 pActiveView->RecalcAllVertScrollBars();
110 pActiveView->RecalcAllHorzScrollBars();
111 pActiveView->EnsureCaretVisible();
112 pActiveView->UpdateCaret();
113 bool bModified = m_viewstates.size() != m_originalstate;
114 if (pLeft)
115 pLeft->SetModified(bModified);
116 if (pRight)
117 pRight->SetModified(bModified);
118 if (pBottom)
119 pBottom->SetModified(bModified);
120 pActiveView->RefreshViews();
123 if (m_viewstates.size() < m_originalstate)
124 // Can never get back to original state now
125 m_originalstate = (size_t)-1;
127 return true;
130 void CUndo::UndoOne(CBaseView * pLeft, CBaseView * pRight, CBaseView * pBottom)
132 allviewstate allstate = m_viewstates.back();
133 POINT pt = m_caretpoints.back();
135 Undo(allstate.left, pLeft, pt);
136 Undo(allstate.right, pRight, pt);
137 Undo(allstate.bottom, pBottom, pt);
139 m_viewstates.pop_back();
140 m_caretpoints.pop_back();
143 void CUndo::Undo(const viewstate& state, CBaseView * pView, const POINT& pt)
145 if (!pView)
146 return;
148 CViewData* viewData = pView->m_pViewData;
149 if (!viewData)
150 return;
152 for (std::list<int>::const_reverse_iterator it = state.addedlines.rbegin(); it != state.addedlines.rend(); ++it)
154 viewData->RemoveData(*it);
156 for (std::map<int, DWORD>::const_iterator it = state.linelines.begin(); it != state.linelines.end(); ++it)
158 viewData->SetLineNumber(it->first, it->second);
160 for (std::map<int, DWORD>::const_iterator it = state.linestates.begin(); it != state.linestates.end(); ++it)
162 viewData->SetState(it->first, (DiffStates)it->second);
164 for (std::map<int, EOL>::const_iterator it = state.linesEOL.begin(); it != state.linesEOL.end(); ++it)
166 viewData->SetLineEnding(it->first, (EOL)it->second);
168 for (std::map<int, CString>::const_iterator it = state.difflines.begin(); it != state.difflines.end(); ++it)
170 viewData->SetLine(it->first, it->second);
172 for (std::map<int, viewdata>::const_iterator it = state.removedlines.begin(); it != state.removedlines.end(); ++it)
174 viewData->InsertData(it->first, it->second);
176 for (std::map<int, viewdata>::const_iterator it = state.replacedlines.begin(); it != state.replacedlines.end(); ++it)
178 viewData->SetData(it->first, it->second);
181 if (pView->IsTarget())
183 pView->SetCaretViewPosition(pt);
184 pView->EnsureCaretVisible();
190 void CUndo::Clear()
192 m_viewstates.clear();
193 m_caretpoints.clear();
194 m_groups.clear();
195 m_originalstate = 0;
196 m_groupCount = 0;