Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / undo / undostyl.cxx
blob1023fce27adc5fe29250dd7861d253df93617c9d
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 <svl/itemset.hxx>
21 #include <vcl/virdev.hxx>
23 #include "undostyl.hxx"
24 #include "docsh.hxx"
25 #include "docpool.hxx"
26 #include "stlpool.hxx"
27 #include "printfun.hxx"
28 #include "scmod.hxx"
29 #include "inputhdl.hxx"
30 #include "globstr.hrc"
32 // -----------------------------------------------------------------------
34 TYPEINIT1(ScUndoModifyStyle, ScSimpleUndo);
35 TYPEINIT1(ScUndoApplyPageStyle, ScSimpleUndo);
37 // -----------------------------------------------------------------------
39 // modify style (cell or page style)
42 ScStyleSaveData::ScStyleSaveData() :
43 pItems( NULL )
47 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData& rOther ) :
48 aName( rOther.aName ),
49 aParent( rOther.aParent )
51 if (rOther.pItems)
52 pItems = new SfxItemSet( *rOther.pItems );
53 else
54 pItems = NULL;
57 ScStyleSaveData::~ScStyleSaveData()
59 delete pItems;
62 ScStyleSaveData& ScStyleSaveData::operator=( const ScStyleSaveData& rOther )
64 aName = rOther.aName;
65 aParent = rOther.aParent;
67 delete pItems;
68 if (rOther.pItems)
69 pItems = new SfxItemSet( *rOther.pItems );
70 else
71 pItems = NULL;
73 return *this;
76 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase* pSource )
78 if ( pSource )
80 aName = pSource->GetName();
81 aParent = pSource->GetParent();
82 delete pItems;
83 pItems = new SfxItemSet( ((SfxStyleSheetBase*)pSource)->GetItemSet() );
85 else
86 *this = ScStyleSaveData(); // empty
89 // -----------------------------------------------------------------------
91 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell* pDocSh, SfxStyleFamily eFam,
92 const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
93 ScSimpleUndo( pDocSh ),
94 eFamily( eFam ),
95 aOldData( rOld ),
96 aNewData( rNew )
100 ScUndoModifyStyle::~ScUndoModifyStyle()
104 OUString ScUndoModifyStyle::GetComment() const
106 sal_uInt16 nId = (eFamily == SFX_STYLE_FAMILY_PARA) ?
107 STR_UNDO_EDITCELLSTYLE :
108 STR_UNDO_EDITPAGESTYLE;
109 return ScGlobal::GetRscString( nId );
112 static void lcl_DocStyleChanged( ScDocument* pDoc, SfxStyleSheetBase* pStyle, sal_Bool bRemoved )
114 //! move to document or docshell
116 VirtualDevice aVDev;
117 Point aLogic = aVDev.LogicToPixel( Point(1000,1000), MAP_TWIP );
118 double nPPTX = aLogic.X() / 1000.0;
119 double nPPTY = aLogic.Y() / 1000.0;
120 Fraction aZoom(1,1);
121 pDoc->StyleSheetChanged( pStyle, bRemoved, &aVDev, nPPTX, nPPTY, aZoom, aZoom );
123 ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
124 if (pHdl)
125 pHdl->ForgetLastPattern();
128 void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const OUString& rName,
129 SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
131 ScDocument* pDoc = pDocSh->GetDocument();
132 ScStyleSheetPool* pStlPool = pDoc->GetStyleSheetPool();
133 OUString aNewName = rData.GetName();
134 sal_Bool bDelete = aNewName.isEmpty(); // no new name -> delete style
135 sal_Bool bNew = ( rName.isEmpty() && !bDelete ); // creating new style
137 SfxStyleSheetBase* pStyle = NULL;
138 if ( !rName.isEmpty() )
140 // find old style to modify
141 pStyle = pStlPool->Find( rName, eStyleFamily );
142 OSL_ENSURE( pStyle, "style not found" );
144 if ( pStyle && !bDelete )
146 // set new name
147 pStyle->SetName( aNewName );
150 else if ( !bDelete )
152 // create style (with new name)
153 pStyle = &pStlPool->Make( aNewName, eStyleFamily, SFXSTYLEBIT_USERDEF );
155 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
156 pDoc->GetPool()->CellStyleCreated( aNewName );
159 if ( pStyle )
161 if ( bDelete )
163 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
164 lcl_DocStyleChanged( pDoc, pStyle, sal_True ); // TRUE: remove usage of style
165 else
166 pDoc->RemovePageStyleInUse( rName );
168 // delete style
169 pStlPool->Remove( pStyle );
171 else
173 // modify style
175 OUString aNewParent = rData.GetParent();
176 if ( aNewParent != pStyle->GetParent() )
177 pStyle->SetParent( aNewParent );
179 SfxItemSet& rStyleSet = pStyle->GetItemSet();
180 const SfxItemSet* pNewSet = rData.GetItems();
181 OSL_ENSURE( pNewSet, "no ItemSet for style" );
182 if (pNewSet)
183 rStyleSet.Set( *pNewSet, false );
185 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
187 lcl_DocStyleChanged( pDoc, pStyle, false ); // cell styles: row heights
189 else
191 // page styles
193 if ( bNew && aNewName != rName )
194 pDoc->RenamePageStyleInUse( rName, aNewName );
196 if (pNewSet)
197 pDoc->ModifyStyleSheet( *pStyle, *pNewSet );
199 pDocSh->PageStyleModified( aNewName, sal_True );
204 pDocSh->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID|PAINT_LEFT );
206 //! undo/redo document modifications for deleted styles
207 //! undo/redo modifications of number formatter
210 void ScUndoModifyStyle::Undo()
212 BeginUndo();
213 DoChange( pDocShell, aNewData.GetName(), eFamily, aOldData );
214 EndUndo();
217 void ScUndoModifyStyle::Redo()
219 BeginRedo();
220 DoChange( pDocShell, aOldData.GetName(), eFamily, aNewData );
221 EndRedo();
224 void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
228 bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
230 return false; // no repeat possible
233 // -----------------------------------------------------------------------
235 // apply page style
237 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab, const OUString& rOldStyle ) :
238 mnTab( nTab ),
239 maOldStyle( rOldStyle )
243 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell* pDocSh, const OUString& rNewStyle ) :
244 ScSimpleUndo( pDocSh ),
245 maNewStyle( rNewStyle )
249 ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
253 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
255 maEntries.push_back( ApplyStyleEntry( nTab, rOldStyle ) );
258 OUString ScUndoApplyPageStyle::GetComment() const
260 return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE );
263 void ScUndoApplyPageStyle::Undo()
265 BeginUndo();
266 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
268 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, aIt->maOldStyle );
269 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
271 EndUndo();
274 void ScUndoApplyPageStyle::Redo()
276 BeginRedo();
277 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
279 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, maNewStyle );
280 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
282 EndRedo();
285 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
287 //! set same page style to current tab
290 bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
292 return false;
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */