1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <refundo.hxx>
21 #include <document.hxx>
23 #include <rangenam.hxx>
24 #include <detdata.hxx>
25 #include <prnsave.hxx>
26 #include <chartlis.hxx>
27 #include <dpobject.hxx>
28 #include <areasave.hxx>
29 #include <unoreflist.hxx>
30 #include <scopetools.hxx>
31 #include <refupdatecontext.hxx>
33 ScRefUndoData::ScRefUndoData( const ScDocument
* pDoc
) :
34 pPrintRanges(pDoc
->CreatePrintRangeSaver())
36 const ScDBCollection
* pOldDBColl
= pDoc
->GetDBCollection();
37 if (pOldDBColl
&& !pOldDBColl
->empty())
38 pDBCollection
.reset(new ScDBCollection(*pOldDBColl
));
40 const ScRangeName
* pOldRanges
= pDoc
->GetRangeName();
41 if (pOldRanges
&& !pOldRanges
->empty())
42 pRangeName
.reset(new ScRangeName(*pOldRanges
));
44 // when handling Pivot solely keep the range?
46 const ScDPCollection
* pOldDP
= pDoc
->GetDPCollection();
47 if (pOldDP
&& pOldDP
->GetCount())
48 pDPCollection
.reset(new ScDPCollection(*pOldDP
));
50 const ScDetOpList
* pOldDetOp
= pDoc
->GetDetOpList();
51 if (pOldDetOp
&& pOldDetOp
->Count())
52 pDetOpList
.reset(new ScDetOpList(*pOldDetOp
));
54 const ScChartListenerCollection
* pOldChartLisColl
= pDoc
->GetChartListenerCollection();
56 pChartListenerCollection
.reset(new ScChartListenerCollection(*pOldChartLisColl
));
58 pAreaLinks
= ScAreaLinkSaveCollection::CreateFromDoc(pDoc
); // returns NULL if empty
60 const_cast<ScDocument
*>(pDoc
)->BeginUnoRefUndo();
63 ScRefUndoData::~ScRefUndoData()
65 pDBCollection
.reset();
68 pDPCollection
.reset();
70 pChartListenerCollection
.reset();
74 void ScRefUndoData::DeleteUnchanged( const ScDocument
* pDoc
)
78 ScDBCollection
* pNewDBColl
= pDoc
->GetDBCollection();
79 if ( pNewDBColl
&& *pDBCollection
== *pNewDBColl
)
80 pDBCollection
.reset();
84 ScRangeName
* pNewRanges
= pDoc
->GetRangeName();
85 if ( pNewRanges
&& *pRangeName
== *pNewRanges
)
91 std::unique_ptr
<ScPrintRangeSaver
> pNewRanges
= pDoc
->CreatePrintRangeSaver();
92 if ( pNewRanges
&& *pPrintRanges
== *pNewRanges
)
98 ScDPCollection
* pNewDP
= const_cast<ScDocument
*>(pDoc
)->GetDPCollection(); //! const
99 if ( pNewDP
&& pDPCollection
->RefsEqual(*pNewDP
) )
100 pDPCollection
.reset();
105 ScDetOpList
* pNewDetOp
= pDoc
->GetDetOpList();
106 if ( pNewDetOp
&& *pDetOpList
== *pNewDetOp
)
110 if ( pChartListenerCollection
)
112 ScChartListenerCollection
* pNewChartListenerCollection
=
113 pDoc
->GetChartListenerCollection();
114 if ( pNewChartListenerCollection
&&
115 *pChartListenerCollection
== *pNewChartListenerCollection
)
116 pChartListenerCollection
.reset();
121 if ( pAreaLinks
->IsEqual( pDoc
) )
125 if ( pDoc
->HasUnoRefUndo() )
127 pUnoRefs
= const_cast<ScDocument
*>(pDoc
)->EndUnoRefUndo();
128 if ( pUnoRefs
&& pUnoRefs
->IsEmpty() )
135 void ScRefUndoData::DoUndo( ScDocument
* pDoc
, bool bUndoRefFirst
)
138 pDoc
->SetDBCollection( std::unique_ptr
<ScDBCollection
>(new ScDBCollection(*pDBCollection
)) );
140 pDoc
->SetRangeName( std::unique_ptr
<ScRangeName
>(new ScRangeName(*pRangeName
)) );
143 pDoc
->RestorePrintRanges(*pPrintRanges
);
147 ScDPCollection
* pDocDP
= pDoc
->GetDPCollection();
149 pDPCollection
->WriteRefsTo( *pDocDP
);
153 pDoc
->SetDetOpList( std::unique_ptr
<ScDetOpList
>(new ScDetOpList(*pDetOpList
)) );
155 // bUndoRefFirst is bSetChartRangeLists
156 if ( pChartListenerCollection
)
157 pDoc
->SetChartListenerCollection( std::make_unique
<ScChartListenerCollection
>(
158 *pChartListenerCollection
), bUndoRefFirst
);
160 if (pDBCollection
|| pRangeName
)
162 sc::AutoCalcSwitch
aACSwitch(*pDoc
, false);
165 sc::SetFormulaDirtyContext aCxt
;
166 pDoc
->SetAllFormulasDirty(aCxt
);
170 pAreaLinks
->Restore( pDoc
);
173 pUnoRefs
->Undo( pDoc
);
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */