1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
25 #include "docpool.hxx"
26 #include "stlpool.hxx"
27 #include "printfun.hxx"
29 #include "inputhdl.hxx"
30 #include "globstr.hrc"
32 TYPEINIT1(ScUndoModifyStyle
, ScSimpleUndo
);
33 TYPEINIT1(ScUndoApplyPageStyle
, ScSimpleUndo
);
35 // modify style (cell or page style)
37 ScStyleSaveData::ScStyleSaveData() :
42 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData
& rOther
) :
43 aName( rOther
.aName
),
44 aParent( rOther
.aParent
)
47 pItems
= new SfxItemSet( *rOther
.pItems
);
52 ScStyleSaveData::~ScStyleSaveData()
57 ScStyleSaveData
& ScStyleSaveData::operator=( const ScStyleSaveData
& rOther
)
60 aParent
= rOther
.aParent
;
64 pItems
= new SfxItemSet( *rOther
.pItems
);
71 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase
* pSource
)
75 aName
= pSource
->GetName();
76 aParent
= pSource
->GetParent();
78 pItems
= new SfxItemSet( const_cast<SfxStyleSheetBase
*>(pSource
)->GetItemSet() );
81 *this = ScStyleSaveData(); // empty
84 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell
* pDocSh
, SfxStyleFamily eFam
,
85 const ScStyleSaveData
& rOld
, const ScStyleSaveData
& rNew
) :
86 ScSimpleUndo( pDocSh
),
93 ScUndoModifyStyle::~ScUndoModifyStyle()
97 OUString
ScUndoModifyStyle::GetComment() const
99 sal_uInt16 nId
= (eFamily
== SFX_STYLE_FAMILY_PARA
) ?
100 STR_UNDO_EDITCELLSTYLE
:
101 STR_UNDO_EDITPAGESTYLE
;
102 return ScGlobal::GetRscString( nId
);
105 static void lcl_DocStyleChanged( ScDocument
* pDoc
, SfxStyleSheetBase
* pStyle
, bool bRemoved
)
107 //! move to document or docshell
109 ScopedVclPtrInstance
< VirtualDevice
> pVDev
;
110 Point aLogic
= pVDev
->LogicToPixel( Point(1000,1000), MAP_TWIP
);
111 double nPPTX
= aLogic
.X() / 1000.0;
112 double nPPTY
= aLogic
.Y() / 1000.0;
114 pDoc
->StyleSheetChanged( pStyle
, bRemoved
, pVDev
, nPPTX
, nPPTY
, aZoom
, aZoom
);
116 ScInputHandler
* pHdl
= SC_MOD()->GetInputHdl();
118 pHdl
->ForgetLastPattern();
121 void ScUndoModifyStyle::DoChange( ScDocShell
* pDocSh
, const OUString
& rName
,
122 SfxStyleFamily eStyleFamily
, const ScStyleSaveData
& rData
)
124 ScDocument
& rDoc
= pDocSh
->GetDocument();
125 ScStyleSheetPool
* pStlPool
= rDoc
.GetStyleSheetPool();
126 OUString aNewName
= rData
.GetName();
127 bool bDelete
= aNewName
.isEmpty(); // no new name -> delete style
128 bool bNew
= ( rName
.isEmpty() && !bDelete
); // creating new style
130 SfxStyleSheetBase
* pStyle
= NULL
;
131 if ( !rName
.isEmpty() )
133 // find old style to modify
134 pStyle
= pStlPool
->Find( rName
, eStyleFamily
);
135 OSL_ENSURE( pStyle
, "style not found" );
137 if ( pStyle
&& !bDelete
)
140 pStyle
->SetName( aNewName
);
145 // create style (with new name)
146 pStyle
= &pStlPool
->Make( aNewName
, eStyleFamily
, SFXSTYLEBIT_USERDEF
);
148 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
149 rDoc
.GetPool()->CellStyleCreated( aNewName
, &rDoc
);
156 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
157 lcl_DocStyleChanged( &rDoc
, pStyle
, true ); // TRUE: remove usage of style
159 rDoc
.RemovePageStyleInUse( rName
);
162 pStlPool
->Remove( pStyle
);
168 OUString aNewParent
= rData
.GetParent();
169 if ( aNewParent
!= pStyle
->GetParent() )
170 pStyle
->SetParent( aNewParent
);
172 SfxItemSet
& rStyleSet
= pStyle
->GetItemSet();
173 const SfxItemSet
* pNewSet
= rData
.GetItems();
174 OSL_ENSURE( pNewSet
, "no ItemSet for style" );
176 rStyleSet
.Set( *pNewSet
, false );
178 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
180 lcl_DocStyleChanged( &rDoc
, pStyle
, false ); // cell styles: row heights
186 if ( bNew
&& aNewName
!= rName
)
187 rDoc
.RenamePageStyleInUse( rName
, aNewName
);
190 rDoc
.ModifyStyleSheet( *pStyle
, *pNewSet
);
192 pDocSh
->PageStyleModified( aNewName
, true );
197 pDocSh
->PostPaint( 0,0,0, MAXCOL
,MAXROW
,MAXTAB
, PAINT_GRID
|PAINT_LEFT
);
199 //! undo/redo document modifications for deleted styles
200 //! undo/redo modifications of number formatter
203 void ScUndoModifyStyle::Undo()
206 DoChange( pDocShell
, aNewData
.GetName(), eFamily
, aOldData
);
210 void ScUndoModifyStyle::Redo()
213 DoChange( pDocShell
, aOldData
.GetName(), eFamily
, aNewData
);
217 void ScUndoModifyStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
221 bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
223 return false; // no repeat possible
228 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab
, const OUString
& rOldStyle
) :
230 maOldStyle( rOldStyle
)
234 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell
* pDocSh
, const OUString
& rNewStyle
) :
235 ScSimpleUndo( pDocSh
),
236 maNewStyle( rNewStyle
)
240 ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
244 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab
, const OUString
& rOldStyle
)
246 maEntries
.push_back( ApplyStyleEntry( nTab
, rOldStyle
) );
249 OUString
ScUndoApplyPageStyle::GetComment() const
251 return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE
);
254 void ScUndoApplyPageStyle::Undo()
257 for( ApplyStyleVec::const_iterator aIt
= maEntries
.begin(), aEnd
= maEntries
.end(); aIt
!= aEnd
; ++aIt
)
259 pDocShell
->GetDocument().SetPageStyle( aIt
->mnTab
, aIt
->maOldStyle
);
260 ScPrintFunc( pDocShell
, pDocShell
->GetPrinter(), aIt
->mnTab
).UpdatePages();
265 void ScUndoApplyPageStyle::Redo()
268 for( ApplyStyleVec::const_iterator aIt
= maEntries
.begin(), aEnd
= maEntries
.end(); aIt
!= aEnd
; ++aIt
)
270 pDocShell
->GetDocument().SetPageStyle( aIt
->mnTab
, maNewStyle
);
271 ScPrintFunc( pDocShell
, pDocShell
->GetPrinter(), aIt
->mnTab
).UpdatePages();
276 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
278 //! set same page style to current tab
281 bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */