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