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