1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: undostyl.cxx,v $
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"
43 #include "stlpool.hxx"
44 #include "printfun.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() :
64 ScStyleSaveData::ScStyleSaveData( const ScStyleSaveData
& rOther
) :
65 aName( rOther
.aName
),
66 aParent( rOther
.aParent
)
69 pItems
= new SfxItemSet( *rOther
.pItems
);
74 ScStyleSaveData::~ScStyleSaveData()
79 ScStyleSaveData
& ScStyleSaveData::operator=( const ScStyleSaveData
& rOther
)
82 aParent
= rOther
.aParent
;
86 pItems
= new SfxItemSet( *rOther
.pItems
);
93 void ScStyleSaveData::InitFromStyle( const SfxStyleSheetBase
* pSource
)
97 aName
= pSource
->GetName();
98 aParent
= pSource
->GetParent();
100 pItems
= new SfxItemSet( ((SfxStyleSheetBase
*)pSource
)->GetItemSet() );
103 *this = ScStyleSaveData(); // empty
106 // -----------------------------------------------------------------------
108 ScUndoModifyStyle::ScUndoModifyStyle( ScDocShell
* pDocSh
, SfxStyleFamily eFam
,
109 const ScStyleSaveData
& rOld
, const ScStyleSaveData
& rNew
) :
110 ScSimpleUndo( pDocSh
),
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
134 Point aLogic
= aVDev
.LogicToPixel( Point(1000,1000), MAP_TWIP
);
135 double nPPTX
= aLogic
.X() / 1000.0;
136 double nPPTY
= aLogic
.Y() / 1000.0;
138 pDoc
->StyleSheetChanged( pStyle
, bRemoved
, &aVDev
, nPPTX
, nPPTY
, aZoom
, aZoom
);
140 ScInputHandler
* pHdl
= SC_MOD()->GetInputHdl();
142 pHdl
->ForgetLastPattern();
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
;
158 // find old style to modify
159 pStyle
= pStlPool
->Find( rName
, eStyleFamily
);
160 DBG_ASSERT( pStyle
, "style not found" );
162 if ( pStyle
&& !bDelete
)
165 pStyle
->SetName( aNewName
);
170 // create style (with new name)
171 pStyle
= &pStlPool
->Make( aNewName
, eStyleFamily
, SFXSTYLEBIT_USERDEF
);
178 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
179 lcl_DocStyleChanged( pDoc
, pStyle
, TRUE
); // TRUE: remove usage of style
181 pDoc
->RemovePageStyleInUse( rName
);
184 pStlPool
->Remove( pStyle
);
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" );
198 rStyleSet
.Set( *pNewSet
, FALSE
);
200 if ( eStyleFamily
== SFX_STYLE_FAMILY_PARA
)
202 lcl_DocStyleChanged( pDoc
, pStyle
, FALSE
); // cell styles: row heights
208 if ( bNew
&& aNewName
!= rName
)
209 pDoc
->RenamePageStyleInUse( rName
, aNewName
);
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()
228 DoChange( pDocShell
, aNewData
.GetName(), eFamily
, aOldData
);
232 void ScUndoModifyStyle::Redo()
235 DoChange( pDocShell
, aOldData
.GetName(), eFamily
, aNewData
);
239 void ScUndoModifyStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
243 BOOL
ScUndoModifyStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const
245 return FALSE
; // no repeat possible
248 // -----------------------------------------------------------------------
252 ScUndoApplyPageStyle::ApplyStyleEntry::ApplyStyleEntry( SCTAB nTab
, const String
& rOldStyle
) :
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()
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();
289 void ScUndoApplyPageStyle::Redo()
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();
300 void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget
& /* rTarget */)
302 //! set same page style to current tab
305 BOOL
ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget
& /* rTarget */) const