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)
38 ScStyleSaveData::ScStyleSaveData() :
43 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData
& rOther
) :
44 aName( rOther
.aName
),
45 aParent( rOther
.aParent
)
48 pItems
= new SfxItemSet( *rOther
.pItems
);
53 ScStyleSaveData::~ScStyleSaveData()
58 ScStyleSaveData
& ScStyleSaveData::operator=( const ScStyleSaveData
& rOther
)
61 aParent
= rOther
.aParent
;
65 pItems
= new SfxItemSet( *rOther
.pItems
);
72 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase
* pSource
)
76 aName
= pSource
->GetName();
77 aParent
= pSource
->GetParent();
79 pItems
= new SfxItemSet( ((SfxStyleSheetBase
*)pSource
)->GetItemSet() );
82 *this = ScStyleSaveData(); // empty
85 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell
* pDocSh
, SfxStyleFamily eFam
,
86 const ScStyleSaveData
& rOld
, const ScStyleSaveData
& rNew
) :
87 ScSimpleUndo( pDocSh
),
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
111 Point aLogic
= aVDev
.LogicToPixel( Point(1000,1000), MAP_TWIP
);
112 double nPPTX
= aLogic
.X() / 1000.0;
113 double nPPTY
= aLogic
.Y() / 1000.0;
115 pDoc
->StyleSheetChanged( pStyle
, bRemoved
, &aVDev
, nPPTX
, nPPTY
, aZoom
, aZoom
);
117 ScInputHandler
* pHdl
= SC_MOD()->GetInputHdl();
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
)
141 pStyle
->SetName( aNewName
);
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
);
157 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
158 lcl_DocStyleChanged( pDoc
, pStyle
, true ); // TRUE: remove usage of style
160 pDoc
->RemovePageStyleInUse( rName
);
163 pStlPool
->Remove( pStyle
);
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" );
177 rStyleSet
.Set( *pNewSet
, false );
179 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
181 lcl_DocStyleChanged( pDoc
, pStyle
, false ); // cell styles: row heights
187 if ( bNew
&& aNewName
!= rName
)
188 pDoc
->RenamePageStyleInUse( rName
, aNewName
);
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()
207 DoChange( pDocShell
, aNewData
.GetName(), eFamily
, aOldData
);
211 void ScUndoModifyStyle::Redo()
214 DoChange( pDocShell
, aOldData
.GetName(), eFamily
, aNewData
);
218 void ScUndoModifyStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
222 bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
224 return false; // no repeat possible
229 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab
, const OUString
& rOldStyle
) :
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()
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();
266 void ScUndoApplyPageStyle::Redo()
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();
277 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
279 //! set same page style to current tab
282 bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */