1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10*b3f79822SAndrew Rist *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19*b3f79822SAndrew Rist *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir
29cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <svl/itemset.hxx>
32cdf0e10cSrcweir #include <vcl/virdev.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include "undostyl.hxx"
35cdf0e10cSrcweir #include "docsh.hxx"
36cdf0e10cSrcweir #include "docpool.hxx"
37cdf0e10cSrcweir #include "stlpool.hxx"
38cdf0e10cSrcweir #include "printfun.hxx"
39cdf0e10cSrcweir #include "scmod.hxx"
40cdf0e10cSrcweir #include "inputhdl.hxx"
41cdf0e10cSrcweir #include "globstr.hrc"
42cdf0e10cSrcweir
43cdf0e10cSrcweir // -----------------------------------------------------------------------
44cdf0e10cSrcweir
45cdf0e10cSrcweir TYPEINIT1(ScUndoModifyStyle, ScSimpleUndo);
46cdf0e10cSrcweir TYPEINIT1(ScUndoApplyPageStyle, ScSimpleUndo);
47cdf0e10cSrcweir
48cdf0e10cSrcweir // -----------------------------------------------------------------------
49cdf0e10cSrcweir //
50cdf0e10cSrcweir // modify style (cell or page style)
51cdf0e10cSrcweir //
52cdf0e10cSrcweir
ScStyleSaveData()53cdf0e10cSrcweir ScStyleSaveData::ScStyleSaveData() :
54cdf0e10cSrcweir pItems( NULL )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
ScStyleSaveData(const ScStyleSaveData & rOther)58cdf0e10cSrcweir ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData& rOther ) :
59cdf0e10cSrcweir aName( rOther.aName ),
60cdf0e10cSrcweir aParent( rOther.aParent )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir if (rOther.pItems)
63cdf0e10cSrcweir pItems = new SfxItemSet( *rOther.pItems );
64cdf0e10cSrcweir else
65cdf0e10cSrcweir pItems = NULL;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
~ScStyleSaveData()68cdf0e10cSrcweir ScStyleSaveData::~ScStyleSaveData()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir delete pItems;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir
operator =(const ScStyleSaveData & rOther)73cdf0e10cSrcweir ScStyleSaveData& ScStyleSaveData::operator=( const ScStyleSaveData& rOther )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir aName = rOther.aName;
76cdf0e10cSrcweir aParent = rOther.aParent;
77cdf0e10cSrcweir
78cdf0e10cSrcweir delete pItems;
79cdf0e10cSrcweir if (rOther.pItems)
80cdf0e10cSrcweir pItems = new SfxItemSet( *rOther.pItems );
81cdf0e10cSrcweir else
82cdf0e10cSrcweir pItems = NULL;
83cdf0e10cSrcweir
84cdf0e10cSrcweir return *this;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
InitFromStyle(const SfxStyleSheetBase * pSource)87cdf0e10cSrcweir void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase* pSource )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir if ( pSource )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir aName = pSource->GetName();
92cdf0e10cSrcweir aParent = pSource->GetParent();
93cdf0e10cSrcweir delete pItems;
94cdf0e10cSrcweir pItems = new SfxItemSet( ((SfxStyleSheetBase*)pSource)->GetItemSet() );
95cdf0e10cSrcweir }
96cdf0e10cSrcweir else
97cdf0e10cSrcweir *this = ScStyleSaveData(); // empty
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
100cdf0e10cSrcweir // -----------------------------------------------------------------------
101cdf0e10cSrcweir
ScUndoModifyStyle(ScDocShell * pDocSh,SfxStyleFamily eFam,const ScStyleSaveData & rOld,const ScStyleSaveData & rNew)102cdf0e10cSrcweir ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell* pDocSh, SfxStyleFamily eFam,
103cdf0e10cSrcweir const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
104cdf0e10cSrcweir ScSimpleUndo( pDocSh ),
105cdf0e10cSrcweir eFamily( eFam ),
106cdf0e10cSrcweir aOldData( rOld ),
107cdf0e10cSrcweir aNewData( rNew )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
~ScUndoModifyStyle()111cdf0e10cSrcweir ScUndoModifyStyle::~ScUndoModifyStyle()
112cdf0e10cSrcweir {
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
GetComment() const115cdf0e10cSrcweir String ScUndoModifyStyle::GetComment() const
116cdf0e10cSrcweir {
117cdf0e10cSrcweir sal_uInt16 nId = (eFamily == SFX_STYLE_FAMILY_PARA) ?
118cdf0e10cSrcweir STR_UNDO_EDITCELLSTYLE :
119cdf0e10cSrcweir STR_UNDO_EDITPAGESTYLE;
120cdf0e10cSrcweir return ScGlobal::GetRscString( nId );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir
lcl_DocStyleChanged(ScDocument * pDoc,SfxStyleSheetBase * pStyle,sal_Bool bRemoved)123cdf0e10cSrcweir void lcl_DocStyleChanged( ScDocument* pDoc, SfxStyleSheetBase* pStyle, sal_Bool bRemoved )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir //! move to document or docshell
126cdf0e10cSrcweir
127cdf0e10cSrcweir VirtualDevice aVDev;
128cdf0e10cSrcweir Point aLogic = aVDev.LogicToPixel( Point(1000,1000), MAP_TWIP );
129cdf0e10cSrcweir double nPPTX = aLogic.X() / 1000.0;
130cdf0e10cSrcweir double nPPTY = aLogic.Y() / 1000.0;
131cdf0e10cSrcweir Fraction aZoom(1,1);
132cdf0e10cSrcweir pDoc->StyleSheetChanged( pStyle, bRemoved, &aVDev, nPPTX, nPPTY, aZoom, aZoom );
133cdf0e10cSrcweir
134cdf0e10cSrcweir ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
135cdf0e10cSrcweir if (pHdl)
136cdf0e10cSrcweir pHdl->ForgetLastPattern();
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
139cdf0e10cSrcweir // static
DoChange(ScDocShell * pDocSh,const String & rName,SfxStyleFamily eStyleFamily,const ScStyleSaveData & rData)140cdf0e10cSrcweir void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const String& rName,
141cdf0e10cSrcweir SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir ScDocument* pDoc = pDocSh->GetDocument();
144cdf0e10cSrcweir ScStyleSheetPool* pStlPool = pDoc->GetStyleSheetPool();
145cdf0e10cSrcweir String aNewName = rData.GetName();
146cdf0e10cSrcweir sal_Bool bDelete = ( aNewName.Len() == 0 ); // no new name -> delete style
147cdf0e10cSrcweir sal_Bool bNew = ( rName.Len() == 0 && !bDelete ); // creating new style
148cdf0e10cSrcweir
149cdf0e10cSrcweir SfxStyleSheetBase* pStyle = NULL;
150cdf0e10cSrcweir if ( rName.Len() )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir // find old style to modify
153cdf0e10cSrcweir pStyle = pStlPool->Find( rName, eStyleFamily );
154cdf0e10cSrcweir DBG_ASSERT( pStyle, "style not found" );
155cdf0e10cSrcweir
156cdf0e10cSrcweir if ( pStyle && !bDelete )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir // set new name
159cdf0e10cSrcweir pStyle->SetName( aNewName );
160cdf0e10cSrcweir }
161cdf0e10cSrcweir }
162cdf0e10cSrcweir else if ( !bDelete )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir // create style (with new name)
165cdf0e10cSrcweir pStyle = &pStlPool->Make( aNewName, eStyleFamily, SFXSTYLEBIT_USERDEF );
166cdf0e10cSrcweir
167cdf0e10cSrcweir if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
168cdf0e10cSrcweir pDoc->GetPool()->CellStyleCreated( aNewName );
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir if ( pStyle )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir if ( bDelete )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
176cdf0e10cSrcweir lcl_DocStyleChanged( pDoc, pStyle, sal_True ); // TRUE: remove usage of style
177cdf0e10cSrcweir else
178cdf0e10cSrcweir pDoc->RemovePageStyleInUse( rName );
179cdf0e10cSrcweir
180cdf0e10cSrcweir // delete style
181cdf0e10cSrcweir pStlPool->Remove( pStyle );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir else
184cdf0e10cSrcweir {
185cdf0e10cSrcweir // modify style
186cdf0e10cSrcweir
187cdf0e10cSrcweir String aNewParent = rData.GetParent();
188cdf0e10cSrcweir if ( aNewParent != pStyle->GetParent() )
189cdf0e10cSrcweir pStyle->SetParent( aNewParent );
190cdf0e10cSrcweir
191cdf0e10cSrcweir SfxItemSet& rStyleSet = pStyle->GetItemSet();
192cdf0e10cSrcweir const SfxItemSet* pNewSet = rData.GetItems();
193cdf0e10cSrcweir DBG_ASSERT( pNewSet, "no ItemSet for style" );
194cdf0e10cSrcweir if (pNewSet)
195cdf0e10cSrcweir rStyleSet.Set( *pNewSet, sal_False );
196cdf0e10cSrcweir
197cdf0e10cSrcweir if ( eStyleFamily == SFX_STYLE_FAMILY_PARA )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir lcl_DocStyleChanged( pDoc, pStyle, sal_False ); // cell styles: row heights
200cdf0e10cSrcweir }
201cdf0e10cSrcweir else
202cdf0e10cSrcweir {
203cdf0e10cSrcweir // page styles
204cdf0e10cSrcweir
205cdf0e10cSrcweir if ( bNew && aNewName != rName )
206cdf0e10cSrcweir pDoc->RenamePageStyleInUse( rName, aNewName );
207cdf0e10cSrcweir
208cdf0e10cSrcweir if (pNewSet)
209cdf0e10cSrcweir pDoc->ModifyStyleSheet( *pStyle, *pNewSet );
210cdf0e10cSrcweir
211cdf0e10cSrcweir pDocSh->PageStyleModified( aNewName, sal_True );
212cdf0e10cSrcweir }
213cdf0e10cSrcweir }
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir pDocSh->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID|PAINT_LEFT );
217cdf0e10cSrcweir
218cdf0e10cSrcweir //! undo/redo document modifications for deleted styles
219cdf0e10cSrcweir //! undo/redo modifications of number formatter
220cdf0e10cSrcweir }
221cdf0e10cSrcweir
Undo()222cdf0e10cSrcweir void ScUndoModifyStyle::Undo()
223cdf0e10cSrcweir {
224cdf0e10cSrcweir BeginUndo();
225cdf0e10cSrcweir DoChange( pDocShell, aNewData.GetName(), eFamily, aOldData );
226cdf0e10cSrcweir EndUndo();
227cdf0e10cSrcweir }
228cdf0e10cSrcweir
Redo()229cdf0e10cSrcweir void ScUndoModifyStyle::Redo()
230cdf0e10cSrcweir {
231cdf0e10cSrcweir BeginRedo();
232cdf0e10cSrcweir DoChange( pDocShell, aOldData.GetName(), eFamily, aNewData );
233cdf0e10cSrcweir EndRedo();
234cdf0e10cSrcweir }
235cdf0e10cSrcweir
Repeat(SfxRepeatTarget &)236cdf0e10cSrcweir void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
237cdf0e10cSrcweir {
238cdf0e10cSrcweir }
239cdf0e10cSrcweir
CanRepeat(SfxRepeatTarget &) const240cdf0e10cSrcweir sal_Bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
241cdf0e10cSrcweir {
242cdf0e10cSrcweir return sal_False; // no repeat possible
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
245cdf0e10cSrcweir // -----------------------------------------------------------------------
246cdf0e10cSrcweir //
247cdf0e10cSrcweir // apply page style
248cdf0e10cSrcweir //
ApplyStyleEntry(SCTAB nTab,const String & rOldStyle)249cdf0e10cSrcweir ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab, const String& rOldStyle ) :
250cdf0e10cSrcweir mnTab( nTab ),
251cdf0e10cSrcweir maOldStyle( rOldStyle )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
ScUndoApplyPageStyle(ScDocShell * pDocSh,const String & rNewStyle)255cdf0e10cSrcweir ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell* pDocSh, const String& rNewStyle ) :
256cdf0e10cSrcweir ScSimpleUndo( pDocSh ),
257cdf0e10cSrcweir maNewStyle( rNewStyle )
258cdf0e10cSrcweir {
259cdf0e10cSrcweir }
260cdf0e10cSrcweir
~ScUndoApplyPageStyle()261cdf0e10cSrcweir ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
262cdf0e10cSrcweir {
263cdf0e10cSrcweir }
264cdf0e10cSrcweir
AddSheetAction(SCTAB nTab,const String & rOldStyle)265cdf0e10cSrcweir void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const String& rOldStyle )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir maEntries.push_back( ApplyStyleEntry( nTab, rOldStyle ) );
268cdf0e10cSrcweir }
269cdf0e10cSrcweir
GetComment() const270cdf0e10cSrcweir String ScUndoApplyPageStyle::GetComment() const
271cdf0e10cSrcweir {
272cdf0e10cSrcweir return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE );
273cdf0e10cSrcweir }
274cdf0e10cSrcweir
Undo()275cdf0e10cSrcweir void ScUndoApplyPageStyle::Undo()
276cdf0e10cSrcweir {
277cdf0e10cSrcweir BeginUndo();
278cdf0e10cSrcweir for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
279cdf0e10cSrcweir {
280cdf0e10cSrcweir pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, aIt->maOldStyle );
281cdf0e10cSrcweir ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
282cdf0e10cSrcweir }
283cdf0e10cSrcweir EndUndo();
284cdf0e10cSrcweir }
285cdf0e10cSrcweir
Redo()286cdf0e10cSrcweir void ScUndoApplyPageStyle::Redo()
287cdf0e10cSrcweir {
288cdf0e10cSrcweir BeginRedo();
289cdf0e10cSrcweir for( ApplyStyleVec::const_iterator aIt = maEntries.begin(), aEnd = maEntries.end(); aIt != aEnd; ++aIt )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir pDocShell->GetDocument()->SetPageStyle( aIt->mnTab, maNewStyle );
292cdf0e10cSrcweir ScPrintFunc( pDocShell, pDocShell->GetPrinter(), aIt->mnTab ).UpdatePages();
293cdf0e10cSrcweir }
294cdf0e10cSrcweir EndRedo();
295cdf0e10cSrcweir }
296cdf0e10cSrcweir
Repeat(SfxRepeatTarget &)297cdf0e10cSrcweir void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
298cdf0e10cSrcweir {
299cdf0e10cSrcweir //! set same page style to current tab
300cdf0e10cSrcweir }
301cdf0e10cSrcweir
CanRepeat(SfxRepeatTarget &) const302cdf0e10cSrcweir sal_Bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
303cdf0e10cSrcweir {
304cdf0e10cSrcweir return sal_False;
305cdf0e10cSrcweir }
306cdf0e10cSrcweir
307cdf0e10cSrcweir
308