merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / undo / undostyl.cxx
blobeea74d7fd27ff3f4e13c366807d78a925099dfba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: undostyl.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
38 #include <svtools/itemset.hxx>
39 #include <vcl/virdev.hxx>
41 #include "undostyl.hxx"
42 #include "docsh.hxx"
43 #include "stlpool.hxx"
44 #include "printfun.hxx"
45 #include "scmod.hxx"
46 #include "inputhdl.hxx"
47 #include "globstr.hrc"
49 // -----------------------------------------------------------------------
51 TYPEINIT1(ScUndoModifyStyle, ScSimpleUndo);
52 TYPEINIT1(ScUndoApplyPageStyle, ScSimpleUndo);
54 // -----------------------------------------------------------------------
56 // modify style (cell or page style)
59 ScStyleSaveData::ScStyleSaveData() :
60 pItems( NULL )
64 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData& rOther ) :
65 aName( rOther.aName ),
66 aParent( rOther.aParent )
68 if (rOther.pItems)
69 pItems = new SfxItemSet( *rOther.pItems );
70 else
71 pItems = NULL;
74 ScStyleSaveData::~ScStyleSaveData()
76 delete pItems;
79 ScStyleSaveData& ScStyleSaveData::operator=( const ScStyleSaveData& rOther )
81 aName = rOther.aName;
82 aParent = rOther.aParent;
84 delete pItems;
85 if (rOther.pItems)
86 pItems = new SfxItemSet( *rOther.pItems );
87 else
88 pItems = NULL;
90 return *this;
93 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase* pSource )
95 if ( pSource )
97 aName = pSource->GetName();
98 aParent = pSource->GetParent();
99 delete pItems;
100 pItems = new SfxItemSet( ((SfxStyleSheetBase*)pSource)->GetItemSet() );
102 else
103 *this = ScStyleSaveData(); // empty
106 // -----------------------------------------------------------------------
108 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell* pDocSh, SfxStyleFamily eFam,
109 const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
110 ScSimpleUndo( pDocSh ),
111 eFamily( eFam ),
112 aOldData( rOld ),
113 aNewData( rNew )
117 ScUndoModifyStyle::~ScUndoModifyStyle()
121 String ScUndoModifyStyle::GetComment() const
123 USHORT nId = (eFamily == SFX_STYLE_FAMILY_PARA) ?
124 STR_UNDO_EDITCELLSTYLE :
125 STR_UNDO_EDITPAGESTYLE;
126 return ScGlobal::GetRscString( nId );
129 void lcl_DocStyleChanged( ScDocument* pDoc, SfxStyleSheetBase* pStyle, BOOL bRemoved )
131 //! move to document or docshell
133 VirtualDevice aVDev;
134 Point aLogic = aVDev.LogicToPixel( Point(1000,1000), MAP_TWIP );
135 double nPPTX = aLogic.X() / 1000.0;
136 double nPPTY = aLogic.Y() / 1000.0;
137 Fraction aZoom(1,1);
138 pDoc->StyleSheetChanged( pStyle, bRemoved, &aVDev, nPPTX, nPPTY, aZoom, aZoom );
140 ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
141 if (pHdl)
142 pHdl->ForgetLastPattern();
145 // static
146 void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const String& rName,
147 SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
149 ScDocument* pDoc = pDocSh->GetDocument();
150 ScStyleSheetPool* pStlPool = pDoc->GetStyleSheetPool();
151 String aNewName = rData.GetName();
152 BOOL bDelete = ( aNewName.Len() == 0 ); // no new name -> delete style
153 BOOL bNew = ( rName.Len() == 0 && !bDelete ); // creating new style
155 SfxStyleSheetBase* pStyle = NULL;
156 if ( rName.Len() )
158 // find old style to modify
159 pStyle = pStlPool->Find( rName, eStyleFamily );
160 DBG_ASSERT( pStyle, "style not found" );
162 if ( pStyle && !bDelete )
164 // set new name
165 pStyle->SetName( aNewName );
168 else if ( !bDelete )
170 // create style (with new name)
171 pStyle = &pStlPool->Make( aNewName, eStyleFamily, SFXSTYLEBIT_USERDEF );
174 if ( pStyle )
176 if ( bDelete )
178 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
179 lcl_DocStyleChanged( pDoc, pStyle, TRUE ); // TRUE: remove usage of style
180 else
181 pDoc->RemovePageStyleInUse( rName );
183 // delete style
184 pStlPool->Remove( pStyle );
186 else
188 // modify style
190 String aNewParent = rData.GetParent();
191 if ( aNewParent != pStyle->GetParent() )
192 pStyle->SetParent( aNewParent );
194 SfxItemSet& rStyleSet = pStyle->GetItemSet();
195 const SfxItemSet* pNewSet = rData.GetItems();
196 DBG_ASSERT( pNewSet, "no ItemSet for style" );
197 if (pNewSet)
198 rStyleSet.Set( *pNewSet, FALSE );
200 if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
202 lcl_DocStyleChanged( pDoc, pStyle, FALSE ); // cell styles: row heights
204 else
206 // page styles
208 if ( bNew && aNewName != rName )
209 pDoc->RenamePageStyleInUse( rName, aNewName );
211 if (pNewSet)
212 pDoc->ModifyStyleSheet( *pStyle, *pNewSet );
214 pDocSh->PageStyleModified( aNewName, TRUE );
219 pDocSh->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID|PAINT_LEFT );
221 //! undo/redo document modifications for deleted styles
222 //! undo/redo modifications of number formatter
225 void ScUndoModifyStyle::Undo()
227 BeginUndo();
228 DoChange( pDocShell, aNewData.GetName(), eFamily, aOldData );
229 EndUndo();
232 void ScUndoModifyStyle::Redo()
234 BeginRedo();
235 DoChange( pDocShell, aOldData.GetName(), eFamily, aNewData );
236 EndRedo();
239 void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
243 BOOL ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
245 return FALSE; // no repeat possible
248 // -----------------------------------------------------------------------
250 // apply page style
252 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab, const String& rOldStyle ) :
253 mnTab( nTab ),
254 maOldStyle( rOldStyle )
258 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell* pDocSh, const String& rNewStyle ) :
259 ScSimpleUndo( pDocSh ),
260 maNewStyle( rNewStyle )
264 ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
268 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const String& rOldStyle )
270 maEntries.push_back( ApplyStyleEntry( nTab, rOldStyle ) );
273 String ScUndoApplyPageStyle::GetComment() const
275 return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE );
278 void ScUndoApplyPageStyle::Undo()
280 BeginUndo();
281 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
283 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, aIt->maOldStyle );
284 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
286 EndUndo();
289 void ScUndoApplyPageStyle::Redo()
291 BeginRedo();
292 for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
294 pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, maNewStyle );
295 ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
297 EndRedo();
300 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
302 //! set same page style to current tab
305 BOOL ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
307 return FALSE;