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