Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sc / source / ui / undo / undostyl.cxx
blobb0e240fcd321a71df7bc68693a5b182636cea829
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 TYPEINIT1(ScUndoModifyStyle, ScSimpleUndo);
33 TYPEINIT1(ScUndoApplyPageStyle, ScSimpleUndo);
35 // modify style (cell or page style)
38 ScStyleSaveData::ScStyleSaveData() :
39 pItems( NULL )
43 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData& rOther ) :
44 aName( rOther.aName ),
45 aParent( rOther.aParent )
47 if (rOther.pItems)
48 pItems = new SfxItemSet( *rOther.pItems );
49 else
50 pItems = NULL;
53 ScStyleSaveData::~ScStyleSaveData()
55 delete pItems;
58 ScStyleSaveData& ScStyleSaveData::operator=( const ScStyleSaveData& rOther )
60 aName = rOther.aName;
61 aParent = rOther.aParent;
63 delete pItems;
64 if (rOther.pItems)
65 pItems = new SfxItemSet( *rOther.pItems );
66 else
67 pItems = NULL;
69 return *this;
72 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase* pSource )
74 if ( pSource )
76 aName = pSource->GetName();
77 aParent = pSource->GetParent();
78 delete pItems;
79 pItems = new SfxItemSet( ((SfxStyleSheetBase*)pSource)->GetItemSet() );
81 else
82 *this = ScStyleSaveData(); // empty
85 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell* pDocSh, SfxStyleFamily eFam,
86 const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
87 ScSimpleUndo( pDocSh ),
88 eFamily( eFam ),
89 aOldData( rOld ),
90 aNewData( rNew )
94 ScUndoModifyStyle::~ScUndoModifyStyle()
98 OUString ScUndoModifyStyle::GetComment() const
100 sal_uInt16 nId = (eFamily == SFX_STYLE_FAMILY_PARA) ?
101 STR_UNDO_EDITCELLSTYLE :
102 STR_UNDO_EDITPAGESTYLE;
103 return ScGlobal::GetRscString( nId );
106 static void lcl_DocStyleChanged( ScDocument* pDoc, SfxStyleSheetBase* pStyle, bool bRemoved )
108 //! move to document or docshell
110 VirtualDevice aVDev;
111 Point aLogic = aVDev.LogicToPixel( Point(1000,1000), MAP_TWIP );
112 double nPPTX = aLogic.X() / 1000.0;
113 double nPPTY = aLogic.Y() / 1000.0;
114 Fraction aZoom(1,1);
115 pDoc->StyleSheetChanged( pStyle, bRemoved, &aVDev, nPPTX, nPPTY, aZoom, aZoom );
117 ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
118 if (pHdl)
119 pHdl->ForgetLastPattern();
122 void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const OUString& rName,
123 SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
125 ScDocument* pDoc = pDocSh->GetDocument();
126 ScStyleSheetPool* pStlPool = pDoc->GetStyleSheetPool();
127 OUString aNewName = rData.GetName();
128 bool bDelete = aNewName.isEmpty(); // no new name -> delete style
129 bool bNew = ( rName.isEmpty() && !bDelete ); // creating new style
131 SfxStyleSheetBase* pStyle = NULL;
132 if ( !rName.isEmpty() )
134 // find old style to modify
135 pStyle = pStlPool->Find( rName, eStyleFamily );
136 OSL_ENSURE( pStyle, "style not found" );
138 if ( pStyle && !bDelete )
140 // set new name
141 pStyle->SetName( aNewName );
144 else if ( !bDelete )
146 // create style (with new name)
147 pStyle = &pStlPool->Make( aNewName, eStyleFamily, SFXSTYLEBIT_USERDEF );
149 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
150 pDoc->GetPool()->CellStyleCreated( aNewName );
153 if ( pStyle )
155 if ( bDelete )
157 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
158 lcl_DocStyleChanged( pDoc, pStyle, true ); // TRUE: remove usage of style
159 else
160 pDoc->RemovePageStyleInUse( rName );
162 // delete style
163 pStlPool->Remove( pStyle );
165 else
167 // modify style
169 OUString aNewParent = rData.GetParent();
170 if ( aNewParent != pStyle->GetParent() )
171 pStyle->SetParent( aNewParent );
173 SfxItemSet& rStyleSet = pStyle->GetItemSet();
174 const SfxItemSet* pNewSet = rData.GetItems();
175 OSL_ENSURE( pNewSet, "no ItemSet for style" );
176 if (pNewSet)
177 rStyleSet.Set( *pNewSet, false );
179 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
181 lcl_DocStyleChanged( pDoc, pStyle, false ); // cell styles: row heights
183 else
185 // page styles
187 if ( bNew && aNewName != rName )
188 pDoc->RenamePageStyleInUse( rName, aNewName );
190 if (pNewSet)
191 pDoc->ModifyStyleSheet( *pStyle, *pNewSet );
193 pDocSh->PageStyleModified( aNewName, true );
198 pDocSh->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID|PAINT_LEFT );
200 //! undo/redo document modifications for deleted styles
201 //! undo/redo modifications of number formatter
204 void ScUndoModifyStyle::Undo()
206 BeginUndo();
207 DoChange( pDocShell, aNewData.GetName(), eFamily, aOldData );
208 EndUndo();
211 void ScUndoModifyStyle::Redo()
213 BeginRedo();
214 DoChange( pDocShell, aOldData.GetName(), eFamily, aNewData );
215 EndRedo();
218 void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
222 bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
224 return false; // no repeat possible
227 // apply page style
229 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab, const OUString& rOldStyle ) :
230 mnTab( nTab ),
231 maOldStyle( rOldStyle )
235 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell* pDocSh, const OUString& rNewStyle ) :
236 ScSimpleUndo( pDocSh ),
237 maNewStyle( rNewStyle )
241 ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
245 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
247 maEntries.push_back( ApplyStyleEntry( nTab, rOldStyle ) );
250 OUString ScUndoApplyPageStyle::GetComment() const
252 return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE );
255 void ScUndoApplyPageStyle::Undo()
257 BeginUndo();
258 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
260 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, aIt->maOldStyle );
261 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
263 EndUndo();
266 void ScUndoApplyPageStyle::Redo()
268 BeginRedo();
269 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
271 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, maNewStyle );
272 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
274 EndRedo();
277 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
279 //! set same page style to current tab
282 bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
284 return false;
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */