Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / undo / refundo.cxx
blob002a2bacea374b13af0af97db73e9e0eb209b92f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "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"
35 ScRefUndoData::ScRefUndoData( const ScDocument* pDoc ) :
36 pUnoRefs( NULL )
38 ScDBCollection* pOldDBColl = pDoc->GetDBCollection();
39 pDBCollection = pOldDBColl ? new ScDBCollection(*pOldDBColl) : NULL;
41 ScRangeName* pOldRanges = ((ScDocument*)pDoc)->GetRangeName(); //! const
42 pRangeName = pOldRanges ? new ScRangeName(*pOldRanges) : NULL;
44 pPrintRanges = pDoc->CreatePrintRangeSaver(); // recreated
46 // when handling Pivot solely keep the range?
48 ScDPCollection* pOldDP = ((ScDocument*)pDoc)->GetDPCollection(); //! const
49 pDPCollection = pOldDP ? new ScDPCollection(*pOldDP) : NULL;
51 ScDetOpList* pOldDetOp = pDoc->GetDetOpList();
52 pDetOpList = pOldDetOp ? new ScDetOpList(*pOldDetOp) : 0;
54 ScChartListenerCollection* pOldChartListenerCollection =
55 pDoc->GetChartListenerCollection();
56 pChartListenerCollection = pOldChartListenerCollection ?
57 new ScChartListenerCollection( *pOldChartListenerCollection ) : NULL;
59 pAreaLinks = ScAreaLinkSaveCollection::CreateFromDoc(pDoc); // returns NULL if empty
61 const_cast<ScDocument*>(pDoc)->BeginUnoRefUndo();
64 ScRefUndoData::~ScRefUndoData()
66 delete pDBCollection;
67 delete pRangeName;
68 delete pPrintRanges;
69 delete pDPCollection;
70 delete pDetOpList;
71 delete pChartListenerCollection;
72 delete pAreaLinks;
73 delete pUnoRefs;
76 void ScRefUndoData::DeleteUnchanged( const ScDocument* pDoc )
78 if (pDBCollection)
80 ScDBCollection* pNewDBColl = pDoc->GetDBCollection();
81 if ( pNewDBColl && *pDBCollection == *pNewDBColl )
82 DELETEZ(pDBCollection);
84 if (pRangeName)
86 ScRangeName* pNewRanges = ((ScDocument*)pDoc)->GetRangeName(); //! const
87 if ( pNewRanges && *pRangeName == *pNewRanges )
88 DELETEZ(pRangeName);
91 if (pPrintRanges)
93 ScPrintRangeSaver* pNewRanges = pDoc->CreatePrintRangeSaver();
94 if ( pNewRanges && *pPrintRanges == *pNewRanges )
95 DELETEZ(pPrintRanges);
96 delete pNewRanges;
99 if (pDPCollection)
101 ScDPCollection* pNewDP = ((ScDocument*)pDoc)->GetDPCollection(); //! const
102 if ( pNewDP && pDPCollection->RefsEqual(*pNewDP) )
103 DELETEZ(pDPCollection);
106 if (pDetOpList)
108 ScDetOpList* pNewDetOp = pDoc->GetDetOpList();
109 if ( pNewDetOp && *pDetOpList == *pNewDetOp )
110 DELETEZ(pDetOpList);
113 if ( pChartListenerCollection )
115 ScChartListenerCollection* pNewChartListenerCollection =
116 pDoc->GetChartListenerCollection();
117 if ( pNewChartListenerCollection &&
118 *pChartListenerCollection == *pNewChartListenerCollection )
119 DELETEZ( pChartListenerCollection );
122 if (pAreaLinks)
124 if ( pAreaLinks->IsEqual( pDoc ) )
125 DELETEZ(pAreaLinks);
128 if ( pDoc->HasUnoRefUndo() )
130 pUnoRefs = const_cast<ScDocument*>(pDoc)->EndUnoRefUndo();
131 if ( pUnoRefs && pUnoRefs->IsEmpty() )
133 DELETEZ( pUnoRefs );
138 void ScRefUndoData::DoUndo( ScDocument* pDoc, sal_Bool bUndoRefFirst )
140 if (pDBCollection)
141 pDoc->SetDBCollection( new ScDBCollection(*pDBCollection) );
142 if (pRangeName)
143 pDoc->SetRangeName( new ScRangeName(*pRangeName) );
145 if (pPrintRanges)
146 pDoc->RestorePrintRanges(*pPrintRanges);
148 if (pDPCollection)
150 ScDPCollection* pDocDP = pDoc->GetDPCollection();
151 if (pDocDP)
152 pDPCollection->WriteRefsTo( *pDocDP );
155 if (pDetOpList)
156 pDoc->SetDetOpList( new ScDetOpList(*pDetOpList) );
158 // bUndoRefFirst is bSetChartRangeLists
159 if ( pChartListenerCollection )
160 pDoc->SetChartListenerCollection( new ScChartListenerCollection(
161 *pChartListenerCollection ), bUndoRefFirst );
163 if (pDBCollection || pRangeName)
165 sal_Bool bOldAutoCalc = pDoc->GetAutoCalc();
166 pDoc->SetAutoCalc( false ); // Avoid multiple calculations
167 pDoc->CompileAll();
168 pDoc->SetDirty();
169 pDoc->SetAutoCalc( bOldAutoCalc );
172 if (pAreaLinks)
173 pAreaLinks->Restore( pDoc );
175 if ( pUnoRefs )
176 pUnoRefs->Undo( pDoc );
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */