Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "refundo.hxx"
21 : #include "undobase.hxx"
22 : #include "document.hxx"
23 : #include "dbdata.hxx"
24 : #include "rangenam.hxx"
25 : #include "pivot.hxx"
26 : #include "chartarr.hxx"
27 : #include "stlpool.hxx"
28 : #include "detdata.hxx"
29 : #include "prnsave.hxx"
30 : #include "chartlis.hxx"
31 : #include "dpobject.hxx"
32 : #include "areasave.hxx"
33 : #include "unoreflist.hxx"
34 : #include <scopetools.hxx>
35 : #include <refupdatecontext.hxx>
36 :
37 66 : ScRefUndoData::ScRefUndoData( const ScDocument* pDoc ) :
38 : pDBCollection(NULL),
39 : pRangeName(NULL),
40 66 : pPrintRanges(pDoc->CreatePrintRangeSaver()),
41 : pDPCollection(NULL),
42 : pDetOpList(NULL),
43 : pChartListenerCollection(NULL),
44 : pAreaLinks(NULL),
45 132 : pUnoRefs(NULL)
46 : {
47 66 : const ScDBCollection* pOldDBColl = pDoc->GetDBCollection();
48 66 : if (pOldDBColl && !pOldDBColl->empty())
49 0 : pDBCollection = new ScDBCollection(*pOldDBColl);
50 :
51 66 : const ScRangeName* pOldRanges = pDoc->GetRangeName();
52 66 : if (pOldRanges && !pOldRanges->empty())
53 11 : pRangeName = new ScRangeName(*pOldRanges);
54 :
55 : // when handling Pivot solely keep the range?
56 :
57 66 : const ScDPCollection* pOldDP = pDoc->GetDPCollection();
58 66 : if (pOldDP && pOldDP->GetCount())
59 16 : pDPCollection = new ScDPCollection(*pOldDP);
60 :
61 66 : const ScDetOpList* pOldDetOp = pDoc->GetDetOpList();
62 66 : if (pOldDetOp && pOldDetOp->Count())
63 0 : pDetOpList = new ScDetOpList(*pOldDetOp);
64 :
65 66 : const ScChartListenerCollection* pOldChartLisColl = pDoc->GetChartListenerCollection();
66 66 : if (pOldChartLisColl)
67 66 : pChartListenerCollection = new ScChartListenerCollection(*pOldChartLisColl);
68 :
69 66 : pAreaLinks = ScAreaLinkSaveCollection::CreateFromDoc(pDoc); // returns NULL if empty
70 :
71 66 : const_cast<ScDocument*>(pDoc)->BeginUnoRefUndo();
72 66 : }
73 :
74 66 : ScRefUndoData::~ScRefUndoData()
75 : {
76 66 : delete pDBCollection;
77 66 : delete pRangeName;
78 66 : delete pPrintRanges;
79 66 : delete pDPCollection;
80 66 : delete pDetOpList;
81 66 : delete pChartListenerCollection;
82 66 : delete pAreaLinks;
83 66 : delete pUnoRefs;
84 66 : }
85 :
86 66 : void ScRefUndoData::DeleteUnchanged( const ScDocument* pDoc )
87 : {
88 66 : if (pDBCollection)
89 : {
90 0 : ScDBCollection* pNewDBColl = pDoc->GetDBCollection();
91 0 : if ( pNewDBColl && *pDBCollection == *pNewDBColl )
92 0 : DELETEZ(pDBCollection);
93 : }
94 66 : if (pRangeName)
95 : {
96 11 : ScRangeName* pNewRanges = pDoc->GetRangeName();
97 11 : if ( pNewRanges && *pRangeName == *pNewRanges )
98 7 : DELETEZ(pRangeName);
99 : }
100 :
101 66 : if (pPrintRanges)
102 : {
103 66 : ScPrintRangeSaver* pNewRanges = pDoc->CreatePrintRangeSaver();
104 66 : if ( pNewRanges && *pPrintRanges == *pNewRanges )
105 46 : DELETEZ(pPrintRanges);
106 66 : delete pNewRanges;
107 : }
108 :
109 66 : if (pDPCollection)
110 : {
111 16 : ScDPCollection* pNewDP = const_cast<ScDocument*>(pDoc)->GetDPCollection(); //! const
112 16 : if ( pNewDP && pDPCollection->RefsEqual(*pNewDP) )
113 0 : DELETEZ(pDPCollection);
114 : }
115 :
116 66 : if (pDetOpList)
117 : {
118 0 : ScDetOpList* pNewDetOp = pDoc->GetDetOpList();
119 0 : if ( pNewDetOp && *pDetOpList == *pNewDetOp )
120 0 : DELETEZ(pDetOpList);
121 : }
122 :
123 66 : if ( pChartListenerCollection )
124 : {
125 : ScChartListenerCollection* pNewChartListenerCollection =
126 66 : pDoc->GetChartListenerCollection();
127 132 : if ( pNewChartListenerCollection &&
128 66 : *pChartListenerCollection == *pNewChartListenerCollection )
129 66 : DELETEZ( pChartListenerCollection );
130 : }
131 :
132 66 : if (pAreaLinks)
133 : {
134 0 : if ( pAreaLinks->IsEqual( pDoc ) )
135 0 : DELETEZ(pAreaLinks);
136 : }
137 :
138 66 : if ( pDoc->HasUnoRefUndo() )
139 : {
140 66 : pUnoRefs = const_cast<ScDocument*>(pDoc)->EndUnoRefUndo();
141 66 : if ( pUnoRefs && pUnoRefs->IsEmpty() )
142 : {
143 37 : DELETEZ( pUnoRefs );
144 : }
145 : }
146 66 : }
147 :
148 31 : void ScRefUndoData::DoUndo( ScDocument* pDoc, bool bUndoRefFirst )
149 : {
150 31 : if (pDBCollection)
151 0 : pDoc->SetDBCollection( new ScDBCollection(*pDBCollection) );
152 31 : if (pRangeName)
153 3 : pDoc->SetRangeName( new ScRangeName(*pRangeName) );
154 :
155 31 : if (pPrintRanges)
156 2 : pDoc->RestorePrintRanges(*pPrintRanges);
157 :
158 31 : if (pDPCollection)
159 : {
160 0 : ScDPCollection* pDocDP = pDoc->GetDPCollection();
161 0 : if (pDocDP)
162 0 : pDPCollection->WriteRefsTo( *pDocDP );
163 : }
164 :
165 31 : if (pDetOpList)
166 0 : pDoc->SetDetOpList( new ScDetOpList(*pDetOpList) );
167 :
168 : // bUndoRefFirst is bSetChartRangeLists
169 31 : if ( pChartListenerCollection )
170 : pDoc->SetChartListenerCollection( new ScChartListenerCollection(
171 0 : *pChartListenerCollection ), bUndoRefFirst );
172 :
173 31 : if (pDBCollection || pRangeName)
174 : {
175 3 : sc::AutoCalcSwitch aACSwitch(*pDoc, false);
176 3 : pDoc->CompileAll();
177 :
178 3 : sc::SetFormulaDirtyContext aCxt;
179 3 : pDoc->SetAllFormulasDirty(aCxt);
180 : }
181 :
182 31 : if (pAreaLinks)
183 0 : pAreaLinks->Restore( pDoc );
184 :
185 31 : if ( pUnoRefs )
186 0 : pUnoRefs->Undo( pDoc );
187 187 : }
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|