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 // -----------------------------------------------------------------------
34 TYPEINIT1(ScUndoModifyStyle
, ScSimpleUndo
);
35 TYPEINIT1(ScUndoApplyPageStyle
, ScSimpleUndo
);
37 // -----------------------------------------------------------------------
39 // modify style (cell or page style)
42 ScStyleSaveData::ScStyleSaveData() :
47 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData
& rOther
) :
48 aName( rOther
.aName
),
49 aParent( rOther
.aParent
)
52 pItems
= new SfxItemSet( *rOther
.pItems
);
57 ScStyleSaveData::~ScStyleSaveData()
62 ScStyleSaveData
& ScStyleSaveData::operator=( const ScStyleSaveData
& rOther
)
65 aParent
= rOther
.aParent
;
69 pItems
= new SfxItemSet( *rOther
.pItems
);
76 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase
* pSource
)
80 aName
= pSource
->GetName();
81 aParent
= pSource
->GetParent();
83 pItems
= new SfxItemSet( ((SfxStyleSheetBase
*)pSource
)->GetItemSet() );
86 *this = ScStyleSaveData(); // empty
89 // -----------------------------------------------------------------------
91 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell
* pDocSh
, SfxStyleFamily eFam
,
92 const ScStyleSaveData
& rOld
, const ScStyleSaveData
& rNew
) :
93 ScSimpleUndo( pDocSh
),
100 ScUndoModifyStyle::~ScUndoModifyStyle()
104 OUString
ScUndoModifyStyle::GetComment() const
106 sal_uInt16 nId
= (eFamily
== SFX_STYLE_FAMILY_PARA
) ?
107 STR_UNDO_EDITCELLSTYLE
:
108 STR_UNDO_EDITPAGESTYLE
;
109 return ScGlobal::GetRscString( nId
);
112 static void lcl_DocStyleChanged( ScDocument
* pDoc
, SfxStyleSheetBase
* pStyle
, sal_Bool bRemoved
)
114 //! move to document or docshell
117 Point aLogic
= aVDev
.LogicToPixel( Point(1000,1000), MAP_TWIP
);
118 double nPPTX
= aLogic
.X() / 1000.0;
119 double nPPTY
= aLogic
.Y() / 1000.0;
121 pDoc
->StyleSheetChanged( pStyle
, bRemoved
, &aVDev
, nPPTX
, nPPTY
, aZoom
, aZoom
);
123 ScInputHandler
* pHdl
= SC_MOD()->GetInputHdl();
125 pHdl
->ForgetLastPattern();
128 void ScUndoModifyStyle::DoChange( ScDocShell
* pDocSh
, const OUString
& rName
,
129 SfxStyleFamily eStyleFamily
, const ScStyleSaveData
& rData
)
131 ScDocument
* pDoc
= pDocSh
->GetDocument();
132 ScStyleSheetPool
* pStlPool
= pDoc
->GetStyleSheetPool();
133 OUString aNewName
= rData
.GetName();
134 sal_Bool bDelete
= aNewName
.isEmpty(); // no new name -> delete style
135 sal_Bool bNew
= ( rName
.isEmpty() && !bDelete
); // creating new style
137 SfxStyleSheetBase
* pStyle
= NULL
;
138 if ( !rName
.isEmpty() )
140 // find old style to modify
141 pStyle
= pStlPool
->Find( rName
, eStyleFamily
);
142 OSL_ENSURE( pStyle
, "style not found" );
144 if ( pStyle
&& !bDelete
)
147 pStyle
->SetName( aNewName
);
152 // create style (with new name)
153 pStyle
= &pStlPool
->Make( aNewName
, eStyleFamily
, SFXSTYLEBIT_USERDEF
);
155 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
156 pDoc
->GetPool()->CellStyleCreated( aNewName
);
163 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
164 lcl_DocStyleChanged( pDoc
, pStyle
, sal_True
); // TRUE: remove usage of style
166 pDoc
->RemovePageStyleInUse( rName
);
169 pStlPool
->Remove( pStyle
);
175 OUString aNewParent
= rData
.GetParent();
176 if ( aNewParent
!= pStyle
->GetParent() )
177 pStyle
->SetParent( aNewParent
);
179 SfxItemSet
& rStyleSet
= pStyle
->GetItemSet();
180 const SfxItemSet
* pNewSet
= rData
.GetItems();
181 OSL_ENSURE( pNewSet
, "no ItemSet for style" );
183 rStyleSet
.Set( *pNewSet
, false );
185 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
187 lcl_DocStyleChanged( pDoc
, pStyle
, false ); // cell styles: row heights
193 if ( bNew
&& aNewName
!= rName
)
194 pDoc
->RenamePageStyleInUse( rName
, aNewName
);
197 pDoc
->ModifyStyleSheet( *pStyle
, *pNewSet
);
199 pDocSh
->PageStyleModified( aNewName
, sal_True
);
204 pDocSh
->PostPaint( 0,0,0, MAXCOL
,MAXROW
,MAXTAB
, PAINT_GRID
|PAINT_LEFT
);
206 //! undo/redo document modifications for deleted styles
207 //! undo/redo modifications of number formatter
210 void ScUndoModifyStyle::Undo()
213 DoChange( pDocShell
, aNewData
.GetName(), eFamily
, aOldData
);
217 void ScUndoModifyStyle::Redo()
220 DoChange( pDocShell
, aOldData
.GetName(), eFamily
, aNewData
);
224 void ScUndoModifyStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
228 bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
230 return false; // no repeat possible
233 // -----------------------------------------------------------------------
237 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab
, const OUString
& rOldStyle
) :
239 maOldStyle( rOldStyle
)
243 ScUndoApplyPageStyle::ScUndoApplyPageStyle( ScDocShell
* pDocSh
, const OUString
& rNewStyle
) :
244 ScSimpleUndo( pDocSh
),
245 maNewStyle( rNewStyle
)
249 ScUndoApplyPageStyle::~ScUndoApplyPageStyle()
253 void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab
, const OUString
& rOldStyle
)
255 maEntries
.push_back( ApplyStyleEntry( nTab
, rOldStyle
) );
258 OUString
ScUndoApplyPageStyle::GetComment() const
260 return ScGlobal::GetRscString( STR_UNDO_APPLYPAGESTYLE
);
263 void ScUndoApplyPageStyle::Undo()
266 for( ApplyStyleVec::const_iterator aIt
= maEntries
.begin(), aEnd
= maEntries
.end(); aIt
!= aEnd
; ++aIt
)
268 pDocShell
->GetDocument()->SetPageStyle( aIt
->mnTab
, aIt
->maOldStyle
);
269 ScPrintFunc( pDocShell
, pDocShell
->GetPrinter(), aIt
->mnTab
).UpdatePages();
274 void ScUndoApplyPageStyle::Redo()
277 for( ApplyStyleVec::const_iterator aIt
= maEntries
.begin(), aEnd
= maEntries
.end(); aIt
!= aEnd
; ++aIt
)
279 pDocShell
->GetDocument()->SetPageStyle( aIt
->mnTab
, maNewStyle
);
280 ScPrintFunc( pDocShell
, pDocShell
->GetPrinter(), aIt
->mnTab
).UpdatePages();
285 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
287 //! set same page style to current tab
290 bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */