xref: /trunk/main/sc/source/ui/undo/refundo.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 // -----------------------------------------------------------------------
32 
33 
34 
35 // INCLUDE ---------------------------------------------------------------
36 
37 #include "refundo.hxx"
38 #include "undobase.hxx"
39 #include "document.hxx"
40 #include "dbcolect.hxx"
41 #include "rangenam.hxx"
42 #include "pivot.hxx"
43 #include "chartarr.hxx"
44 #include "stlpool.hxx"
45 #include "conditio.hxx"
46 #include "detdata.hxx"
47 #include "prnsave.hxx"
48 #include "chartlis.hxx"
49 #include "dpobject.hxx"
50 #include "areasave.hxx"
51 #include "unoreflist.hxx"
52 
53 // -----------------------------------------------------------------------
54 
55 ScRefUndoData::ScRefUndoData( const ScDocument* pDoc ) :
56     pUnoRefs( NULL )
57 {
58     ScDBCollection* pOldDBColl = pDoc->GetDBCollection();
59     pDBCollection = pOldDBColl ? new ScDBCollection(*pOldDBColl) : NULL;
60 
61     ScRangeName* pOldRanges = ((ScDocument*)pDoc)->GetRangeName();          //! const
62     pRangeName = pOldRanges ? new ScRangeName(*pOldRanges) : NULL;
63 
64     pPrintRanges = pDoc->CreatePrintRangeSaver();       // neu erzeugt
65 
66     //! bei Pivot nur Bereiche merken ???
67 
68     ScDPCollection* pOldDP = ((ScDocument*)pDoc)->GetDPCollection();        //! const
69     pDPCollection = pOldDP ? new ScDPCollection(*pOldDP) : NULL;
70 
71     ScConditionalFormatList* pOldCond = pDoc->GetCondFormList();
72     pCondFormList = pOldCond ? new ScConditionalFormatList(*pOldCond) : NULL;
73 
74     ScDetOpList* pOldDetOp = pDoc->GetDetOpList();
75     pDetOpList = pOldDetOp ? new ScDetOpList(*pOldDetOp) : 0;
76 
77     ScChartListenerCollection* pOldChartListenerCollection =
78         pDoc->GetChartListenerCollection();
79     pChartListenerCollection = pOldChartListenerCollection ?
80         new ScChartListenerCollection( *pOldChartListenerCollection ) : NULL;
81 
82     pAreaLinks = ScAreaLinkSaveCollection::CreateFromDoc(pDoc);     // returns NULL if empty
83 
84     const_cast<ScDocument*>(pDoc)->BeginUnoRefUndo();
85 }
86 
87 ScRefUndoData::~ScRefUndoData()
88 {
89     delete pDBCollection;
90     delete pRangeName;
91     delete pPrintRanges;
92     delete pDPCollection;
93     delete pCondFormList;
94     delete pDetOpList;
95     delete pChartListenerCollection;
96     delete pAreaLinks;
97     delete pUnoRefs;
98 }
99 
100 void ScRefUndoData::DeleteUnchanged( const ScDocument* pDoc )
101 {
102     if (pDBCollection)
103     {
104         ScDBCollection* pNewDBColl = pDoc->GetDBCollection();
105         if ( pNewDBColl && *pDBCollection == *pNewDBColl )
106             DELETEZ(pDBCollection);
107     }
108     if (pRangeName)
109     {
110         ScRangeName* pNewRanges = ((ScDocument*)pDoc)->GetRangeName();      //! const
111         if ( pNewRanges && *pRangeName == *pNewRanges )
112             DELETEZ(pRangeName);
113     }
114 
115     if (pPrintRanges)
116     {
117         ScPrintRangeSaver* pNewRanges = pDoc->CreatePrintRangeSaver();
118         if ( pNewRanges && *pPrintRanges == *pNewRanges )
119             DELETEZ(pPrintRanges);
120         delete pNewRanges;
121     }
122 
123     if (pDPCollection)
124     {
125         ScDPCollection* pNewDP = ((ScDocument*)pDoc)->GetDPCollection();    //! const
126         if ( pNewDP && pDPCollection->RefsEqual(*pNewDP) )
127             DELETEZ(pDPCollection);
128     }
129 
130     if (pCondFormList)
131     {
132         ScConditionalFormatList* pNewCond = pDoc->GetCondFormList();
133         if ( pNewCond && *pCondFormList == *pNewCond )
134             DELETEZ(pCondFormList);
135     }
136 
137     if (pDetOpList)
138     {
139         ScDetOpList* pNewDetOp = pDoc->GetDetOpList();
140         if ( pNewDetOp && *pDetOpList == *pNewDetOp )
141             DELETEZ(pDetOpList);
142     }
143 
144     if ( pChartListenerCollection )
145     {
146         ScChartListenerCollection* pNewChartListenerCollection =
147             pDoc->GetChartListenerCollection();
148         if ( pNewChartListenerCollection &&
149                 *pChartListenerCollection == *pNewChartListenerCollection )
150             DELETEZ( pChartListenerCollection );
151     }
152 
153     if (pAreaLinks)
154     {
155         if ( pAreaLinks->IsEqual( pDoc ) )
156             DELETEZ(pAreaLinks);
157     }
158 
159     if ( pDoc->HasUnoRefUndo() )
160     {
161         pUnoRefs = const_cast<ScDocument*>(pDoc)->EndUnoRefUndo();
162         if ( pUnoRefs && pUnoRefs->IsEmpty() )
163         {
164             DELETEZ( pUnoRefs );
165         }
166     }
167 }
168 
169 void ScRefUndoData::DoUndo( ScDocument* pDoc, sal_Bool bUndoRefFirst )
170 {
171     if (pDBCollection)
172         pDoc->SetDBCollection( new ScDBCollection(*pDBCollection) );
173     if (pRangeName)
174         pDoc->SetRangeName( new ScRangeName(*pRangeName) );
175 
176     if (pPrintRanges)
177         pDoc->RestorePrintRanges(*pPrintRanges);
178 
179     if (pDPCollection)
180     {
181         ScDPCollection* pDocDP = pDoc->GetDPCollection();
182         if (pDocDP)
183             pDPCollection->WriteRefsTo( *pDocDP );
184     }
185 
186     if (pCondFormList)
187         pDoc->SetCondFormList( new ScConditionalFormatList(*pCondFormList) );
188     if (pDetOpList)
189         pDoc->SetDetOpList( new ScDetOpList(*pDetOpList) );
190 
191     // #65055# bUndoRefFirst ist bSetChartRangeLists
192     if ( pChartListenerCollection )
193         pDoc->SetChartListenerCollection( new ScChartListenerCollection(
194             *pChartListenerCollection ), bUndoRefFirst );
195 
196     if (pDBCollection || pRangeName)
197     {
198         sal_Bool bOldAutoCalc = pDoc->GetAutoCalc();
199         pDoc->SetAutoCalc( sal_False ); // Mehrfachberechnungen vermeiden
200         pDoc->CompileAll();
201         pDoc->SetDirty();
202         pDoc->SetAutoCalc( bOldAutoCalc );
203     }
204 
205     if (pAreaLinks)
206         pAreaLinks->Restore( pDoc );
207 
208     if ( pUnoRefs )
209         pUnoRefs->Undo( pDoc );
210 }
211 
212 
213 
214 
215